docs: remove more duplicate options from samba-regedit manpage.
[Samba.git] / source3 / auth / auth_domain.c
blob9f88c4a4119f09fef6ea14dc25c96bc685c1540c
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"
31 #undef DBGC_CLASS
32 #define DBGC_CLASS DBGC_AUTH
34 static struct named_mutex *mutex;
36 /**
37 * Connect to a remote server for (inter)domain security authenticaion.
39 * @param cli the cli to return containing the active connection
40 * @param server either a machine name or text IP address to
41 * connect to.
42 * @param setup_creds_as domain account to setup credentials as
43 * @param sec_chan a switch value to distinguish between domain
44 * member and interdomain authentication
45 * @param trust_passwd the trust password to establish the
46 * credentials with.
48 **/
50 static NTSTATUS connect_to_domain_password_server(struct cli_state **cli,
51 const char *domain,
52 const char *dc_name,
53 const struct sockaddr_storage *dc_ss,
54 struct rpc_pipe_client **pipe_ret)
56 NTSTATUS result;
57 struct rpc_pipe_client *netlogon_pipe = NULL;
59 *cli = NULL;
61 *pipe_ret = NULL;
63 /* TODO: Send a SAMLOGON request to determine whether this is a valid
64 logonserver. We can avoid a 30-second timeout if the DC is down
65 if the SAMLOGON request fails as it is only over UDP. */
67 /* we use a mutex to prevent two connections at once - when a
68 Win2k PDC get two connections where one hasn't completed a
69 session setup yet it will send a TCP reset to the first
70 connection (tridge) */
73 * With NT4.x DC's *all* authentication must be serialized to avoid
74 * ACCESS_DENIED errors if 2 auths are done from the same machine. JRA.
77 mutex = grab_named_mutex(NULL, dc_name, 10);
78 if (mutex == NULL) {
79 return NT_STATUS_NO_LOGON_SERVERS;
82 /* Attempt connection */
83 result = cli_full_connection(cli, lp_netbios_name(), dc_name, dc_ss, 0,
84 "IPC$", "IPC", "", "", "", 0, SMB_SIGNING_DEFAULT);
86 if (!NT_STATUS_IS_OK(result)) {
87 /* map to something more useful */
88 if (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)) {
89 result = NT_STATUS_NO_LOGON_SERVERS;
92 if (*cli) {
93 cli_shutdown(*cli);
94 *cli = NULL;
97 TALLOC_FREE(mutex);
98 return result;
102 * We now have an anonymous connection to IPC$ on the domain password server.
106 * Even if the connect succeeds we need to setup the netlogon
107 * pipe here. We do this as we may just have changed the domain
108 * account password on the PDC and yet we may be talking to
109 * a BDC that doesn't have this replicated yet. In this case
110 * a successful connect to a DC needs to take the netlogon connect
111 * into account also. This patch from "Bjart Kvarme" <bjart.kvarme@usit.uio.no>.
114 /* open the netlogon pipe. */
115 if (lp_client_schannel()) {
116 /* We also setup the creds chain in the open_schannel call. */
117 result = cli_rpc_pipe_open_schannel(
118 *cli, &ndr_table_netlogon, NCACN_NP,
119 DCERPC_AUTH_LEVEL_PRIVACY, domain, &netlogon_pipe);
120 } else {
121 result = cli_rpc_pipe_open_noauth(
122 *cli, &ndr_table_netlogon, &netlogon_pipe);
125 if (!NT_STATUS_IS_OK(result)) {
126 DEBUG(0,("connect_to_domain_password_server: unable to open the domain client session to \
127 machine %s. Error was : %s.\n", dc_name, nt_errstr(result)));
128 cli_shutdown(*cli);
129 *cli = NULL;
130 TALLOC_FREE(mutex);
131 return result;
134 if (!lp_client_schannel()) {
135 /* We need to set up a creds chain on an unauthenticated netlogon pipe. */
136 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS |
137 NETLOGON_NEG_SUPPORTS_AES;
138 enum netr_SchannelType sec_chan_type = 0;
139 unsigned char machine_pwd[16];
140 const char *account_name;
142 if (!get_trust_pw_hash(domain, machine_pwd, &account_name,
143 &sec_chan_type))
145 DEBUG(0, ("connect_to_domain_password_server: could not fetch "
146 "trust account password for domain '%s'\n",
147 domain));
148 cli_shutdown(*cli);
149 *cli = NULL;
150 TALLOC_FREE(mutex);
151 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
154 result = rpccli_netlogon_setup_creds(netlogon_pipe,
155 dc_name, /* server name */
156 domain, /* domain */
157 lp_netbios_name(), /* client name */
158 account_name, /* machine account name */
159 machine_pwd,
160 sec_chan_type,
161 &neg_flags);
163 if (!NT_STATUS_IS_OK(result)) {
164 cli_shutdown(*cli);
165 *cli = NULL;
166 TALLOC_FREE(mutex);
167 return result;
171 if(!netlogon_pipe) {
172 DEBUG(0, ("connect_to_domain_password_server: unable to open "
173 "the domain client session to machine %s. Error "
174 "was : %s.\n", dc_name, nt_errstr(result)));
175 cli_shutdown(*cli);
176 *cli = NULL;
177 TALLOC_FREE(mutex);
178 return NT_STATUS_NO_LOGON_SERVERS;
181 /* We exit here with the mutex *locked*. JRA */
183 *pipe_ret = netlogon_pipe;
185 return NT_STATUS_OK;
188 /***********************************************************************
189 Do the same as security=server, but using NT Domain calls and a session
190 key from the machine password. If the server parameter is specified
191 use it, otherwise figure out a server from the 'password server' param.
192 ************************************************************************/
194 static NTSTATUS domain_client_validate(TALLOC_CTX *mem_ctx,
195 const struct auth_usersupplied_info *user_info,
196 const char *domain,
197 uchar chal[8],
198 struct auth_serversupplied_info **server_info,
199 const char *dc_name,
200 const struct sockaddr_storage *dc_ss)
203 struct netr_SamInfo3 *info3 = NULL;
204 struct cli_state *cli = NULL;
205 struct rpc_pipe_client *netlogon_pipe = NULL;
206 NTSTATUS nt_status = NT_STATUS_NO_LOGON_SERVERS;
207 int i;
210 * At this point, smb_apasswd points to the lanman response to
211 * the challenge in local_challenge, and smb_ntpasswd points to
212 * the NT response to the challenge in local_challenge. Ship
213 * these over the secure channel to a domain controller and
214 * see if they were valid.
217 /* rety loop for robustness */
219 for (i = 0; !NT_STATUS_IS_OK(nt_status) && (i < 3); i++) {
220 nt_status = connect_to_domain_password_server(&cli,
221 domain,
222 dc_name,
223 dc_ss,
224 &netlogon_pipe);
227 if ( !NT_STATUS_IS_OK(nt_status) ) {
228 DEBUG(0,("domain_client_validate: Domain password server not available.\n"));
229 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
230 return NT_STATUS_TRUSTED_RELATIONSHIP_FAILURE;
232 return nt_status;
235 /* store a successful connection */
237 saf_store(domain, dc_name);
240 * If this call succeeds, we now have lots of info about the user
241 * in the info3 structure.
244 nt_status = rpccli_netlogon_sam_network_logon(netlogon_pipe,
245 mem_ctx,
246 user_info->logon_parameters, /* flags such as 'allow workstation logon' */
247 dc_name, /* server name */
248 user_info->client.account_name, /* user name logging on. */
249 user_info->client.domain_name, /* domain name */
250 user_info->workstation_name, /* workstation name */
251 chal, /* 8 byte challenge. */
252 3, /* validation level */
253 user_info->password.response.lanman, /* lanman 24 byte response */
254 user_info->password.response.nt, /* nt 24 byte response */
255 &info3); /* info3 out */
257 /* Let go as soon as possible so we avoid any potential deadlocks
258 with winbind lookup up users or groups. */
260 TALLOC_FREE(mutex);
262 if (!NT_STATUS_IS_OK(nt_status)) {
263 DEBUG(0,("domain_client_validate: unable to validate password "
264 "for user %s in domain %s to Domain controller %s. "
265 "Error was %s.\n", user_info->client.account_name,
266 user_info->client.domain_name, dc_name,
267 nt_errstr(nt_status)));
269 /* map to something more useful */
270 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_UNSUCCESSFUL)) {
271 nt_status = NT_STATUS_NO_LOGON_SERVERS;
273 } else {
274 nt_status = make_server_info_info3(mem_ctx,
275 user_info->client.account_name,
276 domain,
277 server_info,
278 info3);
280 if (NT_STATUS_IS_OK(nt_status)) {
281 (*server_info)->nss_token |= user_info->was_mapped;
282 netsamlogon_cache_store(user_info->client.account_name, info3);
283 TALLOC_FREE(info3);
287 /* Note - once the cli stream is shutdown the mem_ctx used
288 to allocate the other_sids and gids structures has been deleted - so
289 these pointers are no longer valid..... */
291 cli_shutdown(cli);
292 return nt_status;
295 /****************************************************************************
296 Check for a valid username and password in security=domain mode.
297 ****************************************************************************/
299 static NTSTATUS check_ntdomain_security(const struct auth_context *auth_context,
300 void *my_private_data,
301 TALLOC_CTX *mem_ctx,
302 const struct auth_usersupplied_info *user_info,
303 struct auth_serversupplied_info **server_info)
305 NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
306 const char *domain = lp_workgroup();
307 fstring dc_name;
308 struct sockaddr_storage dc_ss;
310 if ( lp_server_role() != ROLE_DOMAIN_MEMBER ) {
311 DEBUG(0,("check_ntdomain_security: Configuration error! Cannot use "
312 "ntdomain auth method when not a member of a domain.\n"));
313 return NT_STATUS_NOT_IMPLEMENTED;
316 if (!user_info || !server_info || !auth_context) {
317 DEBUG(1,("check_ntdomain_security: Critical variables not present. Failing.\n"));
318 return NT_STATUS_INVALID_PARAMETER;
321 DEBUG(10, ("Check auth for: [%s]\n", user_info->mapped.account_name));
324 * Check that the requested domain is not our own machine name.
325 * If it is, we should never check the PDC here, we use our own local
326 * password file.
329 if(strequal(get_global_sam_name(), user_info->mapped.domain_name)) {
330 DEBUG(3,("check_ntdomain_security: Requested domain was for this machine.\n"));
331 return NT_STATUS_NOT_IMPLEMENTED;
334 /* we need our DC to send the net_sam_logon() request to */
336 if ( !get_dc_name(domain, NULL, dc_name, &dc_ss) ) {
337 DEBUG(5,("check_ntdomain_security: unable to locate a DC for domain %s\n",
338 user_info->mapped.domain_name));
339 return NT_STATUS_NO_LOGON_SERVERS;
342 nt_status = domain_client_validate(mem_ctx,
343 user_info,
344 domain,
345 (uchar *)auth_context->challenge.data,
346 server_info,
347 dc_name,
348 &dc_ss);
350 return nt_status;
353 /* module initialisation */
354 static NTSTATUS auth_init_ntdomain(struct auth_context *auth_context, const char* param, auth_methods **auth_method)
356 struct auth_methods *result;
358 result = talloc_zero(auth_context, struct auth_methods);
359 if (result == NULL) {
360 return NT_STATUS_NO_MEMORY;
362 result->name = "ntdomain";
363 result->auth = check_ntdomain_security;
365 *auth_method = result;
366 return NT_STATUS_OK;
370 /****************************************************************************
371 Check for a valid username and password in a trusted domain
372 ****************************************************************************/
374 static NTSTATUS check_trustdomain_security(const struct auth_context *auth_context,
375 void *my_private_data,
376 TALLOC_CTX *mem_ctx,
377 const struct auth_usersupplied_info *user_info,
378 struct auth_serversupplied_info **server_info)
380 NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
381 fstring dc_name;
382 struct sockaddr_storage dc_ss;
384 if (!user_info || !server_info || !auth_context) {
385 DEBUG(1,("check_trustdomain_security: Critical variables not present. Failing.\n"));
386 return NT_STATUS_INVALID_PARAMETER;
389 DEBUG(10, ("Check auth for: [%s]\n", user_info->mapped.account_name));
392 * Check that the requested domain is not our own machine name or domain name.
395 if( strequal(get_global_sam_name(), user_info->mapped.domain_name)) {
396 DEBUG(3,("check_trustdomain_security: Requested domain [%s] was for this machine.\n",
397 user_info->mapped.domain_name));
398 return NT_STATUS_NOT_IMPLEMENTED;
401 /* No point is bothering if this is not a trusted domain.
402 This return makes "map to guest = bad user" work again.
403 The logic is that if we know nothing about the domain, that
404 user is not known to us and does not exist */
406 if ( !is_trusted_domain( user_info->mapped.domain_name ) )
407 return NT_STATUS_NOT_IMPLEMENTED;
409 /* use get_dc_name() for consistency even through we know that it will be
410 a netbios name */
412 if ( !get_dc_name(user_info->mapped.domain_name, NULL, dc_name, &dc_ss) ) {
413 DEBUG(5,("check_trustdomain_security: unable to locate a DC for domain %s\n",
414 user_info->mapped.domain_name));
415 return NT_STATUS_NO_LOGON_SERVERS;
418 nt_status = domain_client_validate(mem_ctx,
419 user_info,
420 user_info->mapped.domain_name,
421 (uchar *)auth_context->challenge.data,
422 server_info,
423 dc_name,
424 &dc_ss);
426 return nt_status;
429 /* module initialisation */
430 static NTSTATUS auth_init_trustdomain(struct auth_context *auth_context, const char* param, auth_methods **auth_method)
432 struct auth_methods *result;
434 result = talloc_zero(auth_context, struct auth_methods);
435 if (result == NULL) {
436 return NT_STATUS_NO_MEMORY;
438 result->name = "trustdomain";
439 result->auth = check_trustdomain_security;
441 *auth_method = result;
442 return NT_STATUS_OK;
445 NTSTATUS auth_domain_init(void)
447 smb_register_auth(AUTH_INTERFACE_VERSION, "trustdomain", auth_init_trustdomain);
448 smb_register_auth(AUTH_INTERFACE_VERSION, "ntdomain", auth_init_ntdomain);
449 return NT_STATUS_OK;