2 Unix SMB/CIFS implementation.
3 Implementation of a reliable server_exists()
4 Copyright (C) Volker Lendecke 2010
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "system/filesys.h"
25 #include "lib/util/tdb_wrap.h"
32 struct serverid_data
{
37 bool serverid_parent_init(TALLOC_CTX
*mem_ctx
)
42 * Open the tdb in the parent process (smbd) so that our
43 * CLEAR_IF_FIRST optimization in tdb_reopen_all can properly
47 db
= tdb_wrap_open(mem_ctx
, lock_path("serverid.tdb"),
48 0, TDB_DEFAULT
|TDB_CLEAR_IF_FIRST
|TDB_INCOMPATIBLE_HASH
, O_RDWR
|O_CREAT
,
51 DEBUG(1, ("could not open serverid.tdb: %s\n",
58 static struct db_context
*serverid_db(void)
60 static struct db_context
*db
;
65 db
= db_open(NULL
, lock_path("serverid.tdb"), 0,
66 TDB_DEFAULT
|TDB_CLEAR_IF_FIRST
|TDB_INCOMPATIBLE_HASH
, O_RDWR
|O_CREAT
, 0644);
70 static void serverid_fill_key(const struct server_id
*id
,
71 struct serverid_key
*key
)
78 bool serverid_register(const struct server_id id
, uint32_t msg_flags
)
80 struct db_context
*db
;
81 struct serverid_key key
;
82 struct serverid_data data
;
83 struct db_record
*rec
;
84 TDB_DATA tdbkey
, tdbdata
;
93 serverid_fill_key(&id
, &key
);
94 tdbkey
= make_tdb_data((uint8_t *)&key
, sizeof(key
));
96 rec
= db
->fetch_locked(db
, talloc_tos(), tdbkey
);
98 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
103 data
.unique_id
= id
.unique_id
;
104 data
.msg_flags
= msg_flags
;
106 tdbdata
= make_tdb_data((uint8_t *)&data
, sizeof(data
));
107 status
= rec
->store(rec
, tdbdata
, 0);
108 if (!NT_STATUS_IS_OK(status
)) {
109 DEBUG(1, ("Storing serverid.tdb record failed: %s\n",
119 bool serverid_register_msg_flags(const struct server_id id
, bool do_reg
,
122 struct db_context
*db
;
123 struct serverid_key key
;
124 struct serverid_data
*data
;
125 struct db_record
*rec
;
135 serverid_fill_key(&id
, &key
);
136 tdbkey
= make_tdb_data((uint8_t *)&key
, sizeof(key
));
138 rec
= db
->fetch_locked(db
, talloc_tos(), tdbkey
);
140 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
144 if (rec
->value
.dsize
!= sizeof(struct serverid_data
)) {
145 DEBUG(1, ("serverid record has unexpected size %d "
146 "(wanted %d)\n", (int)rec
->value
.dsize
,
147 (int)sizeof(struct serverid_data
)));
151 data
= (struct serverid_data
*)rec
->value
.dptr
;
154 data
->msg_flags
|= msg_flags
;
156 data
->msg_flags
&= ~msg_flags
;
159 status
= rec
->store(rec
, rec
->value
, 0);
160 if (!NT_STATUS_IS_OK(status
)) {
161 DEBUG(1, ("Storing serverid.tdb record failed: %s\n",
171 bool serverid_deregister(struct server_id id
)
173 struct db_context
*db
;
174 struct serverid_key key
;
175 struct db_record
*rec
;
185 serverid_fill_key(&id
, &key
);
186 tdbkey
= make_tdb_data((uint8_t *)&key
, sizeof(key
));
188 rec
= db
->fetch_locked(db
, talloc_tos(), tdbkey
);
190 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
194 status
= rec
->delete_rec(rec
);
195 if (!NT_STATUS_IS_OK(status
)) {
196 DEBUG(1, ("Deleting serverid.tdb record failed: %s\n",
206 struct serverid_exists_state
{
207 const struct server_id
*id
;
211 static int server_exists_parse(TDB_DATA key
, TDB_DATA data
, void *priv
)
213 struct serverid_exists_state
*state
=
214 (struct serverid_exists_state
*)priv
;
216 if (data
.dsize
!= sizeof(struct serverid_data
)) {
221 * Use memcmp, not direct compare. data.dptr might not be
224 state
->exists
= (memcmp(&state
->id
->unique_id
, data
.dptr
,
225 sizeof(state
->id
->unique_id
)) == 0);
229 bool serverid_exists(const struct server_id
*id
)
231 struct db_context
*db
;
232 struct serverid_exists_state state
;
233 struct serverid_key key
;
236 if (procid_is_me(id
)) {
240 if (!process_exists(*id
)) {
244 if (id
->unique_id
== SERVERID_UNIQUE_ID_NOT_TO_VERIFY
) {
253 serverid_fill_key(id
, &key
);
254 tdbkey
= make_tdb_data((uint8_t *)&key
, sizeof(key
));
257 state
.exists
= false;
259 if (db
->parse_record(db
, tdbkey
, server_exists_parse
, &state
) == -1) {
265 static bool serverid_rec_parse(const struct db_record
*rec
,
266 struct server_id
*id
, uint32_t *msg_flags
)
268 struct serverid_key key
;
269 struct serverid_data data
;
271 if (rec
->key
.dsize
!= sizeof(key
)) {
272 DEBUG(1, ("Found invalid key length %d in serverid.tdb\n",
273 (int)rec
->key
.dsize
));
276 if (rec
->value
.dsize
!= sizeof(data
)) {
277 DEBUG(1, ("Found invalid value length %d in serverid.tdb\n",
278 (int)rec
->value
.dsize
));
282 memcpy(&key
, rec
->key
.dptr
, sizeof(key
));
283 memcpy(&data
, rec
->value
.dptr
, sizeof(data
));
287 id
->unique_id
= data
.unique_id
;
288 *msg_flags
= data
.msg_flags
;
292 struct serverid_traverse_read_state
{
293 int (*fn
)(const struct server_id
*id
, uint32_t msg_flags
,
298 static int serverid_traverse_read_fn(struct db_record
*rec
, void *private_data
)
300 struct serverid_traverse_read_state
*state
=
301 (struct serverid_traverse_read_state
*)private_data
;
305 if (!serverid_rec_parse(rec
, &id
, &msg_flags
)) {
308 return state
->fn(&id
, msg_flags
,state
->private_data
);
311 bool serverid_traverse_read(int (*fn
)(const struct server_id
*id
,
312 uint32_t msg_flags
, void *private_data
),
315 struct db_context
*db
;
316 struct serverid_traverse_read_state state
;
323 state
.private_data
= private_data
;
324 return db
->traverse_read(db
, serverid_traverse_read_fn
, &state
);
327 struct serverid_traverse_state
{
328 int (*fn
)(struct db_record
*rec
, const struct server_id
*id
,
329 uint32_t msg_flags
, void *private_data
);
333 static int serverid_traverse_fn(struct db_record
*rec
, void *private_data
)
335 struct serverid_traverse_state
*state
=
336 (struct serverid_traverse_state
*)private_data
;
340 if (!serverid_rec_parse(rec
, &id
, &msg_flags
)) {
343 return state
->fn(rec
, &id
, msg_flags
, state
->private_data
);
346 bool serverid_traverse(int (*fn
)(struct db_record
*rec
,
347 const struct server_id
*id
,
348 uint32_t msg_flags
, void *private_data
),
351 struct db_context
*db
;
352 struct serverid_traverse_state state
;
359 state
.private_data
= private_data
;
360 return db
->traverse(db
, serverid_traverse_fn
, &state
);
363 uint64_t serverid_get_random_unique_id(void)
365 uint64_t unique_id
= SERVERID_UNIQUE_ID_NOT_TO_VERIFY
;
367 while (unique_id
== SERVERID_UNIQUE_ID_NOT_TO_VERIFY
) {
368 generate_random_buffer((uint8_t *)&unique_id
,