2000-11-14 03:22:53 +00:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2000-11-14 03:22:53 +00:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
2000-11-14 03:22:53 +00:00
|
|
|
*/
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*! \file */
|
2000-11-14 03:22:53 +00:00
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <isc/mem.h>
|
|
|
|
#include <isc/util.h>
|
|
|
|
|
|
|
|
#include <dns/acl.h>
|
2017-09-08 13:39:09 -07:00
|
|
|
#include <dns/message.h>
|
2000-11-14 03:22:53 +00:00
|
|
|
#include <dns/result.h>
|
|
|
|
|
2017-09-08 13:39:09 -07:00
|
|
|
#include <ns/server.h>
|
|
|
|
#include <ns/sortlist.h>
|
2000-11-14 03:22:53 +00:00
|
|
|
|
|
|
|
ns_sortlisttype_t
|
2017-09-08 13:39:09 -07:00
|
|
|
ns_sortlist_setup(dns_acl_t *acl, dns_aclenv_t *env,
|
|
|
|
isc_netaddr_t *clientaddr, const void **argp)
|
2006-02-28 02:39:52 +00:00
|
|
|
{
|
2000-11-14 03:22:53 +00:00
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (acl == NULL)
|
|
|
|
goto dont_sort;
|
|
|
|
|
|
|
|
for (i = 0; i < acl->length; i++) {
|
|
|
|
/*
|
|
|
|
* 'e' refers to the current 'top level statement'
|
|
|
|
* in the sortlist (see ARM).
|
|
|
|
*/
|
|
|
|
dns_aclelement_t *e = &acl->elements[i];
|
2001-10-30 19:45:33 +00:00
|
|
|
dns_aclelement_t *try_elt;
|
|
|
|
dns_aclelement_t *order_elt = NULL;
|
2006-02-28 02:39:52 +00:00
|
|
|
const dns_aclelement_t *matched_elt = NULL;
|
2001-10-30 19:45:33 +00:00
|
|
|
|
|
|
|
if (e->type == dns_aclelementtype_nestedacl) {
|
2007-09-12 01:09:08 +00:00
|
|
|
dns_acl_t *inner = e->nestedacl;
|
2001-10-30 19:45:33 +00:00
|
|
|
|
2007-09-14 01:46:06 +00:00
|
|
|
if (inner->length == 0)
|
|
|
|
try_elt = e;
|
|
|
|
else if (inner->length > 2)
|
2001-10-30 19:45:33 +00:00
|
|
|
goto dont_sort;
|
2007-09-14 01:46:06 +00:00
|
|
|
else if (inner->elements[0].negative)
|
2001-10-30 19:45:33 +00:00
|
|
|
goto dont_sort;
|
2007-09-14 01:46:06 +00:00
|
|
|
else {
|
|
|
|
try_elt = &inner->elements[0];
|
|
|
|
if (inner->length == 2)
|
|
|
|
order_elt = &inner->elements[1];
|
|
|
|
}
|
2001-10-30 19:45:33 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* BIND 8 allows bare elements at the top level
|
|
|
|
* as an undocumented feature.
|
|
|
|
*/
|
|
|
|
try_elt = e;
|
2001-10-30 06:41:58 +00:00
|
|
|
}
|
|
|
|
|
2018-04-03 13:09:45 +02:00
|
|
|
if (dns_aclelement_match(clientaddr, NULL, NULL, 0, NULL,
|
|
|
|
try_elt, env, &matched_elt))
|
2017-09-08 13:39:09 -07:00
|
|
|
{
|
2001-10-30 19:45:33 +00:00
|
|
|
if (order_elt != NULL) {
|
|
|
|
if (order_elt->type ==
|
2017-09-08 13:39:09 -07:00
|
|
|
dns_aclelementtype_nestedacl)
|
|
|
|
{
|
2007-09-12 01:09:08 +00:00
|
|
|
*argp = order_elt->nestedacl;
|
2001-10-30 20:19:34 +00:00
|
|
|
return (NS_SORTLISTTYPE_2ELEMENT);
|
|
|
|
} else if (order_elt->type ==
|
|
|
|
dns_aclelementtype_localhost &&
|
2017-09-08 13:39:09 -07:00
|
|
|
env->localhost != NULL)
|
|
|
|
{
|
|
|
|
*argp = env->localhost;
|
2001-10-30 20:19:34 +00:00
|
|
|
return (NS_SORTLISTTYPE_2ELEMENT);
|
|
|
|
} else if (order_elt->type ==
|
|
|
|
dns_aclelementtype_localnets &&
|
2017-09-08 13:39:09 -07:00
|
|
|
env->localnets != NULL)
|
|
|
|
{
|
|
|
|
*argp = env->localnets;
|
2001-10-30 20:19:34 +00:00
|
|
|
return (NS_SORTLISTTYPE_2ELEMENT);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* BIND 8 allows a bare IP prefix as
|
|
|
|
* the 2nd element of a 2-element
|
|
|
|
* sortlist statement.
|
|
|
|
*/
|
|
|
|
*argp = order_elt;
|
|
|
|
return (NS_SORTLISTTYPE_1ELEMENT);
|
|
|
|
}
|
2000-11-14 03:22:53 +00:00
|
|
|
} else {
|
2001-10-30 19:45:33 +00:00
|
|
|
INSIST(matched_elt != NULL);
|
|
|
|
*argp = matched_elt;
|
2000-11-14 03:22:53 +00:00
|
|
|
return (NS_SORTLISTTYPE_1ELEMENT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No match; don't sort. */
|
|
|
|
dont_sort:
|
|
|
|
*argp = NULL;
|
|
|
|
return (NS_SORTLISTTYPE_NONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2006-02-28 02:39:52 +00:00
|
|
|
ns_sortlist_addrorder2(const isc_netaddr_t *addr, const void *arg) {
|
2017-09-08 13:39:09 -07:00
|
|
|
const dns_sortlist_arg_t *sla = (const dns_sortlist_arg_t *) arg;
|
|
|
|
const dns_aclenv_t *env = sla->env;
|
|
|
|
const dns_acl_t *sortacl = sla->acl;
|
2000-11-14 03:22:53 +00:00
|
|
|
int match;
|
|
|
|
|
2018-04-03 13:09:45 +02:00
|
|
|
(void)dns_acl_match(addr, NULL, NULL, 0, NULL, sortacl, env, &match,
|
|
|
|
NULL);
|
2000-11-14 03:22:53 +00:00
|
|
|
if (match > 0)
|
|
|
|
return (match);
|
|
|
|
else if (match < 0)
|
|
|
|
return (INT_MAX - (-match));
|
|
|
|
else
|
|
|
|
return (INT_MAX / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2006-02-28 02:39:52 +00:00
|
|
|
ns_sortlist_addrorder1(const isc_netaddr_t *addr, const void *arg) {
|
2017-09-08 13:39:09 -07:00
|
|
|
const dns_sortlist_arg_t *sla = (const dns_sortlist_arg_t *) arg;
|
|
|
|
const dns_aclenv_t *env = sla->env;
|
|
|
|
const dns_aclelement_t *element = sla->element;
|
|
|
|
|
2018-04-03 13:09:45 +02:00
|
|
|
if (dns_aclelement_match(addr, NULL, NULL, 0, NULL, element, env,
|
|
|
|
NULL)) {
|
2000-11-14 03:22:53 +00:00
|
|
|
return (0);
|
|
|
|
}
|
2017-09-08 13:39:09 -07:00
|
|
|
|
|
|
|
return (INT_MAX);
|
2000-11-14 03:22:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-09-08 13:39:09 -07:00
|
|
|
ns_sortlist_byaddrsetup(dns_acl_t *sortlist_acl, dns_aclenv_t *env,
|
|
|
|
isc_netaddr_t *client_addr,
|
|
|
|
dns_addressorderfunc_t *orderp, const void **argp)
|
2000-11-14 03:22:53 +00:00
|
|
|
{
|
2000-11-15 20:35:13 +00:00
|
|
|
ns_sortlisttype_t sortlisttype;
|
|
|
|
|
2017-09-08 13:39:09 -07:00
|
|
|
sortlisttype = ns_sortlist_setup(sortlist_acl, env, client_addr, argp);
|
2000-11-15 20:35:13 +00:00
|
|
|
|
|
|
|
switch (sortlisttype) {
|
2000-11-14 03:22:53 +00:00
|
|
|
case NS_SORTLISTTYPE_1ELEMENT:
|
2000-11-15 20:35:13 +00:00
|
|
|
*orderp = ns_sortlist_addrorder1;
|
2000-11-14 03:22:53 +00:00
|
|
|
break;
|
|
|
|
case NS_SORTLISTTYPE_2ELEMENT:
|
2000-11-15 20:35:13 +00:00
|
|
|
*orderp = ns_sortlist_addrorder2;
|
2000-11-14 03:22:53 +00:00
|
|
|
break;
|
|
|
|
case NS_SORTLISTTYPE_NONE:
|
2000-11-15 20:35:13 +00:00
|
|
|
*orderp = NULL;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"unexpected return from ns_sortlist_setup(): "
|
|
|
|
"%d", sortlisttype);
|
2000-11-14 03:22:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|