vfs_catia: add my copyright
[Samba.git] / source3 / auth / auth_server.c
blobfdd7671c091c102824c6faefed9483eb6a599787
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 = NULL;
281 struct cli_state *cli = NULL;
282 static bool tested_password_server = False;
283 static bool bad_password_server = False;
284 NTSTATUS nt_status = NT_STATUS_NOT_IMPLEMENTED;
285 bool locally_made_cli = False;
287 DEBUG(10, ("check_smbserver_security: Check auth for: [%s]\n",
288 user_info->mapped.account_name));
290 if (my_private_data == NULL) {
291 DEBUG(10,("check_smbserver_security: "
292 "password server is not connected\n"));
293 return NT_STATUS_LOGON_FAILURE;
296 state = talloc_get_type_abort(my_private_data, struct server_security_state);
297 cli = state->cli;
299 if (cli) {
300 } else {
301 cli = server_cryptkey(mem_ctx);
302 locally_made_cli = True;
305 if (!cli || !cli->initialised) {
306 DEBUG(1,("password server is not connected (cli not initialised)\n"));
307 return NT_STATUS_LOGON_FAILURE;
310 if ((cli->sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) == 0) {
311 if (user_info->password_state != AUTH_PASSWORD_PLAIN) {
312 DEBUG(1,("password server %s is plaintext, but we are encrypted. This just can't work :-(\n", cli->desthost));
313 return NT_STATUS_LOGON_FAILURE;
315 } else {
316 if (memcmp(cli->secblob.data, auth_context->challenge.data, 8) != 0) {
317 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));
318 return NT_STATUS_LOGON_FAILURE;
323 * Attempt a session setup with a totally incorrect password.
324 * If this succeeds with the guest bit *NOT* set then the password
325 * server is broken and is not correctly setting the guest bit. We
326 * need to detect this as some versions of NT4.x are broken. JRA.
329 /* I sure as hell hope that there aren't servers out there that take
330 * NTLMv2 and have this bug, as we don't test for that...
331 * - abartlet@samba.org
334 if ((!tested_password_server) && (lp_paranoid_server_security())) {
335 unsigned char badpass[24];
336 char *baduser = NULL;
338 memset(badpass, 0x1f, sizeof(badpass));
340 if((user_info->password.response.nt.length == sizeof(badpass)) &&
341 !memcmp(badpass, user_info->password.response.nt.data, sizeof(badpass))) {
343 * Very unlikely, our random bad password is the same as the users
344 * password.
346 memset(badpass, badpass[0]+1, sizeof(badpass));
349 baduser = talloc_asprintf(mem_ctx,
350 "%s%s",
351 INVALID_USER_PREFIX,
352 global_myname());
353 if (!baduser) {
354 return NT_STATUS_NO_MEMORY;
357 if (NT_STATUS_IS_OK(cli_session_setup(cli, baduser,
358 (char *)badpass,
359 sizeof(badpass),
360 (char *)badpass,
361 sizeof(badpass),
362 user_info->mapped.domain_name))) {
365 * We connected to the password server so we
366 * can say we've tested it.
368 tested_password_server = True;
370 if ((SVAL(cli->inbuf,smb_vwv2) & 1) == 0) {
371 DEBUG(0,("server_validate: password server %s allows users as non-guest \
372 with a bad password.\n", cli->desthost));
373 DEBUG(0,("server_validate: This is broken (and insecure) behaviour. Please do not \
374 use this machine as the password server.\n"));
375 cli_ulogoff(cli);
378 * Password server has the bug.
380 bad_password_server = True;
381 return NT_STATUS_LOGON_FAILURE;
383 cli_ulogoff(cli);
385 } else {
388 * We have already tested the password server.
389 * Fail immediately if it has the bug.
392 if(bad_password_server) {
393 DEBUG(0,("server_validate: [1] password server %s allows users as non-guest \
394 with a bad password.\n", cli->desthost));
395 DEBUG(0,("server_validate: [1] This is broken (and insecure) behaviour. Please do not \
396 use this machine as the password server.\n"));
397 return NT_STATUS_LOGON_FAILURE;
402 * Now we know the password server will correctly set the guest bit, or is
403 * not guest enabled, we can try with the real password.
405 switch (user_info->password_state) {
406 case AUTH_PASSWORD_PLAIN:
407 /* Plaintext available */
408 nt_status = cli_session_setup(
409 cli, user_info->client.account_name,
410 user_info->password.plaintext,
411 strlen(user_info->password.plaintext),
412 NULL, 0, user_info->mapped.domain_name);
413 break;
415 /* currently the hash values include a challenge-response as well */
416 case AUTH_PASSWORD_HASH:
417 case AUTH_PASSWORD_RESPONSE:
418 nt_status = cli_session_setup(
419 cli, user_info->client.account_name,
420 (char *)user_info->password.response.lanman.data,
421 user_info->password.response.lanman.length,
422 (char *)user_info->password.response.nt.data,
423 user_info->password.response.nt.length,
424 user_info->mapped.domain_name);
425 break;
426 default:
427 DEBUG(0,("user_info constructed for user '%s' was invalid - password_state=%u invalid.\n",user_info->mapped.account_name, user_info->password_state));
428 nt_status = NT_STATUS_INTERNAL_ERROR;
431 if (!NT_STATUS_IS_OK(nt_status)) {
432 DEBUG(1,("password server %s rejected the password: %s\n",
433 cli->desthost, nt_errstr(nt_status)));
436 /* if logged in as guest then reject */
437 if (cli->is_guestlogin) {
438 DEBUG(1,("password server %s gave us guest only\n", cli->desthost));
439 nt_status = NT_STATUS_LOGON_FAILURE;
442 cli_ulogoff(cli);
444 if (NT_STATUS_IS_OK(nt_status)) {
445 char *real_username = NULL;
446 struct passwd *pass = NULL;
448 if ( (pass = smb_getpwnam(talloc_tos(), user_info->mapped.account_name,
449 &real_username, True )) != NULL )
451 nt_status = make_server_info_pw(server_info, pass->pw_name, pass);
452 TALLOC_FREE(pass);
453 TALLOC_FREE(real_username);
455 else
457 nt_status = NT_STATUS_NO_SUCH_USER;
461 if (locally_made_cli) {
462 cli_shutdown(cli);
465 return(nt_status);
468 static NTSTATUS auth_init_smbserver(struct auth_context *auth_context, const char* param, auth_methods **auth_method)
470 struct auth_methods *result;
472 result = TALLOC_ZERO_P(auth_context, struct auth_methods);
473 if (result == NULL) {
474 return NT_STATUS_NO_MEMORY;
476 result->name = "smbserver";
477 result->auth = check_smbserver_security;
478 result->get_chal = auth_get_challenge_server;
480 *auth_method = result;
481 return NT_STATUS_OK;
484 NTSTATUS auth_server_init(void)
486 return smb_register_auth(AUTH_INTERFACE_VERSION, "smbserver", auth_init_smbserver);