1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2001 Silicon Graphics, Inc.
8 * Run a command with a particular
10 * - effective group id
11 * - supplementary group list
23 fprintf(stderr, "usage: %s [-u uid] [-g gid] [-s gid] cmd\n"
25 " -u - effective user-id\n"
26 " -g - effective group-id\n"
27 " -s - supplementary group-id\n", prog);
34 main(int argc, char **argv)
44 prog = basename(argv[0]);
45 for (p = prog; *p; p++) {
52 while ((c = getopt(argc, argv, "u:g:s:")) != -1) {
61 if (sup_cnt+1 > SUP_MAX) {
62 fprintf(stderr, "%s: too many sup groups\n", prog);
65 sgids[sup_cnt++] = atoi(optarg);
73 /* build up the cmd */
80 p = cmd = (char **)malloc(sizeof(char *) * (argc - optind + 1));
81 for ( ; optind < argc; optind++, p++) {
82 *p = strdup(argv[optind]);
88 if (setgid(gid) == -1) {
89 fprintf(stderr, "%s: setgid(%d) failed: %s\n",
90 prog, (int)gid, strerror(errno));
95 if (gid != -1 || sup_cnt != 0) {
96 if (setgroups(sup_cnt, sgids) == -1) {
97 fprintf(stderr, "%s: setgroups() failed: %s\n",
98 prog, strerror(errno));
104 if (setuid(uid) == -1) {
105 fprintf(stderr, "%s: setuid(%d) failed: %s\n",
106 prog, (int)uid, strerror(errno));
112 fprintf(stderr, "%s: %s\n", cmd[0], strerror(errno));