messaging4: Simplify imessaging_path
[Samba.git] / source4 / lib / messaging / messaging.c
bloba67a58a6e06fc96cae2bef1331f52f8ed6e64a29
1 /*
2 Unix SMB/CIFS implementation.
4 Samba internal messaging functions
6 Copyright (C) Andrew Tridgell 2004
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "lib/events/events.h"
24 #include "system/filesys.h"
25 #include "messaging/messaging.h"
26 #include "../lib/util/dlinklist.h"
27 #include "lib/socket/socket.h"
28 #include "librpc/gen_ndr/ndr_irpc.h"
29 #include "lib/messaging/irpc.h"
30 #include "lib/tdb_wrap/tdb_wrap.h"
31 #include "../lib/util/unix_privs.h"
32 #include "librpc/rpc/dcerpc.h"
33 #include "../lib/tdb_compat/tdb_compat.h"
34 #include "../lib/util/util_tdb.h"
35 #include "cluster/cluster.h"
36 #include "../lib/util/tevent_ntstatus.h"
37 #include "lib/param/param.h"
39 /* change the message version with any incompatible changes in the protocol */
40 #define IMESSAGING_VERSION 1
42 static struct tdb_wrap *irpc_namedb_open(TALLOC_CTX *mem_ctx, const char *base_path,
43 struct loadparm_context *lp_ctx);
46 a pending irpc call
48 struct irpc_request {
49 struct imessaging_context *msg_ctx;
50 int callid;
51 struct {
52 void (*handler)(struct irpc_request *irpc, struct irpc_message *m);
53 void *private_data;
54 } incoming;
57 struct imessaging_context {
58 struct server_id server_id;
59 struct socket_context *sock;
60 const char *base_path;
61 const char *path;
62 struct dispatch_fn **dispatch;
63 uint32_t num_types;
64 struct idr_context *dispatch_tree;
65 struct imessaging_rec *pending;
66 struct imessaging_rec *retry_queue;
67 struct irpc_list *irpc;
68 struct idr_context *idr;
69 const char **names;
70 struct tdb_wrap *names_db;
71 struct timeval start_time;
72 struct tevent_timer *retry_te;
73 struct {
74 struct tevent_fd *fde;
75 } event;
78 /* we have a linked list of dispatch handlers for each msg_type that
79 this messaging server can deal with */
80 struct dispatch_fn {
81 struct dispatch_fn *next, *prev;
82 uint32_t msg_type;
83 void *private_data;
84 msg_callback_t fn;
87 /* an individual message */
88 struct imessaging_rec {
89 struct imessaging_rec *next, *prev;
90 struct imessaging_context *msg;
91 const char *path;
93 struct imessaging_header {
94 uint32_t version;
95 uint32_t msg_type;
96 struct server_id from;
97 struct server_id to;
98 uint32_t length;
99 } *header;
101 DATA_BLOB packet;
102 uint32_t retries;
106 static void irpc_handler(struct imessaging_context *, void *,
107 uint32_t, struct server_id, DATA_BLOB *);
111 A useful function for testing the message system.
113 static void ping_message(struct imessaging_context *msg, void *private_data,
114 uint32_t msg_type, struct server_id src, DATA_BLOB *data)
116 struct server_id_buf idbuf;
117 DEBUG(1,("INFO: Received PING message from server %s [%.*s]\n",
118 server_id_str_buf(src, &idbuf), (int)data->length,
119 data->data?(const char *)data->data:""));
120 imessaging_send(msg, src, MSG_PONG, data);
124 return uptime of messaging server via irpc
126 static NTSTATUS irpc_uptime(struct irpc_message *msg,
127 struct irpc_uptime *r)
129 struct imessaging_context *ctx = talloc_get_type(msg->private_data, struct imessaging_context);
130 *r->out.start_time = timeval_to_nttime(&ctx->start_time);
131 return NT_STATUS_OK;
135 return the path to a messaging socket
137 static char *imessaging_path(struct imessaging_context *msg, struct server_id server_id)
139 struct server_id_buf buf;
141 return talloc_asprintf(msg, "%s/msg.%s", msg->base_path,
142 server_id_str_buf(server_id, &buf));
146 dispatch a fully received message
148 note that this deliberately can match more than one message handler
149 per message. That allows a single messasging context to register
150 (for example) a debug handler for more than one piece of code
152 static void imessaging_dispatch(struct imessaging_context *msg, struct imessaging_rec *rec)
154 struct dispatch_fn *d, *next;
156 /* temporary IDs use an idtree, the rest use a array of pointers */
157 if (rec->header->msg_type >= MSG_TMP_BASE) {
158 d = (struct dispatch_fn *)idr_find(msg->dispatch_tree,
159 rec->header->msg_type);
160 } else if (rec->header->msg_type < msg->num_types) {
161 d = msg->dispatch[rec->header->msg_type];
162 } else {
163 d = NULL;
166 for (; d; d = next) {
167 DATA_BLOB data;
168 next = d->next;
169 data.data = rec->packet.data + sizeof(*rec->header);
170 data.length = rec->header->length;
171 d->fn(msg, d->private_data, d->msg_type, rec->header->from, &data);
173 rec->header->length = 0;
177 handler for messages that arrive from other nodes in the cluster
179 static void cluster_message_handler(struct imessaging_context *msg, DATA_BLOB packet)
181 struct imessaging_rec *rec;
183 rec = talloc(msg, struct imessaging_rec);
184 if (rec == NULL) {
185 smb_panic("Unable to allocate imessaging_rec");
188 rec->msg = msg;
189 rec->path = msg->path;
190 rec->header = (struct imessaging_header *)packet.data;
191 rec->packet = packet;
192 rec->retries = 0;
194 if (packet.length != sizeof(*rec->header) + rec->header->length) {
195 DEBUG(0,("messaging: bad message header size %d should be %d\n",
196 rec->header->length, (int)(packet.length - sizeof(*rec->header))));
197 talloc_free(rec);
198 return;
201 imessaging_dispatch(msg, rec);
202 talloc_free(rec);
208 try to send the message
210 static NTSTATUS try_send(struct imessaging_rec *rec)
212 struct imessaging_context *msg = rec->msg;
213 size_t nsent;
214 void *priv;
215 NTSTATUS status;
216 struct socket_address *path;
218 /* rec->path is the path of the *other* socket, where we want
219 * this to end up */
220 path = socket_address_from_strings(msg, msg->sock->backend_name,
221 rec->path, 0);
222 if (!path) {
223 return NT_STATUS_NO_MEMORY;
226 /* we send with privileges so messages work from any context */
227 priv = root_privileges();
228 status = socket_sendto(msg->sock, &rec->packet, &nsent, path);
229 talloc_free(path);
230 talloc_free(priv);
232 return status;
236 retry backed off messages
238 static void msg_retry_timer(struct tevent_context *ev, struct tevent_timer *te,
239 struct timeval t, void *private_data)
241 struct imessaging_context *msg = talloc_get_type(private_data,
242 struct imessaging_context);
243 msg->retry_te = NULL;
245 /* put the messages back on the main queue */
246 while (msg->retry_queue) {
247 struct imessaging_rec *rec = msg->retry_queue;
248 DLIST_REMOVE(msg->retry_queue, rec);
249 DLIST_ADD_END(msg->pending, rec, struct imessaging_rec *);
252 TEVENT_FD_WRITEABLE(msg->event.fde);
256 handle a socket write event
258 static void imessaging_send_handler(struct imessaging_context *msg, struct tevent_context *ev)
260 while (msg->pending) {
261 struct imessaging_rec *rec = msg->pending;
262 NTSTATUS status;
263 status = try_send(rec);
264 if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
265 rec->retries++;
266 if (rec->retries > 3) {
267 /* we're getting continuous write errors -
268 backoff this record */
269 DLIST_REMOVE(msg->pending, rec);
270 DLIST_ADD_END(msg->retry_queue, rec,
271 struct imessaging_rec *);
272 if (msg->retry_te == NULL) {
273 msg->retry_te =
274 tevent_add_timer(ev, msg,
275 timeval_current_ofs(1, 0),
276 msg_retry_timer, msg);
279 break;
281 rec->retries = 0;
282 if (!NT_STATUS_IS_OK(status)) {
283 TALLOC_CTX *tmp_ctx = talloc_new(msg);
284 DEBUG(1,("messaging: Lost message from %s to %s of type %u - %s\n",
285 server_id_str(tmp_ctx, &rec->header->from),
286 server_id_str(tmp_ctx, &rec->header->to),
287 rec->header->msg_type,
288 nt_errstr(status)));
289 talloc_free(tmp_ctx);
291 DLIST_REMOVE(msg->pending, rec);
292 talloc_free(rec);
294 if (msg->pending == NULL) {
295 TEVENT_FD_NOT_WRITEABLE(msg->event.fde);
300 handle a new incoming packet
302 static void imessaging_recv_handler(struct imessaging_context *msg, struct tevent_context *ev)
304 struct imessaging_rec *rec;
305 NTSTATUS status;
306 DATA_BLOB packet;
307 size_t msize;
309 /* see how many bytes are in the next packet */
310 status = socket_pending(msg->sock, &msize);
311 if (!NT_STATUS_IS_OK(status)) {
312 DEBUG(0,("socket_pending failed in messaging - %s\n",
313 nt_errstr(status)));
314 return;
317 packet = data_blob_talloc(msg, NULL, msize);
318 if (packet.data == NULL) {
319 /* assume this is temporary and retry */
320 return;
323 status = socket_recv(msg->sock, packet.data, msize, &msize);
324 if (!NT_STATUS_IS_OK(status)) {
325 data_blob_free(&packet);
326 return;
329 if (msize < sizeof(*rec->header)) {
330 DEBUG(0,("messaging: bad message of size %d\n", (int)msize));
331 data_blob_free(&packet);
332 return;
335 rec = talloc(msg, struct imessaging_rec);
336 if (rec == NULL) {
337 smb_panic("Unable to allocate imessaging_rec");
340 talloc_steal(rec, packet.data);
341 rec->msg = msg;
342 rec->path = msg->path;
343 rec->header = (struct imessaging_header *)packet.data;
344 rec->packet = packet;
345 rec->retries = 0;
347 if (msize != sizeof(*rec->header) + rec->header->length) {
348 DEBUG(0,("messaging: bad message header size %d should be %d\n",
349 rec->header->length, (int)(msize - sizeof(*rec->header))));
350 talloc_free(rec);
351 return;
354 imessaging_dispatch(msg, rec);
355 talloc_free(rec);
360 handle a socket event
362 static void imessaging_handler(struct tevent_context *ev, struct tevent_fd *fde,
363 uint16_t flags, void *private_data)
365 struct imessaging_context *msg = talloc_get_type(private_data,
366 struct imessaging_context);
367 if (flags & TEVENT_FD_WRITE) {
368 imessaging_send_handler(msg, ev);
370 if (flags & TEVENT_FD_READ) {
371 imessaging_recv_handler(msg, ev);
377 Register a dispatch function for a particular message type.
379 NTSTATUS imessaging_register(struct imessaging_context *msg, void *private_data,
380 uint32_t msg_type, msg_callback_t fn)
382 struct dispatch_fn *d;
384 /* possibly expand dispatch array */
385 if (msg_type >= msg->num_types) {
386 struct dispatch_fn **dp;
387 int i;
388 dp = talloc_realloc(msg, msg->dispatch, struct dispatch_fn *, msg_type+1);
389 NT_STATUS_HAVE_NO_MEMORY(dp);
390 msg->dispatch = dp;
391 for (i=msg->num_types;i<=msg_type;i++) {
392 msg->dispatch[i] = NULL;
394 msg->num_types = msg_type+1;
397 d = talloc_zero(msg->dispatch, struct dispatch_fn);
398 NT_STATUS_HAVE_NO_MEMORY(d);
399 d->msg_type = msg_type;
400 d->private_data = private_data;
401 d->fn = fn;
403 DLIST_ADD(msg->dispatch[msg_type], d);
405 return NT_STATUS_OK;
409 register a temporary message handler. The msg_type is allocated
410 above MSG_TMP_BASE
412 NTSTATUS imessaging_register_tmp(struct imessaging_context *msg, void *private_data,
413 msg_callback_t fn, uint32_t *msg_type)
415 struct dispatch_fn *d;
416 int id;
418 d = talloc_zero(msg->dispatch, struct dispatch_fn);
419 NT_STATUS_HAVE_NO_MEMORY(d);
420 d->private_data = private_data;
421 d->fn = fn;
423 id = idr_get_new_above(msg->dispatch_tree, d, MSG_TMP_BASE, UINT16_MAX);
424 if (id == -1) {
425 talloc_free(d);
426 return NT_STATUS_TOO_MANY_CONTEXT_IDS;
429 d->msg_type = (uint32_t)id;
430 (*msg_type) = d->msg_type;
432 return NT_STATUS_OK;
436 De-register the function for a particular message type.
438 void imessaging_deregister(struct imessaging_context *msg, uint32_t msg_type, void *private_data)
440 struct dispatch_fn *d, *next;
442 if (msg_type >= msg->num_types) {
443 d = (struct dispatch_fn *)idr_find(msg->dispatch_tree,
444 msg_type);
445 if (!d) return;
446 idr_remove(msg->dispatch_tree, msg_type);
447 talloc_free(d);
448 return;
451 for (d = msg->dispatch[msg_type]; d; d = next) {
452 next = d->next;
453 if (d->private_data == private_data) {
454 DLIST_REMOVE(msg->dispatch[msg_type], d);
455 talloc_free(d);
461 Send a message to a particular server
463 NTSTATUS imessaging_send(struct imessaging_context *msg, struct server_id server,
464 uint32_t msg_type, const DATA_BLOB *data)
466 struct imessaging_rec *rec;
467 NTSTATUS status;
468 size_t dlength = data?data->length:0;
470 rec = talloc(msg, struct imessaging_rec);
471 if (rec == NULL) {
472 return NT_STATUS_NO_MEMORY;
475 rec->packet = data_blob_talloc(rec, NULL, sizeof(*rec->header) + dlength);
476 if (rec->packet.data == NULL) {
477 talloc_free(rec);
478 return NT_STATUS_NO_MEMORY;
481 rec->retries = 0;
482 rec->msg = msg;
483 rec->header = (struct imessaging_header *)rec->packet.data;
484 /* zero padding */
485 ZERO_STRUCTP(rec->header);
486 rec->header->version = IMESSAGING_VERSION;
487 rec->header->msg_type = msg_type;
488 rec->header->from = msg->server_id;
489 rec->header->to = server;
490 rec->header->length = dlength;
491 if (dlength != 0) {
492 memcpy(rec->packet.data + sizeof(*rec->header),
493 data->data, dlength);
496 if (!cluster_node_equal(&msg->server_id, &server)) {
497 /* the destination is on another node - dispatch via
498 the cluster layer */
499 status = cluster_message_send(server, &rec->packet);
500 talloc_free(rec);
501 return status;
504 rec->path = imessaging_path(msg, server);
505 talloc_steal(rec, rec->path);
507 if (msg->pending != NULL) {
508 status = STATUS_MORE_ENTRIES;
509 } else {
510 status = try_send(rec);
513 if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
514 if (msg->pending == NULL) {
515 TEVENT_FD_WRITEABLE(msg->event.fde);
517 DLIST_ADD_END(msg->pending, rec, struct imessaging_rec *);
518 return NT_STATUS_OK;
521 talloc_free(rec);
523 return status;
527 Send a message to a particular server, with the message containing a single pointer
529 NTSTATUS imessaging_send_ptr(struct imessaging_context *msg, struct server_id server,
530 uint32_t msg_type, void *ptr)
532 DATA_BLOB blob;
534 blob.data = (uint8_t *)&ptr;
535 blob.length = sizeof(void *);
537 return imessaging_send(msg, server, msg_type, &blob);
542 remove our messaging socket and database entry
544 int imessaging_cleanup(struct imessaging_context *msg)
546 if (!msg) {
547 return 0;
550 DEBUG(5,("imessaging: cleaning up %s\n", msg->path));
551 unlink(msg->path);
552 while (msg->names && msg->names[0]) {
553 irpc_remove_name(msg, msg->names[0]);
555 return 0;
559 create the listening socket and setup the dispatcher
561 use auto_remove=true when you want a destructor to remove the
562 associated messaging socket and database entry on talloc free. Don't
563 use this in processes that may fork and a child may talloc free this
564 memory
566 struct imessaging_context *imessaging_init(TALLOC_CTX *mem_ctx,
567 struct loadparm_context *lp_ctx,
568 struct server_id server_id,
569 struct tevent_context *ev,
570 bool auto_remove)
572 struct imessaging_context *msg;
573 NTSTATUS status;
574 struct socket_address *path;
575 bool ok;
577 if (ev == NULL) {
578 return NULL;
581 msg = talloc_zero(mem_ctx, struct imessaging_context);
582 if (msg == NULL) {
583 return NULL;
586 /* setup a handler for messages from other cluster nodes, if appropriate */
587 status = cluster_message_init(msg, server_id, cluster_message_handler);
588 if (!NT_STATUS_IS_OK(status)) {
589 goto fail;
592 /* create the messaging directory if needed */
594 msg->base_path = lpcfg_imessaging_path(msg, lp_ctx);
595 if (msg->base_path == NULL) {
596 goto fail;
599 ok = directory_create_or_exist_strict(msg->base_path, geteuid(), 0700);
600 if (!ok) {
601 goto fail;
604 msg->path = imessaging_path(msg, server_id);
605 if (msg->path == NULL) {
606 goto fail;
609 msg->server_id = server_id;
610 msg->idr = idr_init(msg);
611 if (msg->idr == NULL) {
612 goto fail;
615 msg->dispatch_tree = idr_init(msg);
616 if (msg->dispatch_tree == NULL) {
617 goto fail;
620 msg->start_time = timeval_current();
622 msg->names_db = irpc_namedb_open(msg, msg->base_path, lp_ctx);
623 if (msg->names_db == NULL) {
624 goto fail;
627 status = socket_create("unix", SOCKET_TYPE_DGRAM, &msg->sock, 0);
628 if (!NT_STATUS_IS_OK(status)) {
629 goto fail;
632 /* by stealing here we ensure that the socket is cleaned up (and even
633 deleted) on exit */
634 talloc_steal(msg, msg->sock);
636 path = socket_address_from_strings(msg, msg->sock->backend_name,
637 msg->path, 0);
638 if (!path) {
639 goto fail;
642 status = socket_listen(msg->sock, path, 50, 0);
643 if (!NT_STATUS_IS_OK(status)) {
644 DEBUG(0,("Unable to setup messaging listener for '%s':%s\n", msg->path, nt_errstr(status)));
645 goto fail;
648 /* it needs to be non blocking for sends */
649 set_blocking(socket_get_fd(msg->sock), false);
651 msg->event.fde = tevent_add_fd(ev, msg, socket_get_fd(msg->sock),
652 TEVENT_FD_READ, imessaging_handler, msg);
653 tevent_fd_set_auto_close(msg->event.fde);
655 if (auto_remove) {
656 talloc_set_destructor(msg, imessaging_cleanup);
659 imessaging_register(msg, NULL, MSG_PING, ping_message);
660 imessaging_register(msg, NULL, MSG_IRPC, irpc_handler);
661 IRPC_REGISTER(msg, irpc, IRPC_UPTIME, irpc_uptime, msg);
663 return msg;
664 fail:
665 talloc_free(msg);
666 return NULL;
670 A hack, for the short term until we get 'client only' messaging in place
672 struct imessaging_context *imessaging_client_init(TALLOC_CTX *mem_ctx,
673 struct loadparm_context *lp_ctx,
674 struct tevent_context *ev)
676 struct server_id id;
677 ZERO_STRUCT(id);
678 id.pid = getpid();
679 id.task_id = generate_random();
680 id.vnn = NONCLUSTER_VNN;
682 /* This is because we are not in the s3 serverid database */
683 id.unique_id = SERVERID_UNIQUE_ID_NOT_TO_VERIFY;
685 return imessaging_init(mem_ctx, lp_ctx, id, ev, true);
688 a list of registered irpc server functions
690 struct irpc_list {
691 struct irpc_list *next, *prev;
692 struct GUID uuid;
693 const struct ndr_interface_table *table;
694 int callnum;
695 irpc_function_t fn;
696 void *private_data;
701 register a irpc server function
703 NTSTATUS irpc_register(struct imessaging_context *msg_ctx,
704 const struct ndr_interface_table *table,
705 int callnum, irpc_function_t fn, void *private_data)
707 struct irpc_list *irpc;
709 /* override an existing handler, if any */
710 for (irpc=msg_ctx->irpc; irpc; irpc=irpc->next) {
711 if (irpc->table == table && irpc->callnum == callnum) {
712 break;
715 if (irpc == NULL) {
716 irpc = talloc(msg_ctx, struct irpc_list);
717 NT_STATUS_HAVE_NO_MEMORY(irpc);
718 DLIST_ADD(msg_ctx->irpc, irpc);
721 irpc->table = table;
722 irpc->callnum = callnum;
723 irpc->fn = fn;
724 irpc->private_data = private_data;
725 irpc->uuid = irpc->table->syntax_id.uuid;
727 return NT_STATUS_OK;
732 handle an incoming irpc reply message
734 static void irpc_handler_reply(struct imessaging_context *msg_ctx, struct irpc_message *m)
736 struct irpc_request *irpc;
738 irpc = (struct irpc_request *)idr_find(msg_ctx->idr, m->header.callid);
739 if (irpc == NULL) return;
741 irpc->incoming.handler(irpc, m);
745 send a irpc reply
747 NTSTATUS irpc_send_reply(struct irpc_message *m, NTSTATUS status)
749 struct ndr_push *push;
750 DATA_BLOB packet;
751 enum ndr_err_code ndr_err;
753 m->header.status = status;
755 /* setup the reply */
756 push = ndr_push_init_ctx(m->ndr);
757 if (push == NULL) {
758 status = NT_STATUS_NO_MEMORY;
759 goto failed;
762 m->header.flags |= IRPC_FLAG_REPLY;
763 m->header.creds.token= NULL;
765 /* construct the packet */
766 ndr_err = ndr_push_irpc_header(push, NDR_SCALARS|NDR_BUFFERS, &m->header);
767 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
768 status = ndr_map_error2ntstatus(ndr_err);
769 goto failed;
772 ndr_err = m->irpc->table->calls[m->irpc->callnum].ndr_push(push, NDR_OUT, m->data);
773 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
774 status = ndr_map_error2ntstatus(ndr_err);
775 goto failed;
778 /* send the reply message */
779 packet = ndr_push_blob(push);
780 status = imessaging_send(m->msg_ctx, m->from, MSG_IRPC, &packet);
781 if (!NT_STATUS_IS_OK(status)) goto failed;
783 failed:
784 talloc_free(m);
785 return status;
789 handle an incoming irpc request message
791 static void irpc_handler_request(struct imessaging_context *msg_ctx,
792 struct irpc_message *m)
794 struct irpc_list *i;
795 void *r;
796 enum ndr_err_code ndr_err;
798 for (i=msg_ctx->irpc; i; i=i->next) {
799 if (GUID_equal(&i->uuid, &m->header.uuid) &&
800 i->table->syntax_id.if_version == m->header.if_version &&
801 i->callnum == m->header.callnum) {
802 break;
806 if (i == NULL) {
807 /* no registered handler for this message */
808 talloc_free(m);
809 return;
812 /* allocate space for the structure */
813 r = talloc_zero_size(m->ndr, i->table->calls[m->header.callnum].struct_size);
814 if (r == NULL) goto failed;
816 m->ndr->flags |= LIBNDR_FLAG_REF_ALLOC;
818 /* parse the request data */
819 ndr_err = i->table->calls[i->callnum].ndr_pull(m->ndr, NDR_IN, r);
820 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) goto failed;
822 /* make the call */
823 m->private_data= i->private_data;
824 m->defer_reply = false;
825 m->no_reply = false;
826 m->msg_ctx = msg_ctx;
827 m->irpc = i;
828 m->data = r;
830 m->header.status = i->fn(m, r);
832 if (m->no_reply) {
833 /* the server function won't ever be replying to this request */
834 talloc_free(m);
835 return;
838 if (m->defer_reply) {
839 /* the server function has asked to defer the reply to later */
840 talloc_steal(msg_ctx, m);
841 return;
844 irpc_send_reply(m, m->header.status);
845 return;
847 failed:
848 talloc_free(m);
852 handle an incoming irpc message
854 static void irpc_handler(struct imessaging_context *msg_ctx, void *private_data,
855 uint32_t msg_type, struct server_id src, DATA_BLOB *packet)
857 struct irpc_message *m;
858 enum ndr_err_code ndr_err;
860 m = talloc(msg_ctx, struct irpc_message);
861 if (m == NULL) goto failed;
863 m->from = src;
865 m->ndr = ndr_pull_init_blob(packet, m);
866 if (m->ndr == NULL) goto failed;
868 m->ndr->flags |= LIBNDR_FLAG_REF_ALLOC;
870 ndr_err = ndr_pull_irpc_header(m->ndr, NDR_BUFFERS|NDR_SCALARS, &m->header);
871 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) goto failed;
873 if (m->header.flags & IRPC_FLAG_REPLY) {
874 irpc_handler_reply(msg_ctx, m);
875 } else {
876 irpc_handler_request(msg_ctx, m);
878 return;
880 failed:
881 talloc_free(m);
886 destroy a irpc request
888 static int irpc_destructor(struct irpc_request *irpc)
890 if (irpc->callid != -1) {
891 idr_remove(irpc->msg_ctx->idr, irpc->callid);
892 irpc->callid = -1;
895 return 0;
899 open the naming database
901 static struct tdb_wrap *irpc_namedb_open(TALLOC_CTX *mem_ctx, const char *base_path,
902 struct loadparm_context *lp_ctx)
904 struct tdb_wrap *t;
905 char *path = talloc_asprintf(mem_ctx, "%s/names.tdb", base_path);
906 if (path == NULL) {
907 return NULL;
909 t = tdb_wrap_open(mem_ctx, path, lpcfg_tdb_hash_size(lp_ctx, path),
910 lpcfg_tdb_flags(lp_ctx, 0), O_RDWR|O_CREAT, 0660);
911 talloc_free(path);
912 return t;
917 add a string name that this irpc server can be called on
919 NTSTATUS irpc_add_name(struct imessaging_context *msg_ctx, const char *name)
921 struct tdb_context *t = msg_ctx->names_db->tdb;
922 struct server_id pid = msg_ctx->server_id;
923 TDB_DATA key, data;
924 int ret;
926 msg_ctx->names = str_list_add(msg_ctx->names, name);
927 if (msg_ctx->names == NULL) {
928 return NT_STATUS_NO_MEMORY;
930 talloc_steal(msg_ctx, msg_ctx->names);
932 key = string_term_tdb_data(name);
933 data = (TDB_DATA) { .dptr = (uint8_t *)&pid, .dsize = sizeof(pid) };
935 ret = tdb_append(t, key, data);
936 if (ret != 0) {
937 enum TDB_ERROR err = tdb_error(t);
938 str_list_remove(msg_ctx->names, name);
939 return map_nt_error_from_tdb(err);
942 return NT_STATUS_OK;
946 return a list of server ids for a server name
948 NTSTATUS irpc_servers_byname(struct imessaging_context *msg_ctx,
949 TALLOC_CTX *mem_ctx, const char *name,
950 unsigned *num_servers,
951 struct server_id **servers)
953 struct tdb_wrap *t = msg_ctx->names_db;
954 TDB_DATA rec;
955 unsigned count;
956 struct server_id *ret;
958 rec = tdb_fetch_bystring(t->tdb, name);
959 if (rec.dptr == NULL) {
960 enum TDB_ERROR err = tdb_error(t->tdb);
961 return map_nt_error_from_tdb(err);
964 count = rec.dsize / sizeof(struct server_id);
965 if (count == 0) {
967 * In a corrupted db we could end up with a record of size
968 * less than a struct server_id. Don't leak in this case.
970 free(rec.dptr);
971 return NT_STATUS_NOT_FOUND;
974 ret = talloc_array(mem_ctx, struct server_id, count);
975 if (ret == NULL) {
976 free(rec.dptr);
977 return NT_STATUS_NO_MEMORY;
979 memcpy(ret, rec.dptr, count * sizeof(struct server_id));
980 free(rec.dptr);
982 *num_servers = count;
983 *servers = ret;
984 return NT_STATUS_OK;
987 static int all_servers_func(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *state)
989 struct irpc_name_records *name_records = talloc_get_type(state, struct irpc_name_records);
990 struct irpc_name_record *name_record;
991 int i;
993 name_records->names
994 = talloc_realloc(name_records, name_records->names,
995 struct irpc_name_record *, name_records->num_records+1);
996 if (!name_records->names) {
997 return -1;
1000 name_records->names[name_records->num_records] = name_record
1001 = talloc(name_records->names,
1002 struct irpc_name_record);
1003 if (!name_record) {
1004 return -1;
1007 name_records->num_records++;
1009 name_record->name
1010 = talloc_strndup(name_record,
1011 (const char *)key.dptr, key.dsize);
1012 if (!name_record->name) {
1013 return -1;
1016 name_record->count = data.dsize / sizeof(struct server_id);
1017 name_record->ids = talloc_array(name_record,
1018 struct server_id,
1019 name_record->count);
1020 if (name_record->ids == NULL) {
1021 return -1;
1023 for (i=0;i<name_record->count;i++) {
1024 name_record->ids[i] = ((struct server_id *)data.dptr)[i];
1026 return 0;
1030 return a list of server ids for a server name
1032 struct irpc_name_records *irpc_all_servers(struct imessaging_context *msg_ctx,
1033 TALLOC_CTX *mem_ctx)
1035 struct tdb_wrap *t = msg_ctx->names_db;
1036 int ret;
1037 struct irpc_name_records *name_records = talloc_zero(mem_ctx, struct irpc_name_records);
1038 if (name_records == NULL) {
1039 return NULL;
1042 ret = tdb_traverse_read(t->tdb, all_servers_func, name_records);
1043 if (ret == -1) {
1044 return NULL;
1047 return name_records;
1051 remove a name from a messaging context
1053 void irpc_remove_name(struct imessaging_context *msg_ctx, const char *name)
1055 struct tdb_wrap *t = msg_ctx->names_db;
1056 TDB_DATA rec;
1057 int count, i;
1058 struct server_id *ids;
1060 str_list_remove(msg_ctx->names, name);
1062 if (tdb_lock_bystring(t->tdb, name) != 0) {
1063 return;
1065 rec = tdb_fetch_bystring(t->tdb, name);
1066 if (rec.dptr == NULL) {
1067 tdb_unlock_bystring(t->tdb, name);
1068 return;
1070 count = rec.dsize / sizeof(struct server_id);
1071 if (count == 0) {
1072 free(rec.dptr);
1073 tdb_unlock_bystring(t->tdb, name);
1074 return;
1076 ids = (struct server_id *)rec.dptr;
1077 for (i=0;i<count;i++) {
1078 if (cluster_id_equal(&ids[i], &msg_ctx->server_id)) {
1079 if (i < count-1) {
1080 memmove(ids+i, ids+i+1,
1081 sizeof(struct server_id) * (count-(i+1)));
1083 rec.dsize -= sizeof(struct server_id);
1084 break;
1087 tdb_store_bystring(t->tdb, name, rec, 0);
1088 free(rec.dptr);
1089 tdb_unlock_bystring(t->tdb, name);
1092 struct server_id imessaging_get_server_id(struct imessaging_context *msg_ctx)
1094 return msg_ctx->server_id;
1097 struct irpc_bh_state {
1098 struct imessaging_context *msg_ctx;
1099 struct server_id server_id;
1100 const struct ndr_interface_table *table;
1101 uint32_t timeout;
1102 struct security_token *token;
1105 static bool irpc_bh_is_connected(struct dcerpc_binding_handle *h)
1107 struct irpc_bh_state *hs = dcerpc_binding_handle_data(h,
1108 struct irpc_bh_state);
1110 if (!hs->msg_ctx) {
1111 return false;
1114 return true;
1117 static uint32_t irpc_bh_set_timeout(struct dcerpc_binding_handle *h,
1118 uint32_t timeout)
1120 struct irpc_bh_state *hs = dcerpc_binding_handle_data(h,
1121 struct irpc_bh_state);
1122 uint32_t old = hs->timeout;
1124 hs->timeout = timeout;
1126 return old;
1129 struct irpc_bh_raw_call_state {
1130 struct irpc_request *irpc;
1131 uint32_t opnum;
1132 DATA_BLOB in_data;
1133 DATA_BLOB in_packet;
1134 DATA_BLOB out_data;
1137 static void irpc_bh_raw_call_incoming_handler(struct irpc_request *irpc,
1138 struct irpc_message *m);
1140 static struct tevent_req *irpc_bh_raw_call_send(TALLOC_CTX *mem_ctx,
1141 struct tevent_context *ev,
1142 struct dcerpc_binding_handle *h,
1143 const struct GUID *object,
1144 uint32_t opnum,
1145 uint32_t in_flags,
1146 const uint8_t *in_data,
1147 size_t in_length)
1149 struct irpc_bh_state *hs =
1150 dcerpc_binding_handle_data(h,
1151 struct irpc_bh_state);
1152 struct tevent_req *req;
1153 struct irpc_bh_raw_call_state *state;
1154 bool ok;
1155 struct irpc_header header;
1156 struct ndr_push *ndr;
1157 NTSTATUS status;
1158 enum ndr_err_code ndr_err;
1160 req = tevent_req_create(mem_ctx, &state,
1161 struct irpc_bh_raw_call_state);
1162 if (req == NULL) {
1163 return NULL;
1165 state->opnum = opnum;
1166 state->in_data.data = discard_const_p(uint8_t, in_data);
1167 state->in_data.length = in_length;
1169 ok = irpc_bh_is_connected(h);
1170 if (!ok) {
1171 tevent_req_nterror(req, NT_STATUS_CONNECTION_DISCONNECTED);
1172 return tevent_req_post(req, ev);
1175 state->irpc = talloc_zero(state, struct irpc_request);
1176 if (tevent_req_nomem(state->irpc, req)) {
1177 return tevent_req_post(req, ev);
1180 state->irpc->msg_ctx = hs->msg_ctx;
1181 state->irpc->callid = idr_get_new(hs->msg_ctx->idr,
1182 state->irpc, UINT16_MAX);
1183 if (state->irpc->callid == -1) {
1184 tevent_req_nterror(req, NT_STATUS_INSUFFICIENT_RESOURCES);
1185 return tevent_req_post(req, ev);
1187 state->irpc->incoming.handler = irpc_bh_raw_call_incoming_handler;
1188 state->irpc->incoming.private_data = req;
1190 talloc_set_destructor(state->irpc, irpc_destructor);
1192 /* setup the header */
1193 header.uuid = hs->table->syntax_id.uuid;
1195 header.if_version = hs->table->syntax_id.if_version;
1196 header.callid = state->irpc->callid;
1197 header.callnum = state->opnum;
1198 header.flags = 0;
1199 header.status = NT_STATUS_OK;
1200 header.creds.token= hs->token;
1202 /* construct the irpc packet */
1203 ndr = ndr_push_init_ctx(state->irpc);
1204 if (tevent_req_nomem(ndr, req)) {
1205 return tevent_req_post(req, ev);
1208 ndr_err = ndr_push_irpc_header(ndr, NDR_SCALARS|NDR_BUFFERS, &header);
1209 status = ndr_map_error2ntstatus(ndr_err);
1210 if (!NT_STATUS_IS_OK(status)) {
1211 tevent_req_nterror(req, status);
1212 return tevent_req_post(req, ev);
1215 ndr_err = ndr_push_bytes(ndr, in_data, in_length);
1216 status = ndr_map_error2ntstatus(ndr_err);
1217 if (!NT_STATUS_IS_OK(status)) {
1218 tevent_req_nterror(req, status);
1219 return tevent_req_post(req, ev);
1222 /* and send it */
1223 state->in_packet = ndr_push_blob(ndr);
1224 status = imessaging_send(hs->msg_ctx, hs->server_id,
1225 MSG_IRPC, &state->in_packet);
1226 if (!NT_STATUS_IS_OK(status)) {
1227 tevent_req_nterror(req, status);
1228 return tevent_req_post(req, ev);
1231 if (hs->timeout != IRPC_CALL_TIMEOUT_INF) {
1232 /* set timeout-callback in case caller wants that */
1233 ok = tevent_req_set_endtime(req, ev, timeval_current_ofs(hs->timeout, 0));
1234 if (!ok) {
1235 return tevent_req_post(req, ev);
1239 return req;
1242 static void irpc_bh_raw_call_incoming_handler(struct irpc_request *irpc,
1243 struct irpc_message *m)
1245 struct tevent_req *req =
1246 talloc_get_type_abort(irpc->incoming.private_data,
1247 struct tevent_req);
1248 struct irpc_bh_raw_call_state *state =
1249 tevent_req_data(req,
1250 struct irpc_bh_raw_call_state);
1252 talloc_steal(state, m);
1254 if (!NT_STATUS_IS_OK(m->header.status)) {
1255 tevent_req_nterror(req, m->header.status);
1256 return;
1259 state->out_data = data_blob_talloc(state,
1260 m->ndr->data + m->ndr->offset,
1261 m->ndr->data_size - m->ndr->offset);
1262 if ((m->ndr->data_size - m->ndr->offset) > 0 && !state->out_data.data) {
1263 tevent_req_oom(req);
1264 return;
1267 tevent_req_done(req);
1270 static NTSTATUS irpc_bh_raw_call_recv(struct tevent_req *req,
1271 TALLOC_CTX *mem_ctx,
1272 uint8_t **out_data,
1273 size_t *out_length,
1274 uint32_t *out_flags)
1276 struct irpc_bh_raw_call_state *state =
1277 tevent_req_data(req,
1278 struct irpc_bh_raw_call_state);
1279 NTSTATUS status;
1281 if (tevent_req_is_nterror(req, &status)) {
1282 tevent_req_received(req);
1283 return status;
1286 *out_data = talloc_move(mem_ctx, &state->out_data.data);
1287 *out_length = state->out_data.length;
1288 *out_flags = 0;
1289 tevent_req_received(req);
1290 return NT_STATUS_OK;
1293 struct irpc_bh_disconnect_state {
1294 uint8_t _dummy;
1297 static struct tevent_req *irpc_bh_disconnect_send(TALLOC_CTX *mem_ctx,
1298 struct tevent_context *ev,
1299 struct dcerpc_binding_handle *h)
1301 struct irpc_bh_state *hs = dcerpc_binding_handle_data(h,
1302 struct irpc_bh_state);
1303 struct tevent_req *req;
1304 struct irpc_bh_disconnect_state *state;
1305 bool ok;
1307 req = tevent_req_create(mem_ctx, &state,
1308 struct irpc_bh_disconnect_state);
1309 if (req == NULL) {
1310 return NULL;
1313 ok = irpc_bh_is_connected(h);
1314 if (!ok) {
1315 tevent_req_nterror(req, NT_STATUS_CONNECTION_DISCONNECTED);
1316 return tevent_req_post(req, ev);
1319 hs->msg_ctx = NULL;
1321 tevent_req_done(req);
1322 return tevent_req_post(req, ev);
1325 static NTSTATUS irpc_bh_disconnect_recv(struct tevent_req *req)
1327 NTSTATUS status;
1329 if (tevent_req_is_nterror(req, &status)) {
1330 tevent_req_received(req);
1331 return status;
1334 tevent_req_received(req);
1335 return NT_STATUS_OK;
1338 static bool irpc_bh_ref_alloc(struct dcerpc_binding_handle *h)
1340 return true;
1343 static const struct dcerpc_binding_handle_ops irpc_bh_ops = {
1344 .name = "wbint",
1345 .is_connected = irpc_bh_is_connected,
1346 .set_timeout = irpc_bh_set_timeout,
1347 .raw_call_send = irpc_bh_raw_call_send,
1348 .raw_call_recv = irpc_bh_raw_call_recv,
1349 .disconnect_send = irpc_bh_disconnect_send,
1350 .disconnect_recv = irpc_bh_disconnect_recv,
1352 .ref_alloc = irpc_bh_ref_alloc,
1355 /* initialise a irpc binding handle */
1356 struct dcerpc_binding_handle *irpc_binding_handle(TALLOC_CTX *mem_ctx,
1357 struct imessaging_context *msg_ctx,
1358 struct server_id server_id,
1359 const struct ndr_interface_table *table)
1361 struct dcerpc_binding_handle *h;
1362 struct irpc_bh_state *hs;
1364 h = dcerpc_binding_handle_create(mem_ctx,
1365 &irpc_bh_ops,
1366 NULL,
1367 table,
1368 &hs,
1369 struct irpc_bh_state,
1370 __location__);
1371 if (h == NULL) {
1372 return NULL;
1374 hs->msg_ctx = msg_ctx;
1375 hs->server_id = server_id;
1376 hs->table = table;
1377 hs->timeout = IRPC_CALL_TIMEOUT;
1379 return h;
1382 struct dcerpc_binding_handle *irpc_binding_handle_by_name(TALLOC_CTX *mem_ctx,
1383 struct imessaging_context *msg_ctx,
1384 const char *dest_task,
1385 const struct ndr_interface_table *table)
1387 struct dcerpc_binding_handle *h;
1388 unsigned num_sids;
1389 struct server_id *sids;
1390 struct server_id sid;
1391 NTSTATUS status;
1393 /* find the server task */
1395 status = irpc_servers_byname(msg_ctx, mem_ctx, dest_task,
1396 &num_sids, &sids);
1397 if (!NT_STATUS_IS_OK(status)) {
1398 errno = EADDRNOTAVAIL;
1399 return NULL;
1401 sid = sids[0];
1402 talloc_free(sids);
1404 h = irpc_binding_handle(mem_ctx, msg_ctx,
1405 sid, table);
1406 if (h == NULL) {
1407 return NULL;
1410 return h;
1413 void irpc_binding_handle_add_security_token(struct dcerpc_binding_handle *h,
1414 struct security_token *token)
1416 struct irpc_bh_state *hs =
1417 dcerpc_binding_handle_data(h,
1418 struct irpc_bh_state);
1420 hs->token = token;