nfs4acls: Introduce a helper variable
[Samba.git] / source3 / lib / serverid.c
blob39c733c113f7d90f82c730440a0fd69d946a6a53
1 /*
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/>.
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "serverid.h"
23 #include "util_tdb.h"
24 #include "dbwrap/dbwrap.h"
25 #include "dbwrap/dbwrap_open.h"
26 #include "lib/tdb_wrap/tdb_wrap.h"
27 #include "lib/param/param.h"
28 #include "ctdbd_conn.h"
29 #include "messages.h"
31 struct serverid_key {
32 pid_t pid;
33 uint32_t task_id;
34 uint32_t vnn;
37 struct serverid_data {
38 uint64_t unique_id;
39 uint32_t msg_flags;
42 static struct db_context *serverid_db(void)
44 static struct db_context *db;
45 char *db_path;
47 if (db != NULL) {
48 return db;
51 db_path = lock_path("serverid.tdb");
52 if (db_path == NULL) {
53 return NULL;
56 db = db_open(NULL, db_path, 0,
57 TDB_DEFAULT|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
58 O_RDWR|O_CREAT, 0644, DBWRAP_LOCK_ORDER_2,
59 DBWRAP_FLAG_NONE);
60 TALLOC_FREE(db_path);
61 return db;
64 bool serverid_parent_init(TALLOC_CTX *mem_ctx)
66 struct db_context *db;
68 db = serverid_db();
69 if (db == NULL) {
70 DEBUG(1, ("could not open serverid.tdb: %s\n",
71 strerror(errno)));
72 return false;
75 return true;
78 static void serverid_fill_key(const struct server_id *id,
79 struct serverid_key *key)
81 ZERO_STRUCTP(key);
82 key->pid = id->pid;
83 key->task_id = id->task_id;
84 key->vnn = id->vnn;
87 bool serverid_register(const struct server_id id, uint32_t msg_flags)
89 struct db_context *db;
90 struct serverid_key key;
91 struct serverid_data data;
92 struct db_record *rec;
93 TDB_DATA tdbkey, tdbdata;
94 NTSTATUS status;
95 bool ret = false;
97 db = serverid_db();
98 if (db == NULL) {
99 return false;
102 serverid_fill_key(&id, &key);
103 tdbkey = make_tdb_data((uint8_t *)&key, sizeof(key));
105 rec = dbwrap_fetch_locked(db, talloc_tos(), tdbkey);
106 if (rec == NULL) {
107 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
108 return false;
111 ZERO_STRUCT(data);
112 data.unique_id = id.unique_id;
113 data.msg_flags = msg_flags;
115 tdbdata = make_tdb_data((uint8_t *)&data, sizeof(data));
116 status = dbwrap_record_store(rec, tdbdata, 0);
117 if (!NT_STATUS_IS_OK(status)) {
118 DEBUG(1, ("Storing serverid.tdb record failed: %s\n",
119 nt_errstr(status)));
120 goto done;
123 if (lp_clustering() &&
124 ctdb_serverids_exist_supported(messaging_ctdbd_connection()))
126 register_with_ctdbd(messaging_ctdbd_connection(), id.unique_id,
127 NULL, NULL);
130 ret = true;
131 done:
132 TALLOC_FREE(rec);
133 return ret;
136 bool serverid_deregister(struct server_id id)
138 struct db_context *db;
139 struct serverid_key key;
140 struct db_record *rec;
141 TDB_DATA tdbkey;
142 NTSTATUS status;
143 bool ret = false;
145 db = serverid_db();
146 if (db == NULL) {
147 return false;
150 serverid_fill_key(&id, &key);
151 tdbkey = make_tdb_data((uint8_t *)&key, sizeof(key));
153 rec = dbwrap_fetch_locked(db, talloc_tos(), tdbkey);
154 if (rec == NULL) {
155 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
156 return false;
159 status = dbwrap_record_delete(rec);
160 if (!NT_STATUS_IS_OK(status)) {
161 DEBUG(1, ("Deleting serverid.tdb record failed: %s\n",
162 nt_errstr(status)));
163 goto done;
165 ret = true;
166 done:
167 TALLOC_FREE(rec);
168 return ret;
171 struct serverid_exists_state {
172 const struct server_id *id;
173 bool exists;
176 static void server_exists_parse(TDB_DATA key, TDB_DATA data, void *priv)
178 struct serverid_exists_state *state =
179 (struct serverid_exists_state *)priv;
181 if (data.dsize != sizeof(struct serverid_data)) {
182 state->exists = false;
183 return;
187 * Use memcmp, not direct compare. data.dptr might not be
188 * aligned.
190 state->exists = (memcmp(&state->id->unique_id, data.dptr,
191 sizeof(state->id->unique_id)) == 0);
194 bool serverid_exists(const struct server_id *id)
196 bool result = false;
197 bool ok = false;
199 ok = serverids_exist(id, 1, &result);
200 if (!ok) {
201 return false;
204 return result;
207 bool serverids_exist(const struct server_id *ids, int num_ids, bool *results)
209 int *todo_idx = NULL;
210 struct server_id *todo_ids = NULL;
211 bool *todo_results = NULL;
212 int todo_num = 0;
213 int *remote_idx = NULL;
214 int remote_num = 0;
215 int *verify_idx = NULL;
216 int verify_num = 0;
217 int t, idx;
218 bool result = false;
219 struct db_context *db;
221 db = serverid_db();
222 if (db == NULL) {
223 return false;
226 todo_idx = talloc_array(talloc_tos(), int, num_ids);
227 if (todo_idx == NULL) {
228 goto fail;
230 todo_ids = talloc_array(talloc_tos(), struct server_id, num_ids);
231 if (todo_ids == NULL) {
232 goto fail;
234 todo_results = talloc_array(talloc_tos(), bool, num_ids);
235 if (todo_results == NULL) {
236 goto fail;
239 remote_idx = talloc_array(talloc_tos(), int, num_ids);
240 if (remote_idx == NULL) {
241 goto fail;
243 verify_idx = talloc_array(talloc_tos(), int, num_ids);
244 if (verify_idx == NULL) {
245 goto fail;
248 for (idx=0; idx<num_ids; idx++) {
249 results[idx] = false;
251 if (server_id_is_disconnected(&ids[idx])) {
252 continue;
255 if (procid_is_me(&ids[idx])) {
256 results[idx] = true;
257 continue;
260 if (procid_is_local(&ids[idx])) {
261 bool exists = process_exists_by_pid(ids[idx].pid);
263 if (!exists) {
264 continue;
267 if (ids[idx].unique_id == SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
268 results[idx] = true;
269 continue;
272 verify_idx[verify_num] = idx;
273 verify_num += 1;
274 continue;
277 if (!lp_clustering()) {
278 continue;
281 remote_idx[remote_num] = idx;
282 remote_num += 1;
285 if (remote_num != 0 &&
286 ctdb_serverids_exist_supported(messaging_ctdbd_connection()))
288 int old_remote_num = remote_num;
290 remote_num = 0;
291 todo_num = 0;
293 for (t=0; t<old_remote_num; t++) {
294 idx = remote_idx[t];
296 if (ids[idx].unique_id == SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
297 remote_idx[remote_num] = idx;
298 remote_num += 1;
299 continue;
302 todo_idx[todo_num] = idx;
303 todo_ids[todo_num] = ids[idx];
304 todo_results[todo_num] = false;
305 todo_num += 1;
309 * Note: this only uses CTDB_CONTROL_CHECK_SRVIDS
310 * to verify that the server_id still exists,
311 * which means only the server_id.unique_id and
312 * server_id.vnn are verified, while server_id.pid
313 * is not verified at all.
315 * TODO: do we want to verify server_id.pid somehow?
317 if (!ctdb_serverids_exist(messaging_ctdbd_connection(),
318 todo_ids, todo_num, todo_results))
320 goto fail;
323 for (t=0; t<todo_num; t++) {
324 idx = todo_idx[t];
326 results[idx] = todo_results[t];
330 if (remote_num != 0) {
331 todo_num = 0;
333 for (t=0; t<remote_num; t++) {
334 idx = remote_idx[t];
335 todo_idx[todo_num] = idx;
336 todo_ids[todo_num] = ids[idx];
337 todo_results[todo_num] = false;
338 todo_num += 1;
341 if (!ctdb_processes_exist(messaging_ctdbd_connection(),
342 todo_ids, todo_num,
343 todo_results)) {
344 goto fail;
347 for (t=0; t<todo_num; t++) {
348 idx = todo_idx[t];
350 if (!todo_results[t]) {
351 continue;
354 if (ids[idx].unique_id == SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
355 results[idx] = true;
356 continue;
359 verify_idx[verify_num] = idx;
360 verify_num += 1;
364 for (t=0; t<verify_num; t++) {
365 struct serverid_exists_state state;
366 struct serverid_key key;
367 TDB_DATA tdbkey;
368 NTSTATUS status;
370 idx = verify_idx[t];
372 serverid_fill_key(&ids[idx], &key);
373 tdbkey = make_tdb_data((uint8_t *)&key, sizeof(key));
375 state.id = &ids[idx];
376 state.exists = false;
377 status = dbwrap_parse_record(db, tdbkey, server_exists_parse, &state);
378 if (!NT_STATUS_IS_OK(status)) {
379 results[idx] = false;
380 continue;
382 results[idx] = state.exists;
385 result = true;
386 fail:
387 TALLOC_FREE(verify_idx);
388 TALLOC_FREE(remote_idx);
389 TALLOC_FREE(todo_results);
390 TALLOC_FREE(todo_ids);
391 TALLOC_FREE(todo_idx);
392 return result;
395 static bool serverid_rec_parse(const struct db_record *rec,
396 struct server_id *id, uint32_t *msg_flags)
398 struct serverid_key key;
399 struct serverid_data data;
400 TDB_DATA tdbkey;
401 TDB_DATA tdbdata;
403 tdbkey = dbwrap_record_get_key(rec);
404 tdbdata = dbwrap_record_get_value(rec);
406 if (tdbkey.dsize != sizeof(key)) {
407 DEBUG(1, ("Found invalid key length %d in serverid.tdb\n",
408 (int)tdbkey.dsize));
409 return false;
411 if (tdbdata.dsize != sizeof(data)) {
412 DEBUG(1, ("Found invalid value length %d in serverid.tdb\n",
413 (int)tdbdata.dsize));
414 return false;
417 memcpy(&key, tdbkey.dptr, sizeof(key));
418 memcpy(&data, tdbdata.dptr, sizeof(data));
420 id->pid = key.pid;
421 id->task_id = key.task_id;
422 id->vnn = key.vnn;
423 id->unique_id = data.unique_id;
424 *msg_flags = data.msg_flags;
425 return true;
428 struct serverid_traverse_read_state {
429 int (*fn)(const struct server_id *id, uint32_t msg_flags,
430 void *private_data);
431 void *private_data;
434 static int serverid_traverse_read_fn(struct db_record *rec, void *private_data)
436 struct serverid_traverse_read_state *state =
437 (struct serverid_traverse_read_state *)private_data;
438 struct server_id id;
439 uint32_t msg_flags;
441 if (!serverid_rec_parse(rec, &id, &msg_flags)) {
442 return 0;
444 return state->fn(&id, msg_flags,state->private_data);
447 bool serverid_traverse_read(int (*fn)(const struct server_id *id,
448 uint32_t msg_flags, void *private_data),
449 void *private_data)
451 struct db_context *db;
452 struct serverid_traverse_read_state state;
453 NTSTATUS status;
455 db = serverid_db();
456 if (db == NULL) {
457 return false;
459 state.fn = fn;
460 state.private_data = private_data;
462 status = dbwrap_traverse_read(db, serverid_traverse_read_fn, &state,
463 NULL);
464 return NT_STATUS_IS_OK(status);
467 struct serverid_traverse_state {
468 int (*fn)(struct db_record *rec, const struct server_id *id,
469 uint32_t msg_flags, void *private_data);
470 void *private_data;
473 static int serverid_traverse_fn(struct db_record *rec, void *private_data)
475 struct serverid_traverse_state *state =
476 (struct serverid_traverse_state *)private_data;
477 struct server_id id;
478 uint32_t msg_flags;
480 if (!serverid_rec_parse(rec, &id, &msg_flags)) {
481 return 0;
483 return state->fn(rec, &id, msg_flags, state->private_data);
486 bool serverid_traverse(int (*fn)(struct db_record *rec,
487 const struct server_id *id,
488 uint32_t msg_flags, void *private_data),
489 void *private_data)
491 struct db_context *db;
492 struct serverid_traverse_state state;
493 NTSTATUS status;
495 db = serverid_db();
496 if (db == NULL) {
497 return false;
499 state.fn = fn;
500 state.private_data = private_data;
502 status = dbwrap_traverse(db, serverid_traverse_fn, &state, NULL);
503 return NT_STATUS_IS_OK(status);
506 uint64_t serverid_get_random_unique_id(void)
508 uint64_t unique_id = SERVERID_UNIQUE_ID_NOT_TO_VERIFY;
510 while (unique_id == SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
511 generate_random_buffer((uint8_t *)&unique_id,
512 sizeof(unique_id));
515 return unique_id;