client.c: New print queue query code from Jeff C. Foster " <jfoste@wgc.woodward...
[Samba/gbeck.git] / source / lib / username.c
blobb8d152c83fd25da5a3e477dee6ac82f9bf385411
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 Username handling
5 Copyright (C) Andrew Tridgell 1992-1997
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.
22 #include "includes.h"
23 extern int DEBUGLEVEL;
26 /****************************************************************************
27 get a users home directory. tries as-is then lower case
28 ****************************************************************************/
29 char *get_home_dir(char *user)
31 static struct passwd *pass;
33 pass = Get_Pwnam(user,False);
35 if (!pass) return(NULL);
36 return(pass->pw_dir);
40 /*******************************************************************
41 map a username from a dos name to a unix name by looking in the username
42 map
43 ********************************************************************/
44 void map_username(char *user)
46 static int depth=0;
47 static BOOL initialised=False;
48 static fstring last_from,last_to;
49 FILE *f;
50 char *s;
51 char *mapfile = lp_username_map();
52 if (!*mapfile || depth) return;
54 if (!*user) return;
56 if (!initialised) {
57 *last_from = *last_to = 0;
58 initialised = True;
61 if (strequal(user,last_to)) return;
63 if (strequal(user,last_from)) {
64 DEBUG(3,("Mapped user %s to %s\n",user,last_to));
65 strcpy(user,last_to);
66 return;
69 f = fopen(mapfile,"r");
70 if (!f) {
71 DEBUG(0,("can't open username map %s\n",mapfile));
72 return;
75 DEBUG(4,("Scanning username map %s\n",mapfile));
77 depth++;
79 for (; (s=fgets_slash(NULL,80,f)); free(s)) {
80 char *unixname = s;
81 char *dosname = strchr(unixname,'=');
83 if (!dosname) continue;
84 *dosname++ = 0;
86 while (isspace(*unixname)) unixname++;
87 if (!*unixname || strchr("#;",*unixname)) continue;
90 int l = strlen(unixname);
91 while (l && isspace(unixname[l-1])) {
92 unixname[l-1] = 0;
93 l--;
97 if (strchr(dosname,'*') || user_in_list(user,dosname)) {
98 DEBUG(3,("Mapped user %s to %s\n",user,unixname));
99 StrnCpy(last_from,user,sizeof(last_from)-1);
100 sscanf(unixname,"%s",user);
101 StrnCpy(last_to,user,sizeof(last_to)-1);
105 fclose(f);
107 depth--;
110 /****************************************************************************
111 internals of Get_Pwnam wrapper
112 ****************************************************************************/
113 static struct passwd *_Get_Pwnam(char *s)
115 struct passwd *ret;
117 ret = getpwnam(s);
118 if (ret)
120 #ifdef GETPWANAM
121 struct passwd_adjunct *pwret;
122 pwret = getpwanam(s);
123 if (pwret)
125 free(ret->pw_passwd);
126 ret->pw_passwd = pwret->pwa_passwd;
128 #endif
132 return(ret);
136 /****************************************************************************
137 a wrapper for getpwnam() that tries with all lower and all upper case
138 if the initial name fails. Also tried with first letter capitalised
139 Note that this changes user!
140 ****************************************************************************/
141 struct passwd *Get_Pwnam(char *user,BOOL allow_change)
143 fstring user2;
145 struct passwd *ret;
147 if (!user || !(*user))
148 return(NULL);
150 StrnCpy(user2,user,sizeof(user2)-1);
152 if (!allow_change) {
153 user = &user2[0];
156 map_username(user);
158 ret = _Get_Pwnam(user);
159 if (ret) return(ret);
161 strlower(user);
162 ret = _Get_Pwnam(user);
163 if (ret) return(ret);
165 strupper(user);
166 ret = _Get_Pwnam(user);
167 if (ret) return(ret);
169 /* try with first letter capitalised */
170 if (strlen(user) > 1)
171 strlower(user+1);
172 ret = _Get_Pwnam(user);
173 if (ret) return(ret);
175 if (allow_change)
176 strcpy(user,user2);
178 return(NULL);
182 /****************************************************************************
183 check if a user is in a user list
184 ****************************************************************************/
185 BOOL user_in_list(char *user,char *list)
187 pstring tok;
188 char *p=list;
190 while (next_token(&p,tok,LIST_SEP))
192 if (strequal(user,tok))
193 return(True);
195 #ifdef NETGROUP
196 if (*tok == '@')
198 static char *mydomain = NULL;
199 if (mydomain == 0)
200 yp_get_default_domain(&mydomain);
202 if(mydomain == 0)
204 DEBUG(5,("Unable to get default yp domain\n"));
206 else
209 DEBUG(5,("looking for user %s of domain %s in netgroup %s\n",
210 user, mydomain, &tok[1]));
211 DEBUG(5,("innetgr is %s\n",
212 innetgr(&tok[1], (char *) 0, user, mydomain)
213 ? "TRUE" : "FALSE"));
215 if (innetgr(&tok[1], (char *)0, user, mydomain))
216 return (True);
219 #endif
222 #if HAVE_GETGRNAM
223 if (*tok == '@')
225 struct group *gptr;
226 char **member;
227 struct passwd *pass = Get_Pwnam(user,False);
229 if (pass) {
230 gptr = getgrgid(pass->pw_gid);
231 if (gptr && strequal(gptr->gr_name,&tok[1]))
232 return(True);
235 gptr = (struct group *)getgrnam(&tok[1]);
237 if (gptr)
239 member = gptr->gr_mem;
240 while (member && *member)
242 if (strequal(*member,user))
243 return(True);
244 member++;
248 #endif
250 return(False);