8e84e3e4e6d8a63a3a4ffdbdc9f4e3aa3628cc60
[samba.git] / source4 / auth / sam.c
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
8    
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.
13    
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.
18    
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/>.
21 */
22
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"
33
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",             \
45                                                 \
46         /* passwords */                         \
47         "dBCSPwd",                              \
48         "unicodePwd",                           \
49                                                 \
50         "userAccountControl",                   \
51         "msDS-User-Account-Control-Computed",   \
52         "objectSid",                            \
53                                                 \
54         "pwdLastSet",                           \
55         "msDS-UserPasswordExpiryTimeComputed",  \
56         "accountExpires"
57
58 const char *krbtgt_attrs[] = {
59         KRBTGT_ATTRS, NULL
60 };
61
62 const char *server_attrs[] = {
63         KRBTGT_ATTRS, NULL
64 };
65
66 const char *user_attrs[] = {
67         KRBTGT_ATTRS,
68
69         "logonHours",
70
71         /*
72          * To allow us to zero the badPwdCount and lockoutTime on
73          * successful logon, without database churn
74          */
75         "lockoutTime",
76
77         /* check 'allowed workstations' */
78         "userWorkstations",
79                        
80         /* required for user_info_dc, not access control: */
81         "displayName",
82         "scriptPath",
83         "profilePath",
84         "homeDirectory",
85         "homeDrive",
86         "lastLogon",
87         "lastLogonTimestamp",
88         "lastLogoff",
89         "accountExpires",
90         "badPwdCount",
91         "logonCount",
92         "primaryGroupID",
93         "memberOf",
94         "badPasswordTime",
95         "lmPwdHistory",
96         "ntPwdHistory",
97         NULL,
98 };
99
100 /****************************************************************************
101  Check if a user is allowed to logon at this time. Note this is the
102  servers local time, as logon hours are just specified as a weekly
103  bitmask.
104 ****************************************************************************/
105                                                                                                               
106 static bool logon_hours_ok(struct ldb_message *msg, const char *name_for_logs)
107 {
108         /* In logon hours first bit is Sunday from 12AM to 1AM */
109         const struct ldb_val *hours;
110         struct tm *utctime;
111         time_t lasttime;
112         const char *asct;
113         uint8_t bitmask, bitpos;
114
115         hours = ldb_msg_find_ldb_val(msg, "logonHours");
116         if (!hours) {
117                 DEBUG(5,("logon_hours_ok: No hours restrictions for user %s\n", name_for_logs));
118                 return true;
119         }
120
121         if (hours->length != 168/8) {
122                 DEBUG(5,("logon_hours_ok: malformed logon hours restrictions for user %s\n", name_for_logs));
123                 return true;            
124         }
125
126         lasttime = time(NULL);
127         utctime = gmtime(&lasttime);
128         if (!utctime) {
129                 DEBUG(1, ("logon_hours_ok: failed to get gmtime. Failing logon for user %s\n",
130                         name_for_logs));
131                 return false;
132         }
133
134         /* find the corresponding byte and bit */
135         bitpos = (utctime->tm_wday * 24 + utctime->tm_hour) % 168;
136         bitmask = 1 << (bitpos % 8);
137
138         if (! (hours->data[bitpos/8] & bitmask)) {
139                 struct tm *t = localtime(&lasttime);
140                 if (!t) {
141                         asct = "INVALID TIME";
142                 } else {
143                         asct = asctime(t);
144                         if (!asct) {
145                                 asct = "INVALID TIME";
146                         }
147                 }
148                 
149                 DEBUG(1, ("logon_hours_ok: Account for user %s not allowed to "
150                           "logon at this time (%s).\n",
151                           name_for_logs, asct ));
152                 return false;
153         }
154
155         asct = asctime(utctime);
156         DEBUG(5,("logon_hours_ok: user %s allowed to logon at this time (%s)\n",
157                 name_for_logs, asct ? asct : "UNKNOWN TIME" ));
158
159         return true;
160 }
161
162 /****************************************************************************
163  Do a specific test for a SAM_ACCOUNT being valid for this connection
164  (ie not disabled, expired and the like).
165 ****************************************************************************/
166 _PUBLIC_ NTSTATUS authsam_account_ok(TALLOC_CTX *mem_ctx,
167                                      struct ldb_context *sam_ctx,
168                                      uint32_t logon_parameters,
169                                      struct ldb_dn *domain_dn,
170                                      struct ldb_message *msg,
171                                      const char *logon_workstation,
172                                      const char *name_for_logs,
173                                      bool allow_domain_trust,
174                                      bool password_change)
175 {
176         uint16_t acct_flags;
177         const char *workstation_list;
178         NTTIME acct_expiry;
179         NTTIME must_change_time;
180         struct timeval tv_now = timeval_current();
181         NTTIME now = timeval_to_nttime(&tv_now);
182
183         DEBUG(4,("authsam_account_ok: Checking SMB password for user %s\n", name_for_logs));
184
185         acct_flags = samdb_result_acct_flags(msg, "msDS-User-Account-Control-Computed");
186         
187         acct_expiry = samdb_result_account_expires(msg);
188
189         /* Check for when we must change this password, taking the
190          * userAccountControl flags into account */
191         must_change_time = samdb_result_nttime(msg,
192                         "msDS-UserPasswordExpiryTimeComputed", 0);
193
194         workstation_list = ldb_msg_find_attr_as_string(msg, "userWorkstations", NULL);
195
196         /* Quit if the account was disabled. */
197         if (acct_flags & ACB_DISABLED) {
198                 DEBUG(2,("authsam_account_ok: Account for user '%s' was disabled.\n", name_for_logs));
199                 return NT_STATUS_ACCOUNT_DISABLED;
200         }
201
202         /* Quit if the account was locked out. */
203         if (acct_flags & ACB_AUTOLOCK) {
204                 DEBUG(2,("authsam_account_ok: Account for user %s was locked out.\n", name_for_logs));
205                 return NT_STATUS_ACCOUNT_LOCKED_OUT;
206         }
207
208         /* Test account expire time */
209         if (now > acct_expiry) {
210                 DEBUG(2,("authsam_account_ok: Account for user '%s' has expired.\n", name_for_logs));
211                 DEBUG(3,("authsam_account_ok: Account expired at '%s'.\n", 
212                          nt_time_string(mem_ctx, acct_expiry)));
213                 return NT_STATUS_ACCOUNT_EXPIRED;
214         }
215
216         /* check for immediate expiry "must change at next logon" (but not if this is a password change request) */
217         if ((must_change_time == 0) && !password_change) {
218                 DEBUG(2,("sam_account_ok: Account for user '%s' password must change!.\n",
219                          name_for_logs));
220                 return NT_STATUS_PASSWORD_MUST_CHANGE;
221         }
222
223         /* check for expired password (but not if this is a password change request) */
224         if ((must_change_time < now) && !password_change) {
225                 DEBUG(2,("sam_account_ok: Account for user '%s' password expired!.\n",
226                          name_for_logs));
227                 DEBUG(2,("sam_account_ok: Password expired at '%s' unix time.\n",
228                          nt_time_string(mem_ctx, must_change_time)));
229                 return NT_STATUS_PASSWORD_EXPIRED;
230         }
231
232         /* Test workstation. Workstation list is comma separated. */
233         if (logon_workstation && workstation_list && *workstation_list) {
234                 bool invalid_ws = true;
235                 int i;
236                 char **workstations = str_list_make(mem_ctx, workstation_list, ",");
237
238                 for (i = 0; workstations && workstations[i]; i++) {
239                         DEBUG(10,("sam_account_ok: checking for workstation match '%s' and '%s'\n",
240                                   workstations[i], logon_workstation));
241
242                         if (strequal(workstations[i], logon_workstation)) {
243                                 invalid_ws = false;
244                                 break;
245                         }
246                 }
247
248                 talloc_free(workstations);
249
250                 if (invalid_ws) {
251                         return NT_STATUS_INVALID_WORKSTATION;
252                 }
253         }
254         
255         if (!logon_hours_ok(msg, name_for_logs)) {
256                 return NT_STATUS_INVALID_LOGON_HOURS;
257         }
258         
259         if (!allow_domain_trust) {
260                 if (acct_flags & ACB_DOMTRUST) {
261                         DEBUG(2,("sam_account_ok: Domain trust account %s denied by server\n", name_for_logs));
262                         return NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT;
263                 }
264         }
265         if (!(logon_parameters & MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT)) {
266                 if (acct_flags & ACB_SVRTRUST) {
267                         DEBUG(2,("sam_account_ok: Server trust account %s denied by server\n", name_for_logs));
268                         return NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT;
269                 }
270         }
271         if (!(logon_parameters & MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT)) {
272                 /* TODO: this fails with current solaris client. We
273                    need to work with Gordon to work out why */
274                 if (acct_flags & ACB_WSTRUST) {
275                         DEBUG(4,("sam_account_ok: Wksta trust account %s denied by server\n", name_for_logs));
276                         return NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT;
277                 }
278         }
279
280         return NT_STATUS_OK;
281 }
282
283 _PUBLIC_ NTSTATUS authsam_make_user_info_dc(TALLOC_CTX *mem_ctx,
284                                            struct ldb_context *sam_ctx,
285                                            const char *netbios_name,
286                                            const char *domain_name,
287                                            const char *dns_domain_name,
288                                            struct ldb_dn *domain_dn, 
289                                            struct ldb_message *msg,
290                                            DATA_BLOB user_sess_key,
291                                            DATA_BLOB lm_sess_key,
292                                            struct auth_user_info_dc **_user_info_dc)
293 {
294         NTSTATUS status;
295         struct auth_user_info_dc *user_info_dc;
296         struct auth_user_info *info;
297         const char *str, *filter;
298         /* SIDs for the account and his primary group */
299         struct dom_sid *account_sid;
300         const char *primary_group_string;
301         const char *primary_group_dn;
302         DATA_BLOB primary_group_blob;
303         /* SID structures for the expanded group memberships */
304         struct dom_sid *sids = NULL;
305         unsigned int num_sids = 0, i;
306         struct dom_sid *domain_sid;
307         TALLOC_CTX *tmp_ctx;
308         struct ldb_message_element *el;
309
310         user_info_dc = talloc(mem_ctx, struct auth_user_info_dc);
311         NT_STATUS_HAVE_NO_MEMORY(user_info_dc);
312
313         tmp_ctx = talloc_new(user_info_dc);
314         if (user_info_dc == NULL) {
315                 TALLOC_FREE(user_info_dc);
316                 return NT_STATUS_NO_MEMORY;
317         }
318
319         sids = talloc_array(user_info_dc, struct dom_sid, 2);
320         if (sids == NULL) {
321                 TALLOC_FREE(user_info_dc);
322                 return NT_STATUS_NO_MEMORY;
323         }
324
325         num_sids = 2;
326
327         account_sid = samdb_result_dom_sid(user_info_dc, msg, "objectSid");
328         if (account_sid == NULL) {
329                 TALLOC_FREE(user_info_dc);
330                 return NT_STATUS_NO_MEMORY;
331         }
332
333         status = dom_sid_split_rid(tmp_ctx, account_sid, &domain_sid, NULL);
334         if (!NT_STATUS_IS_OK(status)) {
335                 talloc_free(user_info_dc);
336                 return status;
337         }
338
339         sids[PRIMARY_USER_SID_INDEX] = *account_sid;
340         sids[PRIMARY_GROUP_SID_INDEX] = *domain_sid;
341         sid_append_rid(&sids[PRIMARY_GROUP_SID_INDEX], ldb_msg_find_attr_as_uint(msg, "primaryGroupID", ~0));
342
343         /* Filter out builtin groups from this token.  We will search
344          * for builtin groups later, and not include them in the PAC
345          * on SamLogon validation info */
346         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);
347         if (filter == NULL) {
348                 TALLOC_FREE(user_info_dc);
349                 return NT_STATUS_NO_MEMORY;
350         }
351
352         primary_group_string = dom_sid_string(tmp_ctx, &sids[PRIMARY_GROUP_SID_INDEX]);
353         if (primary_group_string == NULL) {
354                 TALLOC_FREE(user_info_dc);
355                 return NT_STATUS_NO_MEMORY;
356         }
357
358         primary_group_dn = talloc_asprintf(tmp_ctx, "<SID=%s>", primary_group_string);
359         if (primary_group_dn == NULL) {
360                 TALLOC_FREE(user_info_dc);
361                 return NT_STATUS_NO_MEMORY;
362         }
363
364         primary_group_blob = data_blob_string_const(primary_group_dn);
365
366         /* Expands the primary group - this function takes in
367          * memberOf-like values, so we fake one up with the
368          * <SID=S-...> format of DN and then let it expand
369          * them, as long as they meet the filter - so only
370          * domain groups, not builtin groups
371          *
372          * The primary group is still treated specially, so we set the
373          * 'only childs' flag to true
374          */
375         status = dsdb_expand_nested_groups(sam_ctx, &primary_group_blob, true, filter,
376                                            user_info_dc, &sids, &num_sids);
377         if (!NT_STATUS_IS_OK(status)) {
378                 talloc_free(user_info_dc);
379                 return status;
380         }
381
382         /* Expands the additional groups */
383         el = ldb_msg_find_element(msg, "memberOf");
384         for (i = 0; el && i < el->num_values; i++) {
385                 /* This function takes in memberOf values and expands
386                  * them, as long as they meet the filter - so only
387                  * domain groups, not builtin groups */
388                 status = dsdb_expand_nested_groups(sam_ctx, &el->values[i], false, filter,
389                                                    user_info_dc, &sids, &num_sids);
390                 if (!NT_STATUS_IS_OK(status)) {
391                         talloc_free(user_info_dc);
392                         return status;
393                 }
394         }
395
396         user_info_dc->sids = sids;
397         user_info_dc->num_sids = num_sids;
398
399         user_info_dc->info = info = talloc_zero(user_info_dc, struct auth_user_info);
400         NT_STATUS_HAVE_NO_MEMORY(user_info_dc->info);
401
402         info->account_name = talloc_steal(info,
403                 ldb_msg_find_attr_as_string(msg, "sAMAccountName", NULL));
404
405         info->user_principal_name = talloc_steal(info,
406                 ldb_msg_find_attr_as_string(msg, "userPrincipalName", NULL));
407         if (info->user_principal_name == NULL && dns_domain_name != NULL) {
408                 info->user_principal_name = talloc_asprintf(info, "%s@%s",
409                                         info->account_name,
410                                         dns_domain_name);
411                 if (info->user_principal_name == NULL) {
412                         TALLOC_FREE(user_info_dc);
413                         return NT_STATUS_NO_MEMORY;
414                 }
415                 info->user_principal_constructed = true;
416         }
417
418         info->domain_name = talloc_strdup(info, domain_name);
419         if (info->domain_name == NULL) {
420                 TALLOC_FREE(user_info_dc);
421                 return NT_STATUS_NO_MEMORY;
422         }
423
424         if (dns_domain_name != NULL) {
425                 info->dns_domain_name = talloc_strdup(info, dns_domain_name);
426                 if (info->dns_domain_name == NULL) {
427                         TALLOC_FREE(user_info_dc);
428                         return NT_STATUS_NO_MEMORY;
429                 }
430         }
431
432         str = ldb_msg_find_attr_as_string(msg, "displayName", "");
433         info->full_name = talloc_strdup(info, str);
434         if (info->full_name == NULL) {
435                 TALLOC_FREE(user_info_dc);
436                 return NT_STATUS_NO_MEMORY;
437         }
438
439         str = ldb_msg_find_attr_as_string(msg, "scriptPath", "");
440         info->logon_script = talloc_strdup(info, str);
441         if (info->logon_script == NULL) {
442                 TALLOC_FREE(user_info_dc);
443                 return NT_STATUS_NO_MEMORY;
444         }
445
446         str = ldb_msg_find_attr_as_string(msg, "profilePath", "");
447         info->profile_path = talloc_strdup(info, str);
448         if (info->profile_path == NULL) {
449                 TALLOC_FREE(user_info_dc);
450                 return NT_STATUS_NO_MEMORY;
451         }
452
453         str = ldb_msg_find_attr_as_string(msg, "homeDirectory", "");
454         info->home_directory = talloc_strdup(info, str);
455         if (info->home_directory == NULL) {
456                 TALLOC_FREE(user_info_dc);
457                 return NT_STATUS_NO_MEMORY;
458         }
459
460         str = ldb_msg_find_attr_as_string(msg, "homeDrive", "");
461         info->home_drive = talloc_strdup(info, str);
462         if (info->home_drive == NULL) {
463                 TALLOC_FREE(user_info_dc);
464                 return NT_STATUS_NO_MEMORY;
465         }
466
467         info->logon_server = talloc_strdup(info, netbios_name);
468         if (info->logon_server == NULL) {
469                 TALLOC_FREE(user_info_dc);
470                 return NT_STATUS_NO_MEMORY;
471         }
472
473         info->last_logon = samdb_result_nttime(msg, "lastLogon", 0);
474         info->last_logoff = samdb_result_last_logoff(msg);
475         info->acct_expiry = samdb_result_account_expires(msg);
476         info->last_password_change = samdb_result_nttime(msg,
477                 "pwdLastSet", 0);
478         info->allow_password_change
479                 = samdb_result_allow_password_change(sam_ctx, mem_ctx, 
480                         domain_dn, msg, "pwdLastSet");
481         info->force_password_change = samdb_result_nttime(msg,
482                 "msDS-UserPasswordExpiryTimeComputed", 0);
483         info->logon_count = ldb_msg_find_attr_as_uint(msg, "logonCount", 0);
484         info->bad_password_count = ldb_msg_find_attr_as_uint(msg, "badPwdCount",
485                 0);
486
487         info->acct_flags = samdb_result_acct_flags(msg, "msDS-User-Account-Control-Computed");
488
489         user_info_dc->user_session_key = data_blob_talloc(user_info_dc,
490                                                          user_sess_key.data,
491                                                          user_sess_key.length);
492         if (user_sess_key.data) {
493                 if (user_info_dc->user_session_key.data == NULL) {
494                         TALLOC_FREE(user_info_dc);
495                         return NT_STATUS_NO_MEMORY;
496                 }
497         }
498         user_info_dc->lm_session_key = data_blob_talloc(user_info_dc,
499                                                        lm_sess_key.data,
500                                                        lm_sess_key.length);
501         if (lm_sess_key.data) {
502                 if (user_info_dc->lm_session_key.data == NULL) {
503                         TALLOC_FREE(user_info_dc);
504                         return NT_STATUS_NO_MEMORY;
505                 }
506         }
507
508         if (info->acct_flags & ACB_SVRTRUST) {
509                 /* the SID_NT_ENTERPRISE_DCS SID gets added into the
510                    PAC */
511                 user_info_dc->sids = talloc_realloc(user_info_dc,
512                                                    user_info_dc->sids,
513                                                    struct dom_sid,
514                                                    user_info_dc->num_sids+1);
515                 if (user_info_dc->sids == NULL) {
516                         TALLOC_FREE(user_info_dc);
517                         return NT_STATUS_NO_MEMORY;
518                 }
519                 user_info_dc->sids[user_info_dc->num_sids] = global_sid_Enterprise_DCs;
520                 user_info_dc->num_sids++;
521         }
522
523         if ((info->acct_flags & (ACB_PARTIAL_SECRETS_ACCOUNT | ACB_WSTRUST)) ==
524             (ACB_PARTIAL_SECRETS_ACCOUNT | ACB_WSTRUST)) {
525                 /* the DOMAIN_RID_ENTERPRISE_READONLY_DCS PAC */
526                 user_info_dc->sids = talloc_realloc(user_info_dc,
527                                                    user_info_dc->sids,
528                                                    struct dom_sid,
529                                                    user_info_dc->num_sids+1);
530                 if (user_info_dc->sids == NULL) {
531                         TALLOC_FREE(user_info_dc);
532                         return NT_STATUS_NO_MEMORY;
533                 }
534                 user_info_dc->sids[user_info_dc->num_sids] = *domain_sid;
535                 sid_append_rid(&user_info_dc->sids[user_info_dc->num_sids],
536                             DOMAIN_RID_ENTERPRISE_READONLY_DCS);
537                 user_info_dc->num_sids++;
538         }
539
540         info->authenticated = true;
541
542         talloc_free(tmp_ctx);
543         *_user_info_dc = user_info_dc;
544
545         return NT_STATUS_OK;
546 }
547
548 NTSTATUS sam_get_results_principal(struct ldb_context *sam_ctx,
549                                    TALLOC_CTX *mem_ctx, const char *principal,
550                                    const char **attrs,
551                                    struct ldb_dn **domain_dn,
552                                    struct ldb_message **msg)
553 {                          
554         struct ldb_dn *user_dn;
555         NTSTATUS nt_status;
556         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
557         int ret;
558
559         if (!tmp_ctx) {
560                 return NT_STATUS_NO_MEMORY;
561         }
562
563         nt_status = crack_user_principal_name(sam_ctx, tmp_ctx, principal, 
564                                               &user_dn, domain_dn);
565         if (!NT_STATUS_IS_OK(nt_status)) {
566                 talloc_free(tmp_ctx);
567                 return nt_status;
568         }
569         
570         /* pull the user attributes */
571         ret = dsdb_search_one(sam_ctx, tmp_ctx, msg, user_dn,
572                               LDB_SCOPE_BASE, attrs,
573                               DSDB_SEARCH_SHOW_EXTENDED_DN | DSDB_SEARCH_NO_GLOBAL_CATALOG,
574                               "(objectClass=*)");
575         if (ret != LDB_SUCCESS) {
576                 talloc_free(tmp_ctx);
577                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
578         }
579         talloc_steal(mem_ctx, *msg);
580         talloc_steal(mem_ctx, *domain_dn);
581         talloc_free(tmp_ctx);
582         
583         return NT_STATUS_OK;
584 }
585
586 /* Used in the gensec_gssapi and gensec_krb5 server-side code, where the PAC isn't available, and for tokenGroups in the DSDB stack.
587
588  Supply either a principal or a DN
589 */
590 NTSTATUS authsam_get_user_info_dc_principal(TALLOC_CTX *mem_ctx,
591                                            struct loadparm_context *lp_ctx,
592                                            struct ldb_context *sam_ctx,
593                                            const char *principal,
594                                            struct ldb_dn *user_dn,
595                                            struct auth_user_info_dc **user_info_dc)
596 {
597         NTSTATUS nt_status;
598         DATA_BLOB user_sess_key = data_blob(NULL, 0);
599         DATA_BLOB lm_sess_key = data_blob(NULL, 0);
600
601         struct ldb_message *msg;
602         struct ldb_dn *domain_dn;
603
604         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
605         if (!tmp_ctx) {
606                 return NT_STATUS_NO_MEMORY;
607         }
608
609         if (principal) {
610                 nt_status = sam_get_results_principal(sam_ctx, tmp_ctx, principal,
611                                                       user_attrs, &domain_dn, &msg);
612                 if (!NT_STATUS_IS_OK(nt_status)) {
613                         talloc_free(tmp_ctx);
614                         return nt_status;
615                 }
616         } else if (user_dn) {
617                 struct dom_sid *user_sid, *domain_sid;
618                 int ret;
619                 /* pull the user attributes */
620                 ret = dsdb_search_one(sam_ctx, tmp_ctx, &msg, user_dn,
621                                       LDB_SCOPE_BASE, user_attrs,
622                                       DSDB_SEARCH_SHOW_EXTENDED_DN | DSDB_SEARCH_NO_GLOBAL_CATALOG,
623                                       "(objectClass=*)");
624                 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
625                         talloc_free(tmp_ctx);
626                         return NT_STATUS_NO_SUCH_USER;
627                 } else if (ret != LDB_SUCCESS) {
628                         talloc_free(tmp_ctx);
629                         return NT_STATUS_INTERNAL_DB_CORRUPTION;
630                 }
631
632                 user_sid = samdb_result_dom_sid(msg, msg, "objectSid");
633
634                 nt_status = dom_sid_split_rid(tmp_ctx, user_sid, &domain_sid, NULL);
635                 if (!NT_STATUS_IS_OK(nt_status)) {
636                         return nt_status;
637                 }
638
639                 domain_dn = samdb_search_dn(sam_ctx, mem_ctx, NULL,
640                                           "(&(objectSid=%s)(objectClass=domain))",
641                                             ldap_encode_ndr_dom_sid(tmp_ctx, domain_sid));
642                 if (!domain_dn) {
643                         DEBUG(3, ("authsam_get_user_info_dc_principal: Failed to find domain with: SID %s\n",
644                                   dom_sid_string(tmp_ctx, domain_sid)));
645                         return NT_STATUS_NO_SUCH_USER;
646                 }
647
648         } else {
649                 return NT_STATUS_INVALID_PARAMETER;
650         }
651
652         nt_status = authsam_make_user_info_dc(tmp_ctx, sam_ctx,
653                                              lpcfg_netbios_name(lp_ctx),
654                                              lpcfg_sam_name(lp_ctx),
655                                              lpcfg_sam_dnsname(lp_ctx),
656                                              domain_dn,
657                                              msg,
658                                              user_sess_key, lm_sess_key,
659                                              user_info_dc);
660         if (!NT_STATUS_IS_OK(nt_status)) {
661                 talloc_free(tmp_ctx);
662                 return nt_status;
663         }
664
665         talloc_steal(mem_ctx, *user_info_dc);
666         talloc_free(tmp_ctx);
667
668         return NT_STATUS_OK;
669 }
670
671 NTSTATUS authsam_update_bad_pwd_count(struct ldb_context *sam_ctx,
672                                       struct ldb_message *msg,
673                                       struct ldb_dn *domain_dn)
674 {
675         const char *attrs[] = { "lockoutThreshold",
676                                 "lockOutObservationWindow",
677                                 "lockoutDuration",
678                                 "pwdProperties",
679                                 NULL };
680         int ret;
681         NTSTATUS status;
682         struct ldb_result *domain_res;
683         struct ldb_message *msg_mod = NULL;
684         TALLOC_CTX *mem_ctx;
685
686         mem_ctx = talloc_new(msg);
687         if (mem_ctx == NULL) {
688                 return NT_STATUS_NO_MEMORY;
689         }
690
691         ret = dsdb_search_dn(sam_ctx, mem_ctx, &domain_res, domain_dn, attrs, 0);
692         if (ret != LDB_SUCCESS) {
693                 TALLOC_FREE(mem_ctx);
694                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
695         }
696
697         status = dsdb_update_bad_pwd_count(mem_ctx, sam_ctx,
698                                            msg, domain_res->msgs[0], &msg_mod);
699         if (!NT_STATUS_IS_OK(status)) {
700                 TALLOC_FREE(mem_ctx);
701                 return status;
702         }
703
704         if (msg_mod != NULL) {
705                 ret = dsdb_modify(sam_ctx, msg_mod, 0);
706                 if (ret != LDB_SUCCESS) {
707                         DEBUG(0, ("Failed to update badPwdCount, badPasswordTime or set lockoutTime on %s: %s\n",
708                                   ldb_dn_get_linearized(msg_mod->dn), ldb_errstring(sam_ctx)));
709                         TALLOC_FREE(mem_ctx);
710                         return NT_STATUS_INTERNAL_ERROR;
711                 }
712         }
713
714         TALLOC_FREE(mem_ctx);
715         return NT_STATUS_OK;
716 }
717
718
719 static NTSTATUS authsam_update_lastlogon_timestamp(struct ldb_context *sam_ctx,
720                                             struct ldb_message *msg_mod,
721                                             struct ldb_dn *domain_dn,
722                                             NTTIME old_timestamp,
723                                             NTTIME now)
724 {
725         /*
726          * We only set lastLogonTimestamp if the current value is older than
727          * now - msDS-LogonTimeSyncInterval days.
728          *
729          * msDS-LogonTimeSyncInterval is an int32_t number of days, while
730          * lastLogonTimestamp is in the 64 bit 100ns NTTIME format.
731          *
732          * The docs say: "the initial update, after the domain functional
733          * level is raised to DS_BEHAVIOR_WIN2003 or higher, is calculated as
734          * 14 days minus a random percentage of 5 days", but we aren't doing
735          * that. The blogosphere seems to think that this randomised update
736          * happens everytime, but [MS-ADA1] doesn't agree.
737          *
738          * Dochelp referred us to the following blog post:
739          * http://blogs.technet.com/b/askds/archive/2009/04/15/the-lastlogontimestamp-attribute-what-it-was-designed-for-and-how-it-works.aspx
740          *
741          * en msDS-LogonTimeSyncInterval is zero, the lastLogonTimestamp is
742          * not changed.
743          */
744         static const char *attrs[] = { "msDS-LogonTimeSyncInterval",
745                                         NULL };
746         int ret;
747         struct ldb_result *domain_res = NULL;
748         TALLOC_CTX *mem_ctx = NULL;
749         int32_t sync_interval;
750         NTTIME sync_interval_nt;
751
752         mem_ctx = talloc_new(msg_mod);
753         if (mem_ctx == NULL) {
754                 return NT_STATUS_NO_MEMORY;
755         }
756
757         ret = dsdb_search_dn(sam_ctx, mem_ctx, &domain_res, domain_dn, attrs,
758                              0);
759         if (ret != LDB_SUCCESS || domain_res->count != 1) {
760                 TALLOC_FREE(mem_ctx);
761                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
762         }
763
764         sync_interval = ldb_msg_find_attr_as_int(domain_res->msgs[0],
765                                                  "msDS-LogonTimeSyncInterval",
766                                                  14);
767         DEBUG(5, ("sync interval is %d\n", sync_interval));
768         if (sync_interval == 0){
769                 /*
770                  * Setting msDS-LogonTimeSyncInterval to zero is how you ask
771                  * that nothing happens here.
772                  */
773                 TALLOC_FREE(mem_ctx);
774                 return NT_STATUS_OK;
775         }
776         else if (sync_interval >= 5){
777                 /*
778                  * Subtract "a random percentage of 5" days. Presumably this
779                  * percentage is between 0 and 100, and modulus is accurate
780                  * enough.
781                  */
782                 uint32_t r = generate_random() % 6;
783                 sync_interval -= r;
784                 DEBUG(5, ("randomised sync interval is %d (-%d)\n", sync_interval, r));
785         }
786         /* In the case where sync_interval < 5 there is no randomisation */
787
788         sync_interval_nt = sync_interval * 24LL * 3600LL * 10000000LL;
789
790         DEBUG(5, ("old timestamp is %lld, threshold %lld, diff %lld\n",
791                   (long long int)old_timestamp,
792                   (long long int)(now - sync_interval_nt),
793                   (long long int)(old_timestamp - now + sync_interval_nt)));
794
795         if (old_timestamp > now){
796                 DEBUG(0, ("lastLogonTimestamp is in the future! (%lld > %lld)\n",
797                           (long long int)old_timestamp, (long long int)now));
798                 /* then what? */
799
800         } else if (old_timestamp < now - sync_interval_nt){
801                 DEBUG(5, ("updating lastLogonTimestamp to %lld\n",
802                           (long long int)now));
803
804                 /* The time has come to update lastLogonTimestamp */
805                 ret = samdb_msg_add_int64(sam_ctx, msg_mod, msg_mod,
806                                           "lastLogonTimestamp", now);
807
808                 if (ret != LDB_SUCCESS) {
809                         TALLOC_FREE(mem_ctx);
810                         return NT_STATUS_NO_MEMORY;
811                 }
812         }
813         TALLOC_FREE(mem_ctx);
814         return NT_STATUS_OK;
815 }
816
817
818
819 /* Reset the badPwdCount to zero and update the lastLogon time. */
820 NTSTATUS authsam_logon_success_accounting(struct ldb_context *sam_ctx,
821                                           const struct ldb_message *msg,
822                                           struct ldb_dn *domain_dn,
823                                           bool interactive_or_kerberos)
824 {
825         int ret;
826         NTSTATUS status;
827         int badPwdCount;
828         int64_t lockoutTime;
829         struct ldb_message *msg_mod;
830         TALLOC_CTX *mem_ctx;
831         struct timeval tv_now;
832         NTTIME now;
833         NTTIME lastLogonTimestamp;
834         bool am_rodc = false;
835
836         mem_ctx = talloc_new(msg);
837         if (mem_ctx == NULL) {
838                 return NT_STATUS_NO_MEMORY;
839         }
840
841         lockoutTime = ldb_msg_find_attr_as_int64(msg, "lockoutTime", 0);
842         if (interactive_or_kerberos) {
843                 badPwdCount = ldb_msg_find_attr_as_int(msg, "badPwdCount", 0);
844         } else {
845                 badPwdCount = samdb_result_effective_badPwdCount(sam_ctx, mem_ctx,
846                                                                  domain_dn, msg);
847         }
848         lastLogonTimestamp =
849                 ldb_msg_find_attr_as_int64(msg, "lastLogonTimestamp", 0);
850
851         DEBUG(5, ("lastLogonTimestamp is %lld\n",
852                   (long long int)lastLogonTimestamp));
853
854         msg_mod = ldb_msg_new(mem_ctx);
855         if (msg_mod == NULL) {
856                 TALLOC_FREE(mem_ctx);
857                 return NT_STATUS_NO_MEMORY;
858         }
859         msg_mod->dn = msg->dn;
860
861         if (lockoutTime != 0) {
862                 /*
863                  * This implies "badPwdCount" = 0, see samldb_lockout_time()
864                  */
865                 ret = samdb_msg_add_int(sam_ctx, msg_mod, msg_mod, "lockoutTime", 0);
866                 if (ret != LDB_SUCCESS) {
867                         TALLOC_FREE(mem_ctx);
868                         return NT_STATUS_NO_MEMORY;
869                 }
870         } else if (badPwdCount != 0) {
871                 ret = samdb_msg_add_int(sam_ctx, msg_mod, msg_mod, "badPwdCount", 0);
872                 if (ret != LDB_SUCCESS) {
873                         TALLOC_FREE(mem_ctx);
874                         return NT_STATUS_NO_MEMORY;
875                 }
876         }
877
878         tv_now = timeval_current();
879         now = timeval_to_nttime(&tv_now);
880
881         if (interactive_or_kerberos ||
882             (badPwdCount != 0 && lockoutTime == 0)) {
883                 ret = samdb_msg_add_int64(sam_ctx, msg_mod, msg_mod,
884                                           "lastLogon", now);
885                 if (ret != LDB_SUCCESS) {
886                         TALLOC_FREE(mem_ctx);
887                         return NT_STATUS_NO_MEMORY;
888                 }
889         }
890
891         if (interactive_or_kerberos) {
892                 int logonCount;
893
894                 logonCount = ldb_msg_find_attr_as_int(msg, "logonCount", 0);
895
896                 logonCount += 1;
897
898                 ret = samdb_msg_add_int(sam_ctx, msg_mod, msg_mod,
899                                         "logonCount", logonCount);
900                 if (ret != LDB_SUCCESS) {
901                         TALLOC_FREE(mem_ctx);
902                         return NT_STATUS_NO_MEMORY;
903                 }
904         } else {
905                 /* Set an unset logonCount to 0 on first successful login */
906                 if (ldb_msg_find_ldb_val(msg, "logonCount") == NULL) {
907                         ret = samdb_msg_add_int(sam_ctx, msg_mod, msg_mod,
908                                                 "logonCount", 0);
909                         if (ret != LDB_SUCCESS) {
910                                 TALLOC_FREE(mem_ctx);
911                                 return NT_STATUS_NO_MEMORY;
912                         }
913                 }
914         }
915
916         ret = samdb_rodc(sam_ctx, &am_rodc);
917         if (ret != LDB_SUCCESS) {
918                 TALLOC_FREE(mem_ctx);
919                 return NT_STATUS_INTERNAL_ERROR;
920         }
921
922         if (!am_rodc) {
923                 /* TODO Perform the (async) SendToSAM calls for MS-SAMS */
924                 status = authsam_update_lastlogon_timestamp(sam_ctx, msg_mod, domain_dn,
925                                                             lastLogonTimestamp, now);
926                 if (!NT_STATUS_IS_OK(status)) {
927                         TALLOC_FREE(mem_ctx);
928                         return NT_STATUS_NO_MEMORY;
929                 }
930         }
931
932         if (msg_mod->num_elements > 0) {
933                 ret = dsdb_replace(sam_ctx, msg_mod, 0);
934                 if (ret != LDB_SUCCESS) {
935                         DEBUG(0, ("Failed to set badPwdCount and lockoutTime "
936                                   "to 0 and/or  lastlogon to now (%lld) "
937                                   "%s: %s\n", (long long int)now,
938                                   ldb_dn_get_linearized(msg_mod->dn),
939                                   ldb_errstring(sam_ctx)));
940                         TALLOC_FREE(mem_ctx);
941                         return NT_STATUS_INTERNAL_ERROR;
942                 }
943         }
944         TALLOC_FREE(mem_ctx);
945         return NT_STATUS_OK;
946 }