Commit 80bd2548 authored by James Brister's avatar James Brister
Browse files

Updated use of magic numbers.

parent a2250d24
......@@ -20,12 +20,20 @@
#include <string.h>
#include <isc/assertions.h>
#include <isc/magic.h>
#include <dns/log.h>
#include <dns/confacl.h>
#include <dns/confcommon.h>
#define CONFACL_MAGIC 0x4361636cU
#define CONFACLTABLE_MAGIC 0x32616354U
#define DNS_CONFACL_VALID(confacl) ISC_MAGIC_VALID(confacl, CONFACL_MAGIC)
#define DNS_CONFACLTABLE_VALID(confacltable) \
ISC_MAGIC_VALID(confacltable, CONFACLTABLE_MAGIC)
static isc_result_t acl_delete(isc_log_t *lctx, dns_c_acl_t **aclptr);
......@@ -48,6 +56,7 @@ dns_c_acltable_new(isc_log_t *lctx,
}
table->mem = mem;
table->magic = CONFACLTABLE_MAGIC;
ISC_LIST_INIT(table->acl_list);
......@@ -62,17 +71,26 @@ dns_c_acltable_delete(isc_log_t *lctx,
dns_c_acltable_t **table)
{
dns_c_acltable_t *acltable;
REQUIRE(table != NULL);
isc_mem_t *mem;
REQUIRE(table != NULL);
acltable = *table;
if (acltable == NULL) {
return (ISC_R_SUCCESS);
}
REQUIRE(DNS_CONFACLTABLE_VALID(acltable));
dns_c_acltable_clear(lctx, acltable);
isc_mem_put(acltable->mem, acltable, sizeof *acltable);
mem = acltable->mem;
acltable->magic = 0;
acltable->mem = NULL;
isc_mem_put(mem, acltable, sizeof *acltable);
return (ISC_R_SUCCESS);
}
......@@ -92,6 +110,8 @@ dns_c_acltable_print(isc_log_t *lctx,
return;
}
REQUIRE(DNS_CONFACLTABLE_VALID(table));
acl = ISC_LIST_HEAD(table->acl_list);
while (acl != NULL) {
acltmp = ISC_LIST_NEXT(acl, next);
......@@ -113,8 +133,8 @@ dns_c_acltable_clear(isc_log_t *lctx, dns_c_acltable_t *table)
dns_c_acl_t *tmpelem;
isc_result_t r;
REQUIRE(table != NULL);
REQUIRE(DNS_CONFACLTABLE_VALID(table));
elem = ISC_LIST_HEAD(table->acl_list);
while (elem != NULL) {
tmpelem = ISC_LIST_NEXT(elem, next);
......@@ -142,7 +162,7 @@ dns_c_acltable_getacl(isc_log_t *lctx, dns_c_acltable_t *table,
{
dns_c_acl_t *elem;
REQUIRE(table != NULL);
REQUIRE(DNS_CONFACLTABLE_VALID(table));
REQUIRE(retval != NULL);
REQUIRE(aclname != NULL);
REQUIRE(strlen(aclname) > 0);
......@@ -159,6 +179,7 @@ dns_c_acltable_getacl(isc_log_t *lctx, dns_c_acltable_t *table,
}
if (elem != NULL) {
REQUIRE(DNS_CONFACL_VALID(elem));
*retval = elem;
}
......@@ -173,7 +194,7 @@ dns_c_acltable_removeacl(isc_log_t *lctx,
dns_c_acl_t *acl;
dns_c_acl_t *tmpacl;
REQUIRE(table != NULL);
REQUIRE(DNS_CONFACLTABLE_VALID(table));
REQUIRE(aclname != NULL);
acl = ISC_LIST_HEAD(table->acl_list);
......@@ -199,7 +220,7 @@ dns_c_acl_new(isc_log_t *lctx,
{
dns_c_acl_t *acl;
REQUIRE(table != NULL);
REQUIRE(DNS_CONFACLTABLE_VALID(table));
REQUIRE(aclname != NULL);
REQUIRE(strlen(aclname) > 0);
REQUIRE(newacl != NULL);
......@@ -213,6 +234,7 @@ dns_c_acl_new(isc_log_t *lctx,
}
acl->mytable = table;
acl->magic = CONFACL_MAGIC;
acl->name = NULL;
acl->ipml = NULL;
acl->is_special = isspecial;
......@@ -238,6 +260,8 @@ void
dns_c_acl_print(isc_log_t *lctx,
FILE *fp, int indent, dns_c_acl_t *acl)
{
REQUIRE(DNS_CONFACL_VALID(acl));
dns_c_printtabs(lctx, fp, indent);
fprintf(fp, "acl ");
if (acl->name == NULL) {
......@@ -262,7 +286,7 @@ dns_c_acl_setipml(isc_log_t *lctx, dns_c_acl_t *acl,
{
isc_result_t res;
REQUIRE(acl != NULL);
REQUIRE(DNS_CONFACL_VALID(acl));
REQUIRE(ipml != NULL);
if (acl->ipml != NULL) {
......@@ -288,6 +312,8 @@ dns_c_acl_getipmlexpanded(isc_log_t *lctx, isc_mem_t *mem, dns_c_acl_t *acl,
dns_c_ipmatchlist_t *newlist;
isc_result_t r;
REQUIRE(DNS_CONFACL_VALID(acl));
if (acl->ipml == NULL) {
newlist = NULL;
r = ISC_R_SUCCESS;
......@@ -306,6 +332,9 @@ dns_c_acl_getipmlexpanded(isc_log_t *lctx, isc_mem_t *mem, dns_c_acl_t *acl,
}
/* XXX this should really be a function in the confip module */
isc_result_t
dns_c_acl_expandacls(isc_log_t *lctx, dns_c_acltable_t *table,
dns_c_ipmatchlist_t *list)
......@@ -315,6 +344,8 @@ dns_c_acl_expandacls(isc_log_t *lctx, dns_c_acltable_t *table,
dns_c_acl_t *acl;
isc_result_t r;
isc_boolean_t isneg;
REQUIRE(DNS_CONFACLTABLE_VALID(table));
if (list == NULL) {
return (ISC_R_SUCCESS);
......@@ -370,15 +401,27 @@ acl_delete(isc_log_t *lctx, dns_c_acl_t **aclptr)
{
dns_c_acl_t *acl;
isc_result_t res;
isc_mem_t *mem;
REQUIRE(aclptr != NULL);
acl = *aclptr;
if (acl == NULL) {
return (ISC_R_SUCCESS);
}
isc_mem_free(acl->mytable->mem, acl->name);
REQUIRE(DNS_CONFACL_VALID(acl));
mem = acl->mytable->mem;
acl->mytable = NULL;
isc_mem_free(mem, acl->name);
res = dns_c_ipmatchlist_delete(lctx, &acl->ipml);
isc_mem_put(acl->mytable->mem, acl, sizeof *acl);
acl->magic = 0;
isc_mem_put(mem, acl, sizeof *acl);
return (res);
}
......
......@@ -21,10 +21,75 @@
#include <isc/assertions.h>
#include <isc/net.h>
#include <isc/magic.h>
#include <dns/confctl.h>
#include <dns/confcommon.h>
#define CONFCTL_MAGIC 0x4363746cU
#define CONFCTLLIST_MAGIC 0x4354424cU
#define DNS_CONFCTLLIST_VALID(ctllist) \
ISC_MAGIC_VALID(ctllist, CONFCTLLIST_MAGIC)
#define DNS_CONFCTL_VALID(ctl) ISC_MAGIC_VALID(ctl, CONFCTL_MAGIC)
isc_result_t
dns_c_ctrllist_new(isc_log_t *lctx,
isc_mem_t *mem, dns_c_ctrllist_t **newlist)
{
dns_c_ctrllist_t *newl;
REQUIRE(mem != NULL);
REQUIRE (newlist != NULL);
(void) lctx;
newl = isc_mem_get(mem, sizeof *newl);
if (newl == NULL) {
/* XXXJAB logwrite */
return (ISC_R_NOMEMORY);
}
newl->mem = mem;
newl->magic = CONFCTLLIST_MAGIC;
ISC_LIST_INIT(newl->elements);
*newlist = newl;
return (ISC_R_SUCCESS);
}
void
dns_c_ctrllist_print(isc_log_t *lctx,
FILE *fp, int indent, dns_c_ctrllist_t *cl)
{
dns_c_ctrl_t *ctl;
if (cl == NULL) {
return;
}
REQUIRE(DNS_CONFCTLLIST_VALID(cl));
if (ISC_LIST_EMPTY(cl->elements)) {
return;
}
fprintf(fp, "controls {\n");
ctl = ISC_LIST_HEAD(cl->elements);
while (ctl != NULL) {
dns_c_printtabs(lctx, fp, indent + 1);
dns_c_ctrl_print(lctx, fp, indent + 1, ctl);
ctl = ISC_LIST_NEXT(ctl, next);
}
fprintf(fp, "};\n");
}
isc_result_t
dns_c_ctrllist_delete(isc_log_t *lctx,
......@@ -35,11 +100,14 @@ dns_c_ctrllist_delete(isc_log_t *lctx,
dns_c_ctrllist_t *clist;
REQUIRE(list != NULL);
clist = *list;
if (clist == NULL) {
return (ISC_R_SUCCESS);
}
REQUIRE(DNS_CONFCTLLIST_VALID(clist));
ctrl = ISC_LIST_HEAD(clist->elements);
while (ctrl != NULL) {
tmpctrl = ISC_LIST_NEXT(ctrl, next);
......@@ -71,6 +139,7 @@ dns_c_ctrlinet_new(isc_log_t *lctx, isc_mem_t *mem, dns_c_ctrl_t **control,
return (ISC_R_NOMEMORY);
}
ctrl->magic = CONFCTL_MAGIC;
ctrl->mem = mem;
ctrl->control_type = dns_c_inet_control;
ctrl->u.inet_v.addr = addr;
......@@ -110,6 +179,7 @@ dns_c_ctrlunix_new(isc_log_t *lctx,
return (ISC_R_NOMEMORY);
}
ctrl->magic = CONFCTL_MAGIC;
ctrl->mem = mem;
ctrl->control_type = dns_c_unix_control;
ctrl->u.unix_v.pathname = isc_mem_strdup(mem, path);
......@@ -133,7 +203,7 @@ isc_result_t
dns_c_ctrl_delete(isc_log_t *lctx,
dns_c_ctrl_t **control)
{
isc_result_t res;
isc_result_t res = ISC_R_SUCCESS;
isc_result_t rval;
isc_mem_t *mem;
dns_c_ctrl_t *ctrl;
......@@ -145,6 +215,8 @@ dns_c_ctrl_delete(isc_log_t *lctx,
return (ISC_R_SUCCESS);
}
REQUIRE(DNS_CONFCTL_VALID(ctrl));
mem = ctrl->mem;
switch (ctrl->control_type) {
......@@ -161,11 +233,13 @@ dns_c_ctrl_delete(isc_log_t *lctx,
rval = res;
ctrl->magic = 0;
isc_mem_put(mem, ctrl, sizeof *ctrl);
*control = NULL;
return (ISC_R_SUCCESS);
return (res);
}
......@@ -176,6 +250,8 @@ dns_c_ctrl_print(isc_log_t *lctx,
short port;
dns_c_ipmatchlist_t *iml;
REQUIRE(DNS_CONFCTL_VALID(ctl));
(void) indent;
if (ctl->control_type == dns_c_inet_control) {
......@@ -205,51 +281,3 @@ dns_c_ctrl_print(isc_log_t *lctx,
}
isc_result_t
dns_c_ctrllist_new(isc_log_t *lctx,
isc_mem_t *mem, dns_c_ctrllist_t **newlist)
{
dns_c_ctrllist_t *newl;
REQUIRE(mem != NULL);
REQUIRE (newlist != NULL);
(void) lctx;
newl = isc_mem_get(mem, sizeof *newl);
if (newl == NULL) {
/* XXXJAB logwrite */
return (ISC_R_NOMEMORY);
}
newl->mem = mem;
ISC_LIST_INIT(newl->elements);
*newlist = newl;
return (ISC_R_SUCCESS);
}
void
dns_c_ctrllist_print(isc_log_t *lctx,
FILE *fp, int indent, dns_c_ctrllist_t *cl)
{
dns_c_ctrl_t *ctl;
if (cl == NULL || ISC_LIST_EMPTY(cl->elements)) {
return;
}
fprintf(fp, "controls {\n");
ctl = ISC_LIST_HEAD(cl->elements);
while (ctl != NULL) {
dns_c_printtabs(lctx, fp, indent + 1);
dns_c_ctrl_print(lctx, fp, indent + 1, ctl);
ctl = ISC_LIST_NEXT(ctl, next);
}
fprintf(fp, "};\n");
}
This diff is collapsed.
......@@ -21,11 +21,24 @@
#include <isc/assertions.h>
#include <isc/error.h>
#include <isc/magic.h>
#include <dns/confip.h>
#include <dns/confcommon.h>
#include <dns/log.h>
#define IPLIST_MAGIC 0x49706c73 /* Ipls */ /* dns_c_iplist */
#define IPMDIRECT_MAGIC 0x49506d64 /* IPmd */ /* dns_c_ipmatch_direct */
#define IPMINDIRECT_MAGIC 0x69506d69 /* iPmi */ /* dns_c_ipmatch_indirect */
#define IPMELEM_MAGIC 0x49704d65 /* IpMe */ /* dns_c_ipmatch_element */
#define IPMLIST_MAGIC 0x69706d6c /* ipml */ /* dns_c_ipmatchlist */
#define DNS_IPLIST_VALID(ipl) ISC_MAGIC_VALID(ipl,IPLIST_MAGIC)
#define DNS_IPDIRECT_VALID(ipmld) ISC_MAGIC_VALID(ipmld, IPMDIRECT_MAGIC)
#define DNS_IPINDIRECT_VALID(ipmlid) ISC_MAGIC_VALID(ipmlid, IPMINDIRECT_MAGIC)
#define DNS_IPMELEM_VALID(impe) ISC_MAGIC_VALID(impe, IPMELEM_MAGIC)
#define DNS_IPMLIST_VALID(ipml) ISC_MAGIC_VALID(ipml, IPMLIST_MAGIC)
/* Flag for dns_c_ipmatch_element */
#define DNS_C_IPMATCH_NEGATE 0x01 /* match means deny access */
......@@ -51,6 +64,7 @@ dns_c_ipmatchelement_new(isc_log_t *lctx,
return (ISC_R_NOMEMORY);
}
ime->magic = IPMELEM_MAGIC;
ime->type = dns_c_ipmatch_none;
ime->flags = 0;
memset(&ime->u, 0x0, sizeof ime->u);
......@@ -70,6 +84,8 @@ dns_c_ipmatchelement_isneg(isc_log_t *lctx,
(void) lctx;
REQUIRE(DNS_IPMELEM_VALID(elem));
return ((elem->flags & DNS_C_IPMATCH_NEGATE) == DNS_C_IPMATCH_NEGATE);
}
......@@ -88,6 +104,8 @@ dns_c_ipmatchelement_delete(isc_log_t *lctx,
}
elem = *ipme;
REQUIRE(DNS_IPMELEM_VALID(elem));
switch (elem->type) {
case dns_c_ipmatch_localhost:
......@@ -140,13 +158,13 @@ dns_c_ipmatchelement_copy(isc_log_t *lctx,
REQUIRE(mem != NULL);
REQUIRE(dest != NULL);
REQUIRE(src != NULL);
REQUIRE(DNS_IPMELEM_VALID(src));
result = dns_c_ipmatchelement_new(lctx, mem, &newel);
if (result != ISC_R_SUCCESS) {
return (result);
}
newel->type = src->type;
newel->flags = src->flags;
......@@ -188,10 +206,15 @@ dns_c_ipmatchelement_copy(isc_log_t *lctx,
return (ISC_R_SUCCESS);
}
isc_boolean_t
dns_c_ipmatchelement_equal(dns_c_ipmatchelement_t *e1,
dns_c_ipmatchelement_t *e2)
{
REQUIRE(DNS_IPMELEM_VALID(e1));
REQUIRE(DNS_IPMELEM_VALID(e2));
if ((e1->type != e2->type) || (e1->flags != e2->flags))
return (ISC_FALSE);
......@@ -284,7 +307,7 @@ dns_c_ipmatchindirect_new(isc_log_t *lctx,
REQUIRE(mem != NULL);
REQUIRE(result != NULL);
REQUIRE(iml != NULL);
REQUIRE(DNS_IPMLIST_VALID(iml));
*result = NULL;
......@@ -351,103 +374,6 @@ dns_c_ipmatchpattern_new(isc_log_t *lctx,
}
/*
* Check that the address given is a network address with the given number
* of high order bits.
*/
static isc_result_t
checkmask(isc_sockaddr_t *address, isc_uint32_t bits)
{
if (bits > 0) {
if (address->type.sa.sa_family == AF_INET) {
isc_uint32_t mask;
mask = ntohl(0xffffffffU << (32 - bits));
if ((mask & address->type.sin.sin_addr.s_addr) !=
address->type.sin.sin_addr.s_addr) {
return (ISC_R_FAILURE);
}
} else if (address->type.sa.sa_family == AF_INET6) {
struct in6_addr iaddr;
unsigned char *maskp;
unsigned char *addrp;
int i;
if (bits2v6mask(&iaddr, bits) != ISC_R_SUCCESS) {
return (ISC_R_FAILURE);
}
addrp = (unsigned char *)&address->type.sin6.sin6_addr;
maskp = (unsigned char *)&iaddr;
for (i = 0 ; i < 16 ; i++) {
if ((addrp[i] & maskp[i]) != addrp[i]) {
return (ISC_R_FAILURE);
}
}
}
}
return (ISC_R_SUCCESS);
}
/*
* Create a 128 bits mask in network byte order in the the IPv6 address
* section of the sockaddr. The bits argument is the number of high bits
* that are to be set to 1.
*/
static isc_result_t
bits2v6mask(struct in6_addr *addr, isc_uint32_t bits)
{
int i;
isc_uint32_t bitmask[4];
char addrbuff [ sizeof "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff" + 1 ];
INSIST(bits < 128);
/* Break the 128 bits up into 32-bit sections */
bitmask[0] = bitmask[1] = bitmask[2] = bitmask[3] = 0U;
if (bits > 32) {
bitmask[0] = 0xffffffffU;
} else if (bits > 0) {
bitmask[0] = 0xffffffffU << (32 - bits);
}
if (bits > 64) {
bitmask[1] = 0xffffffffU;
} else if (bits > 32) {
bitmask[1] = 0xffffffffU << (64 - bits);
}
if (bits > 96) {
bitmask[2] = 0xffffffffU;
bitmask[3] = 0xffffffffU << (128 - bits);
} else if (bits > 64) {
bitmask[2] = 0xffffffffU << (96 - bits);
}
memset(addr, 0x0, sizeof *addr);
sprintf(addrbuff, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x",
(((bitmask[0] & 0xffff0000U) >> 16) & 0xffffU),
(bitmask[0] & 0xffff),
(((bitmask[1] & 0xffff0000U) >> 16) & 0xffffU),
(bitmask[1] & 0xffff),
(((bitmask[2] & 0xffff0000U) >> 16) & 0xffffU),
(bitmask[2] & 0xffff),
(((bitmask[3] & 0xffff0000U) >> 16) & 0xffffU),
(bitmask[3] & 0xffff));
i = inet_pton(AF_INET6, addrbuff, &addr);
return (i == 1 ? ISC_R_SUCCESS : ISC_R_FAILURE);
}
isc_result_t
dns_c_ipmatchkey_new(isc_log_t *lctx,
isc_mem_t *mem,
......@@ -511,8 +437,8 @@ isc_result_t
dns_c_ipmatch_negate(isc_log_t *lctx,
dns_c_ipmatchelement_t *ipe)
{
REQUIRE(ipe != NULL);
REQUIRE(DNS_IPMELEM_VALID(ipe));
(void) lctx;
if ((ipe->flags & DNS_C_IPMATCH_NEGATE) == DNS_C_IPMATCH_NEGATE) {
......@@ -541,10 +467,12 @@ dns_c_ipmatchlist_new(isc_log_t *lctx,
return (ISC_R_NOMEMORY);
}
ISC_LIST_INIT(newlist->elements);
newlist->magic = IPMLIST_MAGIC;
newlist->mem = mem;
newlist->refcount = 1;
ISC_LIST_INIT(newlist->elements);
*ptr = newlist;
return (ISC_R_SUCCESS);
......@@ -561,13 +489,14 @@ dns_c_ipmatchlist_delete(isc_log_t *lctx,
isc_mem_t *mem;
REQUIRE(ml != NULL);
iml = *ml;
if (iml == NULL) {
return (ISC_R_SUCCESS);
}
*ml = NULL;
REQUIRE(DNS_IPMLIST_VALID(iml));
INSIST(iml->refcount > 0);
iml->refcount--;
......@@ -599,7 +528,8 @@ dns_c_ipmatchlist_attach(isc_log_t *lctx,
(void) lctx;
REQUIRE(ipml != NULL);
REQUIRE(DNS_IPMLIST_VALID(ipml));
INSIST(ipml->refcount > 0);
ipml->refcount++;
......@@ -615,7 +545,7 @@ dns_c_ipmatchlist_empty(isc_log_t *lctx,
dns_c_ipmatchelement_t *imptmp;
isc_result_t res = ISC_R_SUCCESS;
REQUIRE(ipml != NULL);
REQUIRE(DNS_IPMLIST_VALID(ipml));
ime = ISC_LIST_HEAD(ipml->elements);