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 #include "../lib/util/setid.h"
28 /* Inside autoconf test. */
29 #if defined(HAVE_UNISTD_H)
34 #include <sys/types.h>
37 #if defined(HAVE_UNISTD_H)
40 #ifdef HAVE_SYS_PRIV_H
50 /* autoconf tests don't include setid.h */
51 int samba_setresuid(uid_t ruid
, uid_t euid
, uid_t suid
);
52 int samba_setresgid(gid_t rgid
, gid_t egid
, gid_t sgid
);
53 int samba_setreuid(uid_t ruid
, uid_t euid
);
54 int samba_setregid(gid_t rgid
, gid_t egid
);
55 int samba_seteuid(uid_t euid
);
56 int samba_setegid(gid_t egid
);
57 int samba_setuid(uid_t uid
);
58 int samba_setgid(gid_t gid
);
59 int samba_setuidx(int flags
, uid_t uid
);
60 int samba_setgidx(int flags
, gid_t gid
);
61 int samba_setgroups(size_t setlen
, const gid_t
*gidset
);
65 #if defined(HAVE_LINUX_THREAD_CREDENTIALS)
66 #if defined(HAVE_SYSCALL_H)
70 #if defined(HAVE_SYS_SYSCALL_H)
71 #include <sys/syscall.h>
74 /* Ensure we can't compile in a mixed syscall setup. */
75 #if !defined(USE_LINUX_32BIT_SYSCALLS)
76 #if defined(SYS_setresuid32) || defined(SYS_setresgid32) || defined(SYS_setreuid32) || defined(SYS_setregid32) || defined(SYS_setuid32) || defined(SYS_setgid32) || defined(SYS_setgroups32)
77 #error Mixture of 32-bit Linux system calls and 64-bit calls.
83 /* All the setXX[ug]id functions and setgroups Samba uses. */
84 int samba_setresuid(uid_t ruid
, uid_t euid
, uid_t suid
)
86 #if defined(HAVE_LINUX_THREAD_CREDENTIALS)
87 #if defined(USE_LINUX_32BIT_SYSCALLS)
88 return syscall(SYS_setresuid32
, ruid
, euid
, suid
);
90 return syscall(SYS_setresuid
, ruid
, euid
, suid
);
92 #elif defined(HAVE_SETRESUID)
93 return setresuid(ruid
, euid
, suid
);
100 int samba_setresgid(gid_t rgid
, gid_t egid
, gid_t sgid
)
102 #if defined(HAVE_LINUX_THREAD_CREDENTIALS)
103 #if defined(USE_LINUX_32BIT_SYSCALLS)
104 return syscall(SYS_setresgid32
, rgid
, egid
, sgid
);
106 return syscall(SYS_setresgid
, rgid
, egid
, sgid
);
108 #elif defined(HAVE_SETRESGID)
109 return setresgid(rgid
, egid
, sgid
);
116 int samba_setreuid(uid_t ruid
, uid_t euid
)
118 #if defined(HAVE_LINUX_THREAD_CREDENTIALS)
119 #if defined(USE_LINUX_32BIT_SYSCALLS)
120 return syscall(SYS_setreuid32
, ruid
, euid
);
122 return syscall(SYS_setreuid
, ruid
, euid
);
124 #elif defined(HAVE_SETREUID)
125 return setreuid(ruid
, euid
);
132 int samba_setregid(gid_t rgid
, gid_t egid
)
134 #if defined(HAVE_LINUX_THREAD_CREDENTIALS)
135 #if defined(USE_LINUX_32BIT_SYSCALLS)
136 return syscall(SYS_setregid32
, rgid
, egid
);
138 return syscall(SYS_setregid
, rgid
, egid
);
140 #elif defined(HAVE_SETREGID)
141 return setregid(rgid
, egid
);
148 int samba_seteuid(uid_t euid
)
150 #if defined(HAVE_LINUX_THREAD_CREDENTIALS)
151 #if defined(USE_LINUX_32BIT_SYSCALLS)
152 /* seteuid is not a separate system call. */
153 return syscall(SYS_setresuid32
, -1, euid
, -1);
155 /* seteuid is not a separate system call. */
156 return syscall(SYS_setresuid
, -1, euid
, -1);
158 #elif defined(HAVE_SETEUID)
159 return seteuid(euid
);
166 int samba_setegid(gid_t egid
)
168 #if defined(HAVE_LINUX_THREAD_CREDENTIALS)
169 #if defined(USE_LINUX_32BIT_SYSCALLS)
170 /* setegid is not a separate system call. */
171 return syscall(SYS_setresgid32
, -1, egid
, -1);
173 /* setegid is not a separate system call. */
174 return syscall(SYS_setresgid
, -1, egid
, -1);
176 #elif defined(HAVE_SETEGID)
177 return setegid(egid
);
184 int samba_setuid(uid_t uid
)
186 #if defined(HAVE_LINUX_THREAD_CREDENTIALS)
187 #if defined(USE_LINUX_32BIT_SYSCALLS)
188 return syscall(SYS_setuid32
, uid
);
190 return syscall(SYS_setuid
, uid
);
192 #elif defined(HAVE_SETUID)
200 int samba_setgid(gid_t gid
)
202 #if defined(HAVE_LINUX_THREAD_CREDENTIALS)
203 #if defined(USE_LINUX_32BIT_SYSCALLS)
204 return syscall(SYS_setgid32
, gid
);
206 return syscall(SYS_setgid
, gid
);
208 #elif defined(HAVE_SETGID)
216 int samba_setuidx(int flags
, uid_t uid
)
218 #if defined(HAVE_SETUIDX)
219 return setuidx(flags
, uid
);
221 /* HAVE_LINUX_THREAD_CREDENTIALS doesn't have this. */
227 int samba_setgidx(int flags
, gid_t gid
)
229 #if defined(HAVE_SETGIDX)
230 return setgidx(flags
, gid
);
232 /* HAVE_LINUX_THREAD_CREDENTIALS doesn't have this. */
238 int samba_setgroups(size_t setlen
, const gid_t
*gidset
)
240 #if defined(HAVE_LINUX_THREAD_CREDENTIALS)
241 #if defined(USE_LINUX_32BIT_SYSCALLS)
242 return syscall(SYS_setgroups32
, setlen
, gidset
);
244 return syscall(SYS_setgroups
, setlen
, gidset
);
246 #elif defined(HAVE_SETGROUPS)
247 return setgroups(setlen
, gidset
);