auth_winbind: Allow badPwdCount to be set to 0 with this auth method
[sfrench/samba-autobuild/.git] / source4 / auth / ntlm / auth_sam.c
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
7    
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.
12    
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.
17    
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/>.
20 */
21
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"
38
39 NTSTATUS auth_sam_init(void);
40
41 extern const char *user_attrs[];
42 extern const char *domain_ref_attrs[];
43
44 /****************************************************************************
45  Do a specific test for an smb password being correct, given a smb_password and
46  the lanman and NT responses.
47 ****************************************************************************/
48 static NTSTATUS authsam_password_ok(struct auth4_context *auth_context,
49                                     TALLOC_CTX *mem_ctx,
50                                     uint16_t acct_flags,
51                                     const struct samr_Password *lm_pwd, 
52                                     const struct samr_Password *nt_pwd,
53                                     const struct auth_usersupplied_info *user_info, 
54                                     DATA_BLOB *user_sess_key, 
55                                     DATA_BLOB *lm_sess_key)
56 {
57         NTSTATUS status;
58
59         switch (user_info->password_state) {
60         case AUTH_PASSWORD_PLAIN: 
61         {
62                 const struct auth_usersupplied_info *user_info_temp;    
63                 status = encrypt_user_info(mem_ctx, auth_context, 
64                                            AUTH_PASSWORD_HASH, 
65                                            user_info, &user_info_temp);
66                 if (!NT_STATUS_IS_OK(status)) {
67                         DEBUG(1, ("Failed to convert plaintext password to password HASH: %s\n", nt_errstr(status)));
68                         return status;
69                 }
70                 user_info = user_info_temp;
71
72                 /*fall through*/
73         }
74         case AUTH_PASSWORD_HASH:
75                 *lm_sess_key = data_blob(NULL, 0);
76                 *user_sess_key = data_blob(NULL, 0);
77                 status = hash_password_check(mem_ctx, 
78                                              lpcfg_lanman_auth(auth_context->lp_ctx),
79                                              user_info->password.hash.lanman,
80                                              user_info->password.hash.nt,
81                                              user_info->mapped.account_name,
82                                              lm_pwd, nt_pwd);
83                 NT_STATUS_NOT_OK_RETURN(status);
84                 break;
85                 
86         case AUTH_PASSWORD_RESPONSE:
87                 status = ntlm_password_check(mem_ctx, 
88                                              lpcfg_lanman_auth(auth_context->lp_ctx),
89                                                  lpcfg_ntlm_auth(auth_context->lp_ctx),
90                                              user_info->logon_parameters, 
91                                              &auth_context->challenge.data, 
92                                              &user_info->password.response.lanman, 
93                                              &user_info->password.response.nt,
94                                              user_info->mapped.account_name,
95                                              user_info->client.account_name, 
96                                              user_info->client.domain_name, 
97                                              lm_pwd, nt_pwd,
98                                              user_sess_key, lm_sess_key);
99                 NT_STATUS_NOT_OK_RETURN(status);
100                 break;
101         }
102
103         return NT_STATUS_OK;
104 }
105
106
107 /*
108   send a message to the drepl server telling it to initiate a
109   REPL_SECRET getncchanges extended op to fetch the users secrets
110  */
111 static void auth_sam_trigger_repl_secret(struct auth4_context *auth_context,
112                                          struct ldb_dn *user_dn)
113 {
114         struct dcerpc_binding_handle *irpc_handle;
115         struct drepl_trigger_repl_secret r;
116         struct tevent_req *req;
117         TALLOC_CTX *tmp_ctx;
118
119         tmp_ctx = talloc_new(auth_context);
120         if (tmp_ctx == NULL) {
121                 return;
122         }
123
124         irpc_handle = irpc_binding_handle_by_name(tmp_ctx, auth_context->msg_ctx,
125                                                   "dreplsrv",
126                                                   &ndr_table_irpc);
127         if (irpc_handle == NULL) {
128                 DEBUG(1,(__location__ ": Unable to get binding handle for dreplsrv\n"));
129                 TALLOC_FREE(tmp_ctx);
130                 return;
131         }
132
133         r.in.user_dn = ldb_dn_get_linearized(user_dn);
134
135         /*
136          * This seem to rely on the current IRPC implementation,
137          * which delivers the message in the _send function.
138          *
139          * TODO: we need a ONE_WAY IRPC handle and register
140          * a callback and wait for it to be triggered!
141          */
142         req = dcerpc_drepl_trigger_repl_secret_r_send(tmp_ctx,
143                                                       auth_context->event_ctx,
144                                                       irpc_handle,
145                                                       &r);
146
147         /* we aren't interested in a reply */
148         talloc_free(req);
149         TALLOC_FREE(tmp_ctx);
150 }
151
152
153 /*
154  * Check that a password is OK, and update badPwdCount if required.
155  */
156
157 static NTSTATUS authsam_password_check_and_record(struct auth4_context *auth_context,
158                                                   TALLOC_CTX *mem_ctx,
159                                                   struct ldb_dn *domain_dn,
160                                                   struct ldb_message *msg,
161                                                   uint16_t acct_flags,
162                                                   const struct auth_usersupplied_info *user_info,
163                                                   DATA_BLOB *user_sess_key,
164                                                   DATA_BLOB *lm_sess_key,
165                                                   bool *authoritative)
166 {
167         NTSTATUS nt_status;
168         NTSTATUS auth_status;
169         TALLOC_CTX *tmp_ctx;
170         int i, ret;
171         int history_len = 0;
172         struct ldb_context *sam_ctx = auth_context->sam_ctx;
173         const char * const attrs[] = { "pwdHistoryLength", NULL };
174         struct ldb_message *dom_msg;
175         struct samr_Password *lm_pwd;
176         struct samr_Password *nt_pwd;
177
178         tmp_ctx = talloc_new(mem_ctx);
179         if (tmp_ctx == NULL) {
180                 return NT_STATUS_NO_MEMORY;
181         }
182
183         /*
184          * This call does more than what it appears to do, it also
185          * checks for the account lockout.
186          *
187          * It is done here so that all parts of Samba that read the
188          * password refuse to even operate on it if the account is
189          * locked out, to avoid mistakes like CVE-2013-4496.
190          */
191         nt_status = samdb_result_passwords(tmp_ctx, auth_context->lp_ctx,
192                                            msg, &lm_pwd, &nt_pwd);
193         if (!NT_STATUS_IS_OK(nt_status)) {
194                 TALLOC_FREE(tmp_ctx);
195                 return nt_status;
196         }
197
198         if (lm_pwd == NULL && nt_pwd == NULL) {
199                 bool am_rodc;
200                 if (samdb_rodc(auth_context->sam_ctx, &am_rodc) == LDB_SUCCESS && am_rodc) {
201                         /*
202                          * we don't have passwords for this
203                          * account. We are an RODC, and this account
204                          * may be one for which we either are denied
205                          * REPL_SECRET replication or we haven't yet
206                          * done the replication. We return
207                          * NT_STATUS_NOT_IMPLEMENTED which tells the
208                          * auth code to try the next authentication
209                          * mechanism. We also send a message to our
210                          * drepl server to tell it to try and
211                          * replicate the secrets for this account.
212                          *
213                          * TODO: Should we only trigger this is detected
214                          * there's a chance that the password might be
215                          * replicated, we should be able to detect this
216                          * based on msDS-NeverRevealGroup.
217                          */
218                         auth_sam_trigger_repl_secret(auth_context, msg->dn);
219                         TALLOC_FREE(tmp_ctx);
220                         return NT_STATUS_NOT_IMPLEMENTED;
221                 }
222         }
223
224         auth_status = authsam_password_ok(auth_context, tmp_ctx,
225                                           acct_flags,
226                                           lm_pwd, nt_pwd,
227                                           user_info,
228                                           user_sess_key, lm_sess_key);
229         if (NT_STATUS_IS_OK(auth_status)) {
230                 if (user_sess_key->data) {
231                         talloc_steal(mem_ctx, user_sess_key->data);
232                 }
233                 if (lm_sess_key->data) {
234                         talloc_steal(mem_ctx, lm_sess_key->data);
235                 }
236                 TALLOC_FREE(tmp_ctx);
237                 return NT_STATUS_OK;
238         }
239         *user_sess_key = data_blob_null;
240         *lm_sess_key = data_blob_null;
241
242         if (!NT_STATUS_EQUAL(auth_status, NT_STATUS_WRONG_PASSWORD)) {
243                 TALLOC_FREE(tmp_ctx);
244                 return auth_status;
245         }
246
247         /*
248          * We only continue if this was a wrong password
249          * and we'll always return NT_STATUS_WRONG_PASSWORD
250          * no matter what error happens.
251          */
252
253         /* pull the domain password property attributes */
254         ret = dsdb_search_one(sam_ctx, tmp_ctx, &dom_msg, domain_dn, LDB_SCOPE_BASE,
255                               attrs, 0, "objectClass=domain");
256         if (ret == LDB_SUCCESS) {
257                 history_len = ldb_msg_find_attr_as_uint(dom_msg, "pwdHistoryLength", 0);
258         } else if (ret == LDB_ERR_NO_SUCH_OBJECT) {
259                 DEBUG(3,("Couldn't find domain %s: %s!\n",
260                          ldb_dn_get_linearized(domain_dn),
261                          ldb_errstring(sam_ctx)));
262         } else {
263                 DEBUG(3,("error finding domain %s: %s!\n",
264                          ldb_dn_get_linearized(domain_dn),
265                          ldb_errstring(sam_ctx)));
266         }
267
268         for (i = 1; i < MIN(history_len, 3); i++) {
269                 struct samr_Password zero_string_hash;
270                 struct samr_Password zero_string_des_hash;
271                 struct samr_Password *nt_history_pwd = NULL;
272                 struct samr_Password *lm_history_pwd = NULL;
273                 NTTIME pwdLastSet;
274                 struct timeval tv_now;
275                 NTTIME now;
276                 int allowed_period_mins;
277                 NTTIME allowed_period;
278
279                 nt_status = samdb_result_passwords_from_history(tmp_ctx,
280                                                         auth_context->lp_ctx,
281                                                         msg, i,
282                                                         &lm_history_pwd,
283                                                         &nt_history_pwd);
284                 if (!NT_STATUS_IS_OK(nt_status)) {
285                         /*
286                          * If we don't find element 'i' we won't find
287                          * 'i+1' ...
288                          */
289                         break;
290                 }
291
292                 /*
293                  * We choose to avoid any issues
294                  * around different LM and NT history
295                  * lengths by only checking the NT
296                  * history
297                  */
298                 if (nt_history_pwd == NULL) {
299                         /*
300                          * If we don't find element 'i' we won't find
301                          * 'i+1' ...
302                          */
303                         break;
304                 }
305
306                 /* Skip over all-zero hashes in the history */
307                 if (all_zero(nt_history_pwd->hash,
308                              sizeof(nt_history_pwd->hash))) {
309                         continue;
310                 }
311
312                 /*
313                  * This looks odd, but the password_hash module writes this in if
314                  * (somehow) we didn't have an old NT hash
315                  */
316
317                 E_md4hash("", zero_string_hash.hash);
318                 if (memcmp(nt_history_pwd->hash, zero_string_hash.hash, 16) == 0) {
319                         continue;
320                 }
321
322                 E_deshash("", zero_string_des_hash.hash);
323                 if (!lm_history_pwd || memcmp(lm_history_pwd->hash, zero_string_des_hash.hash, 16) == 0) {
324                         lm_history_pwd = NULL;
325                 }
326
327                 auth_status = authsam_password_ok(auth_context, tmp_ctx,
328                                                   acct_flags,
329                                                   lm_history_pwd,
330                                                   nt_history_pwd,
331                                                   user_info,
332                                                   user_sess_key,
333                                                   lm_sess_key);
334                 if (!NT_STATUS_IS_OK(auth_status)) {
335                         /*
336                          * If this was not a correct password, try the next
337                          * one from the history
338                          */
339                         *user_sess_key = data_blob_null;
340                         *lm_sess_key = data_blob_null;
341                         continue;
342                 }
343
344                 if (i != 1) {
345                         /*
346                          * The authentication was OK, but not against
347                          * the previous password, which is stored at index 1.
348                          *
349                          * We just return the original wrong password.
350                          * This skips the update of the bad pwd count,
351                          * because this is almost certainly user error
352                          * (or automatic login on a computer using a cached
353                          * password from before the password change),
354                          * not an attack.
355                          */
356                         TALLOC_FREE(tmp_ctx);
357                         return NT_STATUS_WRONG_PASSWORD;
358                 }
359
360                 if (user_info->password_state != AUTH_PASSWORD_RESPONSE) {
361                         /*
362                          * The authentication was OK against the previous password,
363                          * but it's not a NTLM network authentication.
364                          *
365                          * We just return the original wrong password.
366                          * This skips the update of the bad pwd count,
367                          * because this is almost certainly user error
368                          * (or automatic login on a computer using a cached
369                          * password from before the password change),
370                          * not an attack.
371                          */
372                         TALLOC_FREE(tmp_ctx);
373                         return NT_STATUS_WRONG_PASSWORD;
374                 }
375
376                 /*
377                  * If the password was OK, it's a NTLM network authentication
378                  * and it was the previous password.
379                  *
380                  * Now we see if it is within the grace period,
381                  * so that we don't break cached sessions on other computers
382                  * before the user can lock and unlock their other screens
383                  * (resetting their cached password).
384                  *
385                  * See http://support.microsoft.com/kb/906305
386                  * OldPasswordAllowedPeriod ("old password allowed period")
387                  * is specified in minutes. The default is 60.
388                  */
389                 allowed_period_mins = lpcfg_old_password_allowed_period(auth_context->lp_ctx);
390                 /*
391                  * NTTIME uses 100ns units
392                  */
393                 allowed_period = allowed_period_mins * 60 * 1000*1000*10;
394                 pwdLastSet = samdb_result_nttime(msg, "pwdLastSet", 0);
395                 tv_now = timeval_current();
396                 now = timeval_to_nttime(&tv_now);
397
398                 if (now < pwdLastSet) {
399                         /*
400                          * time jump?
401                          *
402                          * We just return the original wrong password.
403                          * This skips the update of the bad pwd count,
404                          * because this is almost certainly user error
405                          * (or automatic login on a computer using a cached
406                          * password from before the password change),
407                          * not an attack.
408                          */
409                         TALLOC_FREE(tmp_ctx);
410                         return NT_STATUS_WRONG_PASSWORD;
411                 }
412
413                 if ((now - pwdLastSet) >= allowed_period) {
414                         /*
415                          * The allowed period is over.
416                          *
417                          * We just return the original wrong password.
418                          * This skips the update of the bad pwd count,
419                          * because this is almost certainly user error
420                          * (or automatic login on a computer using a cached
421                          * password from before the password change),
422                          * not an attack.
423                          */
424                         TALLOC_FREE(tmp_ctx);
425                         return NT_STATUS_WRONG_PASSWORD;
426                 }
427
428                 /*
429                  * We finally allow the authentication with the
430                  * previous password within the allowed period.
431                  */
432                 if (user_sess_key->data) {
433                         talloc_steal(mem_ctx, user_sess_key->data);
434                 }
435                 if (lm_sess_key->data) {
436                         talloc_steal(mem_ctx, lm_sess_key->data);
437                 }
438
439                 TALLOC_FREE(tmp_ctx);
440                 return auth_status;
441         }
442
443         /*
444          * If we are not in the allowed period or match an old password,
445          * we didn't return early. Now update the badPwdCount et al.
446          */
447         nt_status = authsam_update_bad_pwd_count(auth_context->sam_ctx,
448                                                  msg, domain_dn);
449         if (!NT_STATUS_IS_OK(nt_status)) {
450                 /*
451                  * We need to return the original
452                  * NT_STATUS_WRONG_PASSWORD error, so there isn't
453                  * anything more we can do than write something into
454                  * the log
455                  */
456                 DEBUG(0, ("Failed to note bad password for user [%s]: %s\n",
457                           user_info->mapped.account_name,
458                           nt_errstr(nt_status)));
459         }
460
461         TALLOC_FREE(tmp_ctx);
462         return NT_STATUS_WRONG_PASSWORD;
463 }
464
465 static NTSTATUS authsam_authenticate(struct auth4_context *auth_context,
466                                      TALLOC_CTX *mem_ctx, struct ldb_context *sam_ctx,
467                                      struct ldb_dn *domain_dn,
468                                      struct ldb_message *msg,
469                                      const struct auth_usersupplied_info *user_info,
470                                      DATA_BLOB *user_sess_key, DATA_BLOB *lm_sess_key,
471                                      bool *authoritative)
472 {
473         NTSTATUS nt_status;
474         bool interactive = (user_info->password_state == AUTH_PASSWORD_HASH);
475         uint32_t acct_flags = samdb_result_acct_flags(msg, NULL);
476         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
477         if (!tmp_ctx) {
478                 return NT_STATUS_NO_MEMORY;
479         }
480
481         /* You can only do an interactive login to normal accounts */
482         if (user_info->flags & USER_INFO_INTERACTIVE_LOGON) {
483                 if (!(acct_flags & ACB_NORMAL)) {
484                         TALLOC_FREE(tmp_ctx);
485                         return NT_STATUS_NO_SUCH_USER;
486                 }
487                 if (acct_flags & ACB_SMARTCARD_REQUIRED) {
488                         if (acct_flags & ACB_DISABLED) {
489                                 DEBUG(2,("authsam_authenticate: Account for user '%s' "
490                                          "was disabled.\n",
491                                          user_info->mapped.account_name));
492                                 TALLOC_FREE(tmp_ctx);
493                                 return NT_STATUS_ACCOUNT_DISABLED;
494                         }
495                         DEBUG(2,("authsam_authenticate: Account for user '%s' "
496                                  "requires interactive smartcard logon.\n",
497                                  user_info->mapped.account_name));
498                         TALLOC_FREE(tmp_ctx);
499                         return NT_STATUS_SMARTCARD_LOGON_REQUIRED;
500                 }
501         }
502
503         nt_status = authsam_password_check_and_record(auth_context, tmp_ctx,
504                                                       domain_dn, msg, acct_flags,
505                                                       user_info,
506                                                       user_sess_key, lm_sess_key,
507                                                       authoritative);
508         if (!NT_STATUS_IS_OK(nt_status)) {
509                 TALLOC_FREE(tmp_ctx);
510                 return nt_status;
511         }
512
513         nt_status = authsam_account_ok(tmp_ctx, auth_context->sam_ctx,
514                                        user_info->logon_parameters,
515                                        domain_dn,
516                                        msg,
517                                        user_info->workstation_name,
518                                        user_info->mapped.account_name,
519                                        false, false);
520         if (!NT_STATUS_IS_OK(nt_status)) {
521                 TALLOC_FREE(tmp_ctx);
522                 return nt_status;
523         }
524
525         nt_status = authsam_logon_success_accounting(auth_context->sam_ctx,
526                                                      msg, domain_dn,
527                                                      interactive);
528         if (!NT_STATUS_IS_OK(nt_status)) {
529                 TALLOC_FREE(tmp_ctx);
530                 return nt_status;
531         }
532
533         if (user_sess_key && user_sess_key->data) {
534                 talloc_steal(mem_ctx, user_sess_key->data);
535         }
536         if (lm_sess_key && lm_sess_key->data) {
537                 talloc_steal(mem_ctx, lm_sess_key->data);
538         }
539
540         TALLOC_FREE(tmp_ctx);
541         return nt_status;
542 }
543
544
545
546 static NTSTATUS authsam_check_password_internals(struct auth_method_context *ctx,
547                                                  TALLOC_CTX *mem_ctx,
548                                                  const struct auth_usersupplied_info *user_info, 
549                                                  struct auth_user_info_dc **user_info_dc,
550                                                  bool *authoritative)
551 {
552         NTSTATUS nt_status;
553         const char *account_name = user_info->mapped.account_name;
554         struct ldb_message *msg;
555         struct ldb_dn *domain_dn;
556         DATA_BLOB user_sess_key, lm_sess_key;
557         TALLOC_CTX *tmp_ctx;
558         const char *p = NULL;
559
560         if (ctx->auth_ctx->sam_ctx == NULL) {
561                 DEBUG(0, ("No SAM available, cannot log in users\n"));
562                 return NT_STATUS_INVALID_SYSTEM_SERVICE;
563         }
564
565         if (!account_name || !*account_name) {
566                 /* 'not for me' */
567                 return NT_STATUS_NOT_IMPLEMENTED;
568         }
569
570         tmp_ctx = talloc_new(mem_ctx);
571         if (!tmp_ctx) {
572                 return NT_STATUS_NO_MEMORY;
573         }
574
575         domain_dn = ldb_get_default_basedn(ctx->auth_ctx->sam_ctx);
576         if (domain_dn == NULL) {
577                 talloc_free(tmp_ctx);
578                 return NT_STATUS_NO_SUCH_DOMAIN;
579         }
580
581         p = strchr_m(account_name, '@');
582         if (p != NULL) {
583                 const char *nt4_domain = NULL;
584                 const char *nt4_account = NULL;
585                 bool is_my_domain = false;
586
587                 nt_status = crack_name_to_nt4_name(mem_ctx,
588                                                    ctx->auth_ctx->event_ctx,
589                                                    ctx->auth_ctx->lp_ctx,
590                                                    /*
591                                                     * DRSUAPI_DS_NAME_FORMAT_UPN_FOR_LOGON ?
592                                                     */
593                                                    DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL,
594                                                    account_name,
595                                                    &nt4_domain, &nt4_account);
596                 if (!NT_STATUS_IS_OK(nt_status)) {
597                         talloc_free(tmp_ctx);
598                         return NT_STATUS_NO_SUCH_USER;
599                 }
600
601                 is_my_domain = lpcfg_is_mydomain(ctx->auth_ctx->lp_ctx, nt4_domain);
602                 if (!is_my_domain) {
603                         /*
604                          * This is a user within our forest,
605                          * but in a different domain,
606                          * we're not authoritative
607                          */
608                         talloc_free(tmp_ctx);
609                         return NT_STATUS_NOT_IMPLEMENTED;
610                 }
611
612                 /*
613                  * Let's use the NT4 account name for the lookup.
614                  */
615                 account_name = nt4_account;
616         }
617
618         nt_status = authsam_search_account(tmp_ctx, ctx->auth_ctx->sam_ctx, account_name, domain_dn, &msg);
619         if (!NT_STATUS_IS_OK(nt_status)) {
620                 talloc_free(tmp_ctx);
621                 return nt_status;
622         }
623
624         nt_status = authsam_authenticate(ctx->auth_ctx, tmp_ctx, ctx->auth_ctx->sam_ctx, domain_dn, msg, user_info,
625                                          &user_sess_key, &lm_sess_key, authoritative);
626         if (!NT_STATUS_IS_OK(nt_status)) {
627                 talloc_free(tmp_ctx);
628                 return nt_status;
629         }
630
631         nt_status = authsam_make_user_info_dc(tmp_ctx, ctx->auth_ctx->sam_ctx,
632                                              lpcfg_netbios_name(ctx->auth_ctx->lp_ctx),
633                                              lpcfg_sam_name(ctx->auth_ctx->lp_ctx),
634                                              lpcfg_sam_dnsname(ctx->auth_ctx->lp_ctx),
635                                              domain_dn,
636                                              msg,
637                                              user_sess_key, lm_sess_key,
638                                              user_info_dc);
639         if (!NT_STATUS_IS_OK(nt_status)) {
640                 talloc_free(tmp_ctx);
641                 return nt_status;
642         }
643
644         talloc_steal(mem_ctx, *user_info_dc);
645         talloc_free(tmp_ctx);
646
647         return NT_STATUS_OK;
648 }
649
650 static NTSTATUS authsam_ignoredomain_want_check(struct auth_method_context *ctx,
651                                                 TALLOC_CTX *mem_ctx,
652                                                 const struct auth_usersupplied_info *user_info)
653 {
654         if (!user_info->mapped.account_name || !*user_info->mapped.account_name) {
655                 return NT_STATUS_NOT_IMPLEMENTED;
656         }
657
658         return NT_STATUS_OK;
659 }
660
661 /****************************************************************************
662 Check SAM security (above) but with a few extra checks.
663 ****************************************************************************/
664 static NTSTATUS authsam_want_check(struct auth_method_context *ctx,
665                                    TALLOC_CTX *mem_ctx,
666                                    const struct auth_usersupplied_info *user_info)
667 {
668         const char *effective_domain = user_info->mapped.domain_name;
669         bool is_local_name = false;
670         bool is_my_domain = false;
671         const char *p = NULL;
672         struct dsdb_trust_routing_table *trt = NULL;
673         const struct lsa_TrustDomainInfoInfoEx *tdo = NULL;
674         NTSTATUS status;
675
676         if (!user_info->mapped.account_name || !*user_info->mapped.account_name) {
677                 return NT_STATUS_NOT_IMPLEMENTED;
678         }
679
680         is_local_name = lpcfg_is_myname(ctx->auth_ctx->lp_ctx,
681                                         effective_domain);
682
683         /* check whether or not we service this domain/workgroup name */
684         switch (lpcfg_server_role(ctx->auth_ctx->lp_ctx)) {
685         case ROLE_STANDALONE:
686                 return NT_STATUS_OK;
687
688         case ROLE_DOMAIN_MEMBER:
689                 if (is_local_name) {
690                         return NT_STATUS_OK;
691                 }
692
693                 DBG_DEBUG("%s is not one of my local names (DOMAIN_MEMBER)\n",
694                           effective_domain);
695                 return NT_STATUS_NOT_IMPLEMENTED;
696
697         case ROLE_ACTIVE_DIRECTORY_DC:
698                 /* handled later */
699                 break;
700
701         default:
702                 DBG_ERR("lpcfg_server_role() has an undefined value\n");
703                 return NT_STATUS_INVALID_SERVER_STATE;
704         }
705
706         /*
707          * Now we handle the AD DC case...
708          */
709
710         is_my_domain = lpcfg_is_my_domain_or_realm(ctx->auth_ctx->lp_ctx,
711                                                    effective_domain);
712         if (is_my_domain) {
713                 return NT_STATUS_OK;
714         }
715
716         if (user_info->mapped_state) {
717                 /*
718                  * The caller already did a cracknames call.
719                  */
720                 DBG_DEBUG("%s is not one domain name (DC)\n",
721                           effective_domain);
722                 return NT_STATUS_NOT_IMPLEMENTED;
723         }
724
725         if (effective_domain != NULL && !strequal(effective_domain, "")) {
726                 DBG_DEBUG("%s is not one domain name (DC)\n",
727                           effective_domain);
728                 return NT_STATUS_NOT_IMPLEMENTED;
729         }
730
731         p = strchr_m(user_info->mapped.account_name, '@');
732         if (p == NULL) {
733                 if (effective_domain == NULL) {
734                         return NT_STATUS_OK;
735                 }
736                 DEBUG(6,("authsam_check_password: '' without upn not handled (DC)\n"));
737                 return NT_STATUS_NOT_IMPLEMENTED;
738         }
739
740         effective_domain = p + 1;
741         is_my_domain = lpcfg_is_my_domain_or_realm(ctx->auth_ctx->lp_ctx,
742                                                    effective_domain);
743         if (is_my_domain) {
744                 return NT_STATUS_OK;
745         }
746
747         if (strequal(effective_domain, "")) {
748                 DBG_DEBUG("authsam_check_password: upn without realm (DC)\n");
749                 return NT_STATUS_NOT_IMPLEMENTED;
750         }
751
752         /*
753          * as last option we check the routing table if the
754          * domain is within our forest.
755          */
756         status = dsdb_trust_routing_table_load(ctx->auth_ctx->sam_ctx,
757                                                mem_ctx, &trt);
758         if (!NT_STATUS_IS_OK(status)) {
759                 DBG_ERR("authsam_check_password: dsdb_trust_routing_table_load() %s\n",
760                          nt_errstr(status));
761                 return status;
762         }
763
764         tdo = dsdb_trust_routing_by_name(trt, effective_domain);
765         if (tdo == NULL) {
766                 DBG_DEBUG("%s is not a known TLN (DC)\n",
767                           effective_domain);
768                 TALLOC_FREE(trt);
769                 return NT_STATUS_NOT_IMPLEMENTED;
770         }
771
772         if (!(tdo->trust_attributes & LSA_TRUST_ATTRIBUTE_WITHIN_FOREST)) {
773                 DBG_DEBUG("%s is not a TLN in our forest (DC)\n",
774                           effective_domain);
775                 TALLOC_FREE(trt);
776                 return NT_STATUS_NOT_IMPLEMENTED;
777         }
778
779         /*
780          * This principal is within our forest.
781          * we'll later do a crack_name_to_nt4_name()
782          * to check if it's in our domain.
783          */
784         TALLOC_FREE(trt);
785         return NT_STATUS_OK;
786 }
787
788 static NTSTATUS authsam_failtrusts_want_check(struct auth_method_context *ctx,
789                                               TALLOC_CTX *mem_ctx,
790                                               const struct auth_usersupplied_info *user_info)
791 {
792         const char *effective_domain = user_info->mapped.domain_name;
793         struct dsdb_trust_routing_table *trt = NULL;
794         const struct lsa_TrustDomainInfoInfoEx *tdo = NULL;
795         NTSTATUS status;
796
797         /* check whether or not we service this domain/workgroup name */
798         switch (lpcfg_server_role(ctx->auth_ctx->lp_ctx)) {
799         case ROLE_ACTIVE_DIRECTORY_DC:
800                 /* handled later */
801                 break;
802
803         default:
804                 DBG_ERR("lpcfg_server_role() has an undefined value\n");
805                 return NT_STATUS_NOT_IMPLEMENTED;
806         }
807
808         /*
809          * Now we handle the AD DC case...
810          */
811         if (!user_info->mapped.account_name || !*user_info->mapped.account_name) {
812                 return NT_STATUS_NOT_IMPLEMENTED;
813         }
814
815         if (effective_domain == NULL || strequal(effective_domain, "")) {
816                 const char *p = NULL;
817
818                 p = strchr_m(user_info->mapped.account_name, '@');
819                 if (p != NULL) {
820                         effective_domain = p + 1;
821                 }
822         }
823
824         if (effective_domain == NULL || strequal(effective_domain, "")) {
825                 DBG_DEBUG("%s is not a trusted domain\n",
826                           effective_domain);
827                 return NT_STATUS_NOT_IMPLEMENTED;
828         }
829
830         /*
831          * as last option we check the routing table if the
832          * domain is within our forest.
833          */
834         status = dsdb_trust_routing_table_load(ctx->auth_ctx->sam_ctx,
835                                                mem_ctx, &trt);
836         if (!NT_STATUS_IS_OK(status)) {
837                 DBG_ERR("authsam_check_password: dsdb_trust_routing_table_load() %s\n",
838                          nt_errstr(status));
839                 return status;
840         }
841
842         tdo = dsdb_trust_routing_by_name(trt, effective_domain);
843         if (tdo == NULL) {
844                 DBG_DEBUG("%s is not a known TLN (DC)\n",
845                           effective_domain);
846                 TALLOC_FREE(trt);
847                 return NT_STATUS_NOT_IMPLEMENTED;
848         }
849
850         /*
851          * We now about the domain...
852          */
853         TALLOC_FREE(trt);
854         return NT_STATUS_OK;
855 }
856
857 static NTSTATUS authsam_failtrusts_check_password(struct auth_method_context *ctx,
858                                                   TALLOC_CTX *mem_ctx,
859                                                   const struct auth_usersupplied_info *user_info,
860                                                   struct auth_user_info_dc **user_info_dc,
861                                                   bool *authoritative)
862 {
863         /*
864          * This should a good error for now,
865          * until this module gets removed
866          * and we have a full async path
867          * to winbind.
868          */
869         return NT_STATUS_NO_TRUST_LSA_SECRET;
870 }
871
872 /* Wrapper for the auth subsystem pointer */
873 static NTSTATUS authsam_get_user_info_dc_principal_wrapper(TALLOC_CTX *mem_ctx,
874                                                           struct auth4_context *auth_context,
875                                                           const char *principal,
876                                                           struct ldb_dn *user_dn,
877                                                           struct auth_user_info_dc **user_info_dc)
878 {
879         return authsam_get_user_info_dc_principal(mem_ctx, auth_context->lp_ctx, auth_context->sam_ctx,
880                                                  principal, user_dn, user_info_dc);
881 }
882 static const struct auth_operations sam_ignoredomain_ops = {
883         .name                      = "sam_ignoredomain",
884         .want_check                = authsam_ignoredomain_want_check,
885         .check_password            = authsam_check_password_internals,
886         .get_user_info_dc_principal = authsam_get_user_info_dc_principal_wrapper,
887 };
888
889 static const struct auth_operations sam_ops = {
890         .name                      = "sam",
891         .want_check                = authsam_want_check,
892         .check_password            = authsam_check_password_internals,
893         .get_user_info_dc_principal = authsam_get_user_info_dc_principal_wrapper,
894 };
895
896 static const struct auth_operations sam_failtrusts_ops = {
897         .name                      = "sam_failtrusts",
898         .want_check                = authsam_failtrusts_want_check,
899         .check_password            = authsam_failtrusts_check_password,
900 };
901
902 _PUBLIC_ NTSTATUS auth4_sam_init(TALLOC_CTX *);
903 _PUBLIC_ NTSTATUS auth4_sam_init(TALLOC_CTX *ctx)
904 {
905         NTSTATUS ret;
906
907         ret = auth_register(ctx, &sam_ops);
908         if (!NT_STATUS_IS_OK(ret)) {
909                 DEBUG(0,("Failed to register 'sam' auth backend!\n"));
910                 return ret;
911         }
912
913         ret = auth_register(ctx, &sam_ignoredomain_ops);
914         if (!NT_STATUS_IS_OK(ret)) {
915                 DEBUG(0,("Failed to register 'sam_ignoredomain' auth backend!\n"));
916                 return ret;
917         }
918
919         ret = auth_register(ctx, &sam_failtrusts_ops);
920         if (!NT_STATUS_IS_OK(ret)) {
921                 DEBUG(0,("Failed to register 'sam_failtrusts' auth backend!\n"));
922                 return ret;
923         }
924
925         return ret;
926 }