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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "lib/events/events.h"
25 #include "system/dir.h"
26 #include "system/filesys.h"
27 #include "system/time.h"
29 #include "dlinklist.h"
30 #include "lib/socket/socket.h"
31 #include "librpc/gen_ndr/ndr_irpc.h"
32 #include "lib/messaging/irpc.h"
34 #include "lib/tdb/include/tdb.h"
35 #include "lib/tdb/include/tdbutil.h"
37 /* change the message version with any incompatible changes in the protocol */
38 #define MESSAGING_VERSION 1
40 struct messaging_context {
42 struct socket_context *sock;
43 const char *base_path;
45 struct dispatch_fn *dispatch;
46 struct messaging_rec *pending;
47 struct irpc_list *irpc;
48 struct idr_context *idr;
52 struct event_context *ev;
57 /* we have a linked list of dispatch handlers that this messaging
58 server can deal with */
60 struct dispatch_fn *next, *prev;
63 void (*fn)(struct messaging_context *msg, void *private,
64 uint32_t msg_type, uint32_t server_id, DATA_BLOB *data);
67 /* an individual message */
68 struct messaging_rec {
69 struct messaging_rec *next, *prev;
70 struct messaging_context *msg;
73 struct messaging_header {
85 static void irpc_handler(struct messaging_context *, void *,
86 uint32_t, uint32_t, DATA_BLOB *);
90 A useful function for testing the message system.
92 static void ping_message(struct messaging_context *msg, void *private,
93 uint32_t msg_type, uint32_t src, DATA_BLOB *data)
95 DEBUG(1,("INFO: Received PING message from server %u [%.*s]\n",
96 (uint_t)src, data->length, data->data?(const char *)data->data:""));
97 messaging_send(msg, src, MSG_PONG, data);
101 return the path to a messaging socket
103 static char *messaging_path(struct messaging_context *msg, uint32_t server_id)
105 return talloc_asprintf(msg, "%s/msg.%u", msg->base_path, (unsigned)server_id);
109 dispatch a fully received message
111 static void messaging_dispatch(struct messaging_context *msg, struct messaging_rec *rec)
113 struct dispatch_fn *d, *next;
114 for (d=msg->dispatch;d;d=next) {
116 if (d->msg_type == rec->header->msg_type) {
118 data.data = rec->packet.data + sizeof(*rec->header);
119 data.length = rec->header->length;
120 d->fn(msg, d->private, d->msg_type, rec->header->from, &data);
123 rec->header->length = 0;
128 try to send the message
130 static NTSTATUS try_send(struct messaging_rec *rec)
132 struct messaging_context *msg = rec->msg;
137 /* we send with privileges so messages work from any context */
138 priv = root_privileges();
139 status = socket_sendto(msg->sock, &rec->packet, &nsent, 0, rec->path, 0);
146 handle a socket write event
148 static void messaging_send_handler(struct messaging_context *msg)
150 while (msg->pending) {
151 struct messaging_rec *rec = msg->pending;
153 status = try_send(rec);
154 if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
157 if (!NT_STATUS_IS_OK(status)) {
158 DEBUG(1,("messaging: Lost message from %u to %u of type %u - %s\n",
159 rec->header->from, rec->header->to, rec->header->msg_type,
162 DLIST_REMOVE(msg->pending, rec);
165 if (msg->pending == NULL) {
166 EVENT_FD_NOT_WRITEABLE(msg->event.fde);
171 handle a new incoming packet
173 static void messaging_recv_handler(struct messaging_context *msg)
175 struct messaging_rec *rec;
180 /* see how many bytes are in the next packet */
181 status = socket_pending(msg->sock, &msize);
182 if (!NT_STATUS_IS_OK(status)) {
183 DEBUG(0,("socket_pending failed in messaging - %s\n",
188 packet = data_blob_talloc(msg, NULL, msize);
189 if (packet.data == NULL) {
190 /* assume this is temporary and retry */
194 status = socket_recv(msg->sock, packet.data, msize, &msize, 0);
195 if (!NT_STATUS_IS_OK(status)) {
196 data_blob_free(&packet);
200 if (msize < sizeof(*rec->header)) {
201 DEBUG(0,("messaging: bad message of size %d\n", msize));
202 data_blob_free(&packet);
206 rec = talloc(msg, struct messaging_rec);
208 smb_panic("Unable to allocate messaging_rec");
211 talloc_steal(rec, packet.data);
213 rec->path = msg->path;
214 rec->header = (struct messaging_header *)packet.data;
215 rec->packet = packet;
217 if (msize != sizeof(*rec->header) + rec->header->length) {
218 DEBUG(0,("messaging: bad message header size %d should be %d\n",
219 rec->header->length, msize - sizeof(*rec->header)));
224 messaging_dispatch(msg, rec);
230 handle a socket event
232 static void messaging_handler(struct event_context *ev, struct fd_event *fde,
233 uint16_t flags, void *private)
235 struct messaging_context *msg = talloc_get_type(private,
236 struct messaging_context);
237 if (flags & EVENT_FD_WRITE) {
238 messaging_send_handler(msg);
240 if (flags & EVENT_FD_READ) {
241 messaging_recv_handler(msg);
247 Register a dispatch function for a particular message type.
249 void messaging_register(struct messaging_context *msg, void *private,
251 void (*fn)(struct messaging_context *, void *, uint32_t, uint32_t, DATA_BLOB *))
253 struct dispatch_fn *d;
255 d = talloc(msg, struct dispatch_fn);
256 d->msg_type = msg_type;
257 d->private = private;
259 DLIST_ADD(msg->dispatch, d);
263 De-register the function for a particular message type.
265 void messaging_deregister(struct messaging_context *msg, uint32_t msg_type, void *private)
267 struct dispatch_fn *d, *next;
269 for (d = msg->dispatch; d; d = next) {
271 if (d->msg_type == msg_type &&
272 d->private == private) {
273 DLIST_REMOVE(msg->dispatch, d);
281 Send a message to a particular server
283 NTSTATUS messaging_send(struct messaging_context *msg, uint32_t server,
284 uint32_t msg_type, DATA_BLOB *data)
286 struct messaging_rec *rec;
288 size_t dlength = data?data->length:0;
290 rec = talloc(msg, struct messaging_rec);
292 return NT_STATUS_NO_MEMORY;
295 rec->packet = data_blob_talloc(rec, NULL, sizeof(*rec->header) + dlength);
296 if (rec->packet.data == NULL) {
298 return NT_STATUS_NO_MEMORY;
302 rec->header = (struct messaging_header *)rec->packet.data;
303 rec->header->version = MESSAGING_VERSION;
304 rec->header->msg_type = msg_type;
305 rec->header->from = msg->server_id;
306 rec->header->to = server;
307 rec->header->length = dlength;
309 memcpy(rec->packet.data + sizeof(*rec->header),
310 data->data, dlength);
313 rec->path = messaging_path(msg, server);
314 talloc_steal(rec, rec->path);
316 if (msg->pending != NULL) {
317 status = STATUS_MORE_ENTRIES;
319 status = try_send(rec);
322 if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
323 if (msg->pending == NULL) {
324 EVENT_FD_WRITEABLE(msg->event.fde);
326 DLIST_ADD_END(msg->pending, rec, struct messaging_rec *);
336 Send a message to a particular server, with the message containing a single pointer
338 NTSTATUS messaging_send_ptr(struct messaging_context *msg, uint32_t server,
339 uint32_t msg_type, void *ptr)
343 blob.data = (void *)&ptr;
344 blob.length = sizeof(void *);
346 return messaging_send(msg, server, msg_type, &blob);
351 destroy the messaging context
353 static int messaging_destructor(void *ptr)
355 struct messaging_context *msg = ptr;
357 while (msg->names && msg->names[0]) {
358 irpc_remove_name(msg, msg->names[0]);
364 create the listening socket and setup the dispatcher
366 struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx, uint32_t server_id,
367 struct event_context *ev)
369 struct messaging_context *msg;
373 msg = talloc(mem_ctx, struct messaging_context);
378 /* create the messaging directory if needed */
379 path = smbd_tmp_path(msg, "messaging");
383 msg->base_path = smbd_tmp_path(msg, "messaging");
384 msg->path = messaging_path(msg, server_id);
385 msg->server_id = server_id;
386 msg->dispatch = NULL;
388 msg->idr = idr_init(msg);
392 status = socket_create("unix", SOCKET_TYPE_DGRAM, &msg->sock, 0);
393 if (!NT_STATUS_IS_OK(status)) {
398 /* by stealing here we ensure that the socket is cleaned up (and even
400 talloc_steal(msg, msg->sock);
402 status = socket_listen(msg->sock, msg->path, 0, 50, 0);
403 if (!NT_STATUS_IS_OK(status)) {
404 DEBUG(0,("Unable to setup messaging listener for '%s':%s\n", msg->path, nt_errstr(status)));
409 /* it needs to be non blocking for sends */
410 set_blocking(socket_get_fd(msg->sock), False);
412 msg->event.ev = talloc_reference(msg, ev);
413 msg->event.fde = event_add_fd(ev, msg, socket_get_fd(msg->sock),
414 EVENT_FD_READ, messaging_handler, msg);
416 talloc_set_destructor(msg, messaging_destructor);
418 messaging_register(msg, NULL, MSG_PING, ping_message);
419 messaging_register(msg, NULL, MSG_IRPC, irpc_handler);
426 a list of registered irpc server functions
429 struct irpc_list *next, *prev;
431 const struct dcerpc_interface_table *table;
439 register a irpc server function
441 NTSTATUS irpc_register(struct messaging_context *msg_ctx,
442 const struct dcerpc_interface_table *table,
443 int callnum, irpc_function_t fn, void *private)
445 struct irpc_list *irpc;
447 /* override an existing handler, if any */
448 for (irpc=msg_ctx->irpc; irpc; irpc=irpc->next) {
449 if (irpc->table == table && irpc->callnum == callnum) {
454 irpc = talloc(msg_ctx, struct irpc_list);
455 NT_STATUS_HAVE_NO_MEMORY(irpc);
456 DLIST_ADD(msg_ctx->irpc, irpc);
460 irpc->callnum = callnum;
462 irpc->private = private;
463 GUID_from_string(irpc->table->uuid, &irpc->uuid);
470 handle an incoming irpc reply message
472 static void irpc_handler_reply(struct messaging_context *msg_ctx,
473 struct ndr_pull *ndr, struct irpc_header *header)
475 struct irpc_request *irpc;
477 irpc = idr_find(msg_ctx->idr, header->callid);
478 if (irpc == NULL) return;
480 /* parse the reply data */
481 irpc->status = irpc->table->calls[irpc->callnum].ndr_pull(ndr, NDR_OUT, irpc->r);
482 if (NT_STATUS_IS_OK(irpc->status)) {
483 irpc->status = header->status;
486 if (irpc->async.fn) {
487 irpc->async.fn(irpc);
493 handle an incoming irpc request message
495 static void irpc_handler_request(struct messaging_context *msg_ctx,
496 struct ndr_pull *ndr, struct irpc_header *header,
502 struct irpc_message m;
503 struct ndr_push *push;
506 for (i=msg_ctx->irpc; i; i=i->next) {
507 if (GUID_equal(&i->uuid, &header->uuid) &&
508 i->table->if_version == header->if_version &&
509 i->callnum == header->callnum) {
515 /* no registered handler for this message */
519 /* allocate space for the structure */
520 r = talloc_zero_size(ndr, i->table->calls[header->callnum].struct_size);
521 if (r == NULL) goto failed;
523 /* parse the request data */
524 status = i->table->calls[i->callnum].ndr_pull(ndr, NDR_IN, r);
525 if (!NT_STATUS_IS_OK(status)) goto failed;
529 m.private = i->private;
530 header->status = i->fn(&m, r);
532 /* setup the reply */
533 push = ndr_push_init_ctx(ndr);
534 if (push == NULL) goto failed;
536 header->flags |= IRPC_FLAG_REPLY;
538 /* construct the packet */
539 status = ndr_push_irpc_header(push, NDR_SCALARS|NDR_BUFFERS, header);
540 if (!NT_STATUS_IS_OK(status)) goto failed;
542 status = i->table->calls[i->callnum].ndr_push(push, NDR_OUT, r);
543 if (!NT_STATUS_IS_OK(status)) goto failed;
545 /* send the reply message */
546 packet = ndr_push_blob(push);
547 status = messaging_send(msg_ctx, src, MSG_IRPC, &packet);
548 if (!NT_STATUS_IS_OK(status)) goto failed;
551 /* nothing to clean up */
556 handle an incoming irpc message
558 static void irpc_handler(struct messaging_context *msg_ctx, void *private,
559 uint32_t msg_type, uint32_t src, DATA_BLOB *packet)
561 struct irpc_header header;
562 struct ndr_pull *ndr;
565 ndr = ndr_pull_init_blob(packet, msg_ctx);
566 if (ndr == NULL) goto failed;
568 status = ndr_pull_irpc_header(ndr, NDR_BUFFERS|NDR_SCALARS, &header);
569 if (!NT_STATUS_IS_OK(status)) goto failed;
571 if (header.flags & IRPC_FLAG_REPLY) {
572 irpc_handler_reply(msg_ctx, ndr, &header);
574 irpc_handler_request(msg_ctx, ndr, &header, src);
583 destroy a irpc request
585 static int irpc_destructor(void *ptr)
587 struct irpc_request *irpc = talloc_get_type(ptr, struct irpc_request);
588 idr_remove(irpc->msg_ctx->idr, irpc->callid);
593 timeout a irpc request
595 static void irpc_timeout(struct event_context *ev, struct timed_event *te,
596 struct timeval t, void *private)
598 struct irpc_request *irpc = talloc_get_type(private, struct irpc_request);
599 irpc->status = NT_STATUS_IO_TIMEOUT;
601 if (irpc->async.fn) {
602 irpc->async.fn(irpc);
608 make a irpc call - async send
610 struct irpc_request *irpc_call_send(struct messaging_context *msg_ctx,
612 const struct dcerpc_interface_table *table,
613 int callnum, void *r)
615 struct irpc_header header;
616 struct ndr_push *ndr;
619 struct irpc_request *irpc;
621 irpc = talloc(msg_ctx, struct irpc_request);
622 if (irpc == NULL) goto failed;
624 irpc->msg_ctx = msg_ctx;
626 irpc->callnum = callnum;
627 irpc->callid = idr_get_new(msg_ctx->idr, irpc, UINT16_MAX);
628 if (irpc->callid == -1) goto failed;
631 irpc->async.fn = NULL;
633 talloc_set_destructor(irpc, irpc_destructor);
635 /* setup the header */
636 status = GUID_from_string(table->uuid, &header.uuid);
637 if (!NT_STATUS_IS_OK(status)) goto failed;
639 header.if_version = table->if_version;
640 header.callid = irpc->callid;
641 header.callnum = callnum;
643 header.status = NT_STATUS_OK;
645 /* construct the irpc packet */
646 ndr = ndr_push_init_ctx(irpc);
647 if (ndr == NULL) goto failed;
649 status = ndr_push_irpc_header(ndr, NDR_SCALARS|NDR_BUFFERS, &header);
650 if (!NT_STATUS_IS_OK(status)) goto failed;
652 status = table->calls[callnum].ndr_push(ndr, NDR_IN, r);
653 if (!NT_STATUS_IS_OK(status)) goto failed;
656 packet = ndr_push_blob(ndr);
657 status = messaging_send(msg_ctx, server_id, MSG_IRPC, &packet);
658 if (!NT_STATUS_IS_OK(status)) goto failed;
660 event_add_timed(msg_ctx->event.ev, irpc,
661 timeval_current_ofs(IRPC_CALL_TIMEOUT, 0),
673 wait for a irpc reply
675 NTSTATUS irpc_call_recv(struct irpc_request *irpc)
678 NT_STATUS_HAVE_NO_MEMORY(irpc);
679 while (!irpc->done) {
680 if (event_loop_once(irpc->msg_ctx->event.ev) != 0) {
681 return NT_STATUS_CONNECTION_DISCONNECTED;
684 status = irpc->status;
690 perform a synchronous irpc request
692 NTSTATUS irpc_call(struct messaging_context *msg_ctx,
694 const struct dcerpc_interface_table *table,
695 int callnum, void *r)
697 struct irpc_request *irpc = irpc_call_send(msg_ctx, server_id,
699 return irpc_call_recv(irpc);
703 open the naming database
705 static struct tdb_wrap *irpc_namedb_open(struct messaging_context *msg_ctx)
708 char *path = talloc_asprintf(msg_ctx, "%s/names.tdb", msg_ctx->base_path);
712 t = tdb_wrap_open(msg_ctx, path, 0, 0, O_RDWR|O_CREAT, 0660);
719 add a string name that this irpc server can be called on
721 NTSTATUS irpc_add_name(struct messaging_context *msg_ctx, const char *name)
726 NTSTATUS status = NT_STATUS_OK;
728 t = irpc_namedb_open(msg_ctx);
729 NT_STATUS_HAVE_NO_MEMORY(t);
731 if (tdb_lock_bystring(t->tdb, name) != 0) {
733 return NT_STATUS_LOCK_NOT_GRANTED;
735 rec = tdb_fetch_bystring(t->tdb, name);
736 count = rec.dsize / sizeof(uint32_t);
737 rec.dptr = (char *)realloc_p(rec.dptr, uint32_t, count+1);
738 rec.dsize += sizeof(uint32_t);
739 if (rec.dptr == NULL) {
740 tdb_unlock_bystring(t->tdb, name);
742 return NT_STATUS_NO_MEMORY;
744 ((uint32_t *)rec.dptr)[count] = msg_ctx->server_id;
745 if (tdb_store_bystring(t->tdb, name, rec, 0) != 0) {
746 status = NT_STATUS_INTERNAL_ERROR;
749 tdb_unlock_bystring(t->tdb, name);
752 msg_ctx->names = str_list_add(msg_ctx->names, name);
753 talloc_steal(msg_ctx, msg_ctx->names);
759 return a list of server ids for a server name
761 uint32_t *irpc_servers_byname(struct messaging_context *msg_ctx, const char *name)
768 t = irpc_namedb_open(msg_ctx);
773 if (tdb_lock_bystring(t->tdb, name) != 0) {
777 rec = tdb_fetch_bystring(t->tdb, name);
778 if (rec.dptr == NULL) {
779 tdb_unlock_bystring(t->tdb, name);
783 count = rec.dsize / sizeof(uint32_t);
784 ret = talloc_array(msg_ctx, uint32_t, count+1);
786 tdb_unlock_bystring(t->tdb, name);
790 for (i=0;i<count;i++) {
791 ret[i] = ((uint32_t *)rec.dptr)[i];
795 tdb_unlock_bystring(t->tdb, name);
802 remove a name from a messaging context
804 void irpc_remove_name(struct messaging_context *msg_ctx, const char *name)
811 str_list_remove(msg_ctx->names, name);
813 t = irpc_namedb_open(msg_ctx);
818 if (tdb_lock_bystring(t->tdb, name) != 0) {
822 rec = tdb_fetch_bystring(t->tdb, name);
823 count = rec.dsize / sizeof(uint32_t);
825 tdb_unlock_bystring(t->tdb, name);
829 ids = (uint32_t *)rec.dptr;
830 for (i=0;i<count;i++) {
831 if (ids[i] == msg_ctx->server_id) {
833 memmove(ids+i, ids+i+1, count-(i+1));
835 rec.dsize -= sizeof(uint32_t);
839 tdb_store_bystring(t->tdb, name, rec, 0);
841 tdb_unlock_bystring(t->tdb, name);