Don't close tdb twice.
[samba.git] / source / passdb / pdb_tdb.c
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  * Copyright (C) Jeremy Allison 2001
7  * 
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)
11  * any later version.
12  * 
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
16  * more details.
17  * 
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.
21  */
22
23 #include "includes.h"
24
25 #ifdef WITH_TDB_SAM
26
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_"
32
33 extern int              DEBUGLEVEL;
34 extern pstring          samlogon_user;
35 extern BOOL             sam_logon_in_ssb;
36
37 struct tdb_enum_info {
38         TDB_CONTEXT     *passwd_tdb;
39         TDB_DATA        key;
40 };
41
42 static struct tdb_enum_info     global_tdb_ent;
43 /*static SAM_ACCOUNT            global_sam_pass;*/
44
45 /**********************************************************************
46  Intialize a SAM_ACCOUNT struct from a BYTE buffer of size len
47  *********************************************************************/
48
49 static BOOL init_sam_from_buffer (SAM_ACCOUNT *sampass, uint8 *buf, uint32 buflen)
50 {
51
52         /* times are stored as 32bit integer
53            take care on system with 64bit wide time_t
54            --SSS */
55         uint32  logon_time,
56                 logoff_time,
57                 kickoff_time,
58                 pass_last_set_time,
59                 pass_can_change_time,
60                 pass_must_change_time;
61         char *username;
62         char *domain;
63         char *nt_username;
64         char *dir_drive;
65         char *unknown_str;
66         char *munged_dial;
67         char *fullname;
68         char *homedir;
69         char *logon_script;
70         char *profile_path;
71         char *acct_desc;
72         char *workstations;
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;
77                 
78         uint32  /* uid, gid,*/ user_rid, group_rid, unknown_3, hours_len, unknown_5, unknown_6;
79         uint16  acct_ctrl, logon_divs;
80         uint8   *hours;
81         static uint8    *lm_pw_ptr, *nt_pw_ptr;
82         uint32          len = 0;
83         uint32          lmpwlen, ntpwlen, hourslen;
84         BOOL ret = True;
85                                                                         
86         /* unpack the buffer into variables */
87         len = tdb_unpack (buf, buflen, TDB_FORMAT_STRING,
88                 &logon_time,
89                 &logoff_time,
90                 &kickoff_time,
91                 &pass_last_set_time,
92                 &pass_can_change_time,
93                 &pass_must_change_time,
94                 &username_len, &username,
95                 &domain_len, &domain,
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,
106                 &user_rid,
107                 &group_rid,
108                 &lmpwlen, &lm_pw_ptr,
109                 &ntpwlen, &nt_pw_ptr,
110                 &acct_ctrl,
111                 &unknown_3,
112                 &logon_divs,
113                 &hours_len,
114                 &hourslen, &hours,
115                 &unknown_5,
116                 &unknown_6);
117                 
118         if (len == -1)  {
119                 ret = False;
120                 goto done;
121         }
122
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);
129
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);
143
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);
155
156 done:
157
158         SAFE_FREE(username);
159         SAFE_FREE(domain);
160         SAFE_FREE(nt_username);
161         SAFE_FREE(fullname);
162         SAFE_FREE(homedir);
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);
169
170         return ret;
171 }
172
173 /**********************************************************************
174  Intialize a BYTE buffer from a SAM_ACCOUNT struct
175  *********************************************************************/
176 static uint32 init_buffer_from_sam (uint8 **buf, const SAM_ACCOUNT *sampass)
177 {
178         size_t          len, buflen;
179
180         /* times are stored as 32bit integer
181            take care on system with 64bit wide time_t
182            --SSS */
183         uint32  logon_time,
184                 logoff_time,
185                 kickoff_time,
186                 pass_last_set_time,
187                 pass_can_change_time,
188                 pass_must_change_time;
189         const char *username;
190         const char *domain;
191         const char *nt_username;
192         const char *dir_drive;
193         const char *unknown_str;
194         const char *munged_dial;
195         const char *fullname;
196         const char *homedir;
197         const char *logon_script;
198         const char *profile_path;
199         const char *acct_desc;
200         const char *workstations;
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;
205
206         const uint8 *lm_pw;
207         const uint8 *nt_pw;
208         uint32  lm_pw_len = 16;
209         uint32  nt_pw_len = 16;
210
211         /* do we have a valid SAM_ACCOUNT pointer? */
212         if (sampass == NULL)
213                 return -1;
214                 
215         *buf = NULL;
216         buflen = 0;
217
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);
224
225
226         username = pdb_get_username(sampass);
227         if (username)
228                 username_len = strlen(username) +1;
229         else
230                 username_len = 0;
231         domain = pdb_get_domain(sampass);
232         if (domain)
233                 domain_len = strlen(domain) +1;
234         else
235                 domain_len = 0;
236         nt_username = pdb_get_nt_username(sampass);
237         if (nt_username)
238                 nt_username_len = strlen(nt_username) +1;
239         else
240                 nt_username_len = 0;
241         dir_drive = pdb_get_dirdrive(sampass);
242         if (dir_drive)
243                 dir_drive_len = strlen(dir_drive) +1;
244         else
245                 dir_drive_len = 0;
246         unknown_str = NULL;
247         unknown_str_len = 0;
248         munged_dial = pdb_get_munged_dial(sampass);
249         if (munged_dial)
250                 munged_dial_len = strlen(munged_dial) +1;
251         else
252                 munged_dial_len = 0;
253                 
254         fullname = pdb_get_fullname(sampass);
255         if (fullname)
256                 fullname_len = strlen(fullname) +1;
257         else
258                 fullname_len = 0;
259         homedir = pdb_get_homedir(sampass);
260         if (homedir)
261                 homedir_len = strlen(homedir) +1;
262         else
263                 homedir_len = 0;
264         logon_script = pdb_get_logon_script(sampass);
265         if (logon_script)
266                 logon_script_len = strlen(logon_script) +1;
267         else
268                 logon_script_len = 0;
269         profile_path = pdb_get_profile_path(sampass);
270         if (profile_path)
271                 profile_path_len = strlen(profile_path) +1;
272         else
273                 profile_path_len = 0;
274         acct_desc = pdb_get_acct_desc(sampass);
275         if (acct_desc)
276                 acct_desc_len = strlen(acct_desc) +1;
277         else
278                 acct_desc_len = 0;
279         workstations = pdb_get_workstations(sampass);
280         if (workstations)
281                 workstations_len = strlen(workstations) +1;
282         else
283                 workstations_len = 0;
284         
285         lm_pw = pdb_get_lanman_passwd(sampass);
286         if (!lm_pw)
287                 lm_pw_len = 0;
288         
289         nt_pw = pdb_get_nt_passwd(sampass);
290         if (!nt_pw)
291                 nt_pw_len = 0;
292                 
293         /* one time to get the size needed */
294         len = tdb_pack(NULL, 0,  TDB_FORMAT_STRING,
295                 logon_time,
296                 logoff_time,
297                 kickoff_time,
298                 pass_last_set_time,
299                 pass_can_change_time,
300                 pass_must_change_time,
301                 username_len, username,
302                 domain_len, domain,
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),
315                 lm_pw_len, lm_pw,
316                 nt_pw_len, nt_pw,
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));
324
325
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"));
329                 return (-1);
330         }
331         
332         /* now for the real call to tdb_pack() */
333         buflen = tdb_pack(*buf, len,  TDB_FORMAT_STRING,
334                 logon_time,
335                 logoff_time,
336                 kickoff_time,
337                 pass_last_set_time,
338                 pass_can_change_time,
339                 pass_must_change_time,
340                 username_len, username,
341                 domain_len, domain,
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),
354                 lm_pw_len, lm_pw,
355                 nt_pw_len, nt_pw,
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));
363         
364         
365         /* check to make sure we got it correct */
366         if (buflen != len) {
367                 /* error */
368                 SAFE_FREE (*buf);
369                 return (-1);
370         }
371
372         return (buflen);
373 }
374
375 /***************************************************************
376  Open the TDB passwd database for SAM account enumeration.
377 ****************************************************************/
378
379 BOOL pdb_setsampwent(BOOL update)
380 {
381         pstring         tdbfile;
382         
383         get_private_directory(tdbfile);
384         pstrcat (tdbfile, PASSDB_FILE_NAME);
385         
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)))
388         {
389                 DEBUG(0, ("Unable to open/create TDB passwd\n"));
390                 return False;
391         }
392         
393         global_tdb_ent.key = tdb_firstkey(global_tdb_ent.passwd_tdb);
394
395         return True;
396 }
397
398 /***************************************************************
399  End enumeration of the TDB passwd list.
400 ****************************************************************/
401
402 void pdb_endsampwent(void)
403 {
404         if (global_tdb_ent.passwd_tdb) {
405                 tdb_close(global_tdb_ent.passwd_tdb);
406                 global_tdb_ent.passwd_tdb = NULL;
407         }
408         
409         DEBUG(7, ("endtdbpwent: closed password file.\n"));
410 }
411
412 /*****************************************************************
413  Get one SAM_ACCOUNT from the TDB (next in line)
414 *****************************************************************/
415
416 BOOL pdb_getsampwent(SAM_ACCOUNT *user)
417 {
418         TDB_DATA        data;
419         struct passwd   *pw;
420         uid_t           uid;
421         gid_t           gid;
422         char *prefix = USERPREFIX;
423         int  prefixlen = strlen (prefix);
424
425         if (user==NULL) {
426                 DEBUG(0,("pdb_get_sampwent: SAM_ACCOUNT is NULL.\n"));
427                 return False;
428         }
429
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);
434
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"));
438                 return False;
439         }
440
441         data = tdb_fetch (global_tdb_ent.passwd_tdb, global_tdb_ent.key);
442         if (!data.dptr) {
443                 DEBUG(5,("pdb_getsampwent: database entry not found.\n"));
444                 return False;
445         }
446   
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);
451                 return False;
452         }
453         SAFE_FREE(data.dptr);
454         
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
457            permutations */
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)));
461                 return False;
462         }
463
464         uid = pw->pw_uid;
465         gid = pw->pw_gid;
466         pdb_set_uid (user, &uid);
467         pdb_set_gid (user, &gid);
468
469         /* increment to next in line */
470         global_tdb_ent.key = tdb_nextkey (global_tdb_ent.passwd_tdb, global_tdb_ent.key);
471
472         return True;
473 }
474
475 /******************************************************************
476  Lookup a name in the SAM TDB
477 ******************************************************************/
478
479 BOOL pdb_getsampwnam (SAM_ACCOUNT *user, const char *sname)
480 {
481         TDB_CONTEXT     *pwd_tdb;
482         TDB_DATA        data, key;
483         fstring         keystr;
484         struct passwd   *pw;
485         pstring         tdbfile;
486         fstring         name;
487         uid_t           uid;
488         gid_t           gid;
489
490
491         if (user==NULL) {
492                 DEBUG(0,("pdb_getsampwnam: SAM_ACCOUNT is NULL.\n"));
493                 return False;
494         }
495
496         /* Data is stored in all lower-case */
497         unix_strlower(sname, -1, name, sizeof(name));
498
499         get_private_directory(tdbfile);
500         pstrcat (tdbfile, PASSDB_FILE_NAME);
501         
502         /* set search key */
503         slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
504         key.dptr = keystr;
505         key.dsize = strlen (keystr) + 1;
506
507         /* open the accounts TDB */
508         if (!(pwd_tdb = tdb_open_log(tdbfile, 0, TDB_DEFAULT, O_RDONLY, 0600))) {
509                 DEBUG(0, ("pdb_getsampwnam: Unable to open TDB passwd!\n"));
510                 return False;
511         }
512
513         /* get the record */
514         data = tdb_fetch (pwd_tdb, key);
515         if (!data.dptr) {
516                 DEBUG(5,("pdb_getsampwnam (TDB): error fetching database.\n"));
517                 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
518                 tdb_close (pwd_tdb);
519                 return False;
520         }
521   
522         /* unpack the buffer */
523         if (!init_sam_from_buffer (user, data.dptr, data.dsize)) {
524                 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
525                 SAFE_FREE(data.dptr);
526                 return False;
527         }
528         SAFE_FREE(data.dptr);
529         
530         /* validate the account and fill in UNIX uid and gid.  sys_getpwnam()
531            is used instaed of Get_Pwnam() as we do not need to try case
532            permutations */
533         if ((pw=sys_getpwnam(pdb_get_username(user)))) {
534                 uid = pw->pw_uid;
535                 gid = pw->pw_gid;
536                 pdb_set_uid (user, &uid);
537                 pdb_set_gid (user, &gid);
538         }
539         
540         /* cleanup */
541         tdb_close (pwd_tdb);
542
543         return True;
544 }
545
546 /***************************************************************************
547  Search by uid
548  **************************************************************************/
549
550 BOOL pdb_getsampwuid (SAM_ACCOUNT* user, uid_t uid)
551 {
552         struct passwd   *pw;
553         fstring         name;
554
555         if (user==NULL) {
556                 DEBUG(0,("pdb_getsampwuid: SAM_ACCOUNT is NULL.\n"));
557                 return False;
558         }
559
560         pw = sys_getpwuid(uid);
561         if (pw == NULL) {
562                 DEBUG(0,("pdb_getsampwuid: getpwuid(%d) return NULL. User does not exist!\n", uid));
563                 return False;
564         }
565         fstrcpy (name, pw->pw_name);
566
567         return pdb_getsampwnam (user, name);
568
569 }
570
571 /***************************************************************************
572  Search by rid
573  **************************************************************************/
574
575 BOOL pdb_getsampwrid (SAM_ACCOUNT *user, uint32 rid)
576 {
577         TDB_CONTEXT             *pwd_tdb;
578         TDB_DATA                data, key;
579         fstring                 keystr;
580         pstring                 tdbfile;
581         fstring                 name;
582         
583         if (user==NULL) {
584                 DEBUG(0,("pdb_getsampwrid: SAM_ACCOUNT is NULL.\n"));
585                 return False;
586         }
587
588         get_private_directory(tdbfile);
589         pstrcat (tdbfile, PASSDB_FILE_NAME);
590         
591         /* set search key */
592         slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
593         key.dptr = keystr;
594         key.dsize = strlen (keystr) + 1;
595
596         /* open the accounts TDB */
597         if (!(pwd_tdb = tdb_open_log(tdbfile, 0, TDB_DEFAULT, O_RDONLY, 0600))) {
598                 DEBUG(0, ("pdb_getsampwrid: Unable to open TDB rid database!\n"));
599                 return False;
600         }
601
602         /* get the record */
603         data = tdb_fetch (pwd_tdb, key);
604         if (!data.dptr) {
605                 DEBUG(5,("pdb_getsampwrid (TDB): error fetching database.\n"));
606                 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
607                 tdb_close (pwd_tdb);
608                 return False;
609         }
610
611         fstrcpy (name, data.dptr);
612         SAFE_FREE(data.dptr);
613         
614         tdb_close (pwd_tdb);
615         
616         return pdb_getsampwnam (user, name);
617 }
618
619 /***************************************************************************
620  Delete a SAM_ACCOUNT
621 ****************************************************************************/
622
623 BOOL pdb_delete_sam_account(const char *sname)
624 {
625         SAM_ACCOUNT     *sam_pass = NULL;
626         TDB_CONTEXT     *pwd_tdb;
627         TDB_DATA        key, data;
628         fstring         keystr;
629         pstring         tdbfile;
630         uint32          rid;
631         fstring         name;
632         
633         unix_strlower(sname, -1, name, sizeof(name));
634         
635         get_private_directory(tdbfile);
636         pstrcat (tdbfile, PASSDB_FILE_NAME);
637
638         /* open the TDB */
639         if (!(pwd_tdb = tdb_open_log(tdbfile, 0, TDB_DEFAULT, O_RDWR, 0600))) {
640                 DEBUG(0, ("Unable to open TDB passwd!"));
641                 return False;
642         }
643   
644         /* set the search key */
645         slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
646         key.dptr = keystr;
647         key.dsize = strlen (keystr) + 1;
648         
649         /* get the record */
650         data = tdb_fetch (pwd_tdb, key);
651         if (!data.dptr) {
652                 DEBUG(5,("pdb_delete_sam_account (TDB): error fetching database.\n"));
653                 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
654                 tdb_close (pwd_tdb);
655                 return False;
656         }
657   
658         /* unpack the buffer */
659         if (!pdb_init_sam (&sam_pass)) {
660                 tdb_close (pwd_tdb);
661                 return False;
662         }
663         
664         if (!init_sam_from_buffer (sam_pass, data.dptr, data.dsize)) {
665                 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
666                 tdb_close (pwd_tdb);
667                 SAFE_FREE(data.dptr);
668                 return False;
669         }
670         SAFE_FREE(data.dptr);
671
672         rid = pdb_get_user_rid(sam_pass);
673
674         pdb_free_sam (&sam_pass);
675         
676         /* it's outaa here!  8^) */
677         if (tdb_delete(pwd_tdb, key) != TDB_SUCCESS) {
678                 DEBUG(5, ("Error deleting entry from tdb passwd database!\n"));
679                 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
680                 tdb_close(pwd_tdb); 
681                 return False;
682         }       
683
684         /* delete also the RID key */
685
686         /* set the search key */
687         slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
688         key.dptr = keystr;
689         key.dsize = strlen (keystr) + 1;
690
691         /* it's outaa here!  8^) */
692         if (tdb_delete(pwd_tdb, key) != TDB_SUCCESS) {
693                 DEBUG(5, ("Error deleting entry from tdb rid database!\n"));
694                 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
695                 tdb_close(pwd_tdb); 
696                 return False;
697         }
698         
699         tdb_close(pwd_tdb);
700         
701         return True;
702 }
703
704 /***************************************************************************
705  Update the TDB SAM
706 ****************************************************************************/
707
708 static BOOL tdb_update_sam(const SAM_ACCOUNT* newpwd, BOOL override, int flag)
709 {
710         TDB_CONTEXT     *pwd_tdb = NULL;
711         TDB_DATA        key, data;
712         uint8           *buf = NULL;
713         fstring         keystr;
714         pstring         tdbfile;
715         fstring         name;
716         BOOL            ret = True;
717         
718         get_private_directory(tdbfile);
719         pstrcat (tdbfile, PASSDB_FILE_NAME);
720         
721         /* if we don't have a RID, then FAIL */
722         if (!pdb_get_user_rid(newpwd))
723                 DEBUG (0,("tdb_update_sam: Failing to store a SAM_ACCOUNT for [%s] without a RID\n",pdb_get_username(newpwd)));
724         if (!pdb_get_group_rid(newpwd))
725                 DEBUG (0,("tdb_update_sam: Failing to store a SAM_ACCOUNT for [%s] without a primary group RID\n",pdb_get_username(newpwd)));
726
727         /* copy the SAM_ACCOUNT struct into a BYTE buffer for storage */
728         if ((data.dsize=init_buffer_from_sam (&buf, newpwd)) == -1) {
729                 DEBUG(0,("tdb_update_sam: ERROR - Unable to copy SAM_ACCOUNT info BYTE buffer!\n"));
730                 ret = False;
731                 goto done;
732         }
733         data.dptr = buf;
734
735         unix_strlower(pdb_get_username(newpwd), -1, name, sizeof(name));
736         
737         /* setup the USER index key */
738         slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
739         key.dptr = keystr;
740         key.dsize = strlen (keystr) + 1;
741
742         /* invalidate the existing TDB iterator if it is open */
743         if (global_tdb_ent.passwd_tdb) {
744                 tdb_close(global_tdb_ent.passwd_tdb);
745                 global_tdb_ent.passwd_tdb = NULL;
746         }
747
748         /* open the account TDB passwd*/
749         pwd_tdb = tdb_open_log(tdbfile, 0, TDB_DEFAULT, O_RDWR | O_CREAT, 0600);
750         if (!pwd_tdb)
751         {
752                 DEBUG(0, ("tdb_update_sam: Unable to open TDB passwd!\n"));
753                 return False;
754         }
755
756         /* add the account */
757         if (tdb_store(pwd_tdb, key, data, flag) != TDB_SUCCESS) {
758                 DEBUG(0, ("Unable to modify passwd TDB!"));
759                 DEBUGADD(0, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
760                 ret = False;
761                 goto done;
762         }
763         
764         /* setup RID data */
765         data.dsize = sizeof(fstring);
766         data.dptr = name;
767
768         /* setup the RID index key */
769         slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, pdb_get_user_rid(newpwd));
770         key.dptr = keystr;
771         key.dsize = strlen (keystr) + 1;
772         
773         /* add the reference */
774         if (tdb_store(pwd_tdb, key, data, flag) != TDB_SUCCESS) {
775                 DEBUG(0, ("Unable to modify TDB passwd !"));
776                 DEBUGADD(0, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
777                 ret = False;
778                 goto done;
779         }
780
781 done:   
782         /* cleanup */
783         tdb_close (pwd_tdb);
784         SAFE_FREE(buf);
785         
786         return (ret);   
787 }
788
789 /***************************************************************************
790  Modifies an existing SAM_ACCOUNT
791 ****************************************************************************/
792
793 BOOL pdb_update_sam_account (const SAM_ACCOUNT *newpwd, BOOL override)
794 {
795         return (tdb_update_sam(newpwd, override, TDB_MODIFY));
796 }
797
798 /***************************************************************************
799  Adds an existing SAM_ACCOUNT
800 ****************************************************************************/
801
802 BOOL pdb_add_sam_account (const SAM_ACCOUNT *newpwd)
803 {
804         return (tdb_update_sam(newpwd, True, TDB_INSERT));
805 }
806
807 #else
808         /* Do *NOT* make this function static. It breaks the compile on gcc. JRA */
809         void samtdb_dummy_function(void) { } /* stop some compilers complaining */
810 #endif /* WITH_TDB_SAM */