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)
56 prog = basename(argv[0]);
57 for (p = prog; *p; p++) {
64 while ((c = getopt(argc, argv, "u:g:s:")) != -1) {
73 if (sup_cnt+1 > SUP_MAX) {
74 fprintf(stderr, "%s: too many sup groups\n", prog);
77 sgids[sup_cnt++] = atoi(optarg);
85 /* build up the cmd */
92 p = cmd = (char **)malloc(sizeof(char *) * (argc - optind + 1));
93 for ( ; optind < argc; optind++, p++) {
94 *p = strdup(argv[optind]);
100 if (setgid(gid) == -1) {
101 fprintf(stderr, "%s: setgid(%d) failed: %s\n",
102 prog, (int)gid, strerror(errno));
107 if (gid != -1 || sup_cnt != 0) {
108 if (setgroups(sup_cnt, sgids) == -1) {
109 fprintf(stderr, "%s: setgroups() failed: %s\n",
110 prog, strerror(errno));
116 if (setuid(uid) == -1) {
117 fprintf(stderr, "%s: setuid(%d) failed: %s\n",
118 prog, (int)uid, strerror(errno));
124 fprintf(stderr, "%s: %s\n", cmd[0], strerror(errno));