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
6 Copyright (C) Stefan Metzmacher 2005
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.
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.
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/>.
23 #include "librpc/gen_ndr/ndr_netlogon.h"
24 #include "system/time.h"
26 #include "lib/ldb/include/ldb.h"
27 #include "auth/auth.h"
28 #include "auth/auth_sam.h"
29 #include "dsdb/samdb/samdb.h"
30 #include "libcli/security/security.h"
31 #include "libcli/ldap/ldap.h"
33 extern const char *user_attrs[];
34 extern const char *domain_ref_attrs[];
36 /****************************************************************************
37 Look for the specified user in the sam, return ldb result structures
38 ****************************************************************************/
40 static NTSTATUS authsam_search_account(TALLOC_CTX *mem_ctx, struct ldb_context *sam_ctx,
41 const char *account_name,
42 const char *domain_name,
43 struct ldb_message ***ret_msgs,
44 struct ldb_message ***ret_msgs_domain_ref)
46 struct ldb_message **msgs_tmp;
47 struct ldb_message **msgs;
48 struct ldb_message **msgs_domain_ref;
49 struct ldb_dn *partitions_basedn = samdb_partitions_dn(sam_ctx, mem_ctx);
54 struct ldb_dn *domain_dn = NULL;
57 domain_dn = samdb_domain_to_dn(sam_ctx, mem_ctx, domain_name);
59 return NT_STATUS_INTERNAL_DB_CORRUPTION;
63 /* pull the user attributes */
64 ret = gendb_search(sam_ctx, mem_ctx, domain_dn, &msgs, user_attrs,
65 "(&(sAMAccountName=%s)(objectclass=user))",
66 ldb_binary_encode_string(mem_ctx, account_name));
68 return NT_STATUS_INTERNAL_DB_CORRUPTION;
72 DEBUG(3,("sam_search_user: Couldn't find user [%s\\%s] in samdb, under %s\n",
73 domain_name, account_name, ldb_dn_get_linearized(domain_dn)));
74 return NT_STATUS_NO_SUCH_USER;
78 DEBUG(0,("Found %d records matching user [%s]\n", ret, account_name));
79 return NT_STATUS_INTERNAL_DB_CORRUPTION;
83 struct dom_sid *domain_sid;
85 domain_sid = samdb_result_sid_prefix(mem_ctx, msgs[0], "objectSid");
87 return NT_STATUS_INTERNAL_DB_CORRUPTION;
90 /* find the domain's DN */
91 ret = gendb_search(sam_ctx, mem_ctx, NULL, &msgs_tmp, NULL,
92 "(&(objectSid=%s)(objectClass=domain))",
93 ldap_encode_ndr_dom_sid(mem_ctx, domain_sid));
95 return NT_STATUS_INTERNAL_DB_CORRUPTION;
99 DEBUG(3,("check_sam_security: Couldn't find domain_sid [%s] in passdb file.\n",
100 dom_sid_string(mem_ctx, domain_sid)));
101 return NT_STATUS_NO_SUCH_USER;
105 DEBUG(0,("Found %d records matching domain_sid [%s]\n",
106 ret, dom_sid_string(mem_ctx, domain_sid)));
107 return NT_STATUS_INTERNAL_DB_CORRUPTION;
110 domain_dn = msgs_tmp[0]->dn;
113 ret_domain = gendb_search(sam_ctx, mem_ctx, partitions_basedn, &msgs_domain_ref, domain_ref_attrs,
114 "(nCName=%s)", ldb_dn_get_linearized(domain_dn));
115 if (ret_domain == -1) {
116 return NT_STATUS_INTERNAL_DB_CORRUPTION;
119 if (ret_domain == 0) {
120 DEBUG(3,("check_sam_security: Couldn't find domain [%s] in passdb file.\n",
121 ldb_dn_get_linearized(msgs_tmp[0]->dn)));
122 return NT_STATUS_NO_SUCH_USER;
125 if (ret_domain > 1) {
126 DEBUG(0,("Found %d records matching domain [%s]\n",
127 ret_domain, ldb_dn_get_linearized(msgs_tmp[0]->dn)));
128 return NT_STATUS_INTERNAL_DB_CORRUPTION;
132 *ret_msgs_domain_ref = msgs_domain_ref;
137 /****************************************************************************
138 Do a specific test for an smb password being correct, given a smb_password and
139 the lanman and NT responses.
140 ****************************************************************************/
141 static NTSTATUS authsam_password_ok(struct auth_context *auth_context,
144 const struct samr_Password *lm_pwd,
145 const struct samr_Password *nt_pwd,
146 const struct auth_usersupplied_info *user_info,
147 DATA_BLOB *user_sess_key,
148 DATA_BLOB *lm_sess_key)
152 if (acct_flags & ACB_PWNOTREQ) {
153 if (lp_null_passwords()) {
154 DEBUG(3,("Account for user '%s' has no password and null passwords are allowed.\n",
155 user_info->mapped.account_name));
158 DEBUG(3,("Account for user '%s' has no password and null passwords are NOT allowed.\n",
159 user_info->mapped.account_name));
160 return NT_STATUS_LOGON_FAILURE;
164 switch (user_info->password_state) {
165 case AUTH_PASSWORD_PLAIN:
167 const struct auth_usersupplied_info *user_info_temp;
168 status = encrypt_user_info(mem_ctx, auth_context,
170 user_info, &user_info_temp);
171 if (!NT_STATUS_IS_OK(status)) {
172 DEBUG(1, ("Failed to convert plaintext password to password HASH: %s\n", nt_errstr(status)));
175 user_info = user_info_temp;
179 case AUTH_PASSWORD_HASH:
180 *lm_sess_key = data_blob(NULL, 0);
181 *user_sess_key = data_blob(NULL, 0);
182 status = hash_password_check(mem_ctx,
183 user_info->password.hash.lanman,
184 user_info->password.hash.nt,
185 user_info->mapped.account_name,
187 NT_STATUS_NOT_OK_RETURN(status);
190 case AUTH_PASSWORD_RESPONSE:
191 status = ntlm_password_check(mem_ctx, user_info->logon_parameters,
192 &auth_context->challenge.data,
193 &user_info->password.response.lanman,
194 &user_info->password.response.nt,
195 user_info->mapped.account_name,
196 user_info->client.account_name,
197 user_info->client.domain_name,
199 user_sess_key, lm_sess_key);
200 NT_STATUS_NOT_OK_RETURN(status);
204 if (user_sess_key && user_sess_key->data) {
205 talloc_steal(auth_context, user_sess_key->data);
207 if (lm_sess_key && lm_sess_key->data) {
208 talloc_steal(auth_context, lm_sess_key->data);
216 static NTSTATUS authsam_authenticate(struct auth_context *auth_context,
217 TALLOC_CTX *mem_ctx, struct ldb_context *sam_ctx,
218 struct ldb_message **msgs,
219 struct ldb_message **msgs_domain_ref,
220 const struct auth_usersupplied_info *user_info,
221 DATA_BLOB *user_sess_key, DATA_BLOB *lm_sess_key)
223 struct samr_Password *lm_pwd, *nt_pwd;
225 uint16_t acct_flags = samdb_result_acct_flags(msgs[0], "userAccountControl");
227 /* Quit if the account was locked out. */
228 if (acct_flags & ACB_AUTOLOCK) {
229 DEBUG(3,("check_sam_security: Account for user %s was locked out.\n",
230 user_info->mapped.account_name));
231 return NT_STATUS_ACCOUNT_LOCKED_OUT;
234 /* You can only do an interactive login to normal accounts */
235 if (user_info->flags & USER_INFO_INTERACTIVE_LOGON) {
236 if (!(acct_flags & ACB_NORMAL)) {
237 return NT_STATUS_NO_SUCH_USER;
241 nt_status = samdb_result_passwords(mem_ctx, msgs[0], &lm_pwd, &nt_pwd);
242 NT_STATUS_NOT_OK_RETURN(nt_status);
244 nt_status = authsam_password_ok(auth_context, mem_ctx,
245 acct_flags, lm_pwd, nt_pwd,
246 user_info, user_sess_key, lm_sess_key);
247 NT_STATUS_NOT_OK_RETURN(nt_status);
249 nt_status = authsam_account_ok(mem_ctx, sam_ctx,
250 user_info->logon_parameters,
253 user_info->workstation_name,
254 user_info->mapped.account_name);
261 static NTSTATUS authsam_check_password_internals(struct auth_method_context *ctx,
264 const struct auth_usersupplied_info *user_info,
265 struct auth_serversupplied_info **server_info)
268 const char *account_name = user_info->mapped.account_name;
269 struct ldb_message **msgs;
270 struct ldb_message **domain_ref_msgs;
271 struct ldb_context *sam_ctx;
272 DATA_BLOB user_sess_key, lm_sess_key;
275 if (!account_name || !*account_name) {
277 return NT_STATUS_NOT_IMPLEMENTED;
280 tmp_ctx = talloc_new(mem_ctx);
282 return NT_STATUS_NO_MEMORY;
285 sam_ctx = samdb_connect(tmp_ctx, system_session(mem_ctx));
286 if (sam_ctx == NULL) {
287 talloc_free(tmp_ctx);
288 return NT_STATUS_INVALID_SYSTEM_SERVICE;
291 nt_status = authsam_search_account(tmp_ctx, sam_ctx, account_name, domain, &msgs, &domain_ref_msgs);
292 if (!NT_STATUS_IS_OK(nt_status)) {
293 talloc_free(tmp_ctx);
297 nt_status = authsam_authenticate(ctx->auth_ctx, tmp_ctx, sam_ctx, msgs, domain_ref_msgs, user_info,
298 &user_sess_key, &lm_sess_key);
299 if (!NT_STATUS_IS_OK(nt_status)) {
300 talloc_free(tmp_ctx);
304 nt_status = authsam_make_server_info(tmp_ctx, sam_ctx, msgs[0], domain_ref_msgs[0],
305 user_sess_key, lm_sess_key,
307 if (!NT_STATUS_IS_OK(nt_status)) {
308 talloc_free(tmp_ctx);
312 talloc_steal(mem_ctx, *server_info);
313 talloc_free(tmp_ctx);
318 static NTSTATUS authsam_ignoredomain_want_check(struct auth_method_context *ctx,
320 const struct auth_usersupplied_info *user_info)
322 if (!user_info->mapped.account_name || !*user_info->mapped.account_name) {
323 return NT_STATUS_NOT_IMPLEMENTED;
329 static NTSTATUS authsam_ignoredomain_check_password(struct auth_method_context *ctx,
331 const struct auth_usersupplied_info *user_info,
332 struct auth_serversupplied_info **server_info)
334 return authsam_check_password_internals(ctx, mem_ctx, NULL, user_info, server_info);
337 /****************************************************************************
338 Check SAM security (above) but with a few extra checks.
339 ****************************************************************************/
340 static NTSTATUS authsam_want_check(struct auth_method_context *ctx,
342 const struct auth_usersupplied_info *user_info)
344 BOOL is_local_name, is_my_domain;
346 if (!user_info->mapped.account_name || !*user_info->mapped.account_name) {
347 return NT_STATUS_NOT_IMPLEMENTED;
350 is_local_name = is_myname(user_info->mapped.domain_name);
351 is_my_domain = strequal(user_info->mapped.domain_name, lp_workgroup());
353 /* check whether or not we service this domain/workgroup name */
354 switch (lp_server_role()) {
355 case ROLE_STANDALONE:
358 case ROLE_DOMAIN_MEMBER:
359 if (!is_local_name) {
360 DEBUG(6,("authsam_check_password: %s is not one of my local names (DOMAIN_MEMBER)\n",
361 user_info->mapped.domain_name));
362 return NT_STATUS_NOT_IMPLEMENTED;
366 case ROLE_DOMAIN_CONTROLLER:
367 if (!is_local_name && !is_my_domain) {
368 DEBUG(6,("authsam_check_password: %s is not one of my local names or domain name (DC)\n",
369 user_info->mapped.domain_name));
370 return NT_STATUS_NOT_IMPLEMENTED;
375 DEBUG(6,("authsam_check_password: lp_server_role() has an undefined value\n"));
376 return NT_STATUS_NOT_IMPLEMENTED;
379 /****************************************************************************
380 Check SAM security (above) but with a few extra checks.
381 ****************************************************************************/
382 static NTSTATUS authsam_check_password(struct auth_method_context *ctx,
384 const struct auth_usersupplied_info *user_info,
385 struct auth_serversupplied_info **server_info)
389 /* check whether or not we service this domain/workgroup name */
390 switch (lp_server_role()) {
391 case ROLE_STANDALONE:
392 case ROLE_DOMAIN_MEMBER:
393 domain = lp_netbios_name();
396 case ROLE_DOMAIN_CONTROLLER:
397 domain = lp_workgroup();
401 return NT_STATUS_NO_SUCH_USER;
404 return authsam_check_password_internals(ctx, mem_ctx, domain, user_info, server_info);
407 static const struct auth_operations sam_ignoredomain_ops = {
408 .name = "sam_ignoredomain",
409 .get_challenge = auth_get_challenge_not_implemented,
410 .want_check = authsam_ignoredomain_want_check,
411 .check_password = authsam_ignoredomain_check_password
414 static const struct auth_operations sam_ops = {
416 .get_challenge = auth_get_challenge_not_implemented,
417 .want_check = authsam_want_check,
418 .check_password = authsam_check_password
421 NTSTATUS auth_sam_init(void)
425 ret = auth_register(&sam_ops);
426 if (!NT_STATUS_IS_OK(ret)) {
427 DEBUG(0,("Failed to register 'sam' auth backend!\n"));
431 ret = auth_register(&sam_ignoredomain_ops);
432 if (!NT_STATUS_IS_OK(ret)) {
433 DEBUG(0,("Failed to register 'sam_ignoredomain' auth backend!\n"));