/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- logon_hours_ok
- authsam_account_ok
- authsam_make_server_info
- sam_get_results_principal
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
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "system/time.h"
24 #include "auth/auth.h"
25 #include <ldb.h>
26 #include "../lib/util/util_ldb.h"
27 #include "dsdb/samdb/samdb.h"
28 #include "libcli/security/security.h"
29 #include "libcli/ldap/ldap.h"
30 #include "librpc/gen_ndr/ndr_netlogon.h"
31 #include "param/param.h"
32 #include "auth/auth_sam.h"
33
34 const char *user_attrs[] = {
35 /* required for the krb5 kdc */
36 "objectClass",
37 "sAMAccountName",
38 "userPrincipalName",
39 "servicePrincipalName",
40 "msDS-KeyVersionNumber",
41 "supplementalCredentials",
42
43 /* passwords */
44 "dBCSPwd",
45 "unicodePwd",
46
47 "userAccountControl",
48
49 "pwdLastSet",
50 "accountExpires",
51 "logonHours",
52 "objectSid",
53
54 /* check 'allowed workstations' */
55 "userWorkstations",
56
57 /* required for server_info, not access control: */
58 "displayName",
59 "scriptPath",
60 "profilePath",
61 "homeDirectory",
62 "homeDrive",
63 "lastLogon",
64 "lastLogoff",
65 "accountExpires",
66 "badPwdCount",
67 "logonCount",
68 "primaryGroupID",
69 NULL,
70 };
71
72 const char *domain_ref_attrs[] = {"nETBIOSName", "nCName",
73 "dnsRoot", "objectClass", NULL};
74
75 /****************************************************************************
76 Check if a user is allowed to logon at this time. Note this is the
77 servers local time, as logon hours are just specified as a weekly
78 bitmask.
79 ****************************************************************************/
80
81 static bool logon_hours_ok(struct ldb_message *msg, const char *name_for_logs)
/* [<][>][^][v][top][bottom][index][help] */
82 {
83 /* In logon hours first bit is Sunday from 12AM to 1AM */
84 const struct ldb_val *hours;
85 struct tm *utctime;
86 time_t lasttime;
87 const char *asct;
88 uint8_t bitmask, bitpos;
89
90 hours = ldb_msg_find_ldb_val(msg, "logonHours");
91 if (!hours) {
92 DEBUG(5,("logon_hours_ok: No hours restrictions for user %s\n", name_for_logs));
93 return true;
94 }
95
96 if (hours->length != 168/8) {
97 DEBUG(5,("logon_hours_ok: malformed logon hours restrictions for user %s\n", name_for_logs));
98 return true;
99 }
100
101 lasttime = time(NULL);
102 utctime = gmtime(&lasttime);
103 if (!utctime) {
104 DEBUG(1, ("logon_hours_ok: failed to get gmtime. Failing logon for user %s\n",
105 name_for_logs));
106 return false;
107 }
108
109 /* find the corresponding byte and bit */
110 bitpos = (utctime->tm_wday * 24 + utctime->tm_hour) % 168;
111 bitmask = 1 << (bitpos % 8);
112
113 if (! (hours->data[bitpos/8] & bitmask)) {
114 struct tm *t = localtime(&lasttime);
115 if (!t) {
116 asct = "INVALID TIME";
117 } else {
118 asct = asctime(t);
119 if (!asct) {
120 asct = "INVALID TIME";
121 }
122 }
123
124 DEBUG(1, ("logon_hours_ok: Account for user %s not allowed to "
125 "logon at this time (%s).\n",
126 name_for_logs, asct ));
127 return false;
128 }
129
130 asct = asctime(utctime);
131 DEBUG(5,("logon_hours_ok: user %s allowed to logon at this time (%s)\n",
132 name_for_logs, asct ? asct : "UNKNOWN TIME" ));
133
134 return true;
135 }
136
137 /****************************************************************************
138 Do a specific test for a SAM_ACCOUNT being vaild for this connection
139 (ie not disabled, expired and the like).
140 ****************************************************************************/
141 _PUBLIC_ NTSTATUS authsam_account_ok(TALLOC_CTX *mem_ctx,
/* [<][>][^][v][top][bottom][index][help] */
142 struct ldb_context *sam_ctx,
143 uint32_t logon_parameters,
144 struct ldb_message *msg,
145 struct ldb_message *msg_domain_ref,
146 const char *logon_workstation,
147 const char *name_for_logs,
148 bool allow_domain_trust)
149 {
150 uint16_t acct_flags;
151 const char *workstation_list;
152 NTTIME acct_expiry;
153 NTTIME must_change_time;
154
155 struct ldb_dn *domain_dn = samdb_result_dn(sam_ctx, mem_ctx, msg_domain_ref, "nCName", ldb_dn_new(mem_ctx, sam_ctx, NULL));
156
157 NTTIME now;
158 DEBUG(4,("authsam_account_ok: Checking SMB password for user %s\n", name_for_logs));
159
160 acct_flags = samdb_result_acct_flags(sam_ctx, mem_ctx, msg, domain_dn);
161
162 acct_expiry = samdb_result_account_expires(msg);
163
164 /* Check for when we must change this password, taking the
165 * userAccountControl flags into account */
166 must_change_time = samdb_result_force_password_change(sam_ctx, mem_ctx,
167 domain_dn, msg);
168
169 workstation_list = samdb_result_string(msg, "userWorkstations", NULL);
170
171 /* Quit if the account was disabled. */
172 if (acct_flags & ACB_DISABLED) {
173 DEBUG(1,("authsam_account_ok: Account for user '%s' was disabled.\n", name_for_logs));
174 return NT_STATUS_ACCOUNT_DISABLED;
175 }
176
177 /* Quit if the account was locked out. */
178 if (acct_flags & ACB_AUTOLOCK) {
179 DEBUG(1,("authsam_account_ok: Account for user %s was locked out.\n", name_for_logs));
180 return NT_STATUS_ACCOUNT_LOCKED_OUT;
181 }
182
183 /* Test account expire time */
184 unix_to_nt_time(&now, time(NULL));
185 if (now > acct_expiry) {
186 DEBUG(1,("authsam_account_ok: Account for user '%s' has expired.\n", name_for_logs));
187 DEBUG(3,("authsam_account_ok: Account expired at '%s'.\n",
188 nt_time_string(mem_ctx, acct_expiry)));
189 return NT_STATUS_ACCOUNT_EXPIRED;
190 }
191
192 /* check for immediate expiry "must change at next logon" */
193 if (must_change_time == 0) {
194 DEBUG(1,("sam_account_ok: Account for user '%s' password must change!.\n",
195 name_for_logs));
196 return NT_STATUS_PASSWORD_MUST_CHANGE;
197 }
198
199 /* check for expired password */
200 if (must_change_time < now) {
201 DEBUG(1,("sam_account_ok: Account for user '%s' password expired!.\n",
202 name_for_logs));
203 DEBUG(1,("sam_account_ok: Password expired at '%s' unix time.\n",
204 nt_time_string(mem_ctx, must_change_time)));
205 return NT_STATUS_PASSWORD_EXPIRED;
206 }
207
208 /* Test workstation. Workstation list is comma separated. */
209 if (logon_workstation && workstation_list && *workstation_list) {
210 bool invalid_ws = true;
211 int i;
212 const char **workstations = (const char **)str_list_make(mem_ctx, workstation_list, ",");
213
214 for (i = 0; workstations && workstations[i]; i++) {
215 DEBUG(10,("sam_account_ok: checking for workstation match '%s' and '%s'\n",
216 workstations[i], logon_workstation));
217
218 if (strequal(workstations[i], logon_workstation)) {
219 invalid_ws = false;
220 break;
221 }
222 }
223
224 talloc_free(workstations);
225
226 if (invalid_ws) {
227 return NT_STATUS_INVALID_WORKSTATION;
228 }
229 }
230
231 if (!logon_hours_ok(msg, name_for_logs)) {
232 return NT_STATUS_INVALID_LOGON_HOURS;
233 }
234
235 if (!allow_domain_trust) {
236 if (acct_flags & ACB_DOMTRUST) {
237 DEBUG(2,("sam_account_ok: Domain trust account %s denied by server\n", name_for_logs));
238 return NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT;
239 }
240 }
241 if (!(logon_parameters & MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT)) {
242 if (acct_flags & ACB_SVRTRUST) {
243 DEBUG(2,("sam_account_ok: Server trust account %s denied by server\n", name_for_logs));
244 return NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT;
245 }
246 }
247 if (!(logon_parameters & MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT)) {
248 if (acct_flags & ACB_WSTRUST) {
249 DEBUG(4,("sam_account_ok: Wksta trust account %s denied by server\n", name_for_logs));
250 return NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT;
251 }
252 }
253
254 return NT_STATUS_OK;
255 }
256
257 _PUBLIC_ NTSTATUS authsam_make_server_info(TALLOC_CTX *mem_ctx, struct ldb_context *sam_ctx,
/* [<][>][^][v][top][bottom][index][help] */
258 const char *netbios_name,
259 struct ldb_message *msg,
260 struct ldb_message *msg_domain_ref,
261 DATA_BLOB user_sess_key, DATA_BLOB lm_sess_key,
262 struct auth_serversupplied_info **_server_info)
263 {
264 struct auth_serversupplied_info *server_info;
265 struct ldb_message **group_msgs;
266 int group_ret;
267 const char *group_attrs[3] = { "sAMAccountType", "objectSid", NULL };
268 /* find list of sids */
269 struct dom_sid **groupSIDs = NULL;
270 struct dom_sid *account_sid;
271 struct dom_sid *primary_group_sid;
272 struct ldb_dn *domain_dn;
273 const char *str;
274 struct ldb_dn *ncname;
275 int i;
276 uint_t rid;
277 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
278
279 group_ret = gendb_search(sam_ctx,
280 tmp_ctx, NULL, &group_msgs, group_attrs,
281 "(&(member=%s)(sAMAccountType=*))",
282 ldb_dn_get_linearized(msg->dn));
283 if (group_ret == -1) {
284 talloc_free(tmp_ctx);
285 return NT_STATUS_INTERNAL_DB_CORRUPTION;
286 }
287
288 server_info = talloc(mem_ctx, struct auth_serversupplied_info);
289 NT_STATUS_HAVE_NO_MEMORY(server_info);
290
291 if (group_ret > 0) {
292 groupSIDs = talloc_array(server_info, struct dom_sid *, group_ret);
293 NT_STATUS_HAVE_NO_MEMORY(groupSIDs);
294 }
295
296 /* Need to unroll some nested groups, but not aliases */
297 for (i = 0; i < group_ret; i++) {
298 groupSIDs[i] = samdb_result_dom_sid(groupSIDs,
299 group_msgs[i], "objectSid");
300 NT_STATUS_HAVE_NO_MEMORY(groupSIDs[i]);
301 }
302
303 talloc_free(tmp_ctx);
304
305 account_sid = samdb_result_dom_sid(server_info, msg, "objectSid");
306 NT_STATUS_HAVE_NO_MEMORY(account_sid);
307
308 primary_group_sid = dom_sid_dup(server_info, account_sid);
309 NT_STATUS_HAVE_NO_MEMORY(primary_group_sid);
310
311 rid = samdb_result_uint(msg, "primaryGroupID", ~0);
312 if (rid == ~0) {
313 if (group_ret > 0) {
314 primary_group_sid = groupSIDs[0];
315 } else {
316 primary_group_sid = NULL;
317 }
318 } else {
319 primary_group_sid->sub_auths[primary_group_sid->num_auths-1] = rid;
320 }
321
322 server_info->account_sid = account_sid;
323 server_info->primary_group_sid = primary_group_sid;
324
325 server_info->n_domain_groups = group_ret;
326 server_info->domain_groups = groupSIDs;
327
328 server_info->account_name = talloc_steal(server_info, samdb_result_string(msg, "sAMAccountName", NULL));
329
330 server_info->domain_name = talloc_steal(server_info, samdb_result_string(msg_domain_ref, "nETBIOSName", NULL));
331
332 str = samdb_result_string(msg, "displayName", "");
333 server_info->full_name = talloc_strdup(server_info, str);
334 NT_STATUS_HAVE_NO_MEMORY(server_info->full_name);
335
336 str = samdb_result_string(msg, "scriptPath", "");
337 server_info->logon_script = talloc_strdup(server_info, str);
338 NT_STATUS_HAVE_NO_MEMORY(server_info->logon_script);
339
340 str = samdb_result_string(msg, "profilePath", "");
341 server_info->profile_path = talloc_strdup(server_info, str);
342 NT_STATUS_HAVE_NO_MEMORY(server_info->profile_path);
343
344 str = samdb_result_string(msg, "homeDirectory", "");
345 server_info->home_directory = talloc_strdup(server_info, str);
346 NT_STATUS_HAVE_NO_MEMORY(server_info->home_directory);
347
348 str = samdb_result_string(msg, "homeDrive", "");
349 server_info->home_drive = talloc_strdup(server_info, str);
350 NT_STATUS_HAVE_NO_MEMORY(server_info->home_drive);
351
352 server_info->logon_server = talloc_strdup(server_info, netbios_name);
353 NT_STATUS_HAVE_NO_MEMORY(server_info->logon_server);
354
355 server_info->last_logon = samdb_result_nttime(msg, "lastLogon", 0);
356 server_info->last_logoff = samdb_result_nttime(msg, "lastLogoff", 0);
357 server_info->acct_expiry = samdb_result_account_expires(msg);
358 server_info->last_password_change = samdb_result_nttime(msg, "pwdLastSet", 0);
359
360 ncname = samdb_result_dn(sam_ctx, mem_ctx, msg_domain_ref, "nCName", NULL);
361 if (!ncname) {
362 return NT_STATUS_INTERNAL_DB_CORRUPTION;
363 }
364 server_info->allow_password_change
365 = samdb_result_allow_password_change(sam_ctx, mem_ctx,
366 ncname, msg, "pwdLastSet");
367 server_info->force_password_change
368 = samdb_result_force_password_change(sam_ctx, mem_ctx,
369 ncname, msg);
370
371 server_info->logon_count = samdb_result_uint(msg, "logonCount", 0);
372 server_info->bad_password_count = samdb_result_uint(msg, "badPwdCount", 0);
373
374 domain_dn = samdb_result_dn(sam_ctx, mem_ctx, msg_domain_ref, "nCName", NULL);
375
376 server_info->acct_flags = samdb_result_acct_flags(sam_ctx, mem_ctx,
377 msg, domain_dn);
378
379 server_info->user_session_key = user_sess_key;
380 server_info->lm_session_key = lm_sess_key;
381
382 server_info->authenticated = true;
383
384 *_server_info = server_info;
385
386 return NT_STATUS_OK;
387 }
388
389 NTSTATUS sam_get_results_principal(struct ldb_context *sam_ctx,
/* [<][>][^][v][top][bottom][index][help] */
390 TALLOC_CTX *mem_ctx, const char *principal,
391 struct ldb_message ***msgs,
392 struct ldb_message ***msgs_domain_ref)
393 {
394 struct ldb_dn *user_dn, *domain_dn;
395 NTSTATUS nt_status;
396 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
397 int ret;
398 struct ldb_dn *partitions_basedn = samdb_partitions_dn(sam_ctx, mem_ctx);
399
400 if (!tmp_ctx) {
401 return NT_STATUS_NO_MEMORY;
402 }
403
404 nt_status = crack_user_principal_name(sam_ctx, tmp_ctx, principal, &user_dn, &domain_dn);
405 if (!NT_STATUS_IS_OK(nt_status)) {
406 talloc_free(tmp_ctx);
407 return nt_status;
408 }
409
410 /* grab domain info from the reference */
411 ret = gendb_search(sam_ctx, tmp_ctx, partitions_basedn, msgs_domain_ref, domain_ref_attrs,
412 "(ncName=%s)", ldb_dn_get_linearized(domain_dn));
413
414 if (ret != 1) {
415 talloc_free(tmp_ctx);
416 return NT_STATUS_INTERNAL_DB_CORRUPTION;
417 }
418
419 /* pull the user attributes */
420 ret = gendb_search_dn(sam_ctx, tmp_ctx, user_dn, msgs, user_attrs);
421 if (ret != 1) {
422 talloc_free(tmp_ctx);
423 return NT_STATUS_INTERNAL_DB_CORRUPTION;
424 }
425 talloc_steal(mem_ctx, *msgs);
426 talloc_steal(mem_ctx, *msgs_domain_ref);
427 talloc_free(tmp_ctx);
428
429 return NT_STATUS_OK;
430 }