2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 1997-2001.
6 Copyright (C) Volker Lendecke 2006
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "system/filesys.h"
27 /* rpc/xdr.h uses TRUE and FALSE */
36 #include "system/nis.h"
39 /*******************************************************************
40 Map a username from a dos name to a unix name by looking in the username
41 map. Note that this modifies the name in place.
42 This is the main function that should be called *once* on
43 any incoming or new username - in order to canonicalize the name.
44 This is being done to de-couple the case conversions from the user mapping
45 function. Previously, the map_username was being called
46 every time Get_Pwnam_alloc was called.
47 Returns True if username was changed, false otherwise.
48 ********************************************************************/
50 static char *last_from
= NULL
;
51 static char *last_to
= NULL
;
53 static const char *get_last_from(void)
61 static const char *get_last_to(void)
69 static bool set_last_from_to(const char *from
, const char *to
)
71 char *orig_from
= last_from
;
72 char *orig_to
= last_to
;
74 last_from
= SMB_STRDUP(from
);
75 last_to
= SMB_STRDUP(to
);
80 if (!last_from
|| !last_to
) {
88 static char *skip_space(char *s
)
90 while (isspace((int)(*s
))) {
96 static bool fetch_map_from_gencache(TALLOC_CTX
*ctx
,
103 if (lp_username_map_cache_time() == 0) {
107 key
= talloc_asprintf_strupper_m(ctx
, "USERNAME_MAP/%s",
112 found
= gencache_get(key
, ctx
, &value
, NULL
);
117 TALLOC_FREE(*p_user_out
);
125 static void store_map_in_gencache(TALLOC_CTX
*ctx
, const char *from
, const char *to
)
128 int cache_time
= lp_username_map_cache_time();
130 if (cache_time
== 0) {
134 key
= talloc_asprintf_strupper_m(ctx
, "USERNAME_MAP/%s",
139 gencache_set(key
, to
, cache_time
+ time(NULL
));
143 /****************************************************************************
144 Check if a user is in a netgroup user list. If at first we don't succeed,
146 ****************************************************************************/
148 bool user_in_netgroup(TALLOC_CTX
*ctx
, const char *user
, const char *ngname
)
151 static char *my_yp_domain
= NULL
;
152 char *lowercase_user
= NULL
;
154 if (my_yp_domain
== NULL
) {
155 yp_get_default_domain(&my_yp_domain
);
158 if (my_yp_domain
== NULL
) {
159 DEBUG(5,("Unable to get default yp domain, "
160 "let's try without specifying it\n"));
163 DEBUG(5,("looking for user %s of domain %s in netgroup %s\n",
164 user
, my_yp_domain
?my_yp_domain
:"(ANY)", ngname
));
166 if (innetgr(ngname
, NULL
, user
, my_yp_domain
)) {
167 DEBUG(5,("user_in_netgroup: Found\n"));
172 * Ok, innetgr is case sensitive. Try once more with lowercase
173 * just in case. Attempt to fix #703. JRA.
175 lowercase_user
= talloc_strdup(ctx
, user
);
176 if (!lowercase_user
) {
179 if (!strlower_m(lowercase_user
)) {
183 if (strcmp(user
,lowercase_user
) == 0) {
184 /* user name was already lower case! */
188 DEBUG(5,("looking for user %s of domain %s in netgroup %s\n",
189 lowercase_user
, my_yp_domain
?my_yp_domain
:"(ANY)", ngname
));
191 if (innetgr(ngname
, NULL
, lowercase_user
, my_yp_domain
)) {
192 DEBUG(5,("user_in_netgroup: Found\n"));
195 #endif /* HAVE_NETGROUP */
199 /****************************************************************************
200 Check if a user is in a user list - can check combinations of UNIX
202 ****************************************************************************/
204 bool user_in_list(TALLOC_CTX
*ctx
, const char *user
, const char * const *list
)
209 DEBUG(10,("user_in_list: checking user %s in list\n", user
));
213 DEBUG(10,("user_in_list: checking user |%s| against |%s|\n",
217 * Check raw username.
219 if (strequal(user
, *list
))
223 * Now check to see if any combination
224 * of UNIX and netgroups has been specified.
229 * Old behaviour. Check netgroup list
230 * followed by UNIX list.
232 if(user_in_netgroup(ctx
, user
, *list
+1))
234 if(user_in_group(user
, *list
+1))
236 } else if (**list
== '+') {
238 if((*(*list
+1)) == '&') {
240 * Search UNIX list followed by netgroup.
242 if(user_in_group(user
, *list
+2))
244 if(user_in_netgroup(ctx
, user
, *list
+2))
250 * Just search UNIX list.
253 if(user_in_group(user
, *list
+1))
257 } else if (**list
== '&') {
259 if(*(*list
+1) == '+') {
261 * Search netgroup list followed by UNIX list.
263 if(user_in_netgroup(ctx
, user
, *list
+2))
265 if(user_in_group(user
, *list
+2))
269 * Just search netgroup list.
271 if(user_in_netgroup(ctx
, user
, *list
+1))
281 bool map_username(TALLOC_CTX
*ctx
, const char *user_in
, char **p_user_out
)
284 char *mapfile
= lp_username_map(talloc_tos());
287 bool mapped_user
= False
;
288 char *cmd
= lp_username_map_script(talloc_tos());
295 /* Initially make a copy of the incoming name. */
296 *p_user_out
= talloc_strdup(ctx
, user_in
);
301 if (strequal(user_in
,get_last_to()))
304 if (strequal(user_in
,get_last_from())) {
305 DEBUG(3,("Mapped user %s to %s\n",user_in
,get_last_to()));
306 TALLOC_FREE(*p_user_out
);
307 *p_user_out
= talloc_strdup(ctx
, get_last_to());
311 if (fetch_map_from_gencache(ctx
, user_in
, p_user_out
)) {
315 /* first try the username map script */
319 char *command
= NULL
;
320 int numlines
, ret
, fd
;
322 command
= talloc_asprintf(ctx
,
330 DEBUG(10,("Running [%s]\n", command
));
331 ret
= smbrun(command
, &fd
, NULL
);
332 DEBUGADD(10,("returned [%d]\n", ret
));
334 TALLOC_FREE(command
);
343 qlines
= fd_lines_load(fd
, &numlines
, 0, ctx
);
344 DEBUGADD(10,("Lines returned = [%d]\n", numlines
));
347 /* should be either no lines or a single line with the mapped username */
349 if (numlines
&& qlines
) {
350 DEBUG(3,("Mapped user %s to %s\n", user_in
, qlines
[0] ));
351 set_last_from_to(user_in
, qlines
[0]);
352 store_map_in_gencache(ctx
, user_in
, qlines
[0]);
353 TALLOC_FREE(*p_user_out
);
354 *p_user_out
= talloc_strdup(ctx
, qlines
[0]);
362 return numlines
!= 0;
365 /* ok. let's try the mapfile */
369 f
= fopen(mapfile
, "r");
371 DEBUG(0,("can't open username map %s. Error %s\n",mapfile
, strerror(errno
) ));
375 DEBUG(4,("Scanning username map %s\n",mapfile
));
377 while((s
=fgets_slash(NULL
,buf
,sizeof(buf
),f
))!=NULL
) {
379 char *dosname
= strchr_m(unixname
,'=');
381 bool return_if_mapped
= False
;
388 unixname
= skip_space(unixname
);
390 if ('!' == *unixname
) {
391 return_if_mapped
= True
;
392 unixname
= skip_space(unixname
+1);
395 if (!*unixname
|| strchr_m("#;",*unixname
))
399 int l
= strlen(unixname
);
400 while (l
&& isspace((int)unixname
[l
-1])) {
406 /* skip lines like 'user = ' */
408 dosuserlist
= str_list_make_v3(ctx
, dosname
, NULL
);
410 DEBUG(0,("Bad username map entry. Unable to build user list. Ignoring.\n"));
414 if (strchr_m(dosname
,'*') ||
415 user_in_list(ctx
, user_in
, (const char * const *)dosuserlist
)) {
416 DEBUG(3,("Mapped user %s to %s\n",user_in
,unixname
));
419 set_last_from_to(user_in
, unixname
);
420 store_map_in_gencache(ctx
, user_in
, unixname
);
421 TALLOC_FREE(*p_user_out
);
422 *p_user_out
= talloc_strdup(ctx
, unixname
);
424 TALLOC_FREE(dosuserlist
);
429 if ( return_if_mapped
) {
430 TALLOC_FREE(dosuserlist
);
436 TALLOC_FREE(dosuserlist
);
442 * If we didn't successfully map a user in the loop above,
443 * setup the last_from and last_to as an optimization so
444 * that we don't scan the file again for the same user.
447 DEBUG(8, ("The user '%s' has no mapping. "
448 "Skip it next time.\n", user_in
));
449 set_last_from_to(user_in
, user_in
);
450 store_map_in_gencache(ctx
, user_in
, user_in
);