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;
50 struct timeval start_time;
53 struct event_context *ev;
58 /* we have a linked list of dispatch handlers that this messaging
59 server can deal with */
61 struct dispatch_fn *next, *prev;
64 void (*fn)(struct messaging_context *msg, void *private,
65 uint32_t msg_type, uint32_t server_id, DATA_BLOB *data);
68 /* an individual message */
69 struct messaging_rec {
70 struct messaging_rec *next, *prev;
71 struct messaging_context *msg;
74 struct messaging_header {
86 static void irpc_handler(struct messaging_context *, void *,
87 uint32_t, uint32_t, DATA_BLOB *);
91 A useful function for testing the message system.
93 static void ping_message(struct messaging_context *msg, void *private,
94 uint32_t msg_type, uint32_t src, DATA_BLOB *data)
96 DEBUG(1,("INFO: Received PING message from server %u [%.*s]\n",
97 (uint_t)src, (int)data->length,
98 data->data?(const char *)data->data:""));
99 messaging_send(msg, src, MSG_PONG, data);
103 return uptime of messaging server via irpc
105 static NTSTATUS irpc_uptime(struct irpc_message *msg,
106 struct irpc_uptime *r)
108 struct messaging_context *ctx = talloc_get_type(msg->private, struct messaging_context);
109 *r->out.start_time = timeval_to_nttime(&ctx->start_time);
114 return the path to a messaging socket
116 static char *messaging_path(struct messaging_context *msg, uint32_t server_id)
118 return talloc_asprintf(msg, "%s/msg.%u", msg->base_path, (unsigned)server_id);
122 dispatch a fully received message
124 static void messaging_dispatch(struct messaging_context *msg, struct messaging_rec *rec)
126 struct dispatch_fn *d, *next;
127 for (d=msg->dispatch;d;d=next) {
129 if (d->msg_type == rec->header->msg_type) {
131 data.data = rec->packet.data + sizeof(*rec->header);
132 data.length = rec->header->length;
133 d->fn(msg, d->private, d->msg_type, rec->header->from, &data);
136 rec->header->length = 0;
141 try to send the message
143 static NTSTATUS try_send(struct messaging_rec *rec)
145 struct messaging_context *msg = rec->msg;
150 /* we send with privileges so messages work from any context */
151 priv = root_privileges();
152 status = socket_sendto(msg->sock, &rec->packet, &nsent, 0, rec->path, 0);
159 handle a socket write event
161 static void messaging_send_handler(struct messaging_context *msg)
163 while (msg->pending) {
164 struct messaging_rec *rec = msg->pending;
166 status = try_send(rec);
167 if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
170 if (!NT_STATUS_IS_OK(status)) {
171 DEBUG(1,("messaging: Lost message from %u to %u of type %u - %s\n",
172 rec->header->from, rec->header->to, rec->header->msg_type,
175 DLIST_REMOVE(msg->pending, rec);
178 if (msg->pending == NULL) {
179 EVENT_FD_NOT_WRITEABLE(msg->event.fde);
184 handle a new incoming packet
186 static void messaging_recv_handler(struct messaging_context *msg)
188 struct messaging_rec *rec;
193 /* see how many bytes are in the next packet */
194 status = socket_pending(msg->sock, &msize);
195 if (!NT_STATUS_IS_OK(status)) {
196 DEBUG(0,("socket_pending failed in messaging - %s\n",
201 packet = data_blob_talloc(msg, NULL, msize);
202 if (packet.data == NULL) {
203 /* assume this is temporary and retry */
207 status = socket_recv(msg->sock, packet.data, msize, &msize, 0);
208 if (!NT_STATUS_IS_OK(status)) {
209 data_blob_free(&packet);
213 if (msize < sizeof(*rec->header)) {
214 DEBUG(0,("messaging: bad message of size %d\n", (int)msize));
215 data_blob_free(&packet);
219 rec = talloc(msg, struct messaging_rec);
221 smb_panic("Unable to allocate messaging_rec");
224 talloc_steal(rec, packet.data);
226 rec->path = msg->path;
227 rec->header = (struct messaging_header *)packet.data;
228 rec->packet = packet;
230 if (msize != sizeof(*rec->header) + rec->header->length) {
231 DEBUG(0,("messaging: bad message header size %d should be %d\n",
232 rec->header->length, (int)(msize - sizeof(*rec->header))));
237 messaging_dispatch(msg, rec);
243 handle a socket event
245 static void messaging_handler(struct event_context *ev, struct fd_event *fde,
246 uint16_t flags, void *private)
248 struct messaging_context *msg = talloc_get_type(private,
249 struct messaging_context);
250 if (flags & EVENT_FD_WRITE) {
251 messaging_send_handler(msg);
253 if (flags & EVENT_FD_READ) {
254 messaging_recv_handler(msg);
260 Register a dispatch function for a particular message type.
262 void messaging_register(struct messaging_context *msg, void *private,
264 void (*fn)(struct messaging_context *, void *, uint32_t, uint32_t, DATA_BLOB *))
266 struct dispatch_fn *d;
268 d = talloc(msg, struct dispatch_fn);
269 d->msg_type = msg_type;
270 d->private = private;
272 DLIST_ADD(msg->dispatch, d);
276 De-register the function for a particular message type.
278 void messaging_deregister(struct messaging_context *msg, uint32_t msg_type, void *private)
280 struct dispatch_fn *d, *next;
282 for (d = msg->dispatch; d; d = next) {
284 if (d->msg_type == msg_type &&
285 d->private == private) {
286 DLIST_REMOVE(msg->dispatch, d);
294 Send a message to a particular server
296 NTSTATUS messaging_send(struct messaging_context *msg, uint32_t server,
297 uint32_t msg_type, DATA_BLOB *data)
299 struct messaging_rec *rec;
301 size_t dlength = data?data->length:0;
303 rec = talloc(msg, struct messaging_rec);
305 return NT_STATUS_NO_MEMORY;
308 rec->packet = data_blob_talloc(rec, NULL, sizeof(*rec->header) + dlength);
309 if (rec->packet.data == NULL) {
311 return NT_STATUS_NO_MEMORY;
315 rec->header = (struct messaging_header *)rec->packet.data;
316 rec->header->version = MESSAGING_VERSION;
317 rec->header->msg_type = msg_type;
318 rec->header->from = msg->server_id;
319 rec->header->to = server;
320 rec->header->length = dlength;
322 memcpy(rec->packet.data + sizeof(*rec->header),
323 data->data, dlength);
326 rec->path = messaging_path(msg, server);
327 talloc_steal(rec, rec->path);
329 if (msg->pending != NULL) {
330 status = STATUS_MORE_ENTRIES;
332 status = try_send(rec);
335 if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
336 if (msg->pending == NULL) {
337 EVENT_FD_WRITEABLE(msg->event.fde);
339 DLIST_ADD_END(msg->pending, rec, struct messaging_rec *);
349 Send a message to a particular server, with the message containing a single pointer
351 NTSTATUS messaging_send_ptr(struct messaging_context *msg, uint32_t server,
352 uint32_t msg_type, void *ptr)
356 blob.data = (void *)&ptr;
357 blob.length = sizeof(void *);
359 return messaging_send(msg, server, msg_type, &blob);
364 destroy the messaging context
366 static int messaging_destructor(void *ptr)
368 struct messaging_context *msg = ptr;
370 while (msg->names && msg->names[0]) {
371 irpc_remove_name(msg, msg->names[0]);
377 create the listening socket and setup the dispatcher
379 struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx, uint32_t server_id,
380 struct event_context *ev)
382 struct messaging_context *msg;
386 msg = talloc(mem_ctx, struct messaging_context);
392 ev = event_context_init(msg);
395 /* create the messaging directory if needed */
396 path = smbd_tmp_path(msg, "messaging");
400 msg->base_path = smbd_tmp_path(msg, "messaging");
401 msg->path = messaging_path(msg, server_id);
402 msg->server_id = server_id;
403 msg->dispatch = NULL;
405 msg->idr = idr_init(msg);
408 msg->start_time = timeval_current();
410 status = socket_create("unix", SOCKET_TYPE_DGRAM, &msg->sock, 0);
411 if (!NT_STATUS_IS_OK(status)) {
416 /* by stealing here we ensure that the socket is cleaned up (and even
418 talloc_steal(msg, msg->sock);
420 status = socket_listen(msg->sock, msg->path, 0, 50, 0);
421 if (!NT_STATUS_IS_OK(status)) {
422 DEBUG(0,("Unable to setup messaging listener for '%s':%s\n", msg->path, nt_errstr(status)));
427 /* it needs to be non blocking for sends */
428 set_blocking(socket_get_fd(msg->sock), False);
430 msg->event.ev = talloc_reference(msg, ev);
431 msg->event.fde = event_add_fd(ev, msg, socket_get_fd(msg->sock),
432 EVENT_FD_READ, messaging_handler, msg);
434 talloc_set_destructor(msg, messaging_destructor);
436 messaging_register(msg, NULL, MSG_PING, ping_message);
437 messaging_register(msg, NULL, MSG_IRPC, irpc_handler);
438 IRPC_REGISTER(msg, irpc, IRPC_UPTIME, irpc_uptime, msg);
445 a list of registered irpc server functions
448 struct irpc_list *next, *prev;
450 const struct dcerpc_interface_table *table;
458 register a irpc server function
460 NTSTATUS irpc_register(struct messaging_context *msg_ctx,
461 const struct dcerpc_interface_table *table,
462 int callnum, irpc_function_t fn, void *private)
464 struct irpc_list *irpc;
466 /* override an existing handler, if any */
467 for (irpc=msg_ctx->irpc; irpc; irpc=irpc->next) {
468 if (irpc->table == table && irpc->callnum == callnum) {
473 irpc = talloc(msg_ctx, struct irpc_list);
474 NT_STATUS_HAVE_NO_MEMORY(irpc);
475 DLIST_ADD(msg_ctx->irpc, irpc);
479 irpc->callnum = callnum;
481 irpc->private = private;
482 irpc->uuid = irpc->table->uuid;
489 handle an incoming irpc reply message
491 static void irpc_handler_reply(struct messaging_context *msg_ctx, struct irpc_message *m)
493 struct irpc_request *irpc;
495 irpc = idr_find(msg_ctx->idr, m->header.callid);
496 if (irpc == NULL) return;
498 /* parse the reply data */
499 irpc->status = irpc->table->calls[irpc->callnum].ndr_pull(m->ndr, NDR_OUT, irpc->r);
500 if (NT_STATUS_IS_OK(irpc->status)) {
501 irpc->status = m->header.status;
502 talloc_steal(irpc->mem_ctx, m);
504 talloc_steal(irpc, m);
507 if (irpc->async.fn) {
508 irpc->async.fn(irpc);
515 NTSTATUS irpc_send_reply(struct irpc_message *m, NTSTATUS status)
517 struct ndr_push *push;
520 m->header.status = status;
522 /* setup the reply */
523 push = ndr_push_init_ctx(m->ndr);
525 status = NT_STATUS_NO_MEMORY;
529 m->header.flags |= IRPC_FLAG_REPLY;
531 /* construct the packet */
532 status = ndr_push_irpc_header(push, NDR_SCALARS|NDR_BUFFERS, &m->header);
533 if (!NT_STATUS_IS_OK(status)) goto failed;
535 status = m->irpc->table->calls[m->irpc->callnum].ndr_push(push, NDR_OUT, m->data);
536 if (!NT_STATUS_IS_OK(status)) goto failed;
538 /* send the reply message */
539 packet = ndr_push_blob(push);
540 status = messaging_send(m->msg_ctx, m->from, MSG_IRPC, &packet);
541 if (!NT_STATUS_IS_OK(status)) goto failed;
549 handle an incoming irpc request message
551 static void irpc_handler_request(struct messaging_context *msg_ctx,
552 struct irpc_message *m)
558 for (i=msg_ctx->irpc; i; i=i->next) {
559 if (GUID_equal(&i->uuid, &m->header.uuid) &&
560 i->table->if_version == m->header.if_version &&
561 i->callnum == m->header.callnum) {
567 /* no registered handler for this message */
572 /* allocate space for the structure */
573 r = talloc_zero_size(m->ndr, i->table->calls[m->header.callnum].struct_size);
574 if (r == NULL) goto failed;
576 /* parse the request data */
577 status = i->table->calls[i->callnum].ndr_pull(m->ndr, NDR_IN, r);
578 if (!NT_STATUS_IS_OK(status)) goto failed;
581 m->private = i->private;
582 m->defer_reply = False;
583 m->msg_ctx = msg_ctx;
586 m->ev = msg_ctx->event.ev;
588 m->header.status = i->fn(m, r);
590 if (m->defer_reply) {
591 /* the server function has asked to defer the reply to later */
592 talloc_steal(msg_ctx, m);
596 irpc_send_reply(m, m->header.status);
604 handle an incoming irpc message
606 static void irpc_handler(struct messaging_context *msg_ctx, void *private,
607 uint32_t msg_type, uint32_t src, DATA_BLOB *packet)
609 struct irpc_message *m;
612 m = talloc(msg_ctx, struct irpc_message);
613 if (m == NULL) goto failed;
617 m->ndr = ndr_pull_init_blob(packet, m);
618 if (m->ndr == NULL) goto failed;
620 m->ndr->flags |= LIBNDR_FLAG_REF_ALLOC;
622 status = ndr_pull_irpc_header(m->ndr, NDR_BUFFERS|NDR_SCALARS, &m->header);
623 if (!NT_STATUS_IS_OK(status)) goto failed;
625 if (m->header.flags & IRPC_FLAG_REPLY) {
626 irpc_handler_reply(msg_ctx, m);
628 irpc_handler_request(msg_ctx, m);
638 destroy a irpc request
640 static int irpc_destructor(void *ptr)
642 struct irpc_request *irpc = talloc_get_type(ptr, struct irpc_request);
643 idr_remove(irpc->msg_ctx->idr, irpc->callid);
648 timeout a irpc request
650 static void irpc_timeout(struct event_context *ev, struct timed_event *te,
651 struct timeval t, void *private)
653 struct irpc_request *irpc = talloc_get_type(private, struct irpc_request);
654 irpc->status = NT_STATUS_IO_TIMEOUT;
656 if (irpc->async.fn) {
657 irpc->async.fn(irpc);
663 make a irpc call - async send
665 struct irpc_request *irpc_call_send(struct messaging_context *msg_ctx,
667 const struct dcerpc_interface_table *table,
668 int callnum, void *r, TALLOC_CTX *ctx)
670 struct irpc_header header;
671 struct ndr_push *ndr;
674 struct irpc_request *irpc;
676 irpc = talloc(msg_ctx, struct irpc_request);
677 if (irpc == NULL) goto failed;
679 irpc->msg_ctx = msg_ctx;
681 irpc->callnum = callnum;
682 irpc->callid = idr_get_new(msg_ctx->idr, irpc, UINT16_MAX);
683 if (irpc->callid == -1) goto failed;
686 irpc->async.fn = NULL;
689 talloc_set_destructor(irpc, irpc_destructor);
691 /* setup the header */
692 header.uuid = table->uuid;
694 header.if_version = table->if_version;
695 header.callid = irpc->callid;
696 header.callnum = callnum;
698 header.status = NT_STATUS_OK;
700 /* construct the irpc packet */
701 ndr = ndr_push_init_ctx(irpc);
702 if (ndr == NULL) goto failed;
704 status = ndr_push_irpc_header(ndr, NDR_SCALARS|NDR_BUFFERS, &header);
705 if (!NT_STATUS_IS_OK(status)) goto failed;
707 status = table->calls[callnum].ndr_push(ndr, NDR_IN, r);
708 if (!NT_STATUS_IS_OK(status)) goto failed;
711 packet = ndr_push_blob(ndr);
712 status = messaging_send(msg_ctx, server_id, MSG_IRPC, &packet);
713 if (!NT_STATUS_IS_OK(status)) goto failed;
715 event_add_timed(msg_ctx->event.ev, irpc,
716 timeval_current_ofs(IRPC_CALL_TIMEOUT, 0),
728 wait for a irpc reply
730 NTSTATUS irpc_call_recv(struct irpc_request *irpc)
734 NT_STATUS_HAVE_NO_MEMORY(irpc);
736 while (!irpc->done) {
737 if (event_loop_once(irpc->msg_ctx->event.ev) != 0) {
738 return NT_STATUS_CONNECTION_DISCONNECTED;
741 status = irpc->status;
747 perform a synchronous irpc request
749 NTSTATUS irpc_call(struct messaging_context *msg_ctx,
751 const struct dcerpc_interface_table *table,
752 int callnum, void *r,
755 struct irpc_request *irpc = irpc_call_send(msg_ctx, server_id,
756 table, callnum, r, mem_ctx);
757 return irpc_call_recv(irpc);
761 open the naming database
763 static struct tdb_wrap *irpc_namedb_open(struct messaging_context *msg_ctx)
766 char *path = talloc_asprintf(msg_ctx, "%s/names.tdb", msg_ctx->base_path);
770 t = tdb_wrap_open(msg_ctx, path, 0, 0, O_RDWR|O_CREAT, 0660);
777 add a string name that this irpc server can be called on
779 NTSTATUS irpc_add_name(struct messaging_context *msg_ctx, const char *name)
784 NTSTATUS status = NT_STATUS_OK;
786 t = irpc_namedb_open(msg_ctx);
787 NT_STATUS_HAVE_NO_MEMORY(t);
789 if (tdb_lock_bystring(t->tdb, name) != 0) {
791 return NT_STATUS_LOCK_NOT_GRANTED;
793 rec = tdb_fetch_bystring(t->tdb, name);
794 count = rec.dsize / sizeof(uint32_t);
795 rec.dptr = (unsigned char *)realloc_p(rec.dptr, uint32_t, count+1);
796 rec.dsize += sizeof(uint32_t);
797 if (rec.dptr == NULL) {
798 tdb_unlock_bystring(t->tdb, name);
800 return NT_STATUS_NO_MEMORY;
802 ((uint32_t *)rec.dptr)[count] = msg_ctx->server_id;
803 if (tdb_store_bystring(t->tdb, name, rec, 0) != 0) {
804 status = NT_STATUS_INTERNAL_ERROR;
807 tdb_unlock_bystring(t->tdb, name);
810 msg_ctx->names = str_list_add(msg_ctx->names, name);
811 talloc_steal(msg_ctx, msg_ctx->names);
817 return a list of server ids for a server name
819 uint32_t *irpc_servers_byname(struct messaging_context *msg_ctx, const char *name)
826 t = irpc_namedb_open(msg_ctx);
831 if (tdb_lock_bystring(t->tdb, name) != 0) {
835 rec = tdb_fetch_bystring(t->tdb, name);
836 if (rec.dptr == NULL) {
837 tdb_unlock_bystring(t->tdb, name);
841 count = rec.dsize / sizeof(uint32_t);
842 ret = talloc_array(msg_ctx, uint32_t, count+1);
844 tdb_unlock_bystring(t->tdb, name);
848 for (i=0;i<count;i++) {
849 ret[i] = ((uint32_t *)rec.dptr)[i];
853 tdb_unlock_bystring(t->tdb, name);
860 remove a name from a messaging context
862 void irpc_remove_name(struct messaging_context *msg_ctx, const char *name)
869 str_list_remove(msg_ctx->names, name);
871 t = irpc_namedb_open(msg_ctx);
876 if (tdb_lock_bystring(t->tdb, name) != 0) {
880 rec = tdb_fetch_bystring(t->tdb, name);
881 count = rec.dsize / sizeof(uint32_t);
883 tdb_unlock_bystring(t->tdb, name);
887 ids = (uint32_t *)rec.dptr;
888 for (i=0;i<count;i++) {
889 if (ids[i] == msg_ctx->server_id) {
891 memmove(ids+i, ids+i+1, count-(i+1));
893 rec.dsize -= sizeof(uint32_t);
897 tdb_store_bystring(t->tdb, name, rec, 0);
899 tdb_unlock_bystring(t->tdb, name);