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"
25 #include "lib/gencache.h"
27 /*******************************************************************
28 Map a username from a dos name to a unix name by looking in the username
29 map. Note that this modifies the name in place.
30 This is the main function that should be called *once* on
31 any incoming or new username - in order to canonicalize the name.
32 This is being done to de-couple the case conversions from the user mapping
33 function. Previously, the map_username was being called
34 every time Get_Pwnam_alloc was called.
35 Returns True if username was changed, false otherwise.
36 ********************************************************************/
38 static char *last_from
= NULL
;
39 static char *last_to
= NULL
;
41 static const char *get_last_from(void)
49 static const char *get_last_to(void)
57 static bool set_last_from_to(const char *from
, const char *to
)
59 char *orig_from
= last_from
;
60 char *orig_to
= last_to
;
62 last_from
= SMB_STRDUP(from
);
63 last_to
= SMB_STRDUP(to
);
68 if (!last_from
|| !last_to
) {
76 static char *skip_space(char *s
)
78 while (isspace((int)(*s
))) {
84 static bool fetch_map_from_gencache(TALLOC_CTX
*ctx
,
91 if (lp_username_map_cache_time() == 0) {
95 key
= talloc_asprintf_strupper_m(ctx
, "USERNAME_MAP/%s",
100 found
= gencache_get(key
, ctx
, &value
, NULL
);
105 TALLOC_FREE(*p_user_out
);
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 user list
134 We removed NIS support in 2021, but need to keep configs working.
136 TOOD FIXME: Remove this funciton
137 ****************************************************************************/
139 bool user_in_list(TALLOC_CTX
*ctx
, const char *user
, const char * const *list
)
142 if (list
== NULL
|| *list
== NULL
) {
146 DBG_DEBUG("Checking user %s in list\n", user
);
149 const char *p
= *list
;
152 /* Check raw username */
153 if (strequal(user
, p
)) {
157 while (*p
== '@' || *p
== '&' || *p
== '+') {
161 ok
= user_in_group(user
, p
);
172 bool map_username(TALLOC_CTX
*ctx
, const char *user_in
, char **p_user_out
)
174 const struct loadparm_substitution
*lp_sub
=
175 loadparm_s3_global_substitution();
177 char *mapfile
= lp_username_map(talloc_tos(), lp_sub
);
180 bool mapped_user
= False
;
181 char *cmd
= lp_username_map_script(talloc_tos(), lp_sub
);
188 /* Initially make a copy of the incoming name. */
189 *p_user_out
= talloc_strdup(ctx
, user_in
);
194 if (strequal(user_in
,get_last_to()))
197 if (strequal(user_in
,get_last_from())) {
198 DEBUG(3,("Mapped user %s to %s\n",user_in
,get_last_to()));
199 TALLOC_FREE(*p_user_out
);
200 *p_user_out
= talloc_strdup(ctx
, get_last_to());
204 if (fetch_map_from_gencache(ctx
, user_in
, p_user_out
)) {
208 /* first try the username map script */
212 char *command
= NULL
;
213 int numlines
, ret
, fd
;
215 command
= talloc_asprintf(ctx
,
223 DEBUG(10,("Running [%s]\n", command
));
224 ret
= smbrun(command
, &fd
, NULL
);
225 DEBUGADD(10,("returned [%d]\n", ret
));
227 TALLOC_FREE(command
);
236 qlines
= fd_lines_load(fd
, &numlines
, 0, ctx
);
237 DEBUGADD(10,("Lines returned = [%d]\n", numlines
));
240 /* should be either no lines or a single line with the mapped username */
242 if (numlines
&& qlines
) {
243 DEBUG(3,("Mapped user %s to %s\n", user_in
, qlines
[0] ));
244 set_last_from_to(user_in
, qlines
[0]);
245 store_map_in_gencache(ctx
, user_in
, qlines
[0]);
246 TALLOC_FREE(*p_user_out
);
247 *p_user_out
= talloc_strdup(ctx
, qlines
[0]);
255 return numlines
!= 0;
258 /* ok. let's try the mapfile */
262 f
= fopen(mapfile
, "r");
264 DEBUG(0,("can't open username map %s. Error %s\n",mapfile
, strerror(errno
) ));
268 DEBUG(4,("Scanning username map %s\n",mapfile
));
270 while((s
=fgets_slash(NULL
,buf
,sizeof(buf
),f
))!=NULL
) {
272 char *dosname
= strchr_m(unixname
,'=');
274 bool return_if_mapped
= False
;
281 unixname
= skip_space(unixname
);
283 if ('!' == *unixname
) {
284 return_if_mapped
= True
;
285 unixname
= skip_space(unixname
+1);
288 if (!*unixname
|| strchr_m("#;",*unixname
))
292 int l
= strlen(unixname
);
293 while (l
&& isspace((int)unixname
[l
-1])) {
299 /* skip lines like 'user = ' */
301 dosuserlist
= str_list_make_v3(ctx
, dosname
, NULL
);
303 DEBUG(0,("Bad username map entry. Unable to build user list. Ignoring.\n"));
307 if (strchr_m(dosname
,'*') ||
308 user_in_list(ctx
, user_in
, (const char * const *)dosuserlist
)) {
309 DEBUG(3,("Mapped user %s to %s\n",user_in
,unixname
));
312 set_last_from_to(user_in
, unixname
);
313 store_map_in_gencache(ctx
, user_in
, unixname
);
314 TALLOC_FREE(*p_user_out
);
315 *p_user_out
= talloc_strdup(ctx
, unixname
);
317 TALLOC_FREE(dosuserlist
);
322 if ( return_if_mapped
) {
323 TALLOC_FREE(dosuserlist
);
329 TALLOC_FREE(dosuserlist
);
335 * If we didn't successfully map a user in the loop above,
336 * setup the last_from and last_to as an optimization so
337 * that we don't scan the file again for the same user.
340 DEBUG(8, ("The user '%s' has no mapping. "
341 "Skip it next time.\n", user_in
));
342 set_last_from_to(user_in
, user_in
);
343 store_map_in_gencache(ctx
, user_in
, user_in
);