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