- fix handling of 0 last_change_time and must_change_time
[sfrench/samba-autobuild/.git] / source3 / 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, 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         char *username;
190         char *domain;
191         char *nt_username;
192         char *dir_drive;
193         char *unknown_str;
194         char *munged_dial;
195         char *fullname;
196         char *homedir;
197         char *logon_script;
198         char *profile_path;
199         char *acct_desc;
200         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         uint8           *lm_pw;
207         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         standard_sub_advanced(-1, pdb_get_username(user), "", gid, pdb_get_logon_script(user));
470         standard_sub_advanced(-1, pdb_get_username(user), "", gid, pdb_get_profile_path(user));
471         standard_sub_advanced(-1, pdb_get_username(user), "", gid, pdb_get_homedir(user));
472
473         /* increment to next in line */
474         global_tdb_ent.key = tdb_nextkey (global_tdb_ent.passwd_tdb, global_tdb_ent.key);
475
476         return True;
477 }
478
479 /******************************************************************
480  Lookup a name in the SAM TDB
481 ******************************************************************/
482
483 BOOL pdb_getsampwnam (SAM_ACCOUNT *user, char *sname)
484 {
485         TDB_CONTEXT     *pwd_tdb;
486         TDB_DATA        data, key;
487         fstring         keystr;
488         struct passwd   *pw;
489         pstring         tdbfile;
490         fstring         name;
491         uid_t           uid;
492         gid_t           gid;
493
494
495         if (user==NULL) {
496                 DEBUG(0,("pdb_getsampwnam: SAM_ACCOUNT is NULL.\n"));
497                 return False;
498         }
499
500         /* Data is stored in all lower-case */
501         unix_strlower(sname, -1, name, sizeof(name));
502
503         get_private_directory(tdbfile);
504         pstrcat (tdbfile, PASSDB_FILE_NAME);
505         
506         /* set search key */
507         slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
508         key.dptr = keystr;
509         key.dsize = strlen (keystr) + 1;
510
511         /* open the accounts TDB */
512         if (!(pwd_tdb = tdb_open_log(tdbfile, 0, TDB_DEFAULT, O_RDONLY, 0600))) {
513                 DEBUG(0, ("pdb_getsampwnam: Unable to open TDB passwd!\n"));
514                 return False;
515         }
516
517         /* get the record */
518         data = tdb_fetch (pwd_tdb, key);
519         if (!data.dptr) {
520                 DEBUG(5,("pdb_getsampwnam (TDB): error fetching database.\n"));
521                 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
522                 tdb_close (pwd_tdb);
523                 return False;
524         }
525   
526         /* unpack the buffer */
527         if (!init_sam_from_buffer (user, data.dptr, data.dsize)) {
528                 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
529                 SAFE_FREE(data.dptr);
530                 return False;
531         }
532         SAFE_FREE(data.dptr);
533         
534         /* validate the account and fill in UNIX uid and gid.  sys_getpwnam()
535            is used instaed of Get_Pwnam() as we do not need to try case
536            permutations */
537         if ((pw=sys_getpwnam(pdb_get_username(user))) == NULL) {
538                 DEBUG(0,("pdb_getsampwent: getpwnam(%s) return NULL.  User does not exist!\n", 
539                           pdb_get_username(user)));
540                 return False;
541         }
542         
543         uid = pw->pw_uid;
544         gid = pw->pw_gid;
545         pdb_set_uid (user, uid);
546         pdb_set_gid (user, gid);
547         
548         /* 21 days from present */
549         pdb_set_pass_must_change_time(user, time(NULL)+1814400);        
550         
551         standard_sub_advanced(-1, pdb_get_username(user), "", gid, pdb_get_logon_script(user));
552         standard_sub_advanced(-1, pdb_get_username(user), "", gid, pdb_get_profile_path(user));
553         standard_sub_advanced(-1, pdb_get_username(user), "", gid, pdb_get_homedir(user));
554
555         /* cleanup */
556         tdb_close (pwd_tdb);
557
558         return True;
559 }
560
561 /***************************************************************************
562  Search by uid
563  **************************************************************************/
564
565 BOOL pdb_getsampwuid (SAM_ACCOUNT* user, uid_t uid)
566 {
567         struct passwd   *pw;
568         fstring         name;
569
570         if (user==NULL) {
571                 DEBUG(0,("pdb_getsampwuid: SAM_ACCOUNT is NULL.\n"));
572                 return False;
573         }
574
575         pw = sys_getpwuid(uid);
576         if (pw == NULL) {
577                 DEBUG(0,("pdb_getsampwuid: getpwuid(%d) return NULL. User does not exist!\n", uid));
578                 return False;
579         }
580         fstrcpy (name, pw->pw_name);
581
582         return pdb_getsampwnam (user, name);
583
584 }
585
586 /***************************************************************************
587  Search by rid
588  **************************************************************************/
589
590 BOOL pdb_getsampwrid (SAM_ACCOUNT *user, uint32 rid)
591 {
592         TDB_CONTEXT             *pwd_tdb;
593         TDB_DATA                data, key;
594         fstring                 keystr;
595         pstring                 tdbfile;
596         fstring                 name;
597         
598         if (user==NULL) {
599                 DEBUG(0,("pdb_getsampwrid: SAM_ACCOUNT is NULL.\n"));
600                 return False;
601         }
602
603         get_private_directory(tdbfile);
604         pstrcat (tdbfile, PASSDB_FILE_NAME);
605         
606         /* set search key */
607         slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
608         key.dptr = keystr;
609         key.dsize = strlen (keystr) + 1;
610
611         /* open the accounts TDB */
612         if (!(pwd_tdb = tdb_open_log(tdbfile, 0, TDB_DEFAULT, O_RDONLY, 0600))) {
613                 DEBUG(0, ("pdb_getsampwrid: Unable to open TDB rid database!\n"));
614                 return False;
615         }
616
617         /* get the record */
618         data = tdb_fetch (pwd_tdb, key);
619         if (!data.dptr) {
620                 DEBUG(5,("pdb_getsampwrid (TDB): error fetching database.\n"));
621                 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
622                 tdb_close (pwd_tdb);
623                 return False;
624         }
625
626         fstrcpy (name, data.dptr);
627         SAFE_FREE(data.dptr);
628         
629         tdb_close (pwd_tdb);
630         
631         return pdb_getsampwnam (user, name);
632 }
633
634 /***************************************************************************
635  Delete a SAM_ACCOUNT
636 ****************************************************************************/
637
638 BOOL pdb_delete_sam_account(char *sname)
639 {
640         SAM_ACCOUNT     *sam_pass = NULL;
641         TDB_CONTEXT     *pwd_tdb;
642         TDB_DATA        key, data;
643         fstring         keystr;
644         pstring         tdbfile;
645         uint32          rid;
646         fstring         name;
647         
648         unix_strlower(sname, -1, name, sizeof(name));
649         
650         get_private_directory(tdbfile);
651         pstrcat (tdbfile, PASSDB_FILE_NAME);
652
653         /* open the TDB */
654         if (!(pwd_tdb = tdb_open_log(tdbfile, 0, TDB_DEFAULT, O_RDWR, 0600))) {
655                 DEBUG(0, ("Unable to open TDB passwd!"));
656                 return False;
657         }
658   
659         /* set the search key */
660         slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
661         key.dptr = keystr;
662         key.dsize = strlen (keystr) + 1;
663         
664         /* get the record */
665         data = tdb_fetch (pwd_tdb, key);
666         if (!data.dptr) {
667                 DEBUG(5,("pdb_delete_sam_account (TDB): error fetching database.\n"));
668                 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
669                 tdb_close (pwd_tdb);
670                 return False;
671         }
672   
673         /* unpack the buffer */
674         if (!pdb_init_sam (&sam_pass)) {
675                 tdb_close (pwd_tdb);
676                 return False;
677         }
678         
679         if (!init_sam_from_buffer (sam_pass, data.dptr, data.dsize)) {
680                 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
681                 tdb_close (pwd_tdb);
682                 SAFE_FREE(data.dptr);
683                 return False;
684         }
685         SAFE_FREE(data.dptr);
686
687         rid = pdb_get_user_rid(sam_pass);
688
689         pdb_free_sam (&sam_pass);
690         
691         /* it's outaa here!  8^) */
692         if (tdb_delete(pwd_tdb, key) != TDB_SUCCESS) {
693                 DEBUG(5, ("Error deleting entry from tdb passwd database!\n"));
694                 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
695                 tdb_close(pwd_tdb); 
696                 return False;
697         }       
698
699         /* delete also the RID key */
700
701         /* set the search key */
702         slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
703         key.dptr = keystr;
704         key.dsize = strlen (keystr) + 1;
705
706         /* it's outaa here!  8^) */
707         if (tdb_delete(pwd_tdb, key) != TDB_SUCCESS) {
708                 DEBUG(5, ("Error deleting entry from tdb rid database!\n"));
709                 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
710                 tdb_close(pwd_tdb); 
711                 return False;
712         }
713         
714         tdb_close(pwd_tdb);
715         
716         return True;
717 }
718
719 /***************************************************************************
720  Update the TDB SAM
721 ****************************************************************************/
722
723 static BOOL tdb_update_sam(SAM_ACCOUNT* newpwd, BOOL override, int flag)
724 {
725         TDB_CONTEXT     *pwd_tdb = NULL;
726         TDB_DATA        key, data;
727         uint8           *buf = NULL;
728         fstring         keystr;
729         pstring         tdbfile;
730         fstring         name;
731         BOOL            ret = True;
732         
733         get_private_directory(tdbfile);
734         pstrcat (tdbfile, PASSDB_FILE_NAME);
735         
736         if ( (!newpwd->uid) || (!newpwd->gid) )
737                 DEBUG (0,("tdb_update_sam: Storing a SAM_ACCOUNT for [%s] with uid %d and gid %d!\n",
738                         newpwd->username, newpwd->uid, newpwd->gid));
739                                 
740         /* if we don't have a RID, then generate one */
741         if (!newpwd->user_rid)
742                 pdb_set_user_rid (newpwd, pdb_uid_to_user_rid (newpwd->uid));
743         if (!newpwd->group_rid)
744                 pdb_set_group_rid (newpwd, pdb_gid_to_group_rid (newpwd->gid));
745
746         /* copy the SAM_ACCOUNT struct into a BYTE buffer for storage */
747         if ((data.dsize=init_buffer_from_sam (&buf, newpwd)) == -1) {
748                 DEBUG(0,("tdb_update_sam: ERROR - Unable to copy SAM_ACCOUNT info BYTE buffer!\n"));
749                 ret = False;
750                 goto done;
751         }
752         data.dptr = buf;
753
754         unix_strlower(pdb_get_username(newpwd), -1, name, sizeof(name));
755         
756         /* setup the USER index key */
757         slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
758         key.dptr = keystr;
759         key.dsize = strlen (keystr) + 1;
760
761         /* invalidate the existing TDB iterator if it is open */
762         if (global_tdb_ent.passwd_tdb) {
763                 tdb_close(global_tdb_ent.passwd_tdb);
764                 global_tdb_ent.passwd_tdb = NULL;
765         }
766
767         /* open the account TDB passwd*/
768         pwd_tdb = tdb_open_log(tdbfile, 0, TDB_DEFAULT, O_RDWR | O_CREAT, 0600);
769         if (!pwd_tdb)
770         {
771                 DEBUG(0, ("tdb_update_sam: Unable to open TDB passwd!\n"));
772                 return False;
773         }
774
775         /* add the account */
776         if (tdb_store(pwd_tdb, key, data, flag) != TDB_SUCCESS) {
777                 DEBUG(0, ("Unable to modify passwd TDB!"));
778                 DEBUGADD(0, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
779                 tdb_close (pwd_tdb);
780                 ret = False;
781                 goto done;
782         }
783         
784         /* setup RID data */
785         data.dsize = sizeof(fstring);
786         data.dptr = name;
787
788         /* setup the RID index key */
789         slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, pdb_get_user_rid(newpwd));
790         key.dptr = keystr;
791         key.dsize = strlen (keystr) + 1;
792         
793         /* add the reference */
794         if (tdb_store(pwd_tdb, key, data, flag) != TDB_SUCCESS) {
795                 DEBUG(0, ("Unable to modify TDB passwd !"));
796                 DEBUGADD(0, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
797                 ret = False;
798                 goto done;
799         }
800
801 done:   
802         /* cleanup */
803         tdb_close (pwd_tdb);
804         SAFE_FREE(buf);
805         
806         return (ret);   
807 }
808
809 /***************************************************************************
810  Modifies an existing SAM_ACCOUNT
811 ****************************************************************************/
812
813 BOOL pdb_update_sam_account (SAM_ACCOUNT *newpwd, BOOL override)
814 {
815         return (tdb_update_sam(newpwd, override, TDB_MODIFY));
816 }
817
818 /***************************************************************************
819  Adds an existing SAM_ACCOUNT
820 ****************************************************************************/
821
822 BOOL pdb_add_sam_account (SAM_ACCOUNT *newpwd)
823 {
824         return (tdb_update_sam(newpwd, True, TDB_INSERT));
825 }
826
827 #else
828         /* Do *NOT* make this function static. It breaks the compile on gcc. JRA */
829         void samtdb_dummy_function(void) { } /* stop some compilers complaining */
830 #endif /* WITH_TDB_SAM */