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 Splits passed user or group name to domain and user/group name parts
41 Returns True if name was splitted and False otherwise.
42 *****************************************************************/
44 BOOL split_domain_and_name(const char *name, char *domain, char* username)
46 char *p = strchr(name,*lp_winbind_separator());
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));
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());
62 DEBUG(10,("split_domain_and_name: all is fine, domain is |%s| and name is |%s|\n", domain, username));
66 /****************************************************************************
67 Get a users home directory.
68 ****************************************************************************/
70 char *get_user_home_dir(const char *user)
72 static struct passwd *pass;
74 /* Ensure the user exists. */
76 pass = Get_Pwnam(user);
80 /* Return home directory from struct passwd. */
85 /****************************************************************************
86 Get a users service home directory.
87 ****************************************************************************/
89 char *get_user_service_home_dir(const char *user)
91 static struct passwd *pass;
94 /* Ensure the user exists. */
96 pass = Get_Pwnam(user);
101 /* If a path is specified in [homes] then use it instead of the
102 user's home directory from struct passwd. */
104 if ((snum = lp_servicenumber(HOMES_NAME)) != -1) {
105 static pstring home_dir;
107 pstrcpy(home_dir, lp_pathname(snum));
108 standard_sub_home(snum, user, home_dir);
114 /* Return home directory from struct passwd. */
116 return(pass->pw_dir);
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 ********************************************************************/
130 BOOL map_username(char *user)
132 static BOOL initialised=False;
133 static fstring last_from,last_to;
135 char *mapfile = lp_username_map();
138 BOOL mapped_user = False;
147 *last_from = *last_to = 0;
151 if (strequal(user,last_to))
154 if (strequal(user,last_from)) {
155 DEBUG(3,("Mapped user %s to %s\n",user,last_to));
156 fstrcpy(user,last_to);
160 f = x_fopen(mapfile,O_RDONLY, 0);
162 DEBUG(0,("can't open username map %s. Error %s\n",mapfile, strerror(errno) ));
166 DEBUG(4,("Scanning username map %s\n",mapfile));
168 while((s=fgets_slash(buf,sizeof(buf),f))!=NULL) {
170 char *dosname = strchr_m(unixname,'=');
172 BOOL return_if_mapped = False;
179 while (isspace((int)*unixname))
182 if ('!' == *unixname) {
183 return_if_mapped = True;
185 while (*unixname && isspace((int)*unixname))
189 if (!*unixname || strchr_m("#;",*unixname))
193 int l = strlen(unixname);
194 while (l && isspace((int)unixname[l-1])) {
200 dosuserlist = lp_list_make(dosname);
202 DEBUG(0,("Unable to build user list\n"));
206 if (strchr_m(dosname,'*') || user_in_list(user, dosuserlist)) {
207 DEBUG(3,("Mapped user %s to %s\n",user,unixname));
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);
219 lp_list_free (&dosuserlist);
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.
228 fstrcpy(last_from,user);
229 fstrcpy(last_to,user);
234 /****************************************************************************
235 * A wrapper for sys_getpwnam(). The following variations are tried:
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 ****************************************************************************/
242 struct passwd *Get_Pwnam_internals(const char *user, char *user2)
244 struct passwd *ret = NULL;
246 if (!user2 || !(*user2))
249 if (!user || !(*user))
252 /* Try in all lower case first as this is the most
253 common case on UNIX systems */
255 DEBUG(5,("Trying _Get_Pwnam(), username as lowercase is %s\n",user2));
256 ret = sys_getpwnam(user2);
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);
268 /* Try as uppercase, if username wasn't originally uppercase */
270 if(strcmp(user,user2) != 0) {
271 DEBUG(5,("Trying _Get_Pwnam(), username as uppercase is %s\n",user2));
272 ret = sys_getpwnam(user2);
277 /* Try all combinations up to usernamelevel */
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());
283 DEBUG(5,("Get_Pwnam %s find a valid username!\n",ret ? "did":"didn't"));
287 /****************************************************************************
288 Get_Pwnam wrapper for modification.
289 NOTE: This can potentially modify 'user'!
290 ****************************************************************************/
292 struct passwd *Get_Pwnam_Modify(char *user)
297 fstrcpy(user2, user);
299 ret = Get_Pwnam_internals(user, user2);
301 /* If caller wants the modified username, ensure they get it */
304 /* We can safely assume ret is NULL if none of the above succeed */
308 /****************************************************************************
309 Get_Pwnam wrapper without modification.
310 NOTE: This with NOT modify 'user'!
311 ****************************************************************************/
313 struct passwd *Get_Pwnam(const char *user)
318 fstrcpy(user2, user);
320 ret = Get_Pwnam_internals(user, user2);
322 /* We can safely assume ret is NULL if none of the above succeed */
326 /****************************************************************************
327 Check if a user is in a netgroup user list.
328 ****************************************************************************/
330 static BOOL user_in_netgroup_list(const char *user, const char *ngname)
333 static char *mydomain = NULL;
334 if (mydomain == NULL)
335 yp_get_default_domain(&mydomain);
337 if(mydomain == NULL) {
338 DEBUG(5,("Unable to get default yp domain\n"));
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"));
347 if (innetgr(ngname, NULL, user, mydomain))
349 #endif /* HAVE_NETGROUP */
353 /****************************************************************************
354 Check if a user is in a winbind group.
355 ****************************************************************************/
357 static BOOL user_in_winbind_group_list(const char *user, const char *gname, BOOL *winbind_answered)
361 gid_t *groups = NULL;
365 *winbind_answered = False;
368 * Get the gid's that this user belongs to.
371 if ((num_groups = winbind_getgroups(user, 0, NULL)) == -1)
374 if (num_groups == 0) {
375 *winbind_answered = True;
379 if ((groups = (gid_t *)malloc(sizeof(gid_t) * num_groups )) == NULL) {
380 DEBUG(0,("user_in_winbind_group_list: malloc fail.\n"));
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) ));
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.
395 if ((gid = nametogid(gname)) == (gid_t)-1) {
396 DEBUG(0,("user_in_winbind_group_list: winbind_lookup_name for group %s failed.\n",
401 for (i = 0; i < num_groups; i++) {
402 if (gid == groups[i]) {
408 *winbind_answered = True;
414 *winbind_answered = False;
419 /****************************************************************************
420 Check if a user is in a UNIX group.
421 ****************************************************************************/
423 static BOOL user_in_unix_group_list(const char *user,const char *gname)
425 struct passwd *pass = Get_Pwnam(user);
426 struct sys_userlist *user_list;
427 struct sys_userlist *member;
429 DEBUG(10,("user_in_unix_group_list: checking user %s in group %s\n", user, gname));
432 * We need to check the users primary group as this
433 * group is implicit and often not listed in the group database.
437 if (strequal(gname,gidtoname(pass->pw_gid))) {
438 DEBUG(10,("user_in_unix_group_list: group %s is primary group.\n", gname ));
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 ));
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);
458 free_userlist(user_list);
462 /****************************************************************************
463 Check if a user is in a group list. Ask winbind first, then use UNIX.
464 ****************************************************************************/
466 BOOL user_in_group_list(const char *user, const char *gname)
468 BOOL winbind_answered = False;
471 ret = user_in_winbind_group_list(user, gname, &winbind_answered);
472 if (!winbind_answered)
473 ret = user_in_unix_group_list(user, gname);
476 DEBUG(10,("user_in_group_list: user |%s| is in group |%s|\n", user, gname));
480 /****************************************************************************
481 Check if a user is in a user list - can check combinations of UNIX
483 ****************************************************************************/
485 BOOL user_in_list(const char *user,char **list)
490 DEBUG(10,("user_in_list: checking user %s in list\n", user));
494 DEBUG(10,("user_in_list: checking user |%s| in group |%s|\n", user, *list));
497 * Check raw username.
499 if (strequal(user, *list))
503 * Now check to see if any combination
504 * of UNIX and netgroups has been specified.
509 * Old behaviour. Check netgroup list
510 * followed by UNIX list.
512 if(user_in_netgroup_list(user, *list +1))
514 if(user_in_group_list(user, *list +1))
516 } else if (**list == '+') {
518 if((*(*list +1)) == '&') {
520 * Search UNIX list followed by netgroup.
522 if(user_in_group_list(user, *list +2))
524 if(user_in_netgroup_list(user, *list +2))
530 * Just search UNIX list.
533 if(user_in_group_list(user, *list +1))
537 } else if (**list == '&') {
539 if(*(*list +1) == '+') {
541 * Search netgroup list followed by UNIX list.
543 if(user_in_netgroup_list(user, *list +2))
545 if(user_in_group_list(user, *list +2))
549 * Just search netgroup list.
551 if(user_in_netgroup_list(user, *list +1))
554 } else if (!name_is_local(*list)) {
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.
562 enum SID_NAME_USE name_type;
563 BOOL winbind_answered = False;
565 fstring groupname, domain;
567 /* Parse a string of the form DOMAIN/user into a domain and a user */
569 char *p = strchr(*list,*lp_winbind_separator());
571 DEBUG(10,("user_in_list: checking if user |%s| is in winbind group |%s|\n", user, *list));
574 fstrcpy(groupname, p+1);
575 fstrcpy(domain, *list);
576 domain[PTR_DIFF(p, *list)] = 0;
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) {
581 /* Check if user name is in the Windows group */
582 ret = user_in_winbind_group_list(user, *list, &winbind_answered);
584 if (winbind_answered && ret == True) {
585 DEBUG(10,("user_in_list: user |%s| is in winbind group |%s|\n", user, *list));
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 ****************************************************************************/
606 static struct passwd *uname_string_combinations2(char *s,int offset,struct passwd *(*fn)(const char *),int N)
608 ssize_t len = (ssize_t)strlen(s);
612 if (N <= 0 || offset >= len)
615 for (i=offset;i<(len-(N-1));i++) {
617 if (!islower((int)c))
620 ret = uname_string_combinations2(s,i+1,fn,N-1);
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 ****************************************************************************/
636 static struct passwd * uname_string_combinations(char *s,struct passwd * (*fn)(const char *),int N)
642 ret = uname_string_combinations2(s,0,fn,n);
649 /****************************************************************************
650 These wrappers allow appliance mode to work. In appliance mode the username
651 takes the form DOMAIN/user.
652 ****************************************************************************/
654 struct passwd *smb_getpwnam(char *user, BOOL allow_change)
659 extern pstring global_myname;
662 pw = Get_Pwnam_Modify(user);
664 pw = Get_Pwnam(user);
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.
675 sep = lp_winbind_separator();
676 p = strchr_m(user,*sep);
677 if (p && strncasecmp(global_myname, user, strlen(global_myname))==0) {
679 pw = Get_Pwnam_Modify(p+1);