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