s3-spoolss: Migrated spoolss_DeletePrinterDataEx to the winreg functions.
[Samba/bjacke.git] / source4 / auth / sam.c
blob7a776b9b745104d671564da5b8c1e47e0d8ac7c0
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
7 Copyright (C) Matthias Dieter Wallnöfer 2009
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "system/time.h"
25 #include "auth/auth.h"
26 #include <ldb.h>
27 #include "dsdb/samdb/samdb.h"
28 #include "libcli/security/security.h"
29 #include "auth/auth_sam.h"
30 #include "dsdb/common/util.h"
32 #define KRBTGT_ATTRS \
33 /* required for the krb5 kdc */ \
34 "objectClass", \
35 "sAMAccountName", \
36 "userPrincipalName", \
37 "servicePrincipalName", \
38 "msDS-KeyVersionNumber", \
39 "msDS-SupportedEncryptionTypes", \
40 "supplementalCredentials", \
42 /* passwords */ \
43 "dBCSPwd", \
44 "unicodePwd", \
46 "userAccountControl", \
47 "objectSid", \
49 "pwdLastSet", \
50 "accountExpires"
52 const char *krbtgt_attrs[] = {
53 KRBTGT_ATTRS
56 const char *server_attrs[] = {
57 KRBTGT_ATTRS
60 const char *user_attrs[] = {
61 KRBTGT_ATTRS,
63 "logonHours",
65 /* check 'allowed workstations' */
66 "userWorkstations",
68 /* required for server_info, not access control: */
69 "displayName",
70 "scriptPath",
71 "profilePath",
72 "homeDirectory",
73 "homeDrive",
74 "lastLogon",
75 "lastLogoff",
76 "accountExpires",
77 "badPwdCount",
78 "logonCount",
79 "primaryGroupID",
80 "memberOf",
81 NULL,
84 /****************************************************************************
85 Check if a user is allowed to logon at this time. Note this is the
86 servers local time, as logon hours are just specified as a weekly
87 bitmask.
88 ****************************************************************************/
90 static bool logon_hours_ok(struct ldb_message *msg, const char *name_for_logs)
92 /* In logon hours first bit is Sunday from 12AM to 1AM */
93 const struct ldb_val *hours;
94 struct tm *utctime;
95 time_t lasttime;
96 const char *asct;
97 uint8_t bitmask, bitpos;
99 hours = ldb_msg_find_ldb_val(msg, "logonHours");
100 if (!hours) {
101 DEBUG(5,("logon_hours_ok: No hours restrictions for user %s\n", name_for_logs));
102 return true;
105 if (hours->length != 168/8) {
106 DEBUG(5,("logon_hours_ok: malformed logon hours restrictions for user %s\n", name_for_logs));
107 return true;
110 lasttime = time(NULL);
111 utctime = gmtime(&lasttime);
112 if (!utctime) {
113 DEBUG(1, ("logon_hours_ok: failed to get gmtime. Failing logon for user %s\n",
114 name_for_logs));
115 return false;
118 /* find the corresponding byte and bit */
119 bitpos = (utctime->tm_wday * 24 + utctime->tm_hour) % 168;
120 bitmask = 1 << (bitpos % 8);
122 if (! (hours->data[bitpos/8] & bitmask)) {
123 struct tm *t = localtime(&lasttime);
124 if (!t) {
125 asct = "INVALID TIME";
126 } else {
127 asct = asctime(t);
128 if (!asct) {
129 asct = "INVALID TIME";
133 DEBUG(1, ("logon_hours_ok: Account for user %s not allowed to "
134 "logon at this time (%s).\n",
135 name_for_logs, asct ));
136 return false;
139 asct = asctime(utctime);
140 DEBUG(5,("logon_hours_ok: user %s allowed to logon at this time (%s)\n",
141 name_for_logs, asct ? asct : "UNKNOWN TIME" ));
143 return true;
146 /****************************************************************************
147 Do a specific test for a SAM_ACCOUNT being valid for this connection
148 (ie not disabled, expired and the like).
149 ****************************************************************************/
150 _PUBLIC_ NTSTATUS authsam_account_ok(TALLOC_CTX *mem_ctx,
151 struct ldb_context *sam_ctx,
152 uint32_t logon_parameters,
153 struct ldb_dn *domain_dn,
154 struct ldb_message *msg,
155 const char *logon_workstation,
156 const char *name_for_logs,
157 bool allow_domain_trust,
158 bool password_change)
160 uint16_t acct_flags;
161 const char *workstation_list;
162 NTTIME acct_expiry;
163 NTTIME must_change_time;
165 NTTIME now;
166 DEBUG(4,("authsam_account_ok: Checking SMB password for user %s\n", name_for_logs));
168 acct_flags = samdb_result_acct_flags(sam_ctx, mem_ctx, msg, domain_dn);
170 acct_expiry = samdb_result_account_expires(msg);
172 /* Check for when we must change this password, taking the
173 * userAccountControl flags into account */
174 must_change_time = samdb_result_force_password_change(sam_ctx, mem_ctx,
175 domain_dn, msg);
177 workstation_list = samdb_result_string(msg, "userWorkstations", NULL);
179 /* Quit if the account was disabled. */
180 if (acct_flags & ACB_DISABLED) {
181 DEBUG(1,("authsam_account_ok: Account for user '%s' was disabled.\n", name_for_logs));
182 return NT_STATUS_ACCOUNT_DISABLED;
185 /* Quit if the account was locked out. */
186 if (acct_flags & ACB_AUTOLOCK) {
187 DEBUG(1,("authsam_account_ok: Account for user %s was locked out.\n", name_for_logs));
188 return NT_STATUS_ACCOUNT_LOCKED_OUT;
191 /* Test account expire time */
192 unix_to_nt_time(&now, time(NULL));
193 if (now > acct_expiry) {
194 DEBUG(1,("authsam_account_ok: Account for user '%s' has expired.\n", name_for_logs));
195 DEBUG(3,("authsam_account_ok: Account expired at '%s'.\n",
196 nt_time_string(mem_ctx, acct_expiry)));
197 return NT_STATUS_ACCOUNT_EXPIRED;
200 /* check for immediate expiry "must change at next logon" (but not if this is a password change request) */
201 if ((must_change_time == 0) && !password_change) {
202 DEBUG(1,("sam_account_ok: Account for user '%s' password must change!.\n",
203 name_for_logs));
204 return NT_STATUS_PASSWORD_MUST_CHANGE;
207 /* check for expired password (but not if this is a password change request) */
208 if ((must_change_time < now) && !password_change) {
209 DEBUG(1,("sam_account_ok: Account for user '%s' password expired!.\n",
210 name_for_logs));
211 DEBUG(1,("sam_account_ok: Password expired at '%s' unix time.\n",
212 nt_time_string(mem_ctx, must_change_time)));
213 return NT_STATUS_PASSWORD_EXPIRED;
216 /* Test workstation. Workstation list is comma separated. */
217 if (logon_workstation && workstation_list && *workstation_list) {
218 bool invalid_ws = true;
219 int i;
220 const char **workstations = (const char **)str_list_make(mem_ctx, workstation_list, ",");
222 for (i = 0; workstations && workstations[i]; i++) {
223 DEBUG(10,("sam_account_ok: checking for workstation match '%s' and '%s'\n",
224 workstations[i], logon_workstation));
226 if (strequal(workstations[i], logon_workstation)) {
227 invalid_ws = false;
228 break;
232 talloc_free(workstations);
234 if (invalid_ws) {
235 return NT_STATUS_INVALID_WORKSTATION;
239 if (!logon_hours_ok(msg, name_for_logs)) {
240 return NT_STATUS_INVALID_LOGON_HOURS;
243 if (!allow_domain_trust) {
244 if (acct_flags & ACB_DOMTRUST) {
245 DEBUG(2,("sam_account_ok: Domain trust account %s denied by server\n", name_for_logs));
246 return NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT;
249 if (!(logon_parameters & MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT)) {
250 if (acct_flags & ACB_SVRTRUST) {
251 DEBUG(2,("sam_account_ok: Server trust account %s denied by server\n", name_for_logs));
252 return NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT;
255 if (!(logon_parameters & MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT)) {
256 /* TODO: this fails with current solaris client. We
257 need to work with Gordon to work out why */
258 if (acct_flags & ACB_WSTRUST) {
259 DEBUG(4,("sam_account_ok: Wksta trust account %s denied by server\n", name_for_logs));
260 return NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT;
264 return NT_STATUS_OK;
267 /* This function tests if a SID structure "sids" contains the SID "sid" */
268 static bool sids_contains_sid(const struct dom_sid **sids,
269 const unsigned int num_sids,
270 const struct dom_sid *sid)
272 unsigned int i;
274 for (i = 0; i < num_sids; i++) {
275 if (dom_sid_equal(sids[i], sid))
276 return true;
278 return false;
283 * This function generates the transitive closure of a given SAM object "dn_val"
284 * (it basically expands nested memberships).
285 * If the object isn't located in the "res_sids" structure yet and the
286 * "only_childs" flag is false, we add it to "res_sids".
287 * Then we've always to consider the "memberOf" attributes. We invoke the
288 * function recursively on each of it with the "only_childs" flag set to
289 * "false".
290 * The "only_childs" flag is particularly useful if you have a user object and
291 * want to include all it's groups (referenced with "memberOf") but not itself
292 * or considering if that object matches the filter.
294 * At the beginning "res_sids" should reference to a NULL pointer.
296 NTSTATUS authsam_expand_nested_groups(struct ldb_context *sam_ctx,
297 struct ldb_val *dn_val, const bool only_childs, const char *filter,
298 TALLOC_CTX *res_sids_ctx, struct dom_sid ***res_sids,
299 unsigned int *num_res_sids)
301 const char * const attrs[] = { "memberOf", NULL };
302 unsigned int i;
303 int ret;
304 bool already_there;
305 struct ldb_dn *dn;
306 struct dom_sid sid;
307 TALLOC_CTX *tmp_ctx;
308 struct ldb_result *res;
309 NTSTATUS status;
310 const struct ldb_message_element *el;
312 if (*res_sids == NULL) {
313 *num_res_sids = 0;
316 tmp_ctx = talloc_new(res_sids_ctx);
318 dn = ldb_dn_from_ldb_val(tmp_ctx, sam_ctx, dn_val);
319 if (dn == NULL) {
320 talloc_free(tmp_ctx);
321 return NT_STATUS_INTERNAL_DB_CORRUPTION;
324 status = dsdb_get_extended_dn_sid(dn, &sid, "SID");
325 if (!NT_STATUS_IS_OK(status)) {
326 DEBUG(0, (__location__ ": when parsing DN %s we failed to find our SID component, so we cannot calculate the group token: %s\n",
327 ldb_dn_get_extended_linearized(tmp_ctx, dn, 1),
328 nt_errstr(status)));
329 talloc_free(tmp_ctx);
330 return NT_STATUS_INTERNAL_DB_CORRUPTION;
333 if (only_childs) {
334 ret = dsdb_search_dn(sam_ctx, tmp_ctx, &res, dn, attrs,
335 DSDB_SEARCH_SHOW_EXTENDED_DN);
336 } else {
337 /* This is an O(n^2) linear search */
338 already_there = sids_contains_sid((const struct dom_sid**) *res_sids,
339 *num_res_sids, &sid);
340 if (already_there) {
341 return NT_STATUS_OK;
344 ret = dsdb_search(sam_ctx, tmp_ctx, &res, dn, LDB_SCOPE_BASE,
345 attrs, DSDB_SEARCH_SHOW_EXTENDED_DN, "%s",
346 filter);
349 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
350 talloc_free(tmp_ctx);
351 return NT_STATUS_OK;
354 if (ret != LDB_SUCCESS) {
355 talloc_free(tmp_ctx);
356 return NT_STATUS_INTERNAL_DB_CORRUPTION;
359 /* We may get back 0 results, if the SID didn't match the filter - such as it wasn't a domain group, for example */
360 if (res->count != 1) {
361 talloc_free(tmp_ctx);
362 return NT_STATUS_OK;
365 /* We only apply this test once we know the SID matches the filter */
366 if (!only_childs) {
367 *res_sids = talloc_realloc(res_sids_ctx, *res_sids,
368 struct dom_sid *, *num_res_sids + 1);
369 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(*res_sids, tmp_ctx);
370 (*res_sids)[*num_res_sids] = dom_sid_dup(*res_sids, &sid);
371 NT_STATUS_HAVE_NO_MEMORY_AND_FREE((*res_sids)[*num_res_sids], tmp_ctx);
372 ++(*num_res_sids);
375 el = ldb_msg_find_element(res->msgs[0], "memberOf");
377 for (i = 0; el && i < el->num_values; i++) {
378 status = authsam_expand_nested_groups(sam_ctx, &el->values[i],
379 false, filter, res_sids_ctx, res_sids, num_res_sids);
380 if (!NT_STATUS_IS_OK(status)) {
381 talloc_free(res);
382 talloc_free(tmp_ctx);
383 return status;
387 talloc_free(tmp_ctx);
389 return NT_STATUS_OK;
392 _PUBLIC_ NTSTATUS authsam_make_server_info(TALLOC_CTX *mem_ctx,
393 struct ldb_context *sam_ctx,
394 const char *netbios_name,
395 const char *domain_name,
396 struct ldb_dn *domain_dn,
397 struct ldb_message *msg,
398 DATA_BLOB user_sess_key,
399 DATA_BLOB lm_sess_key,
400 struct auth_serversupplied_info **_server_info)
402 NTSTATUS status;
403 struct auth_serversupplied_info *server_info;
404 const char *str, *filter;
405 /* SIDs for the account and his primary group */
406 struct dom_sid *account_sid;
407 struct dom_sid *primary_group_sid;
408 const char *primary_group_string;
409 const char *primary_group_dn;
410 DATA_BLOB primary_group_blob;
411 /* SID structures for the expanded group memberships */
412 struct dom_sid **groupSIDs = NULL;
413 unsigned int num_groupSIDs = 0, i;
414 struct dom_sid *domain_sid;
415 TALLOC_CTX *tmp_ctx;
416 struct ldb_message_element *el;
418 server_info = talloc(mem_ctx, struct auth_serversupplied_info);
419 NT_STATUS_HAVE_NO_MEMORY(server_info);
421 tmp_ctx = talloc_new(server_info);
422 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info, server_info);
424 account_sid = samdb_result_dom_sid(server_info, msg, "objectSid");
425 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(account_sid, server_info);
427 status = dom_sid_split_rid(tmp_ctx, account_sid, &domain_sid, NULL);
428 if (!NT_STATUS_IS_OK(status)) {
429 talloc_free(server_info);
430 return status;
433 primary_group_sid = dom_sid_add_rid(server_info,
434 domain_sid,
435 samdb_result_uint(msg, "primaryGroupID", ~0));
436 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(primary_group_sid, server_info);
438 /* Filter out builtin groups from this token. We will search
439 * for builtin groups later, and not include them in the PAC
440 * on SamLogon validation info */
441 filter = talloc_asprintf(tmp_ctx, "(&(objectClass=group)(!(groupType:1.2.840.113556.1.4.803:=%u))(groupType:1.2.840.113556.1.4.803:=%u))", GROUP_TYPE_BUILTIN_LOCAL_GROUP, GROUP_TYPE_SECURITY_ENABLED);
442 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(filter, server_info);
444 primary_group_string = dom_sid_string(tmp_ctx, primary_group_sid);
445 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(primary_group_string, server_info);
447 primary_group_dn = talloc_asprintf(tmp_ctx, "<SID=%s>", primary_group_string);
448 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(primary_group_dn, server_info);
450 primary_group_blob = data_blob_string_const(primary_group_dn);
452 /* Expands the primary group - this function takes in
453 * memberOf-like values, so we fake one up with the
454 * <SID=S-...> format of DN and then let it expand
455 * them, as long as they meet the filter - so only
456 * domain groups, not builtin groups
458 * The primary group is still treated specially, so we set the
459 * 'only childs' flag to true
461 status = authsam_expand_nested_groups(sam_ctx, &primary_group_blob, true, filter,
462 server_info, &groupSIDs, &num_groupSIDs);
463 if (!NT_STATUS_IS_OK(status)) {
464 talloc_free(server_info);
465 return status;
468 /* Expands the additional groups */
469 el = ldb_msg_find_element(msg, "memberOf");
470 for (i = 0; el && i < el->num_values; i++) {
471 /* This function takes in memberOf values and expands
472 * them, as long as they meet the filter - so only
473 * domain groups, not builtin groups */
474 status = authsam_expand_nested_groups(sam_ctx, &el->values[i], false, filter,
475 server_info, &groupSIDs, &num_groupSIDs);
476 if (!NT_STATUS_IS_OK(status)) {
477 talloc_free(server_info);
478 return status;
482 server_info->account_sid = account_sid;
483 server_info->primary_group_sid = primary_group_sid;
485 server_info->domain_groups = groupSIDs;
486 server_info->n_domain_groups = num_groupSIDs;
488 server_info->account_name = talloc_steal(server_info,
489 samdb_result_string(msg, "sAMAccountName", NULL));
491 server_info->domain_name = talloc_strdup(server_info, domain_name);
492 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->domain_name,
493 server_info);
495 str = samdb_result_string(msg, "displayName", "");
496 server_info->full_name = talloc_strdup(server_info, str);
497 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->full_name, server_info);
499 str = samdb_result_string(msg, "scriptPath", "");
500 server_info->logon_script = talloc_strdup(server_info, str);
501 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->logon_script,
502 server_info);
504 str = samdb_result_string(msg, "profilePath", "");
505 server_info->profile_path = talloc_strdup(server_info, str);
506 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->profile_path,
507 server_info);
509 str = samdb_result_string(msg, "homeDirectory", "");
510 server_info->home_directory = talloc_strdup(server_info, str);
511 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->home_directory,
512 server_info);
514 str = samdb_result_string(msg, "homeDrive", "");
515 server_info->home_drive = talloc_strdup(server_info, str);
516 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->home_drive, server_info);
518 server_info->logon_server = talloc_strdup(server_info, netbios_name);
519 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->logon_server,
520 server_info);
522 server_info->last_logon = samdb_result_nttime(msg, "lastLogon", 0);
523 server_info->last_logoff = samdb_result_last_logoff(msg);
524 server_info->acct_expiry = samdb_result_account_expires(msg);
525 server_info->last_password_change = samdb_result_nttime(msg,
526 "pwdLastSet", 0);
527 server_info->allow_password_change
528 = samdb_result_allow_password_change(sam_ctx, mem_ctx,
529 domain_dn, msg, "pwdLastSet");
530 server_info->force_password_change
531 = samdb_result_force_password_change(sam_ctx, mem_ctx,
532 domain_dn, msg);
533 server_info->logon_count = samdb_result_uint(msg, "logonCount", 0);
534 server_info->bad_password_count = samdb_result_uint(msg, "badPwdCount",
537 server_info->acct_flags = samdb_result_acct_flags(sam_ctx, mem_ctx,
538 msg, domain_dn);
540 server_info->user_session_key = data_blob_talloc(server_info,
541 user_sess_key.data,
542 user_sess_key.length);
543 if (user_sess_key.data) {
544 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->user_session_key.data,
545 server_info);
547 server_info->lm_session_key = data_blob_talloc(server_info,
548 lm_sess_key.data,
549 lm_sess_key.length);
550 if (lm_sess_key.data) {
551 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->lm_session_key.data,
552 server_info);
555 server_info->authenticated = true;
557 talloc_free(tmp_ctx);
558 *_server_info = server_info;
560 return NT_STATUS_OK;
563 NTSTATUS sam_get_results_principal(struct ldb_context *sam_ctx,
564 TALLOC_CTX *mem_ctx, const char *principal,
565 const char **attrs,
566 struct ldb_dn **domain_dn,
567 struct ldb_message **msg)
569 struct ldb_dn *user_dn;
570 NTSTATUS nt_status;
571 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
572 int ret;
574 if (!tmp_ctx) {
575 return NT_STATUS_NO_MEMORY;
578 nt_status = crack_user_principal_name(sam_ctx, tmp_ctx, principal,
579 &user_dn, domain_dn);
580 if (!NT_STATUS_IS_OK(nt_status)) {
581 talloc_free(tmp_ctx);
582 return nt_status;
585 /* pull the user attributes */
586 ret = dsdb_search_one(sam_ctx, tmp_ctx, msg, user_dn,
587 LDB_SCOPE_BASE, attrs, DSDB_SEARCH_SHOW_EXTENDED_DN, "(objectClass=*)");
588 if (ret != LDB_SUCCESS) {
589 talloc_free(tmp_ctx);
590 return NT_STATUS_INTERNAL_DB_CORRUPTION;
592 talloc_steal(mem_ctx, *msg);
593 talloc_steal(mem_ctx, *domain_dn);
594 talloc_free(tmp_ctx);
596 return NT_STATUS_OK;