1 /* test-uid.c --- playing with setuid.
2 * xscreensaver, Copyright (c) 1998 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
42 fprintf(stderr, "real user/group: %ld/%ld (%s/%s)\n", (long) uid, (long) gid,
43 (p && p->pw_name ? p->pw_name : "???"),
44 (g && g->gr_name ? g->gr_name : "???"));
48 fprintf(stderr, "eff. user/group: %ld/%ld (%s/%s)\n", (long)euid, (long)egid,
49 (p && p->pw_name ? p->pw_name : "???"),
50 (g && g->gr_name ? g->gr_name : "???"));
54 main (int argc, char **argv)
63 "usage: %s [ user/group ... ]\n"
64 "\tEach argument may be a user name, or user/group.\n"
65 "\tThis program will attempt to setuid/setgid to each\n"
66 "\tin turn, and report the results. The user and group\n"
67 "\tnames may be strings, or numeric.\n",
73 for (i = 1; i < argc; i++)
76 char *group = strchr(user, '/');
78 group = strchr(user, '.');
88 if (*group == '-' || (*group >= '0' && *group <= '9'))
89 if (1 == sscanf(group, "%ld", &gid))
107 fprintf(stderr, "no group numbered %s.\n", group);
112 fprintf(stderr, "no group named %s.\n", group);
117 fprintf(stderr, "setgid(%ld) \"%s\"", gid, group);
118 if (setgid(gid) == 0)
119 fprintf(stderr, " succeeded.\n");
132 if (*user == '-' || (*user >= '0' && *user <= '9'))
133 if (1 == sscanf(user, "%ld", &uid))
151 fprintf(stderr, "no user numbered \"%s\".\n", user);
156 fprintf(stderr, "no user named %s.\n", user);
161 fprintf(stderr, "setuid(%ld) \"%s\"", uid, user);
162 if (setuid(uid) == 0)
163 fprintf(stderr, " succeeded.\n");
172 "running \"whoami\" and \"groups\" in a sub-process reports:\n");
175 system ("/bin/sh -c 'echo \"`whoami` / `groups`\"'");