r18549: move gcc version check to libreplace and reorder the tests a bit
[Samba.git] / source / auth / auth_sam.c
blob3d28b88446a1038e41f492bfa5236914ba179d05
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
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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "includes.h"
24 #include "system/time.h"
25 #include "db_wrap.h"
26 #include "auth/auth.h"
27 #include "auth/auth_sam.h"
28 #include "dsdb/samdb/samdb.h"
29 #include "libcli/security/security.h"
30 #include "libcli/ldap/ldap.h"
32 extern const char *user_attrs[];
33 extern const char *domain_ref_attrs[];
35 /****************************************************************************
36 Look for the specified user in the sam, return ldb result structures
37 ****************************************************************************/
39 static NTSTATUS authsam_search_account(TALLOC_CTX *mem_ctx, struct ldb_context *sam_ctx,
40 const char *account_name,
41 const char *domain_name,
42 struct ldb_message ***ret_msgs,
43 struct ldb_message ***ret_msgs_domain_ref)
45 struct ldb_message **msgs_tmp;
46 struct ldb_message **msgs;
47 struct ldb_message **msgs_domain_ref;
48 const struct ldb_dn *partitions_basedn = samdb_partitions_dn(sam_ctx, mem_ctx);
50 int ret;
51 int ret_domain;
53 const struct ldb_dn *domain_dn = NULL;
55 if (domain_name) {
56 char *escaped_domain = ldb_binary_encode_string(mem_ctx, domain_name);
57 /* find the domain's DN */
58 ret_domain = gendb_search(sam_ctx, mem_ctx, partitions_basedn, &msgs_domain_ref, domain_ref_attrs,
59 "(&(&(|(&(dnsRoot=%s)(nETBIOSName=*))(nETBIOSName=%s))(objectclass=crossRef))(ncName=*))",
60 escaped_domain, escaped_domain);
61 if (ret_domain == -1) {
62 return NT_STATUS_INTERNAL_DB_CORRUPTION;
65 if (ret_domain == 0) {
66 DEBUG(3,("sam_search_user: Couldn't find domain [%s] in samdb.\n",
67 domain_name));
68 return NT_STATUS_NO_SUCH_USER;
71 if (ret_domain > 1) {
72 DEBUG(0,("Found %d records matching domain [%s]\n",
73 ret_domain, domain_name));
74 return NT_STATUS_INTERNAL_DB_CORRUPTION;
77 domain_dn = samdb_result_dn(mem_ctx, msgs_domain_ref[0], "nCName", NULL);
80 /* pull the user attributes */
81 ret = gendb_search(sam_ctx, mem_ctx, domain_dn, &msgs, user_attrs,
82 "(&(sAMAccountName=%s)(objectclass=user))",
83 ldb_binary_encode_string(mem_ctx, account_name));
84 if (ret == -1) {
85 return NT_STATUS_INTERNAL_DB_CORRUPTION;
88 if (ret == 0) {
89 DEBUG(3,("sam_search_user: Couldn't find user [%s\\%s] in samdb, under %s\n",
90 domain_name, account_name, ldb_dn_linearize(mem_ctx, domain_dn)));
91 return NT_STATUS_NO_SUCH_USER;
94 if (ret > 1) {
95 DEBUG(0,("Found %d records matching user [%s]\n", ret, account_name));
96 return NT_STATUS_INTERNAL_DB_CORRUPTION;
99 if (!domain_name) {
100 struct dom_sid *domain_sid;
102 domain_sid = samdb_result_sid_prefix(mem_ctx, msgs[0], "objectSid");
103 if (!domain_sid) {
104 return NT_STATUS_INTERNAL_DB_CORRUPTION;
107 /* find the domain's DN */
108 ret = gendb_search(sam_ctx, mem_ctx, NULL, &msgs_tmp, NULL,
109 "(&(objectSid=%s)(objectclass=domain))",
110 ldap_encode_ndr_dom_sid(mem_ctx, domain_sid));
111 if (ret == -1) {
112 return NT_STATUS_INTERNAL_DB_CORRUPTION;
115 if (ret == 0) {
116 DEBUG(3,("check_sam_security: Couldn't find domain_sid [%s] in passdb file.\n",
117 dom_sid_string(mem_ctx, domain_sid)));
118 return NT_STATUS_NO_SUCH_USER;
121 if (ret > 1) {
122 DEBUG(0,("Found %d records matching domain_sid [%s]\n",
123 ret, dom_sid_string(mem_ctx, domain_sid)));
124 return NT_STATUS_INTERNAL_DB_CORRUPTION;
127 ret_domain = gendb_search(sam_ctx, mem_ctx, partitions_basedn, &msgs_domain_ref, domain_ref_attrs,
128 "(nCName=%s)", ldb_dn_linearize(msgs_tmp, msgs_tmp[0]->dn));
130 if (ret_domain == -1) {
131 return NT_STATUS_INTERNAL_DB_CORRUPTION;
134 if (ret_domain == 0) {
135 DEBUG(3,("check_sam_security: Couldn't find domain [%s] in passdb file.\n",
136 ldb_dn_linearize(msgs_tmp, msgs_tmp[0]->dn)));
137 return NT_STATUS_NO_SUCH_USER;
140 if (ret_domain > 1) {
141 DEBUG(0,("Found %d records matching domain [%s]\n",
142 ret_domain, ldb_dn_linearize(msgs_tmp, msgs_tmp[0]->dn)));
143 return NT_STATUS_INTERNAL_DB_CORRUPTION;
148 *ret_msgs = msgs;
149 *ret_msgs_domain_ref = msgs_domain_ref;
151 return NT_STATUS_OK;
154 /****************************************************************************
155 Do a specific test for an smb password being correct, given a smb_password and
156 the lanman and NT responses.
157 ****************************************************************************/
158 static NTSTATUS authsam_password_ok(struct auth_context *auth_context,
159 TALLOC_CTX *mem_ctx,
160 uint16_t acct_flags,
161 const struct samr_Password *lm_pwd,
162 const struct samr_Password *nt_pwd,
163 const struct auth_usersupplied_info *user_info,
164 DATA_BLOB *user_sess_key,
165 DATA_BLOB *lm_sess_key)
167 NTSTATUS status;
169 if (acct_flags & ACB_PWNOTREQ) {
170 if (lp_null_passwords()) {
171 DEBUG(3,("Account for user '%s' has no password and null passwords are allowed.\n",
172 user_info->mapped.account_name));
173 return NT_STATUS_OK;
174 } else {
175 DEBUG(3,("Account for user '%s' has no password and null passwords are NOT allowed.\n",
176 user_info->mapped.account_name));
177 return NT_STATUS_LOGON_FAILURE;
181 switch (user_info->password_state) {
182 case AUTH_PASSWORD_PLAIN:
184 const struct auth_usersupplied_info *user_info_temp;
185 status = encrypt_user_info(mem_ctx, auth_context,
186 AUTH_PASSWORD_HASH,
187 user_info, &user_info_temp);
188 if (!NT_STATUS_IS_OK(status)) {
189 DEBUG(1, ("Failed to convert plaintext password to password HASH: %s\n", nt_errstr(status)));
190 return status;
192 user_info = user_info_temp;
194 /*fall through*/
196 case AUTH_PASSWORD_HASH:
197 *lm_sess_key = data_blob(NULL, 0);
198 *user_sess_key = data_blob(NULL, 0);
199 status = hash_password_check(mem_ctx,
200 user_info->password.hash.lanman,
201 user_info->password.hash.nt,
202 user_info->mapped.account_name,
203 lm_pwd, nt_pwd);
204 NT_STATUS_NOT_OK_RETURN(status);
205 break;
207 case AUTH_PASSWORD_RESPONSE:
208 status = ntlm_password_check(mem_ctx, user_info->logon_parameters,
209 &auth_context->challenge.data,
210 &user_info->password.response.lanman,
211 &user_info->password.response.nt,
212 user_info->mapped.account_name,
213 user_info->client.account_name,
214 user_info->client.domain_name,
215 lm_pwd, nt_pwd,
216 user_sess_key, lm_sess_key);
217 NT_STATUS_NOT_OK_RETURN(status);
218 break;
221 if (user_sess_key && user_sess_key->data) {
222 talloc_steal(auth_context, user_sess_key->data);
224 if (lm_sess_key && lm_sess_key->data) {
225 talloc_steal(auth_context, lm_sess_key->data);
228 return NT_STATUS_OK;
233 static NTSTATUS authsam_authenticate(struct auth_context *auth_context,
234 TALLOC_CTX *mem_ctx, struct ldb_context *sam_ctx,
235 struct ldb_message **msgs,
236 struct ldb_message **msgs_domain_ref,
237 const struct auth_usersupplied_info *user_info,
238 DATA_BLOB *user_sess_key, DATA_BLOB *lm_sess_key)
240 struct samr_Password *lm_pwd, *nt_pwd;
241 NTSTATUS nt_status;
242 uint16_t acct_flags = samdb_result_acct_flags(msgs[0], "userAccountControl");
244 /* Quit if the account was locked out. */
245 if (acct_flags & ACB_AUTOLOCK) {
246 DEBUG(3,("check_sam_security: Account for user %s was locked out.\n",
247 user_info->mapped.account_name));
248 return NT_STATUS_ACCOUNT_LOCKED_OUT;
251 /* You can only do an interactive login to normal accounts */
252 if (user_info->flags & USER_INFO_INTERACTIVE_LOGON) {
253 if (!(acct_flags & ACB_NORMAL)) {
254 return NT_STATUS_NO_SUCH_USER;
258 nt_status = samdb_result_passwords(mem_ctx, msgs[0], &lm_pwd, &nt_pwd);
259 NT_STATUS_NOT_OK_RETURN(nt_status);
261 nt_status = authsam_password_ok(auth_context, mem_ctx,
262 acct_flags, lm_pwd, nt_pwd,
263 user_info, user_sess_key, lm_sess_key);
264 NT_STATUS_NOT_OK_RETURN(nt_status);
266 nt_status = authsam_account_ok(mem_ctx, sam_ctx,
267 user_info->logon_parameters,
268 msgs[0],
269 msgs_domain_ref[0],
270 user_info->workstation_name,
271 user_info->mapped.account_name);
273 return nt_status;
278 static NTSTATUS authsam_check_password_internals(struct auth_method_context *ctx,
279 TALLOC_CTX *mem_ctx,
280 const char *domain,
281 const struct auth_usersupplied_info *user_info,
282 struct auth_serversupplied_info **server_info)
284 NTSTATUS nt_status;
285 const char *account_name = user_info->mapped.account_name;
286 struct ldb_message **msgs;
287 struct ldb_message **domain_ref_msgs;
288 struct ldb_context *sam_ctx;
289 DATA_BLOB user_sess_key, lm_sess_key;
290 TALLOC_CTX *tmp_ctx;
292 if (!account_name || !*account_name) {
293 /* 'not for me' */
294 return NT_STATUS_NOT_IMPLEMENTED;
297 tmp_ctx = talloc_new(mem_ctx);
298 if (!tmp_ctx) {
299 return NT_STATUS_NO_MEMORY;
302 sam_ctx = samdb_connect(tmp_ctx, system_session(mem_ctx));
303 if (sam_ctx == NULL) {
304 talloc_free(tmp_ctx);
305 return NT_STATUS_INVALID_SYSTEM_SERVICE;
308 nt_status = authsam_search_account(tmp_ctx, sam_ctx, account_name, domain, &msgs, &domain_ref_msgs);
309 if (!NT_STATUS_IS_OK(nt_status)) {
310 talloc_free(tmp_ctx);
311 return nt_status;
314 nt_status = authsam_authenticate(ctx->auth_ctx, tmp_ctx, sam_ctx, msgs, domain_ref_msgs, user_info,
315 &user_sess_key, &lm_sess_key);
316 if (!NT_STATUS_IS_OK(nt_status)) {
317 talloc_free(tmp_ctx);
318 return nt_status;
321 nt_status = authsam_make_server_info(tmp_ctx, sam_ctx, msgs[0], domain_ref_msgs[0],
322 user_sess_key, lm_sess_key,
323 server_info);
324 if (!NT_STATUS_IS_OK(nt_status)) {
325 talloc_free(tmp_ctx);
326 return nt_status;
329 talloc_steal(mem_ctx, *server_info);
330 talloc_free(tmp_ctx);
332 return NT_STATUS_OK;
335 static NTSTATUS authsam_ignoredomain_want_check(struct auth_method_context *ctx,
336 TALLOC_CTX *mem_ctx,
337 const struct auth_usersupplied_info *user_info)
339 if (!user_info->mapped.account_name || !*user_info->mapped.account_name) {
340 return NT_STATUS_NOT_IMPLEMENTED;
343 return NT_STATUS_OK;
346 static NTSTATUS authsam_ignoredomain_check_password(struct auth_method_context *ctx,
347 TALLOC_CTX *mem_ctx,
348 const struct auth_usersupplied_info *user_info,
349 struct auth_serversupplied_info **server_info)
351 return authsam_check_password_internals(ctx, mem_ctx, NULL, user_info, server_info);
354 /****************************************************************************
355 Check SAM security (above) but with a few extra checks.
356 ****************************************************************************/
357 static NTSTATUS authsam_want_check(struct auth_method_context *ctx,
358 TALLOC_CTX *mem_ctx,
359 const struct auth_usersupplied_info *user_info)
361 BOOL is_local_name, is_my_domain;
363 if (!user_info->mapped.account_name || !*user_info->mapped.account_name) {
364 return NT_STATUS_NOT_IMPLEMENTED;
367 is_local_name = is_myname(user_info->mapped.domain_name);
368 is_my_domain = strequal(user_info->mapped.domain_name, lp_workgroup());
370 /* check whether or not we service this domain/workgroup name */
371 switch (lp_server_role()) {
372 case ROLE_STANDALONE:
373 return NT_STATUS_OK;
375 case ROLE_DOMAIN_MEMBER:
376 if (!is_local_name) {
377 DEBUG(6,("authsam_check_password: %s is not one of my local names (DOMAIN_MEMBER)\n",
378 user_info->mapped.domain_name));
379 return NT_STATUS_NOT_IMPLEMENTED;
381 return NT_STATUS_OK;
383 case ROLE_DOMAIN_PDC:
384 case ROLE_DOMAIN_BDC:
385 if (!is_local_name && !is_my_domain) {
386 DEBUG(6,("authsam_check_password: %s is not one of my local names or domain name (DC)\n",
387 user_info->mapped.domain_name));
388 return NT_STATUS_NOT_IMPLEMENTED;
390 return NT_STATUS_OK;
393 DEBUG(6,("authsam_check_password: lp_server_role() has an undefined value\n"));
394 return NT_STATUS_NOT_IMPLEMENTED;
397 /****************************************************************************
398 Check SAM security (above) but with a few extra checks.
399 ****************************************************************************/
400 static NTSTATUS authsam_check_password(struct auth_method_context *ctx,
401 TALLOC_CTX *mem_ctx,
402 const struct auth_usersupplied_info *user_info,
403 struct auth_serversupplied_info **server_info)
405 const char *domain;
407 /* check whether or not we service this domain/workgroup name */
408 switch (lp_server_role()) {
409 case ROLE_STANDALONE:
410 case ROLE_DOMAIN_MEMBER:
411 domain = lp_netbios_name();
412 break;
414 case ROLE_DOMAIN_PDC:
415 case ROLE_DOMAIN_BDC:
416 domain = lp_workgroup();
417 break;
419 default:
420 return NT_STATUS_NO_SUCH_USER;
423 return authsam_check_password_internals(ctx, mem_ctx, domain, user_info, server_info);
426 static const struct auth_operations sam_ignoredomain_ops = {
427 .name = "sam_ignoredomain",
428 .get_challenge = auth_get_challenge_not_implemented,
429 .want_check = authsam_ignoredomain_want_check,
430 .check_password = authsam_ignoredomain_check_password
433 static const struct auth_operations sam_ops = {
434 .name = "sam",
435 .get_challenge = auth_get_challenge_not_implemented,
436 .want_check = authsam_want_check,
437 .check_password = authsam_check_password
440 NTSTATUS auth_sam_init(void)
442 NTSTATUS ret;
444 ret = auth_register(&sam_ops);
445 if (!NT_STATUS_IS_OK(ret)) {
446 DEBUG(0,("Failed to register 'sam' auth backend!\n"));
447 return ret;
450 ret = auth_register(&sam_ignoredomain_ops);
451 if (!NT_STATUS_IS_OK(ret)) {
452 DEBUG(0,("Failed to register 'sam_ignoredomain' auth backend!\n"));
453 return ret;
456 return ret;