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"
26 /*******************************************************************
27 Map a username from a dos name to a unix name by looking in the username
28 map. Note that this modifies the name in place.
29 This is the main function that should be called *once* on
30 any incoming or new username - in order to canonicalize the name.
31 This is being done to de-couple the case conversions from the user mapping
32 function. Previously, the map_username was being called
33 every time Get_Pwnam_alloc was called.
34 Returns True if username was changed, false otherwise.
35 ********************************************************************/
37 static char *last_from
= NULL
;
38 static char *last_to
= NULL
;
40 static const char *get_last_from(void)
48 static const char *get_last_to(void)
56 static bool set_last_from_to(const char *from
, const char *to
)
58 char *orig_from
= last_from
;
59 char *orig_to
= last_to
;
61 last_from
= SMB_STRDUP(from
);
62 last_to
= SMB_STRDUP(to
);
67 if (!last_from
|| !last_to
) {
75 static char *skip_space(char *s
)
77 while (isspace((int)(*s
))) {
83 static bool fetch_map_from_gencache(TALLOC_CTX
*ctx
,
90 if (lp_username_map_cache_time() == 0) {
94 key
= talloc_asprintf_strupper_m(ctx
, "USERNAME_MAP/%s",
99 found
= gencache_get(key
, &value
, NULL
);
104 TALLOC_FREE(*p_user_out
);
105 *p_user_out
= talloc_strdup(ctx
, value
);
113 static void store_map_in_gencache(TALLOC_CTX
*ctx
, const char *from
, const char *to
)
116 int cache_time
= lp_username_map_cache_time();
118 if (cache_time
== 0) {
122 key
= talloc_asprintf_strupper_m(ctx
, "USERNAME_MAP/%s",
127 gencache_set(key
, to
, cache_time
+ time(NULL
));
131 /****************************************************************************
132 Check if a user is in a netgroup user list. If at first we don't succeed,
134 ****************************************************************************/
136 bool user_in_netgroup(TALLOC_CTX
*ctx
, const char *user
, const char *ngname
)
139 static char *my_yp_domain
= NULL
;
140 char *lowercase_user
= NULL
;
142 if (my_yp_domain
== NULL
) {
143 yp_get_default_domain(&my_yp_domain
);
146 if (my_yp_domain
== NULL
) {
147 DEBUG(5,("Unable to get default yp domain, "
148 "let's try without specifying it\n"));
151 DEBUG(5,("looking for user %s of domain %s in netgroup %s\n",
152 user
, my_yp_domain
?my_yp_domain
:"(ANY)", ngname
));
154 if (innetgr(ngname
, NULL
, user
, my_yp_domain
)) {
155 DEBUG(5,("user_in_netgroup: Found\n"));
160 * Ok, innetgr is case sensitive. Try once more with lowercase
161 * just in case. Attempt to fix #703. JRA.
163 lowercase_user
= talloc_strdup(ctx
, user
);
164 if (!lowercase_user
) {
167 if (!strlower_m(lowercase_user
)) {
171 if (strcmp(user
,lowercase_user
) == 0) {
172 /* user name was already lower case! */
176 DEBUG(5,("looking for user %s of domain %s in netgroup %s\n",
177 lowercase_user
, my_yp_domain
?my_yp_domain
:"(ANY)", ngname
));
179 if (innetgr(ngname
, NULL
, lowercase_user
, my_yp_domain
)) {
180 DEBUG(5,("user_in_netgroup: Found\n"));
183 #endif /* HAVE_NETGROUP */
187 /****************************************************************************
188 Check if a user is in a user list - can check combinations of UNIX
190 ****************************************************************************/
192 bool user_in_list(TALLOC_CTX
*ctx
, const char *user
,const char **list
)
197 DEBUG(10,("user_in_list: checking user %s in list\n", user
));
201 DEBUG(10,("user_in_list: checking user |%s| against |%s|\n",
205 * Check raw username.
207 if (strequal(user
, *list
))
211 * Now check to see if any combination
212 * of UNIX and netgroups has been specified.
217 * Old behaviour. Check netgroup list
218 * followed by UNIX list.
220 if(user_in_netgroup(ctx
, user
, *list
+1))
222 if(user_in_group(user
, *list
+1))
224 } else if (**list
== '+') {
226 if((*(*list
+1)) == '&') {
228 * Search UNIX list followed by netgroup.
230 if(user_in_group(user
, *list
+2))
232 if(user_in_netgroup(ctx
, user
, *list
+2))
238 * Just search UNIX list.
241 if(user_in_group(user
, *list
+1))
245 } else if (**list
== '&') {
247 if(*(*list
+1) == '+') {
249 * Search netgroup list followed by UNIX list.
251 if(user_in_netgroup(ctx
, user
, *list
+2))
253 if(user_in_group(user
, *list
+2))
257 * Just search netgroup list.
259 if(user_in_netgroup(ctx
, user
, *list
+1))
269 bool map_username(TALLOC_CTX
*ctx
, const char *user_in
, char **p_user_out
)
272 char *mapfile
= lp_username_map(talloc_tos());
275 bool mapped_user
= False
;
276 char *cmd
= lp_username_map_script(talloc_tos());
283 /* Initially make a copy of the incoming name. */
284 *p_user_out
= talloc_strdup(ctx
, user_in
);
289 if (strequal(user_in
,get_last_to()))
292 if (strequal(user_in
,get_last_from())) {
293 DEBUG(3,("Mapped user %s to %s\n",user_in
,get_last_to()));
294 TALLOC_FREE(*p_user_out
);
295 *p_user_out
= talloc_strdup(ctx
, get_last_to());
299 if (fetch_map_from_gencache(ctx
, user_in
, p_user_out
)) {
303 /* first try the username map script */
307 char *command
= NULL
;
308 int numlines
, ret
, fd
;
310 command
= talloc_asprintf(ctx
,
318 DEBUG(10,("Running [%s]\n", command
));
319 ret
= smbrun(command
, &fd
);
320 DEBUGADD(10,("returned [%d]\n", ret
));
322 TALLOC_FREE(command
);
331 qlines
= fd_lines_load(fd
, &numlines
, 0, ctx
);
332 DEBUGADD(10,("Lines returned = [%d]\n", numlines
));
335 /* should be either no lines or a single line with the mapped username */
337 if (numlines
&& qlines
) {
338 DEBUG(3,("Mapped user %s to %s\n", user_in
, qlines
[0] ));
339 set_last_from_to(user_in
, qlines
[0]);
340 store_map_in_gencache(ctx
, user_in
, qlines
[0]);
341 TALLOC_FREE(*p_user_out
);
342 *p_user_out
= talloc_strdup(ctx
, qlines
[0]);
350 return numlines
!= 0;
353 /* ok. let's try the mapfile */
357 f
= x_fopen(mapfile
,O_RDONLY
, 0);
359 DEBUG(0,("can't open username map %s. Error %s\n",mapfile
, strerror(errno
) ));
363 DEBUG(4,("Scanning username map %s\n",mapfile
));
365 while((s
=fgets_slash(buf
,sizeof(buf
),f
))!=NULL
) {
367 char *dosname
= strchr_m(unixname
,'=');
369 bool return_if_mapped
= False
;
376 unixname
= skip_space(unixname
);
378 if ('!' == *unixname
) {
379 return_if_mapped
= True
;
380 unixname
= skip_space(unixname
+1);
383 if (!*unixname
|| strchr_m("#;",*unixname
))
387 int l
= strlen(unixname
);
388 while (l
&& isspace((int)unixname
[l
-1])) {
394 /* skip lines like 'user = ' */
396 dosuserlist
= str_list_make_v3(ctx
, dosname
, NULL
);
398 DEBUG(0,("Bad username map entry. Unable to build user list. Ignoring.\n"));
402 if (strchr_m(dosname
,'*') ||
403 user_in_list(ctx
, user_in
, (const char **)dosuserlist
)) {
404 DEBUG(3,("Mapped user %s to %s\n",user_in
,unixname
));
407 set_last_from_to(user_in
, unixname
);
408 store_map_in_gencache(ctx
, user_in
, unixname
);
409 TALLOC_FREE(*p_user_out
);
410 *p_user_out
= talloc_strdup(ctx
, unixname
);
412 TALLOC_FREE(dosuserlist
);
417 if ( return_if_mapped
) {
418 TALLOC_FREE(dosuserlist
);
424 TALLOC_FREE(dosuserlist
);
430 * If we didn't successfully map a user in the loop above,
431 * setup the last_from and last_to as an optimization so
432 * that we don't scan the file again for the same user.
435 DEBUG(8, ("The user '%s' has no mapping. "
436 "Skip it next time.\n", user_in
));
437 set_last_from_to(user_in
, user_in
);
438 store_map_in_gencache(ctx
, user_in
, user_in
);