s3-librpc: Add dcerpc_binding_vector_add_np_default().
[Samba/bjacke.git] / source3 / librpc / rpc / dcerpc_ep.c
blobbd4e9ee0d90d77042f4595550a4ecefdf08e0364
1 /*
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/>.
20 #include "includes.h"
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"
25 #include "auth.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,
37 bvec->bindings,
38 struct dcerpc_binding,
39 bvec->allocated * 2);
40 if (tmp == NULL) {
41 return false;
43 bvec->bindings = tmp;
44 bvec->allocated = bvec->allocated * 2;
47 return true;
50 NTSTATUS dcerpc_binding_vector_new(TALLOC_CTX *mem_ctx,
51 struct dcerpc_binding_vector **pbvec)
53 struct dcerpc_binding_vector *bvec;
54 NTSTATUS status;
55 TALLOC_CTX *tmp_ctx;
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);
63 if (bvec == NULL) {
64 status = NT_STATUS_NO_MEMORY;
65 goto done;
68 bvec->bindings = talloc_zero_array(bvec,
69 struct dcerpc_binding,
70 4);
71 if (bvec->bindings == NULL) {
72 status = NT_STATUS_NO_MEMORY;
73 goto done;
76 bvec->allocated = 4;
77 bvec->count = 0;
79 *pbvec = talloc_move(mem_ctx, &bvec);
81 status = NT_STATUS_OK;
82 done:
83 talloc_free(tmp_ctx);
85 return status;
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;
92 uint32_t i;
93 NTSTATUS status;
94 bool ok;
96 for (i = 0; i < ep_count; i++) {
97 struct dcerpc_binding *b;
99 b = talloc_zero(bvec->bindings, struct dcerpc_binding);
100 if (b == NULL) {
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) {
111 talloc_free(b);
112 continue;
115 b->object = iface->syntax_id;
117 b->host = talloc_asprintf(b, "\\\\%s", lp_netbios_name());
118 if (b->host == NULL) {
119 talloc_free(b);
120 return NT_STATUS_NO_MEMORY;
123 ok = binding_vector_realloc(bvec);
124 if (!ok) {
125 talloc_free(b);
126 return NT_STATUS_NO_MEMORY;
129 bvec->bindings[bvec->count] = *b;
130 bvec->count++;
133 return NT_STATUS_OK;
136 NTSTATUS dcerpc_binding_vector_create(TALLOC_CTX *mem_ctx,
137 const struct ndr_interface_table *iface,
138 uint16_t port,
139 const char *ncalrpc,
140 struct dcerpc_binding_vector **pbvec)
142 struct dcerpc_binding_vector *bvec;
143 uint32_t ep_count;
144 uint32_t count = 0;
145 uint32_t i;
146 NTSTATUS status;
147 TALLOC_CTX *tmp_ctx;
149 tmp_ctx = talloc_stackframe();
150 if (tmp_ctx == NULL) {
151 return NT_STATUS_NO_MEMORY;
154 ep_count = iface->endpoints->count;
156 bvec = talloc_zero(tmp_ctx, struct dcerpc_binding_vector);
157 if (bvec == NULL) {
158 status = NT_STATUS_NO_MEMORY;
159 goto done;
162 bvec->bindings = talloc_zero_array(bvec, struct dcerpc_binding, ep_count);
163 if (bvec->bindings == NULL) {
164 status = NT_STATUS_NO_MEMORY;
165 goto done;
168 for (i = 0; i < ep_count; i++) {
169 struct dcerpc_binding *b;
171 b = talloc_zero(bvec->bindings, struct dcerpc_binding);
172 if (b == NULL) {
173 status = NT_STATUS_NO_MEMORY;
174 goto done;
177 status = dcerpc_parse_binding(b, iface->endpoints->names[i], &b);
178 if (!NT_STATUS_IS_OK(status)) {
179 status = NT_STATUS_UNSUCCESSFUL;
180 goto done;
183 b->object = iface->syntax_id;
185 switch (b->transport) {
186 case NCACN_NP:
187 b->host = talloc_asprintf(b, "\\\\%s", lp_netbios_name());
188 if (b->host == NULL) {
189 status = NT_STATUS_NO_MEMORY;
190 goto done;
192 break;
193 case NCACN_IP_TCP:
194 if (port == 0) {
195 talloc_free(b);
196 continue;
199 b->endpoint = talloc_asprintf(b, "%u", port);
200 if (b->endpoint == NULL) {
201 status = NT_STATUS_NO_MEMORY;
202 goto done;
205 break;
206 case NCALRPC:
207 if (ncalrpc == NULL) {
208 talloc_free(b);
209 continue;
212 b->endpoint = talloc_asprintf(b,
213 "%s/%s",
214 lp_ncalrpc_dir(),
215 ncalrpc);
216 if (b->endpoint == NULL) {
217 status = NT_STATUS_NO_MEMORY;
218 goto done;
220 break;
221 default:
222 talloc_free(b);
223 continue;
226 bvec->bindings[count] = *b;
227 count++;
230 bvec->count = count;
232 *pbvec = talloc_move(mem_ctx, &bvec);
234 status = NT_STATUS_OK;
235 done:
236 talloc_free(tmp_ctx);
238 return status;
241 static NTSTATUS ep_register(TALLOC_CTX *mem_ctx,
242 struct messaging_context *msg_ctx,
243 const struct ndr_interface_table *iface,
244 const struct dcerpc_binding_vector *bind_vec,
245 const struct GUID *object_guid,
246 const char *annotation,
247 uint32_t replace,
248 uint32_t unregister,
249 struct dcerpc_binding_handle **pbh)
251 struct rpc_pipe_client *cli = NULL;
252 struct dcerpc_binding_handle *h;
253 struct pipe_auth_data *auth;
254 const char *ncalrpc_sock;
255 const char *rpcsrv_type;
256 struct epm_entry_t *entries;
257 uint32_t num_ents, i;
258 TALLOC_CTX *tmp_ctx;
259 uint32_t result = EPMAPPER_STATUS_OK;
260 NTSTATUS status;
262 if (iface == NULL) {
263 return NT_STATUS_INVALID_PARAMETER;
266 if (bind_vec == NULL || bind_vec->count == 0) {
267 return NT_STATUS_INVALID_PARAMETER;
270 tmp_ctx = talloc_stackframe();
271 if (tmp_ctx == NULL) {
272 return NT_STATUS_NO_MEMORY;
275 rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
276 "rpc_server", "epmapper",
277 "none");
279 if (strcasecmp_m(rpcsrv_type, "embedded") == 0) {
280 struct tsocket_address *local;
281 int rc;
283 rc = tsocket_address_inet_from_strings(tmp_ctx,
284 "ip",
285 "127.0.0.1",
287 &local);
288 if (rc < 0) {
289 return NT_STATUS_NO_MEMORY;
292 status = rpcint_binding_handle(tmp_ctx,
293 &ndr_table_epmapper,
294 local,
295 get_session_info_system(),
296 msg_ctx,
297 &h);
298 if (!NT_STATUS_IS_OK(status)) {
299 DEBUG(1, ("dcerpc_ep_register: Could not connect to "
300 "epmapper (%s)", nt_errstr(status)));
301 goto done;
303 } else if (strcasecmp_m(rpcsrv_type, "daemon") == 0) {
304 /* Connect to the endpoint mapper locally */
305 ncalrpc_sock = talloc_asprintf(tmp_ctx,
306 "%s/%s",
307 lp_ncalrpc_dir(),
308 "EPMAPPER");
309 if (ncalrpc_sock == NULL) {
310 status = NT_STATUS_NO_MEMORY;
311 goto done;
314 status = rpc_pipe_open_ncalrpc(tmp_ctx,
315 ncalrpc_sock,
316 &ndr_table_epmapper.syntax_id,
317 &cli);
318 if (!NT_STATUS_IS_OK(status)) {
319 goto done;
322 status = rpccli_ncalrpc_bind_data(cli, &auth);
323 if (!NT_STATUS_IS_OK(status)) {
324 DEBUG(0, ("Failed to initialize anonymous bind.\n"));
325 goto done;
328 status = rpc_pipe_bind(cli, auth);
329 if (!NT_STATUS_IS_OK(status)) {
330 DEBUG(2, ("Failed to bind ncalrpc socket.\n"));
331 goto done;
334 h = cli->binding_handle;
335 } else {
336 status = NT_STATUS_INVALID_PARAMETER;
337 goto done;
340 num_ents = bind_vec->count;
341 entries = talloc_array(tmp_ctx, struct epm_entry_t, num_ents);
343 for (i = 0; i < num_ents; i++) {
344 struct dcerpc_binding *map_binding = &bind_vec->bindings[i];
345 struct epm_twr_t *map_tower;
347 map_tower = talloc_zero(entries, struct epm_twr_t);
348 if (map_tower == NULL) {
349 status = NT_STATUS_NO_MEMORY;
350 goto done;
353 status = dcerpc_binding_build_tower(entries,
354 map_binding,
355 &map_tower->tower);
356 if (!NT_STATUS_IS_OK(status)) {
357 goto done;
360 entries[i].tower = map_tower;
361 if (annotation == NULL) {
362 entries[i].annotation = talloc_strdup(entries, "");
363 } else {
364 entries[i].annotation = talloc_strndup(entries,
365 annotation,
366 EPM_MAX_ANNOTATION_SIZE);
368 if (entries[i].annotation == NULL) {
369 status = NT_STATUS_NO_MEMORY;
370 goto done;
373 if (object_guid != NULL) {
374 entries[i].object = *object_guid;
375 } else {
376 entries[i].object = map_binding->object.uuid;
380 if (unregister) {
381 status = dcerpc_epm_Delete(h,
382 tmp_ctx,
383 num_ents,
384 entries,
385 &result);
386 } else {
387 status = dcerpc_epm_Insert(h,
388 tmp_ctx,
389 num_ents,
390 entries,
391 replace,
392 &result);
394 if (!NT_STATUS_IS_OK(status)) {
395 DEBUG(0, ("dcerpc_ep_register: Could not insert tower (%s)\n",
396 nt_errstr(status)));
397 goto done;
399 if (result != EPMAPPER_STATUS_OK) {
400 DEBUG(0, ("dcerpc_ep_register: Could not insert tower (0x%.8x)\n",
401 result));
402 status = NT_STATUS_UNSUCCESSFUL;
403 goto done;
406 if (pbh != NULL) {
407 *pbh = talloc_move(mem_ctx, &h);
408 talloc_steal(*pbh, cli);
411 done:
412 talloc_free(tmp_ctx);
414 return status;
417 NTSTATUS dcerpc_ep_register(TALLOC_CTX *mem_ctx,
418 struct messaging_context *msg_ctx,
419 const struct ndr_interface_table *iface,
420 const struct dcerpc_binding_vector *bind_vec,
421 const struct GUID *object_guid,
422 const char *annotation,
423 struct dcerpc_binding_handle **ph)
425 return ep_register(mem_ctx,
426 msg_ctx,
427 iface,
428 bind_vec,
429 object_guid,
430 annotation,
433 ph);
436 NTSTATUS dcerpc_ep_register_noreplace(TALLOC_CTX *mem_ctx,
437 struct messaging_context *msg_ctx,
438 const struct ndr_interface_table *iface,
439 const struct dcerpc_binding_vector *bind_vec,
440 const struct GUID *object_guid,
441 const char *annotation,
442 struct dcerpc_binding_handle **ph)
444 return ep_register(mem_ctx,
445 msg_ctx,
446 iface,
447 bind_vec,
448 object_guid,
449 annotation,
452 ph);
455 NTSTATUS dcerpc_ep_unregister(struct messaging_context *msg_ctx,
456 const struct ndr_interface_table *iface,
457 const struct dcerpc_binding_vector *bind_vec,
458 const struct GUID *object_guid)
460 return ep_register(NULL,
461 msg_ctx,
462 iface,
463 bind_vec,
464 object_guid,
465 NULL,
468 NULL);
471 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */