winbind: set_dc_type_and_flags() is not needed on a DC
[Samba.git] / source3 / auth / user_util.c
blob63841a1fe5145636555db93f6fb81dd5b7e3dde9
1 /*
2 Unix SMB/CIFS implementation.
3 Username handling
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/>.
22 #include "includes.h"
23 #include "system/filesys.h"
24 #include "auth.h"
26 #ifdef HAVE_NETGROUP
27 /* rpc/xdr.h uses TRUE and FALSE */
28 #ifdef TRUE
29 #undef TRUE
30 #endif
32 #ifdef FALSE
33 #undef FALSE
34 #endif
36 #include "system/nis.h"
37 #endif
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)
55 if (!last_from) {
56 return "";
58 return last_from;
61 static const char *get_last_to(void)
63 if (!last_to) {
64 return "";
66 return last_to;
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);
77 SAFE_FREE(orig_from);
78 SAFE_FREE(orig_to);
80 if (!last_from || !last_to) {
81 SAFE_FREE(last_from);
82 SAFE_FREE(last_to);
83 return false;
85 return true;
88 static char *skip_space(char *s)
90 while (isspace((int)(*s))) {
91 s += 1;
93 return s;
96 static bool fetch_map_from_gencache(TALLOC_CTX *ctx,
97 const char *user_in,
98 char **p_user_out)
100 char *key, *value;
101 bool found;
103 if (lp_username_map_cache_time() == 0) {
104 return false;
107 key = talloc_asprintf_strupper_m(ctx, "USERNAME_MAP/%s",
108 user_in);
109 if (key == NULL) {
110 return false;
112 found = gencache_get(key, ctx, &value, NULL);
113 TALLOC_FREE(key);
114 if (!found) {
115 return false;
117 TALLOC_FREE(*p_user_out);
118 *p_user_out = value;
119 if (!*p_user_out) {
120 return false;
122 return true;
125 static void store_map_in_gencache(TALLOC_CTX *ctx, const char *from, const char *to)
127 char *key;
128 int cache_time = lp_username_map_cache_time();
130 if (cache_time == 0) {
131 return;
134 key = talloc_asprintf_strupper_m(ctx, "USERNAME_MAP/%s",
135 from);
136 if (key == NULL) {
137 return;
139 gencache_set(key, to, cache_time + time(NULL));
140 TALLOC_FREE(key);
143 /****************************************************************************
144 Check if a user is in a netgroup user list. If at first we don't succeed,
145 try lower case.
146 ****************************************************************************/
148 bool user_in_netgroup(TALLOC_CTX *ctx, const char *user, const char *ngname)
150 #ifdef HAVE_NETGROUP
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"));
168 return true;
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) {
177 return false;
179 if (!strlower_m(lowercase_user)) {
180 return false;
183 if (strcmp(user,lowercase_user) == 0) {
184 /* user name was already lower case! */
185 return false;
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"));
193 return true;
195 #endif /* HAVE_NETGROUP */
196 return false;
199 /****************************************************************************
200 Check if a user is in a user list - can check combinations of UNIX
201 and netgroup lists.
202 ****************************************************************************/
204 bool user_in_list(TALLOC_CTX *ctx, const char *user, const char * const *list)
206 if (!list || !*list)
207 return False;
209 DEBUG(10,("user_in_list: checking user %s in list\n", user));
211 while (*list) {
213 DEBUG(10,("user_in_list: checking user |%s| against |%s|\n",
214 user, *list));
217 * Check raw username.
219 if (strequal(user, *list))
220 return(True);
223 * Now check to see if any combination
224 * of UNIX and netgroups has been specified.
227 if(**list == '@') {
229 * Old behaviour. Check netgroup list
230 * followed by UNIX list.
232 if(user_in_netgroup(ctx, user, *list +1))
233 return True;
234 if(user_in_group(user, *list +1))
235 return True;
236 } else if (**list == '+') {
238 if((*(*list +1)) == '&') {
240 * Search UNIX list followed by netgroup.
242 if(user_in_group(user, *list +2))
243 return True;
244 if(user_in_netgroup(ctx, user, *list +2))
245 return True;
247 } else {
250 * Just search UNIX list.
253 if(user_in_group(user, *list +1))
254 return True;
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))
264 return True;
265 if(user_in_group(user, *list +2))
266 return True;
267 } else {
269 * Just search netgroup list.
271 if(user_in_netgroup(ctx, user, *list +1))
272 return True;
276 list++;
278 return(False);
281 bool map_username(TALLOC_CTX *ctx, const char *user_in, char **p_user_out)
283 FILE *f;
284 char *mapfile = lp_username_map(talloc_tos());
285 char *s;
286 char buf[512];
287 bool mapped_user = False;
288 char *cmd = lp_username_map_script(talloc_tos());
290 *p_user_out = NULL;
292 if (!user_in)
293 return false;
295 /* Initially make a copy of the incoming name. */
296 *p_user_out = talloc_strdup(ctx, user_in);
297 if (!*p_user_out) {
298 return false;
301 if (strequal(user_in,get_last_to()))
302 return false;
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());
308 return true;
311 if (fetch_map_from_gencache(ctx, user_in, p_user_out)) {
312 return true;
315 /* first try the username map script */
317 if ( *cmd ) {
318 char **qlines;
319 char *command = NULL;
320 int numlines, ret, fd;
322 command = talloc_asprintf(ctx,
323 "%s \"%s\"",
324 cmd,
325 user_in);
326 if (!command) {
327 return false;
330 DEBUG(10,("Running [%s]\n", command));
331 ret = smbrun(command, &fd, NULL);
332 DEBUGADD(10,("returned [%d]\n", ret));
334 TALLOC_FREE(command);
336 if ( ret != 0 ) {
337 if (fd != -1)
338 close(fd);
339 return False;
342 numlines = 0;
343 qlines = fd_lines_load(fd, &numlines, 0, ctx);
344 DEBUGADD(10,("Lines returned = [%d]\n", numlines));
345 close(fd);
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]);
355 if (!*p_user_out) {
356 return false;
360 TALLOC_FREE(qlines);
362 return numlines != 0;
365 /* ok. let's try the mapfile */
366 if (!*mapfile)
367 return False;
369 f = fopen(mapfile, "r");
370 if (!f) {
371 DEBUG(0,("can't open username map %s. Error %s\n",mapfile, strerror(errno) ));
372 return False;
375 DEBUG(4,("Scanning username map %s\n",mapfile));
377 while((s=fgets_slash(NULL,buf,sizeof(buf),f))!=NULL) {
378 char *unixname = s;
379 char *dosname = strchr_m(unixname,'=');
380 char **dosuserlist;
381 bool return_if_mapped = False;
383 if (!dosname)
384 continue;
386 *dosname++ = 0;
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))
396 continue;
399 int l = strlen(unixname);
400 while (l && isspace((int)unixname[l-1])) {
401 unixname[l-1] = 0;
402 l--;
406 /* skip lines like 'user = ' */
408 dosuserlist = str_list_make_v3(ctx, dosname, NULL);
409 if (!dosuserlist) {
410 DEBUG(0,("Bad username map entry. Unable to build user list. Ignoring.\n"));
411 continue;
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));
417 mapped_user = True;
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);
423 if (!*p_user_out) {
424 TALLOC_FREE(dosuserlist);
425 fclose(f);
426 return false;
429 if ( return_if_mapped ) {
430 TALLOC_FREE(dosuserlist);
431 fclose(f);
432 return True;
436 TALLOC_FREE(dosuserlist);
439 fclose(f);
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.
446 if (!mapped_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);
453 return mapped_user;