changed the data format in the tdb, as the time fields were not
[Samba.git] / source / passdb / pdb_tdb.c
blob909a5d809f1060ef9d5742a46c449e6ac649ebe0
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 PDB_VERSION "20010830"
27 #define PASSDB_FILE_NAME "/passdb.tdb"
28 #define TDB_FORMAT_STRING "ddddddBBBBBBBBBBBBddBBwdwdBdd"
29 #define USERPREFIX "USER_"
30 #define RIDPREFIX "RID_"
32 extern int DEBUGLEVEL;
33 extern pstring samlogon_user;
34 extern BOOL sam_logon_in_ssb;
37 struct tdb_enum_info
39 TDB_CONTEXT *passwd_tdb;
40 TDB_DATA key;
43 static struct tdb_enum_info global_tdb_ent;
44 /*static SAM_ACCOUNT global_sam_pass;*/
46 /**********************************************************************
47 Intialize a SAM_ACCOUNT struct from a BYTE buffer of size len
48 *********************************************************************/
49 static BOOL init_sam_from_buffer (SAM_ACCOUNT *sampass, uint8 *buf,
50 uint32 buflen)
53 /* times are stored as 32bit integer
54 take care on system with 64bit wide time_t
55 --SSS */
56 uint32 logon_time,
57 logoff_time,
58 kickoff_time,
59 pass_last_set_time,
60 pass_can_change_time,
61 pass_must_change_time;
62 char *username;
63 char *domain;
64 char *nt_username;
65 char *dir_drive;
66 char *unknown_str;
67 char *munged_dial;
68 char *fullname;
69 char *homedir;
70 char *logon_script;
71 char *profile_path;
72 char *acct_desc;
73 char *workstations;
74 uint32 username_len, domain_len, nt_username_len,
75 dir_drive_len, unknown_str_len, munged_dial_len,
76 fullname_len, homedir_len, logon_script_len,
77 profile_path_len, acct_desc_len, workstations_len;
79 uint32 /* uid, gid,*/ user_rid, group_rid, unknown_3, hours_len, unknown_5, unknown_6;
80 uint16 acct_ctrl, logon_divs;
81 uint8 *hours;
82 static uint8 *lm_pw_ptr,
83 *nt_pw_ptr;
84 uint32 len = 0;
85 uint32 lmpwlen, ntpwlen, hourslen;
88 /* unpack the buffer into variables */
89 len = tdb_unpack (buf, buflen, TDB_FORMAT_STRING,
90 &logon_time,
91 &logoff_time,
92 &kickoff_time,
93 &pass_last_set_time,
94 &pass_can_change_time,
95 &pass_must_change_time,
96 &username_len, &username,
97 &domain_len, &domain,
98 &nt_username_len, &nt_username,
99 &fullname_len, &fullname,
100 &homedir_len, &homedir,
101 &dir_drive_len, &dir_drive,
102 &logon_script_len, &logon_script,
103 &profile_path_len, &profile_path,
104 &acct_desc_len, &acct_desc,
105 &workstations_len, &workstations,
106 &unknown_str_len, &unknown_str,
107 &munged_dial_len, &munged_dial,
108 &user_rid,
109 &group_rid,
110 &lmpwlen, &lm_pw_ptr,
111 &ntpwlen, &nt_pw_ptr,
112 &acct_ctrl,
113 &unknown_3,
114 &logon_divs,
115 &hours_len,
116 &hourslen, &hours,
117 &unknown_5,
118 &unknown_6);
120 if (len == -1)
121 return False;
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);
156 /* TODO: free TDB alloced memory !!!!! */
158 return True;
161 /**********************************************************************
162 Intialize a BYTE buffer from a SAM_ACCOUNT struct
163 *********************************************************************/
164 static uint32 init_buffer_from_sam (uint8 **buf, SAM_ACCOUNT *sampass)
166 size_t len, buflen;
168 /* times are stored as 32bit integer
169 take care on system with 64bit wide time_t
170 --SSS */
171 uint32 logon_time,
172 logoff_time,
173 kickoff_time,
174 pass_last_set_time,
175 pass_can_change_time,
176 pass_must_change_time;
177 char *username;
178 char *domain;
179 char *nt_username;
180 char *dir_drive;
181 char *unknown_str;
182 char *munged_dial;
183 char *fullname;
184 char *homedir;
185 char *logon_script;
186 char *profile_path;
187 char *acct_desc;
188 char *workstations;
189 uint32 username_len, domain_len, nt_username_len,
190 dir_drive_len, unknown_str_len, munged_dial_len,
191 fullname_len, homedir_len, logon_script_len,
192 profile_path_len, acct_desc_len, workstations_len;
194 uint8 *lm_pw;
195 uint8 *nt_pw;
196 uint32 lm_pw_len = 16;
197 uint32 nt_pw_len = 16;
199 /* do we have a valid SAM_ACCOUNT pointer? */
200 if (sampass == NULL)
201 return -1;
203 *buf = NULL;
204 buflen = 0;
206 logon_time = pdb_get_logon_time(sampass);
207 logoff_time = pdb_get_logoff_time(sampass);
208 kickoff_time = pdb_get_kickoff_time(sampass);
209 pass_can_change_time = pdb_get_pass_can_change_time(sampass);
210 pass_must_change_time = pdb_get_pass_must_change_time(sampass);
211 pass_last_set_time = pdb_get_pass_last_set_time(sampass);
214 username = pdb_get_username(sampass);
215 if (username) username_len = strlen(username) +1;
216 else username_len = 0;
217 domain = pdb_get_domain(sampass);
218 if (domain) domain_len = strlen(domain) +1;
219 else domain_len = 0;
220 nt_username = pdb_get_nt_username(sampass);
221 if (nt_username) nt_username_len = strlen(nt_username) +1;
222 else nt_username_len = 0;
223 dir_drive = pdb_get_dirdrive(sampass);
224 if (dir_drive) dir_drive_len = strlen(dir_drive) +1;
225 else dir_drive_len = 0;
226 unknown_str = NULL;
227 unknown_str_len = 0;
228 munged_dial = pdb_get_munged_dial(sampass);
229 if (munged_dial) munged_dial_len = strlen(munged_dial) +1;
230 else munged_dial_len = 0;
232 fullname = pdb_get_fullname(sampass);
233 if (fullname) fullname_len = strlen(fullname) +1;
234 else fullname_len = 0;
235 homedir = pdb_get_homedir(sampass);
236 if (homedir) homedir_len = strlen(homedir) +1;
237 else homedir_len = 0;
238 logon_script = pdb_get_logon_script(sampass);
239 if (logon_script) logon_script_len = strlen(logon_script) +1;
240 else logon_script_len = 0;
241 profile_path = pdb_get_profile_path(sampass);
242 if (profile_path) profile_path_len = strlen(profile_path) +1;
243 else profile_path_len = 0;
244 acct_desc = pdb_get_acct_desc(sampass);
245 if (acct_desc) acct_desc_len = strlen(acct_desc) +1;
246 else acct_desc_len = 0;
247 workstations = pdb_get_workstations(sampass);
248 if (workstations) workstations_len = strlen(workstations) +1;
249 else workstations_len = 0;
251 lm_pw = pdb_get_lanman_passwd(sampass);
252 if (!lm_pw) lm_pw_len = 0;
254 nt_pw = pdb_get_nt_passwd(sampass);
255 if (!nt_pw) nt_pw_len = 0;
257 /* one time to get the size needed */
258 len = tdb_pack(NULL, 0, TDB_FORMAT_STRING,
259 logon_time,
260 logoff_time,
261 kickoff_time,
262 pass_last_set_time,
263 pass_can_change_time,
264 pass_must_change_time,
265 username_len, username,
266 domain_len, domain,
267 nt_username_len, nt_username,
268 fullname_len, fullname,
269 homedir_len, homedir,
270 dir_drive_len, dir_drive,
271 logon_script_len, logon_script,
272 profile_path_len, profile_path,
273 acct_desc_len, acct_desc,
274 workstations_len, workstations,
275 unknown_str_len, unknown_str,
276 munged_dial_len, munged_dial,
277 pdb_get_user_rid(sampass),
278 pdb_get_group_rid(sampass),
279 lm_pw_len, lm_pw,
280 nt_pw_len, nt_pw,
281 pdb_get_acct_ctrl(sampass),
282 pdb_get_unknown3(sampass),
283 pdb_get_logon_divs(sampass),
284 pdb_get_hours_len(sampass),
285 MAX_HOURS_LEN, pdb_get_hours(sampass),
286 pdb_get_unknown5(sampass),
287 pdb_get_unknown6(sampass));
290 /* malloc the space needed */
291 if ( (*buf=(uint8*)malloc(len)) == NULL)
293 DEBUG(0,("init_buffer_from_sam: Unable to malloc() memory for buffer!\n"));
294 return (-1);
297 /* now for the real call to tdb_pack() */
298 buflen = tdb_pack(*buf, len, TDB_FORMAT_STRING,
299 logon_time,
300 logoff_time,
301 kickoff_time,
302 pass_last_set_time,
303 pass_can_change_time,
304 pass_must_change_time,
305 username_len, username,
306 domain_len, domain,
307 nt_username_len, nt_username,
308 fullname_len, fullname,
309 homedir_len, homedir,
310 dir_drive_len, dir_drive,
311 logon_script_len, logon_script,
312 profile_path_len, profile_path,
313 acct_desc_len, acct_desc,
314 workstations_len, workstations,
315 unknown_str_len, unknown_str,
316 munged_dial_len, munged_dial,
317 pdb_get_user_rid(sampass),
318 pdb_get_group_rid(sampass),
319 lm_pw_len, lm_pw,
320 nt_pw_len, nt_pw,
321 pdb_get_acct_ctrl(sampass),
322 pdb_get_unknown3(sampass),
323 pdb_get_logon_divs(sampass),
324 pdb_get_hours_len(sampass),
325 MAX_HOURS_LEN, pdb_get_hours(sampass),
326 pdb_get_unknown5(sampass),
327 pdb_get_unknown6(sampass));
330 /* check to make sure we got it correct */
331 if (buflen != len)
333 /* error */
334 free (*buf);
335 return (-1);
338 return (buflen);
341 /***************************************************************
342 Open the TDB passwd database for SAM account enumeration.
343 ****************************************************************/
344 BOOL pdb_setsampwent(BOOL update)
346 pstring tdbfile;
348 pstrcpy (tdbfile, lp_private_dir());
349 pstrcat (tdbfile, PASSDB_FILE_NAME);
351 /* Open tdb passwd */
352 if (!(global_tdb_ent.passwd_tdb = tdb_open_log(tdbfile, 0, USE_TDB_MMAP_FLAG, update ? O_RDWR : O_RDONLY, 0600)))
354 DEBUG(0, ("Unable to open TDB passwd, trying create new!\n"));
355 if (!(global_tdb_ent.passwd_tdb = tdb_open_log(tdbfile, 0, USE_TDB_MMAP_FLAG, O_RDWR | O_CREAT | O_EXCL, 0600)))
357 DEBUG(0, ("Unable to create TDB passwd (passdb.tdb) !!!"));
358 return False;
362 global_tdb_ent.key = tdb_firstkey(global_tdb_ent.passwd_tdb);
364 return True;
367 /***************************************************************
368 End enumeration of the TDB passwd list.
369 ****************************************************************/
370 void pdb_endsampwent(void)
372 if (global_tdb_ent.passwd_tdb)
374 tdb_close(global_tdb_ent.passwd_tdb);
375 global_tdb_ent.passwd_tdb = NULL;
378 DEBUG(7, ("endtdbpwent: closed password file.\n"));
382 /*****************************************************************
383 Get one SAM_ACCOUNT from the TDB (next in line)
384 *****************************************************************/
385 BOOL pdb_getsampwent(SAM_ACCOUNT *user)
387 TDB_DATA data;
388 struct passwd *pw;
389 uid_t uid;
390 gid_t gid;
391 char *prefix = USERPREFIX;
392 int prefixlen = strlen (prefix);
394 if (user==NULL) {
395 DEBUG(0,("pdb_get_sampwent: SAM_ACCOUNT is NULL.\n"));
396 return False;
399 /* skip all RID entries */
400 while ((global_tdb_ent.key.dsize != 0) && (strncmp (global_tdb_ent.key.dptr, prefix, prefixlen)))
401 /* increment to next in line */
402 global_tdb_ent.key = tdb_nextkey (global_tdb_ent.passwd_tdb, global_tdb_ent.key);
404 /* do we have an valid interation pointer? */
405 if(global_tdb_ent.passwd_tdb == NULL)
407 DEBUG(0,("pdb_get_sampwent: Bad TDB Context pointer.\n"));
408 return False;
411 data = tdb_fetch (global_tdb_ent.passwd_tdb, global_tdb_ent.key);
412 if (!data.dptr)
414 DEBUG(5,("pdb_getsampwent: database entry not found.\n"));
415 return False;
418 /* unpack the buffer */
419 if (!init_sam_from_buffer (user, data.dptr, data.dsize))
421 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
422 return False;
425 /* validate the account and fill in UNIX uid and gid. sys_getpwnam()
426 is used instaed of Get_Pwnam() as we do not need to try case
427 permutations */
428 if ((pw=sys_getpwnam(pdb_get_username(user))) == NULL)
430 DEBUG(0,("pdb_getsampwent: getpwnam(%s) return NULL. User does not exist!\n",
431 pdb_get_username(user)));
432 return False;
435 uid = pw->pw_uid;
436 gid = pw->pw_gid;
437 pdb_set_uid (user, uid);
438 pdb_set_gid (user, gid);
440 /* 21 days from present */
441 pdb_set_pass_must_change_time(user, time(NULL)+1814400);
443 standard_sub_advanced(-1, pdb_get_username(user), "", gid, pdb_get_logon_script(user));
444 standard_sub_advanced(-1, pdb_get_username(user), "", gid, pdb_get_profile_path(user));
445 standard_sub_advanced(-1, pdb_get_username(user), "", gid, pdb_get_homedir(user));
447 /* increment to next in line */
448 global_tdb_ent.key = tdb_nextkey (global_tdb_ent.passwd_tdb, global_tdb_ent.key);
450 return True;
453 /******************************************************************
454 Lookup a name in the SAM TDB
455 ******************************************************************/
456 BOOL pdb_getsampwnam (SAM_ACCOUNT *user, char *sname)
458 TDB_CONTEXT *pwd_tdb;
459 TDB_DATA data, key;
460 fstring keystr;
461 struct passwd *pw;
462 pstring tdbfile;
463 fstring name;
464 uid_t uid;
465 gid_t gid;
468 if (user==NULL) {
469 DEBUG(0,("pdb_getsampwnam: SAM_ACCOUNT is NULL.\n"));
470 return False;
473 fstrcpy (name, sname);
474 strlower (name);
475 pstrcpy (tdbfile, lp_private_dir());
476 pstrcat (tdbfile, PASSDB_FILE_NAME);
478 /* set search key */
479 slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
480 key.dptr = keystr;
481 key.dsize = strlen (keystr) + 1;
483 /* open the accounts TDB */
484 if (!(pwd_tdb = tdb_open_log(tdbfile, 0, USE_TDB_MMAP_FLAG, O_RDONLY, 0600)))
486 DEBUG(0, ("pdb_getsampwnam: Unable to open TDB passwd!\n"));
487 return False;
490 /* get the record */
491 data = tdb_fetch (pwd_tdb, key);
492 if (!data.dptr)
494 DEBUG(5,("pdb_getsampwnam (TDB): error fetching database.\n"));
495 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
496 tdb_close (pwd_tdb);
497 return False;
500 /* unpack the buffer */
501 if (!init_sam_from_buffer (user, data.dptr, data.dsize))
503 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
504 return False;
507 /* validate the account and fill in UNIX uid and gid. sys_getpwnam()
508 is used instaed of Get_Pwnam() as we do not need to try case
509 permutations */
510 if ((pw=sys_getpwnam(pdb_get_username(user))) == NULL)
512 DEBUG(0,("pdb_getsampwent: getpwnam(%s) return NULL. User does not exist!\n",
513 pdb_get_username(user)));
514 return False;
517 uid = pw->pw_uid;
518 gid = pw->pw_gid;
519 pdb_set_uid (user, uid);
520 pdb_set_gid (user, gid);
522 /* 21 days from present */
523 pdb_set_pass_must_change_time(user, time(NULL)+1814400);
525 standard_sub_advanced(-1, pdb_get_username(user), "", gid, pdb_get_logon_script(user));
526 standard_sub_advanced(-1, pdb_get_username(user), "", gid, pdb_get_profile_path(user));
527 standard_sub_advanced(-1, pdb_get_username(user), "", gid, pdb_get_homedir(user));
529 /* cleanup */
530 tdb_close (pwd_tdb);
532 return True;
535 /***************************************************************************
536 Search by uid
537 **************************************************************************/
538 BOOL pdb_getsampwuid (SAM_ACCOUNT* user, uid_t uid)
540 struct passwd *pw;
541 fstring name;
543 if (user==NULL) {
544 DEBUG(0,("pdb_getsampwuid: SAM_ACCOUNT is NULL.\n"));
545 return False;
548 pw = sys_getpwuid(uid);
549 if (pw == NULL)
551 DEBUG(0,("pdb_getsampwuid: getpwuid(%d) return NULL. User does not exist!\n", uid));
552 return False;
554 fstrcpy (name, pw->pw_name);
556 return pdb_getsampwnam (user, name);
560 /***************************************************************************
561 Search by rid
562 **************************************************************************/
563 BOOL pdb_getsampwrid (SAM_ACCOUNT *user, uint32 rid)
565 TDB_CONTEXT *pwd_tdb;
566 TDB_DATA data, key;
567 fstring keystr;
568 pstring tdbfile;
569 fstring name;
571 if (user==NULL) {
572 DEBUG(0,("pdb_getsampwrid: SAM_ACCOUNT is NULL.\n"));
573 return False;
576 pstrcpy (tdbfile, lp_private_dir());
577 pstrcat (tdbfile, PASSDB_FILE_NAME);
579 /* set search key */
580 slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
581 key.dptr = keystr;
582 key.dsize = strlen (keystr) + 1;
584 /* open the accounts TDB */
585 if (!(pwd_tdb = tdb_open_log(tdbfile, 0, USE_TDB_MMAP_FLAG, O_RDONLY, 0600)))
587 DEBUG(0, ("pdb_getsampwrid: Unable to open TDB rid database!\n"));
588 return False;
591 /* get the record */
592 data = tdb_fetch (pwd_tdb, key);
593 if (!data.dptr)
595 DEBUG(5,("pdb_getsampwrid (TDB): error fetching database.\n"));
596 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
597 tdb_close (pwd_tdb);
598 return False;
601 fstrcpy (name, data.dptr);
603 tdb_close (pwd_tdb);
605 return pdb_getsampwnam (user, name);
609 /***************************************************************************
610 Delete a SAM_ACCOUNT
611 ****************************************************************************/
612 BOOL pdb_delete_sam_account(char *sname)
614 struct passwd *pwd = NULL;
615 SAM_ACCOUNT *sam_pass = NULL;
616 TDB_CONTEXT *pwd_tdb;
617 TDB_DATA key, data;
618 fstring keystr;
619 pstring tdbfile;
620 uint32 rid;
621 fstring name;
623 fstrcpy (name, sname);
624 strlower (name);
626 pstrcpy (tdbfile, lp_private_dir());
627 pstrcat (tdbfile, PASSDB_FILE_NAME);
629 /* open the TDB */
630 if (!(pwd_tdb = tdb_open_log(tdbfile, 0, USE_TDB_MMAP_FLAG, O_RDWR, 0600)))
632 DEBUG(0, ("Unable to open TDB passwd!"));
633 return False;
636 /* set the search key */
637 slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
638 key.dptr = keystr;
639 key.dsize = strlen (keystr) + 1;
641 /* get the record */
642 data = tdb_fetch (pwd_tdb, key);
643 if (!data.dptr)
645 DEBUG(5,("pdb_delete_sam_account (TDB): error fetching database.\n"));
646 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
647 tdb_close (pwd_tdb);
648 return False;
651 /* unpack the buffer */
652 if (!pdb_init_sam (&sam_pass))
654 tdb_close (pwd_tdb);
655 return False;
658 if (!init_sam_from_buffer (sam_pass, data.dptr, data.dsize))
660 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
661 tdb_close (pwd_tdb);
662 return False;
665 pwd = sys_getpwnam(sam_pass->username);
667 pdb_free_sam (sam_pass);
669 rid = pdb_uid_to_user_rid (pwd->pw_uid);
671 /* it's outaa here! 8^) */
672 if (tdb_delete(pwd_tdb, key) != TDB_SUCCESS)
674 DEBUG(5, ("Error deleting entry from tdb passwd database!\n"));
675 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
676 tdb_close(pwd_tdb);
677 return False;
680 /* delete also the RID key */
682 /* set the search key */
683 slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
684 key.dptr = keystr;
685 key.dsize = strlen (keystr) + 1;
687 /* it's outaa here! 8^) */
688 if (tdb_delete(pwd_tdb, key) != TDB_SUCCESS)
690 DEBUG(5, ("Error deleting entry from tdb rid database!\n"));
691 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
692 tdb_close(pwd_tdb);
693 return False;
696 tdb_close(pwd_tdb);
698 return True;
701 /***************************************************************************
702 Update the TDB SAM
703 ****************************************************************************/
704 static BOOL tdb_update_sam(SAM_ACCOUNT* newpwd, BOOL override, int flag)
706 TDB_CONTEXT *pwd_tdb;
707 TDB_DATA key, data;
708 uint8 *buf = NULL;
709 fstring keystr;
710 pstring tdbfile;
711 fstring name;
712 int newtdb = FALSE;
714 pstrcpy (tdbfile, lp_private_dir());
715 pstrcat (tdbfile, PASSDB_FILE_NAME);
717 if ( (!newpwd->uid) || (!newpwd->gid) )
718 DEBUG (0,("tdb_update_sam: Storing a SAM_ACCOUNT for [%s] with uid %d and gid %d!\n",
719 newpwd->username, newpwd->uid, newpwd->gid));
721 /* if we don't have a RID, then generate one */
722 if (!newpwd->user_rid)
723 pdb_set_user_rid (newpwd, pdb_uid_to_user_rid (newpwd->uid));
724 if (!newpwd->group_rid)
725 pdb_set_group_rid (newpwd, pdb_gid_to_group_rid (newpwd->gid));
728 /* copy the SAM_ACCOUNT struct into a BYTE buffer for storage */
729 if ((data.dsize=init_buffer_from_sam (&buf, newpwd)) == -1)
731 DEBUG(0,("tdb_update_sam: ERROR - Unable to copy SAM_ACCOUNT info BYTE buffer!\n"));
732 return False;
734 data.dptr = buf;
736 fstrcpy (name, pdb_get_username(newpwd));
737 strlower (name);
739 /* setup the USER index key */
740 slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
741 key.dptr = keystr;
742 key.dsize = strlen (keystr) + 1;
744 /* invalidate the existing TDB iterator if it is open */
745 if (global_tdb_ent.passwd_tdb)
747 tdb_close(global_tdb_ent.passwd_tdb);
748 global_tdb_ent.passwd_tdb = NULL;
751 /* open the account TDB passwd*/
752 if (!(pwd_tdb = tdb_open_log(tdbfile, 0, USE_TDB_MMAP_FLAG, O_RDWR, 0600)))
754 DEBUG(0, ("tdb_update_sam: Unable to open TDB passwd!\n"));
755 if (flag == TDB_INSERT)
757 DEBUG(0, ("Unable to open TDB passwd, trying create new!\n"));
758 if (!(pwd_tdb = tdb_open_log(tdbfile, 0, USE_TDB_MMAP_FLAG, O_RDWR | O_CREAT | O_EXCL, 0600)))
760 DEBUG(0, ("Unable to create TDB passwd (passdb.tdb) !!!\n"));
761 return False;
763 newtdb = True;
767 /* add the account */
768 if (tdb_store(pwd_tdb, key, data, flag) != TDB_SUCCESS)
770 DEBUG(0, ("Unable to modify passwd TDB!"));
771 DEBUGADD(0, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
772 tdb_close (pwd_tdb);
773 return False;
776 /* setup RID data */
777 data.dsize = sizeof(fstring);
778 data.dptr = name;
780 /* setup the RID index key */
781 slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, pdb_get_user_rid(newpwd));
782 key.dptr = keystr;
783 key.dsize = strlen (keystr) + 1;
785 /* add the reference */
786 if (tdb_store(pwd_tdb, key, data, flag) != TDB_SUCCESS)
788 DEBUG(0, ("Unable to modify TDB passwd !"));
789 DEBUGADD(0, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
790 tdb_close (pwd_tdb);
791 return False;
794 /* cleanup */
795 tdb_close (pwd_tdb);
797 return (True);
800 /***************************************************************************
801 Modifies an existing SAM_ACCOUNT
802 ****************************************************************************/
803 BOOL pdb_update_sam_account (SAM_ACCOUNT *newpwd, BOOL override)
805 return (tdb_update_sam(newpwd, override, TDB_MODIFY));
808 /***************************************************************************
809 Adds an existing SAM_ACCOUNT
810 ****************************************************************************/
811 BOOL pdb_add_sam_account (SAM_ACCOUNT *newpwd)
813 return (tdb_update_sam(newpwd, True, TDB_INSERT));
817 #else
818 /* Do *NOT* make this function static. It breaks the compile on gcc. JRA */
819 void samtdb_dummy_function(void) { } /* stop some compilers complaining */
820 #endif /* WITH_TDBPWD */