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/>.
22 #include "system/passwd.h"
25 #define DBGC_CLASS DBGC_AUTH
27 extern userdom_struct current_user_info
;
29 /****************************************************************************
30 Support for server level security.
31 ****************************************************************************/
33 static struct cli_state
*server_cryptkey(TALLOC_CTX
*mem_ctx
)
35 struct cli_state
*cli
= NULL
;
36 char *desthost
= NULL
;
37 struct sockaddr_storage dest_ss
;
40 bool connected_ok
= False
;
41 struct named_mutex
*mutex
= NULL
;
44 if (!(cli
= cli_initialise()))
47 /* security = server just can't function with spnego */
48 cli
->use_spnego
= False
;
50 pserver
= talloc_strdup(mem_ctx
, lp_passwordserver());
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
,
64 if(!resolve_name( desthost
, &dest_ss
, 0x20, false)) {
65 DEBUG(1,("server_cryptkey: Can't resolve address for %s\n",desthost
));
69 if (ismyaddr((struct sockaddr
*)&dest_ss
)) {
70 DEBUG(1,("Password server loop - disabling password server %s\n",desthost
));
74 /* we use a mutex to prevent two connections at once - when a
75 Win2k PDC get two connections where one hasn't completed a
76 session setup yet it will send a TCP reset to the first
77 connection (tridge) */
79 mutex
= grab_named_mutex(talloc_tos(), desthost
, 10);
85 status
= cli_connect(cli
, desthost
, &dest_ss
);
86 if (NT_STATUS_IS_OK(status
)) {
87 DEBUG(3,("connected to password server %s\n",desthost
));
91 DEBUG(10,("server_cryptkey: failed to connect to server %s. Error %s\n",
92 desthost
, nt_errstr(status
) ));
97 DEBUG(0,("password server not available\n"));
102 if (!attempt_netbios_session_request(&cli
, global_myname(),
103 desthost
, &dest_ss
)) {
105 DEBUG(1,("password server fails session request\n"));
110 if (strequal(desthost
,myhostname())) {
111 exit_server_cleanly("Password server loop!");
114 DEBUG(3,("got session\n"));
116 status
= cli_negprot(cli
);
118 if (!NT_STATUS_IS_OK(status
)) {
120 DEBUG(1, ("%s rejected the negprot: %s\n",
121 desthost
, nt_errstr(status
)));
126 if (cli
->protocol
< PROTOCOL_LANMAN2
||
127 !(cli
->sec_mode
& NEGOTIATE_SECURITY_USER_LEVEL
)) {
129 DEBUG(1,("%s isn't in user level security mode\n",desthost
));
134 /* Get the first session setup done quickly, to avoid silly
135 Win2k bugs. (The next connection to the server will kill
139 status
= cli_session_setup(cli
, "", "", 0, "", 0, "");
140 if (!NT_STATUS_IS_OK(status
)) {
142 DEBUG(0,("%s rejected the initial session setup (%s)\n",
143 desthost
, nt_errstr(status
)));
150 DEBUG(3,("password server OK\n"));
155 struct server_security_state
{
156 struct cli_state
*cli
;
159 /****************************************************************************
160 Send a 'keepalive' packet down the cli pipe.
161 ****************************************************************************/
163 static bool send_server_keepalive(const struct timeval
*now
,
166 struct server_security_state
*state
= talloc_get_type_abort(
167 private_data
, struct server_security_state
);
169 if (!state
->cli
|| !state
->cli
->initialised
) {
173 if (send_keepalive(state
->cli
->fd
)) {
177 DEBUG( 2, ( "send_server_keepalive: password server keepalive "
179 cli_shutdown(state
->cli
);
184 static int destroy_server_security(struct server_security_state
*state
)
187 cli_shutdown(state
->cli
);
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"));
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
,
214 DEBUG(0, ("event_add_idle failed\n"));
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
,
231 struct cli_state
*cli
= server_cryptkey(mem_ctx
);
234 DEBUG(3,("using password server validation\n"));
236 if ((cli
->sec_mode
& NEGOTIATE_SECURITY_CHALLENGE_RESPONSE
) == 0) {
237 /* We can't work with unencrypted password servers
238 unless 'encrypt passwords = no' */
239 DEBUG(5,("make_auth_info_server: Server is unencrypted, no challenge available..\n"));
241 /* However, it is still a perfectly fine connection
242 to pass that unencrypted password over */
244 (void *)make_server_security_state(cli
);
245 return data_blob_null
;
246 } else if (cli
->secblob
.length
< 8) {
247 /* We can't do much if we don't get a full challenge */
248 DEBUG(2,("make_auth_info_server: Didn't receive a full challenge from server\n"));
250 return data_blob_null
;
253 if (!(*my_private_data
= (void *)make_server_security_state(cli
))) {
254 return data_blob(NULL
,0);
257 /* The return must be allocated on the caller's mem_ctx, as our own will be
258 destoyed just after the call. */
259 return data_blob_talloc((TALLOC_CTX
*)auth_context
, cli
->secblob
.data
,8);
261 return data_blob_null
;
266 /****************************************************************************
267 Check for a valid username and password in security=server mode.
268 - Validate a password with the password server.
269 ****************************************************************************/
271 static NTSTATUS
check_smbserver_security(const struct auth_context
*auth_context
,
272 void *my_private_data
,
274 const struct auth_usersupplied_info
*user_info
,
275 struct auth_serversupplied_info
**server_info
)
277 struct server_security_state
*state
= talloc_get_type_abort(
278 my_private_data
, struct server_security_state
);
279 struct cli_state
*cli
;
280 static bool tested_password_server
= False
;
281 static bool bad_password_server
= False
;
282 NTSTATUS nt_status
= NT_STATUS_NOT_IMPLEMENTED
;
283 bool locally_made_cli
= False
;
285 DEBUG(10, ("Check auth for: [%s]\n", user_info
->mapped
.account_name
));
291 cli
= server_cryptkey(mem_ctx
);
292 locally_made_cli
= True
;
295 if (!cli
|| !cli
->initialised
) {
296 DEBUG(1,("password server is not connected (cli not initialised)\n"));
297 return NT_STATUS_LOGON_FAILURE
;
300 if ((cli
->sec_mode
& NEGOTIATE_SECURITY_CHALLENGE_RESPONSE
) == 0) {
301 if (user_info
->password_state
!= AUTH_PASSWORD_PLAIN
) {
302 DEBUG(1,("password server %s is plaintext, but we are encrypted. This just can't work :-(\n", cli
->desthost
));
303 return NT_STATUS_LOGON_FAILURE
;
306 if (memcmp(cli
->secblob
.data
, auth_context
->challenge
.data
, 8) != 0) {
307 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
));
308 return NT_STATUS_LOGON_FAILURE
;
313 * Attempt a session setup with a totally incorrect password.
314 * If this succeeds with the guest bit *NOT* set then the password
315 * server is broken and is not correctly setting the guest bit. We
316 * need to detect this as some versions of NT4.x are broken. JRA.
319 /* I sure as hell hope that there aren't servers out there that take
320 * NTLMv2 and have this bug, as we don't test for that...
321 * - abartlet@samba.org
324 if ((!tested_password_server
) && (lp_paranoid_server_security())) {
325 unsigned char badpass
[24];
326 char *baduser
= NULL
;
328 memset(badpass
, 0x1f, sizeof(badpass
));
330 if((user_info
->password
.response
.nt
.length
== sizeof(badpass
)) &&
331 !memcmp(badpass
, user_info
->password
.response
.nt
.data
, sizeof(badpass
))) {
333 * Very unlikely, our random bad password is the same as the users
336 memset(badpass
, badpass
[0]+1, sizeof(badpass
));
339 baduser
= talloc_asprintf(mem_ctx
,
344 return NT_STATUS_NO_MEMORY
;
347 if (NT_STATUS_IS_OK(cli_session_setup(cli
, baduser
,
352 user_info
->mapped
.domain_name
))) {
355 * We connected to the password server so we
356 * can say we've tested it.
358 tested_password_server
= True
;
360 if ((SVAL(cli
->inbuf
,smb_vwv2
) & 1) == 0) {
361 DEBUG(0,("server_validate: password server %s allows users as non-guest \
362 with a bad password.\n", cli
->desthost
));
363 DEBUG(0,("server_validate: This is broken (and insecure) behaviour. Please do not \
364 use this machine as the password server.\n"));
368 * Password server has the bug.
370 bad_password_server
= True
;
371 return NT_STATUS_LOGON_FAILURE
;
378 * We have already tested the password server.
379 * Fail immediately if it has the bug.
382 if(bad_password_server
) {
383 DEBUG(0,("server_validate: [1] password server %s allows users as non-guest \
384 with a bad password.\n", cli
->desthost
));
385 DEBUG(0,("server_validate: [1] This is broken (and insecure) behaviour. Please do not \
386 use this machine as the password server.\n"));
387 return NT_STATUS_LOGON_FAILURE
;
392 * Now we know the password server will correctly set the guest bit, or is
393 * not guest enabled, we can try with the real password.
395 switch (user_info
->password_state
) {
396 case AUTH_PASSWORD_PLAIN
:
397 /* Plaintext available */
398 nt_status
= cli_session_setup(
399 cli
, user_info
->client
.account_name
,
400 user_info
->password
.plaintext
,
401 strlen(user_info
->password
.plaintext
),
402 NULL
, 0, user_info
->mapped
.domain_name
);
405 /* currently the hash values include a challenge-response as well */
406 case AUTH_PASSWORD_HASH
:
407 case AUTH_PASSWORD_RESPONSE
:
408 nt_status
= cli_session_setup(
409 cli
, user_info
->client
.account_name
,
410 (char *)user_info
->password
.response
.lanman
.data
,
411 user_info
->password
.response
.lanman
.length
,
412 (char *)user_info
->password
.response
.nt
.data
,
413 user_info
->password
.response
.nt
.length
,
414 user_info
->mapped
.domain_name
);
417 DEBUG(0,("user_info constructed for user '%s' was invalid - password_state=%u invalid.\n",user_info
->mapped
.account_name
, user_info
->password_state
));
418 nt_status
= NT_STATUS_INTERNAL_ERROR
;
421 if (!NT_STATUS_IS_OK(nt_status
)) {
422 DEBUG(1,("password server %s rejected the password: %s\n",
423 cli
->desthost
, nt_errstr(nt_status
)));
426 /* if logged in as guest then reject */
427 if ((SVAL(cli
->inbuf
,smb_vwv2
) & 1) != 0) {
428 DEBUG(1,("password server %s gave us guest only\n", cli
->desthost
));
429 nt_status
= NT_STATUS_LOGON_FAILURE
;
434 if (NT_STATUS_IS_OK(nt_status
)) {
435 char *real_username
= NULL
;
436 struct passwd
*pass
= NULL
;
438 if ( (pass
= smb_getpwnam(talloc_tos(), user_info
->mapped
.account_name
,
439 &real_username
, True
)) != NULL
)
441 nt_status
= make_server_info_pw(server_info
, pass
->pw_name
, pass
);
443 TALLOC_FREE(real_username
);
447 nt_status
= NT_STATUS_NO_SUCH_USER
;
451 if (locally_made_cli
) {
458 static NTSTATUS
auth_init_smbserver(struct auth_context
*auth_context
, const char* param
, auth_methods
**auth_method
)
460 struct auth_methods
*result
;
462 result
= TALLOC_ZERO_P(auth_context
, struct auth_methods
);
463 if (result
== NULL
) {
464 return NT_STATUS_NO_MEMORY
;
466 result
->name
= "smbserver";
467 result
->auth
= check_smbserver_security
;
468 result
->get_chal
= auth_get_challenge_server
;
470 *auth_method
= result
;
474 NTSTATUS
auth_server_init(void)
476 return smb_register_auth(AUTH_INTERFACE_VERSION
, "smbserver", auth_init_smbserver
);