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
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.
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.
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/>.
23 #include "system/time.h"
24 #include "auth/auth.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 "librpc/gen_ndr/ndr_security.h"
32 #include "param/param.h"
33 #include "auth/auth_sam.h"
35 const char *user_attrs
[] = {
36 /* required for the krb5 kdc */
40 "servicePrincipalName",
41 "msDS-KeyVersionNumber",
42 "supplementalCredentials",
55 /* check 'allowed workstations' */
58 /* required for server_info, not access control: */
74 /****************************************************************************
75 Check if a user is allowed to logon at this time. Note this is the
76 servers local time, as logon hours are just specified as a weekly
78 ****************************************************************************/
80 static bool logon_hours_ok(struct ldb_message
*msg
, const char *name_for_logs
)
82 /* In logon hours first bit is Sunday from 12AM to 1AM */
83 const struct ldb_val
*hours
;
87 uint8_t bitmask
, bitpos
;
89 hours
= ldb_msg_find_ldb_val(msg
, "logonHours");
91 DEBUG(5,("logon_hours_ok: No hours restrictions for user %s\n", name_for_logs
));
95 if (hours
->length
!= 168/8) {
96 DEBUG(5,("logon_hours_ok: malformed logon hours restrictions for user %s\n", name_for_logs
));
100 lasttime
= time(NULL
);
101 utctime
= gmtime(&lasttime
);
103 DEBUG(1, ("logon_hours_ok: failed to get gmtime. Failing logon for user %s\n",
108 /* find the corresponding byte and bit */
109 bitpos
= (utctime
->tm_wday
* 24 + utctime
->tm_hour
) % 168;
110 bitmask
= 1 << (bitpos
% 8);
112 if (! (hours
->data
[bitpos
/8] & bitmask
)) {
113 struct tm
*t
= localtime(&lasttime
);
115 asct
= "INVALID TIME";
119 asct
= "INVALID TIME";
123 DEBUG(1, ("logon_hours_ok: Account for user %s not allowed to "
124 "logon at this time (%s).\n",
125 name_for_logs
, asct
));
129 asct
= asctime(utctime
);
130 DEBUG(5,("logon_hours_ok: user %s allowed to logon at this time (%s)\n",
131 name_for_logs
, asct
? asct
: "UNKNOWN TIME" ));
136 /****************************************************************************
137 Do a specific test for a SAM_ACCOUNT being vaild for this connection
138 (ie not disabled, expired and the like).
139 ****************************************************************************/
140 _PUBLIC_ NTSTATUS
authsam_account_ok(TALLOC_CTX
*mem_ctx
,
141 struct ldb_context
*sam_ctx
,
142 uint32_t logon_parameters
,
143 struct ldb_dn
*domain_dn
,
144 struct ldb_message
*msg
,
145 const char *logon_workstation
,
146 const char *name_for_logs
,
147 bool allow_domain_trust
,
148 bool password_change
)
151 const char *workstation_list
;
153 NTTIME must_change_time
;
156 DEBUG(4,("authsam_account_ok: Checking SMB password for user %s\n", name_for_logs
));
158 acct_flags
= samdb_result_acct_flags(sam_ctx
, mem_ctx
, msg
, domain_dn
);
160 acct_expiry
= samdb_result_account_expires(msg
);
162 /* Check for when we must change this password, taking the
163 * userAccountControl flags into account */
164 must_change_time
= samdb_result_force_password_change(sam_ctx
, mem_ctx
,
167 workstation_list
= samdb_result_string(msg
, "userWorkstations", NULL
);
169 /* Quit if the account was disabled. */
170 if (acct_flags
& ACB_DISABLED
) {
171 DEBUG(1,("authsam_account_ok: Account for user '%s' was disabled.\n", name_for_logs
));
172 return NT_STATUS_ACCOUNT_DISABLED
;
175 /* Quit if the account was locked out. */
176 if (acct_flags
& ACB_AUTOLOCK
) {
177 DEBUG(1,("authsam_account_ok: Account for user %s was locked out.\n", name_for_logs
));
178 return NT_STATUS_ACCOUNT_LOCKED_OUT
;
181 /* Test account expire time */
182 unix_to_nt_time(&now
, time(NULL
));
183 if (now
> acct_expiry
) {
184 DEBUG(1,("authsam_account_ok: Account for user '%s' has expired.\n", name_for_logs
));
185 DEBUG(3,("authsam_account_ok: Account expired at '%s'.\n",
186 nt_time_string(mem_ctx
, acct_expiry
)));
187 return NT_STATUS_ACCOUNT_EXPIRED
;
190 /* check for immediate expiry "must change at next logon" (but not if this is a password change request) */
191 if ((must_change_time
== 0) && !password_change
) {
192 DEBUG(1,("sam_account_ok: Account for user '%s' password must change!.\n",
194 return NT_STATUS_PASSWORD_MUST_CHANGE
;
197 /* check for expired password (but not if this is a password change request) */
198 if ((must_change_time
< now
) && !password_change
) {
199 DEBUG(1,("sam_account_ok: Account for user '%s' password expired!.\n",
201 DEBUG(1,("sam_account_ok: Password expired at '%s' unix time.\n",
202 nt_time_string(mem_ctx
, must_change_time
)));
203 return NT_STATUS_PASSWORD_EXPIRED
;
206 /* Test workstation. Workstation list is comma separated. */
207 if (logon_workstation
&& workstation_list
&& *workstation_list
) {
208 bool invalid_ws
= true;
210 const char **workstations
= (const char **)str_list_make(mem_ctx
, workstation_list
, ",");
212 for (i
= 0; workstations
&& workstations
[i
]; i
++) {
213 DEBUG(10,("sam_account_ok: checking for workstation match '%s' and '%s'\n",
214 workstations
[i
], logon_workstation
));
216 if (strequal(workstations
[i
], logon_workstation
)) {
222 talloc_free(workstations
);
225 return NT_STATUS_INVALID_WORKSTATION
;
229 if (!logon_hours_ok(msg
, name_for_logs
)) {
230 return NT_STATUS_INVALID_LOGON_HOURS
;
233 if (!allow_domain_trust
) {
234 if (acct_flags
& ACB_DOMTRUST
) {
235 DEBUG(2,("sam_account_ok: Domain trust account %s denied by server\n", name_for_logs
));
236 return NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT
;
239 if (!(logon_parameters
& MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT
)) {
240 if (acct_flags
& ACB_SVRTRUST
) {
241 DEBUG(2,("sam_account_ok: Server trust account %s denied by server\n", name_for_logs
));
242 return NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT
;
245 if (!(logon_parameters
& MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
)) {
246 if (acct_flags
& ACB_WSTRUST
) {
247 DEBUG(4,("sam_account_ok: Wksta trust account %s denied by server\n", name_for_logs
));
248 return NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT
;
255 _PUBLIC_ NTSTATUS
authsam_make_server_info(TALLOC_CTX
*mem_ctx
, struct ldb_context
*sam_ctx
,
256 const char *netbios_name
,
257 const char *domain_name
,
258 struct ldb_dn
*domain_dn
,
259 struct ldb_message
*msg
,
260 DATA_BLOB user_sess_key
, DATA_BLOB lm_sess_key
,
261 struct auth_serversupplied_info
**_server_info
)
263 struct auth_serversupplied_info
*server_info
;
265 /* find list of sids */
266 struct dom_sid
**groupSIDs
= NULL
;
267 struct dom_sid
*account_sid
;
268 struct dom_sid
*primary_group_sid
;
272 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
273 struct ldb_message_element
*el
;
275 server_info
= talloc(tmp_ctx
, struct auth_serversupplied_info
);
276 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info
, tmp_ctx
);
278 el
= ldb_msg_find_element(msg
, "memberOf");
280 group_ret
= el
->num_values
;
281 groupSIDs
= talloc_array(server_info
, struct dom_sid
*, group_ret
);
282 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(groupSIDs
, tmp_ctx
);
285 /* TODO Note: this is incomplete. We need to unroll some
286 * nested groups, but not aliases */
287 for (i
= 0; i
< group_ret
; i
++) {
289 const struct ldb_val
*v
;
290 enum ndr_err_code ndr_err
;
292 dn
= ldb_dn_from_ldb_val(tmp_ctx
, sam_ctx
, &el
->values
[i
]);
294 talloc_free(tmp_ctx
);
295 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
297 v
= ldb_dn_get_extended_component(dn
, "SID");
298 groupSIDs
[i
] = talloc(groupSIDs
, struct dom_sid
);
299 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(groupSIDs
[i
], tmp_ctx
);
301 ndr_err
= ndr_pull_struct_blob(v
, groupSIDs
[i
], NULL
, groupSIDs
[i
],
302 (ndr_pull_flags_fn_t
)ndr_pull_dom_sid
);
303 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
304 talloc_free(tmp_ctx
);
305 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
309 account_sid
= samdb_result_dom_sid(server_info
, msg
, "objectSid");
310 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(account_sid
, tmp_ctx
);
312 primary_group_sid
= dom_sid_dup(server_info
, account_sid
);
313 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(primary_group_sid
, tmp_ctx
);
315 rid
= samdb_result_uint(msg
, "primaryGroupID", ~0);
318 primary_group_sid
= groupSIDs
[0];
320 primary_group_sid
= NULL
;
323 primary_group_sid
->sub_auths
[primary_group_sid
->num_auths
-1] = rid
;
326 server_info
->account_sid
= account_sid
;
327 server_info
->primary_group_sid
= primary_group_sid
;
329 server_info
->n_domain_groups
= group_ret
;
330 server_info
->domain_groups
= groupSIDs
;
332 server_info
->account_name
= talloc_steal(server_info
, samdb_result_string(msg
, "sAMAccountName", NULL
));
334 server_info
->domain_name
= talloc_strdup(server_info
, domain_name
);
335 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info
->domain_name
, tmp_ctx
);
337 str
= samdb_result_string(msg
, "displayName", "");
338 server_info
->full_name
= talloc_strdup(server_info
, str
);
339 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info
->full_name
, tmp_ctx
);
341 str
= samdb_result_string(msg
, "scriptPath", "");
342 server_info
->logon_script
= talloc_strdup(server_info
, str
);
343 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info
->logon_script
, tmp_ctx
);
345 str
= samdb_result_string(msg
, "profilePath", "");
346 server_info
->profile_path
= talloc_strdup(server_info
, str
);
347 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info
->profile_path
, tmp_ctx
);
349 str
= samdb_result_string(msg
, "homeDirectory", "");
350 server_info
->home_directory
= talloc_strdup(server_info
, str
);
351 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info
->home_directory
, tmp_ctx
);
353 str
= samdb_result_string(msg
, "homeDrive", "");
354 server_info
->home_drive
= talloc_strdup(server_info
, str
);
355 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info
->home_drive
, tmp_ctx
);
357 server_info
->logon_server
= talloc_strdup(server_info
, netbios_name
);
358 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info
->logon_server
, tmp_ctx
);
360 server_info
->last_logon
= samdb_result_nttime(msg
, "lastLogon", 0);
361 server_info
->last_logoff
= samdb_result_nttime(msg
, "lastLogoff", 0);
362 server_info
->acct_expiry
= samdb_result_account_expires(msg
);
363 server_info
->last_password_change
= samdb_result_nttime(msg
, "pwdLastSet", 0);
365 server_info
->allow_password_change
366 = samdb_result_allow_password_change(sam_ctx
, mem_ctx
,
367 domain_dn
, msg
, "pwdLastSet");
368 server_info
->force_password_change
369 = samdb_result_force_password_change(sam_ctx
, mem_ctx
,
372 server_info
->logon_count
= samdb_result_uint(msg
, "logonCount", 0);
373 server_info
->bad_password_count
= samdb_result_uint(msg
, "badPwdCount", 0);
375 server_info
->acct_flags
= samdb_result_acct_flags(sam_ctx
, mem_ctx
,
378 server_info
->user_session_key
= user_sess_key
;
379 server_info
->lm_session_key
= lm_sess_key
;
381 server_info
->authenticated
= true;
383 *_server_info
= talloc_steal(mem_ctx
, server_info
);
388 NTSTATUS
sam_get_results_principal(struct ldb_context
*sam_ctx
,
389 TALLOC_CTX
*mem_ctx
, const char *principal
,
390 struct ldb_dn
**domain_dn
,
391 struct ldb_message
**msg
)
393 struct ldb_dn
*user_dn
;
395 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
399 return NT_STATUS_NO_MEMORY
;
402 nt_status
= crack_user_principal_name(sam_ctx
, tmp_ctx
, principal
, &user_dn
, domain_dn
);
403 if (!NT_STATUS_IS_OK(nt_status
)) {
404 talloc_free(tmp_ctx
);
408 /* pull the user attributes */
409 ret
= gendb_search_single_extended_dn(sam_ctx
, tmp_ctx
, user_dn
, LDB_SCOPE_BASE
,
410 msg
, user_attrs
, "(objectClass=*)");
411 if (ret
!= LDB_SUCCESS
) {
412 talloc_free(tmp_ctx
);
413 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
415 talloc_steal(mem_ctx
, *msg
);
416 talloc_steal(mem_ctx
, *domain_dn
);
417 talloc_free(tmp_ctx
);