pyceo/src/addmember.c

119 lines
2.7 KiB
C
Raw Normal View History

#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <ctype.h>
#include <pwd.h>
#include <grp.h>
#include <errno.h>
#include <libgen.h>
#include <syslog.h>
#include "util.h"
#include "config.h"
#include "ldap.h"
#include "krb5.h"
#include "kadm.h"
2009-01-31 01:47:28 -05:00
#include "ceo.pb-c.h"
char *prog = NULL;
static int use_stdin = 0;
static char *name = NULL;
static char *userid = NULL;
static char *program = NULL;
static char password[1024];
static struct option opts[] = {
{ "stdin", 0, NULL, 's' },
{ NULL, 0, NULL, '\0' },
};
2009-01-31 01:47:28 -05:00
const char *default_lib_dir = "/usr/lib/ceod";
const char *lib_dir;
static void usage() {
fprintf(stderr, "Usage: %s userid realname [program]\n", prog);
exit(2);
}
2009-01-31 01:47:28 -05:00
int addmember(void) {
struct strbuf preq = STRBUF_INIT;
struct strbuf pret = STRBUF_INIT;
char cpath[1024];
char *cargv[] = { "ceoc", "adduser", NULL };
2009-01-31 01:47:28 -05:00
if (snprintf(cpath, sizeof(cpath), "%s/ceoc", lib_dir) >= sizeof(cpath))
fatal("path too long");
if (ceo_read_password(password, sizeof(password), use_stdin))
2007-12-12 03:09:31 -05:00
return 1;
2009-01-31 01:47:28 -05:00
Ceo__AddUser req;
ceo__add_user__init(&req);
2009-01-31 01:47:28 -05:00
req.username = userid;
req.password = password;
req.program = program;
req.realname = name;
req.type = CEO__ADD_USER__TYPE__MEMBER;
2009-01-31 01:47:28 -05:00
strbuf_grow(&preq, ceo__add_user__get_packed_size(&req));
strbuf_setlen(&preq, ceo__add_user__pack(&req, (uint8_t *)preq.buf));
2009-01-31 01:47:28 -05:00
if (spawnvem(cpath, cargv, environ, &preq, &pret, 0))
return 1;
2009-01-31 01:47:28 -05:00
Ceo__AddUserResponse *ret = ceo__add_user_response__unpack(&protobuf_c_default_allocator,
pret.len, (uint8_t *)pret.buf);
if (!ret)
fatal("failed to unpack response");
2009-01-31 01:47:28 -05:00
for (int i = 0; i < ret->n_messages; i++) {
if (ret->messages[i]->status)
error("%s", ret->messages[i]->message);
else
notice("%s", ret->messages[i]->message);
}
2009-01-31 01:47:28 -05:00
return 0;
}
int main(int argc, char *argv[]) {
int opt;
2008-03-15 01:52:16 -04:00
prog = basename(argv[0]);
2009-01-31 01:47:28 -05:00
init_log(prog, 0, LOG_AUTHPRIV);
configure();
while ((opt = getopt_long(argc, argv, "", opts, NULL)) != -1) {
switch (opt) {
case 's':
use_stdin = 1;
break;
case '?':
usage();
break;
default:
fatal("error parsing arguments");
}
}
if (argc - optind != 2 && argc - optind != 3)
usage();
userid = argv[optind++];
name = argv[optind++];
if (argc - optind)
program = argv[optind++];
2009-01-31 01:47:28 -05:00
lib_dir = getenv("CEO_LIB_DIR") ?: default_lib_dir;
2007-12-12 00:02:53 -05:00
return addmember();
}