docs-xml: fix use of <smbconfoption> tag (fix bug #9345)
[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-2004
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 "../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
37 #define KRBTGT_ATTRS \
38         /* required for the krb5 kdc */         \
39         "objectClass",                          \
40         "sAMAccountName",                       \
41         "userPrincipalName",                    \
42         "servicePrincipalName",                 \
43         "msDS-KeyVersionNumber",                \
44         "supplementalCredentials",              \
45                                                 \
46         /* passwords */                         \
47         "dBCSPwd",                              \
48         "unicodePwd",                           \
49                                                 \
50         "userAccountControl",                   \
51         "objectSid",                            \
52                                                 \
53         "pwdLastSet",                           \
54         "accountExpires"                        
55
56 const char *krbtgt_attrs[] = {
57         KRBTGT_ATTRS
58 };
59
60 const char *server_attrs[] = {
61         KRBTGT_ATTRS
62 };
63
64 const char *user_attrs[] = {
65         KRBTGT_ATTRS,
66
67         "logonHours",
68
69         /* check 'allowed workstations' */
70         "userWorkstations",
71                        
72         /* required for server_info, 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,
86 };
87
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 ****************************************************************************/
93                                                                                                               
94 static bool logon_hours_ok(struct ldb_message *msg, const char *name_for_logs)
95 {
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;
102
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;
107         }
108
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;            
112         }
113
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;
120         }
121
122         /* find the corresponding byte and bit */
123         bitpos = (utctime->tm_wday * 24 + utctime->tm_hour) % 168;
124         bitmask = 1 << (bitpos % 8);
125
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";
134                         }
135                 }
136                 
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;
141         }
142
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" ));
146
147         return true;
148 }
149
150 /****************************************************************************
151  Do a specific test for a SAM_ACCOUNT being vaild 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)
163 {
164         uint16_t acct_flags;
165         const char *workstation_list;
166         NTTIME acct_expiry;
167         NTTIME must_change_time;
168
169         NTTIME now;
170         DEBUG(4,("authsam_account_ok: Checking SMB password for user %s\n", name_for_logs));
171
172         acct_flags = samdb_result_acct_flags(sam_ctx, mem_ctx, msg, domain_dn);
173         
174         acct_expiry = samdb_result_account_expires(msg);
175
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);
180
181         workstation_list = samdb_result_string(msg, "userWorkstations", NULL);
182
183         /* Quit if the account was disabled. */
184         if (acct_flags & ACB_DISABLED) {
185                 DEBUG(1,("authsam_account_ok: Account for user '%s' was disabled.\n", name_for_logs));
186                 return NT_STATUS_ACCOUNT_DISABLED;
187         }
188
189         /* Quit if the account was locked out. */
190         if (acct_flags & ACB_AUTOLOCK) {
191                 DEBUG(1,("authsam_account_ok: Account for user %s was locked out.\n", name_for_logs));
192                 return NT_STATUS_ACCOUNT_LOCKED_OUT;
193         }
194
195         /* Test account expire time */
196         unix_to_nt_time(&now, time(NULL));
197         if (now > acct_expiry) {
198                 DEBUG(1,("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;
202         }
203
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(1,("sam_account_ok: Account for user '%s' password must change!.\n", 
207                          name_for_logs));
208                 return NT_STATUS_PASSWORD_MUST_CHANGE;
209         }
210
211         /* check for expired password (but not if this is a password change request) */
212         if ((must_change_time < now) && !password_change) {
213                 DEBUG(1,("sam_account_ok: Account for user '%s' password expired!.\n", 
214                          name_for_logs));
215                 DEBUG(1,("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;
218         }
219
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, ",");
225                 
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));
229
230                         if (strequal(workstations[i], logon_workstation)) {
231                                 invalid_ws = false;
232                                 break;
233                         }
234                 }
235
236                 talloc_free(workstations);
237
238                 if (invalid_ws) {
239                         return NT_STATUS_INVALID_WORKSTATION;
240                 }
241         }
242         
243         if (!logon_hours_ok(msg, name_for_logs)) {
244                 return NT_STATUS_INVALID_LOGON_HOURS;
245         }
246         
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;
251                 }
252         }
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;
257                 }
258         }
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;
265                 }
266         }
267
268         return NT_STATUS_OK;
269 }
270
271 /* This function tests if a SID structure "sids" contains the SID "sid" */
272 static bool sids_contains_sid(const struct dom_sid **sids, const int num_sids,
273         const struct dom_sid *sid)
274 {
275         int i;
276
277         for (i = 0; i < num_sids; i++) {
278                 if (dom_sid_equal(sids[i], sid))
279                         return true;
280         }
281         return false;
282 }
283
284 /*
285  * This function generates the transitive closure of a given SID "sid" (it
286  * basically expands nested groups of a SID).
287  * If the SID isn't located in the "res_sids" structure yet and the
288  * "only_childs" flag is negative, we add it to "res_sids".
289  * Then we've always to consider the "memberOf" attributes. We invoke the
290  * function recursively on each item of it with the "only_childs" flag set to
291  * "false".
292  * The "only_childs" flag is particularly useful if you have a user SID and
293  * want to include all his groups (referenced with "memberOf") without his SID
294  * itself.
295  *
296  * At the beginning "res_sids" should reference to a NULL pointer.
297  */
298 static NTSTATUS authsam_expand_nested_groups(struct ldb_context *sam_ctx,
299         const struct dom_sid *sid, const bool only_childs,
300         TALLOC_CTX *res_sids_ctx, struct dom_sid ***res_sids,
301         int *num_res_sids)
302 {
303         const char * const attrs[] = { "memberOf", NULL };
304         int i, ret;
305         bool already_there;
306         struct ldb_dn *tmp_dn;
307         struct dom_sid *tmp_sid;
308         TALLOC_CTX *tmp_ctx;
309         struct ldb_message **res;
310         NTSTATUS status;
311
312         if (*res_sids == NULL) {
313                 *num_res_sids = 0;
314         }
315
316         if (sid == NULL) {
317                 return NT_STATUS_OK;
318         }
319
320         already_there = sids_contains_sid((const struct dom_sid**) *res_sids,
321                 *num_res_sids, sid);
322         if (already_there) {
323                 return NT_STATUS_OK;
324         }
325
326         if (!only_childs) {
327                 tmp_sid = dom_sid_dup(res_sids_ctx, sid);
328                 NT_STATUS_HAVE_NO_MEMORY(tmp_sid);
329                 *res_sids = talloc_realloc(res_sids_ctx, *res_sids,
330                         struct dom_sid *, *num_res_sids + 1);
331                 NT_STATUS_HAVE_NO_MEMORY(*res_sids);
332                 (*res_sids)[*num_res_sids] = tmp_sid;
333                 ++(*num_res_sids);
334         }
335
336         tmp_ctx = talloc_new(sam_ctx);
337
338         ret = gendb_search(sam_ctx, tmp_ctx, NULL, &res, attrs,
339                 "objectSid=%s", ldap_encode_ndr_dom_sid(tmp_ctx, sid));
340         if (ret != 1) {
341                 talloc_free(tmp_ctx);
342                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
343         }
344
345         if (res[0]->num_elements == 0) {
346                 talloc_free(res);
347                 talloc_free(tmp_ctx);
348                 return NT_STATUS_OK;
349         }
350
351         for (i = 0; i < res[0]->elements[0].num_values; i++) {
352                 tmp_dn = ldb_dn_from_ldb_val(tmp_ctx, sam_ctx,
353                         &res[0]->elements[0].values[i]);
354                 tmp_sid = samdb_search_dom_sid(sam_ctx, tmp_ctx, tmp_dn,
355                         "objectSid", NULL);
356
357                 status = authsam_expand_nested_groups(sam_ctx, tmp_sid,
358                         false, res_sids_ctx, res_sids, num_res_sids);
359                 if (!NT_STATUS_IS_OK(status)) {
360                         talloc_free(res);
361                         talloc_free(tmp_ctx);
362                         return status;
363                 }
364         }
365
366         talloc_free(res);
367         talloc_free(tmp_ctx);
368
369         return NT_STATUS_OK;
370 }
371
372 _PUBLIC_ NTSTATUS authsam_make_server_info(TALLOC_CTX *mem_ctx,
373                                            struct ldb_context *sam_ctx,
374                                            const char *netbios_name,
375                                            const char *domain_name,
376                                            struct ldb_dn *domain_dn, 
377                                            struct ldb_message *msg,
378                                            DATA_BLOB user_sess_key,
379                                            DATA_BLOB lm_sess_key,
380                                            struct auth_serversupplied_info
381                                                    **_server_info)
382 {
383         NTSTATUS status;
384         struct auth_serversupplied_info *server_info;
385         const char *str;
386         struct dom_sid *tmp_sid;
387         /* SIDs for the account and his primary group */
388         struct dom_sid *account_sid;
389         struct dom_sid *primary_group_sid;
390         /* SID structures for the expanded group memberships */
391         struct dom_sid **groupSIDs = NULL, **groupSIDs_2 = NULL;
392         int num_groupSIDs = 0, num_groupSIDs_2 = 0, i;
393         uint32_t userAccountControl;
394
395         server_info = talloc(mem_ctx, struct auth_serversupplied_info);
396         NT_STATUS_HAVE_NO_MEMORY(server_info);
397
398         account_sid = samdb_result_dom_sid(server_info, msg, "objectSid");
399         NT_STATUS_HAVE_NO_MEMORY_AND_FREE(account_sid, server_info);
400
401         primary_group_sid = dom_sid_add_rid(server_info,
402                 samdb_domain_sid(sam_ctx),
403                 samdb_result_uint(msg, "primaryGroupID", ~0));
404         NT_STATUS_HAVE_NO_MEMORY_AND_FREE(primary_group_sid, server_info);
405
406         /* Expands the primary group */
407         status = authsam_expand_nested_groups(sam_ctx, primary_group_sid, false,
408                                               server_info, &groupSIDs, &num_groupSIDs);
409         if (!NT_STATUS_IS_OK(status)) {
410                 talloc_free(server_info);
411                 return status;
412         }
413
414         /* Expands the additional groups */
415         status = authsam_expand_nested_groups(sam_ctx, account_sid, true,
416                 server_info, &groupSIDs_2, &num_groupSIDs_2);
417         if (!NT_STATUS_IS_OK(status)) {
418                 talloc_free(server_info);
419                 return status;
420         }
421
422         /* Merge the two expanded structures (groupSIDs, groupSIDs_2) */
423         for (i = 0; i < num_groupSIDs_2; i++)
424                 if (!sids_contains_sid((const struct dom_sid **) groupSIDs,
425                                 num_groupSIDs, groupSIDs_2[i])) {
426                         tmp_sid = dom_sid_dup(server_info, groupSIDs_2[i]);
427                         NT_STATUS_HAVE_NO_MEMORY_AND_FREE(tmp_sid, server_info);
428                         groupSIDs = talloc_realloc(server_info, groupSIDs,
429                                 struct dom_sid *, num_groupSIDs + 1);
430                         NT_STATUS_HAVE_NO_MEMORY_AND_FREE(groupSIDs,
431                                 server_info);
432                         groupSIDs[num_groupSIDs] = tmp_sid;
433                         ++num_groupSIDs;
434                 }
435         talloc_free(groupSIDs_2);
436
437         server_info->account_sid = account_sid;
438         server_info->primary_group_sid = primary_group_sid;
439         
440         /* DCs also get SID_NT_ENTERPRISE_DCS */
441         userAccountControl = ldb_msg_find_attr_as_uint(msg, "userAccountControl", 0);
442         if (userAccountControl & UF_SERVER_TRUST_ACCOUNT) {
443                 groupSIDs = talloc_realloc(server_info, groupSIDs, struct dom_sid *,
444                                            num_groupSIDs+1);
445                 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(groupSIDs, server_info);
446                 groupSIDs[num_groupSIDs] = dom_sid_parse_talloc(groupSIDs, SID_NT_ENTERPRISE_DCS);
447                 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(groupSIDs[num_groupSIDs], server_info);
448                 num_groupSIDs++;
449         }
450
451         server_info->domain_groups = groupSIDs;
452         server_info->n_domain_groups = num_groupSIDs;
453
454         server_info->account_name = talloc_steal(server_info,
455                 samdb_result_string(msg, "sAMAccountName", NULL));
456
457         server_info->domain_name = talloc_strdup(server_info, domain_name);
458         NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->domain_name,
459                 server_info);
460
461         str = samdb_result_string(msg, "displayName", "");
462         server_info->full_name = talloc_strdup(server_info, str);
463         NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->full_name, server_info);
464
465         str = samdb_result_string(msg, "scriptPath", "");
466         server_info->logon_script = talloc_strdup(server_info, str);
467         NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->logon_script,
468                 server_info);
469
470         str = samdb_result_string(msg, "profilePath", "");
471         server_info->profile_path = talloc_strdup(server_info, str);
472         NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->profile_path,
473                 server_info);
474
475         str = samdb_result_string(msg, "homeDirectory", "");
476         server_info->home_directory = talloc_strdup(server_info, str);
477         NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->home_directory,
478                 server_info);
479
480         str = samdb_result_string(msg, "homeDrive", "");
481         server_info->home_drive = talloc_strdup(server_info, str);
482         NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->home_drive, server_info);
483
484         server_info->logon_server = talloc_strdup(server_info, netbios_name);
485         NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->logon_server,
486                 server_info);
487
488         server_info->last_logon = samdb_result_nttime(msg, "lastLogon", 0);
489         server_info->last_logoff = samdb_result_last_logoff(msg);
490         server_info->acct_expiry = samdb_result_account_expires(msg);
491         server_info->last_password_change = samdb_result_nttime(msg,
492                 "pwdLastSet", 0);
493         server_info->allow_password_change
494                 = samdb_result_allow_password_change(sam_ctx, mem_ctx, 
495                         domain_dn, msg, "pwdLastSet");
496         server_info->force_password_change
497                 = samdb_result_force_password_change(sam_ctx, mem_ctx,
498                         domain_dn, msg);
499         server_info->logon_count = samdb_result_uint(msg, "logonCount", 0);
500         server_info->bad_password_count = samdb_result_uint(msg, "badPwdCount",
501                 0);
502
503         server_info->acct_flags = samdb_result_acct_flags(sam_ctx, mem_ctx, 
504                                                           msg, domain_dn);
505
506         server_info->user_session_key = data_blob_talloc_reference(server_info,
507                 &user_sess_key);
508         server_info->lm_session_key = data_blob_talloc_reference(server_info,
509                 &lm_sess_key);
510
511         server_info->authenticated = true;
512
513         *_server_info = server_info;
514
515         return NT_STATUS_OK;
516 }
517
518 NTSTATUS sam_get_results_principal(struct ldb_context *sam_ctx,
519                                    TALLOC_CTX *mem_ctx, const char *principal,
520                                    const char **attrs,
521                                    struct ldb_dn **domain_dn,
522                                    struct ldb_message **msg)
523 {                          
524         struct ldb_dn *user_dn;
525         NTSTATUS nt_status;
526         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
527         int ret;
528
529         if (!tmp_ctx) {
530                 return NT_STATUS_NO_MEMORY;
531         }
532
533         nt_status = crack_user_principal_name(sam_ctx, tmp_ctx, principal, 
534                                               &user_dn, domain_dn);
535         if (!NT_STATUS_IS_OK(nt_status)) {
536                 talloc_free(tmp_ctx);
537                 return nt_status;
538         }
539         
540         /* pull the user attributes */
541         ret = gendb_search_single_extended_dn(sam_ctx, tmp_ctx, user_dn,
542                 LDB_SCOPE_BASE, msg, attrs, "(objectClass=*)");
543         if (ret != LDB_SUCCESS) {
544                 talloc_free(tmp_ctx);
545                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
546         }
547         talloc_steal(mem_ctx, *msg);
548         talloc_steal(mem_ctx, *domain_dn);
549         talloc_free(tmp_ctx);
550         
551         return NT_STATUS_OK;
552 }