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