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"
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 static void messaging_dispatch(struct messaging_context *msg, struct messaging_rec *rec)
123 struct dispatch_fn *d, *next;
124 for (d=msg->dispatch;d;d=next) {
126 if (d->msg_type == rec->header->msg_type) {
128 data.data = rec->packet.data + sizeof(*rec->header);
129 data.length = rec->header->length;
130 d->fn(msg, d->private, d->msg_type, rec->header->from, &data);
133 rec->header->length = 0;
138 try to send the message
140 static NTSTATUS try_send(struct messaging_rec *rec)
142 struct messaging_context *msg = rec->msg;
147 /* we send with privileges so messages work from any context */
148 priv = root_privileges();
149 status = socket_sendto(msg->sock, &rec->packet, &nsent, 0, rec->path, 0);
156 handle a socket write event
158 static void messaging_send_handler(struct messaging_context *msg)
160 while (msg->pending) {
161 struct messaging_rec *rec = msg->pending;
163 status = try_send(rec);
164 if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
167 if (!NT_STATUS_IS_OK(status)) {
168 DEBUG(1,("messaging: Lost message from %u to %u of type %u - %s\n",
169 rec->header->from, rec->header->to, rec->header->msg_type,
172 DLIST_REMOVE(msg->pending, rec);
175 if (msg->pending == NULL) {
176 EVENT_FD_NOT_WRITEABLE(msg->event.fde);
181 handle a new incoming packet
183 static void messaging_recv_handler(struct messaging_context *msg)
185 struct messaging_rec *rec;
190 /* see how many bytes are in the next packet */
191 status = socket_pending(msg->sock, &msize);
192 if (!NT_STATUS_IS_OK(status)) {
193 DEBUG(0,("socket_pending failed in messaging - %s\n",
198 packet = data_blob_talloc(msg, NULL, msize);
199 if (packet.data == NULL) {
200 /* assume this is temporary and retry */
204 status = socket_recv(msg->sock, packet.data, msize, &msize, 0);
205 if (!NT_STATUS_IS_OK(status)) {
206 data_blob_free(&packet);
210 if (msize < sizeof(*rec->header)) {
211 DEBUG(0,("messaging: bad message of size %d\n", (int)msize));
212 data_blob_free(&packet);
216 rec = talloc(msg, struct messaging_rec);
218 smb_panic("Unable to allocate messaging_rec");
221 talloc_steal(rec, packet.data);
223 rec->path = msg->path;
224 rec->header = (struct messaging_header *)packet.data;
225 rec->packet = packet;
227 if (msize != sizeof(*rec->header) + rec->header->length) {
228 DEBUG(0,("messaging: bad message header size %d should be %d\n",
229 rec->header->length, (int)(msize - sizeof(*rec->header))));
234 messaging_dispatch(msg, rec);
240 handle a socket event
242 static void messaging_handler(struct event_context *ev, struct fd_event *fde,
243 uint16_t flags, void *private)
245 struct messaging_context *msg = talloc_get_type(private,
246 struct messaging_context);
247 if (flags & EVENT_FD_WRITE) {
248 messaging_send_handler(msg);
250 if (flags & EVENT_FD_READ) {
251 messaging_recv_handler(msg);
257 Register a dispatch function for a particular message type.
259 void messaging_register(struct messaging_context *msg, void *private,
261 void (*fn)(struct messaging_context *, void *, uint32_t, uint32_t, DATA_BLOB *))
263 struct dispatch_fn *d;
265 d = talloc(msg, struct dispatch_fn);
266 d->msg_type = msg_type;
267 d->private = private;
269 DLIST_ADD(msg->dispatch, d);
273 De-register the function for a particular message type.
275 void messaging_deregister(struct messaging_context *msg, uint32_t msg_type, void *private)
277 struct dispatch_fn *d, *next;
279 for (d = msg->dispatch; d; d = next) {
281 if (d->msg_type == msg_type &&
282 d->private == private) {
283 DLIST_REMOVE(msg->dispatch, d);
291 Send a message to a particular server
293 NTSTATUS messaging_send(struct messaging_context *msg, uint32_t server,
294 uint32_t msg_type, DATA_BLOB *data)
296 struct messaging_rec *rec;
298 size_t dlength = data?data->length:0;
300 rec = talloc(msg, struct messaging_rec);
302 return NT_STATUS_NO_MEMORY;
305 rec->packet = data_blob_talloc(rec, NULL, sizeof(*rec->header) + dlength);
306 if (rec->packet.data == NULL) {
308 return NT_STATUS_NO_MEMORY;
312 rec->header = (struct messaging_header *)rec->packet.data;
313 rec->header->version = MESSAGING_VERSION;
314 rec->header->msg_type = msg_type;
315 rec->header->from = msg->server_id;
316 rec->header->to = server;
317 rec->header->length = dlength;
319 memcpy(rec->packet.data + sizeof(*rec->header),
320 data->data, dlength);
323 rec->path = messaging_path(msg, server);
324 talloc_steal(rec, rec->path);
326 if (msg->pending != NULL) {
327 status = STATUS_MORE_ENTRIES;
329 status = try_send(rec);
332 if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
333 if (msg->pending == NULL) {
334 EVENT_FD_WRITEABLE(msg->event.fde);
336 DLIST_ADD_END(msg->pending, rec, struct messaging_rec *);
346 Send a message to a particular server, with the message containing a single pointer
348 NTSTATUS messaging_send_ptr(struct messaging_context *msg, uint32_t server,
349 uint32_t msg_type, void *ptr)
353 blob.data = (void *)&ptr;
354 blob.length = sizeof(void *);
356 return messaging_send(msg, server, msg_type, &blob);
361 destroy the messaging context
363 static int messaging_destructor(void *ptr)
365 struct messaging_context *msg = ptr;
367 while (msg->names && msg->names[0]) {
368 irpc_remove_name(msg, msg->names[0]);
374 create the listening socket and setup the dispatcher
376 struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx, uint32_t server_id,
377 struct event_context *ev)
379 struct messaging_context *msg;
383 msg = talloc(mem_ctx, struct messaging_context);
389 ev = event_context_init(msg);
392 /* create the messaging directory if needed */
393 path = smbd_tmp_path(msg, "messaging");
397 msg->base_path = smbd_tmp_path(msg, "messaging");
398 msg->path = messaging_path(msg, server_id);
399 msg->server_id = server_id;
400 msg->dispatch = NULL;
402 msg->idr = idr_init(msg);
405 msg->start_time = timeval_current();
407 status = socket_create("unix", SOCKET_TYPE_DGRAM, &msg->sock, 0);
408 if (!NT_STATUS_IS_OK(status)) {
413 /* by stealing here we ensure that the socket is cleaned up (and even
415 talloc_steal(msg, msg->sock);
417 status = socket_listen(msg->sock, msg->path, 0, 50, 0);
418 if (!NT_STATUS_IS_OK(status)) {
419 DEBUG(0,("Unable to setup messaging listener for '%s':%s\n", msg->path, nt_errstr(status)));
424 /* it needs to be non blocking for sends */
425 set_blocking(socket_get_fd(msg->sock), False);
427 msg->event.ev = talloc_reference(msg, ev);
428 msg->event.fde = event_add_fd(ev, msg, socket_get_fd(msg->sock),
429 EVENT_FD_READ, messaging_handler, msg);
431 talloc_set_destructor(msg, messaging_destructor);
433 messaging_register(msg, NULL, MSG_PING, ping_message);
434 messaging_register(msg, NULL, MSG_IRPC, irpc_handler);
435 IRPC_REGISTER(msg, irpc, IRPC_UPTIME, irpc_uptime, msg);
442 a list of registered irpc server functions
445 struct irpc_list *next, *prev;
447 const struct dcerpc_interface_table *table;
455 register a irpc server function
457 NTSTATUS irpc_register(struct messaging_context *msg_ctx,
458 const struct dcerpc_interface_table *table,
459 int callnum, irpc_function_t fn, void *private)
461 struct irpc_list *irpc;
463 /* override an existing handler, if any */
464 for (irpc=msg_ctx->irpc; irpc; irpc=irpc->next) {
465 if (irpc->table == table && irpc->callnum == callnum) {
470 irpc = talloc(msg_ctx, struct irpc_list);
471 NT_STATUS_HAVE_NO_MEMORY(irpc);
472 DLIST_ADD(msg_ctx->irpc, irpc);
476 irpc->callnum = callnum;
478 irpc->private = private;
479 irpc->uuid = irpc->table->uuid;
486 handle an incoming irpc reply message
488 static void irpc_handler_reply(struct messaging_context *msg_ctx, struct irpc_message *m)
490 struct irpc_request *irpc;
492 irpc = idr_find(msg_ctx->idr, m->header.callid);
493 if (irpc == NULL) return;
495 /* parse the reply data */
496 irpc->status = irpc->table->calls[irpc->callnum].ndr_pull(m->ndr, NDR_OUT, irpc->r);
497 if (NT_STATUS_IS_OK(irpc->status)) {
498 irpc->status = m->header.status;
499 talloc_steal(irpc->mem_ctx, m);
501 talloc_steal(irpc, m);
504 if (irpc->async.fn) {
505 irpc->async.fn(irpc);
512 NTSTATUS irpc_send_reply(struct irpc_message *m, NTSTATUS status)
514 struct ndr_push *push;
517 m->header.status = status;
519 /* setup the reply */
520 push = ndr_push_init_ctx(m->ndr);
522 status = NT_STATUS_NO_MEMORY;
526 m->header.flags |= IRPC_FLAG_REPLY;
528 /* construct the packet */
529 status = ndr_push_irpc_header(push, NDR_SCALARS|NDR_BUFFERS, &m->header);
530 if (!NT_STATUS_IS_OK(status)) goto failed;
532 status = m->irpc->table->calls[m->irpc->callnum].ndr_push(push, NDR_OUT, m->data);
533 if (!NT_STATUS_IS_OK(status)) goto failed;
535 /* send the reply message */
536 packet = ndr_push_blob(push);
537 status = messaging_send(m->msg_ctx, m->from, MSG_IRPC, &packet);
538 if (!NT_STATUS_IS_OK(status)) goto failed;
546 handle an incoming irpc request message
548 static void irpc_handler_request(struct messaging_context *msg_ctx,
549 struct irpc_message *m)
555 for (i=msg_ctx->irpc; i; i=i->next) {
556 if (GUID_equal(&i->uuid, &m->header.uuid) &&
557 i->table->if_version == m->header.if_version &&
558 i->callnum == m->header.callnum) {
564 /* no registered handler for this message */
569 /* allocate space for the structure */
570 r = talloc_zero_size(m->ndr, i->table->calls[m->header.callnum].struct_size);
571 if (r == NULL) goto failed;
573 /* parse the request data */
574 status = i->table->calls[i->callnum].ndr_pull(m->ndr, NDR_IN, r);
575 if (!NT_STATUS_IS_OK(status)) goto failed;
578 m->private = i->private;
579 m->defer_reply = False;
580 m->msg_ctx = msg_ctx;
583 m->ev = msg_ctx->event.ev;
585 m->header.status = i->fn(m, r);
587 if (m->defer_reply) {
588 /* the server function has asked to defer the reply to later */
589 talloc_steal(msg_ctx, m);
593 irpc_send_reply(m, m->header.status);
601 handle an incoming irpc message
603 static void irpc_handler(struct messaging_context *msg_ctx, void *private,
604 uint32_t msg_type, uint32_t src, DATA_BLOB *packet)
606 struct irpc_message *m;
609 m = talloc(msg_ctx, struct irpc_message);
610 if (m == NULL) goto failed;
614 m->ndr = ndr_pull_init_blob(packet, m);
615 if (m->ndr == NULL) goto failed;
617 m->ndr->flags |= LIBNDR_FLAG_REF_ALLOC;
619 status = ndr_pull_irpc_header(m->ndr, NDR_BUFFERS|NDR_SCALARS, &m->header);
620 if (!NT_STATUS_IS_OK(status)) goto failed;
622 if (m->header.flags & IRPC_FLAG_REPLY) {
623 irpc_handler_reply(msg_ctx, m);
625 irpc_handler_request(msg_ctx, m);
635 destroy a irpc request
637 static int irpc_destructor(void *ptr)
639 struct irpc_request *irpc = talloc_get_type(ptr, struct irpc_request);
640 idr_remove(irpc->msg_ctx->idr, irpc->callid);
645 timeout a irpc request
647 static void irpc_timeout(struct event_context *ev, struct timed_event *te,
648 struct timeval t, void *private)
650 struct irpc_request *irpc = talloc_get_type(private, struct irpc_request);
651 irpc->status = NT_STATUS_IO_TIMEOUT;
653 if (irpc->async.fn) {
654 irpc->async.fn(irpc);
660 make a irpc call - async send
662 struct irpc_request *irpc_call_send(struct messaging_context *msg_ctx,
664 const struct dcerpc_interface_table *table,
665 int callnum, void *r, TALLOC_CTX *ctx)
667 struct irpc_header header;
668 struct ndr_push *ndr;
671 struct irpc_request *irpc;
673 irpc = talloc(msg_ctx, struct irpc_request);
674 if (irpc == NULL) goto failed;
676 irpc->msg_ctx = msg_ctx;
678 irpc->callnum = callnum;
679 irpc->callid = idr_get_new(msg_ctx->idr, irpc, UINT16_MAX);
680 if (irpc->callid == -1) goto failed;
683 irpc->async.fn = NULL;
686 talloc_set_destructor(irpc, irpc_destructor);
688 /* setup the header */
689 header.uuid = table->uuid;
691 header.if_version = table->if_version;
692 header.callid = irpc->callid;
693 header.callnum = callnum;
695 header.status = NT_STATUS_OK;
697 /* construct the irpc packet */
698 ndr = ndr_push_init_ctx(irpc);
699 if (ndr == NULL) goto failed;
701 status = ndr_push_irpc_header(ndr, NDR_SCALARS|NDR_BUFFERS, &header);
702 if (!NT_STATUS_IS_OK(status)) goto failed;
704 status = table->calls[callnum].ndr_push(ndr, NDR_IN, r);
705 if (!NT_STATUS_IS_OK(status)) goto failed;
708 packet = ndr_push_blob(ndr);
709 status = messaging_send(msg_ctx, server_id, MSG_IRPC, &packet);
710 if (!NT_STATUS_IS_OK(status)) goto failed;
712 event_add_timed(msg_ctx->event.ev, irpc,
713 timeval_current_ofs(IRPC_CALL_TIMEOUT, 0),
725 wait for a irpc reply
727 NTSTATUS irpc_call_recv(struct irpc_request *irpc)
731 NT_STATUS_HAVE_NO_MEMORY(irpc);
733 while (!irpc->done) {
734 if (event_loop_once(irpc->msg_ctx->event.ev) != 0) {
735 return NT_STATUS_CONNECTION_DISCONNECTED;
738 status = irpc->status;
744 perform a synchronous irpc request
746 NTSTATUS irpc_call(struct messaging_context *msg_ctx,
748 const struct dcerpc_interface_table *table,
749 int callnum, void *r,
752 struct irpc_request *irpc = irpc_call_send(msg_ctx, server_id,
753 table, callnum, r, mem_ctx);
754 return irpc_call_recv(irpc);
758 open the naming database
760 static struct tdb_wrap *irpc_namedb_open(struct messaging_context *msg_ctx)
763 char *path = talloc_asprintf(msg_ctx, "%s/names.tdb", msg_ctx->base_path);
767 t = tdb_wrap_open(msg_ctx, path, 0, 0, O_RDWR|O_CREAT, 0660);
774 add a string name that this irpc server can be called on
776 NTSTATUS irpc_add_name(struct messaging_context *msg_ctx, const char *name)
781 NTSTATUS status = NT_STATUS_OK;
783 t = irpc_namedb_open(msg_ctx);
784 NT_STATUS_HAVE_NO_MEMORY(t);
786 if (tdb_lock_bystring(t->tdb, name) != 0) {
788 return NT_STATUS_LOCK_NOT_GRANTED;
790 rec = tdb_fetch_bystring(t->tdb, name);
791 count = rec.dsize / sizeof(uint32_t);
792 rec.dptr = (unsigned char *)realloc_p(rec.dptr, uint32_t, count+1);
793 rec.dsize += sizeof(uint32_t);
794 if (rec.dptr == NULL) {
795 tdb_unlock_bystring(t->tdb, name);
797 return NT_STATUS_NO_MEMORY;
799 ((uint32_t *)rec.dptr)[count] = msg_ctx->server_id;
800 if (tdb_store_bystring(t->tdb, name, rec, 0) != 0) {
801 status = NT_STATUS_INTERNAL_ERROR;
804 tdb_unlock_bystring(t->tdb, name);
807 msg_ctx->names = str_list_add(msg_ctx->names, name);
808 talloc_steal(msg_ctx, msg_ctx->names);
814 return a list of server ids for a server name
816 uint32_t *irpc_servers_byname(struct messaging_context *msg_ctx, const char *name)
823 t = irpc_namedb_open(msg_ctx);
828 if (tdb_lock_bystring(t->tdb, name) != 0) {
832 rec = tdb_fetch_bystring(t->tdb, name);
833 if (rec.dptr == NULL) {
834 tdb_unlock_bystring(t->tdb, name);
838 count = rec.dsize / sizeof(uint32_t);
839 ret = talloc_array(msg_ctx, uint32_t, count+1);
841 tdb_unlock_bystring(t->tdb, name);
845 for (i=0;i<count;i++) {
846 ret[i] = ((uint32_t *)rec.dptr)[i];
850 tdb_unlock_bystring(t->tdb, name);
857 remove a name from a messaging context
859 void irpc_remove_name(struct messaging_context *msg_ctx, const char *name)
866 str_list_remove(msg_ctx->names, name);
868 t = irpc_namedb_open(msg_ctx);
873 if (tdb_lock_bystring(t->tdb, name) != 0) {
877 rec = tdb_fetch_bystring(t->tdb, name);
878 count = rec.dsize / sizeof(uint32_t);
880 tdb_unlock_bystring(t->tdb, name);
884 ids = (uint32_t *)rec.dptr;
885 for (i=0;i<count;i++) {
886 if (ids[i] == msg_ctx->server_id) {
888 memmove(ids+i, ids+i+1, count-(i+1));
890 rec.dsize -= sizeof(uint32_t);
894 tdb_store_bystring(t->tdb, name, rec, 0);
896 tdb_unlock_bystring(t->tdb, name);