error: Avoid "function declaration isn't a prototype" warning.
[gnulib.git] / tests / test-idpriv-droptemp.c
blob22995c780bd4e32d957c76651efd8e49a8485dcc
1 /* Test of dropping uid/gid privileges of the current process temporarily.
2 Copyright (C) 2009-2017 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/>. */
17 #include <config.h>
19 #include "idpriv.h"
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
27 #include "macros.h"
29 static void
30 show_uids ()
32 #if HAVE_GETRESUID /* glibc, FreeBSD, OpenBSD, HP-UX */
33 uid_t real;
34 uid_t effective;
35 uid_t saved;
36 ASSERT (getresuid (&real, &effective, &saved) >= 0);
37 printf ("uids: real=%d effective=%d saved=%d",
38 (int) real, (int) effective, (int) saved);
39 #elif HAVE_GETEUID
40 printf ("uids: real=%d effective=%d",
41 (int) getuid (), (int) geteuid ());
42 #elif HAVE_GETUID
43 printf ("uids: real=%d",
44 (int) getuid ());
45 #endif
48 static void
49 show_gids ()
51 #if HAVE_GETRESGID /* glibc, FreeBSD, OpenBSD, HP-UX */
52 gid_t real;
53 gid_t effective;
54 gid_t saved;
55 ASSERT (getresgid (&real, &effective, &saved) >= 0);
56 printf ("gids: real=%d effective=%d saved=%d",
57 (int) real, (int) effective, (int) saved);
58 #elif HAVE_GETEGID
59 printf ("gids: real=%d effective=%d",
60 (int) getgid (), (int) getegid ());
61 #elif HAVE_GETGID
62 printf ("gids: real=%d",
63 (int) getgid ());
64 #endif
67 static void
68 show (const char *prefix)
70 printf ("%s ", prefix);
71 show_uids ();
72 printf (" ");
73 show_gids ();
74 printf ("\n");
77 int
78 main (int argc, char *argv[])
80 bool verbose = false;
81 int i;
83 #if HAVE_GETUID
84 int uid = getuid ();
85 #endif
86 #if HAVE_GETEUID
87 int privileged_uid = geteuid ();
88 #endif
89 #if HAVE_GETGID
90 int gid = getgid ();
91 #endif
92 #if HAVE_GETEGID
93 int privileged_gid = getegid ();
94 #endif
96 /* Parse arguments.
97 -v enables verbose output.
99 for (i = 1; i < argc; i++)
101 const char *arg = argv[i];
102 if (strcmp (arg, "-v") == 0)
103 verbose = true;
106 for (i = 0; i < 3; i++)
108 if (verbose)
109 show ("before droptemp:");
111 ASSERT (idpriv_temp_drop () == 0);
113 if (verbose)
114 show ("privileged: ");
116 /* Verify that the privileges have really been dropped. */
117 #if HAVE_GETEUID
118 if (geteuid () != uid)
119 abort ();
120 #endif
121 #if HAVE_GETUID
122 if (getuid () != uid)
123 abort ();
124 #endif
125 #if HAVE_GETEGID
126 if (getegid () != gid)
127 abort ();
128 #endif
129 #if HAVE_GETGID
130 if (getgid () != gid)
131 abort ();
132 #endif
134 ASSERT (idpriv_temp_restore () == 0);
136 if (verbose)
137 show ("unprivileged: ");
139 /* Verify that the privileges have really been acquired again. */
140 #if HAVE_GETEUID
141 if (geteuid () != privileged_uid)
142 abort ();
143 #endif
144 #if HAVE_GETUID
145 if (getuid () != uid)
146 abort ();
147 #endif
148 #if HAVE_GETEGID
149 if (getegid () != privileged_gid)
150 abort ();
151 #endif
152 #if HAVE_GETGID
153 if (getgid () != gid)
154 abort ();
155 #endif
159 return 0;