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
,
57 struct netlogon_creds_cli_context
**creds_ret
)
59 TALLOC_CTX
*frame
= talloc_stackframe();
60 struct messaging_context
*msg_ctx
= server_messaging_context();
62 struct cli_state
*cli
= NULL
;
63 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
64 struct netlogon_creds_cli_context
*netlogon_creds
= NULL
;
70 /* TODO: Send a SAMLOGON request to determine whether this is a valid
71 logonserver. We can avoid a 30-second timeout if the DC is down
72 if the SAMLOGON request fails as it is only over UDP. */
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) */
80 * With NT4.x DC's *all* authentication must be serialized to avoid
81 * ACCESS_DENIED errors if 2 auths are done from the same machine. JRA.
84 mutex
= grab_named_mutex(NULL
, dc_name
, 10);
87 return NT_STATUS_NO_LOGON_SERVERS
;
90 /* Attempt connection */
91 result
= cli_full_connection(&cli
, lp_netbios_name(), dc_name
, dc_ss
, 0,
92 "IPC$", "IPC", "", "", "", 0, SMB_SIGNING_DEFAULT
);
94 if (!NT_STATUS_IS_OK(result
)) {
95 /* map to something more useful */
96 if (NT_STATUS_EQUAL(result
, NT_STATUS_UNSUCCESSFUL
)) {
97 result
= NT_STATUS_NO_LOGON_SERVERS
;
106 * We now have an anonymous connection to IPC$ on the domain password server.
109 result
= cli_rpc_pipe_open_schannel(cli
,
117 if (!NT_STATUS_IS_OK(result
)) {
118 DEBUG(0,("connect_to_domain_password_server: "
119 "unable to open the domain client session to "
120 "machine %s. Error was : %s.\n",
121 dc_name
, nt_errstr(result
)));
125 return NT_STATUS_NO_LOGON_SERVERS
;
128 /* We exit here with the mutex *locked*. JRA */
131 *pipe_ret
= netlogon_pipe
;
132 *creds_ret
= talloc_move(mem_ctx
, &netlogon_creds
);
138 /***********************************************************************
139 Do the same as security=server, but using NT Domain calls and a session
140 key from the machine password. If the server parameter is specified
141 use it, otherwise figure out a server from the 'password server' param.
142 ************************************************************************/
144 static NTSTATUS
domain_client_validate(TALLOC_CTX
*mem_ctx
,
145 const struct auth_usersupplied_info
*user_info
,
148 struct auth_serversupplied_info
**server_info
,
150 const struct sockaddr_storage
*dc_ss
)
153 TALLOC_CTX
*frame
= talloc_stackframe();
154 struct netr_SamInfo3
*info3
= NULL
;
155 struct cli_state
*cli
= NULL
;
156 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
157 struct netlogon_creds_cli_context
*netlogon_creds
= NULL
;
158 NTSTATUS nt_status
= NT_STATUS_NO_LOGON_SERVERS
;
160 uint8_t authoritative
= 0;
164 * At this point, smb_apasswd points to the lanman response to
165 * the challenge in local_challenge, and smb_ntpasswd points to
166 * the NT response to the challenge in local_challenge. Ship
167 * these over the secure channel to a domain controller and
168 * see if they were valid.
171 /* rety loop for robustness */
173 for (i
= 0; !NT_STATUS_IS_OK(nt_status
) && (i
< 3); i
++) {
174 nt_status
= connect_to_domain_password_server(&cli
,
183 if ( !NT_STATUS_IS_OK(nt_status
) ) {
184 DEBUG(0,("domain_client_validate: Domain password server not available.\n"));
186 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_ACCESS_DENIED
)) {
187 return NT_STATUS_TRUSTED_RELATIONSHIP_FAILURE
;
192 /* store a successful connection */
194 saf_store(domain
, dc_name
);
197 * If this call succeeds, we now have lots of info about the user
198 * in the info3 structure.
201 nt_status
= rpccli_netlogon_network_logon(netlogon_creds
,
202 netlogon_pipe
->binding_handle
,
204 user_info
->logon_parameters
, /* flags such as 'allow workstation logon' */
205 user_info
->client
.account_name
, /* user name logging on. */
206 user_info
->client
.domain_name
, /* domain name */
207 user_info
->workstation_name
, /* workstation name */
208 chal
, /* 8 byte challenge. */
209 user_info
->password
.response
.lanman
, /* lanman 24 byte response */
210 user_info
->password
.response
.nt
, /* nt 24 byte response */
213 &info3
); /* info3 out */
215 /* Let go as soon as possible so we avoid any potential deadlocks
216 with winbind lookup up users or groups. */
220 if (!NT_STATUS_IS_OK(nt_status
)) {
221 DEBUG(0,("domain_client_validate: unable to validate password "
222 "for user %s in domain %s to Domain controller %s. "
223 "Error was %s.\n", user_info
->client
.account_name
,
224 user_info
->client
.domain_name
, dc_name
,
225 nt_errstr(nt_status
)));
227 /* map to something more useful */
228 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_UNSUCCESSFUL
)) {
229 nt_status
= NT_STATUS_NO_LOGON_SERVERS
;
232 nt_status
= make_server_info_info3(mem_ctx
,
233 user_info
->client
.account_name
,
238 if (NT_STATUS_IS_OK(nt_status
)) {
239 (*server_info
)->nss_token
|= user_info
->was_mapped
;
240 netsamlogon_cache_store(user_info
->client
.account_name
, info3
);
245 /* Note - once the cli stream is shutdown the mem_ctx used
246 to allocate the other_sids and gids structures has been deleted - so
247 these pointers are no longer valid..... */
254 /****************************************************************************
255 Check for a valid username and password in security=domain mode.
256 ****************************************************************************/
258 static NTSTATUS
check_ntdomain_security(const struct auth_context
*auth_context
,
259 void *my_private_data
,
261 const struct auth_usersupplied_info
*user_info
,
262 struct auth_serversupplied_info
**server_info
)
264 NTSTATUS nt_status
= NT_STATUS_LOGON_FAILURE
;
265 const char *domain
= lp_workgroup();
267 struct sockaddr_storage dc_ss
;
269 if ( lp_server_role() != ROLE_DOMAIN_MEMBER
) {
270 DEBUG(0,("check_ntdomain_security: Configuration error! Cannot use "
271 "ntdomain auth method when not a member of a domain.\n"));
272 return NT_STATUS_NOT_IMPLEMENTED
;
275 if (!user_info
|| !server_info
|| !auth_context
) {
276 DEBUG(1,("check_ntdomain_security: Critical variables not present. Failing.\n"));
277 return NT_STATUS_INVALID_PARAMETER
;
280 DEBUG(10, ("Check auth for: [%s]\n", user_info
->mapped
.account_name
));
283 * Check that the requested domain is not our own machine name.
284 * If it is, we should never check the PDC here, we use our own local
288 if(strequal(get_global_sam_name(), user_info
->mapped
.domain_name
)) {
289 DEBUG(3,("check_ntdomain_security: Requested domain was for this machine.\n"));
290 return NT_STATUS_NOT_IMPLEMENTED
;
293 /* we need our DC to send the net_sam_logon() request to */
295 if ( !get_dc_name(domain
, NULL
, dc_name
, &dc_ss
) ) {
296 DEBUG(5,("check_ntdomain_security: unable to locate a DC for domain %s\n",
297 user_info
->mapped
.domain_name
));
298 return NT_STATUS_NO_LOGON_SERVERS
;
301 nt_status
= domain_client_validate(mem_ctx
,
304 (uchar
*)auth_context
->challenge
.data
,
312 /* module initialisation */
313 static NTSTATUS
auth_init_ntdomain(struct auth_context
*auth_context
, const char* param
, auth_methods
**auth_method
)
315 struct auth_methods
*result
;
317 result
= talloc_zero(auth_context
, struct auth_methods
);
318 if (result
== NULL
) {
319 return NT_STATUS_NO_MEMORY
;
321 result
->name
= "ntdomain";
322 result
->auth
= check_ntdomain_security
;
324 *auth_method
= result
;
329 /****************************************************************************
330 Check for a valid username and password in a trusted domain
331 ****************************************************************************/
333 static NTSTATUS
check_trustdomain_security(const struct auth_context
*auth_context
,
334 void *my_private_data
,
336 const struct auth_usersupplied_info
*user_info
,
337 struct auth_serversupplied_info
**server_info
)
339 NTSTATUS nt_status
= NT_STATUS_LOGON_FAILURE
;
341 struct sockaddr_storage dc_ss
;
343 if (!user_info
|| !server_info
|| !auth_context
) {
344 DEBUG(1,("check_trustdomain_security: Critical variables not present. Failing.\n"));
345 return NT_STATUS_INVALID_PARAMETER
;
348 DEBUG(10, ("Check auth for: [%s]\n", user_info
->mapped
.account_name
));
351 * Check that the requested domain is not our own machine name or domain name.
354 if( strequal(get_global_sam_name(), user_info
->mapped
.domain_name
)) {
355 DEBUG(3,("check_trustdomain_security: Requested domain [%s] was for this machine.\n",
356 user_info
->mapped
.domain_name
));
357 return NT_STATUS_NOT_IMPLEMENTED
;
360 /* No point is bothering if this is not a trusted domain.
361 This return makes "map to guest = bad user" work again.
362 The logic is that if we know nothing about the domain, that
363 user is not known to us and does not exist */
365 if ( !is_trusted_domain( user_info
->mapped
.domain_name
) )
366 return NT_STATUS_NOT_IMPLEMENTED
;
368 /* use get_dc_name() for consistency even through we know that it will be
371 if ( !get_dc_name(user_info
->mapped
.domain_name
, NULL
, dc_name
, &dc_ss
) ) {
372 DEBUG(5,("check_trustdomain_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
,
379 user_info
->mapped
.domain_name
,
380 (uchar
*)auth_context
->challenge
.data
,
388 /* module initialisation */
389 static NTSTATUS
auth_init_trustdomain(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
= "trustdomain";
398 result
->auth
= check_trustdomain_security
;
400 *auth_method
= result
;
404 NTSTATUS
auth_domain_init(void)
406 smb_register_auth(AUTH_INTERFACE_VERSION
, "trustdomain", auth_init_trustdomain
);
407 smb_register_auth(AUTH_INTERFACE_VERSION
, "ntdomain", auth_init_ntdomain
);