s3: Use the correct guest_login field in auth_server
[Samba.git] / source3 / auth / auth_server.c
blob1dfa64682696caff43b1bcace64a05c028b99e19
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
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.
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.
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/>.
21 #include "includes.h"
22 #include "auth.h"
23 #include "system/passwd.h"
24 #include "smbd/smbd.h"
25 #include "libsmb/libsmb.h"
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_AUTH
30 extern userdom_struct current_user_info;
32 /****************************************************************************
33 Support for server level security.
34 ****************************************************************************/
36 static struct cli_state *server_cryptkey(TALLOC_CTX *mem_ctx)
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;
47 if (!(cli = cli_initialise()))
48 return NULL;
50 /* security = server just can't function with spnego */
51 cli->use_spnego = False;
53 pserver = talloc_strdup(mem_ctx, lp_passwordserver());
54 p = pserver;
56 while(next_token_talloc(mem_ctx, &p, &desthost, LIST_SEP)) {
58 desthost = talloc_sub_basic(mem_ctx,
59 current_user_info.smb_name,
60 current_user_info.domain,
61 desthost);
62 if (!desthost) {
63 return NULL;
65 strupper_m(desthost);
67 if(!resolve_name( desthost, &dest_ss, 0x20, false)) {
68 DEBUG(1,("server_cryptkey: Can't resolve address for %s\n",desthost));
69 continue;
72 if (ismyaddr((struct sockaddr *)&dest_ss)) {
73 DEBUG(1,("Password server loop - disabling password server %s\n",desthost));
74 continue;
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) */
82 mutex = grab_named_mutex(talloc_tos(), desthost, 10);
83 if (mutex == NULL) {
84 cli_shutdown(cli);
85 return NULL;
88 status = cli_connect(cli, desthost, &dest_ss);
89 if (NT_STATUS_IS_OK(status)) {
90 DEBUG(3,("connected to password server %s\n",desthost));
91 connected_ok = True;
92 break;
94 DEBUG(10,("server_cryptkey: failed to connect to server %s. Error %s\n",
95 desthost, nt_errstr(status) ));
96 TALLOC_FREE(mutex);
99 if (!connected_ok) {
100 DEBUG(0,("password server not available\n"));
101 cli_shutdown(cli);
102 return NULL;
105 if (!attempt_netbios_session_request(&cli, global_myname(),
106 desthost, &dest_ss)) {
107 TALLOC_FREE(mutex);
108 DEBUG(1,("password server fails session request\n"));
109 cli_shutdown(cli);
110 return NULL;
113 if (strequal(desthost,myhostname())) {
114 exit_server_cleanly("Password server loop!");
117 DEBUG(3,("got session\n"));
119 status = cli_negprot(cli);
121 if (!NT_STATUS_IS_OK(status)) {
122 TALLOC_FREE(mutex);
123 DEBUG(1, ("%s rejected the negprot: %s\n",
124 desthost, nt_errstr(status)));
125 cli_shutdown(cli);
126 return NULL;
129 if (cli->protocol < PROTOCOL_LANMAN2 ||
130 !(cli->sec_mode & NEGOTIATE_SECURITY_USER_LEVEL)) {
131 TALLOC_FREE(mutex);
132 DEBUG(1,("%s isn't in user level security mode\n",desthost));
133 cli_shutdown(cli);
134 return NULL;
137 /* Get the first session setup done quickly, to avoid silly
138 Win2k bugs. (The next connection to the server will kill
139 this one...
142 status = cli_session_setup(cli, "", "", 0, "", 0, "");
143 if (!NT_STATUS_IS_OK(status)) {
144 TALLOC_FREE(mutex);
145 DEBUG(0,("%s rejected the initial session setup (%s)\n",
146 desthost, nt_errstr(status)));
147 cli_shutdown(cli);
148 return NULL;
151 TALLOC_FREE(mutex);
153 DEBUG(3,("password server OK\n"));
155 return cli;
158 struct server_security_state {
159 struct cli_state *cli;
162 /****************************************************************************
163 Send a 'keepalive' packet down the cli pipe.
164 ****************************************************************************/
166 static bool send_server_keepalive(const struct timeval *now,
167 void *private_data)
169 struct server_security_state *state = talloc_get_type_abort(
170 private_data, struct server_security_state);
172 if (!state->cli || !state->cli->initialised) {
173 return False;
176 if (send_keepalive(state->cli->fd)) {
177 return True;
180 DEBUG( 2, ( "send_server_keepalive: password server keepalive "
181 "failed.\n"));
182 cli_shutdown(state->cli);
183 state->cli = NULL;
184 return False;
187 static int destroy_server_security(struct server_security_state *state)
189 if (state->cli) {
190 cli_shutdown(state->cli);
192 return 0;
195 static struct server_security_state *make_server_security_state(struct cli_state *cli)
197 struct server_security_state *result;
199 if (!(result = talloc(NULL, struct server_security_state))) {
200 DEBUG(0, ("talloc failed\n"));
201 cli_shutdown(cli);
202 return NULL;
205 result->cli = cli;
206 talloc_set_destructor(result, destroy_server_security);
208 if (lp_keepalive() != 0) {
209 struct timeval interval;
210 interval.tv_sec = lp_keepalive();
211 interval.tv_usec = 0;
213 if (event_add_idle(server_event_context(), result, interval,
214 "server_security_keepalive",
215 send_server_keepalive,
216 result) == NULL) {
217 DEBUG(0, ("event_add_idle failed\n"));
218 TALLOC_FREE(result);
219 return NULL;
223 return result;
226 /****************************************************************************
227 Get the challenge out of a password server.
228 ****************************************************************************/
230 static DATA_BLOB auth_get_challenge_server(const struct auth_context *auth_context,
231 void **my_private_data,
232 TALLOC_CTX *mem_ctx)
234 struct cli_state *cli = server_cryptkey(mem_ctx);
236 if (cli) {
237 DEBUG(3,("using password server validation\n"));
239 if ((cli->sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) == 0) {
240 /* We can't work with unencrypted password servers
241 unless 'encrypt passwords = no' */
242 DEBUG(5,("make_auth_info_server: Server is unencrypted, no challenge available..\n"));
244 /* However, it is still a perfectly fine connection
245 to pass that unencrypted password over */
246 *my_private_data =
247 (void *)make_server_security_state(cli);
248 return data_blob_null;
249 } else if (cli->secblob.length < 8) {
250 /* We can't do much if we don't get a full challenge */
251 DEBUG(2,("make_auth_info_server: Didn't receive a full challenge from server\n"));
252 cli_shutdown(cli);
253 return data_blob_null;
256 if (!(*my_private_data = (void *)make_server_security_state(cli))) {
257 return data_blob(NULL,0);
260 /* The return must be allocated on the caller's mem_ctx, as our own will be
261 destoyed just after the call. */
262 return data_blob_talloc((TALLOC_CTX *)auth_context, cli->secblob.data,8);
263 } else {
264 return data_blob_null;
269 /****************************************************************************
270 Check for a valid username and password in security=server mode.
271 - Validate a password with the password server.
272 ****************************************************************************/
274 static NTSTATUS check_smbserver_security(const struct auth_context *auth_context,
275 void *my_private_data,
276 TALLOC_CTX *mem_ctx,
277 const struct auth_usersupplied_info *user_info,
278 struct auth_serversupplied_info **server_info)
280 struct server_security_state *state = talloc_get_type_abort(
281 my_private_data, struct server_security_state);
282 struct cli_state *cli;
283 static bool tested_password_server = False;
284 static bool bad_password_server = False;
285 NTSTATUS nt_status = NT_STATUS_NOT_IMPLEMENTED;
286 bool locally_made_cli = False;
288 DEBUG(10, ("Check auth for: [%s]\n", user_info->mapped.account_name));
290 cli = state->cli;
292 if (cli) {
293 } else {
294 cli = server_cryptkey(mem_ctx);
295 locally_made_cli = True;
298 if (!cli || !cli->initialised) {
299 DEBUG(1,("password server is not connected (cli not initialised)\n"));
300 return NT_STATUS_LOGON_FAILURE;
303 if ((cli->sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) == 0) {
304 if (user_info->password_state != AUTH_PASSWORD_PLAIN) {
305 DEBUG(1,("password server %s is plaintext, but we are encrypted. This just can't work :-(\n", cli->desthost));
306 return NT_STATUS_LOGON_FAILURE;
308 } else {
309 if (memcmp(cli->secblob.data, auth_context->challenge.data, 8) != 0) {
310 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));
311 return NT_STATUS_LOGON_FAILURE;
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.
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
327 if ((!tested_password_server) && (lp_paranoid_server_security())) {
328 unsigned char badpass[24];
329 char *baduser = NULL;
331 memset(badpass, 0x1f, sizeof(badpass));
333 if((user_info->password.response.nt.length == sizeof(badpass)) &&
334 !memcmp(badpass, user_info->password.response.nt.data, sizeof(badpass))) {
336 * Very unlikely, our random bad password is the same as the users
337 * password.
339 memset(badpass, badpass[0]+1, sizeof(badpass));
342 baduser = talloc_asprintf(mem_ctx,
343 "%s%s",
344 INVALID_USER_PREFIX,
345 global_myname());
346 if (!baduser) {
347 return NT_STATUS_NO_MEMORY;
350 if (NT_STATUS_IS_OK(cli_session_setup(cli, baduser,
351 (char *)badpass,
352 sizeof(badpass),
353 (char *)badpass,
354 sizeof(badpass),
355 user_info->mapped.domain_name))) {
358 * We connected to the password server so we
359 * can say we've tested it.
361 tested_password_server = True;
363 if ((SVAL(cli->inbuf,smb_vwv2) & 1) == 0) {
364 DEBUG(0,("server_validate: password server %s allows users as non-guest \
365 with a bad password.\n", cli->desthost));
366 DEBUG(0,("server_validate: This is broken (and insecure) behaviour. Please do not \
367 use this machine as the password server.\n"));
368 cli_ulogoff(cli);
371 * Password server has the bug.
373 bad_password_server = True;
374 return NT_STATUS_LOGON_FAILURE;
376 cli_ulogoff(cli);
378 } else {
381 * We have already tested the password server.
382 * Fail immediately if it has the bug.
385 if(bad_password_server) {
386 DEBUG(0,("server_validate: [1] password server %s allows users as non-guest \
387 with a bad password.\n", cli->desthost));
388 DEBUG(0,("server_validate: [1] This is broken (and insecure) behaviour. Please do not \
389 use this machine as the password server.\n"));
390 return NT_STATUS_LOGON_FAILURE;
395 * Now we know the password server will correctly set the guest bit, or is
396 * not guest enabled, we can try with the real password.
398 switch (user_info->password_state) {
399 case AUTH_PASSWORD_PLAIN:
400 /* Plaintext available */
401 nt_status = cli_session_setup(
402 cli, user_info->client.account_name,
403 user_info->password.plaintext,
404 strlen(user_info->password.plaintext),
405 NULL, 0, user_info->mapped.domain_name);
406 break;
408 /* currently the hash values include a challenge-response as well */
409 case AUTH_PASSWORD_HASH:
410 case AUTH_PASSWORD_RESPONSE:
411 nt_status = cli_session_setup(
412 cli, user_info->client.account_name,
413 (char *)user_info->password.response.lanman.data,
414 user_info->password.response.lanman.length,
415 (char *)user_info->password.response.nt.data,
416 user_info->password.response.nt.length,
417 user_info->mapped.domain_name);
418 break;
419 default:
420 DEBUG(0,("user_info constructed for user '%s' was invalid - password_state=%u invalid.\n",user_info->mapped.account_name, user_info->password_state));
421 nt_status = NT_STATUS_INTERNAL_ERROR;
424 if (!NT_STATUS_IS_OK(nt_status)) {
425 DEBUG(1,("password server %s rejected the password: %s\n",
426 cli->desthost, nt_errstr(nt_status)));
429 /* if logged in as guest then reject */
430 if (cli->is_guestlogin) {
431 DEBUG(1,("password server %s gave us guest only\n", cli->desthost));
432 nt_status = NT_STATUS_LOGON_FAILURE;
435 cli_ulogoff(cli);
437 if (NT_STATUS_IS_OK(nt_status)) {
438 char *real_username = NULL;
439 struct passwd *pass = NULL;
441 if ( (pass = smb_getpwnam(talloc_tos(), user_info->mapped.account_name,
442 &real_username, True )) != NULL )
444 nt_status = make_server_info_pw(server_info, pass->pw_name, pass);
445 TALLOC_FREE(pass);
446 TALLOC_FREE(real_username);
448 else
450 nt_status = NT_STATUS_NO_SUCH_USER;
454 if (locally_made_cli) {
455 cli_shutdown(cli);
458 return(nt_status);
461 static NTSTATUS auth_init_smbserver(struct auth_context *auth_context, const char* param, auth_methods **auth_method)
463 struct auth_methods *result;
465 result = TALLOC_ZERO_P(auth_context, struct auth_methods);
466 if (result == NULL) {
467 return NT_STATUS_NO_MEMORY;
469 result->name = "smbserver";
470 result->auth = check_smbserver_security;
471 result->get_chal = auth_get_challenge_server;
473 *auth_method = result;
474 return NT_STATUS_OK;
477 NTSTATUS auth_server_init(void)
479 return smb_register_auth(AUTH_INTERFACE_VERSION, "smbserver", auth_init_smbserver);