Ken McDonell from SGI was interested in adding some profiling
[bbaumbach/samba-autobuild/.git] / source / smbd / uid.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    uid/user handling
5    Copyright (C) Andrew Tridgell 1992-1998
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
26 static uid_t initial_uid;
27 static gid_t initial_gid;
28
29 /* what user is current? */
30 extern struct current_user current_user;
31
32 pstring OriginalDir;
33
34 /****************************************************************************
35 initialise the uid routines
36 ****************************************************************************/
37 void init_uid(void)
38 {
39         initial_uid = current_user.uid = geteuid();
40         initial_gid = current_user.gid = getegid();
41
42         if (initial_gid != 0 && initial_uid == 0) {
43 #ifdef HAVE_SETRESUID
44                 setresgid(0,0,0);
45 #else
46                 setgid(0);
47                 setegid(0);
48 #endif
49         }
50
51         initial_uid = geteuid();
52         initial_gid = getegid();
53
54         current_user.conn = NULL;
55         current_user.vuid = UID_FIELD_INVALID;
56         
57         dos_ChDir(OriginalDir);
58 }
59
60
61 /****************************************************************************
62   become the specified uid 
63 ****************************************************************************/
64 static BOOL become_uid(uid_t uid)
65 {
66         if (initial_uid != 0) {
67                 return(True);
68         }
69         
70         if (uid == (uid_t)-1 || ((sizeof(uid_t) == 2) && (uid == (uid_t)65535))) {
71                 static int done;
72                 if (!done) {
73                         DEBUG(1,("WARNING: using uid %d is a security risk\n",(int)uid));
74                         done=1;
75                 }
76         }
77
78 #ifdef HAVE_TRAPDOOR_UID
79 #ifdef HAVE_SETUIDX
80         /* AIX3 has setuidx which is NOT a trapoor function (tridge) */
81         if (setuidx(ID_EFFECTIVE, uid) != 0) {
82                 if (seteuid(uid) != 0) {
83                         DEBUG(1,("Can't set uid %d (setuidx)\n", (int)uid));
84                         return False;
85                 }
86         }
87 #endif
88 #endif
89
90 #ifdef HAVE_SETRESUID
91     if (setresuid(-1,uid,-1) != 0)
92 #else
93     if ((seteuid(uid) != 0) && 
94         (setuid(uid) != 0))
95 #endif
96       {
97         DEBUG(0,("Couldn't set uid %d currently set to (%d,%d)\n",
98                  (int)uid,(int)getuid(), (int)geteuid()));
99         if (uid > (uid_t)32000) {
100                 DEBUG(0,("Looks like your OS doesn't like high uid values - try using a different account\n"));
101         }
102         return(False);
103       }
104
105     if (((uid == (uid_t)-1) || ((sizeof(uid_t) == 2) && (uid == 65535))) && (geteuid() != uid)) {
106             DEBUG(0,("Invalid uid -1. perhaps you have a account with uid 65535?\n"));
107             return(False);
108     }
109
110     current_user.uid = uid;
111
112 #ifdef WITH_PROFILE
113     profile_p->uid_changes++;
114 #endif
115
116     return(True);
117 }
118
119
120 /****************************************************************************
121   become the specified gid
122 ****************************************************************************/
123 static BOOL become_gid(gid_t gid)
124 {
125   if (initial_uid != 0)
126     return(True);
127
128   if (gid == (gid_t)-1 || ((sizeof(gid_t) == 2) && (gid == (gid_t)65535))) {
129     DEBUG(1,("WARNING: using gid %d is a security risk\n",(int)gid));    
130   }
131   
132 #ifdef HAVE_SETRESUID
133   if (setresgid(-1,gid,-1) != 0)
134 #else
135   if (setgid(gid) != 0)
136 #endif
137       {
138         DEBUG(0,("Couldn't set gid %d currently set to (%d,%d)\n",
139                  (int)gid,(int)getgid(),(int)getegid()));
140         if (gid > 32000) {
141                 DEBUG(0,("Looks like your OS doesn't like high gid values - try using a different account\n"));
142         }
143         return(False);
144       }
145
146   current_user.gid = gid;
147
148   return(True);
149 }
150
151
152 /****************************************************************************
153   become the specified uid and gid
154 ****************************************************************************/
155 static BOOL become_id(uid_t uid,gid_t gid)
156 {
157         return(become_gid(gid) && become_uid(uid));
158 }
159
160 /****************************************************************************
161 become the guest user
162 ****************************************************************************/
163 BOOL become_guest(void)
164 {
165   BOOL ret;
166   static struct passwd *pass=NULL;
167
168   if (initial_uid != 0) 
169     return(True);
170
171   if (!pass)
172     pass = Get_Pwnam(lp_guestaccount(-1),True);
173   if (!pass) return(False);
174
175 #ifdef AIX
176   /* MWW: From AIX FAQ patch to WU-ftpd: call initgroups before setting IDs */
177   initgroups(pass->pw_name, (gid_t)pass->pw_gid);
178 #endif
179
180   ret = become_id(pass->pw_uid,pass->pw_gid);
181
182   if (!ret) {
183     DEBUG(1,("Failed to become guest. Invalid guest account?\n"));
184   }
185
186   current_user.conn = NULL;
187   current_user.vuid = UID_FIELD_INVALID;
188
189   return(ret);
190 }
191
192 /*******************************************************************
193 check if a username is OK
194 ********************************************************************/
195 static BOOL check_user_ok(connection_struct *conn, user_struct *vuser,int snum)
196 {
197   int i;
198   for (i=0;i<conn->uid_cache.entries;i++)
199     if (conn->uid_cache.list[i] == vuser->uid) return(True);
200
201   if (!user_ok(vuser->name,snum)) return(False);
202
203   i = conn->uid_cache.entries % UID_CACHE_SIZE;
204   conn->uid_cache.list[i] = vuser->uid;
205
206   if (conn->uid_cache.entries < UID_CACHE_SIZE)
207     conn->uid_cache.entries++;
208
209   return(True);
210 }
211
212
213 /****************************************************************************
214   become the user of a connection number
215 ****************************************************************************/
216 BOOL become_user(connection_struct *conn, uint16 vuid)
217 {
218         user_struct *vuser = get_valid_user_struct(vuid);
219         int snum;
220     gid_t gid;
221         uid_t uid;
222
223         /*
224          * We need a separate check in security=share mode due to vuid
225          * always being UID_FIELD_INVALID. If we don't do this then
226          * in share mode security we are *always* changing uid's between
227          * SMB's - this hurts performance - Badly.
228          */
229
230         if((lp_security() == SEC_SHARE) && (current_user.conn == conn) &&
231            (current_user.uid == conn->uid)) {
232                 DEBUG(4,("Skipping become_user - already user\n"));
233                 return(True);
234         } else if ((current_user.conn == conn) && 
235                    (vuser != 0) && (current_user.vuid == vuid) && 
236                    (current_user.uid == vuser->uid)) {
237                 DEBUG(4,("Skipping become_user - already user\n"));
238                 return(True);
239         }
240
241         unbecome_user();
242
243         if (!conn) {
244                 DEBUG(2,("Connection not open\n"));
245                 return(False);
246         }
247
248         snum = SNUM(conn);
249
250         if((vuser != NULL) && !check_user_ok(conn, vuser, snum))
251                 return False;
252
253         if (conn->force_user || 
254             lp_security() == SEC_SHARE ||
255             !(vuser) || (vuser->guest)) {
256                 uid = conn->uid;
257                 gid = conn->gid;
258                 current_user.groups = conn->groups;
259                 current_user.ngroups = conn->ngroups;
260         } else {
261                 if (!vuser) {
262                         DEBUG(2,("Invalid vuid used %d\n",vuid));
263                         return(False);
264                 }
265                 uid = vuser->uid;
266                 if(!*lp_force_group(snum)) {
267                         gid = vuser->gid;
268                 } else {
269                         gid = conn->gid;
270                 }
271                 current_user.ngroups = vuser->n_groups;
272                 current_user.groups  = vuser->groups;
273         }
274         
275         if (initial_uid == 0)  {
276                 if (!become_gid(gid)) return(False);
277
278 #ifdef HAVE_SETGROUPS      
279                 if (!(conn && conn->ipc)) {
280                         /* groups stuff added by ih/wreu */
281                         if (current_user.ngroups > 0)
282                                 if (setgroups(current_user.ngroups,
283                                               current_user.groups)<0) {
284                                         DEBUG(0,("setgroups call failed!\n"));
285                                 }
286                 }
287 #endif
288
289                 if (!conn->admin_user && !become_uid(uid))
290                         return(False);
291         }
292         
293         current_user.conn = conn;
294         current_user.vuid = vuid;
295
296         DEBUG(5,("become_user uid=(%d,%d) gid=(%d,%d)\n",
297                  (int)getuid(),(int)geteuid(),(int)getgid(),(int)getegid()));
298   
299         return(True);
300 }
301
302 /****************************************************************************
303   unbecome the user of a connection number
304 ****************************************************************************/
305 BOOL unbecome_user(void )
306 {
307   if (!current_user.conn)
308     return(False);
309
310   dos_ChDir(OriginalDir);
311
312   if (initial_uid == 0)
313     {
314 #ifdef HAVE_SETRESUID
315       setresuid(-1,getuid(),-1);
316       setresgid(-1,getgid(),-1);
317 #else
318       if (seteuid(initial_uid) != 0) 
319         setuid(initial_uid);
320       setgid(initial_gid);
321 #endif
322     }
323
324 #ifdef NO_EID
325   if (initial_uid == 0)
326     DEBUG(2,("Running with no EID\n"));
327   initial_uid = getuid();
328   initial_gid = getgid();
329 #else
330   if (geteuid() != initial_uid) {
331           DEBUG(0,("Warning: You appear to have a trapdoor uid system\n"));
332           initial_uid = geteuid();
333   }
334   if (getegid() != initial_gid) {
335           DEBUG(0,("Warning: You appear to have a trapdoor gid system\n"));
336           initial_gid = getegid();
337   }
338 #endif
339
340   current_user.uid = initial_uid;
341   current_user.gid = initial_gid;
342   
343   if (dos_ChDir(OriginalDir) != 0)
344     DEBUG( 0, ( "chdir(%s) failed in unbecome_user\n", OriginalDir ) );
345
346   DEBUG(5,("unbecome_user now uid=(%d,%d) gid=(%d,%d)\n",
347         (int)getuid(),(int)geteuid(),(int)getgid(),(int)getegid()));
348
349   current_user.conn = NULL;
350   current_user.vuid = UID_FIELD_INVALID;
351
352   return(True);
353 }
354
355 static struct current_user current_user_saved;
356 static int become_root_depth;
357 static pstring become_root_dir;
358
359 /****************************************************************************
360 This is used when we need to do a privilaged operation (such as mucking
361 with share mode files) and temporarily need root access to do it. This
362 call should always be paired with an unbecome_root() call immediately
363 after the operation
364
365 Set save_dir if you also need to save/restore the CWD 
366 ****************************************************************************/
367 void become_root(BOOL save_dir) 
368 {
369         if (become_root_depth) {
370                 DEBUG(0,("ERROR: become root depth is non zero\n"));
371         }
372         if (save_dir)
373                 dos_GetWd(become_root_dir);
374
375         current_user_saved = current_user;
376         become_root_depth = 1;
377
378         become_uid(0);
379         become_gid(0);
380 }
381
382 /****************************************************************************
383 When the privilaged operation is over call this
384
385 Set save_dir if you also need to save/restore the CWD 
386 ****************************************************************************/
387 void unbecome_root(BOOL restore_dir)
388 {
389         if (become_root_depth != 1) {
390                 DEBUG(0,("ERROR: unbecome root depth is %d\n",
391                          become_root_depth));
392         }
393
394         /* we might have done a become_user() while running as root,
395            if we have then become root again in order to become 
396            non root! */
397         if (current_user.uid != 0) {
398                 become_uid(0);
399         }
400
401         /* restore our gid first */
402         if (!become_gid(current_user_saved.gid)) {
403                 DEBUG(0,("ERROR: Failed to restore gid\n"));
404                 exit_server("Failed to restore gid");
405         }
406
407 #ifdef HAVE_SETGROUPS      
408         if (current_user_saved.ngroups > 0) {
409                 if (setgroups(current_user_saved.ngroups,
410                               current_user_saved.groups)<0)
411                         DEBUG(0,("ERROR: setgroups call failed!\n"));
412         }
413 #endif
414
415         /* now restore our uid */
416         if (!become_uid(current_user_saved.uid)) {
417                 DEBUG(0,("ERROR: Failed to restore uid\n"));
418                 exit_server("Failed to restore uid");
419         }
420
421         if (restore_dir)
422                 dos_ChDir(become_root_dir);
423
424         current_user = current_user_saved;
425
426         become_root_depth = 0;
427 }
428
429