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