ndr: add ndr_pull_charset_to_null()
[Samba.git] / source3 / auth / auth_domain.c
blob8a716038a8251dcba4c8ee9789c350b263fa8d6c
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 extern bool global_machine_password_needs_changing;
35 static struct named_mutex *mutex;
38 * Change machine password (called from main loop
39 * idle timeout. Must be done as root.
42 void attempt_machine_password_change(void)
44 unsigned char trust_passwd_hash[16];
45 time_t lct;
46 void *lock;
48 if (!global_machine_password_needs_changing) {
49 return;
52 if (lp_security() != SEC_DOMAIN) {
53 return;
57 * We're in domain level security, and the code that
58 * read the machine password flagged that the machine
59 * password needs changing.
63 * First, open the machine password file with an exclusive lock.
66 lock = secrets_get_trust_account_lock(NULL, lp_workgroup());
68 if (lock == NULL) {
69 DEBUG(0,("attempt_machine_password_change: unable to lock "
70 "the machine account password for machine %s in "
71 "domain %s.\n",
72 global_myname(), lp_workgroup() ));
73 return;
76 if(!secrets_fetch_trust_account_password(lp_workgroup(),
77 trust_passwd_hash, &lct, NULL)) {
78 DEBUG(0,("attempt_machine_password_change: unable to read the "
79 "machine account password for %s in domain %s.\n",
80 global_myname(), lp_workgroup()));
81 TALLOC_FREE(lock);
82 return;
86 * Make sure someone else hasn't already done this.
89 if(time(NULL) < lct + lp_machine_password_timeout()) {
90 global_machine_password_needs_changing = false;
91 TALLOC_FREE(lock);
92 return;
95 /* always just contact the PDC here */
97 change_trust_account_password( lp_workgroup(), NULL);
98 global_machine_password_needs_changing = false;
99 TALLOC_FREE(lock);
103 * Connect to a remote server for (inter)domain security authenticaion.
105 * @param cli the cli to return containing the active connection
106 * @param server either a machine name or text IP address to
107 * connect to.
108 * @param setup_creds_as domain account to setup credentials as
109 * @param sec_chan a switch value to distinguish between domain
110 * member and interdomain authentication
111 * @param trust_passwd the trust password to establish the
112 * credentials with.
116 static NTSTATUS connect_to_domain_password_server(struct cli_state **cli,
117 const char *domain,
118 const char *dc_name,
119 struct sockaddr_storage *dc_ss,
120 struct rpc_pipe_client **pipe_ret)
122 NTSTATUS result;
123 struct rpc_pipe_client *netlogon_pipe = NULL;
125 *cli = NULL;
127 *pipe_ret = NULL;
129 /* TODO: Send a SAMLOGON request to determine whether this is a valid
130 logonserver. We can avoid a 30-second timeout if the DC is down
131 if the SAMLOGON request fails as it is only over UDP. */
133 /* we use a mutex to prevent two connections at once - when a
134 Win2k PDC get two connections where one hasn't completed a
135 session setup yet it will send a TCP reset to the first
136 connection (tridge) */
139 * With NT4.x DC's *all* authentication must be serialized to avoid
140 * ACCESS_DENIED errors if 2 auths are done from the same machine. JRA.
143 mutex = grab_named_mutex(NULL, dc_name, 10);
144 if (mutex == NULL) {
145 return NT_STATUS_NO_LOGON_SERVERS;
148 /* Attempt connection */
149 result = cli_full_connection(cli, global_myname(), dc_name, dc_ss, 0,
150 "IPC$", "IPC", "", "", "", 0, Undefined);
152 if (!NT_STATUS_IS_OK(result)) {
153 /* map to something more useful */
154 if (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)) {
155 result = NT_STATUS_NO_LOGON_SERVERS;
158 if (*cli) {
159 cli_shutdown(*cli);
160 *cli = NULL;
163 TALLOC_FREE(mutex);
164 return result;
168 * We now have an anonymous connection to IPC$ on the domain password server.
172 * Even if the connect succeeds we need to setup the netlogon
173 * pipe here. We do this as we may just have changed the domain
174 * account password on the PDC and yet we may be talking to
175 * a BDC that doesn't have this replicated yet. In this case
176 * a successful connect to a DC needs to take the netlogon connect
177 * into account also. This patch from "Bjart Kvarme" <bjart.kvarme@usit.uio.no>.
180 /* open the netlogon pipe. */
181 if (lp_client_schannel()) {
182 /* We also setup the creds chain in the open_schannel call. */
183 result = cli_rpc_pipe_open_schannel(
184 *cli, &ndr_table_netlogon.syntax_id, NCACN_NP,
185 DCERPC_AUTH_LEVEL_PRIVACY, domain, &netlogon_pipe);
186 } else {
187 result = cli_rpc_pipe_open_noauth(
188 *cli, &ndr_table_netlogon.syntax_id, &netlogon_pipe);
191 if (!NT_STATUS_IS_OK(result)) {
192 DEBUG(0,("connect_to_domain_password_server: unable to open the domain client session to \
193 machine %s. Error was : %s.\n", dc_name, nt_errstr(result)));
194 cli_shutdown(*cli);
195 *cli = NULL;
196 TALLOC_FREE(mutex);
197 return result;
200 if (!lp_client_schannel()) {
201 /* We need to set up a creds chain on an unauthenticated netlogon pipe. */
202 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
203 enum netr_SchannelType sec_chan_type = 0;
204 unsigned char machine_pwd[16];
205 const char *account_name;
207 if (!get_trust_pw_hash(domain, machine_pwd, &account_name,
208 &sec_chan_type))
210 DEBUG(0, ("connect_to_domain_password_server: could not fetch "
211 "trust account password for domain '%s'\n",
212 domain));
213 cli_shutdown(*cli);
214 *cli = NULL;
215 TALLOC_FREE(mutex);
216 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
219 result = rpccli_netlogon_setup_creds(netlogon_pipe,
220 dc_name, /* server name */
221 domain, /* domain */
222 global_myname(), /* client name */
223 account_name, /* machine account name */
224 machine_pwd,
225 sec_chan_type,
226 &neg_flags);
228 if (!NT_STATUS_IS_OK(result)) {
229 cli_shutdown(*cli);
230 *cli = NULL;
231 TALLOC_FREE(mutex);
232 return result;
236 if(!netlogon_pipe) {
237 DEBUG(0,("connect_to_domain_password_server: unable to open the domain client session to \
238 machine %s. Error was : %s.\n", dc_name, cli_errstr(*cli)));
239 cli_shutdown(*cli);
240 *cli = NULL;
241 TALLOC_FREE(mutex);
242 return NT_STATUS_NO_LOGON_SERVERS;
245 /* We exit here with the mutex *locked*. JRA */
247 *pipe_ret = netlogon_pipe;
249 return NT_STATUS_OK;
252 /***********************************************************************
253 Do the same as security=server, but using NT Domain calls and a session
254 key from the machine password. If the server parameter is specified
255 use it, otherwise figure out a server from the 'password server' param.
256 ************************************************************************/
258 static NTSTATUS domain_client_validate(TALLOC_CTX *mem_ctx,
259 const struct auth_usersupplied_info *user_info,
260 const char *domain,
261 uchar chal[8],
262 struct auth_serversupplied_info **server_info,
263 const char *dc_name,
264 struct sockaddr_storage *dc_ss)
267 struct netr_SamInfo3 *info3 = NULL;
268 struct cli_state *cli = NULL;
269 struct rpc_pipe_client *netlogon_pipe = NULL;
270 NTSTATUS nt_status = NT_STATUS_NO_LOGON_SERVERS;
271 int i;
274 * At this point, smb_apasswd points to the lanman response to
275 * the challenge in local_challenge, and smb_ntpasswd points to
276 * the NT response to the challenge in local_challenge. Ship
277 * these over the secure channel to a domain controller and
278 * see if they were valid.
281 /* rety loop for robustness */
283 for (i = 0; !NT_STATUS_IS_OK(nt_status) && (i < 3); i++) {
284 nt_status = connect_to_domain_password_server(&cli,
285 domain,
286 dc_name,
287 dc_ss,
288 &netlogon_pipe);
291 if ( !NT_STATUS_IS_OK(nt_status) ) {
292 DEBUG(0,("domain_client_validate: Domain password server not available.\n"));
293 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
294 return NT_STATUS_TRUSTED_RELATIONSHIP_FAILURE;
296 return nt_status;
299 /* store a successful connection */
301 saf_store( domain, cli->desthost );
304 * If this call succeeds, we now have lots of info about the user
305 * in the info3 structure.
308 nt_status = rpccli_netlogon_sam_network_logon(netlogon_pipe,
309 mem_ctx,
310 user_info->logon_parameters, /* flags such as 'allow workstation logon' */
311 dc_name, /* server name */
312 user_info->client.account_name, /* user name logging on. */
313 user_info->client.domain_name, /* domain name */
314 user_info->workstation_name, /* workstation name */
315 chal, /* 8 byte challenge. */
316 3, /* validation level */
317 user_info->password.response.lanman, /* lanman 24 byte response */
318 user_info->password.response.nt, /* nt 24 byte response */
319 &info3); /* info3 out */
321 /* Let go as soon as possible so we avoid any potential deadlocks
322 with winbind lookup up users or groups. */
324 TALLOC_FREE(mutex);
326 if (!NT_STATUS_IS_OK(nt_status)) {
327 DEBUG(0,("domain_client_validate: unable to validate password "
328 "for user %s in domain %s to Domain controller %s. "
329 "Error was %s.\n", user_info->client.account_name,
330 user_info->client.domain_name, dc_name,
331 nt_errstr(nt_status)));
333 /* map to something more useful */
334 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_UNSUCCESSFUL)) {
335 nt_status = NT_STATUS_NO_LOGON_SERVERS;
337 } else {
338 nt_status = make_server_info_info3(mem_ctx,
339 user_info->client.account_name,
340 domain,
341 server_info,
342 info3);
344 if (NT_STATUS_IS_OK(nt_status)) {
345 (*server_info)->nss_token |= user_info->was_mapped;
346 netsamlogon_cache_store(user_info->client.account_name, info3);
347 TALLOC_FREE(info3);
351 /* Note - once the cli stream is shutdown the mem_ctx used
352 to allocate the other_sids and gids structures has been deleted - so
353 these pointers are no longer valid..... */
355 cli_shutdown(cli);
356 return nt_status;
359 /****************************************************************************
360 Check for a valid username and password in security=domain mode.
361 ****************************************************************************/
363 static NTSTATUS check_ntdomain_security(const struct auth_context *auth_context,
364 void *my_private_data,
365 TALLOC_CTX *mem_ctx,
366 const struct auth_usersupplied_info *user_info,
367 struct auth_serversupplied_info **server_info)
369 NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
370 const char *domain = lp_workgroup();
371 fstring dc_name;
372 struct sockaddr_storage dc_ss;
374 if ( lp_server_role() != ROLE_DOMAIN_MEMBER ) {
375 DEBUG(0,("check_ntdomain_security: Configuration error! Cannot use "
376 "ntdomain auth method when not a member of a domain.\n"));
377 return NT_STATUS_NOT_IMPLEMENTED;
380 if (!user_info || !server_info || !auth_context) {
381 DEBUG(1,("check_ntdomain_security: Critical variables not present. Failing.\n"));
382 return NT_STATUS_INVALID_PARAMETER;
385 DEBUG(10, ("Check auth for: [%s]\n", user_info->mapped.account_name));
388 * Check that the requested domain is not our own machine name.
389 * If it is, we should never check the PDC here, we use our own local
390 * password file.
393 if(strequal(get_global_sam_name(), user_info->mapped.domain_name)) {
394 DEBUG(3,("check_ntdomain_security: Requested domain was for this machine.\n"));
395 return NT_STATUS_NOT_IMPLEMENTED;
398 /* we need our DC to send the net_sam_logon() request to */
400 if ( !get_dc_name(domain, NULL, dc_name, &dc_ss) ) {
401 DEBUG(5,("check_ntdomain_security: unable to locate a DC for domain %s\n",
402 user_info->mapped.domain_name));
403 return NT_STATUS_NO_LOGON_SERVERS;
406 nt_status = domain_client_validate(mem_ctx,
407 user_info,
408 domain,
409 (uchar *)auth_context->challenge.data,
410 server_info,
411 dc_name,
412 &dc_ss);
414 return nt_status;
417 /* module initialisation */
418 static NTSTATUS auth_init_ntdomain(struct auth_context *auth_context, const char* param, auth_methods **auth_method)
420 struct auth_methods *result;
422 result = TALLOC_ZERO_P(auth_context, struct auth_methods);
423 if (result == NULL) {
424 return NT_STATUS_NO_MEMORY;
426 result->name = "ntdomain";
427 result->auth = check_ntdomain_security;
429 *auth_method = result;
430 return NT_STATUS_OK;
434 /****************************************************************************
435 Check for a valid username and password in a trusted domain
436 ****************************************************************************/
438 static NTSTATUS check_trustdomain_security(const struct auth_context *auth_context,
439 void *my_private_data,
440 TALLOC_CTX *mem_ctx,
441 const struct auth_usersupplied_info *user_info,
442 struct auth_serversupplied_info **server_info)
444 NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
445 unsigned char trust_md4_password[16];
446 char *trust_password;
447 fstring dc_name;
448 struct sockaddr_storage dc_ss;
450 if (!user_info || !server_info || !auth_context) {
451 DEBUG(1,("check_trustdomain_security: Critical variables not present. Failing.\n"));
452 return NT_STATUS_INVALID_PARAMETER;
455 DEBUG(10, ("Check auth for: [%s]\n", user_info->mapped.account_name));
458 * Check that the requested domain is not our own machine name or domain name.
461 if( strequal(get_global_sam_name(), user_info->mapped.domain_name)) {
462 DEBUG(3,("check_trustdomain_security: Requested domain [%s] was for this machine.\n",
463 user_info->mapped.domain_name));
464 return NT_STATUS_NOT_IMPLEMENTED;
467 /* No point is bothering if this is not a trusted domain.
468 This return makes "map to guest = bad user" work again.
469 The logic is that if we know nothing about the domain, that
470 user is not known to us and does not exist */
472 if ( !is_trusted_domain( user_info->mapped.domain_name ) )
473 return NT_STATUS_NOT_IMPLEMENTED;
476 * Get the trusted account password for the trusted domain
477 * No need to become_root() as secrets_init() is done at startup.
480 if (!pdb_get_trusteddom_pw(user_info->mapped.domain_name, &trust_password,
481 NULL, NULL)) {
482 DEBUG(0, ("check_trustdomain_security: could not fetch trust "
483 "account password for domain %s\n",
484 user_info->mapped.domain_name));
485 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
488 #ifdef DEBUG_PASSWORD
489 DEBUG(100, ("Trust password for domain %s is %s\n", user_info->mapped.domain_name,
490 trust_password));
491 #endif
492 E_md4hash(trust_password, trust_md4_password);
493 SAFE_FREE(trust_password);
495 #if 0
496 /* Test if machine password is expired and need to be changed */
497 if (time(NULL) > last_change_time + (time_t)lp_machine_password_timeout())
499 global_machine_password_needs_changing = True;
501 #endif
503 /* use get_dc_name() for consistency even through we know that it will be
504 a netbios name */
506 if ( !get_dc_name(user_info->mapped.domain_name, NULL, dc_name, &dc_ss) ) {
507 DEBUG(5,("check_trustdomain_security: unable to locate a DC for domain %s\n",
508 user_info->mapped.domain_name));
509 return NT_STATUS_NO_LOGON_SERVERS;
512 nt_status = domain_client_validate(mem_ctx,
513 user_info,
514 user_info->mapped.domain_name,
515 (uchar *)auth_context->challenge.data,
516 server_info,
517 dc_name,
518 &dc_ss);
520 return nt_status;
523 /* module initialisation */
524 static NTSTATUS auth_init_trustdomain(struct auth_context *auth_context, const char* param, auth_methods **auth_method)
526 struct auth_methods *result;
528 result = TALLOC_ZERO_P(auth_context, struct auth_methods);
529 if (result == NULL) {
530 return NT_STATUS_NO_MEMORY;
532 result->name = "trustdomain";
533 result->auth = check_trustdomain_security;
535 *auth_method = result;
536 return NT_STATUS_OK;
539 NTSTATUS auth_domain_init(void)
541 smb_register_auth(AUTH_INTERFACE_VERSION, "trustdomain", auth_init_trustdomain);
542 smb_register_auth(AUTH_INTERFACE_VERSION, "ntdomain", auth_init_ntdomain);
543 return NT_STATUS_OK;