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/>.
24 #define DBGC_CLASS DBGC_AUTH
26 extern userdom_struct current_user_info
;
28 /****************************************************************************
29 Support for server level security.
30 ****************************************************************************/
32 static struct cli_state
*server_cryptkey(TALLOC_CTX
*mem_ctx
)
34 struct cli_state
*cli
= NULL
;
36 struct in_addr dest_ip
;
39 BOOL connected_ok
= False
;
41 if (!(cli
= cli_initialise()))
44 /* security = server just can't function with spnego */
45 cli
->use_spnego
= False
;
47 pserver
= talloc_strdup(mem_ctx
, lp_passwordserver());
50 while(next_token( &p
, desthost
, LIST_SEP
, sizeof(desthost
))) {
53 standard_sub_basic(current_user_info
.smb_name
, current_user_info
.domain
,
54 desthost
, sizeof(desthost
));
57 if(!resolve_name( desthost
, &dest_ip
, 0x20)) {
58 DEBUG(1,("server_cryptkey: Can't resolve address for %s\n",desthost
));
62 if (ismyip(dest_ip
)) {
63 DEBUG(1,("Password server loop - disabling password server %s\n",desthost
));
67 /* we use a mutex to prevent two connections at once - when a
68 Win2k PDC get two connections where one hasn't completed a
69 session setup yet it will send a TCP reset to the first
70 connection (tridge) */
72 if (!grab_server_mutex(desthost
)) {
76 status
= cli_connect(cli
, desthost
, &dest_ip
);
77 if (NT_STATUS_IS_OK(status
)) {
78 DEBUG(3,("connected to password server %s\n",desthost
));
82 DEBUG(10,("server_cryptkey: failed to connect to server %s. Error %s\n",
83 desthost
, nt_errstr(status
) ));
87 release_server_mutex();
88 DEBUG(0,("password server not available\n"));
93 if (!attempt_netbios_session_request(&cli
, global_myname(),
94 desthost
, &dest_ip
)) {
95 release_server_mutex();
96 DEBUG(1,("password server fails session request\n"));
101 if (strequal(desthost
,myhostname())) {
102 exit_server_cleanly("Password server loop!");
105 DEBUG(3,("got session\n"));
107 if (!cli_negprot(cli
)) {
108 DEBUG(1,("%s rejected the negprot\n",desthost
));
109 release_server_mutex();
114 if (cli
->protocol
< PROTOCOL_LANMAN2
||
115 !(cli
->sec_mode
& NEGOTIATE_SECURITY_USER_LEVEL
)) {
116 DEBUG(1,("%s isn't in user level security mode\n",desthost
));
117 release_server_mutex();
122 /* Get the first session setup done quickly, to avoid silly
123 Win2k bugs. (The next connection to the server will kill
127 if (!NT_STATUS_IS_OK(cli_session_setup(cli
, "", "", 0, "", 0,
129 DEBUG(0,("%s rejected the initial session setup (%s)\n",
130 desthost
, cli_errstr(cli
)));
131 release_server_mutex();
136 release_server_mutex();
138 DEBUG(3,("password server OK\n"));
143 struct server_security_state
{
144 struct cli_state
*cli
;
147 /****************************************************************************
148 Send a 'keepalive' packet down the cli pipe.
149 ****************************************************************************/
151 static BOOL
send_server_keepalive(const struct timeval
*now
,
154 struct server_security_state
*state
= talloc_get_type_abort(
155 private_data
, struct server_security_state
);
157 if (!state
->cli
|| !state
->cli
->initialised
) {
161 if (send_keepalive(state
->cli
->fd
)) {
165 DEBUG( 2, ( "send_server_keepalive: password server keepalive "
167 cli_shutdown(state
->cli
);
172 static int destroy_server_security(struct server_security_state
*state
)
175 cli_shutdown(state
->cli
);
180 static struct server_security_state
*make_server_security_state(struct cli_state
*cli
)
182 struct server_security_state
*result
;
184 if (!(result
= talloc(NULL
, struct server_security_state
))) {
185 DEBUG(0, ("talloc failed\n"));
191 talloc_set_destructor(result
, destroy_server_security
);
193 if (lp_keepalive() != 0) {
194 struct timeval interval
;
195 interval
.tv_sec
= lp_keepalive();
196 interval
.tv_usec
= 0;
198 if (event_add_idle(smbd_event_context(), result
, interval
,
199 "server_security_keepalive",
200 send_server_keepalive
,
202 DEBUG(0, ("event_add_idle failed\n"));
211 /****************************************************************************
212 Get the challenge out of a password server.
213 ****************************************************************************/
215 static DATA_BLOB
auth_get_challenge_server(const struct auth_context
*auth_context
,
216 void **my_private_data
,
219 struct cli_state
*cli
= server_cryptkey(mem_ctx
);
222 DEBUG(3,("using password server validation\n"));
224 if ((cli
->sec_mode
& NEGOTIATE_SECURITY_CHALLENGE_RESPONSE
) == 0) {
225 /* We can't work with unencrypted password servers
226 unless 'encrypt passwords = no' */
227 DEBUG(5,("make_auth_info_server: Server is unencrypted, no challenge available..\n"));
229 /* However, it is still a perfectly fine connection
230 to pass that unencrypted password over */
232 (void *)make_server_security_state(cli
);
233 return data_blob_null
;
235 } else if (cli
->secblob
.length
< 8) {
236 /* We can't do much if we don't get a full challenge */
237 DEBUG(2,("make_auth_info_server: Didn't receive a full challenge from server\n"));
239 return data_blob_null
;
242 if (!(*my_private_data
= (void *)make_server_security_state(cli
))) {
243 return data_blob_null
;
246 /* The return must be allocated on the caller's mem_ctx, as our own will be
247 destoyed just after the call. */
248 return data_blob_talloc(auth_context
->mem_ctx
, cli
->secblob
.data
,8);
250 return data_blob_null
;
255 /****************************************************************************
256 Check for a valid username and password in security=server mode.
257 - Validate a password with the password server.
258 ****************************************************************************/
260 static NTSTATUS
check_smbserver_security(const struct auth_context
*auth_context
,
263 const auth_usersupplied_info
*user_info
,
264 auth_serversupplied_info
**server_info
)
266 struct cli_state
*cli
;
267 static unsigned char badpass
[24];
268 static fstring baduser
;
269 static BOOL tested_password_server
= False
;
270 static BOOL bad_password_server
= False
;
271 NTSTATUS nt_status
= NT_STATUS_NOT_IMPLEMENTED
;
272 BOOL locally_made_cli
= False
;
273 struct server_security_state
*state
;
275 state
= talloc_get_type_abort(
276 private_data
, struct server_security_state
);
282 cli
= server_cryptkey(mem_ctx
);
283 locally_made_cli
= True
;
286 if (!cli
|| !cli
->initialised
) {
287 DEBUG(1,("password server is not connected (cli not initilised)\n"));
288 return NT_STATUS_LOGON_FAILURE
;
291 if ((cli
->sec_mode
& NEGOTIATE_SECURITY_CHALLENGE_RESPONSE
) == 0) {
292 if (user_info
->encrypted
) {
293 DEBUG(1,("password server %s is plaintext, but we are encrypted. This just can't work :-(\n", cli
->desthost
));
294 return NT_STATUS_LOGON_FAILURE
;
297 if (memcmp(cli
->secblob
.data
, auth_context
->challenge
.data
, 8) != 0) {
298 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
));
299 return NT_STATUS_LOGON_FAILURE
;
304 memset(badpass
, 0x1f, sizeof(badpass
));
306 if((user_info
->nt_resp
.length
== sizeof(badpass
)) &&
307 !memcmp(badpass
, user_info
->nt_resp
.data
, sizeof(badpass
))) {
309 * Very unlikely, our random bad password is the same as the users
312 memset(badpass
, badpass
[0]+1, sizeof(badpass
));
315 if(baduser
[0] == 0) {
316 fstrcpy(baduser
, INVALID_USER_PREFIX
);
317 fstrcat(baduser
, global_myname());
321 * Attempt a session setup with a totally incorrect password.
322 * If this succeeds with the guest bit *NOT* set then the password
323 * server is broken and is not correctly setting the guest bit. We
324 * need to detect this as some versions of NT4.x are broken. JRA.
327 /* I sure as hell hope that there aren't servers out there that take
328 * NTLMv2 and have this bug, as we don't test for that...
329 * - abartlet@samba.org
332 if ((!tested_password_server
) && (lp_paranoid_server_security())) {
333 if (NT_STATUS_IS_OK(cli_session_setup(cli
, baduser
,
338 user_info
->domain
))) {
341 * We connected to the password server so we
342 * can say we've tested it.
344 tested_password_server
= True
;
346 if ((SVAL(cli
->inbuf
,smb_vwv2
) & 1) == 0) {
347 DEBUG(0,("server_validate: password server %s allows users as non-guest \
348 with a bad password.\n", cli
->desthost
));
349 DEBUG(0,("server_validate: This is broken (and insecure) behaviour. Please do not \
350 use this machine as the password server.\n"));
354 * Password server has the bug.
356 bad_password_server
= True
;
357 return NT_STATUS_LOGON_FAILURE
;
364 * We have already tested the password server.
365 * Fail immediately if it has the bug.
368 if(bad_password_server
) {
369 DEBUG(0,("server_validate: [1] password server %s allows users as non-guest \
370 with a bad password.\n", cli
->desthost
));
371 DEBUG(0,("server_validate: [1] This is broken (and insecure) behaviour. Please do not \
372 use this machine as the password server.\n"));
373 return NT_STATUS_LOGON_FAILURE
;
378 * Now we know the password server will correctly set the guest bit, or is
379 * not guest enabled, we can try with the real password.
382 if (!user_info
->encrypted
) {
383 /* Plaintext available */
384 nt_status
= cli_session_setup(
385 cli
, user_info
->smb_name
,
386 (char *)user_info
->plaintext_password
.data
,
387 user_info
->plaintext_password
.length
,
388 NULL
, 0, user_info
->domain
);
391 nt_status
= cli_session_setup(
392 cli
, user_info
->smb_name
,
393 (char *)user_info
->lm_resp
.data
,
394 user_info
->lm_resp
.length
,
395 (char *)user_info
->nt_resp
.data
,
396 user_info
->nt_resp
.length
,
400 if (!NT_STATUS_IS_OK(nt_status
)) {
401 DEBUG(1,("password server %s rejected the password: %s\n",
402 cli
->desthost
, nt_errstr(nt_status
)));
405 /* if logged in as guest then reject */
406 if ((SVAL(cli
->inbuf
,smb_vwv2
) & 1) != 0) {
407 DEBUG(1,("password server %s gave us guest only\n", cli
->desthost
));
408 nt_status
= NT_STATUS_LOGON_FAILURE
;
413 if (NT_STATUS_IS_OK(nt_status
)) {
414 fstring real_username
;
417 if ( (pass
= smb_getpwnam( NULL
, user_info
->internal_username
,
418 real_username
, True
)) != NULL
)
420 /* if a real user check pam account restrictions */
421 /* only really perfomed if "obey pam restriction" is true */
422 nt_status
= smb_pam_accountcheck(pass
->pw_name
);
423 if ( !NT_STATUS_IS_OK(nt_status
)) {
424 DEBUG(1, ("PAM account restriction prevents user login\n"));
427 nt_status
= make_server_info_pw(server_info
, pass
->pw_name
, pass
);
433 nt_status
= NT_STATUS_NO_SUCH_USER
;
437 if (locally_made_cli
) {
444 static NTSTATUS
auth_init_smbserver(struct auth_context
*auth_context
, const char* param
, auth_methods
**auth_method
)
446 if (!make_auth_methods(auth_context
, auth_method
)) {
447 return NT_STATUS_NO_MEMORY
;
449 (*auth_method
)->name
= "smbserver";
450 (*auth_method
)->auth
= check_smbserver_security
;
451 (*auth_method
)->get_chal
= auth_get_challenge_server
;
455 NTSTATUS
auth_server_init(void)
457 return smb_register_auth(AUTH_INTERFACE_VERSION
, "smbserver", auth_init_smbserver
);