2
Unix SMB/CIFS implementation.
3
Copyright (C) Jeremy Allison 1998.
4
rewritten for version 2.0.6 by Tridge
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 2 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, write to the Free Software
18
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24
/* we are running this code in autoconf test mode to see which type of setuid
26
#if defined(HAVE_UNISTD_H)
31
#include <sys/types.h>
34
#ifdef HAVE_SYS_PRIV_H
41
#define DEBUG(x, y) printf y
42
#define smb_panic(x) exit(1)
46
/* are we running as non-root? This is used by the regresison test code,
47
and potentially also for sites that want non-root smbd */
48
static uid_t initial_uid;
49
static gid_t initial_gid;
51
/****************************************************************************
52
remember what uid we got started as - this allows us to run correctly
53
as non-root while catching trapdoor systems
54
****************************************************************************/
57
initial_uid = geteuid();
58
initial_gid = getegid();
61
/****************************************************************************
62
some code (eg. winbindd) needs to know what uid we started as
63
****************************************************************************/
64
uid_t sec_initial_uid(void)
69
/****************************************************************************
70
some code (eg. winbindd, profiling shm) needs to know what gid we started as
71
****************************************************************************/
72
gid_t sec_initial_gid(void)
77
/****************************************************************************
78
are we running in non-root mode?
79
****************************************************************************/
80
BOOL non_root_mode(void)
82
return (initial_uid != (uid_t)0);
85
/****************************************************************************
86
abort if we haven't set the uid correctly
87
****************************************************************************/
88
static void assert_uid(uid_t ruid, uid_t euid)
90
if ((euid != (uid_t)-1 && geteuid() != euid) ||
91
(ruid != (uid_t)-1 && getuid() != ruid)) {
92
if (!non_root_mode()) {
93
DEBUG(0,("Failed to set uid privileges to (%d,%d) now set to (%d,%d)\n",
95
(int)getuid(), (int)geteuid()));
96
smb_panic("failed to set uid\n");
102
/****************************************************************************
103
abort if we haven't set the gid correctly
104
****************************************************************************/
105
static void assert_gid(gid_t rgid, gid_t egid)
107
if ((egid != (gid_t)-1 && getegid() != egid) ||
108
(rgid != (gid_t)-1 && getgid() != rgid)) {
109
if (!non_root_mode()) {
110
DEBUG(0,("Failed to set gid privileges to (%d,%d) now set to (%d,%d) uid=(%d,%d)\n",
111
(int)rgid, (int)egid,
112
(int)getgid(), (int)getegid(),
113
(int)getuid(), (int)geteuid()));
114
smb_panic("failed to set gid\n");
120
/****************************************************************************
121
Gain root privilege before doing something.
122
We want to end up with ruid==euid==0
123
****************************************************************************/
124
void gain_root_privilege(void)
139
setuidx(ID_EFFECTIVE, 0);
143
/* this is needed on some systems */
150
/****************************************************************************
151
Ensure our real and effective groups are zero.
152
we want to end up with rgid==egid==0
153
****************************************************************************/
154
void gain_root_group_privilege(void)
169
setgidx(ID_EFFECTIVE, 0);
179
/****************************************************************************
180
Set effective uid, and possibly the real uid too.
181
We want to end up with either:
183
ruid==uid and euid==uid
187
ruid==0 and euid==uid
189
depending on what the local OS will allow us to regain root from.
190
****************************************************************************/
191
void set_effective_uid(uid_t uid)
194
/* Set the effective as well as the real uid. */
195
setresuid(uid,uid,-1);
207
setuidx(ID_EFFECTIVE, uid);
213
/****************************************************************************
214
Set *only* the effective gid.
215
we want to end up with rgid==0 and egid==gid
216
****************************************************************************/
217
void set_effective_gid(gid_t gid)
220
setresgid(-1,gid,-1);
232
setgidx(ID_EFFECTIVE, gid);
238
static uid_t saved_euid, saved_ruid;
239
static gid_t saved_egid, saved_rgid;
241
/****************************************************************************
242
save the real and effective uid for later restoration. Used by the quotas
244
****************************************************************************/
245
void save_re_uid(void)
247
saved_ruid = getuid();
248
saved_euid = geteuid();
252
/****************************************************************************
254
****************************************************************************/
255
void restore_re_uid(void)
257
set_effective_uid(0);
260
setresuid(saved_ruid, saved_euid, -1);
262
setreuid(saved_ruid, -1);
263
setreuid(-1,saved_euid);
265
setuidx(ID_REAL, saved_ruid);
266
setuidx(ID_EFFECTIVE, saved_euid);
268
set_effective_uid(saved_euid);
269
if (getuid() != saved_ruid)
271
set_effective_uid(saved_euid);
274
assert_uid(saved_ruid, saved_euid);
278
/****************************************************************************
279
save the real and effective gid for later restoration. Used by the
281
****************************************************************************/
282
void save_re_gid(void)
284
saved_rgid = getgid();
285
saved_egid = getegid();
288
/****************************************************************************
290
****************************************************************************/
291
void restore_re_gid(void)
294
setresgid(saved_rgid, saved_egid, -1);
296
setregid(saved_rgid, -1);
297
setregid(-1,saved_egid);
299
setgidx(ID_REAL, saved_rgid);
300
setgidx(ID_EFFECTIVE, saved_egid);
302
set_effective_gid(saved_egid);
303
if (getgid() != saved_rgid)
305
set_effective_gid(saved_egid);
308
assert_gid(saved_rgid, saved_egid);
312
/****************************************************************************
313
set the real AND effective uid to the current effective uid in a way that
314
allows root to be regained.
315
This is only possible on some platforms.
316
****************************************************************************/
319
uid_t uid = geteuid();
322
setresuid(geteuid(), -1, -1);
341
assert_uid(uid, uid);
346
/****************************************************************************
347
Become the specified uid and gid - permanently !
348
there should be no way back if possible
349
****************************************************************************/
350
void become_user_permanently(uid_t uid, gid_t gid)
353
* First - gain root privilege. We do this to ensure
354
* we can lose it again.
357
gain_root_privilege();
358
gain_root_group_privilege();
361
setresgid(gid,gid,gid);
363
setresuid(uid,uid,uid);
383
setgidx(ID_REAL, gid);
384
setgidx(ID_EFFECTIVE, gid);
386
setuidx(ID_REAL, uid);
387
setuidx(ID_EFFECTIVE, uid);
391
assert_uid(uid, uid);
392
assert_gid(gid, gid);
397
/****************************************************************************
398
this function just checks that we don't get ENOSYS back
399
****************************************************************************/
400
static int have_syscall(void)
417
setuidx(ID_EFFECTIVE, -1);
420
if (errno == ENOSYS) return -1;
428
#if (defined(AIX) && defined(USE_SETREUID))
429
/* setreuid is badly broken on AIX 4.1, we avoid it completely */
430
fprintf(stderr,"avoiding possibly broken setreuid\n");
434
/* if not running as root then at least check to see if we get ENOSYS - this
435
handles Linux 2.0.x with glibc 2.1 */
436
fprintf(stderr,"not running as root: checking for ENOSYS\n");
437
exit(have_syscall());
440
gain_root_privilege();
441
gain_root_group_privilege();
442
set_effective_gid(1);
443
set_effective_uid(1);
446
gain_root_privilege();
447
gain_root_group_privilege();
448
become_user_permanently(1, 1);
451
fprintf(stderr,"uid not set permanently\n");
461
/****************************************************************************
462
Check if we are setuid root. Used in libsmb and smbpasswd paranoia checks.
463
****************************************************************************/
464
BOOL is_setuid_root(void)
466
return (geteuid() == (uid_t)0) && (getuid() != (uid_t)0);