s3:smbd: remove code duplication in smb2_create_send()
[Samba/gebeck_regimport.git] / lib / util / setid.c
blobc5ee64484cc81cb5c8ec0ba259e522f60382ef6b
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"
24 #ifdef UID_WRAPPER_REPLACE
26 #ifdef samba_seteuid
27 #undef samba_seteuid
28 #endif
30 #ifdef samba_setreuid
31 #undef samba_setreuid
32 #endif
34 #ifdef samba_setresuid
35 #undef samba_setresuid
36 #endif
38 #ifdef samba_setegid
39 #undef samba_setegid
40 #endif
42 #ifdef samba_setregid
43 #undef samba_setregid
44 #endif
46 #ifdef samba_setresgid
47 #undef samba_setresgid
48 #endif
50 #ifdef samba_setgroups
51 #undef samba_setgroups
52 #endif
54 /* uid_wrapper will have redefined these. */
55 int samba_setresuid(uid_t ruid, uid_t euid, uid_t suid);
56 int samba_setresgid(gid_t rgid, gid_t egid, gid_t sgid);
57 int samba_setreuid(uid_t ruid, uid_t euid);
58 int samba_setregid(gid_t rgid, gid_t egid);
59 int samba_seteuid(uid_t euid);
60 int samba_setegid(gid_t egid);
61 int samba_setuid(uid_t uid);
62 int samba_setgid(gid_t gid);
63 int samba_setuidx(int flags, uid_t uid);
64 int samba_setgidx(int flags, gid_t gid);
65 int samba_setgroups(size_t setlen, const gid_t *gidset);
66 #endif
68 #include "../lib/util/setid.h"
70 #else
72 /* Inside autoconf test. */
73 #if defined(HAVE_UNISTD_H)
74 #include <unistd.h>
75 #endif
76 #include <stdlib.h>
77 #include <stdio.h>
78 #include <sys/types.h>
79 #include <errno.h>
81 #ifdef HAVE_SYS_PRIV_H
82 #include <sys/priv.h>
83 #endif
84 #ifdef HAVE_SYS_ID_H
85 #include <sys/id.h>
86 #endif
88 /* autoconf tests don't include setid.h */
89 int samba_setresuid(uid_t ruid, uid_t euid, uid_t suid);
90 int samba_setresgid(gid_t rgid, gid_t egid, gid_t sgid);
91 int samba_setreuid(uid_t ruid, uid_t euid);
92 int samba_setregid(gid_t rgid, gid_t egid);
93 int samba_seteuid(uid_t euid);
94 int samba_setegid(gid_t egid);
95 int samba_setuid(uid_t uid);
96 int samba_setgid(gid_t gid);
97 int samba_setuidx(int flags, uid_t uid);
98 int samba_setgidx(int flags, gid_t gid);
99 int samba_setgroups(size_t setlen, const gid_t *gidset);
101 #endif
103 #if defined(USE_LINUX_THREAD_CREDENTIALS)
104 #if defined(HAVE_SYSCALL_H)
105 #include <syscall.h>
106 #endif
108 #if defined(HAVE_SYS_SYSCALL_H)
109 #include <sys/syscall.h>
110 #endif
112 /* Ensure we can't compile in a mixed syscall setup. */
113 #if !defined(USE_LINUX_32BIT_SYSCALLS)
114 #if defined(SYS_setresuid32) || defined(SYS_setresgid32) || defined(SYS_setreuid32) || defined(SYS_setregid32) || defined(SYS_setuid32) || defined(SYS_setgid32) || defined(SYS_setgroups32)
115 #error Mixture of 32-bit Linux system calls and 64-bit calls.
116 #endif
117 #endif
119 #endif
121 /* All the setXX[ug]id functions and setgroups Samba uses. */
122 int samba_setresuid(uid_t ruid, uid_t euid, uid_t suid)
124 #if defined(USE_LINUX_THREAD_CREDENTIALS)
125 #if defined(USE_LINUX_32BIT_SYSCALLS)
126 return syscall(SYS_setresuid32, ruid, euid, suid);
127 #else
128 return syscall(SYS_setresuid, ruid, euid, suid);
129 #endif
130 #elif defined(HAVE_SETRESUID)
131 return setresuid(ruid, euid, suid);
132 #else
133 errno = ENOSYS;
134 return -1;
135 #endif
138 int samba_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
140 #if defined(USE_LINUX_THREAD_CREDENTIALS)
141 #if defined(USE_LINUX_32BIT_SYSCALLS)
142 return syscall(SYS_setresgid32, rgid, egid, sgid);
143 #else
144 return syscall(SYS_setresgid, rgid, egid, sgid);
145 #endif
146 #elif defined(HAVE_SETRESGID)
147 return setresgid(rgid, egid, sgid);
148 #else
149 errno = ENOSYS;
150 return -1;
151 #endif
154 int samba_setreuid(uid_t ruid, uid_t euid)
156 #if defined(USE_LINUX_THREAD_CREDENTIALS)
157 #if defined(USE_LINUX_32BIT_SYSCALLS)
158 return syscall(SYS_setreuid32, ruid, euid);
159 #else
160 return syscall(SYS_setreuid, ruid, euid);
161 #endif
162 #elif defined(HAVE_SETREUID)
163 return setreuid(ruid, euid);
164 #else
165 errno = ENOSYS;
166 return -1;
167 #endif
170 int samba_setregid(gid_t rgid, gid_t egid)
172 #if defined(USE_LINUX_THREAD_CREDENTIALS)
173 #if defined(USE_LINUX_32BIT_SYSCALLS)
174 return syscall(SYS_setregid32, rgid, egid);
175 #else
176 return syscall(SYS_setregid, rgid, egid);
177 #endif
178 #elif defined(HAVE_SETREGID)
179 return setregid(rgid, egid);
180 #else
181 errno = ENOSYS;
182 return -1;
183 #endif
186 int samba_seteuid(uid_t euid)
188 #if defined(USE_LINUX_THREAD_CREDENTIALS)
189 #if defined(USE_LINUX_32BIT_SYSCALLS)
190 /* seteuid is not a separate system call. */
191 return syscall(SYS_setresuid32, -1, euid, -1);
192 #else
193 /* seteuid is not a separate system call. */
194 return syscall(SYS_setresuid, -1, euid, -1);
195 #endif
196 #elif defined(HAVE_SETEUID)
197 return seteuid(euid);
198 #else
199 errno = ENOSYS;
200 return -1;
201 #endif
204 int samba_setegid(gid_t egid)
206 #if defined(USE_LINUX_THREAD_CREDENTIALS)
207 #if defined(USE_LINUX_32BIT_SYSCALLS)
208 /* setegid is not a separate system call. */
209 return syscall(SYS_setresgid32, -1, egid, -1);
210 #else
211 /* setegid is not a separate system call. */
212 return syscall(SYS_setresgid, -1, egid, -1);
213 #endif
214 #elif defined(HAVE_SETEGID)
215 return setegid(egid);
216 #else
217 errno = ENOSYS;
218 return -1;
219 #endif
222 int samba_setuid(uid_t uid)
224 #if defined(USE_LINUX_THREAD_CREDENTIALS)
225 #if defined(USE_LINUX_32BIT_SYSCALLS)
226 return syscall(SYS_setuid32, uid);
227 #else
228 return syscall(SYS_setuid, uid);
229 #endif
230 #elif defined(HAVE_SETUID)
231 return setuid(uid);
232 #else
233 errno = ENOSYS;
234 return -1;
235 #endif
238 int samba_setgid(gid_t gid)
240 #if defined(USE_LINUX_THREAD_CREDENTIALS)
241 #if defined(USE_LINUX_32BIT_SYSCALLS)
242 return syscall(SYS_setgid32, gid);
243 #else
244 return syscall(SYS_setgid, gid);
245 #endif
246 #elif defined(HAVE_SETGID)
247 return setgid(gid);
248 #else
249 errno = ENOSYS;
250 return -1;
251 #endif
254 int samba_setuidx(int flags, uid_t uid)
256 #if defined(HAVE_SETUIDX)
257 return setuidx(flags, uid);
258 #else
259 /* USE_LINUX_THREAD_CREDENTIALS doesn't have this. */
260 errno = ENOSYS;
261 return -1;
262 #endif
265 int samba_setgidx(int flags, gid_t gid)
267 #if defined(HAVE_SETGIDX)
268 return setgidx(flags, gid);
269 #else
270 /* USE_LINUX_THREAD_CREDENTIALS doesn't have this. */
271 errno = ENOSYS;
272 return -1;
273 #endif
276 int samba_setgroups(size_t setlen, const gid_t *gidset)
278 #if defined(USE_LINUX_THREAD_CREDENTIALS)
279 #if defined(USE_LINUX_32BIT_SYSCALLS)
280 return syscall(SYS_setgroups32, setlen, gidset);
281 #else
282 return syscall(SYS_setgroups, setlen, gidset);
283 #endif
284 #elif defined(HAVE_SETGROUPS)
285 return setgroups(setlen, gidset);
286 #else
287 errno = ENOSYS;
288 return -1;
289 #endif