1 /* Test of dropping uid/gid privileges of the current process permanently.
2 Copyright (C) 2009-2024 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
[])
90 -v enables verbose output.
92 for (i
= 1; i
< argc
; i
++)
94 const char *arg
= argv
[i
];
95 if (strcmp (arg
, "-v") == 0)
100 show ("before drop:");
102 ASSERT (idpriv_drop () == 0);
105 show ("after drop: ");
107 /* Verify that the privileges have really been dropped. */
108 #if HAVE_GETRESUID /* glibc, FreeBSD, OpenBSD, HP-UX */
113 if (getresuid (&real
, &effective
, &saved
) < 0
121 if (geteuid () != uid
)
125 if (getuid () != uid
)
129 #if HAVE_GETRESGID /* glibc, FreeBSD, OpenBSD, HP-UX */
134 if (getresgid (&real
, &effective
, &saved
) < 0
142 if (getegid () != gid
)
146 if (getgid () != gid
)
151 return test_exit_status
;