2 * Copyright (c) 2000-2001 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Run a command with a particular
22 * - effective group id
23 * - supplementary group list
35 fprintf(stderr, "usage: %s [-u uid] [-g gid] [-s gid] cmd\n"
37 " -u - effective user-id\n"
38 " -g - effective group-id\n"
39 " -s - supplementary group-id\n", prog);
46 main(int argc, char **argv)
58 prog = basename(argv[0]);
59 for (p = prog; *p; p++) {
66 while ((c = getopt(argc, argv, "u:g:s:")) != -1) {
75 if (sup_cnt+1 > SUP_MAX) {
76 fprintf(stderr, "%s: too many sup groups\n", prog);
79 sgids[sup_cnt++] = atoi(optarg);
87 /* build up the cmd */
94 p = cmd = (char **)malloc(sizeof(char *) * (argc - optind + 1));
95 for ( ; optind < argc; optind++, p++) {
96 *p = strdup(argv[optind]);
102 if (setegid(gid) == -1) {
103 fprintf(stderr, "%s: setegid(%d) failed: %s\n",
104 prog, (int)gid, strerror(errno));
110 if (setgroups(sup_cnt, sgids) == -1) {
111 fprintf(stderr, "%s: setgroups() failed: %s\n",
112 prog, strerror(errno));
118 if (seteuid(uid) == -1) {
119 fprintf(stderr, "%s: seteuid(%d) failed: %s\n",
120 prog, (int)uid, strerror(errno));
127 fprintf(stderr, "%s: fork failed: %s\n",
128 prog, strerror(errno));
133 fprintf(stderr, "%s: %s\n", cmd[0], strerror(errno));
138 if (WIFSIGNALED(status)) {
139 fprintf(stderr, "%s: command terminated with signal %d\n",
140 prog, WTERMSIG(status));
143 else if (WIFEXITED(status)) {
144 exit(WEXITSTATUS(status));
147 fprintf(stderr, "%s: command bizarre wait status 0x%x\n",