Move back to using per-thread credentials on Linux. Fixes the glibc native AIO lost...
[gd/samba-autobuild/.git] / lib / util / setid.c
1 /*
2    Unix SMB/CIFS implementation.
3    setXXid() functions for Samba.
4    Copyright (C) Jeremy Allison 2012
5
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.
10
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.
15
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/>.
18 */
19
20 #ifndef AUTOCONF_TEST
21 #include "replace.h"
22 #include "system/passwd.h"
23 #include "include/includes.h"
24
25 #ifdef UID_WRAPPER_REPLACE
26
27 #ifdef samba_seteuid
28 #undef samba_seteuid
29 #endif
30
31 #ifdef samba_setreuid
32 #undef samba_setreuid
33 #endif
34
35 #ifdef samba_setresuid
36 #undef samba_setresuid
37 #endif
38
39 #ifdef samba_setegid
40 #undef samba_setegid
41 #endif
42
43 #ifdef samba_setregid
44 #undef samba_setregid
45 #endif
46
47 #ifdef samba_setresgid
48 #undef samba_setresgid
49 #endif
50
51 #ifdef samba_setgroups
52 #undef samba_setgroups
53 #endif
54
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);
67
68 #endif
69 #endif
70
71 #include "../lib/util/setid.h"
72
73 #if defined(USE_LINUX_THREAD_CREDENTIALS)
74 #if defined(HAVE_SYSCALL_H)
75 #include <syscall.h>
76 #endif
77
78 #if defined(HAVE_SYS_SYSCALL_H)
79 #include <sys/syscall.h>
80 #endif
81 #endif
82
83 /* All the setXX[ug]id functions and setgroups Samba uses. */
84 int samba_setresuid(uid_t ruid, uid_t euid, uid_t suid)
85 {
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);
90 #else
91         errno = ENOSYS;
92         return -1;
93 #endif
94 }
95
96 int samba_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
97 {
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);
102 #else
103         errno = ENOSYS;
104         return -1;
105 #endif
106 }
107
108 int samba_setreuid(uid_t ruid, uid_t euid)
109 {
110 #if defined(USE_LINUX_THREAD_CREDENTIALS)
111         return syscall(SYS_setreuid, ruid, euid);
112 #elif defined(HAVE_SETREUID)
113         return setreuid(ruid, euid);
114 #else
115         errno = ENOSYS;
116         return -1;
117 #endif
118 }
119
120 int samba_setregid(gid_t rgid, gid_t egid)
121 {
122 #if defined(USE_LINUX_THREAD_CREDENTIALS)
123         return syscall(SYS_setregid, rgid, egid);
124 #elif defined(HAVE_SETREGID)
125         return setregid(rgid, egid);
126 #else
127         errno = ENOSYS;
128         return -1;
129 #endif
130 }
131
132 int samba_seteuid(uid_t euid)
133 {
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);
139 #else
140         errno = ENOSYS;
141         return -1;
142 #endif
143 }
144
145 int samba_setegid(gid_t egid)
146 {
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);
152 #else
153         errno = ENOSYS;
154         return -1;
155 #endif
156 }
157
158 int samba_setuid(uid_t uid)
159 {
160 #if defined(USE_LINUX_THREAD_CREDENTIALS)
161         return syscall(SYS_setuid, uid);
162 #elif defined(HAVE_SETUID)
163         return setuid(uid);
164 #else
165         errno = ENOSYS;
166         return -1;
167 #endif
168 }
169
170 int samba_setgid(gid_t gid)
171 {
172 #if defined(USE_LINUX_THREAD_CREDENTIALS)
173         return syscall(SYS_setgid, gid);
174 #elif defined(HAVE_SETGID)
175         return setgid(gid);
176 #else
177         errno = ENOSYS;
178         return -1;
179 #endif
180 }
181
182 int samba_setuidx(int flags, uid_t uid)
183 {
184 #if defined(HAVE_SETUIDX)
185         return setuidx(flags, uid);
186 #else
187         /* USE_LINUX_THREAD_CREDENTIALS doesn't have this. */
188         errno = ENOSYS;
189         return -1;
190 #endif
191 }
192
193 int samba_setgidx(int flags, gid_t gid)
194 {
195 #if defined(HAVE_SETGIDX)
196         return setgidx(flags, gid);
197 #else
198         /* USE_LINUX_THREAD_CREDENTIALS doesn't have this. */
199         errno = ENOSYS;
200         return -1;
201 #endif
202 }
203
204 int samba_setgroups(size_t setlen, const gid_t *gidset)
205 {
206 #if defined(USE_LINUX_THREAD_CREDENTIALS)
207         return syscall(SYS_setgroups, setlen, gidset);
208 #elif defined(HAVE_SETGROUPS)
209         return setgroups(setlen, gidset);
210 #else
211         errno = ENOSYS;
212         return -1;
213 #endif
214 }