VERSION: Bump version number up to 3.6.18.
[Samba.git] / source3 / rpc_server / epmapper / srv_epmapper.c
blobefa9b6f0df40ff6699026318d77d6e862d5d6a9e
1 /*
2 Unix SMB/CIFS implementation.
4 Endpoint server for the epmapper pipe
6 Copyright (C) 2010-2011 Andreas Schneider <asn@samba.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "ntdomain.h"
24 #include "../libcli/security/security.h"
25 #include "librpc/gen_ndr/srv_epmapper.h"
26 #include "srv_epmapper.h"
27 #include "auth.h"
29 typedef uint32_t error_status_t;
31 /* An endpoint combined with an interface description */
32 struct dcesrv_ep_iface {
33 const char *name;
34 struct ndr_syntax_id syntax_id;
35 struct epm_tower ep;
38 /* A rpc service interface like samr, lsarpc or netlogon */
39 struct dcesrv_iface {
40 const char *name;
41 struct ndr_syntax_id syntax_id;
44 struct dcesrv_iface_list {
45 struct dcesrv_iface_list *next, *prev;
46 struct dcesrv_iface *iface;
50 * An endpoint can serve multiple rpc services interfaces.
51 * For example \\pipe\netlogon can be used by lsarpc and netlogon.
53 struct dcesrv_endpoint {
54 struct dcesrv_endpoint *next, *prev;
56 /* The type and the location of the endpoint */
57 struct dcerpc_binding *ep_description;
59 /* A list of rpc services able to connect to the endpoint */
60 struct dcesrv_iface_list *iface_list;
63 struct dcesrv_ep_entry_list {
64 struct dcesrv_ep_entry_list *next, *prev;
66 uint32_t num_ents;
67 struct epm_entry_t *entries;
70 struct rpc_eps {
71 struct dcesrv_ep_iface *e;
72 uint32_t count;
75 static struct dcesrv_endpoint *endpoint_table;
78 * Check if the UUID and if_version match to an interface.
80 static bool interface_match(const struct dcesrv_iface *if1,
81 const struct dcesrv_iface *if2)
83 return GUID_equal(&if1->syntax_id.uuid, &if2->syntax_id.uuid);
87 * Find the interface operations on an endpoint.
89 static const struct dcesrv_iface *find_interface(const struct dcesrv_endpoint *endpoint,
90 const struct dcesrv_iface *iface)
92 struct dcesrv_iface_list *iflist;
94 for (iflist = endpoint->iface_list; iflist; iflist = iflist->next) {
95 if (interface_match(iflist->iface, iface)) {
96 return iflist->iface;
100 return NULL;
104 * See if a uuid and if_version match to an interface
106 static bool interface_match_by_uuid(const struct dcesrv_iface *iface,
107 const struct GUID *uuid)
109 return GUID_equal(&iface->syntax_id.uuid, uuid);
112 static struct dcesrv_iface_list *find_interface_list(const struct dcesrv_endpoint *endpoint,
113 const struct dcesrv_iface *iface)
115 struct dcesrv_iface_list *iflist;
117 for (iflist = endpoint->iface_list; iflist; iflist = iflist->next) {
118 if (interface_match(iflist->iface, iface)) {
119 return iflist;
123 return NULL;
127 * Check if two endpoints match.
129 static bool endpoints_match(const struct dcerpc_binding *ep1,
130 const struct dcerpc_binding *ep2)
132 if (ep1->transport != ep2->transport) {
133 return false;
136 if (!ep1->endpoint || !ep2->endpoint) {
137 return ep1->endpoint == ep2->endpoint;
140 if (!strequal(ep1->endpoint, ep2->endpoint)) {
141 return false;
144 return true;
147 static struct dcesrv_endpoint *find_endpoint(struct dcesrv_endpoint *endpoint_list,
148 struct dcerpc_binding *ep_description) {
149 struct dcesrv_endpoint *ep;
151 for (ep = endpoint_list; ep != NULL; ep = ep->next) {
152 if (endpoints_match(ep->ep_description, ep_description)) {
153 return ep;
157 return NULL;
161 * Build a list of all interfaces handled by all endpoint servers.
163 static uint32_t build_ep_list(TALLOC_CTX *mem_ctx,
164 struct dcesrv_endpoint *endpoint_list,
165 const struct GUID *uuid,
166 const char *srv_addr,
167 struct dcesrv_ep_iface **peps)
169 struct dcesrv_ep_iface *eps = NULL;
170 struct dcesrv_endpoint *d;
171 uint32_t total = 0;
172 NTSTATUS status;
174 *peps = NULL;
176 for (d = endpoint_list; d != NULL; d = d->next) {
177 struct dcesrv_iface_list *iface;
178 struct dcerpc_binding *description;
180 for (iface = d->iface_list; iface != NULL; iface = iface->next) {
181 if (uuid && !interface_match_by_uuid(iface->iface, uuid)) {
182 continue;
185 eps = talloc_realloc(mem_ctx,
186 eps,
187 struct dcesrv_ep_iface,
188 total + 1);
189 if (eps == NULL) {
190 return 0;
192 eps[total].name = talloc_strdup(eps,
193 iface->iface->name);
194 eps[total].syntax_id = iface->iface->syntax_id;
196 description = dcerpc_binding_dup(mem_ctx, d->ep_description);
197 if (description == NULL) {
198 return 0;
200 description->object = iface->iface->syntax_id;
201 if (description->transport == NCACN_IP_TCP &&
202 srv_addr != NULL &&
203 (strcmp(description->host, "0.0.0.0") == 0 ||
204 strcmp(description->host, "::") == 0)) {
205 description->host = srv_addr;
208 status = dcerpc_binding_build_tower(eps,
209 description,
210 &eps[total].ep);
211 TALLOC_FREE(description);
212 if (NT_STATUS_IS_ERR(status)) {
213 DEBUG(1, ("Unable to build tower for %s\n",
214 iface->iface->name));
215 continue;
217 total++;
221 *peps = eps;
223 return total;
226 static bool is_priviledged_pipe(struct auth_serversupplied_info *info) {
227 /* If the user is not root, or has the system token, fail */
228 if ((info->utok.uid != sec_initial_uid()) &&
229 !security_token_is_system(info->security_token)) {
230 return false;
233 return true;
236 bool srv_epmapper_delete_endpoints(struct pipes_struct *p)
238 struct epm_Delete r;
239 struct dcesrv_ep_entry_list *el = p->ep_entries;
240 error_status_t result;
242 while (el) {
243 struct dcesrv_ep_entry_list *next = el->next;
245 r.in.num_ents = el->num_ents;
246 r.in.entries = el->entries;
248 DEBUG(10, ("Delete_endpoints for: %s\n",
249 el->entries[0].annotation));
251 result = _epm_Delete(p, &r);
252 if (result != EPMAPPER_STATUS_OK) {
253 return false;
256 DLIST_REMOVE(p->ep_entries, el);
257 TALLOC_FREE(el);
259 el = next;
262 return true;
265 void srv_epmapper_cleanup(void)
267 struct dcesrv_endpoint *ep = endpoint_table;
269 while (ep) {
270 struct dcesrv_endpoint *next = ep->next;
272 DLIST_REMOVE(endpoint_table, ep);
273 TALLOC_FREE(ep);
275 ep = next;
280 * epm_Insert
282 * Add the specified entries to an endpoint map.
284 error_status_t _epm_Insert(struct pipes_struct *p,
285 struct epm_Insert *r)
287 TALLOC_CTX *tmp_ctx;
288 error_status_t rc;
289 NTSTATUS status;
290 uint32_t i;
291 struct dcerpc_binding *b;
292 struct dcesrv_endpoint *ep;
293 struct dcesrv_iface_list *iflist;
294 struct dcesrv_iface *iface;
295 bool add_ep;
297 /* If this is not a priviledged users, return */
298 if (p->transport != NCALRPC ||
299 !is_priviledged_pipe(p->session_info)) {
300 return EPMAPPER_STATUS_CANT_PERFORM_OP;
303 tmp_ctx = talloc_stackframe();
304 if (tmp_ctx == NULL) {
305 return EPMAPPER_STATUS_NO_MEMORY;
308 DEBUG(3, ("_epm_Insert: Trying to add %u new entries.\n",
309 r->in.num_ents));
311 for (i = 0; i < r->in.num_ents; i++) {
312 add_ep = false;
313 b = NULL;
315 status = dcerpc_binding_from_tower(tmp_ctx,
316 &r->in.entries[i].tower->tower,
317 &b);
318 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
319 rc = EPMAPPER_STATUS_NO_MEMORY;
320 goto done;
322 if (!NT_STATUS_IS_OK(status)) {
323 rc = EPMAPPER_STATUS_CANT_PERFORM_OP;
324 goto done;
327 DEBUG(3, ("_epm_Insert: Adding transport %s for %s\n",
328 derpc_transport_string_by_transport(b->transport),
329 r->in.entries[i].annotation));
331 /* Check if the entry already exits */
332 ep = find_endpoint(endpoint_table, b);
333 if (ep == NULL) {
334 /* No entry found, create it */
335 ep = talloc_zero(NULL, struct dcesrv_endpoint);
336 if (ep == NULL) {
337 rc = EPMAPPER_STATUS_NO_MEMORY;
338 goto done;
340 add_ep = true;
342 ep->ep_description = talloc_steal(ep, b);
345 /* TODO Replace the entry if the replace flag is set */
347 /* Create an interface */
348 iface = talloc(tmp_ctx, struct dcesrv_iface);
349 if (iface == NULL) {
350 rc = EPMAPPER_STATUS_NO_MEMORY;
351 goto done;
354 iface->name = talloc_strdup(iface, r->in.entries[i].annotation);
355 if (iface->name == NULL) {
356 rc = EPMAPPER_STATUS_NO_MEMORY;
357 goto done;
359 iface->syntax_id = b->object;
362 * Check if the rpc service is alrady registered on the
363 * endpoint.
365 if (find_interface(ep, iface) != NULL) {
366 DEBUG(0, ("dcesrv_interface_register: interface '%s' "
367 "already registered on endpoint\n",
368 iface->name));
369 /* FIXME wrong error code? */
370 rc = EPMAPPER_STATUS_OK;
371 goto done;
374 /* Create an entry for the interface */
375 iflist = talloc(ep, struct dcesrv_iface_list);
376 if (iflist == NULL) {
377 rc = EPMAPPER_STATUS_NO_MEMORY;
378 goto done;
380 iflist->iface = talloc_move(iflist, &iface);
382 /* Finally add the interface on the endpoint */
383 DLIST_ADD(ep->iface_list, iflist);
385 /* If it's a new endpoint add it to the endpoint_table */
386 if (add_ep) {
387 DLIST_ADD(endpoint_table, ep);
391 if (r->in.num_ents > 0) {
392 struct dcesrv_ep_entry_list *el;
394 el = talloc_zero(p, struct dcesrv_ep_entry_list);
395 if (el == NULL) {
396 rc = EPMAPPER_STATUS_NO_MEMORY;
397 goto done;
399 el->num_ents = r->in.num_ents;
400 el->entries = talloc_move(el, &r->in.entries);
402 DLIST_ADD(p->ep_entries, el);
405 rc = EPMAPPER_STATUS_OK;
406 done:
407 talloc_free(tmp_ctx);
409 return rc;
414 * epm_Delete
416 * Delete the specified entries from an endpoint map.
418 error_status_t _epm_Delete(struct pipes_struct *p,
419 struct epm_Delete *r)
421 TALLOC_CTX *tmp_ctx;
422 error_status_t rc;
423 NTSTATUS status;
424 uint32_t i;
425 struct dcerpc_binding *b;
426 struct dcesrv_endpoint *ep;
427 struct dcesrv_iface iface;
428 struct dcesrv_iface_list *iflist;
430 DEBUG(3, ("_epm_Delete: Trying to delete %u entries.\n",
431 r->in.num_ents));
433 /* If this is not a priviledged users, return */
434 if (p->transport != NCALRPC ||
435 !is_priviledged_pipe(p->session_info)) {
436 return EPMAPPER_STATUS_CANT_PERFORM_OP;
439 tmp_ctx = talloc_stackframe();
440 if (tmp_ctx == NULL) {
441 return EPMAPPER_STATUS_NO_MEMORY;
444 for (i = 0; i < r->in.num_ents; i++) {
445 b = NULL;
447 status = dcerpc_binding_from_tower(tmp_ctx,
448 &r->in.entries[i].tower->tower,
449 &b);
450 if (!NT_STATUS_IS_OK(status)) {
451 rc = EPMAPPER_STATUS_NO_MEMORY;
452 goto done;
455 DEBUG(3, ("_epm_Delete: Deleting transport '%s' for '%s'\n",
456 derpc_transport_string_by_transport(b->transport),
457 r->in.entries[i].annotation));
459 ep = find_endpoint(endpoint_table, b);
460 if (ep == NULL) {
461 rc = EPMAPPER_STATUS_OK;
462 goto done;
465 iface.name = r->in.entries[i].annotation;
466 iface.syntax_id = b->object;
468 iflist = find_interface_list(ep, &iface);
469 if (iflist == NULL) {
470 DEBUG(0, ("_epm_Delete: No interfaces left, delete endpoint\n"));
471 DLIST_REMOVE(endpoint_table, ep);
472 talloc_free(ep);
474 rc = EPMAPPER_STATUS_OK;
475 goto done;
478 DLIST_REMOVE(ep->iface_list, iflist);
480 if (ep->iface_list == NULL) {
481 DEBUG(0, ("_epm_Delete: No interfaces left, delete endpoint\n"));
482 DLIST_REMOVE(endpoint_table, ep);
483 talloc_free(ep);
485 rc = EPMAPPER_STATUS_OK;
486 goto done;
491 rc = EPMAPPER_STATUS_OK;
492 done:
493 talloc_free(tmp_ctx);
495 return rc;
500 * epm_Lookup
502 * Lookup entries in an endpoint map.
504 error_status_t _epm_Lookup(struct pipes_struct *p,
505 struct epm_Lookup *r)
507 struct policy_handle *entry_handle;
508 struct rpc_eps *eps;
509 TALLOC_CTX *tmp_ctx;
510 error_status_t rc;
511 uint32_t count = 0;
512 uint32_t num_ents = 0;
513 uint32_t i;
514 bool match = false;
515 bool ok;
517 *r->out.num_ents = 0;
518 r->out.entries = NULL;
520 tmp_ctx = talloc_stackframe();
521 if (tmp_ctx == NULL) {
522 return EPMAPPER_STATUS_NO_MEMORY;
525 DEBUG(3, ("_epm_Lookup: Trying to lookup max. %u entries.\n",
526 r->in.max_ents));
528 if (r->in.entry_handle == NULL ||
529 policy_handle_empty(r->in.entry_handle)) {
530 struct GUID *obj;
532 DEBUG(5, ("_epm_Lookup: No entry_handle found, creating it.\n"));
534 eps = talloc_zero(tmp_ctx, struct rpc_eps);
535 if (eps == NULL) {
536 rc = EPMAPPER_STATUS_NO_MEMORY;
537 goto done;
540 if (r->in.object == NULL || GUID_all_zero(r->in.object)) {
541 obj = NULL;
542 } else {
543 obj = r->in.object;
546 switch (r->in.inquiry_type) {
547 case RPC_C_EP_ALL_ELTS:
549 * Return all elements from the endpoint map. The
550 * interface_id, vers_option, and object parameters MUST
551 * be ignored.
553 eps->count = build_ep_list(eps,
554 endpoint_table,
555 NULL,
556 p->server_id == NULL ? NULL : p->server_id->addr,
557 &eps->e);
558 break;
559 case RPC_C_EP_MATCH_BY_IF:
561 * Return endpoint map elements that contain the
562 * interface identifier specified by the interface_id
563 * and vers_option values.
565 * RPC_C_EP_MATCH_BY_IF and RPC_C_EP_MATCH_BY_BOTH
566 * need both the same endpoint list. There is a second
567 * check for the inquiry_type below which differentiates
568 * between them.
570 case RPC_C_EP_MATCH_BY_BOTH:
572 * Return endpoint map elements that contain the
573 * interface identifier and object UUID specified by
574 * interface_id, vers_option, and object.
576 eps->count = build_ep_list(eps,
577 endpoint_table,
578 &r->in.interface_id->uuid,
579 p->server_id == NULL ? NULL : p->server_id->addr,
580 &eps->e);
581 break;
582 case RPC_C_EP_MATCH_BY_OBJ:
584 * Return endpoint map elements that contain the object
585 * UUID specified by object.
587 eps->count = build_ep_list(eps,
588 endpoint_table,
589 r->in.object,
590 p->server_id == NULL ? NULL : p->server_id->addr,
591 &eps->e);
592 break;
593 default:
594 rc = EPMAPPER_STATUS_CANT_PERFORM_OP;
595 goto done;
598 if (eps->count == 0) {
599 rc = EPMAPPER_STATUS_NO_MORE_ENTRIES;
600 goto done;
603 ok = create_policy_hnd(p, r->out.entry_handle, eps);
604 if (!ok) {
605 rc = EPMAPPER_STATUS_NO_MEMORY;
606 goto done;
609 ok = find_policy_by_hnd(p, r->out.entry_handle, (void **)(void*) &eps);
610 if (!ok) {
611 rc = EPMAPPER_STATUS_NO_MEMORY;
612 goto done;
614 entry_handle = r->out.entry_handle;
615 } else {
616 DEBUG(5, ("_epm_Lookup: Trying to find entry_handle.\n"));
618 ok = find_policy_by_hnd(p, r->in.entry_handle, (void **)(void*) &eps);
619 if (!ok) {
620 rc = EPMAPPER_STATUS_NO_MEMORY;
621 goto done;
623 entry_handle = r->in.entry_handle;
626 if (eps == NULL || eps->e == NULL) {
627 rc = EPMAPPER_STATUS_NO_MORE_ENTRIES;
628 goto done;
631 /* return the next N elements */
632 count = r->in.max_ents;
633 if (count > eps->count) {
634 count = eps->count;
637 DEBUG(3, ("_epm_Lookup: Find %u entries\n", count));
639 if (count == 0) {
640 close_policy_hnd(p, entry_handle);
641 ZERO_STRUCTP(r->out.entry_handle);
643 rc = EPMAPPER_STATUS_NO_MORE_ENTRIES;
644 goto done;
647 r->out.entries = talloc_array(p->mem_ctx, struct epm_entry_t, count);
648 if (r->out.entries == NULL) {
649 rc = EPMAPPER_STATUS_NO_MEMORY;
650 goto done;
653 for (i = 0; i < count; i++) {
654 match = false;
656 switch (r->in.inquiry_type) {
657 case RPC_C_EP_ALL_ELTS:
659 * Return all elements from the endpoint map. The
660 * interface_id, vers_option, and object parameters MUST
661 * be ignored.
663 match = true;
664 break;
665 case RPC_C_EP_MATCH_BY_IF:
667 * Return endpoint map elements that contain the
668 * interface identifier specified by the interface_id
669 * and vers_option values.
671 if (GUID_equal(&r->in.interface_id->uuid,
672 &eps->e[i].syntax_id.uuid)) {
673 match = true;
675 break;
676 case RPC_C_EP_MATCH_BY_OBJ:
678 * Return endpoint map elements that contain the object
679 * UUID specified by object.
681 if (GUID_equal(r->in.object,
682 &eps->e[i].syntax_id.uuid)) {
683 match = true;
685 break;
686 case RPC_C_EP_MATCH_BY_BOTH:
688 * Return endpoint map elements that contain the
689 * interface identifier and object UUID specified by
690 * interface_id, vers_option, and object.
692 if (GUID_equal(&r->in.interface_id->uuid,
693 &eps->e[i].syntax_id.uuid) &&
694 GUID_equal(r->in.object, &eps->e[i].syntax_id.uuid)) {
695 match = true;
697 break;
698 default:
699 return EPMAPPER_STATUS_CANT_PERFORM_OP;
702 if (match) {
703 if (r->in.inquiry_type == RPC_C_EP_MATCH_BY_IF ||
704 r->in.inquiry_type == RPC_C_EP_MATCH_BY_OBJ) {
705 /* Check inteface version */
707 match = false;
708 switch (r->in.vers_option) {
709 case RPC_C_VERS_ALL:
711 * Return endpoint map elements that
712 * contain the specified interface UUID,
713 * regardless of the version numbers.
715 match = true;
716 break;
717 case RPC_C_VERS_COMPATIBLE:
719 * Return the endpoint map elements that
720 * contain the same major versions of
721 * the specified interface UUID and a
722 * minor version greater than or equal
723 * to the minor version of the specified
724 * UUID.
726 if (r->in.interface_id->vers_major ==
727 (eps->e[i].syntax_id.if_version >> 16) &&
728 r->in.interface_id->vers_minor <=
729 (eps->e[i].syntax_id.if_version && 0xFFFF)) {
730 match = true;
732 break;
733 case RPC_C_VERS_EXACT:
735 * Return endpoint map elements that
736 * contain the specified version of the
737 * specified interface UUID.
739 if (r->in.interface_id->vers_major ==
740 (eps->e[i].syntax_id.if_version >> 16) &&
741 r->in.interface_id->vers_minor ==
742 (eps->e[i].syntax_id.if_version && 0xFFFF)) {
743 match = true;
745 match = true;
746 break;
747 case RPC_C_VERS_MAJOR_ONLY:
749 * Return endpoint map elements that
750 * contain the same version of the
751 * specified interface UUID and ignore
752 * the minor version.
754 if (r->in.interface_id->vers_major ==
755 (eps->e[i].syntax_id.if_version >> 16)) {
756 match = true;
758 match = true;
759 break;
760 case RPC_C_VERS_UPTO:
762 * Return endpoint map elements that
763 * contain a version of the specified
764 * interface UUID less than or equal to
765 * the specified major and minor
766 * version.
768 if (r->in.interface_id->vers_major >
769 eps->e[i].syntax_id.if_version >> 16) {
770 match = true;
771 } else {
772 if (r->in.interface_id->vers_major ==
773 (eps->e[i].syntax_id.if_version >> 16) &&
774 r->in.interface_id->vers_minor >=
775 (eps->e[i].syntax_id.if_version && 0xFFFF)) {
776 match = true;
779 break;
780 default:
781 return EPMAPPER_STATUS_CANT_PERFORM_OP;
786 if (match) {
787 ZERO_STRUCT(r->out.entries[num_ents].object);
789 DEBUG(10, ("_epm_Lookup: Adding tower for '%s'\n",
790 eps->e[i].name));
791 r->out.entries[num_ents].annotation = talloc_strdup(r->out.entries,
792 eps->e[i].name);
793 r->out.entries[num_ents].tower = talloc(r->out.entries,
794 struct epm_twr_t);
795 if (r->out.entries[num_ents].tower == NULL) {
796 rc = EPMAPPER_STATUS_NO_MEMORY;
797 goto done;
799 r->out.entries[num_ents].tower->tower.floors = talloc_move(r->out.entries[num_ents].tower, &eps->e[i].ep.floors);
800 r->out.entries[num_ents].tower->tower.num_floors = eps->e[i].ep.num_floors;
801 r->out.entries[num_ents].tower->tower_length = 0;
803 num_ents++;
805 } /* end for loop */
807 *r->out.num_ents = num_ents;
809 eps->count -= count;
810 eps->e += count;
811 if (eps->count == 0) {
812 close_policy_hnd(p, entry_handle);
813 ZERO_STRUCTP(r->out.entry_handle);
814 rc = EPMAPPER_STATUS_NO_MORE_ENTRIES;
815 goto done;
818 rc = EPMAPPER_STATUS_OK;
819 done:
820 talloc_free(tmp_ctx);
822 return rc;
826 * epm_Map
828 * Apply some algorithm (using the fields in the map_tower) to an endpoint map
829 * to produce a list of protocol towers.
831 error_status_t _epm_Map(struct pipes_struct *p,
832 struct epm_Map *r)
834 struct policy_handle *entry_handle;
835 enum dcerpc_transport_t transport;
836 struct ndr_syntax_id ifid;
837 struct epm_floor *floors;
838 struct rpc_eps *eps;
839 TALLOC_CTX *tmp_ctx;
840 error_status_t rc;
841 uint32_t count = 0;
842 uint32_t num_towers = 0;
843 uint32_t num_floors = 0;
844 uint32_t i;
845 bool ok;
847 *r->out.num_towers = 0;
848 r->out.towers = NULL;
850 if (r->in.map_tower == NULL || r->in.max_towers == 0 ||
851 r->in.map_tower->tower.num_floors < 3) {
852 return EPMAPPER_STATUS_NO_MORE_ENTRIES;
855 tmp_ctx = talloc_stackframe();
856 if (tmp_ctx == NULL) {
857 return EPMAPPER_STATUS_NO_MEMORY;
860 ZERO_STRUCTP(r->out.entry_handle);
862 DEBUG(3, ("_epm_Map: Trying to map max. %u towers.\n",
863 r->in.max_towers));
866 * A tower has normally up to 6 floors
868 * +-----------------------------------------------------------------+
869 * | Floor 1 | Provides the RPC interface identifier. (e.g. UUID for |
870 * | | netlogon) |
871 * +---------+-------------------------------------------------------+
872 * | Floor 2 | Transfer syntax (NDR endcoded) |
873 * +---------+-------------------------------------------------------+
874 * | Floor 3 | RPC protocol identifier (ncacn_tcp_ip, ncacn_np, ...) |
875 * +---------+-------------------------------------------------------+
876 * | Floor 4 | Port address (e.g. TCP Port: 49156) |
877 * +---------+-------------------------------------------------------+
878 * | Floor 5 | Transport (e.g. IP:192.168.51.10) |
879 * +---------+-------------------------------------------------------+
880 * | Floor 6 | Routing |
881 * +---------+-------------------------------------------------------+
883 num_floors = r->in.map_tower->tower.num_floors;
884 floors = r->in.map_tower->tower.floors;
886 /* We accept NDR as the transfer syntax */
887 dcerpc_floor_get_lhs_data(&floors[1], &ifid);
889 if (floors[1].lhs.protocol != EPM_PROTOCOL_UUID ||
890 !GUID_equal(&ifid.uuid, &ndr_transfer_syntax.uuid) ||
891 ifid.if_version != ndr_transfer_syntax.if_version) {
892 rc = EPMAPPER_STATUS_NO_MORE_ENTRIES;
893 goto done;
896 /* We only talk to sane transports */
897 transport = dcerpc_transport_by_tower(&r->in.map_tower->tower);
898 if (transport == NCA_UNKNOWN) {
899 DEBUG(2, ("epm_Map: Client requested unknown transport with"
900 "levels: "));
901 for (i = 2; i < r->in.map_tower->tower.num_floors; i++) {
902 DEBUG(2, ("%d, ", r->in.map_tower->tower.floors[i].lhs.protocol));
904 DEBUG(2, ("\n"));
905 rc = EPMAPPER_STATUS_NO_MORE_ENTRIES;
906 goto done;
909 if (r->in.entry_handle == NULL ||
910 policy_handle_empty(r->in.entry_handle)) {
911 struct GUID *obj;
913 DEBUG(5, ("_epm_Map: No entry_handle found, creating it.\n"));
915 eps = talloc_zero(tmp_ctx, struct rpc_eps);
916 if (eps == NULL) {
917 rc = EPMAPPER_STATUS_NO_MEMORY;
918 goto done;
922 * *** ATTENTION ***
923 * CDE 1.1 states:
925 * ept_map()
926 * Apply some algorithm (using the fields in the map_tower)
927 * to an endpoint map to produce a list of protocol towers.
929 * The following code is the mysterious "some algorithm"!
932 /* Filter by object id if one was given. */
933 if (r->in.object == NULL || GUID_all_zero(r->in.object)) {
934 obj = NULL;
935 } else {
936 obj = r->in.object;
939 eps->count = build_ep_list(eps,
940 endpoint_table,
941 obj,
942 p->server_id == NULL ? NULL : p->server_id->addr,
943 &eps->e);
944 if (eps->count == 0) {
945 rc = EPMAPPER_STATUS_NO_MORE_ENTRIES;
946 goto done;
949 /* Filter out endpoints which match the interface. */
951 struct rpc_eps *teps;
952 uint32_t total = 0;
954 teps = talloc_zero(tmp_ctx, struct rpc_eps);
955 if (teps == NULL) {
956 rc = EPMAPPER_STATUS_NO_MEMORY;
957 goto done;
960 for (i = 0; i < eps->count; i++) {
961 if (data_blob_cmp(&r->in.map_tower->tower.floors[0].lhs.lhs_data,
962 &eps->e[i].ep.floors[0].lhs.lhs_data) != 0 ||
963 transport != dcerpc_transport_by_tower(&eps->e[i].ep)) {
964 continue;
967 teps->e = talloc_realloc(tmp_ctx,
968 teps->e,
969 struct dcesrv_ep_iface,
970 total + 1);
971 if (teps->e == NULL) {
972 return 0;
975 teps->e[total].ep.floors = talloc_move(teps, &eps->e[i].ep.floors);
976 teps->e[total].ep.num_floors = eps->e[i].ep.num_floors;
977 teps->e[total].name = talloc_move(teps, &eps->e[i].name);
978 teps->e[total].syntax_id = eps->e[i].syntax_id;
980 total++;
983 teps->count = total;
984 talloc_free(eps);
985 eps = teps;
987 /* end of "some algorithm" */
989 ok = create_policy_hnd(p, r->out.entry_handle, eps);
990 if (!ok) {
991 rc = EPMAPPER_STATUS_NO_MEMORY;
992 goto done;
995 ok = find_policy_by_hnd(p, r->out.entry_handle, (void **)(void*) &eps);
996 if (!ok) {
997 rc = EPMAPPER_STATUS_NO_MEMORY;
998 goto done;
1000 entry_handle = r->out.entry_handle;
1001 } else {
1002 DEBUG(5, ("_epm_Map: Trying to find entry_handle.\n"));
1004 ok = find_policy_by_hnd(p, r->in.entry_handle, (void **)(void*) &eps);
1005 if (!ok) {
1006 rc = EPMAPPER_STATUS_NO_MEMORY;
1007 goto done;
1009 entry_handle = r->in.entry_handle;
1012 if (eps == NULL || eps->e == NULL) {
1013 rc = EPMAPPER_STATUS_NO_MORE_ENTRIES;
1014 goto done;
1017 /* return the next N elements */
1018 count = r->in.max_towers;
1019 if (count > eps->count) {
1020 count = eps->count;
1023 if (count == 0) {
1024 close_policy_hnd(p, entry_handle);
1025 ZERO_STRUCTP(r->out.entry_handle);
1027 rc = EPMAPPER_STATUS_NO_MORE_ENTRIES;
1028 goto done;
1031 r->out.towers = talloc_array(p->mem_ctx, struct epm_twr_p_t, count);
1032 if (r->out.towers == NULL) {
1033 rc = EPMAPPER_STATUS_NO_MEMORY;
1034 goto done;
1037 for (i = 0; i < count; i++) {
1038 DEBUG(5, ("_epm_Map: Map tower for '%s'\n",
1039 eps->e[i].name));
1041 r->out.towers[num_towers].twr = talloc(r->out.towers,
1042 struct epm_twr_t);
1043 if (r->out.towers[num_towers].twr == NULL) {
1044 rc = EPMAPPER_STATUS_NO_MEMORY;
1045 goto done;
1047 r->out.towers[num_towers].twr->tower.floors = talloc_move(r->out.towers[num_towers].twr, &eps->e[i].ep.floors);
1048 r->out.towers[num_towers].twr->tower.num_floors = eps->e[i].ep.num_floors;
1049 r->out.towers[num_towers].twr->tower_length = 0;
1051 num_towers++;
1054 *r->out.num_towers = num_towers;
1056 eps->count -= count;
1057 eps->e += count;
1058 if (eps->count == 0) {
1059 close_policy_hnd(p, entry_handle);
1060 ZERO_STRUCTP(r->out.entry_handle);
1063 rc = EPMAPPER_STATUS_OK;
1064 done:
1065 talloc_free(tmp_ctx);
1067 return rc;
1071 * epm_LookupHandleFree
1073 error_status_t _epm_LookupHandleFree(struct pipes_struct *p,
1074 struct epm_LookupHandleFree *r)
1076 if (r->in.entry_handle == NULL) {
1077 return EPMAPPER_STATUS_OK;
1080 if (is_valid_policy_hnd(r->in.entry_handle)) {
1081 close_policy_hnd(p, r->in.entry_handle);
1084 r->out.entry_handle = r->in.entry_handle;
1086 return EPMAPPER_STATUS_OK;
1091 * epm_InqObject
1093 * A client implementation SHOULD NOT call this method. These extensions do not
1094 * provide an alternative method.
1096 error_status_t _epm_InqObject(struct pipes_struct *p,
1097 struct epm_InqObject *r)
1099 p->rng_fault_state = true;
1100 return EPMAPPER_STATUS_CANT_PERFORM_OP;
1105 * epm_MgmtDelete
1107 * A client implementation SHOULD NOT call this method. These extensions do not
1108 * provide an alternative method.
1110 error_status_t _epm_MgmtDelete(struct pipes_struct *p,
1111 struct epm_MgmtDelete *r)
1113 p->rng_fault_state = true;
1114 return EPMAPPER_STATUS_CANT_PERFORM_OP;
1119 epm_MapAuth
1121 error_status_t _epm_MapAuth(struct pipes_struct *p,
1122 struct epm_MapAuth *r)
1124 p->rng_fault_state = true;
1125 return EPMAPPER_STATUS_CANT_PERFORM_OP;
1128 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */