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"
24 #ifdef UID_WRAPPER_REPLACE
34 #ifdef samba_setresuid
35 #undef samba_setresuid
46 #ifdef samba_setresgid
47 #undef samba_setresgid
50 #ifdef samba_setgroups
51 #undef samba_setgroups
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
);
68 #include "../lib/util/setid.h"
72 /* Inside autoconf test. */
73 #if defined(HAVE_UNISTD_H)
78 #include <sys/types.h>
81 #ifdef HAVE_SYS_PRIV_H
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
);
103 #if defined(USE_LINUX_THREAD_CREDENTIALS)
104 #if defined(HAVE_SYSCALL_H)
108 #if defined(HAVE_SYS_SYSCALL_H)
109 #include <sys/syscall.h>
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.
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
);
128 return syscall(SYS_setresuid
, ruid
, euid
, suid
);
130 #elif defined(HAVE_SETRESUID)
131 return setresuid(ruid
, euid
, suid
);
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
);
144 return syscall(SYS_setresgid
, rgid
, egid
, sgid
);
146 #elif defined(HAVE_SETRESGID)
147 return setresgid(rgid
, egid
, sgid
);
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
);
160 return syscall(SYS_setreuid
, ruid
, euid
);
162 #elif defined(HAVE_SETREUID)
163 return setreuid(ruid
, euid
);
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
);
176 return syscall(SYS_setregid
, rgid
, egid
);
178 #elif defined(HAVE_SETREGID)
179 return setregid(rgid
, egid
);
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);
193 /* seteuid is not a separate system call. */
194 return syscall(SYS_setresuid
, -1, euid
, -1);
196 #elif defined(HAVE_SETEUID)
197 return seteuid(euid
);
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);
211 /* setegid is not a separate system call. */
212 return syscall(SYS_setresgid
, -1, egid
, -1);
214 #elif defined(HAVE_SETEGID)
215 return setegid(egid
);
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
);
228 return syscall(SYS_setuid
, uid
);
230 #elif defined(HAVE_SETUID)
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
);
244 return syscall(SYS_setgid
, gid
);
246 #elif defined(HAVE_SETGID)
254 int samba_setuidx(int flags
, uid_t uid
)
256 #if defined(HAVE_SETUIDX)
257 return setuidx(flags
, uid
);
259 /* USE_LINUX_THREAD_CREDENTIALS doesn't have this. */
265 int samba_setgidx(int flags
, gid_t gid
)
267 #if defined(HAVE_SETGIDX)
268 return setgidx(flags
, gid
);
270 /* USE_LINUX_THREAD_CREDENTIALS doesn't have this. */
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
);
282 return syscall(SYS_setgroups
, setlen
, gidset
);
284 #elif defined(HAVE_SETGROUPS)
285 return setgroups(setlen
, gidset
);