summaryrefslogtreecommitdiffstats
path: root/server/tools/sss_groupadd.c
diff options
context:
space:
mode:
authorJakub Hrozek <jhrozek@redhat.com>2009-08-28 18:20:44 +0200
committerStephen Gallagher <sgallagh@redhat.com>2009-09-21 10:35:07 -0400
commitdb329e0f8a35c23416acedaca3683392b0114c92 (patch)
treeb9b393efe9a7af5190edb5c71d80b9fb24e45e89 /server/tools/sss_groupadd.c
parent31e6cb0a887f8c19b8558aa5fe7363201848e815 (diff)
downloadsssd-db329e0f8a35c23416acedaca3683392b0114c92.tar.gz
sssd-db329e0f8a35c23416acedaca3683392b0114c92.tar.xz
sssd-db329e0f8a35c23416acedaca3683392b0114c92.zip
Refactor tools code
Move parameter parsing in tools before attempting to do anything that might fail - so that we have debug_level set correctly for potential error messages. That allows printing the --help and --usage messages without being root. Fix code duplicates in tools and refactor its code a little to lay ground for decoupling the synchronous interfaces. Remove some legacy tools leftovers, re-add sensible error message on removing nonexistent users/groups which was removed by accident. Fixes: Trac ticket #75 Fix typo in groupdel: fixes ticket #136
Diffstat (limited to 'server/tools/sss_groupadd.c')
-rw-r--r--server/tools/sss_groupadd.c44
1 files changed, 16 insertions, 28 deletions
diff --git a/server/tools/sss_groupadd.c b/server/tools/sss_groupadd.c
index d0c028391..830ba79a6 100644
--- a/server/tools/sss_groupadd.c
+++ b/server/tools/sss_groupadd.c
@@ -25,8 +25,6 @@
#include <popt.h>
#include <errno.h>
#include <unistd.h>
-#include <sys/types.h>
-#include <sys/wait.h>
#include "util/util.h"
#include "db/sysdb.h"
@@ -48,7 +46,7 @@ static void add_group_terminate(struct ops_ctx *data, int error)
goto fail;
}
- req = sysdb_transaction_commit_send(data, data->ev, data->handle);
+ req = sysdb_transaction_commit_send(data, data->ctx->ev, data->handle);
if (!req) {
error = ENOMEM;
goto fail;
@@ -78,7 +76,7 @@ static void add_group(struct tevent_req *req)
return add_group_terminate(data, ret);
}
- subreq = sysdb_add_group_send(data, data->ev, data->handle,
+ subreq = sysdb_add_group_send(data, data->ctx->ev, data->handle,
data->domain, data->name,
data->gid, NULL);
if (!subreq) {
@@ -111,7 +109,6 @@ int main(int argc, const char **argv)
POPT_TABLEEND
};
poptContext pc = NULL;
- struct tools_ctx *ctx = NULL;
struct tevent_req *req;
struct ops_ctx *data = NULL;
int ret = EXIT_SUCCESS;
@@ -126,29 +123,11 @@ int main(int argc, const char **argv)
ret = EXIT_FAILURE;
goto fini;
}
- CHECK_ROOT(ret, debug_prg_name);
-
- ret = init_sss_tools(&ctx);
- if(ret != EOK) {
- DEBUG(1, ("init_sss_tools failed (%d): %s\n", ret, strerror(ret)));
- ERROR("Error initializing the tools\n");
- ret = EXIT_FAILURE;
- goto fini;
- }
-
- data = talloc_zero(NULL, struct ops_ctx);
- if (data == NULL) {
- DEBUG(1, ("Could not allocate memory for data context\n"));
- ERROR("Out of memory.\n");
- return ENOMEM;
- }
- data->ctx = ctx;
- data->ev = ctx->ev;
/* parse params */
pc = poptGetContext(NULL, argc, argv, long_options, 0);
poptSetOtherOptionHelp(pc, "GROUPNAME");
- if((ret = poptGetNextOpt(pc)) < -1) {
+ if ((ret = poptGetNextOpt(pc)) < -1) {
usage(pc, poptStrerror(ret));
ret = EXIT_FAILURE;
goto fini;
@@ -164,6 +143,16 @@ int main(int argc, const char **argv)
goto fini;
}
+ CHECK_ROOT(ret, debug_prg_name);
+
+ ret = init_sss_tools(&data);
+ if (ret != EOK) {
+ DEBUG(1, ("init_sss_tools failed (%d): %s\n", ret, strerror(ret)));
+ ERROR("Error initializing the tools\n");
+ ret = EXIT_FAILURE;
+ goto fini;
+ }
+
ret = get_domain(data, pc_groupname);
if (ret != EOK) {
ERROR("Cannot get domain information\n");
@@ -181,7 +170,7 @@ int main(int argc, const char **argv)
}
/* add_group */
- req = sysdb_transaction_send(ctx, ctx->ev, data->ctx->sysdb);
+ req = sysdb_transaction_send(data, data->ctx->ev, data->ctx->sysdb);
if (!req) {
DEBUG(1, ("Could not start transaction (%d)[%s]\n", ret, strerror(ret)));
ERROR("Transaction error. Could not add group.\n");
@@ -191,14 +180,14 @@ int main(int argc, const char **argv)
tevent_req_set_callback(req, add_group, data);
while (!data->done) {
- tevent_loop_once(ctx->ev);
+ tevent_loop_once(data->ctx->ev);
}
if (data->error) {
ret = data->error;
switch (ret) {
case EEXIST:
- ERROR("A group with the same name or UID already exists\n");
+ ERROR("A group with the same name or GID already exists\n");
break;
default:
@@ -213,7 +202,6 @@ int main(int argc, const char **argv)
ret = EXIT_SUCCESS;
fini:
talloc_free(data);
- talloc_free(ctx);
poptFreeContext(pc);
exit(ret);
}