s3:build: move sessionid_tdb.o and conn_tdb.o to SMBD_OBJ_BASE
[Samba/gebeck_regimport.git] / source3 / auth / auth_domain.c
blob286c75c786c016dc39325c45858201cbff3a19d4
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 static struct named_mutex *mutex;
36 /**
37 * Connect to a remote server for (inter)domain security authenticaion.
39 * @param cli the cli to return containing the active connection
40 * @param server either a machine name or text IP address to
41 * connect to.
42 * @param setup_creds_as domain account to setup credentials as
43 * @param sec_chan a switch value to distinguish between domain
44 * member and interdomain authentication
45 * @param trust_passwd the trust password to establish the
46 * credentials with.
48 **/
50 static NTSTATUS connect_to_domain_password_server(struct cli_state **cli,
51 const char *domain,
52 const char *dc_name,
53 const struct sockaddr_storage *dc_ss,
54 struct rpc_pipe_client **pipe_ret)
56 NTSTATUS result;
57 struct rpc_pipe_client *netlogon_pipe = NULL;
59 *cli = NULL;
61 *pipe_ret = NULL;
63 /* TODO: Send a SAMLOGON request to determine whether this is a valid
64 logonserver. We can avoid a 30-second timeout if the DC is down
65 if the SAMLOGON request fails as it is only over UDP. */
67 /* we use a mutex to prevent two connections at once - when a
68 Win2k PDC get two connections where one hasn't completed a
69 session setup yet it will send a TCP reset to the first
70 connection (tridge) */
73 * With NT4.x DC's *all* authentication must be serialized to avoid
74 * ACCESS_DENIED errors if 2 auths are done from the same machine. JRA.
77 mutex = grab_named_mutex(NULL, dc_name, 10);
78 if (mutex == NULL) {
79 return NT_STATUS_NO_LOGON_SERVERS;
82 /* Attempt connection */
83 result = cli_full_connection(cli, lp_netbios_name(), dc_name, dc_ss, 0,
84 "IPC$", "IPC", "", "", "", 0, SMB_SIGNING_DEFAULT);
86 if (!NT_STATUS_IS_OK(result)) {
87 /* map to something more useful */
88 if (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)) {
89 result = NT_STATUS_NO_LOGON_SERVERS;
92 if (*cli) {
93 cli_shutdown(*cli);
94 *cli = NULL;
97 TALLOC_FREE(mutex);
98 return result;
102 * We now have an anonymous connection to IPC$ on the domain password server.
106 * Even if the connect succeeds we need to setup the netlogon
107 * pipe here. We do this as we may just have changed the domain
108 * account password on the PDC and yet we may be talking to
109 * a BDC that doesn't have this replicated yet. In this case
110 * a successful connect to a DC needs to take the netlogon connect
111 * into account also. This patch from "Bjart Kvarme" <bjart.kvarme@usit.uio.no>.
114 /* open the netlogon pipe. */
115 if (lp_client_schannel()) {
116 /* We also setup the creds chain in the open_schannel call. */
117 result = cli_rpc_pipe_open_schannel(
118 *cli, &ndr_table_netlogon.syntax_id, NCACN_NP,
119 DCERPC_AUTH_LEVEL_PRIVACY, domain, &netlogon_pipe);
120 } else {
121 result = cli_rpc_pipe_open_noauth(
122 *cli, &ndr_table_netlogon.syntax_id, &netlogon_pipe);
125 if (!NT_STATUS_IS_OK(result)) {
126 DEBUG(0,("connect_to_domain_password_server: unable to open the domain client session to \
127 machine %s. Error was : %s.\n", dc_name, nt_errstr(result)));
128 cli_shutdown(*cli);
129 *cli = NULL;
130 TALLOC_FREE(mutex);
131 return result;
134 if (!lp_client_schannel()) {
135 /* We need to set up a creds chain on an unauthenticated netlogon pipe. */
136 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
137 enum netr_SchannelType sec_chan_type = 0;
138 unsigned char machine_pwd[16];
139 const char *account_name;
141 if (!get_trust_pw_hash(domain, machine_pwd, &account_name,
142 &sec_chan_type))
144 DEBUG(0, ("connect_to_domain_password_server: could not fetch "
145 "trust account password for domain '%s'\n",
146 domain));
147 cli_shutdown(*cli);
148 *cli = NULL;
149 TALLOC_FREE(mutex);
150 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
153 result = rpccli_netlogon_setup_creds(netlogon_pipe,
154 dc_name, /* server name */
155 domain, /* domain */
156 lp_netbios_name(), /* client name */
157 account_name, /* machine account name */
158 machine_pwd,
159 sec_chan_type,
160 &neg_flags);
162 if (!NT_STATUS_IS_OK(result)) {
163 cli_shutdown(*cli);
164 *cli = NULL;
165 TALLOC_FREE(mutex);
166 return result;
170 if(!netlogon_pipe) {
171 DEBUG(0, ("connect_to_domain_password_server: unable to open "
172 "the domain client session to machine %s. Error "
173 "was : %s.\n", dc_name, nt_errstr(result)));
174 cli_shutdown(*cli);
175 *cli = NULL;
176 TALLOC_FREE(mutex);
177 return NT_STATUS_NO_LOGON_SERVERS;
180 /* We exit here with the mutex *locked*. JRA */
182 *pipe_ret = netlogon_pipe;
184 return NT_STATUS_OK;
187 /***********************************************************************
188 Do the same as security=server, but using NT Domain calls and a session
189 key from the machine password. If the server parameter is specified
190 use it, otherwise figure out a server from the 'password server' param.
191 ************************************************************************/
193 static NTSTATUS domain_client_validate(TALLOC_CTX *mem_ctx,
194 const struct auth_usersupplied_info *user_info,
195 const char *domain,
196 uchar chal[8],
197 struct auth_serversupplied_info **server_info,
198 const char *dc_name,
199 const struct sockaddr_storage *dc_ss)
202 struct netr_SamInfo3 *info3 = NULL;
203 struct cli_state *cli = NULL;
204 struct rpc_pipe_client *netlogon_pipe = NULL;
205 NTSTATUS nt_status = NT_STATUS_NO_LOGON_SERVERS;
206 int i;
209 * At this point, smb_apasswd points to the lanman response to
210 * the challenge in local_challenge, and smb_ntpasswd points to
211 * the NT response to the challenge in local_challenge. Ship
212 * these over the secure channel to a domain controller and
213 * see if they were valid.
216 /* rety loop for robustness */
218 for (i = 0; !NT_STATUS_IS_OK(nt_status) && (i < 3); i++) {
219 nt_status = connect_to_domain_password_server(&cli,
220 domain,
221 dc_name,
222 dc_ss,
223 &netlogon_pipe);
226 if ( !NT_STATUS_IS_OK(nt_status) ) {
227 DEBUG(0,("domain_client_validate: Domain password server not available.\n"));
228 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
229 return NT_STATUS_TRUSTED_RELATIONSHIP_FAILURE;
231 return nt_status;
234 /* store a successful connection */
236 saf_store(domain, dc_name);
239 * If this call succeeds, we now have lots of info about the user
240 * in the info3 structure.
243 nt_status = rpccli_netlogon_sam_network_logon(netlogon_pipe,
244 mem_ctx,
245 user_info->logon_parameters, /* flags such as 'allow workstation logon' */
246 dc_name, /* server name */
247 user_info->client.account_name, /* user name logging on. */
248 user_info->client.domain_name, /* domain name */
249 user_info->workstation_name, /* workstation name */
250 chal, /* 8 byte challenge. */
251 3, /* validation level */
252 user_info->password.response.lanman, /* lanman 24 byte response */
253 user_info->password.response.nt, /* nt 24 byte response */
254 &info3); /* info3 out */
256 /* Let go as soon as possible so we avoid any potential deadlocks
257 with winbind lookup up users or groups. */
259 TALLOC_FREE(mutex);
261 if (!NT_STATUS_IS_OK(nt_status)) {
262 DEBUG(0,("domain_client_validate: unable to validate password "
263 "for user %s in domain %s to Domain controller %s. "
264 "Error was %s.\n", user_info->client.account_name,
265 user_info->client.domain_name, dc_name,
266 nt_errstr(nt_status)));
268 /* map to something more useful */
269 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_UNSUCCESSFUL)) {
270 nt_status = NT_STATUS_NO_LOGON_SERVERS;
272 } else {
273 nt_status = make_server_info_info3(mem_ctx,
274 user_info->client.account_name,
275 domain,
276 server_info,
277 info3);
279 if (NT_STATUS_IS_OK(nt_status)) {
280 (*server_info)->nss_token |= user_info->was_mapped;
281 netsamlogon_cache_store(user_info->client.account_name, info3);
282 TALLOC_FREE(info3);
286 /* Note - once the cli stream is shutdown the mem_ctx used
287 to allocate the other_sids and gids structures has been deleted - so
288 these pointers are no longer valid..... */
290 cli_shutdown(cli);
291 return nt_status;
294 /****************************************************************************
295 Check for a valid username and password in security=domain mode.
296 ****************************************************************************/
298 static NTSTATUS check_ntdomain_security(const struct auth_context *auth_context,
299 void *my_private_data,
300 TALLOC_CTX *mem_ctx,
301 const struct auth_usersupplied_info *user_info,
302 struct auth_serversupplied_info **server_info)
304 NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
305 const char *domain = lp_workgroup();
306 fstring dc_name;
307 struct sockaddr_storage dc_ss;
309 if ( lp_server_role() != ROLE_DOMAIN_MEMBER ) {
310 DEBUG(0,("check_ntdomain_security: Configuration error! Cannot use "
311 "ntdomain auth method when not a member of a domain.\n"));
312 return NT_STATUS_NOT_IMPLEMENTED;
315 if (!user_info || !server_info || !auth_context) {
316 DEBUG(1,("check_ntdomain_security: Critical variables not present. Failing.\n"));
317 return NT_STATUS_INVALID_PARAMETER;
320 DEBUG(10, ("Check auth for: [%s]\n", user_info->mapped.account_name));
323 * Check that the requested domain is not our own machine name.
324 * If it is, we should never check the PDC here, we use our own local
325 * password file.
328 if(strequal(get_global_sam_name(), user_info->mapped.domain_name)) {
329 DEBUG(3,("check_ntdomain_security: Requested domain was for this machine.\n"));
330 return NT_STATUS_NOT_IMPLEMENTED;
333 /* we need our DC to send the net_sam_logon() request to */
335 if ( !get_dc_name(domain, NULL, dc_name, &dc_ss) ) {
336 DEBUG(5,("check_ntdomain_security: unable to locate a DC for domain %s\n",
337 user_info->mapped.domain_name));
338 return NT_STATUS_NO_LOGON_SERVERS;
341 nt_status = domain_client_validate(mem_ctx,
342 user_info,
343 domain,
344 (uchar *)auth_context->challenge.data,
345 server_info,
346 dc_name,
347 &dc_ss);
349 return nt_status;
352 /* module initialisation */
353 static NTSTATUS auth_init_ntdomain(struct auth_context *auth_context, const char* param, auth_methods **auth_method)
355 struct auth_methods *result;
357 result = talloc_zero(auth_context, struct auth_methods);
358 if (result == NULL) {
359 return NT_STATUS_NO_MEMORY;
361 result->name = "ntdomain";
362 result->auth = check_ntdomain_security;
364 *auth_method = result;
365 return NT_STATUS_OK;
369 /****************************************************************************
370 Check for a valid username and password in a trusted domain
371 ****************************************************************************/
373 static NTSTATUS check_trustdomain_security(const struct auth_context *auth_context,
374 void *my_private_data,
375 TALLOC_CTX *mem_ctx,
376 const struct auth_usersupplied_info *user_info,
377 struct auth_serversupplied_info **server_info)
379 NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
380 unsigned char trust_md4_password[16];
381 char *trust_password;
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;
390 DEBUG(10, ("Check auth for: [%s]\n", user_info->mapped.account_name));
393 * Check that the requested domain is not our own machine name or domain name.
396 if( strequal(get_global_sam_name(), user_info->mapped.domain_name)) {
397 DEBUG(3,("check_trustdomain_security: Requested domain [%s] was for this machine.\n",
398 user_info->mapped.domain_name));
399 return NT_STATUS_NOT_IMPLEMENTED;
402 /* No point is bothering if this is not a trusted domain.
403 This return makes "map to guest = bad user" work again.
404 The logic is that if we know nothing about the domain, that
405 user is not known to us and does not exist */
407 if ( !is_trusted_domain( user_info->mapped.domain_name ) )
408 return NT_STATUS_NOT_IMPLEMENTED;
411 * Get the trusted account password for the trusted domain
412 * No need to become_root() as secrets_init() is done at startup.
415 if (!pdb_get_trusteddom_pw(user_info->mapped.domain_name, &trust_password,
416 NULL, NULL)) {
417 DEBUG(0, ("check_trustdomain_security: could not fetch trust "
418 "account password for domain %s\n",
419 user_info->mapped.domain_name));
420 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
423 #ifdef DEBUG_PASSWORD
424 DEBUG(100, ("Trust password for domain %s is %s\n", user_info->mapped.domain_name,
425 trust_password));
426 #endif
427 E_md4hash(trust_password, trust_md4_password);
428 SAFE_FREE(trust_password);
430 /* use get_dc_name() for consistency even through we know that it will be
431 a netbios name */
433 if ( !get_dc_name(user_info->mapped.domain_name, NULL, dc_name, &dc_ss) ) {
434 DEBUG(5,("check_trustdomain_security: unable to locate a DC for domain %s\n",
435 user_info->mapped.domain_name));
436 return NT_STATUS_NO_LOGON_SERVERS;
439 nt_status = domain_client_validate(mem_ctx,
440 user_info,
441 user_info->mapped.domain_name,
442 (uchar *)auth_context->challenge.data,
443 server_info,
444 dc_name,
445 &dc_ss);
447 return nt_status;
450 /* module initialisation */
451 static NTSTATUS auth_init_trustdomain(struct auth_context *auth_context, const char* param, auth_methods **auth_method)
453 struct auth_methods *result;
455 result = talloc_zero(auth_context, struct auth_methods);
456 if (result == NULL) {
457 return NT_STATUS_NO_MEMORY;
459 result->name = "trustdomain";
460 result->auth = check_trustdomain_security;
462 *auth_method = result;
463 return NT_STATUS_OK;
466 NTSTATUS auth_domain_init(void)
468 smb_register_auth(AUTH_INTERFACE_VERSION, "trustdomain", auth_init_trustdomain);
469 smb_register_auth(AUTH_INTERFACE_VERSION, "ntdomain", auth_init_ntdomain);
470 return NT_STATUS_OK;