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"
29 #include "libsmb/libsmb.h"
30 #include "libcli/auth/netlogon_creds_cli.h"
33 #define DBGC_CLASS DBGC_AUTH
35 static struct named_mutex
*mutex
;
38 * Connect to a remote server for (inter)domain security authenticaion.
40 * @param cli the cli to return containing the active connection
41 * @param server either a machine name or text IP address to
43 * @param setup_creds_as domain account to setup credentials as
44 * @param sec_chan a switch value to distinguish between domain
45 * member and interdomain authentication
46 * @param trust_passwd the trust password to establish the
51 static NTSTATUS
connect_to_domain_password_server(struct cli_state
**cli_ret
,
54 const struct sockaddr_storage
*dc_ss
,
55 struct rpc_pipe_client
**pipe_ret
,
56 struct netlogon_creds_cli_context
**creds_ret
)
58 TALLOC_CTX
*frame
= talloc_stackframe();
59 struct messaging_context
*msg_ctx
= server_messaging_context();
61 struct cli_state
*cli
= NULL
;
62 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
63 struct netlogon_creds_cli_context
*netlogon_creds
= NULL
;
64 struct netlogon_creds_CredentialState
*creds
= NULL
;
65 uint32_t netlogon_flags
= 0;
66 enum netr_SchannelType sec_chan_type
= 0;
67 const char *_account_name
= NULL
;
68 const char *account_name
= NULL
;
69 struct samr_Password current_nt_hash
;
70 struct samr_Password
*previous_nt_hash
= NULL
;
78 /* TODO: Send a SAMLOGON request to determine whether this is a valid
79 logonserver. We can avoid a 30-second timeout if the DC is down
80 if the SAMLOGON request fails as it is only over UDP. */
82 /* we use a mutex to prevent two connections at once - when a
83 Win2k PDC get two connections where one hasn't completed a
84 session setup yet it will send a TCP reset to the first
85 connection (tridge) */
88 * With NT4.x DC's *all* authentication must be serialized to avoid
89 * ACCESS_DENIED errors if 2 auths are done from the same machine. JRA.
92 mutex
= grab_named_mutex(NULL
, dc_name
, 10);
95 return NT_STATUS_NO_LOGON_SERVERS
;
98 /* Attempt connection */
99 result
= cli_full_connection(&cli
, lp_netbios_name(), dc_name
, dc_ss
, 0,
100 "IPC$", "IPC", "", "", "", 0, SMB_SIGNING_DEFAULT
);
102 if (!NT_STATUS_IS_OK(result
)) {
103 /* map to something more useful */
104 if (NT_STATUS_EQUAL(result
, NT_STATUS_UNSUCCESSFUL
)) {
105 result
= NT_STATUS_NO_LOGON_SERVERS
;
114 * We now have an anonymous connection to IPC$ on the domain password server.
117 ok
= get_trust_pw_hash(domain
,
118 current_nt_hash
.hash
,
125 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
128 account_name
= talloc_asprintf(talloc_tos(), "%s$", _account_name
);
129 if (account_name
== NULL
) {
133 return NT_STATUS_NO_MEMORY
;
136 result
= rpccli_create_netlogon_creds(dc_name
,
143 if (!NT_STATUS_IS_OK(result
)) {
147 SAFE_FREE(previous_nt_hash
);
151 result
= rpccli_setup_netlogon_creds(cli
,
153 false, /* force_reauth */
156 SAFE_FREE(previous_nt_hash
);
157 if (!NT_STATUS_IS_OK(result
)) {
164 result
= netlogon_creds_cli_get(netlogon_creds
,
167 if (!NT_STATUS_IS_OK(result
)) {
173 netlogon_flags
= creds
->negotiate_flags
;
176 if (netlogon_flags
& NETLOGON_NEG_AUTHENTICATED_RPC
) {
177 result
= cli_rpc_pipe_open_schannel_with_key(
178 cli
, &ndr_table_netlogon
, NCACN_NP
,
179 domain
, netlogon_creds
, &netlogon_pipe
);
181 result
= cli_rpc_pipe_open_noauth(cli
,
186 if (!NT_STATUS_IS_OK(result
)) {
187 DEBUG(0,("connect_to_domain_password_server: "
188 "unable to open the domain client session to "
189 "machine %s. Flags[0x%08X] Error was : %s.\n",
190 dc_name
, (unsigned)netlogon_flags
,
199 DEBUG(0, ("connect_to_domain_password_server: unable to open "
200 "the domain client session to machine %s. Error "
201 "was : %s.\n", dc_name
, nt_errstr(result
)));
205 return NT_STATUS_NO_LOGON_SERVERS
;
208 /* We exit here with the mutex *locked*. JRA */
211 *pipe_ret
= netlogon_pipe
;
212 *creds_ret
= netlogon_creds
;
218 /***********************************************************************
219 Do the same as security=server, but using NT Domain calls and a session
220 key from the machine password. If the server parameter is specified
221 use it, otherwise figure out a server from the 'password server' param.
222 ************************************************************************/
224 static NTSTATUS
domain_client_validate(TALLOC_CTX
*mem_ctx
,
225 const struct auth_usersupplied_info
*user_info
,
228 struct auth_serversupplied_info
**server_info
,
230 const struct sockaddr_storage
*dc_ss
)
233 struct netr_SamInfo3
*info3
= NULL
;
234 struct cli_state
*cli
= NULL
;
235 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
236 struct netlogon_creds_cli_context
*netlogon_creds
= NULL
;
237 NTSTATUS nt_status
= NT_STATUS_NO_LOGON_SERVERS
;
239 uint8_t authoritative
= 0;
243 * At this point, smb_apasswd points to the lanman response to
244 * the challenge in local_challenge, and smb_ntpasswd points to
245 * the NT response to the challenge in local_challenge. Ship
246 * these over the secure channel to a domain controller and
247 * see if they were valid.
250 /* rety loop for robustness */
252 for (i
= 0; !NT_STATUS_IS_OK(nt_status
) && (i
< 3); i
++) {
253 nt_status
= connect_to_domain_password_server(&cli
,
261 if ( !NT_STATUS_IS_OK(nt_status
) ) {
262 DEBUG(0,("domain_client_validate: Domain password server not available.\n"));
263 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_ACCESS_DENIED
)) {
264 return NT_STATUS_TRUSTED_RELATIONSHIP_FAILURE
;
269 /* store a successful connection */
271 saf_store(domain
, dc_name
);
274 * If this call succeeds, we now have lots of info about the user
275 * in the info3 structure.
278 nt_status
= rpccli_netlogon_network_logon(netlogon_creds
,
279 netlogon_pipe
->binding_handle
,
281 user_info
->logon_parameters
, /* flags such as 'allow workstation logon' */
282 user_info
->client
.account_name
, /* user name logging on. */
283 user_info
->client
.domain_name
, /* domain name */
284 user_info
->workstation_name
, /* workstation name */
285 chal
, /* 8 byte challenge. */
286 user_info
->password
.response
.lanman
, /* lanman 24 byte response */
287 user_info
->password
.response
.nt
, /* nt 24 byte response */
290 &info3
); /* info3 out */
292 /* Let go as soon as possible so we avoid any potential deadlocks
293 with winbind lookup up users or groups. */
297 if (!NT_STATUS_IS_OK(nt_status
)) {
298 DEBUG(0,("domain_client_validate: unable to validate password "
299 "for user %s in domain %s to Domain controller %s. "
300 "Error was %s.\n", user_info
->client
.account_name
,
301 user_info
->client
.domain_name
, dc_name
,
302 nt_errstr(nt_status
)));
304 /* map to something more useful */
305 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_UNSUCCESSFUL
)) {
306 nt_status
= NT_STATUS_NO_LOGON_SERVERS
;
309 nt_status
= make_server_info_info3(mem_ctx
,
310 user_info
->client
.account_name
,
315 if (NT_STATUS_IS_OK(nt_status
)) {
316 (*server_info
)->nss_token
|= user_info
->was_mapped
;
317 netsamlogon_cache_store(user_info
->client
.account_name
, info3
);
322 /* Note - once the cli stream is shutdown the mem_ctx used
323 to allocate the other_sids and gids structures has been deleted - so
324 these pointers are no longer valid..... */
330 /****************************************************************************
331 Check for a valid username and password in security=domain mode.
332 ****************************************************************************/
334 static NTSTATUS
check_ntdomain_security(const struct auth_context
*auth_context
,
335 void *my_private_data
,
337 const struct auth_usersupplied_info
*user_info
,
338 struct auth_serversupplied_info
**server_info
)
340 NTSTATUS nt_status
= NT_STATUS_LOGON_FAILURE
;
341 const char *domain
= lp_workgroup();
343 struct sockaddr_storage dc_ss
;
345 if ( lp_server_role() != ROLE_DOMAIN_MEMBER
) {
346 DEBUG(0,("check_ntdomain_security: Configuration error! Cannot use "
347 "ntdomain auth method when not a member of a domain.\n"));
348 return NT_STATUS_NOT_IMPLEMENTED
;
351 if (!user_info
|| !server_info
|| !auth_context
) {
352 DEBUG(1,("check_ntdomain_security: Critical variables not present. Failing.\n"));
353 return NT_STATUS_INVALID_PARAMETER
;
356 DEBUG(10, ("Check auth for: [%s]\n", user_info
->mapped
.account_name
));
359 * Check that the requested domain is not our own machine name.
360 * If it is, we should never check the PDC here, we use our own local
364 if(strequal(get_global_sam_name(), user_info
->mapped
.domain_name
)) {
365 DEBUG(3,("check_ntdomain_security: Requested domain was for this machine.\n"));
366 return NT_STATUS_NOT_IMPLEMENTED
;
369 /* we need our DC to send the net_sam_logon() request to */
371 if ( !get_dc_name(domain
, NULL
, dc_name
, &dc_ss
) ) {
372 DEBUG(5,("check_ntdomain_security: unable to locate a DC for domain %s\n",
373 user_info
->mapped
.domain_name
));
374 return NT_STATUS_NO_LOGON_SERVERS
;
377 nt_status
= domain_client_validate(mem_ctx
,
380 (uchar
*)auth_context
->challenge
.data
,
388 /* module initialisation */
389 static NTSTATUS
auth_init_ntdomain(struct auth_context
*auth_context
, const char* param
, auth_methods
**auth_method
)
391 struct auth_methods
*result
;
393 result
= talloc_zero(auth_context
, struct auth_methods
);
394 if (result
== NULL
) {
395 return NT_STATUS_NO_MEMORY
;
397 result
->name
= "ntdomain";
398 result
->auth
= check_ntdomain_security
;
400 *auth_method
= result
;
405 /****************************************************************************
406 Check for a valid username and password in a trusted domain
407 ****************************************************************************/
409 static NTSTATUS
check_trustdomain_security(const struct auth_context
*auth_context
,
410 void *my_private_data
,
412 const struct auth_usersupplied_info
*user_info
,
413 struct auth_serversupplied_info
**server_info
)
415 NTSTATUS nt_status
= NT_STATUS_LOGON_FAILURE
;
417 struct sockaddr_storage dc_ss
;
419 if (!user_info
|| !server_info
|| !auth_context
) {
420 DEBUG(1,("check_trustdomain_security: Critical variables not present. Failing.\n"));
421 return NT_STATUS_INVALID_PARAMETER
;
424 DEBUG(10, ("Check auth for: [%s]\n", user_info
->mapped
.account_name
));
427 * Check that the requested domain is not our own machine name or domain name.
430 if( strequal(get_global_sam_name(), user_info
->mapped
.domain_name
)) {
431 DEBUG(3,("check_trustdomain_security: Requested domain [%s] was for this machine.\n",
432 user_info
->mapped
.domain_name
));
433 return NT_STATUS_NOT_IMPLEMENTED
;
436 /* No point is bothering if this is not a trusted domain.
437 This return makes "map to guest = bad user" work again.
438 The logic is that if we know nothing about the domain, that
439 user is not known to us and does not exist */
441 if ( !is_trusted_domain( user_info
->mapped
.domain_name
) )
442 return NT_STATUS_NOT_IMPLEMENTED
;
444 /* use get_dc_name() for consistency even through we know that it will be
447 if ( !get_dc_name(user_info
->mapped
.domain_name
, NULL
, dc_name
, &dc_ss
) ) {
448 DEBUG(5,("check_trustdomain_security: unable to locate a DC for domain %s\n",
449 user_info
->mapped
.domain_name
));
450 return NT_STATUS_NO_LOGON_SERVERS
;
453 nt_status
= domain_client_validate(mem_ctx
,
455 user_info
->mapped
.domain_name
,
456 (uchar
*)auth_context
->challenge
.data
,
464 /* module initialisation */
465 static NTSTATUS
auth_init_trustdomain(struct auth_context
*auth_context
, const char* param
, auth_methods
**auth_method
)
467 struct auth_methods
*result
;
469 result
= talloc_zero(auth_context
, struct auth_methods
);
470 if (result
== NULL
) {
471 return NT_STATUS_NO_MEMORY
;
473 result
->name
= "trustdomain";
474 result
->auth
= check_trustdomain_security
;
476 *auth_method
= result
;
480 NTSTATUS
auth_domain_init(void)
482 smb_register_auth(AUTH_INTERFACE_VERSION
, "trustdomain", auth_init_trustdomain
);
483 smb_register_auth(AUTH_INTERFACE_VERSION
, "ntdomain", auth_init_ntdomain
);