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 "lib/util/unix_privs.h"
34 #include "librpc/rpc/dcerpc.h"
36 /* change the message version with any incompatible changes in the protocol */
37 #define MESSAGING_VERSION 1
39 struct messaging_context {
41 struct socket_context *sock;
42 const char *base_path;
44 struct dispatch_fn *dispatch;
45 struct messaging_rec *pending;
46 struct irpc_list *irpc;
47 struct idr_context *idr;
49 struct timeval start_time;
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, (int)data->length,
97 data->data?(const char *)data->data:""));
98 messaging_send(msg, src, MSG_PONG, data);
102 return uptime of messaging server via irpc
104 static NTSTATUS irpc_uptime(struct irpc_message *msg,
105 struct irpc_uptime *r)
107 struct messaging_context *ctx = talloc_get_type(msg->private, struct messaging_context);
108 *r->out.start_time = timeval_to_nttime(&ctx->start_time);
113 return the path to a messaging socket
115 static char *messaging_path(struct messaging_context *msg, uint32_t server_id)
117 return talloc_asprintf(msg, "%s/msg.%u", msg->base_path, (unsigned)server_id);
121 dispatch a fully received message
123 note that this deliberately can match more than one message handler
124 per message. That allows a single messasging context to register
125 (for example) a debug handler for more than one piece of code
127 static void messaging_dispatch(struct messaging_context *msg, struct messaging_rec *rec)
129 struct dispatch_fn *d, *next;
130 for (d=msg->dispatch;d;d=next) {
132 if (d->msg_type == rec->header->msg_type) {
134 data.data = rec->packet.data + sizeof(*rec->header);
135 data.length = rec->header->length;
136 d->fn(msg, d->private, d->msg_type, rec->header->from, &data);
139 rec->header->length = 0;
144 try to send the message
146 static NTSTATUS try_send(struct messaging_rec *rec)
148 struct messaging_context *msg = rec->msg;
152 struct socket_address *path;
154 /* rec->path is the path of the *other* socket, where we want
156 path = socket_address_from_strings(msg, msg->sock->backend_name,
159 return NT_STATUS_NO_MEMORY;
162 /* we send with privileges so messages work from any context */
163 priv = root_privileges();
164 status = socket_sendto(msg->sock, &rec->packet, &nsent, 0, path);
172 handle a socket write event
174 static void messaging_send_handler(struct messaging_context *msg)
176 while (msg->pending) {
177 struct messaging_rec *rec = msg->pending;
179 status = try_send(rec);
180 if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
183 if (!NT_STATUS_IS_OK(status)) {
184 DEBUG(1,("messaging: Lost message from %u to %u of type %u - %s\n",
185 rec->header->from, rec->header->to, rec->header->msg_type,
188 DLIST_REMOVE(msg->pending, rec);
191 if (msg->pending == NULL) {
192 EVENT_FD_NOT_WRITEABLE(msg->event.fde);
197 handle a new incoming packet
199 static void messaging_recv_handler(struct messaging_context *msg)
201 struct messaging_rec *rec;
206 /* see how many bytes are in the next packet */
207 status = socket_pending(msg->sock, &msize);
208 if (!NT_STATUS_IS_OK(status)) {
209 DEBUG(0,("socket_pending failed in messaging - %s\n",
214 packet = data_blob_talloc(msg, NULL, msize);
215 if (packet.data == NULL) {
216 /* assume this is temporary and retry */
220 status = socket_recv(msg->sock, packet.data, msize, &msize, 0);
221 if (!NT_STATUS_IS_OK(status)) {
222 data_blob_free(&packet);
226 if (msize < sizeof(*rec->header)) {
227 DEBUG(0,("messaging: bad message of size %d\n", (int)msize));
228 data_blob_free(&packet);
232 rec = talloc(msg, struct messaging_rec);
234 smb_panic("Unable to allocate messaging_rec");
237 talloc_steal(rec, packet.data);
239 rec->path = msg->path;
240 rec->header = (struct messaging_header *)packet.data;
241 rec->packet = packet;
243 if (msize != sizeof(*rec->header) + rec->header->length) {
244 DEBUG(0,("messaging: bad message header size %d should be %d\n",
245 rec->header->length, (int)(msize - sizeof(*rec->header))));
250 messaging_dispatch(msg, rec);
256 handle a socket event
258 static void messaging_handler(struct event_context *ev, struct fd_event *fde,
259 uint16_t flags, void *private)
261 struct messaging_context *msg = talloc_get_type(private,
262 struct messaging_context);
263 if (flags & EVENT_FD_WRITE) {
264 messaging_send_handler(msg);
266 if (flags & EVENT_FD_READ) {
267 messaging_recv_handler(msg);
273 Register a dispatch function for a particular message type.
275 void messaging_register(struct messaging_context *msg, void *private,
277 void (*fn)(struct messaging_context *, void *, uint32_t, uint32_t, DATA_BLOB *))
279 struct dispatch_fn *d;
281 d = talloc(msg, struct dispatch_fn);
282 d->msg_type = msg_type;
283 d->private = private;
285 DLIST_ADD(msg->dispatch, d);
289 De-register the function for a particular message type.
291 void messaging_deregister(struct messaging_context *msg, uint32_t msg_type, void *private)
293 struct dispatch_fn *d, *next;
295 for (d = msg->dispatch; d; d = next) {
297 if (d->msg_type == msg_type &&
298 d->private == private) {
299 DLIST_REMOVE(msg->dispatch, d);
307 Send a message to a particular server
309 NTSTATUS messaging_send(struct messaging_context *msg, uint32_t server,
310 uint32_t msg_type, DATA_BLOB *data)
312 struct messaging_rec *rec;
314 size_t dlength = data?data->length:0;
316 rec = talloc(msg, struct messaging_rec);
318 return NT_STATUS_NO_MEMORY;
321 rec->packet = data_blob_talloc(rec, NULL, sizeof(*rec->header) + dlength);
322 if (rec->packet.data == NULL) {
324 return NT_STATUS_NO_MEMORY;
328 rec->header = (struct messaging_header *)rec->packet.data;
329 rec->header->version = MESSAGING_VERSION;
330 rec->header->msg_type = msg_type;
331 rec->header->from = msg->server_id;
332 rec->header->to = server;
333 rec->header->length = dlength;
335 memcpy(rec->packet.data + sizeof(*rec->header),
336 data->data, dlength);
339 rec->path = messaging_path(msg, server);
340 talloc_steal(rec, rec->path);
342 if (msg->pending != NULL) {
343 status = STATUS_MORE_ENTRIES;
345 status = try_send(rec);
348 if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
349 if (msg->pending == NULL) {
350 EVENT_FD_WRITEABLE(msg->event.fde);
352 DLIST_ADD_END(msg->pending, rec, struct messaging_rec *);
362 Send a message to a particular server, with the message containing a single pointer
364 NTSTATUS messaging_send_ptr(struct messaging_context *msg, uint32_t server,
365 uint32_t msg_type, void *ptr)
369 blob.data = (void *)&ptr;
370 blob.length = sizeof(void *);
372 return messaging_send(msg, server, msg_type, &blob);
377 destroy the messaging context
379 static int messaging_destructor(void *ptr)
381 struct messaging_context *msg = ptr;
383 while (msg->names && msg->names[0]) {
384 irpc_remove_name(msg, msg->names[0]);
390 create the listening socket and setup the dispatcher
392 struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx, uint32_t server_id,
393 struct event_context *ev)
395 struct messaging_context *msg;
397 struct socket_address *path;
400 msg = talloc(mem_ctx, struct messaging_context);
406 ev = event_context_init(msg);
409 /* create the messaging directory if needed */
410 dir = smbd_tmp_path(msg, "messaging");
414 msg->base_path = smbd_tmp_path(msg, "messaging");
415 msg->path = messaging_path(msg, server_id);
416 msg->server_id = server_id;
417 msg->dispatch = NULL;
419 msg->idr = idr_init(msg);
422 msg->start_time = timeval_current();
424 status = socket_create("unix", SOCKET_TYPE_DGRAM, &msg->sock, 0);
425 if (!NT_STATUS_IS_OK(status)) {
430 /* by stealing here we ensure that the socket is cleaned up (and even
432 talloc_steal(msg, msg->sock);
434 path = socket_address_from_strings(msg, msg->sock->backend_name,
441 status = socket_listen(msg->sock, path, 50, 0);
442 if (!NT_STATUS_IS_OK(status)) {
443 DEBUG(0,("Unable to setup messaging listener for '%s':%s\n", msg->path, nt_errstr(status)));
448 /* it needs to be non blocking for sends */
449 set_blocking(socket_get_fd(msg->sock), False);
451 msg->event.ev = talloc_reference(msg, ev);
452 msg->event.fde = event_add_fd(ev, msg, socket_get_fd(msg->sock),
453 EVENT_FD_READ, messaging_handler, msg);
455 talloc_set_destructor(msg, messaging_destructor);
457 messaging_register(msg, NULL, MSG_PING, ping_message);
458 messaging_register(msg, NULL, MSG_IRPC, irpc_handler);
459 IRPC_REGISTER(msg, irpc, IRPC_UPTIME, irpc_uptime, msg);
465 A hack, for the short term until we get 'client only' messaging in place
467 struct messaging_context *messaging_client_init(TALLOC_CTX *mem_ctx,
468 struct event_context *ev)
470 return messaging_init(mem_ctx, random() % 0x10000000, ev);
473 a list of registered irpc server functions
476 struct irpc_list *next, *prev;
478 const struct dcerpc_interface_table *table;
486 register a irpc server function
488 NTSTATUS irpc_register(struct messaging_context *msg_ctx,
489 const struct dcerpc_interface_table *table,
490 int callnum, irpc_function_t fn, void *private)
492 struct irpc_list *irpc;
494 /* override an existing handler, if any */
495 for (irpc=msg_ctx->irpc; irpc; irpc=irpc->next) {
496 if (irpc->table == table && irpc->callnum == callnum) {
501 irpc = talloc(msg_ctx, struct irpc_list);
502 NT_STATUS_HAVE_NO_MEMORY(irpc);
503 DLIST_ADD(msg_ctx->irpc, irpc);
507 irpc->callnum = callnum;
509 irpc->private = private;
510 irpc->uuid = irpc->table->syntax_id.uuid;
517 handle an incoming irpc reply message
519 static void irpc_handler_reply(struct messaging_context *msg_ctx, struct irpc_message *m)
521 struct irpc_request *irpc;
523 irpc = idr_find(msg_ctx->idr, m->header.callid);
524 if (irpc == NULL) return;
526 /* parse the reply data */
527 irpc->status = irpc->table->calls[irpc->callnum].ndr_pull(m->ndr, NDR_OUT, irpc->r);
528 if (NT_STATUS_IS_OK(irpc->status)) {
529 irpc->status = m->header.status;
530 talloc_steal(irpc->mem_ctx, m);
532 talloc_steal(irpc, m);
535 if (irpc->async.fn) {
536 irpc->async.fn(irpc);
543 NTSTATUS irpc_send_reply(struct irpc_message *m, NTSTATUS status)
545 struct ndr_push *push;
548 m->header.status = status;
550 /* setup the reply */
551 push = ndr_push_init_ctx(m->ndr);
553 status = NT_STATUS_NO_MEMORY;
557 m->header.flags |= IRPC_FLAG_REPLY;
559 /* construct the packet */
560 status = ndr_push_irpc_header(push, NDR_SCALARS|NDR_BUFFERS, &m->header);
561 if (!NT_STATUS_IS_OK(status)) goto failed;
563 status = m->irpc->table->calls[m->irpc->callnum].ndr_push(push, NDR_OUT, m->data);
564 if (!NT_STATUS_IS_OK(status)) goto failed;
566 /* send the reply message */
567 packet = ndr_push_blob(push);
568 status = messaging_send(m->msg_ctx, m->from, MSG_IRPC, &packet);
569 if (!NT_STATUS_IS_OK(status)) goto failed;
577 handle an incoming irpc request message
579 static void irpc_handler_request(struct messaging_context *msg_ctx,
580 struct irpc_message *m)
586 for (i=msg_ctx->irpc; i; i=i->next) {
587 if (GUID_equal(&i->uuid, &m->header.uuid) &&
588 i->table->syntax_id.if_version == m->header.if_version &&
589 i->callnum == m->header.callnum) {
595 /* no registered handler for this message */
600 /* allocate space for the structure */
601 r = talloc_zero_size(m->ndr, i->table->calls[m->header.callnum].struct_size);
602 if (r == NULL) goto failed;
604 /* parse the request data */
605 status = i->table->calls[i->callnum].ndr_pull(m->ndr, NDR_IN, r);
606 if (!NT_STATUS_IS_OK(status)) goto failed;
609 m->private = i->private;
610 m->defer_reply = False;
611 m->msg_ctx = msg_ctx;
614 m->ev = msg_ctx->event.ev;
616 m->header.status = i->fn(m, r);
618 if (m->defer_reply) {
619 /* the server function has asked to defer the reply to later */
620 talloc_steal(msg_ctx, m);
624 irpc_send_reply(m, m->header.status);
632 handle an incoming irpc message
634 static void irpc_handler(struct messaging_context *msg_ctx, void *private,
635 uint32_t msg_type, uint32_t src, DATA_BLOB *packet)
637 struct irpc_message *m;
640 m = talloc(msg_ctx, struct irpc_message);
641 if (m == NULL) goto failed;
645 m->ndr = ndr_pull_init_blob(packet, m);
646 if (m->ndr == NULL) goto failed;
648 m->ndr->flags |= LIBNDR_FLAG_REF_ALLOC;
650 status = ndr_pull_irpc_header(m->ndr, NDR_BUFFERS|NDR_SCALARS, &m->header);
651 if (!NT_STATUS_IS_OK(status)) goto failed;
653 if (m->header.flags & IRPC_FLAG_REPLY) {
654 irpc_handler_reply(msg_ctx, m);
656 irpc_handler_request(msg_ctx, m);
666 destroy a irpc request
668 static int irpc_destructor(void *ptr)
670 struct irpc_request *irpc = talloc_get_type(ptr, struct irpc_request);
671 idr_remove(irpc->msg_ctx->idr, irpc->callid);
676 timeout a irpc request
678 static void irpc_timeout(struct event_context *ev, struct timed_event *te,
679 struct timeval t, void *private)
681 struct irpc_request *irpc = talloc_get_type(private, struct irpc_request);
682 irpc->status = NT_STATUS_IO_TIMEOUT;
684 if (irpc->async.fn) {
685 irpc->async.fn(irpc);
691 make a irpc call - async send
693 struct irpc_request *irpc_call_send(struct messaging_context *msg_ctx,
695 const struct dcerpc_interface_table *table,
696 int callnum, void *r, TALLOC_CTX *ctx)
698 struct irpc_header header;
699 struct ndr_push *ndr;
702 struct irpc_request *irpc;
704 irpc = talloc(msg_ctx, struct irpc_request);
705 if (irpc == NULL) goto failed;
707 irpc->msg_ctx = msg_ctx;
709 irpc->callnum = callnum;
710 irpc->callid = idr_get_new(msg_ctx->idr, irpc, UINT16_MAX);
711 if (irpc->callid == -1) goto failed;
714 irpc->async.fn = NULL;
717 talloc_set_destructor(irpc, irpc_destructor);
719 /* setup the header */
720 header.uuid = table->syntax_id.uuid;
722 header.if_version = table->syntax_id.if_version;
723 header.callid = irpc->callid;
724 header.callnum = callnum;
726 header.status = NT_STATUS_OK;
728 /* construct the irpc packet */
729 ndr = ndr_push_init_ctx(irpc);
730 if (ndr == NULL) goto failed;
732 status = ndr_push_irpc_header(ndr, NDR_SCALARS|NDR_BUFFERS, &header);
733 if (!NT_STATUS_IS_OK(status)) goto failed;
735 status = table->calls[callnum].ndr_push(ndr, NDR_IN, r);
736 if (!NT_STATUS_IS_OK(status)) goto failed;
739 packet = ndr_push_blob(ndr);
740 status = messaging_send(msg_ctx, server_id, MSG_IRPC, &packet);
741 if (!NT_STATUS_IS_OK(status)) goto failed;
743 event_add_timed(msg_ctx->event.ev, irpc,
744 timeval_current_ofs(IRPC_CALL_TIMEOUT, 0),
756 wait for a irpc reply
758 NTSTATUS irpc_call_recv(struct irpc_request *irpc)
762 NT_STATUS_HAVE_NO_MEMORY(irpc);
764 while (!irpc->done) {
765 if (event_loop_once(irpc->msg_ctx->event.ev) != 0) {
766 return NT_STATUS_CONNECTION_DISCONNECTED;
769 status = irpc->status;
775 perform a synchronous irpc request
777 NTSTATUS irpc_call(struct messaging_context *msg_ctx,
779 const struct dcerpc_interface_table *table,
780 int callnum, void *r,
783 struct irpc_request *irpc = irpc_call_send(msg_ctx, server_id,
784 table, callnum, r, mem_ctx);
785 return irpc_call_recv(irpc);
789 open the naming database
791 static struct tdb_wrap *irpc_namedb_open(struct messaging_context *msg_ctx)
794 char *path = talloc_asprintf(msg_ctx, "%s/names.tdb", msg_ctx->base_path);
798 t = tdb_wrap_open(msg_ctx, path, 0, 0, O_RDWR|O_CREAT, 0660);
805 add a string name that this irpc server can be called on
807 NTSTATUS irpc_add_name(struct messaging_context *msg_ctx, const char *name)
812 NTSTATUS status = NT_STATUS_OK;
814 t = irpc_namedb_open(msg_ctx);
815 NT_STATUS_HAVE_NO_MEMORY(t);
817 if (tdb_lock_bystring(t->tdb, name) != 0) {
819 return NT_STATUS_LOCK_NOT_GRANTED;
821 rec = tdb_fetch_bystring(t->tdb, name);
822 count = rec.dsize / sizeof(uint32_t);
823 rec.dptr = (unsigned char *)realloc_p(rec.dptr, uint32_t, count+1);
824 rec.dsize += sizeof(uint32_t);
825 if (rec.dptr == NULL) {
826 tdb_unlock_bystring(t->tdb, name);
828 return NT_STATUS_NO_MEMORY;
830 ((uint32_t *)rec.dptr)[count] = msg_ctx->server_id;
831 if (tdb_store_bystring(t->tdb, name, rec, 0) != 0) {
832 status = NT_STATUS_INTERNAL_ERROR;
835 tdb_unlock_bystring(t->tdb, name);
838 msg_ctx->names = str_list_add(msg_ctx->names, name);
839 talloc_steal(msg_ctx, msg_ctx->names);
845 return a list of server ids for a server name
847 uint32_t *irpc_servers_byname(struct messaging_context *msg_ctx, const char *name)
854 t = irpc_namedb_open(msg_ctx);
859 if (tdb_lock_bystring(t->tdb, name) != 0) {
863 rec = tdb_fetch_bystring(t->tdb, name);
864 if (rec.dptr == NULL) {
865 tdb_unlock_bystring(t->tdb, name);
869 count = rec.dsize / sizeof(uint32_t);
870 ret = talloc_array(msg_ctx, uint32_t, count+1);
872 tdb_unlock_bystring(t->tdb, name);
876 for (i=0;i<count;i++) {
877 ret[i] = ((uint32_t *)rec.dptr)[i];
881 tdb_unlock_bystring(t->tdb, name);
888 remove a name from a messaging context
890 void irpc_remove_name(struct messaging_context *msg_ctx, const char *name)
897 str_list_remove(msg_ctx->names, name);
899 t = irpc_namedb_open(msg_ctx);
904 if (tdb_lock_bystring(t->tdb, name) != 0) {
908 rec = tdb_fetch_bystring(t->tdb, name);
909 count = rec.dsize / sizeof(uint32_t);
911 tdb_unlock_bystring(t->tdb, name);
915 ids = (uint32_t *)rec.dptr;
916 for (i=0;i<count;i++) {
917 if (ids[i] == msg_ctx->server_id) {
919 memmove(ids+i, ids+i+1, count-(i+1));
921 rec.dsize -= sizeof(uint32_t);
925 tdb_store_bystring(t->tdb, name, rec, 0);
927 tdb_unlock_bystring(t->tdb, name);