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 #ifdef HAVE_SYS_PRIV_H
44 /* autoconf tests don't include setid.h */
45 int samba_setresuid(uid_t ruid
, uid_t euid
, uid_t suid
);
46 int samba_setresgid(gid_t rgid
, gid_t egid
, gid_t sgid
);
47 int samba_setreuid(uid_t ruid
, uid_t euid
);
48 int samba_setregid(gid_t rgid
, gid_t egid
);
49 int samba_seteuid(uid_t euid
);
50 int samba_setegid(gid_t egid
);
51 int samba_setuid(uid_t uid
);
52 int samba_setgid(gid_t gid
);
53 int samba_setuidx(int flags
, uid_t uid
);
54 int samba_setgidx(int flags
, gid_t gid
);
55 int samba_setgroups(size_t setlen
, const gid_t
*gidset
);
59 #if defined(USE_LINUX_THREAD_CREDENTIALS)
60 #if defined(HAVE_UNISTD_H)
63 #if defined(HAVE_SYSCALL_H)
67 #if defined(HAVE_SYS_SYSCALL_H)
68 #include <sys/syscall.h>
71 /* Ensure we can't compile in a mixed syscall setup. */
72 #if !defined(USE_LINUX_32BIT_SYSCALLS)
73 #if defined(SYS_setresuid32) || defined(SYS_setresgid32) || defined(SYS_setreuid32) || defined(SYS_setregid32) || defined(SYS_setuid32) || defined(SYS_setgid32) || defined(SYS_setgroups32)
74 #error Mixture of 32-bit Linux system calls and 64-bit calls.
80 /* All the setXX[ug]id functions and setgroups Samba uses. */
81 int samba_setresuid(uid_t ruid
, uid_t euid
, uid_t suid
)
83 #if defined(USE_LINUX_THREAD_CREDENTIALS)
84 #if defined(USE_LINUX_32BIT_SYSCALLS)
85 return syscall(SYS_setresuid32
, ruid
, euid
, suid
);
87 return syscall(SYS_setresuid
, ruid
, euid
, suid
);
89 #elif defined(HAVE_SETRESUID)
90 return setresuid(ruid
, euid
, suid
);
97 int samba_setresgid(gid_t rgid
, gid_t egid
, gid_t sgid
)
99 #if defined(USE_LINUX_THREAD_CREDENTIALS)
100 #if defined(USE_LINUX_32BIT_SYSCALLS)
101 return syscall(SYS_setresgid32
, rgid
, egid
, sgid
);
103 return syscall(SYS_setresgid
, rgid
, egid
, sgid
);
105 #elif defined(HAVE_SETRESGID)
106 return setresgid(rgid
, egid
, sgid
);
113 int samba_setreuid(uid_t ruid
, uid_t euid
)
115 #if defined(USE_LINUX_THREAD_CREDENTIALS)
116 #if defined(USE_LINUX_32BIT_SYSCALLS)
117 return syscall(SYS_setreuid32
, ruid
, euid
);
119 return syscall(SYS_setreuid
, ruid
, euid
);
121 #elif defined(HAVE_SETREUID)
122 return setreuid(ruid
, euid
);
129 int samba_setregid(gid_t rgid
, gid_t egid
)
131 #if defined(USE_LINUX_THREAD_CREDENTIALS)
132 #if defined(USE_LINUX_32BIT_SYSCALLS)
133 return syscall(SYS_setregid32
, rgid
, egid
);
135 return syscall(SYS_setregid
, rgid
, egid
);
137 #elif defined(HAVE_SETREGID)
138 return setregid(rgid
, egid
);
145 int samba_seteuid(uid_t euid
)
147 #if defined(USE_LINUX_THREAD_CREDENTIALS)
148 #if defined(USE_LINUX_32BIT_SYSCALLS)
149 /* seteuid is not a separate system call. */
150 return syscall(SYS_setresuid32
, -1, euid
, -1);
152 /* seteuid is not a separate system call. */
153 return syscall(SYS_setresuid
, -1, euid
, -1);
155 #elif defined(HAVE_SETEUID)
156 return seteuid(euid
);
163 int samba_setegid(gid_t egid
)
165 #if defined(USE_LINUX_THREAD_CREDENTIALS)
166 #if defined(USE_LINUX_32BIT_SYSCALLS)
167 /* setegid is not a separate system call. */
168 return syscall(SYS_setresgid32
, -1, egid
, -1);
170 /* setegid is not a separate system call. */
171 return syscall(SYS_setresgid
, -1, egid
, -1);
173 #elif defined(HAVE_SETEGID)
174 return setegid(egid
);
181 int samba_setuid(uid_t uid
)
183 #if defined(USE_LINUX_THREAD_CREDENTIALS)
184 #if defined(USE_LINUX_32BIT_SYSCALLS)
185 return syscall(SYS_setuid32
, uid
);
187 return syscall(SYS_setuid
, uid
);
189 #elif defined(HAVE_SETUID)
197 int samba_setgid(gid_t gid
)
199 #if defined(USE_LINUX_THREAD_CREDENTIALS)
200 #if defined(USE_LINUX_32BIT_SYSCALLS)
201 return syscall(SYS_setgid32
, gid
);
203 return syscall(SYS_setgid
, gid
);
205 #elif defined(HAVE_SETGID)
213 int samba_setuidx(int flags
, uid_t uid
)
215 #if defined(HAVE_SETUIDX)
216 return setuidx(flags
, uid
);
218 /* USE_LINUX_THREAD_CREDENTIALS doesn't have this. */
224 int samba_setgidx(int flags
, gid_t gid
)
226 #if defined(HAVE_SETGIDX)
227 return setgidx(flags
, gid
);
229 /* USE_LINUX_THREAD_CREDENTIALS doesn't have this. */
235 int samba_setgroups(size_t setlen
, const gid_t
*gidset
)
237 #if defined(USE_LINUX_THREAD_CREDENTIALS)
238 #if defined(USE_LINUX_32BIT_SYSCALLS)
239 return syscall(SYS_setgroups32
, setlen
, gidset
);
241 return syscall(SYS_setgroups
, setlen
, gidset
);
243 #elif defined(HAVE_SETGROUPS)
244 return setgroups(setlen
, gidset
);