/* * Public interface for adding users */ int useradd(TALLOC_CTX *mem_ctx, struct sysdb_ctx *sysdb, struct ops_ctx *data) { int ret; ret = sysdb_add_user(sysdb, data->name, data->uid, data->gid, data->gecos, data->home, data->shell, NULL, 0); if (ret) { goto done; } if (data->addgroups) { struct ldb_dn *member_dn; member_dn = sysdb_user_dn(sysdb, mem_ctx, data->domain->name, data->name); if (!member_dn) { ret = ENOMEM; goto done; } ret = add_to_groups(mem_ctx, sysdb, data, member_dn); if (ret) { goto done; } } flush_nscd_cache(mem_ctx, NSCD_DB_PASSWD); flush_nscd_cache(mem_ctx, NSCD_DB_GROUP); done: return ret; }
/* * Public interface for modifying groups */ int groupmod(TALLOC_CTX *mem_ctx, struct ops_ctx *data) { struct sysdb_attrs *attrs = NULL; struct ldb_dn *member_dn = NULL; int ret; data->sysdb_fqname = sss_create_internal_fqname(data, data->name, data->domain->name); if (data->sysdb_fqname == NULL) { return ENOMEM; } if (data->addgroups || data->rmgroups) { member_dn = sysdb_group_dn(mem_ctx, data->domain, data->sysdb_fqname); if (!member_dn) { return ENOMEM; } } if (data->gid != 0) { attrs = sysdb_new_attrs(mem_ctx); if (!attrs) { return ENOMEM; } ret = sysdb_attrs_add_uint32(attrs, SYSDB_GIDNUM, data->gid); if (ret) { return ret; } ret = sysdb_set_group_attr(data->domain, data->sysdb_fqname, attrs, SYSDB_MOD_REP); if (ret) { return ret; } } if (data->rmgroups != NULL) { ret = remove_from_groups(data, member_dn); if (ret) { return ret; } } if (data->addgroups != NULL) { ret = add_to_groups(data, member_dn); if (ret) { return ret; } } flush_nscd_cache(NSCD_DB_GROUP); return EOK; }
/* * Public interface for modifying users */ int usermod(TALLOC_CTX *mem_ctx, struct sysdb_ctx *sysdb, struct ops_ctx *data) { struct sysdb_attrs *attrs = NULL; struct ldb_dn *member_dn = NULL; int ret; if (data->addgroups || data->rmgroups) { member_dn = sysdb_user_dn(sysdb, mem_ctx, data->domain->name, data->name); if (!member_dn) { return ENOMEM; } } ret = usermod_build_attrs(mem_ctx, data->gecos, data->home, data->shell, data->uid, data->gid, data->lock, &attrs); if (ret != EOK) { return ret; } if (attrs->num != 0) { ret = sysdb_set_user_attr(sysdb, data->name, attrs, SYSDB_MOD_REP); if (ret) { return ret; } } if (data->rmgroups != NULL) { ret = remove_from_groups(mem_ctx, sysdb, data, member_dn); if (ret) { return ret; } } if (data->addgroups != NULL) { ret = add_to_groups(mem_ctx, sysdb, data, member_dn); if (ret) { return ret; } } flush_nscd_cache(mem_ctx, NSCD_DB_PASSWD); flush_nscd_cache(mem_ctx, NSCD_DB_GROUP); return EOK; }
void add_to_groups(struct gp* groups, char *var, int first_n) { struct wd *new_wd = NULL; int cmp = 0; if (groups == NULL) { groups = (struct gp *)malloc(sizeof(struct gp)); groups->words = (struct wd *)malloc(sizeof(struct wd)); groups->words->word = var; groups->words->next = NULL; } else { cmp = strncmp(var, groups->words->word, first_n); if (cmp == 0) { new_wd = (struct wd *)malloc(sizeof(struct wd)); new_wd->word = var; new_wd->next = groups->words; groups->words = new_wd; } else if (cmp < 0){ add_to_groups(groups->left, var, first_n); } else { add_to_groups(groups->right, var, first_n); } } }
int main(int argc, char *argv[]) { char var[VARLEN]; int maxlen = VARLEN; int first_n = 0; struct gp* groups = NULL; /* get parameter */ if ((first_n = get_para(argc, argv)) < 0) { printf ("%s: input group flag n.\n", argv[0]); return -1; } while (readvar(var, maxlen) > 0) { add_to_groups(groups, var, first_n); } print_groups(groups); return 0; }
/* * Public interface for adding users */ int useradd(TALLOC_CTX *mem_ctx, struct ops_ctx *data) { int ret; data->sysdb_fqname = sss_create_internal_fqname(data, data->name, data->domain->name); if (data->sysdb_fqname == NULL) { ret = ENOMEM; goto done; } ret = sysdb_add_user(data->domain, data->sysdb_fqname, data->uid, data->gid, data->gecos, data->home, data->shell, NULL, NULL, 0, 0); if (ret) { goto done; } if (data->addgroups) { struct ldb_dn *member_dn; member_dn = sysdb_user_dn(mem_ctx, data->domain, data->sysdb_fqname); if (!member_dn) { ret = ENOMEM; goto done; } ret = add_to_groups(data, member_dn); if (ret) { goto done; } } flush_nscd_cache(NSCD_DB_PASSWD); flush_nscd_cache(NSCD_DB_GROUP); done: return ret; }