r4231: commiting changes to 3.0.10
[Samba.git] / source / passdb / pdb_tdb.c
blobc792d229b9af49ed45a8605b7c7b90fb39b86e41
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.
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.
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.
25 #include "includes.h"
27 #if 0 /* when made a module use this */
29 static int tdbsam_debug_level = DBGC_ALL;
30 #undef DBGC_CLASS
31 #define DBGC_CLASS tdbsam_debug_level
33 #else
35 #undef DBGC_CLASS
36 #define DBGC_CLASS DBGC_PASSDB
38 #endif
40 #define TDBSAM_VERSION 2 /* 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
47 struct tdbsam_privates {
48 TDB_CONTEXT *passwd_tdb;
50 /* retrive-once info */
51 const char *tdbsam_location;
54 struct pwent_list {
55 struct pwent_list *prev, *next;
56 TDB_DATA key;
58 static struct pwent_list *tdbsam_pwent_list;
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 **/
69 static BOOL tdbsam_convert(TDB_CONTEXT *pdb_tdb, tdbsamver_t from)
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;
78 if (pdb_tdb == NULL) {
79 DEBUG(0,("tdbsam_convert: Bad TDB Context pointer.\n"));
80 return False;
83 /* handle a Samba upgrade */
84 tdb_lock_bystring(pdb_tdb, vstring, 0);
86 if (!NT_STATUS_IS_OK(pdb_init_sam(&user))) {
87 DEBUG(0,("tdbsam_convert: cannot initialized a SAM_ACCOUNT.\n"));
88 return False;
91 /* Enumerate all records and convert them */
92 key = tdb_firstkey(pdb_tdb);
94 while (key.dptr) {
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);
104 if (key.dptr) {
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;
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;
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 case 2:
129 ret = init_sam_from_buffer_v2(user, (uint8 *)data.dptr, data.dsize);
130 break;
131 default:
132 /* unknown tdbsam version */
133 ret = False;
135 if (!ret) {
136 DEBUG(0,("tdbsam_convert: Bad SAM_ACCOUNT entry returned from TDB (key:%s) (version:%d)\n", key.dptr, from));
137 SAFE_FREE(data.dptr);
138 return False;
141 /* We're finished with the old data. */
142 SAFE_FREE(data.dptr);
144 /* pack from the buffer into the new format */
145 DEBUG(10,("tdbsam_convert: Try packing a record (key:%s) (version:%d)\n", key.dptr, from));
146 if ((data.dsize=init_buffer_from_sam (&buf, user, False)) == -1) {
147 DEBUG(0,("tdbsam_convert: cannot pack the SAM_ACCOUNT into the new format\n"));
148 SAFE_FREE(data.dptr);
149 return False;
151 data.dptr = (char *)buf;
153 /* Store the buffer inside the TDBSAM */
154 if (tdb_store(pdb_tdb, key, data, TDB_MODIFY) != TDB_SUCCESS) {
155 DEBUG(0,("tdbsam_convert: cannot store the SAM_ACCOUNT (key:%s) in new format\n",key.dptr));
156 SAFE_FREE(data.dptr);
157 return False;
160 SAFE_FREE(data.dptr);
162 /* increment to next in line */
163 old_key = key;
164 key = tdb_nextkey(pdb_tdb, key);
165 SAFE_FREE(old_key.dptr);
170 pdb_free_sam(&user);
172 /* upgrade finished */
173 tdb_store_int32(pdb_tdb, vstring, TDBSAM_VERSION);
174 tdb_unlock_bystring(pdb_tdb, vstring);
176 return(True);
180 * Open the TDB passwd database, check version and convert it if needed.
181 * @param name filename of the tdbsam file.
182 * @param open_flags file access mode.
183 * @return a TDB_CONTEXT handle on the tdbsam file.
186 static TDB_CONTEXT * tdbsam_tdbopen (const char *name, int open_flags)
188 TDB_CONTEXT *pdb_tdb;
189 tdbsamver_t version;
191 /* Try to open tdb passwd */
192 if (!(pdb_tdb = tdb_open_log(name, 0, TDB_DEFAULT,
193 open_flags, 0600))) {
194 DEBUG(0, ("Unable to open/create TDB passwd\n"));
195 return NULL;
198 /* Check the version */
199 version = (tdbsamver_t) tdb_fetch_int32(pdb_tdb,
200 TDBSAM_VERSION_STRING);
201 if (version == -1)
202 version = 0; /* Version not found, assume version 0 */
204 /* Compare the version */
205 if (version > TDBSAM_VERSION) {
206 /* Version more recent than the latest known */
207 DEBUG(0, ("TDBSAM version unknown: %d\n", version));
208 tdb_close(pdb_tdb);
209 pdb_tdb = NULL;
211 else if (version < TDBSAM_VERSION) {
212 /* Older version, must be converted */
213 DEBUG(1, ("TDBSAM version too old (%d), trying to convert it.\n", version));
215 /* Reopen the pdb file with read-write access if needed */
216 if (!(open_flags & O_RDWR)) {
217 DEBUG(10, ("tdbsam_tdbopen: TDB file opened with read only access, reopen it with read-write access.\n"));
218 tdb_close(pdb_tdb);
219 pdb_tdb = tdb_open_log(name, 0, TDB_DEFAULT, (open_flags & 07777770) | O_RDWR, 0600);
222 /* Convert */
223 if (!tdbsam_convert(pdb_tdb, version)){
224 DEBUG(0, ("tdbsam_tdbopen: Error when trying to convert tdbsam: %s\n",name));
225 tdb_close(pdb_tdb);
226 pdb_tdb = NULL;
227 } else {
228 DEBUG(1, ("TDBSAM converted successfully.\n"));
231 /* Reopen the pdb file as it must be */
232 if (!(open_flags & O_RDWR)) {
233 tdb_close(pdb_tdb);
234 pdb_tdb = tdb_open_log(name, 0, TDB_DEFAULT, open_flags, 0600);
238 return pdb_tdb;
241 /*****************************************************************************
242 Utility functions to close the tdb sam database
243 ****************************************************************************/
245 static void tdbsam_tdbclose ( struct tdbsam_privates *state )
247 if ( !state )
248 return;
250 if ( state->passwd_tdb ) {
251 tdb_close( state->passwd_tdb );
252 state->passwd_tdb = NULL;
255 return;
259 /****************************************************************************
260 creates a list of user keys
261 ****************************************************************************/
263 static int tdbsam_traverse_setpwent(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
265 const char *prefix = USERPREFIX;
266 int prefixlen = strlen (prefix);
267 struct pwent_list *ptr;
269 if ( strncmp(key.dptr, prefix, prefixlen) == 0 ) {
270 if ( !(ptr=SMB_MALLOC_P(struct pwent_list)) ) {
271 DEBUG(0,("tdbsam_traverse_setpwent: Failed to malloc new entry for list\n"));
273 /* just return 0 and let the traversal continue */
274 return 0;
276 ZERO_STRUCTP(ptr);
278 /* save a copy of the key */
280 ptr->key.dptr = memdup( key.dptr, key.dsize );
281 ptr->key.dsize = key.dsize;
283 DLIST_ADD( tdbsam_pwent_list, ptr );
288 return 0;
291 /***************************************************************
292 Open the TDB passwd database for SAM account enumeration.
293 Save a list of user keys for iteration.
294 ****************************************************************/
296 static NTSTATUS tdbsam_setsampwent(struct pdb_methods *my_methods, BOOL update)
298 uint32 flags = update ? (O_RDWR|O_CREAT) : O_RDONLY;
300 struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)my_methods->private_data;
302 if ( !(tdb_state->passwd_tdb = tdbsam_tdbopen(tdb_state->tdbsam_location, flags )) )
303 return NT_STATUS_UNSUCCESSFUL;
305 tdb_traverse( tdb_state->passwd_tdb, tdbsam_traverse_setpwent, NULL );
307 return NT_STATUS_OK;
311 /***************************************************************
312 End enumeration of the TDB passwd list.
313 ****************************************************************/
315 static void tdbsam_endsampwent(struct pdb_methods *my_methods)
317 struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)my_methods->private_data;
318 struct pwent_list *ptr, *ptr_next;
320 tdbsam_tdbclose( tdb_state );
322 /* clear out any remaining entries in the list */
324 for ( ptr=tdbsam_pwent_list; ptr; ptr = ptr_next ) {
325 ptr_next = ptr->next;
326 DLIST_REMOVE( tdbsam_pwent_list, ptr );
327 SAFE_FREE( ptr->key.dptr);
328 SAFE_FREE( ptr );
331 DEBUG(7, ("endtdbpwent: closed sam database.\n"));
334 /*****************************************************************
335 Get one SAM_ACCOUNT from the TDB (next in line)
336 *****************************************************************/
338 static NTSTATUS tdbsam_getsampwent(struct pdb_methods *my_methods, SAM_ACCOUNT *user)
340 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
341 struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)my_methods->private_data;
342 TDB_DATA data;
343 struct pwent_list *pkey;
345 if ( !user ) {
346 DEBUG(0,("tdbsam_getsampwent: SAM_ACCOUNT is NULL.\n"));
347 return nt_status;
350 if ( !tdbsam_pwent_list ) {
351 DEBUG(4,("tdbsam_getsampwent: end of list\n"));
352 tdbsam_tdbclose( tdb_state );
353 return nt_status;
356 if ( !tdb_state->passwd_tdb ) {
357 if ( !(tdb_state->passwd_tdb = tdbsam_tdbopen(tdb_state->tdbsam_location, O_RDONLY)) )
358 return nt_status;
361 /* pull the next entry */
363 pkey = tdbsam_pwent_list;
364 DLIST_REMOVE( tdbsam_pwent_list, pkey );
366 data = tdb_fetch(tdb_state->passwd_tdb, pkey->key);
368 SAFE_FREE( pkey->key.dptr);
369 SAFE_FREE( pkey);
371 if (!data.dptr) {
372 DEBUG(5,("pdb_getsampwent: database entry not found. Was the user deleted?\n"));
373 return nt_status;
376 if (!init_sam_from_buffer(user, (unsigned char *)data.dptr, data.dsize)) {
377 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
380 SAFE_FREE( data.dptr );
383 return NT_STATUS_OK;
386 /******************************************************************
387 Lookup a name in the SAM TDB
388 ******************************************************************/
390 static NTSTATUS tdbsam_getsampwnam (struct pdb_methods *my_methods, SAM_ACCOUNT *user, const char *sname)
392 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
393 struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)my_methods->private_data;
394 TDB_CONTEXT *pwd_tdb;
395 TDB_DATA data, key;
396 fstring keystr;
397 fstring name;
399 if ( !user ) {
400 DEBUG(0,("pdb_getsampwnam: SAM_ACCOUNT is NULL.\n"));
401 return nt_status;
404 /* Data is stored in all lower-case */
405 fstrcpy(name, sname);
406 strlower_m(name);
408 /* set search key */
409 slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
410 key.dptr = keystr;
411 key.dsize = strlen(keystr) + 1;
413 /* open the accounts TDB */
414 if (!(pwd_tdb = tdbsam_tdbopen(tdb_state->tdbsam_location, O_RDONLY))) {
416 if (errno == ENOENT) {
418 * TDB file doesn't exist, so try to create new one. This is useful to avoid
419 * confusing error msg when adding user account first time
421 if (!(pwd_tdb = tdbsam_tdbopen(tdb_state->tdbsam_location, O_CREAT ))) {
422 DEBUG(0, ("pdb_getsampwnam: TDB passwd (%s) did not exist. File successfully created.\n",
423 tdb_state->tdbsam_location));
424 } else {
425 DEBUG(0, ("pdb_getsampwnam: TDB passwd (%s) does not exist. Couldn't create new one. Error was: %s\n",
426 tdb_state->tdbsam_location, strerror(errno)));
429 /* requested user isn't there anyway */
430 nt_status = NT_STATUS_NO_SUCH_USER;
431 return nt_status;
433 DEBUG(0, ("pdb_getsampwnam: Unable to open TDB passwd (%s)!\n", tdb_state->tdbsam_location));
434 return nt_status;
437 /* get the record */
438 data = tdb_fetch(pwd_tdb, key);
439 if (!data.dptr) {
440 DEBUG(5,("pdb_getsampwnam (TDB): error fetching database.\n"));
441 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
442 DEBUGADD(5, (" Key: %s\n", keystr));
443 tdb_close(pwd_tdb);
444 return nt_status;
447 /* unpack the buffer */
448 if (!init_sam_from_buffer(user, (unsigned char *)data.dptr, data.dsize)) {
449 DEBUG(0,("pdb_getsampwent: Bad SAM_ACCOUNT entry returned from TDB!\n"));
450 SAFE_FREE(data.dptr);
451 tdb_close(pwd_tdb);
452 return nt_status;
454 SAFE_FREE(data.dptr);
456 /* no further use for database, close it now */
457 tdb_close(pwd_tdb);
459 return NT_STATUS_OK;
462 /***************************************************************************
463 Search by rid
464 **************************************************************************/
466 static NTSTATUS tdbsam_getsampwrid (struct pdb_methods *my_methods, SAM_ACCOUNT *user, uint32 rid)
468 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
469 struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)my_methods->private_data;
470 TDB_CONTEXT *pwd_tdb;
471 TDB_DATA data, key;
472 fstring keystr;
473 fstring name;
475 if (user==NULL) {
476 DEBUG(0,("pdb_getsampwrid: SAM_ACCOUNT is NULL.\n"));
477 return nt_status;
480 /* set search key */
481 slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
482 key.dptr = keystr;
483 key.dsize = strlen (keystr) + 1;
485 /* open the accounts TDB */
486 if (!(pwd_tdb = tdbsam_tdbopen(tdb_state->tdbsam_location, O_RDONLY))) {
487 DEBUG(0, ("pdb_getsampwrid: Unable to open TDB rid database!\n"));
488 return nt_status;
491 /* get the record */
492 data = tdb_fetch (pwd_tdb, key);
493 if (!data.dptr) {
494 DEBUG(5,("pdb_getsampwrid (TDB): error looking up RID %d by key %s.\n", rid, keystr));
495 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
496 tdb_close (pwd_tdb);
497 return nt_status;
501 fstrcpy(name, data.dptr);
502 SAFE_FREE(data.dptr);
504 tdb_close (pwd_tdb);
506 return tdbsam_getsampwnam (my_methods, user, name);
509 static NTSTATUS tdbsam_getsampwsid(struct pdb_methods *my_methods, SAM_ACCOUNT * user, const DOM_SID *sid)
511 uint32 rid;
512 if (!sid_peek_check_rid(get_global_sam_sid(), sid, &rid))
513 return NT_STATUS_UNSUCCESSFUL;
514 return tdbsam_getsampwrid(my_methods, user, rid);
517 /***************************************************************************
518 Delete a SAM_ACCOUNT
519 ****************************************************************************/
521 static NTSTATUS tdbsam_delete_sam_account(struct pdb_methods *my_methods, SAM_ACCOUNT *sam_pass)
523 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
524 struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)my_methods->private_data;
525 TDB_CONTEXT *pwd_tdb;
526 TDB_DATA key;
527 fstring keystr;
528 uint32 rid;
529 fstring name;
531 fstrcpy(name, pdb_get_username(sam_pass));
532 strlower_m(name);
534 /* open the TDB */
535 if (!(pwd_tdb = tdbsam_tdbopen(tdb_state->tdbsam_location, O_RDWR))) {
536 DEBUG(0, ("Unable to open TDB passwd!"));
537 return nt_status;
540 /* set the search key */
541 slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
542 key.dptr = keystr;
543 key.dsize = strlen (keystr) + 1;
545 rid = pdb_get_user_rid(sam_pass);
547 /* it's outaa here! 8^) */
548 if (tdb_delete(pwd_tdb, key) != TDB_SUCCESS) {
549 DEBUG(5, ("Error deleting entry from tdb passwd database!\n"));
550 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
551 tdb_close(pwd_tdb);
552 return nt_status;
555 /* delete also the RID key */
557 /* set the search key */
558 slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, rid);
559 key.dptr = keystr;
560 key.dsize = strlen (keystr) + 1;
562 /* it's outaa here! 8^) */
563 if (tdb_delete(pwd_tdb, key) != TDB_SUCCESS) {
564 DEBUG(5, ("Error deleting entry from tdb rid database!\n"));
565 DEBUGADD(5, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
566 tdb_close(pwd_tdb);
567 return nt_status;
570 tdb_close(pwd_tdb);
572 return NT_STATUS_OK;
575 /***************************************************************************
576 Update the TDB SAM
577 ****************************************************************************/
579 static BOOL tdb_update_sam(struct pdb_methods *my_methods, SAM_ACCOUNT* newpwd, int flag)
581 struct tdbsam_privates *tdb_state = (struct tdbsam_privates *)my_methods->private_data;
582 TDB_CONTEXT *pwd_tdb = NULL;
583 TDB_DATA key, data;
584 uint8 *buf = NULL;
585 fstring keystr;
586 fstring name;
587 BOOL ret = True;
588 uint32 user_rid;
590 /* invalidate the existing TDB iterator if it is open */
592 if (tdb_state->passwd_tdb) {
593 tdb_close(tdb_state->passwd_tdb);
594 tdb_state->passwd_tdb = NULL;
597 /* open the account TDB passwd*/
599 pwd_tdb = tdbsam_tdbopen(tdb_state->tdbsam_location, O_RDWR | O_CREAT);
601 if (!pwd_tdb) {
602 DEBUG(0, ("tdb_update_sam: Unable to open TDB passwd (%s)!\n",
603 tdb_state->tdbsam_location));
604 return False;
607 if (!pdb_get_group_rid(newpwd)) {
608 DEBUG (0,("tdb_update_sam: Failing to store a SAM_ACCOUNT for [%s] without a primary group RID\n",
609 pdb_get_username(newpwd)));
610 ret = False;
611 goto done;
614 if ( !(user_rid = pdb_get_user_rid(newpwd)) ) {
615 DEBUG(0,("tdb_update_sam: SAM_ACCOUNT (%s) with no RID!\n", pdb_get_username(newpwd)));
616 ret = False;
617 goto done;
620 /* copy the SAM_ACCOUNT struct into a BYTE buffer for storage */
621 if ((data.dsize=init_buffer_from_sam (&buf, newpwd, False)) == -1) {
622 DEBUG(0,("tdb_update_sam: ERROR - Unable to copy SAM_ACCOUNT info BYTE buffer!\n"));
623 ret = False;
624 goto done;
626 data.dptr = (char *)buf;
628 fstrcpy(name, pdb_get_username(newpwd));
629 strlower_m(name);
631 DEBUG(5, ("Storing %saccount %s with RID %d\n", flag == TDB_INSERT ? "(new) " : "", name, user_rid));
633 /* setup the USER index key */
634 slprintf(keystr, sizeof(keystr)-1, "%s%s", USERPREFIX, name);
635 key.dptr = keystr;
636 key.dsize = strlen(keystr) + 1;
638 /* add the account */
639 if (tdb_store(pwd_tdb, key, data, flag) != TDB_SUCCESS) {
640 DEBUG(0, ("Unable to modify passwd TDB!"));
641 DEBUGADD(0, (" Error: %s", tdb_errorstr(pwd_tdb)));
642 DEBUGADD(0, (" occured while storing the main record (%s)\n", keystr));
643 ret = False;
644 goto done;
647 /* setup RID data */
648 data.dsize = strlen(name) + 1;
649 data.dptr = name;
651 /* setup the RID index key */
652 slprintf(keystr, sizeof(keystr)-1, "%s%.8x", RIDPREFIX, user_rid);
653 key.dptr = keystr;
654 key.dsize = strlen (keystr) + 1;
656 /* add the reference */
657 if (tdb_store(pwd_tdb, key, data, flag) != TDB_SUCCESS) {
658 DEBUG(0, ("Unable to modify TDB passwd !"));
659 DEBUGADD(0, (" Error: %s\n", tdb_errorstr(pwd_tdb)));
660 DEBUGADD(0, (" occured while storing the RID index (%s)\n", keystr));
661 ret = False;
662 goto done;
665 done:
666 /* cleanup */
667 tdb_close (pwd_tdb);
668 SAFE_FREE(buf);
670 return (ret);
673 /***************************************************************************
674 Modifies an existing SAM_ACCOUNT
675 ****************************************************************************/
677 static NTSTATUS tdbsam_update_sam_account (struct pdb_methods *my_methods, SAM_ACCOUNT *newpwd)
679 if (tdb_update_sam(my_methods, newpwd, TDB_MODIFY))
680 return NT_STATUS_OK;
681 else
682 return NT_STATUS_UNSUCCESSFUL;
685 /***************************************************************************
686 Adds an existing SAM_ACCOUNT
687 ****************************************************************************/
689 static NTSTATUS tdbsam_add_sam_account (struct pdb_methods *my_methods, SAM_ACCOUNT *newpwd)
691 if (tdb_update_sam(my_methods, newpwd, TDB_INSERT))
692 return NT_STATUS_OK;
693 else
694 return NT_STATUS_UNSUCCESSFUL;
697 static void free_private_data(void **vp)
699 struct tdbsam_privates **tdb_state = (struct tdbsam_privates **)vp;
700 tdbsam_tdbclose(*tdb_state);
701 *tdb_state = NULL;
703 /* No need to free any further, as it is talloc()ed */
707 static NTSTATUS pdb_init_tdbsam(PDB_CONTEXT *pdb_context, PDB_METHODS **pdb_method, const char *location)
709 NTSTATUS nt_status;
710 struct tdbsam_privates *tdb_state;
712 if (!NT_STATUS_IS_OK(nt_status = make_pdb_methods(pdb_context->mem_ctx, pdb_method))) {
713 return nt_status;
716 (*pdb_method)->name = "tdbsam";
718 (*pdb_method)->setsampwent = tdbsam_setsampwent;
719 (*pdb_method)->endsampwent = tdbsam_endsampwent;
720 (*pdb_method)->getsampwent = tdbsam_getsampwent;
721 (*pdb_method)->getsampwnam = tdbsam_getsampwnam;
722 (*pdb_method)->getsampwsid = tdbsam_getsampwsid;
723 (*pdb_method)->add_sam_account = tdbsam_add_sam_account;
724 (*pdb_method)->update_sam_account = tdbsam_update_sam_account;
725 (*pdb_method)->delete_sam_account = tdbsam_delete_sam_account;
727 tdb_state = TALLOC_ZERO_P(pdb_context->mem_ctx, struct tdbsam_privates);
729 if (!tdb_state) {
730 DEBUG(0, ("talloc() failed for tdbsam private_data!\n"));
731 return NT_STATUS_NO_MEMORY;
734 if (location) {
735 tdb_state->tdbsam_location = talloc_strdup(pdb_context->mem_ctx, location);
736 } else {
737 pstring tdbfile;
738 get_private_directory(tdbfile);
739 pstrcat(tdbfile, "/");
740 pstrcat(tdbfile, PASSDB_FILE_NAME);
741 tdb_state->tdbsam_location = talloc_strdup(pdb_context->mem_ctx, tdbfile);
744 (*pdb_method)->private_data = tdb_state;
746 (*pdb_method)->free_private_data = free_private_data;
748 return NT_STATUS_OK;
751 NTSTATUS pdb_tdbsam_init(void)
753 return smb_register_passdb(PASSDB_INTERFACE_VERSION, "tdbsam", pdb_init_tdbsam);