7c99848612775013f3783975e6b77a65b9784ac5
[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
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_talloc(mem_ctx, &p, &desthost, LIST_SEP)) {
51                 NTSTATUS status;
52
53                 desthost = talloc_sub_basic(mem_ctx,
54                                 current_user_info.smb_name,
55                                 current_user_info.domain,
56                                 desthost);
57                 if (!desthost) {
58                         return NULL;
59                 }
60                 strupper_m(desthost);
61
62                 if(!resolve_name( desthost, &dest_ss, 0x20)) {
63                         DEBUG(1,("server_cryptkey: Can't resolve address for %s\n",desthost));
64                         continue;
65                 }
66
67                 if (ismyaddr(&dest_ss)) {
68                         DEBUG(1,("Password server loop - disabling password server %s\n",desthost));
69                         continue;
70                 }
71
72                 /* we use a mutex to prevent two connections at once - when a
73                    Win2k PDC get two connections where one hasn't completed a
74                    session setup yet it will send a TCP reset to the first
75                    connection (tridge) */
76
77                 if (!grab_server_mutex(desthost)) {
78                         return NULL;
79                 }
80
81                 status = cli_connect(cli, desthost, &dest_ss);
82                 if (NT_STATUS_IS_OK(status)) {
83                         DEBUG(3,("connected to password server %s\n",desthost));
84                         connected_ok = True;
85                         break;
86                 }
87                 DEBUG(10,("server_cryptkey: failed to connect to server %s. Error %s\n",
88                         desthost, nt_errstr(status) ));
89                 release_server_mutex();
90         }
91
92         if (!connected_ok) {
93                 DEBUG(0,("password server not available\n"));
94                 cli_shutdown(cli);
95                 return NULL;
96         }
97
98         if (!attempt_netbios_session_request(&cli, global_myname(),
99                                              desthost, &dest_ss)) {
100                 release_server_mutex();
101                 DEBUG(1,("password server fails session request\n"));
102                 cli_shutdown(cli);
103                 return NULL;
104         }
105
106         if (strequal(desthost,myhostname())) {
107                 exit_server_cleanly("Password server loop!");
108         }
109
110         DEBUG(3,("got session\n"));
111
112         if (!cli_negprot(cli)) {
113                 DEBUG(1,("%s rejected the negprot\n",desthost));
114                 release_server_mutex();
115                 cli_shutdown(cli);
116                 return NULL;
117         }
118
119         if (cli->protocol < PROTOCOL_LANMAN2 ||
120             !(cli->sec_mode & NEGOTIATE_SECURITY_USER_LEVEL)) {
121                 DEBUG(1,("%s isn't in user level security mode\n",desthost));
122                 release_server_mutex();
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         if (!NT_STATUS_IS_OK(cli_session_setup(cli, "", "", 0, "", 0,
133                                                ""))) {
134                 DEBUG(0,("%s rejected the initial session setup (%s)\n",
135                          desthost, cli_errstr(cli)));
136                 release_server_mutex();
137                 cli_shutdown(cli);
138                 return NULL;
139         }
140
141         release_server_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(smbd_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(auth_context->mem_ctx, 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 auth_usersupplied_info *user_info, 
268                                          auth_serversupplied_info **server_info)
269 {
270         struct cli_state *cli;
271         static unsigned char badpass[24];
272         static fstring baduser; 
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         cli = (struct cli_state *)my_private_data;
279         
280         if (cli) {
281         } else {
282                 cli = server_cryptkey(mem_ctx);
283                 locally_made_cli = True;
284         }
285
286         if (!cli || !cli->initialised) {
287                 DEBUG(1,("password server is not connected (cli not initilised)\n"));
288                 return NT_STATUS_LOGON_FAILURE;
289         }  
290         
291         if ((cli->sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) == 0) {
292                 if (user_info->encrypted) {
293                         DEBUG(1,("password server %s is plaintext, but we are encrypted. This just can't work :-(\n", cli->desthost));
294                         return NT_STATUS_LOGON_FAILURE;         
295                 }
296         } else {
297                 if (memcmp(cli->secblob.data, auth_context->challenge.data, 8) != 0) {
298                         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));
299                         return NT_STATUS_LOGON_FAILURE;         
300                 }
301         }
302
303         if(badpass[0] == 0)
304                 memset(badpass, 0x1f, sizeof(badpass));
305
306         if((user_info->nt_resp.length == sizeof(badpass)) && 
307            !memcmp(badpass, user_info->nt_resp.data, sizeof(badpass))) {
308                 /* 
309                  * Very unlikely, our random bad password is the same as the users
310                  * password.
311                  */
312                 memset(badpass, badpass[0]+1, sizeof(badpass));
313         }
314
315         if(baduser[0] == 0) {
316                 fstrcpy(baduser, INVALID_USER_PREFIX);
317                 fstrcat(baduser, global_myname());
318         }
319
320         /*
321          * Attempt a session setup with a totally incorrect password.
322          * If this succeeds with the guest bit *NOT* set then the password
323          * server is broken and is not correctly setting the guest bit. We
324          * need to detect this as some versions of NT4.x are broken. JRA.
325          */
326
327         /* I sure as hell hope that there aren't servers out there that take 
328          * NTLMv2 and have this bug, as we don't test for that... 
329          *  - abartlet@samba.org
330          */
331
332         if ((!tested_password_server) && (lp_paranoid_server_security())) {
333                 if (NT_STATUS_IS_OK(cli_session_setup(cli, baduser,
334                                                       (char *)badpass,
335                                                       sizeof(badpass), 
336                                                       (char *)badpass,
337                                                       sizeof(badpass),
338                                                       user_info->domain))) {
339
340                         /*
341                          * We connected to the password server so we
342                          * can say we've tested it.
343                          */
344                         tested_password_server = True;
345
346                         if ((SVAL(cli->inbuf,smb_vwv2) & 1) == 0) {
347                                 DEBUG(0,("server_validate: password server %s allows users as non-guest \
348 with a bad password.\n", cli->desthost));
349                                 DEBUG(0,("server_validate: This is broken (and insecure) behaviour. Please do not \
350 use this machine as the password server.\n"));
351                                 cli_ulogoff(cli);
352
353                                 /*
354                                  * Password server has the bug.
355                                  */
356                                 bad_password_server = True;
357                                 return NT_STATUS_LOGON_FAILURE;
358                         }
359                         cli_ulogoff(cli);
360                 }
361         } else {
362
363                 /*
364                  * We have already tested the password server.
365                  * Fail immediately if it has the bug.
366                  */
367
368                 if(bad_password_server) {
369                         DEBUG(0,("server_validate: [1] password server %s allows users as non-guest \
370 with a bad password.\n", cli->desthost));
371                         DEBUG(0,("server_validate: [1] This is broken (and insecure) behaviour. Please do not \
372 use this machine as the password server.\n"));
373                         return NT_STATUS_LOGON_FAILURE;
374                 }
375         }
376
377         /*
378          * Now we know the password server will correctly set the guest bit, or is
379          * not guest enabled, we can try with the real password.
380          */
381
382         if (!user_info->encrypted) {
383                 /* Plaintext available */
384                 nt_status = cli_session_setup(
385                         cli, user_info->smb_name, 
386                         (char *)user_info->plaintext_password.data, 
387                         user_info->plaintext_password.length, 
388                         NULL, 0, user_info->domain);
389
390         } else {
391                 nt_status = cli_session_setup(
392                         cli, user_info->smb_name, 
393                         (char *)user_info->lm_resp.data, 
394                         user_info->lm_resp.length, 
395                         (char *)user_info->nt_resp.data, 
396                         user_info->nt_resp.length, 
397                         user_info->domain);
398         }
399
400         if (!NT_STATUS_IS_OK(nt_status)) {
401                 DEBUG(1,("password server %s rejected the password: %s\n",
402                          cli->desthost, nt_errstr(nt_status)));
403         }
404
405         /* if logged in as guest then reject */
406         if ((SVAL(cli->inbuf,smb_vwv2) & 1) != 0) {
407                 DEBUG(1,("password server %s gave us guest only\n", cli->desthost));
408                 nt_status = NT_STATUS_LOGON_FAILURE;
409         }
410
411         cli_ulogoff(cli);
412
413         if (NT_STATUS_IS_OK(nt_status)) {
414                 fstring real_username;
415                 struct passwd *pass;
416
417                 if ( (pass = smb_getpwnam( NULL, user_info->internal_username, 
418                         real_username, True )) != NULL ) 
419                 {
420                         /* if a real user check pam account restrictions */
421                         /* only really perfomed if "obey pam restriction" is true */
422                         nt_status = smb_pam_accountcheck(pass->pw_name);
423                         if (  !NT_STATUS_IS_OK(nt_status)) {
424                                 DEBUG(1, ("PAM account restriction prevents user login\n"));
425                         } else {
426
427                                 nt_status = make_server_info_pw(server_info, pass->pw_name, pass);
428                         }
429                         TALLOC_FREE(pass);
430                 }
431                 else
432                 {
433                         nt_status = NT_STATUS_NO_SUCH_USER;
434                 }
435         }
436
437         if (locally_made_cli) {
438                 cli_shutdown(cli);
439         }
440
441         return(nt_status);
442 }
443
444 static NTSTATUS auth_init_smbserver(struct auth_context *auth_context, const char* param, auth_methods **auth_method) 
445 {
446         if (!make_auth_methods(auth_context, auth_method)) {
447                 return NT_STATUS_NO_MEMORY;
448         }
449         (*auth_method)->name = "smbserver";
450         (*auth_method)->auth = check_smbserver_security;
451         (*auth_method)->get_chal = auth_get_challenge_server;
452         return NT_STATUS_OK;
453 }
454
455 NTSTATUS auth_server_init(void)
456 {
457         return smb_register_auth(AUTH_INTERFACE_VERSION, "smbserver", auth_init_smbserver);
458 }