s3:libsmb: add tstream_cli_np_use_trans() and the needed logic
[Samba/gebeck_regimport.git] / source3 / auth / user_util.c
blobd6c47a83321eb6918134ac022e30897afa3e4a7c
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"
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 ********************************************************************/
35 static char *last_from = NULL;
36 static char *last_to = NULL;
38 static const char *get_last_from(void)
40 if (!last_from) {
41 return "";
43 return last_from;
46 static const char *get_last_to(void)
48 if (!last_to) {
49 return "";
51 return last_to;
54 static bool set_last_from_to(const char *from, const char *to)
56 char *orig_from = last_from;
57 char *orig_to = last_to;
59 last_from = SMB_STRDUP(from);
60 last_to = SMB_STRDUP(to);
62 SAFE_FREE(orig_from);
63 SAFE_FREE(orig_to);
65 if (!last_from || !last_to) {
66 SAFE_FREE(last_from);
67 SAFE_FREE(last_to);
68 return false;
70 return true;
73 static char *skip_space(char *s)
75 while (isspace((int)(*s))) {
76 s += 1;
78 return s;
81 static bool fetch_map_from_gencache(TALLOC_CTX *ctx,
82 const char *user_in,
83 char **p_user_out)
85 char *key, *value;
86 bool found;
88 if (lp_username_map_cache_time() == 0) {
89 return false;
92 key = talloc_asprintf_strupper_m(ctx, "USERNAME_MAP/%s",
93 user_in);
94 if (key == NULL) {
95 return false;
97 found = gencache_get(key, &value, NULL);
98 TALLOC_FREE(key);
99 if (!found) {
100 return false;
102 TALLOC_FREE(*p_user_out);
103 *p_user_out = talloc_strdup(ctx, value);
104 SAFE_FREE(value);
105 if (!*p_user_out) {
106 return false;
108 return true;
111 static void store_map_in_gencache(TALLOC_CTX *ctx, const char *from, const char *to)
113 char *key;
114 int cache_time = lp_username_map_cache_time();
116 if (cache_time == 0) {
117 return;
120 key = talloc_asprintf_strupper_m(ctx, "USERNAME_MAP/%s",
121 from);
122 if (key == NULL) {
123 return;
125 gencache_set(key, to, cache_time + time(NULL));
126 TALLOC_FREE(key);
129 /****************************************************************************
130 Check if a user is in a netgroup user list. If at first we don't succeed,
131 try lower case.
132 ****************************************************************************/
134 bool user_in_netgroup(TALLOC_CTX *ctx, const char *user, const char *ngname)
136 #ifdef HAVE_NETGROUP
137 static char *my_yp_domain = NULL;
138 char *lowercase_user = NULL;
140 if (my_yp_domain == NULL) {
141 yp_get_default_domain(&my_yp_domain);
144 if (my_yp_domain == NULL) {
145 DEBUG(5,("Unable to get default yp domain, "
146 "let's try without specifying it\n"));
149 DEBUG(5,("looking for user %s of domain %s in netgroup %s\n",
150 user, my_yp_domain?my_yp_domain:"(ANY)", ngname));
152 if (innetgr(ngname, NULL, user, my_yp_domain)) {
153 DEBUG(5,("user_in_netgroup: Found\n"));
154 return true;
158 * Ok, innetgr is case sensitive. Try once more with lowercase
159 * just in case. Attempt to fix #703. JRA.
161 lowercase_user = talloc_strdup(ctx, user);
162 if (!lowercase_user) {
163 return false;
165 strlower_m(lowercase_user);
167 if (strcmp(user,lowercase_user) == 0) {
168 /* user name was already lower case! */
169 return false;
172 DEBUG(5,("looking for user %s of domain %s in netgroup %s\n",
173 lowercase_user, my_yp_domain?my_yp_domain:"(ANY)", ngname));
175 if (innetgr(ngname, NULL, lowercase_user, my_yp_domain)) {
176 DEBUG(5,("user_in_netgroup: Found\n"));
177 return true;
179 #endif /* HAVE_NETGROUP */
180 return false;
183 /****************************************************************************
184 Check if a user is in a user list - can check combinations of UNIX
185 and netgroup lists.
186 ****************************************************************************/
188 bool user_in_list(TALLOC_CTX *ctx, const char *user,const char **list)
190 if (!list || !*list)
191 return False;
193 DEBUG(10,("user_in_list: checking user %s in list\n", user));
195 while (*list) {
197 DEBUG(10,("user_in_list: checking user |%s| against |%s|\n",
198 user, *list));
201 * Check raw username.
203 if (strequal(user, *list))
204 return(True);
207 * Now check to see if any combination
208 * of UNIX and netgroups has been specified.
211 if(**list == '@') {
213 * Old behaviour. Check netgroup list
214 * followed by UNIX list.
216 if(user_in_netgroup(ctx, user, *list +1))
217 return True;
218 if(user_in_group(user, *list +1))
219 return True;
220 } else if (**list == '+') {
222 if((*(*list +1)) == '&') {
224 * Search UNIX list followed by netgroup.
226 if(user_in_group(user, *list +2))
227 return True;
228 if(user_in_netgroup(ctx, user, *list +2))
229 return True;
231 } else {
234 * Just search UNIX list.
237 if(user_in_group(user, *list +1))
238 return True;
241 } else if (**list == '&') {
243 if(*(*list +1) == '+') {
245 * Search netgroup list followed by UNIX list.
247 if(user_in_netgroup(ctx, user, *list +2))
248 return True;
249 if(user_in_group(user, *list +2))
250 return True;
251 } else {
253 * Just search netgroup list.
255 if(user_in_netgroup(ctx, user, *list +1))
256 return True;
260 list++;
262 return(False);
265 bool map_username(TALLOC_CTX *ctx, const char *user_in, char **p_user_out)
267 XFILE *f;
268 char *mapfile = lp_username_map();
269 char *s;
270 char buf[512];
271 bool mapped_user = False;
272 char *cmd = lp_username_map_script();
274 *p_user_out = NULL;
276 if (!user_in)
277 return false;
279 /* Initially make a copy of the incoming name. */
280 *p_user_out = talloc_strdup(ctx, user_in);
281 if (!*p_user_out) {
282 return false;
285 if (strequal(user_in,get_last_to()))
286 return false;
288 if (strequal(user_in,get_last_from())) {
289 DEBUG(3,("Mapped user %s to %s\n",user_in,get_last_to()));
290 TALLOC_FREE(*p_user_out);
291 *p_user_out = talloc_strdup(ctx, get_last_to());
292 return true;
295 if (fetch_map_from_gencache(ctx, user_in, p_user_out)) {
296 return true;
299 /* first try the username map script */
301 if ( *cmd ) {
302 char **qlines;
303 char *command = NULL;
304 int numlines, ret, fd;
306 command = talloc_asprintf(ctx,
307 "%s \"%s\"",
308 cmd,
309 user_in);
310 if (!command) {
311 return false;
314 DEBUG(10,("Running [%s]\n", command));
315 ret = smbrun(command, &fd);
316 DEBUGADD(10,("returned [%d]\n", ret));
318 TALLOC_FREE(command);
320 if ( ret != 0 ) {
321 if (fd != -1)
322 close(fd);
323 return False;
326 numlines = 0;
327 qlines = fd_lines_load(fd, &numlines, 0, ctx);
328 DEBUGADD(10,("Lines returned = [%d]\n", numlines));
329 close(fd);
331 /* should be either no lines or a single line with the mapped username */
333 if (numlines && qlines) {
334 DEBUG(3,("Mapped user %s to %s\n", user_in, qlines[0] ));
335 set_last_from_to(user_in, qlines[0]);
336 store_map_in_gencache(ctx, user_in, qlines[0]);
337 TALLOC_FREE(*p_user_out);
338 *p_user_out = talloc_strdup(ctx, qlines[0]);
339 if (!*p_user_out) {
340 return false;
344 TALLOC_FREE(qlines);
346 return numlines != 0;
349 /* ok. let's try the mapfile */
350 if (!*mapfile)
351 return False;
353 f = x_fopen(mapfile,O_RDONLY, 0);
354 if (!f) {
355 DEBUG(0,("can't open username map %s. Error %s\n",mapfile, strerror(errno) ));
356 return False;
359 DEBUG(4,("Scanning username map %s\n",mapfile));
361 while((s=fgets_slash(buf,sizeof(buf),f))!=NULL) {
362 char *unixname = s;
363 char *dosname = strchr_m(unixname,'=');
364 char **dosuserlist;
365 bool return_if_mapped = False;
367 if (!dosname)
368 continue;
370 *dosname++ = 0;
372 unixname = skip_space(unixname);
374 if ('!' == *unixname) {
375 return_if_mapped = True;
376 unixname = skip_space(unixname+1);
379 if (!*unixname || strchr_m("#;",*unixname))
380 continue;
383 int l = strlen(unixname);
384 while (l && isspace((int)unixname[l-1])) {
385 unixname[l-1] = 0;
386 l--;
390 /* skip lines like 'user = ' */
392 dosuserlist = str_list_make_v3(ctx, dosname, NULL);
393 if (!dosuserlist) {
394 DEBUG(0,("Bad username map entry. Unable to build user list. Ignoring.\n"));
395 continue;
398 if (strchr_m(dosname,'*') ||
399 user_in_list(ctx, user_in, (const char **)dosuserlist)) {
400 DEBUG(3,("Mapped user %s to %s\n",user_in,unixname));
401 mapped_user = True;
403 set_last_from_to(user_in, unixname);
404 store_map_in_gencache(ctx, user_in, unixname);
405 TALLOC_FREE(*p_user_out);
406 *p_user_out = talloc_strdup(ctx, unixname);
407 if (!*p_user_out) {
408 TALLOC_FREE(dosuserlist);
409 x_fclose(f);
410 return false;
413 if ( return_if_mapped ) {
414 TALLOC_FREE(dosuserlist);
415 x_fclose(f);
416 return True;
420 TALLOC_FREE(dosuserlist);
423 x_fclose(f);
426 * Setup the last_from and last_to as an optimization so
427 * that we don't scan the file again for the same user.
430 set_last_from_to(user_in, user_in);
431 store_map_in_gencache(ctx, user_in, user_in);
433 return mapped_user;