2 Unix SMB/Netbios implementation.
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.
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);
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_m(name, *lp_winbind_separator());
39 /****************************************************************************
40 Get a users home directory.
41 ****************************************************************************/
43 char *get_user_home_dir(const char *user)
45 static struct passwd *pass;
48 /* If a path is specified in [homes] then use it instead of the
49 user's home directory from struct passwd. */
51 if ((snum = lp_servicenumber(HOMES_NAME)) != -1) {
52 static pstring home_dir;
54 pstrcpy(home_dir, lp_pathname(snum));
55 standard_sub_snum(snum, home_dir);
61 /* Get home directory from struct passwd. */
63 pass = Get_Pwnam(user);
70 /*******************************************************************
71 Map a username from a dos name to a unix name by looking in the username
72 map. Note that this modifies the name in place.
73 This is the main function that should be called *once* on
74 any incoming or new username - in order to canonicalize the name.
75 This is being done to de-couple the case conversions from the user mapping
76 function. Previously, the map_username was being called
77 every time Get_Pwnam was called.
78 Returns True if username was changed, false otherwise.
79 ********************************************************************/
81 BOOL map_username(char *user)
83 static BOOL initialised=False;
84 static fstring last_from,last_to;
86 char *mapfile = lp_username_map();
89 BOOL mapped_user = False;
98 *last_from = *last_to = 0;
102 if (strequal(user,last_to))
105 if (strequal(user,last_from)) {
106 DEBUG(3,("Mapped user %s to %s\n",user,last_to));
107 fstrcpy(user,last_to);
111 f = x_fopen(mapfile,O_RDONLY, 0);
113 DEBUG(0,("can't open username map %s. Error %s\n",mapfile, strerror(errno) ));
117 DEBUG(4,("Scanning username map %s\n",mapfile));
119 while((s=fgets_slash(buf,sizeof(buf),f))!=NULL) {
121 char *dosname = strchr_m(unixname,'=');
123 BOOL return_if_mapped = False;
130 while (isspace(*unixname))
133 if ('!' == *unixname) {
134 return_if_mapped = True;
136 while (*unixname && isspace(*unixname))
140 if (!*unixname || strchr_m("#;",*unixname))
144 int l = strlen(unixname);
145 while (l && isspace(unixname[l-1])) {
151 dosuserlist = lp_list_make(dosname);
153 DEBUG(0,("Unable to build user list\n"));
157 if (strchr_m(dosname,'*') || user_in_list(user, dosuserlist)) {
158 DEBUG(3,("Mapped user %s to %s\n",user,unixname));
160 fstrcpy(last_from,user);
161 sscanf(unixname,"%s",user);
162 fstrcpy(last_to,user);
163 if(return_if_mapped) {
164 lp_list_free (&dosuserlist);
170 lp_list_free (&dosuserlist);
176 * Setup the last_from and last_to as an optimization so
177 * that we don't scan the file again for the same user.
179 fstrcpy(last_from,user);
180 fstrcpy(last_to,user);
185 /****************************************************************************
187 ****************************************************************************/
189 static struct passwd *_Get_Pwnam(const char *s)
193 ret = sys_getpwnam(s);
195 #ifdef HAVE_GETPWANAM
196 struct passwd_adjunct *pwret;
197 pwret = getpwanam(s);
198 if (pwret && pwret->pwa_passwd)
199 pstrcpy(ret->pw_passwd,pwret->pwa_passwd);
207 /****************************************************************************
208 * A wrapper for getpwnam(). The following variations are tried:
210 * - in all lower case if this differs from transmitted
211 * - in all upper case if this differs from transmitted
212 * - using lp_usernamelevel() for permutations.
213 ****************************************************************************/
215 struct passwd *Get_Pwnam_internals(const char *user, char *user2)
217 struct passwd *ret = NULL;
219 if (!user2 || !(*user2))
222 if (!user || !(*user))
225 /* Try in all lower case first as this is the most
226 common case on UNIX systems */
228 DEBUG(5,("Trying _Get_Pwnam(), username as lowercase is %s\n",user2));
229 ret = _Get_Pwnam(user2);
233 /* Try as given, if username wasn't originally lowercase */
234 if(strcmp(user,user2) != 0) {
235 DEBUG(5,("Trying _Get_Pwnam(), username as given is %s\n",user));
236 ret = _Get_Pwnam(user);
241 /* Try as uppercase, if username wasn't originally uppercase */
243 if(strcmp(user,user2) != 0) {
244 DEBUG(5,("Trying _Get_Pwnam(), username as uppercase is %s\n",user2));
245 ret = _Get_Pwnam(user2);
250 /* Try all combinations up to usernamelevel */
252 DEBUG(5,("Checking combinations of %d uppercase letters in %s\n",lp_usernamelevel(),user2));
253 ret = uname_string_combinations(user2, _Get_Pwnam, lp_usernamelevel());
256 DEBUG(5,("Get_Pwnam %s find a valid username!\n",ret ? "did":"didn't"));
260 /****************************************************************************
261 Get_Pwnam wrapper for modification.
262 NOTE: This can potentially modify 'user'!
263 ****************************************************************************/
265 struct passwd *Get_Pwnam_Modify(char *user)
270 fstrcpy(user2, user);
272 ret = Get_Pwnam_internals(user, user2);
274 /* If caller wants the modified username, ensure they get it */
277 /* We can safely assume ret is NULL if none of the above succeed */
281 /****************************************************************************
282 Get_Pwnam wrapper without modification.
283 NOTE: This with NOT modify 'user'!
284 ****************************************************************************/
286 struct passwd *Get_Pwnam(const char *user)
291 fstrcpy(user2, user);
293 ret = Get_Pwnam_internals(user, user2);
295 /* We can safely assume ret is NULL if none of the above succeed */
299 /****************************************************************************
300 Check if a user is in a netgroup user list.
301 ****************************************************************************/
303 static BOOL user_in_netgroup_list(const char *user, const char *ngname)
306 static char *mydomain = NULL;
307 if (mydomain == NULL)
308 yp_get_default_domain(&mydomain);
310 if(mydomain == NULL) {
311 DEBUG(5,("Unable to get default yp domain\n"));
315 DEBUG(5,("looking for user %s of domain %s in netgroup %s\n",
316 user, mydomain, ngname));
317 DEBUG(5,("innetgr is %s\n", innetgr(ngname, NULL, user, mydomain)
318 ? "TRUE" : "FALSE"));
320 if (innetgr(ngname, NULL, user, mydomain))
322 #endif /* HAVE_NETGROUP */
326 /****************************************************************************
327 Check if a user is in a winbind group.
328 ****************************************************************************/
330 static BOOL user_in_winbind_group_list(const char *user, const char *gname, BOOL *winbind_answered)
334 gid_t *groups = NULL;
338 *winbind_answered = False;
341 * Get the gid's that this user belongs to.
344 if ((num_groups = winbind_getgroups(user, 0, NULL)) == -1)
347 if (num_groups == 0) {
348 *winbind_answered = True;
352 if ((groups = (gid_t *)malloc(sizeof(gid_t) * num_groups )) == NULL) {
353 DEBUG(0,("user_in_winbind_group_list: malloc fail.\n"));
357 if ((num_groups = winbind_getgroups(user, num_groups, groups)) == -1) {
358 DEBUG(0,("user_in_winbind_group_list: second winbind_getgroups call \
359 failed with error %s\n", strerror(errno) ));
364 * Now we have the gid list for this user - convert the gname
365 * to a gid_t via either winbind or the local UNIX lookup and do the comparison.
368 if ((gid = nametogid(gname)) == (gid_t)-1) {
369 DEBUG(0,("user_in_winbind_group_list: winbind_lookup_name for group %s failed.\n",
374 for (i = 0; i < num_groups; i++) {
375 if (gid == groups[i]) {
381 *winbind_answered = True;
387 *winbind_answered = False;
392 /****************************************************************************
393 Check if a user is in a UNIX group.
394 ****************************************************************************/
396 static BOOL user_in_unix_group_list(const char *user,const char *gname)
398 struct passwd *pass = Get_Pwnam(user);
399 struct sys_userlist *user_list;
400 struct sys_userlist *member;
402 DEBUG(10,("user_in_unix_group_list: checking user %s in group %s\n", user, gname));
405 * We need to check the users primary group as this
406 * group is implicit and often not listed in the group database.
410 if (strequal(gname,gidtoname(pass->pw_gid))) {
411 DEBUG(10,("user_in_unix_group_list: group %s is primary group.\n", gname ));
416 user_list = get_users_in_group(gname);
417 if (user_list == NULL) {
418 DEBUG(10,("user_in_unix_group_list: no such group %s\n", gname ));
422 for (member = user_list; member; member = member->next) {
423 DEBUG(10,("user_in_unix_group_list: checking user %s against member %s\n",
424 user, member->unix_name ));
425 if (strequal(member->unix_name,user)) {
426 free_userlist(user_list);
431 free_userlist(user_list);
435 /****************************************************************************
436 Check if a user is in a group list. Ask winbind first, then use UNIX.
437 ****************************************************************************/
439 BOOL user_in_group_list(const char *user, const char *gname)
441 BOOL winbind_answered = False;
444 ret = user_in_winbind_group_list(user, gname, &winbind_answered);
445 if (!winbind_answered)
446 ret = user_in_unix_group_list(user, gname);
449 DEBUG(10,("user_in_group_list: user |%s| is in group |%s|\n", user, gname));
453 /****************************************************************************
454 Check if a user is in a user list - can check combinations of UNIX
456 ****************************************************************************/
458 BOOL user_in_list(const char *user,char **list)
463 DEBUG(10,("user_in_list: checking user %s in list\n", user));
467 DEBUG(10,("user_in_list: checking user |%s| in group |%s|\n", user, *list));
470 * Check raw username.
472 if (strequal(user, *list))
476 * Now check to see if any combination
477 * of UNIX and netgroups has been specified.
482 * Old behaviour. Check netgroup list
483 * followed by UNIX list.
485 if(user_in_netgroup_list(user, *list +1))
487 if(user_in_group_list(user, *list +1))
489 } else if (**list == '+') {
491 if((*(*list +1)) == '&') {
493 * Search UNIX list followed by netgroup.
495 if(user_in_group_list(user, *list +2))
497 if(user_in_netgroup_list(user, *list +2))
503 * Just search UNIX list.
506 if(user_in_group_list(user, *list +1))
510 } else if (**list == '&') {
512 if(*(*list +1) == '+') {
514 * Search netgroup list followed by UNIX list.
516 if(user_in_netgroup_list(user, *list +2))
518 if(user_in_group_list(user, *list +2))
522 * Just search netgroup list.
524 if(user_in_netgroup_list(user, *list +1))
527 } else if (!name_is_local(*list)) {
529 * If user name did not match and token is not
530 * a unix group and the token has a winbind separator in the
531 * name then see if it is a Windows group.
535 enum SID_NAME_USE name_type;
536 BOOL winbind_answered = False;
539 /* Check to see if name is a Windows group */
540 if (winbind_lookup_name(*list, &g_sid, &name_type) && name_type == SID_NAME_DOM_GRP) {
542 /* Check if user name is in the Windows group */
543 ret = user_in_winbind_group_list(user, *list, &winbind_answered);
545 if (winbind_answered && ret == True) {
546 DEBUG(10,("user_in_list: user |%s| is in group |%s|\n", user, *list));
557 /* The functions below have been taken from password.c and slightly modified */
558 /****************************************************************************
559 Apply a function to upper/lower case combinations
560 of a string and return true if one of them returns true.
561 Try all combinations with N uppercase letters.
562 offset is the first char to try and change (start with 0)
563 it assumes the string starts lowercased
564 ****************************************************************************/
566 static struct passwd *uname_string_combinations2(char *s,int offset,struct passwd *(*fn)(const char *),int N)
568 ssize_t len = (ssize_t)strlen(s);
572 if (N <= 0 || offset >= len)
575 for (i=offset;i<(len-(N-1));i++) {
580 ret = uname_string_combinations2(s,i+1,fn,N-1);
588 /****************************************************************************
589 Apply a function to upper/lower case combinations
590 of a string and return true if one of them returns true.
591 Try all combinations with up to N uppercase letters.
592 offset is the first char to try and change (start with 0)
593 it assumes the string starts lowercased
594 ****************************************************************************/
596 static struct passwd * uname_string_combinations(char *s,struct passwd * (*fn)(const char *),int N)
602 ret = uname_string_combinations2(s,0,fn,n);
609 /****************************************************************************
610 These wrappers allow appliance mode to work. In appliance mode the username
611 takes the form DOMAIN/user.
612 ****************************************************************************/
614 struct passwd *smb_getpwnam(char *user, BOOL allow_change)
619 extern pstring global_myname;
622 pw = Get_Pwnam_Modify(user);
624 pw = Get_Pwnam(user);
630 * If it is a domain qualified name and it isn't in our password
631 * database but the domain portion matches our local machine name then
632 * lookup just the username portion locally.
635 sep = lp_winbind_separator();
636 p = strchr_m(user,*sep);
637 if (p && strncasecmp(global_myname, user, strlen(global_myname))==0) {
639 pw = Get_Pwnam_Modify(p+1);