2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-29 05:28:00 +00:00
bind/lib/dns/config/confctl.c

290 lines
5.9 KiB
C
Raw Normal View History

1999-07-19 13:25:18 +00:00
/*
2000-02-03 23:17:52 +00:00
* Copyright (C) 1999, 2000 Internet Software Consortium.
1999-07-19 13:25:18 +00:00
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
/* $Id: confctl.c,v 1.23 2000/07/07 23:11:42 brister Exp $ */
1999-07-19 13:25:18 +00:00
#include <config.h>
#include <isc/mem.h>
2000-04-28 01:12:23 +00:00
#include <isc/util.h>
1999-07-19 13:25:18 +00:00
#include <dns/confctl.h>
1999-10-16 01:34:30 +00:00
isc_result_t
dns_c_ctrllist_new(isc_mem_t *mem, dns_c_ctrllist_t **newlist) {
1999-10-16 01:34:30 +00:00
dns_c_ctrllist_t *newl;
REQUIRE(mem != NULL);
REQUIRE (newlist != NULL);
newl = isc_mem_get(mem, sizeof *newl);
if (newl == NULL) {
/* XXXJAB logwrite */
return (ISC_R_NOMEMORY);
}
newl->mem = mem;
newl->magic = DNS_C_CONFCTLLIST_MAGIC;
1999-10-16 01:34:30 +00:00
ISC_LIST_INIT(newl->elements);
*newlist = newl;
return (ISC_R_SUCCESS);
}
void
dns_c_ctrllist_print(FILE *fp, int indent, dns_c_ctrllist_t *cl) {
1999-10-16 01:34:30 +00:00
dns_c_ctrl_t *ctl;
if (cl == NULL) {
return;
}
REQUIRE(DNS_C_CONFCTLLIST_VALID(cl));
1999-10-16 01:34:30 +00:00
fprintf(fp, "controls {\n");
ctl = dns_c_ctrllist_head(cl);
if (ctl == NULL) {
dns_c_printtabs(fp, indent + 1);
fprintf(fp,"/* empty list */\n");
} else {
while (ctl != NULL) {
dns_c_printtabs(fp, indent + 1);
dns_c_ctrl_print(fp, indent + 1, ctl);
ctl = dns_c_ctrl_next(ctl);
}
1999-10-16 01:34:30 +00:00
}
1999-10-16 01:34:30 +00:00
fprintf(fp, "};\n");
}
1999-07-19 13:25:18 +00:00
isc_result_t
dns_c_ctrllist_delete(dns_c_ctrllist_t **list) {
1999-07-19 13:25:18 +00:00
dns_c_ctrl_t *ctrl;
dns_c_ctrl_t *tmpctrl;
dns_c_ctrllist_t *clist;
1999-07-19 13:25:18 +00:00
REQUIRE(list != NULL);
REQUIRE(*list != NULL);
1999-10-16 01:34:30 +00:00
1999-07-19 13:25:18 +00:00
clist = *list;
REQUIRE(DNS_C_CONFCTLLIST_VALID(clist));
1999-10-16 01:34:30 +00:00
1999-07-19 13:25:18 +00:00
ctrl = ISC_LIST_HEAD(clist->elements);
while (ctrl != NULL) {
tmpctrl = ISC_LIST_NEXT(ctrl, next);
dns_c_ctrl_delete(&ctrl);
1999-07-19 13:25:18 +00:00
ctrl = tmpctrl;
}
clist->magic = 0;
1999-07-19 13:25:18 +00:00
isc_mem_put(clist->mem, clist, sizeof *clist);
*list = NULL;
return (ISC_R_SUCCESS);
}
isc_result_t
dns_c_ctrlinet_new(isc_mem_t *mem, dns_c_ctrl_t **control,
2000-01-06 16:15:29 +00:00
isc_sockaddr_t addr, in_port_t port,
dns_c_ipmatchlist_t *iml, dns_c_kidlist_t *keylist,
isc_boolean_t copy)
1999-07-19 13:25:18 +00:00
{
dns_c_ctrl_t *ctrl;
isc_result_t res;
REQUIRE(mem != NULL);
REQUIRE(control != NULL);
ctrl = isc_mem_get(mem, sizeof *ctrl);
if (ctrl == NULL) {
return (ISC_R_NOMEMORY);
}
ctrl->magic = DNS_C_CONFCTL_MAGIC;
1999-07-19 13:25:18 +00:00
ctrl->mem = mem;
ctrl->control_type = dns_c_inet_control;
ctrl->u.inet_v.addr = addr;
ctrl->u.inet_v.port = port;
ctrl->keyidlist = NULL;
if (keylist != NULL) {
ctrl->keyidlist = keylist;
}
1999-07-19 13:25:18 +00:00
if (copy) {
res = dns_c_ipmatchlist_copy(mem,
1999-10-02 21:23:11 +00:00
&ctrl->u.inet_v.matchlist, iml);
1999-07-19 13:25:18 +00:00
if (res != ISC_R_SUCCESS) {
isc_mem_put(mem, ctrl, sizeof *ctrl);
return (res);
}
} else {
ctrl->u.inet_v.matchlist = iml;
}
*control = ctrl;
return (ISC_R_SUCCESS);
}
isc_result_t
dns_c_ctrlunix_new(isc_mem_t *mem, dns_c_ctrl_t **control,
1999-10-02 21:23:11 +00:00
const char *path, int perm, uid_t uid, gid_t gid)
1999-07-19 13:25:18 +00:00
{
dns_c_ctrl_t *ctrl;
REQUIRE(mem != NULL);
REQUIRE(control != NULL);
ctrl = isc_mem_get(mem, sizeof *ctrl);
if (ctrl == NULL) {
return (ISC_R_NOMEMORY);
}
ctrl->magic = DNS_C_CONFCTL_MAGIC;
1999-07-19 13:25:18 +00:00
ctrl->mem = mem;
ctrl->control_type = dns_c_unix_control;
ctrl->u.unix_v.pathname = isc_mem_strdup(mem, path);
if (ctrl->u.unix_v.pathname == NULL) {
isc_mem_put(mem, ctrl, sizeof *ctrl);
1999-10-02 21:23:11 +00:00
/* XXXJAB logwrite */
1999-07-19 13:25:18 +00:00
return (ISC_R_NOMEMORY);
}
ctrl->u.unix_v.perm = perm;
ctrl->u.unix_v.owner = uid;
ctrl->u.unix_v.group = gid;
ctrl->keyidlist = NULL;
1999-07-19 13:25:18 +00:00
*control = ctrl;
return (ISC_R_SUCCESS);
}
isc_result_t
dns_c_ctrl_delete(dns_c_ctrl_t **control) {
1999-10-16 01:34:30 +00:00
isc_result_t res = ISC_R_SUCCESS;
1999-07-19 13:25:18 +00:00
isc_mem_t *mem;
dns_c_ctrl_t *ctrl;
REQUIRE(control != NULL);
REQUIRE(*control != NULL);
1999-07-19 13:25:18 +00:00
ctrl = *control;
REQUIRE(DNS_C_CONFCTL_VALID(ctrl));
1999-10-16 01:34:30 +00:00
1999-07-19 13:25:18 +00:00
mem = ctrl->mem;
switch (ctrl->control_type) {
case dns_c_inet_control:
if (ctrl->u.inet_v.matchlist != NULL)
res = dns_c_ipmatchlist_detach(&ctrl->
u.inet_v.matchlist);
else
res = ISC_R_SUCCESS;
1999-07-19 13:25:18 +00:00
break;
case dns_c_unix_control:
isc_mem_free(mem, ctrl->u.unix_v.pathname);
res = ISC_R_SUCCESS;
break;
}
if (ctrl->keyidlist != NULL) {
dns_c_kidlist_delete(&ctrl->keyidlist);
}
1999-10-16 01:34:30 +00:00
ctrl->magic = 0;
1999-07-19 13:25:18 +00:00
isc_mem_put(mem, ctrl, sizeof *ctrl);
*control = NULL;
1999-10-16 01:34:30 +00:00
return (res);
1999-07-19 13:25:18 +00:00
}
void
dns_c_ctrl_print(FILE *fp, int indent, dns_c_ctrl_t *ctl) {
2000-01-06 16:15:29 +00:00
in_port_t port;
dns_c_ipmatchlist_t *iml;
1999-07-19 13:25:18 +00:00
REQUIRE(DNS_C_CONFCTL_VALID(ctl));
1999-10-16 01:34:30 +00:00
1999-07-19 13:25:18 +00:00
(void) indent;
if (ctl->control_type == dns_c_inet_control) {
port = ctl->u.inet_v.port;
iml = ctl->u.inet_v.matchlist;
fprintf(fp, "inet ");
dns_c_print_ipaddr(fp, &ctl->u.inet_v.addr);
if (port == 0) {
1999-07-19 13:25:18 +00:00
fprintf(fp, " port *\n");
} else {
fprintf(fp, " port %d\n", port);
1999-07-19 13:25:18 +00:00
}
dns_c_printtabs(fp, indent + 1);
1999-07-19 13:25:18 +00:00
fprintf(fp, "allow ");
dns_c_ipmatchlist_print(fp, indent + 2, iml);
if (ctl->keyidlist != NULL) {
fprintf(fp, "\n");
dns_c_kidlist_print(fp, indent + 1, ctl->keyidlist);
}
fprintf(fp, ";\n");
1999-07-19 13:25:18 +00:00
} else {
/* The "#" means force a leading zero */
fprintf(fp, "unix \"%s\" perm %#o owner %lu group %lu;\n",
1999-07-19 13:25:18 +00:00
ctl->u.unix_v.pathname,
ctl->u.unix_v.perm,
(unsigned long)ctl->u.unix_v.owner,
(unsigned long)ctl->u.unix_v.group);
1999-07-19 13:25:18 +00:00
}
}
dns_c_ctrl_t *
dns_c_ctrllist_head (dns_c_ctrllist_t *list)
{
REQUIRE(DNS_C_CONFCTLLIST_VALID(list));
return(ISC_LIST_HEAD(list->elements));
}
dns_c_ctrl_t *
dns_c_ctrl_next(dns_c_ctrl_t *ctl)
{
REQUIRE(DNS_C_CONFCTL_VALID(ctl));
return (ISC_LIST_NEXT(ctl, next));
}