again an intrusive patch:
[samba.git] / source / auth / auth_server.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    Authenticate to a remote server
5    Copyright (C) Andrew Tridgell 1992-1998
6    Copyright (C) Andrew Bartlett 2001
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 extern pstring global_myname;
26 extern userdom_struct current_user_info;
27
28 /****************************************************************************
29  Support for server level security.
30 ****************************************************************************/
31
32 static struct cli_state *server_cryptkey(void)
33 {
34         struct cli_state *cli = NULL;
35         fstring desthost;
36         struct in_addr dest_ip;
37         char *p, *pserver;
38         BOOL connected_ok = False;
39
40         if (!(cli = cli_initialise(cli)))
41                 return NULL;
42
43         /* security = server just can't function with spnego */
44         cli->use_spnego = False;
45
46         pserver = strdup(lp_passwordserver());
47         p = pserver;
48
49         while(next_token( &p, desthost, LIST_SEP, sizeof(desthost))) {
50                 standard_sub_basic(current_user_info.smb_name, desthost);
51                 strupper(desthost);
52
53                 if(!resolve_name( desthost, &dest_ip, 0x20)) {
54                         DEBUG(1,("server_cryptkey: Can't resolve address for %s\n",desthost));
55                         continue;
56                 }
57
58                 if (ismyip(dest_ip)) {
59                         DEBUG(1,("Password server loop - disabling password server %s\n",desthost));
60                         continue;
61                 }
62
63                 if (cli_connect(cli, desthost, &dest_ip)) {
64                         DEBUG(3,("connected to password server %s\n",desthost));
65                         connected_ok = True;
66                         break;
67                 }
68         }
69
70         SAFE_FREE(pserver);
71
72         if (!connected_ok) {
73                 DEBUG(0,("password server not available\n"));
74                 cli_shutdown(cli);
75                 return NULL;
76         }
77
78         if (!attempt_netbios_session_request(cli, global_myname, desthost, &dest_ip))
79                 return NULL;
80         
81         if (strequal(desthost,myhostname())) {
82                 exit_server("Password server loop!");
83         }
84         
85         DEBUG(3,("got session\n"));
86
87         if (!cli_negprot(cli)) {
88                 DEBUG(1,("%s rejected the negprot\n",desthost));
89                 cli_shutdown(cli);
90                 return NULL;
91         }
92
93         if (cli->protocol < PROTOCOL_LANMAN2 ||
94             !(cli->sec_mode & 1)) {
95                 DEBUG(1,("%s isn't in user level security mode\n",desthost));
96                 cli_shutdown(cli);
97                 return NULL;
98         }
99
100         DEBUG(3,("password server OK\n"));
101
102         return cli;
103 }
104
105 /****************************************************************************
106  Clean up our allocated cli.
107 ****************************************************************************/
108
109 static void free_server_private_data(void **private_data_pointer) 
110 {
111         struct cli_state **cli = (struct cli_state **)private_data_pointer;
112         if (*cli && (*cli)->initialised) {
113                 cli_shutdown(*cli);
114         }
115 }
116
117 /****************************************************************************
118  Send a 'keepalive' packet down the cli pipe.
119 ****************************************************************************/
120
121 static void send_server_keepalive(void **private_data_pointer) 
122 {
123         struct cli_state **cli = (struct cli_state **)private_data_pointer;
124         
125         /* also send a keepalive to the password server if its still
126            connected */
127         if (cli && *cli && (*cli)->initialised) {
128                 if (!send_keepalive((*cli)->fd)) {
129                         DEBUG( 2, ( "password server keepalive failed.\n"));
130                         cli_shutdown(*cli);
131                 }
132         }
133 }
134
135 /****************************************************************************
136  Get the challenge out of a password server.
137 ****************************************************************************/
138
139 static DATA_BLOB auth_get_challenge_server(void **my_private_data, const struct authsupplied_info *auth_info) 
140 {
141         struct cli_state *cli = server_cryptkey();
142         
143         if (cli) {
144                 DEBUG(3,("using password server validation\n"));
145                 if ((cli->sec_mode & 2) == 0) {
146                         /* We can't work with unencrypted password servers
147                            unless 'encrypt passwords = no' */
148                         DEBUG(5,("make_auth_info_server: Server is unencrypted, no challenge available..\n"));
149
150                         *my_private_data = (void *)cli;
151                         return data_blob(NULL, 0);
152                         
153                 } else if (cli->secblob.length < 8) {
154                         /* We can't do much if we don't get a full challenge */
155                         DEBUG(2,("make_auth_info_server: Didn't receive a full challenge from server\n"));
156                         cli_shutdown(cli);
157                         return data_blob(NULL, 0);
158                 }
159
160                 *my_private_data = (void *)cli;
161                 
162                 return data_blob(cli->secblob.data,8);
163         } else {
164                 return data_blob(NULL, 0);
165         }
166 }
167
168
169 /****************************************************************************
170  Check for a valid username and password in security=server mode.
171   - Validate a password with the password server.
172 ****************************************************************************/
173
174 static NTSTATUS check_smbserver_security(void *my_private_data, 
175                                   const auth_usersupplied_info *user_info, 
176                                   const auth_authsupplied_info *auth_info,
177                                   auth_serversupplied_info **server_info)
178 {
179         struct cli_state *cli;
180         static unsigned char badpass[24];
181         static fstring baduser; 
182         static BOOL tested_password_server = False;
183         static BOOL bad_password_server = False;
184         NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
185         BOOL locally_made_cli = False;
186
187         /* 
188          * Check that the requested domain is not our own machine name.
189          * If it is, we should never check the PDC here, we use our own local
190          * password file.
191          */
192
193         if(is_netbios_alias_or_name(user_info->domain.str)) {
194                 DEBUG(3,("check_smbserver_security: Requested domain was for this machine.\n"));
195                 return NT_STATUS_LOGON_FAILURE;
196         }
197
198         cli = my_private_data;
199         
200         if (cli) {
201         } else {
202                 cli = server_cryptkey();
203                 locally_made_cli = True;
204         }
205
206         if (!cli || !cli->initialised) {
207                 DEBUG(1,("password server %s is not connected\n", cli->desthost));
208                 return NT_STATUS_LOGON_FAILURE;
209         }  
210         
211         if ((cli->sec_mode & 2) == 0) {
212                 if (user_info->encrypted) {
213                         DEBUG(1,("password server %s is plaintext, but we are encrypted. This just can't work :-(\n", cli->desthost));
214                         return NT_STATUS_LOGON_FAILURE;         
215                 }
216         } else {
217                 if (memcmp(cli->secblob.data, auth_info->challenge.data, 8) != 0) {
218                         DEBUG(1,("the challenge that the password server (%s) supplied us is not the one we gave our client. This just can't work :-(\n", cli->desthost));
219                         return NT_STATUS_LOGON_FAILURE;         
220                 }
221         }
222
223         if(badpass[0] == 0)
224                 memset(badpass, 0x1f, sizeof(badpass));
225
226         if((user_info->nt_resp.length == sizeof(badpass)) && 
227            !memcmp(badpass, user_info->nt_resp.data, sizeof(badpass))) {
228                 /* 
229                  * Very unlikely, our random bad password is the same as the users
230                  * password.
231                  */
232                 memset(badpass, badpass[0]+1, sizeof(badpass));
233         }
234
235         if(baduser[0] == 0) {
236                 fstrcpy(baduser, INVALID_USER_PREFIX);
237                 fstrcat(baduser, global_myname);
238         }
239
240         /*
241          * Attempt a session setup with a totally incorrect password.
242          * If this succeeds with the guest bit *NOT* set then the password
243          * server is broken and is not correctly setting the guest bit. We
244          * need to detect this as some versions of NT4.x are broken. JRA.
245          */
246
247         /* I sure as hell hope that there arn't servers out there that take 
248          * NTLMv2 and have this bug, as we don't test for that... 
249          *  - abartlet@samba.org
250          */
251
252         if ((!tested_password_server) && (lp_paranoid_server_security())) {
253                 if (cli_session_setup(cli, baduser, (char *)badpass, sizeof(badpass), 
254                                         (char *)badpass, sizeof(badpass), user_info->domain.str)) {
255
256                         /*
257                          * We connected to the password server so we
258                          * can say we've tested it.
259                          */
260                         tested_password_server = True;
261
262                         if ((SVAL(cli->inbuf,smb_vwv2) & 1) == 0) {
263                                 DEBUG(0,("server_validate: password server %s allows users as non-guest \
264 with a bad password.\n", cli->desthost));
265                                 DEBUG(0,("server_validate: This is broken (and insecure) behaviour. Please do not \
266 use this machine as the password server.\n"));
267                                 cli_ulogoff(cli);
268
269                                 /*
270                                  * Password server has the bug.
271                                  */
272                                 bad_password_server = True;
273                                 return NT_STATUS_LOGON_FAILURE;
274                         }
275                         cli_ulogoff(cli);
276                 }
277         } else {
278
279                 /*
280                  * We have already tested the password server.
281                  * Fail immediately if it has the bug.
282                  */
283
284                 if(bad_password_server) {
285                         DEBUG(0,("server_validate: [1] password server %s allows users as non-guest \
286 with a bad password.\n", cli->desthost));
287                         DEBUG(0,("server_validate: [1] This is broken (and insecure) behaviour. Please do not \
288 use this machine as the password server.\n"));
289                         return NT_STATUS_LOGON_FAILURE;
290                 }
291         }
292
293         /*
294          * Now we know the password server will correctly set the guest bit, or is
295          * not guest enabled, we can try with the real password.
296          */
297
298         if (!user_info->encrypted) {
299                 /* Plaintext available */
300                 if (!cli_session_setup(cli, user_info->smb_name.str, 
301                                        (char *)user_info->plaintext_password.data, 
302                                        user_info->plaintext_password.length, 
303                                        NULL, 0,
304                                        user_info->domain.str)) {
305                         DEBUG(1,("password server %s rejected the password\n", cli->desthost));
306                         /* Make this cli_nt_error() when the conversion is in */
307                         nt_status = cli_nt_error(cli);
308                 } else {
309                         nt_status = NT_STATUS_OK;
310                 }
311         } else {
312                 if (!cli_session_setup(cli, user_info->smb_name.str, 
313                                        (char *)user_info->lm_resp.data, 
314                                        user_info->lm_resp.length, 
315                                        (char *)user_info->nt_resp.data, 
316                                        user_info->nt_resp.length, 
317                                        user_info->domain.str)) {
318                         DEBUG(1,("password server %s rejected the password\n", cli->desthost));
319                         /* Make this cli_nt_error() when the conversion is in */
320                         nt_status = cli_nt_error(cli);
321                 } else {
322                         nt_status = NT_STATUS_OK;
323                 }
324         }
325
326         /* if logged in as guest then reject */
327         if ((SVAL(cli->inbuf,smb_vwv2) & 1) != 0) {
328                 DEBUG(1,("password server %s gave us guest only\n", cli->desthost));
329                 nt_status = NT_STATUS_LOGON_FAILURE;
330         }
331
332         cli_ulogoff(cli);
333
334         if NT_STATUS_IS_OK(nt_status) {
335                 struct passwd *pass = Get_Pwnam(user_info->internal_username.str);
336                 if (pass) {
337                         if (!make_server_info_pw(server_info, pass)) { 
338                                 nt_status = NT_STATUS_NO_MEMORY;
339                         }
340                 } else {
341                         nt_status = NT_STATUS_NO_SUCH_USER;
342                 }
343         }
344
345         if (locally_made_cli) {
346                 cli_shutdown(cli);
347         }
348
349         return(nt_status);
350 }
351
352 BOOL auth_init_smbserver(auth_methods **auth_method) 
353 {
354         if (!make_auth_methods(auth_method)) {
355                 return False;
356         }
357         (*auth_method)->auth = check_smbserver_security;
358         (*auth_method)->get_chal = auth_get_challenge_server;
359         (*auth_method)->send_keepalive = send_server_keepalive;
360         (*auth_method)->free_private_data = free_server_private_data;
361         return True;
362 }