s3:modules: s/struct timed_event/struct tevent_timer
[Samba/vl.git] / source4 / auth / sam.c
blob767e44c45ddc2ccc0776b48652ff2c250d3ec0c7
1 /*
2 Unix SMB/CIFS implementation.
3 Password and authentication handling
4 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2001-2010
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"
31 #include "libcli/ldap/ldap_ndr.h"
32 #include "param/param.h"
34 #define KRBTGT_ATTRS \
35 /* required for the krb5 kdc */ \
36 "objectClass", \
37 "sAMAccountName", \
38 "userPrincipalName", \
39 "servicePrincipalName", \
40 "msDS-KeyVersionNumber", \
41 "msDS-SecondaryKrbTgtNumber", \
42 "msDS-SupportedEncryptionTypes", \
43 "supplementalCredentials", \
44 "msDS-AllowedToDelegateTo", \
46 /* passwords */ \
47 "dBCSPwd", \
48 "unicodePwd", \
50 "userAccountControl", \
51 "objectSid", \
53 "pwdLastSet", \
54 "accountExpires"
56 const char *krbtgt_attrs[] = {
57 KRBTGT_ATTRS, NULL
60 const char *server_attrs[] = {
61 KRBTGT_ATTRS, NULL
64 const char *user_attrs[] = {
65 KRBTGT_ATTRS,
67 "logonHours",
69 /* check 'allowed workstations' */
70 "userWorkstations",
72 /* required for user_info_dc, not access control: */
73 "displayName",
74 "scriptPath",
75 "profilePath",
76 "homeDirectory",
77 "homeDrive",
78 "lastLogon",
79 "lastLogoff",
80 "accountExpires",
81 "badPwdCount",
82 "logonCount",
83 "primaryGroupID",
84 "memberOf",
85 NULL,
88 /****************************************************************************
89 Check if a user is allowed to logon at this time. Note this is the
90 servers local time, as logon hours are just specified as a weekly
91 bitmask.
92 ****************************************************************************/
94 static bool logon_hours_ok(struct ldb_message *msg, const char *name_for_logs)
96 /* In logon hours first bit is Sunday from 12AM to 1AM */
97 const struct ldb_val *hours;
98 struct tm *utctime;
99 time_t lasttime;
100 const char *asct;
101 uint8_t bitmask, bitpos;
103 hours = ldb_msg_find_ldb_val(msg, "logonHours");
104 if (!hours) {
105 DEBUG(5,("logon_hours_ok: No hours restrictions for user %s\n", name_for_logs));
106 return true;
109 if (hours->length != 168/8) {
110 DEBUG(5,("logon_hours_ok: malformed logon hours restrictions for user %s\n", name_for_logs));
111 return true;
114 lasttime = time(NULL);
115 utctime = gmtime(&lasttime);
116 if (!utctime) {
117 DEBUG(1, ("logon_hours_ok: failed to get gmtime. Failing logon for user %s\n",
118 name_for_logs));
119 return false;
122 /* find the corresponding byte and bit */
123 bitpos = (utctime->tm_wday * 24 + utctime->tm_hour) % 168;
124 bitmask = 1 << (bitpos % 8);
126 if (! (hours->data[bitpos/8] & bitmask)) {
127 struct tm *t = localtime(&lasttime);
128 if (!t) {
129 asct = "INVALID TIME";
130 } else {
131 asct = asctime(t);
132 if (!asct) {
133 asct = "INVALID TIME";
137 DEBUG(1, ("logon_hours_ok: Account for user %s not allowed to "
138 "logon at this time (%s).\n",
139 name_for_logs, asct ));
140 return false;
143 asct = asctime(utctime);
144 DEBUG(5,("logon_hours_ok: user %s allowed to logon at this time (%s)\n",
145 name_for_logs, asct ? asct : "UNKNOWN TIME" ));
147 return true;
150 /****************************************************************************
151 Do a specific test for a SAM_ACCOUNT being valid for this connection
152 (ie not disabled, expired and the like).
153 ****************************************************************************/
154 _PUBLIC_ NTSTATUS authsam_account_ok(TALLOC_CTX *mem_ctx,
155 struct ldb_context *sam_ctx,
156 uint32_t logon_parameters,
157 struct ldb_dn *domain_dn,
158 struct ldb_message *msg,
159 const char *logon_workstation,
160 const char *name_for_logs,
161 bool allow_domain_trust,
162 bool password_change)
164 uint16_t acct_flags;
165 const char *workstation_list;
166 NTTIME acct_expiry;
167 NTTIME must_change_time;
169 NTTIME now;
170 DEBUG(4,("authsam_account_ok: Checking SMB password for user %s\n", name_for_logs));
172 acct_flags = samdb_result_acct_flags(sam_ctx, mem_ctx, msg, domain_dn);
174 acct_expiry = samdb_result_account_expires(msg);
176 /* Check for when we must change this password, taking the
177 * userAccountControl flags into account */
178 must_change_time = samdb_result_force_password_change(sam_ctx, mem_ctx,
179 domain_dn, msg);
181 workstation_list = ldb_msg_find_attr_as_string(msg, "userWorkstations", NULL);
183 /* Quit if the account was disabled. */
184 if (acct_flags & ACB_DISABLED) {
185 DEBUG(2,("authsam_account_ok: Account for user '%s' was disabled.\n", name_for_logs));
186 return NT_STATUS_ACCOUNT_DISABLED;
189 /* Quit if the account was locked out. */
190 if (acct_flags & ACB_AUTOLOCK) {
191 DEBUG(2,("authsam_account_ok: Account for user %s was locked out.\n", name_for_logs));
192 return NT_STATUS_ACCOUNT_LOCKED_OUT;
195 /* Test account expire time */
196 unix_to_nt_time(&now, time(NULL));
197 if (now > acct_expiry) {
198 DEBUG(2,("authsam_account_ok: Account for user '%s' has expired.\n", name_for_logs));
199 DEBUG(3,("authsam_account_ok: Account expired at '%s'.\n",
200 nt_time_string(mem_ctx, acct_expiry)));
201 return NT_STATUS_ACCOUNT_EXPIRED;
204 /* check for immediate expiry "must change at next logon" (but not if this is a password change request) */
205 if ((must_change_time == 0) && !password_change) {
206 DEBUG(2,("sam_account_ok: Account for user '%s' password must change!.\n",
207 name_for_logs));
208 return NT_STATUS_PASSWORD_MUST_CHANGE;
211 /* check for expired password (but not if this is a password change request) */
212 if ((must_change_time < now) && !password_change) {
213 DEBUG(2,("sam_account_ok: Account for user '%s' password expired!.\n",
214 name_for_logs));
215 DEBUG(2,("sam_account_ok: Password expired at '%s' unix time.\n",
216 nt_time_string(mem_ctx, must_change_time)));
217 return NT_STATUS_PASSWORD_EXPIRED;
220 /* Test workstation. Workstation list is comma separated. */
221 if (logon_workstation && workstation_list && *workstation_list) {
222 bool invalid_ws = true;
223 int i;
224 const char **workstations = (const char **)str_list_make(mem_ctx, workstation_list, ",");
226 for (i = 0; workstations && workstations[i]; i++) {
227 DEBUG(10,("sam_account_ok: checking for workstation match '%s' and '%s'\n",
228 workstations[i], logon_workstation));
230 if (strequal(workstations[i], logon_workstation)) {
231 invalid_ws = false;
232 break;
236 talloc_free(workstations);
238 if (invalid_ws) {
239 return NT_STATUS_INVALID_WORKSTATION;
243 if (!logon_hours_ok(msg, name_for_logs)) {
244 return NT_STATUS_INVALID_LOGON_HOURS;
247 if (!allow_domain_trust) {
248 if (acct_flags & ACB_DOMTRUST) {
249 DEBUG(2,("sam_account_ok: Domain trust account %s denied by server\n", name_for_logs));
250 return NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT;
253 if (!(logon_parameters & MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT)) {
254 if (acct_flags & ACB_SVRTRUST) {
255 DEBUG(2,("sam_account_ok: Server trust account %s denied by server\n", name_for_logs));
256 return NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT;
259 if (!(logon_parameters & MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT)) {
260 /* TODO: this fails with current solaris client. We
261 need to work with Gordon to work out why */
262 if (acct_flags & ACB_WSTRUST) {
263 DEBUG(4,("sam_account_ok: Wksta trust account %s denied by server\n", name_for_logs));
264 return NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT;
268 return NT_STATUS_OK;
271 _PUBLIC_ NTSTATUS authsam_make_user_info_dc(TALLOC_CTX *mem_ctx,
272 struct ldb_context *sam_ctx,
273 const char *netbios_name,
274 const char *domain_name,
275 struct ldb_dn *domain_dn,
276 struct ldb_message *msg,
277 DATA_BLOB user_sess_key,
278 DATA_BLOB lm_sess_key,
279 struct auth_user_info_dc **_user_info_dc)
281 NTSTATUS status;
282 struct auth_user_info_dc *user_info_dc;
283 struct auth_user_info *info;
284 const char *str, *filter;
285 /* SIDs for the account and his primary group */
286 struct dom_sid *account_sid;
287 const char *primary_group_string;
288 const char *primary_group_dn;
289 DATA_BLOB primary_group_blob;
290 /* SID structures for the expanded group memberships */
291 struct dom_sid *sids = NULL;
292 unsigned int num_sids = 0, i;
293 struct dom_sid *domain_sid;
294 TALLOC_CTX *tmp_ctx;
295 struct ldb_message_element *el;
297 user_info_dc = talloc(mem_ctx, struct auth_user_info_dc);
298 NT_STATUS_HAVE_NO_MEMORY(user_info_dc);
300 tmp_ctx = talloc_new(user_info_dc);
301 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(user_info_dc, user_info_dc);
303 sids = talloc_array(user_info_dc, struct dom_sid, 2);
304 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(sids, user_info_dc);
306 num_sids = 2;
308 account_sid = samdb_result_dom_sid(user_info_dc, msg, "objectSid");
309 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(account_sid, user_info_dc);
311 status = dom_sid_split_rid(tmp_ctx, account_sid, &domain_sid, NULL);
312 if (!NT_STATUS_IS_OK(status)) {
313 talloc_free(user_info_dc);
314 return status;
317 sids[PRIMARY_USER_SID_INDEX] = *account_sid;
318 sids[PRIMARY_GROUP_SID_INDEX] = *domain_sid;
319 sid_append_rid(&sids[PRIMARY_GROUP_SID_INDEX], ldb_msg_find_attr_as_uint(msg, "primaryGroupID", ~0));
321 /* Filter out builtin groups from this token. We will search
322 * for builtin groups later, and not include them in the PAC
323 * on SamLogon validation info */
324 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);
325 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(filter, user_info_dc);
327 primary_group_string = dom_sid_string(tmp_ctx, &sids[PRIMARY_GROUP_SID_INDEX]);
328 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(primary_group_string, user_info_dc);
330 primary_group_dn = talloc_asprintf(tmp_ctx, "<SID=%s>", primary_group_string);
331 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(primary_group_dn, user_info_dc);
333 primary_group_blob = data_blob_string_const(primary_group_dn);
335 /* Expands the primary group - this function takes in
336 * memberOf-like values, so we fake one up with the
337 * <SID=S-...> format of DN and then let it expand
338 * them, as long as they meet the filter - so only
339 * domain groups, not builtin groups
341 * The primary group is still treated specially, so we set the
342 * 'only childs' flag to true
344 status = dsdb_expand_nested_groups(sam_ctx, &primary_group_blob, true, filter,
345 user_info_dc, &sids, &num_sids);
346 if (!NT_STATUS_IS_OK(status)) {
347 talloc_free(user_info_dc);
348 return status;
351 /* Expands the additional groups */
352 el = ldb_msg_find_element(msg, "memberOf");
353 for (i = 0; el && i < el->num_values; i++) {
354 /* This function takes in memberOf values and expands
355 * them, as long as they meet the filter - so only
356 * domain groups, not builtin groups */
357 status = dsdb_expand_nested_groups(sam_ctx, &el->values[i], false, filter,
358 user_info_dc, &sids, &num_sids);
359 if (!NT_STATUS_IS_OK(status)) {
360 talloc_free(user_info_dc);
361 return status;
365 user_info_dc->sids = sids;
366 user_info_dc->num_sids = num_sids;
368 user_info_dc->info = info = talloc_zero(user_info_dc, struct auth_user_info);
369 NT_STATUS_HAVE_NO_MEMORY(user_info_dc->info);
371 info->account_name = talloc_steal(info,
372 ldb_msg_find_attr_as_string(msg, "sAMAccountName", NULL));
374 info->domain_name = talloc_strdup(info, domain_name);
375 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(info->domain_name,
376 user_info_dc);
378 str = ldb_msg_find_attr_as_string(msg, "displayName", "");
379 info->full_name = talloc_strdup(info, str);
380 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(info->full_name, user_info_dc);
382 str = ldb_msg_find_attr_as_string(msg, "scriptPath", "");
383 info->logon_script = talloc_strdup(info, str);
384 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(info->logon_script,
385 user_info_dc);
387 str = ldb_msg_find_attr_as_string(msg, "profilePath", "");
388 info->profile_path = talloc_strdup(info, str);
389 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(info->profile_path,
390 user_info_dc);
392 str = ldb_msg_find_attr_as_string(msg, "homeDirectory", "");
393 info->home_directory = talloc_strdup(info, str);
394 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(info->home_directory,
395 user_info_dc);
397 str = ldb_msg_find_attr_as_string(msg, "homeDrive", "");
398 info->home_drive = talloc_strdup(info, str);
399 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(info->home_drive, user_info_dc);
401 info->logon_server = talloc_strdup(info, netbios_name);
402 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(info->logon_server,
403 user_info_dc);
405 info->last_logon = samdb_result_nttime(msg, "lastLogon", 0);
406 info->last_logoff = samdb_result_last_logoff(msg);
407 info->acct_expiry = samdb_result_account_expires(msg);
408 info->last_password_change = samdb_result_nttime(msg,
409 "pwdLastSet", 0);
410 info->allow_password_change
411 = samdb_result_allow_password_change(sam_ctx, mem_ctx,
412 domain_dn, msg, "pwdLastSet");
413 info->force_password_change
414 = samdb_result_force_password_change(sam_ctx, mem_ctx,
415 domain_dn, msg);
416 info->logon_count = ldb_msg_find_attr_as_uint(msg, "logonCount", 0);
417 info->bad_password_count = ldb_msg_find_attr_as_uint(msg, "badPwdCount",
420 info->acct_flags = samdb_result_acct_flags(sam_ctx, mem_ctx,
421 msg, domain_dn);
423 user_info_dc->user_session_key = data_blob_talloc(user_info_dc,
424 user_sess_key.data,
425 user_sess_key.length);
426 if (user_sess_key.data) {
427 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(user_info_dc->user_session_key.data,
428 user_info_dc);
430 user_info_dc->lm_session_key = data_blob_talloc(user_info_dc,
431 lm_sess_key.data,
432 lm_sess_key.length);
433 if (lm_sess_key.data) {
434 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(user_info_dc->lm_session_key.data,
435 user_info_dc);
438 if (info->acct_flags & ACB_SVRTRUST) {
439 /* the SID_NT_ENTERPRISE_DCS SID gets added into the
440 PAC */
441 user_info_dc->sids = talloc_realloc(user_info_dc,
442 user_info_dc->sids,
443 struct dom_sid,
444 user_info_dc->num_sids+1);
445 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(user_info_dc->sids, user_info_dc);
446 user_info_dc->sids[user_info_dc->num_sids] = global_sid_Enterprise_DCs;
447 user_info_dc->num_sids++;
450 if ((info->acct_flags & (ACB_PARTIAL_SECRETS_ACCOUNT | ACB_WSTRUST)) ==
451 (ACB_PARTIAL_SECRETS_ACCOUNT | ACB_WSTRUST)) {
452 /* the DOMAIN_RID_ENTERPRISE_READONLY_DCS PAC */
453 user_info_dc->sids = talloc_realloc(user_info_dc,
454 user_info_dc->sids,
455 struct dom_sid,
456 user_info_dc->num_sids+1);
457 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(user_info_dc->sids, user_info_dc);
458 user_info_dc->sids[user_info_dc->num_sids] = *domain_sid;
459 sid_append_rid(&user_info_dc->sids[user_info_dc->num_sids],
460 DOMAIN_RID_ENTERPRISE_READONLY_DCS);
461 user_info_dc->num_sids++;
464 info->authenticated = true;
466 talloc_free(tmp_ctx);
467 *_user_info_dc = user_info_dc;
469 return NT_STATUS_OK;
472 NTSTATUS sam_get_results_principal(struct ldb_context *sam_ctx,
473 TALLOC_CTX *mem_ctx, const char *principal,
474 const char **attrs,
475 struct ldb_dn **domain_dn,
476 struct ldb_message **msg)
478 struct ldb_dn *user_dn;
479 NTSTATUS nt_status;
480 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
481 int ret;
483 if (!tmp_ctx) {
484 return NT_STATUS_NO_MEMORY;
487 nt_status = crack_user_principal_name(sam_ctx, tmp_ctx, principal,
488 &user_dn, domain_dn);
489 if (!NT_STATUS_IS_OK(nt_status)) {
490 talloc_free(tmp_ctx);
491 return nt_status;
494 /* pull the user attributes */
495 ret = dsdb_search_one(sam_ctx, tmp_ctx, msg, user_dn,
496 LDB_SCOPE_BASE, attrs,
497 DSDB_SEARCH_SHOW_EXTENDED_DN | DSDB_SEARCH_NO_GLOBAL_CATALOG,
498 "(objectClass=*)");
499 if (ret != LDB_SUCCESS) {
500 talloc_free(tmp_ctx);
501 return NT_STATUS_INTERNAL_DB_CORRUPTION;
503 talloc_steal(mem_ctx, *msg);
504 talloc_steal(mem_ctx, *domain_dn);
505 talloc_free(tmp_ctx);
507 return NT_STATUS_OK;
510 /* Used in the gensec_gssapi and gensec_krb5 server-side code, where the PAC isn't available, and for tokenGroups in the DSDB stack.
512 Supply either a principal or a DN
514 NTSTATUS authsam_get_user_info_dc_principal(TALLOC_CTX *mem_ctx,
515 struct loadparm_context *lp_ctx,
516 struct ldb_context *sam_ctx,
517 const char *principal,
518 struct ldb_dn *user_dn,
519 struct auth_user_info_dc **user_info_dc)
521 NTSTATUS nt_status;
522 DATA_BLOB user_sess_key = data_blob(NULL, 0);
523 DATA_BLOB lm_sess_key = data_blob(NULL, 0);
525 struct ldb_message *msg;
526 struct ldb_dn *domain_dn;
528 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
529 if (!tmp_ctx) {
530 return NT_STATUS_NO_MEMORY;
533 if (principal) {
534 nt_status = sam_get_results_principal(sam_ctx, tmp_ctx, principal,
535 user_attrs, &domain_dn, &msg);
536 if (!NT_STATUS_IS_OK(nt_status)) {
537 talloc_free(tmp_ctx);
538 return nt_status;
540 } else if (user_dn) {
541 struct dom_sid *user_sid, *domain_sid;
542 int ret;
543 /* pull the user attributes */
544 ret = dsdb_search_one(sam_ctx, tmp_ctx, &msg, user_dn,
545 LDB_SCOPE_BASE, user_attrs,
546 DSDB_SEARCH_SHOW_EXTENDED_DN | DSDB_SEARCH_NO_GLOBAL_CATALOG,
547 "(objectClass=*)");
548 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
549 talloc_free(tmp_ctx);
550 return NT_STATUS_NO_SUCH_USER;
551 } else if (ret != LDB_SUCCESS) {
552 talloc_free(tmp_ctx);
553 return NT_STATUS_INTERNAL_DB_CORRUPTION;
556 user_sid = samdb_result_dom_sid(msg, msg, "objectSid");
558 nt_status = dom_sid_split_rid(tmp_ctx, user_sid, &domain_sid, NULL);
559 if (!NT_STATUS_IS_OK(nt_status)) {
560 return nt_status;
563 domain_dn = samdb_search_dn(sam_ctx, mem_ctx, NULL,
564 "(&(objectSid=%s)(objectClass=domain))",
565 ldap_encode_ndr_dom_sid(tmp_ctx, domain_sid));
566 if (!domain_dn) {
567 DEBUG(3, ("authsam_get_user_info_dc_principal: Failed to find domain with: SID %s\n",
568 dom_sid_string(tmp_ctx, domain_sid)));
569 return NT_STATUS_NO_SUCH_USER;
572 } else {
573 return NT_STATUS_INVALID_PARAMETER;
576 nt_status = authsam_make_user_info_dc(tmp_ctx, sam_ctx,
577 lpcfg_netbios_name(lp_ctx),
578 lpcfg_workgroup(lp_ctx),
579 domain_dn,
580 msg,
581 user_sess_key, lm_sess_key,
582 user_info_dc);
583 if (!NT_STATUS_IS_OK(nt_status)) {
584 talloc_free(tmp_ctx);
585 return nt_status;
588 talloc_steal(mem_ctx, *user_info_dc);
589 talloc_free(tmp_ctx);
591 return NT_STATUS_OK;