2 Unix SMB/CIFS implementation.
3 Database interface wrapper: local open code.
5 Copyright (C) Rusty Russell 2012
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "dbwrap/dbwrap.h"
23 #include "dbwrap/dbwrap_tdb.h"
24 #include "dbwrap/dbwrap_ntdb.h"
27 #include "lib/util/util_ntdb.h"
29 #include "lib/param/param.h"
30 #include "system/filesys.h"
31 #include "ccan/str/str.h"
39 static const struct flag_map tdb_ntdb_flags
[] = {
40 { TDB_CLEAR_IF_FIRST
, NTDB_CLEAR_IF_FIRST
},
41 { TDB_INTERNAL
, NTDB_INTERNAL
},
42 { TDB_NOLOCK
, NTDB_NOLOCK
},
43 { TDB_NOMMAP
, NTDB_NOMMAP
},
44 { TDB_CONVERT
, NTDB_CONVERT
},
45 { TDB_NOSYNC
, NTDB_NOSYNC
},
46 { TDB_SEQNUM
, NTDB_SEQNUM
},
48 { TDB_ALLOW_NESTING
, NTDB_ALLOW_NESTING
},
49 { TDB_DISALLOW_NESTING
, 0 },
50 { TDB_INCOMPATIBLE_HASH
, 0 }
53 static int tdb_flags_to_ntdb_flags(int tdb_flags
)
58 /* TDB allows nesting unless told not to. */
59 if (!(tdb_flags
& TDB_DISALLOW_NESTING
))
60 ntdb_flags
|= NTDB_ALLOW_NESTING
;
62 for (i
= 0; i
< sizeof(tdb_ntdb_flags
)/sizeof(tdb_ntdb_flags
[0]); i
++) {
63 if (tdb_flags
& tdb_ntdb_flags
[i
].tdb_flag
) {
64 tdb_flags
&= ~tdb_ntdb_flags
[i
].tdb_flag
;
65 ntdb_flags
|= tdb_ntdb_flags
[i
].ntdb_flag
;
69 SMB_ASSERT(tdb_flags
== 0);
74 struct db_context
*ntdb
;
78 static int write_to_ntdb(struct db_record
*rec
, void *_tdata
)
80 struct trav_data
*tdata
= _tdata
;
83 key
= dbwrap_record_get_key(rec
);
84 value
= dbwrap_record_get_value(rec
);
86 tdata
->status
= dbwrap_store(tdata
->ntdb
, key
, value
, TDB_INSERT
);
87 if (!NT_STATUS_IS_OK(tdata
->status
)) {
93 static bool tdb_to_ntdb(TALLOC_CTX
*ctx
, struct loadparm_context
*lp_ctx
,
94 const char *tdbname
, const char *ntdbname
)
96 struct db_context
*ntdb
, *tdb
;
98 const char *tdbbase
, *bakbase
;
99 struct trav_data tdata
;
102 /* We need permissions from the tdb file. */
103 if (stat(tdbname
, &st
) == -1) {
104 DEBUG(0, ("tdb_to_ntdb: fstat %s failed: %s\n",
105 tdbname
, strerror(errno
)));
108 tdb
= db_open_tdb(ctx
, lp_ctx
, tdbname
, 0,
109 TDB_DEFAULT
, O_RDONLY
, 0, 0);
111 DEBUG(0, ("tdb_to_ntdb: could not open %s: %s\n",
112 tdbname
, strerror(errno
)));
115 ntdb
= db_open_ntdb(ctx
, lp_ctx
, ntdbname
, dbwrap_hash_size(tdb
),
116 TDB_DEFAULT
, O_RDWR
|O_CREAT
|O_EXCL
,
117 st
.st_mode
& 0777, 0);
119 DEBUG(0, ("tdb_to_ntdb: could not create %s: %s\n",
120 ntdbname
, strerror(errno
)));
123 bakname
= talloc_asprintf(ctx
, "%s.bak", tdbname
);
125 DEBUG(0, ("tdb_to_ntdb: could not allocate\n"));
129 tdata
.status
= NT_STATUS_OK
;
131 if (!NT_STATUS_IS_OK(dbwrap_traverse_read(tdb
, write_to_ntdb
, &tdata
,
135 if (!NT_STATUS_IS_OK(tdata
.status
)) {
139 if (rename(tdbname
, bakname
) != 0) {
140 DEBUG(0, ("tdb_to_ntdb: could not rename %s to %s\n",
146 /* Make sure it's never accidentally used. */
147 symlink("This is now in an NTDB", tdbname
);
149 /* Make message a bit shorter by using basenames. */
150 tdbbase
= strrchr(tdbname
, '/');
153 bakbase
= strrchr(bakname
, '/');
156 DEBUG(1, ("Upgraded %s from %s (which moved to %s)\n",
157 ntdbname
, tdbbase
, bakbase
));
160 #endif /* !DISABLE_NTDB */
162 struct db_context
*dbwrap_local_open(TALLOC_CTX
*mem_ctx
,
163 struct loadparm_context
*lp_ctx
,
165 int hash_size
, int tdb_flags
,
166 int open_flags
, mode_t mode
,
167 enum dbwrap_lock_order lock_order
)
169 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
170 const char *ntdbname
, *tdbname
;
171 struct db_context
*db
= NULL
;
173 /* Get both .ntdb and .tdb variants of the name. */
175 tdbname
= ntdbname
= "unnamed database";
176 } else if (strends(name
, ".tdb")) {
178 ntdbname
= talloc_asprintf(tmp_ctx
,
180 (int)strlen(name
) - 4, name
);
181 } else if (strends(name
, ".ntdb")) {
183 tdbname
= talloc_asprintf(tmp_ctx
,
185 (int)strlen(name
) - 5, name
);
187 DEBUG(1, ("WARNING: database '%s' does not end in .[n]tdb:"
188 " treating it as a TDB file!\n", name
));
189 ntdbname
= talloc_strdup(tmp_ctx
, name
);
193 if (ntdbname
== NULL
|| tdbname
== NULL
) {
194 DEBUG(0, ("talloc failed\n"));
198 if (name
== ntdbname
) {
200 DEBUG(1, ("WARNING: no ntdb support to open '%s'\n", name
));
202 int ntdb_flags
= tdb_flags_to_ntdb_flags(tdb_flags
);
204 /* For non-internal databases, we upgrade on demand. */
205 if (!(tdb_flags
& TDB_INTERNAL
)) {
206 if (!file_exist(ntdbname
) && file_exist(tdbname
)) {
207 if (!tdb_to_ntdb(tmp_ctx
, lp_ctx
,
208 tdbname
, ntdbname
)) {
213 db
= db_open_ntdb(mem_ctx
, lp_ctx
, ntdbname
, hash_size
,
214 ntdb_flags
, open_flags
, mode
, lock_order
);
217 if (!streq(ntdbname
, tdbname
) && file_exist(ntdbname
)) {
218 DEBUG(0, ("Refusing to open '%s' when '%s' exists\n",
222 db
= db_open_tdb(mem_ctx
, lp_ctx
, tdbname
, hash_size
,
223 tdb_flags
, open_flags
, mode
,
227 talloc_free(tmp_ctx
);