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/>.
23 #include "../libcli/auth/libcli_auth.h"
24 #include "../librpc/gen_ndr/ndr_netlogon.h"
25 #include "rpc_client/cli_pipe.h"
26 #include "rpc_client/cli_netlogon.h"
31 #define DBGC_CLASS DBGC_AUTH
33 extern bool global_machine_password_needs_changing
;
34 static struct named_mutex
*mutex
;
37 * Change machine password (called from main loop
38 * idle timeout. Must be done as root.
41 void attempt_machine_password_change(void)
43 unsigned char trust_passwd_hash
[16];
47 if (!global_machine_password_needs_changing
) {
51 if (lp_security() != SEC_DOMAIN
) {
56 * We're in domain level security, and the code that
57 * read the machine password flagged that the machine
58 * password needs changing.
62 * First, open the machine password file with an exclusive lock.
65 lock
= secrets_get_trust_account_lock(NULL
, lp_workgroup());
68 DEBUG(0,("attempt_machine_password_change: unable to lock "
69 "the machine account password for machine %s in "
71 global_myname(), lp_workgroup() ));
75 if(!secrets_fetch_trust_account_password(lp_workgroup(),
76 trust_passwd_hash
, &lct
, NULL
)) {
77 DEBUG(0,("attempt_machine_password_change: unable to read the "
78 "machine account password for %s in domain %s.\n",
79 global_myname(), lp_workgroup()));
85 * Make sure someone else hasn't already done this.
88 if(time(NULL
) < lct
+ lp_machine_password_timeout()) {
89 global_machine_password_needs_changing
= false;
94 /* always just contact the PDC here */
96 change_trust_account_password( lp_workgroup(), NULL
);
97 global_machine_password_needs_changing
= false;
102 * Connect to a remote server for (inter)domain security authenticaion.
104 * @param cli the cli to return containing the active connection
105 * @param server either a machine name or text IP address to
107 * @param setup_creds_as domain account to setup credentials as
108 * @param sec_chan a switch value to distinguish between domain
109 * member and interdomain authentication
110 * @param trust_passwd the trust password to establish the
115 static NTSTATUS
connect_to_domain_password_server(struct cli_state
**cli
,
118 struct sockaddr_storage
*dc_ss
,
119 struct rpc_pipe_client
**pipe_ret
)
122 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
128 /* TODO: Send a SAMLOGON request to determine whether this is a valid
129 logonserver. We can avoid a 30-second timeout if the DC is down
130 if the SAMLOGON request fails as it is only over UDP. */
132 /* we use a mutex to prevent two connections at once - when a
133 Win2k PDC get two connections where one hasn't completed a
134 session setup yet it will send a TCP reset to the first
135 connection (tridge) */
138 * With NT4.x DC's *all* authentication must be serialized to avoid
139 * ACCESS_DENIED errors if 2 auths are done from the same machine. JRA.
142 mutex
= grab_named_mutex(NULL
, dc_name
, 10);
144 return NT_STATUS_NO_LOGON_SERVERS
;
147 /* Attempt connection */
148 result
= cli_full_connection(cli
, global_myname(), dc_name
, dc_ss
, 0,
149 "IPC$", "IPC", "", "", "", 0, Undefined
);
151 if (!NT_STATUS_IS_OK(result
)) {
152 /* map to something more useful */
153 if (NT_STATUS_EQUAL(result
, NT_STATUS_UNSUCCESSFUL
)) {
154 result
= NT_STATUS_NO_LOGON_SERVERS
;
167 * We now have an anonymous connection to IPC$ on the domain password server.
171 * Even if the connect succeeds we need to setup the netlogon
172 * pipe here. We do this as we may just have changed the domain
173 * account password on the PDC and yet we may be talking to
174 * a BDC that doesn't have this replicated yet. In this case
175 * a successful connect to a DC needs to take the netlogon connect
176 * into account also. This patch from "Bjart Kvarme" <bjart.kvarme@usit.uio.no>.
179 /* open the netlogon pipe. */
180 if (lp_client_schannel()) {
181 /* We also setup the creds chain in the open_schannel call. */
182 result
= cli_rpc_pipe_open_schannel(
183 *cli
, &ndr_table_netlogon
.syntax_id
, NCACN_NP
,
184 DCERPC_AUTH_LEVEL_PRIVACY
, domain
, &netlogon_pipe
);
186 result
= cli_rpc_pipe_open_noauth(
187 *cli
, &ndr_table_netlogon
.syntax_id
, &netlogon_pipe
);
190 if (!NT_STATUS_IS_OK(result
)) {
191 DEBUG(0,("connect_to_domain_password_server: unable to open the domain client session to \
192 machine %s. Error was : %s.\n", dc_name
, nt_errstr(result
)));
199 if (!lp_client_schannel()) {
200 /* We need to set up a creds chain on an unauthenticated netlogon pipe. */
201 uint32_t neg_flags
= NETLOGON_NEG_AUTH2_ADS_FLAGS
;
202 enum netr_SchannelType sec_chan_type
= 0;
203 unsigned char machine_pwd
[16];
204 const char *account_name
;
206 if (!get_trust_pw_hash(domain
, machine_pwd
, &account_name
,
209 DEBUG(0, ("connect_to_domain_password_server: could not fetch "
210 "trust account password for domain '%s'\n",
215 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
218 result
= rpccli_netlogon_setup_creds(netlogon_pipe
,
219 dc_name
, /* server name */
221 global_myname(), /* client name */
222 account_name
, /* machine account name */
227 if (!NT_STATUS_IS_OK(result
)) {
236 DEBUG(0,("connect_to_domain_password_server: unable to open the domain client session to \
237 machine %s. Error was : %s.\n", dc_name
, cli_errstr(*cli
)));
241 return NT_STATUS_NO_LOGON_SERVERS
;
244 /* We exit here with the mutex *locked*. JRA */
246 *pipe_ret
= netlogon_pipe
;
251 /***********************************************************************
252 Do the same as security=server, but using NT Domain calls and a session
253 key from the machine password. If the server parameter is specified
254 use it, otherwise figure out a server from the 'password server' param.
255 ************************************************************************/
257 static NTSTATUS
domain_client_validate(TALLOC_CTX
*mem_ctx
,
258 const struct auth_usersupplied_info
*user_info
,
261 struct auth_serversupplied_info
**server_info
,
263 struct sockaddr_storage
*dc_ss
)
266 struct netr_SamInfo3
*info3
= NULL
;
267 struct cli_state
*cli
= NULL
;
268 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
269 NTSTATUS nt_status
= NT_STATUS_NO_LOGON_SERVERS
;
273 * At this point, smb_apasswd points to the lanman response to
274 * the challenge in local_challenge, and smb_ntpasswd points to
275 * the NT response to the challenge in local_challenge. Ship
276 * these over the secure channel to a domain controller and
277 * see if they were valid.
280 /* rety loop for robustness */
282 for (i
= 0; !NT_STATUS_IS_OK(nt_status
) && (i
< 3); i
++) {
283 nt_status
= connect_to_domain_password_server(&cli
,
290 if ( !NT_STATUS_IS_OK(nt_status
) ) {
291 DEBUG(0,("domain_client_validate: Domain password server not available.\n"));
292 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_ACCESS_DENIED
)) {
293 return NT_STATUS_TRUSTED_RELATIONSHIP_FAILURE
;
298 /* store a successful connection */
300 saf_store( domain
, cli
->desthost
);
303 * If this call succeeds, we now have lots of info about the user
304 * in the info3 structure.
307 nt_status
= rpccli_netlogon_sam_network_logon(netlogon_pipe
,
309 user_info
->logon_parameters
, /* flags such as 'allow workstation logon' */
310 dc_name
, /* server name */
311 user_info
->client
.account_name
, /* user name logging on. */
312 user_info
->client
.domain_name
, /* domain name */
313 user_info
->workstation_name
, /* workstation name */
314 chal
, /* 8 byte challenge. */
315 3, /* validation level */
316 user_info
->password
.response
.lanman
, /* lanman 24 byte response */
317 user_info
->password
.response
.nt
, /* nt 24 byte response */
318 &info3
); /* info3 out */
320 /* Let go as soon as possible so we avoid any potential deadlocks
321 with winbind lookup up users or groups. */
325 if (!NT_STATUS_IS_OK(nt_status
)) {
326 DEBUG(0,("domain_client_validate: unable to validate password "
327 "for user %s in domain %s to Domain controller %s. "
328 "Error was %s.\n", user_info
->client
.account_name
,
329 user_info
->client
.domain_name
, dc_name
,
330 nt_errstr(nt_status
)));
332 /* map to something more useful */
333 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_UNSUCCESSFUL
)) {
334 nt_status
= NT_STATUS_NO_LOGON_SERVERS
;
337 nt_status
= make_server_info_info3(mem_ctx
,
338 user_info
->client
.account_name
,
343 if (NT_STATUS_IS_OK(nt_status
)) {
344 (*server_info
)->nss_token
|= user_info
->was_mapped
;
345 netsamlogon_cache_store(user_info
->client
.account_name
, info3
);
350 /* Note - once the cli stream is shutdown the mem_ctx used
351 to allocate the other_sids and gids structures has been deleted - so
352 these pointers are no longer valid..... */
358 /****************************************************************************
359 Check for a valid username and password in security=domain mode.
360 ****************************************************************************/
362 static NTSTATUS
check_ntdomain_security(const struct auth_context
*auth_context
,
363 void *my_private_data
,
365 const struct auth_usersupplied_info
*user_info
,
366 struct auth_serversupplied_info
**server_info
)
368 NTSTATUS nt_status
= NT_STATUS_LOGON_FAILURE
;
369 const char *domain
= lp_workgroup();
371 struct sockaddr_storage dc_ss
;
373 if ( lp_server_role() != ROLE_DOMAIN_MEMBER
) {
374 DEBUG(0,("check_ntdomain_security: Configuration error! Cannot use "
375 "ntdomain auth method when not a member of a domain.\n"));
376 return NT_STATUS_NOT_IMPLEMENTED
;
379 if (!user_info
|| !server_info
|| !auth_context
) {
380 DEBUG(1,("check_ntdomain_security: Critical variables not present. Failing.\n"));
381 return NT_STATUS_INVALID_PARAMETER
;
384 DEBUG(10, ("Check auth for: [%s]\n", user_info
->mapped
.account_name
));
387 * Check that the requested domain is not our own machine name.
388 * If it is, we should never check the PDC here, we use our own local
392 if(strequal(get_global_sam_name(), user_info
->mapped
.domain_name
)) {
393 DEBUG(3,("check_ntdomain_security: Requested domain was for this machine.\n"));
394 return NT_STATUS_NOT_IMPLEMENTED
;
397 /* we need our DC to send the net_sam_logon() request to */
399 if ( !get_dc_name(domain
, NULL
, dc_name
, &dc_ss
) ) {
400 DEBUG(5,("check_ntdomain_security: unable to locate a DC for domain %s\n",
401 user_info
->mapped
.domain_name
));
402 return NT_STATUS_NO_LOGON_SERVERS
;
405 nt_status
= domain_client_validate(mem_ctx
,
408 (uchar
*)auth_context
->challenge
.data
,
416 /* module initialisation */
417 static NTSTATUS
auth_init_ntdomain(struct auth_context
*auth_context
, const char* param
, auth_methods
**auth_method
)
419 struct auth_methods
*result
;
421 result
= TALLOC_ZERO_P(auth_context
, struct auth_methods
);
422 if (result
== NULL
) {
423 return NT_STATUS_NO_MEMORY
;
425 result
->name
= "ntdomain";
426 result
->auth
= check_ntdomain_security
;
428 *auth_method
= result
;
433 /****************************************************************************
434 Check for a valid username and password in a trusted domain
435 ****************************************************************************/
437 static NTSTATUS
check_trustdomain_security(const struct auth_context
*auth_context
,
438 void *my_private_data
,
440 const struct auth_usersupplied_info
*user_info
,
441 struct auth_serversupplied_info
**server_info
)
443 NTSTATUS nt_status
= NT_STATUS_LOGON_FAILURE
;
444 unsigned char trust_md4_password
[16];
445 char *trust_password
;
447 struct sockaddr_storage dc_ss
;
449 if (!user_info
|| !server_info
|| !auth_context
) {
450 DEBUG(1,("check_trustdomain_security: Critical variables not present. Failing.\n"));
451 return NT_STATUS_INVALID_PARAMETER
;
454 DEBUG(10, ("Check auth for: [%s]\n", user_info
->mapped
.account_name
));
457 * Check that the requested domain is not our own machine name or domain name.
460 if( strequal(get_global_sam_name(), user_info
->mapped
.domain_name
)) {
461 DEBUG(3,("check_trustdomain_security: Requested domain [%s] was for this machine.\n",
462 user_info
->mapped
.domain_name
));
463 return NT_STATUS_NOT_IMPLEMENTED
;
466 /* No point is bothering if this is not a trusted domain.
467 This return makes "map to guest = bad user" work again.
468 The logic is that if we know nothing about the domain, that
469 user is not known to us and does not exist */
471 if ( !is_trusted_domain( user_info
->mapped
.domain_name
) )
472 return NT_STATUS_NOT_IMPLEMENTED
;
475 * Get the trusted account password for the trusted domain
476 * No need to become_root() as secrets_init() is done at startup.
479 if (!pdb_get_trusteddom_pw(user_info
->mapped
.domain_name
, &trust_password
,
481 DEBUG(0, ("check_trustdomain_security: could not fetch trust "
482 "account password for domain %s\n",
483 user_info
->mapped
.domain_name
));
484 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
487 #ifdef DEBUG_PASSWORD
488 DEBUG(100, ("Trust password for domain %s is %s\n", user_info
->mapped
.domain_name
,
491 E_md4hash(trust_password
, trust_md4_password
);
492 SAFE_FREE(trust_password
);
495 /* Test if machine password is expired and need to be changed */
496 if (time(NULL
) > last_change_time
+ (time_t)lp_machine_password_timeout())
498 global_machine_password_needs_changing
= True
;
502 /* use get_dc_name() for consistency even through we know that it will be
505 if ( !get_dc_name(user_info
->mapped
.domain_name
, NULL
, dc_name
, &dc_ss
) ) {
506 DEBUG(5,("check_trustdomain_security: unable to locate a DC for domain %s\n",
507 user_info
->mapped
.domain_name
));
508 return NT_STATUS_NO_LOGON_SERVERS
;
511 nt_status
= domain_client_validate(mem_ctx
,
513 user_info
->mapped
.domain_name
,
514 (uchar
*)auth_context
->challenge
.data
,
522 /* module initialisation */
523 static NTSTATUS
auth_init_trustdomain(struct auth_context
*auth_context
, const char* param
, auth_methods
**auth_method
)
525 struct auth_methods
*result
;
527 result
= TALLOC_ZERO_P(auth_context
, struct auth_methods
);
528 if (result
== NULL
) {
529 return NT_STATUS_NO_MEMORY
;
531 result
->name
= "trustdomain";
532 result
->auth
= check_trustdomain_security
;
534 *auth_method
= result
;
538 NTSTATUS
auth_domain_init(void)
540 smb_register_auth(AUTH_INTERFACE_VERSION
, "trustdomain", auth_init_trustdomain
);
541 smb_register_auth(AUTH_INTERFACE_VERSION
, "ntdomain", auth_init_ntdomain
);