2 Unix SMB/Netbios implementation.
5 Copyright (C) Andrew Tridgell 1992-1995
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 extern int DEBUGLEVEL;
27 /****************************************************************************
28 get a users home directory. tries as-is then lower case
29 ****************************************************************************/
30 char *get_home_dir(char *user)
32 static struct passwd *pass;
34 pass = Get_Pwnam(user,False);
36 if (!pass) return(NULL);
41 /*******************************************************************
42 map a username from a dos name to a unix name by looking in the username
44 ********************************************************************/
45 void map_username(char *user)
48 static BOOL initialised=False;
49 static fstring last_from,last_to;
52 char *mapfile = lp_username_map();
53 if (!*mapfile || depth) return;
58 *last_from = *last_to = 0;
62 if (strequal(user,last_to)) return;
64 if (strequal(user,last_from)) {
65 DEBUG(3,("Mapped user %s to %s\n",user,last_to));
70 f = fopen(mapfile,"r");
72 DEBUG(0,("can't open username map %s\n",mapfile));
76 DEBUG(4,("Scanning username map %s\n",mapfile));
80 for (; (s=fgets_slash(NULL,80,f)); free(s)) {
82 char *dosname = strchr(unixname,'=');
84 if (!dosname) continue;
87 while (isspace(*unixname)) unixname++;
88 if (!*unixname || strchr("#;",*unixname)) continue;
91 int l = strlen(unixname);
92 while (l && isspace(unixname[l-1])) {
98 if (strchr(dosname,'*') || user_in_list(user,dosname)) {
99 DEBUG(3,("Mapped user %s to %s\n",user,unixname));
100 StrnCpy(last_from,user,sizeof(last_from)-1);
101 sscanf(unixname,"%s",user);
102 StrnCpy(last_to,user,sizeof(last_to)-1);
111 /****************************************************************************
112 internals of Get_Pwnam wrapper
113 ****************************************************************************/
114 static struct passwd *_Get_Pwnam(char *s)
122 struct passwd_adjunct *pwret;
123 pwret = getpwanam(s);
126 free(ret->pw_passwd);
127 ret->pw_passwd = pwret->pwa_passwd;
137 /****************************************************************************
138 a wrapper for getpwnam() that tries with all lower and all upper case
139 if the initial name fails. Also tried with first letter capitalised
140 Note that this changes user!
141 ****************************************************************************/
142 struct passwd *Get_Pwnam(char *user,BOOL allow_change)
148 if (!user || !(*user))
151 StrnCpy(user2,user,sizeof(user2)-1);
159 ret = _Get_Pwnam(user);
160 if (ret) return(ret);
163 ret = _Get_Pwnam(user);
164 if (ret) return(ret);
167 ret = _Get_Pwnam(user);
168 if (ret) return(ret);
170 /* try with first letter capitalised */
171 if (strlen(user) > 1)
173 ret = _Get_Pwnam(user);
174 if (ret) return(ret);
183 /****************************************************************************
184 check if a user is in a user list
185 ****************************************************************************/
186 BOOL user_in_list(char *user,char *list)
191 while (next_token(&p,tok,LIST_SEP))
193 if (strequal(user,tok))
199 static char *mydomain = NULL;
201 yp_get_default_domain(&mydomain);
203 DEBUG(5,("looking for user %s of domain %s in netgroup %s\n",
204 user, mydomain, &tok[1]));
205 DEBUG(5,("innetgr is %s\n",
206 innetgr(&tok[1], (char *) 0, user, mydomain)
207 ? "TRUE" : "FALSE"));
209 if (innetgr(&tok[1], (char *)0, user, mydomain))
220 struct passwd *pass = Get_Pwnam(user,False);
223 gptr = getgrgid(pass->pw_gid);
224 if (gptr && strequal(gptr->gr_name,&tok[1]))
228 gptr = (struct group *)getgrnam(&tok[1]);
232 member = gptr->gr_mem;
233 while (member && *member)
235 if (strequal(*member,user))