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