waf: Add autoconf --target support.
[Samba/id10ts.git] / source3 / auth / auth_server.c
blob3bd69cda4d5132633c98cd0501e3aa249f945cfc
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;
46 /* security = server just can't function with spnego */
47 int flags = CLI_FULL_CONNECTION_DONT_SPNEGO;
48 uint16_t sec_mode = 0;
50 pserver = talloc_strdup(mem_ctx, lp_passwordserver());
51 p = pserver;
53 while(next_token_talloc(mem_ctx, &p, &desthost, LIST_SEP)) {
55 desthost = talloc_sub_basic(mem_ctx,
56 current_user_info.smb_name,
57 current_user_info.domain,
58 desthost);
59 if (!desthost) {
60 return NULL;
62 strupper_m(desthost);
64 if (strequal(desthost, myhostname())) {
65 DEBUG(1,("Password server loop - disabling "
66 "password server %s\n", desthost));
67 continue;
70 if(!resolve_name( desthost, &dest_ss, 0x20, false)) {
71 DEBUG(1,("server_cryptkey: Can't resolve address for %s\n",desthost));
72 continue;
75 if (ismyaddr((struct sockaddr *)(void *)&dest_ss)) {
76 DEBUG(1,("Password server loop - disabling password server %s\n",desthost));
77 continue;
80 /* we use a mutex to prevent two connections at once - when a
81 Win2k PDC get two connections where one hasn't completed a
82 session setup yet it will send a TCP reset to the first
83 connection (tridge) */
85 mutex = grab_named_mutex(talloc_tos(), desthost, 10);
86 if (mutex == NULL) {
87 return NULL;
90 status = cli_connect_nb(desthost, &dest_ss, 0, 0x20,
91 lp_netbios_name(), SMB_SIGNING_DEFAULT,
92 flags, &cli);
93 if (NT_STATUS_IS_OK(status)) {
94 DEBUG(3,("connected to password server %s\n",desthost));
95 connected_ok = True;
96 break;
98 DEBUG(10,("server_cryptkey: failed to connect to server %s. Error %s\n",
99 desthost, nt_errstr(status) ));
100 TALLOC_FREE(mutex);
103 if (!connected_ok) {
104 DEBUG(0,("password server not available\n"));
105 return NULL;
108 DEBUG(3,("got session\n"));
110 status = cli_negprot(cli, PROTOCOL_NT1);
112 if (!NT_STATUS_IS_OK(status)) {
113 TALLOC_FREE(mutex);
114 DEBUG(1, ("%s rejected the negprot: %s\n",
115 desthost, nt_errstr(status)));
116 cli_shutdown(cli);
117 return NULL;
120 sec_mode = cli_state_security_mode(cli);
121 if (cli_state_protocol(cli) < PROTOCOL_LANMAN2 ||
122 !(sec_mode & NEGOTIATE_SECURITY_USER_LEVEL)) {
123 TALLOC_FREE(mutex);
124 DEBUG(1,("%s isn't in user level security mode\n",desthost));
125 cli_shutdown(cli);
126 return NULL;
129 /* Get the first session setup done quickly, to avoid silly
130 Win2k bugs. (The next connection to the server will kill
131 this one...
134 status = cli_session_setup(cli, "", "", 0, "", 0, "");
135 if (!NT_STATUS_IS_OK(status)) {
136 TALLOC_FREE(mutex);
137 DEBUG(0,("%s rejected the initial session setup (%s)\n",
138 desthost, nt_errstr(status)));
139 cli_shutdown(cli);
140 return NULL;
143 TALLOC_FREE(mutex);
145 DEBUG(3,("password server OK\n"));
147 return cli;
150 struct server_security_state {
151 struct cli_state *cli;
154 /****************************************************************************
155 Send a 'keepalive' packet down the cli pipe.
156 ****************************************************************************/
158 static bool send_server_keepalive(const struct timeval *now,
159 void *private_data)
161 struct server_security_state *state = talloc_get_type_abort(
162 private_data, struct server_security_state);
163 NTSTATUS status;
164 unsigned char garbage[16];
166 if (!cli_state_is_connected(state->cli)) {
167 return false;
170 /* Ping the server to keep the connection alive using SMBecho. */
171 memset(garbage, 0xf0, sizeof(garbage));
172 status = cli_echo(state->cli, 1, data_blob_const(garbage, sizeof(garbage)));
173 if (NT_STATUS_IS_OK(status)) {
174 return true;
177 DEBUG(2,("send_server_keepalive: password server SMBecho failed: %s\n",
178 nt_errstr(status)));
179 cli_shutdown(state->cli);
180 state->cli = NULL;
181 return false;
184 static int destroy_server_security(struct server_security_state *state)
186 if (state->cli) {
187 cli_shutdown(state->cli);
189 return 0;
192 static struct server_security_state *make_server_security_state(struct cli_state *cli)
194 struct server_security_state *result;
196 if (!(result = talloc(NULL, struct server_security_state))) {
197 DEBUG(0, ("talloc failed\n"));
198 cli_shutdown(cli);
199 return NULL;
202 result->cli = cli;
203 talloc_set_destructor(result, destroy_server_security);
205 if (lp_keepalive() != 0) {
206 struct timeval interval;
207 interval.tv_sec = lp_keepalive();
208 interval.tv_usec = 0;
210 if (event_add_idle(server_event_context(), result, interval,
211 "server_security_keepalive",
212 send_server_keepalive,
213 result) == NULL) {
214 DEBUG(0, ("event_add_idle failed\n"));
215 TALLOC_FREE(result);
216 return NULL;
220 return result;
223 /****************************************************************************
224 Get the challenge out of a password server.
225 ****************************************************************************/
227 static DATA_BLOB auth_get_challenge_server(const struct auth_context *auth_context,
228 void **my_private_data,
229 TALLOC_CTX *mem_ctx)
231 struct cli_state *cli = server_cryptkey(mem_ctx);
233 if (cli) {
234 uint16_t sec_mode = cli_state_security_mode(cli);
235 const uint8_t *server_challenge = cli_state_server_challenge(cli);
237 DEBUG(3,("using password server validation\n"));
239 if ((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;
251 if (!(*my_private_data = (void *)make_server_security_state(cli))) {
252 return data_blob(NULL,0);
255 /* The return must be allocated on the caller's mem_ctx, as our own will be
256 destoyed just after the call. */
257 return data_blob_talloc(discard_const_p(TALLOC_CTX, auth_context), server_challenge ,8);
258 } else {
259 return data_blob_null;
264 /****************************************************************************
265 Check for a valid username and password in security=server mode.
266 - Validate a password with the password server.
267 ****************************************************************************/
269 static NTSTATUS check_smbserver_security(const struct auth_context *auth_context,
270 void *my_private_data,
271 TALLOC_CTX *mem_ctx,
272 const struct auth_usersupplied_info *user_info,
273 struct auth_serversupplied_info **server_info)
275 struct server_security_state *state = NULL;
276 struct cli_state *cli = NULL;
277 static bool tested_password_server = False;
278 static bool bad_password_server = False;
279 NTSTATUS nt_status = NT_STATUS_NOT_IMPLEMENTED;
280 bool locally_made_cli = False;
281 uint16_t sec_mode = 0;
283 DEBUG(10, ("check_smbserver_security: Check auth for: [%s]\n",
284 user_info->mapped.account_name));
286 if (my_private_data == NULL) {
287 DEBUG(10,("check_smbserver_security: "
288 "password server is not connected\n"));
289 return NT_STATUS_LOGON_FAILURE;
292 state = talloc_get_type_abort(my_private_data, struct server_security_state);
293 cli = state->cli;
295 if (cli) {
296 } else {
297 cli = server_cryptkey(mem_ctx);
298 locally_made_cli = True;
301 if (!cli_state_is_connected(cli)) {
302 DEBUG(1,("password server is not connected (cli not initialised)\n"));
303 return NT_STATUS_LOGON_FAILURE;
306 sec_mode = cli_state_security_mode(cli);
307 if ((sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) == 0) {
308 if (user_info->password_state != AUTH_PASSWORD_PLAIN) {
309 DEBUG(1,("password server %s is plaintext, but we are encrypted. This just can't work :-(\n", cli_state_remote_name(cli)));
310 return NT_STATUS_LOGON_FAILURE;
312 } else {
313 const uint8_t *server_challenge = cli_state_server_challenge(cli);
315 if (memcmp(server_challenge, auth_context->challenge.data, 8) != 0) {
316 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_state_remote_name(cli)));
317 return NT_STATUS_LOGON_FAILURE;
322 * Attempt a session setup with a totally incorrect password.
323 * If this succeeds with the guest bit *NOT* set then the password
324 * server is broken and is not correctly setting the guest bit. We
325 * need to detect this as some versions of NT4.x are broken. JRA.
328 /* I sure as hell hope that there aren't servers out there that take
329 * NTLMv2 and have this bug, as we don't test for that...
330 * - abartlet@samba.org
333 if ((!tested_password_server) && (lp_paranoid_server_security())) {
334 unsigned char badpass[24];
335 char *baduser = NULL;
337 memset(badpass, 0x1f, sizeof(badpass));
339 if((user_info->password.response.nt.length == sizeof(badpass)) &&
340 !memcmp(badpass, user_info->password.response.nt.data, sizeof(badpass))) {
342 * Very unlikely, our random bad password is the same as the users
343 * password.
345 memset(badpass, badpass[0]+1, sizeof(badpass));
348 baduser = talloc_asprintf(mem_ctx,
349 "%s%s",
350 INVALID_USER_PREFIX,
351 lp_netbios_name());
352 if (!baduser) {
353 return NT_STATUS_NO_MEMORY;
356 if (NT_STATUS_IS_OK(cli_session_setup(cli, baduser,
357 (char *)badpass,
358 sizeof(badpass),
359 (char *)badpass,
360 sizeof(badpass),
361 user_info->mapped.domain_name))) {
364 * We connected to the password server so we
365 * can say we've tested it.
367 tested_password_server = True;
369 if (!cli->is_guestlogin) {
370 DEBUG(0,("server_validate: password server %s allows users as non-guest \
371 with a bad password.\n", cli_state_remote_name(cli)));
372 DEBUG(0,("server_validate: This is broken (and insecure) behaviour. Please do not \
373 use this machine as the password server.\n"));
374 cli_ulogoff(cli);
377 * Password server has the bug.
379 bad_password_server = True;
380 return NT_STATUS_LOGON_FAILURE;
382 cli_ulogoff(cli);
384 } else {
387 * We have already tested the password server.
388 * Fail immediately if it has the bug.
391 if(bad_password_server) {
392 DEBUG(0,("server_validate: [1] password server %s allows users as non-guest \
393 with a bad password.\n", cli_state_remote_name(cli)));
394 DEBUG(0,("server_validate: [1] This is broken (and insecure) behaviour. Please do not \
395 use this machine as the password server.\n"));
396 return NT_STATUS_LOGON_FAILURE;
401 * Now we know the password server will correctly set the guest bit, or is
402 * not guest enabled, we can try with the real password.
404 switch (user_info->password_state) {
405 case AUTH_PASSWORD_PLAIN:
406 /* Plaintext available */
407 nt_status = cli_session_setup(
408 cli, user_info->client.account_name,
409 user_info->password.plaintext,
410 strlen(user_info->password.plaintext),
411 NULL, 0, user_info->mapped.domain_name);
412 break;
414 /* currently the hash values include a challenge-response as well */
415 case AUTH_PASSWORD_HASH:
416 case AUTH_PASSWORD_RESPONSE:
417 nt_status = cli_session_setup(
418 cli, user_info->client.account_name,
419 (char *)user_info->password.response.lanman.data,
420 user_info->password.response.lanman.length,
421 (char *)user_info->password.response.nt.data,
422 user_info->password.response.nt.length,
423 user_info->mapped.domain_name);
424 break;
425 default:
426 DEBUG(0,("user_info constructed for user '%s' was invalid - password_state=%u invalid.\n",user_info->mapped.account_name, user_info->password_state));
427 nt_status = NT_STATUS_INTERNAL_ERROR;
430 if (!NT_STATUS_IS_OK(nt_status)) {
431 DEBUG(1,("password server %s rejected the password: %s\n",
432 cli_state_remote_name(cli), nt_errstr(nt_status)));
435 /* if logged in as guest then reject */
436 if (cli->is_guestlogin) {
437 DEBUG(1,("password server %s gave us guest only\n",
438 cli_state_remote_name(cli)));
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(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);