2 Unix SMB/Netbios implementation.
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.
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
);
40 /*******************************************************************
41 map a username from a dos name to a unix name by looking in the username
43 ********************************************************************/
44 void map_username(char *user
)
47 static BOOL initialised
=False
;
48 static fstring last_from
,last_to
;
51 char *mapfile
= lp_username_map();
52 if (!*mapfile
|| depth
) return;
57 *last_from
= *last_to
= 0;
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
));
69 f
= fopen(mapfile
,"r");
71 DEBUG(0,("can't open username map %s\n",mapfile
));
75 DEBUG(4,("Scanning username map %s\n",mapfile
));
79 for (; (s
=fgets_slash(NULL
,80,f
)); free(s
)) {
81 char *dosname
= strchr(unixname
,'=');
83 if (!dosname
) continue;
86 while (isspace(*unixname
)) unixname
++;
87 if (!*unixname
|| strchr("#;",*unixname
)) continue;
90 int l
= strlen(unixname
);
91 while (l
&& isspace(unixname
[l
-1])) {
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);
110 /****************************************************************************
111 internals of Get_Pwnam wrapper
112 ****************************************************************************/
113 static struct passwd
*_Get_Pwnam(char *s
)
121 struct passwd_adjunct
*pwret
;
122 pwret
= getpwanam(s
);
125 free(ret
->pw_passwd
);
126 ret
->pw_passwd
= pwret
->pwa_passwd
;
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
)
147 if (!user
|| !(*user
))
150 StrnCpy(user2
,user
,sizeof(user2
)-1);
158 ret
= _Get_Pwnam(user
);
159 if (ret
) return(ret
);
162 ret
= _Get_Pwnam(user
);
163 if (ret
) return(ret
);
166 ret
= _Get_Pwnam(user
);
167 if (ret
) return(ret
);
169 /* try with first letter capitalised */
170 if (strlen(user
) > 1)
172 ret
= _Get_Pwnam(user
);
173 if (ret
) return(ret
);
182 /****************************************************************************
183 check if a user is in a user list
184 ****************************************************************************/
185 BOOL
user_in_list(char *user
,char *list
)
190 while (next_token(&p
,tok
,LIST_SEP
))
192 if (strequal(user
,tok
))
198 static char *mydomain
= NULL
;
200 yp_get_default_domain(&mydomain
);
204 DEBUG(5,("Unable to get default yp domain\n"));
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
))
227 struct passwd
*pass
= Get_Pwnam(user
,False
);
230 gptr
= getgrgid(pass
->pw_gid
);
231 if (gptr
&& strequal(gptr
->gr_name
,&tok
[1]))
235 gptr
= (struct group
*)getgrnam(&tok
[1]);
239 member
= gptr
->gr_mem
;
240 while (member
&& *member
)
242 if (strequal(*member
,user
))