r23779: Change from v2 or later to v3 or later.
[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 3 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 DBGC_AUTH
26
27 extern userdom_struct current_user_info;
28
29 /****************************************************************************
30  Support for server level security.
31 ****************************************************************************/
32
33 static struct cli_state *server_cryptkey(TALLOC_CTX *mem_ctx)
34 {
35         struct cli_state *cli = NULL;
36         fstring desthost;
37         struct in_addr dest_ip;
38         const char *p;
39         char *pserver;
40         BOOL connected_ok = False;
41
42         if (!(cli = cli_initialise()))
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                 NTSTATUS status;
53
54                 standard_sub_basic(current_user_info.smb_name, current_user_info.domain,
55                                    desthost, sizeof(desthost));
56                 strupper_m(desthost);
57
58                 if(!resolve_name( desthost, &dest_ip, 0x20)) {
59                         DEBUG(1,("server_cryptkey: Can't resolve address for %s\n",desthost));
60                         continue;
61                 }
62
63                 if (ismyip(dest_ip)) {
64                         DEBUG(1,("Password server loop - disabling password server %s\n",desthost));
65                         continue;
66                 }
67
68                 /* we use a mutex to prevent two connections at once - when a 
69                    Win2k PDC get two connections where one hasn't completed a 
70                    session setup yet it will send a TCP reset to the first 
71                    connection (tridge) */
72
73                 if (!grab_server_mutex(desthost)) {
74                         return NULL;
75                 }
76
77                 status = cli_connect(cli, desthost, &dest_ip);
78                 if (NT_STATUS_IS_OK(status)) {
79                         DEBUG(3,("connected to password server %s\n",desthost));
80                         connected_ok = True;
81                         break;
82                 }
83                 DEBUG(10,("server_cryptkey: failed to connect to server %s. Error %s\n",
84                         desthost, nt_errstr(status) ));
85         }
86
87         if (!connected_ok) {
88                 release_server_mutex();
89                 DEBUG(0,("password server not available\n"));
90                 cli_shutdown(cli);
91                 return NULL;
92         }
93         
94         if (!attempt_netbios_session_request(&cli, global_myname(), 
95                                              desthost, &dest_ip)) {
96                 release_server_mutex();
97                 DEBUG(1,("password server fails session request\n"));
98                 cli_shutdown(cli);
99                 return NULL;
100         }
101         
102         if (strequal(desthost,myhostname())) {
103                 exit_server_cleanly("Password server loop!");
104         }
105         
106         DEBUG(3,("got session\n"));
107
108         if (!cli_negprot(cli)) {
109                 DEBUG(1,("%s rejected the negprot\n",desthost));
110                 release_server_mutex();
111                 cli_shutdown(cli);
112                 return NULL;
113         }
114
115         if (cli->protocol < PROTOCOL_LANMAN2 ||
116             !(cli->sec_mode & NEGOTIATE_SECURITY_USER_LEVEL)) {
117                 DEBUG(1,("%s isn't in user level security mode\n",desthost));
118                 release_server_mutex();
119                 cli_shutdown(cli);
120                 return NULL;
121         }
122
123         /* Get the first session setup done quickly, to avoid silly 
124            Win2k bugs.  (The next connection to the server will kill
125            this one... 
126         */
127
128         if (!NT_STATUS_IS_OK(cli_session_setup(cli, "", "", 0, "", 0,
129                                                ""))) {
130                 DEBUG(0,("%s rejected the initial session setup (%s)\n",
131                          desthost, cli_errstr(cli)));
132                 release_server_mutex();
133                 cli_shutdown(cli);
134                 return NULL;
135         }
136         
137         release_server_mutex();
138
139         DEBUG(3,("password server OK\n"));
140         
141         return cli;
142 }
143
144 struct server_security_state {
145         struct cli_state *cli;
146 };
147
148 /****************************************************************************
149  Send a 'keepalive' packet down the cli pipe.
150 ****************************************************************************/
151
152 static BOOL send_server_keepalive(const struct timeval *now,
153                                   void *private_data) 
154 {
155         struct server_security_state *state = talloc_get_type_abort(
156                 private_data, struct server_security_state);
157
158         if (!state->cli || !state->cli->initialised) {
159                 return False;
160         }
161
162         if (send_keepalive(state->cli->fd)) {
163                 return True;
164         }
165
166         DEBUG( 2, ( "send_server_keepalive: password server keepalive "
167                     "failed.\n"));
168         cli_shutdown(state->cli);
169         state->cli = NULL;
170         return False;
171 }
172
173 static int destroy_server_security(struct server_security_state *state)
174 {
175         if (state->cli) {
176                 cli_shutdown(state->cli);
177         }
178         return 0;
179 }
180
181 static struct server_security_state *make_server_security_state(struct cli_state *cli)
182 {
183         struct server_security_state *result;
184
185         if (!(result = talloc(NULL, struct server_security_state))) {
186                 DEBUG(0, ("talloc failed\n"));
187                 cli_shutdown(cli);
188                 return NULL;
189         }
190
191         result->cli = cli;
192         talloc_set_destructor(result, destroy_server_security);
193
194         if (lp_keepalive() != 0) {
195                 struct timeval interval;
196                 interval.tv_sec = lp_keepalive();
197                 interval.tv_usec = 0;
198
199                 if (event_add_idle(smbd_event_context(), result, interval,
200                                    "server_security_keepalive",
201                                    send_server_keepalive,
202                                    result) == NULL) {
203                         DEBUG(0, ("event_add_idle failed\n"));
204                         TALLOC_FREE(result);
205                         return NULL;
206                 }
207         }
208
209         return result;
210 }
211
212 /****************************************************************************
213  Get the challenge out of a password server.
214 ****************************************************************************/
215
216 static DATA_BLOB auth_get_challenge_server(const struct auth_context *auth_context,
217                                            void **my_private_data, 
218                                            TALLOC_CTX *mem_ctx)
219 {
220         struct cli_state *cli = server_cryptkey(mem_ctx);
221         
222         if (cli) {
223                 DEBUG(3,("using password server validation\n"));
224
225                 if ((cli->sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) == 0) {
226                         /* We can't work with unencrypted password servers
227                            unless 'encrypt passwords = no' */
228                         DEBUG(5,("make_auth_info_server: Server is unencrypted, no challenge available..\n"));
229                         
230                         /* However, it is still a perfectly fine connection
231                            to pass that unencrypted password over */
232                         *my_private_data =
233                                 (void *)make_server_security_state(cli);
234                         return data_blob_null;
235                         
236                 } else if (cli->secblob.length < 8) {
237                         /* We can't do much if we don't get a full challenge */
238                         DEBUG(2,("make_auth_info_server: Didn't receive a full challenge from server\n"));
239                         cli_shutdown(cli);
240                         return data_blob_null;
241                 }
242
243                 if (!(*my_private_data = (void *)make_server_security_state(cli))) {
244                         return data_blob_null;
245                 }
246
247                 /* The return must be allocated on the caller's mem_ctx, as our own will be
248                    destoyed just after the call. */
249                 return data_blob_talloc(auth_context->mem_ctx, cli->secblob.data,8);
250         } else {
251                 return data_blob_null;
252         }
253 }
254
255
256 /****************************************************************************
257  Check for a valid username and password in security=server mode.
258   - Validate a password with the password server.
259 ****************************************************************************/
260
261 static NTSTATUS check_smbserver_security(const struct auth_context *auth_context,
262                                          void *private_data, 
263                                          TALLOC_CTX *mem_ctx,
264                                          const auth_usersupplied_info *user_info, 
265                                          auth_serversupplied_info **server_info)
266 {
267         struct cli_state *cli;
268         static unsigned char badpass[24];
269         static fstring baduser; 
270         static BOOL tested_password_server = False;
271         static BOOL bad_password_server = False;
272         NTSTATUS nt_status = NT_STATUS_NOT_IMPLEMENTED;
273         BOOL locally_made_cli = False;
274         struct server_security_state *state;
275
276         state = talloc_get_type_abort(
277                 private_data, struct server_security_state);
278
279         cli = state->cli;
280         
281         if (cli) {
282         } else {
283                 cli = server_cryptkey(mem_ctx);
284                 locally_made_cli = True;
285         }
286
287         if (!cli || !cli->initialised) {
288                 DEBUG(1,("password server is not connected (cli not initilised)\n"));
289                 return NT_STATUS_LOGON_FAILURE;
290         }  
291         
292         if ((cli->sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) == 0) {
293                 if (user_info->encrypted) {
294                         DEBUG(1,("password server %s is plaintext, but we are encrypted. This just can't work :-(\n", cli->desthost));
295                         return NT_STATUS_LOGON_FAILURE;         
296                 }
297         } else {
298                 if (memcmp(cli->secblob.data, auth_context->challenge.data, 8) != 0) {
299                         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));
300                         return NT_STATUS_LOGON_FAILURE;         
301                 }
302         }
303
304         if(badpass[0] == 0)
305                 memset(badpass, 0x1f, sizeof(badpass));
306
307         if((user_info->nt_resp.length == sizeof(badpass)) && 
308            !memcmp(badpass, user_info->nt_resp.data, sizeof(badpass))) {
309                 /* 
310                  * Very unlikely, our random bad password is the same as the users
311                  * password.
312                  */
313                 memset(badpass, badpass[0]+1, sizeof(badpass));
314         }
315
316         if(baduser[0] == 0) {
317                 fstrcpy(baduser, INVALID_USER_PREFIX);
318                 fstrcat(baduser, global_myname());
319         }
320
321         /*
322          * Attempt a session setup with a totally incorrect password.
323          * If this succeeds with the guest bit *NOT* set then the password
324          * server is broken and is not correctly setting the guest bit. We
325          * need to detect this as some versions of NT4.x are broken. JRA.
326          */
327
328         /* I sure as hell hope that there aren't servers out there that take 
329          * NTLMv2 and have this bug, as we don't test for that... 
330          *  - abartlet@samba.org
331          */
332
333         if ((!tested_password_server) && (lp_paranoid_server_security())) {
334                 if (NT_STATUS_IS_OK(cli_session_setup(cli, baduser,
335                                                       (char *)badpass,
336                                                       sizeof(badpass), 
337                                                       (char *)badpass,
338                                                       sizeof(badpass),
339                                                       user_info->domain))) {
340
341                         /*
342                          * We connected to the password server so we
343                          * can say we've tested it.
344                          */
345                         tested_password_server = True;
346
347                         if ((SVAL(cli->inbuf,smb_vwv2) & 1) == 0) {
348                                 DEBUG(0,("server_validate: password server %s allows users as non-guest \
349 with a bad password.\n", cli->desthost));
350                                 DEBUG(0,("server_validate: This is broken (and insecure) behaviour. Please do not \
351 use this machine as the password server.\n"));
352                                 cli_ulogoff(cli);
353
354                                 /*
355                                  * Password server has the bug.
356                                  */
357                                 bad_password_server = True;
358                                 return NT_STATUS_LOGON_FAILURE;
359                         }
360                         cli_ulogoff(cli);
361                 }
362         } else {
363
364                 /*
365                  * We have already tested the password server.
366                  * Fail immediately if it has the bug.
367                  */
368
369                 if(bad_password_server) {
370                         DEBUG(0,("server_validate: [1] password server %s allows users as non-guest \
371 with a bad password.\n", cli->desthost));
372                         DEBUG(0,("server_validate: [1] This is broken (and insecure) behaviour. Please do not \
373 use this machine as the password server.\n"));
374                         return NT_STATUS_LOGON_FAILURE;
375                 }
376         }
377
378         /*
379          * Now we know the password server will correctly set the guest bit, or is
380          * not guest enabled, we can try with the real password.
381          */
382
383         if (!user_info->encrypted) {
384                 /* Plaintext available */
385                 nt_status = cli_session_setup(
386                         cli, user_info->smb_name, 
387                         (char *)user_info->plaintext_password.data, 
388                         user_info->plaintext_password.length, 
389                         NULL, 0, user_info->domain);
390
391         } else {
392                 nt_status = cli_session_setup(
393                         cli, user_info->smb_name, 
394                         (char *)user_info->lm_resp.data, 
395                         user_info->lm_resp.length, 
396                         (char *)user_info->nt_resp.data, 
397                         user_info->nt_resp.length, 
398                         user_info->domain);
399         }
400
401         if (!NT_STATUS_IS_OK(nt_status)) {
402                 DEBUG(1,("password server %s rejected the password: %s\n",
403                          cli->desthost, nt_errstr(nt_status)));
404         }
405
406         /* if logged in as guest then reject */
407         if ((SVAL(cli->inbuf,smb_vwv2) & 1) != 0) {
408                 DEBUG(1,("password server %s gave us guest only\n", cli->desthost));
409                 nt_status = NT_STATUS_LOGON_FAILURE;
410         }
411
412         cli_ulogoff(cli);
413
414         if (NT_STATUS_IS_OK(nt_status)) {
415                 fstring real_username;
416                 struct passwd *pass;
417
418                 if ( (pass = smb_getpwnam( NULL, user_info->internal_username, 
419                         real_username, True )) != NULL ) 
420                 {
421                         /* if a real user check pam account restrictions */
422                         /* only really perfomed if "obey pam restriction" is true */
423                         nt_status = smb_pam_accountcheck(pass->pw_name);
424                         if (  !NT_STATUS_IS_OK(nt_status)) {
425                                 DEBUG(1, ("PAM account restriction prevents user login\n"));
426                         } else {
427
428                                 nt_status = make_server_info_pw(server_info, pass->pw_name, pass);
429                         }
430                         TALLOC_FREE(pass);
431                 }
432                 else
433                 {
434                         nt_status = NT_STATUS_NO_SUCH_USER;
435                 }
436         }
437
438         if (locally_made_cli) {
439                 cli_shutdown(cli);
440         }
441
442         return(nt_status);
443 }
444
445 static NTSTATUS auth_init_smbserver(struct auth_context *auth_context, const char* param, auth_methods **auth_method) 
446 {
447         if (!make_auth_methods(auth_context, auth_method)) {
448                 return NT_STATUS_NO_MEMORY;
449         }
450         (*auth_method)->name = "smbserver";
451         (*auth_method)->auth = check_smbserver_security;
452         (*auth_method)->get_chal = auth_get_challenge_server;
453         return NT_STATUS_OK;
454 }
455
456 NTSTATUS auth_server_init(void)
457 {
458         return smb_register_auth(AUTH_INTERFACE_VERSION, "smbserver", auth_init_smbserver);
459 }