2 * Unix SMB/CIFS implementation.
3 * SMB parameters and setup
4 * Copyright (C) Andrew Tridgell 1992-1998
5 * Copyright (C) Simo Sorce 2000-2003
6 * Copyright (C) Gerald Carter 2000-2006
7 * Copyright (C) Jeremy Allison 2001-2009
8 * Copyright (C) Andrew Bartlett 2002
9 * Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2005
11 * This program is free software; you can redistribute it and/or modify it under
12 * the terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 3 of the License, or (at your option)
16 * This program is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
21 * You should have received a copy of the GNU General Public License along with
22 * this program; if not, see <http://www.gnu.org/licenses/>.
26 #include "system/filesys.h"
28 #include "dbwrap/dbwrap.h"
29 #include "dbwrap/dbwrap_open.h"
30 #include "../libcli/security/security.h"
32 #include "passdb/pdb_tdb.h"
34 #if 0 /* when made a module use this */
36 static int tdbsam_debug_level
= DBGC_ALL
;
38 #define DBGC_CLASS tdbsam_debug_level
43 #define DBGC_CLASS DBGC_PASSDB
47 #define TDBSAM_VERSION 4 /* Most recent TDBSAM version */
48 #define TDBSAM_MINOR_VERSION 0 /* Most recent TDBSAM minor version */
49 #define TDBSAM_VERSION_STRING "INFO/version"
50 #define TDBSAM_MINOR_VERSION_STRING "INFO/minor_version"
51 #define PASSDB_FILE_NAME "passdb.tdb"
52 #define USERPREFIX "USER_"
53 #define USERPREFIX_LEN 5
54 #define RIDPREFIX "RID_"
55 #define PRIVPREFIX "PRIV_"
56 #define NEXT_RID_STRING "NEXT_RID"
58 /* GLOBAL TDB SAM CONTEXT */
60 static struct db_context
*db_sam
;
61 static char *tdbsam_filename
;
63 struct tdbsam_convert_state
{
68 static int tdbsam_convert_one(struct db_record
*rec
, void *priv
)
70 struct tdbsam_convert_state
*state
=
71 (struct tdbsam_convert_state
*)priv
;
79 key
= dbwrap_record_get_key(rec
);
81 if (key
.dsize
< USERPREFIX_LEN
) {
84 if (strncmp((char *)key
.dptr
, USERPREFIX
, USERPREFIX_LEN
) != 0) {
88 user
= samu_new(talloc_tos());
90 DEBUG(0,("tdbsam_convert: samu_new() failed!\n"));
91 state
->success
= false;
95 DEBUG(10,("tdbsam_convert: Try unpacking a record with (key:%s) "
96 "(version:%d)\n", (char *)key
.dptr
, state
->from
));
98 value
= dbwrap_record_get_value(rec
);
100 switch (state
->from
) {
102 ret
= init_samu_from_buffer(user
, SAMU_BUFFER_V0
,
107 ret
= init_samu_from_buffer(user
, SAMU_BUFFER_V1
,
112 ret
= init_samu_from_buffer(user
, SAMU_BUFFER_V2
,
117 ret
= init_samu_from_buffer(user
, SAMU_BUFFER_V3
,
122 ret
= init_samu_from_buffer(user
, SAMU_BUFFER_V4
,
127 /* unknown tdbsam version */
131 DEBUG(0,("tdbsam_convert: Bad struct samu entry returned "
132 "from TDB (key:%s) (version:%d)\n", (char *)key
.dptr
,
135 state
->success
= false;
139 data
.dsize
= init_buffer_from_samu(&data
.dptr
, user
, false);
142 if (data
.dsize
== -1) {
143 DEBUG(0,("tdbsam_convert: cannot pack the struct samu into "
144 "the new format\n"));
145 state
->success
= false;
149 status
= dbwrap_record_store(rec
, data
, TDB_MODIFY
);
150 if (!NT_STATUS_IS_OK(status
)) {
151 DEBUG(0, ("Could not store the new record: %s\n",
153 state
->success
= false;
160 /**********************************************************************
161 Struct and function to backup an old record.
162 *********************************************************************/
164 struct tdbsam_backup_state
{
165 struct db_context
*new_db
;
169 static int backup_copy_fn(struct db_record
*orig_rec
, void *state
)
171 struct tdbsam_backup_state
*bs
= (struct tdbsam_backup_state
*)state
;
172 struct db_record
*new_rec
;
177 key
= dbwrap_record_get_key(orig_rec
);
179 new_rec
= dbwrap_fetch_locked(bs
->new_db
, talloc_tos(), key
);
180 if (new_rec
== NULL
) {
185 value
= dbwrap_record_get_value(orig_rec
);
187 status
= dbwrap_record_store(new_rec
, value
, TDB_INSERT
);
189 TALLOC_FREE(new_rec
);
191 if (!NT_STATUS_IS_OK(status
)) {
198 /**********************************************************************
199 Make a backup of an old passdb and replace the new one with it. We
200 have to do this as between 3.0.x and 3.2.x the hash function changed
201 by mistake (used unsigned char * instead of char *). This means the
202 previous simple update code will fail due to not being able to find
203 existing records to replace in the tdbsam_convert_one() function. JRA.
204 *********************************************************************/
206 static bool tdbsam_convert_backup(const char *dbname
, struct db_context
**pp_db
)
208 TALLOC_CTX
*frame
= talloc_stackframe();
209 const char *tmp_fname
= NULL
;
210 struct db_context
*tmp_db
= NULL
;
211 struct db_context
*orig_db
= *pp_db
;
212 struct tdbsam_backup_state bs
;
215 tmp_fname
= talloc_asprintf(frame
, "%s.tmp", dbname
);
223 /* Remember to open this on the NULL context. We need
224 * it to stay around after we return from here. */
226 tmp_db
= db_open(NULL
, tmp_fname
, 0,
227 TDB_DEFAULT
, O_CREAT
|O_RDWR
, 0600,
228 DBWRAP_LOCK_ORDER_1
);
229 if (tmp_db
== NULL
) {
230 DEBUG(0, ("tdbsam_convert_backup: Failed to create backup TDB passwd "
231 "[%s]\n", tmp_fname
));
236 if (dbwrap_transaction_start(orig_db
) != 0) {
237 DEBUG(0, ("tdbsam_convert_backup: Could not start transaction (1)\n"));
243 if (dbwrap_transaction_start(tmp_db
) != 0) {
244 DEBUG(0, ("tdbsam_convert_backup: Could not start transaction (2)\n"));
245 dbwrap_transaction_cancel(orig_db
);
255 status
= dbwrap_traverse(orig_db
, backup_copy_fn
, (void *)&bs
, NULL
);
256 if (!NT_STATUS_IS_OK(status
)) {
257 DEBUG(0, ("tdbsam_convert_backup: traverse failed\n"));
262 DEBUG(0, ("tdbsam_convert_backup: Rewriting records failed\n"));
266 if (dbwrap_transaction_commit(orig_db
) != 0) {
267 smb_panic("tdbsam_convert_backup: orig commit failed\n");
269 if (dbwrap_transaction_commit(tmp_db
) != 0) {
270 smb_panic("tdbsam_convert_backup: orig commit failed\n");
273 /* be sure to close the DBs _before_ renaming the file */
275 TALLOC_FREE(orig_db
);
278 /* This is safe from other users as we know we're
279 * under a mutex here. */
281 if (rename(tmp_fname
, dbname
) == -1) {
282 DEBUG(0, ("tdbsam_convert_backup: rename of %s to %s failed %s\n",
286 smb_panic("tdbsam_convert_backup: replace passdb failed\n");
291 /* re-open the converted TDB */
293 orig_db
= db_open(NULL
, dbname
, 0,
294 TDB_DEFAULT
, O_CREAT
|O_RDWR
, 0600,
295 DBWRAP_LOCK_ORDER_1
);
296 if (orig_db
== NULL
) {
297 DEBUG(0, ("tdbsam_convert_backup: Failed to re-open "
298 "converted passdb TDB [%s]\n", dbname
));
302 DEBUG(1, ("tdbsam_convert_backup: updated %s file.\n",
305 /* Replace the global db pointer. */
311 if (dbwrap_transaction_cancel(orig_db
) != 0) {
312 smb_panic("tdbsam_convert: transaction_cancel failed");
315 if (dbwrap_transaction_cancel(tmp_db
) != 0) {
316 smb_panic("tdbsam_convert: transaction_cancel failed");
325 static bool tdbsam_upgrade_next_rid(struct db_context
*db
)
332 status
= dbwrap_fetch_uint32_bystring(db
, NEXT_RID_STRING
, &rid
);
333 if (NT_STATUS_IS_OK(status
)) {
337 tdb
= tdb_open_log(state_path("winbindd_idmap.tdb"), 0,
338 TDB_DEFAULT
, O_RDONLY
, 0644);
341 ok
= tdb_fetch_uint32(tdb
, "RID_COUNTER", &rid
);
350 status
= dbwrap_store_uint32_bystring(db
, NEXT_RID_STRING
, rid
);
351 if (!NT_STATUS_IS_OK(status
)) {
358 static bool tdbsam_convert(struct db_context
**pp_db
, const char *name
, int32 from
)
360 struct tdbsam_convert_state state
;
361 struct db_context
*db
= NULL
;
364 /* We only need the update backup for local db's. */
365 if (db_is_local(name
) && !tdbsam_convert_backup(name
, pp_db
)) {
366 DEBUG(0, ("tdbsam_convert: Could not backup %s\n", name
));
372 state
.success
= true;
374 if (dbwrap_transaction_start(db
) != 0) {
375 DEBUG(0, ("tdbsam_convert: Could not start transaction\n"));
379 if (!tdbsam_upgrade_next_rid(db
)) {
380 DEBUG(0, ("tdbsam_convert: tdbsam_upgrade_next_rid failed\n"));
384 status
= dbwrap_traverse(db
, tdbsam_convert_one
, &state
, NULL
);
385 if (!NT_STATUS_IS_OK(status
)) {
386 DEBUG(0, ("tdbsam_convert: traverse failed\n"));
390 if (!state
.success
) {
391 DEBUG(0, ("tdbsam_convert: Converting records failed\n"));
395 status
= dbwrap_store_int32_bystring(db
, TDBSAM_VERSION_STRING
,
397 if (!NT_STATUS_IS_OK(status
)) {
398 DEBUG(0, ("tdbsam_convert: Could not store tdbsam version: "
399 "%s\n", nt_errstr(status
)));
403 status
= dbwrap_store_int32_bystring(db
, TDBSAM_MINOR_VERSION_STRING
,
404 TDBSAM_MINOR_VERSION
);
405 if (!NT_STATUS_IS_OK(status
)) {
406 DEBUG(0, ("tdbsam_convert: Could not store tdbsam minor "
407 "version: %s\n", nt_errstr(status
)));
411 if (dbwrap_transaction_commit(db
) != 0) {
412 DEBUG(0, ("tdbsam_convert: Could not commit transaction\n"));
419 if (dbwrap_transaction_cancel(db
) != 0) {
420 smb_panic("tdbsam_convert: transaction_cancel failed");
426 /*********************************************************************
427 Open the tdbsam file based on the absolute path specified.
428 Uses a reference count to allow multiple open calls.
429 *********************************************************************/
431 static bool tdbsam_open( const char *name
)
437 /* check if we are already open */
443 /* Try to open tdb passwd. Create a new one if necessary */
445 db_sam
= db_open(NULL
, name
, 0, TDB_DEFAULT
, O_CREAT
|O_RDWR
, 0600,
446 DBWRAP_LOCK_ORDER_1
);
447 if (db_sam
== NULL
) {
448 DEBUG(0, ("tdbsam_open: Failed to open/create TDB passwd "
453 /* Check the version */
454 status
= dbwrap_fetch_int32_bystring(db_sam
, TDBSAM_VERSION_STRING
,
456 if (!NT_STATUS_IS_OK(status
)) {
457 version
= 0; /* Version not found, assume version 0 */
460 /* Get the minor version */
461 status
= dbwrap_fetch_int32_bystring(
462 db_sam
, TDBSAM_MINOR_VERSION_STRING
, &minor_version
);
463 if (!NT_STATUS_IS_OK(status
)) {
464 minor_version
= 0; /* Minor version not found, assume 0 */
467 /* Compare the version */
468 if (version
> TDBSAM_VERSION
) {
469 /* Version more recent than the latest known */
470 DEBUG(0, ("tdbsam_open: unknown version => %d\n", version
));
475 if ( version
< TDBSAM_VERSION
||
476 (version
== TDBSAM_VERSION
&&
477 minor_version
< TDBSAM_MINOR_VERSION
) ) {
479 * Ok - we think we're going to have to convert.
480 * Due to the backup process we now must do to
481 * upgrade we have to get a mutex and re-check
482 * the version. Someone else may have upgraded
483 * whilst we were checking.
486 struct named_mutex
*mtx
= grab_named_mutex(NULL
,
487 "tdbsam_upgrade_mutex",
491 DEBUG(0, ("tdbsam_open: failed to grab mutex.\n"));
496 /* Re-check the version */
497 status
= dbwrap_fetch_int32_bystring(
498 db_sam
, TDBSAM_VERSION_STRING
, &version
);
499 if (!NT_STATUS_IS_OK(status
)) {
500 version
= 0; /* Version not found, assume version 0 */
503 /* Re-check the minor version */
504 status
= dbwrap_fetch_int32_bystring(
505 db_sam
, TDBSAM_MINOR_VERSION_STRING
, &minor_version
);
506 if (!NT_STATUS_IS_OK(status
)) {
507 minor_version
= 0; /* Minor version not found, assume 0 */
510 /* Compare the version */
511 if (version
> TDBSAM_VERSION
) {
512 /* Version more recent than the latest known */
513 DEBUG(0, ("tdbsam_open: unknown version => %d\n", version
));
519 if ( version
< TDBSAM_VERSION
||
520 (version
== TDBSAM_VERSION
&&
521 minor_version
< TDBSAM_MINOR_VERSION
) ) {
523 * Note that minor versions we read that are greater
524 * than the current minor version we have hard coded
525 * are assumed to be compatible if they have the same
526 * major version. That allows previous versions of the
527 * passdb code that don't know about minor versions to
528 * still use this database. JRA.
531 DEBUG(1, ("tdbsam_open: Converting version %d.%d database to "
536 TDBSAM_MINOR_VERSION
));
538 if ( !tdbsam_convert(&db_sam
, name
, version
) ) {
539 DEBUG(0, ("tdbsam_open: Error when trying to convert "
540 "tdbsam [%s]\n",name
));
546 DEBUG(3, ("TDBSAM converted successfully.\n"));
551 DEBUG(4,("tdbsam_open: successfully opened %s\n", name
));
556 /******************************************************************
557 Lookup a name in the SAM TDB
558 ******************************************************************/
560 static NTSTATUS
tdbsam_getsampwnam (struct pdb_methods
*my_methods
,
561 struct samu
*user
, const char *sname
)
569 DEBUG(0,("pdb_getsampwnam: struct samu is NULL.\n"));
570 return NT_STATUS_NO_MEMORY
;
573 /* Data is stored in all lower-case */
574 fstrcpy(name
, sname
);
575 if (!strlower_m(name
)) {
576 return NT_STATUS_INVALID_PARAMETER
;
580 slprintf(keystr
, sizeof(keystr
)-1, "%s%s", USERPREFIX
, name
);
582 /* open the database */
584 if ( !tdbsam_open( tdbsam_filename
) ) {
585 DEBUG(0,("tdbsam_getsampwnam: failed to open %s!\n", tdbsam_filename
));
586 return NT_STATUS_ACCESS_DENIED
;
591 status
= dbwrap_fetch_bystring(db_sam
, talloc_tos(), keystr
, &data
);
592 if (!NT_STATUS_IS_OK(status
)) {
593 DEBUG(5,("pdb_getsampwnam (TDB): error fetching database.\n"));
594 DEBUGADD(5, (" Key: %s\n", keystr
));
595 return NT_STATUS_NO_SUCH_USER
;
598 /* unpack the buffer */
600 if (!init_samu_from_buffer(user
, SAMU_BUFFER_LATEST
, data
.dptr
, data
.dsize
)) {
601 DEBUG(0,("pdb_getsampwent: Bad struct samu entry returned from TDB!\n"));
602 SAFE_FREE(data
.dptr
);
603 return NT_STATUS_NO_MEMORY
;
608 TALLOC_FREE(data
.dptr
);
613 /***************************************************************************
615 **************************************************************************/
617 static NTSTATUS
tdbsam_getsampwrid (struct pdb_methods
*my_methods
,
618 struct samu
*user
, uint32 rid
)
620 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
626 DEBUG(0,("pdb_getsampwrid: struct samu is NULL.\n"));
632 slprintf(keystr
, sizeof(keystr
)-1, "%s%.8x", RIDPREFIX
, rid
);
634 /* open the database */
636 if ( !tdbsam_open( tdbsam_filename
) ) {
637 DEBUG(0,("tdbsam_getsampwrid: failed to open %s!\n", tdbsam_filename
));
638 return NT_STATUS_ACCESS_DENIED
;
643 nt_status
= dbwrap_fetch_bystring(db_sam
, talloc_tos(), keystr
, &data
);
644 if (!NT_STATUS_IS_OK(nt_status
)) {
645 DEBUG(5,("pdb_getsampwrid (TDB): error looking up RID %d by key %s.\n", rid
, keystr
));
649 fstrcpy(name
, (const char *)data
.dptr
);
650 TALLOC_FREE(data
.dptr
);
652 return tdbsam_getsampwnam (my_methods
, user
, name
);
655 static NTSTATUS
tdbsam_getsampwsid(struct pdb_methods
*my_methods
,
656 struct samu
* user
, const struct dom_sid
*sid
)
660 if ( !sid_peek_check_rid(get_global_sam_sid(), sid
, &rid
) )
661 return NT_STATUS_UNSUCCESSFUL
;
663 return tdbsam_getsampwrid(my_methods
, user
, rid
);
666 static bool tdb_delete_samacct_only( struct samu
*sam_pass
)
672 fstrcpy(name
, pdb_get_username(sam_pass
));
673 if (!strlower_m(name
)) {
677 /* set the search key */
679 slprintf(keystr
, sizeof(keystr
)-1, "%s%s", USERPREFIX
, name
);
681 /* it's outaa here! 8^) */
682 if ( !tdbsam_open( tdbsam_filename
) ) {
683 DEBUG(0,("tdb_delete_samacct_only: failed to open %s!\n",
688 status
= dbwrap_delete_bystring(db_sam
, keystr
);
689 if (!NT_STATUS_IS_OK(status
)) {
690 DEBUG(5, ("Error deleting entry from tdb passwd "
691 "database: %s!\n", nt_errstr(status
)));
698 /***************************************************************************
699 Delete a struct samu records for the username and RID key
700 ****************************************************************************/
702 static NTSTATUS
tdbsam_delete_sam_account(struct pdb_methods
*my_methods
,
703 struct samu
*sam_pass
)
705 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
710 /* open the database */
712 if ( !tdbsam_open( tdbsam_filename
) ) {
713 DEBUG(0,("tdbsam_delete_sam_account: failed to open %s!\n",
715 return NT_STATUS_ACCESS_DENIED
;
718 fstrcpy(name
, pdb_get_username(sam_pass
));
719 if (!strlower_m(name
)) {
720 return NT_STATUS_INVALID_PARAMETER
;
723 /* set the search key */
725 slprintf(keystr
, sizeof(keystr
)-1, "%s%s", USERPREFIX
, name
);
727 rid
= pdb_get_user_rid(sam_pass
);
729 /* it's outaa here! 8^) */
731 if (dbwrap_transaction_start(db_sam
) != 0) {
732 DEBUG(0, ("Could not start transaction\n"));
733 return NT_STATUS_UNSUCCESSFUL
;
736 nt_status
= dbwrap_delete_bystring(db_sam
, keystr
);
737 if (!NT_STATUS_IS_OK(nt_status
)) {
738 DEBUG(5, ("Error deleting entry from tdb passwd "
739 "database: %s!\n", nt_errstr(nt_status
)));
743 /* set the search key */
745 slprintf(keystr
, sizeof(keystr
)-1, "%s%.8x", RIDPREFIX
, rid
);
747 /* it's outaa here! 8^) */
749 nt_status
= dbwrap_delete_bystring(db_sam
, keystr
);
750 if (!NT_STATUS_IS_OK(nt_status
)) {
751 DEBUG(5, ("Error deleting entry from tdb rid "
752 "database: %s!\n", nt_errstr(nt_status
)));
756 if (dbwrap_transaction_commit(db_sam
) != 0) {
757 DEBUG(0, ("Could not commit transaction\n"));
758 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
764 if (dbwrap_transaction_cancel(db_sam
) != 0) {
765 smb_panic("transaction_cancel failed");
772 /***************************************************************************
773 Update the TDB SAM account record only
774 Assumes that the tdbsam is already open
775 ****************************************************************************/
776 static bool tdb_update_samacct_only( struct samu
* newpwd
, int flag
)
785 /* copy the struct samu struct into a BYTE buffer for storage */
787 if ( (data
.dsize
=init_buffer_from_samu(&buf
, newpwd
, False
)) == -1 ) {
788 DEBUG(0,("tdb_update_sam: ERROR - Unable to copy struct samu info BYTE buffer!\n"));
793 fstrcpy(name
, pdb_get_username(newpwd
));
794 if (!strlower_m(name
)) {
798 DEBUG(5, ("Storing %saccount %s with RID %d\n",
799 flag
== TDB_INSERT
? "(new) " : "", name
,
800 pdb_get_user_rid(newpwd
)));
802 /* setup the USER index key */
803 slprintf(keystr
, sizeof(keystr
)-1, "%s%s", USERPREFIX
, name
);
805 /* add the account */
807 status
= dbwrap_store_bystring(db_sam
, keystr
, data
, flag
);
808 if (!NT_STATUS_IS_OK(status
)) {
809 DEBUG(0, ("Unable to modify passwd TDB: %s!",
822 /***************************************************************************
823 Update the TDB SAM RID record only
824 Assumes that the tdbsam is already open
825 ****************************************************************************/
826 static bool tdb_update_ridrec_only( struct samu
* newpwd
, int flag
)
833 fstrcpy(name
, pdb_get_username(newpwd
));
834 if (!strlower_m(name
)) {
839 data
= string_term_tdb_data(name
);
841 /* setup the RID index key */
842 slprintf(keystr
, sizeof(keystr
)-1, "%s%.8x", RIDPREFIX
,
843 pdb_get_user_rid(newpwd
));
845 /* add the reference */
846 status
= dbwrap_store_bystring(db_sam
, keystr
, data
, flag
);
847 if (!NT_STATUS_IS_OK(status
)) {
848 DEBUG(0, ("Unable to modify TDB passwd: %s!\n",
857 /***************************************************************************
859 ****************************************************************************/
861 static bool tdb_update_sam(struct pdb_methods
*my_methods
, struct samu
* newpwd
,
867 if (!(newrid
= pdb_get_user_rid(newpwd
))) {
868 DEBUG(0,("tdb_update_sam: struct samu (%s) with no RID!\n",
869 pdb_get_username(newpwd
)));
875 /* open the database */
877 if ( !tdbsam_open( tdbsam_filename
) ) {
878 DEBUG(0,("tdbsam_getsampwnam: failed to open %s!\n", tdbsam_filename
));
882 if (dbwrap_transaction_start(db_sam
) != 0) {
883 DEBUG(0, ("Could not start transaction\n"));
887 /* If we are updating, we may be changing this users RID. Retrieve the old RID
890 if (flag
== TDB_MODIFY
) {
891 struct samu
*account
= samu_new(talloc_tos());
892 if (account
== NULL
) {
893 DEBUG(0,("tdb_update_sam: samu_new() failed\n"));
896 if (!NT_STATUS_IS_OK(tdbsam_getsampwnam(my_methods
, account
, pdb_get_username(newpwd
)))) {
897 DEBUG(0,("tdb_update_sam: tdbsam_getsampwnam() for %s failed\n",
898 pdb_get_username(newpwd
)));
899 TALLOC_FREE(account
);
902 if (!(oldrid
= pdb_get_user_rid(account
))) {
903 DEBUG(0,("tdb_update_sam: pdb_get_user_rid() failed\n"));
904 TALLOC_FREE(account
);
907 TALLOC_FREE(account
);
910 /* Update the new samu entry. */
911 if (!tdb_update_samacct_only(newpwd
, flag
)) {
915 /* Now take care of the case where the RID changed. We need
916 * to delete the old RID key and add the new. */
918 if (flag
== TDB_MODIFY
&& newrid
!= oldrid
) {
921 /* Delete old RID key */
922 DEBUG(10, ("tdb_update_sam: Deleting key for RID %u\n", oldrid
));
923 slprintf(keystr
, sizeof(keystr
) - 1, "%s%.8x", RIDPREFIX
, oldrid
);
924 if (!NT_STATUS_IS_OK(dbwrap_delete_bystring(db_sam
, keystr
))) {
925 DEBUG(0, ("tdb_update_sam: Can't delete %s\n", keystr
));
928 /* Insert new RID key */
929 DEBUG(10, ("tdb_update_sam: Inserting key for RID %u\n", newrid
));
930 if (!tdb_update_ridrec_only(newpwd
, TDB_INSERT
)) {
934 DEBUG(10, ("tdb_update_sam: %s key for RID %u\n",
935 flag
== TDB_MODIFY
? "Updating" : "Inserting", newrid
));
936 if (!tdb_update_ridrec_only(newpwd
, flag
)) {
941 if (dbwrap_transaction_commit(db_sam
) != 0) {
942 DEBUG(0, ("Could not commit transaction\n"));
949 if (dbwrap_transaction_cancel(db_sam
) != 0) {
950 smb_panic("transaction_cancel failed");
955 /***************************************************************************
956 Modifies an existing struct samu
957 ****************************************************************************/
959 static NTSTATUS
tdbsam_update_sam_account (struct pdb_methods
*my_methods
, struct samu
*newpwd
)
961 if ( !tdb_update_sam(my_methods
, newpwd
, TDB_MODIFY
) )
962 return NT_STATUS_UNSUCCESSFUL
;
967 /***************************************************************************
968 Adds an existing struct samu
969 ****************************************************************************/
971 static NTSTATUS
tdbsam_add_sam_account (struct pdb_methods
*my_methods
, struct samu
*newpwd
)
973 if ( !tdb_update_sam(my_methods
, newpwd
, TDB_INSERT
) )
974 return NT_STATUS_UNSUCCESSFUL
;
979 /***************************************************************************
980 Renames a struct samu
981 - check for the posix user/rename user script
982 - Add and lock the new user record
983 - rename the posix user
984 - rewrite the rid->username record
985 - delete the old user
986 - unlock the new user record
987 ***************************************************************************/
988 static NTSTATUS
tdbsam_rename_sam_account(struct pdb_methods
*my_methods
,
989 struct samu
*old_acct
,
992 struct samu
*new_acct
= NULL
;
993 char *rename_script
= NULL
;
995 fstring oldname_lower
;
996 fstring newname_lower
;
998 /* can't do anything without an external script */
1000 if ( !(new_acct
= samu_new( talloc_tos() )) ) {
1001 return NT_STATUS_NO_MEMORY
;
1004 rename_script
= lp_renameuser_script(new_acct
);
1005 if (!rename_script
) {
1006 TALLOC_FREE(new_acct
);
1007 return NT_STATUS_NO_MEMORY
;
1009 if (!*rename_script
) {
1010 TALLOC_FREE(new_acct
);
1011 return NT_STATUS_ACCESS_DENIED
;
1014 if ( !pdb_copy_sam_account(new_acct
, old_acct
)
1015 || !pdb_set_username(new_acct
, newname
, PDB_CHANGED
))
1017 TALLOC_FREE(new_acct
);
1018 return NT_STATUS_NO_MEMORY
;
1021 /* open the database */
1022 if ( !tdbsam_open( tdbsam_filename
) ) {
1023 DEBUG(0, ("tdbsam_getsampwnam: failed to open %s!\n",
1025 TALLOC_FREE(new_acct
);
1026 return NT_STATUS_ACCESS_DENIED
;
1029 if (dbwrap_transaction_start(db_sam
) != 0) {
1030 DEBUG(0, ("Could not start transaction\n"));
1031 TALLOC_FREE(new_acct
);
1032 return NT_STATUS_ACCESS_DENIED
;
1036 /* add the new account and lock it */
1037 if ( !tdb_update_samacct_only(new_acct
, TDB_INSERT
) ) {
1041 /* Rename the posix user. Follow the semantics of _samr_create_user()
1042 so that we lower case the posix name but preserve the case in passdb */
1044 fstrcpy( oldname_lower
, pdb_get_username(old_acct
) );
1045 if (!strlower_m( oldname_lower
)) {
1049 fstrcpy( newname_lower
, newname
);
1050 if (!strlower_m( newname_lower
)) {
1054 rename_script
= talloc_string_sub2(new_acct
,
1061 if (!rename_script
) {
1064 rename_script
= talloc_string_sub2(new_acct
,
1071 if (!rename_script
) {
1074 rename_ret
= smbrun(rename_script
, NULL
);
1076 DEBUG(rename_ret
? 0 : 3,("Running the command `%s' gave %d\n",
1077 rename_script
, rename_ret
));
1079 if (rename_ret
!= 0) {
1083 smb_nscd_flush_user_cache();
1085 /* rewrite the rid->username record */
1087 if ( !tdb_update_ridrec_only( new_acct
, TDB_MODIFY
) ) {
1091 tdb_delete_samacct_only( old_acct
);
1093 if (dbwrap_transaction_commit(db_sam
) != 0) {
1095 * Ok, we're screwed. We've changed the posix account, but
1096 * could not adapt passdb.tdb. Shall we change the posix
1099 DEBUG(0, ("transaction_commit failed\n"));
1100 TALLOC_FREE(new_acct
);
1101 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
1104 TALLOC_FREE(new_acct
);
1105 return NT_STATUS_OK
;
1108 if (dbwrap_transaction_cancel(db_sam
) != 0) {
1109 smb_panic("transaction_cancel failed");
1112 TALLOC_FREE(new_acct
);
1114 return NT_STATUS_ACCESS_DENIED
;
1117 static uint32_t tdbsam_capabilities(struct pdb_methods
*methods
)
1119 return PDB_CAP_STORE_RIDS
;
1122 static bool tdbsam_new_rid(struct pdb_methods
*methods
, uint32
*prid
)
1127 rid
= BASE_RID
; /* Default if not set */
1129 if (!tdbsam_open(tdbsam_filename
)) {
1130 DEBUG(0,("tdbsam_new_rid: failed to open %s!\n",
1135 status
= dbwrap_trans_change_uint32_atomic_bystring(
1136 db_sam
, NEXT_RID_STRING
, &rid
, 1);
1137 if (!NT_STATUS_IS_OK(status
)) {
1138 DEBUG(3, ("tdbsam_new_rid: Failed to increase %s: %s\n",
1139 NEXT_RID_STRING
, nt_errstr(status
)));
1148 struct tdbsam_search_state
{
1149 struct pdb_methods
*methods
;
1150 uint32_t acct_flags
;
1158 static int tdbsam_collect_rids(struct db_record
*rec
, void *private_data
)
1160 struct tdbsam_search_state
*state
= talloc_get_type_abort(
1161 private_data
, struct tdbsam_search_state
);
1162 size_t prefixlen
= strlen(RIDPREFIX
);
1166 key
= dbwrap_record_get_key(rec
);
1168 if ((key
.dsize
< prefixlen
)
1169 || (strncmp((char *)key
.dptr
, RIDPREFIX
, prefixlen
))) {
1173 rid
= strtoul((char *)key
.dptr
+prefixlen
, NULL
, 16);
1175 ADD_TO_LARGE_ARRAY(state
, uint32
, rid
, &state
->rids
, &state
->num_rids
,
1176 &state
->array_size
);
1181 static void tdbsam_search_end(struct pdb_search
*search
)
1183 struct tdbsam_search_state
*state
= talloc_get_type_abort(
1184 search
->private_data
, struct tdbsam_search_state
);
1188 static bool tdbsam_search_next_entry(struct pdb_search
*search
,
1189 struct samr_displayentry
*entry
)
1191 struct tdbsam_search_state
*state
= talloc_get_type_abort(
1192 search
->private_data
, struct tdbsam_search_state
);
1193 struct samu
*user
= NULL
;
1199 user
= samu_new(talloc_tos());
1201 DEBUG(0, ("samu_new failed\n"));
1205 if (state
->current
== state
->num_rids
) {
1210 rid
= state
->rids
[state
->current
++];
1212 status
= tdbsam_getsampwrid(state
->methods
, user
, rid
);
1214 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_SUCH_USER
)) {
1216 * Someone has deleted that user since we listed the RIDs
1221 if (!NT_STATUS_IS_OK(status
)) {
1222 DEBUG(10, ("tdbsam_getsampwrid failed: %s\n",
1223 nt_errstr(status
)));
1228 if ((state
->acct_flags
!= 0) &&
1229 ((state
->acct_flags
& pdb_get_acct_ctrl(user
)) == 0)) {
1233 entry
->acct_flags
= pdb_get_acct_ctrl(user
);
1235 entry
->account_name
= talloc_strdup(search
, pdb_get_username(user
));
1236 entry
->fullname
= talloc_strdup(search
, pdb_get_fullname(user
));
1237 entry
->description
= talloc_strdup(search
, pdb_get_acct_desc(user
));
1241 if ((entry
->account_name
== NULL
) || (entry
->fullname
== NULL
)
1242 || (entry
->description
== NULL
)) {
1243 DEBUG(0, ("talloc_strdup failed\n"));
1250 static bool tdbsam_search_users(struct pdb_methods
*methods
,
1251 struct pdb_search
*search
,
1254 struct tdbsam_search_state
*state
;
1256 if (!tdbsam_open(tdbsam_filename
)) {
1257 DEBUG(0,("tdbsam_getsampwnam: failed to open %s!\n",
1262 state
= talloc_zero(search
, struct tdbsam_search_state
);
1263 if (state
== NULL
) {
1264 DEBUG(0, ("talloc failed\n"));
1267 state
->acct_flags
= acct_flags
;
1268 state
->methods
= methods
;
1270 dbwrap_traverse_read(db_sam
, tdbsam_collect_rids
, state
, NULL
);
1272 search
->private_data
= state
;
1273 search
->next_entry
= tdbsam_search_next_entry
;
1274 search
->search_end
= tdbsam_search_end
;
1279 /*********************************************************************
1280 Initialize the tdb sam backend. Setup the dispath table of methods,
1281 open the tdb, etc...
1282 *********************************************************************/
1284 static NTSTATUS
pdb_init_tdbsam(struct pdb_methods
**pdb_method
, const char *location
)
1287 char *tdbfile
= NULL
;
1288 const char *pfile
= location
;
1290 if (!NT_STATUS_IS_OK(nt_status
= make_pdb_method( pdb_method
))) {
1294 (*pdb_method
)->name
= "tdbsam";
1296 (*pdb_method
)->getsampwnam
= tdbsam_getsampwnam
;
1297 (*pdb_method
)->getsampwsid
= tdbsam_getsampwsid
;
1298 (*pdb_method
)->add_sam_account
= tdbsam_add_sam_account
;
1299 (*pdb_method
)->update_sam_account
= tdbsam_update_sam_account
;
1300 (*pdb_method
)->delete_sam_account
= tdbsam_delete_sam_account
;
1301 (*pdb_method
)->rename_sam_account
= tdbsam_rename_sam_account
;
1302 (*pdb_method
)->search_users
= tdbsam_search_users
;
1304 (*pdb_method
)->capabilities
= tdbsam_capabilities
;
1305 (*pdb_method
)->new_rid
= tdbsam_new_rid
;
1307 /* save the path for later */
1310 if (asprintf(&tdbfile
, "%s/%s", lp_private_dir(),
1311 PASSDB_FILE_NAME
) < 0) {
1312 return NT_STATUS_NO_MEMORY
;
1316 tdbsam_filename
= SMB_STRDUP(pfile
);
1317 if (!tdbsam_filename
) {
1318 return NT_STATUS_NO_MEMORY
;
1322 /* no private data */
1324 (*pdb_method
)->private_data
= NULL
;
1325 (*pdb_method
)->free_private_data
= NULL
;
1327 return NT_STATUS_OK
;
1330 NTSTATUS
pdb_tdbsam_init(void)
1332 return smb_register_passdb(PASSDB_INTERFACE_VERSION
, "tdbsam", pdb_init_tdbsam
);