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"
27 #define EPM_MAX_ANNOTATION_SIZE 64
29 NTSTATUS dcerpc_binding_vector_create(TALLOC_CTX *mem_ctx,
30 const struct ndr_interface_table *iface,
33 struct dcerpc_binding_vector **pbvec)
35 struct dcerpc_binding_vector *bvec;
42 tmp_ctx = talloc_stackframe();
43 if (tmp_ctx == NULL) {
44 return NT_STATUS_NO_MEMORY;
47 ep_count = iface->endpoints->count;
49 bvec = talloc_zero(tmp_ctx, struct dcerpc_binding_vector);
51 status = NT_STATUS_NO_MEMORY;
55 bvec->bindings = talloc_zero_array(bvec, struct dcerpc_binding, ep_count);
56 if (bvec->bindings == NULL) {
57 status = NT_STATUS_NO_MEMORY;
61 for (i = 0; i < ep_count; i++) {
62 struct dcerpc_binding *b;
64 b = talloc_zero(bvec->bindings, struct dcerpc_binding);
66 status = NT_STATUS_NO_MEMORY;
70 status = dcerpc_parse_binding(b, iface->endpoints->names[i], &b);
71 if (!NT_STATUS_IS_OK(status)) {
72 status = NT_STATUS_UNSUCCESSFUL;
76 b->object = iface->syntax_id;
78 switch (b->transport) {
80 b->host = talloc_asprintf(b, "\\\\%s", global_myname());
81 if (b->host == NULL) {
82 status = NT_STATUS_NO_MEMORY;
92 b->endpoint = talloc_asprintf(b, "%u", port);
93 if (b->endpoint == NULL) {
94 status = NT_STATUS_NO_MEMORY;
100 if (ncalrpc == NULL) {
105 b->endpoint = talloc_asprintf(b,
109 if (b->endpoint == NULL) {
110 status = NT_STATUS_NO_MEMORY;
119 bvec->bindings[count] = *b;
125 *pbvec = talloc_move(mem_ctx, &bvec);
127 status = NT_STATUS_OK;
129 talloc_free(tmp_ctx);
134 static NTSTATUS ep_register(TALLOC_CTX *mem_ctx,
135 const struct ndr_interface_table *iface,
136 const struct dcerpc_binding_vector *bind_vec,
137 const struct GUID *object_guid,
138 const char *annotation,
141 struct dcerpc_binding_handle **pbh)
143 struct rpc_pipe_client *cli = NULL;
144 struct dcerpc_binding_handle *h;
145 struct pipe_auth_data *auth;
146 const char *ncalrpc_sock;
147 const char *rpcsrv_type;
148 struct epm_entry_t *entries;
149 uint32_t num_ents, i;
151 uint32_t result = EPMAPPER_STATUS_OK;
155 return NT_STATUS_INVALID_PARAMETER;
158 if (bind_vec == NULL || bind_vec->count == 0) {
159 return NT_STATUS_INVALID_PARAMETER;
162 tmp_ctx = talloc_stackframe();
163 if (tmp_ctx == NULL) {
164 return NT_STATUS_NO_MEMORY;
167 rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
168 "rpc_server", "epmapper",
171 if (StrCaseCmp(rpcsrv_type, "embedded") == 0) {
172 static struct client_address client_id;
174 strlcpy(client_id.addr, "localhost", sizeof(client_id.addr));
175 client_id.name = "localhost";
177 status = rpcint_binding_handle(tmp_ctx,
180 get_session_info_system(),
181 server_messaging_context(),
183 if (!NT_STATUS_IS_OK(status)) {
184 DEBUG(1, ("dcerpc_ep_register: Could not connect to "
185 "epmapper (%s)", nt_errstr(status)));
188 } else if (StrCaseCmp(rpcsrv_type, "daemon") == 0) {
189 /* Connect to the endpoint mapper locally */
190 ncalrpc_sock = talloc_asprintf(tmp_ctx,
194 if (ncalrpc_sock == NULL) {
195 status = NT_STATUS_NO_MEMORY;
199 status = rpc_pipe_open_ncalrpc(tmp_ctx,
201 &ndr_table_epmapper.syntax_id,
203 if (!NT_STATUS_IS_OK(status)) {
207 status = rpccli_ncalrpc_bind_data(cli, &auth);
208 if (!NT_STATUS_IS_OK(status)) {
209 DEBUG(0, ("Failed to initialize anonymous bind.\n"));
213 status = rpc_pipe_bind(cli, auth);
214 if (!NT_STATUS_IS_OK(status)) {
215 DEBUG(2, ("Failed to bind ncalrpc socket.\n"));
219 h = cli->binding_handle;
221 status = NT_STATUS_INVALID_PARAMETER;
225 num_ents = bind_vec->count;
226 entries = talloc_array(tmp_ctx, struct epm_entry_t, num_ents);
228 for (i = 0; i < num_ents; i++) {
229 struct dcerpc_binding *map_binding = &bind_vec->bindings[i];
230 struct epm_twr_t *map_tower;
232 map_tower = talloc_zero(entries, struct epm_twr_t);
233 if (map_tower == NULL) {
234 status = NT_STATUS_NO_MEMORY;
238 status = dcerpc_binding_build_tower(entries,
241 if (!NT_STATUS_IS_OK(status)) {
245 entries[i].tower = map_tower;
246 if (annotation == NULL) {
247 entries[i].annotation = talloc_strdup(entries, "");
249 entries[i].annotation = talloc_strndup(entries,
251 EPM_MAX_ANNOTATION_SIZE);
253 if (entries[i].annotation == NULL) {
254 status = NT_STATUS_NO_MEMORY;
258 if (object_guid != NULL) {
259 entries[i].object = *object_guid;
261 entries[i].object = map_binding->object.uuid;
266 status = dcerpc_epm_Delete(h,
272 status = dcerpc_epm_Insert(h,
279 if (!NT_STATUS_IS_OK(status)) {
280 DEBUG(0, ("dcerpc_ep_register: Could not insert tower (%s)\n",
284 if (result != EPMAPPER_STATUS_OK) {
285 DEBUG(0, ("dcerpc_ep_register: Could not insert tower (0x%.8x)\n",
287 status = NT_STATUS_UNSUCCESSFUL;
292 *pbh = talloc_move(mem_ctx, &h);
293 talloc_steal(*pbh, cli);
297 talloc_free(tmp_ctx);
302 NTSTATUS dcerpc_ep_register(TALLOC_CTX *mem_ctx,
303 const struct ndr_interface_table *iface,
304 const struct dcerpc_binding_vector *bind_vec,
305 const struct GUID *object_guid,
306 const char *annotation,
307 struct dcerpc_binding_handle **ph)
309 return ep_register(mem_ctx,
319 NTSTATUS dcerpc_ep_register_noreplace(TALLOC_CTX *mem_ctx,
320 const struct ndr_interface_table *iface,
321 const struct dcerpc_binding_vector *bind_vec,
322 const struct GUID *object_guid,
323 const char *annotation,
324 struct dcerpc_binding_handle **ph)
326 return ep_register(mem_ctx,
336 NTSTATUS dcerpc_ep_unregister(const struct ndr_interface_table *iface,
337 const struct dcerpc_binding_vector *bind_vec,
338 const struct GUID *object_guid)
340 return ep_register(NULL,
350 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */