s4/idl: rename 'guid1' to 'source_dsa_guid' in drsuapi_DsReplicaGetInfoRequest descri...
[Samba.git] / source4 / auth / sam.c
blob7d2f249b064b71f7da7639c334a2fd351bdb7a53
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 "../lib/util/util_ldb.h"
28 #include "dsdb/samdb/samdb.h"
29 #include "libcli/security/security.h"
30 #include "libcli/ldap/ldap.h"
31 #include "../libcli/ldap/ldap_ndr.h"
32 #include "librpc/gen_ndr/ndr_netlogon.h"
33 #include "librpc/gen_ndr/ndr_security.h"
34 #include "param/param.h"
35 #include "auth/auth_sam.h"
36 #include "dsdb/common/util.h"
38 #define KRBTGT_ATTRS \
39 /* required for the krb5 kdc */ \
40 "objectClass", \
41 "sAMAccountName", \
42 "userPrincipalName", \
43 "servicePrincipalName", \
44 "msDS-KeyVersionNumber", \
45 "supplementalCredentials", \
47 /* passwords */ \
48 "dBCSPwd", \
49 "unicodePwd", \
51 "userAccountControl", \
52 "objectSid", \
54 "pwdLastSet", \
55 "accountExpires"
57 const char *krbtgt_attrs[] = {
58 KRBTGT_ATTRS
61 const char *server_attrs[] = {
62 KRBTGT_ATTRS
65 const char *user_attrs[] = {
66 KRBTGT_ATTRS,
68 "logonHours",
70 /* check 'allowed workstations' */
71 "userWorkstations",
73 /* required for server_info, not access control: */
74 "displayName",
75 "scriptPath",
76 "profilePath",
77 "homeDirectory",
78 "homeDrive",
79 "lastLogon",
80 "lastLogoff",
81 "accountExpires",
82 "badPwdCount",
83 "logonCount",
84 "primaryGroupID",
85 "memberOf",
86 NULL,
89 /****************************************************************************
90 Check if a user is allowed to logon at this time. Note this is the
91 servers local time, as logon hours are just specified as a weekly
92 bitmask.
93 ****************************************************************************/
95 static bool logon_hours_ok(struct ldb_message *msg, const char *name_for_logs)
97 /* In logon hours first bit is Sunday from 12AM to 1AM */
98 const struct ldb_val *hours;
99 struct tm *utctime;
100 time_t lasttime;
101 const char *asct;
102 uint8_t bitmask, bitpos;
104 hours = ldb_msg_find_ldb_val(msg, "logonHours");
105 if (!hours) {
106 DEBUG(5,("logon_hours_ok: No hours restrictions for user %s\n", name_for_logs));
107 return true;
110 if (hours->length != 168/8) {
111 DEBUG(5,("logon_hours_ok: malformed logon hours restrictions for user %s\n", name_for_logs));
112 return true;
115 lasttime = time(NULL);
116 utctime = gmtime(&lasttime);
117 if (!utctime) {
118 DEBUG(1, ("logon_hours_ok: failed to get gmtime. Failing logon for user %s\n",
119 name_for_logs));
120 return false;
123 /* find the corresponding byte and bit */
124 bitpos = (utctime->tm_wday * 24 + utctime->tm_hour) % 168;
125 bitmask = 1 << (bitpos % 8);
127 if (! (hours->data[bitpos/8] & bitmask)) {
128 struct tm *t = localtime(&lasttime);
129 if (!t) {
130 asct = "INVALID TIME";
131 } else {
132 asct = asctime(t);
133 if (!asct) {
134 asct = "INVALID TIME";
138 DEBUG(1, ("logon_hours_ok: Account for user %s not allowed to "
139 "logon at this time (%s).\n",
140 name_for_logs, asct ));
141 return false;
144 asct = asctime(utctime);
145 DEBUG(5,("logon_hours_ok: user %s allowed to logon at this time (%s)\n",
146 name_for_logs, asct ? asct : "UNKNOWN TIME" ));
148 return true;
151 /****************************************************************************
152 Do a specific test for a SAM_ACCOUNT being valid for this connection
153 (ie not disabled, expired and the like).
154 ****************************************************************************/
155 _PUBLIC_ NTSTATUS authsam_account_ok(TALLOC_CTX *mem_ctx,
156 struct ldb_context *sam_ctx,
157 uint32_t logon_parameters,
158 struct ldb_dn *domain_dn,
159 struct ldb_message *msg,
160 const char *logon_workstation,
161 const char *name_for_logs,
162 bool allow_domain_trust,
163 bool password_change)
165 uint16_t acct_flags;
166 const char *workstation_list;
167 NTTIME acct_expiry;
168 NTTIME must_change_time;
170 NTTIME now;
171 DEBUG(4,("authsam_account_ok: Checking SMB password for user %s\n", name_for_logs));
173 acct_flags = samdb_result_acct_flags(sam_ctx, mem_ctx, msg, domain_dn);
175 acct_expiry = samdb_result_account_expires(msg);
177 /* Check for when we must change this password, taking the
178 * userAccountControl flags into account */
179 must_change_time = samdb_result_force_password_change(sam_ctx, mem_ctx,
180 domain_dn, msg);
182 workstation_list = samdb_result_string(msg, "userWorkstations", NULL);
184 /* Quit if the account was disabled. */
185 if (acct_flags & ACB_DISABLED) {
186 DEBUG(1,("authsam_account_ok: Account for user '%s' was disabled.\n", name_for_logs));
187 return NT_STATUS_ACCOUNT_DISABLED;
190 /* Quit if the account was locked out. */
191 if (acct_flags & ACB_AUTOLOCK) {
192 DEBUG(1,("authsam_account_ok: Account for user %s was locked out.\n", name_for_logs));
193 return NT_STATUS_ACCOUNT_LOCKED_OUT;
196 /* Test account expire time */
197 unix_to_nt_time(&now, time(NULL));
198 if (now > acct_expiry) {
199 DEBUG(1,("authsam_account_ok: Account for user '%s' has expired.\n", name_for_logs));
200 DEBUG(3,("authsam_account_ok: Account expired at '%s'.\n",
201 nt_time_string(mem_ctx, acct_expiry)));
202 return NT_STATUS_ACCOUNT_EXPIRED;
205 /* check for immediate expiry "must change at next logon" (but not if this is a password change request) */
206 if ((must_change_time == 0) && !password_change) {
207 DEBUG(1,("sam_account_ok: Account for user '%s' password must change!.\n",
208 name_for_logs));
209 return NT_STATUS_PASSWORD_MUST_CHANGE;
212 /* check for expired password (but not if this is a password change request) */
213 if ((must_change_time < now) && !password_change) {
214 DEBUG(1,("sam_account_ok: Account for user '%s' password expired!.\n",
215 name_for_logs));
216 DEBUG(1,("sam_account_ok: Password expired at '%s' unix time.\n",
217 nt_time_string(mem_ctx, must_change_time)));
218 return NT_STATUS_PASSWORD_EXPIRED;
221 /* Test workstation. Workstation list is comma separated. */
222 if (logon_workstation && workstation_list && *workstation_list) {
223 bool invalid_ws = true;
224 int i;
225 const char **workstations = (const char **)str_list_make(mem_ctx, workstation_list, ",");
227 for (i = 0; workstations && workstations[i]; i++) {
228 DEBUG(10,("sam_account_ok: checking for workstation match '%s' and '%s'\n",
229 workstations[i], logon_workstation));
231 if (strequal(workstations[i], logon_workstation)) {
232 invalid_ws = false;
233 break;
237 talloc_free(workstations);
239 if (invalid_ws) {
240 return NT_STATUS_INVALID_WORKSTATION;
244 if (!logon_hours_ok(msg, name_for_logs)) {
245 return NT_STATUS_INVALID_LOGON_HOURS;
248 if (!allow_domain_trust) {
249 if (acct_flags & ACB_DOMTRUST) {
250 DEBUG(2,("sam_account_ok: Domain trust account %s denied by server\n", name_for_logs));
251 return NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT;
254 if (!(logon_parameters & MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT)) {
255 if (acct_flags & ACB_SVRTRUST) {
256 DEBUG(2,("sam_account_ok: Server trust account %s denied by server\n", name_for_logs));
257 return NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT;
260 if (!(logon_parameters & MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT)) {
261 /* TODO: this fails with current solaris client. We
262 need to work with Gordon to work out why */
263 if (acct_flags & ACB_WSTRUST) {
264 DEBUG(4,("sam_account_ok: Wksta trust account %s denied by server\n", name_for_logs));
265 return NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT;
269 return NT_STATUS_OK;
272 /* This function tests if a SID structure "sids" contains the SID "sid" */
273 static bool sids_contains_sid(const struct dom_sid **sids, const int num_sids,
274 const struct dom_sid *sid)
276 int i;
278 for (i = 0; i < num_sids; i++) {
279 if (dom_sid_equal(sids[i], sid))
280 return true;
282 return false;
286 * This function generates the transitive closure of a given SID "sid" (it
287 * basically expands nested groups of a SID).
288 * If the SID isn't located in the "res_sids" structure yet and the
289 * "only_childs" flag is negative, we add it to "res_sids".
290 * Then we've always to consider the "memberOf" attributes. We invoke the
291 * function recursively on each item of it with the "only_childs" flag set to
292 * "false".
293 * The "only_childs" flag is particularly useful if you have a user SID and
294 * want to include all his groups (referenced with "memberOf") without his SID
295 * itself.
297 * At the beginning "res_sids" should reference to a NULL pointer.
299 static NTSTATUS authsam_expand_nested_groups(struct ldb_context *sam_ctx,
300 const struct dom_sid *sid, const bool only_childs,
301 TALLOC_CTX *res_sids_ctx, struct dom_sid ***res_sids,
302 int *num_res_sids)
304 const char * const attrs[] = { "memberOf", NULL };
305 int i, ret;
306 bool already_there;
307 struct ldb_dn *tmp_dn;
308 struct dom_sid *tmp_sid;
309 TALLOC_CTX *tmp_ctx;
310 struct ldb_message **res;
311 NTSTATUS status;
313 if (*res_sids == NULL) {
314 *num_res_sids = 0;
317 if (sid == NULL) {
318 return NT_STATUS_OK;
321 already_there = sids_contains_sid((const struct dom_sid**) *res_sids,
322 *num_res_sids, sid);
323 if (already_there) {
324 return NT_STATUS_OK;
327 if (!only_childs) {
328 tmp_sid = dom_sid_dup(res_sids_ctx, sid);
329 NT_STATUS_HAVE_NO_MEMORY(tmp_sid);
330 *res_sids = talloc_realloc(res_sids_ctx, *res_sids,
331 struct dom_sid *, *num_res_sids + 1);
332 NT_STATUS_HAVE_NO_MEMORY(*res_sids);
333 (*res_sids)[*num_res_sids] = tmp_sid;
334 ++(*num_res_sids);
337 tmp_ctx = talloc_new(sam_ctx);
339 ret = gendb_search(sam_ctx, tmp_ctx, NULL, &res, attrs,
340 "objectSid=%s", ldap_encode_ndr_dom_sid(tmp_ctx, sid));
341 if (ret != 1) {
342 talloc_free(tmp_ctx);
343 return NT_STATUS_INTERNAL_DB_CORRUPTION;
346 if (res[0]->num_elements == 0) {
347 talloc_free(res);
348 talloc_free(tmp_ctx);
349 return NT_STATUS_OK;
352 for (i = 0; i < res[0]->elements[0].num_values; i++) {
353 tmp_dn = ldb_dn_from_ldb_val(tmp_ctx, sam_ctx,
354 &res[0]->elements[0].values[i]);
355 tmp_sid = samdb_search_dom_sid(sam_ctx, tmp_ctx, tmp_dn,
356 "objectSid", NULL);
358 status = authsam_expand_nested_groups(sam_ctx, tmp_sid,
359 false, res_sids_ctx, res_sids, num_res_sids);
360 if (!NT_STATUS_IS_OK(status)) {
361 talloc_free(res);
362 talloc_free(tmp_ctx);
363 return status;
367 talloc_free(res);
368 talloc_free(tmp_ctx);
370 return NT_STATUS_OK;
373 _PUBLIC_ NTSTATUS authsam_make_server_info(TALLOC_CTX *mem_ctx,
374 struct ldb_context *sam_ctx,
375 const char *netbios_name,
376 const char *domain_name,
377 struct ldb_dn *domain_dn,
378 struct ldb_message *msg,
379 DATA_BLOB user_sess_key,
380 DATA_BLOB lm_sess_key,
381 struct auth_serversupplied_info
382 **_server_info)
384 NTSTATUS status;
385 struct auth_serversupplied_info *server_info;
386 const char *str;
387 struct dom_sid *tmp_sid;
388 /* SIDs for the account and his primary group */
389 struct dom_sid *account_sid;
390 struct dom_sid *primary_group_sid;
391 /* SID structures for the expanded group memberships */
392 struct dom_sid **groupSIDs = NULL, **groupSIDs_2 = NULL;
393 int num_groupSIDs = 0, num_groupSIDs_2 = 0, i;
394 uint32_t userAccountControl;
396 server_info = talloc(mem_ctx, struct auth_serversupplied_info);
397 NT_STATUS_HAVE_NO_MEMORY(server_info);
399 account_sid = samdb_result_dom_sid(server_info, msg, "objectSid");
400 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(account_sid, server_info);
402 primary_group_sid = dom_sid_add_rid(server_info,
403 samdb_domain_sid(sam_ctx),
404 samdb_result_uint(msg, "primaryGroupID", ~0));
405 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(primary_group_sid, server_info);
407 /* Expands the primary group */
408 status = authsam_expand_nested_groups(sam_ctx, primary_group_sid, false,
409 server_info, &groupSIDs, &num_groupSIDs);
410 if (!NT_STATUS_IS_OK(status)) {
411 talloc_free(server_info);
412 return status;
415 /* Expands the additional groups */
416 status = authsam_expand_nested_groups(sam_ctx, account_sid, true,
417 server_info, &groupSIDs_2, &num_groupSIDs_2);
418 if (!NT_STATUS_IS_OK(status)) {
419 talloc_free(server_info);
420 return status;
423 /* Merge the two expanded structures (groupSIDs, groupSIDs_2) */
424 for (i = 0; i < num_groupSIDs_2; i++)
425 if (!sids_contains_sid((const struct dom_sid **) groupSIDs,
426 num_groupSIDs, groupSIDs_2[i])) {
427 tmp_sid = dom_sid_dup(server_info, groupSIDs_2[i]);
428 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(tmp_sid, server_info);
429 groupSIDs = talloc_realloc(server_info, groupSIDs,
430 struct dom_sid *, num_groupSIDs + 1);
431 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(groupSIDs,
432 server_info);
433 groupSIDs[num_groupSIDs] = tmp_sid;
434 ++num_groupSIDs;
436 talloc_free(groupSIDs_2);
438 server_info->account_sid = account_sid;
439 server_info->primary_group_sid = primary_group_sid;
441 /* DCs also get SID_NT_ENTERPRISE_DCS */
442 userAccountControl = ldb_msg_find_attr_as_uint(msg, "userAccountControl", 0);
443 if (userAccountControl & UF_SERVER_TRUST_ACCOUNT) {
444 groupSIDs = talloc_realloc(server_info, groupSIDs, struct dom_sid *,
445 num_groupSIDs+1);
446 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(groupSIDs, server_info);
447 groupSIDs[num_groupSIDs] = dom_sid_parse_talloc(groupSIDs, SID_NT_ENTERPRISE_DCS);
448 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(groupSIDs[num_groupSIDs], server_info);
449 num_groupSIDs++;
452 server_info->domain_groups = groupSIDs;
453 server_info->n_domain_groups = num_groupSIDs;
455 server_info->account_name = talloc_steal(server_info,
456 samdb_result_string(msg, "sAMAccountName", NULL));
458 server_info->domain_name = talloc_strdup(server_info, domain_name);
459 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->domain_name,
460 server_info);
462 str = samdb_result_string(msg, "displayName", "");
463 server_info->full_name = talloc_strdup(server_info, str);
464 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->full_name, server_info);
466 str = samdb_result_string(msg, "scriptPath", "");
467 server_info->logon_script = talloc_strdup(server_info, str);
468 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->logon_script,
469 server_info);
471 str = samdb_result_string(msg, "profilePath", "");
472 server_info->profile_path = talloc_strdup(server_info, str);
473 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->profile_path,
474 server_info);
476 str = samdb_result_string(msg, "homeDirectory", "");
477 server_info->home_directory = talloc_strdup(server_info, str);
478 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->home_directory,
479 server_info);
481 str = samdb_result_string(msg, "homeDrive", "");
482 server_info->home_drive = talloc_strdup(server_info, str);
483 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->home_drive, server_info);
485 server_info->logon_server = talloc_strdup(server_info, netbios_name);
486 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->logon_server,
487 server_info);
489 server_info->last_logon = samdb_result_nttime(msg, "lastLogon", 0);
490 server_info->last_logoff = samdb_result_last_logoff(msg);
491 server_info->acct_expiry = samdb_result_account_expires(msg);
492 server_info->last_password_change = samdb_result_nttime(msg,
493 "pwdLastSet", 0);
494 server_info->allow_password_change
495 = samdb_result_allow_password_change(sam_ctx, mem_ctx,
496 domain_dn, msg, "pwdLastSet");
497 server_info->force_password_change
498 = samdb_result_force_password_change(sam_ctx, mem_ctx,
499 domain_dn, msg);
500 server_info->logon_count = samdb_result_uint(msg, "logonCount", 0);
501 server_info->bad_password_count = samdb_result_uint(msg, "badPwdCount",
504 server_info->acct_flags = samdb_result_acct_flags(sam_ctx, mem_ctx,
505 msg, domain_dn);
507 server_info->user_session_key = data_blob_talloc(server_info,
508 user_sess_key.data,
509 user_sess_key.length);
510 if (user_sess_key.data) {
511 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->user_session_key.data,
512 server_info);
514 server_info->lm_session_key = data_blob_talloc(server_info,
515 lm_sess_key.data,
516 lm_sess_key.length);
517 if (lm_sess_key.data) {
518 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->lm_session_key.data,
519 server_info);
522 server_info->authenticated = true;
524 *_server_info = server_info;
526 return NT_STATUS_OK;
529 NTSTATUS sam_get_results_principal(struct ldb_context *sam_ctx,
530 TALLOC_CTX *mem_ctx, const char *principal,
531 const char **attrs,
532 struct ldb_dn **domain_dn,
533 struct ldb_message **msg)
535 struct ldb_dn *user_dn;
536 NTSTATUS nt_status;
537 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
538 int ret;
540 if (!tmp_ctx) {
541 return NT_STATUS_NO_MEMORY;
544 nt_status = crack_user_principal_name(sam_ctx, tmp_ctx, principal,
545 &user_dn, domain_dn);
546 if (!NT_STATUS_IS_OK(nt_status)) {
547 talloc_free(tmp_ctx);
548 return nt_status;
551 /* pull the user attributes */
552 ret = dsdb_search_one(sam_ctx, tmp_ctx, msg, user_dn,
553 LDB_SCOPE_BASE, attrs, DSDB_SEARCH_SHOW_EXTENDED_DN, "(objectClass=*)");
554 if (ret != LDB_SUCCESS) {
555 talloc_free(tmp_ctx);
556 return NT_STATUS_INTERNAL_DB_CORRUPTION;
558 talloc_steal(mem_ctx, *msg);
559 talloc_steal(mem_ctx, *domain_dn);
560 talloc_free(tmp_ctx);
562 return NT_STATUS_OK;