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.
23 #include "librpc/gen_ndr/ndr_samr.h"
24 #include "system/time.h"
27 #define DBGC_CLASS DBGC_AUTH
29 /****************************************************************************
30 Do a specific test for an smb password being correct, given a smb_password and
31 the lanman and NT responses.
32 ****************************************************************************/
34 static NTSTATUS sam_password_ok(const struct auth_context *auth_context,
38 const struct samr_Password *lm_pwd,
39 const struct samr_Password *nt_pwd,
40 const struct auth_usersupplied_info *user_info,
41 DATA_BLOB *user_sess_key,
42 DATA_BLOB *lm_sess_key)
46 if (acct_flags & ACB_PWNOTREQ) {
47 if (lp_null_passwords()) {
48 DEBUG(3,("Account for user '%s' has no password and null passwords are allowed.\n",
52 DEBUG(3,("Account for user '%s' has no password and null passwords are NOT allowed.\n",
54 return NT_STATUS_LOGON_FAILURE;
58 status = ntlm_password_check(mem_ctx, &auth_context->challenge,
59 &user_info->lm_resp, &user_info->nt_resp,
60 &user_info->lm_interactive_password,
61 &user_info->nt_interactive_password,
63 user_info->smb_name.str,
64 user_info->client_domain.str,
65 lm_pwd->hash, nt_pwd->hash, user_sess_key, lm_sess_key);
67 if (NT_STATUS_IS_OK(status)) {
68 if (user_sess_key && user_sess_key->data) {
69 talloc_steal(auth_context, user_sess_key->data);
71 if (lm_sess_key && lm_sess_key->data) {
72 talloc_steal(auth_context, lm_sess_key->data);
80 /****************************************************************************
81 Do a specific test for a SAM_ACCOUNT being vaild for this connection
82 (ie not disabled, expired and the like).
83 ****************************************************************************/
85 static NTSTATUS sam_account_ok(TALLOC_CTX *mem_ctx,
89 NTTIME *must_change_time,
90 NTTIME *last_set_time,
91 const char *workstation_list,
92 const struct auth_usersupplied_info *user_info)
94 DEBUG(4,("sam_account_ok: Checking SMB password for user %s\n", username));
96 /* Quit if the account was disabled. */
97 if (acct_flags & ACB_DISABLED) {
98 DEBUG(1,("sam_account_ok: Account for user '%s' was disabled.\n", username));
99 return NT_STATUS_ACCOUNT_DISABLED;
102 /* Quit if the account was locked out. */
103 if (acct_flags & ACB_AUTOLOCK) {
104 DEBUG(1,("sam_account_ok: Account for user %s was locked out.\n", username));
105 return NT_STATUS_ACCOUNT_LOCKED_OUT;
108 /* Test account expire time */
109 if ((*acct_expiry) != -1 && time(NULL) > nt_time_to_unix(*acct_expiry)) {
110 DEBUG(1,("sam_account_ok: Account for user '%s' has expired.\n", username));
111 DEBUG(3,("sam_account_ok: Account expired at '%s'.\n",
112 nt_time_string(mem_ctx, *acct_expiry)));
113 return NT_STATUS_ACCOUNT_EXPIRED;
116 if (!(acct_flags & ACB_PWNOEXP)) {
118 /* check for immediate expiry "must change at next logon" */
119 if (*must_change_time == 0 && *last_set_time != 0) {
120 DEBUG(1,("sam_account_ok: Account for user '%s' password must change!.\n",
122 return NT_STATUS_PASSWORD_MUST_CHANGE;
125 /* check for expired password */
126 if ((*must_change_time) != 0 && nt_time_to_unix(*must_change_time) < time(NULL)) {
127 DEBUG(1,("sam_account_ok: Account for user '%s' password expired!.\n",
129 DEBUG(1,("sam_account_ok: Password expired at '%s' unix time.\n",
130 nt_time_string(mem_ctx, *must_change_time)));
131 return NT_STATUS_PASSWORD_EXPIRED;
135 /* Test workstation. Workstation list is comma separated. */
137 if (workstation_list && *workstation_list) {
138 BOOL invalid_ws = True;
139 const char *s = workstation_list;
143 while (next_token(&s, tok, ",", sizeof(tok))) {
144 DEBUG(10,("sam_account_ok: checking for workstation match %s and %s (len=%d)\n",
145 tok, user_info->wksta_name.str, user_info->wksta_name.len));
147 if(strequal(tok, user_info->wksta_name.str)) {
155 return NT_STATUS_INVALID_WORKSTATION;
158 if (acct_flags & ACB_DOMTRUST) {
159 DEBUG(2,("sam_account_ok: Domain trust account %s denied by server\n", username));
160 return NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT;
163 if (acct_flags & ACB_SVRTRUST) {
164 DEBUG(2,("sam_account_ok: Server trust account %s denied by server\n", username));
165 return NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT;
168 if (acct_flags & ACB_WSTRUST) {
169 DEBUG(4,("sam_account_ok: Wksta trust account %s denied by server\n", username));
170 return NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT;
176 /****************************************************************************
177 Look for the specified user in the sam, return ldb result structures
178 ****************************************************************************/
180 static NTSTATUS sam_search_user(const char *username, const char *domain,
181 TALLOC_CTX *mem_ctx, void *sam_ctx,
182 struct ldb_message ***ret_msgs,
183 struct ldb_message ***ret_msgs_domain)
185 struct ldb_message **msgs;
186 struct ldb_message **msgs_domain;
191 const char *domain_dn = NULL;
192 const char *domain_sid;
194 const char *attrs[] = {"unicodePwd", "lmPwdHash", "ntPwdHash",
195 "userAccountControl",
201 /* required for server_info, not access control: */
217 const char *domain_attrs[] = {"name", "objectSid"};
220 /* find the domain's DN */
221 ret_domain = samdb_search(sam_ctx, mem_ctx, NULL, &msgs_domain, domain_attrs,
222 "(&(|(realm=%s)(name=%s))(objectclass=domain))",
225 if (ret_domain == 0) {
226 DEBUG(3,("check_sam_security: Couldn't find domain [%s] in passdb file.\n",
228 return NT_STATUS_NO_SUCH_USER;
231 if (ret_domain > 1) {
232 DEBUG(0,("Found %d records matching domain [%s]\n",
233 ret_domain, domain));
234 return NT_STATUS_INTERNAL_DB_CORRUPTION;
237 domain_dn = msgs_domain[0]->dn;
240 /* pull the user attributes */
241 ret = samdb_search(sam_ctx, mem_ctx, domain_dn, &msgs, attrs,
242 "(&(sAMAccountName=%s)(objectclass=user))",
246 DEBUG(3,("check_sam_security: Couldn't find user [%s] in passdb file.\n",
248 return NT_STATUS_NO_SUCH_USER;
252 DEBUG(0,("Found %d records matching user [%s]\n", ret, username));
253 return NT_STATUS_INTERNAL_DB_CORRUPTION;
257 domain_sid = samdb_result_sid_prefix(mem_ctx, msgs[0], "objectSid");
259 return NT_STATUS_INTERNAL_DB_CORRUPTION;
262 /* find the domain's DN */
263 ret_domain = samdb_search(sam_ctx, mem_ctx, NULL, &msgs_domain, domain_attrs,
264 "(&(objectSid=%s)(objectclass=domain))",
267 if (ret_domain == 0) {
268 DEBUG(3,("check_sam_security: Couldn't find domain [%s] in passdb file.\n",
270 return NT_STATUS_NO_SUCH_USER;
273 if (ret_domain > 1) {
274 DEBUG(0,("Found %d records matching domain [%s]\n",
275 ret_domain, domain_sid));
276 return NT_STATUS_INTERNAL_DB_CORRUPTION;
279 domain_dn = msgs_domain[0]->dn;
282 *ret_msgs_domain = msgs_domain;
287 NTSTATUS sam_check_password(const struct auth_context *auth_context,
288 const char *username,
289 TALLOC_CTX *mem_ctx, void *sam_ctx,
290 struct ldb_message **msgs,
291 const char *domain_dn,
292 const struct auth_usersupplied_info *user_info,
293 DATA_BLOB *user_sess_key, DATA_BLOB *lm_sess_key)
297 const char *workstation_list;
299 NTTIME must_change_time;
300 NTTIME last_set_time;
301 struct samr_Password *lm_pwd, *nt_pwd;
306 acct_flags = samdb_result_acct_flags(msgs[0], "sAMAcctFlags");
308 /* Quit if the account was locked out. */
309 if (acct_flags & ACB_AUTOLOCK) {
310 DEBUG(3,("check_sam_security: Account for user %s was locked out.\n",
312 return NT_STATUS_ACCOUNT_LOCKED_OUT;
315 if (!NT_STATUS_IS_OK(nt_status = samdb_result_passwords(mem_ctx, msgs[0],
316 &lm_pwd, &nt_pwd))) {
320 nt_status = sam_password_ok(auth_context, mem_ctx,
321 username, acct_flags,
323 user_info, user_sess_key, lm_sess_key);
325 if (!NT_STATUS_IS_OK(nt_status)) {
329 acct_expiry = samdb_result_nttime(msgs[0], "accountExpires", 0);
330 must_change_time = samdb_result_force_password_change(sam_ctx, mem_ctx,
333 last_set_time = samdb_result_nttime(msgs[0], "pwdLastSet", 0);
335 workstation_list = samdb_result_string(msgs[0], "userWorkstations", NULL);
337 nt_status = sam_account_ok(mem_ctx, username, acct_flags,
347 NTSTATUS sam_make_server_info(TALLOC_CTX *mem_ctx, void *sam_ctx,
348 struct ldb_message **msgs, struct ldb_message **msgs_domain,
349 struct auth_serversupplied_info **server_info)
352 struct ldb_message **group_msgs;
354 const char *group_attrs[3] = { "sAMAccountType", "objectSid", NULL };
355 /* find list of sids */
356 struct dom_sid **groupSIDs = NULL;
357 struct dom_sid *user_sid;
358 struct dom_sid *primary_group_sid;
365 if (!NT_STATUS_IS_OK(nt_status = make_server_info(mem_ctx, server_info,
366 samdb_result_string(msgs[0], "sAMAccountName", "")))) {
367 DEBUG(0,("check_sam_security: make_server_info_sam() failed with '%s'\n", nt_errstr(nt_status)));
371 group_ret = samdb_search(sam_ctx,
372 mem_ctx, NULL, &group_msgs, group_attrs,
373 "(&(member=%s)(sAMAccountType=*))",
375 if (group_ret == -1) {
376 return NT_STATUS_INTERNAL_DB_CORRUPTION;
380 !(groupSIDs = talloc_array_p(*server_info, struct dom_sid *, group_ret))) {
381 talloc_free(*server_info);
382 return NT_STATUS_NO_MEMORY;
385 /* Need to unroll some nested groups, but not aliases */
386 for (i = 0; i < group_ret; i++) {
387 sidstr = ldb_msg_find_string(group_msgs[i], "objectSid", NULL);
388 groupSIDs[i] = dom_sid_parse_talloc(*server_info, sidstr);
391 sidstr = ldb_msg_find_string(msgs[0], "objectSid", NULL);
392 user_sid = dom_sid_parse_talloc(*server_info, sidstr);
393 primary_group_sid = dom_sid_parse_talloc(*server_info, sidstr);
394 rid = samdb_result_uint(msgs[0], "primaryGroupID", ~0);
397 primary_group_sid = groupSIDs[0];
399 primary_group_sid = NULL;
402 primary_group_sid->sub_auths[primary_group_sid->num_auths-1] = rid;
405 (*server_info)->user_sid = user_sid;
406 (*server_info)->primary_group_sid = primary_group_sid;
408 (*server_info)->n_domain_groups = group_ret;
409 (*server_info)->domain_groups = groupSIDs;
411 (*server_info)->account_name
412 = talloc_strdup(*server_info,
413 samdb_result_string(msgs[0], "sAMAccountName", ""));
415 (*server_info)->domain
416 = talloc_strdup(*server_info,
417 samdb_result_string(msgs_domain[0], "name", ""));
419 (*server_info)->full_name
420 = talloc_strdup(*server_info,
421 samdb_result_string(msgs[0], "displayName", ""));
423 (*server_info)->logon_script
424 = talloc_strdup(*server_info,
425 samdb_result_string(msgs[0], "scriptPath", ""));
426 (*server_info)->profile_path
427 = talloc_strdup(*server_info,
428 samdb_result_string(msgs[0], "profilePath", ""));
429 (*server_info)->home_directory
430 = talloc_strdup(*server_info,
431 samdb_result_string(msgs[0], "homeDirectory", ""));
433 (*server_info)->home_drive
434 = talloc_strdup(*server_info,
435 samdb_result_string(msgs[0], "homeDrive", ""));
437 (*server_info)->last_logon = samdb_result_nttime(msgs[0], "lastLogon", 0);
438 (*server_info)->last_logoff = samdb_result_nttime(msgs[0], "lastLogoff", 0);
439 (*server_info)->acct_expiry = samdb_result_nttime(msgs[0], "accountExpires", 0);
440 (*server_info)->last_password_change = samdb_result_nttime(msgs[0], "pwdLastSet", 0);
441 (*server_info)->allow_password_change
442 = samdb_result_allow_password_change(sam_ctx, mem_ctx,
443 msgs_domain[0]->dn, msgs[0], "pwdLastSet");
444 (*server_info)->force_password_change
445 = samdb_result_force_password_change(sam_ctx, mem_ctx,
446 msgs_domain[0]->dn, msgs[0], "pwdLastSet");
448 (*server_info)->logon_count = samdb_result_uint(msgs[0], "logonCount", 0);
449 (*server_info)->bad_password_count = samdb_result_uint(msgs[0], "badPwdCount", 0);
451 (*server_info)->acct_flags = samdb_result_acct_flags(msgs[0], "userAccountControl");
453 (*server_info)->guest = False;
455 if (!(*server_info)->account_name
456 || !(*server_info)->full_name
457 || !(*server_info)->logon_script
458 || !(*server_info)->profile_path
459 || !(*server_info)->home_directory
460 || !(*server_info)->home_drive) {
461 talloc_destroy(*server_info);
462 return NT_STATUS_NO_MEMORY;
468 NTSTATUS sam_get_server_info(const char *username, const char *domain, TALLOC_CTX *mem_ctx,
469 struct auth_serversupplied_info **server_info)
473 struct ldb_message **msgs;
474 struct ldb_message **domain_msgs;
477 sam_ctx = samdb_connect(mem_ctx);
478 if (sam_ctx == NULL) {
479 return NT_STATUS_INVALID_SYSTEM_SERVICE;
482 nt_status = sam_search_user(username, domain, mem_ctx, sam_ctx, &msgs, &domain_msgs);
483 if (!NT_STATUS_IS_OK(nt_status)) {
487 nt_status = sam_make_server_info(mem_ctx, sam_ctx, msgs, domain_msgs, server_info);
488 if (!NT_STATUS_IS_OK(nt_status)) {
495 static NTSTATUS check_sam_security_internals(const struct auth_context *auth_context,
498 const struct auth_usersupplied_info *user_info,
499 struct auth_serversupplied_info **server_info)
503 const char *username = user_info->internal_username.str;
504 struct ldb_message **msgs;
505 struct ldb_message **domain_msgs;
507 DATA_BLOB user_sess_key, lm_sess_key;
509 sam_ctx = samdb_connect(mem_ctx);
510 if (sam_ctx == NULL) {
511 return NT_STATUS_INVALID_SYSTEM_SERVICE;
514 nt_status = sam_search_user(username, domain, mem_ctx, sam_ctx, &msgs, &domain_msgs);
515 if (!NT_STATUS_IS_OK(nt_status)) {
519 nt_status = sam_check_password(auth_context, username, mem_ctx, sam_ctx, msgs, domain_msgs[0]->dn, user_info,
520 &user_sess_key, &lm_sess_key);
521 if (!NT_STATUS_IS_OK(nt_status)) {
525 nt_status = sam_make_server_info(mem_ctx, sam_ctx, msgs, domain_msgs, server_info);
526 if (!NT_STATUS_IS_OK(nt_status)) {
530 talloc_reference(auth_context, *server_info);
532 (*server_info)->user_session_key = user_sess_key;
533 (*server_info)->lm_session_key = lm_sess_key;
537 static NTSTATUS check_sam_security(const struct auth_context *auth_context,
538 void *my_private_data,
540 const struct auth_usersupplied_info *user_info,
541 struct auth_serversupplied_info **server_info)
543 return check_sam_security_internals(auth_context, NULL,
544 mem_ctx, user_info, server_info);
547 /* module initialisation */
548 static NTSTATUS auth_init_sam_ignoredomain(struct auth_context *auth_context,
550 struct auth_methods **auth_method)
552 if (!make_auth_methods(auth_context, auth_method)) {
553 return NT_STATUS_NO_MEMORY;
556 (*auth_method)->auth = check_sam_security;
557 (*auth_method)->name = "sam_ignoredomain";
562 /****************************************************************************
563 Check SAM security (above) but with a few extra checks.
564 ****************************************************************************/
566 static NTSTATUS check_samstrict_security(const struct auth_context *auth_context,
567 void *my_private_data,
569 const struct auth_usersupplied_info *user_info,
570 struct auth_serversupplied_info **server_info)
573 BOOL is_local_name, is_my_domain;
575 if (!user_info || !auth_context) {
576 return NT_STATUS_LOGON_FAILURE;
579 is_local_name = is_myname(user_info->domain.str);
580 is_my_domain = strequal(user_info->domain.str, lp_workgroup());
582 /* check whether or not we service this domain/workgroup name */
584 switch ( lp_server_role() ) {
585 case ROLE_STANDALONE:
586 case ROLE_DOMAIN_MEMBER:
587 if ( !is_local_name ) {
588 DEBUG(6,("check_samstrict_security: %s is not one of my local names (%s)\n",
589 user_info->domain.str, (lp_server_role() == ROLE_DOMAIN_MEMBER
590 ? "ROLE_DOMAIN_MEMBER" : "ROLE_STANDALONE") ));
591 return NT_STATUS_NOT_IMPLEMENTED;
593 domain = lp_netbios_name();
595 case ROLE_DOMAIN_PDC:
596 case ROLE_DOMAIN_BDC:
597 if ( !is_local_name && !is_my_domain ) {
598 DEBUG(6,("check_samstrict_security: %s is not one of my local names or domain name (DC)\n",
599 user_info->domain.str));
600 return NT_STATUS_NOT_IMPLEMENTED;
602 domain = lp_workgroup();
604 default: /* name is ok */
605 domain = user_info->domain.str;
609 return check_sam_security_internals(auth_context, domain, mem_ctx, user_info, server_info);
612 /* module initialisation */
613 static NTSTATUS auth_init_sam(struct auth_context *auth_context,
615 struct auth_methods **auth_method)
617 if (!make_auth_methods(auth_context, auth_method)) {
618 return NT_STATUS_NO_MEMORY;
621 (*auth_method)->auth = check_samstrict_security;
622 (*auth_method)->name = "sam";
626 NTSTATUS auth_sam_init(void)
629 struct auth_operations ops;
632 ops.init = auth_init_sam;
633 ret = register_backend("auth", &ops);
634 if (!NT_STATUS_IS_OK(ret)) {
635 DEBUG(0,("Failed to register '%s' auth backend!\n",
640 ops.name = "sam_ignoredomain";
641 ops.init = auth_init_sam_ignoredomain;
642 ret = register_backend("auth", &ops);
643 if (!NT_STATUS_IS_OK(ret)) {
644 DEBUG(0,("Failed to register '%s' auth backend!\n",