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