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
]) {
368 *winbind_answered
= True
;
374 *winbind_answered
= 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.
397 if (strequal(gname
, gidtoname(pass
->pw_gid
))) {
398 DEBUG(10,("user_in_unix_group_list: group %s is primary group.\n", gname
));
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
));
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
);
418 free_userlist(user_list
);
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
;
431 ret
= user_in_winbind_group_list(user
, gname
, &winbind_answered
);
432 if (!winbind_answered
)
433 ret
= user_in_unix_group_list(user
, gname
);
436 DEBUG(10,("user_in_group_list: user |%s| is in group |%s|\n", user
, gname
));
440 /****************************************************************************
441 Check if a user is in a user list - can check combinations of UNIX
443 ****************************************************************************/
445 BOOL
user_in_list(char *user
,char *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
))
463 * Now check to see if any combination
464 * of UNIX and netgroups has been specified.
469 * Old behaviour. Check netgroup list
470 * followed by UNIX list.
472 if(user_in_netgroup_list(user
,&tok
[1]))
474 if(user_in_group_list(user
,&tok
[1]))
476 } else if (*tok
== '+') {
480 * Search UNIX list followed by netgroup.
482 if(user_in_group_list(user
,&tok
[2]))
484 if(user_in_netgroup_list(user
,&tok
[2]))
490 * Just search UNIX list.
493 if(user_in_group_list(user
,&tok
[1]))
497 } else if (*tok
== '&') {
501 * Search netgroup list followed by UNIX list.
503 if(user_in_netgroup_list(user
,&tok
[2]))
505 if(user_in_group_list(user
,&tok
[2]))
509 * Just search netgroup list.
511 if(user_in_netgroup_list(user
,&tok
[1]))
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.
522 enum SID_NAME_USE name_type
;
523 BOOL winbind_answered
= False
;
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
));
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
);
557 if (N
<= 0 || offset
>= len
)
560 for (i
=offset
;i
<(len
-(N
-1));i
++) {
562 if (!islower((int)c
))
565 ret
= uname_string_combinations2(s
,i
+1,fn
,N
-1);
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
)
587 ret
= uname_string_combinations2(s
,0,fn
,n
);
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
)
604 extern pstring global_myname
;
606 pw
= Get_Pwnam(user
, allow_change
);
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
);