2 * Endpoint Mapper Functions
3 * DCERPC local endpoint mapper client routines
4 * Copyright (c) 2010 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 NTSTATUS
dcerpc_binding_vector_create(TALLOC_CTX
*mem_ctx
,
32 const struct ndr_interface_table
*iface
,
35 struct dcerpc_binding_vector
**pbvec
)
37 struct dcerpc_binding_vector
*bvec
;
44 tmp_ctx
= talloc_stackframe();
45 if (tmp_ctx
== NULL
) {
46 return NT_STATUS_NO_MEMORY
;
49 ep_count
= iface
->endpoints
->count
;
51 bvec
= talloc_zero(tmp_ctx
, struct dcerpc_binding_vector
);
53 status
= NT_STATUS_NO_MEMORY
;
57 bvec
->bindings
= talloc_zero_array(bvec
, struct dcerpc_binding
, ep_count
);
58 if (bvec
->bindings
== NULL
) {
59 status
= NT_STATUS_NO_MEMORY
;
63 for (i
= 0; i
< ep_count
; i
++) {
64 struct dcerpc_binding
*b
;
66 b
= talloc_zero(bvec
->bindings
, struct dcerpc_binding
);
68 status
= NT_STATUS_NO_MEMORY
;
72 status
= dcerpc_parse_binding(b
, iface
->endpoints
->names
[i
], &b
);
73 if (!NT_STATUS_IS_OK(status
)) {
74 status
= NT_STATUS_UNSUCCESSFUL
;
78 b
->object
= iface
->syntax_id
;
80 switch (b
->transport
) {
82 b
->host
= talloc_asprintf(b
, "\\\\%s", lp_netbios_name());
83 if (b
->host
== NULL
) {
84 status
= NT_STATUS_NO_MEMORY
;
94 b
->endpoint
= talloc_asprintf(b
, "%u", port
);
95 if (b
->endpoint
== NULL
) {
96 status
= NT_STATUS_NO_MEMORY
;
102 if (ncalrpc
== NULL
) {
107 b
->endpoint
= talloc_asprintf(b
,
111 if (b
->endpoint
== NULL
) {
112 status
= NT_STATUS_NO_MEMORY
;
121 bvec
->bindings
[count
] = *b
;
127 *pbvec
= talloc_move(mem_ctx
, &bvec
);
129 status
= NT_STATUS_OK
;
131 talloc_free(tmp_ctx
);
136 static NTSTATUS
ep_register(TALLOC_CTX
*mem_ctx
,
137 const struct ndr_interface_table
*iface
,
138 const struct dcerpc_binding_vector
*bind_vec
,
139 const struct GUID
*object_guid
,
140 const char *annotation
,
143 struct dcerpc_binding_handle
**pbh
)
145 struct rpc_pipe_client
*cli
= NULL
;
146 struct dcerpc_binding_handle
*h
;
147 struct pipe_auth_data
*auth
;
148 const char *ncalrpc_sock
;
149 const char *rpcsrv_type
;
150 struct epm_entry_t
*entries
;
151 uint32_t num_ents
, i
;
153 uint32_t result
= EPMAPPER_STATUS_OK
;
157 return NT_STATUS_INVALID_PARAMETER
;
160 if (bind_vec
== NULL
|| bind_vec
->count
== 0) {
161 return NT_STATUS_INVALID_PARAMETER
;
164 tmp_ctx
= talloc_stackframe();
165 if (tmp_ctx
== NULL
) {
166 return NT_STATUS_NO_MEMORY
;
169 rpcsrv_type
= lp_parm_const_string(GLOBAL_SECTION_SNUM
,
170 "rpc_server", "epmapper",
173 if (strcasecmp_m(rpcsrv_type
, "embedded") == 0) {
174 struct tsocket_address
*local
;
177 rc
= tsocket_address_inet_from_strings(tmp_ctx
,
183 return NT_STATUS_NO_MEMORY
;
186 status
= rpcint_binding_handle(tmp_ctx
,
189 get_session_info_system(),
190 server_messaging_context(),
192 if (!NT_STATUS_IS_OK(status
)) {
193 DEBUG(1, ("dcerpc_ep_register: Could not connect to "
194 "epmapper (%s)", nt_errstr(status
)));
197 } else if (strcasecmp_m(rpcsrv_type
, "daemon") == 0) {
198 /* Connect to the endpoint mapper locally */
199 ncalrpc_sock
= talloc_asprintf(tmp_ctx
,
203 if (ncalrpc_sock
== NULL
) {
204 status
= NT_STATUS_NO_MEMORY
;
208 status
= rpc_pipe_open_ncalrpc(tmp_ctx
,
210 &ndr_table_epmapper
.syntax_id
,
212 if (!NT_STATUS_IS_OK(status
)) {
216 status
= rpccli_ncalrpc_bind_data(cli
, &auth
);
217 if (!NT_STATUS_IS_OK(status
)) {
218 DEBUG(0, ("Failed to initialize anonymous bind.\n"));
222 status
= rpc_pipe_bind(cli
, auth
);
223 if (!NT_STATUS_IS_OK(status
)) {
224 DEBUG(2, ("Failed to bind ncalrpc socket.\n"));
228 h
= cli
->binding_handle
;
230 status
= NT_STATUS_INVALID_PARAMETER
;
234 num_ents
= bind_vec
->count
;
235 entries
= talloc_array(tmp_ctx
, struct epm_entry_t
, num_ents
);
237 for (i
= 0; i
< num_ents
; i
++) {
238 struct dcerpc_binding
*map_binding
= &bind_vec
->bindings
[i
];
239 struct epm_twr_t
*map_tower
;
241 map_tower
= talloc_zero(entries
, struct epm_twr_t
);
242 if (map_tower
== NULL
) {
243 status
= NT_STATUS_NO_MEMORY
;
247 status
= dcerpc_binding_build_tower(entries
,
250 if (!NT_STATUS_IS_OK(status
)) {
254 entries
[i
].tower
= map_tower
;
255 if (annotation
== NULL
) {
256 entries
[i
].annotation
= talloc_strdup(entries
, "");
258 entries
[i
].annotation
= talloc_strndup(entries
,
260 EPM_MAX_ANNOTATION_SIZE
);
262 if (entries
[i
].annotation
== NULL
) {
263 status
= NT_STATUS_NO_MEMORY
;
267 if (object_guid
!= NULL
) {
268 entries
[i
].object
= *object_guid
;
270 entries
[i
].object
= map_binding
->object
.uuid
;
275 status
= dcerpc_epm_Delete(h
,
281 status
= dcerpc_epm_Insert(h
,
288 if (!NT_STATUS_IS_OK(status
)) {
289 DEBUG(0, ("dcerpc_ep_register: Could not insert tower (%s)\n",
293 if (result
!= EPMAPPER_STATUS_OK
) {
294 DEBUG(0, ("dcerpc_ep_register: Could not insert tower (0x%.8x)\n",
296 status
= NT_STATUS_UNSUCCESSFUL
;
301 *pbh
= talloc_move(mem_ctx
, &h
);
302 talloc_steal(*pbh
, cli
);
306 talloc_free(tmp_ctx
);
311 NTSTATUS
dcerpc_ep_register(TALLOC_CTX
*mem_ctx
,
312 const struct ndr_interface_table
*iface
,
313 const struct dcerpc_binding_vector
*bind_vec
,
314 const struct GUID
*object_guid
,
315 const char *annotation
,
316 struct dcerpc_binding_handle
**ph
)
318 return ep_register(mem_ctx
,
328 NTSTATUS
dcerpc_ep_register_noreplace(TALLOC_CTX
*mem_ctx
,
329 const struct ndr_interface_table
*iface
,
330 const struct dcerpc_binding_vector
*bind_vec
,
331 const struct GUID
*object_guid
,
332 const char *annotation
,
333 struct dcerpc_binding_handle
**ph
)
335 return ep_register(mem_ctx
,
345 NTSTATUS
dcerpc_ep_unregister(const struct ndr_interface_table
*iface
,
346 const struct dcerpc_binding_vector
*bind_vec
,
347 const struct GUID
*object_guid
)
349 return ep_register(NULL
,
359 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */