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