900fa345a1449f45609519ee04bd0d63ccc51be6
[samba.git] / source3 / lib / ctdbd_conn.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba internal messaging functions
4    Copyright (C) 2007 by Volker Lendecke
5    Copyright (C) 2007 by Andrew Tridgell
6
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.
11
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.
16
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/>.
19 */
20
21 #include "includes.h"
22
23 #ifdef CLUSTER_SUPPORT
24
25 #include "librpc/gen_ndr/messaging.h"
26 #include "librpc/gen_ndr/ndr_messaging.h"
27
28 /* paths to these include files come from --with-ctdb= in configure */
29 #include "ctdb.h"
30 #include "ctdb_private.h"
31
32 struct ctdbd_connection {
33         struct messaging_context *msg_ctx;
34         uint32 reqid;
35         uint32 our_vnn;
36         uint64 rand_srvid;
37         struct packet_context *pkt;
38         struct fd_event *fde;
39
40         void (*release_ip_handler)(const char *ip_addr, void *private_data);
41         void *release_ip_priv;
42 };
43
44 static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
45                               uint32_t vnn, uint32 opcode, 
46                               uint64_t srvid, uint32_t flags, TDB_DATA data, 
47                               TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
48                               int *cstatus);
49
50 /*
51  * exit on fatal communications errors with the ctdbd daemon
52  */
53 static void cluster_fatal(const char *why)
54 {
55         DEBUG(0,("cluster fatal event: %s - exiting immediately\n", why));
56         /* we don't use smb_panic() as we don't want to delay to write
57            a core file. We need to release this process id immediately
58            so that someone else can take over without getting sharing
59            violations */
60         _exit(0);
61 }
62
63 /*
64  *
65  */
66 static void ctdb_packet_dump(struct ctdb_req_header *hdr)
67 {
68         if (DEBUGLEVEL < 10) {
69                 return;
70         }
71         DEBUGADD(10, ("len=%d, magic=%x, vers=%d, gen=%d, op=%d, reqid=%d\n",
72                       (int)hdr->length, (int)hdr->ctdb_magic,
73                       (int)hdr->ctdb_version, (int)hdr->generation,
74                       (int)hdr->operation, (int)hdr->reqid));
75 }
76
77 /*
78  * Register a srvid with ctdbd
79  */
80 static NTSTATUS register_with_ctdbd(struct ctdbd_connection *conn,
81                                     uint64_t srvid)
82 {
83
84         int cstatus;
85         return ctdbd_control(conn, CTDB_CURRENT_NODE,
86                              CTDB_CONTROL_REGISTER_SRVID, srvid, 0,
87                              tdb_null, NULL, NULL, &cstatus);
88 }
89
90 /*
91  * get our vnn from the cluster
92  */
93 static NTSTATUS get_cluster_vnn(struct ctdbd_connection *conn, uint32 *vnn)
94 {
95         int32_t cstatus=-1;
96         NTSTATUS status;
97         status = ctdbd_control(conn,
98                                CTDB_CURRENT_NODE, CTDB_CONTROL_GET_PNN, 0, 0,
99                                tdb_null, NULL, NULL, &cstatus);
100         if (!NT_STATUS_IS_OK(status)) {
101                 cluster_fatal("ctdbd_control failed\n");
102         }
103         *vnn = (uint32_t)cstatus;
104         return status;
105 }
106
107 uint32 ctdbd_vnn(const struct ctdbd_connection *conn)
108 {
109         return conn->our_vnn;
110 }
111
112 /*
113  * Get us a ctdb connection
114  */
115
116 static NTSTATUS ctdbd_connect(TALLOC_CTX *mem_ctx,
117                               struct packet_context **presult)
118 {
119         struct packet_context *result;
120         const char *sockname = lp_ctdbd_socket();
121         struct sockaddr_un addr;
122         int fd;
123
124         if (!sockname || !*sockname) {
125                 sockname = CTDB_PATH;
126         }
127
128         fd = socket(AF_UNIX, SOCK_STREAM, 0);
129         if (fd == -1) {
130                 DEBUG(3, ("Could not create socket: %s\n", strerror(errno)));
131                 return map_nt_error_from_unix(errno);
132         }
133
134         ZERO_STRUCT(addr);
135         addr.sun_family = AF_UNIX;
136         strncpy(addr.sun_path, sockname, sizeof(addr.sun_path));
137
138         if (sys_connect(fd, (struct sockaddr *)&addr) == -1) {
139                 DEBUG(1, ("connect(%s) failed: %s\n", sockname,
140                           strerror(errno)));
141                 close(fd);
142                 return map_nt_error_from_unix(errno);
143         }
144
145         if (!(result = packet_init(mem_ctx, fd))) {
146                 close(fd);
147                 return NT_STATUS_NO_MEMORY;
148         }
149
150         *presult = result;
151         return NT_STATUS_OK;
152 }
153
154 /*
155  * Do we have a complete ctdb packet in the queue?
156  */
157
158 static bool ctdb_req_complete(const uint8_t *buf, size_t available,
159                               size_t *length,
160                               void *private_data)
161 {
162         uint32 msglen;
163
164         if (available < sizeof(msglen)) {
165                 return False;
166         }
167
168         msglen = *((uint32 *)buf);
169
170         DEBUG(10, ("msglen = %d\n", msglen));
171
172         if (msglen < sizeof(struct ctdb_req_header)) {
173                 DEBUG(0, ("Got invalid msglen: %d, expected at least %d for "
174                           "the req_header\n", (int)msglen,
175                           (int)sizeof(struct ctdb_req_header)));
176                 cluster_fatal("ctdbd protocol error\n");
177         }
178
179         if (available < msglen) {
180                 return false;
181         }
182
183         *length = msglen;
184         return true;
185 }
186
187 /*
188  * State necessary to defer an incoming message while we are waiting for a
189  * ctdb reply.
190  */
191
192 struct deferred_msg_state {
193         struct messaging_context *msg_ctx;
194         struct messaging_rec *rec;
195 };
196
197 /*
198  * Timed event handler for the deferred message
199  */
200
201 static void deferred_message_dispatch(struct event_context *event_ctx,
202                                       struct timed_event *te,
203                                       struct timeval now,
204                                       void *private_data)
205 {
206         struct deferred_msg_state *state = talloc_get_type_abort(
207                 private_data, struct deferred_msg_state);
208
209         messaging_dispatch_rec(state->msg_ctx, state->rec);
210         TALLOC_FREE(state);
211         TALLOC_FREE(te);
212 }
213
214 struct req_pull_state {
215         TALLOC_CTX *mem_ctx;
216         DATA_BLOB req;
217 };
218
219 /*
220  * Pull a ctdb request out of the incoming packet queue
221  */
222
223 static NTSTATUS ctdb_req_pull(uint8_t *buf, size_t length,
224                               void *private_data)
225 {
226         struct req_pull_state *state = (struct req_pull_state *)private_data;
227
228         state->req.data = talloc_move(state->mem_ctx, &buf);
229         state->req.length = length;
230         return NT_STATUS_OK;
231 }
232
233 /*
234  * Fetch a messaging_rec from an incoming ctdb style message
235  */
236
237 static struct messaging_rec *ctdb_pull_messaging_rec(TALLOC_CTX *mem_ctx,
238                                                      size_t overall_length,
239                                                      struct ctdb_req_message *msg)
240 {
241         struct messaging_rec *result;
242         DATA_BLOB blob;
243         enum ndr_err_code ndr_err;
244
245         if ((overall_length < offsetof(struct ctdb_req_message, data))
246             || (overall_length
247                 < offsetof(struct ctdb_req_message, data) + msg->datalen)) {
248
249                 cluster_fatal("got invalid msg length");
250         }
251
252         if (!(result = TALLOC_P(mem_ctx, struct messaging_rec))) {
253                 DEBUG(0, ("talloc failed\n"));
254                 return NULL;
255         }
256
257         blob = data_blob_const(msg->data, msg->datalen);
258
259         ndr_err = ndr_pull_struct_blob(
260                 &blob, result, NULL, result,
261                 (ndr_pull_flags_fn_t)ndr_pull_messaging_rec);
262
263         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
264                 DEBUG(0, ("ndr_pull_struct_blob failed: %s\n",
265                           ndr_errstr(ndr_err)));
266                 TALLOC_FREE(result);
267                 return NULL;
268         }
269
270         if (DEBUGLEVEL >= 10) {
271                 DEBUG(10, ("ctdb_pull_messaging_rec:\n"));
272                 NDR_PRINT_DEBUG(messaging_rec, result);
273         }
274
275         return result;
276 }
277
278 static NTSTATUS ctdb_packet_fd_read_sync(struct packet_context *ctx)
279 {
280         struct timeval timeout;
281         struct timeval *ptimeout;
282
283         timeout = timeval_set(lp_ctdb_timeout(), 0);
284         ptimeout = (timeout.tv_sec != 0) ? &timeout : NULL;
285
286         return packet_fd_read_sync(ctx, ptimeout);
287 }
288
289 /*
290  * Read a full ctdbd request. If we have a messaging context, defer incoming
291  * messages that might come in between.
292  */
293
294 static NTSTATUS ctdb_read_req(struct ctdbd_connection *conn, uint32 reqid,
295                               TALLOC_CTX *mem_ctx, void *result)
296 {
297         struct ctdb_req_header *hdr;
298         struct req_pull_state state;
299         NTSTATUS status;
300
301  again:
302
303         status = ctdb_packet_fd_read_sync(conn->pkt);
304
305         if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_BUSY)) {
306                 /* EAGAIN */
307                 goto again;
308         } else if (NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
309                 /* EAGAIN */
310                 goto again;
311         }
312
313         if (!NT_STATUS_IS_OK(status)) {
314                 DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status)));
315                 cluster_fatal("ctdbd died\n");
316         }
317
318  next_pkt:
319
320         ZERO_STRUCT(state);
321         state.mem_ctx = mem_ctx;
322
323         if (!packet_handler(conn->pkt, ctdb_req_complete, ctdb_req_pull,
324                             &state, &status)) {
325                 /*
326                  * Not enough data
327                  */
328                 DEBUG(10, ("not enough data from ctdb socket, retrying\n"));
329                 goto again;
330         }
331
332         if (!NT_STATUS_IS_OK(status)) {
333                 DEBUG(0, ("Could not read packet: %s\n", nt_errstr(status)));
334                 cluster_fatal("ctdbd died\n");
335         }
336
337         hdr = (struct ctdb_req_header *)state.req.data;
338
339         DEBUG(10, ("Received ctdb packet\n"));
340         ctdb_packet_dump(hdr);
341
342         if (hdr->operation == CTDB_REQ_MESSAGE) {
343                 struct timed_event *evt;
344                 struct deferred_msg_state *msg_state;
345                 struct ctdb_req_message *msg = (struct ctdb_req_message *)hdr;
346
347                 if (conn->msg_ctx == NULL) {
348                         DEBUG(1, ("Got a message without having a msg ctx, "
349                                   "dropping msg %llu\n",
350                                   (long long unsigned)msg->srvid));
351                         goto next_pkt;
352                 }
353
354                 if ((conn->release_ip_handler != NULL)
355                     && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
356                         /* must be dispatched immediately */
357                         DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
358                         conn->release_ip_handler((const char *)msg->data,
359                                                  conn->release_ip_priv);
360                         TALLOC_FREE(hdr);
361                         goto next_pkt;
362                 }
363
364                 if ((msg->srvid == CTDB_SRVID_RECONFIGURE)
365                     || (msg->srvid == CTDB_SRVID_SAMBA_NOTIFY)) {
366
367                         DEBUG(1, ("ctdb_read_req: Got %s message\n",
368                                   (msg->srvid == CTDB_SRVID_RECONFIGURE)
369                                   ? "cluster reconfigure" : "SAMBA_NOTIFY"));
370
371                         messaging_send(conn->msg_ctx, procid_self(),
372                                        MSG_SMB_BRL_VALIDATE, &data_blob_null);
373                         messaging_send(conn->msg_ctx, procid_self(),
374                                        MSG_DBWRAP_G_LOCK_RETRY,
375                                        &data_blob_null);
376                         TALLOC_FREE(hdr);
377                         goto next_pkt;
378                 }
379
380                 if (!(msg_state = TALLOC_P(talloc_autofree_context(), struct deferred_msg_state))) {
381                         DEBUG(0, ("talloc failed\n"));
382                         TALLOC_FREE(hdr);
383                         goto next_pkt;
384                 }
385
386                 if (!(msg_state->rec = ctdb_pull_messaging_rec(
387                               msg_state, state.req.length, msg))) {
388                         DEBUG(0, ("ctdbd_pull_messaging_rec failed\n"));
389                         TALLOC_FREE(msg_state);
390                         TALLOC_FREE(hdr);
391                         goto next_pkt;
392                 }
393
394                 TALLOC_FREE(hdr);
395
396                 msg_state->msg_ctx = conn->msg_ctx;
397
398                 /*
399                  * We're waiting for a call reply, but an async message has
400                  * crossed. Defer dispatching to the toplevel event loop.
401                  */
402                 evt = event_add_timed(conn->msg_ctx->event_ctx,
403                                       conn->msg_ctx->event_ctx,
404                                       timeval_zero(),
405                                       deferred_message_dispatch,
406                                       msg_state);
407                 if (evt == NULL) {
408                         DEBUG(0, ("event_add_timed failed\n"));
409                         TALLOC_FREE(msg_state);
410                         TALLOC_FREE(hdr);
411                         goto next_pkt;
412                 }
413
414                 goto next_pkt;
415         }
416
417         if (hdr->reqid != reqid) {
418                 /* we got the wrong reply */
419                 DEBUG(0,("Discarding mismatched ctdb reqid %u should have "
420                          "been %u\n", hdr->reqid, reqid));
421                 TALLOC_FREE(hdr);
422                 goto again;
423         }
424
425         *((void **)result) = talloc_move(mem_ctx, &hdr);
426
427         return NT_STATUS_OK;
428 }
429
430 /*
431  * Get us a ctdbd connection
432  */
433
434 NTSTATUS ctdbd_init_connection(TALLOC_CTX *mem_ctx,
435                                struct ctdbd_connection **pconn)
436 {
437         struct ctdbd_connection *conn;
438         NTSTATUS status;
439
440         if (!(conn = TALLOC_ZERO_P(mem_ctx, struct ctdbd_connection))) {
441                 DEBUG(0, ("talloc failed\n"));
442                 return NT_STATUS_NO_MEMORY;
443         }
444
445         status = ctdbd_connect(conn, &conn->pkt);
446
447         if (!NT_STATUS_IS_OK(status)) {
448                 DEBUG(10, ("ctdbd_connect failed: %s\n", nt_errstr(status)));
449                 goto fail;
450         }
451
452         status = get_cluster_vnn(conn, &conn->our_vnn);
453
454         if (!NT_STATUS_IS_OK(status)) {
455                 DEBUG(10, ("get_cluster_vnn failed: %s\n", nt_errstr(status)));
456                 goto fail;
457         }
458
459         generate_random_buffer((unsigned char *)&conn->rand_srvid,
460                                sizeof(conn->rand_srvid));
461
462         status = register_with_ctdbd(conn, conn->rand_srvid);
463
464         if (!NT_STATUS_IS_OK(status)) {
465                 DEBUG(5, ("Could not register random srvid: %s\n",
466                           nt_errstr(status)));
467                 goto fail;
468         }
469
470         *pconn = conn;
471         return NT_STATUS_OK;
472
473  fail:
474         TALLOC_FREE(conn);
475         return status;
476 }
477
478 /*
479  * Get us a ctdbd connection and register us as a process
480  */
481
482 NTSTATUS ctdbd_messaging_connection(TALLOC_CTX *mem_ctx,
483                                     struct ctdbd_connection **pconn)
484 {
485         struct ctdbd_connection *conn;
486         NTSTATUS status;
487
488         status = ctdbd_init_connection(mem_ctx, &conn);
489
490         if (!NT_STATUS_IS_OK(status)) {
491                 return status;
492         }
493
494         status = register_with_ctdbd(conn, (uint64_t)sys_getpid());
495         if (!NT_STATUS_IS_OK(status)) {
496                 goto fail;
497         }
498
499         status = register_with_ctdbd(conn, MSG_SRVID_SAMBA);
500         if (!NT_STATUS_IS_OK(status)) {
501                 goto fail;
502         }
503
504         status = register_with_ctdbd(conn, CTDB_SRVID_SAMBA_NOTIFY);
505         if (!NT_STATUS_IS_OK(status)) {
506                 goto fail;
507         }
508
509         *pconn = conn;
510         return NT_STATUS_OK;
511
512  fail:
513         TALLOC_FREE(conn);
514         return status;
515 }
516
517 /*
518  * Packet handler to receive and handle a ctdb message
519  */
520 static NTSTATUS ctdb_handle_message(uint8_t *buf, size_t length,
521                                     void *private_data)
522 {
523         struct ctdbd_connection *conn = talloc_get_type_abort(
524                 private_data, struct ctdbd_connection);
525         struct ctdb_req_message *msg;
526         struct messaging_rec *msg_rec;
527
528         msg = (struct ctdb_req_message *)buf;
529
530         if (msg->hdr.operation != CTDB_REQ_MESSAGE) {
531                 DEBUG(0, ("Received async msg of type %u, discarding\n",
532                           msg->hdr.operation));
533                 TALLOC_FREE(buf);
534                 return NT_STATUS_INVALID_PARAMETER;
535         }
536
537         if ((conn->release_ip_handler != NULL)
538             && (msg->srvid == CTDB_SRVID_RELEASE_IP)) {
539                 /* must be dispatched immediately */
540                 DEBUG(10, ("received CTDB_SRVID_RELEASE_IP\n"));
541                 conn->release_ip_handler((const char *)msg->data,
542                                          conn->release_ip_priv);
543                 TALLOC_FREE(buf);
544                 return NT_STATUS_OK;
545         }
546
547         SMB_ASSERT(conn->msg_ctx != NULL);
548
549         if ((msg->srvid == CTDB_SRVID_RECONFIGURE)
550             || (msg->srvid == CTDB_SRVID_SAMBA_NOTIFY)){
551                 DEBUG(0,("Got cluster reconfigure message\n"));
552                 /*
553                  * when the cluster is reconfigured or someone of the
554                  * family has passed away (SAMBA_NOTIFY), we need to
555                  * clean the brl database
556                  */
557                 messaging_send(conn->msg_ctx, procid_self(),
558                                MSG_SMB_BRL_VALIDATE, &data_blob_null);
559
560                 messaging_send(conn->msg_ctx, procid_self(),
561                                MSG_DBWRAP_G_LOCK_RETRY,
562                                &data_blob_null);
563
564                 TALLOC_FREE(buf);
565                 return NT_STATUS_OK;
566         }
567
568         /* only messages to our pid or the broadcast are valid here */
569         if (msg->srvid != sys_getpid() && msg->srvid != MSG_SRVID_SAMBA) {
570                 DEBUG(0,("Got unexpected message with srvid=%llu\n", 
571                          (unsigned long long)msg->srvid));
572                 TALLOC_FREE(buf);
573                 return NT_STATUS_OK;
574         }
575
576         if (!(msg_rec = ctdb_pull_messaging_rec(NULL, length, msg))) {
577                 DEBUG(10, ("ctdb_pull_messaging_rec failed\n"));
578                 TALLOC_FREE(buf);
579                 return NT_STATUS_NO_MEMORY;
580         }
581
582         messaging_dispatch_rec(conn->msg_ctx, msg_rec);
583
584         TALLOC_FREE(msg_rec);
585         TALLOC_FREE(buf);
586         return NT_STATUS_OK;
587 }
588
589 /*
590  * The ctdbd socket is readable asynchronuously
591  */
592
593 static void ctdbd_socket_handler(struct event_context *event_ctx,
594                                  struct fd_event *event,
595                                  uint16 flags,
596                                  void *private_data)
597 {
598         struct ctdbd_connection *conn = talloc_get_type_abort(
599                 private_data, struct ctdbd_connection);
600
601         NTSTATUS status;
602
603         status = packet_fd_read(conn->pkt);
604
605         if (!NT_STATUS_IS_OK(status)) {
606                 DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status)));
607                 cluster_fatal("ctdbd died\n");
608         }
609
610         while (packet_handler(conn->pkt, ctdb_req_complete,
611                               ctdb_handle_message, conn, &status)) {
612                 if (!NT_STATUS_IS_OK(status)) {
613                         DEBUG(10, ("could not handle incoming message: %s\n",
614                                    nt_errstr(status)));
615                 }
616         }
617 }
618
619 /*
620  * Prepare a ctdbd connection to receive messages
621  */
622
623 NTSTATUS ctdbd_register_msg_ctx(struct ctdbd_connection *conn,
624                                 struct messaging_context *msg_ctx)
625 {
626         SMB_ASSERT(conn->msg_ctx == NULL);
627         SMB_ASSERT(conn->fde == NULL);
628
629         if (!(conn->fde = event_add_fd(msg_ctx->event_ctx, conn,
630                                        packet_get_fd(conn->pkt),
631                                        EVENT_FD_READ,
632                                        ctdbd_socket_handler,
633                                        conn))) {
634                 DEBUG(0, ("event_add_fd failed\n"));
635                 return NT_STATUS_NO_MEMORY;
636         }
637
638         conn->msg_ctx = msg_ctx;
639
640         return NT_STATUS_OK;
641 }
642
643 /*
644  * Send a messaging message across a ctdbd
645  */
646
647 NTSTATUS ctdbd_messaging_send(struct ctdbd_connection *conn,
648                               uint32 dst_vnn, uint64 dst_srvid,
649                               struct messaging_rec *msg)
650 {
651         struct ctdb_req_message r;
652         TALLOC_CTX *mem_ctx;
653         DATA_BLOB blob;
654         NTSTATUS status;
655         enum ndr_err_code ndr_err;
656
657         if (!(mem_ctx = talloc_init("ctdbd_messaging_send"))) {
658                 DEBUG(0, ("talloc failed\n"));
659                 return NT_STATUS_NO_MEMORY;
660         }
661
662         ndr_err = ndr_push_struct_blob(
663                 &blob, mem_ctx, NULL, msg,
664                 (ndr_push_flags_fn_t)ndr_push_messaging_rec);
665
666         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
667                 DEBUG(0, ("ndr_push_struct_blob failed: %s\n",
668                           ndr_errstr(ndr_err)));
669                 status = ndr_map_error2ntstatus(ndr_err);
670                 goto fail;
671         }
672
673         r.hdr.length = offsetof(struct ctdb_req_message, data) + blob.length;
674         r.hdr.ctdb_magic = CTDB_MAGIC;
675         r.hdr.ctdb_version = CTDB_VERSION;
676         r.hdr.generation = 1;
677         r.hdr.operation  = CTDB_REQ_MESSAGE;
678         r.hdr.destnode   = dst_vnn;
679         r.hdr.srcnode    = conn->our_vnn;
680         r.hdr.reqid      = 0;
681         r.srvid          = dst_srvid;
682         r.datalen        = blob.length;
683
684         DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
685         ctdb_packet_dump(&r.hdr);
686
687         status = packet_send(
688                 conn->pkt, 2,
689                 data_blob_const(&r, offsetof(struct ctdb_req_message, data)),
690                 blob);
691
692         if (!NT_STATUS_IS_OK(status)) {
693                 DEBUG(0, ("packet_send failed: %s\n", nt_errstr(status)));
694                 goto fail;
695         }
696
697         status = packet_flush(conn->pkt);
698
699         if (!NT_STATUS_IS_OK(status)) {
700                 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
701                 cluster_fatal("cluster dispatch daemon msg write error\n");
702         }
703
704         status = NT_STATUS_OK;
705  fail:
706         TALLOC_FREE(mem_ctx);
707         return status;
708 }
709
710 /*
711  * send/recv a generic ctdb control message
712  */
713 static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
714                               uint32_t vnn, uint32 opcode, 
715                               uint64_t srvid, uint32_t flags, 
716                               TDB_DATA data, 
717                               TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
718                               int *cstatus)
719 {
720         struct ctdb_req_control req;
721         struct ctdb_reply_control *reply = NULL;
722         struct ctdbd_connection *new_conn = NULL;
723         NTSTATUS status;
724
725         /* the samba3 ctdb code can't handle NOREPLY yet */
726         flags &= ~CTDB_CTRL_FLAG_NOREPLY;
727
728         if (conn == NULL) {
729                 status = ctdbd_init_connection(NULL, &new_conn);
730
731                 if (!NT_STATUS_IS_OK(status)) {
732                         DEBUG(10, ("Could not init temp connection: %s\n",
733                                    nt_errstr(status)));
734                         goto fail;
735                 }
736
737                 conn = new_conn;
738         }
739
740         ZERO_STRUCT(req);
741         req.hdr.length = offsetof(struct ctdb_req_control, data) + data.dsize;
742         req.hdr.ctdb_magic   = CTDB_MAGIC;
743         req.hdr.ctdb_version = CTDB_VERSION;
744         req.hdr.operation    = CTDB_REQ_CONTROL;
745         req.hdr.reqid        = ++conn->reqid;
746         req.hdr.destnode     = vnn;
747         req.opcode           = opcode;
748         req.srvid            = srvid;
749         req.datalen          = data.dsize;
750
751         DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
752         ctdb_packet_dump(&req.hdr);
753
754         status = packet_send(
755                 conn->pkt, 2,
756                 data_blob_const(&req, offsetof(struct ctdb_req_control, data)),
757                 data_blob_const(data.dptr, data.dsize));
758
759         if (!NT_STATUS_IS_OK(status)) {
760                 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status)));
761                 goto fail;
762         }
763
764         status = packet_flush(conn->pkt);
765
766         if (!NT_STATUS_IS_OK(status)) {
767                 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
768                 cluster_fatal("cluster dispatch daemon control write error\n");
769         }
770
771         if (flags & CTDB_CTRL_FLAG_NOREPLY) {
772                 TALLOC_FREE(new_conn);
773                 return NT_STATUS_OK;
774         }
775
776         status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
777
778         if (!NT_STATUS_IS_OK(status)) {
779                 DEBUG(10, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
780                 goto fail;
781         }
782
783         if (reply->hdr.operation != CTDB_REPLY_CONTROL) {
784                 DEBUG(0, ("received invalid reply\n"));
785                 goto fail;
786         }
787
788         if (outdata) {
789                 if (!(outdata->dptr = (uint8 *)talloc_memdup(
790                               mem_ctx, reply->data, reply->datalen))) {
791                         TALLOC_FREE(reply);
792                         return NT_STATUS_NO_MEMORY;
793                 }
794                 outdata->dsize = reply->datalen;
795         }
796         if (cstatus) {
797                 (*cstatus) = reply->status;
798         }
799
800         status = NT_STATUS_OK;
801
802  fail:
803         TALLOC_FREE(new_conn);
804         TALLOC_FREE(reply);
805         return status;
806 }
807
808 /*
809  * see if a remote process exists
810  */
811 bool ctdbd_process_exists(struct ctdbd_connection *conn, uint32 vnn, pid_t pid)
812 {
813         NTSTATUS status;
814         TDB_DATA data;
815         int32_t cstatus;
816
817         data.dptr = (uint8_t*)&pid;
818         data.dsize = sizeof(pid);
819
820         status = ctdbd_control(conn, vnn, CTDB_CONTROL_PROCESS_EXISTS, 0, 0,
821                                data, NULL, NULL, &cstatus);
822         if (!NT_STATUS_IS_OK(status)) {
823                 DEBUG(0, (__location__ " ctdb_control for process_exists "
824                           "failed\n"));
825                 return False;
826         }
827
828         return cstatus == 0;
829 }
830
831 /*
832  * Get a db path
833  */
834 char *ctdbd_dbpath(struct ctdbd_connection *conn,
835                    TALLOC_CTX *mem_ctx, uint32_t db_id)
836 {
837         NTSTATUS status;
838         TDB_DATA data;
839         int32_t cstatus;
840
841         data.dptr = (uint8_t*)&db_id;
842         data.dsize = sizeof(db_id);
843
844         status = ctdbd_control(conn, CTDB_CURRENT_NODE,
845                                CTDB_CONTROL_GETDBPATH, 0, 0, data, 
846                                mem_ctx, &data, &cstatus);
847         if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
848                 DEBUG(0,(__location__ " ctdb_control for getdbpath failed\n"));
849                 return NULL;
850         }
851
852         return (char *)data.dptr;
853 }
854
855 /*
856  * attach to a ctdb database
857  */
858 NTSTATUS ctdbd_db_attach(struct ctdbd_connection *conn,
859                          const char *name, uint32_t *db_id, int tdb_flags)
860 {
861         NTSTATUS status;
862         TDB_DATA data;
863         int32_t cstatus;
864         bool persistent = (tdb_flags & TDB_CLEAR_IF_FIRST) == 0;
865
866         data.dptr = (uint8_t*)name;
867         data.dsize = strlen(name)+1;
868
869         status = ctdbd_control(conn, CTDB_CURRENT_NODE,
870                                persistent
871                                ? CTDB_CONTROL_DB_ATTACH_PERSISTENT
872                                : CTDB_CONTROL_DB_ATTACH,
873                                0, 0, data, NULL, &data, &cstatus);
874         if (!NT_STATUS_IS_OK(status)) {
875                 DEBUG(0, (__location__ " ctdb_control for db_attach "
876                           "failed: %s\n", nt_errstr(status)));
877                 return status;
878         }
879
880         if (cstatus != 0 || data.dsize != sizeof(uint32_t)) {
881                 DEBUG(0,(__location__ " ctdb_control for db_attach failed\n"));
882                 return NT_STATUS_INTERNAL_ERROR;
883         }
884
885         *db_id = *(uint32_t *)data.dptr;
886         talloc_free(data.dptr);
887
888         if (!(tdb_flags & TDB_SEQNUM)) {
889                 return NT_STATUS_OK;
890         }
891
892         data.dptr = (uint8_t *)db_id;
893         data.dsize = sizeof(*db_id);
894
895         status = ctdbd_control(conn, CTDB_CURRENT_NODE,
896                                CTDB_CONTROL_ENABLE_SEQNUM, 0, 0, data, 
897                                NULL, NULL, &cstatus);
898         if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
899                 DEBUG(0,(__location__ " ctdb_control for enable seqnum "
900                          "failed\n"));
901                 return NT_STATUS_IS_OK(status) ? NT_STATUS_INTERNAL_ERROR :
902                         status;
903         }
904
905         return NT_STATUS_OK;
906 }
907
908 /*
909  * force the migration of a record to this node
910  */
911 NTSTATUS ctdbd_migrate(struct ctdbd_connection *conn, uint32 db_id,
912                        TDB_DATA key)
913 {
914         struct ctdb_req_call req;
915         struct ctdb_reply_call *reply;
916         NTSTATUS status;
917
918         ZERO_STRUCT(req);
919
920         req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
921         req.hdr.ctdb_magic   = CTDB_MAGIC;
922         req.hdr.ctdb_version = CTDB_VERSION;
923         req.hdr.operation    = CTDB_REQ_CALL;
924         req.hdr.reqid        = ++conn->reqid;
925         req.flags            = CTDB_IMMEDIATE_MIGRATION;
926         req.callid           = CTDB_NULL_FUNC;
927         req.db_id            = db_id;
928         req.keylen           = key.dsize;
929
930         DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
931         ctdb_packet_dump(&req.hdr);
932
933         status = packet_send(
934                 conn->pkt, 2,
935                 data_blob_const(&req, offsetof(struct ctdb_req_call, data)),
936                 data_blob_const(key.dptr, key.dsize));
937
938         if (!NT_STATUS_IS_OK(status)) {
939                 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status)));
940                 return status;
941         }
942
943         status = packet_flush(conn->pkt);
944
945         if (!NT_STATUS_IS_OK(status)) {
946                 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
947                 cluster_fatal("cluster dispatch daemon control write error\n");
948         }
949
950         status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
951
952         if (!NT_STATUS_IS_OK(status)) {
953                 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
954                 goto fail;
955         }
956
957         if (reply->hdr.operation != CTDB_REPLY_CALL) {
958                 DEBUG(0, ("received invalid reply\n"));
959                 status = NT_STATUS_INTERNAL_ERROR;
960                 goto fail;
961         }
962
963         status = NT_STATUS_OK;
964  fail:
965
966         TALLOC_FREE(reply);
967         return status;
968 }
969
970 /*
971  * remotely fetch a record without locking it or forcing a migration
972  */
973 NTSTATUS ctdbd_fetch(struct ctdbd_connection *conn, uint32 db_id,
974                      TDB_DATA key, TALLOC_CTX *mem_ctx, TDB_DATA *data)
975 {
976         struct ctdb_req_call req;
977         struct ctdb_reply_call *reply;
978         NTSTATUS status;
979
980         ZERO_STRUCT(req);
981
982         req.hdr.length = offsetof(struct ctdb_req_call, data) + key.dsize;
983         req.hdr.ctdb_magic   = CTDB_MAGIC;
984         req.hdr.ctdb_version = CTDB_VERSION;
985         req.hdr.operation    = CTDB_REQ_CALL;
986         req.hdr.reqid        = ++conn->reqid;
987         req.flags            = 0;
988         req.callid           = CTDB_FETCH_FUNC;
989         req.db_id            = db_id;
990         req.keylen           = key.dsize;
991
992         status = packet_send(
993                 conn->pkt, 2,
994                 data_blob_const(&req, offsetof(struct ctdb_req_call, data)),
995                 data_blob_const(key.dptr, key.dsize));
996
997         if (!NT_STATUS_IS_OK(status)) {
998                 DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status)));
999                 return status;
1000         }
1001
1002         status = packet_flush(conn->pkt);
1003
1004         if (!NT_STATUS_IS_OK(status)) {
1005                 DEBUG(3, ("write to ctdbd failed: %s\n", nt_errstr(status)));
1006                 cluster_fatal("cluster dispatch daemon control write error\n");
1007         }
1008
1009         status = ctdb_read_req(conn, req.hdr.reqid, NULL, (void *)&reply);
1010
1011         if (!NT_STATUS_IS_OK(status)) {
1012                 DEBUG(0, ("ctdb_read_req failed: %s\n", nt_errstr(status)));
1013                 goto fail;
1014         }
1015
1016         if (reply->hdr.operation != CTDB_REPLY_CALL) {
1017                 DEBUG(0, ("received invalid reply\n"));
1018                 status = NT_STATUS_INTERNAL_ERROR;
1019                 goto fail;
1020         }
1021
1022         data->dsize = reply->datalen;
1023         if (data->dsize == 0) {
1024                 data->dptr = NULL;
1025                 goto done;
1026         }
1027
1028         data->dptr = (uint8 *)talloc_memdup(mem_ctx, &reply->data[0],
1029                                             reply->datalen);
1030         if (data->dptr == NULL) {
1031                 DEBUG(0, ("talloc failed\n"));
1032                 status = NT_STATUS_NO_MEMORY;
1033                 goto fail;
1034         }
1035
1036  done:
1037         status = NT_STATUS_OK;
1038  fail:
1039         TALLOC_FREE(reply);
1040         return status;
1041 }
1042
1043 struct ctdbd_traverse_state {
1044         void (*fn)(TDB_DATA key, TDB_DATA data, void *private_data);
1045         void *private_data;
1046 };
1047
1048 /*
1049  * Handle a traverse record coming in on the ctdbd connection
1050  */
1051
1052 static NTSTATUS ctdb_traverse_handler(uint8_t *buf, size_t length,
1053                                       void *private_data)
1054 {
1055         struct ctdbd_traverse_state *state =
1056                 (struct ctdbd_traverse_state *)private_data;
1057
1058         struct ctdb_req_message *m;
1059         struct ctdb_rec_data *d;
1060         TDB_DATA key, data;
1061
1062         m = (struct ctdb_req_message *)buf;
1063
1064         if (length < sizeof(*m) || m->hdr.length != length) {
1065                 DEBUG(0, ("Got invalid message of length %d\n", (int)length));
1066                 TALLOC_FREE(buf);
1067                 return NT_STATUS_UNEXPECTED_IO_ERROR;
1068         }
1069
1070         d = (struct ctdb_rec_data *)&m->data[0];
1071         if (m->datalen < sizeof(uint32_t) || m->datalen != d->length) {
1072                 DEBUG(0, ("Got invalid traverse data of length %d\n",
1073                           (int)m->datalen));
1074                 TALLOC_FREE(buf);
1075                 return NT_STATUS_UNEXPECTED_IO_ERROR;
1076         }
1077
1078         key.dsize = d->keylen;
1079         key.dptr  = &d->data[0];
1080         data.dsize = d->datalen;
1081         data.dptr = &d->data[d->keylen];                
1082
1083         if (key.dsize == 0 && data.dsize == 0) {
1084                 /* end of traverse */
1085                 return NT_STATUS_END_OF_FILE;
1086         }
1087
1088         if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
1089                 DEBUG(0, ("Got invalid ltdb header length %d\n",
1090                           (int)data.dsize));
1091                 TALLOC_FREE(buf);
1092                 return NT_STATUS_UNEXPECTED_IO_ERROR;
1093         }
1094         data.dsize -= sizeof(struct ctdb_ltdb_header);
1095         data.dptr += sizeof(struct ctdb_ltdb_header);
1096
1097         if (state->fn) {
1098                 state->fn(key, data, state->private_data);
1099         }
1100
1101         TALLOC_FREE(buf);
1102         return NT_STATUS_OK;
1103 }
1104
1105 /*
1106   Traverse a ctdb database. This uses a kind-of hackish way to open a second
1107   connection to ctdbd to avoid the hairy recursive and async problems with
1108   everything in-line.
1109 */
1110
1111 NTSTATUS ctdbd_traverse(uint32 db_id,
1112                         void (*fn)(TDB_DATA key, TDB_DATA data,
1113                                    void *private_data),
1114                         void *private_data)
1115 {
1116         struct ctdbd_connection *conn;
1117         NTSTATUS status;
1118
1119         TDB_DATA data;
1120         struct ctdb_traverse_start t;
1121         int cstatus;
1122         struct ctdbd_traverse_state state;
1123
1124         status = ctdbd_init_connection(NULL, &conn);
1125         if (!NT_STATUS_IS_OK(status)) {
1126                 DEBUG(0, ("ctdbd_init_connection failed: %s\n",
1127                           nt_errstr(status)));
1128                 return status;
1129         }
1130
1131         t.db_id = db_id;
1132         t.srvid = conn->rand_srvid;
1133         t.reqid = ++conn->reqid;
1134
1135         data.dptr = (uint8_t *)&t;
1136         data.dsize = sizeof(t);
1137
1138         status = ctdbd_control(conn, CTDB_CURRENT_NODE,
1139                                CTDB_CONTROL_TRAVERSE_START, conn->rand_srvid, 0,
1140                                data, NULL, NULL, &cstatus);
1141
1142         if (!NT_STATUS_IS_OK(status) || (cstatus != 0)) {
1143
1144                 DEBUG(0,("ctdbd_control failed: %s, %d\n", nt_errstr(status),
1145                          cstatus));
1146
1147                 if (NT_STATUS_IS_OK(status)) {
1148                         /*
1149                          * We need a mapping here
1150                          */
1151                         status = NT_STATUS_UNSUCCESSFUL;
1152                 }
1153                 goto done;
1154         }
1155
1156         state.fn = fn;
1157         state.private_data = private_data;
1158
1159         while (True) {
1160
1161                 status = NT_STATUS_OK;
1162
1163                 if (packet_handler(conn->pkt, ctdb_req_complete,
1164                                    ctdb_traverse_handler, &state, &status)) {
1165
1166                         if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
1167                                 status = NT_STATUS_OK;
1168                                 break;
1169                         }
1170
1171                         /*
1172                          * There might be more in the queue
1173                          */
1174                         continue;
1175                 }
1176
1177                 if (!NT_STATUS_IS_OK(status)) {
1178                         break;
1179                 }
1180
1181                 status = ctdb_packet_fd_read_sync(conn->pkt);
1182
1183                 if (NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
1184                         /*
1185                          * There might be more in the queue
1186                          */
1187                         continue;
1188                 }
1189
1190                 if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
1191                         status = NT_STATUS_OK;
1192                         break;
1193                 }
1194
1195                 if (!NT_STATUS_IS_OK(status)) {
1196                         DEBUG(0, ("packet_fd_read_sync failed: %s\n", nt_errstr(status)));
1197                         cluster_fatal("ctdbd died\n");
1198                 }
1199         }
1200
1201  done:
1202         TALLOC_FREE(conn);
1203         return status;
1204 }
1205
1206 /*
1207    This is used to canonicalize a ctdb_sock_addr structure.
1208 */
1209 static void smbd_ctdb_canonicalize_ip(const struct sockaddr_storage *in,
1210                                       struct sockaddr_storage *out)
1211 {
1212         memcpy(out, in, sizeof (*out));
1213
1214 #ifdef HAVE_IPV6
1215         if (in->ss_family == AF_INET6) {
1216                 const char prefix[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
1217                 const struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)in;
1218                 struct sockaddr_in *out4 = (struct sockaddr_in *)out;
1219                 if (memcmp(&in6->sin6_addr, prefix, 12) == 0) {
1220                         memset(out, 0, sizeof(*out));
1221 #ifdef HAVE_SOCK_SIN_LEN
1222                         out4->sin_len = sizeof(*out);
1223 #endif
1224                         out4->sin_family = AF_INET;
1225                         out4->sin_port   = in6->sin6_port;
1226                         memcpy(&out4->sin_addr, &in6->sin6_addr.s6_addr32[3], 4);
1227                 }
1228         }
1229 #endif
1230 }
1231
1232 /*
1233  * Register us as a server for a particular tcp connection
1234  */
1235
1236 NTSTATUS ctdbd_register_ips(struct ctdbd_connection *conn,
1237                             const struct sockaddr_storage *_server,
1238                             const struct sockaddr_storage *_client,
1239                             void (*release_ip_handler)(const char *ip_addr,
1240                                                        void *private_data),
1241                             void *private_data)
1242 {
1243         /*
1244          * we still use ctdb_control_tcp for ipv4
1245          * because we want to work against older ctdb
1246          * versions at runtime
1247          */
1248         struct ctdb_control_tcp p4;
1249 #ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
1250         struct ctdb_control_tcp_addr p;
1251 #endif
1252         TDB_DATA data;
1253         NTSTATUS status;
1254         struct sockaddr_storage client;
1255         struct sockaddr_storage server;
1256
1257         /*
1258          * Only one connection so far
1259          */
1260         SMB_ASSERT(conn->release_ip_handler == NULL);
1261
1262         smbd_ctdb_canonicalize_ip(_client, &client);
1263         smbd_ctdb_canonicalize_ip(_server, &server);
1264
1265         switch (client.ss_family) {
1266         case AF_INET:
1267                 p4.dest = *(struct sockaddr_in *)&server;
1268                 p4.src = *(struct sockaddr_in *)&client;
1269                 data.dptr = (uint8_t *)&p4;
1270                 data.dsize = sizeof(p4);
1271                 break;
1272 #ifdef HAVE_STRUCT_CTDB_CONTROL_TCP_ADDR
1273         case AF_INET6:
1274                 p.dest.ip6 = *(struct sockaddr_in6 *)&server;
1275                 p.src.ip6 = *(struct sockaddr_in6 *)&client;
1276                 data.dptr = (uint8_t *)&p;
1277                 data.dsize = sizeof(p);
1278                 break;
1279 #endif
1280         default:
1281                 return NT_STATUS_INTERNAL_ERROR;
1282         }
1283
1284         conn->release_ip_handler = release_ip_handler;
1285
1286         /*
1287          * We want to be told about IP releases
1288          */
1289
1290         status = register_with_ctdbd(conn, CTDB_SRVID_RELEASE_IP);
1291         if (!NT_STATUS_IS_OK(status)) {
1292                 return status;
1293         }
1294
1295         /*
1296          * inform ctdb of our tcp connection, so if IP takeover happens ctdb
1297          * can send an extra ack to trigger a reset for our client, so it
1298          * immediately reconnects
1299          */
1300         return ctdbd_control(conn, CTDB_CURRENT_NODE, 
1301                              CTDB_CONTROL_TCP_CLIENT, 0,
1302                              CTDB_CTRL_FLAG_NOREPLY, data, NULL, NULL, NULL);
1303 }
1304
1305 /*
1306  * We want to handle reconfigure events
1307  */
1308 NTSTATUS ctdbd_register_reconfigure(struct ctdbd_connection *conn)
1309 {
1310         return register_with_ctdbd(conn, CTDB_SRVID_RECONFIGURE);
1311 }
1312
1313 /*
1314   call a control on the local node
1315  */
1316 NTSTATUS ctdbd_control_local(struct ctdbd_connection *conn, uint32 opcode, 
1317                              uint64_t srvid, uint32_t flags, TDB_DATA data, 
1318                              TALLOC_CTX *mem_ctx, TDB_DATA *outdata,
1319                              int *cstatus)
1320 {
1321         return ctdbd_control(conn, CTDB_CURRENT_NODE, opcode, srvid, flags, data, mem_ctx, outdata, cstatus);
1322 }
1323
1324 NTSTATUS ctdb_watch_us(struct ctdbd_connection *conn)
1325 {
1326         struct ctdb_client_notify_register reg_data;
1327         size_t struct_len;
1328         NTSTATUS status;
1329         int cstatus;
1330
1331         reg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1332         reg_data.len = 1;
1333         reg_data.notify_data[0] = 0;
1334
1335         struct_len = offsetof(struct ctdb_client_notify_register,
1336                               notify_data) + reg_data.len;
1337
1338         status = ctdbd_control_local(
1339                 conn, CTDB_CONTROL_REGISTER_NOTIFY, conn->rand_srvid, 0,
1340                 make_tdb_data((uint8_t *)&reg_data, struct_len),
1341                 NULL, NULL, &cstatus);
1342         if (!NT_STATUS_IS_OK(status)) {
1343                 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1344                           nt_errstr(status)));
1345         }
1346         return status;
1347 }
1348
1349 NTSTATUS ctdb_unwatch(struct ctdbd_connection *conn)
1350 {
1351         struct ctdb_client_notify_deregister dereg_data;
1352         NTSTATUS status;
1353         int cstatus;
1354
1355         dereg_data.srvid = CTDB_SRVID_SAMBA_NOTIFY;
1356
1357         status = ctdbd_control_local(
1358                 conn, CTDB_CONTROL_DEREGISTER_NOTIFY, conn->rand_srvid, 0,
1359                 make_tdb_data((uint8_t *)&dereg_data, sizeof(dereg_data)),
1360                 NULL, NULL, &cstatus);
1361         if (!NT_STATUS_IS_OK(status)) {
1362                 DEBUG(1, ("ctdbd_control_local failed: %s\n",
1363                           nt_errstr(status)));
1364         }
1365         return status;
1366 }
1367
1368 #else
1369
1370 NTSTATUS ctdbd_init_connection(TALLOC_CTX *mem_ctx,
1371                                struct ctdbd_connection **pconn)
1372 {
1373         return NT_STATUS_NOT_IMPLEMENTED;
1374 }
1375
1376 #endif