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"
28 #define EPM_MAX_ANNOTATION_SIZE 64
30 NTSTATUS
dcerpc_binding_vector_create(TALLOC_CTX
*mem_ctx
,
31 const struct ndr_interface_table
*iface
,
34 struct dcerpc_binding_vector
**pbvec
)
36 struct dcerpc_binding_vector
*bvec
;
43 tmp_ctx
= talloc_stackframe();
44 if (tmp_ctx
== NULL
) {
45 return NT_STATUS_NO_MEMORY
;
48 ep_count
= iface
->endpoints
->count
;
50 bvec
= talloc_zero(tmp_ctx
, struct dcerpc_binding_vector
);
52 status
= NT_STATUS_NO_MEMORY
;
56 bvec
->bindings
= talloc_zero_array(bvec
, struct dcerpc_binding
, ep_count
);
57 if (bvec
->bindings
== NULL
) {
58 status
= NT_STATUS_NO_MEMORY
;
62 for (i
= 0; i
< ep_count
; i
++) {
63 struct dcerpc_binding
*b
;
65 b
= talloc_zero(bvec
->bindings
, struct dcerpc_binding
);
67 status
= NT_STATUS_NO_MEMORY
;
71 status
= dcerpc_parse_binding(b
, iface
->endpoints
->names
[i
], &b
);
72 if (!NT_STATUS_IS_OK(status
)) {
73 status
= NT_STATUS_UNSUCCESSFUL
;
77 b
->object
= iface
->syntax_id
;
79 switch (b
->transport
) {
81 b
->host
= talloc_asprintf(b
, "\\\\%s", global_myname());
82 if (b
->host
== NULL
) {
83 status
= NT_STATUS_NO_MEMORY
;
93 b
->endpoint
= talloc_asprintf(b
, "%u", port
);
94 if (b
->endpoint
== NULL
) {
95 status
= NT_STATUS_NO_MEMORY
;
101 if (ncalrpc
== NULL
) {
106 b
->endpoint
= talloc_asprintf(b
,
110 if (b
->endpoint
== NULL
) {
111 status
= NT_STATUS_NO_MEMORY
;
120 bvec
->bindings
[count
] = *b
;
126 *pbvec
= talloc_move(mem_ctx
, &bvec
);
128 status
= NT_STATUS_OK
;
130 talloc_free(tmp_ctx
);
135 static NTSTATUS
ep_register(TALLOC_CTX
*mem_ctx
,
136 const struct ndr_interface_table
*iface
,
137 const struct dcerpc_binding_vector
*bind_vec
,
138 const struct GUID
*object_guid
,
139 const char *annotation
,
142 struct dcerpc_binding_handle
**pbh
)
144 struct rpc_pipe_client
*cli
= NULL
;
145 struct dcerpc_binding_handle
*h
;
146 struct pipe_auth_data
*auth
;
147 const char *ncalrpc_sock
;
148 const char *rpcsrv_type
;
149 struct epm_entry_t
*entries
;
150 uint32_t num_ents
, i
;
152 uint32_t result
= EPMAPPER_STATUS_OK
;
156 return NT_STATUS_INVALID_PARAMETER
;
159 if (bind_vec
== NULL
|| bind_vec
->count
== 0) {
160 return NT_STATUS_INVALID_PARAMETER
;
163 tmp_ctx
= talloc_stackframe();
164 if (tmp_ctx
== NULL
) {
165 return NT_STATUS_NO_MEMORY
;
168 rpcsrv_type
= lp_parm_const_string(GLOBAL_SECTION_SNUM
,
169 "rpc_server", "epmapper",
172 if (strcasecmp_m(rpcsrv_type
, "embedded") == 0) {
173 static struct client_address client_id
;
175 strlcpy(client_id
.addr
, "localhost", sizeof(client_id
.addr
));
176 client_id
.name
= "localhost";
178 status
= rpcint_binding_handle(tmp_ctx
,
181 get_session_info_system(),
182 server_messaging_context(),
184 if (!NT_STATUS_IS_OK(status
)) {
185 DEBUG(1, ("dcerpc_ep_register: Could not connect to "
186 "epmapper (%s)", nt_errstr(status
)));
189 } else if (strcasecmp_m(rpcsrv_type
, "daemon") == 0) {
190 /* Connect to the endpoint mapper locally */
191 ncalrpc_sock
= talloc_asprintf(tmp_ctx
,
195 if (ncalrpc_sock
== NULL
) {
196 status
= NT_STATUS_NO_MEMORY
;
200 status
= rpc_pipe_open_ncalrpc(tmp_ctx
,
202 &ndr_table_epmapper
.syntax_id
,
204 if (!NT_STATUS_IS_OK(status
)) {
208 status
= rpccli_ncalrpc_bind_data(cli
, &auth
);
209 if (!NT_STATUS_IS_OK(status
)) {
210 DEBUG(0, ("Failed to initialize anonymous bind.\n"));
214 status
= rpc_pipe_bind(cli
, auth
);
215 if (!NT_STATUS_IS_OK(status
)) {
216 DEBUG(2, ("Failed to bind ncalrpc socket.\n"));
220 h
= cli
->binding_handle
;
222 status
= NT_STATUS_INVALID_PARAMETER
;
226 num_ents
= bind_vec
->count
;
227 entries
= talloc_array(tmp_ctx
, struct epm_entry_t
, num_ents
);
229 for (i
= 0; i
< num_ents
; i
++) {
230 struct dcerpc_binding
*map_binding
= &bind_vec
->bindings
[i
];
231 struct epm_twr_t
*map_tower
;
233 map_tower
= talloc_zero(entries
, struct epm_twr_t
);
234 if (map_tower
== NULL
) {
235 status
= NT_STATUS_NO_MEMORY
;
239 status
= dcerpc_binding_build_tower(entries
,
242 if (!NT_STATUS_IS_OK(status
)) {
246 entries
[i
].tower
= map_tower
;
247 if (annotation
== NULL
) {
248 entries
[i
].annotation
= talloc_strdup(entries
, "");
250 entries
[i
].annotation
= talloc_strndup(entries
,
252 EPM_MAX_ANNOTATION_SIZE
);
254 if (entries
[i
].annotation
== NULL
) {
255 status
= NT_STATUS_NO_MEMORY
;
259 if (object_guid
!= NULL
) {
260 entries
[i
].object
= *object_guid
;
262 entries
[i
].object
= map_binding
->object
.uuid
;
267 status
= dcerpc_epm_Delete(h
,
273 status
= dcerpc_epm_Insert(h
,
280 if (!NT_STATUS_IS_OK(status
)) {
281 DEBUG(0, ("dcerpc_ep_register: Could not insert tower (%s)\n",
285 if (result
!= EPMAPPER_STATUS_OK
) {
286 DEBUG(0, ("dcerpc_ep_register: Could not insert tower (0x%.8x)\n",
288 status
= NT_STATUS_UNSUCCESSFUL
;
293 *pbh
= talloc_move(mem_ctx
, &h
);
294 talloc_steal(*pbh
, cli
);
298 talloc_free(tmp_ctx
);
303 NTSTATUS
dcerpc_ep_register(TALLOC_CTX
*mem_ctx
,
304 const struct ndr_interface_table
*iface
,
305 const struct dcerpc_binding_vector
*bind_vec
,
306 const struct GUID
*object_guid
,
307 const char *annotation
,
308 struct dcerpc_binding_handle
**ph
)
310 return ep_register(mem_ctx
,
320 NTSTATUS
dcerpc_ep_register_noreplace(TALLOC_CTX
*mem_ctx
,
321 const struct ndr_interface_table
*iface
,
322 const struct dcerpc_binding_vector
*bind_vec
,
323 const struct GUID
*object_guid
,
324 const char *annotation
,
325 struct dcerpc_binding_handle
**ph
)
327 return ep_register(mem_ctx
,
337 NTSTATUS
dcerpc_ep_unregister(const struct ndr_interface_table
*iface
,
338 const struct dcerpc_binding_vector
*bind_vec
,
339 const struct GUID
*object_guid
)
341 return ep_register(NULL
,
351 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */