lib/util_unistr.c: Off-by-one fix for dos_PutUniStr from John Reilly jreilly@hp.com.
[tprouty/samba.git] / source3 / smbd / sec_ctx.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    uid/user handling
5    Copyright (C) Tim Potter 2000
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 extern int DEBUGLEVEL;
25 extern struct current_user current_user;
26
27 struct sec_ctx {
28         uid_t uid;
29         uid_t gid;
30         int ngroups;
31         gid_t *groups;
32 };
33
34 /* A stack of security contexts.  We include the current context as being
35    the first one, so there is room for another MAX_SEC_CTX_DEPTH more. */
36
37 static struct sec_ctx sec_ctx_stack[MAX_SEC_CTX_DEPTH + 1];
38 static int sec_ctx_stack_ndx;
39
40 /* Become the specified uid */
41
42 static BOOL become_uid(uid_t uid)
43 {
44         /* Check for dodgy uid values */
45
46         if (uid == (uid_t)-1 || 
47             ((sizeof(uid_t) == 2) && (uid == (uid_t)65535))) {
48                 static int done;
49  
50                 if (!done) {
51                         DEBUG(1,("WARNING: using uid %d is a security risk\n",
52                                  (int)uid));
53                         done = 1;
54                 }
55         }
56
57         /* Set effective user id */
58
59         set_effective_uid(uid);
60         current_user.uid = uid;
61
62 #ifdef WITH_PROFILE
63         profile_p->uid_changes++;
64 #endif
65
66         return True;
67 }
68
69 /* Become the specified gid */
70
71 static BOOL become_gid(gid_t gid)
72 {
73         /* Check for dodgy gid values */
74
75         if (gid == (gid_t)-1 || ((sizeof(gid_t) == 2) && 
76                                  (gid == (gid_t)65535))) {
77                 static int done;
78                 
79                 if (!done) {
80                         DEBUG(1,("WARNING: using gid %d is a security risk\n",
81                                  (int)gid));  
82                         done = 1;
83                 }
84         }
85   
86         /* Set effective group id */
87
88         set_effective_gid(gid);
89         current_user.gid = gid;
90         
91         return True;
92 }
93
94 /* Become the specified uid and gid */
95
96 static BOOL become_id(uid_t uid, gid_t gid)
97 {
98         return become_gid(gid) && become_uid(uid);
99 }
100
101 /* Drop back to root privileges in order to change to another user */
102
103 static void gain_root(void)
104 {
105         if (geteuid() != 0) {
106                 set_effective_uid(0);
107
108                 if (geteuid() != 0) {
109                         DEBUG(0,
110                               ("Warning: You appear to have a trapdoor "
111                                "uid system\n"));
112                 }
113         }
114
115         if (getegid() != 0) {
116                 set_effective_gid(0);
117
118                 if (getegid() != 0) {
119                         DEBUG(0,
120                               ("Warning: You appear to have a trapdoor "
121                                "gid system\n"));
122                 }
123         }
124 }
125
126 /* Get the list of current groups */
127
128 static void get_current_groups(int *ngroups, gid_t **groups)
129 {
130         *ngroups = getgroups(0, NULL);
131         *groups = (gid_t *)malloc(*ngroups * sizeof(gid_t));
132
133         if (!groups) {
134                 DEBUG(0, ("Out of memory in get_current_groups\n"));
135                 return;
136         }
137
138         getgroups(*ngroups, *groups);
139 }
140
141 /* Create a new security context on the stack.  It is the same as the old
142    one.  User changes are done using the set_sec_ctx() function. */
143
144 BOOL push_sec_ctx(void)
145 {
146         /* Check we don't overflow our stack */
147
148         if (sec_ctx_stack_ndx == (MAX_SEC_CTX_DEPTH)) {
149                 DEBUG(0, ("Security context stack overflow!\n"));
150                 return False;
151         }
152
153         /* Store previous user context */
154
155         sec_ctx_stack_ndx++;
156
157         sec_ctx_stack[sec_ctx_stack_ndx].uid = geteuid();
158         sec_ctx_stack[sec_ctx_stack_ndx].gid = getegid();
159
160         sec_ctx_stack[sec_ctx_stack_ndx].ngroups = sys_getgroups(0, NULL);
161
162         if (!(sec_ctx_stack[sec_ctx_stack_ndx].groups = 
163               malloc(sec_ctx_stack[sec_ctx_stack_ndx].ngroups * 
164                      sizeof(gid_t)))) {
165                 DEBUG(0, ("Out of memory in push_sec_ctx()\n"));
166                 return False;
167         }
168
169         sys_getgroups(sec_ctx_stack[sec_ctx_stack_ndx].ngroups,
170                   sec_ctx_stack[sec_ctx_stack_ndx].groups);
171
172         return True;
173 }
174
175 /* Set the current security context to a given user */
176
177 void set_sec_ctx(uid_t uid, gid_t gid, int ngroups, gid_t *groups)
178 {
179         /* Set the security context */
180
181         DEBUG(3, ("setting sec ctx (%d, %d)\n", uid, gid));
182
183         gain_root();
184
185 #ifdef HAVE_SETGROUPS
186         sys_setgroups(ngroups, groups);
187 #endif
188
189         sec_ctx_stack[sec_ctx_stack_ndx].ngroups = ngroups;
190
191         if (sec_ctx_stack[sec_ctx_stack_ndx].groups != NULL)
192                 free(sec_ctx_stack[sec_ctx_stack_ndx].groups);
193
194         sec_ctx_stack[sec_ctx_stack_ndx].groups = 
195                 memdup(groups, sizeof(gid_t) * ngroups);
196
197         become_id(uid, gid);
198
199         sec_ctx_stack[sec_ctx_stack_ndx].uid = uid;
200         sec_ctx_stack[sec_ctx_stack_ndx].gid = gid;
201
202         /* Update current_user stuff */
203
204         current_user.uid = uid;
205         current_user.gid = gid;
206         current_user.ngroups = ngroups;
207         current_user.groups = groups;
208 }
209
210 /* Become root context */
211
212 void set_root_sec_ctx(void)
213 {
214         /* May need to worry about supplementary groups at some stage */
215
216         set_sec_ctx(0, 0, 0, NULL);
217 }
218
219 /* Pop a security context from the stack */
220
221 BOOL pop_sec_ctx(void)
222 {
223         /* Check for stack underflow */
224
225         if (sec_ctx_stack_ndx == 0) {
226                 DEBUG(0, ("Security context stack underflow!\n"));
227                 return False;
228         }
229
230         /* Clear previous user info */
231
232         sec_ctx_stack[sec_ctx_stack_ndx].uid = (uid_t)-1;
233         sec_ctx_stack[sec_ctx_stack_ndx].gid = (gid_t)-1;
234
235         safe_free(sec_ctx_stack[sec_ctx_stack_ndx].groups);
236         sec_ctx_stack[sec_ctx_stack_ndx].ngroups = 0;
237
238         /* Pop back previous user */
239
240         sec_ctx_stack_ndx--;
241
242         gain_root();
243
244 #ifdef HAVE_SETGROUPS
245         sys_setgroups(sec_ctx_stack[sec_ctx_stack_ndx].ngroups,
246                       sec_ctx_stack[sec_ctx_stack_ndx].groups);
247 #endif
248
249         become_id(sec_ctx_stack[sec_ctx_stack_ndx].uid,
250                   sec_ctx_stack[sec_ctx_stack_ndx].gid);
251
252         /* Update current_user stuff */
253
254         current_user.uid = sec_ctx_stack[sec_ctx_stack_ndx].uid;
255         current_user.gid = sec_ctx_stack[sec_ctx_stack_ndx].gid;
256         current_user.ngroups = sec_ctx_stack[sec_ctx_stack_ndx].ngroups;
257         current_user.groups = sec_ctx_stack[sec_ctx_stack_ndx].groups;
258
259         DEBUG(3, ("popped off to sec ctx (%d, %d)\n", geteuid(), getegid()));
260
261         return True;
262 }
263
264 /* Initialise the security context system */
265
266 void init_sec_ctx(void)
267 {
268         int i;
269
270         /* Initialise security context stack */
271
272         memset(sec_ctx_stack, 0, sizeof(struct sec_ctx) * MAX_SEC_CTX_DEPTH);
273
274         for (i = 0; i < MAX_SEC_CTX_DEPTH; i++) {
275                 sec_ctx_stack[i].uid = (uid_t)-1;
276                 sec_ctx_stack[i].gid = (gid_t)-1;
277         }
278
279         /* Initialise first level of stack.  It is the current context */
280
281         sec_ctx_stack[0].uid = geteuid();
282         sec_ctx_stack[0].gid = getegid();
283
284         get_current_groups(&sec_ctx_stack[0].ngroups,
285                            &sec_ctx_stack[0].groups);
286
287         /* Initialise current_user global */
288
289         current_user.uid = sec_ctx_stack[sec_ctx_stack_ndx].uid;
290         current_user.gid = sec_ctx_stack[sec_ctx_stack_ndx].gid;
291         current_user.ngroups = sec_ctx_stack[sec_ctx_stack_ndx].ngroups;
292         current_user.groups = sec_ctx_stack[sec_ctx_stack_ndx].groups;
293
294         /* The conn and vuid are usually taken care of by other modules.
295            We initialise them here. */
296
297         current_user.conn = NULL;
298         current_user.vuid = UID_FIELD_INVALID;
299 }