s4: auth: Add TALLOC_CTX * to auth_register()
[Samba.git] / source4 / auth / ntlm / auth_sam.c
blobf7da04e657375afbe98b15ce4bd09ddc4d5970e1
1 /*
2 Unix SMB/CIFS implementation.
3 Password and authentication handling
4 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2001-2009
5 Copyright (C) Gerald Carter 2003
6 Copyright (C) Stefan Metzmacher 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "system/time.h"
24 #include <ldb.h>
25 #include "libcli/ldap/ldap_ndr.h"
26 #include "libcli/security/security.h"
27 #include "auth/auth.h"
28 #include "../libcli/auth/ntlm_check.h"
29 #include "auth/ntlm/auth_proto.h"
30 #include "auth/auth_sam.h"
31 #include "dsdb/samdb/samdb.h"
32 #include "dsdb/common/util.h"
33 #include "param/param.h"
34 #include "librpc/gen_ndr/ndr_irpc_c.h"
35 #include "lib/messaging/irpc.h"
36 #include "libcli/auth/libcli_auth.h"
37 #include "libds/common/roles.h"
39 NTSTATUS auth_sam_init(void);
41 extern const char *user_attrs[];
42 extern const char *domain_ref_attrs[];
44 /****************************************************************************
45 Look for the specified user in the sam, return ldb result structures
46 ****************************************************************************/
48 static NTSTATUS authsam_search_account(TALLOC_CTX *mem_ctx, struct ldb_context *sam_ctx,
49 const char *account_name,
50 struct ldb_dn *domain_dn,
51 struct ldb_message **ret_msg)
53 int ret;
55 /* pull the user attributes */
56 ret = dsdb_search_one(sam_ctx, mem_ctx, ret_msg, domain_dn, LDB_SCOPE_SUBTREE,
57 user_attrs,
58 DSDB_SEARCH_SHOW_EXTENDED_DN,
59 "(&(sAMAccountName=%s)(objectclass=user))",
60 ldb_binary_encode_string(mem_ctx, account_name));
61 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
62 DEBUG(3,("sam_search_user: Couldn't find user [%s] in samdb, under %s\n",
63 account_name, ldb_dn_get_linearized(domain_dn)));
64 return NT_STATUS_NO_SUCH_USER;
66 if (ret != LDB_SUCCESS) {
67 return NT_STATUS_INTERNAL_DB_CORRUPTION;
70 return NT_STATUS_OK;
73 /****************************************************************************
74 Do a specific test for an smb password being correct, given a smb_password and
75 the lanman and NT responses.
76 ****************************************************************************/
77 static NTSTATUS authsam_password_ok(struct auth4_context *auth_context,
78 TALLOC_CTX *mem_ctx,
79 uint16_t acct_flags,
80 const struct samr_Password *lm_pwd,
81 const struct samr_Password *nt_pwd,
82 const struct auth_usersupplied_info *user_info,
83 DATA_BLOB *user_sess_key,
84 DATA_BLOB *lm_sess_key)
86 NTSTATUS status;
88 switch (user_info->password_state) {
89 case AUTH_PASSWORD_PLAIN:
91 const struct auth_usersupplied_info *user_info_temp;
92 status = encrypt_user_info(mem_ctx, auth_context,
93 AUTH_PASSWORD_HASH,
94 user_info, &user_info_temp);
95 if (!NT_STATUS_IS_OK(status)) {
96 DEBUG(1, ("Failed to convert plaintext password to password HASH: %s\n", nt_errstr(status)));
97 return status;
99 user_info = user_info_temp;
101 /*fall through*/
103 case AUTH_PASSWORD_HASH:
104 *lm_sess_key = data_blob(NULL, 0);
105 *user_sess_key = data_blob(NULL, 0);
106 status = hash_password_check(mem_ctx,
107 lpcfg_lanman_auth(auth_context->lp_ctx),
108 user_info->password.hash.lanman,
109 user_info->password.hash.nt,
110 user_info->mapped.account_name,
111 lm_pwd, nt_pwd);
112 NT_STATUS_NOT_OK_RETURN(status);
113 break;
115 case AUTH_PASSWORD_RESPONSE:
116 status = ntlm_password_check(mem_ctx,
117 lpcfg_lanman_auth(auth_context->lp_ctx),
118 lpcfg_ntlm_auth(auth_context->lp_ctx),
119 user_info->logon_parameters,
120 &auth_context->challenge.data,
121 &user_info->password.response.lanman,
122 &user_info->password.response.nt,
123 user_info->mapped.account_name,
124 user_info->client.account_name,
125 user_info->client.domain_name,
126 lm_pwd, nt_pwd,
127 user_sess_key, lm_sess_key);
128 NT_STATUS_NOT_OK_RETURN(status);
129 break;
132 return NT_STATUS_OK;
137 send a message to the drepl server telling it to initiate a
138 REPL_SECRET getncchanges extended op to fetch the users secrets
140 static void auth_sam_trigger_repl_secret(struct auth4_context *auth_context,
141 struct ldb_dn *user_dn)
143 struct dcerpc_binding_handle *irpc_handle;
144 struct drepl_trigger_repl_secret r;
145 struct tevent_req *req;
146 TALLOC_CTX *tmp_ctx;
148 tmp_ctx = talloc_new(auth_context);
149 if (tmp_ctx == NULL) {
150 return;
153 irpc_handle = irpc_binding_handle_by_name(tmp_ctx, auth_context->msg_ctx,
154 "dreplsrv",
155 &ndr_table_irpc);
156 if (irpc_handle == NULL) {
157 DEBUG(1,(__location__ ": Unable to get binding handle for dreplsrv\n"));
158 TALLOC_FREE(tmp_ctx);
159 return;
162 r.in.user_dn = ldb_dn_get_linearized(user_dn);
165 * This seem to rely on the current IRPC implementation,
166 * which delivers the message in the _send function.
168 * TODO: we need a ONE_WAY IRPC handle and register
169 * a callback and wait for it to be triggered!
171 req = dcerpc_drepl_trigger_repl_secret_r_send(tmp_ctx,
172 auth_context->event_ctx,
173 irpc_handle,
174 &r);
176 /* we aren't interested in a reply */
177 talloc_free(req);
178 TALLOC_FREE(tmp_ctx);
183 * Check that a password is OK, and update badPwdCount if required.
186 static NTSTATUS authsam_password_check_and_record(struct auth4_context *auth_context,
187 TALLOC_CTX *mem_ctx,
188 struct ldb_dn *domain_dn,
189 struct ldb_message *msg,
190 uint16_t acct_flags,
191 const struct auth_usersupplied_info *user_info,
192 DATA_BLOB *user_sess_key,
193 DATA_BLOB *lm_sess_key)
195 NTSTATUS nt_status;
196 NTSTATUS auth_status;
197 TALLOC_CTX *tmp_ctx;
198 int i, ret;
199 int history_len = 0;
200 struct ldb_context *sam_ctx = auth_context->sam_ctx;
201 const char * const attrs[] = { "pwdHistoryLength", NULL };
202 struct ldb_message *dom_msg;
203 struct samr_Password *lm_pwd;
204 struct samr_Password *nt_pwd;
206 tmp_ctx = talloc_new(mem_ctx);
207 if (tmp_ctx == NULL) {
208 return NT_STATUS_NO_MEMORY;
212 * This call does more than what it appears to do, it also
213 * checks for the account lockout.
215 * It is done here so that all parts of Samba that read the
216 * password refuse to even operate on it if the account is
217 * locked out, to avoid mistakes like CVE-2013-4496.
219 nt_status = samdb_result_passwords(tmp_ctx, auth_context->lp_ctx,
220 msg, &lm_pwd, &nt_pwd);
221 if (!NT_STATUS_IS_OK(nt_status)) {
222 TALLOC_FREE(tmp_ctx);
223 return nt_status;
226 if (lm_pwd == NULL && nt_pwd == NULL) {
227 bool am_rodc;
228 if (samdb_rodc(auth_context->sam_ctx, &am_rodc) == LDB_SUCCESS && am_rodc) {
230 * we don't have passwords for this
231 * account. We are an RODC, and this account
232 * may be one for which we either are denied
233 * REPL_SECRET replication or we haven't yet
234 * done the replication. We return
235 * NT_STATUS_NOT_IMPLEMENTED which tells the
236 * auth code to try the next authentication
237 * mechanism. We also send a message to our
238 * drepl server to tell it to try and
239 * replicate the secrets for this account.
241 * TODO: Should we only trigger this is detected
242 * there's a chance that the password might be
243 * replicated, we should be able to detect this
244 * based on msDS-NeverRevealGroup.
246 auth_sam_trigger_repl_secret(auth_context, msg->dn);
247 TALLOC_FREE(tmp_ctx);
248 return NT_STATUS_NOT_IMPLEMENTED;
252 auth_status = authsam_password_ok(auth_context, tmp_ctx,
253 acct_flags,
254 lm_pwd, nt_pwd,
255 user_info,
256 user_sess_key, lm_sess_key);
257 if (NT_STATUS_IS_OK(auth_status)) {
258 if (user_sess_key->data) {
259 talloc_steal(mem_ctx, user_sess_key->data);
261 if (lm_sess_key->data) {
262 talloc_steal(mem_ctx, lm_sess_key->data);
264 TALLOC_FREE(tmp_ctx);
265 return NT_STATUS_OK;
267 *user_sess_key = data_blob_null;
268 *lm_sess_key = data_blob_null;
270 if (!NT_STATUS_EQUAL(auth_status, NT_STATUS_WRONG_PASSWORD)) {
271 TALLOC_FREE(tmp_ctx);
272 return auth_status;
276 * We only continue if this was a wrong password
277 * and we'll always return NT_STATUS_WRONG_PASSWORD
278 * no matter what error happens.
281 /* pull the domain password property attributes */
282 ret = dsdb_search_one(sam_ctx, tmp_ctx, &dom_msg, domain_dn, LDB_SCOPE_BASE,
283 attrs, 0, "objectClass=domain");
284 if (ret == LDB_SUCCESS) {
285 history_len = ldb_msg_find_attr_as_uint(dom_msg, "pwdHistoryLength", 0);
286 } else if (ret == LDB_ERR_NO_SUCH_OBJECT) {
287 DEBUG(3,("Couldn't find domain %s: %s!\n",
288 ldb_dn_get_linearized(domain_dn),
289 ldb_errstring(sam_ctx)));
290 } else {
291 DEBUG(3,("error finding domain %s: %s!\n",
292 ldb_dn_get_linearized(domain_dn),
293 ldb_errstring(sam_ctx)));
296 for (i = 1; i < MIN(history_len, 3); i++) {
297 struct samr_Password zero_string_hash;
298 struct samr_Password zero_string_des_hash;
299 struct samr_Password *nt_history_pwd = NULL;
300 struct samr_Password *lm_history_pwd = NULL;
301 NTTIME pwdLastSet;
302 struct timeval tv_now;
303 NTTIME now;
304 int allowed_period_mins;
305 NTTIME allowed_period;
307 nt_status = samdb_result_passwords_from_history(tmp_ctx,
308 auth_context->lp_ctx,
309 msg, i,
310 &lm_history_pwd,
311 &nt_history_pwd);
312 if (!NT_STATUS_IS_OK(nt_status)) {
314 * If we don't find element 'i' we won't find
315 * 'i+1' ...
317 break;
321 * We choose to avoid any issues
322 * around different LM and NT history
323 * lengths by only checking the NT
324 * history
326 if (nt_history_pwd == NULL) {
328 * If we don't find element 'i' we won't find
329 * 'i+1' ...
331 break;
334 /* Skip over all-zero hashes in the history */
335 if (all_zero(nt_history_pwd->hash,
336 sizeof(nt_history_pwd->hash))) {
337 continue;
341 * This looks odd, but the password_hash module writes this in if
342 * (somehow) we didn't have an old NT hash
345 E_md4hash("", zero_string_hash.hash);
346 if (memcmp(nt_history_pwd->hash, zero_string_hash.hash, 16) == 0) {
347 continue;
350 E_deshash("", zero_string_des_hash.hash);
351 if (!lm_history_pwd || memcmp(lm_history_pwd->hash, zero_string_des_hash.hash, 16) == 0) {
352 lm_history_pwd = NULL;
355 auth_status = authsam_password_ok(auth_context, tmp_ctx,
356 acct_flags,
357 lm_history_pwd,
358 nt_history_pwd,
359 user_info,
360 user_sess_key,
361 lm_sess_key);
362 if (!NT_STATUS_IS_OK(auth_status)) {
364 * If this was not a correct password, try the next
365 * one from the history
367 *user_sess_key = data_blob_null;
368 *lm_sess_key = data_blob_null;
369 continue;
372 if (i != 1) {
374 * The authentication was OK, but not against
375 * the previous password, which is stored at index 1.
377 * We just return the original wrong password.
378 * This skips the update of the bad pwd count,
379 * because this is almost certainly user error
380 * (or automatic login on a computer using a cached
381 * password from before the password change),
382 * not an attack.
384 TALLOC_FREE(tmp_ctx);
385 return NT_STATUS_WRONG_PASSWORD;
388 if (user_info->password_state != AUTH_PASSWORD_RESPONSE) {
390 * The authentication was OK against the previous password,
391 * but it's not a NTLM network authentication.
393 * We just return the original wrong password.
394 * This skips the update of the bad pwd count,
395 * because this is almost certainly user error
396 * (or automatic login on a computer using a cached
397 * password from before the password change),
398 * not an attack.
400 TALLOC_FREE(tmp_ctx);
401 return NT_STATUS_WRONG_PASSWORD;
405 * If the password was OK, it's a NTLM network authentication
406 * and it was the previous password.
408 * Now we see if it is within the grace period,
409 * so that we don't break cached sessions on other computers
410 * before the user can lock and unlock their other screens
411 * (resetting their cached password).
413 * See http://support.microsoft.com/kb/906305
414 * OldPasswordAllowedPeriod ("old password allowed period")
415 * is specified in minutes. The default is 60.
417 allowed_period_mins = lpcfg_old_password_allowed_period(auth_context->lp_ctx);
419 * NTTIME uses 100ns units
421 allowed_period = allowed_period_mins * 60 * 1000*1000*10;
422 pwdLastSet = samdb_result_nttime(msg, "pwdLastSet", 0);
423 tv_now = timeval_current();
424 now = timeval_to_nttime(&tv_now);
426 if (now < pwdLastSet) {
428 * time jump?
430 * We just return the original wrong password.
431 * This skips the update of the bad pwd count,
432 * because this is almost certainly user error
433 * (or automatic login on a computer using a cached
434 * password from before the password change),
435 * not an attack.
437 TALLOC_FREE(tmp_ctx);
438 return NT_STATUS_WRONG_PASSWORD;
441 if ((now - pwdLastSet) >= allowed_period) {
443 * The allowed period is over.
445 * We just return the original wrong password.
446 * This skips the update of the bad pwd count,
447 * because this is almost certainly user error
448 * (or automatic login on a computer using a cached
449 * password from before the password change),
450 * not an attack.
452 TALLOC_FREE(tmp_ctx);
453 return NT_STATUS_WRONG_PASSWORD;
457 * We finally allow the authentication with the
458 * previous password within the allowed period.
460 if (user_sess_key->data) {
461 talloc_steal(mem_ctx, user_sess_key->data);
463 if (lm_sess_key->data) {
464 talloc_steal(mem_ctx, lm_sess_key->data);
467 TALLOC_FREE(tmp_ctx);
468 return auth_status;
472 * If we are not in the allowed period or match an old password,
473 * we didn't return early. Now update the badPwdCount et al.
475 nt_status = authsam_update_bad_pwd_count(auth_context->sam_ctx,
476 msg, domain_dn);
477 if (!NT_STATUS_IS_OK(nt_status)) {
479 * We need to return the original
480 * NT_STATUS_WRONG_PASSWORD error, so there isn't
481 * anything more we can do than write something into
482 * the log
484 DEBUG(0, ("Failed to note bad password for user [%s]: %s\n",
485 user_info->mapped.account_name,
486 nt_errstr(nt_status)));
489 TALLOC_FREE(tmp_ctx);
490 return NT_STATUS_WRONG_PASSWORD;
493 static NTSTATUS authsam_authenticate(struct auth4_context *auth_context,
494 TALLOC_CTX *mem_ctx, struct ldb_context *sam_ctx,
495 struct ldb_dn *domain_dn,
496 struct ldb_message *msg,
497 const struct auth_usersupplied_info *user_info,
498 DATA_BLOB *user_sess_key, DATA_BLOB *lm_sess_key)
500 NTSTATUS nt_status;
501 bool interactive = (user_info->password_state == AUTH_PASSWORD_HASH);
502 uint32_t acct_flags = samdb_result_acct_flags(msg, NULL);
503 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
504 if (!tmp_ctx) {
505 return NT_STATUS_NO_MEMORY;
508 /* You can only do an interactive login to normal accounts */
509 if (user_info->flags & USER_INFO_INTERACTIVE_LOGON) {
510 if (!(acct_flags & ACB_NORMAL)) {
511 TALLOC_FREE(tmp_ctx);
512 return NT_STATUS_NO_SUCH_USER;
514 if (acct_flags & ACB_SMARTCARD_REQUIRED) {
515 if (acct_flags & ACB_DISABLED) {
516 DEBUG(2,("authsam_authenticate: Account for user '%s' "
517 "was disabled.\n",
518 user_info->mapped.account_name));
519 TALLOC_FREE(tmp_ctx);
520 return NT_STATUS_ACCOUNT_DISABLED;
522 DEBUG(2,("authsam_authenticate: Account for user '%s' "
523 "requires interactive smartcard logon.\n",
524 user_info->mapped.account_name));
525 TALLOC_FREE(tmp_ctx);
526 return NT_STATUS_SMARTCARD_LOGON_REQUIRED;
530 nt_status = authsam_password_check_and_record(auth_context, tmp_ctx,
531 domain_dn, msg, acct_flags,
532 user_info,
533 user_sess_key, lm_sess_key);
534 if (!NT_STATUS_IS_OK(nt_status)) {
535 TALLOC_FREE(tmp_ctx);
536 return nt_status;
539 nt_status = authsam_account_ok(tmp_ctx, auth_context->sam_ctx,
540 user_info->logon_parameters,
541 domain_dn,
542 msg,
543 user_info->workstation_name,
544 user_info->mapped.account_name,
545 false, false);
546 if (!NT_STATUS_IS_OK(nt_status)) {
547 TALLOC_FREE(tmp_ctx);
548 return nt_status;
551 nt_status = authsam_logon_success_accounting(auth_context->sam_ctx,
552 msg, domain_dn,
553 interactive);
554 if (!NT_STATUS_IS_OK(nt_status)) {
555 TALLOC_FREE(tmp_ctx);
556 return nt_status;
559 if (user_sess_key && user_sess_key->data) {
560 talloc_steal(mem_ctx, user_sess_key->data);
562 if (lm_sess_key && lm_sess_key->data) {
563 talloc_steal(mem_ctx, lm_sess_key->data);
566 TALLOC_FREE(tmp_ctx);
567 return nt_status;
572 static NTSTATUS authsam_check_password_internals(struct auth_method_context *ctx,
573 TALLOC_CTX *mem_ctx,
574 const struct auth_usersupplied_info *user_info,
575 struct auth_user_info_dc **user_info_dc)
577 NTSTATUS nt_status;
578 const char *account_name = user_info->mapped.account_name;
579 struct ldb_message *msg;
580 struct ldb_dn *domain_dn;
581 DATA_BLOB user_sess_key, lm_sess_key;
582 TALLOC_CTX *tmp_ctx;
583 const char *p = NULL;
585 if (ctx->auth_ctx->sam_ctx == NULL) {
586 DEBUG(0, ("No SAM available, cannot log in users\n"));
587 return NT_STATUS_INVALID_SYSTEM_SERVICE;
590 if (!account_name || !*account_name) {
591 /* 'not for me' */
592 return NT_STATUS_NOT_IMPLEMENTED;
595 tmp_ctx = talloc_new(mem_ctx);
596 if (!tmp_ctx) {
597 return NT_STATUS_NO_MEMORY;
600 domain_dn = ldb_get_default_basedn(ctx->auth_ctx->sam_ctx);
601 if (domain_dn == NULL) {
602 talloc_free(tmp_ctx);
603 return NT_STATUS_NO_SUCH_DOMAIN;
606 p = strchr_m(account_name, '@');
607 if (p != NULL) {
608 const char *nt4_domain = NULL;
609 const char *nt4_account = NULL;
610 bool is_my_domain = false;
612 nt_status = crack_name_to_nt4_name(mem_ctx,
613 ctx->auth_ctx->event_ctx,
614 ctx->auth_ctx->lp_ctx,
616 * DRSUAPI_DS_NAME_FORMAT_UPN_FOR_LOGON ?
618 DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL,
619 account_name,
620 &nt4_domain, &nt4_account);
621 if (!NT_STATUS_IS_OK(nt_status)) {
622 talloc_free(tmp_ctx);
623 return NT_STATUS_NO_SUCH_USER;
626 is_my_domain = lpcfg_is_mydomain(ctx->auth_ctx->lp_ctx, nt4_domain);
627 if (!is_my_domain) {
629 * This is a user within our forest,
630 * but in a different domain,
631 * we're not authoritative
633 talloc_free(tmp_ctx);
634 return NT_STATUS_NOT_IMPLEMENTED;
638 * Let's use the NT4 account name for the lookup.
640 account_name = nt4_account;
643 nt_status = authsam_search_account(tmp_ctx, ctx->auth_ctx->sam_ctx, account_name, domain_dn, &msg);
644 if (!NT_STATUS_IS_OK(nt_status)) {
645 talloc_free(tmp_ctx);
646 return nt_status;
649 nt_status = authsam_authenticate(ctx->auth_ctx, tmp_ctx, ctx->auth_ctx->sam_ctx, domain_dn, msg, user_info,
650 &user_sess_key, &lm_sess_key);
651 if (!NT_STATUS_IS_OK(nt_status)) {
652 talloc_free(tmp_ctx);
653 return nt_status;
656 nt_status = authsam_make_user_info_dc(tmp_ctx, ctx->auth_ctx->sam_ctx,
657 lpcfg_netbios_name(ctx->auth_ctx->lp_ctx),
658 lpcfg_sam_name(ctx->auth_ctx->lp_ctx),
659 lpcfg_sam_dnsname(ctx->auth_ctx->lp_ctx),
660 domain_dn,
661 msg,
662 user_sess_key, lm_sess_key,
663 user_info_dc);
664 if (!NT_STATUS_IS_OK(nt_status)) {
665 talloc_free(tmp_ctx);
666 return nt_status;
669 talloc_steal(mem_ctx, *user_info_dc);
670 talloc_free(tmp_ctx);
672 return NT_STATUS_OK;
675 static NTSTATUS authsam_ignoredomain_want_check(struct auth_method_context *ctx,
676 TALLOC_CTX *mem_ctx,
677 const struct auth_usersupplied_info *user_info)
679 if (!user_info->mapped.account_name || !*user_info->mapped.account_name) {
680 return NT_STATUS_NOT_IMPLEMENTED;
683 return NT_STATUS_OK;
686 /****************************************************************************
687 Check SAM security (above) but with a few extra checks.
688 ****************************************************************************/
689 static NTSTATUS authsam_want_check(struct auth_method_context *ctx,
690 TALLOC_CTX *mem_ctx,
691 const struct auth_usersupplied_info *user_info)
693 const char *effective_domain = user_info->mapped.domain_name;
694 bool is_local_name = false;
695 bool is_my_domain = false;
696 const char *p = NULL;
697 struct dsdb_trust_routing_table *trt = NULL;
698 const struct lsa_TrustDomainInfoInfoEx *tdo = NULL;
699 NTSTATUS status;
701 if (!user_info->mapped.account_name || !*user_info->mapped.account_name) {
702 return NT_STATUS_NOT_IMPLEMENTED;
705 is_local_name = lpcfg_is_myname(ctx->auth_ctx->lp_ctx,
706 effective_domain);
708 /* check whether or not we service this domain/workgroup name */
709 switch (lpcfg_server_role(ctx->auth_ctx->lp_ctx)) {
710 case ROLE_STANDALONE:
711 return NT_STATUS_OK;
713 case ROLE_DOMAIN_MEMBER:
714 if (is_local_name) {
715 return NT_STATUS_OK;
718 DBG_DEBUG("%s is not one of my local names (DOMAIN_MEMBER)\n",
719 effective_domain);
720 return NT_STATUS_NOT_IMPLEMENTED;
722 case ROLE_ACTIVE_DIRECTORY_DC:
723 /* handled later */
724 break;
726 default:
727 DBG_ERR("lpcfg_server_role() has an undefined value\n");
728 return NT_STATUS_INVALID_SERVER_STATE;
732 * Now we handle the AD DC case...
735 is_my_domain = lpcfg_is_my_domain_or_realm(ctx->auth_ctx->lp_ctx,
736 effective_domain);
737 if (is_my_domain) {
738 return NT_STATUS_OK;
741 if (user_info->mapped_state) {
743 * The caller already did a cracknames call.
745 DBG_DEBUG("%s is not one domain name (DC)\n",
746 effective_domain);
747 return NT_STATUS_NOT_IMPLEMENTED;
750 if (effective_domain != NULL && !strequal(effective_domain, "")) {
751 DBG_DEBUG("%s is not one domain name (DC)\n",
752 effective_domain);
753 return NT_STATUS_NOT_IMPLEMENTED;
756 p = strchr_m(user_info->mapped.account_name, '@');
757 if (p == NULL) {
758 if (effective_domain == NULL) {
759 return NT_STATUS_OK;
761 DEBUG(6,("authsam_check_password: '' without upn not handled (DC)\n"));
762 return NT_STATUS_NOT_IMPLEMENTED;
765 effective_domain = p + 1;
766 is_my_domain = lpcfg_is_my_domain_or_realm(ctx->auth_ctx->lp_ctx,
767 effective_domain);
768 if (is_my_domain) {
769 return NT_STATUS_OK;
772 if (strequal(effective_domain, "")) {
773 DBG_DEBUG("authsam_check_password: upn without realm (DC)\n");
774 return NT_STATUS_NOT_IMPLEMENTED;
778 * as last option we check the routing table if the
779 * domain is within our forest.
781 status = dsdb_trust_routing_table_load(ctx->auth_ctx->sam_ctx,
782 mem_ctx, &trt);
783 if (!NT_STATUS_IS_OK(status)) {
784 DBG_ERR("authsam_check_password: dsdb_trust_routing_table_load() %s\n",
785 nt_errstr(status));
786 return status;
789 tdo = dsdb_trust_routing_by_name(trt, effective_domain);
790 if (tdo == NULL) {
791 DBG_DEBUG("%s is not a known TLN (DC)\n",
792 effective_domain);
793 TALLOC_FREE(trt);
794 return NT_STATUS_NOT_IMPLEMENTED;
797 if (!(tdo->trust_attributes & LSA_TRUST_ATTRIBUTE_WITHIN_FOREST)) {
798 DBG_DEBUG("%s is not a TLN in our forest (DC)\n",
799 effective_domain);
800 TALLOC_FREE(trt);
801 return NT_STATUS_NOT_IMPLEMENTED;
805 * This principal is within our forest.
806 * we'll later do a crack_name_to_nt4_name()
807 * to check if it's in our domain.
809 TALLOC_FREE(trt);
810 return NT_STATUS_OK;
813 static NTSTATUS authsam_failtrusts_want_check(struct auth_method_context *ctx,
814 TALLOC_CTX *mem_ctx,
815 const struct auth_usersupplied_info *user_info)
817 const char *effective_domain = user_info->mapped.domain_name;
818 struct dsdb_trust_routing_table *trt = NULL;
819 const struct lsa_TrustDomainInfoInfoEx *tdo = NULL;
820 NTSTATUS status;
822 /* check whether or not we service this domain/workgroup name */
823 switch (lpcfg_server_role(ctx->auth_ctx->lp_ctx)) {
824 case ROLE_ACTIVE_DIRECTORY_DC:
825 /* handled later */
826 break;
828 default:
829 DBG_ERR("lpcfg_server_role() has an undefined value\n");
830 return NT_STATUS_NOT_IMPLEMENTED;
834 * Now we handle the AD DC case...
836 if (!user_info->mapped.account_name || !*user_info->mapped.account_name) {
837 return NT_STATUS_NOT_IMPLEMENTED;
840 if (effective_domain == NULL || strequal(effective_domain, "")) {
841 const char *p = NULL;
843 p = strchr_m(user_info->mapped.account_name, '@');
844 if (p != NULL) {
845 effective_domain = p + 1;
849 if (effective_domain == NULL || strequal(effective_domain, "")) {
850 DBG_DEBUG("%s is not a trusted domain\n",
851 effective_domain);
852 return NT_STATUS_NOT_IMPLEMENTED;
856 * as last option we check the routing table if the
857 * domain is within our forest.
859 status = dsdb_trust_routing_table_load(ctx->auth_ctx->sam_ctx,
860 mem_ctx, &trt);
861 if (!NT_STATUS_IS_OK(status)) {
862 DBG_ERR("authsam_check_password: dsdb_trust_routing_table_load() %s\n",
863 nt_errstr(status));
864 return status;
867 tdo = dsdb_trust_routing_by_name(trt, effective_domain);
868 if (tdo == NULL) {
869 DBG_DEBUG("%s is not a known TLN (DC)\n",
870 effective_domain);
871 TALLOC_FREE(trt);
872 return NT_STATUS_NOT_IMPLEMENTED;
876 * We now about the domain...
878 TALLOC_FREE(trt);
879 return NT_STATUS_OK;
882 static NTSTATUS authsam_failtrusts_check_password(struct auth_method_context *ctx,
883 TALLOC_CTX *mem_ctx,
884 const struct auth_usersupplied_info *user_info,
885 struct auth_user_info_dc **user_info_dc)
888 * This should a good error for now,
889 * until this module gets removed
890 * and we have a full async path
891 * to winbind.
893 return NT_STATUS_NO_TRUST_LSA_SECRET;
896 /* Wrapper for the auth subsystem pointer */
897 static NTSTATUS authsam_get_user_info_dc_principal_wrapper(TALLOC_CTX *mem_ctx,
898 struct auth4_context *auth_context,
899 const char *principal,
900 struct ldb_dn *user_dn,
901 struct auth_user_info_dc **user_info_dc)
903 return authsam_get_user_info_dc_principal(mem_ctx, auth_context->lp_ctx, auth_context->sam_ctx,
904 principal, user_dn, user_info_dc);
906 static const struct auth_operations sam_ignoredomain_ops = {
907 .name = "sam_ignoredomain",
908 .want_check = authsam_ignoredomain_want_check,
909 .check_password = authsam_check_password_internals,
910 .get_user_info_dc_principal = authsam_get_user_info_dc_principal_wrapper,
913 static const struct auth_operations sam_ops = {
914 .name = "sam",
915 .want_check = authsam_want_check,
916 .check_password = authsam_check_password_internals,
917 .get_user_info_dc_principal = authsam_get_user_info_dc_principal_wrapper,
920 static const struct auth_operations sam_failtrusts_ops = {
921 .name = "sam_failtrusts",
922 .want_check = authsam_failtrusts_want_check,
923 .check_password = authsam_failtrusts_check_password,
926 _PUBLIC_ NTSTATUS auth4_sam_init(TALLOC_CTX *);
927 _PUBLIC_ NTSTATUS auth4_sam_init(TALLOC_CTX *ctx)
929 NTSTATUS ret;
931 ret = auth_register(ctx, &sam_ops);
932 if (!NT_STATUS_IS_OK(ret)) {
933 DEBUG(0,("Failed to register 'sam' auth backend!\n"));
934 return ret;
937 ret = auth_register(ctx, &sam_ignoredomain_ops);
938 if (!NT_STATUS_IS_OK(ret)) {
939 DEBUG(0,("Failed to register 'sam_ignoredomain' auth backend!\n"));
940 return ret;
943 ret = auth_register(ctx, &sam_failtrusts_ops);
944 if (!NT_STATUS_IS_OK(ret)) {
945 DEBUG(0,("Failed to register 'sam_failtrusts' auth backend!\n"));
946 return ret;
949 return ret;