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/>.
23 #include "system/passwd.h"
24 #include "smbd/smbd.h"
25 #include "libsmb/libsmb.h"
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
;
43 bool connected_ok
= False
;
44 struct named_mutex
*mutex
= NULL
;
47 pserver
= talloc_strdup(mem_ctx
, lp_passwordserver());
50 while(next_token_talloc(mem_ctx
, &p
, &desthost
, LIST_SEP
)) {
52 desthost
= talloc_sub_basic(mem_ctx
,
53 current_user_info
.smb_name
,
54 current_user_info
.domain
,
61 if (strequal(desthost
, myhostname())) {
62 DEBUG(1,("Password server loop - disabling "
63 "password server %s\n", desthost
));
67 if(!resolve_name( desthost
, &dest_ss
, 0x20, false)) {
68 DEBUG(1,("server_cryptkey: Can't resolve address for %s\n",desthost
));
72 if (ismyaddr((struct sockaddr
*)(void *)&dest_ss
)) {
73 DEBUG(1,("Password server loop - disabling password server %s\n",desthost
));
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);
87 status
= cli_connect_nb(desthost
, &dest_ss
, 0, 0x20,
88 lp_netbios_name(), Undefined
, &cli
);
89 if (NT_STATUS_IS_OK(status
)) {
90 DEBUG(3,("connected to password server %s\n",desthost
));
94 DEBUG(10,("server_cryptkey: failed to connect to server %s. Error %s\n",
95 desthost
, nt_errstr(status
) ));
100 DEBUG(0,("password server not available\n"));
104 /* security = server just can't function with spnego */
105 cli
->use_spnego
= False
;
107 DEBUG(3,("got session\n"));
109 status
= cli_negprot(cli
);
111 if (!NT_STATUS_IS_OK(status
)) {
113 DEBUG(1, ("%s rejected the negprot: %s\n",
114 desthost
, nt_errstr(status
)));
119 if (cli
->protocol
< PROTOCOL_LANMAN2
||
120 !(cli
->sec_mode
& NEGOTIATE_SECURITY_USER_LEVEL
)) {
122 DEBUG(1,("%s isn't in user level security mode\n",desthost
));
127 /* Get the first session setup done quickly, to avoid silly
128 Win2k bugs. (The next connection to the server will kill
132 status
= cli_session_setup(cli
, "", "", 0, "", 0, "");
133 if (!NT_STATUS_IS_OK(status
)) {
135 DEBUG(0,("%s rejected the initial session setup (%s)\n",
136 desthost
, nt_errstr(status
)));
143 DEBUG(3,("password server OK\n"));
148 struct server_security_state
{
149 struct cli_state
*cli
;
152 /****************************************************************************
153 Send a 'keepalive' packet down the cli pipe.
154 ****************************************************************************/
156 static bool send_server_keepalive(const struct timeval
*now
,
159 struct server_security_state
*state
= talloc_get_type_abort(
160 private_data
, struct server_security_state
);
162 unsigned char garbage
[16];
164 if (!cli_state_is_connected(state
->cli
)) {
168 /* Ping the server to keep the connection alive using SMBecho. */
169 memset(garbage
, 0xf0, sizeof(garbage
));
170 status
= cli_echo(state
->cli
, 1, data_blob_const(garbage
, sizeof(garbage
)));
171 if (NT_STATUS_IS_OK(status
)) {
175 DEBUG(2,("send_server_keepalive: password server SMBecho failed: %s\n",
177 cli_shutdown(state
->cli
);
182 static int destroy_server_security(struct server_security_state
*state
)
185 cli_shutdown(state
->cli
);
190 static struct server_security_state
*make_server_security_state(struct cli_state
*cli
)
192 struct server_security_state
*result
;
194 if (!(result
= talloc(NULL
, struct server_security_state
))) {
195 DEBUG(0, ("talloc failed\n"));
201 talloc_set_destructor(result
, destroy_server_security
);
203 if (lp_keepalive() != 0) {
204 struct timeval interval
;
205 interval
.tv_sec
= lp_keepalive();
206 interval
.tv_usec
= 0;
208 if (event_add_idle(server_event_context(), result
, interval
,
209 "server_security_keepalive",
210 send_server_keepalive
,
212 DEBUG(0, ("event_add_idle failed\n"));
221 /****************************************************************************
222 Get the challenge out of a password server.
223 ****************************************************************************/
225 static DATA_BLOB
auth_get_challenge_server(const struct auth_context
*auth_context
,
226 void **my_private_data
,
229 struct cli_state
*cli
= server_cryptkey(mem_ctx
);
232 DEBUG(3,("using password server validation\n"));
234 if ((cli
->sec_mode
& NEGOTIATE_SECURITY_CHALLENGE_RESPONSE
) == 0) {
235 /* We can't work with unencrypted password servers
236 unless 'encrypt passwords = no' */
237 DEBUG(5,("make_auth_info_server: Server is unencrypted, no challenge available..\n"));
239 /* However, it is still a perfectly fine connection
240 to pass that unencrypted password over */
242 (void *)make_server_security_state(cli
);
243 return data_blob_null
;
244 } else if (cli
->secblob
.length
< 8) {
245 /* We can't do much if we don't get a full challenge */
246 DEBUG(2,("make_auth_info_server: Didn't receive a full challenge from server\n"));
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
), cli
->secblob
.data
,8);
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
,
272 const struct auth_usersupplied_info
*user_info
,
273 struct auth_serversupplied_info
**server_info
)
275 struct server_security_state
*state
= talloc_get_type_abort(
276 my_private_data
, struct server_security_state
);
277 struct cli_state
*cli
;
278 static bool tested_password_server
= False
;
279 static bool bad_password_server
= False
;
280 NTSTATUS nt_status
= NT_STATUS_NOT_IMPLEMENTED
;
281 bool locally_made_cli
= False
;
283 DEBUG(10, ("Check auth for: [%s]\n", user_info
->mapped
.account_name
));
289 cli
= server_cryptkey(mem_ctx
);
290 locally_made_cli
= True
;
293 if (!cli_state_is_connected(cli
)) {
294 DEBUG(1,("password server is not connected (cli not initialised)\n"));
295 return NT_STATUS_LOGON_FAILURE
;
298 if ((cli
->sec_mode
& NEGOTIATE_SECURITY_CHALLENGE_RESPONSE
) == 0) {
299 if (user_info
->password_state
!= AUTH_PASSWORD_PLAIN
) {
300 DEBUG(1,("password server %s is plaintext, but we are encrypted. This just can't work :-(\n", cli_state_remote_name(cli
)));
301 return NT_STATUS_LOGON_FAILURE
;
304 if (memcmp(cli
->secblob
.data
, auth_context
->challenge
.data
, 8) != 0) {
305 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
)));
306 return NT_STATUS_LOGON_FAILURE
;
311 * Attempt a session setup with a totally incorrect password.
312 * If this succeeds with the guest bit *NOT* set then the password
313 * server is broken and is not correctly setting the guest bit. We
314 * need to detect this as some versions of NT4.x are broken. JRA.
317 /* I sure as hell hope that there aren't servers out there that take
318 * NTLMv2 and have this bug, as we don't test for that...
319 * - abartlet@samba.org
322 if ((!tested_password_server
) && (lp_paranoid_server_security())) {
323 unsigned char badpass
[24];
324 char *baduser
= NULL
;
326 memset(badpass
, 0x1f, sizeof(badpass
));
328 if((user_info
->password
.response
.nt
.length
== sizeof(badpass
)) &&
329 !memcmp(badpass
, user_info
->password
.response
.nt
.data
, sizeof(badpass
))) {
331 * Very unlikely, our random bad password is the same as the users
334 memset(badpass
, badpass
[0]+1, sizeof(badpass
));
337 baduser
= talloc_asprintf(mem_ctx
,
342 return NT_STATUS_NO_MEMORY
;
345 if (NT_STATUS_IS_OK(cli_session_setup(cli
, baduser
,
350 user_info
->mapped
.domain_name
))) {
353 * We connected to the password server so we
354 * can say we've tested it.
356 tested_password_server
= True
;
358 if (!cli
->is_guestlogin
) {
359 DEBUG(0,("server_validate: password server %s allows users as non-guest \
360 with a bad password.\n", cli_state_remote_name(cli
)));
361 DEBUG(0,("server_validate: This is broken (and insecure) behaviour. Please do not \
362 use this machine as the password server.\n"));
366 * Password server has the bug.
368 bad_password_server
= True
;
369 return NT_STATUS_LOGON_FAILURE
;
376 * We have already tested the password server.
377 * Fail immediately if it has the bug.
380 if(bad_password_server
) {
381 DEBUG(0,("server_validate: [1] password server %s allows users as non-guest \
382 with a bad password.\n", cli_state_remote_name(cli
)));
383 DEBUG(0,("server_validate: [1] This is broken (and insecure) behaviour. Please do not \
384 use this machine as the password server.\n"));
385 return NT_STATUS_LOGON_FAILURE
;
390 * Now we know the password server will correctly set the guest bit, or is
391 * not guest enabled, we can try with the real password.
393 switch (user_info
->password_state
) {
394 case AUTH_PASSWORD_PLAIN
:
395 /* Plaintext available */
396 nt_status
= cli_session_setup(
397 cli
, user_info
->client
.account_name
,
398 user_info
->password
.plaintext
,
399 strlen(user_info
->password
.plaintext
),
400 NULL
, 0, user_info
->mapped
.domain_name
);
403 /* currently the hash values include a challenge-response as well */
404 case AUTH_PASSWORD_HASH
:
405 case AUTH_PASSWORD_RESPONSE
:
406 nt_status
= cli_session_setup(
407 cli
, user_info
->client
.account_name
,
408 (char *)user_info
->password
.response
.lanman
.data
,
409 user_info
->password
.response
.lanman
.length
,
410 (char *)user_info
->password
.response
.nt
.data
,
411 user_info
->password
.response
.nt
.length
,
412 user_info
->mapped
.domain_name
);
415 DEBUG(0,("user_info constructed for user '%s' was invalid - password_state=%u invalid.\n",user_info
->mapped
.account_name
, user_info
->password_state
));
416 nt_status
= NT_STATUS_INTERNAL_ERROR
;
419 if (!NT_STATUS_IS_OK(nt_status
)) {
420 DEBUG(1,("password server %s rejected the password: %s\n",
421 cli_state_remote_name(cli
), nt_errstr(nt_status
)));
424 /* if logged in as guest then reject */
425 if (cli
->is_guestlogin
) {
426 DEBUG(1,("password server %s gave us guest only\n",
427 cli_state_remote_name(cli
)));
428 nt_status
= NT_STATUS_LOGON_FAILURE
;
433 if (NT_STATUS_IS_OK(nt_status
)) {
434 char *real_username
= NULL
;
435 struct passwd
*pass
= NULL
;
437 if ( (pass
= smb_getpwnam(talloc_tos(), user_info
->mapped
.account_name
,
438 &real_username
, True
)) != NULL
)
440 nt_status
= make_server_info_pw(server_info
, pass
->pw_name
, pass
);
442 TALLOC_FREE(real_username
);
446 nt_status
= NT_STATUS_NO_SUCH_USER
;
450 if (locally_made_cli
) {
457 static NTSTATUS
auth_init_smbserver(struct auth_context
*auth_context
, const char* param
, auth_methods
**auth_method
)
459 struct auth_methods
*result
;
461 result
= talloc_zero(auth_context
, struct auth_methods
);
462 if (result
== NULL
) {
463 return NT_STATUS_NO_MEMORY
;
465 result
->name
= "smbserver";
466 result
->auth
= check_smbserver_security
;
467 result
->get_chal
= auth_get_challenge_server
;
469 *auth_method
= result
;
473 NTSTATUS
auth_server_init(void)
475 return smb_register_auth(AUTH_INTERFACE_VERSION
, "smbserver", auth_init_smbserver
);