smbXcli: rebuild smb1.recv_iov array if we expect more than one response
[Samba/gebeck_regimport.git] / source3 / auth / auth_domain.c
blob696b18bd4207f50a3e3048645d47d2d13e8f8512
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 lp_netbios_name(), 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 lp_netbios_name(), 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 const 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, lp_netbios_name(), dc_name, dc_ss, 0,
150 "IPC$", "IPC", "", "", "", 0, SMB_SIGNING_DEFAULT);
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 lp_netbios_name(), /* 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 "
238 "the domain client session to machine %s. Error "
239 "was : %s.\n", dc_name, nt_errstr(result)));
240 cli_shutdown(*cli);
241 *cli = NULL;
242 TALLOC_FREE(mutex);
243 return NT_STATUS_NO_LOGON_SERVERS;
246 /* We exit here with the mutex *locked*. JRA */
248 *pipe_ret = netlogon_pipe;
250 return NT_STATUS_OK;
253 /***********************************************************************
254 Do the same as security=server, but using NT Domain calls and a session
255 key from the machine password. If the server parameter is specified
256 use it, otherwise figure out a server from the 'password server' param.
257 ************************************************************************/
259 static NTSTATUS domain_client_validate(TALLOC_CTX *mem_ctx,
260 const struct auth_usersupplied_info *user_info,
261 const char *domain,
262 uchar chal[8],
263 struct auth_serversupplied_info **server_info,
264 const char *dc_name,
265 const struct sockaddr_storage *dc_ss)
268 struct netr_SamInfo3 *info3 = NULL;
269 struct cli_state *cli = NULL;
270 struct rpc_pipe_client *netlogon_pipe = NULL;
271 NTSTATUS nt_status = NT_STATUS_NO_LOGON_SERVERS;
272 int i;
275 * At this point, smb_apasswd points to the lanman response to
276 * the challenge in local_challenge, and smb_ntpasswd points to
277 * the NT response to the challenge in local_challenge. Ship
278 * these over the secure channel to a domain controller and
279 * see if they were valid.
282 /* rety loop for robustness */
284 for (i = 0; !NT_STATUS_IS_OK(nt_status) && (i < 3); i++) {
285 nt_status = connect_to_domain_password_server(&cli,
286 domain,
287 dc_name,
288 dc_ss,
289 &netlogon_pipe);
292 if ( !NT_STATUS_IS_OK(nt_status) ) {
293 DEBUG(0,("domain_client_validate: Domain password server not available.\n"));
294 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
295 return NT_STATUS_TRUSTED_RELATIONSHIP_FAILURE;
297 return nt_status;
300 /* store a successful connection */
302 saf_store(domain, dc_name);
305 * If this call succeeds, we now have lots of info about the user
306 * in the info3 structure.
309 nt_status = rpccli_netlogon_sam_network_logon(netlogon_pipe,
310 mem_ctx,
311 user_info->logon_parameters, /* flags such as 'allow workstation logon' */
312 dc_name, /* server name */
313 user_info->client.account_name, /* user name logging on. */
314 user_info->client.domain_name, /* domain name */
315 user_info->workstation_name, /* workstation name */
316 chal, /* 8 byte challenge. */
317 3, /* validation level */
318 user_info->password.response.lanman, /* lanman 24 byte response */
319 user_info->password.response.nt, /* nt 24 byte response */
320 &info3); /* info3 out */
322 /* Let go as soon as possible so we avoid any potential deadlocks
323 with winbind lookup up users or groups. */
325 TALLOC_FREE(mutex);
327 if (!NT_STATUS_IS_OK(nt_status)) {
328 DEBUG(0,("domain_client_validate: unable to validate password "
329 "for user %s in domain %s to Domain controller %s. "
330 "Error was %s.\n", user_info->client.account_name,
331 user_info->client.domain_name, dc_name,
332 nt_errstr(nt_status)));
334 /* map to something more useful */
335 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_UNSUCCESSFUL)) {
336 nt_status = NT_STATUS_NO_LOGON_SERVERS;
338 } else {
339 nt_status = make_server_info_info3(mem_ctx,
340 user_info->client.account_name,
341 domain,
342 server_info,
343 info3);
345 if (NT_STATUS_IS_OK(nt_status)) {
346 (*server_info)->nss_token |= user_info->was_mapped;
347 netsamlogon_cache_store(user_info->client.account_name, info3);
348 TALLOC_FREE(info3);
352 /* Note - once the cli stream is shutdown the mem_ctx used
353 to allocate the other_sids and gids structures has been deleted - so
354 these pointers are no longer valid..... */
356 cli_shutdown(cli);
357 return nt_status;
360 /****************************************************************************
361 Check for a valid username and password in security=domain mode.
362 ****************************************************************************/
364 static NTSTATUS check_ntdomain_security(const struct auth_context *auth_context,
365 void *my_private_data,
366 TALLOC_CTX *mem_ctx,
367 const struct auth_usersupplied_info *user_info,
368 struct auth_serversupplied_info **server_info)
370 NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
371 const char *domain = lp_workgroup();
372 fstring dc_name;
373 struct sockaddr_storage dc_ss;
375 if ( lp_server_role() != ROLE_DOMAIN_MEMBER ) {
376 DEBUG(0,("check_ntdomain_security: Configuration error! Cannot use "
377 "ntdomain auth method when not a member of a domain.\n"));
378 return NT_STATUS_NOT_IMPLEMENTED;
381 if (!user_info || !server_info || !auth_context) {
382 DEBUG(1,("check_ntdomain_security: Critical variables not present. Failing.\n"));
383 return NT_STATUS_INVALID_PARAMETER;
386 DEBUG(10, ("Check auth for: [%s]\n", user_info->mapped.account_name));
389 * Check that the requested domain is not our own machine name.
390 * If it is, we should never check the PDC here, we use our own local
391 * password file.
394 if(strequal(get_global_sam_name(), user_info->mapped.domain_name)) {
395 DEBUG(3,("check_ntdomain_security: Requested domain was for this machine.\n"));
396 return NT_STATUS_NOT_IMPLEMENTED;
399 /* we need our DC to send the net_sam_logon() request to */
401 if ( !get_dc_name(domain, NULL, dc_name, &dc_ss) ) {
402 DEBUG(5,("check_ntdomain_security: unable to locate a DC for domain %s\n",
403 user_info->mapped.domain_name));
404 return NT_STATUS_NO_LOGON_SERVERS;
407 nt_status = domain_client_validate(mem_ctx,
408 user_info,
409 domain,
410 (uchar *)auth_context->challenge.data,
411 server_info,
412 dc_name,
413 &dc_ss);
415 return nt_status;
418 /* module initialisation */
419 static NTSTATUS auth_init_ntdomain(struct auth_context *auth_context, const char* param, auth_methods **auth_method)
421 struct auth_methods *result;
423 result = talloc_zero(auth_context, struct auth_methods);
424 if (result == NULL) {
425 return NT_STATUS_NO_MEMORY;
427 result->name = "ntdomain";
428 result->auth = check_ntdomain_security;
430 *auth_method = result;
431 return NT_STATUS_OK;
435 /****************************************************************************
436 Check for a valid username and password in a trusted domain
437 ****************************************************************************/
439 static NTSTATUS check_trustdomain_security(const struct auth_context *auth_context,
440 void *my_private_data,
441 TALLOC_CTX *mem_ctx,
442 const struct auth_usersupplied_info *user_info,
443 struct auth_serversupplied_info **server_info)
445 NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
446 unsigned char trust_md4_password[16];
447 char *trust_password;
448 fstring dc_name;
449 struct sockaddr_storage dc_ss;
451 if (!user_info || !server_info || !auth_context) {
452 DEBUG(1,("check_trustdomain_security: Critical variables not present. Failing.\n"));
453 return NT_STATUS_INVALID_PARAMETER;
456 DEBUG(10, ("Check auth for: [%s]\n", user_info->mapped.account_name));
459 * Check that the requested domain is not our own machine name or domain name.
462 if( strequal(get_global_sam_name(), user_info->mapped.domain_name)) {
463 DEBUG(3,("check_trustdomain_security: Requested domain [%s] was for this machine.\n",
464 user_info->mapped.domain_name));
465 return NT_STATUS_NOT_IMPLEMENTED;
468 /* No point is bothering if this is not a trusted domain.
469 This return makes "map to guest = bad user" work again.
470 The logic is that if we know nothing about the domain, that
471 user is not known to us and does not exist */
473 if ( !is_trusted_domain( user_info->mapped.domain_name ) )
474 return NT_STATUS_NOT_IMPLEMENTED;
477 * Get the trusted account password for the trusted domain
478 * No need to become_root() as secrets_init() is done at startup.
481 if (!pdb_get_trusteddom_pw(user_info->mapped.domain_name, &trust_password,
482 NULL, NULL)) {
483 DEBUG(0, ("check_trustdomain_security: could not fetch trust "
484 "account password for domain %s\n",
485 user_info->mapped.domain_name));
486 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
489 #ifdef DEBUG_PASSWORD
490 DEBUG(100, ("Trust password for domain %s is %s\n", user_info->mapped.domain_name,
491 trust_password));
492 #endif
493 E_md4hash(trust_password, trust_md4_password);
494 SAFE_FREE(trust_password);
496 #if 0
497 /* Test if machine password is expired and need to be changed */
498 if (time(NULL) > last_change_time + (time_t)lp_machine_password_timeout())
500 global_machine_password_needs_changing = True;
502 #endif
504 /* use get_dc_name() for consistency even through we know that it will be
505 a netbios name */
507 if ( !get_dc_name(user_info->mapped.domain_name, NULL, dc_name, &dc_ss) ) {
508 DEBUG(5,("check_trustdomain_security: unable to locate a DC for domain %s\n",
509 user_info->mapped.domain_name));
510 return NT_STATUS_NO_LOGON_SERVERS;
513 nt_status = domain_client_validate(mem_ctx,
514 user_info,
515 user_info->mapped.domain_name,
516 (uchar *)auth_context->challenge.data,
517 server_info,
518 dc_name,
519 &dc_ss);
521 return nt_status;
524 /* module initialisation */
525 static NTSTATUS auth_init_trustdomain(struct auth_context *auth_context, const char* param, auth_methods **auth_method)
527 struct auth_methods *result;
529 result = talloc_zero(auth_context, struct auth_methods);
530 if (result == NULL) {
531 return NT_STATUS_NO_MEMORY;
533 result->name = "trustdomain";
534 result->auth = check_trustdomain_security;
536 *auth_method = result;
537 return NT_STATUS_OK;
540 NTSTATUS auth_domain_init(void)
542 smb_register_auth(AUTH_INTERFACE_VERSION, "trustdomain", auth_init_trustdomain);
543 smb_register_auth(AUTH_INTERFACE_VERSION, "ntdomain", auth_init_ntdomain);
544 return NT_STATUS_OK;