From 30a7e4686b1b85455c8922548d54a20729135adc Mon Sep 17 00:00:00 2001
From: Jakub Hrozek <jhrozek@redhat.com>
Date: Tue, 31 Aug 2010 18:12:25 +0200
Subject: [PATCH] Fix assorted minor bugs in sss_ tools

Fixes: #585
---
 src/tools/sss_groupadd.c  |    8 ++------
 src/tools/sss_groupdel.c  |    8 ++------
 src/tools/sss_groupmod.c  |   14 ++++++--------
 src/tools/sss_groupshow.c |    8 ++------
 src/tools/sss_useradd.c   |   13 ++++++-------
 src/tools/sss_userdel.c   |    8 ++------
 src/tools/sss_usermod.c   |   14 ++++++--------
 src/tools/tools_util.c    |    5 +++++
 src/tools/tools_util.h    |    6 ++++++
 9 files changed, 37 insertions(+), 47 deletions(-)

diff --git a/src/tools/sss_groupadd.c b/src/tools/sss_groupadd.c
index e5ef721..8817625 100644
--- a/src/tools/sss_groupadd.c
+++ b/src/tools/sss_groupadd.c
@@ -62,9 +62,7 @@ int main(int argc, const char **argv)
     pc = poptGetContext(NULL, argc, argv, long_options, 0);
     poptSetOtherOptionHelp(pc, "GROUPNAME");
     if ((ret = poptGetNextOpt(pc)) < -1) {
-        usage(pc, poptStrerror(ret));
-        ret = EXIT_FAILURE;
-        goto fini;
+        BAD_POPT_PARAMS(pc, poptStrerror(ret), ret, fini);
     }
 
     debug_level = pc_debug;
@@ -72,9 +70,7 @@ int main(int argc, const char **argv)
     /* groupname is an argument, not option */
     pc_groupname = poptGetArg(pc);
     if (pc_groupname == NULL) {
-        usage(pc, _("Specify group to add\n"));
-        ret = EXIT_FAILURE;
-        goto fini;
+        BAD_POPT_PARAMS(pc, _("Specify group to add\n"), ret, fini);
     }
 
     CHECK_ROOT(ret, debug_prg_name);
diff --git a/src/tools/sss_groupdel.c b/src/tools/sss_groupdel.c
index 006ed09..99b6908 100644
--- a/src/tools/sss_groupdel.c
+++ b/src/tools/sss_groupdel.c
@@ -58,18 +58,14 @@ int main(int argc, const char **argv)
     pc = poptGetContext(NULL, argc, argv, long_options, 0);
     poptSetOtherOptionHelp(pc, "GROUPNAME");
     if ((ret = poptGetNextOpt(pc)) < -1) {
-        usage(pc, poptStrerror(ret));
-        ret = EXIT_FAILURE;
-        goto fini;
+        BAD_POPT_PARAMS(pc, poptStrerror(ret), ret, fini);
     }
 
     debug_level = pc_debug;
 
     pc_groupname = poptGetArg(pc);
     if (pc_groupname == NULL) {
-        usage(pc, _("Specify group to delete\n"));
-        ret = EXIT_FAILURE;
-        goto fini;
+        BAD_POPT_PARAMS(pc, _("Specify group to delete\n"), ret, fini);
     }
 
     CHECK_ROOT(ret, debug_prg_name);
diff --git a/src/tools/sss_groupmod.c b/src/tools/sss_groupmod.c
index bd684ba..59ec10b 100644
--- a/src/tools/sss_groupmod.c
+++ b/src/tools/sss_groupmod.c
@@ -73,31 +73,29 @@ int main(int argc, const char **argv)
             case 'a':
                 addgroups = poptGetOptArg(pc);
                 if (addgroups == NULL) {
-                    ret = -1;
+                    BAD_POPT_PARAMS(pc, _("Specify group to add to\n"),
+                                    ret, fini);
                 }
                 break;
 
             case 'r':
                 rmgroups = poptGetOptArg(pc);
                 if (rmgroups == NULL) {
-                    ret = -1;
+                    BAD_POPT_PARAMS(pc, _("Specify group to remove from\n"),
+                                    ret, fini);
                 }
                 break;
         }
     }
 
     if (ret != -1) {
-        usage(pc, poptStrerror(ret));
-        ret = EXIT_FAILURE;
-        goto fini;
+        BAD_POPT_PARAMS(pc, poptStrerror(ret), ret, fini);
     }
 
     /* groupname is an argument without --option */
     pc_groupname = poptGetArg(pc);
     if (pc_groupname == NULL) {
-        usage(pc, _("Specify group to modify\n"));
-        ret = EXIT_FAILURE;
-        goto fini;
+        BAD_POPT_PARAMS(pc, _("Specify group to modify\n"), ret, fini);
     }
 
     debug_level = pc_debug;
diff --git a/src/tools/sss_groupshow.c b/src/tools/sss_groupshow.c
index be28f16..1532485 100644
--- a/src/tools/sss_groupshow.c
+++ b/src/tools/sss_groupshow.c
@@ -647,16 +647,12 @@ int main(int argc, const char **argv)
     debug_level = pc_debug;
 
     if (ret != -1) {
-        usage(pc, poptStrerror(ret));
-        ret = EXIT_FAILURE;
-        goto fini;
+        BAD_POPT_PARAMS(pc, poptStrerror(ret), ret, fini);
     }
 
     pc_groupname = poptGetArg(pc);
     if (pc_groupname == NULL) {
-        usage(pc, _("Specify group to show\n"));
-        ret = EXIT_FAILURE;
-        goto fini;
+        BAD_POPT_PARAMS(pc, _("Specify group to show\n"), ret, fini);
     }
 
     CHECK_ROOT(ret, debug_prg_name);
diff --git a/src/tools/sss_useradd.c b/src/tools/sss_useradd.c
index f9b9515..edff6e1 100644
--- a/src/tools/sss_useradd.c
+++ b/src/tools/sss_useradd.c
@@ -80,7 +80,10 @@ int main(int argc, const char **argv)
         switch (ret) {
             case 'G':
                 groups = poptGetOptArg(pc);
-                if (!groups) goto fini;
+                if (!groups) {
+                    BAD_POPT_PARAMS(pc, _("Specify group to add to\n"),
+                                    ret, fini);
+                }
                 break;
 
             case 'm':
@@ -96,17 +99,13 @@ int main(int argc, const char **argv)
     debug_level = pc_debug;
 
     if (ret != -1) {
-        usage(pc, poptStrerror(ret));
-        ret = EXIT_FAILURE;
-        goto fini;
+        BAD_POPT_PARAMS(pc, poptStrerror(ret), ret, fini);
     }
 
     /* username is an argument without --option */
     pc_username = poptGetArg(pc);
     if (pc_username == NULL) {
-        usage(pc, (_("Specify user to add\n")));
-        ret = EXIT_FAILURE;
-        goto fini;
+        BAD_POPT_PARAMS(pc, _("Specify user to add\n"), ret, fini);
     }
 
     CHECK_ROOT(ret, debug_prg_name);
diff --git a/src/tools/sss_userdel.c b/src/tools/sss_userdel.c
index 869ec9c..1d0cb12 100644
--- a/src/tools/sss_userdel.c
+++ b/src/tools/sss_userdel.c
@@ -176,16 +176,12 @@ int main(int argc, const char **argv)
     debug_level = pc_debug;
 
     if (ret != -1) {
-        usage(pc, poptStrerror(ret));
-        ret = EXIT_FAILURE;
-        goto fini;
+        BAD_POPT_PARAMS(pc, poptStrerror(ret), ret, fini);
     }
 
     pc_username = poptGetArg(pc);
     if (pc_username == NULL) {
-        usage(pc, _("Specify user to delete\n"));
-        ret = EXIT_FAILURE;
-        goto fini;
+        BAD_POPT_PARAMS(pc, _("Specify user to delete\n"), ret, fini);
     }
 
     CHECK_ROOT(ret, debug_prg_name);
diff --git a/src/tools/sss_usermod.c b/src/tools/sss_usermod.c
index 54e3304..bcf2b3a 100644
--- a/src/tools/sss_usermod.c
+++ b/src/tools/sss_usermod.c
@@ -82,14 +82,16 @@ int main(int argc, const char **argv)
             case 'a':
                 addgroups = poptGetOptArg(pc);
                 if (addgroups == NULL) {
-                    ret = -1;
+                    BAD_POPT_PARAMS(pc, _("Specify group to add to\n"),
+                                    ret, fini);
                 }
                 break;
 
             case 'r':
                 rmgroups = poptGetOptArg(pc);
                 if (rmgroups == NULL) {
-                    ret = -1;
+                    BAD_POPT_PARAMS(pc, _("Specify group to remove from\n"),
+                                    ret, fini);
                 }
                 break;
 
@@ -104,9 +106,7 @@ int main(int argc, const char **argv)
     }
 
     if (ret != -1) {
-        usage(pc, poptStrerror(ret));
-        ret = EXIT_FAILURE;
-        goto fini;
+        BAD_POPT_PARAMS(pc, poptStrerror(ret), ret, fini);
     }
 
     debug_level = pc_debug;
@@ -114,9 +114,7 @@ int main(int argc, const char **argv)
     /* username is an argument without --option */
     pc_username = poptGetArg(pc);
     if (pc_username == NULL) {
-        usage(pc, _("Specify user to modify\n"));
-        ret = EXIT_FAILURE;
-        goto fini;
+        BAD_POPT_PARAMS(pc, _("Specify user to modify\n"), ret, fini);
     }
 
     CHECK_ROOT(ret, debug_prg_name);
diff --git a/src/tools/tools_util.c b/src/tools/tools_util.c
index e76ffe7..96ca584 100644
--- a/src/tools/tools_util.c
+++ b/src/tools/tools_util.c
@@ -139,6 +139,11 @@ int parse_group_name_domain(struct tools_ctx *tctx,
 
     for (i = 0; groups[i]; ++i) {
         ret = sss_parse_name(tctx, tctx->snctx, groups[i], &domain, &name);
+        if (ret != EOK) {
+            DEBUG(1, ("Invalid name in group list, skipping: [%s] (%d)\n",
+                       groups[i], ret));
+            continue;
+        }
 
         /* If FQDN is specified, it must be within the same domain as user */
         if (domain) {
diff --git a/src/tools/tools_util.h b/src/tools/tools_util.h
index 84fc3be..e8b2536 100644
--- a/src/tools/tools_util.h
+++ b/src/tools/tools_util.h
@@ -27,6 +27,12 @@
 
 #include "util/util.h"
 
+#define BAD_POPT_PARAMS(pc, msg, val, label) do { \
+        usage(pc, msg);                           \
+        val = EXIT_FAILURE;                       \
+        goto label;                               \
+} while(0)
+
 #define CHECK_ROOT(val, prg_name) do { \
     val = getuid(); \
     if (val != 0) { \
-- 
1.7.2.2

