Wrap the DEBUG checks in a "unlikely"
[Samba/gbeck.git] / source3 / auth / auth_domain.c
blobb2c87174fd332baa33dcc1cfef282dadbf043952
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;
28 /**
29 * Connect to a remote server for (inter)domain security authenticaion.
31 * @param cli the cli to return containing the active connection
32 * @param server either a machine name or text IP address to
33 * connect to.
34 * @param setup_creds_as domain account to setup credentials as
35 * @param sec_chan a switch value to distinguish between domain
36 * member and interdomain authentication
37 * @param trust_passwd the trust password to establish the
38 * credentials with.
40 **/
42 static NTSTATUS connect_to_domain_password_server(struct cli_state **cli,
43 const char *domain,
44 const char *dc_name,
45 struct sockaddr_storage *dc_ss,
46 struct rpc_pipe_client **pipe_ret,
47 bool *retry)
49 NTSTATUS result;
50 struct rpc_pipe_client *netlogon_pipe = NULL;
52 *cli = NULL;
54 *pipe_ret = NULL;
56 /* TODO: Send a SAMLOGON request to determine whether this is a valid
57 logonserver. We can avoid a 30-second timeout if the DC is down
58 if the SAMLOGON request fails as it is only over UDP. */
60 /* we use a mutex to prevent two connections at once - when a
61 Win2k PDC get two connections where one hasn't completed a
62 session setup yet it will send a TCP reset to the first
63 connection (tridge) */
66 * With NT4.x DC's *all* authentication must be serialized to avoid
67 * ACCESS_DENIED errors if 2 auths are done from the same machine. JRA.
70 if (!grab_server_mutex(dc_name)) {
71 return NT_STATUS_NO_LOGON_SERVERS;
74 /* Attempt connection */
75 *retry = True;
76 result = cli_full_connection(cli, global_myname(), dc_name, dc_ss, 0,
77 "IPC$", "IPC", "", "", "", 0, Undefined, retry);
79 if (!NT_STATUS_IS_OK(result)) {
80 /* map to something more useful */
81 if (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)) {
82 result = NT_STATUS_NO_LOGON_SERVERS;
85 if (*cli) {
86 cli_shutdown(*cli);
87 *cli = NULL;
90 release_server_mutex();
91 return result;
95 * We now have an anonymous connection to IPC$ on the domain password server.
99 * Even if the connect succeeds we need to setup the netlogon
100 * pipe here. We do this as we may just have changed the domain
101 * account password on the PDC and yet we may be talking to
102 * a BDC that doesn't have this replicated yet. In this case
103 * a successful connect to a DC needs to take the netlogon connect
104 * into account also. This patch from "Bjart Kvarme" <bjart.kvarme@usit.uio.no>.
107 /* open the netlogon pipe. */
108 if (lp_client_schannel()) {
109 /* We also setup the creds chain in the open_schannel call. */
110 netlogon_pipe = cli_rpc_pipe_open_schannel(*cli, PI_NETLOGON,
111 PIPE_AUTH_LEVEL_PRIVACY, domain, &result);
112 } else {
113 netlogon_pipe = cli_rpc_pipe_open_noauth(*cli, PI_NETLOGON, &result);
116 if(!netlogon_pipe) {
117 DEBUG(0,("connect_to_domain_password_server: unable to open the domain client session to \
118 machine %s. Error was : %s.\n", dc_name, nt_errstr(result)));
119 cli_shutdown(*cli);
120 *cli = NULL;
121 release_server_mutex();
122 return result;
125 if (!lp_client_schannel()) {
126 /* We need to set up a creds chain on an unauthenticated netlogon pipe. */
127 uint32 neg_flags = NETLOGON_NEG_AUTH2_FLAGS;
128 uint32 sec_chan_type = 0;
129 unsigned char machine_pwd[16];
130 const char *account_name;
132 if (!get_trust_pw_hash(domain, machine_pwd, &account_name,
133 &sec_chan_type))
135 DEBUG(0, ("connect_to_domain_password_server: could not fetch "
136 "trust account password for domain '%s'\n",
137 domain));
138 cli_shutdown(*cli);
139 *cli = NULL;
140 release_server_mutex();
141 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
144 result = rpccli_netlogon_setup_creds(netlogon_pipe,
145 dc_name, /* server name */
146 domain, /* domain */
147 global_myname(), /* client name */
148 account_name, /* machine account name */
149 machine_pwd,
150 sec_chan_type,
151 &neg_flags);
153 if (!NT_STATUS_IS_OK(result)) {
154 cli_shutdown(*cli);
155 *cli = NULL;
156 release_server_mutex();
157 return result;
161 if(!netlogon_pipe) {
162 DEBUG(0,("connect_to_domain_password_server: unable to open the domain client session to \
163 machine %s. Error was : %s.\n", dc_name, cli_errstr(*cli)));
164 cli_shutdown(*cli);
165 *cli = NULL;
166 release_server_mutex();
167 return NT_STATUS_NO_LOGON_SERVERS;
170 /* We exit here with the mutex *locked*. JRA */
172 *pipe_ret = netlogon_pipe;
174 return NT_STATUS_OK;
177 /***********************************************************************
178 Do the same as security=server, but using NT Domain calls and a session
179 key from the machine password. If the server parameter is specified
180 use it, otherwise figure out a server from the 'password server' param.
181 ************************************************************************/
183 static NTSTATUS domain_client_validate(TALLOC_CTX *mem_ctx,
184 const auth_usersupplied_info *user_info,
185 const char *domain,
186 uchar chal[8],
187 auth_serversupplied_info **server_info,
188 const char *dc_name,
189 struct sockaddr_storage *dc_ss)
192 NET_USER_INFO_3 info3;
193 struct cli_state *cli = NULL;
194 struct rpc_pipe_client *netlogon_pipe = NULL;
195 NTSTATUS nt_status = NT_STATUS_NO_LOGON_SERVERS;
196 int i;
197 bool retry = True;
200 * At this point, smb_apasswd points to the lanman response to
201 * the challenge in local_challenge, and smb_ntpasswd points to
202 * the NT response to the challenge in local_challenge. Ship
203 * these over the secure channel to a domain controller and
204 * see if they were valid.
207 /* rety loop for robustness */
209 for (i = 0; !NT_STATUS_IS_OK(nt_status) && retry && (i < 3); i++) {
210 nt_status = connect_to_domain_password_server(&cli,
211 domain,
212 dc_name,
213 dc_ss,
214 &netlogon_pipe,
215 &retry);
218 if ( !NT_STATUS_IS_OK(nt_status) ) {
219 DEBUG(0,("domain_client_validate: Domain password server not available.\n"));
220 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
221 return NT_STATUS_TRUSTED_RELATIONSHIP_FAILURE;
223 return nt_status;
226 /* store a successful connection */
228 saf_store( domain, cli->desthost );
230 ZERO_STRUCT(info3);
233 * If this call succeeds, we now have lots of info about the user
234 * in the info3 structure.
237 nt_status = rpccli_netlogon_sam_network_logon(netlogon_pipe,
238 mem_ctx,
239 user_info->logon_parameters,/* flags such as 'allow workstation logon' */
240 dc_name, /* server name */
241 user_info->smb_name, /* user name logging on. */
242 user_info->client_domain, /* domain name */
243 user_info->wksta_name, /* workstation name */
244 chal, /* 8 byte challenge. */
245 user_info->lm_resp, /* lanman 24 byte response */
246 user_info->nt_resp, /* nt 24 byte response */
247 &info3); /* info3 out */
249 /* Let go as soon as possible so we avoid any potential deadlocks
250 with winbind lookup up users or groups. */
252 release_server_mutex();
254 if (!NT_STATUS_IS_OK(nt_status)) {
255 DEBUG(0,("domain_client_validate: unable to validate password "
256 "for user %s in domain %s to Domain controller %s. "
257 "Error was %s.\n", user_info->smb_name,
258 user_info->domain, dc_name,
259 nt_errstr(nt_status)));
261 /* map to something more useful */
262 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_UNSUCCESSFUL)) {
263 nt_status = NT_STATUS_NO_LOGON_SERVERS;
265 } else {
266 nt_status = make_server_info_info3(mem_ctx,
267 user_info->smb_name,
268 domain,
269 server_info,
270 &info3);
272 if (NT_STATUS_IS_OK(nt_status)) {
273 (*server_info)->was_mapped |= user_info->was_mapped;
275 if ( ! (*server_info)->guest) {
276 /* if a real user check pam account restrictions */
277 /* only really perfomed if "obey pam restriction" is true */
278 nt_status = smb_pam_accountcheck((*server_info)->unix_name);
279 if ( !NT_STATUS_IS_OK(nt_status)) {
280 DEBUG(1, ("PAM account restriction prevents user login\n"));
281 cli_shutdown(cli);
282 return nt_status;
287 netsamlogon_cache_store( user_info->smb_name, &info3 );
290 /* Note - once the cli stream is shutdown the mem_ctx used
291 to allocate the other_sids and gids structures has been deleted - so
292 these pointers are no longer valid..... */
294 cli_shutdown(cli);
295 return nt_status;
298 /****************************************************************************
299 Check for a valid username and password in security=domain mode.
300 ****************************************************************************/
302 static NTSTATUS check_ntdomain_security(const struct auth_context *auth_context,
303 void *my_private_data,
304 TALLOC_CTX *mem_ctx,
305 const auth_usersupplied_info *user_info,
306 auth_serversupplied_info **server_info)
308 NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
309 const char *domain = lp_workgroup();
310 fstring dc_name;
311 struct sockaddr_storage dc_ss;
313 if ( lp_server_role() != ROLE_DOMAIN_MEMBER ) {
314 DEBUG(0,("check_ntdomain_security: Configuration error! Cannot use "
315 "ntdomain auth method when not a member of a domain.\n"));
316 return NT_STATUS_NOT_IMPLEMENTED;
319 if (!user_info || !server_info || !auth_context) {
320 DEBUG(1,("check_ntdomain_security: Critical variables not present. Failing.\n"));
321 return NT_STATUS_INVALID_PARAMETER;
325 * Check that the requested domain is not our own machine name.
326 * If it is, we should never check the PDC here, we use our own local
327 * password file.
330 if(strequal(get_global_sam_name(), user_info->domain)) {
331 DEBUG(3,("check_ntdomain_security: Requested domain was for this machine.\n"));
332 return NT_STATUS_NOT_IMPLEMENTED;
335 /* we need our DC to send the net_sam_logon() request to */
337 if ( !get_dc_name(domain, NULL, dc_name, &dc_ss) ) {
338 DEBUG(5,("check_ntdomain_security: unable to locate a DC for domain %s\n",
339 user_info->domain));
340 return NT_STATUS_NO_LOGON_SERVERS;
343 nt_status = domain_client_validate(mem_ctx,
344 user_info,
345 domain,
346 (uchar *)auth_context->challenge.data,
347 server_info,
348 dc_name,
349 &dc_ss);
351 return nt_status;
354 /* module initialisation */
355 static NTSTATUS auth_init_ntdomain(struct auth_context *auth_context, const char* param, auth_methods **auth_method)
357 if (!make_auth_methods(auth_context, auth_method)) {
358 return NT_STATUS_NO_MEMORY;
361 (*auth_method)->name = "ntdomain";
362 (*auth_method)->auth = check_ntdomain_security;
363 return NT_STATUS_OK;
367 /****************************************************************************
368 Check for a valid username and password in a trusted domain
369 ****************************************************************************/
371 static NTSTATUS check_trustdomain_security(const struct auth_context *auth_context,
372 void *my_private_data,
373 TALLOC_CTX *mem_ctx,
374 const auth_usersupplied_info *user_info,
375 auth_serversupplied_info **server_info)
377 NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
378 unsigned char trust_md4_password[16];
379 char *trust_password;
380 time_t last_change_time;
381 DOM_SID sid;
382 fstring dc_name;
383 struct sockaddr_storage dc_ss;
385 if (!user_info || !server_info || !auth_context) {
386 DEBUG(1,("check_trustdomain_security: Critical variables not present. Failing.\n"));
387 return NT_STATUS_INVALID_PARAMETER;
391 * Check that the requested domain is not our own machine name or domain name.
394 if( strequal(get_global_sam_name(), user_info->domain)) {
395 DEBUG(3,("check_trustdomain_security: Requested domain [%s] was for this machine.\n",
396 user_info->domain));
397 return NT_STATUS_NOT_IMPLEMENTED;
400 /* No point is bothering if this is not a trusted domain.
401 This return makes "map to guest = bad user" work again.
402 The logic is that if we know nothing about the domain, that
403 user is not known to us and does not exist */
405 if ( !is_trusted_domain( user_info->domain ) )
406 return NT_STATUS_NOT_IMPLEMENTED;
409 * Get the trusted account password for the trusted domain
410 * No need to become_root() as secrets_init() is done at startup.
413 if (!pdb_get_trusteddom_pw(user_info->domain, &trust_password,
414 &sid, &last_change_time)) {
415 DEBUG(0, ("check_trustdomain_security: could not fetch trust "
416 "account password for domain %s\n",
417 user_info->domain));
418 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
421 #ifdef DEBUG_PASSWORD
422 DEBUG(100, ("Trust password for domain %s is %s\n", user_info->domain,
423 trust_password));
424 #endif
425 E_md4hash(trust_password, trust_md4_password);
426 SAFE_FREE(trust_password);
428 #if 0
429 /* Test if machine password is expired and need to be changed */
430 if (time(NULL) > last_change_time + (time_t)lp_machine_password_timeout())
432 global_machine_password_needs_changing = True;
434 #endif
436 /* use get_dc_name() for consistency even through we know that it will be
437 a netbios name */
439 if ( !get_dc_name(user_info->domain, NULL, dc_name, &dc_ss) ) {
440 DEBUG(5,("check_trustdomain_security: unable to locate a DC for domain %s\n",
441 user_info->domain));
442 return NT_STATUS_NO_LOGON_SERVERS;
445 nt_status = domain_client_validate(mem_ctx,
446 user_info,
447 user_info->domain,
448 (uchar *)auth_context->challenge.data,
449 server_info,
450 dc_name,
451 &dc_ss);
453 return nt_status;
456 /* module initialisation */
457 static NTSTATUS auth_init_trustdomain(struct auth_context *auth_context, const char* param, auth_methods **auth_method)
459 if (!make_auth_methods(auth_context, auth_method)) {
460 return NT_STATUS_NO_MEMORY;
463 (*auth_method)->name = "trustdomain";
464 (*auth_method)->auth = check_trustdomain_security;
465 return NT_STATUS_OK;
468 NTSTATUS auth_domain_init(void)
470 smb_register_auth(AUTH_INTERFACE_VERSION, "trustdomain", auth_init_trustdomain);
471 smb_register_auth(AUTH_INTERFACE_VERSION, "ntdomain", auth_init_ntdomain);
472 return NT_STATUS_OK;