s4:libcli/smb2: make smb2_session_setup_spnego_* completely async
[Samba.git] / source3 / auth / auth_domain.c
blob40d717d91a9bed5c4bd00814d987b47765b9d213
1 /*
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/>.
21 #include "includes.h"
22 #include "auth.h"
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"
27 #include "secrets.h"
28 #include "passdb.h"
29 #include "libsmb/libsmb.h"
30 #include "libcli/auth/netlogon_creds_cli.h"
31 #include "libsmb/samlogon_cache.h"
33 #undef DBGC_CLASS
34 #define DBGC_CLASS DBGC_AUTH
36 static struct named_mutex *mutex;
38 /**
39 * Connect to a remote server for (inter)domain security authenticaion.
41 * @param cli the cli to return containing the active connection
42 * @param server either a machine name or text IP address to
43 * connect to.
44 * @param setup_creds_as domain account to setup credentials as
45 * @param sec_chan a switch value to distinguish between domain
46 * member and interdomain authentication
47 * @param trust_passwd the trust password to establish the
48 * credentials with.
50 **/
52 static NTSTATUS connect_to_domain_password_server(struct cli_state **cli_ret,
53 const char *domain,
54 const char *dc_name,
55 const struct sockaddr_storage *dc_ss,
56 struct rpc_pipe_client **pipe_ret,
57 TALLOC_CTX *mem_ctx,
58 struct netlogon_creds_cli_context **creds_ret)
60 TALLOC_CTX *frame = talloc_stackframe();
61 struct messaging_context *msg_ctx = server_messaging_context();
62 NTSTATUS result;
63 struct cli_state *cli = NULL;
64 struct rpc_pipe_client *netlogon_pipe = NULL;
65 struct netlogon_creds_cli_context *netlogon_creds = NULL;
67 *cli_ret = NULL;
68 *pipe_ret = NULL;
69 *creds_ret = NULL;
71 /* TODO: Send a SAMLOGON request to determine whether this is a valid
72 logonserver. We can avoid a 30-second timeout if the DC is down
73 if the SAMLOGON request fails as it is only over UDP. */
75 /* we use a mutex to prevent two connections at once - when a
76 Win2k PDC get two connections where one hasn't completed a
77 session setup yet it will send a TCP reset to the first
78 connection (tridge) */
81 * With NT4.x DC's *all* authentication must be serialized to avoid
82 * ACCESS_DENIED errors if 2 auths are done from the same machine. JRA.
85 mutex = grab_named_mutex(NULL, dc_name, 10);
86 if (mutex == NULL) {
87 TALLOC_FREE(frame);
88 return NT_STATUS_NO_LOGON_SERVERS;
91 /* Attempt connection */
92 result = cli_full_connection(&cli, lp_netbios_name(), dc_name, dc_ss, 0,
93 "IPC$", "IPC", "", "", "", 0, SMB_SIGNING_IPC_DEFAULT);
95 if (!NT_STATUS_IS_OK(result)) {
96 /* map to something more useful */
97 if (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)) {
98 result = NT_STATUS_NO_LOGON_SERVERS;
101 TALLOC_FREE(mutex);
102 TALLOC_FREE(frame);
103 return result;
107 * We now have an anonymous connection to IPC$ on the domain password server.
110 result = cli_rpc_pipe_open_schannel(cli,
111 msg_ctx,
112 &ndr_table_netlogon,
113 NCACN_NP,
114 domain,
115 &netlogon_pipe,
116 frame,
117 &netlogon_creds);
118 if (!NT_STATUS_IS_OK(result)) {
119 DEBUG(0,("connect_to_domain_password_server: "
120 "unable to open the domain client session to "
121 "machine %s. Error was : %s.\n",
122 dc_name, nt_errstr(result)));
123 cli_shutdown(cli);
124 TALLOC_FREE(mutex);
125 TALLOC_FREE(frame);
126 return NT_STATUS_NO_LOGON_SERVERS;
129 /* We exit here with the mutex *locked*. JRA */
131 *cli_ret = cli;
132 *pipe_ret = netlogon_pipe;
133 *creds_ret = talloc_move(mem_ctx, &netlogon_creds);
135 TALLOC_FREE(frame);
136 return NT_STATUS_OK;
139 /***********************************************************************
140 Do the same as security=server, but using NT Domain calls and a session
141 key from the machine password. If the server parameter is specified
142 use it, otherwise figure out a server from the 'password server' param.
143 ************************************************************************/
145 static NTSTATUS domain_client_validate(TALLOC_CTX *mem_ctx,
146 const struct auth_usersupplied_info *user_info,
147 const char *domain,
148 uchar chal[8],
149 struct auth_serversupplied_info **server_info,
150 const char *dc_name,
151 const struct sockaddr_storage *dc_ss)
154 TALLOC_CTX *frame = talloc_stackframe();
155 struct netr_SamInfo3 *info3 = NULL;
156 struct cli_state *cli = NULL;
157 struct rpc_pipe_client *netlogon_pipe = NULL;
158 struct netlogon_creds_cli_context *netlogon_creds = NULL;
159 NTSTATUS nt_status = NT_STATUS_NO_LOGON_SERVERS;
160 int i;
161 uint8_t authoritative = 0;
162 uint32_t flags = 0;
165 * At this point, smb_apasswd points to the lanman response to
166 * the challenge in local_challenge, and smb_ntpasswd points to
167 * the NT response to the challenge in local_challenge. Ship
168 * these over the secure channel to a domain controller and
169 * see if they were valid.
172 /* rety loop for robustness */
174 for (i = 0; !NT_STATUS_IS_OK(nt_status) && (i < 3); i++) {
175 nt_status = connect_to_domain_password_server(&cli,
176 domain,
177 dc_name,
178 dc_ss,
179 &netlogon_pipe,
180 frame,
181 &netlogon_creds);
184 if ( !NT_STATUS_IS_OK(nt_status) ) {
185 DEBUG(0,("domain_client_validate: Domain password server not available.\n"));
186 TALLOC_FREE(frame);
187 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
188 return NT_STATUS_TRUSTED_RELATIONSHIP_FAILURE;
190 return nt_status;
193 /* store a successful connection */
195 saf_store(domain, dc_name);
198 * If this call succeeds, we now have lots of info about the user
199 * in the info3 structure.
202 nt_status = rpccli_netlogon_network_logon(netlogon_creds,
203 netlogon_pipe->binding_handle,
204 mem_ctx,
205 user_info->logon_parameters, /* flags such as 'allow workstation logon' */
206 user_info->client.account_name, /* user name logging on. */
207 user_info->client.domain_name, /* domain name */
208 user_info->workstation_name, /* workstation name */
209 chal, /* 8 byte challenge. */
210 user_info->password.response.lanman, /* lanman 24 byte response */
211 user_info->password.response.nt, /* nt 24 byte response */
212 &authoritative,
213 &flags,
214 &info3); /* info3 out */
216 /* Let go as soon as possible so we avoid any potential deadlocks
217 with winbind lookup up users or groups. */
219 TALLOC_FREE(mutex);
221 if (!NT_STATUS_IS_OK(nt_status)) {
222 DEBUG(0,("domain_client_validate: unable to validate password "
223 "for user %s in domain %s to Domain controller %s. "
224 "Error was %s.\n", user_info->client.account_name,
225 user_info->client.domain_name, dc_name,
226 nt_errstr(nt_status)));
228 /* map to something more useful */
229 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_UNSUCCESSFUL)) {
230 nt_status = NT_STATUS_NO_LOGON_SERVERS;
232 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER) &&
233 (authoritative == 0)) {
234 nt_status = NT_STATUS_NOT_IMPLEMENTED;
236 } else {
237 nt_status = make_server_info_info3(mem_ctx,
238 user_info->client.account_name,
239 domain,
240 server_info,
241 info3);
243 if (NT_STATUS_IS_OK(nt_status)) {
244 (*server_info)->nss_token |= user_info->was_mapped;
245 netsamlogon_cache_store(user_info->client.account_name, info3);
246 TALLOC_FREE(info3);
250 /* Note - once the cli stream is shutdown the mem_ctx used
251 to allocate the other_sids and gids structures has been deleted - so
252 these pointers are no longer valid..... */
254 cli_shutdown(cli);
255 TALLOC_FREE(frame);
256 return nt_status;
259 /****************************************************************************
260 Check for a valid username and password in security=domain mode.
261 ****************************************************************************/
263 static NTSTATUS check_ntdomain_security(const struct auth_context *auth_context,
264 void *my_private_data,
265 TALLOC_CTX *mem_ctx,
266 const struct auth_usersupplied_info *user_info,
267 struct auth_serversupplied_info **server_info)
269 NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
270 const char *domain = lp_workgroup();
271 fstring dc_name;
272 struct sockaddr_storage dc_ss;
274 if ( lp_server_role() != ROLE_DOMAIN_MEMBER ) {
275 DEBUG(0,("check_ntdomain_security: Configuration error! Cannot use "
276 "ntdomain auth method when not a member of a domain.\n"));
277 return NT_STATUS_NOT_IMPLEMENTED;
280 if (!user_info || !server_info || !auth_context) {
281 DEBUG(1,("check_ntdomain_security: Critical variables not present. Failing.\n"));
282 return NT_STATUS_INVALID_PARAMETER;
285 DEBUG(10, ("Check auth for: [%s]\n", user_info->mapped.account_name));
288 * Check that the requested domain is not our own machine name.
289 * If it is, we should never check the PDC here, we use our own local
290 * password file.
293 if(strequal(get_global_sam_name(), user_info->mapped.domain_name)) {
294 DEBUG(3,("check_ntdomain_security: Requested domain was for this machine.\n"));
295 return NT_STATUS_NOT_IMPLEMENTED;
298 /* we need our DC to send the net_sam_logon() request to */
300 if ( !get_dc_name(domain, NULL, dc_name, &dc_ss) ) {
301 DEBUG(5,("check_ntdomain_security: unable to locate a DC for domain %s\n",
302 user_info->mapped.domain_name));
303 return NT_STATUS_NO_LOGON_SERVERS;
306 nt_status = domain_client_validate(mem_ctx,
307 user_info,
308 domain,
309 (uchar *)auth_context->challenge.data,
310 server_info,
311 dc_name,
312 &dc_ss);
314 return nt_status;
317 /* module initialisation */
318 static NTSTATUS auth_init_ntdomain(struct auth_context *auth_context, const char* param, auth_methods **auth_method)
320 struct auth_methods *result;
322 result = talloc_zero(auth_context, struct auth_methods);
323 if (result == NULL) {
324 return NT_STATUS_NO_MEMORY;
326 result->name = "ntdomain";
327 result->auth = check_ntdomain_security;
329 *auth_method = result;
330 return NT_STATUS_OK;
334 /****************************************************************************
335 Check for a valid username and password in a trusted domain
336 ****************************************************************************/
338 static NTSTATUS check_trustdomain_security(const struct auth_context *auth_context,
339 void *my_private_data,
340 TALLOC_CTX *mem_ctx,
341 const struct auth_usersupplied_info *user_info,
342 struct auth_serversupplied_info **server_info)
344 NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
345 fstring dc_name;
346 struct sockaddr_storage dc_ss;
348 if (!user_info || !server_info || !auth_context) {
349 DEBUG(1,("check_trustdomain_security: Critical variables not present. Failing.\n"));
350 return NT_STATUS_INVALID_PARAMETER;
353 DEBUG(10, ("Check auth for: [%s]\n", user_info->mapped.account_name));
356 * Check that the requested domain is not our own machine name or domain name.
359 if( strequal(get_global_sam_name(), user_info->mapped.domain_name)) {
360 DEBUG(3,("check_trustdomain_security: Requested domain [%s] was for this machine.\n",
361 user_info->mapped.domain_name));
362 return NT_STATUS_NOT_IMPLEMENTED;
365 /* No point is bothering if this is not a trusted domain.
366 This return makes "map to guest = bad user" work again.
367 The logic is that if we know nothing about the domain, that
368 user is not known to us and does not exist */
370 if ( !is_trusted_domain( user_info->mapped.domain_name ) )
371 return NT_STATUS_NOT_IMPLEMENTED;
373 /* use get_dc_name() for consistency even through we know that it will be
374 a netbios name */
376 if ( !get_dc_name(user_info->mapped.domain_name, NULL, dc_name, &dc_ss) ) {
377 DEBUG(5,("check_trustdomain_security: unable to locate a DC for domain %s\n",
378 user_info->mapped.domain_name));
379 return NT_STATUS_NO_LOGON_SERVERS;
382 nt_status = domain_client_validate(mem_ctx,
383 user_info,
384 user_info->mapped.domain_name,
385 (uchar *)auth_context->challenge.data,
386 server_info,
387 dc_name,
388 &dc_ss);
390 return nt_status;
393 /* module initialisation */
394 static NTSTATUS auth_init_trustdomain(struct auth_context *auth_context, const char* param, auth_methods **auth_method)
396 struct auth_methods *result;
398 result = talloc_zero(auth_context, struct auth_methods);
399 if (result == NULL) {
400 return NT_STATUS_NO_MEMORY;
402 result->name = "trustdomain";
403 result->auth = check_trustdomain_security;
405 *auth_method = result;
406 return NT_STATUS_OK;
409 NTSTATUS auth_domain_init(TALLOC_CTX *mem_ctx)
411 smb_register_auth(AUTH_INTERFACE_VERSION, "trustdomain", auth_init_trustdomain);
412 smb_register_auth(AUTH_INTERFACE_VERSION, "ntdomain", auth_init_ntdomain);
413 return NT_STATUS_OK;