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