Use spaces for indentation, not tabs.
[gnulib.git] / tests / test-idpriv-droptemp.c
1 /* Test of dropping uid/gid privileges of the current process temporarily.
2    Copyright (C) 2009 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>.  */
16
17 #include <config.h>
18
19 #include "idpriv.h"
20
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #define ASSERT(expr) \
28   do                                                                         \
29     {                                                                        \
30       if (!(expr))                                                           \
31         {                                                                    \
32           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
33           fflush (stderr);                                                   \
34           abort ();                                                          \
35         }                                                                    \
36     }                                                                        \
37   while (0)
38
39 static void
40 show_uids ()
41 {
42 #if HAVE_GETRESUID /* glibc, FreeBSD, OpenBSD, HP-UX */
43   uid_t real;
44   uid_t effective;
45   uid_t saved;
46   ASSERT (getresuid (&real, &effective, &saved) >= 0);
47   printf ("uids: real=%d effective=%d saved=%d",
48           (int) real, (int) effective, (int) saved);
49 #elif HAVE_GETEUID
50   printf ("uids: real=%d effective=%d",
51           (int) getuid (), (int) geteuid());
52 #elif HAVE_GETUID
53   printf ("uids: real=%d",
54           (int) getuid ());
55 #endif
56 }
57
58 static void
59 show_gids ()
60 {
61 #if HAVE_GETRESGID /* glibc, FreeBSD, OpenBSD, HP-UX */
62   gid_t real;
63   gid_t effective;
64   gid_t saved;
65   ASSERT (getresgid (&real, &effective, &saved) >= 0);
66   printf ("gids: real=%d effective=%d saved=%d",
67           (int) real, (int) effective, (int) saved);
68 #elif HAVE_GETEGID
69   printf ("gids: real=%d effective=%d",
70           (int) getgid (), (int) getegid());
71 #elif HAVE_GETGID
72   printf ("gids: real=%d",
73           (int) getgid ());
74 #endif
75 }
76
77 static void
78 show (const char *prefix)
79 {
80   printf ("%s  ", prefix);
81   show_uids ();
82   printf ("  ");
83   show_gids ();
84   printf ("\n");
85 }
86
87 int
88 main (int argc, char *argv[])
89 {
90   bool verbose = false;
91   int i;
92
93 #if HAVE_GETUID
94   int uid = getuid ();
95 #endif
96 #if HAVE_GETEUID
97   int privileged_uid = geteuid ();
98 #endif
99 #if HAVE_GETGID
100   int gid = getgid ();
101 #endif
102 #if HAVE_GETEGID
103   int privileged_gid = getegid ();
104 #endif
105
106   /* Parse arguments.
107      -v  enables verbose output.
108    */
109   for (i = 1; i < argc; i++)
110     {
111       const char *arg = argv[i];
112       if (strcmp (arg, "-v") == 0)
113         verbose = true;
114     }
115
116   for (i = 0; i < 3; i++)
117     {
118       if (verbose)
119         show ("before droptemp:");
120
121       ASSERT (idpriv_temp_drop () == 0);
122
123       if (verbose)
124         show ("privileged:     ");
125
126       /* Verify that the privileges have really been dropped.  */
127 #if HAVE_GETEUID
128       if (geteuid () != uid)
129         abort ();
130 #endif
131 #if HAVE_GETUID
132       if (getuid () != uid)
133         abort ();
134 #endif
135 #if HAVE_GETEGID
136       if (getegid () != gid)
137         abort ();
138 #endif
139 #if HAVE_GETGID
140       if (getgid () != gid)
141         abort ();
142 #endif
143
144       ASSERT (idpriv_temp_restore () == 0);
145
146       if (verbose)
147         show ("unprivileged:   ");
148
149       /* Verify that the privileges have really been acquired again.  */
150 #if HAVE_GETEUID
151       if (geteuid () != privileged_uid)
152         abort ();
153 #endif
154 #if HAVE_GETUID
155       if (getuid () != uid)
156         abort ();
157 #endif
158 #if HAVE_GETEGID
159       if (getegid () != privileged_gid)
160         abort ();
161 #endif
162 #if HAVE_GETGID
163       if (getgid () != gid)
164         abort ();
165 #endif
166     }
167
168
169   return 0;
170 }