Use spaces for indentation, not tabs.
[gnulib.git] / tests / test-idpriv-drop.c
1 /* Test of dropping uid/gid privileges of the current process permanently.
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_GETGID
97   int gid = getgid ();
98 #endif
99
100   /* Parse arguments.
101      -v  enables verbose output.
102    */
103   for (i = 1; i < argc; i++)
104     {
105       const char *arg = argv[i];
106       if (strcmp (arg, "-v") == 0)
107         verbose = true;
108     }
109
110   if (verbose)
111     show ("before drop:");
112
113   ASSERT (idpriv_drop () == 0);
114
115   if (verbose)
116     show ("after drop: ");
117
118   /* Verify that the privileges have really been dropped.  */
119 #if HAVE_GETRESUID /* glibc, FreeBSD, OpenBSD, HP-UX */
120   {
121     uid_t real;
122     uid_t effective;
123     uid_t saved;
124     if (getresuid (&real, &effective, &saved) < 0
125         || real != uid
126         || effective != uid
127         || saved != uid)
128       abort ();
129   }
130 #else
131 # if HAVE_GETEUID
132   if (geteuid () != uid)
133     abort ();
134 # endif
135 # if HAVE_GETUID
136   if (getuid () != uid)
137     abort ();
138 # endif
139 #endif
140 #if HAVE_GETRESGID /* glibc, FreeBSD, OpenBSD, HP-UX */
141   {
142     gid_t real;
143     gid_t effective;
144     gid_t saved;
145     if (getresgid (&real, &effective, &saved) < 0
146         || real != gid
147         || effective != gid
148         || saved != gid)
149       abort ();
150   }
151 #else
152 # if HAVE_GETEGID
153   if (getegid () != gid)
154     abort ();
155 # endif
156 # if HAVE_GETGID
157   if (getgid () != gid)
158     abort ();
159 # endif
160 #endif
161
162   return 0;
163 }