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)
47 while ((c = getopt (argc, argv, "u:g:h")) != -1) {
50 user_id = (uid_t)atoi(optarg);
58 grp_id = (gid_t)atoi(optarg);
67 // fprintf(stderr, "Bad parameters.\n");
68 // Usage_and_abort ();
78 fprintf(stderr, "Bad parameters.\n");
83 // assemble the command
85 for (i = optind; i < argc; i++)
86 cmd_ptr += sprintf(cmd_ptr, "%s ", argv[i]);
94 status = setregid(grp_id, grp_id );
96 fprintf(stderr, "Cannot change grp_ID to %d, errno=%d (%s)\n",
97 grp_id, errno, strerror(errno) );
102 status = setreuid(user_id, user_id );
104 fprintf(stderr,"Cannot change user_ID to %d, errno=%d (%s)\n",
105 user_id, errno, strerror(errno) );
113 fprintf(stdout, "running as USER(%d), Grp (%d): \"%s\" \n",
114 user_id, grp_id, command );
117 status = system(command);
119 // pass the return code of command_to_be_run out of this wrapper
121 fprintf(stderr, "%s: system() command failed to run\n",
125 status = WEXITSTATUS(status);
126 fprintf(stderr, "[%s #%d] \"%s\" returns %d (%s).\n", argv[0],
127 user_id, argv[optind], status, strerror(status));