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