s4:samba-tool: Fix samba-tool fsmo --role=schema
[Samba/vl.git] / lib / util / setid.c
blobed8615517cc2f859416d60843e5f9e2101cb804c
1 /*
2 Unix SMB/CIFS implementation.
3 setXXid() functions for Samba.
4 Copyright (C) Jeremy Allison 2012
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #ifndef AUTOCONF_TEST
21 #include "replace.h"
22 #include "system/passwd.h"
23 #include "include/includes.h"
25 #ifdef UID_WRAPPER_REPLACE
27 #ifdef samba_seteuid
28 #undef samba_seteuid
29 #endif
31 #ifdef samba_setreuid
32 #undef samba_setreuid
33 #endif
35 #ifdef samba_setresuid
36 #undef samba_setresuid
37 #endif
39 #ifdef samba_setegid
40 #undef samba_setegid
41 #endif
43 #ifdef samba_setregid
44 #undef samba_setregid
45 #endif
47 #ifdef samba_setresgid
48 #undef samba_setresgid
49 #endif
51 #ifdef samba_setgroups
52 #undef samba_setgroups
53 #endif
55 /* uid_wrapper will have redefined these. */
56 int samba_setresuid(uid_t ruid, uid_t euid, uid_t suid);
57 int samba_setresgid(gid_t rgid, gid_t egid, gid_t sgid);
58 int samba_setreuid(uid_t ruid, uid_t euid);
59 int samba_setregid(gid_t rgid, gid_t egid);
60 int samba_seteuid(uid_t euid);
61 int samba_setegid(gid_t egid);
62 int samba_setuid(uid_t uid);
63 int samba_setgid(gid_t gid);
64 int samba_setuidx(int flags, uid_t uid);
65 int samba_setgidx(int flags, gid_t gid);
66 int samba_setgroups(size_t setlen, const gid_t *gidset);
67 #endif
69 #include "../lib/util/setid.h"
71 #else
73 /* Inside autoconf test. */
74 #if defined(HAVE_UNISTD_H)
75 #include <unistd.h>
76 #endif
77 #include <stdlib.h>
78 #include <stdio.h>
79 #include <sys/types.h>
80 #include <errno.h>
82 #ifdef HAVE_SYS_PRIV_H
83 #include <sys/priv.h>
84 #endif
85 #ifdef HAVE_SYS_ID_H
86 #include <sys/id.h>
87 #endif
89 /* autoconf tests don't include setid.h */
90 int samba_setresuid(uid_t ruid, uid_t euid, uid_t suid);
91 int samba_setresgid(gid_t rgid, gid_t egid, gid_t sgid);
92 int samba_setreuid(uid_t ruid, uid_t euid);
93 int samba_setregid(gid_t rgid, gid_t egid);
94 int samba_seteuid(uid_t euid);
95 int samba_setegid(gid_t egid);
96 int samba_setuid(uid_t uid);
97 int samba_setgid(gid_t gid);
98 int samba_setuidx(int flags, uid_t uid);
99 int samba_setgidx(int flags, gid_t gid);
100 int samba_setgroups(size_t setlen, const gid_t *gidset);
102 #endif
104 #if defined(USE_LINUX_THREAD_CREDENTIALS)
105 #if defined(HAVE_SYSCALL_H)
106 #include <syscall.h>
107 #endif
109 #if defined(HAVE_SYS_SYSCALL_H)
110 #include <sys/syscall.h>
111 #endif
113 /* Ensure we can't compile in a mixed syscall setup. */
114 #if !defined(USE_LINUX_32BIT_SYSCALLS)
115 #if defined(SYS_setresuid32) || defined(SYS_setresgid32) || defined(SYS_setreuid32) || defined(SYS_setregid32) || defined(SYS_setuid32) || defined(SYS_setgid32) || defined(SYS_setgroups32)
116 #error Mixture of 32-bit Linux system calls and 64-bit calls.
117 #endif
118 #endif
120 #endif
122 /* All the setXX[ug]id functions and setgroups Samba uses. */
123 int samba_setresuid(uid_t ruid, uid_t euid, uid_t suid)
125 #if defined(USE_LINUX_THREAD_CREDENTIALS)
126 #if defined(USE_LINUX_32BIT_SYSCALLS)
127 return syscall(SYS_setresuid32, ruid, euid, suid);
128 #else
129 return syscall(SYS_setresuid, ruid, euid, suid);
130 #endif
131 #elif defined(HAVE_SETRESUID)
132 return setresuid(ruid, euid, suid);
133 #else
134 errno = ENOSYS;
135 return -1;
136 #endif
139 int samba_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
141 #if defined(USE_LINUX_THREAD_CREDENTIALS)
142 #if defined(USE_LINUX_32BIT_SYSCALLS)
143 return syscall(SYS_setresgid32, rgid, egid, sgid);
144 #else
145 return syscall(SYS_setresgid, rgid, egid, sgid);
146 #endif
147 #elif defined(HAVE_SETRESGID)
148 return setresgid(rgid, egid, sgid);
149 #else
150 errno = ENOSYS;
151 return -1;
152 #endif
155 int samba_setreuid(uid_t ruid, uid_t euid)
157 #if defined(USE_LINUX_THREAD_CREDENTIALS)
158 #if defined(USE_LINUX_32BIT_SYSCALLS)
159 return syscall(SYS_setreuid32, ruid, euid);
160 #else
161 return syscall(SYS_setreuid, ruid, euid);
162 #endif
163 #elif defined(HAVE_SETREUID)
164 return setreuid(ruid, euid);
165 #else
166 errno = ENOSYS;
167 return -1;
168 #endif
171 int samba_setregid(gid_t rgid, gid_t egid)
173 #if defined(USE_LINUX_THREAD_CREDENTIALS)
174 #if defined(USE_LINUX_32BIT_SYSCALLS)
175 return syscall(SYS_setregid32, rgid, egid);
176 #else
177 return syscall(SYS_setregid, rgid, egid);
178 #endif
179 #elif defined(HAVE_SETREGID)
180 return setregid(rgid, egid);
181 #else
182 errno = ENOSYS;
183 return -1;
184 #endif
187 int samba_seteuid(uid_t euid)
189 #if defined(USE_LINUX_THREAD_CREDENTIALS)
190 #if defined(USE_LINUX_32BIT_SYSCALLS)
191 /* seteuid is not a separate system call. */
192 return syscall(SYS_setresuid32, -1, euid, -1);
193 #else
194 /* seteuid is not a separate system call. */
195 return syscall(SYS_setresuid, -1, euid, -1);
196 #endif
197 #elif defined(HAVE_SETEUID)
198 return seteuid(euid);
199 #else
200 errno = ENOSYS;
201 return -1;
202 #endif
205 int samba_setegid(gid_t egid)
207 #if defined(USE_LINUX_THREAD_CREDENTIALS)
208 #if defined(USE_LINUX_32BIT_SYSCALLS)
209 /* setegid is not a separate system call. */
210 return syscall(SYS_setresgid32, -1, egid, -1);
211 #else
212 /* setegid is not a separate system call. */
213 return syscall(SYS_setresgid, -1, egid, -1);
214 #endif
215 #elif defined(HAVE_SETEGID)
216 return setegid(egid);
217 #else
218 errno = ENOSYS;
219 return -1;
220 #endif
223 int samba_setuid(uid_t uid)
225 #if defined(USE_LINUX_THREAD_CREDENTIALS)
226 #if defined(USE_LINUX_32BIT_SYSCALLS)
227 return syscall(SYS_setuid32, uid);
228 #else
229 return syscall(SYS_setuid, uid);
230 #endif
231 #elif defined(HAVE_SETUID)
232 return setuid(uid);
233 #else
234 errno = ENOSYS;
235 return -1;
236 #endif
239 int samba_setgid(gid_t gid)
241 #if defined(USE_LINUX_THREAD_CREDENTIALS)
242 #if defined(USE_LINUX_32BIT_SYSCALLS)
243 return syscall(SYS_setgid32, gid);
244 #else
245 return syscall(SYS_setgid, gid);
246 #endif
247 #elif defined(HAVE_SETGID)
248 return setgid(gid);
249 #else
250 errno = ENOSYS;
251 return -1;
252 #endif
255 int samba_setuidx(int flags, uid_t uid)
257 #if defined(HAVE_SETUIDX)
258 return setuidx(flags, uid);
259 #else
260 /* USE_LINUX_THREAD_CREDENTIALS doesn't have this. */
261 errno = ENOSYS;
262 return -1;
263 #endif
266 int samba_setgidx(int flags, gid_t gid)
268 #if defined(HAVE_SETGIDX)
269 return setgidx(flags, gid);
270 #else
271 /* USE_LINUX_THREAD_CREDENTIALS doesn't have this. */
272 errno = ENOSYS;
273 return -1;
274 #endif
277 int samba_setgroups(size_t setlen, const gid_t *gidset)
279 #if defined(USE_LINUX_THREAD_CREDENTIALS)
280 #if defined(USE_LINUX_32BIT_SYSCALLS)
281 return syscall(SYS_setgroups32, setlen, gidset);
282 #else
283 return syscall(SYS_setgroups, setlen, gidset);
284 #endif
285 #elif defined(HAVE_SETGROUPS)
286 return setgroups(setlen, gidset);
287 #else
288 errno = ENOSYS;
289 return -1;
290 #endif