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"
33 #include "util/unix_privs.h"
35 /* change the message version with any incompatible changes in the protocol */
36 #define MESSAGING_VERSION 1
38 struct messaging_context {
40 struct socket_context *sock;
41 const char *base_path;
43 struct dispatch_fn *dispatch;
44 struct messaging_rec *pending;
45 struct irpc_list *irpc;
46 struct idr_context *idr;
48 struct timeval start_time;
51 struct event_context *ev;
56 /* we have a linked list of dispatch handlers that this messaging
57 server can deal with */
59 struct dispatch_fn *next, *prev;
62 void (*fn)(struct messaging_context *msg, void *private,
63 uint32_t msg_type, uint32_t server_id, DATA_BLOB *data);
66 /* an individual message */
67 struct messaging_rec {
68 struct messaging_rec *next, *prev;
69 struct messaging_context *msg;
72 struct messaging_header {
84 static void irpc_handler(struct messaging_context *, void *,
85 uint32_t, uint32_t, DATA_BLOB *);
89 A useful function for testing the message system.
91 static void ping_message(struct messaging_context *msg, void *private,
92 uint32_t msg_type, uint32_t src, DATA_BLOB *data)
94 DEBUG(1,("INFO: Received PING message from server %u [%.*s]\n",
95 (uint_t)src, (int)data->length,
96 data->data?(const char *)data->data:""));
97 messaging_send(msg, src, MSG_PONG, data);
101 return uptime of messaging server via irpc
103 static NTSTATUS irpc_uptime(struct irpc_message *msg,
104 struct irpc_uptime *r)
106 struct messaging_context *ctx = talloc_get_type(msg->private, struct messaging_context);
107 *r->out.start_time = timeval_to_nttime(&ctx->start_time);
112 return the path to a messaging socket
114 static char *messaging_path(struct messaging_context *msg, uint32_t server_id)
116 return talloc_asprintf(msg, "%s/msg.%u", msg->base_path, (unsigned)server_id);
120 dispatch a fully received message
122 note that this deliberately can match more than one message handler
123 per message. That allows a single messasging context to register
124 (for example) a debug handler for more than one piece of code
126 static void messaging_dispatch(struct messaging_context *msg, struct messaging_rec *rec)
128 struct dispatch_fn *d, *next;
129 for (d=msg->dispatch;d;d=next) {
131 if (d->msg_type == rec->header->msg_type) {
133 data.data = rec->packet.data + sizeof(*rec->header);
134 data.length = rec->header->length;
135 d->fn(msg, d->private, d->msg_type, rec->header->from, &data);
138 rec->header->length = 0;
143 try to send the message
145 static NTSTATUS try_send(struct messaging_rec *rec)
147 struct messaging_context *msg = rec->msg;
151 struct socket_address *path;
153 /* rec->path is the path of the *other* socket, where we want
155 path = socket_address_from_strings(msg, msg->sock->backend_name,
158 return NT_STATUS_NO_MEMORY;
161 /* we send with privileges so messages work from any context */
162 priv = root_privileges();
163 status = socket_sendto(msg->sock, &rec->packet, &nsent, 0, path);
171 handle a socket write event
173 static void messaging_send_handler(struct messaging_context *msg)
175 while (msg->pending) {
176 struct messaging_rec *rec = msg->pending;
178 status = try_send(rec);
179 if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
182 if (!NT_STATUS_IS_OK(status)) {
183 DEBUG(1,("messaging: Lost message from %u to %u of type %u - %s\n",
184 rec->header->from, rec->header->to, rec->header->msg_type,
187 DLIST_REMOVE(msg->pending, rec);
190 if (msg->pending == NULL) {
191 EVENT_FD_NOT_WRITEABLE(msg->event.fde);
196 handle a new incoming packet
198 static void messaging_recv_handler(struct messaging_context *msg)
200 struct messaging_rec *rec;
205 /* see how many bytes are in the next packet */
206 status = socket_pending(msg->sock, &msize);
207 if (!NT_STATUS_IS_OK(status)) {
208 DEBUG(0,("socket_pending failed in messaging - %s\n",
213 packet = data_blob_talloc(msg, NULL, msize);
214 if (packet.data == NULL) {
215 /* assume this is temporary and retry */
219 status = socket_recv(msg->sock, packet.data, msize, &msize, 0);
220 if (!NT_STATUS_IS_OK(status)) {
221 data_blob_free(&packet);
225 if (msize < sizeof(*rec->header)) {
226 DEBUG(0,("messaging: bad message of size %d\n", (int)msize));
227 data_blob_free(&packet);
231 rec = talloc(msg, struct messaging_rec);
233 smb_panic("Unable to allocate messaging_rec");
236 talloc_steal(rec, packet.data);
238 rec->path = msg->path;
239 rec->header = (struct messaging_header *)packet.data;
240 rec->packet = packet;
242 if (msize != sizeof(*rec->header) + rec->header->length) {
243 DEBUG(0,("messaging: bad message header size %d should be %d\n",
244 rec->header->length, (int)(msize - sizeof(*rec->header))));
249 messaging_dispatch(msg, rec);
255 handle a socket event
257 static void messaging_handler(struct event_context *ev, struct fd_event *fde,
258 uint16_t flags, void *private)
260 struct messaging_context *msg = talloc_get_type(private,
261 struct messaging_context);
262 if (flags & EVENT_FD_WRITE) {
263 messaging_send_handler(msg);
265 if (flags & EVENT_FD_READ) {
266 messaging_recv_handler(msg);
272 Register a dispatch function for a particular message type.
274 void messaging_register(struct messaging_context *msg, void *private,
276 void (*fn)(struct messaging_context *, void *, uint32_t, uint32_t, DATA_BLOB *))
278 struct dispatch_fn *d;
280 d = talloc(msg, struct dispatch_fn);
281 d->msg_type = msg_type;
282 d->private = private;
284 DLIST_ADD(msg->dispatch, d);
288 De-register the function for a particular message type.
290 void messaging_deregister(struct messaging_context *msg, uint32_t msg_type, void *private)
292 struct dispatch_fn *d, *next;
294 for (d = msg->dispatch; d; d = next) {
296 if (d->msg_type == msg_type &&
297 d->private == private) {
298 DLIST_REMOVE(msg->dispatch, d);
306 Send a message to a particular server
308 NTSTATUS messaging_send(struct messaging_context *msg, uint32_t server,
309 uint32_t msg_type, DATA_BLOB *data)
311 struct messaging_rec *rec;
313 size_t dlength = data?data->length:0;
315 rec = talloc(msg, struct messaging_rec);
317 return NT_STATUS_NO_MEMORY;
320 rec->packet = data_blob_talloc(rec, NULL, sizeof(*rec->header) + dlength);
321 if (rec->packet.data == NULL) {
323 return NT_STATUS_NO_MEMORY;
327 rec->header = (struct messaging_header *)rec->packet.data;
328 rec->header->version = MESSAGING_VERSION;
329 rec->header->msg_type = msg_type;
330 rec->header->from = msg->server_id;
331 rec->header->to = server;
332 rec->header->length = dlength;
334 memcpy(rec->packet.data + sizeof(*rec->header),
335 data->data, dlength);
338 rec->path = messaging_path(msg, server);
339 talloc_steal(rec, rec->path);
341 if (msg->pending != NULL) {
342 status = STATUS_MORE_ENTRIES;
344 status = try_send(rec);
347 if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
348 if (msg->pending == NULL) {
349 EVENT_FD_WRITEABLE(msg->event.fde);
351 DLIST_ADD_END(msg->pending, rec, struct messaging_rec *);
361 Send a message to a particular server, with the message containing a single pointer
363 NTSTATUS messaging_send_ptr(struct messaging_context *msg, uint32_t server,
364 uint32_t msg_type, void *ptr)
368 blob.data = (void *)&ptr;
369 blob.length = sizeof(void *);
371 return messaging_send(msg, server, msg_type, &blob);
376 destroy the messaging context
378 static int messaging_destructor(void *ptr)
380 struct messaging_context *msg = ptr;
382 while (msg->names && msg->names[0]) {
383 irpc_remove_name(msg, msg->names[0]);
389 create the listening socket and setup the dispatcher
391 struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx, uint32_t server_id,
392 struct event_context *ev)
394 struct messaging_context *msg;
396 struct socket_address *path;
399 msg = talloc(mem_ctx, struct messaging_context);
405 ev = event_context_init(msg);
408 /* create the messaging directory if needed */
409 dir = smbd_tmp_path(msg, "messaging");
413 msg->base_path = smbd_tmp_path(msg, "messaging");
414 msg->path = messaging_path(msg, server_id);
415 msg->server_id = server_id;
416 msg->dispatch = NULL;
418 msg->idr = idr_init(msg);
421 msg->start_time = timeval_current();
423 status = socket_create("unix", SOCKET_TYPE_DGRAM, &msg->sock, 0);
424 if (!NT_STATUS_IS_OK(status)) {
429 /* by stealing here we ensure that the socket is cleaned up (and even
431 talloc_steal(msg, msg->sock);
433 path = socket_address_from_strings(msg, msg->sock->backend_name,
440 status = socket_listen(msg->sock, path, 50, 0);
441 if (!NT_STATUS_IS_OK(status)) {
442 DEBUG(0,("Unable to setup messaging listener for '%s':%s\n", msg->path, nt_errstr(status)));
447 /* it needs to be non blocking for sends */
448 set_blocking(socket_get_fd(msg->sock), False);
450 msg->event.ev = talloc_reference(msg, ev);
451 msg->event.fde = event_add_fd(ev, msg, socket_get_fd(msg->sock),
452 EVENT_FD_READ, messaging_handler, msg);
454 talloc_set_destructor(msg, messaging_destructor);
456 messaging_register(msg, NULL, MSG_PING, ping_message);
457 messaging_register(msg, NULL, MSG_IRPC, irpc_handler);
458 IRPC_REGISTER(msg, irpc, IRPC_UPTIME, irpc_uptime, msg);
464 A hack, for the short term until we get 'client only' messaging in place
466 struct messaging_context *messaging_client_init(TALLOC_CTX *mem_ctx,
467 struct event_context *ev)
469 return messaging_init(mem_ctx, random() % 0x10000000, ev);
472 a list of registered irpc server functions
475 struct irpc_list *next, *prev;
477 const struct dcerpc_interface_table *table;
485 register a irpc server function
487 NTSTATUS irpc_register(struct messaging_context *msg_ctx,
488 const struct dcerpc_interface_table *table,
489 int callnum, irpc_function_t fn, void *private)
491 struct irpc_list *irpc;
493 /* override an existing handler, if any */
494 for (irpc=msg_ctx->irpc; irpc; irpc=irpc->next) {
495 if (irpc->table == table && irpc->callnum == callnum) {
500 irpc = talloc(msg_ctx, struct irpc_list);
501 NT_STATUS_HAVE_NO_MEMORY(irpc);
502 DLIST_ADD(msg_ctx->irpc, irpc);
506 irpc->callnum = callnum;
508 irpc->private = private;
509 irpc->uuid = irpc->table->uuid;
516 handle an incoming irpc reply message
518 static void irpc_handler_reply(struct messaging_context *msg_ctx, struct irpc_message *m)
520 struct irpc_request *irpc;
522 irpc = idr_find(msg_ctx->idr, m->header.callid);
523 if (irpc == NULL) return;
525 /* parse the reply data */
526 irpc->status = irpc->table->calls[irpc->callnum].ndr_pull(m->ndr, NDR_OUT, irpc->r);
527 if (NT_STATUS_IS_OK(irpc->status)) {
528 irpc->status = m->header.status;
529 talloc_steal(irpc->mem_ctx, m);
531 talloc_steal(irpc, m);
534 if (irpc->async.fn) {
535 irpc->async.fn(irpc);
542 NTSTATUS irpc_send_reply(struct irpc_message *m, NTSTATUS status)
544 struct ndr_push *push;
547 m->header.status = status;
549 /* setup the reply */
550 push = ndr_push_init_ctx(m->ndr);
552 status = NT_STATUS_NO_MEMORY;
556 m->header.flags |= IRPC_FLAG_REPLY;
558 /* construct the packet */
559 status = ndr_push_irpc_header(push, NDR_SCALARS|NDR_BUFFERS, &m->header);
560 if (!NT_STATUS_IS_OK(status)) goto failed;
562 status = m->irpc->table->calls[m->irpc->callnum].ndr_push(push, NDR_OUT, m->data);
563 if (!NT_STATUS_IS_OK(status)) goto failed;
565 /* send the reply message */
566 packet = ndr_push_blob(push);
567 status = messaging_send(m->msg_ctx, m->from, MSG_IRPC, &packet);
568 if (!NT_STATUS_IS_OK(status)) goto failed;
576 handle an incoming irpc request message
578 static void irpc_handler_request(struct messaging_context *msg_ctx,
579 struct irpc_message *m)
585 for (i=msg_ctx->irpc; i; i=i->next) {
586 if (GUID_equal(&i->uuid, &m->header.uuid) &&
587 i->table->if_version == m->header.if_version &&
588 i->callnum == m->header.callnum) {
594 /* no registered handler for this message */
599 /* allocate space for the structure */
600 r = talloc_zero_size(m->ndr, i->table->calls[m->header.callnum].struct_size);
601 if (r == NULL) goto failed;
603 /* parse the request data */
604 status = i->table->calls[i->callnum].ndr_pull(m->ndr, NDR_IN, r);
605 if (!NT_STATUS_IS_OK(status)) goto failed;
608 m->private = i->private;
609 m->defer_reply = False;
610 m->msg_ctx = msg_ctx;
613 m->ev = msg_ctx->event.ev;
615 m->header.status = i->fn(m, r);
617 if (m->defer_reply) {
618 /* the server function has asked to defer the reply to later */
619 talloc_steal(msg_ctx, m);
623 irpc_send_reply(m, m->header.status);
631 handle an incoming irpc message
633 static void irpc_handler(struct messaging_context *msg_ctx, void *private,
634 uint32_t msg_type, uint32_t src, DATA_BLOB *packet)
636 struct irpc_message *m;
639 m = talloc(msg_ctx, struct irpc_message);
640 if (m == NULL) goto failed;
644 m->ndr = ndr_pull_init_blob(packet, m);
645 if (m->ndr == NULL) goto failed;
647 m->ndr->flags |= LIBNDR_FLAG_REF_ALLOC;
649 status = ndr_pull_irpc_header(m->ndr, NDR_BUFFERS|NDR_SCALARS, &m->header);
650 if (!NT_STATUS_IS_OK(status)) goto failed;
652 if (m->header.flags & IRPC_FLAG_REPLY) {
653 irpc_handler_reply(msg_ctx, m);
655 irpc_handler_request(msg_ctx, m);
665 destroy a irpc request
667 static int irpc_destructor(void *ptr)
669 struct irpc_request *irpc = talloc_get_type(ptr, struct irpc_request);
670 idr_remove(irpc->msg_ctx->idr, irpc->callid);
675 timeout a irpc request
677 static void irpc_timeout(struct event_context *ev, struct timed_event *te,
678 struct timeval t, void *private)
680 struct irpc_request *irpc = talloc_get_type(private, struct irpc_request);
681 irpc->status = NT_STATUS_IO_TIMEOUT;
683 if (irpc->async.fn) {
684 irpc->async.fn(irpc);
690 make a irpc call - async send
692 struct irpc_request *irpc_call_send(struct messaging_context *msg_ctx,
694 const struct dcerpc_interface_table *table,
695 int callnum, void *r, TALLOC_CTX *ctx)
697 struct irpc_header header;
698 struct ndr_push *ndr;
701 struct irpc_request *irpc;
703 irpc = talloc(msg_ctx, struct irpc_request);
704 if (irpc == NULL) goto failed;
706 irpc->msg_ctx = msg_ctx;
708 irpc->callnum = callnum;
709 irpc->callid = idr_get_new(msg_ctx->idr, irpc, UINT16_MAX);
710 if (irpc->callid == -1) goto failed;
713 irpc->async.fn = NULL;
716 talloc_set_destructor(irpc, irpc_destructor);
718 /* setup the header */
719 header.uuid = table->uuid;
721 header.if_version = table->if_version;
722 header.callid = irpc->callid;
723 header.callnum = callnum;
725 header.status = NT_STATUS_OK;
727 /* construct the irpc packet */
728 ndr = ndr_push_init_ctx(irpc);
729 if (ndr == NULL) goto failed;
731 status = ndr_push_irpc_header(ndr, NDR_SCALARS|NDR_BUFFERS, &header);
732 if (!NT_STATUS_IS_OK(status)) goto failed;
734 status = table->calls[callnum].ndr_push(ndr, NDR_IN, r);
735 if (!NT_STATUS_IS_OK(status)) goto failed;
738 packet = ndr_push_blob(ndr);
739 status = messaging_send(msg_ctx, server_id, MSG_IRPC, &packet);
740 if (!NT_STATUS_IS_OK(status)) goto failed;
742 event_add_timed(msg_ctx->event.ev, irpc,
743 timeval_current_ofs(IRPC_CALL_TIMEOUT, 0),
755 wait for a irpc reply
757 NTSTATUS irpc_call_recv(struct irpc_request *irpc)
761 NT_STATUS_HAVE_NO_MEMORY(irpc);
763 while (!irpc->done) {
764 if (event_loop_once(irpc->msg_ctx->event.ev) != 0) {
765 return NT_STATUS_CONNECTION_DISCONNECTED;
768 status = irpc->status;
774 perform a synchronous irpc request
776 NTSTATUS irpc_call(struct messaging_context *msg_ctx,
778 const struct dcerpc_interface_table *table,
779 int callnum, void *r,
782 struct irpc_request *irpc = irpc_call_send(msg_ctx, server_id,
783 table, callnum, r, mem_ctx);
784 return irpc_call_recv(irpc);
788 open the naming database
790 static struct tdb_wrap *irpc_namedb_open(struct messaging_context *msg_ctx)
793 char *path = talloc_asprintf(msg_ctx, "%s/names.tdb", msg_ctx->base_path);
797 t = tdb_wrap_open(msg_ctx, path, 0, 0, O_RDWR|O_CREAT, 0660);
804 add a string name that this irpc server can be called on
806 NTSTATUS irpc_add_name(struct messaging_context *msg_ctx, const char *name)
811 NTSTATUS status = NT_STATUS_OK;
813 t = irpc_namedb_open(msg_ctx);
814 NT_STATUS_HAVE_NO_MEMORY(t);
816 if (tdb_lock_bystring(t->tdb, name) != 0) {
818 return NT_STATUS_LOCK_NOT_GRANTED;
820 rec = tdb_fetch_bystring(t->tdb, name);
821 count = rec.dsize / sizeof(uint32_t);
822 rec.dptr = (unsigned char *)realloc_p(rec.dptr, uint32_t, count+1);
823 rec.dsize += sizeof(uint32_t);
824 if (rec.dptr == NULL) {
825 tdb_unlock_bystring(t->tdb, name);
827 return NT_STATUS_NO_MEMORY;
829 ((uint32_t *)rec.dptr)[count] = msg_ctx->server_id;
830 if (tdb_store_bystring(t->tdb, name, rec, 0) != 0) {
831 status = NT_STATUS_INTERNAL_ERROR;
834 tdb_unlock_bystring(t->tdb, name);
837 msg_ctx->names = str_list_add(msg_ctx->names, name);
838 talloc_steal(msg_ctx, msg_ctx->names);
844 return a list of server ids for a server name
846 uint32_t *irpc_servers_byname(struct messaging_context *msg_ctx, const char *name)
853 t = irpc_namedb_open(msg_ctx);
858 if (tdb_lock_bystring(t->tdb, name) != 0) {
862 rec = tdb_fetch_bystring(t->tdb, name);
863 if (rec.dptr == NULL) {
864 tdb_unlock_bystring(t->tdb, name);
868 count = rec.dsize / sizeof(uint32_t);
869 ret = talloc_array(msg_ctx, uint32_t, count+1);
871 tdb_unlock_bystring(t->tdb, name);
875 for (i=0;i<count;i++) {
876 ret[i] = ((uint32_t *)rec.dptr)[i];
880 tdb_unlock_bystring(t->tdb, name);
887 remove a name from a messaging context
889 void irpc_remove_name(struct messaging_context *msg_ctx, const char *name)
896 str_list_remove(msg_ctx->names, name);
898 t = irpc_namedb_open(msg_ctx);
903 if (tdb_lock_bystring(t->tdb, name) != 0) {
907 rec = tdb_fetch_bystring(t->tdb, name);
908 count = rec.dsize / sizeof(uint32_t);
910 tdb_unlock_bystring(t->tdb, name);
914 ids = (uint32_t *)rec.dptr;
915 for (i=0;i<count;i++) {
916 if (ids[i] == msg_ctx->server_id) {
918 memmove(ids+i, ids+i+1, count-(i+1));
920 rec.dsize -= sizeof(uint32_t);
924 tdb_store_bystring(t->tdb, name, rec, 0);
926 tdb_unlock_bystring(t->tdb, name);