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 size = sizeof(groups) / sizeof(gid_t);
55 n = getgroups(size - 1, groups);
57 perror("getgroups failed");
61 fprintf (stderr, "eff. group list: [");
62 for (i = 0; i < n; i++)
64 g = getgrgid (groups[i]);
65 fprintf(stderr, "%s%s=%ld", (i == 0 ? "" : ", "),
66 (g->gr_name ? g->gr_name : "???"),
69 fprintf (stderr, "]\n");
74 main (int argc, char **argv)
83 "usage: %s [ user/group ... ]\n"
84 "\tEach argument may be a user name, or user/group.\n"
85 "\tThis program will attempt to setuid/setgid to each\n"
86 "\tin turn, and report the results. The user and group\n"
87 "\tnames may be strings, or numeric.\n",
93 for (i = 1; i < argc; i++)
96 char *group = strchr(user, '/');
98 group = strchr(user, '.');
108 if (*group == '-' || (*group >= '0' && *group <= '9'))
109 if (1 == sscanf(group, "%ld", &gid))
127 fprintf(stderr, "no group numbered %s.\n", group);
132 fprintf(stderr, "no group named %s.\n", group);
137 fprintf(stderr, "setgroups(1, [%ld]) \"%s\"", gid, group);
140 if (setgroups(1, &g2) == 0)
141 fprintf(stderr, " succeeded.\n");
146 fprintf(stderr, "setgid(%ld) \"%s\"", gid, group);
147 if (setgid(gid) == 0)
148 fprintf(stderr, " succeeded.\n");
161 if (*user == '-' || (*user >= '0' && *user <= '9'))
162 if (1 == sscanf(user, "%ld", &uid))
180 fprintf(stderr, "no user numbered \"%s\".\n", user);
185 fprintf(stderr, "no user named %s.\n", user);
190 fprintf(stderr, "setuid(%ld) \"%s\"", uid, user);
191 if (setuid(uid) == 0)
192 fprintf(stderr, " succeeded.\n");
201 "running \"whoami\" and \"groups\" in a sub-process reports:\n");
204 system ("/bin/sh -c 'echo \"`whoami` / `groups`\"'");