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);
71 #include "../lib/util/setid.h"
73 #if defined(USE_LINUX_THREAD_CREDENTIALS)
74 #if defined(HAVE_SYSCALL_H)
78 #if defined(HAVE_SYS_SYSCALL_H)
79 #include <sys/syscall.h>
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(USE_LINUX_THREAD_CREDENTIALS)
87 return syscall(SYS_setresuid, ruid, euid, suid);
88 #elif defined(HAVE_SETRESUID)
89 return setresuid(ruid, euid, suid);
96 int samba_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
98 #if defined(USE_LINUX_THREAD_CREDENTIALS)
99 return syscall(SYS_setresgid, rgid, egid, sgid);
100 #elif defined(HAVE_SETRESGID)
101 return setresgid(rgid, egid, sgid);
108 int samba_setreuid(uid_t ruid, uid_t euid)
110 #if defined(USE_LINUX_THREAD_CREDENTIALS)
111 return syscall(SYS_setreuid, ruid, euid);
112 #elif defined(HAVE_SETREUID)
113 return setreuid(ruid, euid);
120 int samba_setregid(gid_t rgid, gid_t egid)
122 #if defined(USE_LINUX_THREAD_CREDENTIALS)
123 return syscall(SYS_setregid, rgid, egid);
124 #elif defined(HAVE_SETREGID)
125 return setregid(rgid, egid);
132 int samba_seteuid(uid_t euid)
134 #if defined(USE_LINUX_THREAD_CREDENTIALS)
135 /* seteuid is not a separate system call. */
136 return syscall(SYS_setresuid, -1, euid, -1);
137 #elif defined(HAVE_SETEUID)
138 return seteuid(euid);
145 int samba_setegid(gid_t egid)
147 #if defined(USE_LINUX_THREAD_CREDENTIALS)
148 /* setegid is not a separate system call. */
149 return syscall(SYS_setresgid, -1, egid, -1);
150 #elif defined(HAVE_SETEGID)
151 return setegid(egid);
158 int samba_setuid(uid_t uid)
160 #if defined(USE_LINUX_THREAD_CREDENTIALS)
161 return syscall(SYS_setuid, uid);
162 #elif defined(HAVE_SETUID)
170 int samba_setgid(gid_t gid)
172 #if defined(USE_LINUX_THREAD_CREDENTIALS)
173 return syscall(SYS_setgid, gid);
174 #elif defined(HAVE_SETGID)
182 int samba_setuidx(int flags, uid_t uid)
184 #if defined(HAVE_SETUIDX)
185 return setuidx(flags, uid);
187 /* USE_LINUX_THREAD_CREDENTIALS doesn't have this. */
193 int samba_setgidx(int flags, gid_t gid)
195 #if defined(HAVE_SETGIDX)
196 return setgidx(flags, gid);
198 /* USE_LINUX_THREAD_CREDENTIALS doesn't have this. */
204 int samba_setgroups(size_t setlen, const gid_t *gidset)
206 #if defined(USE_LINUX_THREAD_CREDENTIALS)
207 return syscall(SYS_setgroups, setlen, gidset);
208 #elif defined(HAVE_SETGROUPS)
209 return setgroups(setlen, gidset);