2 Unix SMB/CIFS implementation.
3 SAM_ACCOUNT 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-2001
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 2 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, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #define DBGC_CLASS DBGC_PASSDB
31 * @todo Redefine this to NULL, but this changes the API becouse
32 * much of samba assumes that the pdb_get...() funtions
33 * return pstrings. (ie not null-pointers).
34 * See also pdb_fill_default_sam().
37 #define PDB_NOT_QUITE_NULL ""
39 /*********************************************************************
40 Collection of get...() functions for SAM_ACCOUNT_INFO.
41 ********************************************************************/
43 uint16 pdb_get_acct_ctrl (const SAM_ACCOUNT *sampass)
46 return (sampass->private.acct_ctrl);
48 return (ACB_DISABLED);
51 time_t pdb_get_logon_time (const SAM_ACCOUNT *sampass)
54 return (sampass->private.logon_time);
59 time_t pdb_get_logoff_time (const SAM_ACCOUNT *sampass)
62 return (sampass->private.logoff_time);
67 time_t pdb_get_kickoff_time (const SAM_ACCOUNT *sampass)
70 return (sampass->private.kickoff_time);
75 time_t pdb_get_pass_last_set_time (const SAM_ACCOUNT *sampass)
78 return (sampass->private.pass_last_set_time);
83 time_t pdb_get_pass_can_change_time (const SAM_ACCOUNT *sampass)
86 return (sampass->private.pass_can_change_time);
91 time_t pdb_get_pass_must_change_time (const SAM_ACCOUNT *sampass)
94 return (sampass->private.pass_must_change_time);
99 uint16 pdb_get_logon_divs (const SAM_ACCOUNT *sampass)
102 return (sampass->private.logon_divs);
107 uint32 pdb_get_hours_len (const SAM_ACCOUNT *sampass)
110 return (sampass->private.hours_len);
115 const uint8* pdb_get_hours (const SAM_ACCOUNT *sampass)
118 return (sampass->private.hours);
123 const uint8* pdb_get_nt_passwd (const SAM_ACCOUNT *sampass)
126 SMB_ASSERT((!sampass->private.nt_pw.data)
127 || sampass->private.nt_pw.length == NT_HASH_LEN);
128 return ((uint8*)sampass->private.nt_pw.data);
134 const uint8* pdb_get_lanman_passwd (const SAM_ACCOUNT *sampass)
137 SMB_ASSERT((!sampass->private.lm_pw.data)
138 || sampass->private.lm_pw.length == LM_HASH_LEN);
139 return ((uint8*)sampass->private.lm_pw.data);
145 /* Return the plaintext password if known. Most of the time
146 it isn't, so don't assume anything magic about this function.
148 Used to pass the plaintext to passdb backends that might
149 want to store more than just the NTLM hashes.
151 const char* pdb_get_plaintext_passwd (const SAM_ACCOUNT *sampass)
154 return ((char*)sampass->private.plaintext_pw.data);
159 const DOM_SID *pdb_get_user_sid(const SAM_ACCOUNT *sampass)
162 return &sampass->private.user_sid;
167 const DOM_SID *pdb_get_group_sid(const SAM_ACCOUNT *sampass)
170 return &sampass->private.group_sid;
175 uint32 pdb_get_user_rid (const SAM_ACCOUNT *sampass)
180 if (sid_peek_check_rid(get_global_sam_sid(), pdb_get_user_sid(sampass),&u_rid))
186 uint32 pdb_get_group_rid (const SAM_ACCOUNT *sampass)
191 if (sid_peek_check_rid(get_global_sam_sid(), pdb_get_group_sid(sampass),&g_rid))
197 * Get flags showing what is initalised in the SAM_ACCOUNT
198 * @param sampass the SAM_ACCOUNT in question
199 * @return the flags indicating the members initialised in the struct.
202 uint32 pdb_get_init_flag (const SAM_ACCOUNT *sampass)
205 return sampass->private.init_flag;
207 return FLAG_SAM_UNINIT;
210 uid_t pdb_get_uid (const SAM_ACCOUNT *sampass)
213 return (sampass->private.uid);
218 gid_t pdb_get_gid (const SAM_ACCOUNT *sampass)
221 return (sampass->private.gid);
226 const char* pdb_get_username (const SAM_ACCOUNT *sampass)
229 return (sampass->private.username);
234 const char* pdb_get_domain (const SAM_ACCOUNT *sampass)
237 return (sampass->private.domain);
242 const char* pdb_get_nt_username (const SAM_ACCOUNT *sampass)
245 return (sampass->private.nt_username);
250 const char* pdb_get_fullname (const SAM_ACCOUNT *sampass)
253 return (sampass->private.full_name);
258 const char* pdb_get_homedir (const SAM_ACCOUNT *sampass)
261 return (sampass->private.home_dir);
266 const char* pdb_get_unix_homedir (const SAM_ACCOUNT *sampass)
269 return (sampass->private.unix_home_dir);
274 const char* pdb_get_dirdrive (const SAM_ACCOUNT *sampass)
277 return (sampass->private.dir_drive);
282 const char* pdb_get_logon_script (const SAM_ACCOUNT *sampass)
285 return (sampass->private.logon_script);
290 const char* pdb_get_profile_path (const SAM_ACCOUNT *sampass)
293 return (sampass->private.profile_path);
298 const char* pdb_get_acct_desc (const SAM_ACCOUNT *sampass)
301 return (sampass->private.acct_desc);
306 const char* pdb_get_workstations (const SAM_ACCOUNT *sampass)
309 return (sampass->private.workstations);
314 const char* pdb_get_unknown_str (const SAM_ACCOUNT *sampass)
317 return (sampass->private.unknown_str);
322 const char* pdb_get_munged_dial (const SAM_ACCOUNT *sampass)
325 return (sampass->private.munged_dial);
330 uint32 pdb_get_unknown3 (const SAM_ACCOUNT *sampass)
333 return (sampass->private.unknown_3);
338 uint32 pdb_get_unknown5 (const SAM_ACCOUNT *sampass)
341 return (sampass->private.unknown_5);
346 uint32 pdb_get_unknown6 (const SAM_ACCOUNT *sampass)
349 return (sampass->private.unknown_6);
354 /*********************************************************************
355 Collection of set...() functions for SAM_ACCOUNT_INFO.
356 ********************************************************************/
358 BOOL pdb_set_acct_ctrl (SAM_ACCOUNT *sampass, uint16 flags)
364 sampass->private.acct_ctrl = flags;
371 BOOL pdb_set_logon_time (SAM_ACCOUNT *sampass, time_t mytime, BOOL store)
376 sampass->private.logon_time = mytime;
379 pdb_set_init_flag(sampass, FLAG_SAM_LOGONTIME);
384 BOOL pdb_set_logoff_time (SAM_ACCOUNT *sampass, time_t mytime, BOOL store)
389 sampass->private.logoff_time = mytime;
392 pdb_set_init_flag(sampass, FLAG_SAM_LOGOFFTIME);
397 BOOL pdb_set_kickoff_time (SAM_ACCOUNT *sampass, time_t mytime, BOOL store)
402 sampass->private.kickoff_time = mytime;
405 pdb_set_init_flag(sampass, FLAG_SAM_KICKOFFTIME);
410 BOOL pdb_set_pass_can_change_time (SAM_ACCOUNT *sampass, time_t mytime, BOOL store)
415 sampass->private.pass_can_change_time = mytime;
418 pdb_set_init_flag(sampass, FLAG_SAM_CANCHANGETIME);
423 BOOL pdb_set_pass_must_change_time (SAM_ACCOUNT *sampass, time_t mytime, BOOL store)
428 sampass->private.pass_must_change_time = mytime;
431 pdb_set_init_flag(sampass, FLAG_SAM_MUSTCHANGETIME);
436 BOOL pdb_set_pass_last_set_time (SAM_ACCOUNT *sampass, time_t mytime)
441 sampass->private.pass_last_set_time = mytime;
446 BOOL pdb_set_hours_len (SAM_ACCOUNT *sampass, uint32 len)
451 sampass->private.hours_len = len;
455 BOOL pdb_set_logon_divs (SAM_ACCOUNT *sampass, uint16 hours)
460 sampass->private.logon_divs = hours;
465 * Set flags showing what is initalised in the SAM_ACCOUNT
466 * @param sampass the SAM_ACCOUNT in question
467 * @param flag The *new* flag to be set. Old flags preserved
468 * this flag is only added.
471 BOOL pdb_set_init_flag (SAM_ACCOUNT *sampass, uint32 flag)
476 sampass->private.init_flag |= flag;
481 BOOL pdb_set_uid (SAM_ACCOUNT *sampass, const uid_t uid)
486 DEBUG(10, ("pdb_set_uid: setting uid %d, was %d\n",
487 (int)uid, (int)sampass->private.uid));
489 sampass->private.uid = uid;
490 pdb_set_init_flag(sampass, FLAG_SAM_UID);
496 BOOL pdb_set_gid (SAM_ACCOUNT *sampass, const gid_t gid)
501 DEBUG(10, ("pdb_set_gid: setting gid %d, was %d\n",
502 (int)gid, (int)sampass->private.gid));
504 sampass->private.gid = gid;
505 pdb_set_init_flag(sampass, FLAG_SAM_GID);
511 BOOL pdb_set_user_sid (SAM_ACCOUNT *sampass, DOM_SID *u_sid)
513 if (!sampass || !u_sid)
516 sid_copy(&sampass->private.user_sid, u_sid);
518 DEBUG(10, ("pdb_set_user_sid: setting user sid %s\n",
519 sid_string_static(&sampass->private.user_sid)));
524 BOOL pdb_set_group_sid(SAM_ACCOUNT *sampass, DOM_SID *g_sid)
526 if (!sampass || !g_sid)
529 sid_copy(&sampass->private.group_sid, g_sid);
531 DEBUG(10, ("pdb_set_group_sid: setting group sid %s\n",
532 sid_string_static(&sampass->private.group_sid)));
537 BOOL pdb_set_user_sid_from_rid (SAM_ACCOUNT *sampass, uint32 rid)
540 const DOM_SID *global_sam_sid;
545 if (!(global_sam_sid = get_global_sam_sid())) {
546 DEBUG(1, ("pdb_set_user_sid_from_rid: Could not read global sam sid!\n"));
550 sid_copy(&u_sid, global_sam_sid);
552 if (!sid_append_rid(&u_sid, rid))
555 if (!pdb_set_user_sid(sampass, &u_sid))
558 DEBUG(10, ("pdb_set_user_sid_from_rid:\n\tsetting user sid %s from rid %d\n",
559 sid_string_static(&u_sid),rid));
564 BOOL pdb_set_group_sid_from_rid (SAM_ACCOUNT *sampass, uint32 grid)
567 const DOM_SID *global_sam_sid;
572 if (!(global_sam_sid = get_global_sam_sid())) {
573 DEBUG(1, ("pdb_set_user_sid_from_rid: Could not read global sam sid!\n"));
577 sid_copy(&g_sid, global_sam_sid);
579 if (!sid_append_rid(&g_sid, grid))
582 if (!pdb_set_group_sid(sampass, &g_sid))
585 DEBUG(10, ("pdb_set_group_sid_from_rid:\n\tsetting group sid %s from rid %d\n",
586 sid_string_static(&g_sid), grid));
591 /*********************************************************************
592 Set the user's UNIX name.
593 ********************************************************************/
595 BOOL pdb_set_username(SAM_ACCOUNT *sampass, const char *username)
601 DEBUG(10, ("pdb_set_username: setting username %s, was %s\n", username,
602 (sampass->private.username)?(sampass->private.username):"NULL"));
604 sampass->private.username = talloc_strdup(sampass->mem_ctx, username);
606 if (!sampass->private.username) {
607 DEBUG(0, ("pdb_set_username: talloc_strdup() failed!\n"));
612 sampass->private.username = PDB_NOT_QUITE_NULL;
618 /*********************************************************************
620 ********************************************************************/
622 BOOL pdb_set_domain(SAM_ACCOUNT *sampass, const char *domain)
628 DEBUG(10, ("pdb_set_domain: setting domain %s, was %s\n", domain,
629 (sampass->private.domain)?(sampass->private.domain):"NULL"));
631 sampass->private.domain = talloc_strdup(sampass->mem_ctx, domain);
633 if (!sampass->private.domain) {
634 DEBUG(0, ("pdb_set_domain: talloc_strdup() failed!\n"));
639 sampass->private.domain = PDB_NOT_QUITE_NULL;
645 /*********************************************************************
646 Set the user's NT name.
647 ********************************************************************/
649 BOOL pdb_set_nt_username(SAM_ACCOUNT *sampass, const char *nt_username)
655 DEBUG(10, ("pdb_set_nt_username: setting nt username %s, was %s\n", nt_username,
656 (sampass->private.nt_username)?(sampass->private.nt_username):"NULL"));
658 sampass->private.nt_username = talloc_strdup(sampass->mem_ctx, nt_username);
660 if (!sampass->private.nt_username) {
661 DEBUG(0, ("pdb_set_nt_username: talloc_strdup() failed!\n"));
666 sampass->private.nt_username = PDB_NOT_QUITE_NULL;
672 /*********************************************************************
673 Set the user's full name.
674 ********************************************************************/
676 BOOL pdb_set_fullname(SAM_ACCOUNT *sampass, const char *full_name)
682 DEBUG(10, ("pdb_set_full_name: setting full name %s, was %s\n", full_name,
683 (sampass->private.full_name)?(sampass->private.full_name):"NULL"));
685 sampass->private.full_name = talloc_strdup(sampass->mem_ctx, full_name);
687 if (!sampass->private.full_name) {
688 DEBUG(0, ("pdb_set_fullname: talloc_strdup() failed!\n"));
693 sampass->private.full_name = PDB_NOT_QUITE_NULL;
699 /*********************************************************************
700 Set the user's logon script.
701 ********************************************************************/
703 BOOL pdb_set_logon_script(SAM_ACCOUNT *sampass, const char *logon_script, BOOL store)
709 DEBUG(10, ("pdb_set_logon_script: setting logon script %s, was %s\n", logon_script,
710 (sampass->private.logon_script)?(sampass->private.logon_script):"NULL"));
712 sampass->private.logon_script = talloc_strdup(sampass->mem_ctx, logon_script);
714 if (!sampass->private.logon_script) {
715 DEBUG(0, ("pdb_set_logon_script: talloc_strdup() failed!\n"));
720 sampass->private.logon_script = PDB_NOT_QUITE_NULL;
724 DEBUG(10, ("pdb_set_logon_script: setting logon script sam flag!\n"));
725 pdb_set_init_flag(sampass, FLAG_SAM_LOGONSCRIPT);
731 /*********************************************************************
732 Set the user's profile path.
733 ********************************************************************/
735 BOOL pdb_set_profile_path (SAM_ACCOUNT *sampass, const char *profile_path, BOOL store)
741 DEBUG(10, ("pdb_set_profile_path: setting profile path %s, was %s\n", profile_path,
742 (sampass->private.profile_path)?(sampass->private.profile_path):"NULL"));
744 sampass->private.profile_path = talloc_strdup(sampass->mem_ctx, profile_path);
746 if (!sampass->private.profile_path) {
747 DEBUG(0, ("pdb_set_profile_path: talloc_strdup() failed!\n"));
752 sampass->private.profile_path = PDB_NOT_QUITE_NULL;
756 DEBUG(10, ("pdb_set_profile_path: setting profile path sam flag!\n"));
757 pdb_set_init_flag(sampass, FLAG_SAM_PROFILE);
763 /*********************************************************************
764 Set the user's directory drive.
765 ********************************************************************/
767 BOOL pdb_set_dir_drive (SAM_ACCOUNT *sampass, const char *dir_drive, BOOL store)
773 DEBUG(10, ("pdb_set_dir_drive: setting dir drive %s, was %s\n", dir_drive,
774 (sampass->private.dir_drive)?(sampass->private.dir_drive):"NULL"));
776 sampass->private.dir_drive = talloc_strdup(sampass->mem_ctx, dir_drive);
778 if (!sampass->private.dir_drive) {
779 DEBUG(0, ("pdb_set_dir_drive: talloc_strdup() failed!\n"));
784 sampass->private.dir_drive = PDB_NOT_QUITE_NULL;
788 DEBUG(10, ("pdb_set_dir_drive: setting dir drive sam flag!\n"));
789 pdb_set_init_flag(sampass, FLAG_SAM_DRIVE);
795 /*********************************************************************
796 Set the user's home directory.
797 ********************************************************************/
799 BOOL pdb_set_homedir (SAM_ACCOUNT *sampass, const char *home_dir, BOOL store)
805 DEBUG(10, ("pdb_set_homedir: setting home dir %s, was %s\n", home_dir,
806 (sampass->private.home_dir)?(sampass->private.home_dir):"NULL"));
808 sampass->private.home_dir = talloc_strdup(sampass->mem_ctx, home_dir);
810 if (!sampass->private.home_dir) {
811 DEBUG(0, ("pdb_set_home_dir: talloc_strdup() failed!\n"));
816 sampass->private.home_dir = PDB_NOT_QUITE_NULL;
820 DEBUG(10, ("pdb_set_homedir: setting home dir sam flag!"));
821 pdb_set_init_flag(sampass, FLAG_SAM_SMBHOME);
827 /*********************************************************************
828 Set the user's unix home directory.
829 ********************************************************************/
831 BOOL pdb_set_unix_homedir (SAM_ACCOUNT *sampass, const char *unix_home_dir)
837 DEBUG(10, ("pdb_set_homedir: setting home dir %s, was %s\n", unix_home_dir,
838 (sampass->private.unix_home_dir)?(sampass->private.unix_home_dir):"NULL"));
840 sampass->private.unix_home_dir = talloc_strdup(sampass->mem_ctx,
843 if (!sampass->private.unix_home_dir) {
844 DEBUG(0, ("pdb_set_unix_home_dir: talloc_strdup() failed!\n"));
849 sampass->private.unix_home_dir = PDB_NOT_QUITE_NULL;
855 /*********************************************************************
856 Set the user's account description.
857 ********************************************************************/
859 BOOL pdb_set_acct_desc (SAM_ACCOUNT *sampass, const char *acct_desc)
865 sampass->private.acct_desc = talloc_strdup(sampass->mem_ctx, acct_desc);
867 if (!sampass->private.acct_desc) {
868 DEBUG(0, ("pdb_set_acct_desc: talloc_strdup() failed!\n"));
873 sampass->private.acct_desc = PDB_NOT_QUITE_NULL;
879 /*********************************************************************
880 Set the user's workstation allowed list.
881 ********************************************************************/
883 BOOL pdb_set_workstations (SAM_ACCOUNT *sampass, const char *workstations)
889 DEBUG(10, ("pdb_set_workstations: setting workstations %s, was %s\n", workstations,
890 (sampass->private.workstations)?(sampass->private.workstations):"NULL"));
892 sampass->private.workstations = talloc_strdup(sampass->mem_ctx, workstations);
894 if (!sampass->private.workstations) {
895 DEBUG(0, ("pdb_set_workstations: talloc_strdup() failed!\n"));
900 sampass->private.workstations = PDB_NOT_QUITE_NULL;
906 /*********************************************************************
907 Set the user's 'unknown_str', whatever the heck this actually is...
908 ********************************************************************/
910 BOOL pdb_set_unknown_str (SAM_ACCOUNT *sampass, const char *unknown_str)
916 sampass->private.unknown_str = talloc_strdup(sampass->mem_ctx, unknown_str);
918 if (!sampass->private.unknown_str) {
919 DEBUG(0, ("pdb_set_unknown_str: talloc_strdup() failed!\n"));
924 sampass->private.unknown_str = PDB_NOT_QUITE_NULL;
930 /*********************************************************************
931 Set the user's dial string.
932 ********************************************************************/
934 BOOL pdb_set_munged_dial (SAM_ACCOUNT *sampass, const char *munged_dial)
940 sampass->private.munged_dial = talloc_strdup(sampass->mem_ctx, munged_dial);
942 if (!sampass->private.munged_dial) {
943 DEBUG(0, ("pdb_set_munged_dial: talloc_strdup() failed!\n"));
948 sampass->private.munged_dial = PDB_NOT_QUITE_NULL;
954 /*********************************************************************
955 Set the user's NT hash.
956 ********************************************************************/
958 BOOL pdb_set_nt_passwd (SAM_ACCOUNT *sampass, const uint8 *pwd)
963 data_blob_clear_free(&sampass->private.nt_pw);
965 sampass->private.nt_pw = data_blob(pwd, NT_HASH_LEN);
970 /*********************************************************************
971 Set the user's LM hash.
972 ********************************************************************/
974 BOOL pdb_set_lanman_passwd (SAM_ACCOUNT *sampass, const uint8 pwd[16])
979 data_blob_clear_free(&sampass->private.lm_pw);
981 sampass->private.lm_pw = data_blob(pwd, LM_HASH_LEN);
986 /*********************************************************************
987 Set the user's plaintext password only (base procedure, see helper
989 ********************************************************************/
991 BOOL pdb_set_plaintext_pw_only (SAM_ACCOUNT *sampass, const uint8 *password, size_t len)
996 data_blob_clear_free(&sampass->private.plaintext_pw);
998 sampass->private.plaintext_pw = data_blob(password, len);
1003 BOOL pdb_set_unknown_3 (SAM_ACCOUNT *sampass, uint32 unkn)
1008 sampass->private.unknown_3 = unkn;
1012 BOOL pdb_set_unknown_5 (SAM_ACCOUNT *sampass, uint32 unkn)
1017 sampass->private.unknown_5 = unkn;
1021 BOOL pdb_set_unknown_6 (SAM_ACCOUNT *sampass, uint32 unkn)
1026 sampass->private.unknown_6 = unkn;
1030 BOOL pdb_set_hours (SAM_ACCOUNT *sampass, const uint8 *hours)
1036 memset ((char *)sampass->private.hours, 0, MAX_HOURS_LEN);
1040 memcpy (sampass->private.hours, hours, MAX_HOURS_LEN);
1046 /* Helpful interfaces to the above */
1048 /*********************************************************************
1049 Sets the last changed times and must change times for a normal
1051 ********************************************************************/
1053 BOOL pdb_set_pass_changed_now (SAM_ACCOUNT *sampass)
1060 if (!pdb_set_pass_last_set_time (sampass, time(NULL)))
1063 account_policy_get(AP_MAX_PASSWORD_AGE, &expire);
1065 if (expire==(uint32)-1) {
1066 if (!pdb_set_pass_must_change_time (sampass, get_time_t_max(), False))
1069 if (!pdb_set_pass_must_change_time (sampass,
1070 pdb_get_pass_last_set_time(sampass)
1078 /*********************************************************************
1079 Set the user's PLAINTEXT password. Used as an interface to the above.
1080 Also sets the last change time to NOW.
1081 ********************************************************************/
1083 BOOL pdb_set_plaintext_passwd (SAM_ACCOUNT *sampass, const char *plaintext)
1085 uchar new_lanman_p16[16];
1086 uchar new_nt_p16[16];
1088 if (!sampass || !plaintext)
1091 nt_lm_owf_gen (plaintext, new_nt_p16, new_lanman_p16);
1093 if (!pdb_set_nt_passwd (sampass, new_nt_p16))
1096 if (!pdb_set_lanman_passwd (sampass, new_lanman_p16))
1099 if (!pdb_set_pass_changed_now (sampass))