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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 BOOL global_machine_password_needs_changing = False;
26 extern pstring global_myname;
27 extern userdom_struct current_user_info;
30 * Connect to a remote server for 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
35 * @param trust_password the trust password to establish the
40 static NTSTATUS connect_to_domain_password_server(struct cli_state **cli,
42 const char *setup_creds_as,
44 const unsigned char *trust_passwd)
46 struct in_addr dest_ip;
47 fstring remote_machine;
50 if (is_ipaddress(server)) {
53 /* we shouldn't have 255.255.255.255 forthe IP address of
54 a password server anyways */
55 if ((to_ip.s_addr=inet_addr(server)) == 0xFFFFFFFF) {
56 DEBUG (0,("connect_to_domain_password_server: inet_addr(%s) returned 0xFFFFFFFF!\n", server));
57 return NT_STATUS_UNSUCCESSFUL;
60 if (!name_status_find("*", 0x20, 0x20, to_ip, remote_machine)) {
61 DEBUG(0, ("connect_to_domain_password_server: Can't "
62 "resolve name for IP %s\n", server));
63 return NT_STATUS_UNSUCCESSFUL;
66 fstrcpy(remote_machine, server);
69 standard_sub_basic(current_user_info.smb_name, remote_machine);
70 strupper(remote_machine);
72 if(!resolve_name( remote_machine, &dest_ip, 0x20)) {
73 DEBUG(1,("connect_to_domain_password_server: Can't resolve address for %s\n", remote_machine));
74 return NT_STATUS_UNSUCCESSFUL;
77 if (ismyip(dest_ip)) {
78 DEBUG(1,("connect_to_domain_password_server: Password server loop - not using password server %s\n",
80 return NT_STATUS_UNSUCCESSFUL;
83 /* TODO: Send a SAMLOGON request to determine whether this is a valid
84 logonserver. We can avoid a 30-second timeout if the DC is down
85 if the SAMLOGON request fails as it is only over UDP. */
87 /* we use a mutex to prevent two connections at once - when a NT PDC gets
88 two connections where one hasn't completed a negprot yet it will send a
89 TCP reset to the first connection (tridge) */
90 if (!message_named_mutex(server)) {
91 DEBUG(1,("domain mutex failed for %s\n", server));
92 return NT_STATUS_UNSUCCESSFUL;
95 /* Attempt connection */
96 result = cli_full_connection(cli, global_myname, server,
97 &dest_ip, 0, "IPC$", "IPC", "", "", "", 0);
99 message_named_mutex_release(server);
101 if (!NT_STATUS_IS_OK(result)) {
106 * We now have an anonymous connection to IPC$ on the domain password server.
110 * Even if the connect succeeds we need to setup the netlogon
111 * pipe here. We do this as we may just have changed the domain
112 * account password on the PDC and yet we may be talking to
113 * a BDC that doesn't have this replicated yet. In this case
114 * a successful connect to a DC needs to take the netlogon connect
115 * into account also. This patch from "Bjart Kvarme" <bjart.kvarme@usit.uio.no>.
118 if(cli_nt_session_open(*cli, PIPE_NETLOGON) == False) {
119 DEBUG(0,("connect_to_domain_password_server: unable to open the domain client session to \
120 machine %s. Error was : %s.\n", remote_machine, cli_errstr(*cli)));
121 cli_nt_session_close(*cli);
124 return NT_STATUS_UNSUCCESSFUL;
127 snprintf((*cli)->mach_acct, sizeof((*cli)->mach_acct) - 1, "%s$", setup_creds_as);
129 if (!(*cli)->mach_acct) {
130 return NT_STATUS_NO_MEMORY;
133 result = new_cli_nt_setup_creds(*cli, sec_chan, trust_passwd);
135 if (!NT_STATUS_IS_OK(result)) {
136 DEBUG(0,("connect_to_domain_password_server: unable to setup the PDC credentials to machine \
137 %s. Error was : %s.\n", remote_machine, nt_errstr(result)));
138 cli_nt_session_close(*cli);
147 /***********************************************************************
148 Utility function to attempt a connection to an IP address of a DC.
149 ************************************************************************/
151 static NTSTATUS attempt_connect_to_dc(struct cli_state **cli,
154 const char *setup_creds_as,
156 const unsigned char *trust_passwd)
161 * Ignore addresses we have already tried.
165 return NT_STATUS_UNSUCCESSFUL;
167 if (!lookup_dc_name(global_myname, domain, ip, dc_name))
168 return NT_STATUS_UNSUCCESSFUL;
170 return connect_to_domain_password_server(cli, dc_name, setup_creds_as, sec_chan, trust_passwd);
173 /***********************************************************************
174 We have been asked to dynamcially determine the IP addresses of
175 the PDC and BDC's for DOMAIN, and query them in turn.
176 ************************************************************************/
177 static NTSTATUS find_connect_pdc(struct cli_state **cli,
179 const char *setup_creds_as,
181 unsigned char *trust_passwd,
182 time_t last_change_time)
184 struct in_addr *ip_list = NULL;
187 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
188 time_t time_now = time(NULL);
189 BOOL use_pdc_only = False;
192 * If the time the machine password has changed
193 * was less than an hour ago then we need to contact
194 * the PDC only, as we cannot be sure domain replication
195 * has yet taken place. Bug found by Gerald (way to go
199 if (time_now - last_change_time < 3600)
202 if (!get_dc_list(use_pdc_only, domain, &ip_list, &count))
203 return NT_STATUS_UNSUCCESSFUL;
206 * Firstly try and contact a PDC/BDC who has the same
207 * network address as any of our interfaces.
209 for(i = 0; i < count; i++) {
210 if(!is_local_net(ip_list[i]))
213 if(NT_STATUS_IS_OK(nt_status =
214 attempt_connect_to_dc(cli, domain,
215 &ip_list[i], setup_creds_as,
216 sec_chan, trust_passwd)))
219 zero_ip(&ip_list[i]); /* Tried and failed. */
223 * Secondly try and contact a random PDC/BDC.
225 if(!NT_STATUS_IS_OK(nt_status)) {
226 i = (sys_random() % count);
228 if (!is_zero_ip(ip_list[i])) {
229 if (!NT_STATUS_IS_OK(nt_status =
230 attempt_connect_to_dc(cli, domain,
231 &ip_list[i], setup_creds_as,
232 sec_chan, trust_passwd)))
233 zero_ip(&ip_list[i]); /* Tried and failed. */
238 * Finally go through the IP list in turn, ignoring any addresses
239 * we have already tried.
241 if(!NT_STATUS_IS_OK(nt_status)) {
243 * Try and connect to any of the other IP addresses in the PDC/BDC list.
244 * Note that from a WINS server the #1 IP address is the PDC.
246 for(i = 0; i < count; i++) {
247 if (is_zero_ip(ip_list[i]))
250 if (NT_STATUS_IS_OK(nt_status = attempt_connect_to_dc(cli, domain,
251 &ip_list[i], setup_creds_as, sec_chan, trust_passwd)))
260 /***********************************************************************
261 Do the same as security=server, but using NT Domain calls and a session
262 key from the machine password. If the server parameter is specified
263 use it, otherwise figure out a server from the 'password server' param.
264 ************************************************************************/
266 static NTSTATUS domain_client_validate(TALLOC_CTX *mem_ctx,
267 const auth_usersupplied_info *user_info,
270 auth_serversupplied_info **server_info,
271 char *server, char *setup_creds_as,
273 unsigned char *trust_passwd,
274 time_t last_change_time)
276 fstring remote_machine;
277 NET_USER_INFO_3 info3;
278 struct cli_state *cli = NULL;
279 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
283 * At this point, smb_apasswd points to the lanman response to
284 * the challenge in local_challenge, and smb_ntpasswd points to
285 * the NT response to the challenge in local_challenge. Ship
286 * these over the secure channel to a domain controller and
287 * see if they were valid.
290 while (!NT_STATUS_IS_OK(nt_status) &&
291 next_token(&server,remote_machine,LIST_SEP,sizeof(remote_machine))) {
292 if(strequal(remote_machine, "*")) {
293 nt_status = find_connect_pdc(&cli, domain, setup_creds_as, sec_chan, trust_passwd, last_change_time);
295 nt_status = connect_to_domain_password_server(&cli, remote_machine, setup_creds_as, sec_chan, trust_passwd);
299 if (!NT_STATUS_IS_OK(nt_status)) {
300 DEBUG(0,("domain_client_validate: Domain password server not available.\n"));
307 * If this call succeeds, we now have lots of info about the user
308 * in the info3 structure.
311 nt_status = cli_netlogon_sam_network_logon(cli, mem_ctx,
312 user_info->smb_name.str, user_info->domain.str,
313 user_info->wksta_name.str, chal,
314 user_info->lm_resp, user_info->nt_resp,
317 if (!NT_STATUS_IS_OK(nt_status)) {
318 DEBUG(0,("domain_client_validate: unable to validate password "
319 "for user %s in domain %s to Domain controller %s. "
320 "Error was %s.\n", user_info->smb_name.str,
321 user_info->domain.str, cli->srv_name_slash,
322 nt_errstr(nt_status)));
326 /* Check DOMAIN\username first to catch winbind users, then
327 just the username for local users. */
329 dom_user = talloc_asprintf(mem_ctx, "%s%s%s", user_info->domain.str,
330 lp_winbind_separator(),
331 user_info->internal_username.str);
334 DEBUG(0, ("talloc_asprintf failed!\n"));
335 nt_status = NT_STATUS_NO_MEMORY;
338 if (!(pass = Get_Pwnam(dom_user)))
339 pass = Get_Pwnam(user_info->internal_username.str);
342 make_server_info_pw(server_info, pass);
344 nt_status = NT_STATUS_NO_MEMORY;
347 nt_status = NT_STATUS_NO_SUCH_USER;
352 /* Store the user group information in the server_info returned to the caller. */
354 if (NT_STATUS_IS_OK(nt_status) && (info3.num_groups2 != 0)) {
357 auth_serversupplied_info *pserver_info = *server_info;
359 if ((pserver_info->ptok = malloc( sizeof(NT_USER_TOKEN) ) ) == NULL) {
360 DEBUG(0, ("domain_client_validate: out of memory allocating rid group membership\n"));
361 nt_status = NT_STATUS_NO_MEMORY;
362 free_server_info(server_info);
366 ptok = pserver_info->ptok;
367 ptok->num_sids = (size_t)info3.num_groups2;
369 if ((ptok->user_sids = (DOM_SID *)malloc( sizeof(DOM_SID) * ptok->num_sids )) == NULL) {
370 DEBUG(0, ("domain_client_validate: Out of memory allocating group SIDS\n"));
371 nt_status = NT_STATUS_NO_MEMORY;
372 free_server_info(server_info);
376 for (i = 0; i < ptok->num_sids; i++) {
377 sid_copy(&ptok->user_sids[i], &info3.dom_sid.sid);
378 sid_append_rid(&ptok->user_sids[i], info3.gids[i].g_rid);
381 uni_group_cache_store_netlogon(mem_ctx, &info3);
386 * We don't actually need to do this - plus it fails currently with
387 * NT_STATUS_INVALID_INFO_CLASS - we need to know *exactly* what to
391 if (NT_STATUS_IS_OK(status)) {
392 if(cli_nt_logoff(&cli, &ctr) == False) {
393 DEBUG(0,("domain_client_validate: unable to log off user %s in domain \
394 %s to Domain controller %s. Error was %s.\n", user, domain, remote_machine, cli_errstr(&cli)));
395 nt_status = NT_STATUS_LOGON_FAILURE;
402 /* Note - once the cli stream is shutdown the mem_ctx used
403 to allocate the other_sids and gids structures has been deleted - so
404 these pointers are no longer valid..... */
406 cli_nt_session_close(cli);
412 /****************************************************************************
413 Check for a valid username and password in security=domain mode.
414 ****************************************************************************/
416 static NTSTATUS check_ntdomain_security(const struct auth_context *auth_context,
417 void *my_private_data,
419 const auth_usersupplied_info *user_info,
420 auth_serversupplied_info **server_info)
422 NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
423 char *password_server;
424 unsigned char trust_passwd[16];
425 time_t last_change_time;
426 char *domain = lp_workgroup();
428 if (!user_info || !server_info || !auth_context) {
429 DEBUG(1,("check_ntdomain_security: Critical variables not present. Failing.\n"));
430 return NT_STATUS_INVALID_PARAMETER;
434 * Check that the requested domain is not our own machine name.
435 * If it is, we should never check the PDC here, we use our own local
439 if(is_netbios_alias_or_name(user_info->domain.str)) {
440 DEBUG(3,("check_ntdomain_security: Requested domain was for this machine.\n"));
441 return NT_STATUS_LOGON_FAILURE;
445 * Get the machine account password for our primary domain
446 * No need to become_root() as secrets_init() is done at startup.
449 if (!secrets_fetch_trust_account_password(domain, trust_passwd, &last_change_time))
451 DEBUG(0, ("check_ntdomain_security: could not fetch trust account password for domain %s\n", lp_workgroup()));
452 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
455 /* Test if machine password is expired and need to be changed */
456 if (time(NULL) > last_change_time + lp_machine_password_timeout())
458 global_machine_password_needs_changing = True;
462 * Treat each name in the 'password server =' line as a potential
463 * PDC/BDC. Contact each in turn and try and authenticate.
466 password_server = lp_passwordserver();
468 nt_status = domain_client_validate(mem_ctx, user_info, domain,
469 (uchar *)auth_context->challenge.data,
471 password_server, global_myname, SEC_CHAN_WKSTA, trust_passwd, last_change_time);
475 /* module initialisation */
476 BOOL auth_init_ntdomain(struct auth_context *auth_context, auth_methods **auth_method)
478 if (!make_auth_methods(auth_context, auth_method)) {
482 (*auth_method)->auth = check_ntdomain_security;
487 /****************************************************************************
488 Check for a valid username and password in a trusted domain
489 ****************************************************************************/
491 static NTSTATUS check_trustdomain_security(const struct auth_context *auth_context,
492 void *my_private_data,
494 const auth_usersupplied_info *user_info,
495 auth_serversupplied_info **server_info)
497 NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
498 unsigned char trust_md4_password[16];
499 char *trust_password;
500 time_t last_change_time;
503 if (!user_info || !server_info || !auth_context) {
504 DEBUG(1,("check_trustdomain_security: Critical variables not present. Failing.\n"));
505 return NT_STATUS_INVALID_PARAMETER;
509 * Check that the requested domain is not our own machine name.
510 * If it is, we should never check the PDC here, we use our own local
514 if(is_netbios_alias_or_name(user_info->domain.str)) {
515 DEBUG(3,("check_trustdomain_security: Requested domain was for this machine.\n"));
516 return NT_STATUS_LOGON_FAILURE;
520 * Check that the requested domain is not our own domain,
521 * If it is, we should use our own local password file.
524 if(strequal(lp_workgroup(), (user_info->domain.str))) {
525 DEBUG(3,("check_trustdomain_security: Requested domain was for this domain.\n"));
526 return NT_STATUS_LOGON_FAILURE;
530 * Get the machine account password for the trusted domain
531 * No need to become_root() as secrets_init() is done at startup.
534 if (!secrets_fetch_trusted_domain_password(user_info->domain.str, &trust_password, &sid, &last_change_time))
536 DEBUG(0, ("check_trustdomain_security: could not fetch trust account password for domain %s\n", user_info->domain.str));
537 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
540 #ifdef DEBUG_PASSWORD
541 DEBUG(100, ("Trust password for domain %s is %s\n", user_info->domain.str, trust_password));
543 E_md4hash((uchar *)trust_password, trust_md4_password);
544 SAFE_FREE(trust_password);
547 /* Test if machine password is expired and need to be changed */
548 if (time(NULL) > last_change_time + lp_machine_password_timeout())
550 global_machine_password_needs_changing = True;
554 nt_status = domain_client_validate(mem_ctx, user_info, user_info->domain.str,
555 (uchar *)auth_context->challenge.data,
556 server_info, "*" /* Do a lookup */,
557 lp_workgroup(), SEC_CHAN_DOMAIN, trust_md4_password, last_change_time);
562 /* module initialisation */
563 BOOL auth_init_trustdomain(struct auth_context *auth_context, auth_methods **auth_method)
565 if (!make_auth_methods(auth_context, auth_method)) {
569 (*auth_method)->auth = check_trustdomain_security;