More pstring removal from smbd/*.c
[kai/samba-autobuild/.git] / source3 / smbd / map_username.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 was called.
32  Returns True if username was changed, false otherwise.
33 ********************************************************************/
34
35 bool map_username(fstring user)
36 {
37         static bool initialised=False;
38         static fstring last_from,last_to;
39         XFILE *f;
40         char *mapfile = lp_username_map();
41         char *s;
42         char buf[512];
43         bool mapped_user = False;
44         char *cmd = lp_username_map_script();
45
46         if (!*user)
47                 return false;
48
49         if (strequal(user,last_to))
50                 return false;
51
52         if (strequal(user,last_from)) {
53                 DEBUG(3,("Mapped user %s to %s\n",user,last_to));
54                 fstrcpy(user,last_to);
55                 return true;
56         }
57
58         /* first try the username map script */
59
60         if ( *cmd ) {
61                 char **qlines;
62                 char *command = NULL;
63                 int numlines, ret, fd;
64
65                 command = talloc_asprintf(talloc_tos(),
66                                         "%s \"%s\"",
67                                         cmd,
68                                         user);
69                 if (!command) {
70                         return false;
71                 }
72
73                 DEBUG(10,("Running [%s]\n", command));
74                 ret = smbrun(command, &fd);
75                 DEBUGADD(10,("returned [%d]\n", ret));
76
77                 if ( ret != 0 ) {
78                         if (fd != -1)
79                                 close(fd);
80                         return False;
81                 }
82
83                 numlines = 0;
84                 qlines = fd_lines_load(fd, &numlines,0);
85                 DEBUGADD(10,("Lines returned = [%d]\n", numlines));
86                 close(fd);
87
88                 /* should be either no lines or a single line with the mapped username */
89
90                 if (numlines && qlines) {
91                         DEBUG(3,("Mapped user %s to %s\n", user, qlines[0] ));
92                         fstrcpy( user, qlines[0] );
93                 }
94
95                 file_lines_free(qlines);
96
97                 return numlines != 0;
98         }
99
100         /* ok.  let's try the mapfile */
101         
102         if (!*mapfile)
103                 return False;
104
105         if (!initialised) {
106                 *last_from = *last_to = 0;
107                 initialised = True;
108         }
109   
110         f = x_fopen(mapfile,O_RDONLY, 0);
111         if (!f) {
112                 DEBUG(0,("can't open username map %s. Error %s\n",mapfile, strerror(errno) ));
113                 return False;
114         }
115
116         DEBUG(4,("Scanning username map %s\n",mapfile));
117
118         while((s=fgets_slash(buf,sizeof(buf),f))!=NULL) {
119                 char *unixname = s;
120                 char *dosname = strchr_m(unixname,'=');
121                 char **dosuserlist;
122                 bool return_if_mapped = False;
123
124                 if (!dosname)
125                         continue;
126
127                 *dosname++ = 0;
128
129                 while (isspace((int)*unixname))
130                         unixname++;
131
132                 if ('!' == *unixname) {
133                         return_if_mapped = True;
134                         unixname++;
135                         while (*unixname && isspace((int)*unixname))
136                                 unixname++;
137                 }
138     
139                 if (!*unixname || strchr_m("#;",*unixname))
140                         continue;
141
142                 {
143                         int l = strlen(unixname);
144                         while (l && isspace((int)unixname[l-1])) {
145                                 unixname[l-1] = 0;
146                                 l--;
147                         }
148                 }
149
150                 /* skip lines like 'user = ' */
151
152                 dosuserlist = str_list_make(dosname, NULL);
153                 if (!dosuserlist) {
154                         DEBUG(0,("Bad username map entry.  Unable to build user list.  Ignoring.\n"));
155                         continue;
156                 }
157
158                 if (strchr_m(dosname,'*') ||
159                     user_in_list(user, (const char **)dosuserlist)) {
160                         DEBUG(3,("Mapped user %s to %s\n",user,unixname));
161                         mapped_user = True;
162                         fstrcpy( last_from,user );
163                         fstrcpy( user, unixname );
164                         fstrcpy( last_to,user );
165                         if ( return_if_mapped ) {
166                                 str_list_free (&dosuserlist);
167                                 x_fclose(f);
168                                 return True;
169                         }
170                 }
171     
172                 str_list_free (&dosuserlist);
173         }
174
175         x_fclose(f);
176
177         /*
178          * Setup the last_from and last_to as an optimization so 
179          * that we don't scan the file again for the same user.
180          */
181         fstrcpy(last_from,user);
182         fstrcpy(last_to,user);
183
184         return mapped_user;
185 }