s4-dns: dlz_bind9: Fix ipv6 updates
[samba.git] / source3 / lib / serverid.c
1 /*
2    Unix SMB/CIFS implementation.
3    Implementation of a reliable server_exists()
4    Copyright (C) Volker Lendecke 2010
5
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.
10
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.
15
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/>.
18 */
19
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"
30
31 struct serverid_key {
32         pid_t pid;
33         uint32_t task_id;
34         uint32_t vnn;
35 };
36
37 struct serverid_data {
38         uint64_t unique_id;
39         uint32_t msg_flags;
40 };
41
42 static struct db_context *serverid_db(void)
43 {
44         static struct db_context *db;
45
46         if (db != NULL) {
47                 return db;
48         }
49         db = db_open(NULL, lock_path("serverid.tdb"), 0,
50                      TDB_DEFAULT|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
51                      O_RDWR|O_CREAT, 0644, DBWRAP_LOCK_ORDER_2,
52                      DBWRAP_FLAG_NONE);
53         return db;
54 }
55
56 bool serverid_parent_init(TALLOC_CTX *mem_ctx)
57 {
58         struct tdb_wrap *db;
59
60         db = serverid_db();
61         if (db == NULL) {
62                 DEBUG(1, ("could not open serverid.tdb: %s\n",
63                           strerror(errno)));
64                 return false;
65         }
66
67         return true;
68 }
69
70 static void serverid_fill_key(const struct server_id *id,
71                               struct serverid_key *key)
72 {
73         ZERO_STRUCTP(key);
74         key->pid = id->pid;
75         key->task_id = id->task_id;
76         key->vnn = id->vnn;
77 }
78
79 bool serverid_register(const struct server_id id, uint32_t msg_flags)
80 {
81         struct db_context *db;
82         struct serverid_key key;
83         struct serverid_data data;
84         struct db_record *rec;
85         TDB_DATA tdbkey, tdbdata;
86         NTSTATUS status;
87         bool ret = false;
88
89         db = serverid_db();
90         if (db == NULL) {
91                 return false;
92         }
93
94         serverid_fill_key(&id, &key);
95         tdbkey = make_tdb_data((uint8_t *)&key, sizeof(key));
96
97         rec = dbwrap_fetch_locked(db, talloc_tos(), tdbkey);
98         if (rec == NULL) {
99                 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
100                 return false;
101         }
102
103         ZERO_STRUCT(data);
104         data.unique_id = id.unique_id;
105         data.msg_flags = msg_flags;
106
107         tdbdata = make_tdb_data((uint8_t *)&data, sizeof(data));
108         status = dbwrap_record_store(rec, tdbdata, 0);
109         if (!NT_STATUS_IS_OK(status)) {
110                 DEBUG(1, ("Storing serverid.tdb record failed: %s\n",
111                           nt_errstr(status)));
112                 goto done;
113         }
114
115         if (lp_clustering() &&
116             ctdb_serverids_exist_supported(messaging_ctdbd_connection()))
117         {
118                 register_with_ctdbd(messaging_ctdbd_connection(), id.unique_id);
119         }
120
121         ret = true;
122 done:
123         TALLOC_FREE(rec);
124         return ret;
125 }
126
127 bool serverid_register_msg_flags(const struct server_id id, bool do_reg,
128                                  uint32_t msg_flags)
129 {
130         struct db_context *db;
131         struct serverid_key key;
132         struct serverid_data *data;
133         struct db_record *rec;
134         TDB_DATA tdbkey;
135         TDB_DATA value;
136         NTSTATUS status;
137         bool ret = false;
138
139         db = serverid_db();
140         if (db == NULL) {
141                 return false;
142         }
143
144         serverid_fill_key(&id, &key);
145         tdbkey = make_tdb_data((uint8_t *)&key, sizeof(key));
146
147         rec = dbwrap_fetch_locked(db, talloc_tos(), tdbkey);
148         if (rec == NULL) {
149                 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
150                 return false;
151         }
152
153         value = dbwrap_record_get_value(rec);
154
155         if (value.dsize != sizeof(struct serverid_data)) {
156                 DEBUG(1, ("serverid record has unexpected size %d "
157                           "(wanted %d)\n", (int)value.dsize,
158                           (int)sizeof(struct serverid_data)));
159                 goto done;
160         }
161
162         data = (struct serverid_data *)value.dptr;
163
164         if (do_reg) {
165                 data->msg_flags |= msg_flags;
166         } else {
167                 data->msg_flags &= ~msg_flags;
168         }
169
170         status = dbwrap_record_store(rec, value, 0);
171         if (!NT_STATUS_IS_OK(status)) {
172                 DEBUG(1, ("Storing serverid.tdb record failed: %s\n",
173                           nt_errstr(status)));
174                 goto done;
175         }
176         ret = true;
177 done:
178         TALLOC_FREE(rec);
179         return ret;
180 }
181
182 bool serverid_deregister(struct server_id id)
183 {
184         struct db_context *db;
185         struct serverid_key key;
186         struct db_record *rec;
187         TDB_DATA tdbkey;
188         NTSTATUS status;
189         bool ret = false;
190
191         db = serverid_db();
192         if (db == NULL) {
193                 return false;
194         }
195
196         serverid_fill_key(&id, &key);
197         tdbkey = make_tdb_data((uint8_t *)&key, sizeof(key));
198
199         rec = dbwrap_fetch_locked(db, talloc_tos(), tdbkey);
200         if (rec == NULL) {
201                 DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
202                 return false;
203         }
204
205         status = dbwrap_record_delete(rec);
206         if (!NT_STATUS_IS_OK(status)) {
207                 DEBUG(1, ("Deleting serverid.tdb record failed: %s\n",
208                           nt_errstr(status)));
209                 goto done;
210         }
211         ret = true;
212 done:
213         TALLOC_FREE(rec);
214         return ret;
215 }
216
217 struct serverid_exists_state {
218         const struct server_id *id;
219         bool exists;
220 };
221
222 static void server_exists_parse(TDB_DATA key, TDB_DATA data, void *priv)
223 {
224         struct serverid_exists_state *state =
225                 (struct serverid_exists_state *)priv;
226
227         if (data.dsize != sizeof(struct serverid_data)) {
228                 state->exists = false;
229                 return;
230         }
231
232         /*
233          * Use memcmp, not direct compare. data.dptr might not be
234          * aligned.
235          */
236         state->exists = (memcmp(&state->id->unique_id, data.dptr,
237                                 sizeof(state->id->unique_id)) == 0);
238 }
239
240 bool serverid_exists(const struct server_id *id)
241 {
242         bool result = false;
243         bool ok = false;
244
245         ok = serverids_exist(id, 1, &result);
246         if (!ok) {
247                 return false;
248         }
249
250         return result;
251 }
252
253 bool serverids_exist(const struct server_id *ids, int num_ids, bool *results)
254 {
255         int *todo_idx = NULL;
256         struct server_id *todo_ids = NULL;
257         bool *todo_results = NULL;
258         int todo_num = 0;
259         int *remote_idx = NULL;
260         int remote_num = 0;
261         int *verify_idx = NULL;
262         int verify_num = 0;
263         int t, idx;
264         bool result = false;
265         struct db_context *db;
266
267         db = serverid_db();
268         if (db == NULL) {
269                 return false;
270         }
271
272         todo_idx = talloc_array(talloc_tos(), int, num_ids);
273         if (todo_idx == NULL) {
274                 goto fail;
275         }
276         todo_ids = talloc_array(talloc_tos(), struct server_id, num_ids);
277         if (todo_ids == NULL) {
278                 goto fail;
279         }
280         todo_results = talloc_array(talloc_tos(), bool, num_ids);
281         if (todo_results == NULL) {
282                 goto fail;
283         }
284
285         remote_idx = talloc_array(talloc_tos(), int, num_ids);
286         if (remote_idx == NULL) {
287                 goto fail;
288         }
289         verify_idx = talloc_array(talloc_tos(), int, num_ids);
290         if (verify_idx == NULL) {
291                 goto fail;
292         }
293
294         for (idx=0; idx<num_ids; idx++) {
295                 results[idx] = false;
296
297                 if (server_id_is_disconnected(&ids[idx])) {
298                         continue;
299                 }
300
301                 if (procid_is_me(&ids[idx])) {
302                         results[idx] = true;
303                         continue;
304                 }
305
306                 if (procid_is_local(&ids[idx])) {
307                         bool exists = process_exists_by_pid(ids[idx].pid);
308
309                         if (!exists) {
310                                 continue;
311                         }
312
313                         if (ids[idx].unique_id == SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
314                                 results[idx] = true;
315                                 continue;
316                         }
317
318                         verify_idx[verify_num] = idx;
319                         verify_num += 1;
320                         continue;
321                 }
322
323                 if (!lp_clustering()) {
324                         continue;
325                 }
326
327                 remote_idx[remote_num] = idx;
328                 remote_num += 1;
329         }
330
331         if (remote_num != 0 &&
332             ctdb_serverids_exist_supported(messaging_ctdbd_connection()))
333         {
334                 int old_remote_num = remote_num;
335
336                 remote_num = 0;
337                 todo_num = 0;
338
339                 for (t=0; t<old_remote_num; t++) {
340                         idx = remote_idx[t];
341
342                         if (ids[idx].unique_id == SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
343                                 remote_idx[remote_num] = idx;
344                                 remote_num += 1;
345                                 continue;
346                         }
347
348                         todo_idx[todo_num] = idx;
349                         todo_ids[todo_num] = ids[idx];
350                         todo_results[todo_num] = false;
351                         todo_num += 1;
352                 }
353
354                 /*
355                  * Note: this only uses CTDB_CONTROL_CHECK_SRVIDS
356                  * to verify that the server_id still exists,
357                  * which means only the server_id.unique_id and
358                  * server_id.vnn are verified, while server_id.pid
359                  * is not verified at all.
360                  *
361                  * TODO: do we want to verify server_id.pid somehow?
362                  */
363                 if (!ctdb_serverids_exist(messaging_ctdbd_connection(),
364                                           todo_ids, todo_num, todo_results))
365                 {
366                         goto fail;
367                 }
368
369                 for (t=0; t<todo_num; t++) {
370                         idx = todo_idx[t];
371
372                         results[idx] = todo_results[t];
373                 }
374         }
375
376         if (remote_num != 0) {
377                 todo_num = 0;
378
379                 for (t=0; t<remote_num; t++) {
380                         idx = remote_idx[t];
381                         todo_idx[todo_num] = idx;
382                         todo_ids[todo_num] = ids[idx];
383                         todo_results[todo_num] = false;
384                         todo_num += 1;
385                 }
386
387                 if (!ctdb_processes_exist(messaging_ctdbd_connection(),
388                                           todo_ids, todo_num,
389                                           todo_results)) {
390                         goto fail;
391                 }
392
393                 for (t=0; t<todo_num; t++) {
394                         idx = todo_idx[t];
395
396                         if (!todo_results[t]) {
397                                 continue;
398                         }
399
400                         if (ids[idx].unique_id == SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
401                                 results[idx] = true;
402                                 continue;
403                         }
404
405                         verify_idx[verify_num] = idx;
406                         verify_num += 1;
407                 }
408         }
409
410         for (t=0; t<verify_num; t++) {
411                 struct serverid_exists_state state;
412                 struct serverid_key key;
413                 TDB_DATA tdbkey;
414                 NTSTATUS status;
415
416                 idx = verify_idx[t];
417
418                 serverid_fill_key(&ids[idx], &key);
419                 tdbkey = make_tdb_data((uint8_t *)&key, sizeof(key));
420
421                 state.id = &ids[idx];
422                 state.exists = false;
423                 status = dbwrap_parse_record(db, tdbkey, server_exists_parse, &state);
424                 if (!NT_STATUS_IS_OK(status)) {
425                         results[idx] = false;
426                         continue;
427                 }
428                 results[idx] = state.exists;
429         }
430
431         result = true;
432 fail:
433         TALLOC_FREE(verify_idx);
434         TALLOC_FREE(remote_idx);
435         TALLOC_FREE(todo_results);
436         TALLOC_FREE(todo_ids);
437         TALLOC_FREE(todo_idx);
438         return result;
439 }
440
441 static bool serverid_rec_parse(const struct db_record *rec,
442                                struct server_id *id, uint32_t *msg_flags)
443 {
444         struct serverid_key key;
445         struct serverid_data data;
446         TDB_DATA tdbkey;
447         TDB_DATA tdbdata;
448
449         tdbkey = dbwrap_record_get_key(rec);
450         tdbdata = dbwrap_record_get_value(rec);
451
452         if (tdbkey.dsize != sizeof(key)) {
453                 DEBUG(1, ("Found invalid key length %d in serverid.tdb\n",
454                           (int)tdbkey.dsize));
455                 return false;
456         }
457         if (tdbdata.dsize != sizeof(data)) {
458                 DEBUG(1, ("Found invalid value length %d in serverid.tdb\n",
459                           (int)tdbdata.dsize));
460                 return false;
461         }
462
463         memcpy(&key, tdbkey.dptr, sizeof(key));
464         memcpy(&data, tdbdata.dptr, sizeof(data));
465
466         id->pid = key.pid;
467         id->task_id = key.task_id;
468         id->vnn = key.vnn;
469         id->unique_id = data.unique_id;
470         *msg_flags = data.msg_flags;
471         return true;
472 }
473
474 struct serverid_traverse_read_state {
475         int (*fn)(const struct server_id *id, uint32_t msg_flags,
476                   void *private_data);
477         void *private_data;
478 };
479
480 static int serverid_traverse_read_fn(struct db_record *rec, void *private_data)
481 {
482         struct serverid_traverse_read_state *state =
483                 (struct serverid_traverse_read_state *)private_data;
484         struct server_id id;
485         uint32_t msg_flags;
486
487         if (!serverid_rec_parse(rec, &id, &msg_flags)) {
488                 return 0;
489         }
490         return state->fn(&id, msg_flags,state->private_data);
491 }
492
493 bool serverid_traverse_read(int (*fn)(const struct server_id *id,
494                                       uint32_t msg_flags, void *private_data),
495                             void *private_data)
496 {
497         struct db_context *db;
498         struct serverid_traverse_read_state state;
499         NTSTATUS status;
500
501         db = serverid_db();
502         if (db == NULL) {
503                 return false;
504         }
505         state.fn = fn;
506         state.private_data = private_data;
507
508         status = dbwrap_traverse_read(db, serverid_traverse_read_fn, &state,
509                                       NULL);
510         return NT_STATUS_IS_OK(status);
511 }
512
513 struct serverid_traverse_state {
514         int (*fn)(struct db_record *rec, const struct server_id *id,
515                   uint32_t msg_flags, void *private_data);
516         void *private_data;
517 };
518
519 static int serverid_traverse_fn(struct db_record *rec, void *private_data)
520 {
521         struct serverid_traverse_state *state =
522                 (struct serverid_traverse_state *)private_data;
523         struct server_id id;
524         uint32_t msg_flags;
525
526         if (!serverid_rec_parse(rec, &id, &msg_flags)) {
527                 return 0;
528         }
529         return state->fn(rec, &id, msg_flags, state->private_data);
530 }
531
532 bool serverid_traverse(int (*fn)(struct db_record *rec,
533                                  const struct server_id *id,
534                                  uint32_t msg_flags, void *private_data),
535                             void *private_data)
536 {
537         struct db_context *db;
538         struct serverid_traverse_state state;
539         NTSTATUS status;
540
541         db = serverid_db();
542         if (db == NULL) {
543                 return false;
544         }
545         state.fn = fn;
546         state.private_data = private_data;
547
548         status = dbwrap_traverse(db, serverid_traverse_fn, &state, NULL);
549         return NT_STATUS_IS_OK(status);
550 }
551
552 uint64_t serverid_get_random_unique_id(void)
553 {
554         uint64_t unique_id = SERVERID_UNIQUE_ID_NOT_TO_VERIFY;
555
556         while (unique_id == SERVERID_UNIQUE_ID_NOT_TO_VERIFY) {
557                 generate_random_buffer((uint8_t *)&unique_id,
558                                        sizeof(unique_id));
559         }
560
561         return unique_id;
562 }