1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
14 void Usage_and_abort(void)
16 fprintf(stderr, "Usage: runas -u user_id [ -g grp_id ]"
17 " command_to_be_run \n");
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"
26 // ROOT runs "runas" for free
27 // Other users run "runas" requires chmod 6755 "command_to_be_run"
29 int main(int argc, char **argv)
43 while ((c = getopt (argc, argv, "+u:g:h")) != -1) {
46 user_id = (uid_t)atoi(optarg);
53 grp_id = (gid_t)atoi(optarg);
62 //fprintf(stderr, "Bad parameters.\n");
72 fprintf(stderr, "Bad parameters.\n");
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",
84 for (i = optind; i < argc; i++) {
85 my_argv[i-optind] = argv[i];
86 //printf("%s\n",my_argv[i-optind]);
88 my_argv[i-optind] = NULL;
95 status = setregid(grp_id, grp_id);
97 fprintf(stderr, "Cannot change grp_ID to %d, errno=%d (%s)\n",
98 grp_id, errno, strerror(errno) );
103 status = setreuid(user_id, user_id );
105 fprintf(stderr,"Cannot change user_ID to %d, errno=%d (%s)\n",
106 user_id, errno, strerror(errno) );
111 fprintf(stderr, "running as USER(%d), Grp (%d): ", user_id, grp_id );
113 for (i = 0; i < argc - optind; i++)
114 fprintf(stderr, " [%s]", my_argv[i]);
116 fprintf(stderr, "\n");
119 // The command to be run
120 execvp(my_argv[0], my_argv);
121 fprintf(stderr, "execvp fails running %s\n", my_argv[0]);