s3:smbd user_in_list() doesn't need sconn anymore
[Samba/ekacnet.git] / source3 / smbd / map_username.c
blob4d2a9db307be3a4476c8571a5450002a2a976051
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"
23 #include "smbd/globals.h"
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 ********************************************************************/
36 static const char *get_last_from(void)
38 if (!last_from) {
39 return "";
41 return last_from;
44 static const char *get_last_to(void)
46 if (!last_to) {
47 return "";
49 return last_to;
52 static bool set_last_from_to(const char *from, const char *to)
54 char *orig_from = last_from;
55 char *orig_to = last_to;
57 last_from = SMB_STRDUP(from);
58 last_to = SMB_STRDUP(to);
60 SAFE_FREE(orig_from);
61 SAFE_FREE(orig_to);
63 if (!last_from || !last_to) {
64 SAFE_FREE(last_from);
65 SAFE_FREE(last_to);
66 return false;
68 return true;
71 static char *skip_space(char *s)
73 while (isspace((int)(*s))) {
74 s += 1;
76 return s;
79 static bool fetch_map_from_gencache(fstring user)
81 char *key, *value;
82 bool found;
84 if (lp_username_map_cache_time() == 0) {
85 return false;
88 key = talloc_asprintf_strupper_m(talloc_tos(), "USERNAME_MAP/%s",
89 user);
90 if (key == NULL) {
91 return false;
93 found = gencache_get(key, &value, NULL);
94 TALLOC_FREE(key);
95 if (!found) {
96 return false;
98 fstrcpy(user, value);
99 SAFE_FREE(value);
100 return true;
103 static void store_map_in_gencache(const char *from, const char *to)
105 char *key;
106 int cache_time = lp_username_map_cache_time();
108 if (cache_time == 0) {
109 return;
112 key = talloc_asprintf_strupper_m(talloc_tos(), "USERNAME_MAP/%s",
113 from);
114 if (key == NULL) {
115 return;
117 gencache_set(key, to, cache_time + time(NULL));
118 TALLOC_FREE(key);
121 bool map_username(struct smbd_server_connection *sconn, fstring user)
123 XFILE *f;
124 char *mapfile = lp_username_map();
125 char *s;
126 char buf[512];
127 bool mapped_user = False;
128 char *cmd = lp_username_map_script();
130 if (!*user)
131 return false;
133 if (strequal(user,get_last_to()))
134 return false;
136 if (strequal(user,get_last_from())) {
137 DEBUG(3,("Mapped user %s to %s\n",user,get_last_to()));
138 fstrcpy(user,get_last_to());
139 return true;
142 if (fetch_map_from_gencache(user)) {
143 return true;
146 /* first try the username map script */
148 if ( *cmd ) {
149 char **qlines;
150 char *command = NULL;
151 int numlines, ret, fd;
153 command = talloc_asprintf(talloc_tos(),
154 "%s \"%s\"",
155 cmd,
156 user);
157 if (!command) {
158 return false;
161 DEBUG(10,("Running [%s]\n", command));
162 ret = smbrun(command, &fd);
163 DEBUGADD(10,("returned [%d]\n", ret));
165 TALLOC_FREE(command);
167 if ( ret != 0 ) {
168 if (fd != -1)
169 close(fd);
170 return False;
173 numlines = 0;
174 qlines = fd_lines_load(fd, &numlines, 0, talloc_tos());
175 DEBUGADD(10,("Lines returned = [%d]\n", numlines));
176 close(fd);
178 /* should be either no lines or a single line with the mapped username */
180 if (numlines && qlines) {
181 DEBUG(3,("Mapped user %s to %s\n", user, qlines[0] ));
182 set_last_from_to(user, qlines[0]);
183 store_map_in_gencache(user, qlines[0]);
184 fstrcpy( user, qlines[0] );
187 TALLOC_FREE(qlines);
189 return numlines != 0;
192 /* ok. let's try the mapfile */
193 if (!*mapfile)
194 return False;
196 f = x_fopen(mapfile,O_RDONLY, 0);
197 if (!f) {
198 DEBUG(0,("can't open username map %s. Error %s\n",mapfile, strerror(errno) ));
199 return False;
202 DEBUG(4,("Scanning username map %s\n",mapfile));
204 while((s=fgets_slash(buf,sizeof(buf),f))!=NULL) {
205 char *unixname = s;
206 char *dosname = strchr_m(unixname,'=');
207 char **dosuserlist;
208 bool return_if_mapped = False;
210 if (!dosname)
211 continue;
213 *dosname++ = 0;
215 unixname = skip_space(unixname);
217 if ('!' == *unixname) {
218 return_if_mapped = True;
219 unixname = skip_space(unixname+1);
222 if (!*unixname || strchr_m("#;",*unixname))
223 continue;
226 int l = strlen(unixname);
227 while (l && isspace((int)unixname[l-1])) {
228 unixname[l-1] = 0;
229 l--;
233 /* skip lines like 'user = ' */
235 dosuserlist = str_list_make_v3(talloc_tos(), dosname, NULL);
236 if (!dosuserlist) {
237 DEBUG(0,("Bad username map entry. Unable to build user list. Ignoring.\n"));
238 continue;
241 if (strchr_m(dosname,'*') ||
242 user_in_list(user, (const char **)dosuserlist)) {
243 DEBUG(3,("Mapped user %s to %s\n",user,unixname));
244 mapped_user = True;
246 set_last_from_to(user, unixname);
247 store_map_in_gencache(user, unixname);
248 fstrcpy( user, unixname );
250 if ( return_if_mapped ) {
251 TALLOC_FREE(dosuserlist);
252 x_fclose(f);
253 return True;
257 TALLOC_FREE(dosuserlist);
260 x_fclose(f);
263 * Setup the last_from and last_to as an optimization so
264 * that we don't scan the file again for the same user.
267 set_last_from_to(user, user);
268 store_map_in_gencache(user, user);
270 return mapped_user;