winbindd: Do not overwrite domain list with conflicting info from a trusted domain
[sfrench/samba-autobuild/.git] / source3 / auth / user_util.c
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
7
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.
12
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.
17
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/>.
20 */
21
22 #include "includes.h"
23 #include "system/filesys.h"
24 #include "auth.h"
25
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 ********************************************************************/
36
37 static char *last_from = NULL;
38 static char *last_to = NULL;
39
40 static const char *get_last_from(void)
41 {
42         if (!last_from) {
43                 return "";
44         }
45         return last_from;
46 }
47
48 static const char *get_last_to(void)
49 {
50         if (!last_to) {
51                 return "";
52         }
53         return last_to;
54 }
55
56 static bool set_last_from_to(const char *from, const char *to)
57 {
58         char *orig_from = last_from;
59         char *orig_to = last_to;
60
61         last_from = SMB_STRDUP(from);
62         last_to = SMB_STRDUP(to);
63
64         SAFE_FREE(orig_from);
65         SAFE_FREE(orig_to);
66
67         if (!last_from || !last_to) {
68                 SAFE_FREE(last_from);
69                 SAFE_FREE(last_to);
70                 return false;
71         }
72         return true;
73 }
74
75 static char *skip_space(char *s)
76 {
77         while (isspace((int)(*s))) {
78                 s += 1;
79         }
80         return s;
81 }
82
83 static bool fetch_map_from_gencache(TALLOC_CTX *ctx,
84                         const char *user_in,
85                         char **p_user_out)
86 {
87         char *key, *value;
88         bool found;
89
90         if (lp_username_map_cache_time() == 0) {
91                 return false;
92         }
93
94         key = talloc_asprintf_strupper_m(ctx, "USERNAME_MAP/%s",
95                                          user_in);
96         if (key == NULL) {
97                 return false;
98         }
99         found = gencache_get(key, ctx, &value, NULL);
100         TALLOC_FREE(key);
101         if (!found) {
102                 return false;
103         }
104         TALLOC_FREE(*p_user_out);
105         *p_user_out = value;
106         if (!*p_user_out) {
107                 return false;
108         }
109         return true;
110 }
111
112 static void store_map_in_gencache(TALLOC_CTX *ctx, const char *from, const char *to)
113 {
114         char *key;
115         int cache_time = lp_username_map_cache_time();
116
117         if (cache_time == 0) {
118                 return;
119         }
120
121         key = talloc_asprintf_strupper_m(ctx, "USERNAME_MAP/%s",
122                                          from);
123         if (key == NULL) {
124                 return;
125         }
126         gencache_set(key, to, cache_time + time(NULL));
127         TALLOC_FREE(key);
128 }
129
130 /****************************************************************************
131  Check if a user is in a netgroup user list. If at first we don't succeed,
132  try lower case.
133 ****************************************************************************/
134
135 bool user_in_netgroup(TALLOC_CTX *ctx, const char *user, const char *ngname)
136 {
137 #ifdef HAVE_NETGROUP
138         static char *my_yp_domain = NULL;
139         char *lowercase_user = NULL;
140
141         if (my_yp_domain == NULL) {
142                 yp_get_default_domain(&my_yp_domain);
143         }
144
145         if (my_yp_domain == NULL) {
146                 DEBUG(5,("Unable to get default yp domain, "
147                         "let's try without specifying it\n"));
148         }
149
150         DEBUG(5,("looking for user %s of domain %s in netgroup %s\n",
151                 user, my_yp_domain?my_yp_domain:"(ANY)", ngname));
152
153         if (innetgr(ngname, NULL, user, my_yp_domain)) {
154                 DEBUG(5,("user_in_netgroup: Found\n"));
155                 return true;
156         }
157
158         /*
159          * Ok, innetgr is case sensitive. Try once more with lowercase
160          * just in case. Attempt to fix #703. JRA.
161          */
162         lowercase_user = talloc_strdup(ctx, user);
163         if (!lowercase_user) {
164                 return false;
165         }
166         if (!strlower_m(lowercase_user)) {
167                 return false;
168         }
169
170         if (strcmp(user,lowercase_user) == 0) {
171                 /* user name was already lower case! */
172                 return false;
173         }
174
175         DEBUG(5,("looking for user %s of domain %s in netgroup %s\n",
176                 lowercase_user, my_yp_domain?my_yp_domain:"(ANY)", ngname));
177
178         if (innetgr(ngname, NULL, lowercase_user, my_yp_domain)) {
179                 DEBUG(5,("user_in_netgroup: Found\n"));
180                 return true;
181         }
182 #endif /* HAVE_NETGROUP */
183         return false;
184 }
185
186 /****************************************************************************
187  Check if a user is in a user list - can check combinations of UNIX
188  and netgroup lists.
189 ****************************************************************************/
190
191 bool user_in_list(TALLOC_CTX *ctx, const char *user,const char **list)
192 {
193         if (!list || !*list)
194                 return False;
195
196         DEBUG(10,("user_in_list: checking user %s in list\n", user));
197
198         while (*list) {
199
200                 DEBUG(10,("user_in_list: checking user |%s| against |%s|\n",
201                           user, *list));
202
203                 /*
204                  * Check raw username.
205                  */
206                 if (strequal(user, *list))
207                         return(True);
208
209                 /*
210                  * Now check to see if any combination
211                  * of UNIX and netgroups has been specified.
212                  */
213
214                 if(**list == '@') {
215                         /*
216                          * Old behaviour. Check netgroup list
217                          * followed by UNIX list.
218                          */
219                         if(user_in_netgroup(ctx, user, *list +1))
220                                 return True;
221                         if(user_in_group(user, *list +1))
222                                 return True;
223                 } else if (**list == '+') {
224
225                         if((*(*list +1)) == '&') {
226                                 /*
227                                  * Search UNIX list followed by netgroup.
228                                  */
229                                 if(user_in_group(user, *list +2))
230                                         return True;
231                                 if(user_in_netgroup(ctx, user, *list +2))
232                                         return True;
233
234                         } else {
235
236                                 /*
237                                  * Just search UNIX list.
238                                  */
239
240                                 if(user_in_group(user, *list +1))
241                                         return True;
242                         }
243
244                 } else if (**list == '&') {
245
246                         if(*(*list +1) == '+') {
247                                 /*
248                                  * Search netgroup list followed by UNIX list.
249                                  */
250                                 if(user_in_netgroup(ctx, user, *list +2))
251                                         return True;
252                                 if(user_in_group(user, *list +2))
253                                         return True;
254                         } else {
255                                 /*
256                                  * Just search netgroup list.
257                                  */
258                                 if(user_in_netgroup(ctx, user, *list +1))
259                                         return True;
260                         }
261                 }
262
263                 list++;
264         }
265         return(False);
266 }
267
268 bool map_username(TALLOC_CTX *ctx, const char *user_in, char **p_user_out)
269 {
270         XFILE *f;
271         char *mapfile = lp_username_map(talloc_tos());
272         char *s;
273         char buf[512];
274         bool mapped_user = False;
275         char *cmd = lp_username_map_script(talloc_tos());
276
277         *p_user_out = NULL;
278
279         if (!user_in)
280                 return false;
281
282         /* Initially make a copy of the incoming name. */
283         *p_user_out = talloc_strdup(ctx, user_in);
284         if (!*p_user_out) {
285                 return false;
286         }
287
288         if (strequal(user_in,get_last_to()))
289                 return false;
290
291         if (strequal(user_in,get_last_from())) {
292                 DEBUG(3,("Mapped user %s to %s\n",user_in,get_last_to()));
293                 TALLOC_FREE(*p_user_out);
294                 *p_user_out = talloc_strdup(ctx, get_last_to());
295                 return true;
296         }
297
298         if (fetch_map_from_gencache(ctx, user_in, p_user_out)) {
299                 return true;
300         }
301
302         /* first try the username map script */
303
304         if ( *cmd ) {
305                 char **qlines;
306                 char *command = NULL;
307                 int numlines, ret, fd;
308
309                 command = talloc_asprintf(ctx,
310                                         "%s \"%s\"",
311                                         cmd,
312                                         user_in);
313                 if (!command) {
314                         return false;
315                 }
316
317                 DEBUG(10,("Running [%s]\n", command));
318                 ret = smbrun(command, &fd);
319                 DEBUGADD(10,("returned [%d]\n", ret));
320
321                 TALLOC_FREE(command);
322
323                 if ( ret != 0 ) {
324                         if (fd != -1)
325                                 close(fd);
326                         return False;
327                 }
328
329                 numlines = 0;
330                 qlines = fd_lines_load(fd, &numlines, 0, ctx);
331                 DEBUGADD(10,("Lines returned = [%d]\n", numlines));
332                 close(fd);
333
334                 /* should be either no lines or a single line with the mapped username */
335
336                 if (numlines && qlines) {
337                         DEBUG(3,("Mapped user %s to %s\n", user_in, qlines[0] ));
338                         set_last_from_to(user_in, qlines[0]);
339                         store_map_in_gencache(ctx, user_in, qlines[0]);
340                         TALLOC_FREE(*p_user_out);
341                         *p_user_out = talloc_strdup(ctx, qlines[0]);
342                         if (!*p_user_out) {
343                                 return false;
344                         }
345                 }
346
347                 TALLOC_FREE(qlines);
348
349                 return numlines != 0;
350         }
351
352         /* ok.  let's try the mapfile */
353         if (!*mapfile)
354                 return False;
355
356         f = x_fopen(mapfile,O_RDONLY, 0);
357         if (!f) {
358                 DEBUG(0,("can't open username map %s. Error %s\n",mapfile, strerror(errno) ));
359                 return False;
360         }
361
362         DEBUG(4,("Scanning username map %s\n",mapfile));
363
364         while((s=fgets_slash(buf,sizeof(buf),f))!=NULL) {
365                 char *unixname = s;
366                 char *dosname = strchr_m(unixname,'=');
367                 char **dosuserlist;
368                 bool return_if_mapped = False;
369
370                 if (!dosname)
371                         continue;
372
373                 *dosname++ = 0;
374
375                 unixname = skip_space(unixname);
376
377                 if ('!' == *unixname) {
378                         return_if_mapped = True;
379                         unixname = skip_space(unixname+1);
380                 }
381
382                 if (!*unixname || strchr_m("#;",*unixname))
383                         continue;
384
385                 {
386                         int l = strlen(unixname);
387                         while (l && isspace((int)unixname[l-1])) {
388                                 unixname[l-1] = 0;
389                                 l--;
390                         }
391                 }
392
393                 /* skip lines like 'user = ' */
394
395                 dosuserlist = str_list_make_v3(ctx, dosname, NULL);
396                 if (!dosuserlist) {
397                         DEBUG(0,("Bad username map entry.  Unable to build user list.  Ignoring.\n"));
398                         continue;
399                 }
400
401                 if (strchr_m(dosname,'*') ||
402                     user_in_list(ctx, user_in, (const char **)dosuserlist)) {
403                         DEBUG(3,("Mapped user %s to %s\n",user_in,unixname));
404                         mapped_user = True;
405
406                         set_last_from_to(user_in, unixname);
407                         store_map_in_gencache(ctx, user_in, unixname);
408                         TALLOC_FREE(*p_user_out);
409                         *p_user_out = talloc_strdup(ctx, unixname);
410                         if (!*p_user_out) {
411                                 TALLOC_FREE(dosuserlist);
412                                 x_fclose(f);
413                                 return false;
414                         }
415
416                         if ( return_if_mapped ) {
417                                 TALLOC_FREE(dosuserlist);
418                                 x_fclose(f);
419                                 return True;
420                         }
421                 }
422
423                 TALLOC_FREE(dosuserlist);
424         }
425
426         x_fclose(f);
427
428         /*
429          * If we didn't successfully map a user in the loop above,
430          * setup the last_from and last_to as an optimization so
431          * that we don't scan the file again for the same user.
432          */
433         if (!mapped_user) {
434                 DEBUG(8, ("The user '%s' has no mapping. "
435                           "Skip it next time.\n", user_in));
436                 set_last_from_to(user_in, user_in);
437                 store_map_in_gencache(ctx, user_in, user_in);
438         }
439
440         return mapped_user;
441 }