Fix segfault, and add a comment.
[nivanova/samba-autobuild/.git] / source3 / 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
146                 if ((cli->sec_mode & 2) == 0) {
147                         /* We can't work with unencrypted password servers
148                            unless 'encrypt passwords = no' */
149                         DEBUG(5,("make_auth_info_server: Server is unencrypted, no challenge available..\n"));
150                         
151                         /* However, it is still a perfectly fine connection
152                            to pass that unencrypted password over */
153                         *my_private_data = (void *)cli;
154                         return data_blob(NULL, 0);
155                         
156                 } else if (cli->secblob.length < 8) {
157                         /* We can't do much if we don't get a full challenge */
158                         DEBUG(2,("make_auth_info_server: Didn't receive a full challenge from server\n"));
159                         cli_shutdown(cli);
160                         return data_blob(NULL, 0);
161                 }
162
163                 *my_private_data = (void *)cli;
164                 
165                 return data_blob(cli->secblob.data,8);
166         } else {
167                 return data_blob(NULL, 0);
168         }
169 }
170
171
172 /****************************************************************************
173  Check for a valid username and password in security=server mode.
174   - Validate a password with the password server.
175 ****************************************************************************/
176
177 static NTSTATUS check_smbserver_security(void *my_private_data, 
178                                   const auth_usersupplied_info *user_info, 
179                                   const auth_authsupplied_info *auth_info,
180                                   auth_serversupplied_info **server_info)
181 {
182         struct cli_state *cli;
183         static unsigned char badpass[24];
184         static fstring baduser; 
185         static BOOL tested_password_server = False;
186         static BOOL bad_password_server = False;
187         NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
188         BOOL locally_made_cli = False;
189
190         /* 
191          * Check that the requested domain is not our own machine name.
192          * If it is, we should never check the PDC here, we use our own local
193          * password file.
194          */
195
196         if(is_netbios_alias_or_name(user_info->domain.str)) {
197                 DEBUG(3,("check_smbserver_security: Requested domain was for this machine.\n"));
198                 return NT_STATUS_LOGON_FAILURE;
199         }
200
201         cli = my_private_data;
202         
203         if (cli) {
204         } else {
205                 cli = server_cryptkey();
206                 locally_made_cli = True;
207         }
208
209         if (!cli || !cli->initialised) {
210                 DEBUG(1,("password server is not connected (cli not initilised)\n"));
211                 return NT_STATUS_LOGON_FAILURE;
212         }  
213         
214         if ((cli->sec_mode & 2) == 0) {
215                 if (user_info->encrypted) {
216                         DEBUG(1,("password server %s is plaintext, but we are encrypted. This just can't work :-(\n", cli->desthost));
217                         return NT_STATUS_LOGON_FAILURE;         
218                 }
219         } else {
220                 if (memcmp(cli->secblob.data, auth_info->challenge.data, 8) != 0) {
221                         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));
222                         return NT_STATUS_LOGON_FAILURE;         
223                 }
224         }
225
226         if(badpass[0] == 0)
227                 memset(badpass, 0x1f, sizeof(badpass));
228
229         if((user_info->nt_resp.length == sizeof(badpass)) && 
230            !memcmp(badpass, user_info->nt_resp.data, sizeof(badpass))) {
231                 /* 
232                  * Very unlikely, our random bad password is the same as the users
233                  * password.
234                  */
235                 memset(badpass, badpass[0]+1, sizeof(badpass));
236         }
237
238         if(baduser[0] == 0) {
239                 fstrcpy(baduser, INVALID_USER_PREFIX);
240                 fstrcat(baduser, global_myname);
241         }
242
243         /*
244          * Attempt a session setup with a totally incorrect password.
245          * If this succeeds with the guest bit *NOT* set then the password
246          * server is broken and is not correctly setting the guest bit. We
247          * need to detect this as some versions of NT4.x are broken. JRA.
248          */
249
250         /* I sure as hell hope that there arn't servers out there that take 
251          * NTLMv2 and have this bug, as we don't test for that... 
252          *  - abartlet@samba.org
253          */
254
255         if ((!tested_password_server) && (lp_paranoid_server_security())) {
256                 if (cli_session_setup(cli, baduser, (char *)badpass, sizeof(badpass), 
257                                         (char *)badpass, sizeof(badpass), user_info->domain.str)) {
258
259                         /*
260                          * We connected to the password server so we
261                          * can say we've tested it.
262                          */
263                         tested_password_server = True;
264
265                         if ((SVAL(cli->inbuf,smb_vwv2) & 1) == 0) {
266                                 DEBUG(0,("server_validate: password server %s allows users as non-guest \
267 with a bad password.\n", cli->desthost));
268                                 DEBUG(0,("server_validate: This is broken (and insecure) behaviour. Please do not \
269 use this machine as the password server.\n"));
270                                 cli_ulogoff(cli);
271
272                                 /*
273                                  * Password server has the bug.
274                                  */
275                                 bad_password_server = True;
276                                 return NT_STATUS_LOGON_FAILURE;
277                         }
278                         cli_ulogoff(cli);
279                 }
280         } else {
281
282                 /*
283                  * We have already tested the password server.
284                  * Fail immediately if it has the bug.
285                  */
286
287                 if(bad_password_server) {
288                         DEBUG(0,("server_validate: [1] password server %s allows users as non-guest \
289 with a bad password.\n", cli->desthost));
290                         DEBUG(0,("server_validate: [1] This is broken (and insecure) behaviour. Please do not \
291 use this machine as the password server.\n"));
292                         return NT_STATUS_LOGON_FAILURE;
293                 }
294         }
295
296         /*
297          * Now we know the password server will correctly set the guest bit, or is
298          * not guest enabled, we can try with the real password.
299          */
300
301         if (!user_info->encrypted) {
302                 /* Plaintext available */
303                 if (!cli_session_setup(cli, user_info->smb_name.str, 
304                                        (char *)user_info->plaintext_password.data, 
305                                        user_info->plaintext_password.length, 
306                                        NULL, 0,
307                                        user_info->domain.str)) {
308                         DEBUG(1,("password server %s rejected the password\n", cli->desthost));
309                         /* Make this cli_nt_error() when the conversion is in */
310                         nt_status = cli_nt_error(cli);
311                 } else {
312                         nt_status = NT_STATUS_OK;
313                 }
314         } else {
315                 if (!cli_session_setup(cli, user_info->smb_name.str, 
316                                        (char *)user_info->lm_resp.data, 
317                                        user_info->lm_resp.length, 
318                                        (char *)user_info->nt_resp.data, 
319                                        user_info->nt_resp.length, 
320                                        user_info->domain.str)) {
321                         DEBUG(1,("password server %s rejected the password\n", cli->desthost));
322                         /* Make this cli_nt_error() when the conversion is in */
323                         nt_status = cli_nt_error(cli);
324                 } else {
325                         nt_status = NT_STATUS_OK;
326                 }
327         }
328
329         /* if logged in as guest then reject */
330         if ((SVAL(cli->inbuf,smb_vwv2) & 1) != 0) {
331                 DEBUG(1,("password server %s gave us guest only\n", cli->desthost));
332                 nt_status = NT_STATUS_LOGON_FAILURE;
333         }
334
335         cli_ulogoff(cli);
336
337         if NT_STATUS_IS_OK(nt_status) {
338                 struct passwd *pass = Get_Pwnam(user_info->internal_username.str);
339                 if (pass) {
340                         if (!make_server_info_pw(server_info, pass)) { 
341                                 nt_status = NT_STATUS_NO_MEMORY;
342                         }
343                 } else {
344                         nt_status = NT_STATUS_NO_SUCH_USER;
345                 }
346         }
347
348         if (locally_made_cli) {
349                 cli_shutdown(cli);
350         }
351
352         return(nt_status);
353 }
354
355 BOOL auth_init_smbserver(auth_methods **auth_method) 
356 {
357         if (!make_auth_methods(auth_method)) {
358                 return False;
359         }
360         (*auth_method)->auth = check_smbserver_security;
361         (*auth_method)->get_chal = auth_get_challenge_server;
362         (*auth_method)->send_keepalive = send_server_keepalive;
363         (*auth_method)->free_private_data = free_server_private_data;
364         return True;
365 }