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"
25 #include "lib/ldb/include/ldb.h"
26 #include "util/util_ldb.h"
27 #include "auth/auth.h"
28 #include "auth/ntlm/ntlm_check.h"
29 #include "auth/ntlm/auth_proto.h"
30 #include "auth/auth_sam.h"
31 #include "dsdb/samdb/samdb.h"
32 #include "libcli/security/security.h"
33 #include "libcli/ldap/ldap_ndr.h"
34 #include "param/param.h"
36 extern const char *user_attrs[];
37 extern const char *domain_ref_attrs[];
39 /****************************************************************************
40 Look for the specified user in the sam, return ldb result structures
41 ****************************************************************************/
43 static NTSTATUS authsam_search_account(TALLOC_CTX *mem_ctx, struct ldb_context *sam_ctx,
44 const char *account_name,
45 const char *domain_name,
46 struct ldb_message ***ret_msgs,
47 struct ldb_message ***ret_msgs_domain_ref)
49 struct ldb_message **msgs_tmp;
50 struct ldb_message **msgs;
51 struct ldb_message **msgs_domain_ref;
52 struct ldb_dn *partitions_basedn = samdb_partitions_dn(sam_ctx, mem_ctx);
57 struct ldb_dn *domain_dn = NULL;
60 domain_dn = samdb_domain_to_dn(sam_ctx, mem_ctx, domain_name);
62 return NT_STATUS_INTERNAL_DB_CORRUPTION;
66 /* pull the user attributes */
67 ret = gendb_search(sam_ctx, mem_ctx, domain_dn, &msgs, user_attrs,
68 "(&(sAMAccountName=%s)(objectclass=user))",
69 ldb_binary_encode_string(mem_ctx, account_name));
71 return NT_STATUS_INTERNAL_DB_CORRUPTION;
75 DEBUG(3,("sam_search_user: Couldn't find user [%s\\%s] in samdb, under %s\n",
76 domain_name, account_name, ldb_dn_get_linearized(domain_dn)));
77 return NT_STATUS_NO_SUCH_USER;
81 DEBUG(0,("Found %d records matching user [%s]\n", ret, account_name));
82 return NT_STATUS_INTERNAL_DB_CORRUPTION;
86 struct dom_sid *domain_sid;
88 domain_sid = samdb_result_sid_prefix(mem_ctx, msgs[0], "objectSid");
90 return NT_STATUS_INTERNAL_DB_CORRUPTION;
93 /* find the domain's DN */
94 ret = gendb_search(sam_ctx, mem_ctx, NULL, &msgs_tmp, NULL,
95 "(&(objectSid=%s)(objectClass=domain))",
96 ldap_encode_ndr_dom_sid(mem_ctx, domain_sid));
98 return NT_STATUS_INTERNAL_DB_CORRUPTION;
102 DEBUG(3,("check_sam_security: Couldn't find domain_sid [%s] in passdb file.\n",
103 dom_sid_string(mem_ctx, domain_sid)));
104 return NT_STATUS_NO_SUCH_USER;
108 DEBUG(0,("Found %d records matching domain_sid [%s]\n",
109 ret, dom_sid_string(mem_ctx, domain_sid)));
110 return NT_STATUS_INTERNAL_DB_CORRUPTION;
113 domain_dn = msgs_tmp[0]->dn;
116 ret_domain = gendb_search(sam_ctx, mem_ctx, partitions_basedn, &msgs_domain_ref, domain_ref_attrs,
117 "(nCName=%s)", ldb_dn_get_linearized(domain_dn));
118 if (ret_domain == -1) {
119 return NT_STATUS_INTERNAL_DB_CORRUPTION;
122 if (ret_domain == 0) {
123 DEBUG(3,("check_sam_security: Couldn't find domain [%s] in passdb file.\n",
124 ldb_dn_get_linearized(msgs_tmp[0]->dn)));
125 return NT_STATUS_NO_SUCH_USER;
128 if (ret_domain > 1) {
129 DEBUG(0,("Found %d records matching domain [%s]\n",
130 ret_domain, ldb_dn_get_linearized(msgs_tmp[0]->dn)));
131 return NT_STATUS_INTERNAL_DB_CORRUPTION;
135 *ret_msgs_domain_ref = msgs_domain_ref;
140 /****************************************************************************
141 Do a specific test for an smb password being correct, given a smb_password and
142 the lanman and NT responses.
143 ****************************************************************************/
144 static NTSTATUS authsam_password_ok(struct auth_context *auth_context,
147 const struct samr_Password *lm_pwd,
148 const struct samr_Password *nt_pwd,
149 const struct auth_usersupplied_info *user_info,
150 DATA_BLOB *user_sess_key,
151 DATA_BLOB *lm_sess_key)
155 if (acct_flags & ACB_PWNOTREQ) {
156 if (lp_null_passwords(auth_context->lp_ctx)) {
157 DEBUG(3,("Account for user '%s' has no password and null passwords are allowed.\n",
158 user_info->mapped.account_name));
161 DEBUG(3,("Account for user '%s' has no password and null passwords are NOT allowed.\n",
162 user_info->mapped.account_name));
163 return NT_STATUS_LOGON_FAILURE;
167 switch (user_info->password_state) {
168 case AUTH_PASSWORD_PLAIN:
170 const struct auth_usersupplied_info *user_info_temp;
171 status = encrypt_user_info(mem_ctx, auth_context,
173 user_info, &user_info_temp);
174 if (!NT_STATUS_IS_OK(status)) {
175 DEBUG(1, ("Failed to convert plaintext password to password HASH: %s\n", nt_errstr(status)));
178 user_info = user_info_temp;
182 case AUTH_PASSWORD_HASH:
183 *lm_sess_key = data_blob(NULL, 0);
184 *user_sess_key = data_blob(NULL, 0);
185 status = hash_password_check(mem_ctx,
186 auth_context->lp_ctx,
187 user_info->password.hash.lanman,
188 user_info->password.hash.nt,
189 user_info->mapped.account_name,
191 NT_STATUS_NOT_OK_RETURN(status);
194 case AUTH_PASSWORD_RESPONSE:
195 status = ntlm_password_check(mem_ctx,
196 auth_context->lp_ctx,
197 user_info->logon_parameters,
198 &auth_context->challenge.data,
199 &user_info->password.response.lanman,
200 &user_info->password.response.nt,
201 user_info->mapped.account_name,
202 user_info->client.account_name,
203 user_info->client.domain_name,
205 user_sess_key, lm_sess_key);
206 NT_STATUS_NOT_OK_RETURN(status);
210 if (user_sess_key && user_sess_key->data) {
211 talloc_steal(auth_context, user_sess_key->data);
213 if (lm_sess_key && lm_sess_key->data) {
214 talloc_steal(auth_context, lm_sess_key->data);
222 static NTSTATUS authsam_authenticate(struct auth_context *auth_context,
223 TALLOC_CTX *mem_ctx, struct ldb_context *sam_ctx,
224 struct ldb_message **msgs,
225 struct ldb_message **msgs_domain_ref,
226 const struct auth_usersupplied_info *user_info,
227 DATA_BLOB *user_sess_key, DATA_BLOB *lm_sess_key)
229 struct samr_Password *lm_pwd, *nt_pwd;
231 struct ldb_dn *domain_dn = samdb_result_dn(sam_ctx, mem_ctx, msgs_domain_ref[0], "nCName", NULL);
233 uint16_t acct_flags = samdb_result_acct_flags(sam_ctx, mem_ctx, msgs[0], domain_dn);
235 /* Quit if the account was locked out. */
236 if (acct_flags & ACB_AUTOLOCK) {
237 DEBUG(3,("check_sam_security: Account for user %s was locked out.\n",
238 user_info->mapped.account_name));
239 return NT_STATUS_ACCOUNT_LOCKED_OUT;
242 /* You can only do an interactive login to normal accounts */
243 if (user_info->flags & USER_INFO_INTERACTIVE_LOGON) {
244 if (!(acct_flags & ACB_NORMAL)) {
245 return NT_STATUS_NO_SUCH_USER;
249 nt_status = samdb_result_passwords(mem_ctx, msgs[0], &lm_pwd, &nt_pwd);
250 NT_STATUS_NOT_OK_RETURN(nt_status);
252 nt_status = authsam_password_ok(auth_context, mem_ctx,
253 acct_flags, lm_pwd, nt_pwd,
254 user_info, user_sess_key, lm_sess_key);
255 NT_STATUS_NOT_OK_RETURN(nt_status);
257 nt_status = authsam_account_ok(mem_ctx, sam_ctx,
258 user_info->logon_parameters,
261 user_info->workstation_name,
262 user_info->mapped.account_name);
269 static NTSTATUS authsam_check_password_internals(struct auth_method_context *ctx,
272 const struct auth_usersupplied_info *user_info,
273 struct auth_serversupplied_info **server_info)
276 const char *account_name = user_info->mapped.account_name;
277 struct ldb_message **msgs;
278 struct ldb_message **domain_ref_msgs;
279 struct ldb_context *sam_ctx;
280 DATA_BLOB user_sess_key, lm_sess_key;
283 if (!account_name || !*account_name) {
285 return NT_STATUS_NOT_IMPLEMENTED;
288 tmp_ctx = talloc_new(mem_ctx);
290 return NT_STATUS_NO_MEMORY;
293 sam_ctx = samdb_connect(tmp_ctx, ctx->auth_ctx->event_ctx, ctx->auth_ctx->lp_ctx, system_session(mem_ctx, ctx->auth_ctx->lp_ctx));
294 if (sam_ctx == NULL) {
295 talloc_free(tmp_ctx);
296 return NT_STATUS_INVALID_SYSTEM_SERVICE;
299 nt_status = authsam_search_account(tmp_ctx, sam_ctx, account_name, domain, &msgs, &domain_ref_msgs);
300 if (!NT_STATUS_IS_OK(nt_status)) {
301 talloc_free(tmp_ctx);
305 nt_status = authsam_authenticate(ctx->auth_ctx, tmp_ctx, sam_ctx, msgs, domain_ref_msgs, user_info,
306 &user_sess_key, &lm_sess_key);
307 if (!NT_STATUS_IS_OK(nt_status)) {
308 talloc_free(tmp_ctx);
312 nt_status = authsam_make_server_info(tmp_ctx, sam_ctx, lp_netbios_name(ctx->auth_ctx->lp_ctx),
313 msgs[0], domain_ref_msgs[0],
314 user_sess_key, lm_sess_key,
316 if (!NT_STATUS_IS_OK(nt_status)) {
317 talloc_free(tmp_ctx);
321 talloc_steal(mem_ctx, *server_info);
322 talloc_free(tmp_ctx);
327 static NTSTATUS authsam_ignoredomain_want_check(struct auth_method_context *ctx,
329 const struct auth_usersupplied_info *user_info)
331 if (!user_info->mapped.account_name || !*user_info->mapped.account_name) {
332 return NT_STATUS_NOT_IMPLEMENTED;
338 static NTSTATUS authsam_ignoredomain_check_password(struct auth_method_context *ctx,
340 const struct auth_usersupplied_info *user_info,
341 struct auth_serversupplied_info **server_info)
343 return authsam_check_password_internals(ctx, mem_ctx, NULL, user_info, server_info);
346 /****************************************************************************
347 Check SAM security (above) but with a few extra checks.
348 ****************************************************************************/
349 static NTSTATUS authsam_want_check(struct auth_method_context *ctx,
351 const struct auth_usersupplied_info *user_info)
353 bool is_local_name, is_my_domain;
355 if (!user_info->mapped.account_name || !*user_info->mapped.account_name) {
356 return NT_STATUS_NOT_IMPLEMENTED;
359 is_local_name = lp_is_myname(ctx->auth_ctx->lp_ctx,
360 user_info->mapped.domain_name);
361 is_my_domain = lp_is_mydomain(ctx->auth_ctx->lp_ctx,
362 user_info->mapped.domain_name);
364 /* check whether or not we service this domain/workgroup name */
365 switch (lp_server_role(ctx->auth_ctx->lp_ctx)) {
366 case ROLE_STANDALONE:
369 case ROLE_DOMAIN_MEMBER:
370 if (!is_local_name) {
371 DEBUG(6,("authsam_check_password: %s is not one of my local names (DOMAIN_MEMBER)\n",
372 user_info->mapped.domain_name));
373 return NT_STATUS_NOT_IMPLEMENTED;
377 case ROLE_DOMAIN_CONTROLLER:
378 if (!is_local_name && !is_my_domain) {
379 DEBUG(6,("authsam_check_password: %s is not one of my local names or domain name (DC)\n",
380 user_info->mapped.domain_name));
381 return NT_STATUS_NOT_IMPLEMENTED;
386 DEBUG(6,("authsam_check_password: lp_server_role() has an undefined value\n"));
387 return NT_STATUS_NOT_IMPLEMENTED;
390 /****************************************************************************
391 Check SAM security (above) but with a few extra checks.
392 ****************************************************************************/
393 static NTSTATUS authsam_check_password(struct auth_method_context *ctx,
395 const struct auth_usersupplied_info *user_info,
396 struct auth_serversupplied_info **server_info)
400 /* check whether or not we service this domain/workgroup name */
401 switch (lp_server_role(ctx->auth_ctx->lp_ctx)) {
402 case ROLE_STANDALONE:
403 case ROLE_DOMAIN_MEMBER:
404 domain = lp_netbios_name(ctx->auth_ctx->lp_ctx);
407 case ROLE_DOMAIN_CONTROLLER:
408 domain = lp_workgroup(ctx->auth_ctx->lp_ctx);
412 return NT_STATUS_NO_SUCH_USER;
415 return authsam_check_password_internals(ctx, mem_ctx, domain, user_info, server_info);
418 static const struct auth_operations sam_ignoredomain_ops = {
419 .name = "sam_ignoredomain",
420 .get_challenge = auth_get_challenge_not_implemented,
421 .want_check = authsam_ignoredomain_want_check,
422 .check_password = authsam_ignoredomain_check_password
425 static const struct auth_operations sam_ops = {
427 .get_challenge = auth_get_challenge_not_implemented,
428 .want_check = authsam_want_check,
429 .check_password = authsam_check_password
432 _PUBLIC_ NTSTATUS auth_sam_init(void)
436 ret = auth_register(&sam_ops);
437 if (!NT_STATUS_IS_OK(ret)) {
438 DEBUG(0,("Failed to register 'sam' auth backend!\n"));
442 ret = auth_register(&sam_ignoredomain_ops);
443 if (!NT_STATUS_IS_OK(ret)) {
444 DEBUG(0,("Failed to register 'sam_ignoredomain' auth backend!\n"));