summaryrefslogtreecommitdiffstats
path: root/source/lib/util_sec.c
blob: e579fd3696378241c52bb05b9388a2604f9ff644 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
   Unix SMB/Netbios implementation.
   Version 2.0
   Copyright (C) Jeremy Allison 1998.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"

/****************************************************************************
 Gain root privilege before doing something.
****************************************************************************/

void gain_root_privilege(void)
{
#if defined(HAVE_SETRESUID)

    /*
     * Ensure all our uids are set to root.
	 * Easy method - just use setresuid().
     */
    setresuid(0,0,0);

#else /* !HAVE_SETRESUID */

    /*
     * Ensure all our uids are set to root.
	 * Older method - first use setuid.
     */

    setuid(0);

#if defined(HAVE_SETREUID)
	setreuid(0,0);
#else
    seteuid(0);
#endif

#endif /* HAVE_SETRESUID */
}

/****************************************************************************
 Ensure our real and effective groups are zero.
****************************************************************************/

void gain_root_group_privilege(void)
{
#ifdef HAVE_SETRESGID
	setresgid(0,0,0);
#elif defined(HAVE_SETREGID)
	setregid(0,0);
#elif defined(HAVE_SETEGID)
	setegid(0);
#endif
	setgid(0);
}

/****************************************************************************
 Set *only* the effective uid.
****************************************************************************/

int set_effective_uid(uid_t uid)
{
#if defined(HAVE_TRAPDOOR_UID)
#if defined(HAVE_SETUIDX)
	/* AIX3 has setuidx which is NOT a trapoor function (tridge) */
	if (setuidx(ID_EFFECTIVE, uid) != 0) {
		if (seteuid(uid) != 0) {
			return -1;
		}
	}
	return 0;
#endif
#endif

#if defined(HAVE_SETRESUID)
    return setresuid(-1,uid,-1);
#elif defined(HAVE_SETREUID)
	return setreuid(-1,uid);
#else
    if ((seteuid(uid) != 0) && (setuid(uid) != 0))
		return -1;
	return 0;
#endif
}

/****************************************************************************
 Set *only* the effective gid.
****************************************************************************/

int set_effective_gid(gid_t gid)
{
#if defined(HAVE_SETRESGID)
	return setresgid(-1,gid,-1);
#elif defined(HAVE_SETREGID)
	return setregid(-1,gid);
#else
	if ((setegid(gid) != 0) && (setgid(gid) != 0))
		return -1;
	return 0;
#endif
}

/****************************************************************************
 Set *only* the real uid.
****************************************************************************/

int set_real_uid(uid_t uid)
{
#if defined(HAVE_TRAPDOOR_UID)
#if defined(HAVE_SETUIDX)
    /* AIX3 has setuidx which is NOT a trapoor function (tridge) */
    return setuidx(ID_REAL,uid);
#endif
#endif

#if defined(HAVE_SETRESUID)
    return setresuid(uid,-1,-1);
#elif defined(HAVE_SETREUID)
    return setreuid(uid,-1);
#else
	/* 
	 * Without either setresuid or setreuid we cannot
	 * independently set the real uid.
	 */
    return -1;
#endif
}

/****************************************************************************
 Become the specified uid - permanently !
****************************************************************************/

BOOL become_user_permanently(uid_t uid, gid_t gid)
{
	/* 
	 * Now completely lose our privileges. This is a fairly paranoid
	 * way of doing it, but it does work on all systems that I know of.
	 */

	/*
	 * First - gain root privilege. We do this to ensure
	 * we can lose it again.
	 */

	gain_root_privilege();
	gain_root_group_privilege();

#if defined(HAVE_SETRESUID) && defined(HAVE_SETRESGID)
	/*
	 * Ensure we change all our gids.
	 */
	setresgid(gid,gid,gid);
	
	/*
	 * Ensure all the uids are the user.
	 */
	setresuid(uid,uid,uid);
#else /* !( defined(HAVE_SETRESUID) && defined(HAVE_SETRESGID) ) */

	/*
	 * Ensure we change all our gids.
	 */
	setgid(gid);
#if defined(HAVE_SETREGID)
	setregid(gid,gid);
#else
	setegid(gid);
#endif
	
	/*
	 * Ensure all the uids are the user.
	 */
	setuid(uid);

#if defined(HAVE_SETREUID)
	setreuid(uid,uid);
#else
	seteuid(uid);
#endif

#endif /* !( defined(HAVE_SETRESUID) && defined(HAVE_SETRESGID) ) */
	
	if (getuid() != uid || geteuid() != uid ||
	    getgid() != gid || getegid() != gid) {
		/* We failed to lose our privileges. */
		return False;
	}
	
	return(True);
}