1 /* Test of dropping uid/gid privileges of the current process temporarily.
2 Copyright (C) 2009-2013 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
32 #if HAVE_GETRESUID /* glibc, FreeBSD, OpenBSD, HP-UX */
36 ASSERT (getresuid (&real, &effective, &saved) >= 0);
37 printf ("uids: real=%d effective=%d saved=%d",
38 (int) real, (int) effective, (int) saved);
40 printf ("uids: real=%d effective=%d",
41 (int) getuid (), (int) geteuid ());
43 printf ("uids: real=%d",
51 #if HAVE_GETRESGID /* glibc, FreeBSD, OpenBSD, HP-UX */
55 ASSERT (getresgid (&real, &effective, &saved) >= 0);
56 printf ("gids: real=%d effective=%d saved=%d",
57 (int) real, (int) effective, (int) saved);
59 printf ("gids: real=%d effective=%d",
60 (int) getgid (), (int) getegid ());
62 printf ("gids: real=%d",
68 show (const char *prefix)
70 printf ("%s ", prefix);
78 main (int argc, char *argv[])
87 int privileged_uid = geteuid ();
93 int privileged_gid = getegid ();
97 -v enables verbose output.
99 for (i = 1; i < argc; i++)
101 const char *arg = argv[i];
102 if (strcmp (arg, "-v") == 0)
106 for (i = 0; i < 3; i++)
109 show ("before droptemp:");
111 ASSERT (idpriv_temp_drop () == 0);
114 show ("privileged: ");
116 /* Verify that the privileges have really been dropped. */
118 if (geteuid () != uid)
122 if (getuid () != uid)
126 if (getegid () != gid)
130 if (getgid () != gid)
134 ASSERT (idpriv_temp_restore () == 0);
137 show ("unprivileged: ");
139 /* Verify that the privileges have really been acquired again. */
141 if (geteuid () != privileged_uid)
145 if (getuid () != uid)
149 if (getegid () != privileged_gid)
153 if (getgid () != gid)