debug classized
[ira/wip.git] / source3 / auth / auth_server.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Authenticate to a remote server
4    Copyright (C) Andrew Tridgell 1992-1998
5    Copyright (C) Andrew Bartlett 2001
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DGBC_AUTH
26
27 extern pstring global_myname;
28 extern userdom_struct current_user_info;
29
30 /****************************************************************************
31  Support for server level security.
32 ****************************************************************************/
33
34 static struct cli_state *server_cryptkey(TALLOC_CTX *mem_ctx)
35 {
36         struct cli_state *cli = NULL;
37         fstring desthost;
38         struct in_addr dest_ip;
39         char *p, *pserver;
40         BOOL connected_ok = False;
41
42         if (!(cli = cli_initialise(cli)))
43                 return NULL;
44
45         /* security = server just can't function with spnego */
46         cli->use_spnego = False;
47
48         pserver = talloc_strdup(mem_ctx, lp_passwordserver());
49         p = pserver;
50
51         while(next_token( &p, desthost, LIST_SEP, sizeof(desthost))) {
52                 standard_sub_basic(current_user_info.smb_name, desthost);
53                 strupper(desthost);
54
55                 if(!resolve_name( desthost, &dest_ip, 0x20)) {
56                         DEBUG(1,("server_cryptkey: Can't resolve address for %s\n",desthost));
57                         continue;
58                 }
59
60                 if (ismyip(dest_ip)) {
61                         DEBUG(1,("Password server loop - disabling password server %s\n",desthost));
62                         continue;
63                 }
64
65                 if (cli_connect(cli, desthost, &dest_ip)) {
66                         DEBUG(3,("connected to password server %s\n",desthost));
67                         connected_ok = True;
68                         break;
69                 }
70         }
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(const struct auth_context *auth_context,
140                                            void **my_private_data, 
141                                            TALLOC_CTX *mem_ctx)
142 {
143         struct cli_state *cli = server_cryptkey(mem_ctx);
144         
145         if (cli) {
146                 DEBUG(3,("using password server validation\n"));
147
148                 if ((cli->sec_mode & 2) == 0) {
149                         /* We can't work with unencrypted password servers
150                            unless 'encrypt passwords = no' */
151                         DEBUG(5,("make_auth_info_server: Server is unencrypted, no challenge available..\n"));
152                         
153                         /* However, it is still a perfectly fine connection
154                            to pass that unencrypted password over */
155                         *my_private_data = (void *)cli;
156                         return data_blob(NULL, 0);
157                         
158                 } else if (cli->secblob.length < 8) {
159                         /* We can't do much if we don't get a full challenge */
160                         DEBUG(2,("make_auth_info_server: Didn't receive a full challenge from server\n"));
161                         cli_shutdown(cli);
162                         return data_blob(NULL, 0);
163                 }
164
165                 *my_private_data = (void *)cli;
166
167                 /* The return must be allocated on the caller's mem_ctx, as our own will be
168                    destoyed just after the call. */
169                 return data_blob_talloc(auth_context->mem_ctx, cli->secblob.data,8);
170         } else {
171                 return data_blob(NULL, 0);
172         }
173 }
174
175
176 /****************************************************************************
177  Check for a valid username and password in security=server mode.
178   - Validate a password with the password server.
179 ****************************************************************************/
180
181 static NTSTATUS check_smbserver_security(const struct auth_context *auth_context,
182                                          void *my_private_data, 
183                                          TALLOC_CTX *mem_ctx,
184                                          const auth_usersupplied_info *user_info, 
185                                          auth_serversupplied_info **server_info)
186 {
187         struct cli_state *cli;
188         static unsigned char badpass[24];
189         static fstring baduser; 
190         static BOOL tested_password_server = False;
191         static BOOL bad_password_server = False;
192         NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
193         BOOL locally_made_cli = False;
194
195         /* 
196          * Check that the requested domain is not our own machine name.
197          * If it is, we should never check the PDC here, we use our own local
198          * password file.
199          */
200
201         if(is_netbios_alias_or_name(user_info->domain.str)) {
202                 DEBUG(3,("check_smbserver_security: Requested domain was for this machine.\n"));
203                 return NT_STATUS_LOGON_FAILURE;
204         }
205
206         cli = my_private_data;
207         
208         if (cli) {
209         } else {
210                 cli = server_cryptkey(mem_ctx);
211                 locally_made_cli = True;
212         }
213
214         if (!cli || !cli->initialised) {
215                 DEBUG(1,("password server is not connected (cli not initilised)\n"));
216                 return NT_STATUS_LOGON_FAILURE;
217         }  
218         
219         if ((cli->sec_mode & 2) == 0) {
220                 if (user_info->encrypted) {
221                         DEBUG(1,("password server %s is plaintext, but we are encrypted. This just can't work :-(\n", cli->desthost));
222                         return NT_STATUS_LOGON_FAILURE;         
223                 }
224         } else {
225                 if (memcmp(cli->secblob.data, auth_context->challenge.data, 8) != 0) {
226                         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));
227                         return NT_STATUS_LOGON_FAILURE;         
228                 }
229         }
230
231         if(badpass[0] == 0)
232                 memset(badpass, 0x1f, sizeof(badpass));
233
234         if((user_info->nt_resp.length == sizeof(badpass)) && 
235            !memcmp(badpass, user_info->nt_resp.data, sizeof(badpass))) {
236                 /* 
237                  * Very unlikely, our random bad password is the same as the users
238                  * password.
239                  */
240                 memset(badpass, badpass[0]+1, sizeof(badpass));
241         }
242
243         if(baduser[0] == 0) {
244                 fstrcpy(baduser, INVALID_USER_PREFIX);
245                 fstrcat(baduser, global_myname);
246         }
247
248         /*
249          * Attempt a session setup with a totally incorrect password.
250          * If this succeeds with the guest bit *NOT* set then the password
251          * server is broken and is not correctly setting the guest bit. We
252          * need to detect this as some versions of NT4.x are broken. JRA.
253          */
254
255         /* I sure as hell hope that there arn't servers out there that take 
256          * NTLMv2 and have this bug, as we don't test for that... 
257          *  - abartlet@samba.org
258          */
259
260         if ((!tested_password_server) && (lp_paranoid_server_security())) {
261                 if (cli_session_setup(cli, baduser, (char *)badpass, sizeof(badpass), 
262                                         (char *)badpass, sizeof(badpass), user_info->domain.str)) {
263
264                         /*
265                          * We connected to the password server so we
266                          * can say we've tested it.
267                          */
268                         tested_password_server = True;
269
270                         if ((SVAL(cli->inbuf,smb_vwv2) & 1) == 0) {
271                                 DEBUG(0,("server_validate: password server %s allows users as non-guest \
272 with a bad password.\n", cli->desthost));
273                                 DEBUG(0,("server_validate: This is broken (and insecure) behaviour. Please do not \
274 use this machine as the password server.\n"));
275                                 cli_ulogoff(cli);
276
277                                 /*
278                                  * Password server has the bug.
279                                  */
280                                 bad_password_server = True;
281                                 return NT_STATUS_LOGON_FAILURE;
282                         }
283                         cli_ulogoff(cli);
284                 }
285         } else {
286
287                 /*
288                  * We have already tested the password server.
289                  * Fail immediately if it has the bug.
290                  */
291
292                 if(bad_password_server) {
293                         DEBUG(0,("server_validate: [1] password server %s allows users as non-guest \
294 with a bad password.\n", cli->desthost));
295                         DEBUG(0,("server_validate: [1] This is broken (and insecure) behaviour. Please do not \
296 use this machine as the password server.\n"));
297                         return NT_STATUS_LOGON_FAILURE;
298                 }
299         }
300
301         /*
302          * Now we know the password server will correctly set the guest bit, or is
303          * not guest enabled, we can try with the real password.
304          */
305
306         if (!user_info->encrypted) {
307                 /* Plaintext available */
308                 if (!cli_session_setup(cli, user_info->smb_name.str, 
309                                        (char *)user_info->plaintext_password.data, 
310                                        user_info->plaintext_password.length, 
311                                        NULL, 0,
312                                        user_info->domain.str)) {
313                         DEBUG(1,("password server %s rejected the password\n", cli->desthost));
314                         /* Make this cli_nt_error() when the conversion is in */
315                         nt_status = cli_nt_error(cli);
316                 } else {
317                         nt_status = NT_STATUS_OK;
318                 }
319         } else {
320                 if (!cli_session_setup(cli, user_info->smb_name.str, 
321                                        (char *)user_info->lm_resp.data, 
322                                        user_info->lm_resp.length, 
323                                        (char *)user_info->nt_resp.data, 
324                                        user_info->nt_resp.length, 
325                                        user_info->domain.str)) {
326                         DEBUG(1,("password server %s rejected the password\n", cli->desthost));
327                         /* Make this cli_nt_error() when the conversion is in */
328                         nt_status = cli_nt_error(cli);
329                 } else {
330                         nt_status = NT_STATUS_OK;
331                 }
332         }
333
334         /* if logged in as guest then reject */
335         if ((SVAL(cli->inbuf,smb_vwv2) & 1) != 0) {
336                 DEBUG(1,("password server %s gave us guest only\n", cli->desthost));
337                 nt_status = NT_STATUS_LOGON_FAILURE;
338         }
339
340         cli_ulogoff(cli);
341
342         if NT_STATUS_IS_OK(nt_status) {
343                 struct passwd *pass = Get_Pwnam(user_info->internal_username.str);
344                 if (pass) {
345                         if (!make_server_info_pw(server_info, pass)) { 
346                                 nt_status = NT_STATUS_NO_MEMORY;
347                         }
348                 } else {
349                         nt_status = NT_STATUS_NO_SUCH_USER;
350                 }
351         }
352
353         if (locally_made_cli) {
354                 cli_shutdown(cli);
355         }
356
357         return(nt_status);
358 }
359
360 BOOL auth_init_smbserver(struct auth_context *auth_context, auth_methods **auth_method) 
361 {
362         if (!make_auth_methods(auth_context, auth_method)) {
363                 return False;
364         }
365         (*auth_method)->auth = check_smbserver_security;
366         (*auth_method)->get_chal = auth_get_challenge_server;
367         (*auth_method)->send_keepalive = send_server_keepalive;
368         (*auth_method)->free_private_data = free_server_private_data;
369         return True;
370 }