s3: Fix a memory leak in check_sam_security_info3
[garming/samba-autobuild/.git] / source3 / auth / check_samsec.c
1 /*
2    Unix SMB/CIFS implementation.
3    Password and authentication handling
4    Copyright (C) Andrew Tridgell              1992-2000
5    Copyright (C) Luke Kenneth Casson Leighton 1996-2000
6    Copyright (C) Andrew Bartlett              2001-2003
7    Copyright (C) Gerald Carter                2003
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 "../libcli/auth/libcli_auth.h"
25
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_AUTH
28
29 /****************************************************************************
30  Do a specific test for an smb password being correct, given a smb_password and
31  the lanman and NT responses.
32 ****************************************************************************/
33
34 static NTSTATUS sam_password_ok(TALLOC_CTX *mem_ctx,
35                                 const char *username,
36                                 uint32_t acct_ctrl,
37                                 const DATA_BLOB *challenge,
38                                 const uint8_t *lm_pw,
39                                 const uint8_t *nt_pw,
40                                 const struct auth_usersupplied_info *user_info,
41                                 DATA_BLOB *user_sess_key,
42                                 DATA_BLOB *lm_sess_key)
43 {
44         NTSTATUS status;
45         struct samr_Password _lm_hash, _nt_hash;
46         struct samr_Password *lm_hash = NULL;
47         struct samr_Password *nt_hash = NULL;
48
49         *user_sess_key = data_blob_null;
50         *lm_sess_key = data_blob_null;
51
52         if (acct_ctrl & ACB_PWNOTREQ) {
53                 if (lp_null_passwords()) {
54                         DEBUG(3,("Account for user '%s' has no password and null passwords are allowed.\n", username));
55                         return NT_STATUS_OK;
56                 } else {
57                         DEBUG(3,("Account for user '%s' has no password and null passwords are NOT allowed.\n", username));
58                         return NT_STATUS_LOGON_FAILURE;
59                 }
60         }
61
62         if (lm_pw) {
63                 memcpy(_lm_hash.hash, lm_pw, sizeof(_lm_hash.hash));
64                 lm_hash = &_lm_hash;
65         }
66         if (nt_pw) {
67                 memcpy(_nt_hash.hash, nt_pw, sizeof(_nt_hash.hash));
68                 nt_hash = &_nt_hash;
69         }
70         switch (user_info->password_state) {
71         case AUTH_PASSWORD_HASH:
72                 status = hash_password_check(mem_ctx, lp_lanman_auth(),
73                                              user_info->password.hash.lanman,
74                                              user_info->password.hash.nt,
75                                              username,
76                                              lm_hash,
77                                              nt_hash);
78                 if (NT_STATUS_IS_OK(status)) {
79                         if (nt_pw) {
80                                 *user_sess_key = data_blob_talloc(mem_ctx, NULL, 16);
81                                 if (!user_sess_key->data) {
82                                         return NT_STATUS_NO_MEMORY;
83                                 }
84                                 SMBsesskeygen_ntv1(nt_pw, user_sess_key->data);
85                         }
86                 }
87                 return status;
88
89         /* Eventually we should test plaintext passwords in their own
90          * function, not assuming the caller has done a
91          * mapping */
92         case AUTH_PASSWORD_PLAIN:
93         case AUTH_PASSWORD_RESPONSE:
94                 return ntlm_password_check(mem_ctx, lp_lanman_auth(),
95                                            lp_ntlm_auth(),
96                                            user_info->logon_parameters,
97                                            challenge,
98                                            &user_info->password.response.lanman, &user_info->password.response.nt,
99                                            username,
100                                            user_info->client.account_name,
101                                            user_info->client.domain_name,
102                                            lm_hash,
103                                            nt_hash,
104                                            user_sess_key, lm_sess_key);
105         default:
106                 DEBUG(0,("user_info constructed for user '%s' was invalid - password_state=%u invalid.\n", username, user_info->password_state));
107                 return NT_STATUS_INTERNAL_ERROR;
108         }
109 }
110
111 /****************************************************************************
112  Check if a user is allowed to logon at this time. Note this is the
113  servers local time, as logon hours are just specified as a weekly
114  bitmask.
115 ****************************************************************************/
116
117 static bool logon_hours_ok(struct samu *sampass)
118 {
119         /* In logon hours first bit is Sunday from 12AM to 1AM */
120         const uint8 *hours;
121         struct tm *utctime;
122         time_t lasttime;
123         const char *asct;
124         uint8 bitmask, bitpos;
125
126         hours = pdb_get_hours(sampass);
127         if (!hours) {
128                 DEBUG(5,("logon_hours_ok: No hours restrictions for user %s\n",pdb_get_username(sampass)));
129                 return True;
130         }
131
132         lasttime = time(NULL);
133         utctime = gmtime(&lasttime);
134         if (!utctime) {
135                 DEBUG(1, ("logon_hours_ok: failed to get gmtime. Failing logon for user %s\n",
136                         pdb_get_username(sampass) ));
137                 return False;
138         }
139
140         /* find the corresponding byte and bit */
141         bitpos = (utctime->tm_wday * 24 + utctime->tm_hour) % 168;
142         bitmask = 1 << (bitpos % 8);
143
144         if (! (hours[bitpos/8] & bitmask)) {
145                 struct tm *t = localtime(&lasttime);
146                 if (!t) {
147                         asct = "INVALID TIME";
148                 } else {
149                         asct = asctime(t);
150                         if (!asct) {
151                                 asct = "INVALID TIME";
152                         }
153                 }
154
155                 DEBUG(1, ("logon_hours_ok: Account for user %s not allowed to "
156                           "logon at this time (%s).\n",
157                           pdb_get_username(sampass), asct ));
158                 return False;
159         }
160
161         asct = asctime(utctime);
162         DEBUG(5,("logon_hours_ok: user %s allowed to logon at this time (%s)\n",
163                 pdb_get_username(sampass), asct ? asct : "UNKNOWN TIME" ));
164
165         return True;
166 }
167
168 /****************************************************************************
169  Do a specific test for a struct samu being valid for this connection
170  (ie not disabled, expired and the like).
171 ****************************************************************************/
172
173 static NTSTATUS sam_account_ok(TALLOC_CTX *mem_ctx,
174                                struct samu *sampass,
175                                const struct auth_usersupplied_info *user_info)
176 {
177         uint32  acct_ctrl = pdb_get_acct_ctrl(sampass);
178         char *workstation_list;
179         time_t kickoff_time;
180
181         DEBUG(4,("sam_account_ok: Checking SMB password for user %s\n",pdb_get_username(sampass)));
182
183         /* Quit if the account was disabled. */
184         if (acct_ctrl & ACB_DISABLED) {
185                 DEBUG(1,("sam_account_ok: Account for user '%s' was disabled.\n", pdb_get_username(sampass)));
186                 return NT_STATUS_ACCOUNT_DISABLED;
187         }
188
189         /* Quit if the account was locked out. */
190         if (acct_ctrl & ACB_AUTOLOCK) {
191                 DEBUG(1,("sam_account_ok: Account for user %s was locked out.\n", pdb_get_username(sampass)));
192                 return NT_STATUS_ACCOUNT_LOCKED_OUT;
193         }
194
195         /* Quit if the account is not allowed to logon at this time. */
196         if (! logon_hours_ok(sampass)) {
197                 return NT_STATUS_INVALID_LOGON_HOURS;
198         }
199
200         /* Test account expire time */
201
202         kickoff_time = pdb_get_kickoff_time(sampass);
203         if (kickoff_time != 0 && time(NULL) > kickoff_time) {
204                 DEBUG(1,("sam_account_ok: Account for user '%s' has expired.\n", pdb_get_username(sampass)));
205                 DEBUG(3,("sam_account_ok: Account expired at '%ld' unix time.\n", (long)kickoff_time));
206                 return NT_STATUS_ACCOUNT_EXPIRED;
207         }
208
209         if (!(pdb_get_acct_ctrl(sampass) & ACB_PWNOEXP) && !(pdb_get_acct_ctrl(sampass) & ACB_PWNOTREQ)) {
210                 time_t must_change_time = pdb_get_pass_must_change_time(sampass);
211                 time_t last_set_time = pdb_get_pass_last_set_time(sampass);
212
213                 /* check for immediate expiry "must change at next logon"
214                  * for a user account. */
215                 if (((acct_ctrl & (ACB_WSTRUST|ACB_SVRTRUST)) == 0) && (last_set_time == 0)) {
216                         DEBUG(1,("sam_account_ok: Account for user '%s' password must change!\n", pdb_get_username(sampass)));
217                         return NT_STATUS_PASSWORD_MUST_CHANGE;
218                 }
219
220                 /* check for expired password */
221                 if (must_change_time < time(NULL) && must_change_time != 0) {
222                         DEBUG(1,("sam_account_ok: Account for user '%s' password expired!\n", pdb_get_username(sampass)));
223                         DEBUG(1,("sam_account_ok: Password expired at '%s' (%ld) unix time.\n", http_timestring(talloc_tos(), must_change_time), (long)must_change_time));
224                         return NT_STATUS_PASSWORD_EXPIRED;
225                 }
226         }
227
228         /* Test workstation. Workstation list is comma separated. */
229
230         workstation_list = talloc_strdup(mem_ctx, pdb_get_workstations(sampass));
231         if (!workstation_list)
232                 return NT_STATUS_NO_MEMORY;
233
234         if (*workstation_list) {
235                 bool invalid_ws = True;
236                 char *tok = NULL;
237                 const char *s = workstation_list;
238                 char *machine_name = talloc_asprintf(mem_ctx, "%s$", user_info->workstation_name);
239
240                 if (machine_name == NULL)
241                         return NT_STATUS_NO_MEMORY;
242
243                 while (next_token_talloc(mem_ctx, &s, &tok, ",")) {
244                         DEBUG(10,("sam_account_ok: checking for workstation match %s and %s\n",
245                                   tok, user_info->workstation_name));
246                         if(strequal(tok, user_info->workstation_name)) {
247                                 invalid_ws = False;
248                                 break;
249                         }
250                         if (tok[0] == '+') {
251                                 DEBUG(10,("sam_account_ok: checking for workstation %s in group: %s\n",
252                                         machine_name, tok + 1));
253                                 if (user_in_group(machine_name, tok + 1)) {
254                                         invalid_ws = False;
255                                         break;
256                                 }
257                         }
258                         TALLOC_FREE(tok);
259                 }
260                 TALLOC_FREE(tok);
261                 TALLOC_FREE(machine_name);
262
263                 if (invalid_ws)
264                         return NT_STATUS_INVALID_WORKSTATION;
265         }
266
267         if (acct_ctrl & ACB_DOMTRUST) {
268                 DEBUG(2,("sam_account_ok: Domain trust account %s denied by server\n", pdb_get_username(sampass)));
269                 return NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT;
270         }
271
272         if (acct_ctrl & ACB_SVRTRUST) {
273                 if (!(user_info->logon_parameters & MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT)) {
274                         DEBUG(2,("sam_account_ok: Server trust account %s denied by server\n", pdb_get_username(sampass)));
275                         return NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT;
276                 }
277         }
278
279         if (acct_ctrl & ACB_WSTRUST) {
280                 if (!(user_info->logon_parameters & MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT)) {
281                         DEBUG(2,("sam_account_ok: Wksta trust account %s denied by server\n", pdb_get_username(sampass)));
282                         return NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT;
283                 }
284         }
285         return NT_STATUS_OK;
286 }
287
288 /**
289  * Check whether the given password is one of the last two
290  * password history entries. If so, the bad pwcount should
291  * not be incremented even thought the actual password check
292  * failed.
293  */
294 static bool need_to_increment_bad_pw_count(
295         const DATA_BLOB *challenge,
296         struct samu* sampass,
297         const struct auth_usersupplied_info *user_info)
298 {
299         uint8_t i;
300         const uint8_t *pwhistory;
301         uint32_t pwhistory_len;
302         uint32_t policy_pwhistory_len;
303         uint32_t acct_ctrl;
304         const char *username;
305         TALLOC_CTX *mem_ctx = talloc_stackframe();
306         bool result = true;
307
308         pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY,
309                                &policy_pwhistory_len);
310         if (policy_pwhistory_len == 0) {
311                 goto done;
312         }
313
314         pwhistory = pdb_get_pw_history(sampass, &pwhistory_len);
315         if (!pwhistory || pwhistory_len == 0) {
316                 goto done;
317         }
318
319         acct_ctrl = pdb_get_acct_ctrl(sampass);
320         username = pdb_get_username(sampass);
321
322         for (i=1; i < MIN(MIN(3, policy_pwhistory_len), pwhistory_len); i++) {
323                 static const uint8_t zero16[SALTED_MD5_HASH_LEN];
324                 const uint8_t *salt;
325                 const uint8_t *nt_pw;
326                 NTSTATUS status;
327                 DATA_BLOB user_sess_key = data_blob_null;
328                 DATA_BLOB lm_sess_key = data_blob_null;
329
330                 salt = &pwhistory[i*PW_HISTORY_ENTRY_LEN];
331                 nt_pw = salt + PW_HISTORY_SALT_LEN;
332
333                 if (memcmp(zero16, nt_pw, NT_HASH_LEN) == 0) {
334                         /* skip zero password hash */
335                         continue;
336                 }
337
338                 if (memcmp(zero16, salt, PW_HISTORY_SALT_LEN) != 0) {
339                         /* skip nonzero salt (old format entry) */
340                         continue;
341                 }
342
343                 status = sam_password_ok(mem_ctx,
344                                          username, acct_ctrl,
345                                          challenge,
346                                          NULL, nt_pw,
347                                          user_info, &user_sess_key, &lm_sess_key);
348                 if (NT_STATUS_IS_OK(status)) {
349                         result = false;
350                         break;
351                 }
352         }
353
354 done:
355         TALLOC_FREE(mem_ctx);
356         return result;
357 }
358
359 /****************************************************************************
360 check if a username/password is OK assuming the password is a 24 byte
361 SMB hash supplied in the user_info structure
362 return an NT_STATUS constant.
363 ****************************************************************************/
364
365 NTSTATUS check_sam_security(const DATA_BLOB *challenge,
366                             TALLOC_CTX *mem_ctx,
367                             const struct auth_usersupplied_info *user_info,
368                             struct auth_serversupplied_info **server_info)
369 {
370         struct samu *sampass=NULL;
371         bool ret;
372         NTSTATUS nt_status;
373         NTSTATUS update_login_attempts_status;
374         DATA_BLOB user_sess_key = data_blob_null;
375         DATA_BLOB lm_sess_key = data_blob_null;
376         bool updated_badpw = False;
377         const char *username;
378         const uint8_t *nt_pw;
379         const uint8_t *lm_pw;
380
381         /* the returned struct gets kept on the server_info, by means
382            of a steal further down */
383
384         sampass = samu_new(mem_ctx);
385         if (sampass == NULL) {
386                 return NT_STATUS_NO_MEMORY;
387         }
388
389         /* get the account information */
390
391         become_root();
392         ret = pdb_getsampwnam(sampass, user_info->mapped.account_name);
393         unbecome_root();
394
395         if (ret == False) {
396                 DEBUG(3,("check_sam_security: Couldn't find user '%s' in "
397                          "passdb.\n", user_info->mapped.account_name));
398                 TALLOC_FREE(sampass);
399                 return NT_STATUS_NO_SUCH_USER;
400         }
401
402         username = pdb_get_username(sampass);
403         nt_pw = pdb_get_nt_passwd(sampass);
404         lm_pw = pdb_get_lanman_passwd(sampass);
405
406         /* Quit if the account was locked out. */
407         if (pdb_get_acct_ctrl(sampass) & ACB_AUTOLOCK) {
408                 DEBUG(3,("check_sam_security: Account for user %s was locked out.\n", username));
409                 return NT_STATUS_ACCOUNT_LOCKED_OUT;
410         }
411
412         nt_status = sam_password_ok(mem_ctx,
413                                     username, pdb_get_acct_ctrl(sampass),
414                                     challenge, lm_pw, nt_pw,
415                                     user_info, &user_sess_key, &lm_sess_key);
416
417         /* Notify passdb backend of login success/failure. If not
418            NT_STATUS_OK the backend doesn't like the login */
419
420         update_login_attempts_status = pdb_update_login_attempts(sampass, NT_STATUS_IS_OK(nt_status));
421
422         if (!NT_STATUS_IS_OK(nt_status)) {
423                 bool increment_bad_pw_count = false;
424
425                 if (NT_STATUS_EQUAL(nt_status,NT_STATUS_WRONG_PASSWORD) &&
426                     pdb_get_acct_ctrl(sampass) & ACB_NORMAL &&
427                     NT_STATUS_IS_OK(update_login_attempts_status))
428                 {
429                         increment_bad_pw_count =
430                                 need_to_increment_bad_pw_count(
431                                         challenge, sampass, user_info);
432                 }
433
434                 if (increment_bad_pw_count) {
435                         pdb_increment_bad_password_count(sampass);
436                         updated_badpw = True;
437                 } else {
438                         pdb_update_bad_password_count(sampass,
439                                                       &updated_badpw);
440                 }
441                 if (updated_badpw){
442                         NTSTATUS status;
443
444                         become_root();
445                         status = pdb_update_sam_account(sampass);
446                         unbecome_root();
447
448                         if (!NT_STATUS_IS_OK(status)) {
449                                 DEBUG(1, ("Failed to modify entry: %s\n",
450                                           nt_errstr(status)));
451                         }
452                 }
453                 goto done;
454         }
455
456         if ((pdb_get_acct_ctrl(sampass) & ACB_NORMAL) &&
457             (pdb_get_bad_password_count(sampass) > 0)){
458                 pdb_set_bad_password_count(sampass, 0, PDB_CHANGED);
459                 pdb_set_bad_password_time(sampass, 0, PDB_CHANGED);
460                 updated_badpw = True;
461         }
462
463         if (updated_badpw){
464                 NTSTATUS status;
465
466                 become_root();
467                 status = pdb_update_sam_account(sampass);
468                 unbecome_root();
469
470                 if (!NT_STATUS_IS_OK(status)) {
471                         DEBUG(1, ("Failed to modify entry: %s\n",
472                                   nt_errstr(status)));
473                 }
474         }
475
476         nt_status = sam_account_ok(mem_ctx, sampass, user_info);
477
478         if (!NT_STATUS_IS_OK(nt_status)) {
479                 goto done;
480         }
481
482         become_root();
483         nt_status = make_server_info_sam(server_info, sampass);
484         unbecome_root();
485
486         TALLOC_FREE(sampass);
487
488         if (!NT_STATUS_IS_OK(nt_status)) {
489                 DEBUG(0,("check_sam_security: make_server_info_sam() failed with '%s'\n", nt_errstr(nt_status)));
490                 goto done;
491         }
492
493         (*server_info)->user_session_key =
494                 data_blob_talloc(*server_info, user_sess_key.data,
495                                  user_sess_key.length);
496         data_blob_free(&user_sess_key);
497
498         (*server_info)->lm_session_key =
499                 data_blob_talloc(*server_info, lm_sess_key.data,
500                                  lm_sess_key.length);
501         data_blob_free(&lm_sess_key);
502
503         (*server_info)->nss_token |= user_info->was_mapped;
504
505 done:
506         TALLOC_FREE(sampass);
507         data_blob_free(&user_sess_key);
508         data_blob_free(&lm_sess_key);
509         return nt_status;
510 }
511
512 /* This helper function for winbindd returns a very similar value to
513  * what a NETLOGON call would give, without the indirection */
514 NTSTATUS check_sam_security_info3(const DATA_BLOB *challenge,
515                                   TALLOC_CTX *mem_ctx,
516                                   const struct auth_usersupplied_info *user_info,
517                                   struct netr_SamInfo3 **pinfo3)
518 {
519         struct auth_serversupplied_info *server_info = NULL;
520         struct netr_SamInfo3 *info3;
521         NTSTATUS status;
522         TALLOC_CTX *frame = talloc_stackframe();
523
524         status = check_sam_security(challenge, talloc_tos(), user_info,
525                                     &server_info);
526         if (!NT_STATUS_IS_OK(status)) {
527                 DEBUG(10, ("check_sam_security failed: %s\n",
528                            nt_errstr(status)));
529                 goto done;
530         }
531
532         info3 = TALLOC_ZERO_P(mem_ctx, struct netr_SamInfo3);
533         if (info3 == NULL) {
534                 status = NT_STATUS_NO_MEMORY;
535                 goto done;
536         }
537
538         status = serverinfo_to_SamInfo3(server_info, NULL, 0, info3);
539         if (!NT_STATUS_IS_OK(status)) {
540                 DEBUG(10, ("serverinfo_to_SamInfo3 failed: %s\n",
541                            nt_errstr(status)));
542                 goto done;
543         }
544         *pinfo3 = info3;
545         status =  NT_STATUS_OK;
546 done:
547         TALLOC_FREE(frame);
548         return status;
549 }