2 * Unix SMB/Netbios implementation. Version 1.9. SMB parameters and setup
3 * Copyright (C) Andrew Tridgell 1992-1998
4 * Copyright (C) Simo Sorce 2000
5 * Copyright (C) Gerald Carter 2000
6 * Copyright (C) Jeremy Allison 2001
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * You should have received a copy of the GNU General Public License along with
19 * this program; if not, write to the Free Software Foundation, Inc., 675
20 * Mass Ave, Cambridge, MA 02139, USA.
27 #define PDB_VERSION "20010830"
28 #define PASSDB_FILE_NAME "/passdb.tdb"
29 #define TDB_FORMAT_STRING "ddddddBBBBBBBBBBBBddBBwdwdBdd"
30 #define USERPREFIX "USER_"
31 #define RIDPREFIX "RID_"
33 extern int DEBUGLEVEL;
34 extern pstring samlogon_user;
35 extern BOOL sam_logon_in_ssb;
37 struct tdb_enum_info {
38 TDB_CONTEXT *passwd_tdb;
42 static struct tdb_enum_info global_tdb_ent;
43 /*static SAM_ACCOUNT global_sam_pass;*/
45 /**********************************************************************
46 Intialize a SAM_ACCOUNT struct from a BYTE buffer of size len
47 *********************************************************************/
49 static BOOL init_sam_from_buffer (SAM_ACCOUNT *sampass, uint8 *buf, uint32 buflen)
52 /* times are stored as 32bit integer
53 take care on system with 64bit wide time_t
60 pass_must_change_time;
73 uint32 username_len, domain_len, nt_username_len,
74 dir_drive_len, unknown_str_len, munged_dial_len,
75 fullname_len, homedir_len, logon_script_len,
76 profile_path_len, acct_desc_len, workstations_len;
78 uint32 /* uid, gid,*/ user_rid, group_rid, unknown_3, hours_len, unknown_5, unknown_6;
79 uint16 acct_ctrl, logon_divs;
81 static uint8 *lm_pw_ptr, *nt_pw_ptr;
83 uint32 lmpwlen, ntpwlen, hourslen;
86 /* unpack the buffer into variables */
87 len = tdb_unpack (buf, buflen, TDB_FORMAT_STRING,
92 &pass_can_change_time,
93 &pass_must_change_time,
94 &username_len, &username,
96 &nt_username_len, &nt_username,
97 &fullname_len, &fullname,
98 &homedir_len, &homedir,
99 &dir_drive_len, &dir_drive,
100 &logon_script_len, &logon_script,
101 &profile_path_len, &profile_path,
102 &acct_desc_len, &acct_desc,
103 &workstations_len, &workstations,
104 &unknown_str_len, &unknown_str,
105 &munged_dial_len, &munged_dial,
108 &lmpwlen, &lm_pw_ptr,
109 &ntpwlen, &nt_pw_ptr,
123 pdb_set_logon_time(sampass, logon_time);
124 pdb_set_logoff_time(sampass, logoff_time);
125 pdb_set_kickoff_time(sampass, kickoff_time);
126 pdb_set_pass_can_change_time(sampass, pass_can_change_time);
127 pdb_set_pass_must_change_time(sampass, pass_must_change_time);
128 pdb_set_pass_last_set_time(sampass, pass_last_set_time);
130 pdb_set_username (sampass, username_len?username:NULL);
131 pdb_set_domain (sampass, domain_len?domain:NULL);
132 pdb_set_nt_username (sampass, nt_username_len?nt_username:NULL);
133 pdb_set_fullname (sampass, fullname_len?fullname:NULL);
134 pdb_set_homedir (sampass, homedir_len?homedir:NULL);
135 pdb_set_dir_drive (sampass, dir_drive_len?dir_drive:NULL);
136 pdb_set_logon_script (sampass, logon_script_len?logon_script:NULL);
137 pdb_set_profile_path (sampass, profile_path_len?profile_path:NULL);
138 pdb_set_acct_desc (sampass, acct_desc_len?acct_desc:NULL);
139 pdb_set_workstations (sampass, workstations_len?workstations:NULL);
140 pdb_set_munged_dial (sampass, munged_dial_len?munged_dial:NULL);
141 pdb_set_lanman_passwd(sampass, lmpwlen?lm_pw_ptr:NULL);
142 pdb_set_nt_passwd (sampass, ntpwlen?nt_pw_ptr:NULL);
144 /*pdb_set_uid(sampass, uid);
145 pdb_set_gid(sampass, gid);*/
146 pdb_set_user_rid(sampass, user_rid);
147 pdb_set_group_rid(sampass, group_rid);
148 pdb_set_unknown_3(sampass, unknown_3);
149 pdb_set_hours_len(sampass, hours_len);
150 pdb_set_unknown_5(sampass, unknown_5);
151 pdb_set_unknown_6(sampass, unknown_6);
152 pdb_set_acct_ctrl(sampass, acct_ctrl);
153 pdb_set_logons_divs(sampass, logon_divs);
154 pdb_set_hours(sampass, hours);
160 SAFE_FREE(nt_username);
163 SAFE_FREE(dir_drive);
164 SAFE_FREE(logon_script);
165 SAFE_FREE(profile_path);
166 SAFE_FREE(acct_desc);
167 SAFE_FREE(workstations);
168 SAFE_FREE(munged_dial);
173 /**********************************************************************
174 Intialize a BYTE buffer from a SAM_ACCOUNT struct
175 *********************************************************************/
176 static uint32 init_buffer_from_sam (uint8 **buf, SAM_ACCOUNT *sampass)
180 /* times are stored as 32bit integer
181 take care on system with 64bit wide time_t
187 pass_can_change_time,
188 pass_must_change_time;
201 uint32 username_len, domain_len, nt_username_len,
202 dir_drive_len, unknown_str_len, munged_dial_len,
203 fullname_len, homedir_len, logon_script_len,
204 profile_path_len, acct_desc_len, workstations_len;
208 uint32 lm_pw_len = 16;
209 uint32 nt_pw_len = 16;
211 /* do we have a valid SAM_ACCOUNT pointer? */
218 logon_time = (uint32)pdb_get_logon_time(sampass);
219 logoff_time = (uint32)pdb_get_logoff_time(sampass);
220 kickoff_time = (uint32)pdb_get_kickoff_time(sampass);
221 pass_can_change_time = (uint32)pdb_get_pass_can_change_time(sampass);
222 pass_must_change_time = (uint32)pdb_get_pass_must_change_time(sampass);
223 pass_last_set_time = (uint32)pdb_get_pass_last_set_time(sampass);
226 username = pdb_get_username(sampass);
228 username_len = strlen(username) +1;
231 domain = pdb_get_domain(sampass);
233 domain_len = strlen(domain) +1;
236 nt_username = pdb_get_nt_username(sampass);
238 nt_username_len = strlen(nt_username) +1;
241 dir_drive = pdb_get_dirdrive(sampass);
243 dir_drive_len = strlen(dir_drive) +1;
248 munged_dial = pdb_get_munged_dial(sampass);
250 munged_dial_len = strlen(munged_dial) +1;
254 fullname = pdb_get_fullname(sampass);
256 fullname_len = strlen(fullname) +1;
259 homedir = pdb_get_homedir(sampass);
261 homedir_len = strlen(homedir) +1;
264 logon_script = pdb_get_logon_script(sampass);
266 logon_script_len = strlen(logon_script) +1;
268 logon_script_len = 0;
269 profile_path = pdb_get_profile_path(sampass);
271 profile_path_len = strlen(profile_path) +1;
273 profile_path_len = 0;
274 acct_desc = pdb_get_acct_desc(sampass);
276 acct_desc_len = strlen(acct_desc) +1;
279 workstations = pdb_get_workstations(sampass);
281 workstations_len = strlen(workstations) +1;
283 workstations_len = 0;
285 lm_pw = pdb_get_lanman_passwd(sampass);
289 nt_pw = pdb_get_nt_passwd(sampass);
293 /* one time to get the size needed */
294 len = tdb_pack(NULL, 0, TDB_FORMAT_STRING,
299 pass_can_change_time,
300 pass_must_change_time,
301 username_len, username,
303 nt_username_len, nt_username,
304 fullname_len, fullname,
305 homedir_len, homedir,
306 dir_drive_len, dir_drive,
307 logon_script_len, logon_script,
308 profile_path_len, profile_path,
309 acct_desc_len, acct_desc,
310 workstations_len, workstations,
311 unknown_str_len, unknown_str,
312 munged_dial_len, munged_dial,
313 pdb_get_user_rid(sampass),
314 pdb_get_group_rid(sampass),
317 pdb_get_acct_ctrl(sampass),
318 pdb_get_unknown3(sampass),
319 pdb_get_logon_divs(sampass),
320 pdb_get_hours_len(sampass),
321 MAX_HOURS_LEN, pdb_get_hours(sampass),
322 pdb_get_unknown5(sampass),
323 pdb_get_unknown6(sampass));
326 /* malloc the space needed */
327 if ( (*buf=(uint8*)malloc(len)) == NULL) {
328 DEBUG(0,("init_buffer_from_sam: Unable to malloc() memory for buffer!\n"));
332 /* now for the real call to tdb_pack() */
333 buflen = tdb_pack(*buf, len, TDB_FORMAT_STRING,
338 pass_can_change_time,
339 pass_must_change_time,
340 username_len, username,
342 nt_username_len, nt_username,
343 fullname_len, fullname,
344 homedir_len, homedir,
345 dir_drive_len, dir_drive,
346 logon_script_len, logon_script,
347 profile_path_len, profile_path,
348 acct_desc_len, acct_desc,
349 workstations_len, workstations,
350 unknown_str_len, unknown_str,
351 munged_dial_len, munged_dial,
352 pdb_get_user_rid(sampass),
353 pdb_get_group_rid(sampass),
356 pdb_get_acct_ctrl(sampass),
357 pdb_get_unknown3(sampass),
358 pdb_get_logon_divs(sampass),
359 pdb_get_hours_len(sampass),
360 MAX_HOURS_LEN, pdb_get_hours(sampass),
361 pdb_get_unknown5(sampass),
362 pdb_get_unknown6(sampass));
365 /* check to make sure we got it correct */
375 /***************************************************************
376 Open the TDB passwd database for SAM account enumeration.
377 ****************************************************************/
379 BOOL pdb_setsampwent(BOOL update)
383 get_private_directory(tdbfile);
384 pstrcat (tdbfile, PASSDB_FILE_NAME);
386 /* Open tdb passwd */
387 if (!(global_tdb_ent.passwd_tdb = tdb_open_log(tdbfile, 0, TDB_DEFAULT, update?(O_RDWR|O_CREAT):O_RDONLY, 0600)))
389 DEBUG(0, ("Unable to open/create TDB passwd\n"));
393 global_tdb_ent.key = tdb_firstkey(global_tdb_ent.passwd_tdb);
398 /***************************************************************
399 End enumeration of the TDB passwd list.
400 ****************************************************************/
402 void pdb_endsampwent(void)
404 if (global_tdb_ent.passwd_tdb) {
405 tdb_close(global_tdb_ent.passwd_tdb);
406 global_tdb_ent.passwd_tdb = NULL;
409 DEBUG(7, ("endtdbpwent: closed password file.\n"));
412 /*****************************************************************
413 Get one SAM_ACCOUNT from the TDB (next in line)
414 *****************************************************************/
416 BOOL pdb_getsampwent(SAM_ACCOUNT *user)
422 char *prefix = USERPREFIX;
423 int prefixlen = strlen (prefix);
426 DEBUG(0,("pdb_get_sampwent: SAM_ACCOUNT is NULL.\n"));
430 /* skip all RID entries */
431 while ((global_tdb_ent.key.dsize != 0) && (strncmp (global_tdb_ent.key.dptr, prefix, prefixlen)))
432 /* increment to next in line */
433 global_tdb_ent.key = tdb_nextkey (global_tdb_ent.passwd_tdb, global_tdb_ent.key);
435 /* do we have an valid interation pointer? */
436 if(global_tdb_ent.passwd_tdb == NULL) {
437 DEBUG(0,("pdb_get_sampwent: Bad TDB Context pointer.\n"));
441 data = tdb_fetch (global_tdb_ent.passwd_tdb, global_tdb_ent.key);
443 DEBUG(5,("pdb_getsampwent: database entry not found.\n"));
447 /* unpack the buffer */
448 if (!init_sam_from_buffer (user, data.dptr, data.dsize)) {
449 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
450 SAFE_FREE(data.dptr);
453 SAFE_FREE(data.dptr);
455 /* validate the account and fill in UNIX uid and gid. sys_getpwnam()
456 is used instaed of Get_Pwnam() as we do not need to try case
458 if ((pw=sys_getpwnam(pdb_get_username(user))) == NULL) {
459 DEBUG(0,("pdb_getsampwent: getpwnam(%s) return NULL. User does not exist!\n",
460 pdb_get_username(user)));
466 pdb_set_uid (user, uid);
467 pdb_set_gid (user, gid);
469 standard_sub_advanced(-1, pdb_get_username(user), "", gid, pdb_get_logon_script(user));
470 standard_sub_advanced(-1, pdb_get_username(user), "", gid, pdb_get_profile_path(user));
471 standard_sub_advanced(-1, pdb_get_username(user), "", gid, pdb_get_homedir(user));
473 /* increment to next in line */
474 global_tdb_ent.key = tdb_nextkey (global_tdb_ent.passwd_tdb, global_tdb_ent.key);
479 /******************************************************************
480 Lookup a name in the SAM TDB
481 ******************************************************************/
483 BOOL pdb_getsampwnam (SAM_ACCOUNT *user, char *sname)
485 TDB_CONTEXT *pwd_tdb;
496 DEBUG(0,("pdb_getsampwnam: SAM_ACCOUNT is NULL.\n"));
500 /* Data is stored in all lower-case */
501 unix_strlower(sname, -1, name, sizeof(name));
503 get_private_directory(tdbfile);
504 pstrcat (tdbfile, PASSDB_FILE_NAME);
507 slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
509 key.dsize = strlen (keystr) + 1;
511 /* open the accounts TDB */
512 if (!(pwd_tdb = tdb_open_log(tdbfile, 0, TDB_DEFAULT, O_RDONLY, 0600))) {
513 DEBUG(0, ("pdb_getsampwnam: Unable to open TDB passwd!\n"));
518 data = tdb_fetch (pwd_tdb, key);
520 DEBUG(5,("pdb_getsampwnam (TDB): error fetching database.\n"));
521 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
526 /* unpack the buffer */
527 if (!init_sam_from_buffer (user, data.dptr, data.dsize)) {
528 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
529 SAFE_FREE(data.dptr);
532 SAFE_FREE(data.dptr);
534 /* validate the account and fill in UNIX uid and gid. sys_getpwnam()
535 is used instaed of Get_Pwnam() as we do not need to try case
537 if ((pw=sys_getpwnam(pdb_get_username(user))) == NULL) {
538 DEBUG(0,("pdb_getsampwent: getpwnam(%s) return NULL. User does not exist!\n",
539 pdb_get_username(user)));
545 pdb_set_uid (user, uid);
546 pdb_set_gid (user, gid);
548 /* 21 days from present */
549 pdb_set_pass_must_change_time(user, time(NULL)+1814400);
551 standard_sub_advanced(-1, pdb_get_username(user), "", gid, pdb_get_logon_script(user));
552 standard_sub_advanced(-1, pdb_get_username(user), "", gid, pdb_get_profile_path(user));
553 standard_sub_advanced(-1, pdb_get_username(user), "", gid, pdb_get_homedir(user));
561 /***************************************************************************
563 **************************************************************************/
565 BOOL pdb_getsampwuid (SAM_ACCOUNT* user, uid_t uid)
571 DEBUG(0,("pdb_getsampwuid: SAM_ACCOUNT is NULL.\n"));
575 pw = sys_getpwuid(uid);
577 DEBUG(0,("pdb_getsampwuid: getpwuid(%d) return NULL. User does not exist!\n", uid));
580 fstrcpy (name, pw->pw_name);
582 return pdb_getsampwnam (user, name);
586 /***************************************************************************
588 **************************************************************************/
590 BOOL pdb_getsampwrid (SAM_ACCOUNT *user, uint32 rid)
592 TDB_CONTEXT *pwd_tdb;
599 DEBUG(0,("pdb_getsampwrid: SAM_ACCOUNT is NULL.\n"));
603 get_private_directory(tdbfile);
604 pstrcat (tdbfile, PASSDB_FILE_NAME);
607 slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
609 key.dsize = strlen (keystr) + 1;
611 /* open the accounts TDB */
612 if (!(pwd_tdb = tdb_open_log(tdbfile, 0, TDB_DEFAULT, O_RDONLY, 0600))) {
613 DEBUG(0, ("pdb_getsampwrid: Unable to open TDB rid database!\n"));
618 data = tdb_fetch (pwd_tdb, key);
620 DEBUG(5,("pdb_getsampwrid (TDB): error fetching database.\n"));
621 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
626 fstrcpy (name, data.dptr);
627 SAFE_FREE(data.dptr);
631 return pdb_getsampwnam (user, name);
634 /***************************************************************************
636 ****************************************************************************/
638 BOOL pdb_delete_sam_account(char *sname)
640 SAM_ACCOUNT *sam_pass = NULL;
641 TDB_CONTEXT *pwd_tdb;
648 unix_strlower(sname, -1, name, sizeof(name));
650 get_private_directory(tdbfile);
651 pstrcat (tdbfile, PASSDB_FILE_NAME);
654 if (!(pwd_tdb = tdb_open_log(tdbfile, 0, TDB_DEFAULT, O_RDWR, 0600))) {
655 DEBUG(0, ("Unable to open TDB passwd!"));
659 /* set the search key */
660 slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
662 key.dsize = strlen (keystr) + 1;
665 data = tdb_fetch (pwd_tdb, key);
667 DEBUG(5,("pdb_delete_sam_account (TDB): error fetching database.\n"));
668 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
673 /* unpack the buffer */
674 if (!pdb_init_sam (&sam_pass)) {
679 if (!init_sam_from_buffer (sam_pass, data.dptr, data.dsize)) {
680 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
682 SAFE_FREE(data.dptr);
685 SAFE_FREE(data.dptr);
687 rid = pdb_get_user_rid(sam_pass);
689 pdb_free_sam (&sam_pass);
691 /* it's outaa here! 8^) */
692 if (tdb_delete(pwd_tdb, key) != TDB_SUCCESS) {
693 DEBUG(5, ("Error deleting entry from tdb passwd database!\n"));
694 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
699 /* delete also the RID key */
701 /* set the search key */
702 slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
704 key.dsize = strlen (keystr) + 1;
706 /* it's outaa here! 8^) */
707 if (tdb_delete(pwd_tdb, key) != TDB_SUCCESS) {
708 DEBUG(5, ("Error deleting entry from tdb rid database!\n"));
709 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
719 /***************************************************************************
721 ****************************************************************************/
723 static BOOL tdb_update_sam(SAM_ACCOUNT* newpwd, BOOL override, int flag)
725 TDB_CONTEXT *pwd_tdb = NULL;
733 get_private_directory(tdbfile);
734 pstrcat (tdbfile, PASSDB_FILE_NAME);
736 if ( (!newpwd->uid) || (!newpwd->gid) )
737 DEBUG (0,("tdb_update_sam: Storing a SAM_ACCOUNT for [%s] with uid %d and gid %d!\n",
738 newpwd->username, newpwd->uid, newpwd->gid));
740 /* if we don't have a RID, then generate one */
741 if (!newpwd->user_rid)
742 pdb_set_user_rid (newpwd, pdb_uid_to_user_rid (newpwd->uid));
743 if (!newpwd->group_rid)
744 pdb_set_group_rid (newpwd, pdb_gid_to_group_rid (newpwd->gid));
746 /* copy the SAM_ACCOUNT struct into a BYTE buffer for storage */
747 if ((data.dsize=init_buffer_from_sam (&buf, newpwd)) == -1) {
748 DEBUG(0,("tdb_update_sam: ERROR - Unable to copy SAM_ACCOUNT info BYTE buffer!\n"));
754 unix_strlower(pdb_get_username(newpwd), -1, name, sizeof(name));
756 /* setup the USER index key */
757 slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
759 key.dsize = strlen (keystr) + 1;
761 /* invalidate the existing TDB iterator if it is open */
762 if (global_tdb_ent.passwd_tdb) {
763 tdb_close(global_tdb_ent.passwd_tdb);
764 global_tdb_ent.passwd_tdb = NULL;
767 /* open the account TDB passwd*/
768 pwd_tdb = tdb_open_log(tdbfile, 0, TDB_DEFAULT, O_RDWR | O_CREAT, 0600);
771 DEBUG(0, ("tdb_update_sam: Unable to open TDB passwd!\n"));
775 /* add the account */
776 if (tdb_store(pwd_tdb, key, data, flag) != TDB_SUCCESS) {
777 DEBUG(0, ("Unable to modify passwd TDB!"));
778 DEBUGADD(0, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
785 data.dsize = sizeof(fstring);
788 /* setup the RID index key */
789 slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, pdb_get_user_rid(newpwd));
791 key.dsize = strlen (keystr) + 1;
793 /* add the reference */
794 if (tdb_store(pwd_tdb, key, data, flag) != TDB_SUCCESS) {
795 DEBUG(0, ("Unable to modify TDB passwd !"));
796 DEBUGADD(0, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
809 /***************************************************************************
810 Modifies an existing SAM_ACCOUNT
811 ****************************************************************************/
813 BOOL pdb_update_sam_account (SAM_ACCOUNT *newpwd, BOOL override)
815 return (tdb_update_sam(newpwd, override, TDB_MODIFY));
818 /***************************************************************************
819 Adds an existing SAM_ACCOUNT
820 ****************************************************************************/
822 BOOL pdb_add_sam_account (SAM_ACCOUNT *newpwd)
824 return (tdb_update_sam(newpwd, True, TDB_INSERT));
828 /* Do *NOT* make this function static. It breaks the compile on gcc. JRA */
829 void samtdb_dummy_function(void) { } /* stop some compilers complaining */
830 #endif /* WITH_TDB_SAM */