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"
27 #define DBGC_CLASS DBGC_AUTH
29 extern userdom_struct current_user_info
;
31 /****************************************************************************
32 Support for server level security.
33 ****************************************************************************/
35 static struct cli_state
*server_cryptkey(TALLOC_CTX
*mem_ctx
)
37 struct cli_state
*cli
= NULL
;
38 char *desthost
= NULL
;
39 struct sockaddr_storage dest_ss
;
42 bool connected_ok
= False
;
43 struct named_mutex
*mutex
= NULL
;
46 if (!(cli
= cli_initialise()))
49 /* security = server just can't function with spnego */
50 cli
->use_spnego
= False
;
52 pserver
= talloc_strdup(mem_ctx
, lp_passwordserver());
55 while(next_token_talloc(mem_ctx
, &p
, &desthost
, LIST_SEP
)) {
57 desthost
= talloc_sub_basic(mem_ctx
,
58 current_user_info
.smb_name
,
59 current_user_info
.domain
,
66 if(!resolve_name( desthost
, &dest_ss
, 0x20, false)) {
67 DEBUG(1,("server_cryptkey: Can't resolve address for %s\n",desthost
));
71 if (ismyaddr((struct sockaddr
*)&dest_ss
)) {
72 DEBUG(1,("Password server loop - disabling password server %s\n",desthost
));
76 /* we use a mutex to prevent two connections at once - when a
77 Win2k PDC get two connections where one hasn't completed a
78 session setup yet it will send a TCP reset to the first
79 connection (tridge) */
81 mutex
= grab_named_mutex(talloc_tos(), desthost
, 10);
87 status
= cli_connect(cli
, desthost
, &dest_ss
);
88 if (NT_STATUS_IS_OK(status
)) {
89 DEBUG(3,("connected to password server %s\n",desthost
));
93 DEBUG(10,("server_cryptkey: failed to connect to server %s. Error %s\n",
94 desthost
, nt_errstr(status
) ));
99 DEBUG(0,("password server not available\n"));
104 if (!attempt_netbios_session_request(&cli
, global_myname(),
105 desthost
, &dest_ss
)) {
107 DEBUG(1,("password server fails session request\n"));
112 if (strequal(desthost
,myhostname())) {
113 exit_server_cleanly("Password server loop!");
116 DEBUG(3,("got session\n"));
118 status
= cli_negprot(cli
);
120 if (!NT_STATUS_IS_OK(status
)) {
122 DEBUG(1, ("%s rejected the negprot: %s\n",
123 desthost
, nt_errstr(status
)));
128 if (cli
->protocol
< PROTOCOL_LANMAN2
||
129 !(cli
->sec_mode
& NEGOTIATE_SECURITY_USER_LEVEL
)) {
131 DEBUG(1,("%s isn't in user level security mode\n",desthost
));
136 /* Get the first session setup done quickly, to avoid silly
137 Win2k bugs. (The next connection to the server will kill
141 status
= cli_session_setup(cli
, "", "", 0, "", 0, "");
142 if (!NT_STATUS_IS_OK(status
)) {
144 DEBUG(0,("%s rejected the initial session setup (%s)\n",
145 desthost
, nt_errstr(status
)));
152 DEBUG(3,("password server OK\n"));
157 struct server_security_state
{
158 struct cli_state
*cli
;
161 /****************************************************************************
162 Send a 'keepalive' packet down the cli pipe.
163 ****************************************************************************/
165 static bool send_server_keepalive(const struct timeval
*now
,
168 struct server_security_state
*state
= talloc_get_type_abort(
169 private_data
, struct server_security_state
);
171 if (!state
->cli
|| !state
->cli
->initialised
) {
175 if (send_keepalive(state
->cli
->fd
)) {
179 DEBUG( 2, ( "send_server_keepalive: password server keepalive "
181 cli_shutdown(state
->cli
);
186 static int destroy_server_security(struct server_security_state
*state
)
189 cli_shutdown(state
->cli
);
194 static struct server_security_state
*make_server_security_state(struct cli_state
*cli
)
196 struct server_security_state
*result
;
198 if (!(result
= talloc(NULL
, struct server_security_state
))) {
199 DEBUG(0, ("talloc failed\n"));
205 talloc_set_destructor(result
, destroy_server_security
);
207 if (lp_keepalive() != 0) {
208 struct timeval interval
;
209 interval
.tv_sec
= lp_keepalive();
210 interval
.tv_usec
= 0;
212 if (event_add_idle(server_event_context(), result
, interval
,
213 "server_security_keepalive",
214 send_server_keepalive
,
216 DEBUG(0, ("event_add_idle failed\n"));
225 /****************************************************************************
226 Get the challenge out of a password server.
227 ****************************************************************************/
229 static DATA_BLOB
auth_get_challenge_server(const struct auth_context
*auth_context
,
230 void **my_private_data
,
233 struct cli_state
*cli
= server_cryptkey(mem_ctx
);
236 DEBUG(3,("using password server validation\n"));
238 if ((cli
->sec_mode
& NEGOTIATE_SECURITY_CHALLENGE_RESPONSE
) == 0) {
239 /* We can't work with unencrypted password servers
240 unless 'encrypt passwords = no' */
241 DEBUG(5,("make_auth_info_server: Server is unencrypted, no challenge available..\n"));
243 /* However, it is still a perfectly fine connection
244 to pass that unencrypted password over */
246 (void *)make_server_security_state(cli
);
247 return data_blob_null
;
248 } else if (cli
->secblob
.length
< 8) {
249 /* We can't do much if we don't get a full challenge */
250 DEBUG(2,("make_auth_info_server: Didn't receive a full challenge from server\n"));
252 return data_blob_null
;
255 if (!(*my_private_data
= (void *)make_server_security_state(cli
))) {
256 return data_blob(NULL
,0);
259 /* The return must be allocated on the caller's mem_ctx, as our own will be
260 destoyed just after the call. */
261 return data_blob_talloc((TALLOC_CTX
*)auth_context
, cli
->secblob
.data
,8);
263 return data_blob_null
;
268 /****************************************************************************
269 Check for a valid username and password in security=server mode.
270 - Validate a password with the password server.
271 ****************************************************************************/
273 static NTSTATUS
check_smbserver_security(const struct auth_context
*auth_context
,
274 void *my_private_data
,
276 const struct auth_usersupplied_info
*user_info
,
277 struct auth_serversupplied_info
**server_info
)
279 struct server_security_state
*state
= talloc_get_type_abort(
280 my_private_data
, struct server_security_state
);
281 struct cli_state
*cli
;
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 auth for: [%s]\n", user_info
->mapped
.account_name
));
293 cli
= server_cryptkey(mem_ctx
);
294 locally_made_cli
= True
;
297 if (!cli
|| !cli
->initialised
) {
298 DEBUG(1,("password server is not connected (cli not initialised)\n"));
299 return NT_STATUS_LOGON_FAILURE
;
302 if ((cli
->sec_mode
& NEGOTIATE_SECURITY_CHALLENGE_RESPONSE
) == 0) {
303 if (user_info
->password_state
!= AUTH_PASSWORD_PLAIN
) {
304 DEBUG(1,("password server %s is plaintext, but we are encrypted. This just can't work :-(\n", cli
->desthost
));
305 return NT_STATUS_LOGON_FAILURE
;
308 if (memcmp(cli
->secblob
.data
, auth_context
->challenge
.data
, 8) != 0) {
309 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
));
310 return NT_STATUS_LOGON_FAILURE
;
315 * Attempt a session setup with a totally incorrect password.
316 * If this succeeds with the guest bit *NOT* set then the password
317 * server is broken and is not correctly setting the guest bit. We
318 * need to detect this as some versions of NT4.x are broken. JRA.
321 /* I sure as hell hope that there aren't servers out there that take
322 * NTLMv2 and have this bug, as we don't test for that...
323 * - abartlet@samba.org
326 if ((!tested_password_server
) && (lp_paranoid_server_security())) {
327 unsigned char badpass
[24];
328 char *baduser
= NULL
;
330 memset(badpass
, 0x1f, sizeof(badpass
));
332 if((user_info
->password
.response
.nt
.length
== sizeof(badpass
)) &&
333 !memcmp(badpass
, user_info
->password
.response
.nt
.data
, sizeof(badpass
))) {
335 * Very unlikely, our random bad password is the same as the users
338 memset(badpass
, badpass
[0]+1, sizeof(badpass
));
341 baduser
= talloc_asprintf(mem_ctx
,
346 return NT_STATUS_NO_MEMORY
;
349 if (NT_STATUS_IS_OK(cli_session_setup(cli
, baduser
,
354 user_info
->mapped
.domain_name
))) {
357 * We connected to the password server so we
358 * can say we've tested it.
360 tested_password_server
= True
;
362 if ((SVAL(cli
->inbuf
,smb_vwv2
) & 1) == 0) {
363 DEBUG(0,("server_validate: password server %s allows users as non-guest \
364 with a bad password.\n", cli
->desthost
));
365 DEBUG(0,("server_validate: This is broken (and insecure) behaviour. Please do not \
366 use this machine as the password server.\n"));
370 * Password server has the bug.
372 bad_password_server
= True
;
373 return NT_STATUS_LOGON_FAILURE
;
380 * We have already tested the password server.
381 * Fail immediately if it has the bug.
384 if(bad_password_server
) {
385 DEBUG(0,("server_validate: [1] password server %s allows users as non-guest \
386 with a bad password.\n", cli
->desthost
));
387 DEBUG(0,("server_validate: [1] This is broken (and insecure) behaviour. Please do not \
388 use this machine as the password server.\n"));
389 return NT_STATUS_LOGON_FAILURE
;
394 * Now we know the password server will correctly set the guest bit, or is
395 * not guest enabled, we can try with the real password.
397 switch (user_info
->password_state
) {
398 case AUTH_PASSWORD_PLAIN
:
399 /* Plaintext available */
400 nt_status
= cli_session_setup(
401 cli
, user_info
->client
.account_name
,
402 user_info
->password
.plaintext
,
403 strlen(user_info
->password
.plaintext
),
404 NULL
, 0, user_info
->mapped
.domain_name
);
407 /* currently the hash values include a challenge-response as well */
408 case AUTH_PASSWORD_HASH
:
409 case AUTH_PASSWORD_RESPONSE
:
410 nt_status
= cli_session_setup(
411 cli
, user_info
->client
.account_name
,
412 (char *)user_info
->password
.response
.lanman
.data
,
413 user_info
->password
.response
.lanman
.length
,
414 (char *)user_info
->password
.response
.nt
.data
,
415 user_info
->password
.response
.nt
.length
,
416 user_info
->mapped
.domain_name
);
419 DEBUG(0,("user_info constructed for user '%s' was invalid - password_state=%u invalid.\n",user_info
->mapped
.account_name
, user_info
->password_state
));
420 nt_status
= NT_STATUS_INTERNAL_ERROR
;
423 if (!NT_STATUS_IS_OK(nt_status
)) {
424 DEBUG(1,("password server %s rejected the password: %s\n",
425 cli
->desthost
, nt_errstr(nt_status
)));
428 /* if logged in as guest then reject */
429 if ((SVAL(cli
->inbuf
,smb_vwv2
) & 1) != 0) {
430 DEBUG(1,("password server %s gave us guest only\n", cli
->desthost
));
431 nt_status
= NT_STATUS_LOGON_FAILURE
;
436 if (NT_STATUS_IS_OK(nt_status
)) {
437 char *real_username
= NULL
;
438 struct passwd
*pass
= NULL
;
440 if ( (pass
= smb_getpwnam(talloc_tos(), user_info
->mapped
.account_name
,
441 &real_username
, True
)) != NULL
)
443 nt_status
= make_server_info_pw(server_info
, pass
->pw_name
, pass
);
445 TALLOC_FREE(real_username
);
449 nt_status
= NT_STATUS_NO_SUCH_USER
;
453 if (locally_made_cli
) {
460 static NTSTATUS
auth_init_smbserver(struct auth_context
*auth_context
, const char* param
, auth_methods
**auth_method
)
462 struct auth_methods
*result
;
464 result
= TALLOC_ZERO_P(auth_context
, struct auth_methods
);
465 if (result
== NULL
) {
466 return NT_STATUS_NO_MEMORY
;
468 result
->name
= "smbserver";
469 result
->auth
= check_smbserver_security
;
470 result
->get_chal
= auth_get_challenge_server
;
472 *auth_method
= result
;
476 NTSTATUS
auth_server_init(void)
478 return smb_register_auth(AUTH_INTERFACE_VERSION
, "smbserver", auth_init_smbserver
);