s3-epmapper: Fix adding tcpip endpoints.
[samba.git] / source3 / rpc_server / epmapper / srv_epmapper.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Endpoint server for the epmapper pipe
5
6    Copyright (C) 2010-2011 Andreas Schneider <asn@samba.org>
7
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.
12
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.
17
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/>.
20 */
21
22 #include "includes.h"
23 #include "ntdomain.h"
24 #include "../libcli/security/security.h"
25 #include "../lib/tsocket/tsocket.h"
26 #include "librpc/gen_ndr/srv_epmapper.h"
27 #include "srv_epmapper.h"
28 #include "auth.h"
29
30 typedef uint32_t error_status_t;
31
32 /* An endpoint combined with an interface description */
33 struct dcesrv_ep_iface {
34         const char *name;
35         struct ndr_syntax_id syntax_id;
36         struct epm_tower ep;
37 };
38
39 /* A rpc service interface like samr, lsarpc or netlogon */
40 struct dcesrv_iface {
41         const char *name;
42         struct ndr_syntax_id syntax_id;
43 };
44
45 struct dcesrv_iface_list {
46         struct dcesrv_iface_list *next, *prev;
47         struct dcesrv_iface *iface;
48 };
49
50 /*
51  * An endpoint can serve multiple rpc services interfaces.
52  * For example \\pipe\netlogon can be used by lsarpc and netlogon.
53  */
54 struct dcesrv_endpoint {
55         struct dcesrv_endpoint *next, *prev;
56
57         /* The type and the location of the endpoint */
58         struct dcerpc_binding *ep_description;
59
60         /* A list of rpc services able to connect to the endpoint */
61         struct dcesrv_iface_list *iface_list;
62 };
63
64 struct dcesrv_ep_entry_list {
65         struct dcesrv_ep_entry_list *next, *prev;
66
67         uint32_t num_ents;
68         struct epm_entry_t *entries;
69 };
70
71 struct rpc_eps {
72         struct dcesrv_ep_iface *e;
73         uint32_t count;
74 };
75
76 static struct dcesrv_endpoint *endpoint_table;
77
78 /*
79  * Check if the UUID and if_version match to an interface.
80  */
81 static bool interface_match(const struct dcesrv_iface *if1,
82                             const struct dcesrv_iface *if2)
83 {
84         return GUID_equal(&if1->syntax_id.uuid, &if2->syntax_id.uuid);
85 }
86
87 /*
88  * Find the interface operations on an endpoint.
89  */
90 static const struct dcesrv_iface *find_interface(const struct dcesrv_endpoint *endpoint,
91                                                  const struct dcesrv_iface *iface)
92 {
93         struct dcesrv_iface_list *iflist;
94
95         for (iflist = endpoint->iface_list; iflist; iflist = iflist->next) {
96                 if (interface_match(iflist->iface, iface)) {
97                         return iflist->iface;
98                 }
99         }
100
101         return NULL;
102 }
103
104 /*
105  * See if a uuid and if_version match to an interface
106  */
107 static bool interface_match_by_uuid(const struct dcesrv_iface *iface,
108                                     const struct GUID *uuid)
109 {
110         return GUID_equal(&iface->syntax_id.uuid, uuid);
111 }
112
113 static struct dcesrv_iface_list *find_interface_list(const struct dcesrv_endpoint *endpoint,
114                                                      const struct dcesrv_iface *iface)
115 {
116         struct dcesrv_iface_list *iflist;
117
118         for (iflist = endpoint->iface_list; iflist; iflist = iflist->next) {
119                 if (interface_match(iflist->iface, iface)) {
120                         return iflist;
121                 }
122         }
123
124         return NULL;
125 }
126
127 /*
128  * Check if two endpoints match.
129  */
130 static bool endpoints_match(const struct dcerpc_binding *ep1,
131                             const struct dcerpc_binding *ep2)
132 {
133         if (ep1->transport != ep2->transport) {
134                 return false;
135         }
136
137         if (!ep1->endpoint || !ep2->endpoint) {
138                 return ep1->endpoint == ep2->endpoint;
139         }
140
141         if (!strequal(ep1->endpoint, ep2->endpoint)) {
142                 return false;
143         }
144
145         if (!ep1->host || !ep2->host) {
146                 return ep1->endpoint == ep2->endpoint;
147         }
148
149         if (!strequal(ep1->host, ep2->host)) {
150                 return false;
151         }
152
153         return true;
154 }
155
156 static struct dcesrv_endpoint *find_endpoint(struct dcesrv_endpoint *endpoint_list,
157                                              struct dcerpc_binding *ep_description) {
158         struct dcesrv_endpoint *ep;
159
160         for (ep = endpoint_list; ep != NULL; ep = ep->next) {
161                 if (endpoints_match(ep->ep_description, ep_description)) {
162                         return ep;
163                 }
164         }
165
166         return NULL;
167 }
168
169 /*
170  * Build a list of all interfaces handled by all endpoint servers.
171  */
172 static uint32_t build_ep_list(TALLOC_CTX *mem_ctx,
173                               struct dcesrv_endpoint *endpoint_list,
174                               const struct GUID *uuid,
175                               const char *srv_addr,
176                               struct dcesrv_ep_iface **peps)
177 {
178         struct dcesrv_ep_iface *eps = NULL;
179         struct dcesrv_endpoint *d;
180         uint32_t total = 0;
181         NTSTATUS status;
182
183         *peps = NULL;
184
185         for (d = endpoint_list; d != NULL; d = d->next) {
186                 struct dcesrv_iface_list *iface;
187                 struct dcerpc_binding *description;
188
189                 for (iface = d->iface_list; iface != NULL; iface = iface->next) {
190                         if (uuid && !interface_match_by_uuid(iface->iface, uuid)) {
191                                 continue;
192                         }
193
194                         eps = talloc_realloc(mem_ctx,
195                                              eps,
196                                              struct dcesrv_ep_iface,
197                                              total + 1);
198                         if (eps == NULL) {
199                                 return 0;
200                         }
201                         eps[total].name = talloc_strdup(eps,
202                                                         iface->iface->name);
203                         eps[total].syntax_id = iface->iface->syntax_id;
204
205                         description = dcerpc_binding_dup(mem_ctx, d->ep_description);
206                         if (description == NULL) {
207                                 return 0;
208                         }
209                         description->object = iface->iface->syntax_id;
210                         if (description->transport == NCACN_IP_TCP &&
211                             srv_addr != NULL &&
212                             (strcmp(description->host, "0.0.0.0") == 0 ||
213                              strcmp(description->host, "::") == 0)) {
214                                 description->host = srv_addr;
215                         }
216
217                         status = dcerpc_binding_build_tower(eps,
218                                                             description,
219                                                             &eps[total].ep);
220                         TALLOC_FREE(description);
221                         if (NT_STATUS_IS_ERR(status)) {
222                                 DEBUG(1, ("Unable to build tower for %s\n",
223                                           iface->iface->name));
224                                 continue;
225                         }
226                         total++;
227                 }
228         }
229
230         *peps = eps;
231
232         return total;
233 }
234
235 static bool is_priviledged_pipe(struct auth_serversupplied_info *info) {
236         /* If the user is not root, or has the system token, fail */
237         if ((info->utok.uid != sec_initial_uid()) &&
238             !security_token_is_system(info->security_token)) {
239                 return false;
240         }
241
242         return true;
243 }
244
245 bool srv_epmapper_delete_endpoints(struct pipes_struct *p)
246 {
247         struct epm_Delete r;
248         struct dcesrv_ep_entry_list *el = p->ep_entries;
249         error_status_t result;
250
251         while (el) {
252                 struct dcesrv_ep_entry_list *next = el->next;
253
254                 r.in.num_ents = el->num_ents;
255                 r.in.entries = el->entries;
256
257                 DEBUG(10, ("Delete_endpoints for: %s\n",
258                            el->entries[0].annotation));
259
260                 result = _epm_Delete(p, &r);
261                 if (result != EPMAPPER_STATUS_OK) {
262                         return false;
263                 }
264
265                 DLIST_REMOVE(p->ep_entries, el);
266                 TALLOC_FREE(el);
267
268                 el = next;
269         }
270
271         return true;
272 }
273
274 void srv_epmapper_cleanup(void)
275 {
276         struct dcesrv_endpoint *ep = endpoint_table;
277
278         while (ep) {
279                 struct dcesrv_endpoint *next = ep->next;
280
281                 DLIST_REMOVE(endpoint_table, ep);
282                 TALLOC_FREE(ep);
283
284                 ep = next;
285         }
286 }
287
288 /*
289  * epm_Insert
290  *
291  * Add the specified entries to an endpoint map.
292  */
293 error_status_t _epm_Insert(struct pipes_struct *p,
294                            struct epm_Insert *r)
295 {
296         TALLOC_CTX *tmp_ctx;
297         error_status_t rc;
298         NTSTATUS status;
299         uint32_t i;
300         struct dcerpc_binding *b;
301         struct dcesrv_endpoint *ep;
302         struct dcesrv_iface_list *iflist;
303         struct dcesrv_iface *iface;
304         bool add_ep;
305
306         /* If this is not a priviledged users, return */
307         if (p->transport != NCALRPC ||
308             !is_priviledged_pipe(p->session_info)) {
309                 return EPMAPPER_STATUS_CANT_PERFORM_OP;
310         }
311
312         tmp_ctx = talloc_stackframe();
313         if (tmp_ctx == NULL) {
314                 return EPMAPPER_STATUS_NO_MEMORY;
315         }
316
317         DEBUG(3, ("_epm_Insert: Trying to add %u new entries.\n",
318                   r->in.num_ents));
319
320         for (i = 0; i < r->in.num_ents; i++) {
321                 add_ep = false;
322                 b = NULL;
323
324                 status = dcerpc_binding_from_tower(tmp_ctx,
325                                                    &r->in.entries[i].tower->tower,
326                                                    &b);
327                 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
328                         rc = EPMAPPER_STATUS_NO_MEMORY;
329                         goto done;
330                 }
331                 if (!NT_STATUS_IS_OK(status)) {
332                         rc = EPMAPPER_STATUS_CANT_PERFORM_OP;
333                         goto done;
334                 }
335
336                 DEBUG(3, ("_epm_Insert: Adding transport %s for %s\n",
337                           derpc_transport_string_by_transport(b->transport),
338                           r->in.entries[i].annotation));
339
340                 /* Check if the entry already exits */
341                 ep = find_endpoint(endpoint_table, b);
342                 if (ep == NULL) {
343                         /* No entry found, create it */
344                         ep = talloc_zero(NULL, struct dcesrv_endpoint);
345                         if (ep == NULL) {
346                                 rc = EPMAPPER_STATUS_NO_MEMORY;
347                                 goto done;
348                         }
349                         add_ep = true;
350
351                         ep->ep_description = talloc_steal(ep, b);
352                 }
353
354                 /* TODO Replace the entry if the replace flag is set */
355
356                 /* Create an interface */
357                 iface = talloc(tmp_ctx, struct dcesrv_iface);
358                 if (iface == NULL) {
359                         rc = EPMAPPER_STATUS_NO_MEMORY;
360                         goto done;
361                 }
362
363                 iface->name = talloc_strdup(iface, r->in.entries[i].annotation);
364                 if (iface->name == NULL) {
365                         rc = EPMAPPER_STATUS_NO_MEMORY;
366                         goto done;
367                 }
368                 iface->syntax_id = b->object;
369
370                 /*
371                  * Check if the rpc service is alrady registered on the
372                  * endpoint.
373                  */
374                 if (find_interface(ep, iface) != NULL) {
375                         DEBUG(0, ("dcesrv_interface_register: interface '%s' "
376                                   "already registered on endpoint\n",
377                                   iface->name));
378                         /* FIXME wrong error code? */
379                         rc = EPMAPPER_STATUS_OK;
380                         goto done;
381                 }
382
383                 /* Create an entry for the interface */
384                 iflist = talloc(ep, struct dcesrv_iface_list);
385                 if (iflist == NULL) {
386                         rc = EPMAPPER_STATUS_NO_MEMORY;
387                         goto done;
388                 }
389                 iflist->iface = talloc_move(iflist, &iface);
390
391                 /* Finally add the interface on the endpoint */
392                 DLIST_ADD(ep->iface_list, iflist);
393
394                 /* If it's a new endpoint add it to the endpoint_table */
395                 if (add_ep) {
396                         DLIST_ADD(endpoint_table, ep);
397                 }
398         }
399
400         if (r->in.num_ents > 0) {
401                 struct dcesrv_ep_entry_list *el;
402
403                 el = talloc_zero(p, struct dcesrv_ep_entry_list);
404                 if (el == NULL) {
405                         rc = EPMAPPER_STATUS_NO_MEMORY;
406                         goto done;
407                 }
408                 el->num_ents = r->in.num_ents;
409                 el->entries = talloc_move(el, &r->in.entries);
410
411                 DLIST_ADD(p->ep_entries, el);
412         }
413
414         rc = EPMAPPER_STATUS_OK;
415 done:
416         talloc_free(tmp_ctx);
417
418         return rc;
419 }
420
421
422 /*
423  * epm_Delete
424  *
425  * Delete the specified entries from an endpoint map.
426  */
427 error_status_t _epm_Delete(struct pipes_struct *p,
428                            struct epm_Delete *r)
429 {
430         TALLOC_CTX *tmp_ctx;
431         error_status_t rc;
432         NTSTATUS status;
433         uint32_t i;
434         struct dcerpc_binding *b;
435         struct dcesrv_endpoint *ep;
436         struct dcesrv_iface iface;
437         struct dcesrv_iface_list *iflist;
438
439         DEBUG(3, ("_epm_Delete: Trying to delete %u entries.\n",
440                   r->in.num_ents));
441
442         /* If this is not a priviledged users, return */
443         if (p->transport != NCALRPC ||
444             !is_priviledged_pipe(p->session_info)) {
445                 return EPMAPPER_STATUS_CANT_PERFORM_OP;
446         }
447
448         tmp_ctx = talloc_stackframe();
449         if (tmp_ctx == NULL) {
450                 return EPMAPPER_STATUS_NO_MEMORY;
451         }
452
453         for (i = 0; i < r->in.num_ents; i++) {
454                 b = NULL;
455
456                 status = dcerpc_binding_from_tower(tmp_ctx,
457                                                    &r->in.entries[i].tower->tower,
458                                                    &b);
459                 if (!NT_STATUS_IS_OK(status)) {
460                         rc = EPMAPPER_STATUS_NO_MEMORY;
461                         goto done;
462                 }
463
464                 DEBUG(3, ("_epm_Delete: Deleting transport '%s' for '%s'\n",
465                           derpc_transport_string_by_transport(b->transport),
466                           r->in.entries[i].annotation));
467
468                 ep = find_endpoint(endpoint_table, b);
469                 if (ep == NULL) {
470                         rc = EPMAPPER_STATUS_OK;
471                         goto done;
472                 }
473
474                 iface.name = r->in.entries[i].annotation;
475                 iface.syntax_id = b->object;
476
477                 iflist = find_interface_list(ep, &iface);
478                 if (iflist == NULL) {
479                         DEBUG(0, ("_epm_Delete: No interfaces left, delete endpoint\n"));
480                         DLIST_REMOVE(endpoint_table, ep);
481                         talloc_free(ep);
482
483                         rc = EPMAPPER_STATUS_OK;
484                         goto done;
485                 }
486
487                 DLIST_REMOVE(ep->iface_list, iflist);
488
489                 if (ep->iface_list == NULL) {
490                         DEBUG(0, ("_epm_Delete: No interfaces left, delete endpoint\n"));
491                         DLIST_REMOVE(endpoint_table, ep);
492                         talloc_free(ep);
493
494                         rc = EPMAPPER_STATUS_OK;
495                         goto done;
496                 }
497
498         }
499
500         rc = EPMAPPER_STATUS_OK;
501 done:
502         talloc_free(tmp_ctx);
503
504         return rc;
505 }
506
507
508 /*
509  * epm_Lookup
510  *
511  * Lookup entries in an endpoint map.
512  */
513 error_status_t _epm_Lookup(struct pipes_struct *p,
514                            struct epm_Lookup *r)
515 {
516         struct policy_handle *entry_handle;
517         struct rpc_eps *eps;
518         TALLOC_CTX *tmp_ctx;
519         error_status_t rc;
520         uint32_t count = 0;
521         uint32_t num_ents = 0;
522         uint32_t i;
523         bool match = false;
524         bool ok;
525
526         *r->out.num_ents = 0;
527         r->out.entries = NULL;
528
529         tmp_ctx = talloc_stackframe();
530         if (tmp_ctx == NULL) {
531                 return EPMAPPER_STATUS_NO_MEMORY;
532         }
533
534         DEBUG(5, ("_epm_Lookup: Trying to lookup max. %u entries.\n",
535                   r->in.max_ents));
536
537         if (r->in.entry_handle == NULL ||
538             policy_handle_empty(r->in.entry_handle)) {
539                 struct GUID *obj;
540                 char *srv_addr = NULL;
541
542                 DEBUG(7, ("_epm_Lookup: No entry_handle found, creating it.\n"));
543
544                 eps = talloc_zero(tmp_ctx, struct rpc_eps);
545                 if (eps == NULL) {
546                         rc = EPMAPPER_STATUS_NO_MEMORY;
547                         goto done;
548                 }
549
550                 if (r->in.object == NULL || GUID_all_zero(r->in.object)) {
551                         obj = NULL;
552                 } else {
553                         obj = r->in.object;
554                 }
555
556                 if (p->local_address != NULL) {
557                         srv_addr = tsocket_address_inet_addr_string(p->local_address,
558                                                                     tmp_ctx);
559                 }
560
561                 switch (r->in.inquiry_type) {
562                 case RPC_C_EP_ALL_ELTS:
563                         /*
564                          * Return all elements from the endpoint map. The
565                          * interface_id, vers_option, and object parameters MUST
566                          * be ignored.
567                          */
568                         eps->count = build_ep_list(eps,
569                                                    endpoint_table,
570                                                    NULL,
571                                                    srv_addr,
572                                                    &eps->e);
573                         break;
574                 case RPC_C_EP_MATCH_BY_IF:
575                         /*
576                          * Return endpoint map elements that contain the
577                          * interface identifier specified by the interface_id
578                          * and vers_option values.
579                          *
580                          * RPC_C_EP_MATCH_BY_IF and RPC_C_EP_MATCH_BY_BOTH
581                          * need both the same endpoint list. There is a second
582                          * check for the inquiry_type below which differentiates
583                          * between them.
584                          */
585                 case RPC_C_EP_MATCH_BY_BOTH:
586                         /*
587                          * Return endpoint map elements that contain the
588                          * interface identifier and object UUID specified by
589                          * interface_id, vers_option, and object.
590                          */
591                         eps->count = build_ep_list(eps,
592                                                    endpoint_table,
593                                                    &r->in.interface_id->uuid,
594                                                    srv_addr,
595                                                    &eps->e);
596                         break;
597                 case RPC_C_EP_MATCH_BY_OBJ:
598                         /*
599                          * Return endpoint map elements that contain the object
600                          * UUID specified by object.
601                          */
602                         eps->count = build_ep_list(eps,
603                                                    endpoint_table,
604                                                    r->in.object,
605                                                    srv_addr,
606                                                    &eps->e);
607                         break;
608                 default:
609                         rc = EPMAPPER_STATUS_CANT_PERFORM_OP;
610                         goto done;
611                 }
612
613                 if (eps->count == 0) {
614                         rc = EPMAPPER_STATUS_NO_MORE_ENTRIES;
615                         goto done;
616                 }
617
618                 ok = create_policy_hnd(p, r->out.entry_handle, eps);
619                 if (!ok) {
620                         rc = EPMAPPER_STATUS_NO_MEMORY;
621                         goto done;
622                 }
623
624                 ok = find_policy_by_hnd(p, r->out.entry_handle, (void **)(void*) &eps);
625                 if (!ok) {
626                         rc = EPMAPPER_STATUS_NO_MEMORY;
627                         goto done;
628                 }
629                 entry_handle = r->out.entry_handle;
630         } else {
631                 DEBUG(7, ("_epm_Lookup: Trying to find entry_handle.\n"));
632
633                 ok = find_policy_by_hnd(p, r->in.entry_handle, (void **)(void*) &eps);
634                 if (!ok) {
635                         rc = EPMAPPER_STATUS_NO_MEMORY;
636                         goto done;
637                 }
638                 entry_handle = r->in.entry_handle;
639         }
640
641         if (eps == NULL || eps->e == NULL) {
642                 rc = EPMAPPER_STATUS_NO_MORE_ENTRIES;
643                 goto done;
644         }
645
646         /* return the next N elements */
647         count = r->in.max_ents;
648         if (count > eps->count) {
649                 count = eps->count;
650         }
651
652         DEBUG(5, ("_epm_Lookup: Find %u entries\n", count));
653
654         if (count == 0) {
655                 close_policy_hnd(p, entry_handle);
656                 ZERO_STRUCTP(r->out.entry_handle);
657
658                 rc = EPMAPPER_STATUS_NO_MORE_ENTRIES;
659                 goto done;
660         }
661
662         r->out.entries = talloc_array(p->mem_ctx, struct epm_entry_t, count);
663         if (r->out.entries == NULL) {
664                 rc = EPMAPPER_STATUS_NO_MEMORY;
665                 goto done;
666         }
667
668         for (i = 0; i < count; i++) {
669                 match = false;
670
671                 switch (r->in.inquiry_type) {
672                 case RPC_C_EP_ALL_ELTS:
673                         /*
674                          * Return all elements from the endpoint map. The
675                          * interface_id, vers_option, and object parameters MUST
676                          * be ignored.
677                          */
678                         match = true;
679                         break;
680                 case RPC_C_EP_MATCH_BY_IF:
681                         /*
682                          * Return endpoint map elements that contain the
683                          * interface identifier specified by the interface_id
684                          * and vers_option values.
685                          */
686                         if (GUID_equal(&r->in.interface_id->uuid,
687                                        &eps->e[i].syntax_id.uuid)) {
688                                 match = true;
689                         }
690                         break;
691                 case RPC_C_EP_MATCH_BY_OBJ:
692                         /*
693                          * Return endpoint map elements that contain the object
694                          * UUID specified by object.
695                          */
696                         if (GUID_equal(r->in.object,
697                                        &eps->e[i].syntax_id.uuid)) {
698                                 match = true;
699                         }
700                         break;
701                 case RPC_C_EP_MATCH_BY_BOTH:
702                         /*
703                          * Return endpoint map elements that contain the
704                          * interface identifier and object UUID specified by
705                          * interface_id, vers_option, and object.
706                          */
707                         if (GUID_equal(&r->in.interface_id->uuid,
708                                        &eps->e[i].syntax_id.uuid) &&
709                             GUID_equal(r->in.object, &eps->e[i].syntax_id.uuid)) {
710                                 match = true;
711                         }
712                         break;
713                 default:
714                         return EPMAPPER_STATUS_CANT_PERFORM_OP;
715                 }
716
717                 if (match) {
718                         if (r->in.inquiry_type == RPC_C_EP_MATCH_BY_IF ||
719                             r->in.inquiry_type == RPC_C_EP_MATCH_BY_OBJ) {
720                                 /* Check inteface version */
721
722                                 match = false;
723                                 switch (r->in.vers_option) {
724                                 case RPC_C_VERS_ALL:
725                                         /*
726                                          * Return endpoint map elements that
727                                          * contain the specified interface UUID,
728                                          * regardless of the version numbers.
729                                          */
730                                         match = true;
731                                         break;
732                                 case RPC_C_VERS_COMPATIBLE:
733                                         /*
734                                          * Return the endpoint map elements that
735                                          * contain the same major versions of
736                                          * the specified interface UUID and a
737                                          * minor version greater than or equal
738                                          * to the minor version of the specified
739                                          * UUID.
740                                          */
741                                         if (r->in.interface_id->vers_major ==
742                                             (eps->e[i].syntax_id.if_version >> 16) &&
743                                             r->in.interface_id->vers_minor <=
744                                             (eps->e[i].syntax_id.if_version && 0xFFFF)) {
745                                                 match = true;
746                                         }
747                                         break;
748                                 case RPC_C_VERS_EXACT:
749                                         /*
750                                          * Return endpoint map elements that
751                                          * contain the specified version of the
752                                          * specified interface UUID.
753                                          */
754                                         if (r->in.interface_id->vers_major ==
755                                             (eps->e[i].syntax_id.if_version >> 16) &&
756                                             r->in.interface_id->vers_minor ==
757                                             (eps->e[i].syntax_id.if_version && 0xFFFF)) {
758                                                 match = true;
759                                         }
760                                         match = true;
761                                         break;
762                                 case RPC_C_VERS_MAJOR_ONLY:
763                                         /*
764                                          * Return endpoint map elements that
765                                          * contain the same version of the
766                                          * specified interface UUID and ignore
767                                          * the minor version.
768                                          */
769                                         if (r->in.interface_id->vers_major ==
770                                             (eps->e[i].syntax_id.if_version >> 16)) {
771                                                 match = true;
772                                         }
773                                         match = true;
774                                         break;
775                                 case RPC_C_VERS_UPTO:
776                                         /*
777                                          * Return endpoint map elements that
778                                          * contain a version of the specified
779                                          * interface UUID less than or equal to
780                                          * the specified major and minor
781                                          * version.
782                                          */
783                                         if (r->in.interface_id->vers_major >
784                                             eps->e[i].syntax_id.if_version >> 16) {
785                                                 match = true;
786                                         } else {
787                                                 if (r->in.interface_id->vers_major ==
788                                                     (eps->e[i].syntax_id.if_version >> 16) &&
789                                                     r->in.interface_id->vers_minor >=
790                                                     (eps->e[i].syntax_id.if_version && 0xFFFF)) {
791                                                         match = true;
792                                                 }
793                                         }
794                                         break;
795                                 default:
796                                         return EPMAPPER_STATUS_CANT_PERFORM_OP;
797                                 }
798                         }
799                 }
800
801                 if (match) {
802                         ZERO_STRUCT(r->out.entries[num_ents].object);
803
804                         DEBUG(10, ("_epm_Lookup: Adding tower for '%s'\n",
805                                    eps->e[i].name));
806                         r->out.entries[num_ents].annotation = talloc_strdup(r->out.entries,
807                                                                             eps->e[i].name);
808                         r->out.entries[num_ents].tower = talloc(r->out.entries,
809                                                                 struct epm_twr_t);
810                         if (r->out.entries[num_ents].tower == NULL) {
811                                 rc = EPMAPPER_STATUS_NO_MEMORY;
812                                 goto done;
813                         }
814                         r->out.entries[num_ents].tower->tower.floors = talloc_move(r->out.entries[num_ents].tower, &eps->e[i].ep.floors);
815                         r->out.entries[num_ents].tower->tower.num_floors = eps->e[i].ep.num_floors;
816                         r->out.entries[num_ents].tower->tower_length = 0;
817
818                         num_ents++;
819                 }
820         } /* end for loop */
821
822         *r->out.num_ents = num_ents;
823
824         eps->count -= count;
825         eps->e += count;
826         if (eps->count == 0) {
827                 close_policy_hnd(p, entry_handle);
828                 ZERO_STRUCTP(r->out.entry_handle);
829                 rc = EPMAPPER_STATUS_NO_MORE_ENTRIES;
830                 goto done;
831         }
832
833         rc = EPMAPPER_STATUS_OK;
834 done:
835         talloc_free(tmp_ctx);
836
837         return rc;
838 }
839
840 /*
841  * epm_Map
842  *
843  * Apply some algorithm (using the fields in the map_tower) to an endpoint map
844  * to produce a list of protocol towers.
845  */
846 error_status_t _epm_Map(struct pipes_struct *p,
847                         struct epm_Map *r)
848 {
849         struct policy_handle *entry_handle;
850         enum dcerpc_transport_t transport;
851         struct ndr_syntax_id ifid;
852         struct epm_floor *floors;
853         struct rpc_eps *eps;
854         TALLOC_CTX *tmp_ctx;
855         error_status_t rc;
856         uint32_t count = 0;
857         uint32_t num_towers = 0;
858         uint32_t num_floors = 0;
859         uint32_t i;
860         bool ok;
861
862         *r->out.num_towers = 0;
863         r->out.towers = NULL;
864
865         if (r->in.map_tower == NULL || r->in.max_towers == 0 ||
866             r->in.map_tower->tower.num_floors < 3) {
867                 return EPMAPPER_STATUS_NO_MORE_ENTRIES;
868         }
869
870         tmp_ctx = talloc_stackframe();
871         if (tmp_ctx == NULL) {
872                 return EPMAPPER_STATUS_NO_MEMORY;
873         }
874
875         ZERO_STRUCTP(r->out.entry_handle);
876
877         DEBUG(5, ("_epm_Map: Trying to map max. %u towers.\n",
878                   r->in.max_towers));
879
880         /*
881          * A tower has normally up to 6 floors
882          *
883          * +-----------------------------------------------------------------+
884          * | Floor 1 | Provides the RPC interface identifier. (e.g. UUID for |
885          * |         | netlogon)                                             |
886          * +---------+-------------------------------------------------------+
887          * | Floor 2 | Transfer syntax (NDR endcoded)                        |
888          * +---------+-------------------------------------------------------+
889          * | Floor 3 | RPC protocol identifier (ncacn_tcp_ip, ncacn_np, ...) |
890          * +---------+-------------------------------------------------------+
891          * | Floor 4 | Port address (e.g. TCP Port: 49156)                   |
892          * +---------+-------------------------------------------------------+
893          * | Floor 5 | Transport (e.g. IP:192.168.51.10)                     |
894          * +---------+-------------------------------------------------------+
895          * | Floor 6 | Routing                                               |
896          * +---------+-------------------------------------------------------+
897          */
898         num_floors = r->in.map_tower->tower.num_floors;
899         floors = r->in.map_tower->tower.floors;
900
901         /* We accept NDR as the transfer syntax */
902         dcerpc_floor_get_lhs_data(&floors[1], &ifid);
903
904         if (floors[1].lhs.protocol != EPM_PROTOCOL_UUID ||
905             !GUID_equal(&ifid.uuid, &ndr_transfer_syntax.uuid) ||
906             ifid.if_version != ndr_transfer_syntax.if_version) {
907                 rc = EPMAPPER_STATUS_NO_MORE_ENTRIES;
908                 goto done;
909         }
910
911         /* We only talk to sane transports */
912         transport = dcerpc_transport_by_tower(&r->in.map_tower->tower);
913         if (transport == NCA_UNKNOWN) {
914                 DEBUG(2, ("epm_Map: Client requested unknown transport with"
915                           "levels: "));
916                 for (i = 2; i < r->in.map_tower->tower.num_floors; i++) {
917                         DEBUG(2, ("%d, ", r->in.map_tower->tower.floors[i].lhs.protocol));
918                 }
919                 DEBUG(2, ("\n"));
920                 rc = EPMAPPER_STATUS_NO_MORE_ENTRIES;
921                 goto done;
922         }
923
924         if (r->in.entry_handle == NULL ||
925             policy_handle_empty(r->in.entry_handle)) {
926                 struct GUID *obj;
927                 char *srv_addr = NULL;
928
929                 DEBUG(7, ("_epm_Map: No entry_handle found, creating it.\n"));
930
931                 eps = talloc_zero(tmp_ctx, struct rpc_eps);
932                 if (eps == NULL) {
933                         rc = EPMAPPER_STATUS_NO_MEMORY;
934                         goto done;
935                 }
936
937                 /*
938                  * *** ATTENTION ***
939                  * CDE 1.1 states:
940                  *
941                  * ept_map()
942                  *     Apply some algorithm (using the fields in the map_tower)
943                  *     to an endpoint map to produce a list of protocol towers.
944                  *
945                  * The following code is the mysterious "some algorithm"!
946                  */
947
948                 /* Filter by object id if one was given. */
949                 if (r->in.object == NULL || GUID_all_zero(r->in.object)) {
950                         obj = NULL;
951                 } else {
952                         obj = r->in.object;
953                 }
954
955                 if (p->local_address != NULL) {
956                         srv_addr = tsocket_address_inet_addr_string(p->local_address,
957                                                                     tmp_ctx);
958                 }
959
960                 eps->count = build_ep_list(eps,
961                                            endpoint_table,
962                                            obj,
963                                            srv_addr,
964                                            &eps->e);
965                 if (eps->count == 0) {
966                         rc = EPMAPPER_STATUS_NO_MORE_ENTRIES;
967                         goto done;
968                 }
969
970                 /* Filter out endpoints which match the interface. */
971                 {
972                         struct rpc_eps *teps;
973                         uint32_t total = 0;
974
975                         teps = talloc_zero(tmp_ctx, struct rpc_eps);
976                         if (teps == NULL) {
977                                 rc = EPMAPPER_STATUS_NO_MEMORY;
978                                 goto done;
979                         }
980
981                         for (i = 0; i < eps->count; i++) {
982                                 if (data_blob_cmp(&r->in.map_tower->tower.floors[0].lhs.lhs_data,
983                                                   &eps->e[i].ep.floors[0].lhs.lhs_data) != 0 ||
984                                     transport != dcerpc_transport_by_tower(&eps->e[i].ep)) {
985                                         continue;
986                                 }
987
988                                 teps->e = talloc_realloc(tmp_ctx,
989                                                          teps->e,
990                                                          struct dcesrv_ep_iface,
991                                                          total + 1);
992                                 if (teps->e == NULL) {
993                                         return 0;
994                                 }
995
996                                 teps->e[total].ep.floors = talloc_move(teps, &eps->e[i].ep.floors);
997                                 teps->e[total].ep.num_floors = eps->e[i].ep.num_floors;
998                                 teps->e[total].name = talloc_move(teps, &eps->e[i].name);
999                                 teps->e[total].syntax_id = eps->e[i].syntax_id;
1000
1001                                 total++;
1002                         }
1003
1004                         teps->count = total;
1005                         talloc_free(eps);
1006                         eps = teps;
1007                 }
1008                 /* end of "some algorithm" */
1009
1010                 ok = create_policy_hnd(p, r->out.entry_handle, eps);
1011                 if (!ok) {
1012                         rc = EPMAPPER_STATUS_NO_MEMORY;
1013                         goto done;
1014                 }
1015
1016                 ok = find_policy_by_hnd(p, r->out.entry_handle, (void **)(void*) &eps);
1017                 if (!ok) {
1018                         rc = EPMAPPER_STATUS_NO_MEMORY;
1019                         goto done;
1020                 }
1021                 entry_handle = r->out.entry_handle;
1022         } else {
1023                 DEBUG(7, ("_epm_Map: Trying to find entry_handle.\n"));
1024
1025                 ok = find_policy_by_hnd(p, r->in.entry_handle, (void **)(void*) &eps);
1026                 if (!ok) {
1027                         rc = EPMAPPER_STATUS_NO_MEMORY;
1028                         goto done;
1029                 }
1030                 entry_handle = r->in.entry_handle;
1031         }
1032
1033         if (eps == NULL || eps->e == NULL) {
1034                 rc = EPMAPPER_STATUS_NO_MORE_ENTRIES;
1035                 goto done;
1036         }
1037
1038         /* return the next N elements */
1039         count = r->in.max_towers;
1040         if (count > eps->count) {
1041                 count = eps->count;
1042         }
1043
1044         if (count == 0) {
1045                 close_policy_hnd(p, entry_handle);
1046                 ZERO_STRUCTP(r->out.entry_handle);
1047
1048                 rc = EPMAPPER_STATUS_NO_MORE_ENTRIES;
1049                 goto done;
1050         }
1051
1052         r->out.towers = talloc_array(p->mem_ctx, struct epm_twr_p_t, count);
1053         if (r->out.towers == NULL) {
1054                 rc = EPMAPPER_STATUS_NO_MEMORY;
1055                 goto done;
1056         }
1057
1058         for (i = 0; i < count; i++) {
1059                 DEBUG(7, ("_epm_Map: Map tower for '%s'\n",
1060                            eps->e[i].name));
1061
1062                 r->out.towers[num_towers].twr = talloc(r->out.towers,
1063                                                        struct epm_twr_t);
1064                 if (r->out.towers[num_towers].twr == NULL) {
1065                         rc = EPMAPPER_STATUS_NO_MEMORY;
1066                         goto done;
1067                 }
1068                 r->out.towers[num_towers].twr->tower.floors = talloc_move(r->out.towers[num_towers].twr, &eps->e[i].ep.floors);
1069                 r->out.towers[num_towers].twr->tower.num_floors = eps->e[i].ep.num_floors;
1070                 r->out.towers[num_towers].twr->tower_length = 0;
1071
1072                 num_towers++;
1073         }
1074
1075         *r->out.num_towers = num_towers;
1076
1077         eps->count -= count;
1078         eps->e += count;
1079         if (eps->count == 0) {
1080                 close_policy_hnd(p, entry_handle);
1081                 ZERO_STRUCTP(r->out.entry_handle);
1082         }
1083
1084         rc = EPMAPPER_STATUS_OK;
1085 done:
1086         talloc_free(tmp_ctx);
1087
1088         return rc;
1089 }
1090
1091 /*
1092  * epm_LookupHandleFree
1093  */
1094 error_status_t _epm_LookupHandleFree(struct pipes_struct *p,
1095                                      struct epm_LookupHandleFree *r)
1096 {
1097         if (r->in.entry_handle == NULL) {
1098                 return EPMAPPER_STATUS_OK;
1099         }
1100
1101         if (is_valid_policy_hnd(r->in.entry_handle)) {
1102                 close_policy_hnd(p, r->in.entry_handle);
1103         }
1104
1105         r->out.entry_handle = r->in.entry_handle;
1106
1107         return EPMAPPER_STATUS_OK;
1108 }
1109
1110
1111 /*
1112  * epm_InqObject
1113  *
1114  * A client implementation SHOULD NOT call this method. These extensions do not
1115  * provide an alternative method.
1116  */
1117 error_status_t _epm_InqObject(struct pipes_struct *p,
1118                       struct epm_InqObject *r)
1119 {
1120         p->rng_fault_state = true;
1121         return EPMAPPER_STATUS_CANT_PERFORM_OP;
1122 }
1123
1124
1125 /*
1126  * epm_MgmtDelete
1127  *
1128  * A client implementation SHOULD NOT call this method. These extensions do not
1129  * provide an alternative method.
1130 */
1131 error_status_t _epm_MgmtDelete(struct pipes_struct *p,
1132                        struct epm_MgmtDelete *r)
1133 {
1134         p->rng_fault_state = true;
1135         return EPMAPPER_STATUS_CANT_PERFORM_OP;
1136 }
1137
1138
1139 /*
1140   epm_MapAuth
1141 */
1142 error_status_t _epm_MapAuth(struct pipes_struct *p,
1143                     struct epm_MapAuth *r)
1144 {
1145         p->rng_fault_state = true;
1146         return EPMAPPER_STATUS_CANT_PERFORM_OP;
1147 }
1148
1149 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */