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/>.
22 #include "system/passwd.h"
23 #include "include/includes.h"
25 #ifdef UID_WRAPPER_REPLACE
35 #ifdef samba_setresuid
36 #undef samba_setresuid
47 #ifdef samba_setresgid
48 #undef samba_setresgid
51 #ifdef samba_setgroups
52 #undef samba_setgroups
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
);
69 #include "../lib/util/setid.h"
73 /* Inside autoconf test. */
74 #if defined(HAVE_UNISTD_H)
79 #include <sys/types.h>
82 #ifdef HAVE_SYS_PRIV_H
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
);
104 #if defined(USE_LINUX_THREAD_CREDENTIALS)
105 #if defined(HAVE_SYSCALL_H)
109 #if defined(HAVE_SYS_SYSCALL_H)
110 #include <sys/syscall.h>
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.
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
);
129 return syscall(SYS_setresuid
, ruid
, euid
, suid
);
131 #elif defined(HAVE_SETRESUID)
132 return setresuid(ruid
, euid
, suid
);
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
);
145 return syscall(SYS_setresgid
, rgid
, egid
, sgid
);
147 #elif defined(HAVE_SETRESGID)
148 return setresgid(rgid
, egid
, sgid
);
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
);
161 return syscall(SYS_setreuid
, ruid
, euid
);
163 #elif defined(HAVE_SETREUID)
164 return setreuid(ruid
, euid
);
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
);
177 return syscall(SYS_setregid
, rgid
, egid
);
179 #elif defined(HAVE_SETREGID)
180 return setregid(rgid
, egid
);
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);
194 /* seteuid is not a separate system call. */
195 return syscall(SYS_setresuid
, -1, euid
, -1);
197 #elif defined(HAVE_SETEUID)
198 return seteuid(euid
);
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);
212 /* setegid is not a separate system call. */
213 return syscall(SYS_setresgid
, -1, egid
, -1);
215 #elif defined(HAVE_SETEGID)
216 return setegid(egid
);
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
);
229 return syscall(SYS_setuid
, uid
);
231 #elif defined(HAVE_SETUID)
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
);
245 return syscall(SYS_setgid
, gid
);
247 #elif defined(HAVE_SETGID)
255 int samba_setuidx(int flags
, uid_t uid
)
257 #if defined(HAVE_SETUIDX)
258 return setuidx(flags
, uid
);
260 /* USE_LINUX_THREAD_CREDENTIALS doesn't have this. */
266 int samba_setgidx(int flags
, gid_t gid
)
268 #if defined(HAVE_SETGIDX)
269 return setgidx(flags
, gid
);
271 /* USE_LINUX_THREAD_CREDENTIALS doesn't have this. */
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
);
283 return syscall(SYS_setgroups
, setlen
, gidset
);
285 #elif defined(HAVE_SETGROUPS)
286 return setgroups(setlen
, gidset
);