2 Unix SMB/CIFS implementation.
3 Authenticate against a remote domain
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 "../libcli/auth/libcli_auth.h"
25 #define DBGC_CLASS DBGC_AUTH
27 extern bool global_machine_password_needs_changing
;
28 static struct named_mutex
*mutex
;
31 * Change machine password (called from main loop
32 * idle timeout. Must be done as root.
35 void attempt_machine_password_change(void)
37 unsigned char trust_passwd_hash
[16];
41 if (!global_machine_password_needs_changing
) {
45 if (lp_security() != SEC_DOMAIN
) {
50 * We're in domain level security, and the code that
51 * read the machine password flagged that the machine
52 * password needs changing.
56 * First, open the machine password file with an exclusive lock.
59 lock
= secrets_get_trust_account_lock(NULL
, lp_workgroup());
62 DEBUG(0,("attempt_machine_password_change: unable to lock "
63 "the machine account password for machine %s in "
65 global_myname(), lp_workgroup() ));
69 if(!secrets_fetch_trust_account_password(lp_workgroup(),
70 trust_passwd_hash
, &lct
, NULL
)) {
71 DEBUG(0,("attempt_machine_password_change: unable to read the "
72 "machine account password for %s in domain %s.\n",
73 global_myname(), lp_workgroup()));
79 * Make sure someone else hasn't already done this.
82 if(time(NULL
) < lct
+ lp_machine_password_timeout()) {
83 global_machine_password_needs_changing
= false;
88 /* always just contact the PDC here */
90 change_trust_account_password( lp_workgroup(), NULL
);
91 global_machine_password_needs_changing
= false;
96 * Connect to a remote server for (inter)domain security authenticaion.
98 * @param cli the cli to return containing the active connection
99 * @param server either a machine name or text IP address to
101 * @param setup_creds_as domain account to setup credentials as
102 * @param sec_chan a switch value to distinguish between domain
103 * member and interdomain authentication
104 * @param trust_passwd the trust password to establish the
109 static NTSTATUS
connect_to_domain_password_server(struct cli_state
**cli
,
112 struct sockaddr_storage
*dc_ss
,
113 struct rpc_pipe_client
**pipe_ret
,
117 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
123 /* TODO: Send a SAMLOGON request to determine whether this is a valid
124 logonserver. We can avoid a 30-second timeout if the DC is down
125 if the SAMLOGON request fails as it is only over UDP. */
127 /* we use a mutex to prevent two connections at once - when a
128 Win2k PDC get two connections where one hasn't completed a
129 session setup yet it will send a TCP reset to the first
130 connection (tridge) */
133 * With NT4.x DC's *all* authentication must be serialized to avoid
134 * ACCESS_DENIED errors if 2 auths are done from the same machine. JRA.
137 mutex
= grab_named_mutex(NULL
, dc_name
, 10);
139 return NT_STATUS_NO_LOGON_SERVERS
;
142 /* Attempt connection */
144 result
= cli_full_connection(cli
, global_myname(), dc_name
, dc_ss
, 0,
145 "IPC$", "IPC", "", "", "", 0, Undefined
, retry
);
147 if (!NT_STATUS_IS_OK(result
)) {
148 /* map to something more useful */
149 if (NT_STATUS_EQUAL(result
, NT_STATUS_UNSUCCESSFUL
)) {
150 result
= NT_STATUS_NO_LOGON_SERVERS
;
163 * We now have an anonymous connection to IPC$ on the domain password server.
167 * Even if the connect succeeds we need to setup the netlogon
168 * pipe here. We do this as we may just have changed the domain
169 * account password on the PDC and yet we may be talking to
170 * a BDC that doesn't have this replicated yet. In this case
171 * a successful connect to a DC needs to take the netlogon connect
172 * into account also. This patch from "Bjart Kvarme" <bjart.kvarme@usit.uio.no>.
175 /* open the netlogon pipe. */
176 if (lp_client_schannel()) {
177 /* We also setup the creds chain in the open_schannel call. */
178 result
= cli_rpc_pipe_open_schannel(
179 *cli
, &ndr_table_netlogon
.syntax_id
, NCACN_NP
,
180 DCERPC_AUTH_LEVEL_PRIVACY
, domain
, &netlogon_pipe
);
182 result
= cli_rpc_pipe_open_noauth(
183 *cli
, &ndr_table_netlogon
.syntax_id
, &netlogon_pipe
);
186 if (!NT_STATUS_IS_OK(result
)) {
187 DEBUG(0,("connect_to_domain_password_server: unable to open the domain client session to \
188 machine %s. Error was : %s.\n", dc_name
, nt_errstr(result
)));
195 if (!lp_client_schannel()) {
196 /* We need to set up a creds chain on an unauthenticated netlogon pipe. */
197 uint32_t neg_flags
= NETLOGON_NEG_AUTH2_ADS_FLAGS
;
198 enum netr_SchannelType sec_chan_type
= 0;
199 unsigned char machine_pwd
[16];
200 const char *account_name
;
202 if (!get_trust_pw_hash(domain
, machine_pwd
, &account_name
,
205 DEBUG(0, ("connect_to_domain_password_server: could not fetch "
206 "trust account password for domain '%s'\n",
211 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
214 result
= rpccli_netlogon_setup_creds(netlogon_pipe
,
215 dc_name
, /* server name */
217 global_myname(), /* client name */
218 account_name
, /* machine account name */
223 if (!NT_STATUS_IS_OK(result
)) {
232 DEBUG(0,("connect_to_domain_password_server: unable to open the domain client session to \
233 machine %s. Error was : %s.\n", dc_name
, cli_errstr(*cli
)));
237 return NT_STATUS_NO_LOGON_SERVERS
;
240 /* We exit here with the mutex *locked*. JRA */
242 *pipe_ret
= netlogon_pipe
;
247 /***********************************************************************
248 Do the same as security=server, but using NT Domain calls and a session
249 key from the machine password. If the server parameter is specified
250 use it, otherwise figure out a server from the 'password server' param.
251 ************************************************************************/
253 static NTSTATUS
domain_client_validate(TALLOC_CTX
*mem_ctx
,
254 const struct auth_usersupplied_info
*user_info
,
257 struct auth_serversupplied_info
**server_info
,
259 struct sockaddr_storage
*dc_ss
)
262 struct netr_SamInfo3
*info3
= NULL
;
263 struct cli_state
*cli
= NULL
;
264 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
265 NTSTATUS nt_status
= NT_STATUS_NO_LOGON_SERVERS
;
270 * At this point, smb_apasswd points to the lanman response to
271 * the challenge in local_challenge, and smb_ntpasswd points to
272 * the NT response to the challenge in local_challenge. Ship
273 * these over the secure channel to a domain controller and
274 * see if they were valid.
277 /* rety loop for robustness */
279 for (i
= 0; !NT_STATUS_IS_OK(nt_status
) && retry
&& (i
< 3); i
++) {
280 nt_status
= connect_to_domain_password_server(&cli
,
288 if ( !NT_STATUS_IS_OK(nt_status
) ) {
289 DEBUG(0,("domain_client_validate: Domain password server not available.\n"));
290 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_ACCESS_DENIED
)) {
291 return NT_STATUS_TRUSTED_RELATIONSHIP_FAILURE
;
296 /* store a successful connection */
298 saf_store( domain
, cli
->desthost
);
301 * If this call succeeds, we now have lots of info about the user
302 * in the info3 structure.
305 nt_status
= rpccli_netlogon_sam_network_logon(netlogon_pipe
,
307 user_info
->logon_parameters
,/* flags such as 'allow workstation logon' */
308 dc_name
, /* server name */
309 user_info
->smb_name
, /* user name logging on. */
310 user_info
->client_domain
, /* domain name */
311 user_info
->wksta_name
, /* workstation name */
312 chal
, /* 8 byte challenge. */
313 user_info
->lm_resp
, /* lanman 24 byte response */
314 user_info
->nt_resp
, /* nt 24 byte response */
315 &info3
); /* info3 out */
317 /* Let go as soon as possible so we avoid any potential deadlocks
318 with winbind lookup up users or groups. */
322 if (!NT_STATUS_IS_OK(nt_status
)) {
323 DEBUG(0,("domain_client_validate: unable to validate password "
324 "for user %s in domain %s to Domain controller %s. "
325 "Error was %s.\n", user_info
->smb_name
,
326 user_info
->client_domain
, dc_name
,
327 nt_errstr(nt_status
)));
329 /* map to something more useful */
330 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_UNSUCCESSFUL
)) {
331 nt_status
= NT_STATUS_NO_LOGON_SERVERS
;
334 nt_status
= make_server_info_info3(mem_ctx
,
340 if (NT_STATUS_IS_OK(nt_status
)) {
341 (*server_info
)->nss_token
|= user_info
->was_mapped
;
343 if ( ! (*server_info
)->guest
) {
344 /* if a real user check pam account restrictions */
345 /* only really perfomed if "obey pam restriction" is true */
346 nt_status
= smb_pam_accountcheck((*server_info
)->unix_name
);
347 if ( !NT_STATUS_IS_OK(nt_status
)) {
348 DEBUG(1, ("PAM account restriction prevents user login\n"));
356 netsamlogon_cache_store(user_info
->smb_name
, info3
);
360 /* Note - once the cli stream is shutdown the mem_ctx used
361 to allocate the other_sids and gids structures has been deleted - so
362 these pointers are no longer valid..... */
368 /****************************************************************************
369 Check for a valid username and password in security=domain mode.
370 ****************************************************************************/
372 static NTSTATUS
check_ntdomain_security(const struct auth_context
*auth_context
,
373 void *my_private_data
,
375 const struct auth_usersupplied_info
*user_info
,
376 struct auth_serversupplied_info
**server_info
)
378 NTSTATUS nt_status
= NT_STATUS_LOGON_FAILURE
;
379 const char *domain
= lp_workgroup();
381 struct sockaddr_storage dc_ss
;
383 if ( lp_server_role() != ROLE_DOMAIN_MEMBER
) {
384 DEBUG(0,("check_ntdomain_security: Configuration error! Cannot use "
385 "ntdomain auth method when not a member of a domain.\n"));
386 return NT_STATUS_NOT_IMPLEMENTED
;
389 if (!user_info
|| !server_info
|| !auth_context
) {
390 DEBUG(1,("check_ntdomain_security: Critical variables not present. Failing.\n"));
391 return NT_STATUS_INVALID_PARAMETER
;
395 * Check that the requested domain is not our own machine name.
396 * If it is, we should never check the PDC here, we use our own local
400 if(strequal(get_global_sam_name(), user_info
->domain
)) {
401 DEBUG(3,("check_ntdomain_security: Requested domain was for this machine.\n"));
402 return NT_STATUS_NOT_IMPLEMENTED
;
405 /* we need our DC to send the net_sam_logon() request to */
407 if ( !get_dc_name(domain
, NULL
, dc_name
, &dc_ss
) ) {
408 DEBUG(5,("check_ntdomain_security: unable to locate a DC for domain %s\n",
410 return NT_STATUS_NO_LOGON_SERVERS
;
413 nt_status
= domain_client_validate(mem_ctx
,
416 (uchar
*)auth_context
->challenge
.data
,
424 /* module initialisation */
425 static NTSTATUS
auth_init_ntdomain(struct auth_context
*auth_context
, const char* param
, auth_methods
**auth_method
)
427 if (!make_auth_methods(auth_context
, auth_method
)) {
428 return NT_STATUS_NO_MEMORY
;
431 (*auth_method
)->name
= "ntdomain";
432 (*auth_method
)->auth
= check_ntdomain_security
;
437 /****************************************************************************
438 Check for a valid username and password in a trusted domain
439 ****************************************************************************/
441 static NTSTATUS
check_trustdomain_security(const struct auth_context
*auth_context
,
442 void *my_private_data
,
444 const struct auth_usersupplied_info
*user_info
,
445 struct auth_serversupplied_info
**server_info
)
447 NTSTATUS nt_status
= NT_STATUS_LOGON_FAILURE
;
448 unsigned char trust_md4_password
[16];
449 char *trust_password
;
451 struct sockaddr_storage dc_ss
;
453 if (!user_info
|| !server_info
|| !auth_context
) {
454 DEBUG(1,("check_trustdomain_security: Critical variables not present. Failing.\n"));
455 return NT_STATUS_INVALID_PARAMETER
;
459 * Check that the requested domain is not our own machine name or domain name.
462 if( strequal(get_global_sam_name(), user_info
->domain
)) {
463 DEBUG(3,("check_trustdomain_security: Requested domain [%s] was for this machine.\n",
465 return NT_STATUS_NOT_IMPLEMENTED
;
468 /* No point is bothering if this is not a trusted domain.
469 This return makes "map to guest = bad user" work again.
470 The logic is that if we know nothing about the domain, that
471 user is not known to us and does not exist */
473 if ( !is_trusted_domain( user_info
->domain
) )
474 return NT_STATUS_NOT_IMPLEMENTED
;
477 * Get the trusted account password for the trusted domain
478 * No need to become_root() as secrets_init() is done at startup.
481 if (!pdb_get_trusteddom_pw(user_info
->domain
, &trust_password
,
483 DEBUG(0, ("check_trustdomain_security: could not fetch trust "
484 "account password for domain %s\n",
486 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
489 #ifdef DEBUG_PASSWORD
490 DEBUG(100, ("Trust password for domain %s is %s\n", user_info
->domain
,
493 E_md4hash(trust_password
, trust_md4_password
);
494 SAFE_FREE(trust_password
);
497 /* Test if machine password is expired and need to be changed */
498 if (time(NULL
) > last_change_time
+ (time_t)lp_machine_password_timeout())
500 global_machine_password_needs_changing
= True
;
504 /* use get_dc_name() for consistency even through we know that it will be
507 if ( !get_dc_name(user_info
->domain
, NULL
, dc_name
, &dc_ss
) ) {
508 DEBUG(5,("check_trustdomain_security: unable to locate a DC for domain %s\n",
510 return NT_STATUS_NO_LOGON_SERVERS
;
513 nt_status
= domain_client_validate(mem_ctx
,
516 (uchar
*)auth_context
->challenge
.data
,
524 /* module initialisation */
525 static NTSTATUS
auth_init_trustdomain(struct auth_context
*auth_context
, const char* param
, auth_methods
**auth_method
)
527 if (!make_auth_methods(auth_context
, auth_method
)) {
528 return NT_STATUS_NO_MEMORY
;
531 (*auth_method
)->name
= "trustdomain";
532 (*auth_method
)->auth
= check_trustdomain_security
;
536 NTSTATUS
auth_domain_init(void)
538 smb_register_auth(AUTH_INTERFACE_VERSION
, "trustdomain", auth_init_trustdomain
);
539 smb_register_auth(AUTH_INTERFACE_VERSION
, "ntdomain", auth_init_ntdomain
);