Initial version imported to CVS
[Samba.git] / source / lib / username.c
blob3d214fbbdab3e4e0a7b5a2ea56d0cc93c355e87c
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 Username handling
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.
22 #include "includes.h"
23 #include "loadparm.h"
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);
37 return(pass->pw_dir);
41 /*******************************************************************
42 map a username from a dos name to a unix name by looking in the username
43 map
44 ********************************************************************/
45 void map_username(char *user)
47 static int depth=0;
48 static BOOL initialised=False;
49 static fstring last_from,last_to;
50 FILE *f;
51 char *s;
52 char *mapfile = lp_username_map();
53 if (!*mapfile || depth) return;
55 if (!*user) return;
57 if (!initialised) {
58 *last_from = *last_to = 0;
59 initialised = True;
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));
66 strcpy(user,last_to);
67 return;
70 f = fopen(mapfile,"r");
71 if (!f) {
72 DEBUG(0,("can't open username map %s\n",mapfile));
73 return;
76 DEBUG(4,("Scanning username map %s\n",mapfile));
78 depth++;
80 for (; (s=fgets_slash(NULL,80,f)); free(s)) {
81 char *unixname = s;
82 char *dosname = strchr(unixname,'=');
84 if (!dosname) continue;
85 *dosname++ = 0;
87 while (isspace(*unixname)) unixname++;
88 if (!*unixname || strchr("#;",*unixname)) continue;
91 int l = strlen(unixname);
92 while (l && isspace(unixname[l-1])) {
93 unixname[l-1] = 0;
94 l--;
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);
106 fclose(f);
108 depth--;
111 /****************************************************************************
112 internals of Get_Pwnam wrapper
113 ****************************************************************************/
114 static struct passwd *_Get_Pwnam(char *s)
116 struct passwd *ret;
118 ret = getpwnam(s);
119 if (ret)
121 #ifdef GETPWANAM
122 struct passwd_adjunct *pwret;
123 pwret = getpwanam(s);
124 if (pwret)
126 free(ret->pw_passwd);
127 ret->pw_passwd = pwret->pwa_passwd;
129 #endif
133 return(ret);
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)
144 fstring user2;
146 struct passwd *ret;
148 if (!user || !(*user))
149 return(NULL);
151 StrnCpy(user2,user,sizeof(user2)-1);
153 if (!allow_change) {
154 user = &user2[0];
157 map_username(user);
159 ret = _Get_Pwnam(user);
160 if (ret) return(ret);
162 strlower(user);
163 ret = _Get_Pwnam(user);
164 if (ret) return(ret);
166 strupper(user);
167 ret = _Get_Pwnam(user);
168 if (ret) return(ret);
170 /* try with first letter capitalised */
171 if (strlen(user) > 1)
172 strlower(user+1);
173 ret = _Get_Pwnam(user);
174 if (ret) return(ret);
176 if (allow_change)
177 strcpy(user,user2);
179 return(NULL);
183 /****************************************************************************
184 check if a user is in a user list
185 ****************************************************************************/
186 BOOL user_in_list(char *user,char *list)
188 pstring tok;
189 char *p=list;
191 while (next_token(&p,tok,LIST_SEP))
193 if (strequal(user,tok))
194 return(True);
196 #ifdef NETGROUP
197 if (*tok == '@')
199 static char *mydomain = NULL;
200 if (mydomain == 0)
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))
210 return (True);
212 #endif
215 #if HAVE_GETGRNAM
216 if (*tok == '@')
218 struct group *gptr;
219 char **member;
220 struct passwd *pass = Get_Pwnam(user,False);
222 if (pass) {
223 gptr = getgrgid(pass->pw_gid);
224 if (gptr && strequal(gptr->gr_name,&tok[1]))
225 return(True);
228 gptr = (struct group *)getgrnam(&tok[1]);
230 if (gptr)
232 member = gptr->gr_mem;
233 while (member && *member)
235 if (strequal(*member,user))
236 return(True);
237 member++;
241 #endif
243 return(False);