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 static NTSTATUS ep_register(TALLOC_CTX *mem_ctx,
295 struct messaging_context *msg_ctx,
296 const struct ndr_interface_table *iface,
297 const struct dcerpc_binding_vector *bind_vec,
298 const struct GUID *object_guid,
299 const char *annotation,
302 struct dcerpc_binding_handle **pbh)
304 struct rpc_pipe_client *cli = NULL;
305 struct dcerpc_binding_handle *h;
306 struct pipe_auth_data *auth;
307 const char *ncalrpc_sock;
308 const char *rpcsrv_type;
309 struct epm_entry_t *entries;
310 uint32_t num_ents, i;
312 uint32_t result = EPMAPPER_STATUS_OK;
316 return NT_STATUS_INVALID_PARAMETER;
319 if (bind_vec == NULL || bind_vec->count == 0) {
320 return NT_STATUS_INVALID_PARAMETER;
323 tmp_ctx = talloc_stackframe();
324 if (tmp_ctx == NULL) {
325 return NT_STATUS_NO_MEMORY;
328 rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
329 "rpc_server", "epmapper",
332 if (strcasecmp_m(rpcsrv_type, "embedded") == 0) {
333 struct tsocket_address *local;
336 rc = tsocket_address_inet_from_strings(tmp_ctx,
342 return NT_STATUS_NO_MEMORY;
345 status = rpcint_binding_handle(tmp_ctx,
348 get_session_info_system(),
351 if (!NT_STATUS_IS_OK(status)) {
352 DEBUG(1, ("dcerpc_ep_register: Could not connect to "
353 "epmapper (%s)", nt_errstr(status)));
356 } else if (strcasecmp_m(rpcsrv_type, "daemon") == 0) {
357 /* Connect to the endpoint mapper locally */
358 ncalrpc_sock = talloc_asprintf(tmp_ctx,
362 if (ncalrpc_sock == NULL) {
363 status = NT_STATUS_NO_MEMORY;
367 status = rpc_pipe_open_ncalrpc(tmp_ctx,
369 &ndr_table_epmapper.syntax_id,
371 if (!NT_STATUS_IS_OK(status)) {
375 status = rpccli_ncalrpc_bind_data(cli, &auth);
376 if (!NT_STATUS_IS_OK(status)) {
377 DEBUG(0, ("Failed to initialize anonymous bind.\n"));
381 status = rpc_pipe_bind(cli, auth);
382 if (!NT_STATUS_IS_OK(status)) {
383 DEBUG(2, ("Failed to bind ncalrpc socket.\n"));
387 h = cli->binding_handle;
389 status = NT_STATUS_INVALID_PARAMETER;
393 num_ents = bind_vec->count;
394 entries = talloc_array(tmp_ctx, struct epm_entry_t, num_ents);
396 for (i = 0; i < num_ents; i++) {
397 struct dcerpc_binding *map_binding = &bind_vec->bindings[i];
398 struct epm_twr_t *map_tower;
400 map_tower = talloc_zero(entries, struct epm_twr_t);
401 if (map_tower == NULL) {
402 status = NT_STATUS_NO_MEMORY;
406 status = dcerpc_binding_build_tower(entries,
409 if (!NT_STATUS_IS_OK(status)) {
413 entries[i].tower = map_tower;
414 if (annotation == NULL) {
415 entries[i].annotation = talloc_strdup(entries, "");
417 entries[i].annotation = talloc_strndup(entries,
419 EPM_MAX_ANNOTATION_SIZE);
421 if (entries[i].annotation == NULL) {
422 status = NT_STATUS_NO_MEMORY;
426 if (object_guid != NULL) {
427 entries[i].object = *object_guid;
429 entries[i].object = map_binding->object.uuid;
434 status = dcerpc_epm_Delete(h,
440 status = dcerpc_epm_Insert(h,
447 if (!NT_STATUS_IS_OK(status)) {
448 DEBUG(0, ("dcerpc_ep_register: Could not insert tower (%s)\n",
452 if (result != EPMAPPER_STATUS_OK) {
453 DEBUG(0, ("dcerpc_ep_register: Could not insert tower (0x%.8x)\n",
455 status = NT_STATUS_UNSUCCESSFUL;
460 *pbh = talloc_move(mem_ctx, &h);
461 talloc_steal(*pbh, cli);
465 talloc_free(tmp_ctx);
470 NTSTATUS dcerpc_ep_register(TALLOC_CTX *mem_ctx,
471 struct messaging_context *msg_ctx,
472 const struct ndr_interface_table *iface,
473 const struct dcerpc_binding_vector *bind_vec,
474 const struct GUID *object_guid,
475 const char *annotation,
476 struct dcerpc_binding_handle **ph)
478 return ep_register(mem_ctx,
489 NTSTATUS dcerpc_ep_register_noreplace(TALLOC_CTX *mem_ctx,
490 struct messaging_context *msg_ctx,
491 const struct ndr_interface_table *iface,
492 const struct dcerpc_binding_vector *bind_vec,
493 const struct GUID *object_guid,
494 const char *annotation,
495 struct dcerpc_binding_handle **ph)
497 return ep_register(mem_ctx,
508 NTSTATUS dcerpc_ep_unregister(struct messaging_context *msg_ctx,
509 const struct ndr_interface_table *iface,
510 const struct dcerpc_binding_vector *bind_vec,
511 const struct GUID *object_guid)
513 return ep_register(NULL,
524 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */