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 NTSTATUS dcerpc_binding_vector_replace_iface(const struct ndr_interface_table *iface,
247 struct dcerpc_binding_vector *v)
251 for (i = 0; i < v->count; i++) {
252 struct dcerpc_binding *b;
254 b = &(v->bindings[i]);
255 b->object = iface->syntax_id;
261 struct dcerpc_binding_vector *dcerpc_binding_vector_dup(TALLOC_CTX *mem_ctx,
262 const struct dcerpc_binding_vector *bvec)
264 struct dcerpc_binding_vector *v;
267 v = talloc(mem_ctx, struct dcerpc_binding_vector);
272 v->bindings = talloc_array(v, struct dcerpc_binding, bvec->allocated);
273 if (v->bindings == NULL) {
277 v->allocated = bvec->allocated;
279 for (i = 0; i < bvec->count; i++) {
280 struct dcerpc_binding *b;
282 b = dcerpc_binding_dup(v->bindings, &bvec->bindings[i]);
289 v->count = bvec->count;
294 NTSTATUS dcerpc_binding_vector_create(TALLOC_CTX *mem_ctx,
295 const struct ndr_interface_table *iface,
298 struct dcerpc_binding_vector **pbvec)
300 struct dcerpc_binding_vector *bvec;
307 tmp_ctx = talloc_stackframe();
308 if (tmp_ctx == NULL) {
309 return NT_STATUS_NO_MEMORY;
312 ep_count = iface->endpoints->count;
314 bvec = talloc_zero(tmp_ctx, struct dcerpc_binding_vector);
316 status = NT_STATUS_NO_MEMORY;
320 bvec->bindings = talloc_zero_array(bvec, struct dcerpc_binding, ep_count);
321 if (bvec->bindings == NULL) {
322 status = NT_STATUS_NO_MEMORY;
326 for (i = 0; i < ep_count; i++) {
327 struct dcerpc_binding *b;
329 b = talloc_zero(bvec->bindings, struct dcerpc_binding);
331 status = NT_STATUS_NO_MEMORY;
335 status = dcerpc_parse_binding(b, iface->endpoints->names[i], &b);
336 if (!NT_STATUS_IS_OK(status)) {
337 status = NT_STATUS_UNSUCCESSFUL;
341 b->object = iface->syntax_id;
343 switch (b->transport) {
345 b->host = talloc_asprintf(b, "\\\\%s", lp_netbios_name());
346 if (b->host == NULL) {
347 status = NT_STATUS_NO_MEMORY;
357 b->endpoint = talloc_asprintf(b, "%u", port);
358 if (b->endpoint == NULL) {
359 status = NT_STATUS_NO_MEMORY;
365 if (ncalrpc == NULL) {
370 b->endpoint = talloc_asprintf(b,
374 if (b->endpoint == NULL) {
375 status = NT_STATUS_NO_MEMORY;
384 bvec->bindings[count] = *b;
390 *pbvec = talloc_move(mem_ctx, &bvec);
392 status = NT_STATUS_OK;
394 talloc_free(tmp_ctx);
399 static NTSTATUS ep_register(TALLOC_CTX *mem_ctx,
400 struct messaging_context *msg_ctx,
401 const struct ndr_interface_table *iface,
402 const struct dcerpc_binding_vector *bind_vec,
403 const struct GUID *object_guid,
404 const char *annotation,
407 struct dcerpc_binding_handle **pbh)
409 struct rpc_pipe_client *cli = NULL;
410 struct dcerpc_binding_handle *h;
411 struct pipe_auth_data *auth;
412 const char *ncalrpc_sock;
413 const char *rpcsrv_type;
414 struct epm_entry_t *entries;
415 uint32_t num_ents, i;
417 uint32_t result = EPMAPPER_STATUS_OK;
421 return NT_STATUS_INVALID_PARAMETER;
424 if (bind_vec == NULL || bind_vec->count == 0) {
425 return NT_STATUS_INVALID_PARAMETER;
428 tmp_ctx = talloc_stackframe();
429 if (tmp_ctx == NULL) {
430 return NT_STATUS_NO_MEMORY;
433 rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
434 "rpc_server", "epmapper",
437 if (strcasecmp_m(rpcsrv_type, "embedded") == 0) {
438 struct tsocket_address *local;
441 rc = tsocket_address_inet_from_strings(tmp_ctx,
447 return NT_STATUS_NO_MEMORY;
450 status = rpcint_binding_handle(tmp_ctx,
453 get_session_info_system(),
456 if (!NT_STATUS_IS_OK(status)) {
457 DEBUG(1, ("dcerpc_ep_register: Could not connect to "
458 "epmapper (%s)", nt_errstr(status)));
461 } else if (strcasecmp_m(rpcsrv_type, "daemon") == 0) {
462 /* Connect to the endpoint mapper locally */
463 ncalrpc_sock = talloc_asprintf(tmp_ctx,
467 if (ncalrpc_sock == NULL) {
468 status = NT_STATUS_NO_MEMORY;
472 status = rpc_pipe_open_ncalrpc(tmp_ctx,
474 &ndr_table_epmapper.syntax_id,
476 if (!NT_STATUS_IS_OK(status)) {
480 status = rpccli_ncalrpc_bind_data(cli, &auth);
481 if (!NT_STATUS_IS_OK(status)) {
482 DEBUG(0, ("Failed to initialize anonymous bind.\n"));
486 status = rpc_pipe_bind(cli, auth);
487 if (!NT_STATUS_IS_OK(status)) {
488 DEBUG(2, ("Failed to bind ncalrpc socket.\n"));
492 h = cli->binding_handle;
494 status = NT_STATUS_INVALID_PARAMETER;
498 num_ents = bind_vec->count;
499 entries = talloc_array(tmp_ctx, struct epm_entry_t, num_ents);
501 for (i = 0; i < num_ents; i++) {
502 struct dcerpc_binding *map_binding = &bind_vec->bindings[i];
503 struct epm_twr_t *map_tower;
505 map_tower = talloc_zero(entries, struct epm_twr_t);
506 if (map_tower == NULL) {
507 status = NT_STATUS_NO_MEMORY;
511 status = dcerpc_binding_build_tower(entries,
514 if (!NT_STATUS_IS_OK(status)) {
518 entries[i].tower = map_tower;
519 if (annotation == NULL) {
520 entries[i].annotation = talloc_strdup(entries, "");
522 entries[i].annotation = talloc_strndup(entries,
524 EPM_MAX_ANNOTATION_SIZE);
526 if (entries[i].annotation == NULL) {
527 status = NT_STATUS_NO_MEMORY;
531 if (object_guid != NULL) {
532 entries[i].object = *object_guid;
534 entries[i].object = map_binding->object.uuid;
539 status = dcerpc_epm_Delete(h,
545 status = dcerpc_epm_Insert(h,
552 if (!NT_STATUS_IS_OK(status)) {
553 DEBUG(0, ("dcerpc_ep_register: Could not insert tower (%s)\n",
557 if (result != EPMAPPER_STATUS_OK) {
558 DEBUG(0, ("dcerpc_ep_register: Could not insert tower (0x%.8x)\n",
560 status = NT_STATUS_UNSUCCESSFUL;
565 *pbh = talloc_move(mem_ctx, &h);
566 talloc_steal(*pbh, cli);
570 talloc_free(tmp_ctx);
575 NTSTATUS dcerpc_ep_register(TALLOC_CTX *mem_ctx,
576 struct messaging_context *msg_ctx,
577 const struct ndr_interface_table *iface,
578 const struct dcerpc_binding_vector *bind_vec,
579 const struct GUID *object_guid,
580 const char *annotation,
581 struct dcerpc_binding_handle **ph)
583 return ep_register(mem_ctx,
594 NTSTATUS dcerpc_ep_register_noreplace(TALLOC_CTX *mem_ctx,
595 struct messaging_context *msg_ctx,
596 const struct ndr_interface_table *iface,
597 const struct dcerpc_binding_vector *bind_vec,
598 const struct GUID *object_guid,
599 const char *annotation,
600 struct dcerpc_binding_handle **ph)
602 return ep_register(mem_ctx,
613 NTSTATUS dcerpc_ep_unregister(struct messaging_context *msg_ctx,
614 const struct ndr_interface_table *iface,
615 const struct dcerpc_binding_vector *bind_vec,
616 const struct GUID *object_guid)
618 return ep_register(NULL,
629 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */