4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
27 * This file is part of Lustre, http://www.lustre.org/
36 #include <sys/types.h>
44 #define NGROUPS_MAX 32
47 static const char usage[] =
48 "Usage: %s -u user_id [-g grp_id] [-v euid] [-j egid] [-G[gid0,gid1,...]] command\n"
49 " -u user_id switch to UID user_id\n"
50 " -g grp_id switch to GID grp_id\n"
51 " -v euid switch euid to UID\n"
52 " -j egid switch egid to GID\n"
53 " -G[gid0,gid1,...] set supplementary groups\n";
55 void Usage_and_abort(const char *name)
57 fprintf(stderr, usage, name);
61 int main(int argc, char **argv)
63 char **my_argv, *name = argv[0], *grp;
65 int gid_is_set = 0, uid_is_set = 0, num_supp = -1;
67 gid_t grp_id = 0, supp_groups[NGROUPS_MAX] = { 0 };
68 int euid_is_set = 0, egid_is_set = 0;
73 fprintf(stderr, "No parameter count\n");
74 Usage_and_abort(name);
78 while ((c = getopt(argc, argv, "+u:g:v:j:hG::")) != -1) {
81 if (!isdigit(optarg[0])) {
82 struct passwd *pw = getpwnam(optarg);
85 fprintf(stderr, "parameter '%s' bad\n",
87 Usage_and_abort(name);
91 user_id = (uid_t)atoi(optarg);
99 if (!isdigit(optarg[0])) {
100 struct group *gr = getgrnam(optarg);
103 fprintf(stderr, "getgrname %s failed\n",
105 Usage_and_abort(name);
109 grp_id = (gid_t)atoi(optarg);
115 if (!isdigit(optarg[0])) {
116 struct passwd *pw = getpwnam(optarg);
119 fprintf(stderr, "parameter '%s' bad\n",
121 Usage_and_abort(name);
125 euid = (uid_t)atoi(optarg);
131 if (!isdigit(optarg[0])) {
132 struct group *gr = getgrnam(optarg);
135 fprintf(stderr, "getgrname %s failed\n",
137 Usage_and_abort(name);
141 egid = (gid_t)atoi(optarg);
148 if (!optarg || !isdigit(optarg[0]))
150 while ((grp = strsep(&optarg, ",")) != NULL) {
151 printf("adding supp group %d\n", atoi(grp));
152 supp_groups[num_supp++] = atoi(grp);
153 if (num_supp >= NGROUPS_MAX)
160 Usage_and_abort(name);
166 fprintf(stderr, "Must specify uid to run.\n");
167 Usage_and_abort(name);
170 if (optind == argc) {
171 fprintf(stderr, "Must specify command to run.\n");
172 Usage_and_abort(name);
175 /* assemble the command */
176 my_argv = (char **)malloc(sizeof(char *) * (argc + 1 - optind));
178 fprintf(stderr, "Error in allocating memory. (%s)\n",
183 for (i = optind; i < argc; i++)
184 my_argv[i - optind] = argv[i];
186 my_argv[i - optind] = NULL;
195 status = setregid(grp_id, egid);
197 fprintf(stderr, "Cannot change gid to %d/%d, errno=%d (%s)\n",
198 grp_id, egid, errno, strerror(errno));
203 status = setgroups(num_supp, supp_groups);
205 perror("setting supplementary groups");
213 status = setreuid(user_id, euid);
215 fprintf(stderr, "Cannot change uid to %d/%d, errno=%d (%s)\n",
216 user_id, euid, errno, strerror(errno));
220 fprintf(stderr, "running as uid/gid/euid/egid %d/%d/%d/%d, groups:",
221 user_id, grp_id, euid, egid);
222 for (i = 0; i < num_supp; i++)
223 fprintf(stderr, " %d", supp_groups[i]);
224 fprintf(stderr, "\n");
226 for (i = 0; i < argc - optind; i++)
227 fprintf(stderr, " [%s]", my_argv[i]);
229 fprintf(stderr, "\n");
232 /* The command to be run */
233 execvp(my_argv[0], my_argv);
234 fprintf(stderr, "execvp fails running %s (%d): %s\n", my_argv[0],
235 errno, strerror(errno));