2 Unix SMB/Netbios implementation.
5 Copyright (C) Andrew Tridgell 1992-1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 /* internal functions */
25 static struct passwd *uname_string_combinations(char *s, struct passwd * (*fn) (char *), int N);
26 static struct passwd *uname_string_combinations2(char *s, int offset, struct passwd * (*fn) (char *), int N);
28 /****************************************************************************
29 Get a users home directory.
30 ****************************************************************************/
32 char *get_user_home_dir(char *user)
34 static struct passwd *pass;
36 pass = Get_Pwnam(user, False);
38 if (!pass) return(NULL);
43 /*******************************************************************
44 Map a username from a dos name to a unix name by looking in the username
45 map. Note that this modifies the name in place.
46 This is the main function that should be called *once* on
47 any incoming or new username - in order to canonicalize the name.
48 This is being done to de-couple the case conversions from the user mapping
49 function. Previously, the map_username was being called
50 every time Get_Pwnam was called.
51 Returns True if username was changed, false otherwise.
52 ********************************************************************/
54 BOOL map_username(char *user)
56 static BOOL initialised=False;
57 static fstring last_from,last_to;
59 char *mapfile = lp_username_map();
62 BOOL mapped_user = False;
71 *last_from = *last_to = 0;
75 if (strequal(user,last_to))
78 if (strequal(user,last_from)) {
79 DEBUG(3,("Mapped user %s to %s\n",user,last_to));
80 fstrcpy(user,last_to);
84 f = x_fopen(mapfile,O_RDONLY, 0);
86 DEBUG(0,("can't open username map %s. Error %s\n",mapfile, strerror(errno) ));
90 DEBUG(4,("Scanning username map %s\n",mapfile));
92 while((s=fgets_slash(buf,sizeof(buf),f))!=NULL) {
94 char *dosname = strchr_m(unixname,'=');
96 BOOL return_if_mapped = False;
103 while (isspace(*unixname))
105 if ('!' == *unixname) {
106 return_if_mapped = True;
108 while (*unixname && isspace(*unixname))
112 if (!*unixname || strchr_m("#;",*unixname))
116 int l = strlen(unixname);
117 while (l && isspace(unixname[l-1])) {
123 dosuserlist = lp_list_make(dosname);
125 DEBUG(0,("Unable to build user list\n"));
129 if (strchr_m(dosname,'*') || user_in_list(user, dosuserlist)) {
130 DEBUG(3,("Mapped user %s to %s\n",user,unixname));
132 fstrcpy(last_from,user);
133 sscanf(unixname,"%s",user);
134 fstrcpy(last_to,user);
135 if(return_if_mapped) {
136 lp_list_free (&dosuserlist);
142 lp_list_free (&dosuserlist);
148 * Setup the last_from and last_to as an optimization so
149 * that we don't scan the file again for the same user.
151 fstrcpy(last_from,user);
152 fstrcpy(last_to,user);
157 /****************************************************************************
159 ****************************************************************************/
161 static struct passwd *_Get_Pwnam(char *s)
165 ret = sys_getpwnam(s);
167 #ifdef HAVE_GETPWANAM
168 struct passwd_adjunct *pwret;
169 pwret = getpwanam(s);
170 if (pwret && pwret->pwa_passwd) {
171 pstrcpy(ret->pw_passwd,pwret->pwa_passwd);
181 * A wrapper for getpwnam(). The following variations are tried:
183 * - in all lower case if this differs from transmitted
184 * - in all upper case if this differs from transmitted
185 * - using lp_usernamelevel() for permutations.
186 * NOTE: This can potentially modify 'user' depending on value of
189 struct passwd *Get_Pwnam(char *user,BOOL allow_change)
192 struct passwd *ret = NULL;
194 if (!user || !(*user))
197 fstrcpy(user2, user);
199 /* Try in all lower case first as this is the most
200 common case on UNIX systems */
202 DEBUG(5,("Trying _Get_Pwnam(), username as lowercase is %s\n",user2));
203 ret = _Get_Pwnam(user2);
207 /* Try as given, if username wasn't originally lowercase */
208 if(strcmp(user,user2) != 0) {
209 DEBUG(5,("Trying _Get_Pwnam(), username as given is %s\n",user));
210 ret = _Get_Pwnam(user);
215 /* Try as uppercase, if username wasn't originally uppercase */
217 if(strcmp(user,user2) != 0) {
218 DEBUG(5,("Trying _Get_Pwnam(), username as uppercase is %s\n",user2));
219 ret = _Get_Pwnam(user2);
224 /* Try all combinations up to usernamelevel */
226 DEBUG(5,("Checking combinations of %d uppercase letters in %s\n",lp_usernamelevel(),user2));
227 ret = uname_string_combinations(user2, _Get_Pwnam, lp_usernamelevel());
230 DEBUG(5,("Get_Pwnam %s find a valid username!\n",ret ? "did":"didn't"));
231 /* If caller wants the modified username, ensure they get it */
235 /* We can safely assume ret is NULL if none of the above succeed */
239 /****************************************************************************
240 Check if a user is in a netgroup user list.
241 ****************************************************************************/
243 static BOOL user_in_netgroup_list(char *user,char *ngname)
246 static char *mydomain = NULL;
247 if (mydomain == NULL)
248 yp_get_default_domain(&mydomain);
250 if(mydomain == NULL) {
251 DEBUG(5,("Unable to get default yp domain\n"));
253 DEBUG(5,("looking for user %s of domain %s in netgroup %s\n",
254 user, mydomain, ngname));
255 DEBUG(5,("innetgr is %s\n",
256 innetgr(ngname, NULL, user, mydomain)
257 ? "TRUE" : "FALSE"));
259 if (innetgr(ngname, NULL, user, mydomain))
262 #endif /* HAVE_NETGROUP */
266 /****************************************************************************
267 Check if a user is in a winbind group.
268 ****************************************************************************/
270 static BOOL user_in_winbind_group_list(char *user,char *gname, BOOL *winbind_answered)
274 gid_t *groups = NULL;
278 *winbind_answered = False;
281 * Get the gid's that this user belongs to.
284 if ((num_groups = winbind_getgroups(user, 0, NULL)) == -1)
287 if (num_groups == 0) {
288 *winbind_answered = True;
292 if ((groups = (gid_t *)malloc(sizeof(gid_t) * num_groups )) == NULL) {
293 DEBUG(0,("user_in_winbind_group_list: malloc fail.\n"));
297 if ((num_groups = winbind_getgroups(user, num_groups, groups)) == -1) {
298 DEBUG(0,("user_in_winbind_group_list: second winbind_getgroups call \
299 failed with error %s\n", strerror(errno) ));
304 * Now we have the gid list for this user - convert the gname
305 * to a gid_t via either winbind or the local UNIX lookup and do the comparison.
308 if ((gid = nametogid(gname)) == (gid_t)-1) {
309 DEBUG(0,("user_in_winbind_group_list: winbind_lookup_name for group %s failed.\n",
314 for (i = 0; i < num_groups; i++) {
315 if (gid == groups[i]) {
321 *winbind_answered = True;
327 *winbind_answered = False;
332 /****************************************************************************
333 Check if a user is in a UNIX group.
334 ****************************************************************************/
336 static BOOL user_in_unix_group_list(char *user,char *gname)
340 struct passwd *pass = Get_Pwnam(user,False);
342 DEBUG(10,("user_in_unix_group_list: checking user %s in group %s\n", user, gname));
345 * We need to check the users primary group as this
346 * group is implicit and often not listed in the group database.
350 gptr = getgrgid(pass->pw_gid);
351 if (gptr && strequal(gptr->gr_name,gname)) {
352 DEBUG(10,("user_in_unix_group_list: group %s is primary group.\n", gname ));
357 if ((gptr = (struct group *)getgrnam(gname)) == NULL) {
358 DEBUG(10,("user_in_unix_group_list: no such group %s\n", gname ));
362 member = gptr->gr_mem;
363 while (member && *member) {
364 DEBUG(10,("user_in_unix_group_list: checking user %s against member %s\n", user, *member ));
365 if (strequal(*member,user)) {
374 /****************************************************************************
375 Check if a user is in a group list. Ask winbind first, then use UNIX.
376 ****************************************************************************/
378 BOOL user_in_group_list(char *user,char *gname)
380 BOOL winbind_answered = False;
381 BOOL ret = user_in_winbind_group_list(user, gname, &winbind_answered);
383 if (winbind_answered)
386 return user_in_unix_group_list(user, gname);
389 /****************************************************************************
390 Check if a user is in a user list - can check combinations of UNIX
392 ****************************************************************************/
394 BOOL user_in_list(char *user,char **list)
397 if (!list || !*list) return False;
399 DEBUG(10,("user_in_list: checking user %s in list\n", user));
403 * Check raw username.
405 if (strequal(user, *list))
409 * Now check to see if any combination
410 * of UNIX and netgroups has been specified.
415 * Old behaviour. Check netgroup list
416 * followed by UNIX list.
418 if(user_in_netgroup_list(user, *list +1))
420 if(user_in_group_list(user, *list +1))
422 } else if (**list == '+') {
424 if((*(*list +1)) == '&') {
426 * Search UNIX list followed by netgroup.
428 if(user_in_group_list(user, *list +2))
430 if(user_in_netgroup_list(user, *list +2))
436 * Just search UNIX list.
439 if(user_in_group_list(user, *list +1))
443 } else if (**list == '&') {
445 if(*(*list +1) == '+') {
447 * Search netgroup list followed by UNIX list.
449 if(user_in_netgroup_list(user, *list +2))
451 if(user_in_group_list(user, *list +2))
455 * Just search netgroup list.
457 if(user_in_netgroup_list(user, *list +1))
467 /* The functions below have been taken from password.c and slightly modified */
468 /****************************************************************************
469 Apply a function to upper/lower case combinations
470 of a string and return true if one of them returns true.
471 Try all combinations with N uppercase letters.
472 offset is the first char to try and change (start with 0)
473 it assumes the string starts lowercased
474 ****************************************************************************/
476 static struct passwd *uname_string_combinations2(char *s,int offset,struct passwd *(*fn)(char *),int N)
478 ssize_t len = (ssize_t)strlen(s);
482 #ifdef PASSWORD_LENGTH
483 len = MIN(len,PASSWORD_LENGTH);
486 if (N <= 0 || offset >= len)
489 for (i=offset;i<(len-(N-1));i++) {
494 ret = uname_string_combinations2(s,i+1,fn,N-1);
502 /****************************************************************************
503 Apply a function to upper/lower case combinations
504 of a string and return true if one of them returns true.
505 Try all combinations with up to N uppercase letters.
506 offset is the first char to try and change (start with 0)
507 it assumes the string starts lowercased
508 ****************************************************************************/
510 static struct passwd * uname_string_combinations(char *s,struct passwd * (*fn)(char *),int N)
516 ret = uname_string_combinations2(s,0,fn,n);
524 /****************************************************************************
525 these wrappers allow appliance mode to work. In appliance mode the username
526 takes the form DOMAIN/user
527 ****************************************************************************/
528 struct passwd *smb_getpwnam(char *user, BOOL allow_change)
533 extern pstring global_myname;
535 pw = Get_Pwnam(user, allow_change);
538 /* if it is a domain qualified name and it isn't in our password
539 database but the domain portion matches our local machine name then
540 lookup just the username portion locally */
541 sep = lp_winbind_separator();
542 if (!sep || !*sep) sep = "\\";
543 p = strchr_m(user,*sep);
545 strncasecmp(global_myname, user, strlen(global_myname))==0) {
546 return Get_Pwnam(p+1, allow_change);