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/filesys.h"
26 #include "messaging/messaging.h"
27 #include "dlinklist.h"
28 #include "lib/socket/socket.h"
29 #include "librpc/gen_ndr/ndr_irpc.h"
30 #include "lib/messaging/irpc.h"
32 #include "lib/tdb/include/tdbutil.h"
34 /* change the message version with any incompatible changes in the protocol */
35 #define MESSAGING_VERSION 1
37 struct messaging_context {
39 struct socket_context *sock;
40 const char *base_path;
42 struct dispatch_fn *dispatch;
43 struct messaging_rec *pending;
44 struct irpc_list *irpc;
45 struct idr_context *idr;
47 struct timeval start_time;
50 struct event_context *ev;
55 /* we have a linked list of dispatch handlers that this messaging
56 server can deal with */
58 struct dispatch_fn *next, *prev;
61 void (*fn)(struct messaging_context *msg, void *private,
62 uint32_t msg_type, uint32_t server_id, DATA_BLOB *data);
65 /* an individual message */
66 struct messaging_rec {
67 struct messaging_rec *next, *prev;
68 struct messaging_context *msg;
71 struct messaging_header {
83 static void irpc_handler(struct messaging_context *, void *,
84 uint32_t, uint32_t, DATA_BLOB *);
88 A useful function for testing the message system.
90 static void ping_message(struct messaging_context *msg, void *private,
91 uint32_t msg_type, uint32_t src, DATA_BLOB *data)
93 DEBUG(1,("INFO: Received PING message from server %u [%.*s]\n",
94 (uint_t)src, (int)data->length,
95 data->data?(const char *)data->data:""));
96 messaging_send(msg, src, MSG_PONG, data);
100 return uptime of messaging server via irpc
102 static NTSTATUS irpc_uptime(struct irpc_message *msg,
103 struct irpc_uptime *r)
105 struct messaging_context *ctx = talloc_get_type(msg->private, struct messaging_context);
106 *r->out.start_time = timeval_to_nttime(&ctx->start_time);
111 return the path to a messaging socket
113 static char *messaging_path(struct messaging_context *msg, uint32_t server_id)
115 return talloc_asprintf(msg, "%s/msg.%u", msg->base_path, (unsigned)server_id);
119 dispatch a fully received message
121 note that this deliberately can match more than one message handler
122 per message. That allows a single messasging context to register
123 (for example) a debug handler for more than one piece of code
125 static void messaging_dispatch(struct messaging_context *msg, struct messaging_rec *rec)
127 struct dispatch_fn *d, *next;
128 for (d=msg->dispatch;d;d=next) {
130 if (d->msg_type == rec->header->msg_type) {
132 data.data = rec->packet.data + sizeof(*rec->header);
133 data.length = rec->header->length;
134 d->fn(msg, d->private, d->msg_type, rec->header->from, &data);
137 rec->header->length = 0;
142 try to send the message
144 static NTSTATUS try_send(struct messaging_rec *rec)
146 struct messaging_context *msg = rec->msg;
150 struct socket_address *path;
152 /* rec->path is the path of the *other* socket, where we want
154 path = socket_address_from_strings(msg, msg->sock->backend_name,
157 return NT_STATUS_NO_MEMORY;
160 /* we send with privileges so messages work from any context */
161 priv = root_privileges();
162 status = socket_sendto(msg->sock, &rec->packet, &nsent, 0, path);
170 handle a socket write event
172 static void messaging_send_handler(struct messaging_context *msg)
174 while (msg->pending) {
175 struct messaging_rec *rec = msg->pending;
177 status = try_send(rec);
178 if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
181 if (!NT_STATUS_IS_OK(status)) {
182 DEBUG(1,("messaging: Lost message from %u to %u of type %u - %s\n",
183 rec->header->from, rec->header->to, rec->header->msg_type,
186 DLIST_REMOVE(msg->pending, rec);
189 if (msg->pending == NULL) {
190 EVENT_FD_NOT_WRITEABLE(msg->event.fde);
195 handle a new incoming packet
197 static void messaging_recv_handler(struct messaging_context *msg)
199 struct messaging_rec *rec;
204 /* see how many bytes are in the next packet */
205 status = socket_pending(msg->sock, &msize);
206 if (!NT_STATUS_IS_OK(status)) {
207 DEBUG(0,("socket_pending failed in messaging - %s\n",
212 packet = data_blob_talloc(msg, NULL, msize);
213 if (packet.data == NULL) {
214 /* assume this is temporary and retry */
218 status = socket_recv(msg->sock, packet.data, msize, &msize, 0);
219 if (!NT_STATUS_IS_OK(status)) {
220 data_blob_free(&packet);
224 if (msize < sizeof(*rec->header)) {
225 DEBUG(0,("messaging: bad message of size %d\n", (int)msize));
226 data_blob_free(&packet);
230 rec = talloc(msg, struct messaging_rec);
232 smb_panic("Unable to allocate messaging_rec");
235 talloc_steal(rec, packet.data);
237 rec->path = msg->path;
238 rec->header = (struct messaging_header *)packet.data;
239 rec->packet = packet;
241 if (msize != sizeof(*rec->header) + rec->header->length) {
242 DEBUG(0,("messaging: bad message header size %d should be %d\n",
243 rec->header->length, (int)(msize - sizeof(*rec->header))));
248 messaging_dispatch(msg, rec);
254 handle a socket event
256 static void messaging_handler(struct event_context *ev, struct fd_event *fde,
257 uint16_t flags, void *private)
259 struct messaging_context *msg = talloc_get_type(private,
260 struct messaging_context);
261 if (flags & EVENT_FD_WRITE) {
262 messaging_send_handler(msg);
264 if (flags & EVENT_FD_READ) {
265 messaging_recv_handler(msg);
271 Register a dispatch function for a particular message type.
273 void messaging_register(struct messaging_context *msg, void *private,
275 void (*fn)(struct messaging_context *, void *, uint32_t, uint32_t, DATA_BLOB *))
277 struct dispatch_fn *d;
279 d = talloc(msg, struct dispatch_fn);
280 d->msg_type = msg_type;
281 d->private = private;
283 DLIST_ADD(msg->dispatch, d);
287 De-register the function for a particular message type.
289 void messaging_deregister(struct messaging_context *msg, uint32_t msg_type, void *private)
291 struct dispatch_fn *d, *next;
293 for (d = msg->dispatch; d; d = next) {
295 if (d->msg_type == msg_type &&
296 d->private == private) {
297 DLIST_REMOVE(msg->dispatch, d);
305 Send a message to a particular server
307 NTSTATUS messaging_send(struct messaging_context *msg, uint32_t server,
308 uint32_t msg_type, DATA_BLOB *data)
310 struct messaging_rec *rec;
312 size_t dlength = data?data->length:0;
314 rec = talloc(msg, struct messaging_rec);
316 return NT_STATUS_NO_MEMORY;
319 rec->packet = data_blob_talloc(rec, NULL, sizeof(*rec->header) + dlength);
320 if (rec->packet.data == NULL) {
322 return NT_STATUS_NO_MEMORY;
326 rec->header = (struct messaging_header *)rec->packet.data;
327 rec->header->version = MESSAGING_VERSION;
328 rec->header->msg_type = msg_type;
329 rec->header->from = msg->server_id;
330 rec->header->to = server;
331 rec->header->length = dlength;
333 memcpy(rec->packet.data + sizeof(*rec->header),
334 data->data, dlength);
337 rec->path = messaging_path(msg, server);
338 talloc_steal(rec, rec->path);
340 if (msg->pending != NULL) {
341 status = STATUS_MORE_ENTRIES;
343 status = try_send(rec);
346 if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
347 if (msg->pending == NULL) {
348 EVENT_FD_WRITEABLE(msg->event.fde);
350 DLIST_ADD_END(msg->pending, rec, struct messaging_rec *);
360 Send a message to a particular server, with the message containing a single pointer
362 NTSTATUS messaging_send_ptr(struct messaging_context *msg, uint32_t server,
363 uint32_t msg_type, void *ptr)
367 blob.data = (void *)&ptr;
368 blob.length = sizeof(void *);
370 return messaging_send(msg, server, msg_type, &blob);
375 destroy the messaging context
377 static int messaging_destructor(void *ptr)
379 struct messaging_context *msg = ptr;
381 while (msg->names && msg->names[0]) {
382 irpc_remove_name(msg, msg->names[0]);
388 create the listening socket and setup the dispatcher
390 struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx, uint32_t server_id,
391 struct event_context *ev)
393 struct messaging_context *msg;
395 struct socket_address *path;
398 msg = talloc(mem_ctx, struct messaging_context);
404 ev = event_context_init(msg);
407 /* create the messaging directory if needed */
408 dir = smbd_tmp_path(msg, "messaging");
412 msg->base_path = smbd_tmp_path(msg, "messaging");
413 msg->path = messaging_path(msg, server_id);
414 msg->server_id = server_id;
415 msg->dispatch = NULL;
417 msg->idr = idr_init(msg);
420 msg->start_time = timeval_current();
422 status = socket_create("unix", SOCKET_TYPE_DGRAM, &msg->sock, 0);
423 if (!NT_STATUS_IS_OK(status)) {
428 /* by stealing here we ensure that the socket is cleaned up (and even
430 talloc_steal(msg, msg->sock);
432 path = socket_address_from_strings(msg, msg->sock->backend_name,
439 status = socket_listen(msg->sock, path, 50, 0);
440 if (!NT_STATUS_IS_OK(status)) {
441 DEBUG(0,("Unable to setup messaging listener for '%s':%s\n", msg->path, nt_errstr(status)));
446 /* it needs to be non blocking for sends */
447 set_blocking(socket_get_fd(msg->sock), False);
449 msg->event.ev = talloc_reference(msg, ev);
450 msg->event.fde = event_add_fd(ev, msg, socket_get_fd(msg->sock),
451 EVENT_FD_READ, messaging_handler, msg);
453 talloc_set_destructor(msg, messaging_destructor);
455 messaging_register(msg, NULL, MSG_PING, ping_message);
456 messaging_register(msg, NULL, MSG_IRPC, irpc_handler);
457 IRPC_REGISTER(msg, irpc, IRPC_UPTIME, irpc_uptime, msg);
463 A hack, for the short term until we get 'client only' messaging in place
465 struct messaging_context *messaging_client_init(TALLOC_CTX *mem_ctx,
466 struct event_context *ev)
468 return messaging_init(mem_ctx, random() % 0x10000000, ev);
471 a list of registered irpc server functions
474 struct irpc_list *next, *prev;
476 const struct dcerpc_interface_table *table;
484 register a irpc server function
486 NTSTATUS irpc_register(struct messaging_context *msg_ctx,
487 const struct dcerpc_interface_table *table,
488 int callnum, irpc_function_t fn, void *private)
490 struct irpc_list *irpc;
492 /* override an existing handler, if any */
493 for (irpc=msg_ctx->irpc; irpc; irpc=irpc->next) {
494 if (irpc->table == table && irpc->callnum == callnum) {
499 irpc = talloc(msg_ctx, struct irpc_list);
500 NT_STATUS_HAVE_NO_MEMORY(irpc);
501 DLIST_ADD(msg_ctx->irpc, irpc);
505 irpc->callnum = callnum;
507 irpc->private = private;
508 irpc->uuid = irpc->table->uuid;
515 handle an incoming irpc reply message
517 static void irpc_handler_reply(struct messaging_context *msg_ctx, struct irpc_message *m)
519 struct irpc_request *irpc;
521 irpc = idr_find(msg_ctx->idr, m->header.callid);
522 if (irpc == NULL) return;
524 /* parse the reply data */
525 irpc->status = irpc->table->calls[irpc->callnum].ndr_pull(m->ndr, NDR_OUT, irpc->r);
526 if (NT_STATUS_IS_OK(irpc->status)) {
527 irpc->status = m->header.status;
528 talloc_steal(irpc->mem_ctx, m);
530 talloc_steal(irpc, m);
533 if (irpc->async.fn) {
534 irpc->async.fn(irpc);
541 NTSTATUS irpc_send_reply(struct irpc_message *m, NTSTATUS status)
543 struct ndr_push *push;
546 m->header.status = status;
548 /* setup the reply */
549 push = ndr_push_init_ctx(m->ndr);
551 status = NT_STATUS_NO_MEMORY;
555 m->header.flags |= IRPC_FLAG_REPLY;
557 /* construct the packet */
558 status = ndr_push_irpc_header(push, NDR_SCALARS|NDR_BUFFERS, &m->header);
559 if (!NT_STATUS_IS_OK(status)) goto failed;
561 status = m->irpc->table->calls[m->irpc->callnum].ndr_push(push, NDR_OUT, m->data);
562 if (!NT_STATUS_IS_OK(status)) goto failed;
564 /* send the reply message */
565 packet = ndr_push_blob(push);
566 status = messaging_send(m->msg_ctx, m->from, MSG_IRPC, &packet);
567 if (!NT_STATUS_IS_OK(status)) goto failed;
575 handle an incoming irpc request message
577 static void irpc_handler_request(struct messaging_context *msg_ctx,
578 struct irpc_message *m)
584 for (i=msg_ctx->irpc; i; i=i->next) {
585 if (GUID_equal(&i->uuid, &m->header.uuid) &&
586 i->table->if_version == m->header.if_version &&
587 i->callnum == m->header.callnum) {
593 /* no registered handler for this message */
598 /* allocate space for the structure */
599 r = talloc_zero_size(m->ndr, i->table->calls[m->header.callnum].struct_size);
600 if (r == NULL) goto failed;
602 /* parse the request data */
603 status = i->table->calls[i->callnum].ndr_pull(m->ndr, NDR_IN, r);
604 if (!NT_STATUS_IS_OK(status)) goto failed;
607 m->private = i->private;
608 m->defer_reply = False;
609 m->msg_ctx = msg_ctx;
612 m->ev = msg_ctx->event.ev;
614 m->header.status = i->fn(m, r);
616 if (m->defer_reply) {
617 /* the server function has asked to defer the reply to later */
618 talloc_steal(msg_ctx, m);
622 irpc_send_reply(m, m->header.status);
630 handle an incoming irpc message
632 static void irpc_handler(struct messaging_context *msg_ctx, void *private,
633 uint32_t msg_type, uint32_t src, DATA_BLOB *packet)
635 struct irpc_message *m;
638 m = talloc(msg_ctx, struct irpc_message);
639 if (m == NULL) goto failed;
643 m->ndr = ndr_pull_init_blob(packet, m);
644 if (m->ndr == NULL) goto failed;
646 m->ndr->flags |= LIBNDR_FLAG_REF_ALLOC;
648 status = ndr_pull_irpc_header(m->ndr, NDR_BUFFERS|NDR_SCALARS, &m->header);
649 if (!NT_STATUS_IS_OK(status)) goto failed;
651 if (m->header.flags & IRPC_FLAG_REPLY) {
652 irpc_handler_reply(msg_ctx, m);
654 irpc_handler_request(msg_ctx, m);
664 destroy a irpc request
666 static int irpc_destructor(void *ptr)
668 struct irpc_request *irpc = talloc_get_type(ptr, struct irpc_request);
669 idr_remove(irpc->msg_ctx->idr, irpc->callid);
674 timeout a irpc request
676 static void irpc_timeout(struct event_context *ev, struct timed_event *te,
677 struct timeval t, void *private)
679 struct irpc_request *irpc = talloc_get_type(private, struct irpc_request);
680 irpc->status = NT_STATUS_IO_TIMEOUT;
682 if (irpc->async.fn) {
683 irpc->async.fn(irpc);
689 make a irpc call - async send
691 struct irpc_request *irpc_call_send(struct messaging_context *msg_ctx,
693 const struct dcerpc_interface_table *table,
694 int callnum, void *r, TALLOC_CTX *ctx)
696 struct irpc_header header;
697 struct ndr_push *ndr;
700 struct irpc_request *irpc;
702 irpc = talloc(msg_ctx, struct irpc_request);
703 if (irpc == NULL) goto failed;
705 irpc->msg_ctx = msg_ctx;
707 irpc->callnum = callnum;
708 irpc->callid = idr_get_new(msg_ctx->idr, irpc, UINT16_MAX);
709 if (irpc->callid == -1) goto failed;
712 irpc->async.fn = NULL;
715 talloc_set_destructor(irpc, irpc_destructor);
717 /* setup the header */
718 header.uuid = table->uuid;
720 header.if_version = table->if_version;
721 header.callid = irpc->callid;
722 header.callnum = callnum;
724 header.status = NT_STATUS_OK;
726 /* construct the irpc packet */
727 ndr = ndr_push_init_ctx(irpc);
728 if (ndr == NULL) goto failed;
730 status = ndr_push_irpc_header(ndr, NDR_SCALARS|NDR_BUFFERS, &header);
731 if (!NT_STATUS_IS_OK(status)) goto failed;
733 status = table->calls[callnum].ndr_push(ndr, NDR_IN, r);
734 if (!NT_STATUS_IS_OK(status)) goto failed;
737 packet = ndr_push_blob(ndr);
738 status = messaging_send(msg_ctx, server_id, MSG_IRPC, &packet);
739 if (!NT_STATUS_IS_OK(status)) goto failed;
741 event_add_timed(msg_ctx->event.ev, irpc,
742 timeval_current_ofs(IRPC_CALL_TIMEOUT, 0),
754 wait for a irpc reply
756 NTSTATUS irpc_call_recv(struct irpc_request *irpc)
760 NT_STATUS_HAVE_NO_MEMORY(irpc);
762 while (!irpc->done) {
763 if (event_loop_once(irpc->msg_ctx->event.ev) != 0) {
764 return NT_STATUS_CONNECTION_DISCONNECTED;
767 status = irpc->status;
773 perform a synchronous irpc request
775 NTSTATUS irpc_call(struct messaging_context *msg_ctx,
777 const struct dcerpc_interface_table *table,
778 int callnum, void *r,
781 struct irpc_request *irpc = irpc_call_send(msg_ctx, server_id,
782 table, callnum, r, mem_ctx);
783 return irpc_call_recv(irpc);
787 open the naming database
789 static struct tdb_wrap *irpc_namedb_open(struct messaging_context *msg_ctx)
792 char *path = talloc_asprintf(msg_ctx, "%s/names.tdb", msg_ctx->base_path);
796 t = tdb_wrap_open(msg_ctx, path, 0, 0, O_RDWR|O_CREAT, 0660);
803 add a string name that this irpc server can be called on
805 NTSTATUS irpc_add_name(struct messaging_context *msg_ctx, const char *name)
810 NTSTATUS status = NT_STATUS_OK;
812 t = irpc_namedb_open(msg_ctx);
813 NT_STATUS_HAVE_NO_MEMORY(t);
815 if (tdb_lock_bystring(t->tdb, name) != 0) {
817 return NT_STATUS_LOCK_NOT_GRANTED;
819 rec = tdb_fetch_bystring(t->tdb, name);
820 count = rec.dsize / sizeof(uint32_t);
821 rec.dptr = (unsigned char *)realloc_p(rec.dptr, uint32_t, count+1);
822 rec.dsize += sizeof(uint32_t);
823 if (rec.dptr == NULL) {
824 tdb_unlock_bystring(t->tdb, name);
826 return NT_STATUS_NO_MEMORY;
828 ((uint32_t *)rec.dptr)[count] = msg_ctx->server_id;
829 if (tdb_store_bystring(t->tdb, name, rec, 0) != 0) {
830 status = NT_STATUS_INTERNAL_ERROR;
833 tdb_unlock_bystring(t->tdb, name);
836 msg_ctx->names = str_list_add(msg_ctx->names, name);
837 talloc_steal(msg_ctx, msg_ctx->names);
843 return a list of server ids for a server name
845 uint32_t *irpc_servers_byname(struct messaging_context *msg_ctx, const char *name)
852 t = irpc_namedb_open(msg_ctx);
857 if (tdb_lock_bystring(t->tdb, name) != 0) {
861 rec = tdb_fetch_bystring(t->tdb, name);
862 if (rec.dptr == NULL) {
863 tdb_unlock_bystring(t->tdb, name);
867 count = rec.dsize / sizeof(uint32_t);
868 ret = talloc_array(msg_ctx, uint32_t, count+1);
870 tdb_unlock_bystring(t->tdb, name);
874 for (i=0;i<count;i++) {
875 ret[i] = ((uint32_t *)rec.dptr)[i];
879 tdb_unlock_bystring(t->tdb, name);
886 remove a name from a messaging context
888 void irpc_remove_name(struct messaging_context *msg_ctx, const char *name)
895 str_list_remove(msg_ctx->names, name);
897 t = irpc_namedb_open(msg_ctx);
902 if (tdb_lock_bystring(t->tdb, name) != 0) {
906 rec = tdb_fetch_bystring(t->tdb, name);
907 count = rec.dsize / sizeof(uint32_t);
909 tdb_unlock_bystring(t->tdb, name);
913 ids = (uint32_t *)rec.dptr;
914 for (i=0;i<count;i++) {
915 if (ids[i] == msg_ctx->server_id) {
917 memmove(ids+i, ids+i+1, count-(i+1));
919 rec.dsize -= sizeof(uint32_t);
923 tdb_store_bystring(t->tdb, name, rec, 0);
925 tdb_unlock_bystring(t->tdb, name);