Whamcloud - gitweb
merge b_devel into HEAD (20030703)
[fs/lustre-release.git] / lustre / tests / runas.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  */
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <string.h>
8 #include <errno.h>
9 #include <sys/types.h>
10 #include <sys/wait.h>
11
12 #define DEBUG 0
13
14 void Usage_and_abort(void)
15 {
16        fprintf(stderr, "Usage: runas -u user_id [ -g grp_id ]"
17                " command_to_be_run \n");
18        exit(-1);
19 }
20
21 // Usage: runas -u user_id [ -g grp_id ] [--] command_to_be_run
22 // return: the return value of "command_to_be_run"
23 // NOTE: returning -1 might be the return code of this program itself or
24 // the "command_to_be_run"
25
26 // ROOT runs "runas" for free
27 // Other users run "runas" requires  chmod 6755 "command_to_be_run"
28
29 int main(int argc, char **argv)
30 {
31         char **my_argv;
32         int status;
33         int c,i;
34         int gid_is_set = 0;
35         int uid_is_set = 0;
36         uid_t user_id;
37         gid_t grp_id;
38
39         if (argc == 1)
40                 Usage_and_abort();
41
42         // get UID and GID
43         while ((c = getopt (argc, argv, "+u:g:h")) != -1) {
44                 switch (c) {
45                 case 'u':
46                         user_id = (uid_t)atoi(optarg);
47                         uid_is_set = 1;
48                         if (!gid_is_set)
49                                 grp_id = user_id;
50                         break;
51
52                 case 'g':
53                         grp_id = (gid_t)atoi(optarg);
54                         gid_is_set = 1;
55                         break;
56
57                 case 'h':
58                         Usage_and_abort();
59                         break;
60
61                 default:
62                         //fprintf(stderr, "Bad parameters.\n");
63                         //Usage_and_abort ();
64                         break;
65                 }
66         }
67
68         if (!uid_is_set)
69                 Usage_and_abort();
70
71         if (optind == argc) {
72                 fprintf(stderr, "Bad parameters.\n");
73                 Usage_and_abort();
74         }
75
76         // assemble the command
77         my_argv = (char**)malloc(sizeof(char*)*(argc+1-optind));
78         if (my_argv == NULL) {
79                 fprintf(stderr, "Error in allocating memory. (%s)\n",
80                         strerror(errno));
81                 exit(-1);
82         }
83
84         for (i = optind; i < argc; i++) {
85                 my_argv[i-optind] = argv[i];
86                 //printf("%s\n",my_argv[i-optind]);
87         }
88         my_argv[i-optind] = NULL;
89
90 #if DEBUG
91         system("whoami");
92 #endif
93
94         // set GID
95         status = setregid(grp_id, grp_id);
96         if (status == -1) {
97                  fprintf(stderr, "Cannot change grp_ID to %d, errno=%d (%s)\n",
98                          grp_id, errno, strerror(errno) );
99                  exit(-1);
100         }
101
102         // set UID
103         status = setreuid(user_id, user_id );
104         if(status == -1) {
105                   fprintf(stderr,"Cannot change user_ID to %d, errno=%d (%s)\n",
106                            user_id, errno, strerror(errno) );
107                   exit(-1);
108         }
109
110
111         fprintf(stderr, "running as USER(%d), Grp (%d):  ", user_id, grp_id );
112
113         for (i = 0; i < argc - optind; i++)
114                  fprintf(stderr, " [%s]", my_argv[i]);
115
116         fprintf(stderr, "\n");
117         fflush(stderr);
118
119         // The command to be run
120         execvp(my_argv[0], my_argv);
121         fprintf(stderr, "execvp fails running %s\n", my_argv[0]);
122         exit(-1);
123 }
124