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.
25 #define DBGC_CLASS DBGC_AUTH
27 extern BOOL global_machine_password_needs_changing;
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
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
43 static NTSTATUS connect_to_domain_password_server(struct cli_state **cli,
44 const char *domain, const char *dc_name,
46 const char *setup_creds_as,
48 const unsigned char *trust_passwd,
53 /* TODO: Send a SAMLOGON request to determine whether this is a valid
54 logonserver. We can avoid a 30-second timeout if the DC is down
55 if the SAMLOGON request fails as it is only over UDP. */
57 /* we use a mutex to prevent two connections at once - when a
58 Win2k PDC get two connections where one hasn't completed a
59 session setup yet it will send a TCP reset to the first
60 connection (tridge) */
63 * With NT4.x DC's *all* authentication must be serialized to avoid
64 * ACCESS_DENIED errors if 2 auths are done from the same machine. JRA.
67 if (!grab_server_mutex(dc_name))
68 return NT_STATUS_NO_LOGON_SERVERS;
70 /* Attempt connection */
72 result = cli_full_connection(cli, global_myname(), dc_name, &dc_ip, 0,
73 "IPC$", "IPC", "", "", "", 0, Undefined, retry);
75 if (!NT_STATUS_IS_OK(result)) {
76 /* map to something more useful */
77 if (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)) {
78 result = NT_STATUS_NO_LOGON_SERVERS;
81 release_server_mutex();
86 * We now have an anonymous connection to IPC$ on the domain password server.
90 * Even if the connect succeeds we need to setup the netlogon
91 * pipe here. We do this as we may just have changed the domain
92 * account password on the PDC and yet we may be talking to
93 * a BDC that doesn't have this replicated yet. In this case
94 * a successful connect to a DC needs to take the netlogon connect
95 * into account also. This patch from "Bjart Kvarme" <bjart.kvarme@usit.uio.no>.
98 if(cli_nt_session_open(*cli, PI_NETLOGON) == False) {
99 DEBUG(0,("connect_to_domain_password_server: unable to open the domain client session to \
100 machine %s. Error was : %s.\n", dc_name, cli_errstr(*cli)));
101 cli_nt_session_close(*cli);
104 release_server_mutex();
105 return NT_STATUS_NO_LOGON_SERVERS;
108 fstr_sprintf((*cli)->mach_acct, "%s$", setup_creds_as);
110 if (!(*cli)->mach_acct) {
111 release_server_mutex();
112 return NT_STATUS_NO_MEMORY;
115 /* This must be the remote domain (not ours) for schannel */
117 fstrcpy( (*cli)->domain, domain );
119 result = cli_nt_establish_netlogon(*cli, sec_chan, trust_passwd);
121 if (!NT_STATUS_IS_OK(result)) {
122 DEBUG(0,("connect_to_domain_password_server: unable to setup the NETLOGON credentials to machine \
123 %s. Error was : %s.\n", dc_name, nt_errstr(result)));
124 cli_nt_session_close(*cli);
127 release_server_mutex();
131 /* We exit here with the mutex *locked*. JRA */
136 /***********************************************************************
137 Do the same as security=server, but using NT Domain calls and a session
138 key from the machine password. If the server parameter is specified
139 use it, otherwise figure out a server from the 'password server' param.
140 ************************************************************************/
142 static NTSTATUS domain_client_validate(TALLOC_CTX *mem_ctx,
143 const auth_usersupplied_info *user_info,
146 auth_serversupplied_info **server_info,
147 const char *dc_name, struct in_addr dc_ip,
148 const char *setup_creds_as,
150 unsigned char trust_passwd[16],
151 time_t last_change_time)
153 NET_USER_INFO_3 info3;
154 struct cli_state *cli = NULL;
155 NTSTATUS nt_status = NT_STATUS_NO_LOGON_SERVERS;
160 * At this point, smb_apasswd points to the lanman response to
161 * the challenge in local_challenge, and smb_ntpasswd points to
162 * the NT response to the challenge in local_challenge. Ship
163 * these over the secure channel to a domain controller and
164 * see if they were valid.
167 /* rety loop for robustness */
169 for (i = 0; !NT_STATUS_IS_OK(nt_status) && retry && (i < 3); i++) {
170 nt_status = connect_to_domain_password_server(&cli, domain, dc_name,
171 dc_ip, setup_creds_as, sec_chan, trust_passwd, &retry);
174 if ( !NT_STATUS_IS_OK(nt_status) ) {
175 DEBUG(0,("domain_client_validate: Domain password server not available.\n"));
176 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
177 return NT_STATUS_TRUSTED_RELATIONSHIP_FAILURE;
185 * If this call succeeds, we now have lots of info about the user
186 * in the info3 structure.
189 nt_status = cli_netlogon_sam_network_logon(cli, mem_ctx,
190 NULL, user_info->smb_name.str, user_info->domain.str,
191 user_info->wksta_name.str, chal, user_info->lm_resp,
192 user_info->nt_resp, &info3);
194 /* let go as soon as possible so we avoid any potential deadlocks
195 with winbind lookup up users or groups */
197 release_server_mutex();
199 if (!NT_STATUS_IS_OK(nt_status)) {
200 DEBUG(0,("domain_client_validate: unable to validate password "
201 "for user %s in domain %s to Domain controller %s. "
202 "Error was %s.\n", user_info->smb_name.str,
203 user_info->domain.str, cli->srv_name_slash,
204 nt_errstr(nt_status)));
206 /* map to something more useful */
207 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_UNSUCCESSFUL)) {
208 nt_status = NT_STATUS_NO_LOGON_SERVERS;
211 nt_status = make_server_info_info3(mem_ctx, user_info->internal_username.str,
212 user_info->smb_name.str, domain, server_info, &info3);
213 netsamlogon_cache_store( mem_ctx, &info3 );
218 * We don't actually need to do this - plus it fails currently with
219 * NT_STATUS_INVALID_INFO_CLASS - we need to know *exactly* what to
223 if (NT_STATUS_IS_OK(status)) {
224 if(cli_nt_logoff(&cli, &ctr) == False) {
225 DEBUG(0,("domain_client_validate: unable to log off user %s in domain \
226 %s to Domain controller %s. Error was %s.\n", user, domain, dc_name, cli_errstr(&cli)));
227 nt_status = NT_STATUS_LOGON_FAILURE;
232 /* Note - once the cli stream is shutdown the mem_ctx used
233 to allocate the other_sids and gids structures has been deleted - so
234 these pointers are no longer valid..... */
236 cli_nt_session_close(cli);
242 /****************************************************************************
243 Check for a valid username and password in security=domain mode.
244 ****************************************************************************/
246 static NTSTATUS check_ntdomain_security(const struct auth_context *auth_context,
247 void *my_private_data,
249 const auth_usersupplied_info *user_info,
250 auth_serversupplied_info **server_info)
252 NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
253 unsigned char trust_passwd[16];
254 time_t last_change_time;
255 const char *domain = lp_workgroup();
256 uint32 sec_channel_type = 0;
258 struct in_addr dc_ip;
260 if ( lp_server_role() != ROLE_DOMAIN_MEMBER ) {
261 DEBUG(0,("check_ntdomain_security: Configuration error! Cannot use "
262 "ntdomain auth method when not a member of a domain.\n"));
263 return NT_STATUS_NOT_IMPLEMENTED;
266 if (!user_info || !server_info || !auth_context) {
267 DEBUG(1,("check_ntdomain_security: Critical variables not present. Failing.\n"));
268 return NT_STATUS_INVALID_PARAMETER;
272 * Check that the requested domain is not our own machine name.
273 * If it is, we should never check the PDC here, we use our own local
277 if(strequal(get_global_sam_name(), user_info->domain.str)) {
278 DEBUG(3,("check_ntdomain_security: Requested domain was for this machine.\n"));
279 return NT_STATUS_NOT_IMPLEMENTED;
283 * Get the machine account password for our primary domain
284 * No need to become_root() as secrets_init() is done at startup.
287 if (!secrets_fetch_trust_account_password(domain, trust_passwd, &last_change_time, &sec_channel_type))
289 DEBUG(0, ("check_ntdomain_security: could not fetch trust account password for domain '%s'\n", domain));
290 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
293 /* Test if machine password has expired and needs to be changed */
294 if (lp_machine_password_timeout()) {
295 if (last_change_time > 0 &&
296 time(NULL) > (last_change_time +
297 lp_machine_password_timeout())) {
298 global_machine_password_needs_changing = True;
302 /* we need our DC to send the net_sam_logon() request to */
304 if ( !get_dc_name(domain, NULL, dc_name, &dc_ip) ) {
305 DEBUG(5,("check_ntdomain_security: unable to locate a DC for domain %s\n",
306 user_info->domain.str));
307 return NT_STATUS_NO_LOGON_SERVERS;
310 nt_status = domain_client_validate(mem_ctx, user_info, domain,
311 (uchar *)auth_context->challenge.data, server_info, dc_name, dc_ip,
312 global_myname(), sec_channel_type,trust_passwd, last_change_time);
317 /* module initialisation */
318 static NTSTATUS auth_init_ntdomain(struct auth_context *auth_context, const char* param, auth_methods **auth_method)
320 if (!make_auth_methods(auth_context, auth_method)) {
321 return NT_STATUS_NO_MEMORY;
324 (*auth_method)->name = "ntdomain";
325 (*auth_method)->auth = check_ntdomain_security;
330 /****************************************************************************
331 Check for a valid username and password in a trusted domain
332 ****************************************************************************/
334 static NTSTATUS check_trustdomain_security(const struct auth_context *auth_context,
335 void *my_private_data,
337 const auth_usersupplied_info *user_info,
338 auth_serversupplied_info **server_info)
340 NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
341 unsigned char trust_md4_password[16];
342 char *trust_password;
343 time_t last_change_time;
346 struct in_addr dc_ip;
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;
354 * Check that the requested domain is not our own machine name or domain name.
357 if( strequal(get_global_sam_name(), user_info->domain.str)) {
358 DEBUG(3,("check_trustdomain_security: Requested domain [%s] was for this machine.\n",
359 user_info->domain.str));
360 return NT_STATUS_NOT_IMPLEMENTED;
363 /* No point is bothering if this is not a trusted domain.
364 This return makes "map to guest = bad user" work again.
365 The logic is that if we know nothing about the domain, that
366 user is known to us and does not exist */
368 if ( !is_trusted_domain( user_info->domain.str ) )
369 return NT_STATUS_NOT_IMPLEMENTED;
372 * Get the trusted account password for the trusted domain
373 * No need to become_root() as secrets_init() is done at startup.
376 if (!secrets_fetch_trusted_domain_password(user_info->domain.str, &trust_password, &sid, &last_change_time))
378 DEBUG(0, ("check_trustdomain_security: could not fetch trust account password for domain %s\n", user_info->domain.str));
379 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
382 #ifdef DEBUG_PASSWORD
383 DEBUG(100, ("Trust password for domain %s is %s\n", user_info->domain.str, trust_password));
385 E_md4hash(trust_password, trust_md4_password);
386 SAFE_FREE(trust_password);
389 /* Test if machine password is expired and need to be changed */
390 if (time(NULL) > last_change_time + lp_machine_password_timeout())
392 global_machine_password_needs_changing = True;
396 /* use get_dc_name() for consistency even through we know that it will be
399 if ( !get_dc_name(user_info->domain.str, NULL, dc_name, &dc_ip) ) {
400 DEBUG(5,("check_trustdomain_security: unable to locate a DC for domain %s\n",
401 user_info->domain.str));
402 return NT_STATUS_NO_LOGON_SERVERS;
405 nt_status = domain_client_validate(mem_ctx, user_info, user_info->domain.str,
406 (uchar *)auth_context->challenge.data, server_info, dc_name, dc_ip,
407 lp_workgroup(), SEC_CHAN_DOMAIN, trust_md4_password, last_change_time);
412 /* module initialisation */
413 static NTSTATUS auth_init_trustdomain(struct auth_context *auth_context, const char* param, auth_methods **auth_method)
415 if (!make_auth_methods(auth_context, auth_method)) {
416 return NT_STATUS_NO_MEMORY;
419 (*auth_method)->name = "trustdomain";
420 (*auth_method)->auth = check_trustdomain_security;
424 NTSTATUS auth_domain_init(void)
426 smb_register_auth(AUTH_INTERFACE_VERSION, "trustdomain", auth_init_trustdomain);
427 smb_register_auth(AUTH_INTERFACE_VERSION, "ntdomain", auth_init_ntdomain);