sync with SAMBA_2_2
[Samba/gbeck.git] / source / passdb / pdb_tdb.c
blobdc80c6250bc51f254e3d4742b344578669641a0d
1 /*
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 *
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)
10 * any later version.
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
15 * more details.
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.
22 #include "includes.h"
24 #ifdef WITH_TDBPWD
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;
36 struct tdb_enum_info
38 TDB_CONTEXT *passwd_tdb;
39 TDB_DATA key;
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,
49 uint32 buflen)
51 time_t logon_time,
52 logoff_time,
53 kickoff_time,
54 pass_last_set_time,
55 pass_can_change_time,
56 pass_must_change_time;
57 uint32 time_t_len;
58 char *username;
59 char *domain;
60 char *nt_username;
61 char *dir_drive;
62 char *unknown_str;
63 char *munged_dial;
64 char *fullname;
65 char *homedir;
66 char *logon_script;
67 char *profile_path;
68 char *acct_desc;
69 char *workstations;
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;
77 uint8 *hours;
78 static uint8 *lm_pw_ptr,
79 *nt_pw_ptr;
80 uint32 len = 0;
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,
93 &domain_len, &domain,
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,
104 &user_rid,
105 &group_rid,
106 &lmpwlen, &lm_pw_ptr,
107 &ntpwlen, &nt_pw_ptr,
108 &acct_ctrl,
109 &unknown_3,
110 &logon_divs,
111 &hours_len,
112 &hourslen, &hours,
113 &unknown_5,
114 &unknown_6);
116 if (len == -1)
117 return False;
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 !!!!! */
154 return True;
157 /**********************************************************************
158 Intialize a BYTE buffer from a SAM_ACCOUNT struct
159 *********************************************************************/
160 static uint32 init_buffer_from_sam (uint8 **buf, SAM_ACCOUNT *sampass)
162 size_t len, buflen;
164 time_t logon_time,
165 logoff_time,
166 kickoff_time,
167 pass_last_set_time,
168 pass_can_change_time,
169 pass_must_change_time;
170 char *username;
171 char *domain;
172 char *nt_username;
173 char *dir_drive;
174 char *unknown_str;
175 char *munged_dial;
176 char *fullname;
177 char *homedir;
178 char *logon_script;
179 char *profile_path;
180 char *acct_desc;
181 char *workstations;
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;
187 uint8 *lm_pw;
188 uint8 *nt_pw;
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? */
194 if (sampass == NULL)
195 return -1;
197 *buf = NULL;
198 buflen = 0;
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;
213 else domain_len = 0;
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;
220 unknown_str = NULL;
221 unknown_str_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,
260 domain_len, domain,
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),
273 lm_pw_len, lm_pw,
274 nt_pw_len, nt_pw,
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"));
288 return (-1);
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,
301 domain_len, domain,
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),
314 lm_pw_len, lm_pw,
315 nt_pw_len, nt_pw,
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 */
326 if (buflen != len)
328 /* error */
329 free (*buf);
330 return (-1);
333 return (buflen);
336 /***************************************************************
337 Open the TDB passwd database for SAM account enumeration.
338 ****************************************************************/
339 BOOL pdb_setsampwent(BOOL update)
341 pstring tdbfile;
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) !!!"));
353 return False;
357 global_tdb_ent.key = tdb_firstkey(global_tdb_ent.passwd_tdb);
359 return True;
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)
382 TDB_DATA data;
383 struct passwd *pw;
384 uid_t uid;
385 gid_t gid;
386 char *prefix = USERPREFIX;
387 int prefixlen = strlen (prefix);
389 if (user==NULL) {
390 DEBUG(0,("pdb_get_sampwent: SAM_ACCOUNT is NULL.\n"));
391 return False;
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"));
403 return False;
406 data = tdb_fetch (global_tdb_ent.passwd_tdb, global_tdb_ent.key);
407 if (!data.dptr)
409 DEBUG(5,("pdb_getsampwent: database entry not found.\n"));
410 return False;
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"));
417 return False;
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
422 permutations */
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)));
427 return False;
430 uid = pw->pw_uid;
431 gid = pw->pw_gid;
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);
445 return True;
448 /******************************************************************
449 Lookup a name in the SAM TDB
450 ******************************************************************/
451 BOOL pdb_getsampwnam (SAM_ACCOUNT *user, char *sname)
453 TDB_CONTEXT *pwd_tdb;
454 TDB_DATA data, key;
455 fstring keystr;
456 struct passwd *pw;
457 pstring tdbfile;
458 fstring name;
459 uid_t uid;
460 gid_t gid;
463 if (user==NULL) {
464 DEBUG(0,("pdb_getsampwnam: SAM_ACCOUNT is NULL.\n"));
465 return False;
468 fstrcpy (name, sname);
469 strlower (name);
470 pstrcpy (tdbfile, lp_private_dir());
471 pstrcat (tdbfile, PASSDB_FILE_NAME);
473 /* set search key */
474 slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
475 key.dptr = keystr;
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"));
482 return False;
485 /* get the record */
486 data = tdb_fetch (pwd_tdb, key);
487 if (!data.dptr)
489 DEBUG(5,("pdb_getsampwnam (TDB): error fetching database.\n"));
490 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
491 tdb_close (pwd_tdb);
492 return False;
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"));
499 return False;
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
504 permutations */
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)));
509 return False;
512 uid = pw->pw_uid;
513 gid = pw->pw_gid;
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));
524 /* cleanup */
525 tdb_close (pwd_tdb);
527 return True;
530 /***************************************************************************
531 Search by uid
532 **************************************************************************/
533 BOOL pdb_getsampwuid (SAM_ACCOUNT* user, uid_t uid)
535 struct passwd *pw;
536 fstring name;
538 if (user==NULL) {
539 DEBUG(0,("pdb_getsampwuid: SAM_ACCOUNT is NULL.\n"));
540 return False;
543 pw = sys_getpwuid(uid);
544 if (pw == NULL)
546 DEBUG(0,("pdb_getsampwuid: getpwuid(%d) return NULL. User does not exist!\n", uid));
547 return False;
549 fstrcpy (name, pw->pw_name);
551 return pdb_getsampwnam (user, name);
555 /***************************************************************************
556 Search by rid
557 **************************************************************************/
558 BOOL pdb_getsampwrid (SAM_ACCOUNT *user, uint32 rid)
560 TDB_CONTEXT *pwd_tdb;
561 TDB_DATA data, key;
562 fstring keystr;
563 pstring tdbfile;
564 fstring name;
566 if (user==NULL) {
567 DEBUG(0,("pdb_getsampwrid: SAM_ACCOUNT is NULL.\n"));
568 return False;
571 pstrcpy (tdbfile, lp_private_dir());
572 pstrcat (tdbfile, PASSDB_FILE_NAME);
574 /* set search key */
575 slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
576 key.dptr = keystr;
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"));
583 return False;
586 /* get the record */
587 data = tdb_fetch (pwd_tdb, key);
588 if (!data.dptr)
590 DEBUG(5,("pdb_getsampwrid (TDB): error fetching database.\n"));
591 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
592 tdb_close (pwd_tdb);
593 return False;
596 fstrcpy (name, data.dptr);
598 tdb_close (pwd_tdb);
600 return pdb_getsampwnam (user, name);
604 /***************************************************************************
605 Delete a SAM_ACCOUNT
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;
612 TDB_DATA key, data;
613 fstring keystr;
614 pstring tdbfile;
615 uint32 rid;
616 fstring name;
618 fstrcpy (name, sname);
619 strlower (name);
621 pstrcpy (tdbfile, lp_private_dir());
622 pstrcat (tdbfile, PASSDB_FILE_NAME);
624 /* open the TDB */
625 if (!(pwd_tdb = tdb_open(tdbfile, 0, 0, O_RDWR, 0600)))
627 DEBUG(0, ("Unable to open TDB passwd!"));
628 return False;
631 /* set the search key */
632 slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
633 key.dptr = keystr;
634 key.dsize = strlen (keystr) + 1;
636 /* get the record */
637 data = tdb_fetch (pwd_tdb, key);
638 if (!data.dptr)
640 DEBUG(5,("pdb_delete_sam_account (TDB): error fetching database.\n"));
641 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
642 tdb_close (pwd_tdb);
643 return False;
646 /* unpack the buffer */
647 if (!pdb_init_sam (&sam_pass))
649 tdb_close (pwd_tdb);
650 return False;
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"));
656 tdb_close (pwd_tdb);
657 return False;
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)));
671 tdb_close(pwd_tdb);
672 return False;
675 /* delete also the RID key */
677 /* set the search key */
678 slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
679 key.dptr = keystr;
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)));
687 tdb_close(pwd_tdb);
688 return False;
691 tdb_close(pwd_tdb);
693 return True;
696 /***************************************************************************
697 Update the TDB SAM
698 ****************************************************************************/
699 static BOOL tdb_update_sam(SAM_ACCOUNT* newpwd, BOOL override, int flag)
701 TDB_CONTEXT *pwd_tdb;
702 TDB_DATA key, data;
703 uint8 *buf = NULL;
704 fstring keystr;
705 pstring tdbfile;
706 fstring name;
707 int newtdb = FALSE;
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"));
727 return False;
729 data.dptr = buf;
731 fstrcpy (name, pdb_get_username(newpwd));
732 strlower (name);
734 /* setup the USER index key */
735 slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
736 key.dptr = keystr;
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"));
756 return False;
758 newtdb = TRUE;
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)));
767 tdb_close (pwd_tdb);
768 return False;
771 /* setup RID data */
772 data.dsize = sizeof(fstring);
773 data.dptr = name;
775 /* setup the RID index key */
776 slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, pdb_get_user_rid(newpwd));
777 key.dptr = keystr;
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)));
785 tdb_close (pwd_tdb);
786 return False;
789 /* cleanup */
790 tdb_close (pwd_tdb);
792 return (True);
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));
812 #else
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 */