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