1 /* Test of dropping uid/gid privileges of the current process temporarily.
2 Copyright (C) 2009-2025 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 <https://www.gnu.org/licenses/>. */
31 #if HAVE_GETRESUID /* glibc, FreeBSD, OpenBSD, HP-UX */
35 ASSERT (getresuid (&real
, &effective
, &saved
) >= 0);
36 printf ("uids: real=%d effective=%d saved=%d",
37 (int) real
, (int) effective
, (int) saved
);
39 printf ("uids: real=%d effective=%d",
40 (int) getuid (), (int) geteuid ());
42 printf ("uids: real=%d",
50 #if HAVE_GETRESGID /* glibc, FreeBSD, OpenBSD, HP-UX */
54 ASSERT (getresgid (&real
, &effective
, &saved
) >= 0);
55 printf ("gids: real=%d effective=%d saved=%d",
56 (int) real
, (int) effective
, (int) saved
);
58 printf ("gids: real=%d effective=%d",
59 (int) getgid (), (int) getegid ());
61 printf ("gids: real=%d",
67 show (const char *prefix
)
69 printf ("%s ", prefix
);
77 main (int argc
, char *argv
[])
86 int privileged_uid
= geteuid ();
92 int privileged_gid
= getegid ();
96 -v enables verbose output.
98 for (i
= 1; i
< argc
; i
++)
100 const char *arg
= argv
[i
];
101 if (strcmp (arg
, "-v") == 0)
105 for (i
= 0; i
< 3; i
++)
108 show ("before droptemp:");
110 ASSERT (idpriv_temp_drop () == 0);
113 show ("privileged: ");
115 /* Verify that the privileges have really been dropped. */
117 if (geteuid () != uid
)
121 if (getuid () != uid
)
125 if (getegid () != gid
)
129 if (getgid () != gid
)
133 ASSERT (idpriv_temp_restore () == 0);
136 show ("unprivileged: ");
138 /* Verify that the privileges have really been acquired again. */
140 if (geteuid () != privileged_uid
)
144 if (getuid () != uid
)
148 if (getegid () != privileged_gid
)
152 if (getgid () != gid
)
158 return test_exit_status
;