Move tdbsam versioning/upgrade code into 3.0
[jra/samba/.git] / source3 / passdb / pdb_tdb.c
1 /*
2  * Unix SMB/CIFS implementation. 
3  * SMB parameters and setup
4  * Copyright (C) Andrew Tridgell   1992-1998
5  * Copyright (C) Simo Sorce        2000-2002
6  * Copyright (C) Gerald Carter     2000
7  * Copyright (C) Jeremy Allison    2001
8  * Copyright (C) Andrew Bartlett   2002
9  * 
10  * This program is free software; you can redistribute it and/or modify it under
11  * the terms of the GNU General Public License as published by the Free
12  * Software Foundation; either version 2 of the License, or (at your option)
13  * any later version.
14  * 
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
18  * more details.
19  * 
20  * You should have received a copy of the GNU General Public License along with
21  * this program; if not, write to the Free Software Foundation, Inc., 675
22  * Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25 #include "includes.h"
26
27 #if 0 /* when made a module use this */
28
29 static int tdbsam_debug_level = DBGC_ALL;
30 #undef DBGC_CLASS
31 #define DBGC_CLASS tdbsam_debug_level
32
33 #else
34
35 #undef DBGC_CLASS
36 #define DBGC_CLASS DBGC_PASSDB
37
38 #endif
39
40 #define TDBSAM_VERSION  1       /* Most recent TDBSAM version */
41 #define TDBSAM_VERSION_STRING   "INFO/version"
42 #define PASSDB_FILE_NAME        "passdb.tdb"
43 #define USERPREFIX              "USER_"
44 #define RIDPREFIX               "RID_"
45 #define tdbsamver_t     int32
46
47 struct tdbsam_privates {
48         TDB_CONTEXT     *passwd_tdb;
49
50         /* retrive-once info */
51         const char *tdbsam_location;
52 };
53
54 struct pwent_list {
55         struct pwent_list *prev, *next;
56         TDB_DATA key;
57 };
58 static struct pwent_list *tdbsam_pwent_list;
59
60
61 /**
62  * Convert old TDBSAM to the latest version.
63  * @param pdb_tdb A pointer to the opened TDBSAM file which must be converted. 
64  *                This file must be opened with read/write access.
65  * @param from Current version of the TDBSAM file.
66  * @return True if the conversion has been successful, false otherwise. 
67  **/
68
69 static BOOL tdbsam_convert(TDB_CONTEXT *pdb_tdb, tdbsamver_t from) 
70 {
71         const char * vstring = TDBSAM_VERSION_STRING;
72         SAM_ACCOUNT *user = NULL;
73         const char *prefix = USERPREFIX;
74         TDB_DATA        data, key, old_key;
75         uint8           *buf = NULL;
76         BOOL            ret;
77
78         if (pdb_tdb == NULL) {
79                 DEBUG(0,("tdbsam_convert: Bad TDB Context pointer.\n"));
80                 return False;
81         }
82
83         /* handle a Samba upgrade */
84         tdb_lock_bystring(pdb_tdb, vstring, 0);
85         
86         if (!NT_STATUS_IS_OK(pdb_init_sam(&user))) {
87                 DEBUG(0,("tdbsam_convert: cannot initialized a SAM_ACCOUNT.\n"));
88                 return False;
89         }
90
91         /* Enumerate all records and convert them */
92         key = tdb_firstkey(pdb_tdb);
93
94         while (key.dptr) {
95         
96                 /* skip all non-USER entries (eg. RIDs) */
97                 while ((key.dsize != 0) && (strncmp(key.dptr, prefix, strlen (prefix)))) {
98                         old_key = key;
99                         /* increment to next in line */
100                         key = tdb_nextkey(pdb_tdb, key);
101                         SAFE_FREE(old_key.dptr);
102                 }
103         
104                 if (key.dptr) {
105                         
106                         /* read from tdbsam */
107                         data = tdb_fetch(pdb_tdb, key);
108                         if (!data.dptr) {
109                                 DEBUG(0,("tdbsam_convert: database entry not found: %s.\n",key.dptr));
110                                 return False;
111                         }
112         
113                         if (!NT_STATUS_IS_OK(pdb_reset_sam(user))) {
114                                 DEBUG(0,("tdbsam_convert: cannot reset SAM_ACCOUNT.\n"));
115                                 SAFE_FREE(data.dptr);
116                                 return False;
117                         }
118                         
119                         /* unpack the buffer from the former format */
120                         DEBUG(10,("tdbsam_convert: Try unpacking a record with (key:%s) (version:%d)\n", key.dptr, from));
121                         switch (from) {
122                                 case 0:
123                                         ret = init_sam_from_buffer_v0(user, (uint8 *)data.dptr, data.dsize);
124                                         break;
125                                 case 1:
126                                         ret = init_sam_from_buffer_v1(user, (uint8 *)data.dptr, data.dsize);
127                                         break;
128                                 default:
129                                         /* unknown tdbsam version */
130                                         ret = False;
131                         }
132                         if (!ret) {
133                                 DEBUG(0,("tdbsam_convert: Bad SAM_ACCOUNT entry returned from TDB (key:%s) (version:%d)\n", key.dptr, from));
134                                 SAFE_FREE(data.dptr);
135                                 return False;
136                         }
137         
138                         /* pack from the buffer into the new format */
139                         DEBUG(10,("tdbsam_convert: Try packing a record (key:%s) (version:%d)\n", key.dptr, from));
140                         if ((data.dsize=init_buffer_from_sam (&buf, user, False)) == -1) {
141                                 DEBUG(0,("tdbsam_convert: cannot pack the SAM_ACCOUNT into the new format\n"));
142                                 SAFE_FREE(data.dptr);
143                                 return False;
144                         }
145                         data.dptr = (char *)buf;
146                         
147                         /* Store the buffer inside the TDBSAM */
148                         if (tdb_store(pdb_tdb, key, data, TDB_MODIFY) != TDB_SUCCESS) {
149                                 DEBUG(0,("tdbsam_convert: cannot store the SAM_ACCOUNT (key:%s) in new format\n",key.dptr));
150                                 SAFE_FREE(data.dptr);
151                                 return False;
152                         }
153                         
154                         SAFE_FREE(data.dptr);
155                         
156                         /* increment to next in line */
157                         old_key = key;
158                         key = tdb_nextkey(pdb_tdb, key);
159                         SAFE_FREE(old_key.dptr);
160                 }
161                 
162         }
163
164         pdb_free_sam(&user);
165         
166         /* upgrade finished */
167         tdb_store_int32(pdb_tdb, vstring, TDBSAM_VERSION);
168         tdb_unlock_bystring(pdb_tdb, vstring);
169
170         return(True);   
171 }
172
173 /**
174  * Open the TDB passwd database, check version and convert it if needed.
175  * @param name filename of the tdbsam file.
176  * @param open_flags file access mode.
177  * @return a TDB_CONTEXT handle on the tdbsam file.
178  **/
179
180 static TDB_CONTEXT * tdbsam_tdbopen (const char *name, int open_flags)
181 {
182         TDB_CONTEXT     *pdb_tdb;
183         tdbsamver_t     version;
184         
185         /* Try to open tdb passwd */
186         if (!(pdb_tdb = tdb_open_log(name, 0, TDB_DEFAULT, 
187                                      open_flags, 0600))) {
188                 DEBUG(0, ("Unable to open/create TDB passwd\n"));
189                 return NULL;
190         }
191
192         /* Check the version */
193         version = (tdbsamver_t) tdb_fetch_int32(pdb_tdb, 
194                                                 TDBSAM_VERSION_STRING);
195         if (version == -1)
196                 version = 0;    /* Version not found, assume version 0 */
197         
198         /* Compare the version */
199         if (version > TDBSAM_VERSION) {
200                 /* Version more recent than the latest known */ 
201                 DEBUG(0, ("TDBSAM version unknown: %d\n", version));
202                 tdb_close(pdb_tdb);
203                 pdb_tdb = NULL;
204         } 
205         else if (version < TDBSAM_VERSION) {
206                 /* Older version, must be converted */
207                 DEBUG(1, ("TDBSAM version too old (%d), trying to convert it.\n", version));
208                 
209                 /* Reopen the pdb file with read-write access if needed */
210                 if (!(open_flags & O_RDWR)) {
211                         DEBUG(10, ("tdbsam_tdbopen: TDB file opened with read only access, reopen it with read-write access.\n"));
212                         tdb_close(pdb_tdb);
213                         pdb_tdb = tdb_open_log(name, 0, TDB_DEFAULT, (open_flags & 07777770) | O_RDWR, 0600);
214                 }
215                 
216                 /* Convert */
217                 if (!tdbsam_convert(pdb_tdb, version)){
218                         DEBUG(0, ("tdbsam_tdbopen: Error when trying to convert tdbsam: %s\n",name));
219                         tdb_close(pdb_tdb);
220                         pdb_tdb = NULL;
221                 } else {
222                         DEBUG(1, ("TDBSAM converted successfully.\n"));
223                 }
224
225                 /* Reopen the pdb file as it must be */
226                 if (!(open_flags & O_RDWR)) {
227                         tdb_close(pdb_tdb);
228                         pdb_tdb = tdb_open_log(name, 0, TDB_DEFAULT, open_flags, 0600);
229                 }
230         }
231         
232         return pdb_tdb;
233 }
234
235 /*****************************************************************************
236  Utility functions to close the tdb sam database
237  ****************************************************************************/
238
239 static void tdbsam_tdbclose ( struct tdbsam_privates *state )
240 {
241         if ( !state )
242                 return;
243                 
244         if ( state->passwd_tdb ) {
245                 tdb_close( state->passwd_tdb );
246                 state->passwd_tdb = NULL;
247         }
248         
249         return;
250                 
251 }
252
253 /****************************************************************************
254  creates a list of user keys
255 ****************************************************************************/
256
257 static int tdbsam_traverse_setpwent(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
258 {
259         const char *prefix = USERPREFIX;
260         int  prefixlen = strlen (prefix);
261         struct pwent_list *ptr;
262         
263         if ( strncmp(key.dptr, prefix, prefixlen) == 0 ) {
264                 if ( !(ptr=(struct pwent_list*)malloc(sizeof(struct pwent_list))) ) {
265                         DEBUG(0,("tdbsam_traverse_setpwent: Failed to malloc new entry for list\n"));
266                         
267                         /* just return 0 and let the traversal continue */
268                         return 0;
269                 }
270                 ZERO_STRUCTP(ptr);
271                 
272                 /* save a copy of the key */
273                 
274                 ptr->key.dptr = memdup( key.dptr, key.dsize );
275                 ptr->key.dsize = key.dsize;
276                 
277                 DLIST_ADD( tdbsam_pwent_list, ptr );
278         
279         }
280         
281         
282         return 0;
283 }
284
285 /***************************************************************
286  Open the TDB passwd database for SAM account enumeration.
287  Save a list of user keys for iteration.
288 ****************************************************************/
289
290 static NTSTATUS tdbsam_setsampwent(struct pdb_methods *my_methods, BOOL update)
291 {
292         uint32 flags = update ? (O_RDWR|O_CREAT) : O_RDONLY;
293         
294         struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)my_methods->private_data;
295         
296         if ( !(tdb_state->passwd_tdb = tdbsam_tdbopen(tdb_state->tdbsam_location, flags )) ) 
297                 return NT_STATUS_UNSUCCESSFUL;
298
299         tdb_traverse( tdb_state->passwd_tdb, tdbsam_traverse_setpwent, NULL );
300         
301         return NT_STATUS_OK;
302 }
303
304
305 /***************************************************************
306  End enumeration of the TDB passwd list.
307 ****************************************************************/
308
309 static void tdbsam_endsampwent(struct pdb_methods *my_methods)
310 {
311         struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)my_methods->private_data;
312         struct pwent_list *ptr, *ptr_next;
313         
314         tdbsam_tdbclose( tdb_state );
315         
316         /* clear out any remaining entries in the list */
317         
318         for ( ptr=tdbsam_pwent_list; ptr; ptr = ptr_next ) {
319                 ptr_next = ptr->next;
320                 DLIST_REMOVE( tdbsam_pwent_list, ptr );
321                 SAFE_FREE( ptr->key.dptr);
322                 SAFE_FREE( ptr );
323         }
324         
325         DEBUG(7, ("endtdbpwent: closed sam database.\n"));
326 }
327
328 /*****************************************************************
329  Get one SAM_ACCOUNT from the TDB (next in line)
330 *****************************************************************/
331
332 static NTSTATUS tdbsam_getsampwent(struct pdb_methods *my_methods, SAM_ACCOUNT *user)
333 {
334         NTSTATUS                nt_status = NT_STATUS_UNSUCCESSFUL;
335         struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)my_methods->private_data;
336         TDB_DATA                data;
337         struct pwent_list       *pkey;
338
339         if ( !user ) {
340                 DEBUG(0,("tdbsam_getsampwent: SAM_ACCOUNT is NULL.\n"));
341                 return nt_status;
342         }
343
344         if ( !tdbsam_pwent_list ) {
345                 DEBUG(4,("tdbsam_getsampwent: end of list\n"));
346                 tdbsam_tdbclose( tdb_state );
347                 return nt_status;
348         }
349         
350         if ( !tdb_state->passwd_tdb ) {
351                 if ( !(tdb_state->passwd_tdb = tdbsam_tdbopen(tdb_state->tdbsam_location, O_RDONLY)) )
352                         return nt_status;
353         }
354
355         /* pull the next entry */
356                 
357         pkey = tdbsam_pwent_list;
358         DLIST_REMOVE( tdbsam_pwent_list, pkey );
359         
360         data = tdb_fetch(tdb_state->passwd_tdb, pkey->key);
361
362         SAFE_FREE( pkey->key.dptr);
363         SAFE_FREE( pkey);
364         
365         if (!data.dptr) {
366                 DEBUG(5,("pdb_getsampwent: database entry not found.  Was the user deleted?\n"));
367                 return nt_status;
368         }
369   
370         if (!init_sam_from_buffer(user, (unsigned char *)data.dptr, data.dsize)) {
371                 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
372         }
373         
374         SAFE_FREE( data.dptr );
375         
376
377         return NT_STATUS_OK;
378 }
379
380 /******************************************************************
381  Lookup a name in the SAM TDB
382 ******************************************************************/
383
384 static NTSTATUS tdbsam_getsampwnam (struct pdb_methods *my_methods, SAM_ACCOUNT *user, const char *sname)
385 {
386         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
387         struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)my_methods->private_data;
388         TDB_CONTEXT     *pwd_tdb;
389         TDB_DATA        data, key;
390         fstring         keystr;
391         fstring         name;
392
393         if ( !user ) {
394                 DEBUG(0,("pdb_getsampwnam: SAM_ACCOUNT is NULL.\n"));
395                 return nt_status;
396         }
397
398         /* Data is stored in all lower-case */
399         fstrcpy(name, sname);
400         strlower_m(name);
401
402         /* set search key */
403         slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
404         key.dptr = keystr;
405         key.dsize = strlen(keystr) + 1;
406
407         /* open the accounts TDB */
408         if (!(pwd_tdb = tdbsam_tdbopen(tdb_state->tdbsam_location, O_RDONLY))) {
409         
410                 if (errno == ENOENT) {
411                         /*
412                          * TDB file doesn't exist, so try to create new one. This is useful to avoid
413                          * confusing error msg when adding user account first time
414                          */
415                         if (!(pwd_tdb = tdbsam_tdbopen(tdb_state->tdbsam_location, O_CREAT ))) {
416                                 DEBUG(0, ("pdb_getsampwnam: TDB passwd (%s) did not exist. File successfully created.\n",
417                                           tdb_state->tdbsam_location));
418                         } else {
419                                 DEBUG(0, ("pdb_getsampwnam: TDB passwd (%s) does not exist. Couldn't create new one. Error was: %s\n",
420                                           tdb_state->tdbsam_location, strerror(errno)));
421                         }
422                         
423                         /* requested user isn't there anyway */
424                         nt_status = NT_STATUS_NO_SUCH_USER;
425                         return nt_status;
426                 }
427                 DEBUG(0, ("pdb_getsampwnam: Unable to open TDB passwd (%s)!\n", tdb_state->tdbsam_location));
428                 return nt_status;
429         }
430
431         /* get the record */
432         data = tdb_fetch(pwd_tdb, key);
433         if (!data.dptr) {
434                 DEBUG(5,("pdb_getsampwnam (TDB): error fetching database.\n"));
435                 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
436                 DEBUGADD(5, (" Key: %s\n", keystr));
437                 tdb_close(pwd_tdb);
438                 return nt_status;
439         }
440   
441         /* unpack the buffer */
442         if (!init_sam_from_buffer(user, (unsigned char *)data.dptr, data.dsize)) {
443                 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
444                 SAFE_FREE(data.dptr);
445                 tdb_close(pwd_tdb);
446                 return nt_status;
447         }
448         SAFE_FREE(data.dptr);
449
450         /* no further use for database, close it now */
451         tdb_close(pwd_tdb);
452         
453         return NT_STATUS_OK;
454 }
455
456 /***************************************************************************
457  Search by rid
458  **************************************************************************/
459
460 static NTSTATUS tdbsam_getsampwrid (struct pdb_methods *my_methods, SAM_ACCOUNT *user, uint32 rid)
461 {
462         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
463         struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)my_methods->private_data;
464         TDB_CONTEXT             *pwd_tdb;
465         TDB_DATA                data, key;
466         fstring                 keystr;
467         fstring                 name;
468         
469         if (user==NULL) {
470                 DEBUG(0,("pdb_getsampwrid: SAM_ACCOUNT is NULL.\n"));
471                 return nt_status;
472         }
473
474         /* set search key */
475         slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
476         key.dptr = keystr;
477         key.dsize = strlen (keystr) + 1;
478
479         /* open the accounts TDB */
480         if (!(pwd_tdb = tdbsam_tdbopen(tdb_state->tdbsam_location, O_RDONLY))) {
481                 DEBUG(0, ("pdb_getsampwrid: Unable to open TDB rid database!\n"));
482                 return nt_status;
483         }
484
485         /* get the record */
486         data = tdb_fetch (pwd_tdb, key);
487         if (!data.dptr) {
488                 DEBUG(5,("pdb_getsampwrid (TDB): error looking up RID %d by key %s.\n", rid, keystr));
489                 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
490                 tdb_close (pwd_tdb);
491                 return nt_status;
492         }
493
494
495         fstrcpy(name, data.dptr);
496         SAFE_FREE(data.dptr);
497         
498         tdb_close (pwd_tdb);
499         
500         return tdbsam_getsampwnam (my_methods, user, name);
501 }
502
503 static NTSTATUS tdbsam_getsampwsid(struct pdb_methods *my_methods, SAM_ACCOUNT * user, const DOM_SID *sid)
504 {
505         uint32 rid;
506         if (!sid_peek_check_rid(get_global_sam_sid(), sid, &rid))
507                 return NT_STATUS_UNSUCCESSFUL;
508         return tdbsam_getsampwrid(my_methods, user, rid);
509 }
510
511 /***************************************************************************
512  Delete a SAM_ACCOUNT
513 ****************************************************************************/
514
515 static NTSTATUS tdbsam_delete_sam_account(struct pdb_methods *my_methods, SAM_ACCOUNT *sam_pass)
516 {
517         NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
518         struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)my_methods->private_data;
519         TDB_CONTEXT     *pwd_tdb;
520         TDB_DATA        key;
521         fstring         keystr;
522         uint32          rid;
523         fstring         name;
524         
525         fstrcpy(name, pdb_get_username(sam_pass));
526         strlower_m(name);
527         
528         /* open the TDB */
529         if (!(pwd_tdb = tdbsam_tdbopen(tdb_state->tdbsam_location, O_RDWR))) {
530                 DEBUG(0, ("Unable to open TDB passwd!"));
531                 return nt_status;
532         }
533   
534         /* set the search key */
535         slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
536         key.dptr = keystr;
537         key.dsize = strlen (keystr) + 1;
538         
539         rid = pdb_get_user_rid(sam_pass);
540
541         /* it's outaa here!  8^) */
542         if (tdb_delete(pwd_tdb, key) != TDB_SUCCESS) {
543                 DEBUG(5, ("Error deleting entry from tdb passwd database!\n"));
544                 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
545                 tdb_close(pwd_tdb); 
546                 return nt_status;
547         }       
548
549         /* delete also the RID key */
550
551         /* set the search key */
552         slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
553         key.dptr = keystr;
554         key.dsize = strlen (keystr) + 1;
555
556         /* it's outaa here!  8^) */
557         if (tdb_delete(pwd_tdb, key) != TDB_SUCCESS) {
558                 DEBUG(5, ("Error deleting entry from tdb rid database!\n"));
559                 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
560                 tdb_close(pwd_tdb); 
561                 return nt_status;
562         }
563         
564         tdb_close(pwd_tdb);
565         
566         return NT_STATUS_OK;
567 }
568
569 /***************************************************************************
570  Update the TDB SAM
571 ****************************************************************************/
572
573 static BOOL tdb_update_sam(struct pdb_methods *my_methods, SAM_ACCOUNT* newpwd, int flag)
574 {
575         struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)my_methods->private_data;
576         TDB_CONTEXT     *pwd_tdb = NULL;
577         TDB_DATA        key, data;
578         uint8           *buf = NULL;
579         fstring         keystr;
580         fstring         name;
581         BOOL            ret = True;
582         uint32          user_rid;
583
584         /* invalidate the existing TDB iterator if it is open */
585         
586         if (tdb_state->passwd_tdb) {
587                 tdb_close(tdb_state->passwd_tdb);
588                 tdb_state->passwd_tdb = NULL;
589         }
590
591         /* open the account TDB passwd*/
592         
593         pwd_tdb = tdbsam_tdbopen(tdb_state->tdbsam_location, O_RDWR | O_CREAT);
594         
595         if (!pwd_tdb) {
596                 DEBUG(0, ("tdb_update_sam: Unable to open TDB passwd (%s)!\n", 
597                         tdb_state->tdbsam_location));
598                 return False;
599         }
600
601         if (!pdb_get_group_rid(newpwd)) {
602                 DEBUG (0,("tdb_update_sam: Failing to store a SAM_ACCOUNT for [%s] without a primary group RID\n",
603                         pdb_get_username(newpwd)));
604                 ret = False;
605                 goto done;
606         }
607
608         if ( !(user_rid = pdb_get_user_rid(newpwd)) ) {
609                 DEBUG(0,("tdb_update_sam: SAM_ACCOUNT (%s) with no RID!\n", pdb_get_username(newpwd)));
610                 ret = False;
611                 goto done;
612         }
613
614         /* copy the SAM_ACCOUNT struct into a BYTE buffer for storage */
615         if ((data.dsize=init_buffer_from_sam (&buf, newpwd, False)) == -1) {
616                 DEBUG(0,("tdb_update_sam: ERROR - Unable to copy SAM_ACCOUNT info BYTE buffer!\n"));
617                 ret = False;
618                 goto done;
619         }
620         data.dptr = (char *)buf;
621
622         fstrcpy(name, pdb_get_username(newpwd));
623         strlower_m(name);
624         
625         DEBUG(5, ("Storing %saccount %s with RID %d\n", flag == TDB_INSERT ? "(new) " : "", name, user_rid));
626
627         /* setup the USER index key */
628         slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
629         key.dptr = keystr;
630         key.dsize = strlen(keystr) + 1;
631
632         /* add the account */
633         if (tdb_store(pwd_tdb, key, data, flag) != TDB_SUCCESS) {
634                 DEBUG(0, ("Unable to modify passwd TDB!"));
635                 DEBUGADD(0, (" Error: %s", tdb_errorstr(pwd_tdb)));
636                 DEBUGADD(0, (" occured while storing the main record (%s)\n", keystr));
637                 ret = False;
638                 goto done;
639         }
640         
641         /* setup RID data */
642         data.dsize = strlen(name) + 1;
643         data.dptr = name;
644
645         /* setup the RID index key */
646         slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, user_rid);
647         key.dptr = keystr;
648         key.dsize = strlen (keystr) + 1;
649         
650         /* add the reference */
651         if (tdb_store(pwd_tdb, key, data, flag) != TDB_SUCCESS) {
652                 DEBUG(0, ("Unable to modify TDB passwd !"));
653                 DEBUGADD(0, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
654                 DEBUGADD(0, (" occured while storing the RID index (%s)\n", keystr));
655                 ret = False;
656                 goto done;
657         }
658
659 done:   
660         /* cleanup */
661         tdb_close (pwd_tdb);
662         SAFE_FREE(buf);
663         
664         return (ret);   
665 }
666
667 /***************************************************************************
668  Modifies an existing SAM_ACCOUNT
669 ****************************************************************************/
670
671 static NTSTATUS tdbsam_update_sam_account (struct pdb_methods *my_methods, SAM_ACCOUNT *newpwd)
672 {
673         if (tdb_update_sam(my_methods, newpwd, TDB_MODIFY))
674                 return NT_STATUS_OK;
675         else
676                 return NT_STATUS_UNSUCCESSFUL;
677 }
678
679 /***************************************************************************
680  Adds an existing SAM_ACCOUNT
681 ****************************************************************************/
682
683 static NTSTATUS tdbsam_add_sam_account (struct pdb_methods *my_methods, SAM_ACCOUNT *newpwd)
684 {
685         if (tdb_update_sam(my_methods, newpwd, TDB_INSERT))
686                 return NT_STATUS_OK;
687         else
688                 return NT_STATUS_UNSUCCESSFUL;
689 }
690
691 static void free_private_data(void **vp) 
692 {
693         struct tdbsam_privates **tdb_state = (struct tdbsam_privates **)vp;
694         tdbsam_tdbclose(*tdb_state);
695         *tdb_state = NULL;
696
697         /* No need to free any further, as it is talloc()ed */
698 }
699
700
701 static NTSTATUS pdb_init_tdbsam(PDB_CONTEXT *pdb_context, PDB_METHODS **pdb_method, const char *location)
702 {
703         NTSTATUS nt_status;
704         struct tdbsam_privates *tdb_state;
705
706         if (!NT_STATUS_IS_OK(nt_status = make_pdb_methods(pdb_context->mem_ctx, pdb_method))) {
707                 return nt_status;
708         }
709
710         (*pdb_method)->name = "tdbsam";
711
712         (*pdb_method)->setsampwent = tdbsam_setsampwent;
713         (*pdb_method)->endsampwent = tdbsam_endsampwent;
714         (*pdb_method)->getsampwent = tdbsam_getsampwent;
715         (*pdb_method)->getsampwnam = tdbsam_getsampwnam;
716         (*pdb_method)->getsampwsid = tdbsam_getsampwsid;
717         (*pdb_method)->add_sam_account = tdbsam_add_sam_account;
718         (*pdb_method)->update_sam_account = tdbsam_update_sam_account;
719         (*pdb_method)->delete_sam_account = tdbsam_delete_sam_account;
720
721         tdb_state = talloc_zero(pdb_context->mem_ctx, sizeof(struct tdbsam_privates));
722
723         if (!tdb_state) {
724                 DEBUG(0, ("talloc() failed for tdbsam private_data!\n"));
725                 return NT_STATUS_NO_MEMORY;
726         }
727
728         if (location) {
729                 tdb_state->tdbsam_location = talloc_strdup(pdb_context->mem_ctx, location);
730         } else {
731                 pstring tdbfile;
732                 get_private_directory(tdbfile);
733                 pstrcat(tdbfile, "/");
734                 pstrcat(tdbfile, PASSDB_FILE_NAME);
735                 tdb_state->tdbsam_location = talloc_strdup(pdb_context->mem_ctx, tdbfile);
736         }
737
738         (*pdb_method)->private_data = tdb_state;
739
740         (*pdb_method)->free_private_data = free_private_data;
741
742         return NT_STATUS_OK;
743 }
744
745 NTSTATUS pdb_tdbsam_init(void)
746 {
747         return smb_register_passdb(PASSDB_INTERFACE_VERSION, "tdbsam", pdb_init_tdbsam);
748 }
749