Make this function static
[nivanova/samba-autobuild/.git] / source3 / 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 %s find a valid username!\n",ret ? "did":"didn't"));
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         ret = Get_Pwnam_internals(user, user2);
320         
321         /* We can safely assume ret is NULL if none of the above succeed */
322         return(ret);  
323 }
324
325 /****************************************************************************
326  Check if a user is in a netgroup user list.
327 ****************************************************************************/
328
329 static BOOL user_in_netgroup_list(const char *user, const char *ngname)
330 {
331 #ifdef HAVE_NETGROUP
332         static char *mydomain = NULL;
333         if (mydomain == NULL)
334                 yp_get_default_domain(&mydomain);
335
336         if(mydomain == NULL) {
337                 DEBUG(5,("Unable to get default yp domain\n"));
338                 return False;
339         }
340
341         DEBUG(5,("looking for user %s of domain %s in netgroup %s\n",
342                 user, mydomain, ngname));
343         DEBUG(5,("innetgr is %s\n", innetgr(ngname, NULL, user, mydomain)
344                 ? "TRUE" : "FALSE"));
345
346         if (innetgr(ngname, NULL, user, mydomain))
347                 return (True);
348 #endif /* HAVE_NETGROUP */
349         return False;
350 }
351
352 /****************************************************************************
353  Check if a user is in a winbind group.
354 ****************************************************************************/
355   
356 static BOOL user_in_winbind_group_list(const char *user, const char *gname, BOOL *winbind_answered)
357 {
358         int num_groups;
359         int i;
360         gid_t *groups = NULL;
361         gid_t gid;
362         BOOL ret = False;
363  
364         *winbind_answered = False;
365  
366         /*
367          * Get the gid's that this user belongs to.
368          */
369  
370         if ((num_groups = winbind_getgroups(user, 0, NULL)) == -1)
371                 return False;
372  
373         if (num_groups == 0) {
374                 *winbind_answered = True;
375                 return False;
376         }
377  
378         if ((groups = (gid_t *)malloc(sizeof(gid_t) * num_groups )) == NULL) {
379                 DEBUG(0,("user_in_winbind_group_list: malloc fail.\n"));
380                 goto err;
381         }
382  
383         if ((num_groups = winbind_getgroups(user, num_groups, groups)) == -1) {
384                 DEBUG(0,("user_in_winbind_group_list: second winbind_getgroups call \
385 failed with error %s\n", strerror(errno) ));
386                 goto err;
387         }
388  
389         /*
390          * Now we have the gid list for this user - convert the gname
391          * to a gid_t via either winbind or the local UNIX lookup and do the comparison.
392          */
393  
394         if ((gid = nametogid(gname)) == (gid_t)-1) {
395                 DEBUG(0,("user_in_winbind_group_list: winbind_lookup_name for group %s failed.\n",
396                         gname ));
397                 goto err;
398         }
399  
400         for (i = 0; i < num_groups; i++) {
401                 if (gid == groups[i]) {
402                         ret = True;
403                         break;
404                 }
405         }
406  
407         *winbind_answered = True;
408         SAFE_FREE(groups);
409         return ret;
410  
411    err:
412  
413         *winbind_answered = False;
414         SAFE_FREE(groups);
415         return False;
416 }             
417  
418 /****************************************************************************
419  Check if a user is in a UNIX group.
420 ****************************************************************************/
421
422 static BOOL user_in_unix_group_list(const char *user,const char *gname)
423 {
424         struct passwd *pass = Get_Pwnam(user);
425         struct sys_userlist *user_list;
426         struct sys_userlist *member;
427
428         DEBUG(10,("user_in_unix_group_list: checking user %s in group %s\n", user, gname));
429
430         /*
431          * We need to check the users primary group as this
432          * group is implicit and often not listed in the group database.
433          */
434  
435         if (pass) {
436                 if (strequal(gname,gidtoname(pass->pw_gid))) {
437                         DEBUG(10,("user_in_unix_group_list: group %s is primary group.\n", gname ));
438                         return True;
439                 }
440         }
441  
442         user_list = get_users_in_group(gname);
443         if (user_list == NULL) {
444                 DEBUG(10,("user_in_unix_group_list: no such group %s\n", gname ));
445                 return False;
446         }
447
448         for (member = user_list; member; member = member->next) {
449                 DEBUG(10,("user_in_unix_group_list: checking user %s against member %s\n",
450                         user, member->unix_name ));
451                 if (strequal(member->unix_name,user)) {
452                         free_userlist(user_list);
453                         return(True);
454                 }
455         }
456
457         free_userlist(user_list);
458         return False;
459 }             
460
461 /****************************************************************************
462  Check if a user is in a group list. Ask winbind first, then use UNIX.
463 ****************************************************************************/
464
465 BOOL user_in_group_list(const char *user, const char *gname)
466 {
467         BOOL winbind_answered = False;
468         BOOL ret;
469
470         ret = user_in_winbind_group_list(user, gname, &winbind_answered);
471         if (!winbind_answered)
472                 ret = user_in_unix_group_list(user, gname);
473
474         if (ret)
475                 DEBUG(10,("user_in_group_list: user |%s| is in group |%s|\n", user, gname));
476         return ret;
477 }
478
479 /****************************************************************************
480  Check if a user is in a user list - can check combinations of UNIX
481  and netgroup lists.
482 ****************************************************************************/
483
484 BOOL user_in_list(const char *user,char **list)
485 {
486         if (!list || !*list)
487                 return False;
488
489         DEBUG(10,("user_in_list: checking user %s in list\n", user));
490
491         while (*list) {
492
493                 DEBUG(10,("user_in_list: checking user |%s| in group |%s|\n", user, *list));
494
495                 /*
496                  * Check raw username.
497                  */
498                 if (strequal(user, *list))
499                         return(True);
500
501                 /*
502                  * Now check to see if any combination
503                  * of UNIX and netgroups has been specified.
504                  */
505
506                 if(**list == '@') {
507                         /*
508                          * Old behaviour. Check netgroup list
509                          * followed by UNIX list.
510                          */
511                         if(user_in_netgroup_list(user, *list +1))
512                                 return True;
513                         if(user_in_group_list(user, *list +1))
514                                 return True;
515                 } else if (**list == '+') {
516
517                         if((*(*list +1)) == '&') {
518                                 /*
519                                  * Search UNIX list followed by netgroup.
520                                  */
521                                 if(user_in_group_list(user, *list +2))
522                                         return True;
523                                 if(user_in_netgroup_list(user, *list +2))
524                                         return True;
525
526                         } else {
527
528                                 /*
529                                  * Just search UNIX list.
530                                  */
531
532                                 if(user_in_group_list(user, *list +1))
533                                         return True;
534                         }
535
536                 } else if (**list == '&') {
537
538                         if(*(*list +1) == '+') {
539                                 /*
540                                  * Search netgroup list followed by UNIX list.
541                                  */
542                                 if(user_in_netgroup_list(user, *list +2))
543                                         return True;
544                                 if(user_in_group_list(user, *list +2))
545                                         return True;
546                         } else {
547                                 /*
548                                  * Just search netgroup list.
549                                  */
550                                 if(user_in_netgroup_list(user, *list +1))
551                                         return True;
552                         }
553                 } else if (!name_is_local(*list)) {
554                         /*
555                          * If user name did not match and token is not
556                          * a unix group and the token has a winbind separator in the
557                          * name then see if it is a Windows group.
558                          */
559
560                         DOM_SID g_sid;
561                         enum SID_NAME_USE name_type;
562                         BOOL winbind_answered = False;
563                         BOOL ret;
564                         fstring groupname, domain;
565                         
566                         /* Parse a string of the form DOMAIN/user into a domain and a user */
567
568                         char *p = strchr(*list,*lp_winbind_separator());
569                         
570                         DEBUG(10,("user_in_list: checking if user |%s| is in winbind group |%s|\n", user, *list));
571
572                         if (p) {
573                                 fstrcpy(groupname, p+1);
574                                 fstrcpy(domain, *list);
575                                 domain[PTR_DIFF(p, *list)] = 0;
576
577                                 /* Check to see if name is a Windows group */
578                                 if (winbind_lookup_name(domain, groupname, &g_sid, &name_type) && name_type == SID_NAME_DOM_GRP) {
579                                         
580                                 /* Check if user name is in the Windows group */
581                                         ret = user_in_winbind_group_list(user, *list, &winbind_answered);
582                                         
583                                         if (winbind_answered && ret == True) {
584                                                 DEBUG(10,("user_in_list: user |%s| is in winbind group |%s|\n", user, *list));
585                                                 return ret;
586                                         }
587                                 }
588                         }
589                 }
590     
591                 list++;
592         }
593         return(False);
594 }
595
596 /* The functions below have been taken from password.c and slightly modified */
597 /****************************************************************************
598  Apply a function to upper/lower case combinations
599  of a string and return true if one of them returns true.
600  Try all combinations with N uppercase letters.
601  offset is the first char to try and change (start with 0)
602  it assumes the string starts lowercased
603 ****************************************************************************/
604
605 static struct passwd *uname_string_combinations2(char *s,int offset,struct passwd *(*fn)(const char *),int N)
606 {
607         ssize_t len = (ssize_t)strlen(s);
608         int i;
609         struct passwd *ret;
610
611         if (N <= 0 || offset >= len)
612                 return(fn(s));
613
614         for (i=offset;i<(len-(N-1));i++) {
615                 char c = s[i];
616                 if (!islower((int)c))
617                         continue;
618                 s[i] = toupper(c);
619                 ret = uname_string_combinations2(s,i+1,fn,N-1);
620                 if(ret)
621                         return(ret);
622                 s[i] = c;
623         }
624         return(NULL);
625 }
626
627 /****************************************************************************
628  Apply a function to upper/lower case combinations
629  of a string and return true if one of them returns true.
630  Try all combinations with up to N uppercase letters.
631  offset is the first char to try and change (start with 0)
632  it assumes the string starts lowercased
633 ****************************************************************************/
634
635 static struct passwd * uname_string_combinations(char *s,struct passwd * (*fn)(const char *),int N)
636 {
637         int n;
638         struct passwd *ret;
639
640         for (n=1;n<=N;n++) {
641                 ret = uname_string_combinations2(s,0,fn,n);
642                 if(ret)
643                         return(ret);
644         }  
645         return(NULL);
646 }
647
648 /****************************************************************************
649  These wrappers allow appliance mode to work. In appliance mode the username
650  takes the form DOMAIN/user.
651 ****************************************************************************/
652
653 struct passwd *smb_getpwnam(char *user, BOOL allow_change)
654 {
655         struct passwd *pw;
656         char *p;
657         char *sep;
658         extern pstring global_myname;
659
660         if (allow_change)
661                 pw = Get_Pwnam_Modify(user);
662         else
663                 pw = Get_Pwnam(user);
664
665         if (pw)
666                 return pw;
667
668         /*
669          * If it is a domain qualified name and it isn't in our password
670          * database but the domain portion matches our local machine name then
671          * lookup just the username portion locally.
672          */
673
674         sep = lp_winbind_separator();
675         p = strchr_m(user,*sep);
676         if (p && strncasecmp(global_myname, user, strlen(global_myname))==0) {
677                 if (allow_change)
678                         pw = Get_Pwnam_Modify(p+1);
679                 else
680                         pw = Get_Pwnam(p+1);
681         }
682         return NULL;
683 }