Avoid including libds/common/roles.h in public loadparm.h header.
[obnox/samba/samba-obnox.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  Look for the specified user in the sam, return ldb result structures
46 ****************************************************************************/
47
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)
52 {
53         int ret;
54
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;          
65         }
66         if (ret != LDB_SUCCESS) {
67                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
68         }
69         
70         return NT_STATUS_OK;
71 }
72
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)
85 {
86         NTSTATUS status;
87
88         switch (user_info->password_state) {
89         case AUTH_PASSWORD_PLAIN: 
90         {
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;
98                 }
99                 user_info = user_info_temp;
100
101                 /*fall through*/
102         }
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;
114                 
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;
130         }
131
132         return NT_STATUS_OK;
133 }
134
135
136 /*
137   send a message to the drepl server telling it to initiate a
138   REPL_SECRET getncchanges extended op to fetch the users secrets
139  */
140 static void auth_sam_trigger_repl_secret(struct auth4_context *auth_context,
141                                          struct ldb_dn *user_dn)
142 {
143         struct dcerpc_binding_handle *irpc_handle;
144         struct drepl_trigger_repl_secret r;
145         struct tevent_req *req;
146         TALLOC_CTX *tmp_ctx;
147
148         tmp_ctx = talloc_new(auth_context);
149         if (tmp_ctx == NULL) {
150                 return;
151         }
152
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;
160         }
161
162         r.in.user_dn = ldb_dn_get_linearized(user_dn);
163
164         /*
165          * This seem to rely on the current IRPC implementation,
166          * which delivers the message in the _send function.
167          *
168          * TODO: we need a ONE_WAY IRPC handle and register
169          * a callback and wait for it to be triggered!
170          */
171         req = dcerpc_drepl_trigger_repl_secret_r_send(tmp_ctx,
172                                                       auth_context->event_ctx,
173                                                       irpc_handle,
174                                                       &r);
175
176         /* we aren't interested in a reply */
177         talloc_free(req);
178         TALLOC_FREE(tmp_ctx);
179 }
180
181
182 /*
183  * Check that a password is OK, and update badPwdCount if required.
184  */
185
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)
194 {
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;
205
206         tmp_ctx = talloc_new(mem_ctx);
207         if (tmp_ctx == NULL) {
208                 return NT_STATUS_NO_MEMORY;
209         }
210
211         /*
212          * This call does more than what it appears to do, it also
213          * checks for the account lockout.
214          *
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.
218          */
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;
224         }
225
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) {
229                         /*
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.
240                          */
241                         auth_sam_trigger_repl_secret(auth_context, msg->dn);
242                         TALLOC_FREE(tmp_ctx);
243                         return NT_STATUS_NOT_IMPLEMENTED;
244                 }
245         }
246
247         auth_status = authsam_password_ok(auth_context, tmp_ctx,
248                                           acct_flags,
249                                           lm_pwd, nt_pwd,
250                                           user_info,
251                                           user_sess_key, lm_sess_key);
252         if (NT_STATUS_IS_OK(auth_status)) {
253                 if (user_sess_key->data) {
254                         talloc_steal(mem_ctx, user_sess_key->data);
255                 }
256                 if (lm_sess_key->data) {
257                         talloc_steal(mem_ctx, lm_sess_key->data);
258                 }
259                 TALLOC_FREE(tmp_ctx);
260                 return NT_STATUS_OK;
261         }
262         *user_sess_key = data_blob_null;
263         *lm_sess_key = data_blob_null;
264
265         if (!NT_STATUS_EQUAL(auth_status, NT_STATUS_WRONG_PASSWORD)) {
266                 TALLOC_FREE(tmp_ctx);
267                 return auth_status;
268         }
269
270         /*
271          * We only continue if this was a wrong password
272          * and we'll always return NT_STATUS_WRONG_PASSWORD
273          * no matter what error happens.
274          */
275
276         /* pull the domain password property attributes */
277         ret = dsdb_search_one(sam_ctx, tmp_ctx, &dom_msg, domain_dn, LDB_SCOPE_BASE,
278                               attrs, 0, "objectClass=domain");
279         if (ret == LDB_SUCCESS) {
280                 history_len = ldb_msg_find_attr_as_uint(dom_msg, "pwdHistoryLength", 0);
281         } else if (ret == LDB_ERR_NO_SUCH_OBJECT) {
282                 DEBUG(3,("Couldn't find domain %s: %s!\n",
283                          ldb_dn_get_linearized(domain_dn),
284                          ldb_errstring(sam_ctx)));
285         } else {
286                 DEBUG(3,("error finding domain %s: %s!\n",
287                          ldb_dn_get_linearized(domain_dn),
288                          ldb_errstring(sam_ctx)));
289         }
290
291         for (i = 1; i < MIN(history_len, 3); i++) {
292                 static const struct samr_Password zero_hash;
293                 struct samr_Password zero_string_hash;
294                 struct samr_Password zero_string_des_hash;
295                 struct samr_Password *nt_history_pwd = NULL;
296                 struct samr_Password *lm_history_pwd = NULL;
297                 NTTIME pwdLastSet;
298                 struct timeval tv_now;
299                 NTTIME now;
300                 int allowed_period_mins;
301                 NTTIME allowed_period;
302
303                 nt_status = samdb_result_passwords_from_history(tmp_ctx,
304                                                         auth_context->lp_ctx,
305                                                         msg, i,
306                                                         &lm_history_pwd,
307                                                         &nt_history_pwd);
308                 if (!NT_STATUS_IS_OK(nt_status)) {
309                         /*
310                          * If we don't find element 'i' we won't find
311                          * 'i+1' ...
312                          */
313                         break;
314                 }
315
316                 /*
317                  * We choose to avoid any issues
318                  * around different LM and NT history
319                  * lengths by only checking the NT
320                  * history
321                  */
322                 if (nt_history_pwd == NULL) {
323                         /*
324                          * If we don't find element 'i' we won't find
325                          * 'i+1' ...
326                          */
327                         break;
328                 }
329
330                 /* Skip over all-zero hashes in the history */
331                 if (memcmp(nt_history_pwd->hash, zero_hash.hash, 
332                            sizeof(zero_hash.hash)) == 0) {
333                         continue;
334                 }
335
336                 /*
337                  * This looks odd, but the password_hash module writes this in if
338                  * (somehow) we didn't have an old NT hash
339                  */
340
341                 E_md4hash("", zero_string_hash.hash);
342                 if (memcmp(nt_history_pwd->hash, zero_string_hash.hash, 16) == 0) {
343                         continue;
344                 }
345
346                 E_deshash("", zero_string_des_hash.hash);
347                 if (!lm_history_pwd || memcmp(lm_history_pwd->hash, zero_string_des_hash.hash, 16) == 0) {
348                         lm_history_pwd = NULL;
349                 }
350
351                 auth_status = authsam_password_ok(auth_context, tmp_ctx,
352                                                   acct_flags,
353                                                   lm_history_pwd,
354                                                   nt_history_pwd,
355                                                   user_info,
356                                                   user_sess_key,
357                                                   lm_sess_key);
358                 if (!NT_STATUS_IS_OK(auth_status)) {
359                         /*
360                          * If this was not a correct password, try the next
361                          * one from the history
362                          */
363                         *user_sess_key = data_blob_null;
364                         *lm_sess_key = data_blob_null;
365                         continue;
366                 }
367
368                 if (i != 1) {
369                         /*
370                          * The authentication was OK, but not against
371                          * the previous password, which is stored at index 1.
372                          *
373                          * We just return the original wrong password.
374                          * This skips the update of the bad pwd count,
375                          * because this is almost certainly user error
376                          * (or automatic login on a computer using a cached
377                          * password from before the password change),
378                          * not an attack.
379                          */
380                         TALLOC_FREE(tmp_ctx);
381                         return NT_STATUS_WRONG_PASSWORD;
382                 }
383
384                 if (user_info->password_state != AUTH_PASSWORD_RESPONSE) {
385                         /*
386                          * The authentication was OK against the previous password,
387                          * but it's not a NTLM network authentication.
388                          *
389                          * We just return the original wrong password.
390                          * This skips the update of the bad pwd count,
391                          * because this is almost certainly user error
392                          * (or automatic login on a computer using a cached
393                          * password from before the password change),
394                          * not an attack.
395                          */
396                         TALLOC_FREE(tmp_ctx);
397                         return NT_STATUS_WRONG_PASSWORD;
398                 }
399
400                 /*
401                  * If the password was OK, it's a NTLM network authentication
402                  * and it was the previous password.
403                  *
404                  * Now we see if it is within the grace period,
405                  * so that we don't break cached sessions on other computers
406                  * before the user can lock and unlock their other screens
407                  * (resetting their cached password).
408                  *
409                  * See http://support.microsoft.com/kb/906305
410                  * OldPasswordAllowedPeriod ("old password allowed period")
411                  * is specified in minutes. The default is 60.
412                  */
413                 allowed_period_mins = lpcfg_old_password_allowed_period(auth_context->lp_ctx);
414                 /*
415                  * NTTIME uses 100ns units
416                  */
417                 allowed_period = allowed_period_mins * 60 * 1000*1000*10;
418                 pwdLastSet = samdb_result_nttime(msg, "pwdLastSet", 0);
419                 tv_now = timeval_current();
420                 now = timeval_to_nttime(&tv_now);
421
422                 if (now < pwdLastSet) {
423                         /*
424                          * time jump?
425                          *
426                          * We just return the original wrong password.
427                          * This skips the update of the bad pwd count,
428                          * because this is almost certainly user error
429                          * (or automatic login on a computer using a cached
430                          * password from before the password change),
431                          * not an attack.
432                          */
433                         TALLOC_FREE(tmp_ctx);
434                         return NT_STATUS_WRONG_PASSWORD;
435                 }
436
437                 if ((now - pwdLastSet) >= allowed_period) {
438                         /*
439                          * The allowed period is over.
440                          *
441                          * We just return the original wrong password.
442                          * This skips the update of the bad pwd count,
443                          * because this is almost certainly user error
444                          * (or automatic login on a computer using a cached
445                          * password from before the password change),
446                          * not an attack.
447                          */
448                         TALLOC_FREE(tmp_ctx);
449                         return NT_STATUS_WRONG_PASSWORD;
450                 }
451
452                 /*
453                  * We finally allow the authentication with the
454                  * previous password within the allowed period.
455                  */
456                 if (user_sess_key->data) {
457                         talloc_steal(mem_ctx, user_sess_key->data);
458                 }
459                 if (lm_sess_key->data) {
460                         talloc_steal(mem_ctx, lm_sess_key->data);
461                 }
462
463                 TALLOC_FREE(tmp_ctx);
464                 return auth_status;
465         }
466
467         /*
468          * If we are not in the allowed period or match an old password,
469          * we didn't return early. Now update the badPwdCount et al.
470          */
471         nt_status = authsam_update_bad_pwd_count(auth_context->sam_ctx,
472                                                  msg, domain_dn);
473         if (!NT_STATUS_IS_OK(nt_status)) {
474                 /*
475                  * We need to return the original
476                  * NT_STATUS_WRONG_PASSWORD error, so there isn't
477                  * anything more we can do than write something into
478                  * the log
479                  */
480                 DEBUG(0, ("Failed to note bad password for user [%s]: %s\n",
481                           user_info->mapped.account_name,
482                           nt_errstr(nt_status)));
483         }
484
485         TALLOC_FREE(tmp_ctx);
486         return NT_STATUS_WRONG_PASSWORD;
487 }
488
489 static NTSTATUS authsam_authenticate(struct auth4_context *auth_context,
490                                      TALLOC_CTX *mem_ctx, struct ldb_context *sam_ctx,
491                                      struct ldb_dn *domain_dn,
492                                      struct ldb_message *msg,
493                                      const struct auth_usersupplied_info *user_info,
494                                      DATA_BLOB *user_sess_key, DATA_BLOB *lm_sess_key)
495 {
496         NTSTATUS nt_status;
497         bool interactive = (user_info->password_state == AUTH_PASSWORD_HASH);
498         uint16_t acct_flags = samdb_result_acct_flags(msg, NULL);
499         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
500         if (!tmp_ctx) {
501                 return NT_STATUS_NO_MEMORY;
502         }
503
504         /* You can only do an interactive login to normal accounts */
505         if (user_info->flags & USER_INFO_INTERACTIVE_LOGON) {
506                 if (!(acct_flags & ACB_NORMAL)) {
507                         TALLOC_FREE(tmp_ctx);
508                         return NT_STATUS_NO_SUCH_USER;
509                 }
510         }
511
512         nt_status = authsam_password_check_and_record(auth_context, tmp_ctx,
513                                                       domain_dn, msg, acct_flags,
514                                                       user_info,
515                                                       user_sess_key, lm_sess_key);
516         if (!NT_STATUS_IS_OK(nt_status)) {
517                 TALLOC_FREE(tmp_ctx);
518                 return nt_status;
519         }
520
521         nt_status = authsam_account_ok(tmp_ctx, auth_context->sam_ctx,
522                                        user_info->logon_parameters,
523                                        domain_dn,
524                                        msg,
525                                        user_info->workstation_name,
526                                        user_info->mapped.account_name,
527                                        false, false);
528         if (!NT_STATUS_IS_OK(nt_status)) {
529                 TALLOC_FREE(tmp_ctx);
530                 return nt_status;
531         }
532
533         nt_status = authsam_logon_success_accounting(auth_context->sam_ctx,
534                                                      msg, domain_dn,
535                                                      interactive);
536         if (!NT_STATUS_IS_OK(nt_status)) {
537                 TALLOC_FREE(tmp_ctx);
538                 return nt_status;
539         }
540
541         if (user_sess_key && user_sess_key->data) {
542                 talloc_steal(mem_ctx, user_sess_key->data);
543         }
544         if (lm_sess_key && lm_sess_key->data) {
545                 talloc_steal(mem_ctx, lm_sess_key->data);
546         }
547
548         TALLOC_FREE(tmp_ctx);
549         return nt_status;
550 }
551
552
553
554 static NTSTATUS authsam_check_password_internals(struct auth_method_context *ctx,
555                                                  TALLOC_CTX *mem_ctx,
556                                                  const struct auth_usersupplied_info *user_info, 
557                                                  struct auth_user_info_dc **user_info_dc)
558 {
559         NTSTATUS nt_status;
560         const char *account_name = user_info->mapped.account_name;
561         struct ldb_message *msg;
562         struct ldb_dn *domain_dn;
563         DATA_BLOB user_sess_key, lm_sess_key;
564         TALLOC_CTX *tmp_ctx;
565
566         if (ctx->auth_ctx->sam_ctx == NULL) {
567                 DEBUG(0, ("No SAM available, cannot log in users\n"));
568                 return NT_STATUS_INVALID_SYSTEM_SERVICE;
569         }
570
571         if (!account_name || !*account_name) {
572                 /* 'not for me' */
573                 return NT_STATUS_NOT_IMPLEMENTED;
574         }
575
576         tmp_ctx = talloc_new(mem_ctx);
577         if (!tmp_ctx) {
578                 return NT_STATUS_NO_MEMORY;
579         }
580
581         domain_dn = ldb_get_default_basedn(ctx->auth_ctx->sam_ctx);
582         if (domain_dn == NULL) {
583                 talloc_free(tmp_ctx);
584                 return NT_STATUS_NO_SUCH_DOMAIN;
585         }
586
587         nt_status = authsam_search_account(tmp_ctx, ctx->auth_ctx->sam_ctx, account_name, domain_dn, &msg);
588         if (!NT_STATUS_IS_OK(nt_status)) {
589                 talloc_free(tmp_ctx);
590                 return nt_status;
591         }
592
593         nt_status = authsam_authenticate(ctx->auth_ctx, tmp_ctx, ctx->auth_ctx->sam_ctx, domain_dn, msg, user_info,
594                                          &user_sess_key, &lm_sess_key);
595         if (!NT_STATUS_IS_OK(nt_status)) {
596                 talloc_free(tmp_ctx);
597                 return nt_status;
598         }
599
600         nt_status = authsam_make_user_info_dc(tmp_ctx, ctx->auth_ctx->sam_ctx, lpcfg_netbios_name(ctx->auth_ctx->lp_ctx),
601                                              lpcfg_sam_name(ctx->auth_ctx->lp_ctx),
602                                              domain_dn,
603                                              msg,
604                                              user_sess_key, lm_sess_key,
605                                              user_info_dc);
606         if (!NT_STATUS_IS_OK(nt_status)) {
607                 talloc_free(tmp_ctx);
608                 return nt_status;
609         }
610
611         talloc_steal(mem_ctx, *user_info_dc);
612         talloc_free(tmp_ctx);
613
614         return NT_STATUS_OK;
615 }
616
617 static NTSTATUS authsam_ignoredomain_want_check(struct auth_method_context *ctx,
618                                                 TALLOC_CTX *mem_ctx,
619                                                 const struct auth_usersupplied_info *user_info)
620 {
621         if (!user_info->mapped.account_name || !*user_info->mapped.account_name) {
622                 return NT_STATUS_NOT_IMPLEMENTED;
623         }
624
625         return NT_STATUS_OK;
626 }
627
628 /****************************************************************************
629 Check SAM security (above) but with a few extra checks.
630 ****************************************************************************/
631 static NTSTATUS authsam_want_check(struct auth_method_context *ctx,
632                                    TALLOC_CTX *mem_ctx,
633                                    const struct auth_usersupplied_info *user_info)
634 {
635         bool is_local_name, is_my_domain;
636
637         if (!user_info->mapped.account_name || !*user_info->mapped.account_name) {
638                 return NT_STATUS_NOT_IMPLEMENTED;
639         }
640
641         is_local_name = lpcfg_is_myname(ctx->auth_ctx->lp_ctx,
642                                   user_info->mapped.domain_name);
643         is_my_domain  = lpcfg_is_mydomain(ctx->auth_ctx->lp_ctx,
644                                        user_info->mapped.domain_name); 
645
646         /* check whether or not we service this domain/workgroup name */
647         switch (lpcfg_server_role(ctx->auth_ctx->lp_ctx)) {
648                 case ROLE_STANDALONE:
649                         return NT_STATUS_OK;
650
651                 case ROLE_DOMAIN_MEMBER:
652                         if (!is_local_name) {
653                                 DEBUG(6,("authsam_check_password: %s is not one of my local names (DOMAIN_MEMBER)\n",
654                                         user_info->mapped.domain_name));
655                                 return NT_STATUS_NOT_IMPLEMENTED;
656                         }
657                         return NT_STATUS_OK;
658
659                 case ROLE_ACTIVE_DIRECTORY_DC:
660                         if (!is_local_name && !is_my_domain) {
661                                 DEBUG(6,("authsam_check_password: %s is not one of my local names or domain name (DC)\n",
662                                         user_info->mapped.domain_name));
663                                 return NT_STATUS_NOT_IMPLEMENTED;
664                         }
665                         return NT_STATUS_OK;
666         }
667
668         DEBUG(6,("authsam_check_password: lpcfg_server_role() has an undefined value\n"));
669         return NT_STATUS_NOT_IMPLEMENTED;
670 }
671
672                                    
673 /* Wrapper for the auth subsystem pointer */
674 static NTSTATUS authsam_get_user_info_dc_principal_wrapper(TALLOC_CTX *mem_ctx,
675                                                           struct auth4_context *auth_context,
676                                                           const char *principal,
677                                                           struct ldb_dn *user_dn,
678                                                           struct auth_user_info_dc **user_info_dc)
679 {
680         return authsam_get_user_info_dc_principal(mem_ctx, auth_context->lp_ctx, auth_context->sam_ctx,
681                                                  principal, user_dn, user_info_dc);
682 }
683 static const struct auth_operations sam_ignoredomain_ops = {
684         .name                      = "sam_ignoredomain",
685         .want_check                = authsam_ignoredomain_want_check,
686         .check_password            = authsam_check_password_internals,
687         .get_user_info_dc_principal = authsam_get_user_info_dc_principal_wrapper,
688         .flags                     = AUTH_METHOD_LOCAL_SAM
689 };
690
691 static const struct auth_operations sam_ops = {
692         .name                      = "sam",
693         .want_check                = authsam_want_check,
694         .check_password            = authsam_check_password_internals,
695         .get_user_info_dc_principal = authsam_get_user_info_dc_principal_wrapper,
696         .flags                     = AUTH_METHOD_LOCAL_SAM
697 };
698
699 _PUBLIC_ NTSTATUS auth4_sam_init(void);
700 _PUBLIC_ NTSTATUS auth4_sam_init(void)
701 {
702         NTSTATUS ret;
703
704         ret = auth_register(&sam_ops);
705         if (!NT_STATUS_IS_OK(ret)) {
706                 DEBUG(0,("Failed to register 'sam' auth backend!\n"));
707                 return ret;
708         }
709
710         ret = auth_register(&sam_ignoredomain_ops);
711         if (!NT_STATUS_IS_OK(ret)) {
712                 DEBUG(0,("Failed to register 'sam_ignoredomain' auth backend!\n"));
713                 return ret;
714         }
715
716         return ret;
717 }