1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
43 #include <sys/types.h>
51 #define NGROUPS_MAX 32
54 static const char usage[] =
55 "Usage: %s -u user_id [-g grp_id] [-v euid] [-j egid] [-G[gid0,gid1,...]] command\n"
56 " -u user_id switch to UID user_id\n"
57 " -g grp_id switch to GID grp_id\n"
58 " -v euid switch euid to UID\n"
59 " -j egid switch egid to GID\n"
60 " -G[gid0,gid1,...] set supplementary groups\n";
62 void Usage_and_abort(const char *name)
64 fprintf(stderr, usage, name);
68 int main(int argc, char **argv)
70 char **my_argv, *name = argv[0], *grp;
72 int gid_is_set = 0, uid_is_set = 0, num_supp = -1;
74 gid_t grp_id = 0, supp_groups[NGROUPS_MAX] = { 0 };
75 int euid_is_set = 0, egid_is_set = 0;
80 fprintf(stderr, "No parameter count\n");
81 Usage_and_abort(name);
85 while ((c = getopt(argc, argv, "+u:g:v:j:hG::")) != -1) {
88 if (!isdigit(optarg[0])) {
89 struct passwd *pw = getpwnam(optarg);
91 fprintf(stderr, "parameter '%s' bad\n",
93 Usage_and_abort(name);
97 user_id = (uid_t)atoi(optarg);
105 if (!isdigit(optarg[0])) {
106 struct group *gr = getgrnam(optarg);
108 fprintf(stderr, "getgrname %s failed\n",
110 Usage_and_abort(name);
114 grp_id = (gid_t)atoi(optarg);
120 if (!isdigit(optarg[0])) {
121 struct passwd *pw = getpwnam(optarg);
123 fprintf(stderr, "parameter '%s' bad\n",
125 Usage_and_abort(name);
129 euid = (uid_t)atoi(optarg);
135 if (!isdigit(optarg[0])) {
136 struct group *gr = getgrnam(optarg);
138 fprintf(stderr, "getgrname %s failed\n",
140 Usage_and_abort(name);
144 egid = (gid_t)atoi(optarg);
151 if (optarg == NULL || !isdigit(optarg[0]))
153 while ((grp = strsep(&optarg, ",")) != NULL) {
154 printf("adding supp group %d\n", atoi(grp));
155 supp_groups[num_supp++] = atoi(grp);
156 if (num_supp >= NGROUPS_MAX)
163 Usage_and_abort(name);
169 fprintf(stderr, "Must specify uid to run.\n");
170 Usage_and_abort(name);
173 if (optind == argc) {
174 fprintf(stderr, "Must specify command to run.\n");
175 Usage_and_abort(name);
178 // assemble the command
179 my_argv = (char**)malloc(sizeof(char*)*(argc+1-optind));
180 if (my_argv == NULL) {
181 fprintf(stderr, "Error in allocating memory. (%s)\n",
186 for (i = optind; i < argc; i++) {
187 my_argv[i-optind] = argv[i];
188 //printf("%s\n",my_argv[i-optind]);
190 my_argv[i-optind] = NULL;
199 status = setregid(grp_id, egid);
201 fprintf(stderr, "Cannot change gid to %d/%d, errno=%d (%s)\n",
202 grp_id, egid, errno, strerror(errno) );
207 status = setgroups(num_supp, supp_groups);
209 perror("setting supplementary groups");
217 status = setreuid(user_id, euid);
219 fprintf(stderr,"Cannot change uid to %d/%d, errno=%d (%s)\n",
220 user_id, euid, errno, strerror(errno) );
224 fprintf(stderr, "running as uid/gid/euid/egid %d/%d/%d/%d, groups:",
225 user_id, grp_id, euid, egid);
226 for (i = 0; i < num_supp; i++)
227 fprintf(stderr, " %d", supp_groups[i]);
228 fprintf(stderr, "\n");
230 for (i = 0; i < argc - optind; i++)
231 fprintf(stderr, " [%s]", my_argv[i]);
233 fprintf(stderr, "\n");
236 // The command to be run
237 execvp(my_argv[0], my_argv);
238 fprintf(stderr, "execvp fails running %s (%d): %s\n", my_argv[0],
239 errno, strerror(errno));