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.
23 extern int DEBUGLEVEL;
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 Get a users home directory.
31 ****************************************************************************/
33 char *get_user_home_dir(char *user)
35 static struct passwd *pass;
37 pass = Get_Pwnam(user, False);
39 if (!pass) return(NULL);
44 /*******************************************************************
45 Map a username from a dos name to a unix name by looking in the username
46 map. Note that this modifies the name in place.
47 This is the main function that should be called *once* on
48 any incoming or new username - in order to canonicalize the name.
49 This is being done to de-couple the case conversions from the user mapping
50 function. Previously, the map_username was being called
51 every time Get_Pwnam was called.
52 Returns True if username was changed, false otherwise.
53 ********************************************************************/
55 BOOL map_username(char *user)
57 static BOOL initialised=False;
58 static fstring last_from,last_to;
60 char *mapfile = lp_username_map();
63 BOOL mapped_user = False;
72 *last_from = *last_to = 0;
76 if (strequal(user,last_to))
79 if (strequal(user,last_from)) {
80 DEBUG(3,("Mapped user %s to %s\n",user,last_to));
81 fstrcpy(user,last_to);
85 f = sys_fopen(mapfile,"r");
87 DEBUG(0,("can't open username map %s. Error %s\n",mapfile, strerror(errno) ));
91 DEBUG(4,("Scanning username map %s\n",mapfile));
93 while((s=fgets_slash(buf,sizeof(buf),f))!=NULL) {
95 char *dosname = strchr(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("#;",*unixname))
116 int l = strlen(unixname);
117 while (l && isspace(unixname[l-1])) {
123 if (strchr(dosname,'*') || user_in_list(user,dosname)) {
124 DEBUG(3,("Mapped user %s to %s\n",user,unixname));
126 fstrcpy(last_from,user);
127 sscanf(unixname,"%s",user);
128 fstrcpy(last_to,user);
129 if(return_if_mapped) {
139 * Setup the last_from and last_to as an optimization so
140 * that we don't scan the file again for the same user.
142 fstrcpy(last_from,user);
143 fstrcpy(last_to,user);
148 /****************************************************************************
150 ****************************************************************************/
152 static struct passwd *_Get_Pwnam(char *s)
156 ret = sys_getpwnam(s);
158 #ifdef HAVE_GETPWANAM
159 struct passwd_adjunct *pwret;
160 pwret = getpwanam(s);
161 if (pwret && pwret->pwa_passwd) {
162 pstrcpy(ret->pw_passwd,pwret->pwa_passwd);
172 * A wrapper for getpwnam(). The following variations are tried:
174 * - in all lower case if this differs from transmitted
175 * - in all upper case if this differs from transmitted
176 * - using lp_usernamelevel() for permutations.
177 * NOTE: This can potentially modify 'user' depending on value of
180 struct passwd *Get_Pwnam(char *user,BOOL allow_change)
183 struct passwd *ret = NULL;
185 if (!user || !(*user))
188 fstrcpy(user2, user);
190 /* Try in all lower case first as this is the most
191 common case on UNIX systems */
193 DEBUG(5,("Trying _Get_Pwnam(), username as lowercase is %s\n",user2));
194 ret = _Get_Pwnam(user2);
198 /* Try as given, if username wasn't originally lowercase */
199 if(strcmp(user,user2) != 0) {
200 DEBUG(5,("Trying _Get_Pwnam(), username as given is %s\n",user));
201 ret = _Get_Pwnam(user);
206 /* Try as uppercase, if username wasn't originally uppercase */
208 if(strcmp(user,user2) != 0) {
209 DEBUG(5,("Trying _Get_Pwnam(), username as uppercase is %s\n",user2));
210 ret = _Get_Pwnam(user2);
215 /* Try all combinations up to usernamelevel */
217 DEBUG(5,("Checking combinations of %d uppercase letters in %s\n",lp_usernamelevel(),user2));
218 ret = uname_string_combinations(user2, _Get_Pwnam, lp_usernamelevel());
221 DEBUG(5,("Get_Pwnam %s find a valid username!\n",ret ? "did":"didn't"));
222 /* If caller wants the modified username, ensure they get it */
226 /* We can safely assume ret is NULL if none of the above succeed */
230 /****************************************************************************
231 Check if a user is in a netgroup user list.
232 ****************************************************************************/
234 static BOOL user_in_netgroup_list(char *user,char *ngname)
237 static char *mydomain = NULL;
238 if (mydomain == NULL)
239 yp_get_default_domain(&mydomain);
241 if(mydomain == NULL) {
242 DEBUG(5,("Unable to get default yp domain\n"));
244 DEBUG(5,("looking for user %s of domain %s in netgroup %s\n",
245 user, mydomain, ngname));
246 DEBUG(5,("innetgr is %s\n",
247 innetgr(ngname, NULL, user, mydomain)
248 ? "TRUE" : "FALSE"));
250 if (innetgr(ngname, NULL, user, mydomain))
253 #endif /* HAVE_NETGROUP */
257 /****************************************************************************
258 Check if a user is in a winbind group.
259 ****************************************************************************/
261 static BOOL user_in_winbind_group_list(char *user,char *gname, BOOL *winbind_answered)
265 gid_t *groups = NULL;
269 *winbind_answered = False;
272 * Get the gid's that this user belongs to.
275 if ((num_groups = winbind_getgroups(user, 0, NULL)) == -1)
278 if (num_groups == 0) {
279 *winbind_answered = True;
283 if ((groups = (gid_t *)malloc(sizeof(gid_t) * num_groups )) == NULL) {
284 DEBUG(0,("user_in_winbind_group_list: malloc fail.\n"));
288 if ((num_groups = winbind_getgroups(user, num_groups, groups)) == -1) {
289 DEBUG(0,("user_in_winbind_group_list: second winbind_getgroups call \
290 failed with error %s\n", strerror(errno) ));
295 * Now we have the gid list for this user - convert the gname
296 * to a gid_t via either winbind or the local UNIX lookup and do the comparison.
299 if ((gid = nametogid(gname)) == (gid_t)-1) {
300 DEBUG(0,("user_in_winbind_group_list: winbind_lookup_name for group %s failed.\n",
305 for (i = 0; i < num_groups; i++) {
306 if (gid == groups[i]) {
312 *winbind_answered = True;
318 *winbind_answered = False;
323 /****************************************************************************
324 Check if a user is in a UNIX group.
325 ****************************************************************************/
327 static BOOL user_in_unix_group_list(char *user,char *gname)
331 struct passwd *pass = Get_Pwnam(user,False);
333 DEBUG(10,("user_in_unix_group_list: checking user %s in group %s\n", user, gname));
336 * We need to check the users primary group as this
337 * group is implicit and often not listed in the group database.
341 gptr = getgrgid(pass->pw_gid);
342 if (gptr && strequal(gptr->gr_name,gname)) {
343 DEBUG(10,("user_in_unix_group_list: group %s is primary group.\n", gname ));
348 if ((gptr = (struct group *)getgrnam(gname)) == NULL) {
349 DEBUG(10,("user_in_unix_group_list: no such group %s\n", gname ));
353 member = gptr->gr_mem;
354 while (member && *member) {
355 DEBUG(10,("user_in_unix_group_list: checking user %s against member %s\n", user, *member ));
356 if (strequal(*member,user)) {
365 /****************************************************************************
366 Check if a user is in a group list. Ask winbind first, then use UNIX.
367 ****************************************************************************/
369 BOOL user_in_group_list(char *user,char *gname)
371 BOOL winbind_answered = False;
372 BOOL ret = user_in_winbind_group_list(user, gname, &winbind_answered);
374 if (winbind_answered)
377 return user_in_unix_group_list(user, gname);
380 /****************************************************************************
381 Check if a user is in a user list - can check combinations of UNIX
383 ****************************************************************************/
385 BOOL user_in_list(char *user,char *list)
390 DEBUG(10,("user_in_list: checking user %s in list %s\n", user, list));
392 while (next_token(&p,tok,LIST_SEP, sizeof(tok))) {
394 * Check raw username.
396 if (strequal(user,tok))
400 * Now check to see if any combination
401 * of UNIX and netgroups has been specified.
406 * Old behaviour. Check netgroup list
407 * followed by UNIX list.
409 if(user_in_netgroup_list(user,&tok[1]))
411 if(user_in_group_list(user,&tok[1]))
413 } else if (*tok == '+') {
417 * Search UNIX list followed by netgroup.
419 if(user_in_group_list(user,&tok[2]))
421 if(user_in_netgroup_list(user,&tok[2]))
427 * Just search UNIX list.
430 if(user_in_group_list(user,&tok[1]))
434 } else if (*tok == '&') {
438 * Search netgroup list followed by UNIX list.
440 if(user_in_netgroup_list(user,&tok[2]))
442 if(user_in_group_list(user,&tok[2]))
446 * Just search netgroup list.
448 if(user_in_netgroup_list(user,&tok[1]))
456 /* The functions below have been taken from password.c and slightly modified */
457 /****************************************************************************
458 Apply a function to upper/lower case combinations
459 of a string and return true if one of them returns true.
460 Try all combinations with N uppercase letters.
461 offset is the first char to try and change (start with 0)
462 it assumes the string starts lowercased
463 ****************************************************************************/
465 static struct passwd *uname_string_combinations2(char *s,int offset,struct passwd *(*fn)(char *),int N)
467 ssize_t len = (ssize_t)strlen(s);
471 #ifdef PASSWORD_LENGTH
472 len = MIN(len,PASSWORD_LENGTH);
475 if (N <= 0 || offset >= len)
478 for (i=offset;i<(len-(N-1));i++) {
483 ret = uname_string_combinations2(s,i+1,fn,N-1);
491 /****************************************************************************
492 Apply a function to upper/lower case combinations
493 of a string and return true if one of them returns true.
494 Try all combinations with up to N uppercase letters.
495 offset is the first char to try and change (start with 0)
496 it assumes the string starts lowercased
497 ****************************************************************************/
499 static struct passwd * uname_string_combinations(char *s,struct passwd * (*fn)(char *),int N)
505 ret = uname_string_combinations2(s,0,fn,n);
513 /****************************************************************************
514 these wrappers allow appliance mode to work. In appliance mode the username
515 takes the form DOMAIN/user
516 ****************************************************************************/
517 struct passwd *smb_getpwnam(char *user, BOOL allow_change)
522 extern pstring global_myname;
524 pw = Get_Pwnam(user, allow_change);
527 /* if it is a domain qualified name and it isn't in our password
528 database but the domain portion matches our local machine name then
529 lookup just the username portion locally */
530 sep = lp_winbind_separator();
531 if (!sep || !*sep) sep = "\\";
532 p = strchr(user,*sep);
534 strncasecmp(global_myname, user, strlen(global_myname))==0) {
535 return Get_Pwnam(p+1, allow_change);