WHATSNEW: Update changes since 3.3.0pre1 and release date.
[Samba.git] / source / auth / auth_domain.c
blobc25e62ab809310a213789954014e674620c62ba4
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"
23 #undef DBGC_CLASS
24 #define DBGC_CLASS DBGC_AUTH
26 extern bool global_machine_password_needs_changing;
27 static struct named_mutex *mutex;
29 /**
30 * Connect to a remote server for (inter)domain security authenticaion.
32 * @param cli the cli to return containing the active connection
33 * @param server either a machine name or text IP address to
34 * connect to.
35 * @param setup_creds_as domain account to setup credentials as
36 * @param sec_chan a switch value to distinguish between domain
37 * member and interdomain authentication
38 * @param trust_passwd the trust password to establish the
39 * credentials with.
41 **/
43 static NTSTATUS connect_to_domain_password_server(struct cli_state **cli,
44 const char *domain,
45 const char *dc_name,
46 struct sockaddr_storage *dc_ss,
47 struct rpc_pipe_client **pipe_ret,
48 bool *retry)
50 NTSTATUS result;
51 struct rpc_pipe_client *netlogon_pipe = NULL;
53 *cli = NULL;
55 *pipe_ret = NULL;
57 /* TODO: Send a SAMLOGON request to determine whether this is a valid
58 logonserver. We can avoid a 30-second timeout if the DC is down
59 if the SAMLOGON request fails as it is only over UDP. */
61 /* we use a mutex to prevent two connections at once - when a
62 Win2k PDC get two connections where one hasn't completed a
63 session setup yet it will send a TCP reset to the first
64 connection (tridge) */
67 * With NT4.x DC's *all* authentication must be serialized to avoid
68 * ACCESS_DENIED errors if 2 auths are done from the same machine. JRA.
71 mutex = grab_named_mutex(NULL, dc_name, 10);
72 if (mutex == NULL) {
73 return NT_STATUS_NO_LOGON_SERVERS;
76 /* Attempt connection */
77 *retry = True;
78 result = cli_full_connection(cli, global_myname(), dc_name, dc_ss, 0,
79 "IPC$", "IPC", "", "", "", 0, Undefined, retry);
81 if (!NT_STATUS_IS_OK(result)) {
82 /* map to something more useful */
83 if (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)) {
84 result = NT_STATUS_NO_LOGON_SERVERS;
87 if (*cli) {
88 cli_shutdown(*cli);
89 *cli = NULL;
92 TALLOC_FREE(mutex);
93 return result;
97 * We now have an anonymous connection to IPC$ on the domain password server.
101 * Even if the connect succeeds we need to setup the netlogon
102 * pipe here. We do this as we may just have changed the domain
103 * account password on the PDC and yet we may be talking to
104 * a BDC that doesn't have this replicated yet. In this case
105 * a successful connect to a DC needs to take the netlogon connect
106 * into account also. This patch from "Bjart Kvarme" <bjart.kvarme@usit.uio.no>.
109 /* open the netlogon pipe. */
110 if (lp_client_schannel()) {
111 /* We also setup the creds chain in the open_schannel call. */
112 result = cli_rpc_pipe_open_schannel(
113 *cli, &ndr_table_netlogon.syntax_id,
114 PIPE_AUTH_LEVEL_PRIVACY, domain, &netlogon_pipe);
115 } else {
116 result = cli_rpc_pipe_open_noauth(
117 *cli, &ndr_table_netlogon.syntax_id, &netlogon_pipe);
120 if (!NT_STATUS_IS_OK(result)) {
121 DEBUG(0,("connect_to_domain_password_server: unable to open the domain client session to \
122 machine %s. Error was : %s.\n", dc_name, nt_errstr(result)));
123 cli_shutdown(*cli);
124 *cli = NULL;
125 TALLOC_FREE(mutex);
126 return result;
129 if (!lp_client_schannel()) {
130 /* We need to set up a creds chain on an unauthenticated netlogon pipe. */
131 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
132 uint32 sec_chan_type = 0;
133 unsigned char machine_pwd[16];
134 const char *account_name;
136 if (!get_trust_pw_hash(domain, machine_pwd, &account_name,
137 &sec_chan_type))
139 DEBUG(0, ("connect_to_domain_password_server: could not fetch "
140 "trust account password for domain '%s'\n",
141 domain));
142 cli_shutdown(*cli);
143 *cli = NULL;
144 TALLOC_FREE(mutex);
145 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
148 result = rpccli_netlogon_setup_creds(netlogon_pipe,
149 dc_name, /* server name */
150 domain, /* domain */
151 global_myname(), /* client name */
152 account_name, /* machine account name */
153 machine_pwd,
154 sec_chan_type,
155 &neg_flags);
157 if (!NT_STATUS_IS_OK(result)) {
158 cli_shutdown(*cli);
159 *cli = NULL;
160 TALLOC_FREE(mutex);
161 return result;
165 if(!netlogon_pipe) {
166 DEBUG(0,("connect_to_domain_password_server: unable to open the domain client session to \
167 machine %s. Error was : %s.\n", dc_name, cli_errstr(*cli)));
168 cli_shutdown(*cli);
169 *cli = NULL;
170 TALLOC_FREE(mutex);
171 return NT_STATUS_NO_LOGON_SERVERS;
174 /* We exit here with the mutex *locked*. JRA */
176 *pipe_ret = netlogon_pipe;
178 return NT_STATUS_OK;
181 /***********************************************************************
182 Do the same as security=server, but using NT Domain calls and a session
183 key from the machine password. If the server parameter is specified
184 use it, otherwise figure out a server from the 'password server' param.
185 ************************************************************************/
187 static NTSTATUS domain_client_validate(TALLOC_CTX *mem_ctx,
188 const auth_usersupplied_info *user_info,
189 const char *domain,
190 uchar chal[8],
191 auth_serversupplied_info **server_info,
192 const char *dc_name,
193 struct sockaddr_storage *dc_ss)
196 struct netr_SamInfo3 *info3 = NULL;
197 struct cli_state *cli = NULL;
198 struct rpc_pipe_client *netlogon_pipe = NULL;
199 NTSTATUS nt_status = NT_STATUS_NO_LOGON_SERVERS;
200 int i;
201 bool retry = True;
204 * At this point, smb_apasswd points to the lanman response to
205 * the challenge in local_challenge, and smb_ntpasswd points to
206 * the NT response to the challenge in local_challenge. Ship
207 * these over the secure channel to a domain controller and
208 * see if they were valid.
211 /* rety loop for robustness */
213 for (i = 0; !NT_STATUS_IS_OK(nt_status) && retry && (i < 3); i++) {
214 nt_status = connect_to_domain_password_server(&cli,
215 domain,
216 dc_name,
217 dc_ss,
218 &netlogon_pipe,
219 &retry);
222 if ( !NT_STATUS_IS_OK(nt_status) ) {
223 DEBUG(0,("domain_client_validate: Domain password server not available.\n"));
224 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
225 return NT_STATUS_TRUSTED_RELATIONSHIP_FAILURE;
227 return nt_status;
230 /* store a successful connection */
232 saf_store( domain, cli->desthost );
235 * If this call succeeds, we now have lots of info about the user
236 * in the info3 structure.
239 nt_status = rpccli_netlogon_sam_network_logon(netlogon_pipe,
240 mem_ctx,
241 user_info->logon_parameters,/* flags such as 'allow workstation logon' */
242 dc_name, /* server name */
243 user_info->smb_name, /* user name logging on. */
244 user_info->client_domain, /* domain name */
245 user_info->wksta_name, /* workstation name */
246 chal, /* 8 byte challenge. */
247 user_info->lm_resp, /* lanman 24 byte response */
248 user_info->nt_resp, /* nt 24 byte response */
249 &info3); /* info3 out */
251 /* Let go as soon as possible so we avoid any potential deadlocks
252 with winbind lookup up users or groups. */
254 TALLOC_FREE(mutex);
256 if (!NT_STATUS_IS_OK(nt_status)) {
257 DEBUG(0,("domain_client_validate: unable to validate password "
258 "for user %s in domain %s to Domain controller %s. "
259 "Error was %s.\n", user_info->smb_name,
260 user_info->client_domain, dc_name,
261 nt_errstr(nt_status)));
263 /* map to something more useful */
264 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_UNSUCCESSFUL)) {
265 nt_status = NT_STATUS_NO_LOGON_SERVERS;
267 } else {
268 nt_status = make_server_info_info3(mem_ctx,
269 user_info->smb_name,
270 domain,
271 server_info,
272 info3);
274 if (NT_STATUS_IS_OK(nt_status)) {
275 (*server_info)->nss_token |= user_info->was_mapped;
277 if ( ! (*server_info)->guest) {
278 /* if a real user check pam account restrictions */
279 /* only really perfomed if "obey pam restriction" is true */
280 nt_status = smb_pam_accountcheck((*server_info)->unix_name);
281 if ( !NT_STATUS_IS_OK(nt_status)) {
282 DEBUG(1, ("PAM account restriction prevents user login\n"));
283 cli_shutdown(cli);
284 TALLOC_FREE(info3);
285 return nt_status;
290 netsamlogon_cache_store(user_info->smb_name, info3);
291 TALLOC_FREE(info3);
294 /* Note - once the cli stream is shutdown the mem_ctx used
295 to allocate the other_sids and gids structures has been deleted - so
296 these pointers are no longer valid..... */
298 cli_shutdown(cli);
299 return nt_status;
302 /****************************************************************************
303 Check for a valid username and password in security=domain mode.
304 ****************************************************************************/
306 static NTSTATUS check_ntdomain_security(const struct auth_context *auth_context,
307 void *my_private_data,
308 TALLOC_CTX *mem_ctx,
309 const auth_usersupplied_info *user_info,
310 auth_serversupplied_info **server_info)
312 NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
313 const char *domain = lp_workgroup();
314 fstring dc_name;
315 struct sockaddr_storage dc_ss;
317 if ( lp_server_role() != ROLE_DOMAIN_MEMBER ) {
318 DEBUG(0,("check_ntdomain_security: Configuration error! Cannot use "
319 "ntdomain auth method when not a member of a domain.\n"));
320 return NT_STATUS_NOT_IMPLEMENTED;
323 if (!user_info || !server_info || !auth_context) {
324 DEBUG(1,("check_ntdomain_security: Critical variables not present. Failing.\n"));
325 return NT_STATUS_INVALID_PARAMETER;
329 * Check that the requested domain is not our own machine name.
330 * If it is, we should never check the PDC here, we use our own local
331 * password file.
334 if(strequal(get_global_sam_name(), user_info->domain)) {
335 DEBUG(3,("check_ntdomain_security: Requested domain was for this machine.\n"));
336 return NT_STATUS_NOT_IMPLEMENTED;
339 /* we need our DC to send the net_sam_logon() request to */
341 if ( !get_dc_name(domain, NULL, dc_name, &dc_ss) ) {
342 DEBUG(5,("check_ntdomain_security: unable to locate a DC for domain %s\n",
343 user_info->domain));
344 return NT_STATUS_NO_LOGON_SERVERS;
347 nt_status = domain_client_validate(mem_ctx,
348 user_info,
349 domain,
350 (uchar *)auth_context->challenge.data,
351 server_info,
352 dc_name,
353 &dc_ss);
355 return nt_status;
358 /* module initialisation */
359 static NTSTATUS auth_init_ntdomain(struct auth_context *auth_context, const char* param, auth_methods **auth_method)
361 if (!make_auth_methods(auth_context, auth_method)) {
362 return NT_STATUS_NO_MEMORY;
365 (*auth_method)->name = "ntdomain";
366 (*auth_method)->auth = check_ntdomain_security;
367 return NT_STATUS_OK;
371 /****************************************************************************
372 Check for a valid username and password in a trusted domain
373 ****************************************************************************/
375 static NTSTATUS check_trustdomain_security(const struct auth_context *auth_context,
376 void *my_private_data,
377 TALLOC_CTX *mem_ctx,
378 const auth_usersupplied_info *user_info,
379 auth_serversupplied_info **server_info)
381 NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
382 unsigned char trust_md4_password[16];
383 char *trust_password;
384 time_t last_change_time;
385 DOM_SID sid;
386 fstring dc_name;
387 struct sockaddr_storage dc_ss;
389 if (!user_info || !server_info || !auth_context) {
390 DEBUG(1,("check_trustdomain_security: Critical variables not present. Failing.\n"));
391 return NT_STATUS_INVALID_PARAMETER;
395 * Check that the requested domain is not our own machine name or domain name.
398 if( strequal(get_global_sam_name(), user_info->domain)) {
399 DEBUG(3,("check_trustdomain_security: Requested domain [%s] was for this machine.\n",
400 user_info->domain));
401 return NT_STATUS_NOT_IMPLEMENTED;
404 /* No point is bothering if this is not a trusted domain.
405 This return makes "map to guest = bad user" work again.
406 The logic is that if we know nothing about the domain, that
407 user is not known to us and does not exist */
409 if ( !is_trusted_domain( user_info->domain ) )
410 return NT_STATUS_NOT_IMPLEMENTED;
413 * Get the trusted account password for the trusted domain
414 * No need to become_root() as secrets_init() is done at startup.
417 if (!pdb_get_trusteddom_pw(user_info->domain, &trust_password,
418 &sid, &last_change_time)) {
419 DEBUG(0, ("check_trustdomain_security: could not fetch trust "
420 "account password for domain %s\n",
421 user_info->domain));
422 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
425 #ifdef DEBUG_PASSWORD
426 DEBUG(100, ("Trust password for domain %s is %s\n", user_info->domain,
427 trust_password));
428 #endif
429 E_md4hash(trust_password, trust_md4_password);
430 SAFE_FREE(trust_password);
432 #if 0
433 /* Test if machine password is expired and need to be changed */
434 if (time(NULL) > last_change_time + (time_t)lp_machine_password_timeout())
436 global_machine_password_needs_changing = True;
438 #endif
440 /* use get_dc_name() for consistency even through we know that it will be
441 a netbios name */
443 if ( !get_dc_name(user_info->domain, NULL, dc_name, &dc_ss) ) {
444 DEBUG(5,("check_trustdomain_security: unable to locate a DC for domain %s\n",
445 user_info->domain));
446 return NT_STATUS_NO_LOGON_SERVERS;
449 nt_status = domain_client_validate(mem_ctx,
450 user_info,
451 user_info->domain,
452 (uchar *)auth_context->challenge.data,
453 server_info,
454 dc_name,
455 &dc_ss);
457 return nt_status;
460 /* module initialisation */
461 static NTSTATUS auth_init_trustdomain(struct auth_context *auth_context, const char* param, auth_methods **auth_method)
463 if (!make_auth_methods(auth_context, auth_method)) {
464 return NT_STATUS_NO_MEMORY;
467 (*auth_method)->name = "trustdomain";
468 (*auth_method)->auth = check_trustdomain_security;
469 return NT_STATUS_OK;
472 NTSTATUS auth_domain_init(void)
474 smb_register_auth(AUTH_INTERFACE_VERSION, "trustdomain", auth_init_trustdomain);
475 smb_register_auth(AUTH_INTERFACE_VERSION, "ntdomain", auth_init_ntdomain);
476 return NT_STATUS_OK;