ctdb-vacuum: rename ctdb_vacuum_db_full --> ctdb_vacuum_traverse_db
[Samba.git] / source3 / passdb / pdb_get_set.c
bloba9b22bbb40926780ed30b413ea4d7472a7afc6be
1 /*
2 Unix SMB/CIFS implementation.
3 struct samu access routines
4 Copyright (C) Jeremy Allison 1996-2001
5 Copyright (C) Luke Kenneth Casson Leighton 1996-1998
6 Copyright (C) Gerald (Jerry) Carter 2000-2006
7 Copyright (C) Andrew Bartlett 2001-2002
8 Copyright (C) Stefan (metze) Metzmacher 2002
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "includes.h"
25 #include "passdb.h"
26 #include "../libcli/auth/libcli_auth.h"
27 #include "../libcli/security/security.h"
28 #include "../lib/util/bitmap.h"
30 #undef DBGC_CLASS
31 #define DBGC_CLASS DBGC_PASSDB
33 /**
34 * @todo Redefine this to NULL, but this changes the API because
35 * much of samba assumes that the pdb_get...() funtions
36 * return strings. (ie not null-pointers).
37 * See also pdb_fill_default_sam().
40 #define PDB_NOT_QUITE_NULL ""
42 /*********************************************************************
43 Test if a change time is a max value. Copes with old and new values
44 of max.
45 ********************************************************************/
47 bool pdb_is_password_change_time_max(time_t test_time)
49 if (test_time == get_time_t_max()) {
50 return true;
52 #if (defined(SIZEOF_TIME_T) && (SIZEOF_TIME_T == 8))
53 if (test_time == 0x7FFFFFFFFFFFFFFFLL) {
54 return true;
56 #endif
57 if (test_time == 0x7FFFFFFF) {
58 return true;
60 return false;
63 /*********************************************************************
64 Return an unchanging version of max password change time - 0x7FFFFFFF.
65 ********************************************************************/
67 static time_t pdb_password_change_time_max(void)
69 return 0x7FFFFFFF;
72 /*********************************************************************
73 Collection of get...() functions for struct samu.
74 ********************************************************************/
76 uint32_t pdb_get_acct_ctrl(const struct samu *sampass)
78 return sampass->acct_ctrl;
81 time_t pdb_get_logon_time(const struct samu *sampass)
83 return sampass->logon_time;
86 time_t pdb_get_logoff_time(const struct samu *sampass)
88 return sampass->logoff_time;
91 time_t pdb_get_kickoff_time(const struct samu *sampass)
93 return sampass->kickoff_time;
96 time_t pdb_get_bad_password_time(const struct samu *sampass)
98 return sampass->bad_password_time;
101 time_t pdb_get_pass_last_set_time(const struct samu *sampass)
103 return sampass->pass_last_set_time;
106 time_t pdb_get_pass_can_change_time(const struct samu *sampass)
108 uint32_t allow;
110 /* if the last set time is zero, it means the user cannot
111 change their password, and this time must be zero. jmcd
113 if (sampass->pass_last_set_time == 0)
114 return (time_t) 0;
116 /* if the time is max, and the field has been changed,
117 we're trying to update this real value from the sampass
118 to indicate that the user cannot change their password. jmcd
120 if (pdb_is_password_change_time_max(sampass->pass_can_change_time) &&
121 IS_SAM_CHANGED(sampass, PDB_CANCHANGETIME))
122 return sampass->pass_can_change_time;
124 if (!pdb_get_account_policy(PDB_POLICY_MIN_PASSWORD_AGE, &allow))
125 allow = 0;
127 /* in normal cases, just calculate it from policy */
128 return sampass->pass_last_set_time + allow;
131 /* we need this for loading from the backend, so that we don't overwrite
132 non-changed max times, otherwise the pass_can_change checking won't work */
133 time_t pdb_get_pass_can_change_time_noncalc(const struct samu *sampass)
135 return sampass->pass_can_change_time;
138 time_t pdb_get_pass_must_change_time(const struct samu *sampass)
140 uint32_t expire;
142 if (sampass->pass_last_set_time == 0)
143 return (time_t) 0;
145 if (sampass->acct_ctrl & ACB_PWNOEXP)
146 return pdb_password_change_time_max();
148 if (!pdb_get_account_policy(PDB_POLICY_MAX_PASSWORD_AGE, &expire)
149 || expire == (uint32_t)-1 || expire == 0)
150 return get_time_t_max();
152 return sampass->pass_last_set_time + expire;
155 bool pdb_get_pass_can_change(const struct samu *sampass)
157 if (pdb_is_password_change_time_max(sampass->pass_can_change_time))
158 return False;
159 return True;
162 uint16_t pdb_get_logon_divs(const struct samu *sampass)
164 return sampass->logon_divs;
167 uint32_t pdb_get_hours_len(const struct samu *sampass)
169 return sampass->hours_len;
172 const uint8 *pdb_get_hours(const struct samu *sampass)
174 return (sampass->hours);
177 const uint8 *pdb_get_nt_passwd(const struct samu *sampass)
179 SMB_ASSERT((!sampass->nt_pw.data)
180 || sampass->nt_pw.length == NT_HASH_LEN);
181 return (uint8 *)sampass->nt_pw.data;
184 const uint8 *pdb_get_lanman_passwd(const struct samu *sampass)
186 SMB_ASSERT((!sampass->lm_pw.data)
187 || sampass->lm_pw.length == LM_HASH_LEN);
188 return (uint8 *)sampass->lm_pw.data;
191 const uint8 *pdb_get_pw_history(const struct samu *sampass, uint32_t *current_hist_len)
193 SMB_ASSERT((!sampass->nt_pw_his.data)
194 || ((sampass->nt_pw_his.length % PW_HISTORY_ENTRY_LEN) == 0));
195 *current_hist_len = sampass->nt_pw_his.length / PW_HISTORY_ENTRY_LEN;
196 return (uint8 *)sampass->nt_pw_his.data;
199 /* Return the plaintext password if known. Most of the time
200 it isn't, so don't assume anything magic about this function.
202 Used to pass the plaintext to passdb backends that might
203 want to store more than just the NTLM hashes.
205 const char *pdb_get_plaintext_passwd(const struct samu *sampass)
207 return sampass->plaintext_pw;
210 const struct dom_sid *pdb_get_user_sid(const struct samu *sampass)
212 return &sampass->user_sid;
215 const struct dom_sid *pdb_get_group_sid(struct samu *sampass)
217 NTSTATUS status;
219 /* Return the cached group SID if we have that */
220 if (sampass->group_sid) {
221 return sampass->group_sid;
224 /* No algorithmic mapping, meaning that we have to figure out the
225 primary group SID according to group mapping and the user SID must
226 be a newly allocated one. We rely on the user's Unix primary gid.
227 We have no choice but to fail if we can't find it. */
228 status = get_primary_group_sid(sampass,
229 pdb_get_username(sampass),
230 &sampass->unix_pw,
231 &sampass->group_sid);
232 if (!NT_STATUS_IS_OK(status)) {
233 return NULL;
236 return sampass->group_sid;
240 * Get flags showing what is initalised in the struct samu
241 * @param sampass the struct samu in question
242 * @return the flags indicating the members initialised in the struct.
245 enum pdb_value_state pdb_get_init_flags(const struct samu *sampass, enum pdb_elements element)
247 enum pdb_value_state ret = PDB_DEFAULT;
249 if (!sampass->change_flags || !sampass->set_flags)
250 return ret;
252 if (bitmap_query(sampass->set_flags, element)) {
253 DEBUG(11, ("element %d: SET\n", element));
254 ret = PDB_SET;
257 if (bitmap_query(sampass->change_flags, element)) {
258 DEBUG(11, ("element %d: CHANGED\n", element));
259 ret = PDB_CHANGED;
262 if (ret == PDB_DEFAULT) {
263 DEBUG(11, ("element %d: DEFAULT\n", element));
266 return ret;
269 const char *pdb_get_username(const struct samu *sampass)
271 return sampass->username;
274 const char *pdb_get_domain(const struct samu *sampass)
276 return sampass->domain;
279 const char *pdb_get_nt_username(const struct samu *sampass)
281 return sampass->nt_username;
284 const char *pdb_get_fullname(const struct samu *sampass)
286 return sampass->full_name;
289 const char *pdb_get_homedir(const struct samu *sampass)
291 return sampass->home_dir;
294 const char *pdb_get_dir_drive(const struct samu *sampass)
296 return sampass->dir_drive;
299 const char *pdb_get_logon_script(const struct samu *sampass)
301 return sampass->logon_script;
304 const char *pdb_get_profile_path(const struct samu *sampass)
306 return sampass->profile_path;
309 const char *pdb_get_acct_desc(const struct samu *sampass)
311 return sampass->acct_desc;
314 const char *pdb_get_workstations(const struct samu *sampass)
316 return sampass->workstations;
319 const char *pdb_get_comment(const struct samu *sampass)
321 return sampass->comment;
324 const char *pdb_get_munged_dial(const struct samu *sampass)
326 return sampass->munged_dial;
329 uint16_t pdb_get_bad_password_count(const struct samu *sampass)
331 return sampass->bad_password_count;
334 uint16_t pdb_get_logon_count(const struct samu *sampass)
336 return sampass->logon_count;
339 uint16_t pdb_get_country_code(const struct samu *sampass)
341 return sampass->country_code;
344 uint16_t pdb_get_code_page(const struct samu *sampass)
346 return sampass->code_page;
349 uint32_t pdb_get_unknown_6(const struct samu *sampass)
351 return sampass->unknown_6;
354 void *pdb_get_backend_private_data(const struct samu *sampass, const struct pdb_methods *my_methods)
356 if (my_methods == sampass->backend_private_methods) {
357 return sampass->backend_private_data;
358 } else {
359 return NULL;
363 /*********************************************************************
364 Collection of set...() functions for struct samu.
365 ********************************************************************/
367 bool pdb_set_acct_ctrl(struct samu *sampass, uint32_t acct_ctrl, enum pdb_value_state flag)
369 sampass->acct_ctrl = acct_ctrl;
370 return pdb_set_init_flags(sampass, PDB_ACCTCTRL, flag);
373 bool pdb_set_logon_time(struct samu *sampass, time_t mytime, enum pdb_value_state flag)
375 sampass->logon_time = mytime;
376 return pdb_set_init_flags(sampass, PDB_LOGONTIME, flag);
379 bool pdb_set_logoff_time(struct samu *sampass, time_t mytime, enum pdb_value_state flag)
381 sampass->logoff_time = mytime;
382 return pdb_set_init_flags(sampass, PDB_LOGOFFTIME, flag);
385 bool pdb_set_kickoff_time(struct samu *sampass, time_t mytime, enum pdb_value_state flag)
387 sampass->kickoff_time = mytime;
388 return pdb_set_init_flags(sampass, PDB_KICKOFFTIME, flag);
391 bool pdb_set_bad_password_time(struct samu *sampass, time_t mytime, enum pdb_value_state flag)
393 sampass->bad_password_time = mytime;
394 return pdb_set_init_flags(sampass, PDB_BAD_PASSWORD_TIME, flag);
397 bool pdb_set_pass_can_change_time(struct samu *sampass, time_t mytime, enum pdb_value_state flag)
399 sampass->pass_can_change_time = mytime;
400 return pdb_set_init_flags(sampass, PDB_CANCHANGETIME, flag);
403 bool pdb_set_pass_last_set_time(struct samu *sampass, time_t mytime, enum pdb_value_state flag)
405 sampass->pass_last_set_time = mytime;
406 return pdb_set_init_flags(sampass, PDB_PASSLASTSET, flag);
409 bool pdb_set_hours_len(struct samu *sampass, uint32_t len, enum pdb_value_state flag)
411 sampass->hours_len = len;
412 return pdb_set_init_flags(sampass, PDB_HOURSLEN, flag);
415 bool pdb_set_logon_divs(struct samu *sampass, uint16_t hours, enum pdb_value_state flag)
417 sampass->logon_divs = hours;
418 return pdb_set_init_flags(sampass, PDB_LOGONDIVS, flag);
422 * Set flags showing what is initalised in the struct samu
423 * @param sampass the struct samu in question
424 * @param flag The *new* flag to be set. Old flags preserved
425 * this flag is only added.
428 bool pdb_set_init_flags(struct samu *sampass, enum pdb_elements element, enum pdb_value_state value_flag)
430 if (!sampass->set_flags) {
431 if ((sampass->set_flags =
432 bitmap_talloc(sampass,
433 PDB_COUNT))==NULL) {
434 DEBUG(0,("bitmap_talloc failed\n"));
435 return False;
438 if (!sampass->change_flags) {
439 if ((sampass->change_flags =
440 bitmap_talloc(sampass,
441 PDB_COUNT))==NULL) {
442 DEBUG(0,("bitmap_talloc failed\n"));
443 return False;
447 switch(value_flag) {
448 case PDB_CHANGED:
449 if (!bitmap_set(sampass->change_flags, element)) {
450 DEBUG(0,("Can't set flag: %d in change_flags.\n",element));
451 return False;
453 if (!bitmap_set(sampass->set_flags, element)) {
454 DEBUG(0,("Can't set flag: %d in set_flags.\n",element));
455 return False;
457 DEBUG(11, ("element %d -> now CHANGED\n", element));
458 break;
459 case PDB_SET:
460 if (!bitmap_clear(sampass->change_flags, element)) {
461 DEBUG(0,("Can't set flag: %d in change_flags.\n",element));
462 return False;
464 if (!bitmap_set(sampass->set_flags, element)) {
465 DEBUG(0,("Can't set flag: %d in set_flags.\n",element));
466 return False;
468 DEBUG(11, ("element %d -> now SET\n", element));
469 break;
470 case PDB_DEFAULT:
471 default:
472 if (!bitmap_clear(sampass->change_flags, element)) {
473 DEBUG(0,("Can't set flag: %d in change_flags.\n",element));
474 return False;
476 if (!bitmap_clear(sampass->set_flags, element)) {
477 DEBUG(0,("Can't set flag: %d in set_flags.\n",element));
478 return False;
480 DEBUG(11, ("element %d -> now DEFAULT\n", element));
481 break;
484 return True;
487 bool pdb_set_user_sid(struct samu *sampass, const struct dom_sid *u_sid, enum pdb_value_state flag)
489 if (!u_sid)
490 return False;
492 sid_copy(&sampass->user_sid, u_sid);
494 DEBUG(10, ("pdb_set_user_sid: setting user sid %s\n",
495 sid_string_dbg(&sampass->user_sid)));
497 return pdb_set_init_flags(sampass, PDB_USERSID, flag);
500 bool pdb_set_user_sid_from_string(struct samu *sampass, const char *u_sid, enum pdb_value_state flag)
502 struct dom_sid new_sid;
504 if (!u_sid)
505 return False;
507 DEBUG(10, ("pdb_set_user_sid_from_string: setting user sid %s\n",
508 u_sid));
510 if (!string_to_sid(&new_sid, u_sid)) {
511 DEBUG(1, ("pdb_set_user_sid_from_string: %s isn't a valid SID!\n", u_sid));
512 return False;
515 if (!pdb_set_user_sid(sampass, &new_sid, flag)) {
516 DEBUG(1, ("pdb_set_user_sid_from_string: could not set sid %s on struct samu!\n", u_sid));
517 return False;
520 return True;
523 /********************************************************************
524 We never fill this in from a passdb backend but rather set is
525 based on the user's primary group membership. However, the
526 struct samu* is overloaded and reused in domain memship code
527 as well and built from the netr_SamInfo3 or PAC so we
528 have to allow the explicitly setting of a group SID here.
529 ********************************************************************/
531 bool pdb_set_group_sid(struct samu *sampass, const struct dom_sid *g_sid, enum pdb_value_state flag)
533 gid_t gid;
534 struct dom_sid dug_sid;
536 if (!g_sid)
537 return False;
539 if ( !(sampass->group_sid = talloc( sampass, struct dom_sid )) ) {
540 return False;
543 /* if we cannot resolve the SID to gid, then just ignore it and
544 store DOMAIN_USERS as the primary groupSID */
546 sid_compose(&dug_sid, get_global_sam_sid(), DOMAIN_RID_USERS);
548 if (dom_sid_equal(&dug_sid, g_sid)) {
549 sid_copy(sampass->group_sid, &dug_sid);
550 } else if (sid_to_gid( g_sid, &gid ) ) {
551 sid_copy(sampass->group_sid, g_sid);
552 } else {
553 sid_copy(sampass->group_sid, &dug_sid);
556 DEBUG(10, ("pdb_set_group_sid: setting group sid %s\n",
557 sid_string_dbg(sampass->group_sid)));
559 return pdb_set_init_flags(sampass, PDB_GROUPSID, flag);
562 /*********************************************************************
563 Set the user's UNIX name.
564 ********************************************************************/
566 bool pdb_set_username(struct samu *sampass, const char *username, enum pdb_value_state flag)
568 if (username) {
569 DEBUG(10, ("pdb_set_username: setting username %s, was %s\n", username,
570 (sampass->username)?(sampass->username):"NULL"));
572 sampass->username = talloc_strdup(sampass, username);
574 if (!sampass->username) {
575 DEBUG(0, ("pdb_set_username: talloc_strdup() failed!\n"));
576 return False;
578 } else {
579 sampass->username = PDB_NOT_QUITE_NULL;
582 return pdb_set_init_flags(sampass, PDB_USERNAME, flag);
585 /*********************************************************************
586 Set the domain name.
587 ********************************************************************/
589 bool pdb_set_domain(struct samu *sampass, const char *domain, enum pdb_value_state flag)
591 if (domain) {
592 DEBUG(10, ("pdb_set_domain: setting domain %s, was %s\n", domain,
593 (sampass->domain)?(sampass->domain):"NULL"));
595 sampass->domain = talloc_strdup(sampass, domain);
597 if (!sampass->domain) {
598 DEBUG(0, ("pdb_set_domain: talloc_strdup() failed!\n"));
599 return False;
601 } else {
602 sampass->domain = PDB_NOT_QUITE_NULL;
605 return pdb_set_init_flags(sampass, PDB_DOMAIN, flag);
608 /*********************************************************************
609 Set the user's NT name.
610 ********************************************************************/
612 bool pdb_set_nt_username(struct samu *sampass, const char *nt_username, enum pdb_value_state flag)
614 if (nt_username) {
615 DEBUG(10, ("pdb_set_nt_username: setting nt username %s, was %s\n", nt_username,
616 (sampass->nt_username)?(sampass->nt_username):"NULL"));
618 sampass->nt_username = talloc_strdup(sampass, nt_username);
620 if (!sampass->nt_username) {
621 DEBUG(0, ("pdb_set_nt_username: talloc_strdup() failed!\n"));
622 return False;
624 } else {
625 sampass->nt_username = PDB_NOT_QUITE_NULL;
628 return pdb_set_init_flags(sampass, PDB_NTUSERNAME, flag);
631 /*********************************************************************
632 Set the user's full name.
633 ********************************************************************/
635 bool pdb_set_fullname(struct samu *sampass, const char *full_name, enum pdb_value_state flag)
637 if (full_name) {
638 DEBUG(10, ("pdb_set_full_name: setting full name %s, was %s\n", full_name,
639 (sampass->full_name)?(sampass->full_name):"NULL"));
641 sampass->full_name = talloc_strdup(sampass, full_name);
643 if (!sampass->full_name) {
644 DEBUG(0, ("pdb_set_fullname: talloc_strdup() failed!\n"));
645 return False;
647 } else {
648 sampass->full_name = PDB_NOT_QUITE_NULL;
651 return pdb_set_init_flags(sampass, PDB_FULLNAME, flag);
654 /*********************************************************************
655 Set the user's logon script.
656 ********************************************************************/
658 bool pdb_set_logon_script(struct samu *sampass, const char *logon_script, enum pdb_value_state flag)
660 if (logon_script) {
661 DEBUG(10, ("pdb_set_logon_script: setting logon script %s, was %s\n", logon_script,
662 (sampass->logon_script)?(sampass->logon_script):"NULL"));
664 sampass->logon_script = talloc_strdup(sampass, logon_script);
666 if (!sampass->logon_script) {
667 DEBUG(0, ("pdb_set_logon_script: talloc_strdup() failed!\n"));
668 return False;
670 } else {
671 sampass->logon_script = PDB_NOT_QUITE_NULL;
674 return pdb_set_init_flags(sampass, PDB_LOGONSCRIPT, flag);
677 /*********************************************************************
678 Set the user's profile path.
679 ********************************************************************/
681 bool pdb_set_profile_path(struct samu *sampass, const char *profile_path, enum pdb_value_state flag)
683 if (profile_path) {
684 DEBUG(10, ("pdb_set_profile_path: setting profile path %s, was %s\n", profile_path,
685 (sampass->profile_path)?(sampass->profile_path):"NULL"));
687 sampass->profile_path = talloc_strdup(sampass, profile_path);
689 if (!sampass->profile_path) {
690 DEBUG(0, ("pdb_set_profile_path: talloc_strdup() failed!\n"));
691 return False;
693 } else {
694 sampass->profile_path = PDB_NOT_QUITE_NULL;
697 return pdb_set_init_flags(sampass, PDB_PROFILE, flag);
700 /*********************************************************************
701 Set the user's directory drive.
702 ********************************************************************/
704 bool pdb_set_dir_drive(struct samu *sampass, const char *dir_drive, enum pdb_value_state flag)
706 if (dir_drive) {
707 DEBUG(10, ("pdb_set_dir_drive: setting dir drive %s, was %s\n", dir_drive,
708 (sampass->dir_drive)?(sampass->dir_drive):"NULL"));
710 sampass->dir_drive = talloc_strdup(sampass, dir_drive);
712 if (!sampass->dir_drive) {
713 DEBUG(0, ("pdb_set_dir_drive: talloc_strdup() failed!\n"));
714 return False;
717 } else {
718 sampass->dir_drive = PDB_NOT_QUITE_NULL;
721 return pdb_set_init_flags(sampass, PDB_DRIVE, flag);
724 /*********************************************************************
725 Set the user's home directory.
726 ********************************************************************/
728 bool pdb_set_homedir(struct samu *sampass, const char *home_dir, enum pdb_value_state flag)
730 if (home_dir) {
731 DEBUG(10, ("pdb_set_homedir: setting home dir %s, was %s\n", home_dir,
732 (sampass->home_dir)?(sampass->home_dir):"NULL"));
734 sampass->home_dir = talloc_strdup(sampass, home_dir);
736 if (!sampass->home_dir) {
737 DEBUG(0, ("pdb_set_home_dir: talloc_strdup() failed!\n"));
738 return False;
740 } else {
741 sampass->home_dir = PDB_NOT_QUITE_NULL;
744 return pdb_set_init_flags(sampass, PDB_SMBHOME, flag);
747 /*********************************************************************
748 Set the user's account description.
749 ********************************************************************/
751 bool pdb_set_acct_desc(struct samu *sampass, const char *acct_desc, enum pdb_value_state flag)
753 if (acct_desc) {
754 sampass->acct_desc = talloc_strdup(sampass, acct_desc);
756 if (!sampass->acct_desc) {
757 DEBUG(0, ("pdb_set_acct_desc: talloc_strdup() failed!\n"));
758 return False;
760 } else {
761 sampass->acct_desc = PDB_NOT_QUITE_NULL;
764 return pdb_set_init_flags(sampass, PDB_ACCTDESC, flag);
767 /*********************************************************************
768 Set the user's workstation allowed list.
769 ********************************************************************/
771 bool pdb_set_workstations(struct samu *sampass, const char *workstations, enum pdb_value_state flag)
773 if (workstations) {
774 DEBUG(10, ("pdb_set_workstations: setting workstations %s, was %s\n", workstations,
775 (sampass->workstations)?(sampass->workstations):"NULL"));
777 sampass->workstations = talloc_strdup(sampass, workstations);
779 if (!sampass->workstations) {
780 DEBUG(0, ("pdb_set_workstations: talloc_strdup() failed!\n"));
781 return False;
783 } else {
784 sampass->workstations = PDB_NOT_QUITE_NULL;
787 return pdb_set_init_flags(sampass, PDB_WORKSTATIONS, flag);
790 /*********************************************************************
791 ********************************************************************/
793 bool pdb_set_comment(struct samu *sampass, const char *comment, enum pdb_value_state flag)
795 if (comment) {
796 sampass->comment = talloc_strdup(sampass, comment);
798 if (!sampass->comment) {
799 DEBUG(0, ("pdb_set_comment: talloc_strdup() failed!\n"));
800 return False;
802 } else {
803 sampass->comment = PDB_NOT_QUITE_NULL;
806 return pdb_set_init_flags(sampass, PDB_COMMENT, flag);
809 /*********************************************************************
810 Set the user's dial string.
811 ********************************************************************/
813 bool pdb_set_munged_dial(struct samu *sampass, const char *munged_dial, enum pdb_value_state flag)
815 if (munged_dial) {
816 sampass->munged_dial = talloc_strdup(sampass, munged_dial);
818 if (!sampass->munged_dial) {
819 DEBUG(0, ("pdb_set_munged_dial: talloc_strdup() failed!\n"));
820 return False;
822 } else {
823 sampass->munged_dial = PDB_NOT_QUITE_NULL;
826 return pdb_set_init_flags(sampass, PDB_MUNGEDDIAL, flag);
829 /*********************************************************************
830 Set the user's NT hash.
831 ********************************************************************/
833 bool pdb_set_nt_passwd(struct samu *sampass, const uint8 pwd[NT_HASH_LEN], enum pdb_value_state flag)
835 data_blob_clear_free(&sampass->nt_pw);
837 if (pwd) {
838 sampass->nt_pw =
839 data_blob_talloc(sampass, pwd, NT_HASH_LEN);
840 } else {
841 sampass->nt_pw = data_blob_null;
844 return pdb_set_init_flags(sampass, PDB_NTPASSWD, flag);
847 /*********************************************************************
848 Set the user's LM hash.
849 ********************************************************************/
851 bool pdb_set_lanman_passwd(struct samu *sampass, const uint8 pwd[LM_HASH_LEN], enum pdb_value_state flag)
853 data_blob_clear_free(&sampass->lm_pw);
855 /* on keep the password if we are allowing LANMAN authentication */
857 if (pwd && lp_lanman_auth() ) {
858 sampass->lm_pw = data_blob_talloc(sampass, pwd, LM_HASH_LEN);
859 } else {
860 sampass->lm_pw = data_blob_null;
863 return pdb_set_init_flags(sampass, PDB_LMPASSWD, flag);
866 /*********************************************************************
867 Set the user's password history hash. historyLen is the number of
868 PW_HISTORY_SALT_LEN+SALTED_MD5_HASH_LEN length
869 entries to store in the history - this must match the size of the uint8 array
870 in pwd.
871 ********************************************************************/
873 bool pdb_set_pw_history(struct samu *sampass, const uint8 *pwd, uint32_t historyLen, enum pdb_value_state flag)
875 if (historyLen && pwd){
876 data_blob_free(&(sampass->nt_pw_his));
877 sampass->nt_pw_his = data_blob_talloc(sampass,
878 pwd, historyLen*PW_HISTORY_ENTRY_LEN);
879 if (!sampass->nt_pw_his.length) {
880 DEBUG(0, ("pdb_set_pw_history: data_blob_talloc() failed!\n"));
881 return False;
883 } else {
884 sampass->nt_pw_his = data_blob_talloc(sampass, NULL, 0);
887 return pdb_set_init_flags(sampass, PDB_PWHISTORY, flag);
890 /*********************************************************************
891 Set the user's plaintext password only (base procedure, see helper
892 below)
893 ********************************************************************/
895 bool pdb_set_plaintext_pw_only(struct samu *sampass, const char *password, enum pdb_value_state flag)
897 if (password) {
898 if (sampass->plaintext_pw!=NULL)
899 memset(sampass->plaintext_pw,'\0',strlen(sampass->plaintext_pw)+1);
901 sampass->plaintext_pw = talloc_strdup(sampass, password);
903 if (!sampass->plaintext_pw) {
904 DEBUG(0, ("pdb_set_unknown_str: talloc_strdup() failed!\n"));
905 return False;
907 } else {
908 sampass->plaintext_pw = NULL;
911 return pdb_set_init_flags(sampass, PDB_PLAINTEXT_PW, flag);
914 bool pdb_set_bad_password_count(struct samu *sampass, uint16_t bad_password_count, enum pdb_value_state flag)
916 sampass->bad_password_count = bad_password_count;
917 return pdb_set_init_flags(sampass, PDB_BAD_PASSWORD_COUNT, flag);
920 bool pdb_set_logon_count(struct samu *sampass, uint16_t logon_count, enum pdb_value_state flag)
922 sampass->logon_count = logon_count;
923 return pdb_set_init_flags(sampass, PDB_LOGON_COUNT, flag);
926 bool pdb_set_country_code(struct samu *sampass, uint16_t country_code,
927 enum pdb_value_state flag)
929 sampass->country_code = country_code;
930 return pdb_set_init_flags(sampass, PDB_COUNTRY_CODE, flag);
933 bool pdb_set_code_page(struct samu *sampass, uint16_t code_page,
934 enum pdb_value_state flag)
936 sampass->code_page = code_page;
937 return pdb_set_init_flags(sampass, PDB_CODE_PAGE, flag);
940 bool pdb_set_unknown_6(struct samu *sampass, uint32_t unkn, enum pdb_value_state flag)
942 sampass->unknown_6 = unkn;
943 return pdb_set_init_flags(sampass, PDB_UNKNOWN6, flag);
946 bool pdb_set_hours(struct samu *sampass, const uint8 *hours, int hours_len,
947 enum pdb_value_state flag)
949 if (hours_len > sizeof(sampass->hours)) {
950 return false;
953 if (!hours) {
954 memset ((char *)sampass->hours, 0, hours_len);
955 } else {
956 memcpy (sampass->hours, hours, hours_len);
959 return pdb_set_init_flags(sampass, PDB_HOURS, flag);
962 bool pdb_set_backend_private_data(struct samu *sampass, void *private_data,
963 void (*free_fn)(void **),
964 const struct pdb_methods *my_methods,
965 enum pdb_value_state flag)
967 if (sampass->backend_private_data &&
968 sampass->backend_private_data_free_fn) {
969 sampass->backend_private_data_free_fn(
970 &sampass->backend_private_data);
973 sampass->backend_private_data = private_data;
974 sampass->backend_private_data_free_fn = free_fn;
975 sampass->backend_private_methods = my_methods;
977 return pdb_set_init_flags(sampass, PDB_BACKEND_PRIVATE_DATA, flag);
981 /* Helpful interfaces to the above */
983 bool pdb_set_pass_can_change(struct samu *sampass, bool canchange)
985 return pdb_set_pass_can_change_time(sampass,
986 canchange ? 0 : pdb_password_change_time_max(),
987 PDB_CHANGED);
991 /*********************************************************************
992 Set the user's PLAINTEXT password. Used as an interface to the above.
993 Also sets the last change time to NOW.
994 ********************************************************************/
996 bool pdb_set_plaintext_passwd(struct samu *sampass, const char *plaintext)
998 uchar new_lanman_p16[LM_HASH_LEN];
999 uchar new_nt_p16[NT_HASH_LEN];
1000 uchar *pwhistory;
1001 uint32_t pwHistLen;
1002 uint32_t current_history_len;
1004 if (!plaintext)
1005 return False;
1007 /* Calculate the MD4 hash (NT compatible) of the password */
1008 E_md4hash(plaintext, new_nt_p16);
1010 if (!pdb_set_nt_passwd (sampass, new_nt_p16, PDB_CHANGED))
1011 return False;
1013 if (!E_deshash(plaintext, new_lanman_p16)) {
1014 /* E_deshash returns false for 'long' passwords (> 14
1015 DOS chars). This allows us to match Win2k, which
1016 does not store a LM hash for these passwords (which
1017 would reduce the effective password length to 14 */
1019 if (!pdb_set_lanman_passwd (sampass, NULL, PDB_CHANGED))
1020 return False;
1021 } else {
1022 if (!pdb_set_lanman_passwd (sampass, new_lanman_p16, PDB_CHANGED))
1023 return False;
1026 if (!pdb_set_plaintext_pw_only (sampass, plaintext, PDB_CHANGED))
1027 return False;
1029 if (!pdb_set_pass_last_set_time (sampass, time(NULL), PDB_CHANGED))
1030 return False;
1032 if ((pdb_get_acct_ctrl(sampass) & ACB_NORMAL) == 0) {
1034 * No password history for non-user accounts
1036 return true;
1039 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY, &pwHistLen);
1041 if (pwHistLen == 0) {
1042 /* Set the history length to zero. */
1043 pdb_set_pw_history(sampass, NULL, 0, PDB_CHANGED);
1044 return true;
1048 * We need to make sure we don't have a race condition here -
1049 * the account policy history length can change between when
1050 * the pw_history was first loaded into the struct samu struct
1051 * and now.... JRA.
1053 pwhistory = (uchar *)pdb_get_pw_history(sampass, &current_history_len);
1055 if ((current_history_len != 0) && (pwhistory == NULL)) {
1056 DEBUG(1, ("pdb_set_plaintext_passwd: pwhistory == NULL!\n"));
1057 return false;
1060 if (current_history_len < pwHistLen) {
1062 * Ensure we have space for the needed history. This
1063 * also takes care of an account which did not have
1064 * any history at all so far, i.e. pwhistory==NULL
1066 uchar *new_history = talloc_zero_array(
1067 sampass, uchar,
1068 pwHistLen*PW_HISTORY_ENTRY_LEN);
1070 if (!new_history) {
1071 return False;
1074 memcpy(new_history, pwhistory,
1075 current_history_len*PW_HISTORY_ENTRY_LEN);
1077 pwhistory = new_history;
1081 * Make room for the new password in the history list.
1083 if (pwHistLen > 1) {
1084 memmove(&pwhistory[PW_HISTORY_ENTRY_LEN], pwhistory,
1085 (pwHistLen-1)*PW_HISTORY_ENTRY_LEN );
1089 * Fill the salt area with 0-s: this indicates that
1090 * a plain nt hash is stored in the has area.
1091 * The old format was to store a 16 byte salt and
1092 * then an md5hash of the nt_hash concatenated with
1093 * the salt.
1095 memset(pwhistory, 0, PW_HISTORY_SALT_LEN);
1098 * Store the plain nt hash in the second 16 bytes.
1099 * The old format was to store the md5 hash of
1100 * the salt+newpw.
1102 memcpy(&pwhistory[PW_HISTORY_SALT_LEN], new_nt_p16, SALTED_MD5_HASH_LEN);
1104 pdb_set_pw_history(sampass, pwhistory, pwHistLen, PDB_CHANGED);
1106 return True;
1109 /* check for any PDB_SET/CHANGED field and fill the appropriate mask bit */
1110 uint32_t pdb_build_fields_present(struct samu *sampass)
1112 /* value set to all for testing */
1113 return 0x00ffffff;
1116 /**********************************************************************
1117 Helper function to determine for update_sam_account whether
1118 we need LDAP modification.
1119 *********************************************************************/
1121 bool pdb_element_is_changed(const struct samu *sampass,
1122 enum pdb_elements element)
1124 return IS_SAM_CHANGED(sampass, element);
1127 /**********************************************************************
1128 Helper function to determine for update_sam_account whether
1129 we need LDAP modification.
1130 *********************************************************************/
1132 bool pdb_element_is_set_or_changed(const struct samu *sampass,
1133 enum pdb_elements element)
1135 return (IS_SAM_SET(sampass, element) ||
1136 IS_SAM_CHANGED(sampass, element));