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
7 * This program is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 675
19 * Mass Ave, Cambridge, MA 02139, USA.
26 #define PASSDB_FILE_NAME "/passdb.tdb"
27 #define TDB_FORMAT_STRING "BBBBBBBBBBBBBBBBBBddBBwdwdBdd"
28 #define USERPREFIX "USER_"
29 #define RIDPREFIX "RID_"
31 extern int DEBUGLEVEL;
32 extern pstring samlogon_user;
33 extern BOOL sam_logon_in_ssb;
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 *********************************************************************/
48 static BOOL init_sam_from_buffer (SAM_ACCOUNT *sampass, uint8 *buf,
56 pass_must_change_time;
70 uint32 username_len, domain_len, nt_username_len,
71 dir_drive_len, unknown_str_len, munged_dial_len,
72 fullname_len, homedir_len, logon_script_len,
73 profile_path_len, acct_desc_len, workstations_len;
75 uint32 /* uid, gid,*/ user_rid, group_rid, unknown_3, hours_len, unknown_5, unknown_6;
76 uint16 acct_ctrl, logon_divs;
78 static uint8 *lm_pw_ptr,
81 uint32 lmpwlen, ntpwlen, hourslen;
84 /* unpack the buffer into variables */
85 len = tdb_unpack (buf, buflen, TDB_FORMAT_STRING,
86 &time_t_len, &logon_time,
87 &time_t_len, &logoff_time,
88 &time_t_len, &kickoff_time,
89 &time_t_len, &pass_last_set_time,
90 &time_t_len, &pass_can_change_time,
91 &time_t_len, &pass_must_change_time,
92 &username_len, &username,
94 &nt_username_len, &nt_username,
95 &fullname_len, &fullname,
96 &homedir_len, &homedir,
97 &dir_drive_len, &dir_drive,
98 &logon_script_len, &logon_script,
99 &profile_path_len, &profile_path,
100 &acct_desc_len, &acct_desc,
101 &workstations_len, &workstations,
102 &unknown_str_len, &unknown_str,
103 &munged_dial_len, &munged_dial,
106 &lmpwlen, &lm_pw_ptr,
107 &ntpwlen, &nt_pw_ptr,
119 pdb_set_logon_time(sampass, logon_time);
120 pdb_set_logoff_time(sampass, logoff_time);
121 pdb_set_kickoff_time(sampass, kickoff_time);
122 pdb_set_pass_can_change_time(sampass, pass_can_change_time);
123 pdb_set_pass_must_change_time(sampass, pass_must_change_time);
124 pdb_set_pass_last_set_time(sampass, pass_last_set_time);
126 pdb_set_username (sampass, username_len?username:NULL);
127 pdb_set_domain (sampass, domain_len?domain:NULL);
128 pdb_set_nt_username (sampass, nt_username_len?nt_username:NULL);
129 pdb_set_fullname (sampass, fullname_len?fullname:NULL);
130 pdb_set_homedir (sampass, homedir_len?homedir:NULL);
131 pdb_set_dir_drive (sampass, dir_drive_len?dir_drive:NULL);
132 pdb_set_logon_script (sampass, logon_script_len?logon_script:NULL);
133 pdb_set_profile_path (sampass, profile_path_len?profile_path:NULL);
134 pdb_set_acct_desc (sampass, acct_desc_len?acct_desc:NULL);
135 pdb_set_workstations (sampass, workstations_len?workstations:NULL);
136 pdb_set_munged_dial (sampass, munged_dial_len?munged_dial:NULL);
137 pdb_set_lanman_passwd(sampass, lmpwlen?lm_pw_ptr:NULL);
138 pdb_set_nt_passwd (sampass, ntpwlen?nt_pw_ptr:NULL);
140 /*pdb_set_uid(sampass, uid);
141 pdb_set_gid(sampass, gid);*/
142 pdb_set_user_rid(sampass, user_rid);
143 pdb_set_group_rid(sampass, group_rid);
144 pdb_set_unknown_3(sampass, unknown_3);
145 pdb_set_hours_len(sampass, hours_len);
146 pdb_set_unknown_5(sampass, unknown_5);
147 pdb_set_unknown_6(sampass, unknown_6);
148 pdb_set_acct_ctrl(sampass, acct_ctrl);
149 pdb_set_logons_divs(sampass, logon_divs);
150 pdb_set_hours(sampass, hours);
152 /* TODO: free TDB alloced memory !!!!! */
157 /**********************************************************************
158 Intialize a BYTE buffer from a SAM_ACCOUNT struct
159 *********************************************************************/
160 static uint32 init_buffer_from_sam (uint8 **buf, SAM_ACCOUNT *sampass)
168 pass_can_change_time,
169 pass_must_change_time;
182 uint32 username_len, domain_len, nt_username_len,
183 dir_drive_len, unknown_str_len, munged_dial_len,
184 fullname_len, homedir_len, logon_script_len,
185 profile_path_len, acct_desc_len, workstations_len;
189 uint32 time_t_len = sizeof (time_t);
190 uint32 lm_pw_len = 16;
191 uint32 nt_pw_len = 16;
193 /* do we have a valid SAM_ACCOUNT pointer? */
200 logon_time = pdb_get_logon_time(sampass);
201 logoff_time = pdb_get_logoff_time(sampass);
202 kickoff_time = pdb_get_kickoff_time(sampass);
203 pass_can_change_time = pdb_get_pass_can_change_time(sampass);
204 pass_must_change_time = pdb_get_pass_must_change_time(sampass);
205 pass_last_set_time = pdb_get_pass_last_set_time(sampass);
208 username = pdb_get_username(sampass);
209 if (username) username_len = strlen(username) +1;
210 else username_len = 0;
211 domain = pdb_get_domain(sampass);
212 if (domain) domain_len = strlen(domain) +1;
214 nt_username = pdb_get_nt_username(sampass);
215 if (nt_username) nt_username_len = strlen(nt_username) +1;
216 else nt_username_len = 0;
217 dir_drive = pdb_get_dirdrive(sampass);
218 if (dir_drive) dir_drive_len = strlen(dir_drive) +1;
219 else dir_drive_len = 0;
222 munged_dial = pdb_get_munged_dial(sampass);
223 if (munged_dial) munged_dial_len = strlen(munged_dial) +1;
224 else munged_dial_len = 0;
226 fullname = pdb_get_fullname(sampass);
227 if (fullname) fullname_len = strlen(fullname) +1;
228 else fullname_len = 0;
229 homedir = pdb_get_homedir(sampass);
230 if (homedir) homedir_len = strlen(homedir) +1;
231 else homedir_len = 0;
232 logon_script = pdb_get_logon_script(sampass);
233 if (logon_script) logon_script_len = strlen(logon_script) +1;
234 else logon_script_len = 0;
235 profile_path = pdb_get_profile_path(sampass);
236 if (profile_path) profile_path_len = strlen(profile_path) +1;
237 else profile_path_len = 0;
238 acct_desc = pdb_get_acct_desc(sampass);
239 if (acct_desc) acct_desc_len = strlen(acct_desc) +1;
240 else acct_desc_len = 0;
241 workstations = pdb_get_workstations(sampass);
242 if (workstations) workstations_len = strlen(workstations) +1;
243 else workstations_len = 0;
245 lm_pw = pdb_get_lanman_passwd(sampass);
246 if (!lm_pw) lm_pw_len = 0;
248 nt_pw = pdb_get_nt_passwd(sampass);
249 if (!nt_pw) nt_pw_len = 0;
251 /* one time to get the size needed */
252 len = tdb_pack(NULL, 0, TDB_FORMAT_STRING,
253 time_t_len, &logon_time,
254 time_t_len, &logoff_time,
255 time_t_len, &kickoff_time,
256 time_t_len, &pass_last_set_time,
257 time_t_len, &pass_can_change_time,
258 time_t_len, &pass_must_change_time,
259 username_len, username,
261 nt_username_len, nt_username,
262 fullname_len, fullname,
263 homedir_len, homedir,
264 dir_drive_len, dir_drive,
265 logon_script_len, logon_script,
266 profile_path_len, profile_path,
267 acct_desc_len, acct_desc,
268 workstations_len, workstations,
269 unknown_str_len, unknown_str,
270 munged_dial_len, munged_dial,
271 pdb_get_user_rid(sampass),
272 pdb_get_group_rid(sampass),
275 pdb_get_acct_ctrl(sampass),
276 pdb_get_unknown3(sampass),
277 pdb_get_logon_divs(sampass),
278 pdb_get_hours_len(sampass),
279 MAX_HOURS_LEN, pdb_get_hours(sampass),
280 pdb_get_unknown5(sampass),
281 pdb_get_unknown6(sampass));
284 /* malloc the space needed */
285 if ( (*buf=(uint8*)malloc(len)) == NULL)
287 DEBUG(0,("init_buffer_from_sam: Unable to malloc() memory for buffer!\n"));
291 /* now for the real call to tdb_pack() */
292 /* one time to get the size needed */
293 buflen = tdb_pack(*buf, len, TDB_FORMAT_STRING,
294 time_t_len, &logon_time,
295 time_t_len, &logoff_time,
296 time_t_len, &kickoff_time,
297 time_t_len, &pass_last_set_time,
298 time_t_len, &pass_can_change_time,
299 time_t_len, &pass_must_change_time,
300 username_len, username,
302 nt_username_len, nt_username,
303 fullname_len, fullname,
304 homedir_len, homedir,
305 dir_drive_len, dir_drive,
306 logon_script_len, logon_script,
307 profile_path_len, profile_path,
308 acct_desc_len, acct_desc,
309 workstations_len, workstations,
310 unknown_str_len, unknown_str,
311 munged_dial_len, munged_dial,
312 pdb_get_user_rid(sampass),
313 pdb_get_group_rid(sampass),
316 pdb_get_acct_ctrl(sampass),
317 pdb_get_unknown3(sampass),
318 pdb_get_logon_divs(sampass),
319 pdb_get_hours_len(sampass),
320 MAX_HOURS_LEN, pdb_get_hours(sampass),
321 pdb_get_unknown5(sampass),
322 pdb_get_unknown6(sampass));
325 /* check to make sure we got it correct */
336 /***************************************************************
337 Open the TDB passwd database for SAM account enumeration.
338 ****************************************************************/
339 BOOL pdb_setsampwent(BOOL update)
343 pstrcpy (tdbfile, lp_private_dir());
344 pstrcat (tdbfile, PASSDB_FILE_NAME);
346 /* Open tdb passwd */
347 if (!(global_tdb_ent.passwd_tdb = tdb_open(tdbfile, 0, 0, update ? O_RDWR : O_RDONLY, 0600)))
349 DEBUG(0, ("Unable to open TDB passwd, trying create new!\n"));
350 if (!(global_tdb_ent.passwd_tdb = tdb_open(tdbfile, 0, 0, O_RDWR | O_CREAT | O_EXCL, 0600)))
352 DEBUG(0, ("Unable to create TDB passwd (passdb.tdb) !!!"));
357 global_tdb_ent.key = tdb_firstkey(global_tdb_ent.passwd_tdb);
362 /***************************************************************
363 End enumeration of the TDB passwd list.
364 ****************************************************************/
365 void pdb_endsampwent(void)
367 if (global_tdb_ent.passwd_tdb)
369 tdb_close(global_tdb_ent.passwd_tdb);
370 global_tdb_ent.passwd_tdb = NULL;
373 DEBUG(7, ("endtdbpwent: closed password file.\n"));
377 /*****************************************************************
378 Get one SAM_ACCOUNT from the TDB (next in line)
379 *****************************************************************/
380 BOOL pdb_getsampwent(SAM_ACCOUNT *user)
386 char *prefix = USERPREFIX;
387 int prefixlen = strlen (prefix);
390 DEBUG(0,("pdb_get_sampwent: SAM_ACCOUNT is NULL.\n"));
394 /* skip all RID entries */
395 while ((global_tdb_ent.key.dsize != 0) && (strncmp (global_tdb_ent.key.dptr, prefix, prefixlen)))
396 /* increment to next in line */
397 global_tdb_ent.key = tdb_nextkey (global_tdb_ent.passwd_tdb, global_tdb_ent.key);
399 /* do we have an valid interation pointer? */
400 if(global_tdb_ent.passwd_tdb == NULL)
402 DEBUG(0,("pdb_get_sampwent: Bad TDB Context pointer.\n"));
406 data = tdb_fetch (global_tdb_ent.passwd_tdb, global_tdb_ent.key);
409 DEBUG(5,("pdb_getsampwent: database entry not found.\n"));
413 /* unpack the buffer */
414 if (!init_sam_from_buffer (user, data.dptr, data.dsize))
416 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
420 /* validate the account and fill in UNIX uid and gid. sys_getpwnam()
421 is used instaed of Get_Pwnam() as we do not need to try case
423 if ((pw=sys_getpwnam(pdb_get_username(user))) == NULL)
425 DEBUG(0,("pdb_getsampwent: getpwnam(%s) return NULL. User does not exist!\n",
426 pdb_get_username(user)));
432 pdb_set_uid (user, uid);
433 pdb_set_gid (user, gid);
435 /* 21 days from present */
436 pdb_set_pass_must_change_time(user, time(NULL)+1814400);
438 standard_sub_advanced(-1, pdb_get_username(user), "", gid, pdb_get_logon_script(user));
439 standard_sub_advanced(-1, pdb_get_username(user), "", gid, pdb_get_profile_path(user));
440 standard_sub_advanced(-1, pdb_get_username(user), "", gid, pdb_get_homedir(user));
442 /* increment to next in line */
443 global_tdb_ent.key = tdb_nextkey (global_tdb_ent.passwd_tdb, global_tdb_ent.key);
448 /******************************************************************
449 Lookup a name in the SAM TDB
450 ******************************************************************/
451 BOOL pdb_getsampwnam (SAM_ACCOUNT *user, char *sname)
453 TDB_CONTEXT *pwd_tdb;
464 DEBUG(0,("pdb_getsampwnam: SAM_ACCOUNT is NULL.\n"));
468 fstrcpy (name, sname);
470 pstrcpy (tdbfile, lp_private_dir());
471 pstrcat (tdbfile, PASSDB_FILE_NAME);
474 slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
476 key.dsize = strlen (keystr) + 1;
478 /* open the accounts TDB */
479 if (!(pwd_tdb = tdb_open(tdbfile, 0, 0, O_RDONLY, 0600)))
481 DEBUG(0, ("pdb_getsampwnam: Unable to open TDB passwd!\n"));
486 data = tdb_fetch (pwd_tdb, key);
489 DEBUG(5,("pdb_getsampwnam (TDB): error fetching database.\n"));
490 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
495 /* unpack the buffer */
496 if (!init_sam_from_buffer (user, data.dptr, data.dsize))
498 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
502 /* validate the account and fill in UNIX uid and gid. sys_getpwnam()
503 is used instaed of Get_Pwnam() as we do not need to try case
505 if ((pw=sys_getpwnam(pdb_get_username(user))) == NULL)
507 DEBUG(0,("pdb_getsampwent: getpwnam(%s) return NULL. User does not exist!\n",
508 pdb_get_username(user)));
514 pdb_set_uid (user, uid);
515 pdb_set_gid (user, gid);
517 /* 21 days from present */
518 pdb_set_pass_must_change_time(user, time(NULL)+1814400);
520 standard_sub_advanced(-1, pdb_get_username(user), "", gid, pdb_get_logon_script(user));
521 standard_sub_advanced(-1, pdb_get_username(user), "", gid, pdb_get_profile_path(user));
522 standard_sub_advanced(-1, pdb_get_username(user), "", gid, pdb_get_homedir(user));
530 /***************************************************************************
532 **************************************************************************/
533 BOOL pdb_getsampwuid (SAM_ACCOUNT* user, uid_t uid)
539 DEBUG(0,("pdb_getsampwuid: SAM_ACCOUNT is NULL.\n"));
543 pw = sys_getpwuid(uid);
546 DEBUG(0,("pdb_getsampwuid: getpwuid(%d) return NULL. User does not exist!\n", uid));
549 fstrcpy (name, pw->pw_name);
551 return pdb_getsampwnam (user, name);
555 /***************************************************************************
557 **************************************************************************/
558 BOOL pdb_getsampwrid (SAM_ACCOUNT *user, uint32 rid)
560 TDB_CONTEXT *pwd_tdb;
567 DEBUG(0,("pdb_getsampwrid: SAM_ACCOUNT is NULL.\n"));
571 pstrcpy (tdbfile, lp_private_dir());
572 pstrcat (tdbfile, PASSDB_FILE_NAME);
575 slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
577 key.dsize = strlen (keystr) + 1;
579 /* open the accounts TDB */
580 if (!(pwd_tdb = tdb_open(tdbfile, 0, 0, O_RDONLY, 0600)))
582 DEBUG(0, ("pdb_getsampwrid: Unable to open TDB rid database!\n"));
587 data = tdb_fetch (pwd_tdb, key);
590 DEBUG(5,("pdb_getsampwrid (TDB): error fetching database.\n"));
591 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
596 fstrcpy (name, data.dptr);
600 return pdb_getsampwnam (user, name);
604 /***************************************************************************
606 ****************************************************************************/
607 BOOL pdb_delete_sam_account(char *sname)
609 struct passwd *pwd = NULL;
610 SAM_ACCOUNT *sam_pass = NULL;
611 TDB_CONTEXT *pwd_tdb;
618 fstrcpy (name, sname);
621 pstrcpy (tdbfile, lp_private_dir());
622 pstrcat (tdbfile, PASSDB_FILE_NAME);
625 if (!(pwd_tdb = tdb_open(tdbfile, 0, 0, O_RDWR, 0600)))
627 DEBUG(0, ("Unable to open TDB passwd!"));
631 /* set the search key */
632 slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
634 key.dsize = strlen (keystr) + 1;
637 data = tdb_fetch (pwd_tdb, key);
640 DEBUG(5,("pdb_delete_sam_account (TDB): error fetching database.\n"));
641 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
646 /* unpack the buffer */
647 if (!pdb_init_sam (&sam_pass))
653 if (!init_sam_from_buffer (sam_pass, data.dptr, data.dsize))
655 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
660 pwd = sys_getpwnam(sam_pass->username);
662 pdb_free_sam (sam_pass);
664 rid = pdb_uid_to_user_rid (pwd->pw_uid);
666 /* it's outaa here! 8^) */
667 if (tdb_delete(pwd_tdb, key) != TDB_SUCCESS)
669 DEBUG(5, ("Error deleting entry from tdb passwd database!\n"));
670 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
675 /* delete also the RID key */
677 /* set the search key */
678 slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
680 key.dsize = strlen (keystr) + 1;
682 /* it's outaa here! 8^) */
683 if (tdb_delete(pwd_tdb, key) != TDB_SUCCESS)
685 DEBUG(5, ("Error deleting entry from tdb rid database!\n"));
686 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
696 /***************************************************************************
698 ****************************************************************************/
699 static BOOL tdb_update_sam(SAM_ACCOUNT* newpwd, BOOL override, int flag)
701 TDB_CONTEXT *pwd_tdb;
709 pstrcpy (tdbfile, lp_private_dir());
710 pstrcat (tdbfile, PASSDB_FILE_NAME);
712 if ( (!newpwd->uid) || (!newpwd->gid) )
713 DEBUG (0,("tdb_update_sam: Storing a SAM_ACCOUNT for [%s] with uid %d and gid %d!\n",
714 newpwd->username, newpwd->uid, newpwd->gid));
716 /* if we don't have a RID, then generate one */
717 if (!newpwd->user_rid)
718 pdb_set_user_rid (newpwd, pdb_uid_to_user_rid (newpwd->uid));
719 if (!newpwd->group_rid)
720 pdb_set_group_rid (newpwd, pdb_gid_to_group_rid (newpwd->gid));
723 /* copy the SAM_ACCOUNT struct into a BYTE buffer for storage */
724 if ((data.dsize=init_buffer_from_sam (&buf, newpwd)) == -1)
726 DEBUG(0,("tdb_update_sam: ERROR - Unable to copy SAM_ACCOUNT info BYTE buffer!\n"));
731 fstrcpy (name, pdb_get_username(newpwd));
734 /* setup the USER index key */
735 slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
737 key.dsize = strlen (keystr) + 1;
739 /* invalidate the existing TDB iterator if it is open */
740 if (global_tdb_ent.passwd_tdb)
742 tdb_close(global_tdb_ent.passwd_tdb);
743 global_tdb_ent.passwd_tdb = NULL;
746 /* open the account TDB passwd*/
747 if (!(pwd_tdb = tdb_open(tdbfile, 0, 0, O_RDWR, 0600)))
749 DEBUG(0, ("tdb_update_sam: Unable to open TDB passwd!\n"));
750 if (flag == TDB_INSERT)
752 DEBUG(0, ("Unable to open TDB passwd, trying create new!\n"));
753 if (!(pwd_tdb = tdb_open(tdbfile, 0, 0, O_RDWR | O_CREAT | O_EXCL, 0600)))
755 DEBUG(0, ("Unable to create TDB passwd (passdb.tdb) !!!\n"));
762 /* add the account */
763 if (tdb_store(pwd_tdb, key, data, flag) != TDB_SUCCESS)
765 DEBUG(0, ("Unable to modify passwd TDB!"));
766 DEBUGADD(0, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
772 data.dsize = sizeof(fstring);
775 /* setup the RID index key */
776 slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, pdb_get_user_rid(newpwd));
778 key.dsize = strlen (keystr) + 1;
780 /* add the reference */
781 if (tdb_store(pwd_tdb, key, data, flag) != TDB_SUCCESS)
783 DEBUG(0, ("Unable to modify TDB passwd !"));
784 DEBUGADD(0, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
795 /***************************************************************************
796 Modifies an existing SAM_ACCOUNT
797 ****************************************************************************/
798 BOOL pdb_update_sam_account (SAM_ACCOUNT *newpwd, BOOL override)
800 return (tdb_update_sam(newpwd, override, TDB_MODIFY));
803 /***************************************************************************
804 Adds an existing SAM_ACCOUNT
805 ****************************************************************************/
806 BOOL pdb_add_sam_account (SAM_ACCOUNT *newpwd)
808 return (tdb_update_sam(newpwd, True, TDB_INSERT));
813 /* Do *NOT* make this function static. It breaks the compile on gcc. JRA */
814 void samtdb_dummy_function(void) { } /* stop some compilers complaining */
815 #endif /* WITH_TDBPWD */