A fix to override the location of a user's home directory if it is
[gd/samba-autobuild/.git] / source3 / lib / username.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    Username handling
5    Copyright (C) Andrew Tridgell 1992-1998
6    Copyright (C) Jeremy Allison 1997-2001.
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24
25 /* internal functions */
26 static struct passwd *uname_string_combinations(char *s, struct passwd * (*fn) (const char *), int N);
27 static struct passwd *uname_string_combinations2(char *s, int offset, struct passwd * (*fn) (const char *), int N);
28
29 /*****************************************************************
30  Check if a user or group name is local (this is a *local* name for
31  *local* people, there's nothing for you here...).
32 *****************************************************************/
33
34 BOOL name_is_local(const char *name)
35 {
36         return !strchr_m(name, *lp_winbind_separator());
37 }
38
39 /****************************************************************************
40  Get a users home directory.
41 ****************************************************************************/
42
43 char *get_user_home_dir(const char *user)
44 {
45         static struct passwd *pass;
46         int snum;
47
48         /* If a path is specified in [homes] then use it instead of the
49            user's home directory from struct passwd. */
50
51         if ((snum = lp_servicenumber(HOMES_NAME)) != -1) {
52                 static pstring home_dir;
53                 
54                 pstrcpy(home_dir, lp_pathname(snum));
55                 standard_sub_snum(snum, home_dir);
56
57                 if (home_dir[0])
58                         return home_dir;
59         }
60
61         /* Get home directory from struct passwd. */
62
63         pass = Get_Pwnam(user);
64
65         if (!pass)
66                 return(NULL);
67         return(pass->pw_dir);      
68 }
69
70 /*******************************************************************
71  Map a username from a dos name to a unix name by looking in the username
72  map. Note that this modifies the name in place.
73  This is the main function that should be called *once* on
74  any incoming or new username - in order to canonicalize the name.
75  This is being done to de-couple the case conversions from the user mapping
76  function. Previously, the map_username was being called
77  every time Get_Pwnam was called.
78  Returns True if username was changed, false otherwise.
79 ********************************************************************/
80
81 BOOL map_username(char *user)
82 {
83         static BOOL initialised=False;
84         static fstring last_from,last_to;
85         XFILE *f;
86         char *mapfile = lp_username_map();
87         char *s;
88         pstring buf;
89         BOOL mapped_user = False;
90
91         if (!*user)
92                 return False;
93
94         if (!*mapfile)
95                 return False;
96
97         if (!initialised) {
98                 *last_from = *last_to = 0;
99                 initialised = True;
100         }
101
102         if (strequal(user,last_to))
103                 return False;
104
105         if (strequal(user,last_from)) {
106                 DEBUG(3,("Mapped user %s to %s\n",user,last_to));
107                 fstrcpy(user,last_to);
108                 return True;
109         }
110   
111         f = x_fopen(mapfile,O_RDONLY, 0);
112         if (!f) {
113                 DEBUG(0,("can't open username map %s. Error %s\n",mapfile, strerror(errno) ));
114                 return False;
115         }
116
117         DEBUG(4,("Scanning username map %s\n",mapfile));
118
119         while((s=fgets_slash(buf,sizeof(buf),f))!=NULL) {
120                 char *unixname = s;
121                 char *dosname = strchr_m(unixname,'=');
122                 char **dosuserlist;
123                 BOOL return_if_mapped = False;
124
125                 if (!dosname)
126                         continue;
127
128                 *dosname++ = 0;
129
130                 while (isspace(*unixname))
131                         unixname++;
132
133                 if ('!' == *unixname) {
134                         return_if_mapped = True;
135                         unixname++;
136                         while (*unixname && isspace(*unixname))
137                                 unixname++;
138                 }
139     
140                 if (!*unixname || strchr_m("#;",*unixname))
141                         continue;
142
143                 {
144                         int l = strlen(unixname);
145                         while (l && isspace(unixname[l-1])) {
146                                 unixname[l-1] = 0;
147                                 l--;
148                         }
149                 }
150
151                 dosuserlist = lp_list_make(dosname);
152                 if (!dosuserlist) {
153                         DEBUG(0,("Unable to build user list\n"));
154                         return False;
155                 }
156
157                 if (strchr_m(dosname,'*') || user_in_list(user, dosuserlist)) {
158                         DEBUG(3,("Mapped user %s to %s\n",user,unixname));
159                         mapped_user = True;
160                         fstrcpy(last_from,user);
161                         sscanf(unixname,"%s",user);
162                         fstrcpy(last_to,user);
163                         if(return_if_mapped) {
164                                 lp_list_free (&dosuserlist);
165                                 x_fclose(f);
166                                 return True;
167                         }
168                 }
169     
170                 lp_list_free (&dosuserlist);
171         }
172
173         x_fclose(f);
174
175         /*
176          * Setup the last_from and last_to as an optimization so 
177          * that we don't scan the file again for the same user.
178          */
179         fstrcpy(last_from,user);
180         fstrcpy(last_to,user);
181
182         return mapped_user;
183 }
184
185 /****************************************************************************
186  Get_Pwnam wrapper
187 ****************************************************************************/
188
189 static struct passwd *_Get_Pwnam(const char *s)
190 {
191         struct passwd *ret;
192
193         ret = sys_getpwnam(s);
194         if (ret) {
195 #ifdef HAVE_GETPWANAM
196                 struct passwd_adjunct *pwret;
197                 pwret = getpwanam(s);
198                 if (pwret && pwret->pwa_passwd)
199                         pstrcpy(ret->pw_passwd,pwret->pwa_passwd);
200 #endif
201         }
202
203         return(ret);
204 }
205
206
207 /****************************************************************************
208  * A wrapper for getpwnam().  The following variations are tried:
209  *   - as transmitted
210  *   - in all lower case if this differs from transmitted
211  *   - in all upper case if this differs from transmitted
212  *   - using lp_usernamelevel() for permutations.
213 ****************************************************************************/
214
215 struct passwd *Get_Pwnam_internals(const char *user, char *user2)
216 {
217         struct passwd *ret = NULL;
218
219         if (!user2 || !(*user2))
220                 return(NULL);
221
222         if (!user || !(*user))
223                 return(NULL);
224
225         /* Try in all lower case first as this is the most 
226            common case on UNIX systems */
227         strlower(user2);
228         DEBUG(5,("Trying _Get_Pwnam(), username as lowercase is %s\n",user2));
229         ret = _Get_Pwnam(user2);
230         if(ret)
231                 goto done;
232
233         /* Try as given, if username wasn't originally lowercase */
234         if(strcmp(user,user2) != 0) {
235                 DEBUG(5,("Trying _Get_Pwnam(), username as given is %s\n",user));
236                 ret = _Get_Pwnam(user);
237                 if(ret)
238                         goto done;
239         }       
240
241         /* Try as uppercase, if username wasn't originally uppercase */
242         strupper(user2);
243         if(strcmp(user,user2) != 0) {
244                 DEBUG(5,("Trying _Get_Pwnam(), username as uppercase is %s\n",user2));
245                 ret = _Get_Pwnam(user2);
246                 if(ret)
247                         goto done;
248         }
249
250         /* Try all combinations up to usernamelevel */
251         strlower(user2);
252         DEBUG(5,("Checking combinations of %d uppercase letters in %s\n",lp_usernamelevel(),user2));
253         ret = uname_string_combinations(user2, _Get_Pwnam, lp_usernamelevel());
254
255 done:
256         DEBUG(5,("Get_Pwnam %s find a valid username!\n",ret ? "did":"didn't"));
257         return ret;
258 }
259
260 /****************************************************************************
261  Get_Pwnam wrapper for modification.
262   NOTE: This can potentially modify 'user'! 
263 ****************************************************************************/
264
265 struct passwd *Get_Pwnam_Modify(char *user)
266 {
267         fstring user2;
268         struct passwd *ret;
269
270         fstrcpy(user2, user);
271
272         ret = Get_Pwnam_internals(user, user2);
273         
274         /* If caller wants the modified username, ensure they get it  */
275         fstrcpy(user,user2);
276
277         /* We can safely assume ret is NULL if none of the above succeed */
278         return(ret);  
279 }
280
281 /****************************************************************************
282  Get_Pwnam wrapper without modification.
283   NOTE: This with NOT modify 'user'! 
284 ****************************************************************************/
285
286 struct passwd *Get_Pwnam(const char *user)
287 {
288         fstring user2;
289         struct passwd *ret;
290
291         fstrcpy(user2, user);
292
293         ret = Get_Pwnam_internals(user, user2);
294         
295         /* We can safely assume ret is NULL if none of the above succeed */
296         return(ret);  
297 }
298
299 /****************************************************************************
300  Check if a user is in a netgroup user list.
301 ****************************************************************************/
302
303 static BOOL user_in_netgroup_list(const char *user, const char *ngname)
304 {
305 #ifdef HAVE_NETGROUP
306         static char *mydomain = NULL;
307         if (mydomain == NULL)
308                 yp_get_default_domain(&mydomain);
309
310         if(mydomain == NULL) {
311                 DEBUG(5,("Unable to get default yp domain\n"));
312                 return False;
313         }
314
315         DEBUG(5,("looking for user %s of domain %s in netgroup %s\n",
316                 user, mydomain, ngname));
317         DEBUG(5,("innetgr is %s\n", innetgr(ngname, NULL, user, mydomain)
318                 ? "TRUE" : "FALSE"));
319
320         if (innetgr(ngname, NULL, user, mydomain))
321                 return (True);
322 #endif /* HAVE_NETGROUP */
323         return False;
324 }
325
326 /****************************************************************************
327  Check if a user is in a winbind group.
328 ****************************************************************************/
329   
330 static BOOL user_in_winbind_group_list(const char *user, const char *gname, BOOL *winbind_answered)
331 {
332         int num_groups;
333         int i;
334         gid_t *groups = NULL;
335         gid_t gid;
336         BOOL ret = False;
337  
338         *winbind_answered = False;
339  
340         /*
341          * Get the gid's that this user belongs to.
342          */
343  
344         if ((num_groups = winbind_getgroups(user, 0, NULL)) == -1)
345                 return False;
346  
347         if (num_groups == 0) {
348                 *winbind_answered = True;
349                 return False;
350         }
351  
352         if ((groups = (gid_t *)malloc(sizeof(gid_t) * num_groups )) == NULL) {
353                 DEBUG(0,("user_in_winbind_group_list: malloc fail.\n"));
354                 goto err;
355         }
356  
357         if ((num_groups = winbind_getgroups(user, num_groups, groups)) == -1) {
358                 DEBUG(0,("user_in_winbind_group_list: second winbind_getgroups call \
359 failed with error %s\n", strerror(errno) ));
360                 goto err;
361         }
362  
363         /*
364          * Now we have the gid list for this user - convert the gname
365          * to a gid_t via either winbind or the local UNIX lookup and do the comparison.
366          */
367  
368         if ((gid = nametogid(gname)) == (gid_t)-1) {
369                 DEBUG(0,("user_in_winbind_group_list: winbind_lookup_name for group %s failed.\n",
370                         gname ));
371                 goto err;
372         }
373  
374         for (i = 0; i < num_groups; i++) {
375                 if (gid == groups[i]) {
376                         ret = True;
377                         break;
378                 }
379         }
380  
381         *winbind_answered = True;
382         SAFE_FREE(groups);
383         return ret;
384  
385    err:
386  
387         *winbind_answered = False;
388         SAFE_FREE(groups);
389         return False;
390 }             
391  
392 /****************************************************************************
393  Check if a user is in a UNIX group.
394 ****************************************************************************/
395
396 static BOOL user_in_unix_group_list(const char *user,const char *gname)
397 {
398         struct passwd *pass = Get_Pwnam(user);
399         struct sys_userlist *user_list;
400         struct sys_userlist *member;
401
402         DEBUG(10,("user_in_unix_group_list: checking user %s in group %s\n", user, gname));
403
404         /*
405          * We need to check the users primary group as this
406          * group is implicit and often not listed in the group database.
407          */
408  
409         if (pass) {
410                 if (strequal(gname,gidtoname(pass->pw_gid))) {
411                         DEBUG(10,("user_in_unix_group_list: group %s is primary group.\n", gname ));
412                         return True;
413                 }
414         }
415  
416         user_list = get_users_in_group(gname);
417         if (user_list == NULL) {
418                 DEBUG(10,("user_in_unix_group_list: no such group %s\n", gname ));
419                 return False;
420         }
421
422         for (member = user_list; member; member = member->next) {
423                 DEBUG(10,("user_in_unix_group_list: checking user %s against member %s\n",
424                         user, member->unix_name ));
425                 if (strequal(member->unix_name,user)) {
426                         free_userlist(user_list);
427                         return(True);
428                 }
429         }
430
431         free_userlist(user_list);
432         return False;
433 }             
434
435 /****************************************************************************
436  Check if a user is in a group list. Ask winbind first, then use UNIX.
437 ****************************************************************************/
438
439 BOOL user_in_group_list(const char *user, const char *gname)
440 {
441         BOOL winbind_answered = False;
442         BOOL ret;
443
444         ret = user_in_winbind_group_list(user, gname, &winbind_answered);
445         if (!winbind_answered)
446                 ret = user_in_unix_group_list(user, gname);
447
448         if (ret)
449                 DEBUG(10,("user_in_group_list: user |%s| is in group |%s|\n", user, gname));
450         return ret;
451 }
452
453 /****************************************************************************
454  Check if a user is in a user list - can check combinations of UNIX
455  and netgroup lists.
456 ****************************************************************************/
457
458 BOOL user_in_list(const char *user,char **list)
459 {
460         if (!list || !*list)
461                 return False;
462
463         DEBUG(10,("user_in_list: checking user %s in list\n", user));
464
465         while (*list) {
466
467                 DEBUG(10,("user_in_list: checking user |%s| in group |%s|\n", user, *list));
468
469                 /*
470                  * Check raw username.
471                  */
472                 if (strequal(user, *list))
473                         return(True);
474
475                 /*
476                  * Now check to see if any combination
477                  * of UNIX and netgroups has been specified.
478                  */
479
480                 if(**list == '@') {
481                         /*
482                          * Old behaviour. Check netgroup list
483                          * followed by UNIX list.
484                          */
485                         if(user_in_netgroup_list(user, *list +1))
486                                 return True;
487                         if(user_in_group_list(user, *list +1))
488                                 return True;
489                 } else if (**list == '+') {
490
491                         if((*(*list +1)) == '&') {
492                                 /*
493                                  * Search UNIX list followed by netgroup.
494                                  */
495                                 if(user_in_group_list(user, *list +2))
496                                         return True;
497                                 if(user_in_netgroup_list(user, *list +2))
498                                         return True;
499
500                         } else {
501
502                                 /*
503                                  * Just search UNIX list.
504                                  */
505
506                                 if(user_in_group_list(user, *list +1))
507                                         return True;
508                         }
509
510                 } else if (**list == '&') {
511
512                         if(*(*list +1) == '+') {
513                                 /*
514                                  * Search netgroup list followed by UNIX list.
515                                  */
516                                 if(user_in_netgroup_list(user, *list +2))
517                                         return True;
518                                 if(user_in_group_list(user, *list +2))
519                                         return True;
520                         } else {
521                                 /*
522                                  * Just search netgroup list.
523                                  */
524                                 if(user_in_netgroup_list(user, *list +1))
525                                         return True;
526                         }
527                 } else if (!name_is_local(*list)) {
528                         /*
529                          * If user name did not match and token is not
530                          * a unix group and the token has a winbind separator in the
531                          * name then see if it is a Windows group.
532                          */
533
534                         DOM_SID g_sid;
535                         enum SID_NAME_USE name_type;
536                         BOOL winbind_answered = False;
537                         BOOL ret;
538
539                         /* Check to see if name is a Windows group */
540                         if (winbind_lookup_name(*list, &g_sid, &name_type) && name_type == SID_NAME_DOM_GRP) {
541
542                                 /* Check if user name is in the Windows group */
543                                 ret = user_in_winbind_group_list(user, *list, &winbind_answered);
544
545                                 if (winbind_answered && ret == True) {
546                                         DEBUG(10,("user_in_list: user |%s| is in group |%s|\n", user, *list));
547                                         return ret;
548                                 }
549                         }
550                 }
551     
552                 list++;
553         }
554         return(False);
555 }
556
557 /* The functions below have been taken from password.c and slightly modified */
558 /****************************************************************************
559  Apply a function to upper/lower case combinations
560  of a string and return true if one of them returns true.
561  Try all combinations with N uppercase letters.
562  offset is the first char to try and change (start with 0)
563  it assumes the string starts lowercased
564 ****************************************************************************/
565
566 static struct passwd *uname_string_combinations2(char *s,int offset,struct passwd *(*fn)(const char *),int N)
567 {
568         ssize_t len = (ssize_t)strlen(s);
569         int i;
570         struct passwd *ret;
571
572         if (N <= 0 || offset >= len)
573                 return(fn(s));
574
575         for (i=offset;i<(len-(N-1));i++) {
576                 char c = s[i];
577                 if (!islower(c))
578                         continue;
579                 s[i] = toupper(c);
580                 ret = uname_string_combinations2(s,i+1,fn,N-1);
581                 if(ret)
582                         return(ret);
583                 s[i] = c;
584         }
585         return(NULL);
586 }
587
588 /****************************************************************************
589  Apply a function to upper/lower case combinations
590  of a string and return true if one of them returns true.
591  Try all combinations with up to N uppercase letters.
592  offset is the first char to try and change (start with 0)
593  it assumes the string starts lowercased
594 ****************************************************************************/
595
596 static struct passwd * uname_string_combinations(char *s,struct passwd * (*fn)(const char *),int N)
597 {
598         int n;
599         struct passwd *ret;
600
601         for (n=1;n<=N;n++) {
602                 ret = uname_string_combinations2(s,0,fn,n);
603                 if(ret)
604                         return(ret);
605         }  
606         return(NULL);
607 }
608
609 /****************************************************************************
610  These wrappers allow appliance mode to work. In appliance mode the username
611  takes the form DOMAIN/user.
612 ****************************************************************************/
613
614 struct passwd *smb_getpwnam(char *user, BOOL allow_change)
615 {
616         struct passwd *pw;
617         char *p;
618         char *sep;
619         extern pstring global_myname;
620
621         if (allow_change)
622                 pw = Get_Pwnam_Modify(user);
623         else
624                 pw = Get_Pwnam(user);
625
626         if (pw)
627                 return pw;
628
629         /*
630          * If it is a domain qualified name and it isn't in our password
631          * database but the domain portion matches our local machine name then
632          * lookup just the username portion locally.
633          */
634
635         sep = lp_winbind_separator();
636         p = strchr_m(user,*sep);
637         if (p && strncasecmp(global_myname, user, strlen(global_myname))==0) {
638                 if (allow_change)
639                         pw = Get_Pwnam_Modify(p+1);
640                 else
641                         pw = Get_Pwnam(p+1);
642         }
643         return NULL;
644 }