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 3 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, see <http://www.gnu.org/licenses/>.
23 #include "lib/events/events.h"
24 #include "system/filesys.h"
25 #include "messaging/messaging.h"
26 #include "../lib/util/dlinklist.h"
27 #include "lib/socket/socket.h"
28 #include "librpc/gen_ndr/ndr_irpc.h"
29 #include "lib/messaging/irpc.h"
30 #include "lib/tdb_wrap/tdb_wrap.h"
31 #include "../lib/util/unix_privs.h"
32 #include "librpc/rpc/dcerpc.h"
33 #include "../lib/tdb_compat/tdb_compat.h"
34 #include "../lib/util/util_tdb.h"
35 #include "cluster/cluster.h"
36 #include "../lib/util/tevent_ntstatus.h"
37 #include "lib/param/param.h"
39 /* change the message version with any incompatible changes in the protocol */
40 #define IMESSAGING_VERSION 1
42 static struct tdb_wrap *irpc_namedb_open(TALLOC_CTX *mem_ctx, const char *base_path,
43 struct loadparm_context *lp_ctx);
49 struct imessaging_context *msg_ctx;
52 void (*handler)(struct irpc_request *irpc, struct irpc_message *m);
57 struct imessaging_context {
58 struct server_id server_id;
59 struct socket_context *sock;
60 const char *base_path;
62 struct dispatch_fn **dispatch;
64 struct idr_context *dispatch_tree;
65 struct imessaging_rec *pending;
66 struct imessaging_rec *retry_queue;
67 struct irpc_list *irpc;
68 struct idr_context *idr;
70 struct tdb_wrap *names_db;
71 struct timeval start_time;
72 struct tevent_timer *retry_te;
74 struct tevent_fd *fde;
78 /* we have a linked list of dispatch handlers for each msg_type that
79 this messaging server can deal with */
81 struct dispatch_fn *next, *prev;
87 /* an individual message */
88 struct imessaging_rec {
89 struct imessaging_rec *next, *prev;
90 struct imessaging_context *msg;
93 struct imessaging_header {
96 struct server_id from;
106 static void irpc_handler(struct imessaging_context *, void *,
107 uint32_t, struct server_id, DATA_BLOB *);
111 A useful function for testing the message system.
113 static void ping_message(struct imessaging_context *msg, void *private_data,
114 uint32_t msg_type, struct server_id src, DATA_BLOB *data)
116 char *task_id = server_id_str(NULL, &src);
117 DEBUG(1,("INFO: Received PING message from server %s [%.*s]\n",
118 task_id, (int)data->length,
119 data->data?(const char *)data->data:""));
120 talloc_free(task_id);
121 imessaging_send(msg, src, MSG_PONG, data);
125 return uptime of messaging server via irpc
127 static NTSTATUS irpc_uptime(struct irpc_message *msg,
128 struct irpc_uptime *r)
130 struct imessaging_context *ctx = talloc_get_type(msg->private_data, struct imessaging_context);
131 *r->out.start_time = timeval_to_nttime(&ctx->start_time);
136 return the path to a messaging socket
138 static char *imessaging_path(struct imessaging_context *msg, struct server_id server_id)
140 TALLOC_CTX *tmp_ctx = talloc_new(msg);
141 const char *id = server_id_str(tmp_ctx, &server_id);
146 s = talloc_asprintf(msg, "%s/msg.%s", msg->base_path, id);
147 talloc_steal(s, tmp_ctx);
152 dispatch a fully received message
154 note that this deliberately can match more than one message handler
155 per message. That allows a single messasging context to register
156 (for example) a debug handler for more than one piece of code
158 static void imessaging_dispatch(struct imessaging_context *msg, struct imessaging_rec *rec)
160 struct dispatch_fn *d, *next;
162 /* temporary IDs use an idtree, the rest use a array of pointers */
163 if (rec->header->msg_type >= MSG_TMP_BASE) {
164 d = (struct dispatch_fn *)idr_find(msg->dispatch_tree,
165 rec->header->msg_type);
166 } else if (rec->header->msg_type < msg->num_types) {
167 d = msg->dispatch[rec->header->msg_type];
172 for (; d; d = next) {
175 data.data = rec->packet.data + sizeof(*rec->header);
176 data.length = rec->header->length;
177 d->fn(msg, d->private_data, d->msg_type, rec->header->from, &data);
179 rec->header->length = 0;
183 handler for messages that arrive from other nodes in the cluster
185 static void cluster_message_handler(struct imessaging_context *msg, DATA_BLOB packet)
187 struct imessaging_rec *rec;
189 rec = talloc(msg, struct imessaging_rec);
191 smb_panic("Unable to allocate imessaging_rec");
195 rec->path = msg->path;
196 rec->header = (struct imessaging_header *)packet.data;
197 rec->packet = packet;
200 if (packet.length != sizeof(*rec->header) + rec->header->length) {
201 DEBUG(0,("messaging: bad message header size %d should be %d\n",
202 rec->header->length, (int)(packet.length - sizeof(*rec->header))));
207 imessaging_dispatch(msg, rec);
214 try to send the message
216 static NTSTATUS try_send(struct imessaging_rec *rec)
218 struct imessaging_context *msg = rec->msg;
222 struct socket_address *path;
224 /* rec->path is the path of the *other* socket, where we want
226 path = socket_address_from_strings(msg, msg->sock->backend_name,
229 return NT_STATUS_NO_MEMORY;
232 /* we send with privileges so messages work from any context */
233 priv = root_privileges();
234 status = socket_sendto(msg->sock, &rec->packet, &nsent, path);
242 retry backed off messages
244 static void msg_retry_timer(struct tevent_context *ev, struct tevent_timer *te,
245 struct timeval t, void *private_data)
247 struct imessaging_context *msg = talloc_get_type(private_data,
248 struct imessaging_context);
249 msg->retry_te = NULL;
251 /* put the messages back on the main queue */
252 while (msg->retry_queue) {
253 struct imessaging_rec *rec = msg->retry_queue;
254 DLIST_REMOVE(msg->retry_queue, rec);
255 DLIST_ADD_END(msg->pending, rec, struct imessaging_rec *);
258 TEVENT_FD_WRITEABLE(msg->event.fde);
262 handle a socket write event
264 static void imessaging_send_handler(struct imessaging_context *msg, struct tevent_context *ev)
266 while (msg->pending) {
267 struct imessaging_rec *rec = msg->pending;
269 status = try_send(rec);
270 if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
272 if (rec->retries > 3) {
273 /* we're getting continuous write errors -
274 backoff this record */
275 DLIST_REMOVE(msg->pending, rec);
276 DLIST_ADD_END(msg->retry_queue, rec,
277 struct imessaging_rec *);
278 if (msg->retry_te == NULL) {
280 tevent_add_timer(ev, msg,
281 timeval_current_ofs(1, 0),
282 msg_retry_timer, msg);
288 if (!NT_STATUS_IS_OK(status)) {
289 TALLOC_CTX *tmp_ctx = talloc_new(msg);
290 DEBUG(1,("messaging: Lost message from %s to %s of type %u - %s\n",
291 server_id_str(tmp_ctx, &rec->header->from),
292 server_id_str(tmp_ctx, &rec->header->to),
293 rec->header->msg_type,
295 talloc_free(tmp_ctx);
297 DLIST_REMOVE(msg->pending, rec);
300 if (msg->pending == NULL) {
301 TEVENT_FD_NOT_WRITEABLE(msg->event.fde);
306 handle a new incoming packet
308 static void imessaging_recv_handler(struct imessaging_context *msg, struct tevent_context *ev)
310 struct imessaging_rec *rec;
315 /* see how many bytes are in the next packet */
316 status = socket_pending(msg->sock, &msize);
317 if (!NT_STATUS_IS_OK(status)) {
318 DEBUG(0,("socket_pending failed in messaging - %s\n",
323 packet = data_blob_talloc(msg, NULL, msize);
324 if (packet.data == NULL) {
325 /* assume this is temporary and retry */
329 status = socket_recv(msg->sock, packet.data, msize, &msize);
330 if (!NT_STATUS_IS_OK(status)) {
331 data_blob_free(&packet);
335 if (msize < sizeof(*rec->header)) {
336 DEBUG(0,("messaging: bad message of size %d\n", (int)msize));
337 data_blob_free(&packet);
341 rec = talloc(msg, struct imessaging_rec);
343 smb_panic("Unable to allocate imessaging_rec");
346 talloc_steal(rec, packet.data);
348 rec->path = msg->path;
349 rec->header = (struct imessaging_header *)packet.data;
350 rec->packet = packet;
353 if (msize != sizeof(*rec->header) + rec->header->length) {
354 DEBUG(0,("messaging: bad message header size %d should be %d\n",
355 rec->header->length, (int)(msize - sizeof(*rec->header))));
360 imessaging_dispatch(msg, rec);
366 handle a socket event
368 static void imessaging_handler(struct tevent_context *ev, struct tevent_fd *fde,
369 uint16_t flags, void *private_data)
371 struct imessaging_context *msg = talloc_get_type(private_data,
372 struct imessaging_context);
373 if (flags & TEVENT_FD_WRITE) {
374 imessaging_send_handler(msg, ev);
376 if (flags & TEVENT_FD_READ) {
377 imessaging_recv_handler(msg, ev);
383 Register a dispatch function for a particular message type.
385 NTSTATUS imessaging_register(struct imessaging_context *msg, void *private_data,
386 uint32_t msg_type, msg_callback_t fn)
388 struct dispatch_fn *d;
390 /* possibly expand dispatch array */
391 if (msg_type >= msg->num_types) {
392 struct dispatch_fn **dp;
394 dp = talloc_realloc(msg, msg->dispatch, struct dispatch_fn *, msg_type+1);
395 NT_STATUS_HAVE_NO_MEMORY(dp);
397 for (i=msg->num_types;i<=msg_type;i++) {
398 msg->dispatch[i] = NULL;
400 msg->num_types = msg_type+1;
403 d = talloc_zero(msg->dispatch, struct dispatch_fn);
404 NT_STATUS_HAVE_NO_MEMORY(d);
405 d->msg_type = msg_type;
406 d->private_data = private_data;
409 DLIST_ADD(msg->dispatch[msg_type], d);
415 register a temporary message handler. The msg_type is allocated
418 NTSTATUS imessaging_register_tmp(struct imessaging_context *msg, void *private_data,
419 msg_callback_t fn, uint32_t *msg_type)
421 struct dispatch_fn *d;
424 d = talloc_zero(msg->dispatch, struct dispatch_fn);
425 NT_STATUS_HAVE_NO_MEMORY(d);
426 d->private_data = private_data;
429 id = idr_get_new_above(msg->dispatch_tree, d, MSG_TMP_BASE, UINT16_MAX);
432 return NT_STATUS_TOO_MANY_CONTEXT_IDS;
435 d->msg_type = (uint32_t)id;
436 (*msg_type) = d->msg_type;
442 De-register the function for a particular message type.
444 void imessaging_deregister(struct imessaging_context *msg, uint32_t msg_type, void *private_data)
446 struct dispatch_fn *d, *next;
448 if (msg_type >= msg->num_types) {
449 d = (struct dispatch_fn *)idr_find(msg->dispatch_tree,
452 idr_remove(msg->dispatch_tree, msg_type);
457 for (d = msg->dispatch[msg_type]; d; d = next) {
459 if (d->private_data == private_data) {
460 DLIST_REMOVE(msg->dispatch[msg_type], d);
467 Send a message to a particular server
469 NTSTATUS imessaging_send(struct imessaging_context *msg, struct server_id server,
470 uint32_t msg_type, const DATA_BLOB *data)
472 struct imessaging_rec *rec;
474 size_t dlength = data?data->length:0;
476 rec = talloc(msg, struct imessaging_rec);
478 return NT_STATUS_NO_MEMORY;
481 rec->packet = data_blob_talloc(rec, NULL, sizeof(*rec->header) + dlength);
482 if (rec->packet.data == NULL) {
484 return NT_STATUS_NO_MEMORY;
489 rec->header = (struct imessaging_header *)rec->packet.data;
491 ZERO_STRUCTP(rec->header);
492 rec->header->version = IMESSAGING_VERSION;
493 rec->header->msg_type = msg_type;
494 rec->header->from = msg->server_id;
495 rec->header->to = server;
496 rec->header->length = dlength;
498 memcpy(rec->packet.data + sizeof(*rec->header),
499 data->data, dlength);
502 if (!cluster_node_equal(&msg->server_id, &server)) {
503 /* the destination is on another node - dispatch via
505 status = cluster_message_send(server, &rec->packet);
510 rec->path = imessaging_path(msg, server);
511 talloc_steal(rec, rec->path);
513 if (msg->pending != NULL) {
514 status = STATUS_MORE_ENTRIES;
516 status = try_send(rec);
519 if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
520 if (msg->pending == NULL) {
521 TEVENT_FD_WRITEABLE(msg->event.fde);
523 DLIST_ADD_END(msg->pending, rec, struct imessaging_rec *);
533 Send a message to a particular server, with the message containing a single pointer
535 NTSTATUS imessaging_send_ptr(struct imessaging_context *msg, struct server_id server,
536 uint32_t msg_type, void *ptr)
540 blob.data = (uint8_t *)&ptr;
541 blob.length = sizeof(void *);
543 return imessaging_send(msg, server, msg_type, &blob);
548 remove our messaging socket and database entry
550 int imessaging_cleanup(struct imessaging_context *msg)
556 DEBUG(5,("imessaging: cleaning up %s\n", msg->path));
558 while (msg->names && msg->names[0]) {
559 irpc_remove_name(msg, msg->names[0]);
565 create the listening socket and setup the dispatcher
567 use auto_remove=true when you want a destructor to remove the
568 associated messaging socket and database entry on talloc free. Don't
569 use this in processes that may fork and a child may talloc free this
572 struct imessaging_context *imessaging_init(TALLOC_CTX *mem_ctx,
573 struct loadparm_context *lp_ctx,
574 struct server_id server_id,
575 struct tevent_context *ev,
578 struct imessaging_context *msg;
580 struct socket_address *path;
587 msg = talloc_zero(mem_ctx, struct imessaging_context);
592 /* setup a handler for messages from other cluster nodes, if appropriate */
593 status = cluster_message_init(msg, server_id, cluster_message_handler);
594 if (!NT_STATUS_IS_OK(status)) {
598 /* create the messaging directory if needed */
600 msg->base_path = lpcfg_imessaging_path(msg, lp_ctx);
601 if (msg->base_path == NULL) {
605 ok = directory_create_or_exist_strict(msg->base_path, geteuid(), 0700);
610 msg->path = imessaging_path(msg, server_id);
611 if (msg->path == NULL) {
615 msg->server_id = server_id;
616 msg->idr = idr_init(msg);
617 if (msg->idr == NULL) {
621 msg->dispatch_tree = idr_init(msg);
622 if (msg->dispatch_tree == NULL) {
626 msg->start_time = timeval_current();
628 msg->names_db = irpc_namedb_open(msg, msg->base_path, lp_ctx);
629 if (msg->names_db == NULL) {
633 status = socket_create("unix", SOCKET_TYPE_DGRAM, &msg->sock, 0);
634 if (!NT_STATUS_IS_OK(status)) {
638 /* by stealing here we ensure that the socket is cleaned up (and even
640 talloc_steal(msg, msg->sock);
642 path = socket_address_from_strings(msg, msg->sock->backend_name,
648 status = socket_listen(msg->sock, path, 50, 0);
649 if (!NT_STATUS_IS_OK(status)) {
650 DEBUG(0,("Unable to setup messaging listener for '%s':%s\n", msg->path, nt_errstr(status)));
654 /* it needs to be non blocking for sends */
655 set_blocking(socket_get_fd(msg->sock), false);
657 msg->event.fde = tevent_add_fd(ev, msg, socket_get_fd(msg->sock),
658 TEVENT_FD_READ, imessaging_handler, msg);
659 tevent_fd_set_auto_close(msg->event.fde);
662 talloc_set_destructor(msg, imessaging_cleanup);
665 imessaging_register(msg, NULL, MSG_PING, ping_message);
666 imessaging_register(msg, NULL, MSG_IRPC, irpc_handler);
667 IRPC_REGISTER(msg, irpc, IRPC_UPTIME, irpc_uptime, msg);
676 A hack, for the short term until we get 'client only' messaging in place
678 struct imessaging_context *imessaging_client_init(TALLOC_CTX *mem_ctx,
679 struct loadparm_context *lp_ctx,
680 struct tevent_context *ev)
685 id.task_id = generate_random();
686 id.vnn = NONCLUSTER_VNN;
688 /* This is because we are not in the s3 serverid database */
689 id.unique_id = SERVERID_UNIQUE_ID_NOT_TO_VERIFY;
691 return imessaging_init(mem_ctx, lp_ctx, id, ev, true);
694 a list of registered irpc server functions
697 struct irpc_list *next, *prev;
699 const struct ndr_interface_table *table;
707 register a irpc server function
709 NTSTATUS irpc_register(struct imessaging_context *msg_ctx,
710 const struct ndr_interface_table *table,
711 int callnum, irpc_function_t fn, void *private_data)
713 struct irpc_list *irpc;
715 /* override an existing handler, if any */
716 for (irpc=msg_ctx->irpc; irpc; irpc=irpc->next) {
717 if (irpc->table == table && irpc->callnum == callnum) {
722 irpc = talloc(msg_ctx, struct irpc_list);
723 NT_STATUS_HAVE_NO_MEMORY(irpc);
724 DLIST_ADD(msg_ctx->irpc, irpc);
728 irpc->callnum = callnum;
730 irpc->private_data = private_data;
731 irpc->uuid = irpc->table->syntax_id.uuid;
738 handle an incoming irpc reply message
740 static void irpc_handler_reply(struct imessaging_context *msg_ctx, struct irpc_message *m)
742 struct irpc_request *irpc;
744 irpc = (struct irpc_request *)idr_find(msg_ctx->idr, m->header.callid);
745 if (irpc == NULL) return;
747 irpc->incoming.handler(irpc, m);
753 NTSTATUS irpc_send_reply(struct irpc_message *m, NTSTATUS status)
755 struct ndr_push *push;
757 enum ndr_err_code ndr_err;
759 m->header.status = status;
761 /* setup the reply */
762 push = ndr_push_init_ctx(m->ndr);
764 status = NT_STATUS_NO_MEMORY;
768 m->header.flags |= IRPC_FLAG_REPLY;
769 m->header.creds.token= NULL;
771 /* construct the packet */
772 ndr_err = ndr_push_irpc_header(push, NDR_SCALARS|NDR_BUFFERS, &m->header);
773 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
774 status = ndr_map_error2ntstatus(ndr_err);
778 ndr_err = m->irpc->table->calls[m->irpc->callnum].ndr_push(push, NDR_OUT, m->data);
779 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
780 status = ndr_map_error2ntstatus(ndr_err);
784 /* send the reply message */
785 packet = ndr_push_blob(push);
786 status = imessaging_send(m->msg_ctx, m->from, MSG_IRPC, &packet);
787 if (!NT_STATUS_IS_OK(status)) goto failed;
795 handle an incoming irpc request message
797 static void irpc_handler_request(struct imessaging_context *msg_ctx,
798 struct irpc_message *m)
802 enum ndr_err_code ndr_err;
804 for (i=msg_ctx->irpc; i; i=i->next) {
805 if (GUID_equal(&i->uuid, &m->header.uuid) &&
806 i->table->syntax_id.if_version == m->header.if_version &&
807 i->callnum == m->header.callnum) {
813 /* no registered handler for this message */
818 /* allocate space for the structure */
819 r = talloc_zero_size(m->ndr, i->table->calls[m->header.callnum].struct_size);
820 if (r == NULL) goto failed;
822 m->ndr->flags |= LIBNDR_FLAG_REF_ALLOC;
824 /* parse the request data */
825 ndr_err = i->table->calls[i->callnum].ndr_pull(m->ndr, NDR_IN, r);
826 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) goto failed;
829 m->private_data= i->private_data;
830 m->defer_reply = false;
832 m->msg_ctx = msg_ctx;
836 m->header.status = i->fn(m, r);
839 /* the server function won't ever be replying to this request */
844 if (m->defer_reply) {
845 /* the server function has asked to defer the reply to later */
846 talloc_steal(msg_ctx, m);
850 irpc_send_reply(m, m->header.status);
858 handle an incoming irpc message
860 static void irpc_handler(struct imessaging_context *msg_ctx, void *private_data,
861 uint32_t msg_type, struct server_id src, DATA_BLOB *packet)
863 struct irpc_message *m;
864 enum ndr_err_code ndr_err;
866 m = talloc(msg_ctx, struct irpc_message);
867 if (m == NULL) goto failed;
871 m->ndr = ndr_pull_init_blob(packet, m);
872 if (m->ndr == NULL) goto failed;
874 m->ndr->flags |= LIBNDR_FLAG_REF_ALLOC;
876 ndr_err = ndr_pull_irpc_header(m->ndr, NDR_BUFFERS|NDR_SCALARS, &m->header);
877 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) goto failed;
879 if (m->header.flags & IRPC_FLAG_REPLY) {
880 irpc_handler_reply(msg_ctx, m);
882 irpc_handler_request(msg_ctx, m);
892 destroy a irpc request
894 static int irpc_destructor(struct irpc_request *irpc)
896 if (irpc->callid != -1) {
897 idr_remove(irpc->msg_ctx->idr, irpc->callid);
905 open the naming database
907 static struct tdb_wrap *irpc_namedb_open(TALLOC_CTX *mem_ctx, const char *base_path,
908 struct loadparm_context *lp_ctx)
911 char *path = talloc_asprintf(mem_ctx, "%s/names.tdb", base_path);
915 t = tdb_wrap_open(mem_ctx, path, lpcfg_tdb_hash_size(lp_ctx, path),
916 lpcfg_tdb_flags(lp_ctx, 0), O_RDWR|O_CREAT, 0660);
923 add a string name that this irpc server can be called on
925 NTSTATUS irpc_add_name(struct imessaging_context *msg_ctx, const char *name)
927 struct tdb_context *t = msg_ctx->names_db->tdb;
928 struct server_id pid = msg_ctx->server_id;
932 msg_ctx->names = str_list_add(msg_ctx->names, name);
933 if (msg_ctx->names == NULL) {
934 return NT_STATUS_NO_MEMORY;
936 talloc_steal(msg_ctx, msg_ctx->names);
938 key = string_term_tdb_data(name);
939 data = (TDB_DATA) { .dptr = (uint8_t *)&pid, .dsize = sizeof(pid) };
941 ret = tdb_append(t, key, data);
943 enum TDB_ERROR err = tdb_error(t);
944 str_list_remove(msg_ctx->names, name);
945 return map_nt_error_from_tdb(err);
952 return a list of server ids for a server name
954 NTSTATUS irpc_servers_byname(struct imessaging_context *msg_ctx,
955 TALLOC_CTX *mem_ctx, const char *name,
956 unsigned *num_servers,
957 struct server_id **servers)
959 struct tdb_wrap *t = msg_ctx->names_db;
962 struct server_id *ret;
964 rec = tdb_fetch_bystring(t->tdb, name);
965 if (rec.dptr == NULL) {
966 enum TDB_ERROR err = tdb_error(t->tdb);
967 return map_nt_error_from_tdb(err);
970 count = rec.dsize / sizeof(struct server_id);
973 * In a corrupted db we could end up with a record of size
974 * less than a struct server_id. Don't leak in this case.
977 return NT_STATUS_NOT_FOUND;
980 ret = talloc_array(mem_ctx, struct server_id, count);
983 return NT_STATUS_NO_MEMORY;
985 memcpy(ret, rec.dptr, count * sizeof(struct server_id));
988 *num_servers = count;
993 static int all_servers_func(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *state)
995 struct irpc_name_records *name_records = talloc_get_type(state, struct irpc_name_records);
996 struct irpc_name_record *name_record;
1000 = talloc_realloc(name_records, name_records->names,
1001 struct irpc_name_record *, name_records->num_records+1);
1002 if (!name_records->names) {
1006 name_records->names[name_records->num_records] = name_record
1007 = talloc(name_records->names,
1008 struct irpc_name_record);
1013 name_records->num_records++;
1016 = talloc_strndup(name_record,
1017 (const char *)key.dptr, key.dsize);
1018 if (!name_record->name) {
1022 name_record->count = data.dsize / sizeof(struct server_id);
1023 name_record->ids = talloc_array(name_record,
1025 name_record->count);
1026 if (name_record->ids == NULL) {
1029 for (i=0;i<name_record->count;i++) {
1030 name_record->ids[i] = ((struct server_id *)data.dptr)[i];
1036 return a list of server ids for a server name
1038 struct irpc_name_records *irpc_all_servers(struct imessaging_context *msg_ctx,
1039 TALLOC_CTX *mem_ctx)
1041 struct tdb_wrap *t = msg_ctx->names_db;
1043 struct irpc_name_records *name_records = talloc_zero(mem_ctx, struct irpc_name_records);
1044 if (name_records == NULL) {
1048 ret = tdb_traverse_read(t->tdb, all_servers_func, name_records);
1053 return name_records;
1057 remove a name from a messaging context
1059 void irpc_remove_name(struct imessaging_context *msg_ctx, const char *name)
1061 struct tdb_wrap *t = msg_ctx->names_db;
1064 struct server_id *ids;
1066 str_list_remove(msg_ctx->names, name);
1068 if (tdb_lock_bystring(t->tdb, name) != 0) {
1071 rec = tdb_fetch_bystring(t->tdb, name);
1072 if (rec.dptr == NULL) {
1073 tdb_unlock_bystring(t->tdb, name);
1076 count = rec.dsize / sizeof(struct server_id);
1079 tdb_unlock_bystring(t->tdb, name);
1082 ids = (struct server_id *)rec.dptr;
1083 for (i=0;i<count;i++) {
1084 if (cluster_id_equal(&ids[i], &msg_ctx->server_id)) {
1086 memmove(ids+i, ids+i+1,
1087 sizeof(struct server_id) * (count-(i+1)));
1089 rec.dsize -= sizeof(struct server_id);
1093 tdb_store_bystring(t->tdb, name, rec, 0);
1095 tdb_unlock_bystring(t->tdb, name);
1098 struct server_id imessaging_get_server_id(struct imessaging_context *msg_ctx)
1100 return msg_ctx->server_id;
1103 struct irpc_bh_state {
1104 struct imessaging_context *msg_ctx;
1105 struct server_id server_id;
1106 const struct ndr_interface_table *table;
1108 struct security_token *token;
1111 static bool irpc_bh_is_connected(struct dcerpc_binding_handle *h)
1113 struct irpc_bh_state *hs = dcerpc_binding_handle_data(h,
1114 struct irpc_bh_state);
1123 static uint32_t irpc_bh_set_timeout(struct dcerpc_binding_handle *h,
1126 struct irpc_bh_state *hs = dcerpc_binding_handle_data(h,
1127 struct irpc_bh_state);
1128 uint32_t old = hs->timeout;
1130 hs->timeout = timeout;
1135 struct irpc_bh_raw_call_state {
1136 struct irpc_request *irpc;
1139 DATA_BLOB in_packet;
1143 static void irpc_bh_raw_call_incoming_handler(struct irpc_request *irpc,
1144 struct irpc_message *m);
1146 static struct tevent_req *irpc_bh_raw_call_send(TALLOC_CTX *mem_ctx,
1147 struct tevent_context *ev,
1148 struct dcerpc_binding_handle *h,
1149 const struct GUID *object,
1152 const uint8_t *in_data,
1155 struct irpc_bh_state *hs =
1156 dcerpc_binding_handle_data(h,
1157 struct irpc_bh_state);
1158 struct tevent_req *req;
1159 struct irpc_bh_raw_call_state *state;
1161 struct irpc_header header;
1162 struct ndr_push *ndr;
1164 enum ndr_err_code ndr_err;
1166 req = tevent_req_create(mem_ctx, &state,
1167 struct irpc_bh_raw_call_state);
1171 state->opnum = opnum;
1172 state->in_data.data = discard_const_p(uint8_t, in_data);
1173 state->in_data.length = in_length;
1175 ok = irpc_bh_is_connected(h);
1177 tevent_req_nterror(req, NT_STATUS_CONNECTION_DISCONNECTED);
1178 return tevent_req_post(req, ev);
1181 state->irpc = talloc_zero(state, struct irpc_request);
1182 if (tevent_req_nomem(state->irpc, req)) {
1183 return tevent_req_post(req, ev);
1186 state->irpc->msg_ctx = hs->msg_ctx;
1187 state->irpc->callid = idr_get_new(hs->msg_ctx->idr,
1188 state->irpc, UINT16_MAX);
1189 if (state->irpc->callid == -1) {
1190 tevent_req_nterror(req, NT_STATUS_INSUFFICIENT_RESOURCES);
1191 return tevent_req_post(req, ev);
1193 state->irpc->incoming.handler = irpc_bh_raw_call_incoming_handler;
1194 state->irpc->incoming.private_data = req;
1196 talloc_set_destructor(state->irpc, irpc_destructor);
1198 /* setup the header */
1199 header.uuid = hs->table->syntax_id.uuid;
1201 header.if_version = hs->table->syntax_id.if_version;
1202 header.callid = state->irpc->callid;
1203 header.callnum = state->opnum;
1205 header.status = NT_STATUS_OK;
1206 header.creds.token= hs->token;
1208 /* construct the irpc packet */
1209 ndr = ndr_push_init_ctx(state->irpc);
1210 if (tevent_req_nomem(ndr, req)) {
1211 return tevent_req_post(req, ev);
1214 ndr_err = ndr_push_irpc_header(ndr, NDR_SCALARS|NDR_BUFFERS, &header);
1215 status = ndr_map_error2ntstatus(ndr_err);
1216 if (!NT_STATUS_IS_OK(status)) {
1217 tevent_req_nterror(req, status);
1218 return tevent_req_post(req, ev);
1221 ndr_err = ndr_push_bytes(ndr, in_data, in_length);
1222 status = ndr_map_error2ntstatus(ndr_err);
1223 if (!NT_STATUS_IS_OK(status)) {
1224 tevent_req_nterror(req, status);
1225 return tevent_req_post(req, ev);
1229 state->in_packet = ndr_push_blob(ndr);
1230 status = imessaging_send(hs->msg_ctx, hs->server_id,
1231 MSG_IRPC, &state->in_packet);
1232 if (!NT_STATUS_IS_OK(status)) {
1233 tevent_req_nterror(req, status);
1234 return tevent_req_post(req, ev);
1237 if (hs->timeout != IRPC_CALL_TIMEOUT_INF) {
1238 /* set timeout-callback in case caller wants that */
1239 ok = tevent_req_set_endtime(req, ev, timeval_current_ofs(hs->timeout, 0));
1241 return tevent_req_post(req, ev);
1248 static void irpc_bh_raw_call_incoming_handler(struct irpc_request *irpc,
1249 struct irpc_message *m)
1251 struct tevent_req *req =
1252 talloc_get_type_abort(irpc->incoming.private_data,
1254 struct irpc_bh_raw_call_state *state =
1255 tevent_req_data(req,
1256 struct irpc_bh_raw_call_state);
1258 talloc_steal(state, m);
1260 if (!NT_STATUS_IS_OK(m->header.status)) {
1261 tevent_req_nterror(req, m->header.status);
1265 state->out_data = data_blob_talloc(state,
1266 m->ndr->data + m->ndr->offset,
1267 m->ndr->data_size - m->ndr->offset);
1268 if ((m->ndr->data_size - m->ndr->offset) > 0 && !state->out_data.data) {
1269 tevent_req_oom(req);
1273 tevent_req_done(req);
1276 static NTSTATUS irpc_bh_raw_call_recv(struct tevent_req *req,
1277 TALLOC_CTX *mem_ctx,
1280 uint32_t *out_flags)
1282 struct irpc_bh_raw_call_state *state =
1283 tevent_req_data(req,
1284 struct irpc_bh_raw_call_state);
1287 if (tevent_req_is_nterror(req, &status)) {
1288 tevent_req_received(req);
1292 *out_data = talloc_move(mem_ctx, &state->out_data.data);
1293 *out_length = state->out_data.length;
1295 tevent_req_received(req);
1296 return NT_STATUS_OK;
1299 struct irpc_bh_disconnect_state {
1303 static struct tevent_req *irpc_bh_disconnect_send(TALLOC_CTX *mem_ctx,
1304 struct tevent_context *ev,
1305 struct dcerpc_binding_handle *h)
1307 struct irpc_bh_state *hs = dcerpc_binding_handle_data(h,
1308 struct irpc_bh_state);
1309 struct tevent_req *req;
1310 struct irpc_bh_disconnect_state *state;
1313 req = tevent_req_create(mem_ctx, &state,
1314 struct irpc_bh_disconnect_state);
1319 ok = irpc_bh_is_connected(h);
1321 tevent_req_nterror(req, NT_STATUS_CONNECTION_DISCONNECTED);
1322 return tevent_req_post(req, ev);
1327 tevent_req_done(req);
1328 return tevent_req_post(req, ev);
1331 static NTSTATUS irpc_bh_disconnect_recv(struct tevent_req *req)
1335 if (tevent_req_is_nterror(req, &status)) {
1336 tevent_req_received(req);
1340 tevent_req_received(req);
1341 return NT_STATUS_OK;
1344 static bool irpc_bh_ref_alloc(struct dcerpc_binding_handle *h)
1349 static const struct dcerpc_binding_handle_ops irpc_bh_ops = {
1351 .is_connected = irpc_bh_is_connected,
1352 .set_timeout = irpc_bh_set_timeout,
1353 .raw_call_send = irpc_bh_raw_call_send,
1354 .raw_call_recv = irpc_bh_raw_call_recv,
1355 .disconnect_send = irpc_bh_disconnect_send,
1356 .disconnect_recv = irpc_bh_disconnect_recv,
1358 .ref_alloc = irpc_bh_ref_alloc,
1361 /* initialise a irpc binding handle */
1362 struct dcerpc_binding_handle *irpc_binding_handle(TALLOC_CTX *mem_ctx,
1363 struct imessaging_context *msg_ctx,
1364 struct server_id server_id,
1365 const struct ndr_interface_table *table)
1367 struct dcerpc_binding_handle *h;
1368 struct irpc_bh_state *hs;
1370 h = dcerpc_binding_handle_create(mem_ctx,
1375 struct irpc_bh_state,
1380 hs->msg_ctx = msg_ctx;
1381 hs->server_id = server_id;
1383 hs->timeout = IRPC_CALL_TIMEOUT;
1388 struct dcerpc_binding_handle *irpc_binding_handle_by_name(TALLOC_CTX *mem_ctx,
1389 struct imessaging_context *msg_ctx,
1390 const char *dest_task,
1391 const struct ndr_interface_table *table)
1393 struct dcerpc_binding_handle *h;
1395 struct server_id *sids;
1396 struct server_id sid;
1399 /* find the server task */
1401 status = irpc_servers_byname(msg_ctx, mem_ctx, dest_task,
1403 if (!NT_STATUS_IS_OK(status)) {
1404 errno = EADDRNOTAVAIL;
1410 h = irpc_binding_handle(mem_ctx, msg_ctx,
1419 void irpc_binding_handle_add_security_token(struct dcerpc_binding_handle *h,
1420 struct security_token *token)
1422 struct irpc_bh_state *hs =
1423 dcerpc_binding_handle_data(h,
1424 struct irpc_bh_state);