r4339: - rename auth_guest to auth_anonymous
[Samba/gebeck_regimport.git] / source4 / auth / auth_sam.c
blob236a68fe9dd10440340fc1c825b91e19a948123a
1 /*
2 Unix SMB/CIFS implementation.
3 Password and authentication handling
4 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2001-2004
5 Copyright (C) Gerald Carter 2003
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "includes.h"
23 #include "librpc/gen_ndr/ndr_samr.h"
24 #include "system/time.h"
25 #include "auth/auth.h"
26 #include "lib/ldb/include/ldb.h"
28 #undef DBGC_CLASS
29 #define DBGC_CLASS DBGC_AUTH
31 /****************************************************************************
32 Do a specific test for an smb password being correct, given a smb_password and
33 the lanman and NT responses.
34 ****************************************************************************/
36 static NTSTATUS sam_password_ok(const struct auth_context *auth_context,
37 TALLOC_CTX *mem_ctx,
38 const char *username,
39 uint16_t acct_flags,
40 const struct samr_Password *lm_pwd,
41 const struct samr_Password *nt_pwd,
42 const struct auth_usersupplied_info *user_info,
43 DATA_BLOB *user_sess_key,
44 DATA_BLOB *lm_sess_key)
46 NTSTATUS status;
48 if (acct_flags & ACB_PWNOTREQ) {
49 if (lp_null_passwords()) {
50 DEBUG(3,("Account for user '%s' has no password and null passwords are allowed.\n",
51 username));
52 return NT_STATUS_OK;
53 } else {
54 DEBUG(3,("Account for user '%s' has no password and null passwords are NOT allowed.\n",
55 username));
56 return NT_STATUS_LOGON_FAILURE;
60 status = ntlm_password_check(mem_ctx, &auth_context->challenge,
61 &user_info->lm_resp, &user_info->nt_resp,
62 &user_info->lm_interactive_password,
63 &user_info->nt_interactive_password,
64 username,
65 user_info->smb_name.str,
66 user_info->client_domain.str,
67 lm_pwd->hash, nt_pwd->hash, user_sess_key, lm_sess_key);
69 if (NT_STATUS_IS_OK(status)) {
70 if (user_sess_key && user_sess_key->data) {
71 talloc_steal(auth_context, user_sess_key->data);
73 if (lm_sess_key && lm_sess_key->data) {
74 talloc_steal(auth_context, lm_sess_key->data);
78 return status;
82 /****************************************************************************
83 Do a specific test for a SAM_ACCOUNT being vaild for this connection
84 (ie not disabled, expired and the like).
85 ****************************************************************************/
87 static NTSTATUS sam_account_ok(TALLOC_CTX *mem_ctx,
88 const char *username,
89 uint16_t acct_flags,
90 NTTIME *acct_expiry,
91 NTTIME *must_change_time,
92 NTTIME *last_set_time,
93 const char *workstation_list,
94 const struct auth_usersupplied_info *user_info)
96 DEBUG(4,("sam_account_ok: Checking SMB password for user %s\n", username));
98 /* Quit if the account was disabled. */
99 if (acct_flags & ACB_DISABLED) {
100 DEBUG(1,("sam_account_ok: Account for user '%s' was disabled.\n", username));
101 return NT_STATUS_ACCOUNT_DISABLED;
104 /* Quit if the account was locked out. */
105 if (acct_flags & ACB_AUTOLOCK) {
106 DEBUG(1,("sam_account_ok: Account for user %s was locked out.\n", username));
107 return NT_STATUS_ACCOUNT_LOCKED_OUT;
110 /* Test account expire time */
111 if ((*acct_expiry) != -1 && time(NULL) > nt_time_to_unix(*acct_expiry)) {
112 DEBUG(1,("sam_account_ok: Account for user '%s' has expired.\n", username));
113 DEBUG(3,("sam_account_ok: Account expired at '%s'.\n",
114 nt_time_string(mem_ctx, *acct_expiry)));
115 return NT_STATUS_ACCOUNT_EXPIRED;
118 if (!(acct_flags & ACB_PWNOEXP)) {
120 /* check for immediate expiry "must change at next logon" */
121 if (*must_change_time == 0 && *last_set_time != 0) {
122 DEBUG(1,("sam_account_ok: Account for user '%s' password must change!.\n",
123 username));
124 return NT_STATUS_PASSWORD_MUST_CHANGE;
127 /* check for expired password */
128 if ((*must_change_time) != 0 && nt_time_to_unix(*must_change_time) < time(NULL)) {
129 DEBUG(1,("sam_account_ok: Account for user '%s' password expired!.\n",
130 username));
131 DEBUG(1,("sam_account_ok: Password expired at '%s' unix time.\n",
132 nt_time_string(mem_ctx, *must_change_time)));
133 return NT_STATUS_PASSWORD_EXPIRED;
137 /* Test workstation. Workstation list is comma separated. */
139 if (workstation_list && *workstation_list) {
140 BOOL invalid_ws = True;
141 const char *s = workstation_list;
143 fstring tok;
145 while (next_token(&s, tok, ",", sizeof(tok))) {
146 DEBUG(10,("sam_account_ok: checking for workstation match %s and %s (len=%d)\n",
147 tok, user_info->wksta_name.str, user_info->wksta_name.len));
149 if(strequal(tok, user_info->wksta_name.str)) {
150 invalid_ws = False;
152 break;
156 if (invalid_ws)
157 return NT_STATUS_INVALID_WORKSTATION;
160 if (acct_flags & ACB_DOMTRUST) {
161 DEBUG(2,("sam_account_ok: Domain trust account %s denied by server\n", username));
162 return NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT;
165 if (acct_flags & ACB_SVRTRUST) {
166 DEBUG(2,("sam_account_ok: Server trust account %s denied by server\n", username));
167 return NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT;
170 if (acct_flags & ACB_WSTRUST) {
171 DEBUG(4,("sam_account_ok: Wksta trust account %s denied by server\n", username));
172 return NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT;
175 return NT_STATUS_OK;
178 /****************************************************************************
179 Look for the specified user in the sam, return ldb result structures
180 ****************************************************************************/
182 static NTSTATUS sam_search_user(const char *username, const char *domain,
183 TALLOC_CTX *mem_ctx, void *sam_ctx,
184 struct ldb_message ***ret_msgs,
185 struct ldb_message ***ret_msgs_domain)
187 struct ldb_message **msgs;
188 struct ldb_message **msgs_domain;
190 uint_t ret;
191 uint_t ret_domain;
193 const char *domain_dn = NULL;
194 const char *domain_sid;
196 const char *attrs[] = {"unicodePwd", "lmPwdHash", "ntPwdHash",
197 "userAccountControl",
198 "pwdLastSet",
199 "accountExpires",
200 "objectSid",
201 "userWorkstations",
203 /* required for server_info, not access control: */
204 "sAMAccountName",
205 "displayName",
206 "scriptPath",
207 "profilePath",
208 "homeDirectory",
209 "homeDrive",
210 "lastLogon",
211 "lastLogoff",
212 "accountExpires",
213 "badPwdCount",
214 "logonCount",
215 "primaryGroupID",
216 NULL,
219 const char *domain_attrs[] = {"name", "objectSid"};
221 if (domain) {
222 /* find the domain's DN */
223 ret_domain = samdb_search(sam_ctx, mem_ctx, NULL, &msgs_domain, domain_attrs,
224 "(&(|(realm=%s)(name=%s))(objectclass=domain))",
225 domain, domain);
227 if (ret_domain == 0) {
228 DEBUG(3,("check_sam_security: Couldn't find domain [%s] in passdb file.\n",
229 domain));
230 return NT_STATUS_NO_SUCH_USER;
233 if (ret_domain > 1) {
234 DEBUG(0,("Found %d records matching domain [%s]\n",
235 ret_domain, domain));
236 return NT_STATUS_INTERNAL_DB_CORRUPTION;
239 domain_dn = msgs_domain[0]->dn;
242 /* pull the user attributes */
243 ret = samdb_search(sam_ctx, mem_ctx, domain_dn, &msgs, attrs,
244 "(&(sAMAccountName=%s)(objectclass=user))",
245 username);
247 if (ret == 0) {
248 DEBUG(3,("check_sam_security: Couldn't find user [%s] in passdb file.\n",
249 username));
250 return NT_STATUS_NO_SUCH_USER;
253 if (ret > 1) {
254 DEBUG(0,("Found %d records matching user [%s]\n", ret, username));
255 return NT_STATUS_INTERNAL_DB_CORRUPTION;
258 if (!domain) {
259 domain_sid = samdb_result_sid_prefix(mem_ctx, msgs[0], "objectSid");
260 if (!domain_sid) {
261 return NT_STATUS_INTERNAL_DB_CORRUPTION;
264 /* find the domain's DN */
265 ret_domain = samdb_search(sam_ctx, mem_ctx, NULL, &msgs_domain, domain_attrs,
266 "(&(objectSid=%s)(objectclass=domain))",
267 domain_sid);
269 if (ret_domain == 0) {
270 DEBUG(3,("check_sam_security: Couldn't find domain [%s] in passdb file.\n",
271 domain_sid));
272 return NT_STATUS_NO_SUCH_USER;
275 if (ret_domain > 1) {
276 DEBUG(0,("Found %d records matching domain [%s]\n",
277 ret_domain, domain_sid));
278 return NT_STATUS_INTERNAL_DB_CORRUPTION;
281 domain_dn = msgs_domain[0]->dn;
283 *ret_msgs = msgs;
284 *ret_msgs_domain = msgs_domain;
286 return NT_STATUS_OK;
289 NTSTATUS sam_check_password(const struct auth_context *auth_context,
290 const char *username,
291 TALLOC_CTX *mem_ctx, void *sam_ctx,
292 struct ldb_message **msgs,
293 const char *domain_dn,
294 const struct auth_usersupplied_info *user_info,
295 DATA_BLOB *user_sess_key, DATA_BLOB *lm_sess_key)
298 uint16_t acct_flags;
299 const char *workstation_list;
300 NTTIME acct_expiry;
301 NTTIME must_change_time;
302 NTTIME last_set_time;
303 struct samr_Password *lm_pwd, *nt_pwd;
305 NTSTATUS nt_status;
308 acct_flags = samdb_result_acct_flags(msgs[0], "sAMAcctFlags");
310 /* Quit if the account was locked out. */
311 if (acct_flags & ACB_AUTOLOCK) {
312 DEBUG(3,("check_sam_security: Account for user %s was locked out.\n",
313 username));
314 return NT_STATUS_ACCOUNT_LOCKED_OUT;
317 if (!NT_STATUS_IS_OK(nt_status = samdb_result_passwords(mem_ctx, msgs[0],
318 &lm_pwd, &nt_pwd))) {
319 return nt_status;
322 nt_status = sam_password_ok(auth_context, mem_ctx,
323 username, acct_flags,
324 lm_pwd, nt_pwd,
325 user_info, user_sess_key, lm_sess_key);
327 if (!NT_STATUS_IS_OK(nt_status)) {
328 return nt_status;
331 acct_expiry = samdb_result_nttime(msgs[0], "accountExpires", 0);
332 must_change_time = samdb_result_force_password_change(sam_ctx, mem_ctx,
333 domain_dn, msgs[0],
334 "pwdLastSet");
335 last_set_time = samdb_result_nttime(msgs[0], "pwdLastSet", 0);
337 workstation_list = samdb_result_string(msgs[0], "userWorkstations", NULL);
339 nt_status = sam_account_ok(mem_ctx, username, acct_flags,
340 &acct_expiry,
341 &must_change_time,
342 &last_set_time,
343 workstation_list,
344 user_info);
346 return nt_status;
349 NTSTATUS sam_make_server_info(TALLOC_CTX *mem_ctx, void *sam_ctx,
350 struct ldb_message **msgs, struct ldb_message **msgs_domain,
351 struct auth_serversupplied_info **server_info)
354 struct ldb_message **group_msgs;
355 int group_ret;
356 const char *group_attrs[3] = { "sAMAccountType", "objectSid", NULL };
357 /* find list of sids */
358 struct dom_sid **groupSIDs = NULL;
359 struct dom_sid *user_sid;
360 struct dom_sid *primary_group_sid;
361 const char *sidstr;
362 int i;
363 uint_t rid;
365 NTSTATUS nt_status;
367 if (!NT_STATUS_IS_OK(nt_status = make_server_info(mem_ctx, server_info,
368 samdb_result_string(msgs[0], "sAMAccountName", "")))) {
369 DEBUG(0,("check_sam_security: make_server_info_sam() failed with '%s'\n", nt_errstr(nt_status)));
370 return nt_status;
373 group_ret = samdb_search(sam_ctx,
374 mem_ctx, NULL, &group_msgs, group_attrs,
375 "(&(member=%s)(sAMAccountType=*))",
376 msgs[0]->dn);
377 if (group_ret == -1) {
378 return NT_STATUS_INTERNAL_DB_CORRUPTION;
381 if (group_ret > 0 &&
382 !(groupSIDs = talloc_array_p(*server_info, struct dom_sid *, group_ret))) {
383 talloc_free(*server_info);
384 return NT_STATUS_NO_MEMORY;
387 /* Need to unroll some nested groups, but not aliases */
388 for (i = 0; i < group_ret; i++) {
389 sidstr = ldb_msg_find_string(group_msgs[i], "objectSid", NULL);
390 groupSIDs[i] = dom_sid_parse_talloc(*server_info, sidstr);
393 sidstr = ldb_msg_find_string(msgs[0], "objectSid", NULL);
394 user_sid = dom_sid_parse_talloc(*server_info, sidstr);
395 primary_group_sid = dom_sid_parse_talloc(*server_info, sidstr);
396 rid = samdb_result_uint(msgs[0], "primaryGroupID", ~0);
397 if (rid == ~0) {
398 if (group_ret > 0) {
399 primary_group_sid = groupSIDs[0];
400 } else {
401 primary_group_sid = NULL;
403 } else {
404 primary_group_sid->sub_auths[primary_group_sid->num_auths-1] = rid;
407 (*server_info)->user_sid = user_sid;
408 (*server_info)->primary_group_sid = primary_group_sid;
410 (*server_info)->n_domain_groups = group_ret;
411 (*server_info)->domain_groups = groupSIDs;
413 (*server_info)->account_name
414 = talloc_strdup(*server_info,
415 samdb_result_string(msgs[0], "sAMAccountName", ""));
417 (*server_info)->domain
418 = talloc_strdup(*server_info,
419 samdb_result_string(msgs_domain[0], "name", ""));
421 (*server_info)->full_name
422 = talloc_strdup(*server_info,
423 samdb_result_string(msgs[0], "displayName", ""));
425 (*server_info)->logon_script
426 = talloc_strdup(*server_info,
427 samdb_result_string(msgs[0], "scriptPath", ""));
428 (*server_info)->profile_path
429 = talloc_strdup(*server_info,
430 samdb_result_string(msgs[0], "profilePath", ""));
431 (*server_info)->home_directory
432 = talloc_strdup(*server_info,
433 samdb_result_string(msgs[0], "homeDirectory", ""));
435 (*server_info)->home_drive
436 = talloc_strdup(*server_info,
437 samdb_result_string(msgs[0], "homeDrive", ""));
439 (*server_info)->last_logon = samdb_result_nttime(msgs[0], "lastLogon", 0);
440 (*server_info)->last_logoff = samdb_result_nttime(msgs[0], "lastLogoff", 0);
441 (*server_info)->acct_expiry = samdb_result_nttime(msgs[0], "accountExpires", 0);
442 (*server_info)->last_password_change = samdb_result_nttime(msgs[0], "pwdLastSet", 0);
443 (*server_info)->allow_password_change
444 = samdb_result_allow_password_change(sam_ctx, mem_ctx,
445 msgs_domain[0]->dn, msgs[0], "pwdLastSet");
446 (*server_info)->force_password_change
447 = samdb_result_force_password_change(sam_ctx, mem_ctx,
448 msgs_domain[0]->dn, msgs[0], "pwdLastSet");
450 (*server_info)->logon_count = samdb_result_uint(msgs[0], "logonCount", 0);
451 (*server_info)->bad_password_count = samdb_result_uint(msgs[0], "badPwdCount", 0);
453 (*server_info)->acct_flags = samdb_result_acct_flags(msgs[0], "userAccountControl");
455 (*server_info)->guest = False;
457 if (!(*server_info)->account_name
458 || !(*server_info)->full_name
459 || !(*server_info)->logon_script
460 || !(*server_info)->profile_path
461 || !(*server_info)->home_directory
462 || !(*server_info)->home_drive) {
463 talloc_destroy(*server_info);
464 return NT_STATUS_NO_MEMORY;
467 return nt_status;
470 NTSTATUS sam_get_server_info(const char *username, const char *domain, TALLOC_CTX *mem_ctx,
471 struct auth_serversupplied_info **server_info)
473 NTSTATUS nt_status;
475 struct ldb_message **msgs;
476 struct ldb_message **domain_msgs;
477 void *sam_ctx;
479 sam_ctx = samdb_connect(mem_ctx);
480 if (sam_ctx == NULL) {
481 return NT_STATUS_INVALID_SYSTEM_SERVICE;
484 nt_status = sam_search_user(username, domain, mem_ctx, sam_ctx, &msgs, &domain_msgs);
485 if (!NT_STATUS_IS_OK(nt_status)) {
486 return nt_status;
489 nt_status = sam_make_server_info(mem_ctx, sam_ctx, msgs, domain_msgs, server_info);
490 if (!NT_STATUS_IS_OK(nt_status)) {
491 return nt_status;
494 return NT_STATUS_OK;
497 static NTSTATUS check_sam_security_internals(const struct auth_context *auth_context,
498 const char *domain,
499 TALLOC_CTX *mem_ctx,
500 const struct auth_usersupplied_info *user_info,
501 struct auth_serversupplied_info **server_info)
503 /* mark this as 'not for me' */
504 NTSTATUS nt_status = NT_STATUS_NOT_IMPLEMENTED;
505 const char *username = user_info->internal_username.str;
506 struct ldb_message **msgs;
507 struct ldb_message **domain_msgs;
508 void *sam_ctx;
509 DATA_BLOB user_sess_key, lm_sess_key;
511 if (!username || !*username) {
512 return nt_status;
515 sam_ctx = samdb_connect(mem_ctx);
516 if (sam_ctx == NULL) {
517 return NT_STATUS_INVALID_SYSTEM_SERVICE;
520 nt_status = sam_search_user(username, domain, mem_ctx, sam_ctx, &msgs, &domain_msgs);
521 if (!NT_STATUS_IS_OK(nt_status)) {
522 return nt_status;
525 nt_status = sam_check_password(auth_context, username, mem_ctx, sam_ctx, msgs, domain_msgs[0]->dn, user_info,
526 &user_sess_key, &lm_sess_key);
527 if (!NT_STATUS_IS_OK(nt_status)) {
528 return nt_status;
531 nt_status = sam_make_server_info(mem_ctx, sam_ctx, msgs, domain_msgs, server_info);
532 if (!NT_STATUS_IS_OK(nt_status)) {
533 return nt_status;
536 talloc_reference(auth_context, *server_info);
538 (*server_info)->user_session_key = user_sess_key;
539 (*server_info)->lm_session_key = lm_sess_key;
540 return NT_STATUS_OK;
543 static NTSTATUS check_sam_security(const struct auth_context *auth_context,
544 void *my_private_data,
545 TALLOC_CTX *mem_ctx,
546 const struct auth_usersupplied_info *user_info,
547 struct auth_serversupplied_info **server_info)
549 return check_sam_security_internals(auth_context, NULL,
550 mem_ctx, user_info, server_info);
553 /* module initialisation */
554 static NTSTATUS auth_init_sam_ignoredomain(struct auth_context *auth_context,
555 const char *param,
556 struct auth_methods **auth_method)
558 if (!make_auth_methods(auth_context, auth_method)) {
559 return NT_STATUS_NO_MEMORY;
562 (*auth_method)->auth = check_sam_security;
563 (*auth_method)->name = "sam_ignoredomain";
564 return NT_STATUS_OK;
568 /****************************************************************************
569 Check SAM security (above) but with a few extra checks.
570 ****************************************************************************/
572 static NTSTATUS check_samstrict_security(const struct auth_context *auth_context,
573 void *my_private_data,
574 TALLOC_CTX *mem_ctx,
575 const struct auth_usersupplied_info *user_info,
576 struct auth_serversupplied_info **server_info)
578 const char *domain;
579 BOOL is_local_name, is_my_domain;
581 if (!user_info || !auth_context) {
582 return NT_STATUS_LOGON_FAILURE;
585 is_local_name = is_myname(user_info->domain.str);
586 is_my_domain = strequal(user_info->domain.str, lp_workgroup());
588 /* check whether or not we service this domain/workgroup name */
590 switch ( lp_server_role() ) {
591 case ROLE_STANDALONE:
592 case ROLE_DOMAIN_MEMBER:
593 if ( !is_local_name ) {
594 DEBUG(6,("check_samstrict_security: %s is not one of my local names (%s)\n",
595 user_info->domain.str, (lp_server_role() == ROLE_DOMAIN_MEMBER
596 ? "ROLE_DOMAIN_MEMBER" : "ROLE_STANDALONE") ));
597 return NT_STATUS_NOT_IMPLEMENTED;
599 domain = lp_netbios_name();
600 break;
601 case ROLE_DOMAIN_PDC:
602 case ROLE_DOMAIN_BDC:
603 if ( !is_local_name && !is_my_domain ) {
604 DEBUG(6,("check_samstrict_security: %s is not one of my local names or domain name (DC)\n",
605 user_info->domain.str));
606 return NT_STATUS_NOT_IMPLEMENTED;
608 domain = lp_workgroup();
609 break;
610 default: /* name is ok */
611 domain = user_info->domain.str;
612 break;
615 return check_sam_security_internals(auth_context, domain, mem_ctx, user_info, server_info);
618 /* module initialisation */
619 static NTSTATUS auth_init_sam(struct auth_context *auth_context,
620 const char *param,
621 struct auth_methods **auth_method)
623 if (!make_auth_methods(auth_context, auth_method)) {
624 return NT_STATUS_NO_MEMORY;
627 (*auth_method)->auth = check_samstrict_security;
628 (*auth_method)->name = "sam";
629 return NT_STATUS_OK;
632 NTSTATUS auth_sam_init(void)
634 NTSTATUS ret;
635 struct auth_operations ops;
637 ops.name = "sam";
638 ops.init = auth_init_sam;
639 ret = auth_register(&ops);
640 if (!NT_STATUS_IS_OK(ret)) {
641 DEBUG(0,("Failed to register '%s' auth backend!\n",
642 ops.name));
643 return ret;
646 ops.name = "sam_ignoredomain";
647 ops.init = auth_init_sam_ignoredomain;
648 ret = auth_register(&ops);
649 if (!NT_STATUS_IS_OK(ret)) {
650 DEBUG(0,("Failed to register '%s' auth backend!\n",
651 ops.name));
652 return ret;
655 return ret;