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