2 * Endpoint Mapper Functions
3 * DCERPC local endpoint mapper client routines
4 * Copyright (c) 2010-2011 Andreas Schneider.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "librpc/rpc/dcerpc.h"
22 #include "librpc/rpc/dcerpc_ep.h"
23 #include "../librpc/gen_ndr/ndr_epmapper_c.h"
24 #include "rpc_client/cli_pipe.h"
26 #include "rpc_server/rpc_ncacn_np.h"
27 #include "../lib/tsocket/tsocket.h"
29 #define EPM_MAX_ANNOTATION_SIZE 64
31 static bool binding_vector_realloc(struct dcerpc_binding_vector *bvec)
33 if (bvec->count >= bvec->allocated) {
34 struct dcerpc_binding *tmp;
36 tmp = talloc_realloc(bvec,
38 struct dcerpc_binding,
44 bvec->allocated = bvec->allocated * 2;
50 NTSTATUS dcerpc_binding_vector_new(TALLOC_CTX *mem_ctx,
51 struct dcerpc_binding_vector **pbvec)
53 struct dcerpc_binding_vector *bvec;
57 tmp_ctx = talloc_stackframe();
58 if (tmp_ctx == NULL) {
59 return NT_STATUS_NO_MEMORY;
62 bvec = talloc_zero(tmp_ctx, struct dcerpc_binding_vector);
64 status = NT_STATUS_NO_MEMORY;
68 bvec->bindings = talloc_zero_array(bvec,
69 struct dcerpc_binding,
71 if (bvec->bindings == NULL) {
72 status = NT_STATUS_NO_MEMORY;
79 *pbvec = talloc_move(mem_ctx, &bvec);
81 status = NT_STATUS_OK;
88 NTSTATUS dcerpc_binding_vector_add_np_default(const struct ndr_interface_table *iface,
89 struct dcerpc_binding_vector *bvec)
91 uint32_t ep_count = iface->endpoints->count;
96 for (i = 0; i < ep_count; i++) {
97 struct dcerpc_binding *b;
99 b = talloc_zero(bvec->bindings, struct dcerpc_binding);
101 return NT_STATUS_NO_MEMORY;
104 status = dcerpc_parse_binding(b, iface->endpoints->names[i], &b);
105 if (!NT_STATUS_IS_OK(status)) {
106 return NT_STATUS_UNSUCCESSFUL;
109 /* Only add the named pipes defined in the iface endpoints */
110 if (b->transport != NCACN_NP) {
115 b->object = iface->syntax_id;
117 b->host = talloc_asprintf(b, "\\\\%s", lp_netbios_name());
118 if (b->host == NULL) {
120 return NT_STATUS_NO_MEMORY;
123 ok = binding_vector_realloc(bvec);
126 return NT_STATUS_NO_MEMORY;
129 bvec->bindings[bvec->count] = *b;
136 NTSTATUS dcerpc_binding_vector_add_port(const struct ndr_interface_table *iface,
137 struct dcerpc_binding_vector *bvec,
141 uint32_t ep_count = iface->endpoints->count;
146 for (i = 0; i < ep_count; i++) {
147 struct dcerpc_binding *b;
149 b = talloc_zero(bvec->bindings, struct dcerpc_binding);
151 return NT_STATUS_NO_MEMORY;
154 status = dcerpc_parse_binding(b, iface->endpoints->names[i], &b);
155 if (!NT_STATUS_IS_OK(status)) {
156 return NT_STATUS_UNSUCCESSFUL;
159 if (b->transport != NCACN_IP_TCP) {
164 b->object = iface->syntax_id;
166 b->host = talloc_strdup(b, host);
167 if (b->host == NULL) {
169 return NT_STATUS_NO_MEMORY;
172 b->endpoint = talloc_asprintf(b, "%u", port);
173 if (b->endpoint == NULL) {
175 return NT_STATUS_NO_MEMORY;
178 ok = binding_vector_realloc(bvec);
181 return NT_STATUS_NO_MEMORY;
184 bvec->bindings[bvec->count] = *b;
193 NTSTATUS dcerpc_binding_vector_add_unix(const struct ndr_interface_table *iface,
194 struct dcerpc_binding_vector *bvec,
197 uint32_t ep_count = iface->endpoints->count;
202 for (i = 0; i < ep_count; i++) {
203 struct dcerpc_binding *b;
205 b = talloc_zero(bvec->bindings, struct dcerpc_binding);
207 return NT_STATUS_NO_MEMORY;
210 status = dcerpc_parse_binding(b, iface->endpoints->names[i], &b);
211 if (!NT_STATUS_IS_OK(status)) {
212 return NT_STATUS_UNSUCCESSFUL;
215 if (b->transport != NCALRPC) {
220 b->object = iface->syntax_id;
222 b->endpoint = talloc_asprintf(b,
226 if (b->endpoint == NULL) {
228 return NT_STATUS_NO_MEMORY;
231 ok = binding_vector_realloc(bvec);
234 return NT_STATUS_NO_MEMORY;
237 bvec->bindings[bvec->count] = *b;
246 struct dcerpc_binding_vector *dcerpc_binding_vector_dup(TALLOC_CTX *mem_ctx,
247 const struct dcerpc_binding_vector *bvec)
249 struct dcerpc_binding_vector *v;
252 v = talloc(mem_ctx, struct dcerpc_binding_vector);
257 v->bindings = talloc_array(v, struct dcerpc_binding, bvec->allocated);
258 if (v->bindings == NULL) {
262 v->allocated = bvec->allocated;
264 for (i = 0; i < bvec->count; i++) {
265 struct dcerpc_binding *b;
267 b = dcerpc_binding_dup(v->bindings, &bvec->bindings[i]);
274 v->count = bvec->count;
279 NTSTATUS dcerpc_binding_vector_create(TALLOC_CTX *mem_ctx,
280 const struct ndr_interface_table *iface,
283 struct dcerpc_binding_vector **pbvec)
285 struct dcerpc_binding_vector *bvec;
292 tmp_ctx = talloc_stackframe();
293 if (tmp_ctx == NULL) {
294 return NT_STATUS_NO_MEMORY;
297 ep_count = iface->endpoints->count;
299 bvec = talloc_zero(tmp_ctx, struct dcerpc_binding_vector);
301 status = NT_STATUS_NO_MEMORY;
305 bvec->bindings = talloc_zero_array(bvec, struct dcerpc_binding, ep_count);
306 if (bvec->bindings == NULL) {
307 status = NT_STATUS_NO_MEMORY;
311 for (i = 0; i < ep_count; i++) {
312 struct dcerpc_binding *b;
314 b = talloc_zero(bvec->bindings, struct dcerpc_binding);
316 status = NT_STATUS_NO_MEMORY;
320 status = dcerpc_parse_binding(b, iface->endpoints->names[i], &b);
321 if (!NT_STATUS_IS_OK(status)) {
322 status = NT_STATUS_UNSUCCESSFUL;
326 b->object = iface->syntax_id;
328 switch (b->transport) {
330 b->host = talloc_asprintf(b, "\\\\%s", lp_netbios_name());
331 if (b->host == NULL) {
332 status = NT_STATUS_NO_MEMORY;
342 b->endpoint = talloc_asprintf(b, "%u", port);
343 if (b->endpoint == NULL) {
344 status = NT_STATUS_NO_MEMORY;
350 if (ncalrpc == NULL) {
355 b->endpoint = talloc_asprintf(b,
359 if (b->endpoint == NULL) {
360 status = NT_STATUS_NO_MEMORY;
369 bvec->bindings[count] = *b;
375 *pbvec = talloc_move(mem_ctx, &bvec);
377 status = NT_STATUS_OK;
379 talloc_free(tmp_ctx);
384 static NTSTATUS ep_register(TALLOC_CTX *mem_ctx,
385 struct messaging_context *msg_ctx,
386 const struct ndr_interface_table *iface,
387 const struct dcerpc_binding_vector *bind_vec,
388 const struct GUID *object_guid,
389 const char *annotation,
392 struct dcerpc_binding_handle **pbh)
394 struct rpc_pipe_client *cli = NULL;
395 struct dcerpc_binding_handle *h;
396 struct pipe_auth_data *auth;
397 const char *ncalrpc_sock;
398 const char *rpcsrv_type;
399 struct epm_entry_t *entries;
400 uint32_t num_ents, i;
402 uint32_t result = EPMAPPER_STATUS_OK;
406 return NT_STATUS_INVALID_PARAMETER;
409 if (bind_vec == NULL || bind_vec->count == 0) {
410 return NT_STATUS_INVALID_PARAMETER;
413 tmp_ctx = talloc_stackframe();
414 if (tmp_ctx == NULL) {
415 return NT_STATUS_NO_MEMORY;
418 rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
419 "rpc_server", "epmapper",
422 if (strcasecmp_m(rpcsrv_type, "embedded") == 0) {
423 struct tsocket_address *local;
426 rc = tsocket_address_inet_from_strings(tmp_ctx,
432 return NT_STATUS_NO_MEMORY;
435 status = rpcint_binding_handle(tmp_ctx,
438 get_session_info_system(),
441 if (!NT_STATUS_IS_OK(status)) {
442 DEBUG(1, ("dcerpc_ep_register: Could not connect to "
443 "epmapper (%s)", nt_errstr(status)));
446 } else if (strcasecmp_m(rpcsrv_type, "daemon") == 0) {
447 /* Connect to the endpoint mapper locally */
448 ncalrpc_sock = talloc_asprintf(tmp_ctx,
452 if (ncalrpc_sock == NULL) {
453 status = NT_STATUS_NO_MEMORY;
457 status = rpc_pipe_open_ncalrpc(tmp_ctx,
459 &ndr_table_epmapper.syntax_id,
461 if (!NT_STATUS_IS_OK(status)) {
465 status = rpccli_ncalrpc_bind_data(cli, &auth);
466 if (!NT_STATUS_IS_OK(status)) {
467 DEBUG(0, ("Failed to initialize anonymous bind.\n"));
471 status = rpc_pipe_bind(cli, auth);
472 if (!NT_STATUS_IS_OK(status)) {
473 DEBUG(2, ("Failed to bind ncalrpc socket.\n"));
477 h = cli->binding_handle;
479 status = NT_STATUS_INVALID_PARAMETER;
483 num_ents = bind_vec->count;
484 entries = talloc_array(tmp_ctx, struct epm_entry_t, num_ents);
486 for (i = 0; i < num_ents; i++) {
487 struct dcerpc_binding *map_binding = &bind_vec->bindings[i];
488 struct epm_twr_t *map_tower;
490 map_tower = talloc_zero(entries, struct epm_twr_t);
491 if (map_tower == NULL) {
492 status = NT_STATUS_NO_MEMORY;
496 status = dcerpc_binding_build_tower(entries,
499 if (!NT_STATUS_IS_OK(status)) {
503 entries[i].tower = map_tower;
504 if (annotation == NULL) {
505 entries[i].annotation = talloc_strdup(entries, "");
507 entries[i].annotation = talloc_strndup(entries,
509 EPM_MAX_ANNOTATION_SIZE);
511 if (entries[i].annotation == NULL) {
512 status = NT_STATUS_NO_MEMORY;
516 if (object_guid != NULL) {
517 entries[i].object = *object_guid;
519 entries[i].object = map_binding->object.uuid;
524 status = dcerpc_epm_Delete(h,
530 status = dcerpc_epm_Insert(h,
537 if (!NT_STATUS_IS_OK(status)) {
538 DEBUG(0, ("dcerpc_ep_register: Could not insert tower (%s)\n",
542 if (result != EPMAPPER_STATUS_OK) {
543 DEBUG(0, ("dcerpc_ep_register: Could not insert tower (0x%.8x)\n",
545 status = NT_STATUS_UNSUCCESSFUL;
550 *pbh = talloc_move(mem_ctx, &h);
551 talloc_steal(*pbh, cli);
555 talloc_free(tmp_ctx);
560 NTSTATUS dcerpc_ep_register(TALLOC_CTX *mem_ctx,
561 struct messaging_context *msg_ctx,
562 const struct ndr_interface_table *iface,
563 const struct dcerpc_binding_vector *bind_vec,
564 const struct GUID *object_guid,
565 const char *annotation,
566 struct dcerpc_binding_handle **ph)
568 return ep_register(mem_ctx,
579 NTSTATUS dcerpc_ep_register_noreplace(TALLOC_CTX *mem_ctx,
580 struct messaging_context *msg_ctx,
581 const struct ndr_interface_table *iface,
582 const struct dcerpc_binding_vector *bind_vec,
583 const struct GUID *object_guid,
584 const char *annotation,
585 struct dcerpc_binding_handle **ph)
587 return ep_register(mem_ctx,
598 NTSTATUS dcerpc_ep_unregister(struct messaging_context *msg_ctx,
599 const struct ndr_interface_table *iface,
600 const struct dcerpc_binding_vector *bind_vec,
601 const struct GUID *object_guid)
603 return ep_register(NULL,
614 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */