Whamcloud - gitweb
b=3983
[fs/lustre-release.git] / lustre / tests / runas.c
index 11e888b..91c9c7f 100644 (file)
@@ -6,41 +6,42 @@
 #include <unistd.h>
 #include <string.h>
 #include <errno.h>
+#include <ctype.h>
 #include <sys/types.h>
+#include <grp.h>
 #include <sys/wait.h>
 
 #define DEBUG 0
 
-void Usage_and_abort(void)
-{
-       fprintf(stderr, "Usage: runas -u user_id [ -g grp_id ]"
-               " command_to_be_run \n");
-       exit(-1);
-}
+#ifndef NGROUPS_MAX
+#define NGROUPS_MAX 32
+#endif
 
-// Usage: runas -u user_id [ -g grp_id ] [--] command_to_be_run
-// return: the return value of "command_to_be_run"
-// NOTE: returning -1 might be the return code of this program itself or
-// the "command_to_be_run"
+static const char usage[] =
+"Usage: %s -u user_id [-g grp_id ] [ -G ] command\n"
+"  -u user_id           switch to UID user_id\n"
+"  -g grp_id            switch to GID grp_id\n"
+"  -G[gid0,gid1,...]    set supplementary groups\n";
 
-// ROOT runs "runas" for free
-// Other users run "runas" requires  chmod 6755 "command_to_be_run"
+void Usage_and_abort(const char *name)
+{
+        fprintf(stderr, usage, name);
+        exit(-1);
+}
 
 int main(int argc, char **argv)
 {
-        char **my_argv;
-        int status;
-        int c,i;
-        int gid_is_set = 0;
-        int uid_is_set = 0;
-        uid_t user_id;
-        gid_t grp_id;
+        char **my_argv, *name = argv[0], *grp;
+        int status, c, i;
+        int gid_is_set = 0, uid_is_set = 0, num_supp = -1;
+        uid_t user_id = 0;
+        gid_t grp_id = 0, supp_groups[NGROUPS_MAX] = { 0 };
 
         if (argc == 1)
-                Usage_and_abort();
+                Usage_and_abort(name);
 
         // get UID and GID
-        while ((c = getopt (argc, argv, "+u:g:h")) != -1) {
+        while ((c = getopt(argc, argv, "+u:g:hG::")) != -1) {
                 switch (c) {
                 case 'u':
                         user_id = (uid_t)atoi(optarg);
@@ -54,22 +55,31 @@ int main(int argc, char **argv)
                         gid_is_set = 1;
                         break;
 
-                case 'h':
-                        Usage_and_abort();
+                case 'G':
+                        num_supp = 0;
+                        if (optarg == NULL || !isdigit(optarg[0]))
+                                break;
+                        while ((grp = strsep(&optarg, ",")) != NULL) {
+                                printf("adding supp group %d\n", atoi(grp));
+                                supp_groups[num_supp++] = atoi(grp);
+                                if (num_supp >= NGROUPS_MAX)
+                                        break;
+                        }
                         break;
 
                 default:
-                        //fprintf(stderr, "Bad parameters.\n");
-                        //Usage_and_abort ();
+                case 'h':
+                        Usage_and_abort(name);
+                        break;
                 }
         }
 
         if (!uid_is_set)
-                Usage_and_abort();
+                Usage_and_abort(name);
 
         if (optind == argc) {
-                fprintf(stderr, "Bad parameters.\n");
-                Usage_and_abort();
+                fputs("Must specify command to run.\n", stderr);
+                Usage_and_abort(name);
         }
 
         // assemble the command
@@ -98,6 +108,14 @@ int main(int argc, char **argv)
                  exit(-1);
         }
 
+        if (num_supp >= 0) {
+                status = setgroups(num_supp, supp_groups);
+                if (status == -1) {
+                        perror("setting supplementary groups");
+                        exit(-1);
+                }
+        }
+
         // set UID
         status = setreuid(user_id, user_id );
         if(status == -1) {
@@ -106,8 +124,10 @@ int main(int argc, char **argv)
                   exit(-1);
         }
 
-
-        fprintf(stderr, "running as USER(%d), Grp (%d):  ", user_id, grp_id );
+        fprintf(stderr, "running as UID %d, GID %d", user_id, grp_id);
+        for (i = 0; i < num_supp; i++)
+                fprintf(stderr, ":%d", supp_groups[i]);
+        fprintf(stderr, "\n");
 
         for (i = 0; i < argc - optind; i++)
                  fprintf(stderr, " [%s]", my_argv[i]);