1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
17 fprintf(stderr, "Usage: runas -u user_id [ -g grp_id ]" \
18 " command_to_be_run \n");
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"
27 // ROOT runs "runas" for free
28 // Other users run "runas" requires chmod 6755 "command_to_be_run"
31 main(int argc, char**argv)
46 while ((c = getopt (argc, argv, "+u:g:h")) != -1) {
49 user_id = (uid_t)atoi(optarg);
57 grp_id = (gid_t)atoi(optarg);
66 // fprintf(stderr, "Bad parameters.\n");
67 // Usage_and_abort ();
77 fprintf(stderr, "Bad parameters.\n");
81 // assemble the command
82 my_argv = (char**)malloc(sizeof(char*)*(argc+1-optind));
84 fprintf(stderr, "Error in allocating memory. (%s)\n", strerror(errno));
88 for(i=optind; i< argc; i++) {
89 my_argv[i-optind] = argv[i];
90 // printf("%s\n",my_argv[i-optind]);
92 my_argv[i-optind]=NULL;
99 status = setregid(grp_id, grp_id );
101 fprintf(stderr, "Cannot change grp_ID to %d, errno=%d (%s)\n",
102 grp_id, errno, strerror(errno) );
107 status = setreuid(user_id, user_id );
109 fprintf(stderr,"Cannot change user_ID to %d, errno=%d (%s)\n",
110 user_id, errno, strerror(errno) );
115 fprintf(stderr, "running as USER(%d), Grp (%d): ",
118 for(i=0; i<argc-optind; i++)
119 fprintf(stderr, " [%s]", my_argv[i]);
121 fprintf(stderr, "\n");
124 // The command to be run
125 execvp(my_argv[0], my_argv);
126 fprintf(stderr, "execvp fails running %s\n", my_argv[0]);