Fix winbindd running on a Samba DC,
[ira/wip.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 bool tested_password_server = False;
272         static bool bad_password_server = False;
273         NTSTATUS nt_status = NT_STATUS_NOT_IMPLEMENTED;
274         bool locally_made_cli = False;
275
276         cli = (struct cli_state *)my_private_data;
277         
278         if (cli) {
279         } else {
280                 cli = server_cryptkey(mem_ctx);
281                 locally_made_cli = True;
282         }
283
284         if (!cli || !cli->initialised) {
285                 DEBUG(1,("password server is not connected (cli not initilised)\n"));
286                 return NT_STATUS_LOGON_FAILURE;
287         }  
288         
289         if ((cli->sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) == 0) {
290                 if (user_info->encrypted) {
291                         DEBUG(1,("password server %s is plaintext, but we are encrypted. This just can't work :-(\n", cli->desthost));
292                         return NT_STATUS_LOGON_FAILURE;         
293                 }
294         } else {
295                 if (memcmp(cli->secblob.data, auth_context->challenge.data, 8) != 0) {
296                         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));
297                         return NT_STATUS_LOGON_FAILURE;         
298                 }
299         }
300
301         /*
302          * Attempt a session setup with a totally incorrect password.
303          * If this succeeds with the guest bit *NOT* set then the password
304          * server is broken and is not correctly setting the guest bit. We
305          * need to detect this as some versions of NT4.x are broken. JRA.
306          */
307
308         /* I sure as hell hope that there aren't servers out there that take 
309          * NTLMv2 and have this bug, as we don't test for that... 
310          *  - abartlet@samba.org
311          */
312
313         if ((!tested_password_server) && (lp_paranoid_server_security())) {
314                 unsigned char badpass[24];
315                 char *baduser = NULL;
316
317                 memset(badpass, 0x1f, sizeof(badpass));
318
319                 if((user_info->nt_resp.length == sizeof(badpass)) && 
320                    !memcmp(badpass, user_info->nt_resp.data, sizeof(badpass))) {
321                         /* 
322                          * Very unlikely, our random bad password is the same as the users
323                          * password.
324                          */
325                         memset(badpass, badpass[0]+1, sizeof(badpass));
326                 }
327
328                 baduser = talloc_asprintf(mem_ctx,
329                                         "%s%s",
330                                         INVALID_USER_PREFIX,
331                                         global_myname());
332                 if (!baduser) {
333                         return NT_STATUS_NO_MEMORY;
334                 }
335
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 }