1 /* test-uid.c --- playing with setuid.
2 * xscreensaver, Copyright (c) 1998, 2005 Jamie Zawinski <jwz@jwz.org>
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation. No representations are made about the suitability of this
9 * software for any purpose. It is provided "as is" without express or
44 fprintf(stderr, "real user/group: %ld/%ld (%s/%s)\n", (long) uid, (long) gid,
45 (p && p->pw_name ? p->pw_name : "???"),
46 (g && g->gr_name ? g->gr_name : "???"));
50 fprintf(stderr, "eff. user/group: %ld/%ld (%s/%s)\n", (long)euid, (long)egid,
51 (p && p->pw_name ? p->pw_name : "???"),
52 (g && g->gr_name ? g->gr_name : "???"));
54 n = getgroups(sizeof(groups)-1, groups);
56 perror("getgroups failed");
60 fprintf (stderr, "eff. group list: [");
61 for (i = 0; i < n; i++)
63 g = getgrgid (groups[i]);
64 fprintf(stderr, "%s%s=%ld", (i == 0 ? "" : ", "),
65 (g->gr_name ? g->gr_name : "???"),
68 fprintf (stderr, "]\n");
73 main (int argc, char **argv)
82 "usage: %s [ user/group ... ]\n"
83 "\tEach argument may be a user name, or user/group.\n"
84 "\tThis program will attempt to setuid/setgid to each\n"
85 "\tin turn, and report the results. The user and group\n"
86 "\tnames may be strings, or numeric.\n",
92 for (i = 1; i < argc; i++)
95 char *group = strchr(user, '/');
97 group = strchr(user, '.');
107 if (*group == '-' || (*group >= '0' && *group <= '9'))
108 if (1 == sscanf(group, "%ld", &gid))
126 fprintf(stderr, "no group numbered %s.\n", group);
131 fprintf(stderr, "no group named %s.\n", group);
136 fprintf(stderr, "setgroups(1, [%ld]) \"%s\"", gid, group);
139 if (setgroups(1, &g2) == 0)
140 fprintf(stderr, " succeeded.\n");
145 fprintf(stderr, "setgid(%ld) \"%s\"", gid, group);
146 if (setgid(gid) == 0)
147 fprintf(stderr, " succeeded.\n");
160 if (*user == '-' || (*user >= '0' && *user <= '9'))
161 if (1 == sscanf(user, "%ld", &uid))
179 fprintf(stderr, "no user numbered \"%s\".\n", user);
184 fprintf(stderr, "no user named %s.\n", user);
189 fprintf(stderr, "setuid(%ld) \"%s\"", uid, user);
190 if (setuid(uid) == 0)
191 fprintf(stderr, " succeeded.\n");
200 "running \"whoami\" and \"groups\" in a sub-process reports:\n");
203 system ("/bin/sh -c 'echo \"`whoami` / `groups`\"'");