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"
25 #include "auth/auth.h"
28 #define DBGC_CLASS DBGC_AUTH
30 /****************************************************************************
31 Do a specific test for an smb password being correct, given a smb_password and
32 the lanman and NT responses.
33 ****************************************************************************/
35 static NTSTATUS sam_password_ok(const struct auth_context *auth_context,
39 const struct samr_Password *lm_pwd,
40 const struct samr_Password *nt_pwd,
41 const struct auth_usersupplied_info *user_info,
42 DATA_BLOB *user_sess_key,
43 DATA_BLOB *lm_sess_key)
47 if (acct_flags & ACB_PWNOTREQ) {
48 if (lp_null_passwords()) {
49 DEBUG(3,("Account for user '%s' has no password and null passwords are allowed.\n",
53 DEBUG(3,("Account for user '%s' has no password and null passwords are NOT allowed.\n",
55 return NT_STATUS_LOGON_FAILURE;
59 status = ntlm_password_check(mem_ctx, &auth_context->challenge,
60 &user_info->lm_resp, &user_info->nt_resp,
61 &user_info->lm_interactive_password,
62 &user_info->nt_interactive_password,
64 user_info->smb_name.str,
65 user_info->client_domain.str,
66 lm_pwd->hash, nt_pwd->hash, user_sess_key, lm_sess_key);
68 if (NT_STATUS_IS_OK(status)) {
69 if (user_sess_key && user_sess_key->data) {
70 talloc_steal(auth_context, user_sess_key->data);
72 if (lm_sess_key && lm_sess_key->data) {
73 talloc_steal(auth_context, lm_sess_key->data);
81 /****************************************************************************
82 Do a specific test for a SAM_ACCOUNT being vaild for this connection
83 (ie not disabled, expired and the like).
84 ****************************************************************************/
86 static NTSTATUS sam_account_ok(TALLOC_CTX *mem_ctx,
90 NTTIME *must_change_time,
91 NTTIME *last_set_time,
92 const char *workstation_list,
93 const struct auth_usersupplied_info *user_info)
95 DEBUG(4,("sam_account_ok: Checking SMB password for user %s\n", username));
97 /* Quit if the account was disabled. */
98 if (acct_flags & ACB_DISABLED) {
99 DEBUG(1,("sam_account_ok: Account for user '%s' was disabled.\n", username));
100 return NT_STATUS_ACCOUNT_DISABLED;
103 /* Quit if the account was locked out. */
104 if (acct_flags & ACB_AUTOLOCK) {
105 DEBUG(1,("sam_account_ok: Account for user %s was locked out.\n", username));
106 return NT_STATUS_ACCOUNT_LOCKED_OUT;
109 /* Test account expire time */
110 if ((*acct_expiry) != -1 && time(NULL) > nt_time_to_unix(*acct_expiry)) {
111 DEBUG(1,("sam_account_ok: Account for user '%s' has expired.\n", username));
112 DEBUG(3,("sam_account_ok: Account expired at '%s'.\n",
113 nt_time_string(mem_ctx, *acct_expiry)));
114 return NT_STATUS_ACCOUNT_EXPIRED;
117 if (!(acct_flags & ACB_PWNOEXP)) {
119 /* check for immediate expiry "must change at next logon" */
120 if (*must_change_time == 0 && *last_set_time != 0) {
121 DEBUG(1,("sam_account_ok: Account for user '%s' password must change!.\n",
123 return NT_STATUS_PASSWORD_MUST_CHANGE;
126 /* check for expired password */
127 if ((*must_change_time) != 0 && nt_time_to_unix(*must_change_time) < time(NULL)) {
128 DEBUG(1,("sam_account_ok: Account for user '%s' password expired!.\n",
130 DEBUG(1,("sam_account_ok: Password expired at '%s' unix time.\n",
131 nt_time_string(mem_ctx, *must_change_time)));
132 return NT_STATUS_PASSWORD_EXPIRED;
136 /* Test workstation. Workstation list is comma separated. */
138 if (workstation_list && *workstation_list) {
139 BOOL invalid_ws = True;
140 const char *s = workstation_list;
144 while (next_token(&s, tok, ",", sizeof(tok))) {
145 DEBUG(10,("sam_account_ok: checking for workstation match %s and %s (len=%d)\n",
146 tok, user_info->wksta_name.str, user_info->wksta_name.len));
148 if(strequal(tok, user_info->wksta_name.str)) {
156 return NT_STATUS_INVALID_WORKSTATION;
159 if (acct_flags & ACB_DOMTRUST) {
160 DEBUG(2,("sam_account_ok: Domain trust account %s denied by server\n", username));
161 return NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT;
164 if (acct_flags & ACB_SVRTRUST) {
165 DEBUG(2,("sam_account_ok: Server trust account %s denied by server\n", username));
166 return NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT;
169 if (acct_flags & ACB_WSTRUST) {
170 DEBUG(4,("sam_account_ok: Wksta trust account %s denied by server\n", username));
171 return NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT;
177 /****************************************************************************
178 Look for the specified user in the sam, return ldb result structures
179 ****************************************************************************/
181 static NTSTATUS sam_search_user(const char *username, const char *domain,
182 TALLOC_CTX *mem_ctx, void *sam_ctx,
183 struct ldb_message ***ret_msgs,
184 struct ldb_message ***ret_msgs_domain)
186 struct ldb_message **msgs;
187 struct ldb_message **msgs_domain;
192 const char *domain_dn = NULL;
193 const char *domain_sid;
195 const char *attrs[] = {"unicodePwd", "lmPwdHash", "ntPwdHash",
196 "userAccountControl",
202 /* required for server_info, not access control: */
218 const char *domain_attrs[] = {"name", "objectSid"};
221 /* find the domain's DN */
222 ret_domain = samdb_search(sam_ctx, mem_ctx, NULL, &msgs_domain, domain_attrs,
223 "(&(|(realm=%s)(name=%s))(objectclass=domain))",
226 if (ret_domain == 0) {
227 DEBUG(3,("check_sam_security: Couldn't find domain [%s] in passdb file.\n",
229 return NT_STATUS_NO_SUCH_USER;
232 if (ret_domain > 1) {
233 DEBUG(0,("Found %d records matching domain [%s]\n",
234 ret_domain, domain));
235 return NT_STATUS_INTERNAL_DB_CORRUPTION;
238 domain_dn = msgs_domain[0]->dn;
241 /* pull the user attributes */
242 ret = samdb_search(sam_ctx, mem_ctx, domain_dn, &msgs, attrs,
243 "(&(sAMAccountName=%s)(objectclass=user))",
247 DEBUG(3,("check_sam_security: Couldn't find user [%s] in passdb file.\n",
249 return NT_STATUS_NO_SUCH_USER;
253 DEBUG(0,("Found %d records matching user [%s]\n", ret, username));
254 return NT_STATUS_INTERNAL_DB_CORRUPTION;
258 domain_sid = samdb_result_sid_prefix(mem_ctx, msgs[0], "objectSid");
260 return NT_STATUS_INTERNAL_DB_CORRUPTION;
263 /* find the domain's DN */
264 ret_domain = samdb_search(sam_ctx, mem_ctx, NULL, &msgs_domain, domain_attrs,
265 "(&(objectSid=%s)(objectclass=domain))",
268 if (ret_domain == 0) {
269 DEBUG(3,("check_sam_security: Couldn't find domain [%s] in passdb file.\n",
271 return NT_STATUS_NO_SUCH_USER;
274 if (ret_domain > 1) {
275 DEBUG(0,("Found %d records matching domain [%s]\n",
276 ret_domain, domain_sid));
277 return NT_STATUS_INTERNAL_DB_CORRUPTION;
280 domain_dn = msgs_domain[0]->dn;
283 *ret_msgs_domain = msgs_domain;
288 NTSTATUS sam_check_password(const struct auth_context *auth_context,
289 const char *username,
290 TALLOC_CTX *mem_ctx, void *sam_ctx,
291 struct ldb_message **msgs,
292 const char *domain_dn,
293 const struct auth_usersupplied_info *user_info,
294 DATA_BLOB *user_sess_key, DATA_BLOB *lm_sess_key)
298 const char *workstation_list;
300 NTTIME must_change_time;
301 NTTIME last_set_time;
302 struct samr_Password *lm_pwd, *nt_pwd;
307 acct_flags = samdb_result_acct_flags(msgs[0], "sAMAcctFlags");
309 /* Quit if the account was locked out. */
310 if (acct_flags & ACB_AUTOLOCK) {
311 DEBUG(3,("check_sam_security: Account for user %s was locked out.\n",
313 return NT_STATUS_ACCOUNT_LOCKED_OUT;
316 if (!NT_STATUS_IS_OK(nt_status = samdb_result_passwords(mem_ctx, msgs[0],
317 &lm_pwd, &nt_pwd))) {
321 nt_status = sam_password_ok(auth_context, mem_ctx,
322 username, acct_flags,
324 user_info, user_sess_key, lm_sess_key);
326 if (!NT_STATUS_IS_OK(nt_status)) {
330 acct_expiry = samdb_result_nttime(msgs[0], "accountExpires", 0);
331 must_change_time = samdb_result_force_password_change(sam_ctx, mem_ctx,
334 last_set_time = samdb_result_nttime(msgs[0], "pwdLastSet", 0);
336 workstation_list = samdb_result_string(msgs[0], "userWorkstations", NULL);
338 nt_status = sam_account_ok(mem_ctx, username, acct_flags,
348 NTSTATUS sam_make_server_info(TALLOC_CTX *mem_ctx, void *sam_ctx,
349 struct ldb_message **msgs, struct ldb_message **msgs_domain,
350 struct auth_serversupplied_info **server_info)
353 struct ldb_message **group_msgs;
355 const char *group_attrs[3] = { "sAMAccountType", "objectSid", NULL };
356 /* find list of sids */
357 struct dom_sid **groupSIDs = NULL;
358 struct dom_sid *user_sid;
359 struct dom_sid *primary_group_sid;
366 if (!NT_STATUS_IS_OK(nt_status = make_server_info(mem_ctx, server_info,
367 samdb_result_string(msgs[0], "sAMAccountName", "")))) {
368 DEBUG(0,("check_sam_security: make_server_info_sam() failed with '%s'\n", nt_errstr(nt_status)));
372 group_ret = samdb_search(sam_ctx,
373 mem_ctx, NULL, &group_msgs, group_attrs,
374 "(&(member=%s)(sAMAccountType=*))",
376 if (group_ret == -1) {
377 return NT_STATUS_INTERNAL_DB_CORRUPTION;
381 !(groupSIDs = talloc_array_p(*server_info, struct dom_sid *, group_ret))) {
382 talloc_free(*server_info);
383 return NT_STATUS_NO_MEMORY;
386 /* Need to unroll some nested groups, but not aliases */
387 for (i = 0; i < group_ret; i++) {
388 sidstr = ldb_msg_find_string(group_msgs[i], "objectSid", NULL);
389 groupSIDs[i] = dom_sid_parse_talloc(*server_info, sidstr);
392 sidstr = ldb_msg_find_string(msgs[0], "objectSid", NULL);
393 user_sid = dom_sid_parse_talloc(*server_info, sidstr);
394 primary_group_sid = dom_sid_parse_talloc(*server_info, sidstr);
395 rid = samdb_result_uint(msgs[0], "primaryGroupID", ~0);
398 primary_group_sid = groupSIDs[0];
400 primary_group_sid = NULL;
403 primary_group_sid->sub_auths[primary_group_sid->num_auths-1] = rid;
406 (*server_info)->user_sid = user_sid;
407 (*server_info)->primary_group_sid = primary_group_sid;
409 (*server_info)->n_domain_groups = group_ret;
410 (*server_info)->domain_groups = groupSIDs;
412 (*server_info)->account_name
413 = talloc_strdup(*server_info,
414 samdb_result_string(msgs[0], "sAMAccountName", ""));
416 (*server_info)->domain
417 = talloc_strdup(*server_info,
418 samdb_result_string(msgs_domain[0], "name", ""));
420 (*server_info)->full_name
421 = talloc_strdup(*server_info,
422 samdb_result_string(msgs[0], "displayName", ""));
424 (*server_info)->logon_script
425 = talloc_strdup(*server_info,
426 samdb_result_string(msgs[0], "scriptPath", ""));
427 (*server_info)->profile_path
428 = talloc_strdup(*server_info,
429 samdb_result_string(msgs[0], "profilePath", ""));
430 (*server_info)->home_directory
431 = talloc_strdup(*server_info,
432 samdb_result_string(msgs[0], "homeDirectory", ""));
434 (*server_info)->home_drive
435 = talloc_strdup(*server_info,
436 samdb_result_string(msgs[0], "homeDrive", ""));
438 (*server_info)->last_logon = samdb_result_nttime(msgs[0], "lastLogon", 0);
439 (*server_info)->last_logoff = samdb_result_nttime(msgs[0], "lastLogoff", 0);
440 (*server_info)->acct_expiry = samdb_result_nttime(msgs[0], "accountExpires", 0);
441 (*server_info)->last_password_change = samdb_result_nttime(msgs[0], "pwdLastSet", 0);
442 (*server_info)->allow_password_change
443 = samdb_result_allow_password_change(sam_ctx, mem_ctx,
444 msgs_domain[0]->dn, msgs[0], "pwdLastSet");
445 (*server_info)->force_password_change
446 = samdb_result_force_password_change(sam_ctx, mem_ctx,
447 msgs_domain[0]->dn, msgs[0], "pwdLastSet");
449 (*server_info)->logon_count = samdb_result_uint(msgs[0], "logonCount", 0);
450 (*server_info)->bad_password_count = samdb_result_uint(msgs[0], "badPwdCount", 0);
452 (*server_info)->acct_flags = samdb_result_acct_flags(msgs[0], "userAccountControl");
454 (*server_info)->guest = False;
456 if (!(*server_info)->account_name
457 || !(*server_info)->full_name
458 || !(*server_info)->logon_script
459 || !(*server_info)->profile_path
460 || !(*server_info)->home_directory
461 || !(*server_info)->home_drive) {
462 talloc_destroy(*server_info);
463 return NT_STATUS_NO_MEMORY;
469 NTSTATUS sam_get_server_info(const char *username, const char *domain, TALLOC_CTX *mem_ctx,
470 struct auth_serversupplied_info **server_info)
474 struct ldb_message **msgs;
475 struct ldb_message **domain_msgs;
478 sam_ctx = samdb_connect(mem_ctx);
479 if (sam_ctx == NULL) {
480 return NT_STATUS_INVALID_SYSTEM_SERVICE;
483 nt_status = sam_search_user(username, domain, mem_ctx, sam_ctx, &msgs, &domain_msgs);
484 if (!NT_STATUS_IS_OK(nt_status)) {
488 nt_status = sam_make_server_info(mem_ctx, sam_ctx, msgs, domain_msgs, server_info);
489 if (!NT_STATUS_IS_OK(nt_status)) {
496 static NTSTATUS check_sam_security_internals(const struct auth_context *auth_context,
499 const struct auth_usersupplied_info *user_info,
500 struct auth_serversupplied_info **server_info)
504 const char *username = user_info->internal_username.str;
505 struct ldb_message **msgs;
506 struct ldb_message **domain_msgs;
508 DATA_BLOB user_sess_key, lm_sess_key;
510 sam_ctx = samdb_connect(mem_ctx);
511 if (sam_ctx == NULL) {
512 return NT_STATUS_INVALID_SYSTEM_SERVICE;
515 nt_status = sam_search_user(username, domain, mem_ctx, sam_ctx, &msgs, &domain_msgs);
516 if (!NT_STATUS_IS_OK(nt_status)) {
520 nt_status = sam_check_password(auth_context, username, mem_ctx, sam_ctx, msgs, domain_msgs[0]->dn, user_info,
521 &user_sess_key, &lm_sess_key);
522 if (!NT_STATUS_IS_OK(nt_status)) {
526 nt_status = sam_make_server_info(mem_ctx, sam_ctx, msgs, domain_msgs, server_info);
527 if (!NT_STATUS_IS_OK(nt_status)) {
531 talloc_reference(auth_context, *server_info);
533 (*server_info)->user_session_key = user_sess_key;
534 (*server_info)->lm_session_key = lm_sess_key;
538 static NTSTATUS check_sam_security(const struct auth_context *auth_context,
539 void *my_private_data,
541 const struct auth_usersupplied_info *user_info,
542 struct auth_serversupplied_info **server_info)
544 return check_sam_security_internals(auth_context, NULL,
545 mem_ctx, user_info, server_info);
548 /* module initialisation */
549 static NTSTATUS auth_init_sam_ignoredomain(struct auth_context *auth_context,
551 struct auth_methods **auth_method)
553 if (!make_auth_methods(auth_context, auth_method)) {
554 return NT_STATUS_NO_MEMORY;
557 (*auth_method)->auth = check_sam_security;
558 (*auth_method)->name = "sam_ignoredomain";
563 /****************************************************************************
564 Check SAM security (above) but with a few extra checks.
565 ****************************************************************************/
567 static NTSTATUS check_samstrict_security(const struct auth_context *auth_context,
568 void *my_private_data,
570 const struct auth_usersupplied_info *user_info,
571 struct auth_serversupplied_info **server_info)
574 BOOL is_local_name, is_my_domain;
576 if (!user_info || !auth_context) {
577 return NT_STATUS_LOGON_FAILURE;
580 is_local_name = is_myname(user_info->domain.str);
581 is_my_domain = strequal(user_info->domain.str, lp_workgroup());
583 /* check whether or not we service this domain/workgroup name */
585 switch ( lp_server_role() ) {
586 case ROLE_STANDALONE:
587 case ROLE_DOMAIN_MEMBER:
588 if ( !is_local_name ) {
589 DEBUG(6,("check_samstrict_security: %s is not one of my local names (%s)\n",
590 user_info->domain.str, (lp_server_role() == ROLE_DOMAIN_MEMBER
591 ? "ROLE_DOMAIN_MEMBER" : "ROLE_STANDALONE") ));
592 return NT_STATUS_NOT_IMPLEMENTED;
594 domain = lp_netbios_name();
596 case ROLE_DOMAIN_PDC:
597 case ROLE_DOMAIN_BDC:
598 if ( !is_local_name && !is_my_domain ) {
599 DEBUG(6,("check_samstrict_security: %s is not one of my local names or domain name (DC)\n",
600 user_info->domain.str));
601 return NT_STATUS_NOT_IMPLEMENTED;
603 domain = lp_workgroup();
605 default: /* name is ok */
606 domain = user_info->domain.str;
610 return check_sam_security_internals(auth_context, domain, mem_ctx, user_info, server_info);
613 /* module initialisation */
614 static NTSTATUS auth_init_sam(struct auth_context *auth_context,
616 struct auth_methods **auth_method)
618 if (!make_auth_methods(auth_context, auth_method)) {
619 return NT_STATUS_NO_MEMORY;
622 (*auth_method)->auth = check_samstrict_security;
623 (*auth_method)->name = "sam";
627 NTSTATUS auth_sam_init(void)
630 struct auth_operations ops;
633 ops.init = auth_init_sam;
634 ret = auth_register(&ops);
635 if (!NT_STATUS_IS_OK(ret)) {
636 DEBUG(0,("Failed to register '%s' auth backend!\n",
641 ops.name = "sam_ignoredomain";
642 ops.init = auth_init_sam_ignoredomain;
643 ret = auth_register(&ops);
644 if (!NT_STATUS_IS_OK(ret)) {
645 DEBUG(0,("Failed to register '%s' auth backend!\n",