Created net gpo and new libgpo for samba 4. This adds the init function which finds...
[Samba.git] / source4 / auth / sam.c
blob894ba37fd5856599b0e4ff517d55743aea2bb4f3
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 "supplementalCredentials", \
41 /* passwords */ \
42 "dBCSPwd", \
43 "unicodePwd", \
45 "userAccountControl", \
46 "objectSid", \
48 "pwdLastSet", \
49 "accountExpires"
51 const char *krbtgt_attrs[] = {
52 KRBTGT_ATTRS
55 const char *server_attrs[] = {
56 KRBTGT_ATTRS
59 const char *user_attrs[] = {
60 KRBTGT_ATTRS,
62 "logonHours",
64 /* check 'allowed workstations' */
65 "userWorkstations",
67 /* required for server_info, not access control: */
68 "displayName",
69 "scriptPath",
70 "profilePath",
71 "homeDirectory",
72 "homeDrive",
73 "lastLogon",
74 "lastLogoff",
75 "accountExpires",
76 "badPwdCount",
77 "logonCount",
78 "primaryGroupID",
79 "memberOf",
80 NULL,
83 /****************************************************************************
84 Check if a user is allowed to logon at this time. Note this is the
85 servers local time, as logon hours are just specified as a weekly
86 bitmask.
87 ****************************************************************************/
89 static bool logon_hours_ok(struct ldb_message *msg, const char *name_for_logs)
91 /* In logon hours first bit is Sunday from 12AM to 1AM */
92 const struct ldb_val *hours;
93 struct tm *utctime;
94 time_t lasttime;
95 const char *asct;
96 uint8_t bitmask, bitpos;
98 hours = ldb_msg_find_ldb_val(msg, "logonHours");
99 if (!hours) {
100 DEBUG(5,("logon_hours_ok: No hours restrictions for user %s\n", name_for_logs));
101 return true;
104 if (hours->length != 168/8) {
105 DEBUG(5,("logon_hours_ok: malformed logon hours restrictions for user %s\n", name_for_logs));
106 return true;
109 lasttime = time(NULL);
110 utctime = gmtime(&lasttime);
111 if (!utctime) {
112 DEBUG(1, ("logon_hours_ok: failed to get gmtime. Failing logon for user %s\n",
113 name_for_logs));
114 return false;
117 /* find the corresponding byte and bit */
118 bitpos = (utctime->tm_wday * 24 + utctime->tm_hour) % 168;
119 bitmask = 1 << (bitpos % 8);
121 if (! (hours->data[bitpos/8] & bitmask)) {
122 struct tm *t = localtime(&lasttime);
123 if (!t) {
124 asct = "INVALID TIME";
125 } else {
126 asct = asctime(t);
127 if (!asct) {
128 asct = "INVALID TIME";
132 DEBUG(1, ("logon_hours_ok: Account for user %s not allowed to "
133 "logon at this time (%s).\n",
134 name_for_logs, asct ));
135 return false;
138 asct = asctime(utctime);
139 DEBUG(5,("logon_hours_ok: user %s allowed to logon at this time (%s)\n",
140 name_for_logs, asct ? asct : "UNKNOWN TIME" ));
142 return true;
145 /****************************************************************************
146 Do a specific test for a SAM_ACCOUNT being valid for this connection
147 (ie not disabled, expired and the like).
148 ****************************************************************************/
149 _PUBLIC_ NTSTATUS authsam_account_ok(TALLOC_CTX *mem_ctx,
150 struct ldb_context *sam_ctx,
151 uint32_t logon_parameters,
152 struct ldb_dn *domain_dn,
153 struct ldb_message *msg,
154 const char *logon_workstation,
155 const char *name_for_logs,
156 bool allow_domain_trust,
157 bool password_change)
159 uint16_t acct_flags;
160 const char *workstation_list;
161 NTTIME acct_expiry;
162 NTTIME must_change_time;
164 NTTIME now;
165 DEBUG(4,("authsam_account_ok: Checking SMB password for user %s\n", name_for_logs));
167 acct_flags = samdb_result_acct_flags(sam_ctx, mem_ctx, msg, domain_dn);
169 acct_expiry = samdb_result_account_expires(msg);
171 /* Check for when we must change this password, taking the
172 * userAccountControl flags into account */
173 must_change_time = samdb_result_force_password_change(sam_ctx, mem_ctx,
174 domain_dn, msg);
176 workstation_list = samdb_result_string(msg, "userWorkstations", NULL);
178 /* Quit if the account was disabled. */
179 if (acct_flags & ACB_DISABLED) {
180 DEBUG(1,("authsam_account_ok: Account for user '%s' was disabled.\n", name_for_logs));
181 return NT_STATUS_ACCOUNT_DISABLED;
184 /* Quit if the account was locked out. */
185 if (acct_flags & ACB_AUTOLOCK) {
186 DEBUG(1,("authsam_account_ok: Account for user %s was locked out.\n", name_for_logs));
187 return NT_STATUS_ACCOUNT_LOCKED_OUT;
190 /* Test account expire time */
191 unix_to_nt_time(&now, time(NULL));
192 if (now > acct_expiry) {
193 DEBUG(1,("authsam_account_ok: Account for user '%s' has expired.\n", name_for_logs));
194 DEBUG(3,("authsam_account_ok: Account expired at '%s'.\n",
195 nt_time_string(mem_ctx, acct_expiry)));
196 return NT_STATUS_ACCOUNT_EXPIRED;
199 /* check for immediate expiry "must change at next logon" (but not if this is a password change request) */
200 if ((must_change_time == 0) && !password_change) {
201 DEBUG(1,("sam_account_ok: Account for user '%s' password must change!.\n",
202 name_for_logs));
203 return NT_STATUS_PASSWORD_MUST_CHANGE;
206 /* check for expired password (but not if this is a password change request) */
207 if ((must_change_time < now) && !password_change) {
208 DEBUG(1,("sam_account_ok: Account for user '%s' password expired!.\n",
209 name_for_logs));
210 DEBUG(1,("sam_account_ok: Password expired at '%s' unix time.\n",
211 nt_time_string(mem_ctx, must_change_time)));
212 return NT_STATUS_PASSWORD_EXPIRED;
215 /* Test workstation. Workstation list is comma separated. */
216 if (logon_workstation && workstation_list && *workstation_list) {
217 bool invalid_ws = true;
218 int i;
219 const char **workstations = (const char **)str_list_make(mem_ctx, workstation_list, ",");
221 for (i = 0; workstations && workstations[i]; i++) {
222 DEBUG(10,("sam_account_ok: checking for workstation match '%s' and '%s'\n",
223 workstations[i], logon_workstation));
225 if (strequal(workstations[i], logon_workstation)) {
226 invalid_ws = false;
227 break;
231 talloc_free(workstations);
233 if (invalid_ws) {
234 return NT_STATUS_INVALID_WORKSTATION;
238 if (!logon_hours_ok(msg, name_for_logs)) {
239 return NT_STATUS_INVALID_LOGON_HOURS;
242 if (!allow_domain_trust) {
243 if (acct_flags & ACB_DOMTRUST) {
244 DEBUG(2,("sam_account_ok: Domain trust account %s denied by server\n", name_for_logs));
245 return NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT;
248 if (!(logon_parameters & MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT)) {
249 if (acct_flags & ACB_SVRTRUST) {
250 DEBUG(2,("sam_account_ok: Server trust account %s denied by server\n", name_for_logs));
251 return NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT;
254 if (!(logon_parameters & MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT)) {
255 /* TODO: this fails with current solaris client. We
256 need to work with Gordon to work out why */
257 if (acct_flags & ACB_WSTRUST) {
258 DEBUG(4,("sam_account_ok: Wksta trust account %s denied by server\n", name_for_logs));
259 return NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT;
263 return NT_STATUS_OK;
266 /* This function tests if a SID structure "sids" contains the SID "sid" */
267 static bool sids_contains_sid(const struct dom_sid **sids,
268 const unsigned int num_sids,
269 const struct dom_sid *sid)
271 unsigned int i;
273 for (i = 0; i < num_sids; i++) {
274 if (dom_sid_equal(sids[i], sid))
275 return true;
277 return false;
282 * This function generates the transitive closure of a given SID "sid" (it
283 * basically expands nested groups of a SID).
284 * If the SID isn't located in the "res_sids" structure yet and the
285 * "only_childs" flag is negative, we add it to "res_sids".
286 * Then we've always to consider the "memberOf" attributes. We invoke the
287 * function recursively on each item of it with the "only_childs" flag set to
288 * "false".
289 * The "only_childs" flag is particularly useful if you have a user SID and
290 * want to include all his groups (referenced with "memberOf") without his SID
291 * itself, or considering if that SID matches the filter
293 * At the beginning "res_sids" should reference to a NULL pointer.
295 NTSTATUS authsam_expand_nested_groups(struct ldb_context *sam_ctx,
296 struct ldb_val *dn_val, const bool only_childs, const char *filter,
297 TALLOC_CTX *res_sids_ctx, struct dom_sid ***res_sids,
298 unsigned int *num_res_sids)
300 const char * const attrs[] = { "memberOf", NULL };
301 unsigned int i;
302 int ret;
303 bool already_there;
304 struct ldb_dn *dn;
305 struct dom_sid sid;
306 TALLOC_CTX *tmp_ctx;
307 struct ldb_result *res;
308 NTSTATUS status;
309 const struct ldb_message_element *el;
311 if (*res_sids == NULL) {
312 *num_res_sids = 0;
315 tmp_ctx = talloc_new(res_sids_ctx);
317 dn = ldb_dn_from_ldb_val(tmp_ctx, sam_ctx, dn_val);
318 if (dn == NULL) {
319 talloc_free(tmp_ctx);
320 return NT_STATUS_INTERNAL_DB_CORRUPTION;
322 status = dsdb_get_extended_dn_sid(dn, &sid, "SID");
323 if (!NT_STATUS_IS_OK(status)) {
324 DEBUG(0, (__location__ "when parsing DN %s we failed to find or parse SID component, so we cannot calculate the group token: %s",
325 ldb_dn_get_extended_linearized(tmp_ctx, dn, 1),
326 nt_errstr(status)));
327 talloc_free(tmp_ctx);
328 return NT_STATUS_INTERNAL_DB_CORRUPTION;
331 if (only_childs) {
332 ret = dsdb_search(sam_ctx, tmp_ctx, &res, dn, LDB_SCOPE_BASE, attrs, DSDB_SEARCH_SHOW_EXTENDED_DN, NULL);
333 } else {
334 /* This is an O(n^2) linear search */
335 already_there = sids_contains_sid((const struct dom_sid**) *res_sids,
336 *num_res_sids, &sid);
337 if (already_there) {
338 return NT_STATUS_OK;
341 ret = dsdb_search(sam_ctx, tmp_ctx, &res, dn, LDB_SCOPE_BASE, attrs, DSDB_SEARCH_SHOW_EXTENDED_DN, "%s", filter);
344 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
345 talloc_free(tmp_ctx);
346 return NT_STATUS_OK;
349 if (ret != LDB_SUCCESS) {
350 talloc_free(tmp_ctx);
351 return NT_STATUS_INTERNAL_DB_CORRUPTION;
354 /* We may get back 0 results, if the SID didn't match the filter - such as it wasn't a domain group, for example */
355 if (res->count != 1) {
356 talloc_free(tmp_ctx);
357 return NT_STATUS_OK;
360 /* We only apply this test once we know the SID matches the filter */
361 if (!only_childs) {
362 *res_sids = talloc_realloc(res_sids_ctx, *res_sids,
363 struct dom_sid *, *num_res_sids + 1);
364 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(*res_sids, tmp_ctx);
365 (*res_sids)[*num_res_sids] = dom_sid_dup(*res_sids, &sid);
366 NT_STATUS_HAVE_NO_MEMORY_AND_FREE((*res_sids)[*num_res_sids], tmp_ctx);
367 ++(*num_res_sids);
370 el = ldb_msg_find_element(res->msgs[0], "memberOf");
372 for (i = 0; el && i < el->num_values; i++) {
373 status = authsam_expand_nested_groups(sam_ctx, &el->values[i],
374 false, filter, res_sids_ctx, res_sids, num_res_sids);
375 if (!NT_STATUS_IS_OK(status)) {
376 talloc_free(res);
377 talloc_free(tmp_ctx);
378 return status;
382 talloc_free(tmp_ctx);
384 return NT_STATUS_OK;
387 _PUBLIC_ NTSTATUS authsam_make_server_info(TALLOC_CTX *mem_ctx,
388 struct ldb_context *sam_ctx,
389 const char *netbios_name,
390 const char *domain_name,
391 struct ldb_dn *domain_dn,
392 struct ldb_message *msg,
393 DATA_BLOB user_sess_key,
394 DATA_BLOB lm_sess_key,
395 struct auth_serversupplied_info **_server_info)
397 NTSTATUS status;
398 struct auth_serversupplied_info *server_info;
399 const char *str, *filter;
400 /* SIDs for the account and his primary group */
401 struct dom_sid *account_sid;
402 struct dom_sid *primary_group_sid;
403 const char *primary_group_string;
404 const char *primary_group_dn;
405 DATA_BLOB primary_group_blob;
406 /* SID structures for the expanded group memberships */
407 struct dom_sid **groupSIDs = NULL;
408 unsigned int num_groupSIDs = 0, i;
409 struct dom_sid *domain_sid;
410 TALLOC_CTX *tmp_ctx;
411 struct ldb_message_element *el;
413 server_info = talloc(mem_ctx, struct auth_serversupplied_info);
414 NT_STATUS_HAVE_NO_MEMORY(server_info);
416 tmp_ctx = talloc_new(server_info);
417 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info, server_info);
419 account_sid = samdb_result_dom_sid(server_info, msg, "objectSid");
420 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(account_sid, server_info);
422 status = dom_sid_split_rid(tmp_ctx, account_sid, &domain_sid, NULL);
423 if (!NT_STATUS_IS_OK(status)) {
424 talloc_free(server_info);
425 return status;
428 primary_group_sid = dom_sid_add_rid(server_info,
429 domain_sid,
430 samdb_result_uint(msg, "primaryGroupID", ~0));
431 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(primary_group_sid, server_info);
433 /* Filter out builtin groups from this token. We will search
434 * for builtin groups later, and not include them in the PAC
435 * on SamLogon validation info */
436 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);
437 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(filter, server_info);
439 primary_group_string = dom_sid_string(tmp_ctx, primary_group_sid);
440 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(primary_group_string, server_info);
442 primary_group_dn = talloc_asprintf(tmp_ctx, "<SID=%s>", primary_group_string);
443 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(primary_group_dn, server_info);
445 primary_group_blob = data_blob_string_const(primary_group_dn);
447 /* Expands the primary group - this function takes in
448 * memberOf-like values, so we fake one up with the
449 * <SID=S-...> format of DN and then let it expand
450 * them, as long as they meet the filter - so only
451 * domain groups, not builtin groups
453 * The primary group is still treated specially, so we set the
454 * 'only childs' flag to true
456 status = authsam_expand_nested_groups(sam_ctx, &primary_group_blob, true, filter,
457 server_info, &groupSIDs, &num_groupSIDs);
458 if (!NT_STATUS_IS_OK(status)) {
459 talloc_free(server_info);
460 return status;
463 /* Expands the additional groups */
464 el = ldb_msg_find_element(msg, "memberOf");
465 for (i = 0; el && i < el->num_values; i++) {
466 /* This function takes in memberOf values and expands
467 * them, as long as they meet the filter - so only
468 * domain groups, not builtin groups */
469 status = authsam_expand_nested_groups(sam_ctx, &el->values[i], false, filter,
470 server_info, &groupSIDs, &num_groupSIDs);
471 if (!NT_STATUS_IS_OK(status)) {
472 talloc_free(server_info);
473 return status;
477 server_info->account_sid = account_sid;
478 server_info->primary_group_sid = primary_group_sid;
480 server_info->domain_groups = groupSIDs;
481 server_info->n_domain_groups = num_groupSIDs;
483 server_info->account_name = talloc_steal(server_info,
484 samdb_result_string(msg, "sAMAccountName", NULL));
486 server_info->domain_name = talloc_strdup(server_info, domain_name);
487 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->domain_name,
488 server_info);
490 str = samdb_result_string(msg, "displayName", "");
491 server_info->full_name = talloc_strdup(server_info, str);
492 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->full_name, server_info);
494 str = samdb_result_string(msg, "scriptPath", "");
495 server_info->logon_script = talloc_strdup(server_info, str);
496 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->logon_script,
497 server_info);
499 str = samdb_result_string(msg, "profilePath", "");
500 server_info->profile_path = talloc_strdup(server_info, str);
501 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->profile_path,
502 server_info);
504 str = samdb_result_string(msg, "homeDirectory", "");
505 server_info->home_directory = talloc_strdup(server_info, str);
506 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->home_directory,
507 server_info);
509 str = samdb_result_string(msg, "homeDrive", "");
510 server_info->home_drive = talloc_strdup(server_info, str);
511 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->home_drive, server_info);
513 server_info->logon_server = talloc_strdup(server_info, netbios_name);
514 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->logon_server,
515 server_info);
517 server_info->last_logon = samdb_result_nttime(msg, "lastLogon", 0);
518 server_info->last_logoff = samdb_result_last_logoff(msg);
519 server_info->acct_expiry = samdb_result_account_expires(msg);
520 server_info->last_password_change = samdb_result_nttime(msg,
521 "pwdLastSet", 0);
522 server_info->allow_password_change
523 = samdb_result_allow_password_change(sam_ctx, mem_ctx,
524 domain_dn, msg, "pwdLastSet");
525 server_info->force_password_change
526 = samdb_result_force_password_change(sam_ctx, mem_ctx,
527 domain_dn, msg);
528 server_info->logon_count = samdb_result_uint(msg, "logonCount", 0);
529 server_info->bad_password_count = samdb_result_uint(msg, "badPwdCount",
532 server_info->acct_flags = samdb_result_acct_flags(sam_ctx, mem_ctx,
533 msg, domain_dn);
535 server_info->user_session_key = data_blob_talloc(server_info,
536 user_sess_key.data,
537 user_sess_key.length);
538 if (user_sess_key.data) {
539 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->user_session_key.data,
540 server_info);
542 server_info->lm_session_key = data_blob_talloc(server_info,
543 lm_sess_key.data,
544 lm_sess_key.length);
545 if (lm_sess_key.data) {
546 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->lm_session_key.data,
547 server_info);
550 server_info->authenticated = true;
552 talloc_free(tmp_ctx);
553 *_server_info = server_info;
555 return NT_STATUS_OK;
558 NTSTATUS sam_get_results_principal(struct ldb_context *sam_ctx,
559 TALLOC_CTX *mem_ctx, const char *principal,
560 const char **attrs,
561 struct ldb_dn **domain_dn,
562 struct ldb_message **msg)
564 struct ldb_dn *user_dn;
565 NTSTATUS nt_status;
566 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
567 int ret;
569 if (!tmp_ctx) {
570 return NT_STATUS_NO_MEMORY;
573 nt_status = crack_user_principal_name(sam_ctx, tmp_ctx, principal,
574 &user_dn, domain_dn);
575 if (!NT_STATUS_IS_OK(nt_status)) {
576 talloc_free(tmp_ctx);
577 return nt_status;
580 /* pull the user attributes */
581 ret = dsdb_search_one(sam_ctx, tmp_ctx, msg, user_dn,
582 LDB_SCOPE_BASE, attrs, DSDB_SEARCH_SHOW_EXTENDED_DN, "(objectClass=*)");
583 if (ret != LDB_SUCCESS) {
584 talloc_free(tmp_ctx);
585 return NT_STATUS_INTERNAL_DB_CORRUPTION;
587 talloc_steal(mem_ctx, *msg);
588 talloc_steal(mem_ctx, *domain_dn);
589 talloc_free(tmp_ctx);
591 return NT_STATUS_OK;