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