Skip to content

Commit

Permalink
Refactor tools code
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jhrozek authored and sgallagher committed Sep 21, 2009
1 parent 31e6cb0 commit db329e0
Show file tree
Hide file tree
Showing 8 changed files with 244 additions and 268 deletions.
44 changes: 16 additions & 28 deletions server/tools/sss_groupadd.c
Expand Up @@ -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"
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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");
Expand All @@ -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");
Expand All @@ -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:
Expand All @@ -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);
}
Expand Down
56 changes: 26 additions & 30 deletions server/tools/sss_groupdel.c
Expand Up @@ -24,8 +24,6 @@
#include <talloc.h>
#include <popt.h>
#include <grp.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#include "db/sysdb.h"
Expand All @@ -50,7 +48,7 @@ static void groupdel_done(void *pvt, int error, struct ldb_result *ignore)
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;
Expand Down Expand Up @@ -88,7 +86,7 @@ static void group_del(struct tevent_req *req)
return groupdel_done(data, ENOMEM, NULL);
}

subreq = sysdb_delete_entry_send(data, data->ev, data->handle, group_dn, false);
subreq = sysdb_delete_entry_send(data, data->ctx->ev, data->handle, group_dn, false);
if (!subreq)
return groupdel_done(data, ENOMEM, NULL);

Expand All @@ -110,7 +108,6 @@ int main(int argc, const char **argv)
int ret = EXIT_SUCCESS;
int pc_debug = 0;
struct ops_ctx *data = NULL;
struct tools_ctx *ctx = NULL;
struct tevent_req *req;
struct group *grp_info;
const char *pc_groupname = NULL;
Expand All @@ -132,29 +129,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 ops_ctx */
pc = poptGetContext(NULL, argc, argv, long_options, 0);
poptSetOtherOptionHelp(pc, "USERNAME");
if((ret = poptGetNextOpt(pc)) < -1) {
poptSetOtherOptionHelp(pc, "GROUPNAME");
if ((ret = poptGetNextOpt(pc)) < -1) {
usage(pc, poptStrerror(ret));
ret = EXIT_FAILURE;
goto fini;
Expand All @@ -163,12 +142,22 @@ int main(int argc, const char **argv)
debug_level = pc_debug;

pc_groupname = poptGetArg(pc);
if(pc_groupname == NULL) {
if (pc_groupname == NULL) {
usage(pc, _("Specify group to delete\n"));
ret = EXIT_FAILURE;
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;
}

/* if the domain was not given as part of FQDN, default to local domain */
ret = get_domain(data, pc_groupname);
if (ret != EOK) {
Expand All @@ -191,7 +180,7 @@ int main(int argc, const char **argv)
}

/* groupdel */
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 remove group.\n");
Expand All @@ -201,21 +190,28 @@ int main(int argc, const char **argv)
tevent_req_set_callback(req, group_del, data);

while (!data->done) {
tevent_loop_once(ctx->ev);
tevent_loop_once(data->ctx->ev);
}

if (data->error) {
ret = data->error;
DEBUG(1, ("sysdb operation failed (%d)[%s]\n", ret, strerror(ret)));
ERROR("Internal error. Could not remove group.\n");
switch (ret) {
case ENOENT:
ERROR("No such group\n");
break;

default:
ERROR("Internal error. Could not remove group.\n");
break;
}
ret = EXIT_FAILURE;
goto fini;
}

ret = EXIT_SUCCESS;

fini:
talloc_free(ctx);
talloc_free(data);
poptFreeContext(pc);
exit(ret);
Expand Down

0 comments on commit db329e0

Please sign in to comment.