merging for 2.2.6pre1
[Samba.git] / source / lib / username.c
blobc28b28bf4e9f7fb9a26168cfd9fdcc1f6ae80c19
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.
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.
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.
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.
23 #include "includes.h"
25 /* internal functions */
26 static struct passwd *uname_string_combinations(char *s, struct passwd * (*fn) (char *), int N);
27 static struct passwd *uname_string_combinations2(char *s, int offset, struct passwd * (*fn) (char *), int N);
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 *****************************************************************/
34 BOOL name_is_local(const char *name)
36 return !strchr(name, *lp_winbind_separator());
39 /****************************************************************************
40 Get a users home directory.
41 ****************************************************************************/
43 char *get_user_home_dir(char *user)
45 static struct passwd *pass;
47 pass = Get_Pwnam(user, False);
48 if (!pass)
49 return(NULL);
50 /* Return home directory from struct passwd. */
51 return(pass->pw_dir);
54 /****************************************************************************
55 Get a users home service directory.
56 ****************************************************************************/
58 char *get_user_service_home_dir(char *user)
60 static struct passwd *pass;
61 int snum;
63 /* Ensure the user exists. */
65 pass = Get_Pwnam(user, False);
66 if (!pass)
67 return(NULL);
69 /* If a path is specified in [homes] then use it instead of the
70 user's home directory from struct passwd. */
72 if ((snum = lp_servicenumber(HOMES_NAME)) != -1) {
73 static pstring home_dir;
75 pstrcpy(home_dir, lp_pathname(snum));
76 standard_sub_home(snum, user, home_dir, sizeof(home_dir));
78 if (home_dir[0])
79 return home_dir;
82 /* Return home directory from struct passwd. */
84 return(pass->pw_dir);
87 /*******************************************************************
88 Map a username from a dos name to a unix name by looking in the username
89 map. Note that this modifies the name in place.
90 This is the main function that should be called *once* on
91 any incoming or new username - in order to canonicalize the name.
92 This is being done to de-couple the case conversions from the user mapping
93 function. Previously, the map_username was being called
94 every time Get_Pwnam was called.
95 Returns True if username was changed, false otherwise.
96 ********************************************************************/
98 BOOL map_username(char *user)
100 static BOOL initialised=False;
101 static fstring last_from,last_to;
102 FILE *f;
103 char *mapfile = lp_username_map();
104 char *s;
105 pstring buf;
106 BOOL mapped_user = False;
108 if (!*user)
109 return False;
111 if (!*mapfile)
112 return False;
114 if (!initialised) {
115 *last_from = *last_to = 0;
116 initialised = True;
119 if (strequal(user,last_to))
120 return False;
122 if (strequal(user,last_from)) {
123 DEBUG(3,("Mapped user %s to %s\n",user,last_to));
124 fstrcpy(user,last_to);
125 return True;
128 f = sys_fopen(mapfile,"r");
129 if (!f) {
130 DEBUG(0,("can't open username map %s. Error %s\n",mapfile, strerror(errno) ));
131 return False;
134 DEBUG(4,("Scanning username map %s\n",mapfile));
136 while((s=fgets_slash(buf,sizeof(buf),f))!=NULL) {
137 char *unixname = s;
138 char *dosname = strchr(unixname,'=');
139 BOOL return_if_mapped = False;
141 if (!dosname)
142 continue;
144 *dosname++ = 0;
146 while (isspace((int)*unixname))
147 unixname++;
148 if ('!' == *unixname) {
149 return_if_mapped = True;
150 unixname++;
152 while (*unixname && isspace((int)*unixname))
153 unixname++;
156 if (!*unixname || strchr("#;",*unixname))
157 continue;
160 int l = strlen(unixname);
161 while (l && isspace((int)unixname[l-1])) {
162 unixname[l-1] = 0;
163 l--;
167 if (strchr(dosname,'*') || user_in_list(user,dosname)) {
168 DEBUG(3,("Mapped user %s to %s\n",user,unixname));
169 mapped_user = True;
170 fstrcpy(last_from,user);
171 sscanf(unixname,"%s",user);
172 fstrcpy(last_to,user);
173 if(return_if_mapped) {
174 fclose(f);
175 return True;
180 fclose(f);
183 * Setup the last_from and last_to as an optimization so
184 * that we don't scan the file again for the same user.
186 fstrcpy(last_from,user);
187 fstrcpy(last_to,user);
189 return mapped_user;
192 /****************************************************************************
193 Get_Pwnam wrapper.
194 ****************************************************************************/
196 static struct passwd *_Get_Pwnam(char *s)
198 struct passwd *ret;
200 ret = sys_getpwnam(s);
201 if (ret) {
202 #ifdef HAVE_GETPWANAM
203 struct passwd_adjunct *pwret;
204 pwret = getpwanam(s);
205 if (pwret && pwret->pwa_passwd)
206 pstrcpy(ret->pw_passwd,pwret->pwa_passwd);
207 #endif
210 return(ret);
213 /****************************************************************************
214 A wrapper for getpwnam(). The following variations are tried...
215 - in all lower case
216 - as transmitted IF a different case
217 - in all upper case IF that is different from the transmitted username
218 - using the lp_usernamelevel() for permutations
219 Note that this can change user! The user name is in Unix code page.
220 ****************************************************************************/
222 struct passwd *Get_Pwnam(char *user,BOOL allow_change)
224 fstring user2, orig_username;
225 int usernamelevel = lp_usernamelevel();
226 struct passwd *ret;
228 if (!user || !(*user))
229 return(NULL);
231 /* make a few copies to work with */
232 fstrcpy(orig_username, user);
233 if (!allow_change) {
234 /* allow_change was False, so make a copy and temporarily
235 assign the char* user to the temp copy */
236 fstrcpy(user2,user);
237 user = &user2[0];
240 /* try in all lower case first as this is the most
241 common case on UNIX systems */
242 unix_to_dos(user);
243 strlower(user);
244 dos_to_unix(user);
246 ret = _Get_Pwnam(user);
247 if (ret)
248 return(ret);
250 /* try as transmitted, but only if the original username
251 gives us a different case */
252 if (strcmp(user, orig_username) != 0) {
253 ret = _Get_Pwnam(orig_username);
254 if (ret) {
255 if (allow_change)
256 fstrcpy(user, orig_username);
258 return(ret);
262 /* finally, try in all caps if that is a new case */
263 unix_to_dos(user);
264 strupper(user);
265 dos_to_unix(user);
267 if (strcmp(user, orig_username) != 0) {
268 ret = _Get_Pwnam(user);
269 if (ret)
270 return(ret);
273 /* Try all combinations up to usernamelevel. */
274 unix_to_dos(user);
275 strlower(user);
276 dos_to_unix(user);
278 ret = uname_string_combinations(user, _Get_Pwnam, usernamelevel);
280 if (ret)
281 return(ret);
283 return(NULL);
286 /****************************************************************************
287 Check if a user is in a netgroup user list.
288 ****************************************************************************/
290 static BOOL user_in_netgroup_list(char *user,char *ngname)
292 #ifdef HAVE_NETGROUP
293 static char *mydomain = NULL;
294 if (mydomain == NULL)
295 yp_get_default_domain(&mydomain);
297 if(mydomain == NULL) {
298 DEBUG(5,("Unable to get default yp domain\n"));
299 return False;
302 DEBUG(5,("looking for user %s of domain %s in netgroup %s\n",
303 user, mydomain, ngname));
304 DEBUG(5,("innetgr is %s\n", innetgr(ngname, NULL, user, mydomain)
305 ? "True" : "False"));
307 if (innetgr(ngname, NULL, user, mydomain))
308 return (True);
309 #endif /* HAVE_NETGROUP */
310 return False;
313 /****************************************************************************
314 Check if a user is in a winbind group.
315 ****************************************************************************/
317 static BOOL user_in_winbind_group_list(char *user,char *gname, BOOL *winbind_answered)
319 int num_groups;
320 int i;
321 gid_t *groups = NULL;
322 gid_t gid;
323 BOOL ret = False;
325 *winbind_answered = False;
328 * Get the gid's that this user belongs to.
331 if ((num_groups = winbind_getgroups(user, 0, NULL)) == -1)
332 return False;
334 if (num_groups == 0) {
335 *winbind_answered = True;
336 return False;
339 if ((groups = (gid_t *)malloc(sizeof(gid_t) * num_groups )) == NULL) {
340 DEBUG(0,("user_in_winbind_group_list: malloc fail.\n"));
341 goto err;
344 if ((num_groups = winbind_getgroups(user, num_groups, groups)) == -1) {
345 DEBUG(0,("user_in_winbind_group_list: second winbind_getgroups call \
346 failed with error %s\n", strerror(errno) ));
347 goto err;
351 * Now we have the gid list for this user - convert the gname
352 * to a gid_t via either winbind or the local UNIX lookup and do the comparison.
355 if ((gid = nametogid(gname)) == (gid_t)-1) {
356 DEBUG(0,("user_in_winbind_group_list: winbind_lookup_name for group %s failed.\n",
357 gname ));
358 goto err;
361 for (i = 0; i < num_groups; i++) {
362 if (gid == groups[i]) {
363 ret = True;
364 break;
368 *winbind_answered = True;
369 SAFE_FREE(groups);
370 return ret;
372 err:
374 *winbind_answered = False;
375 SAFE_FREE(groups);
376 return False;
379 /****************************************************************************
380 Check if a user is in a UNIX group.
381 ****************************************************************************/
383 static BOOL user_in_unix_group_list(char *user,const char *gname)
385 struct passwd *pass = Get_Pwnam(user,False);
386 struct sys_userlist *user_list;
387 struct sys_userlist *member;
389 DEBUG(10,("user_in_unix_group_list: checking user %s in group %s\n", user, gname));
392 * We need to check the users primary group as this
393 * group is implicit and often not listed in the group database.
396 if (pass) {
397 if (strequal(gname, gidtoname(pass->pw_gid))) {
398 DEBUG(10,("user_in_unix_group_list: group %s is primary group.\n", gname ));
399 return True;
403 user_list = get_users_in_group(gname);
404 if (user_list == NULL) {
405 DEBUG(10,("user_in_unix_group_list: no such group %s\n", gname ));
406 return False;
409 for (member = user_list; member; member = member->next) {
410 DEBUG(10,("user_in_unix_group_list: checking user %s against member %s\n",
411 user, member->unix_name ));
412 if (strequal(member->unix_name,user)) {
413 free_userlist(user_list);
414 return(True);
418 free_userlist(user_list);
419 return False;
422 /****************************************************************************
423 Check if a user is in a group list. Ask winbind first, then use UNIX.
424 ****************************************************************************/
426 BOOL user_in_group_list(char *user,char *gname)
428 BOOL winbind_answered = False;
429 BOOL ret;
431 ret = user_in_winbind_group_list(user, gname, &winbind_answered);
432 if (!winbind_answered)
433 ret = user_in_unix_group_list(user, gname);
435 if (ret)
436 DEBUG(10,("user_in_group_list: user |%s| is in group |%s|\n", user, gname));
437 return ret;
440 /****************************************************************************
441 Check if a user is in a user list - can check combinations of UNIX
442 and netgroup lists.
443 ****************************************************************************/
445 BOOL user_in_list(char *user,char *list)
447 pstring tok;
448 char *p=list;
450 DEBUG(10,("user_in_list: checking user %s in list %s\n", user, list));
452 while (next_token(&p,tok,LIST_SEP, sizeof(tok))) {
454 DEBUG(10,("user_in_list: checking user |%s| against |%s|\n", user, tok));
457 * Check raw username.
459 if (strequal(user,tok))
460 return(True);
463 * Now check to see if any combination
464 * of UNIX and netgroups has been specified.
467 if(*tok == '@') {
469 * Old behaviour. Check netgroup list
470 * followed by UNIX list.
472 if(user_in_netgroup_list(user,&tok[1]))
473 return True;
474 if(user_in_group_list(user,&tok[1]))
475 return True;
476 } else if (*tok == '+') {
478 if(tok[1] == '&') {
480 * Search UNIX list followed by netgroup.
482 if(user_in_group_list(user,&tok[2]))
483 return True;
484 if(user_in_netgroup_list(user,&tok[2]))
485 return True;
487 } else {
490 * Just search UNIX list.
493 if(user_in_group_list(user,&tok[1]))
494 return True;
497 } else if (*tok == '&') {
499 if(tok[1] == '+') {
501 * Search netgroup list followed by UNIX list.
503 if(user_in_netgroup_list(user,&tok[2]))
504 return True;
505 if(user_in_group_list(user,&tok[2]))
506 return True;
507 } else {
509 * Just search netgroup list.
511 if(user_in_netgroup_list(user,&tok[1]))
512 return True;
514 } else if (!name_is_local(tok)) {
516 * If user name did not match and token is not
517 * a unix group and the token has a winbind separator in the
518 * name then see if it is a Windows group.
521 DOM_SID g_sid;
522 enum SID_NAME_USE name_type;
523 BOOL winbind_answered = False;
524 BOOL ret;
526 /* Check to see if name is a Windows group */
527 if (winbind_lookup_name(NULL, tok, &g_sid, &name_type) && name_type == SID_NAME_DOM_GRP) {
529 /* Check if user name is in the Windows group */
530 ret = user_in_winbind_group_list(user, tok, &winbind_answered);
532 if (winbind_answered && ret == True) {
533 DEBUG(10,("user_in_list: user |%s| is in group |%s|\n", user, tok));
534 return ret;
539 return(False);
542 /* The functions below have been taken from password.c and slightly modified */
543 /****************************************************************************
544 Apply a function to upper/lower case combinations
545 of a string and return true if one of them returns true.
546 Try all combinations with N uppercase letters.
547 offset is the first char to try and change (start with 0)
548 it assumes the string starts lowercased
549 ****************************************************************************/
551 static struct passwd *uname_string_combinations2(char *s,int offset,struct passwd *(*fn)(char *),int N)
553 ssize_t len = (ssize_t)strlen(s);
554 int i;
555 struct passwd *ret;
557 if (N <= 0 || offset >= len)
558 return(fn(s));
560 for (i=offset;i<(len-(N-1));i++) {
561 char c = s[i];
562 if (!islower((int)c))
563 continue;
564 s[i] = toupper(c);
565 ret = uname_string_combinations2(s,i+1,fn,N-1);
566 if(ret)
567 return(ret);
568 s[i] = c;
570 return(NULL);
573 /****************************************************************************
574 Apply a function to upper/lower case combinations
575 of a string and return true if one of them returns true.
576 Try all combinations with up to N uppercase letters.
577 offset is the first char to try and change (start with 0)
578 it assumes the string starts lowercased
579 ****************************************************************************/
581 static struct passwd * uname_string_combinations(char *s,struct passwd * (*fn)(char *),int N)
583 int n;
584 struct passwd *ret;
586 for (n=1;n<=N;n++) {
587 ret = uname_string_combinations2(s,0,fn,n);
588 if(ret)
589 return(ret);
591 return(NULL);
595 /****************************************************************************
596 these wrappers allow appliance mode to work. In appliance mode the username
597 takes the form DOMAIN/user
598 ****************************************************************************/
599 struct passwd *smb_getpwnam(char *user, BOOL allow_change)
601 struct passwd *pw;
602 char *p;
603 char *sep;
604 extern pstring global_myname;
606 pw = Get_Pwnam(user, allow_change);
607 if (pw)
608 return pw;
611 * If it is a domain qualified name and it isn't in our password
612 * database but the domain portion matches our local machine name then
613 * lookup just the username portion locally.
616 sep = lp_winbind_separator();
617 p = strchr(user,*sep);
618 if (p && strncasecmp(global_myname, user, strlen(global_myname))==0)
619 return Get_Pwnam(p+1, allow_change);
621 return NULL;