0e0bf541e390e757cd3dad93173b21db6fce5bc9
[kai/samba.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  * Copyright (C) Andrew Bartlett 2002
8  * 
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License as published by the Free
11  * Software Foundation; either version 2 of the License, or (at your option)
12  * any later version.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
17  * more details.
18  * 
19  * You should have received a copy of the GNU General Public License along with
20  * this program; if not, write to the Free Software Foundation, Inc., 675
21  * Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #include "includes.h"
25
26 #ifdef WITH_TDB_SAM
27
28 #define PDB_VERSION             "20010830"
29 #define PASSDB_FILE_NAME        "/passdb.tdb"
30 #define TDB_FORMAT_STRING       "ddddddBBBBBBBBBBBBddBBwdwdBdd"
31 #define USERPREFIX              "USER_"
32 #define RIDPREFIX               "RID_"
33
34 struct tdbsam_privates {
35         TDB_CONTEXT     *passwd_tdb;
36         TDB_DATA        key;
37
38         /* retrive-once info */
39         const char *tdbsam_location;
40
41         BOOL permit_non_unix_accounts;
42
43         uint32 low_nua_rid; 
44         uint32 high_nua_rid; 
45 };
46
47 /**********************************************************************
48  Intialize a SAM_ACCOUNT struct from a BYTE buffer of size len
49  *********************************************************************/
50
51 static BOOL init_sam_from_buffer (struct tdbsam_privates *tdb_state,
52                                   SAM_ACCOUNT *sampass, uint8 *buf, uint32 buflen)
53 {
54
55         /* times are stored as 32bit integer
56            take care on system with 64bit wide time_t
57            --SSS */
58         uint32  logon_time,
59                 logoff_time,
60                 kickoff_time,
61                 pass_last_set_time,
62                 pass_can_change_time,
63                 pass_must_change_time;
64         char *username;
65         char *domain;
66         char *nt_username;
67         char *dir_drive;
68         char *unknown_str;
69         char *munged_dial;
70         char *fullname;
71         char *homedir;
72         char *logon_script;
73         char *profile_path;
74         char *acct_desc;
75         char *workstations;
76         uint32  username_len, domain_len, nt_username_len,
77                 dir_drive_len, unknown_str_len, munged_dial_len,
78                 fullname_len, homedir_len, logon_script_len,
79                 profile_path_len, acct_desc_len, workstations_len;
80                 
81         uint32  /* uid, gid,*/ user_rid, group_rid, unknown_3, hours_len, unknown_5, unknown_6;
82         uint16  acct_ctrl, logon_divs;
83         uint8   *hours;
84         static uint8    *lm_pw_ptr, *nt_pw_ptr;
85         uint32          len = 0;
86         uint32          lmpwlen, ntpwlen, hourslen;
87         BOOL ret = True;
88         
89         if(sampass == NULL || buf == NULL) {
90                 DEBUG(0, ("init_sam_from_buffer: NULL parameters found!\n"));
91                 return False;
92         }
93                                                                         
94         /* unpack the buffer into variables */
95         len = tdb_unpack (buf, buflen, TDB_FORMAT_STRING,
96                 &logon_time,
97                 &logoff_time,
98                 &kickoff_time,
99                 &pass_last_set_time,
100                 &pass_can_change_time,
101                 &pass_must_change_time,
102                 &username_len, &username,
103                 &domain_len, &domain,
104                 &nt_username_len, &nt_username,
105                 &fullname_len, &fullname,
106                 &homedir_len, &homedir,
107                 &dir_drive_len, &dir_drive,
108                 &logon_script_len, &logon_script,
109                 &profile_path_len, &profile_path,
110                 &acct_desc_len, &acct_desc,
111                 &workstations_len, &workstations,
112                 &unknown_str_len, &unknown_str,
113                 &munged_dial_len, &munged_dial,
114                 &user_rid,
115                 &group_rid,
116                 &lmpwlen, &lm_pw_ptr,
117                 &ntpwlen, &nt_pw_ptr,
118                 &acct_ctrl,
119                 &unknown_3,
120                 &logon_divs,
121                 &hours_len,
122                 &hourslen, &hours,
123                 &unknown_5,
124                 &unknown_6);
125                 
126         if (len == -1)  {
127                 ret = False;
128                 goto done;
129         }
130
131         pdb_set_logon_time(sampass, logon_time);
132         pdb_set_logoff_time(sampass, logoff_time);
133         pdb_set_kickoff_time(sampass, kickoff_time);
134         pdb_set_pass_can_change_time(sampass, pass_can_change_time);
135         pdb_set_pass_must_change_time(sampass, pass_must_change_time);
136         pdb_set_pass_last_set_time(sampass, pass_last_set_time);
137
138         pdb_set_username     (sampass, username_len?username:NULL);
139         pdb_set_domain       (sampass, domain_len?domain:NULL);
140         pdb_set_nt_username  (sampass, nt_username_len?nt_username:NULL);
141         pdb_set_fullname     (sampass, fullname_len?fullname:NULL);
142         pdb_set_homedir      (sampass, homedir_len?homedir:NULL, True);
143         pdb_set_dir_drive    (sampass, dir_drive_len?dir_drive:NULL, True);
144         pdb_set_logon_script (sampass, logon_script_len?logon_script:NULL, True);
145         pdb_set_profile_path (sampass, profile_path_len?profile_path:NULL, True);
146         pdb_set_acct_desc    (sampass, acct_desc_len?acct_desc:NULL);
147         pdb_set_workstations (sampass, workstations_len?workstations:NULL);
148         pdb_set_munged_dial  (sampass, munged_dial_len?munged_dial:NULL);
149         if (!pdb_set_lanman_passwd(sampass, lmpwlen?lm_pw_ptr:NULL)) {
150                 ret = False;
151                 goto done;
152         }
153         if (!pdb_set_nt_passwd(sampass, ntpwlen?nt_pw_ptr:NULL)) {
154                 ret = False;
155                 goto done;
156         }
157
158         pdb_set_user_rid(sampass, user_rid);
159         pdb_set_group_rid(sampass, group_rid);
160         pdb_set_unknown_3(sampass, unknown_3);
161         pdb_set_hours_len(sampass, hours_len);
162         pdb_set_unknown_5(sampass, unknown_5);
163         pdb_set_unknown_6(sampass, unknown_6);
164         pdb_set_acct_ctrl(sampass, acct_ctrl);
165         pdb_set_logon_divs(sampass, logon_divs);
166         pdb_set_hours(sampass, hours);
167
168         if ((tdb_state->permit_non_unix_accounts) 
169             && (pdb_get_user_rid(sampass) >= tdb_state->low_nua_rid)
170             && (pdb_get_user_rid(sampass) <= tdb_state->high_nua_rid)) {
171                 
172         } else {
173                 struct passwd *pw;
174                 /* validate the account and fill in UNIX uid and gid.  sys_getpwnam()
175                    is used instaed of Get_Pwnam() as we do not need to try case
176                    permutations */
177
178                 if ((pw=getpwnam_alloc(pdb_get_username(sampass))) == NULL) {
179                         DEBUG(0,("init_sam_from_buffer: (tdbsam) getpwnam(%s) return NULL.  User does not exist!\n", 
180                                  pdb_get_username(sampass)));
181                         return False;
182                 }
183
184                 pdb_set_uid(sampass, pw->pw_uid);
185                 pdb_set_gid(sampass, pw->pw_gid);
186                 
187                 passwd_free(&pw);
188         }
189
190 done:
191
192         SAFE_FREE(username);
193         SAFE_FREE(domain);
194         SAFE_FREE(nt_username);
195         SAFE_FREE(fullname);
196         SAFE_FREE(homedir);
197         SAFE_FREE(dir_drive);
198         SAFE_FREE(logon_script);
199         SAFE_FREE(profile_path);
200         SAFE_FREE(acct_desc);
201         SAFE_FREE(workstations);
202         SAFE_FREE(munged_dial);
203
204         return ret;
205 }
206
207 /**********************************************************************
208  Intialize a BYTE buffer from a SAM_ACCOUNT struct
209  *********************************************************************/
210 static uint32 init_buffer_from_sam (struct tdbsam_privates *tdb_state, uint8 **buf, 
211                                     const SAM_ACCOUNT *sampass, uint32 user_rid, uint32 group_rid)
212 {
213         size_t          len, buflen;
214
215         /* times are stored as 32bit integer
216            take care on system with 64bit wide time_t
217            --SSS */
218         uint32  logon_time,
219                 logoff_time,
220                 kickoff_time,
221                 pass_last_set_time,
222                 pass_can_change_time,
223                 pass_must_change_time;
224         const char *username;
225         const char *domain;
226         const char *nt_username;
227         const char *dir_drive;
228         const char *unknown_str;
229         const char *munged_dial;
230         const char *fullname;
231         const char *homedir;
232         const char *logon_script;
233         const char *profile_path;
234         const char *acct_desc;
235         const char *workstations;
236         uint32  username_len, domain_len, nt_username_len,
237                 dir_drive_len, unknown_str_len, munged_dial_len,
238                 fullname_len, homedir_len, logon_script_len,
239                 profile_path_len, acct_desc_len, workstations_len;
240
241         const uint8 *lm_pw;
242         const uint8 *nt_pw;
243         uint32  lm_pw_len = 16;
244         uint32  nt_pw_len = 16;
245
246         /* do we have a valid SAM_ACCOUNT pointer? */
247         if (sampass == NULL) {
248                 DEBUG(0, ("init_buffer_from_sam: SAM_ACCOUNT is NULL!\n"));
249                 return -1;
250         }
251         
252         *buf = NULL;
253         buflen = 0;
254
255         logon_time = (uint32)pdb_get_logon_time(sampass);
256         logoff_time = (uint32)pdb_get_logoff_time(sampass);
257         kickoff_time = (uint32)pdb_get_kickoff_time(sampass);
258         pass_can_change_time = (uint32)pdb_get_pass_can_change_time(sampass);
259         pass_must_change_time = (uint32)pdb_get_pass_must_change_time(sampass);
260         pass_last_set_time = (uint32)pdb_get_pass_last_set_time(sampass);
261
262
263         username = pdb_get_username(sampass);
264         if (username)
265                 username_len = strlen(username) +1;
266         else
267                 username_len = 0;
268         domain = pdb_get_domain(sampass);
269         if (domain)
270                 domain_len = strlen(domain) +1;
271         else
272                 domain_len = 0;
273         nt_username = pdb_get_nt_username(sampass);
274         if (nt_username)
275                 nt_username_len = strlen(nt_username) +1;
276         else
277                 nt_username_len = 0;
278         dir_drive = pdb_get_dirdrive(sampass);
279         if (dir_drive)
280                 dir_drive_len = strlen(dir_drive) +1;
281         else
282                 dir_drive_len = 0;
283         unknown_str = NULL;
284         unknown_str_len = 0;
285         munged_dial = pdb_get_munged_dial(sampass);
286         if (munged_dial)
287                 munged_dial_len = strlen(munged_dial) +1;
288         else
289                 munged_dial_len = 0;
290                 
291         fullname = pdb_get_fullname(sampass);
292         if (fullname)
293                 fullname_len = strlen(fullname) +1;
294         else
295                 fullname_len = 0;
296         homedir = pdb_get_homedir(sampass);
297         if (homedir)
298                 homedir_len = strlen(homedir) +1;
299         else
300                 homedir_len = 0;
301         logon_script = pdb_get_logon_script(sampass);
302         if (logon_script)
303                 logon_script_len = strlen(logon_script) +1;
304         else
305                 logon_script_len = 0;
306         profile_path = pdb_get_profile_path(sampass);
307         if (profile_path)
308                 profile_path_len = strlen(profile_path) +1;
309         else
310                 profile_path_len = 0;
311         acct_desc = pdb_get_acct_desc(sampass);
312         if (acct_desc)
313                 acct_desc_len = strlen(acct_desc) +1;
314         else
315                 acct_desc_len = 0;
316         workstations = pdb_get_workstations(sampass);
317         if (workstations)
318                 workstations_len = strlen(workstations) +1;
319         else
320                 workstations_len = 0;
321         
322         lm_pw = pdb_get_lanman_passwd(sampass);
323         if (!lm_pw)
324                 lm_pw_len = 0;
325         
326         nt_pw = pdb_get_nt_passwd(sampass);
327         if (!nt_pw)
328                 nt_pw_len = 0;
329                 
330         /* one time to get the size needed */
331         len = tdb_pack(NULL, 0,  TDB_FORMAT_STRING,
332                 logon_time,
333                 logoff_time,
334                 kickoff_time,
335                 pass_last_set_time,
336                 pass_can_change_time,
337                 pass_must_change_time,
338                 username_len, username,
339                 domain_len, domain,
340                 nt_username_len, nt_username,
341                 fullname_len, fullname,
342                 homedir_len, homedir,
343                 dir_drive_len, dir_drive,
344                 logon_script_len, logon_script,
345                 profile_path_len, profile_path,
346                 acct_desc_len, acct_desc,
347                 workstations_len, workstations,
348                 unknown_str_len, unknown_str,
349                 munged_dial_len, munged_dial,
350                 user_rid,
351                 group_rid,
352                 lm_pw_len, lm_pw,
353                 nt_pw_len, nt_pw,
354                 pdb_get_acct_ctrl(sampass),
355                 pdb_get_unknown3(sampass),
356                 pdb_get_logon_divs(sampass),
357                 pdb_get_hours_len(sampass),
358                 MAX_HOURS_LEN, pdb_get_hours(sampass),
359                 pdb_get_unknown5(sampass),
360                 pdb_get_unknown6(sampass));
361
362
363         /* malloc the space needed */
364         if ( (*buf=(uint8*)malloc(len)) == NULL) {
365                 DEBUG(0,("init_buffer_from_sam: Unable to malloc() memory for buffer!\n"));
366                 return (-1);
367         }
368         
369         /* now for the real call to tdb_pack() */
370         buflen = tdb_pack(*buf, len,  TDB_FORMAT_STRING,
371                 logon_time,
372                 logoff_time,
373                 kickoff_time,
374                 pass_last_set_time,
375                 pass_can_change_time,
376                 pass_must_change_time,
377                 username_len, username,
378                 domain_len, domain,
379                 nt_username_len, nt_username,
380                 fullname_len, fullname,
381                 homedir_len, homedir,
382                 dir_drive_len, dir_drive,
383                 logon_script_len, logon_script,
384                 profile_path_len, profile_path,
385                 acct_desc_len, acct_desc,
386                 workstations_len, workstations,
387                 unknown_str_len, unknown_str,
388                 munged_dial_len, munged_dial,
389                 user_rid,
390                 group_rid,
391                 lm_pw_len, lm_pw,
392                 nt_pw_len, nt_pw,
393                 pdb_get_acct_ctrl(sampass),
394                 pdb_get_unknown3(sampass),
395                 pdb_get_logon_divs(sampass),
396                 pdb_get_hours_len(sampass),
397                 MAX_HOURS_LEN, pdb_get_hours(sampass),
398                 pdb_get_unknown5(sampass),
399                 pdb_get_unknown6(sampass));
400         
401         
402         /* check to make sure we got it correct */
403         if (buflen != len) {
404                 DEBUG(0, ("init_buffer_from_sam: somthing odd is going on here: bufflen (%d) != len (%d) in tdb_pack operations!\n", 
405                           buflen, len));  
406                 /* error */
407                 SAFE_FREE (*buf);
408                 return (-1);
409         }
410
411         return (buflen);
412 }
413
414 /***************************************************************
415  Open the TDB passwd database for SAM account enumeration.
416 ****************************************************************/
417
418 static BOOL tdbsam_setsampwent(struct pdb_context *context, BOOL update)
419 {
420         struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)context->pdb_selected->private_data;
421         
422         /* Open tdb passwd */
423         if (!(tdb_state->passwd_tdb = tdb_open_log(tdb_state->tdbsam_location, 0, TDB_DEFAULT, update?(O_RDWR|O_CREAT):O_RDONLY, 0600)))
424         {
425                 DEBUG(0, ("Unable to open/create TDB passwd\n"));
426                 return False;
427         }
428         
429         tdb_state->key = tdb_firstkey(tdb_state->passwd_tdb);
430
431         return True;
432 }
433
434 static void close_tdb(struct tdbsam_privates *tdb_state) 
435 {
436         if (tdb_state->passwd_tdb) {
437                 tdb_close(tdb_state->passwd_tdb);
438                 tdb_state->passwd_tdb = NULL;
439         }
440 }
441
442 /***************************************************************
443  End enumeration of the TDB passwd list.
444 ****************************************************************/
445
446 static void tdbsam_endsampwent(struct pdb_context *context)
447 {
448         struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)context->pdb_selected->private_data;
449         close_tdb(tdb_state);
450         
451         DEBUG(7, ("endtdbpwent: closed sam database.\n"));
452 }
453
454 /*****************************************************************
455  Get one SAM_ACCOUNT from the TDB (next in line)
456 *****************************************************************/
457
458 static BOOL tdbsam_getsampwent(struct pdb_context *context, SAM_ACCOUNT *user)
459 {
460         struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)context->pdb_selected->private_data;
461         TDB_DATA        data;
462         char *prefix = USERPREFIX;
463         int  prefixlen = strlen (prefix);
464
465
466         if (user==NULL) {
467                 DEBUG(0,("pdb_get_sampwent: SAM_ACCOUNT is NULL.\n"));
468                 return False;
469         }
470
471         /* skip all non-USER entries (eg. RIDs) */
472         while ((tdb_state->key.dsize != 0) && (strncmp(tdb_state->key.dptr, prefix, prefixlen)))
473                 /* increment to next in line */
474                 tdb_state->key = tdb_nextkey(tdb_state->passwd_tdb, tdb_state->key);
475
476         /* do we have an valid interation pointer? */
477         if(tdb_state->passwd_tdb == NULL) {
478                 DEBUG(0,("pdb_get_sampwent: Bad TDB Context pointer.\n"));
479                 return False;
480         }
481
482         data = tdb_fetch(tdb_state->passwd_tdb, tdb_state->key);
483         if (!data.dptr) {
484                 DEBUG(5,("pdb_getsampwent: database entry not found.\n"));
485                 return False;
486         }
487   
488         /* unpack the buffer */
489         if (!init_sam_from_buffer(tdb_state, user, data.dptr, data.dsize)) {
490                 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
491                 SAFE_FREE(data.dptr);
492                 return False;
493         }
494         SAFE_FREE(data.dptr);
495         
496         /* increment to next in line */
497         tdb_state->key = tdb_nextkey(tdb_state->passwd_tdb, tdb_state->key);
498
499         return True;
500 }
501
502 /******************************************************************
503  Lookup a name in the SAM TDB
504 ******************************************************************/
505
506 static BOOL tdbsam_getsampwnam (struct pdb_context *context, SAM_ACCOUNT *user, const char *sname)
507 {
508         struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)context->pdb_selected->private_data;
509         TDB_CONTEXT     *pwd_tdb;
510         TDB_DATA        data, key;
511         fstring         keystr;
512         fstring         name;
513
514         if (user==NULL) {
515                 DEBUG(0,("pdb_getsampwnam: SAM_ACCOUNT is NULL.\n"));
516                 return False;
517         }
518
519         /* Data is stored in all lower-case */
520         unix_strlower(sname, -1, name, sizeof(name));
521
522         /* set search key */
523         slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
524         key.dptr = keystr;
525         key.dsize = strlen(keystr) + 1;
526
527         /* open the accounts TDB */
528         if (!(pwd_tdb = tdb_open_log(tdb_state->tdbsam_location, 0, TDB_DEFAULT, O_RDONLY, 0600))) {
529                 DEBUG(0, ("pdb_getsampwnam: Unable to open TDB passwd (%s)!\n", tdb_state->tdbsam_location));
530                 return False;
531         }
532
533         /* get the record */
534         data = tdb_fetch(pwd_tdb, key);
535         if (!data.dptr) {
536                 DEBUG(5,("pdb_getsampwnam (TDB): error fetching database.\n"));
537                 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
538                 DEBUGADD(5, (" Key: %s\n", keystr));
539                 tdb_close(pwd_tdb);
540                 return False;
541         }
542   
543         /* unpack the buffer */
544         if (!init_sam_from_buffer(tdb_state, user, data.dptr, data.dsize)) {
545                 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
546                 SAFE_FREE(data.dptr);
547                 tdb_close(pwd_tdb);
548                 return False;
549         }
550         SAFE_FREE(data.dptr);
551
552         /* no further use for database, close it now */
553         tdb_close(pwd_tdb);
554         
555         return True;
556 }
557
558 /***************************************************************************
559  Search by rid
560  **************************************************************************/
561
562 static BOOL tdbsam_getsampwrid (struct pdb_context *context, SAM_ACCOUNT *user, uint32 rid)
563 {
564         struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)context->pdb_selected->private_data;
565         TDB_CONTEXT             *pwd_tdb;
566         TDB_DATA                data, key;
567         fstring                 keystr;
568         fstring                 name;
569         
570         if (user==NULL) {
571                 DEBUG(0,("pdb_getsampwrid: SAM_ACCOUNT is NULL.\n"));
572                 return False;
573         }
574
575         /* set search key */
576         slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
577         key.dptr = keystr;
578         key.dsize = strlen (keystr) + 1;
579
580         /* open the accounts TDB */
581         if (!(pwd_tdb = tdb_open_log(tdb_state->tdbsam_location, 0, TDB_DEFAULT, O_RDONLY, 0600))) {
582                 DEBUG(0, ("pdb_getsampwrid: Unable to open TDB rid database!\n"));
583                 return False;
584         }
585
586         /* get the record */
587         data = tdb_fetch (pwd_tdb, key);
588         if (!data.dptr) {
589                 DEBUG(5,("pdb_getsampwrid (TDB): error fetching database.\n"));
590                 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
591                 tdb_close (pwd_tdb);
592                 return False;
593         }
594
595         fstrcpy (name, data.dptr);
596         SAFE_FREE(data.dptr);
597         
598         tdb_close (pwd_tdb);
599         
600         return tdbsam_getsampwnam (context, user, name);
601 }
602
603 /***************************************************************************
604  Delete a SAM_ACCOUNT
605 ****************************************************************************/
606
607 static BOOL tdbsam_delete_sam_account(struct pdb_context *context, const SAM_ACCOUNT *sam_pass)
608 {
609         struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)context->pdb_selected->private_data;
610         TDB_CONTEXT     *pwd_tdb;
611         TDB_DATA        key;
612         fstring         keystr;
613         uint32          rid;
614         fstring         name;
615         
616         unix_strlower(pdb_get_username(sam_pass), -1, name, sizeof(name));
617         
618         /* open the TDB */
619         if (!(pwd_tdb = tdb_open_log(tdb_state->tdbsam_location, 0, TDB_DEFAULT, O_RDWR, 0600))) {
620                 DEBUG(0, ("Unable to open TDB passwd!"));
621                 return False;
622         }
623   
624         /* set the search key */
625         slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
626         key.dptr = keystr;
627         key.dsize = strlen (keystr) + 1;
628         
629         rid = pdb_get_user_rid(sam_pass);
630
631         /* it's outaa here!  8^) */
632         if (tdb_delete(pwd_tdb, key) != TDB_SUCCESS) {
633                 DEBUG(5, ("Error deleting entry from tdb passwd database!\n"));
634                 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
635                 tdb_close(pwd_tdb); 
636                 return False;
637         }       
638
639         /* delete also the RID key */
640
641         /* set the search key */
642         slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
643         key.dptr = keystr;
644         key.dsize = strlen (keystr) + 1;
645
646         /* it's outaa here!  8^) */
647         if (tdb_delete(pwd_tdb, key) != TDB_SUCCESS) {
648                 DEBUG(5, ("Error deleting entry from tdb rid database!\n"));
649                 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
650                 tdb_close(pwd_tdb); 
651                 return False;
652         }
653         
654         tdb_close(pwd_tdb);
655         
656         return True;
657 }
658
659 /***************************************************************************
660  Update the TDB SAM
661 ****************************************************************************/
662
663 static BOOL tdb_update_sam(struct pdb_context *context, const SAM_ACCOUNT* newpwd, int flag)
664 {
665         struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)context->pdb_selected->private_data;
666         TDB_CONTEXT     *pwd_tdb = NULL;
667         TDB_DATA        key, data;
668         uint8           *buf = NULL;
669         fstring         keystr;
670         fstring         name;
671         BOOL            ret = True;
672         uint32          user_rid;
673         uint32         group_rid;
674         int32          tdb_ret;
675
676         /* invalidate the existing TDB iterator if it is open */
677         if (tdb_state->passwd_tdb) {
678                 tdb_close(tdb_state->passwd_tdb);
679                 tdb_state->passwd_tdb = NULL;
680         }
681
682         /* open the account TDB passwd*/
683         pwd_tdb = tdb_open_log(tdb_state->tdbsam_location, 0, TDB_DEFAULT, O_RDWR | O_CREAT, 0600);
684         if (!pwd_tdb)
685         {
686                 DEBUG(0, ("tdb_update_sam: Unable to open TDB passwd (%s)!\n", tdb_state->tdbsam_location));
687                 return False;
688         }
689
690         /* if we don't have a RID, then make them up. */
691         if (!(user_rid = pdb_get_user_rid(newpwd))) {
692                 if (!tdb_state->permit_non_unix_accounts) {
693                         DEBUG (0,("tdb_update_sam: Failing to store a SAM_ACCOUNT for [%s] without a RID\n",pdb_get_username(newpwd)));
694                         ret = False;
695                         goto done;
696                 } else {
697                         user_rid = tdb_state->low_nua_rid;
698                         tdb_ret = tdb_change_int32_atomic(pwd_tdb, "NUA_NEXT_RID", &user_rid, RID_MULTIPLIER);
699                         if (tdb_ret == -1) {
700                                 ret = False;
701                                 goto done;
702                         }
703                 }
704         }
705
706         if (!(group_rid = pdb_get_group_rid(newpwd))) {
707                 if (!tdb_state->permit_non_unix_accounts) {
708                         DEBUG (0,("tdb_update_sam: Failing to store a SAM_ACCOUNT for [%s] without a primary group RID\n",pdb_get_username(newpwd)));
709                         ret = False;
710                         goto done;
711                 } else {
712                         /* This seems like a good default choice for non-unix users */
713                         group_rid = DOMAIN_GROUP_RID_USERS;
714                 }
715         }
716
717         /* copy the SAM_ACCOUNT struct into a BYTE buffer for storage */
718         if ((data.dsize=init_buffer_from_sam (tdb_state, &buf, newpwd, user_rid, group_rid)) == -1) {
719                 DEBUG(0,("tdb_update_sam: ERROR - Unable to copy SAM_ACCOUNT info BYTE buffer!\n"));
720                 ret = False;
721                 goto done;
722         }
723         data.dptr = buf;
724
725         unix_strlower(pdb_get_username(newpwd), -1, name, sizeof(name));
726         
727         DEBUG(5, ("Storing %saccount %s with RID %d\n", flag == TDB_INSERT ? "(new) " : "", name, user_rid));
728
729         /* setup the USER index key */
730         slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
731         key.dptr = keystr;
732         key.dsize = strlen (keystr) + 1;
733
734         /* add the account */
735         if (tdb_store(pwd_tdb, key, data, flag) != TDB_SUCCESS) {
736                 DEBUG(0, ("Unable to modify passwd TDB!"));
737                 DEBUGADD(0, (" Error: %s", tdb_errorstr(pwd_tdb)));
738                 DEBUGADD(0, (" occured while storing the main record (%s)\n", keystr));
739                 ret = False;
740                 goto done;
741         }
742         
743         /* setup RID data */
744         data.dsize = sizeof(fstring);
745         data.dptr = name;
746
747         /* setup the RID index key */
748         slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, user_rid);
749         key.dptr = keystr;
750         key.dsize = strlen (keystr) + 1;
751         
752         /* add the reference */
753         if (tdb_store(pwd_tdb, key, data, flag) != TDB_SUCCESS) {
754                 DEBUG(0, ("Unable to modify TDB passwd !"));
755                 DEBUGADD(0, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
756                 DEBUGADD(0, (" occured while storing the RID index (%s)\n", keystr));
757                 ret = False;
758                 goto done;
759         }
760
761 done:   
762         /* cleanup */
763         tdb_close (pwd_tdb);
764         SAFE_FREE(buf);
765         
766         return (ret);   
767 }
768
769 /***************************************************************************
770  Modifies an existing SAM_ACCOUNT
771 ****************************************************************************/
772
773 static BOOL tdbsam_update_sam_account (struct pdb_context *context, const SAM_ACCOUNT *newpwd)
774 {
775         return (tdb_update_sam(context, newpwd, TDB_MODIFY));
776 }
777
778 /***************************************************************************
779  Adds an existing SAM_ACCOUNT
780 ****************************************************************************/
781
782 static BOOL tdbsam_add_sam_account (struct pdb_context *context, const SAM_ACCOUNT *newpwd)
783 {
784         return (tdb_update_sam(context, newpwd, TDB_INSERT));
785 }
786
787 static void free_private_data(void **vp) 
788 {
789         struct tdbsam_privates **tdb_state = (struct tdbsam_privates **)vp;
790         close_tdb(*tdb_state);
791         *tdb_state = NULL;
792
793         /* No need to free any further, as it is talloc()ed */
794 }
795
796
797 NTSTATUS pdb_init_tdbsam(PDB_CONTEXT *pdb_context, PDB_METHODS **pdb_method, const char *location)
798 {
799         NTSTATUS nt_status;
800         struct tdbsam_privates *tdb_state;
801
802         if (!NT_STATUS_IS_OK(nt_status = make_pdb_methods(pdb_context->mem_ctx, pdb_method))) {
803                 return nt_status;
804         }
805
806         (*pdb_method)->setsampwent = tdbsam_setsampwent;
807         (*pdb_method)->endsampwent = tdbsam_endsampwent;
808         (*pdb_method)->getsampwent = tdbsam_getsampwent;
809         (*pdb_method)->getsampwnam = tdbsam_getsampwnam;
810         (*pdb_method)->getsampwrid = tdbsam_getsampwrid;
811         (*pdb_method)->add_sam_account = tdbsam_add_sam_account;
812         (*pdb_method)->update_sam_account = tdbsam_update_sam_account;
813         (*pdb_method)->delete_sam_account = tdbsam_delete_sam_account;
814
815         /* TODO: Setup private data and free */
816
817         tdb_state = talloc_zero(pdb_context->mem_ctx, sizeof(struct tdbsam_privates));
818
819         if (!tdb_state) {
820                 DEBUG(0, ("talloc() failed for tdbsam private_data!\n"));
821                 return NT_STATUS_NO_MEMORY;
822         }
823
824         if (location) {
825                 tdb_state->tdbsam_location = talloc_strdup(pdb_context->mem_ctx, location);
826         } else {
827                 pstring tdbfile;
828                 get_private_directory(tdbfile);
829                 pstrcat (tdbfile, PASSDB_FILE_NAME);
830                 tdb_state->tdbsam_location = talloc_strdup(pdb_context->mem_ctx, tdbfile);
831         }
832
833         (*pdb_method)->private_data = tdb_state;
834
835         (*pdb_method)->free_private_data = free_private_data;
836
837         return NT_STATUS_OK;
838 }
839
840 NTSTATUS pdb_init_tdbsam_nua(PDB_CONTEXT *pdb_context, PDB_METHODS **pdb_method, const char *location)
841 {
842         NTSTATUS nt_status;
843         struct tdbsam_privates *tdb_state;
844         uint32 low_nua_uid, high_nua_uid;
845
846         if (!NT_STATUS_IS_OK(nt_status = pdb_init_tdbsam(pdb_context, pdb_method, location))) {
847                 return nt_status;
848         }
849
850         tdb_state = (*pdb_method)->private_data;
851         
852         tdb_state->permit_non_unix_accounts = True;
853
854         if (!lp_non_unix_account_range(&low_nua_uid, &high_nua_uid)) {
855                 DEBUG(0, ("cannot use tdbsam_nua without 'non unix account range' in smb.conf!\n"));
856                 return NT_STATUS_UNSUCCESSFUL;
857         }
858
859         tdb_state->low_nua_rid=pdb_uid_to_user_rid(low_nua_uid);
860
861         tdb_state->high_nua_rid=pdb_uid_to_user_rid(high_nua_uid);
862
863         return NT_STATUS_OK;
864 }
865
866
867 #else
868
869 NTSTATUS pdb_init_tdbsam(PDB_CONTEXT *pdb_context, PDB_METHODS **pdb_method, const char *location)
870 {
871         DEBUG(0, ("tdbsam not compiled in!\n"));
872         return NT_STATUS_UNSUCCESSFUL;
873 }
874
875 NTSTATUS pdb_init_tdbsam_nua(PDB_CONTEXT *pdb_context, PDB_METHODS **pdb_method, const char *location)
876 {
877         DEBUG(0, ("tdbsam_nua not compiled in!\n"));
878         return NT_STATUS_UNSUCCESSFUL;
879 }
880
881
882 #endif