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 "lib/util/server_id.h"
25 #include "system/filesys.h"
26 #include "messaging/messaging.h"
27 #include "messaging/messaging_internal.h"
28 #include "../lib/util/dlinklist.h"
29 #include "lib/socket/socket.h"
30 #include "librpc/gen_ndr/ndr_irpc.h"
31 #include "lib/messaging/irpc.h"
32 #include "../lib/util/unix_privs.h"
33 #include "librpc/rpc/dcerpc.h"
34 #include "cluster/cluster.h"
35 #include "../lib/util/tevent_ntstatus.h"
36 #include "lib/param/param.h"
37 #include "lib/util/server_id_db.h"
38 #include "lib/util/talloc_report.h"
39 #include "../source3/lib/messages_dgm.h"
40 #include "../source3/lib/messages_dgm_ref.h"
41 #include "../source3/lib/messages_util.h"
44 /* change the message version with any incompatible changes in the protocol */
45 #define IMESSAGING_VERSION 1
51 struct imessaging_context *msg_ctx;
54 void (*handler)(struct irpc_request *irpc, struct irpc_message *m);
59 /* we have a linked list of dispatch handlers for each msg_type that
60 this messaging server can deal with */
62 struct dispatch_fn *next, *prev;
68 /* an individual message */
70 static void irpc_handler(struct imessaging_context *, void *,
71 uint32_t, struct server_id, DATA_BLOB *);
75 A useful function for testing the message system.
77 static void ping_message(struct imessaging_context *msg, void *private_data,
78 uint32_t msg_type, struct server_id src, DATA_BLOB *data)
80 struct server_id_buf idbuf;
81 DEBUG(1,("INFO: Received PING message from server %s [%.*s]\n",
82 server_id_str_buf(src, &idbuf), (int)data->length,
83 data->data?(const char *)data->data:""));
84 imessaging_send(msg, src, MSG_PONG, data);
87 static void pool_message(struct imessaging_context *msg, void *private_data,
88 uint32_t msg_type, struct server_id src,
93 report = talloc_report_str(msg, NULL);
96 DATA_BLOB blob = { .data = (uint8_t *)report,
97 .length = talloc_get_size(report) - 1};
98 imessaging_send(msg, src, MSG_POOL_USAGE, &blob);
103 static void ringbuf_log_msg(struct imessaging_context *msg,
106 struct server_id src,
109 char *log = debug_get_ringbuf();
110 size_t logsize = debug_get_ringbuf_size();
114 log = discard_const_p(char, "*disabled*\n");
115 logsize = strlen(log) + 1;
118 blob.data = (uint8_t *)log;
119 blob.length = logsize;
121 imessaging_send(msg, src, MSG_RINGBUF_LOG, &blob);
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);
135 static struct dispatch_fn *imessaging_find_dispatch(
136 struct imessaging_context *msg, uint32_t msg_type)
138 /* temporary IDs use an idtree, the rest use a array of pointers */
139 if (msg_type >= MSG_TMP_BASE) {
140 return (struct dispatch_fn *)idr_find(msg->dispatch_tree,
143 if (msg_type < msg->num_types) {
144 return msg->dispatch[msg_type];
150 Register a dispatch function for a particular message type.
152 NTSTATUS imessaging_register(struct imessaging_context *msg, void *private_data,
153 uint32_t msg_type, msg_callback_t fn)
155 struct dispatch_fn *d;
157 /* possibly expand dispatch array */
158 if (msg_type >= msg->num_types) {
159 struct dispatch_fn **dp;
161 dp = talloc_realloc(msg, msg->dispatch, struct dispatch_fn *, msg_type+1);
162 NT_STATUS_HAVE_NO_MEMORY(dp);
164 for (i=msg->num_types;i<=msg_type;i++) {
165 msg->dispatch[i] = NULL;
167 msg->num_types = msg_type+1;
170 d = talloc_zero(msg->dispatch, struct dispatch_fn);
171 NT_STATUS_HAVE_NO_MEMORY(d);
172 d->msg_type = msg_type;
173 d->private_data = private_data;
176 DLIST_ADD(msg->dispatch[msg_type], d);
182 register a temporary message handler. The msg_type is allocated
185 NTSTATUS imessaging_register_tmp(struct imessaging_context *msg, void *private_data,
186 msg_callback_t fn, uint32_t *msg_type)
188 struct dispatch_fn *d;
191 d = talloc_zero(msg->dispatch, struct dispatch_fn);
192 NT_STATUS_HAVE_NO_MEMORY(d);
193 d->private_data = private_data;
196 id = idr_get_new_above(msg->dispatch_tree, d, MSG_TMP_BASE, UINT16_MAX);
199 return NT_STATUS_TOO_MANY_CONTEXT_IDS;
202 d->msg_type = (uint32_t)id;
203 (*msg_type) = d->msg_type;
209 De-register the function for a particular message type.
211 void imessaging_deregister(struct imessaging_context *msg, uint32_t msg_type, void *private_data)
213 struct dispatch_fn *d, *next;
215 if (msg_type >= msg->num_types) {
216 d = (struct dispatch_fn *)idr_find(msg->dispatch_tree,
219 idr_remove(msg->dispatch_tree, msg_type);
224 for (d = msg->dispatch[msg_type]; d; d = next) {
226 if (d->private_data == private_data) {
227 DLIST_REMOVE(msg->dispatch[msg_type], d);
235 int imessaging_cleanup(struct imessaging_context *msg)
243 static void imessaging_dgm_recv(struct tevent_context *ev,
244 const uint8_t *buf, size_t buf_len,
245 int *fds, size_t num_fds,
248 /* Keep a list of imessaging contexts */
249 static struct imessaging_context *msg_ctxs;
251 static int imessaging_context_destructor(struct imessaging_context *msg)
253 DLIST_REMOVE(msg_ctxs, msg);
254 TALLOC_FREE(msg->msg_dgm_ref);
259 * Cleanup messaging dgm contexts on a specific event context.
261 * We must make sure to unref all messaging_dgm_ref's *before* the
262 * tevent context goes away. Only when the last ref is freed, the
263 * refcounted messaging dgm context will be freed.
265 void imessaging_dgm_unref_ev(struct tevent_context *ev)
267 struct imessaging_context *msg = NULL;
269 for (msg = msg_ctxs; msg != NULL; msg = msg->next) {
271 TALLOC_FREE(msg->msg_dgm_ref);
276 static NTSTATUS imessaging_reinit(struct imessaging_context *msg)
280 TALLOC_FREE(msg->msg_dgm_ref);
282 msg->server_id.pid = getpid();
284 msg->msg_dgm_ref = messaging_dgm_ref(msg,
286 &msg->server_id.unique_id,
293 if (msg->msg_dgm_ref == NULL) {
294 DEBUG(2, ("messaging_dgm_ref failed: %s\n",
296 return map_nt_error_from_unix_common(ret);
299 server_id_db_reinit(msg->names, msg->server_id);
304 * Must be called after a fork.
306 NTSTATUS imessaging_reinit_all(void)
308 struct imessaging_context *msg = NULL;
310 for (msg = msg_ctxs; msg != NULL; msg = msg->next) {
311 NTSTATUS status = imessaging_reinit(msg);
312 if (!NT_STATUS_IS_OK(status)) {
320 create the listening socket and setup the dispatcher
322 static struct imessaging_context *imessaging_init_internal(TALLOC_CTX *mem_ctx,
323 struct loadparm_context *lp_ctx,
324 struct server_id server_id,
325 struct tevent_context *ev)
328 struct imessaging_context *msg;
331 const char *lock_dir = NULL;
332 int tdb_flags = TDB_INCOMPATIBLE_HASH | TDB_CLEAR_IF_FIRST;
338 msg = talloc_zero(mem_ctx, struct imessaging_context);
344 talloc_set_destructor(msg, imessaging_context_destructor);
346 /* create the messaging directory if needed */
348 lock_dir = lpcfg_lock_directory(lp_ctx);
349 if (lock_dir == NULL) {
353 msg->sock_dir = lpcfg_private_path(msg, lp_ctx, "msg.sock");
354 if (msg->sock_dir == NULL) {
357 ok = directory_create_or_exist_strict(msg->sock_dir, geteuid(), 0700);
362 msg->lock_dir = lpcfg_lock_path(msg, lp_ctx, "msg.lock");
363 if (msg->lock_dir == NULL) {
366 ok = directory_create_or_exist_strict(msg->lock_dir, geteuid(), 0755);
371 msg->msg_dgm_ref = messaging_dgm_ref(
372 msg, ev, &server_id.unique_id, msg->sock_dir, msg->lock_dir,
373 imessaging_dgm_recv, msg, &ret);
375 if (msg->msg_dgm_ref == NULL) {
379 msg->server_id = server_id;
380 msg->idr = idr_init(msg);
381 if (msg->idr == NULL) {
385 msg->dispatch_tree = idr_init(msg);
386 if (msg->dispatch_tree == NULL) {
390 msg->start_time = timeval_current();
392 tdb_flags |= lpcfg_tdb_flags(lp_ctx, 0);
395 * This context holds a destructor that cleans up any names
396 * registered on this context on talloc_free()
398 msg->names = server_id_db_init(msg, server_id, lock_dir, 0, tdb_flags);
399 if (msg->names == NULL) {
403 status = imessaging_register(msg, NULL, MSG_PING, ping_message);
404 if (!NT_STATUS_IS_OK(status)) {
407 status = imessaging_register(msg, NULL, MSG_REQ_POOL_USAGE,
409 if (!NT_STATUS_IS_OK(status)) {
412 status = imessaging_register(msg, NULL, MSG_IRPC, irpc_handler);
413 if (!NT_STATUS_IS_OK(status)) {
416 status = imessaging_register(msg, NULL, MSG_REQ_RINGBUF_LOG,
418 if (!NT_STATUS_IS_OK(status)) {
421 status = IRPC_REGISTER(msg, irpc, IRPC_UPTIME, irpc_uptime, msg);
422 if (!NT_STATUS_IS_OK(status)) {
426 DLIST_ADD(msg_ctxs, msg);
434 struct imessaging_post_state {
435 struct imessaging_context *msg_ctx;
436 struct imessaging_post_state **busy_ref;
441 static int imessaging_post_state_destructor(struct imessaging_post_state *state)
443 if (state->busy_ref != NULL) {
444 *state->busy_ref = NULL;
445 state->busy_ref = NULL;
450 static void imessaging_post_handler(struct tevent_context *ev,
451 struct tevent_immediate *ti,
454 struct imessaging_post_state *state = talloc_get_type_abort(
455 private_data, struct imessaging_post_state);
458 * In usecases like using messaging_client_init() with irpc processing
459 * we may free the imessaging_context during the messaging handler.
460 * imessaging_post_state is a child of imessaging_context and
461 * might be implicitly free'ed before the explicit TALLOC_FREE(state).
463 * The busy_ref pointer makes sure the destructor clears
464 * the local 'state' variable.
467 SMB_ASSERT(state->busy_ref == NULL);
468 state->busy_ref = &state;
470 imessaging_dgm_recv(ev, state->buf, state->buf_len, NULL, 0,
477 state->busy_ref = NULL;
481 static int imessaging_post_self(struct imessaging_context *msg,
482 const uint8_t *buf, size_t buf_len)
484 struct tevent_immediate *ti;
485 struct imessaging_post_state *state;
488 msg, offsetof(struct imessaging_post_state, buf) + buf_len);
492 talloc_set_name_const(state, "struct imessaging_post_state");
494 talloc_set_destructor(state, imessaging_post_state_destructor);
496 ti = tevent_create_immediate(state);
502 state->msg_ctx = msg;
503 state->busy_ref = NULL;
504 state->buf_len = buf_len;
505 memcpy(state->buf, buf, buf_len);
507 tevent_schedule_immediate(ti, msg->ev, imessaging_post_handler,
513 static void imessaging_dgm_recv(struct tevent_context *ev,
514 const uint8_t *buf, size_t buf_len,
515 int *fds, size_t num_fds,
518 struct imessaging_context *msg = talloc_get_type_abort(
519 private_data, struct imessaging_context);
521 struct server_id src, dst;
522 struct server_id_buf srcbuf, dstbuf;
525 if (buf_len < MESSAGE_HDR_LENGTH) {
526 /* Invalid message, ignore */
532 * Source4 based messaging does not expect fd's yet
539 ret = imessaging_post_self(msg, buf, buf_len);
541 DBG_WARNING("imessaging_post_self failed: %s\n",
547 message_hdr_get(&msg_type, &src, &dst, buf);
549 data.data = discard_const_p(uint8_t, buf + MESSAGE_HDR_LENGTH);
550 data.length = buf_len - MESSAGE_HDR_LENGTH;
552 if ((cluster_id_equal(&dst, &msg->server_id)) ||
553 ((dst.task_id == 0) && (msg->server_id.pid == 0))) {
554 struct dispatch_fn *d, *next;
556 DEBUG(10, ("%s: dst %s matches my id: %s, type=0x%x\n",
558 server_id_str_buf(dst, &dstbuf),
559 server_id_str_buf(msg->server_id, &srcbuf),
560 (unsigned)msg_type));
562 d = imessaging_find_dispatch(msg, msg_type);
564 for (; d; d = next) {
566 d->fn(msg, d->private_data, d->msg_type, src, &data);
569 DEBUG(10, ("%s: Ignoring type=0x%x dst %s, I am %s, \n",
570 __func__, (unsigned)msg_type,
571 server_id_str_buf(dst, &dstbuf),
572 server_id_str_buf(msg->server_id, &srcbuf)));
576 struct imessaging_context *imessaging_init(TALLOC_CTX *mem_ctx,
577 struct loadparm_context *lp_ctx,
578 struct server_id server_id,
579 struct tevent_context *ev)
585 if (tevent_context_is_wrapper(ev)) {
587 * This is really a programmer error!
589 * The main/raw tevent context should
590 * have been registered first!
592 DBG_ERR("Should not be used with a wrapper tevent context\n");
597 return imessaging_init_internal(mem_ctx, lp_ctx, server_id, ev);
601 A hack, for the short term until we get 'client only' messaging in place
603 struct imessaging_context *imessaging_client_init(TALLOC_CTX *mem_ctx,
604 struct loadparm_context *lp_ctx,
605 struct tevent_context *ev)
610 id.task_id = generate_random();
611 id.vnn = NONCLUSTER_VNN;
613 /* This is because we are not in the s3 serverid database */
614 id.unique_id = SERVERID_UNIQUE_ID_NOT_TO_VERIFY;
616 return imessaging_init_internal(mem_ctx, lp_ctx, id, ev);
619 a list of registered irpc server functions
622 struct irpc_list *next, *prev;
624 const struct ndr_interface_table *table;
632 register a irpc server function
634 NTSTATUS irpc_register(struct imessaging_context *msg_ctx,
635 const struct ndr_interface_table *table,
636 int callnum, irpc_function_t fn, void *private_data)
638 struct irpc_list *irpc;
640 /* override an existing handler, if any */
641 for (irpc=msg_ctx->irpc; irpc; irpc=irpc->next) {
642 if (irpc->table == table && irpc->callnum == callnum) {
647 irpc = talloc(msg_ctx, struct irpc_list);
648 NT_STATUS_HAVE_NO_MEMORY(irpc);
649 DLIST_ADD(msg_ctx->irpc, irpc);
653 irpc->callnum = callnum;
655 irpc->private_data = private_data;
656 irpc->uuid = irpc->table->syntax_id.uuid;
663 handle an incoming irpc reply message
665 static void irpc_handler_reply(struct imessaging_context *msg_ctx, struct irpc_message *m)
667 struct irpc_request *irpc;
669 irpc = (struct irpc_request *)idr_find(msg_ctx->idr, m->header.callid);
670 if (irpc == NULL) return;
672 irpc->incoming.handler(irpc, m);
678 NTSTATUS irpc_send_reply(struct irpc_message *m, NTSTATUS status)
680 struct ndr_push *push;
682 enum ndr_err_code ndr_err;
684 m->header.status = status;
686 /* setup the reply */
687 push = ndr_push_init_ctx(m->ndr);
689 status = NT_STATUS_NO_MEMORY;
693 m->header.flags |= IRPC_FLAG_REPLY;
694 m->header.creds.token= NULL;
696 /* construct the packet */
697 ndr_err = ndr_push_irpc_header(push, NDR_SCALARS|NDR_BUFFERS, &m->header);
698 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
699 status = ndr_map_error2ntstatus(ndr_err);
703 ndr_err = m->irpc->table->calls[m->irpc->callnum].ndr_push(push, NDR_OUT, m->data);
704 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
705 status = ndr_map_error2ntstatus(ndr_err);
709 /* send the reply message */
710 packet = ndr_push_blob(push);
711 status = imessaging_send(m->msg_ctx, m->from, MSG_IRPC, &packet);
712 if (!NT_STATUS_IS_OK(status)) goto failed;
720 handle an incoming irpc request message
722 static void irpc_handler_request(struct imessaging_context *msg_ctx,
723 struct irpc_message *m)
727 enum ndr_err_code ndr_err;
729 for (i=msg_ctx->irpc; i; i=i->next) {
730 if (GUID_equal(&i->uuid, &m->header.uuid) &&
731 i->table->syntax_id.if_version == m->header.if_version &&
732 i->callnum == m->header.callnum) {
738 /* no registered handler for this message */
743 /* allocate space for the structure */
744 r = talloc_zero_size(m->ndr, i->table->calls[m->header.callnum].struct_size);
745 if (r == NULL) goto failed;
747 m->ndr->flags |= LIBNDR_FLAG_REF_ALLOC;
749 /* parse the request data */
750 ndr_err = i->table->calls[i->callnum].ndr_pull(m->ndr, NDR_IN, r);
751 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) goto failed;
754 m->private_data= i->private_data;
755 m->defer_reply = false;
757 m->msg_ctx = msg_ctx;
761 m->header.status = i->fn(m, r);
764 /* the server function won't ever be replying to this request */
769 if (m->defer_reply) {
770 /* the server function has asked to defer the reply to later */
771 talloc_steal(msg_ctx, m);
775 irpc_send_reply(m, m->header.status);
783 handle an incoming irpc message
785 static void irpc_handler(struct imessaging_context *msg_ctx, void *private_data,
786 uint32_t msg_type, struct server_id src, DATA_BLOB *packet)
788 struct irpc_message *m;
789 enum ndr_err_code ndr_err;
791 m = talloc(msg_ctx, struct irpc_message);
792 if (m == NULL) goto failed;
796 m->ndr = ndr_pull_init_blob(packet, m);
797 if (m->ndr == NULL) goto failed;
799 m->ndr->flags |= LIBNDR_FLAG_REF_ALLOC;
801 ndr_err = ndr_pull_irpc_header(m->ndr, NDR_BUFFERS|NDR_SCALARS, &m->header);
802 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) goto failed;
804 if (m->header.flags & IRPC_FLAG_REPLY) {
805 irpc_handler_reply(msg_ctx, m);
807 irpc_handler_request(msg_ctx, m);
817 destroy a irpc request
819 static int irpc_destructor(struct irpc_request *irpc)
821 if (irpc->callid != -1) {
822 idr_remove(irpc->msg_ctx->idr, irpc->callid);
830 add a string name that this irpc server can be called on
832 It will be removed from the DB either via irpc_remove_name or on
833 talloc_free(msg_ctx->names).
835 NTSTATUS irpc_add_name(struct imessaging_context *msg_ctx, const char *name)
839 ret = server_id_db_add(msg_ctx->names, name);
841 return map_nt_error_from_unix_common(ret);
846 static int all_servers_func(const char *name, unsigned num_servers,
847 const struct server_id *servers,
850 struct irpc_name_records *name_records = talloc_get_type(
851 private_data, struct irpc_name_records);
852 struct irpc_name_record *name_record;
856 = talloc_realloc(name_records, name_records->names,
857 struct irpc_name_record *, name_records->num_records+1);
858 if (!name_records->names) {
862 name_records->names[name_records->num_records] = name_record
863 = talloc(name_records->names,
864 struct irpc_name_record);
869 name_records->num_records++;
871 name_record->name = talloc_strdup(name_record, name);
872 if (!name_record->name) {
876 name_record->count = num_servers;
877 name_record->ids = talloc_array(name_record, struct server_id,
879 if (name_record->ids == NULL) {
882 for (i=0;i<name_record->count;i++) {
883 name_record->ids[i] = servers[i];
889 return a list of server ids for a server name
891 struct irpc_name_records *irpc_all_servers(struct imessaging_context *msg_ctx,
895 struct irpc_name_records *name_records = talloc_zero(mem_ctx, struct irpc_name_records);
896 if (name_records == NULL) {
900 ret = server_id_db_traverse_read(msg_ctx->names, all_servers_func,
903 TALLOC_FREE(name_records);
911 remove a name from a messaging context
913 void irpc_remove_name(struct imessaging_context *msg_ctx, const char *name)
915 server_id_db_remove(msg_ctx->names, name);
918 struct server_id imessaging_get_server_id(struct imessaging_context *msg_ctx)
920 return msg_ctx->server_id;
923 struct irpc_bh_state {
924 struct imessaging_context *msg_ctx;
925 struct server_id server_id;
926 const struct ndr_interface_table *table;
928 struct security_token *token;
931 static bool irpc_bh_is_connected(struct dcerpc_binding_handle *h)
933 struct irpc_bh_state *hs = dcerpc_binding_handle_data(h,
934 struct irpc_bh_state);
943 static uint32_t irpc_bh_set_timeout(struct dcerpc_binding_handle *h,
946 struct irpc_bh_state *hs = dcerpc_binding_handle_data(h,
947 struct irpc_bh_state);
948 uint32_t old = hs->timeout;
950 hs->timeout = timeout;
955 struct irpc_bh_raw_call_state {
956 struct irpc_request *irpc;
963 static void irpc_bh_raw_call_incoming_handler(struct irpc_request *irpc,
964 struct irpc_message *m);
966 static struct tevent_req *irpc_bh_raw_call_send(TALLOC_CTX *mem_ctx,
967 struct tevent_context *ev,
968 struct dcerpc_binding_handle *h,
969 const struct GUID *object,
972 const uint8_t *in_data,
975 struct irpc_bh_state *hs =
976 dcerpc_binding_handle_data(h,
977 struct irpc_bh_state);
978 struct tevent_req *req;
979 struct irpc_bh_raw_call_state *state;
981 struct irpc_header header;
982 struct ndr_push *ndr;
984 enum ndr_err_code ndr_err;
986 req = tevent_req_create(mem_ctx, &state,
987 struct irpc_bh_raw_call_state);
991 state->opnum = opnum;
992 state->in_data.data = discard_const_p(uint8_t, in_data);
993 state->in_data.length = in_length;
995 ok = irpc_bh_is_connected(h);
997 tevent_req_nterror(req, NT_STATUS_CONNECTION_DISCONNECTED);
998 return tevent_req_post(req, ev);
1001 state->irpc = talloc_zero(state, struct irpc_request);
1002 if (tevent_req_nomem(state->irpc, req)) {
1003 return tevent_req_post(req, ev);
1006 state->irpc->msg_ctx = hs->msg_ctx;
1007 state->irpc->callid = idr_get_new(hs->msg_ctx->idr,
1008 state->irpc, UINT16_MAX);
1009 if (state->irpc->callid == -1) {
1010 tevent_req_nterror(req, NT_STATUS_INSUFFICIENT_RESOURCES);
1011 return tevent_req_post(req, ev);
1013 state->irpc->incoming.handler = irpc_bh_raw_call_incoming_handler;
1014 state->irpc->incoming.private_data = req;
1016 talloc_set_destructor(state->irpc, irpc_destructor);
1018 /* setup the header */
1019 header.uuid = hs->table->syntax_id.uuid;
1021 header.if_version = hs->table->syntax_id.if_version;
1022 header.callid = state->irpc->callid;
1023 header.callnum = state->opnum;
1025 header.status = NT_STATUS_OK;
1026 header.creds.token= hs->token;
1028 /* construct the irpc packet */
1029 ndr = ndr_push_init_ctx(state->irpc);
1030 if (tevent_req_nomem(ndr, req)) {
1031 return tevent_req_post(req, ev);
1034 ndr_err = ndr_push_irpc_header(ndr, NDR_SCALARS|NDR_BUFFERS, &header);
1035 status = ndr_map_error2ntstatus(ndr_err);
1036 if (!NT_STATUS_IS_OK(status)) {
1037 tevent_req_nterror(req, status);
1038 return tevent_req_post(req, ev);
1041 ndr_err = ndr_push_bytes(ndr, in_data, in_length);
1042 status = ndr_map_error2ntstatus(ndr_err);
1043 if (!NT_STATUS_IS_OK(status)) {
1044 tevent_req_nterror(req, status);
1045 return tevent_req_post(req, ev);
1049 state->in_packet = ndr_push_blob(ndr);
1050 status = imessaging_send(hs->msg_ctx, hs->server_id,
1051 MSG_IRPC, &state->in_packet);
1052 if (!NT_STATUS_IS_OK(status)) {
1053 tevent_req_nterror(req, status);
1054 return tevent_req_post(req, ev);
1057 if (hs->timeout != IRPC_CALL_TIMEOUT_INF) {
1058 /* set timeout-callback in case caller wants that */
1059 ok = tevent_req_set_endtime(req, ev, timeval_current_ofs(hs->timeout, 0));
1061 return tevent_req_post(req, ev);
1068 static void irpc_bh_raw_call_incoming_handler(struct irpc_request *irpc,
1069 struct irpc_message *m)
1071 struct tevent_req *req =
1072 talloc_get_type_abort(irpc->incoming.private_data,
1074 struct irpc_bh_raw_call_state *state =
1075 tevent_req_data(req,
1076 struct irpc_bh_raw_call_state);
1078 talloc_steal(state, m);
1080 if (!NT_STATUS_IS_OK(m->header.status)) {
1081 tevent_req_nterror(req, m->header.status);
1085 state->out_data = data_blob_talloc(state,
1086 m->ndr->data + m->ndr->offset,
1087 m->ndr->data_size - m->ndr->offset);
1088 if ((m->ndr->data_size - m->ndr->offset) > 0 && !state->out_data.data) {
1089 tevent_req_oom(req);
1093 tevent_req_done(req);
1096 static NTSTATUS irpc_bh_raw_call_recv(struct tevent_req *req,
1097 TALLOC_CTX *mem_ctx,
1100 uint32_t *out_flags)
1102 struct irpc_bh_raw_call_state *state =
1103 tevent_req_data(req,
1104 struct irpc_bh_raw_call_state);
1107 if (tevent_req_is_nterror(req, &status)) {
1108 tevent_req_received(req);
1112 *out_data = talloc_move(mem_ctx, &state->out_data.data);
1113 *out_length = state->out_data.length;
1115 tevent_req_received(req);
1116 return NT_STATUS_OK;
1119 struct irpc_bh_disconnect_state {
1123 static struct tevent_req *irpc_bh_disconnect_send(TALLOC_CTX *mem_ctx,
1124 struct tevent_context *ev,
1125 struct dcerpc_binding_handle *h)
1127 struct irpc_bh_state *hs = dcerpc_binding_handle_data(h,
1128 struct irpc_bh_state);
1129 struct tevent_req *req;
1130 struct irpc_bh_disconnect_state *state;
1133 req = tevent_req_create(mem_ctx, &state,
1134 struct irpc_bh_disconnect_state);
1139 ok = irpc_bh_is_connected(h);
1141 tevent_req_nterror(req, NT_STATUS_CONNECTION_DISCONNECTED);
1142 return tevent_req_post(req, ev);
1147 tevent_req_done(req);
1148 return tevent_req_post(req, ev);
1151 static NTSTATUS irpc_bh_disconnect_recv(struct tevent_req *req)
1155 if (tevent_req_is_nterror(req, &status)) {
1156 tevent_req_received(req);
1160 tevent_req_received(req);
1161 return NT_STATUS_OK;
1164 static bool irpc_bh_ref_alloc(struct dcerpc_binding_handle *h)
1169 static const struct dcerpc_binding_handle_ops irpc_bh_ops = {
1171 .is_connected = irpc_bh_is_connected,
1172 .set_timeout = irpc_bh_set_timeout,
1173 .raw_call_send = irpc_bh_raw_call_send,
1174 .raw_call_recv = irpc_bh_raw_call_recv,
1175 .disconnect_send = irpc_bh_disconnect_send,
1176 .disconnect_recv = irpc_bh_disconnect_recv,
1178 .ref_alloc = irpc_bh_ref_alloc,
1181 /* initialise a irpc binding handle */
1182 struct dcerpc_binding_handle *irpc_binding_handle(TALLOC_CTX *mem_ctx,
1183 struct imessaging_context *msg_ctx,
1184 struct server_id server_id,
1185 const struct ndr_interface_table *table)
1187 struct dcerpc_binding_handle *h;
1188 struct irpc_bh_state *hs;
1190 h = dcerpc_binding_handle_create(mem_ctx,
1195 struct irpc_bh_state,
1200 hs->msg_ctx = msg_ctx;
1201 hs->server_id = server_id;
1203 hs->timeout = IRPC_CALL_TIMEOUT;
1208 struct dcerpc_binding_handle *irpc_binding_handle_by_name(TALLOC_CTX *mem_ctx,
1209 struct imessaging_context *msg_ctx,
1210 const char *dest_task,
1211 const struct ndr_interface_table *table)
1213 struct dcerpc_binding_handle *h;
1215 struct server_id *sids;
1216 struct server_id sid;
1219 /* find the server task */
1221 status = irpc_servers_byname(msg_ctx, mem_ctx, dest_task,
1223 if (!NT_STATUS_IS_OK(status)) {
1224 errno = EADDRNOTAVAIL;
1230 h = irpc_binding_handle(mem_ctx, msg_ctx,
1239 void irpc_binding_handle_add_security_token(struct dcerpc_binding_handle *h,
1240 struct security_token *token)
1242 struct irpc_bh_state *hs =
1243 dcerpc_binding_handle_data(h,
1244 struct irpc_bh_state);