Try and fix the autoconf build on Solaris/Nexenta/etc.
[mat/samba.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 #endif
68
69 #include "../lib/util/setid.h"
70
71 #else
72
73 /* Inside autoconf test. */
74 #if defined(HAVE_UNISTD_H)
75 #include <unistd.h>
76 #endif
77 #include <stdlib.h>
78 #include <stdio.h>
79 #include <sys/types.h>
80 #include <errno.h>
81
82 #ifdef HAVE_SYS_PRIV_H
83 #include <sys/priv.h>
84 #endif
85 #ifdef HAVE_SYS_ID_H
86 #include <sys/id.h>
87 #endif
88
89 /* autoconf tests don't include setid.h */
90 int samba_setresuid(uid_t ruid, uid_t euid, uid_t suid);
91 int samba_setresgid(gid_t rgid, gid_t egid, gid_t sgid);
92 int samba_setreuid(uid_t ruid, uid_t euid);
93 int samba_setregid(gid_t rgid, gid_t egid);
94 int samba_seteuid(uid_t euid);
95 int samba_setegid(gid_t egid);
96 int samba_setuid(uid_t uid);
97 int samba_setgid(gid_t gid);
98 int samba_setuidx(int flags, uid_t uid);
99 int samba_setgidx(int flags, gid_t gid);
100 int samba_setgroups(size_t setlen, const gid_t *gidset);
101
102 #endif
103
104 #if defined(USE_LINUX_THREAD_CREDENTIALS)
105 #if defined(HAVE_SYSCALL_H)
106 #include <syscall.h>
107 #endif
108
109 #if defined(HAVE_SYS_SYSCALL_H)
110 #include <sys/syscall.h>
111 #endif
112 #endif
113
114 /* All the setXX[ug]id functions and setgroups Samba uses. */
115 int samba_setresuid(uid_t ruid, uid_t euid, uid_t suid)
116 {
117 #if defined(USE_LINUX_THREAD_CREDENTIALS)
118         return syscall(SYS_setresuid, ruid, euid, suid);
119 #elif defined(HAVE_SETRESUID)
120         return setresuid(ruid, euid, suid);
121 #else
122         errno = ENOSYS;
123         return -1;
124 #endif
125 }
126
127 int samba_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
128 {
129 #if defined(USE_LINUX_THREAD_CREDENTIALS)
130         return syscall(SYS_setresgid, rgid, egid, sgid);
131 #elif defined(HAVE_SETRESGID)
132         return setresgid(rgid, egid, sgid);
133 #else
134         errno = ENOSYS;
135         return -1;
136 #endif
137 }
138
139 int samba_setreuid(uid_t ruid, uid_t euid)
140 {
141 #if defined(USE_LINUX_THREAD_CREDENTIALS)
142         return syscall(SYS_setreuid, ruid, euid);
143 #elif defined(HAVE_SETREUID)
144         return setreuid(ruid, euid);
145 #else
146         errno = ENOSYS;
147         return -1;
148 #endif
149 }
150
151 int samba_setregid(gid_t rgid, gid_t egid)
152 {
153 #if defined(USE_LINUX_THREAD_CREDENTIALS)
154         return syscall(SYS_setregid, rgid, egid);
155 #elif defined(HAVE_SETREGID)
156         return setregid(rgid, egid);
157 #else
158         errno = ENOSYS;
159         return -1;
160 #endif
161 }
162
163 int samba_seteuid(uid_t euid)
164 {
165 #if defined(USE_LINUX_THREAD_CREDENTIALS)
166         /* seteuid is not a separate system call. */
167         return syscall(SYS_setresuid, -1, euid, -1);
168 #elif defined(HAVE_SETEUID)
169         return seteuid(euid);
170 #else
171         errno = ENOSYS;
172         return -1;
173 #endif
174 }
175
176 int samba_setegid(gid_t egid)
177 {
178 #if defined(USE_LINUX_THREAD_CREDENTIALS)
179         /* setegid is not a separate system call. */
180         return syscall(SYS_setresgid, -1, egid, -1);
181 #elif defined(HAVE_SETEGID)
182         return setegid(egid);
183 #else
184         errno = ENOSYS;
185         return -1;
186 #endif
187 }
188
189 int samba_setuid(uid_t uid)
190 {
191 #if defined(USE_LINUX_THREAD_CREDENTIALS)
192         return syscall(SYS_setuid, uid);
193 #elif defined(HAVE_SETUID)
194         return setuid(uid);
195 #else
196         errno = ENOSYS;
197         return -1;
198 #endif
199 }
200
201 int samba_setgid(gid_t gid)
202 {
203 #if defined(USE_LINUX_THREAD_CREDENTIALS)
204         return syscall(SYS_setgid, gid);
205 #elif defined(HAVE_SETGID)
206         return setgid(gid);
207 #else
208         errno = ENOSYS;
209         return -1;
210 #endif
211 }
212
213 int samba_setuidx(int flags, uid_t uid)
214 {
215 #if defined(HAVE_SETUIDX)
216         return setuidx(flags, uid);
217 #else
218         /* USE_LINUX_THREAD_CREDENTIALS doesn't have this. */
219         errno = ENOSYS;
220         return -1;
221 #endif
222 }
223
224 int samba_setgidx(int flags, gid_t gid)
225 {
226 #if defined(HAVE_SETGIDX)
227         return setgidx(flags, gid);
228 #else
229         /* USE_LINUX_THREAD_CREDENTIALS doesn't have this. */
230         errno = ENOSYS;
231         return -1;
232 #endif
233 }
234
235 int samba_setgroups(size_t setlen, const gid_t *gidset)
236 {
237 #if defined(USE_LINUX_THREAD_CREDENTIALS)
238         return syscall(SYS_setgroups, setlen, gidset);
239 #elif defined(HAVE_SETGROUPS)
240         return setgroups(setlen, gidset);
241 #else
242         errno = ENOSYS;
243         return -1;
244 #endif
245 }