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
) (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
);
50 /* Return home directory from struct passwd. */
54 /****************************************************************************
55 Get a users home service directory.
56 ****************************************************************************/
58 char *get_user_service_home_dir(char *user
)
60 static struct passwd
*pass
;
63 /* Ensure the user exists. */
65 pass
= Get_Pwnam(user
, False
);
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
));
82 /* Return home directory from struct passwd. */
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
;
103 char *mapfile
= lp_username_map();
106 BOOL mapped_user
= False
;
115 *last_from
= *last_to
= 0;
119 if (strequal(user
,last_to
))
122 if (strequal(user
,last_from
)) {
123 DEBUG(3,("Mapped user %s to %s\n",user
,last_to
));
124 fstrcpy(user
,last_to
);
128 f
= sys_fopen(mapfile
,"r");
130 DEBUG(0,("can't open username map %s. Error %s\n",mapfile
, strerror(errno
) ));
134 DEBUG(4,("Scanning username map %s\n",mapfile
));
136 while((s
=fgets_slash(buf
,sizeof(buf
),f
))!=NULL
) {
138 char *dosname
= strchr(unixname
,'=');
139 BOOL return_if_mapped
= False
;
146 while (isspace((int)*unixname
))
148 if ('!' == *unixname
) {
149 return_if_mapped
= True
;
152 while (*unixname
&& isspace((int)*unixname
))
156 if (!*unixname
|| strchr("#;",*unixname
))
160 int l
= strlen(unixname
);
161 while (l
&& isspace((int)unixname
[l
-1])) {
167 if (strchr(dosname
,'*') || user_in_list(user
,dosname
)) {
168 DEBUG(3,("Mapped user %s to %s\n",user
,unixname
));
170 fstrcpy(last_from
,user
);
171 sscanf(unixname
,"%s",user
);
172 fstrcpy(last_to
,user
);
173 if(return_if_mapped
) {
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
);
192 /****************************************************************************
194 ****************************************************************************/
196 static struct passwd
*_Get_Pwnam(char *s
)
200 ret
= sys_getpwnam(s
);
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
);
213 /****************************************************************************
214 A wrapper for getpwnam(). The following variations are tried...
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();
228 if (!user
|| !(*user
))
231 /* make a few copies to work with */
232 fstrcpy(orig_username
, user
);
234 /* allow_change was False, so make a copy and temporarily
235 assign the char* user to the temp copy */
240 /* try in all lower case first as this is the most
241 common case on UNIX systems */
246 ret
= _Get_Pwnam(user
);
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
);
256 fstrcpy(user
, orig_username
);
262 /* finally, try in all caps if that is a new case */
267 if (strcmp(user
, orig_username
) != 0) {
268 ret
= _Get_Pwnam(user
);
273 /* Try all combinations up to usernamelevel. */
278 ret
= uname_string_combinations(user
, _Get_Pwnam
, usernamelevel
);
286 /****************************************************************************
287 Check if a user is in a netgroup user list.
288 ****************************************************************************/
290 static BOOL
user_in_netgroup_list(char *user
,char *ngname
)
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"));
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
))
309 #endif /* HAVE_NETGROUP */
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
)
321 gid_t
*groups
= NULL
;
325 *winbind_answered
= False
;
328 * Get the gid's that this user belongs to.
331 if ((num_groups
= winbind_getgroups(user
, 0, NULL
)) == -1)
334 if (num_groups
== 0) {
335 *winbind_answered
= True
;
339 if ((groups
= (gid_t
*)malloc(sizeof(gid_t
) * num_groups
)) == NULL
) {
340 DEBUG(0,("user_in_winbind_group_list: malloc fail.\n"));
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
) ));
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",
361 for (i
= 0; i
< num_groups
; i
++) {
362 if (gid
== groups
[i
]) {
364 DEBUG(10,("user_in_winbind_group_list: user |%s| is in group |%s|\n",
370 *winbind_answered
= True
;
376 *winbind_answered
= False
;
381 /****************************************************************************
382 Check if a user is in a UNIX group.
383 Names are in UNIX character set format.
384 ****************************************************************************/
386 static BOOL
user_in_unix_group_list(char *user
,const char *gname
)
388 struct passwd
*pass
= Get_Pwnam(user
,False
);
389 struct sys_userlist
*user_list
;
390 struct sys_userlist
*member
;
392 DEBUG(10,("user_in_unix_group_list: checking user %s in group %s\n", user
, gname
));
395 * We need to check the users primary group as this
396 * group is implicit and often not listed in the group database.
400 if (strequal_unix(gname
, gidtoname(pass
->pw_gid
))) {
401 DEBUG(10,("user_in_unix_group_list: group %s is primary group.\n", gname
));
406 user_list
= get_users_in_group(gname
);
407 if (user_list
== NULL
) {
408 DEBUG(10,("user_in_unix_group_list: no such group %s\n", gname
));
412 for (member
= user_list
; member
; member
= member
->next
) {
413 DEBUG(10,("user_in_unix_group_list: checking user %s against member %s\n",
414 user
, member
->unix_name
));
415 if (strequal_unix(member
->unix_name
,user
)) {
416 free_userlist(user_list
);
417 DEBUG(10,("user_in_unix_group_list: user |%s| is in group |%s|\n", user
, gname
));
422 free_userlist(user_list
);
426 /****************************************************************************
427 Check if a user is in a group list. Ask winbind first, then use UNIX.
428 Names are in UNIX character set format.
429 ****************************************************************************/
431 BOOL
user_in_group_list(char *user
,char *gname
)
433 BOOL winbind_answered
= False
;
436 ret
= user_in_winbind_group_list(user
, gname
, &winbind_answered
);
437 if (!winbind_answered
)
438 ret
= user_in_unix_group_list(user
, gname
);
441 DEBUG(10,("user_in_group_list: user |%s| is in group |%s|\n", user
, gname
));
445 /****************************************************************************
446 Check if a user is in a user list - can check combinations of UNIX
448 Names are in UNIX character set format.
449 ****************************************************************************/
451 BOOL
user_in_list(char *user
,char *list
)
456 DEBUG(10,("user_in_list: checking user %s in list %s\n", user
, list
));
458 while (next_token(&p
,tok
,LIST_SEP
, sizeof(tok
))) {
460 DEBUG(10,("user_in_list: checking user |%s| against |%s|\n", user
, tok
));
463 * Check raw username.
465 if (strequal_unix(user
,tok
)) {
466 DEBUG(10,("user_in_list: user |%s| matches |%s|\n", user
, tok
));
471 * Now check to see if any combination
472 * of UNIX and netgroups has been specified.
477 * Old behaviour. Check netgroup list
478 * followed by UNIX list.
480 if(user_in_netgroup_list(user
,&tok
[1]))
482 if(user_in_group_list(user
,&tok
[1]))
484 } else if (*tok
== '+') {
488 * Search UNIX list followed by netgroup.
490 if(user_in_group_list(user
,&tok
[2]))
492 if(user_in_netgroup_list(user
,&tok
[2]))
498 * Just search UNIX list.
501 if(user_in_group_list(user
,&tok
[1]))
505 } else if (*tok
== '&') {
509 * Search netgroup list followed by UNIX list.
511 if(user_in_netgroup_list(user
,&tok
[2]))
513 if(user_in_group_list(user
,&tok
[2]))
517 * Just search netgroup list.
519 if(user_in_netgroup_list(user
,&tok
[1]))
522 } else if (!name_is_local(tok
)) {
524 * If user name did not match and token is not
525 * a unix group and the token has a winbind separator in the
526 * name then see if it is a Windows group.
530 enum SID_NAME_USE name_type
;
531 BOOL winbind_answered
= False
;
534 /* Check to see if name is a Windows group */
535 if (winbind_lookup_name(NULL
, tok
, &g_sid
, &name_type
) && name_type
== SID_NAME_DOM_GRP
) {
537 /* Check if user name is in the Windows group */
538 ret
= user_in_winbind_group_list(user
, tok
, &winbind_answered
);
540 if (winbind_answered
&& ret
== True
) {
541 DEBUG(10,("user_in_list: user |%s| is in group |%s|\n", user
, tok
));
550 /* The functions below have been taken from password.c and slightly modified */
551 /****************************************************************************
552 Apply a function to upper/lower case combinations
553 of a string and return true if one of them returns true.
554 Try all combinations with N uppercase letters.
555 offset is the first char to try and change (start with 0)
556 it assumes the string starts lowercased
557 ****************************************************************************/
559 static struct passwd
*uname_string_combinations2(char *s
,int offset
,struct passwd
*(*fn
)(char *),int N
)
561 ssize_t len
= (ssize_t
)strlen(s
);
565 if (N
<= 0 || offset
>= len
)
568 for (i
=offset
;i
<(len
-(N
-1));i
++) {
570 if (!islower((int)c
))
573 ret
= uname_string_combinations2(s
,i
+1,fn
,N
-1);
581 /****************************************************************************
582 Apply a function to upper/lower case combinations
583 of a string and return true if one of them returns true.
584 Try all combinations with up to N uppercase letters.
585 offset is the first char to try and change (start with 0)
586 it assumes the string starts lowercased
587 ****************************************************************************/
589 static struct passwd
* uname_string_combinations(char *s
,struct passwd
* (*fn
)(char *),int N
)
595 ret
= uname_string_combinations2(s
,0,fn
,n
);
603 /****************************************************************************
604 these wrappers allow appliance mode to work. In appliance mode the username
605 takes the form DOMAIN/user
606 ****************************************************************************/
607 struct passwd
*smb_getpwnam(char *user
, BOOL allow_change
)
612 extern pstring global_myname
;
614 pw
= Get_Pwnam(user
, allow_change
);
619 * If it is a domain qualified name and it isn't in our password
620 * database but the domain portion matches our local machine name then
621 * lookup just the username portion locally.
624 sep
= lp_winbind_separator();
625 p
= strchr(user
,*sep
);
626 if (p
&& strncasecmp(global_myname
, user
, strlen(global_myname
))==0)
627 return Get_Pwnam(p
+1, allow_change
);