This source file includes following definitions.
- idpriv_temp_drop
- idpriv_temp_restore
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 #include <config.h>
18
19 #include "idpriv.h"
20
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25
26
27 #if HAVE_GETUID
28 static int saved_uid = -1;
29 #endif
30 #if HAVE_GETGID
31 static int saved_gid = -1;
32 #endif
33
34 int
35 idpriv_temp_drop (void)
36 {
37 #if HAVE_GETEUID && HAVE_GETEGID && (HAVE_SETRESUID || HAVE_SETREUID) && (HAVE_SETRESGID || HAVE_SETREGID)
38 int uid = getuid ();
39 int gid = getgid ();
40
41
42 if (saved_uid == -1)
43 saved_uid = geteuid ();
44 if (saved_gid == -1)
45 saved_gid = getegid ();
46
47
48
49
50
51 # if HAVE_SETRESGID
52 if (setresgid (-1, gid, saved_gid) < 0)
53 return -1;
54 # else
55 if (setregid (-1, gid) < 0)
56 return -1;
57 # endif
58
59
60 # if HAVE_SETRESUID
61
62
63
64
65 if (setresuid (-1, uid, saved_uid) < 0)
66 return -1;
67 # else
68 if (setreuid (-1, uid) < 0)
69 return -1;
70 # endif
71
72
73
74
75
76
77 # if HAVE_GETRESUID
78 {
79 uid_t real;
80 uid_t effective;
81 uid_t saved;
82 if (getresuid (&real, &effective, &saved) < 0
83 || real != uid
84 || effective != uid
85 || saved != saved_uid)
86 abort ();
87 }
88 # else
89 # if HAVE_GETEUID
90 if (geteuid () != uid)
91 abort ();
92 # endif
93 if (getuid () != uid)
94 abort ();
95 # endif
96 # if HAVE_GETRESGID
97 {
98 uid_t real;
99 uid_t effective;
100 uid_t saved;
101 if (getresgid (&real, &effective, &saved) < 0
102 || real != gid
103 || effective != gid
104 || saved != saved_gid)
105 abort ();
106 }
107 # else
108 # if HAVE_GETEGID
109 if (getegid () != gid)
110 abort ();
111 # endif
112 if (getgid () != gid)
113 abort ();
114 # endif
115
116 return 0;
117 #else
118 errno = ENOSYS;
119 return -1;
120 #endif
121 }
122
123 int
124 idpriv_temp_restore (void)
125 {
126 #if HAVE_GETEUID && HAVE_GETEGID && (HAVE_SETRESUID || HAVE_SETREUID) && (HAVE_SETRESGID || HAVE_SETREGID)
127 int uid = getuid ();
128 int gid = getgid ();
129
130 if (saved_uid == -1 || saved_gid == -1)
131
132 abort ();
133
134
135
136
137
138 # if HAVE_SETRESUID
139
140
141
142
143 if (setresuid (-1, saved_uid, -1) < 0)
144 return -1;
145 # else
146 if (setreuid (-1, saved_uid) < 0)
147 return -1;
148 # endif
149
150
151 # if HAVE_SETRESGID
152 if (setresgid (-1, saved_gid, -1) < 0)
153 return -1;
154 # else
155 if (setregid (-1, saved_gid) < 0)
156 return -1;
157 # endif
158
159
160
161
162
163
164 # if HAVE_GETRESUID
165 {
166 uid_t real;
167 uid_t effective;
168 uid_t saved;
169 if (getresuid (&real, &effective, &saved) < 0
170 || real != uid
171 || effective != saved_uid
172 || saved != saved_uid)
173 abort ();
174 }
175 # else
176 # if HAVE_GETEUID
177 if (geteuid () != saved_uid)
178 abort ();
179 # endif
180 if (getuid () != uid)
181 abort ();
182 # endif
183 # if HAVE_GETRESGID
184 {
185 uid_t real;
186 uid_t effective;
187 uid_t saved;
188 if (getresgid (&real, &effective, &saved) < 0
189 || real != gid
190 || effective != saved_gid
191 || saved != saved_gid)
192 abort ();
193 }
194 # else
195 # if HAVE_GETEGID
196 if (getegid () != saved_gid)
197 abort ();
198 # endif
199 if (getgid () != gid)
200 abort ();
201 # endif
202
203 return 0;
204 #else
205 errno = ENOSYS;
206 return -1;
207 #endif
208 }