statschannel.c revision cffe96e26744abcf33494837b234219046a631d8
dd1ce8b52478fa98c844720af9e77fae2978f18dTinderbox User/*
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont * Copyright (C) 2008 Internet Systems Consortium, Inc. ("ISC")
4a14ce5ba00ab7bc55c99ffdcf59c7a4ab902721Automatic Updater *
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont * Permission to use, copy, modify, and/or distribute this software for any
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont * purpose with or without fee is hereby granted, provided that the above
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont * copyright notice and this permission notice appear in all copies.
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont *
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont * PERFORMANCE OF THIS SOFTWARE.
ea94d370123a5892f6c47a97f21d1b28d44bb168Tinderbox User */
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont/* $Id: statschannel.c,v 1.6 2008/03/31 05:00:29 marka Exp $ */
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont/*! \file */
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont#include <config.h>
b27ce68bae92006e2ad7a9b75602c6385e529c3bAutomatic Updater
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont#include <isc/buffer.h>
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont#include <isc/httpd.h>
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont#include <isc/mem.h>
b27ce68bae92006e2ad7a9b75602c6385e529c3bAutomatic Updater#include <isc/print.h>
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont#include <isc/socket.h>
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont#include <isc/task.h>
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont#include <dns/stats.h>
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont#include <dns/view.h>
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont#include <named/log.h>
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont#include <named/server.h>
a3f8c8e20780e488141d200acdfea6c5f3303513Automatic Updater#include <named/statschannel.h>
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupontstruct ns_statschannel {
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont /* Unlocked */
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont isc_httpdmgr_t *httpdmgr;
c7d32c0b0ff4c01f0d4479af3410d3c06044d48aAutomatic Updater isc_sockaddr_t address;
c7d32c0b0ff4c01f0d4479af3410d3c06044d48aAutomatic Updater isc_mem_t *mctx;
c7d32c0b0ff4c01f0d4479af3410d3c06044d48aAutomatic Updater
c7d32c0b0ff4c01f0d4479af3410d3c06044d48aAutomatic Updater /*
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont * Locked by channel lock: can be refererenced and modified by both
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont * the server task and the channel task.
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont */
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont isc_mutex_t lock;
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont dns_acl_t *acl;
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont
aaaf8d4f4873d21e55c3ffb4f656203d08339865Mark Andrews /* Locked by server task */
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont ISC_LINK(struct ns_statschannel) link;
2a6d4c9948b3f4f31311bd799d114585a30419a9Automatic Updater};
2a6d4c9948b3f4f31311bd799d114585a30419a9Automatic Updater
8e821eea5f57ac47a94305aa7ab0c3570d92a311Automatic Updater#ifdef HAVE_LIBXML2
8e821eea5f57ac47a94305aa7ab0c3570d92a311Automatic Updater
8e821eea5f57ac47a94305aa7ab0c3570d92a311Automatic Updater/* XXXMLG below here sucks. */
2a6d4c9948b3f4f31311bd799d114585a30419a9Automatic Updater
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont#define TRY(a) do { result = (a); INSIST(result == ISC_R_SUCCESS); } while(0);
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont#define TRY0(a) do { xmlrc = (a); INSIST(xmlrc >= 0); } while(0);
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont#define NODES 8
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont#define SPACES 3
2a6d4c9948b3f4f31311bd799d114585a30419a9Automatic Updater
2a6d4c9948b3f4f31311bd799d114585a30419a9Automatic Updaterstatic void
2a6d4c9948b3f4f31311bd799d114585a30419a9Automatic Updatergeneratexml(ns_server_t *server, int *buflen, xmlChar **buf) {
2a6d4c9948b3f4f31311bd799d114585a30419a9Automatic Updater char boottime[sizeof "yyyy-mm-ddThh:mm:ssZ"];
2a6d4c9948b3f4f31311bd799d114585a30419a9Automatic Updater char nowstr[sizeof "yyyy-mm-ddThh:mm:ssZ"];
8ec3c085233cedb22b05da36e2773c8f357a7e45Automatic Updater isc_time_t now;
8ec3c085233cedb22b05da36e2773c8f357a7e45Automatic Updater xmlTextWriterPtr writer;
6ea2385360e9e2167e65f9286447da9eea189457Tinderbox User xmlDocPtr doc;
6ea2385360e9e2167e65f9286447da9eea189457Tinderbox User int xmlrc;
6ea2385360e9e2167e65f9286447da9eea189457Tinderbox User dns_view_t *view;
8ec3c085233cedb22b05da36e2773c8f357a7e45Automatic Updater int i;
8ec3c085233cedb22b05da36e2773c8f357a7e45Automatic Updater isc_uint64_t counters[DNS_STATS_NCOUNTERS];
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont isc_time_now(&now);
bbbf2e27d3a981163dab139497d6b2dc85449db0Tinderbox User isc_time_formatISO8601(&ns_g_boottime, boottime, sizeof boottime);
bbbf2e27d3a981163dab139497d6b2dc85449db0Tinderbox User isc_time_formatISO8601(&now, nowstr, sizeof nowstr);
bbbf2e27d3a981163dab139497d6b2dc85449db0Tinderbox User
bbbf2e27d3a981163dab139497d6b2dc85449db0Tinderbox User writer = xmlNewTextWriterDoc(&doc, 0);
bbbf2e27d3a981163dab139497d6b2dc85449db0Tinderbox User TRY0(xmlTextWriterStartDocument(writer, NULL, "UTF-8", NULL));
bbbf2e27d3a981163dab139497d6b2dc85449db0Tinderbox User TRY0(xmlTextWriterWritePI(writer, ISC_XMLCHAR "xml-stylesheet",
bbbf2e27d3a981163dab139497d6b2dc85449db0Tinderbox User ISC_XMLCHAR "type=\"text/xsl\" href=\"/bind9.xsl\""));
bbbf2e27d3a981163dab139497d6b2dc85449db0Tinderbox User TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "isc"));
bbbf2e27d3a981163dab139497d6b2dc85449db0Tinderbox User TRY0(xmlTextWriterWriteAttribute(writer, ISC_XMLCHAR "version",
bbbf2e27d3a981163dab139497d6b2dc85449db0Tinderbox User ISC_XMLCHAR "1.0"));
bbbf2e27d3a981163dab139497d6b2dc85449db0Tinderbox User
bbbf2e27d3a981163dab139497d6b2dc85449db0Tinderbox User TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "bind"));
bbbf2e27d3a981163dab139497d6b2dc85449db0Tinderbox User TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "statistics"));
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont TRY0(xmlTextWriterWriteAttribute(writer, ISC_XMLCHAR "version",
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont ISC_XMLCHAR "1.0"));
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont /*
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont * Start by rendering the views we know of here. For each view we
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont * know of, call its rendering function.
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont */
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont view = ISC_LIST_HEAD(server->viewlist);
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "views"));
c7d32c0b0ff4c01f0d4479af3410d3c06044d48aAutomatic Updater while (view != NULL) {
c7d32c0b0ff4c01f0d4479af3410d3c06044d48aAutomatic Updater dns_view_xmlrender(view, writer, ISC_XML_RENDERALL);
c7d32c0b0ff4c01f0d4479af3410d3c06044d48aAutomatic Updater view = ISC_LIST_NEXT(view, link);
c7d32c0b0ff4c01f0d4479af3410d3c06044d48aAutomatic Updater }
c7d32c0b0ff4c01f0d4479af3410d3c06044d48aAutomatic Updater TRY0(xmlTextWriterEndElement(writer)); /* views */
c7d32c0b0ff4c01f0d4479af3410d3c06044d48aAutomatic Updater
c7d32c0b0ff4c01f0d4479af3410d3c06044d48aAutomatic Updater TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "socketmgr"));
c7d32c0b0ff4c01f0d4479af3410d3c06044d48aAutomatic Updater isc_socketmgr_renderxml(ns_g_socketmgr, writer);
c7d32c0b0ff4c01f0d4479af3410d3c06044d48aAutomatic Updater TRY0(xmlTextWriterEndElement(writer)); /* socketmgr */
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "taskmgr"));
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont isc_taskmgr_renderxml(ns_g_taskmgr, writer);
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont TRY0(xmlTextWriterEndElement(writer)); /* taskmgr */
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "server"));
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont xmlTextWriterStartElement(writer, ISC_XMLCHAR "boot-time");
c7d32c0b0ff4c01f0d4479af3410d3c06044d48aAutomatic Updater xmlTextWriterWriteString(writer, ISC_XMLCHAR boottime);
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont xmlTextWriterEndElement(writer);
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont xmlTextWriterStartElement(writer, ISC_XMLCHAR "current-time");
f8e3e03cacd16ffb923a9603fca23a9e1a1fee07Automatic Updater xmlTextWriterWriteString(writer, ISC_XMLCHAR nowstr);
f8e3e03cacd16ffb923a9603fca23a9e1a1fee07Automatic Updater xmlTextWriterEndElement(writer);
f8e3e03cacd16ffb923a9603fca23a9e1a1fee07Automatic Updater TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "counters"));
f8e3e03cacd16ffb923a9603fca23a9e1a1fee07Automatic Updater dns_stats_copy(server->querystats, counters);
f8e3e03cacd16ffb923a9603fca23a9e1a1fee07Automatic Updater for (i = 0; i < DNS_STATS_NCOUNTERS; i++) {
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont xmlTextWriterStartElement(writer,
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont ISC_XMLCHAR dns_statscounter_names[i]);
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont xmlTextWriterWriteFormatString(writer,
c7d32c0b0ff4c01f0d4479af3410d3c06044d48aAutomatic Updater "%" ISC_PRINT_QUADFORMAT "u",
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont counters[i]);
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont xmlTextWriterEndElement(writer);
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater }
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater xmlTextWriterEndElement(writer); /* counters */
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater xmlTextWriterEndElement(writer); /* server */
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "memory"));
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont isc_mem_renderxml(writer);
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont TRY0(xmlTextWriterEndElement(writer)); /* memory */
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont TRY0(xmlTextWriterEndElement(writer)); /* statistics */
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont TRY0(xmlTextWriterEndElement(writer)); /* bind */
a3f8c8e20780e488141d200acdfea6c5f3303513Automatic Updater TRY0(xmlTextWriterEndElement(writer)); /* isc */
a3f8c8e20780e488141d200acdfea6c5f3303513Automatic Updater
a3f8c8e20780e488141d200acdfea6c5f3303513Automatic Updater TRY0(xmlTextWriterEndDocument(writer));
a3f8c8e20780e488141d200acdfea6c5f3303513Automatic Updater
a3f8c8e20780e488141d200acdfea6c5f3303513Automatic Updater xmlFreeTextWriter(writer);
a3f8c8e20780e488141d200acdfea6c5f3303513Automatic Updater
a3f8c8e20780e488141d200acdfea6c5f3303513Automatic Updater xmlDocDumpFormatMemoryEnc(doc, buf, buflen, "UTF-8", 1);
a3f8c8e20780e488141d200acdfea6c5f3303513Automatic Updater xmlFreeDoc(doc);
a3f8c8e20780e488141d200acdfea6c5f3303513Automatic Updater}
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupontstatic void
c7d32c0b0ff4c01f0d4479af3410d3c06044d48aAutomatic Updaterwrap_xmlfree(isc_buffer_t *buffer, void *arg) {
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont UNUSED(arg);
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont xmlFree(isc_buffer_base(buffer));
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont}
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupontstatic isc_result_t
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupontrender_index(const char *url, const char *querystring, void *arg,
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont unsigned int *retcode, const char **retmsg, const char **mimetype,
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont isc_buffer_t *b, isc_httpdfree_t **freecb,
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont void **freecb_args)
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont{
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont unsigned char *msg;
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont int msglen;
a3416b0a1b5482b6df32839445ca98c016945570Automatic Updater ns_server_t *server = arg;
a3416b0a1b5482b6df32839445ca98c016945570Automatic Updater
a3416b0a1b5482b6df32839445ca98c016945570Automatic Updater UNUSED(url);
a3416b0a1b5482b6df32839445ca98c016945570Automatic Updater UNUSED(querystring);
a3416b0a1b5482b6df32839445ca98c016945570Automatic Updater
c7d32c0b0ff4c01f0d4479af3410d3c06044d48aAutomatic Updater generatexml(server, &msglen, &msg);
c7d32c0b0ff4c01f0d4479af3410d3c06044d48aAutomatic Updater
bbbf2e27d3a981163dab139497d6b2dc85449db0Tinderbox User *retcode = 200;
c7d32c0b0ff4c01f0d4479af3410d3c06044d48aAutomatic Updater *retmsg = "OK";
c7d32c0b0ff4c01f0d4479af3410d3c06044d48aAutomatic Updater *mimetype = "text/xml";
c7d32c0b0ff4c01f0d4479af3410d3c06044d48aAutomatic Updater isc_buffer_reinit(b, msg, msglen);
f8e3e03cacd16ffb923a9603fca23a9e1a1fee07Automatic Updater isc_buffer_add(b, msglen);
c7d32c0b0ff4c01f0d4479af3410d3c06044d48aAutomatic Updater *freecb = wrap_xmlfree;
c7d32c0b0ff4c01f0d4479af3410d3c06044d48aAutomatic Updater *freecb_args = NULL;
c7d32c0b0ff4c01f0d4479af3410d3c06044d48aAutomatic Updater
c7d32c0b0ff4c01f0d4479af3410d3c06044d48aAutomatic Updater return (ISC_R_SUCCESS);
5a24d24c8fba3480d707c0c902379ddb36501e12Automatic Updater}
c7d32c0b0ff4c01f0d4479af3410d3c06044d48aAutomatic Updater
c7d32c0b0ff4c01f0d4479af3410d3c06044d48aAutomatic Updater#endif /* HAVE_LIBXML2 */
c7d32c0b0ff4c01f0d4479af3410d3c06044d48aAutomatic Updater
c7d32c0b0ff4c01f0d4479af3410d3c06044d48aAutomatic Updaterstatic isc_result_t
c7d32c0b0ff4c01f0d4479af3410d3c06044d48aAutomatic Updaterrender_xsl(const char *url, const char *querystring, void *args,
c7d32c0b0ff4c01f0d4479af3410d3c06044d48aAutomatic Updater unsigned int *retcode, const char **retmsg, const char **mimetype,
c7d32c0b0ff4c01f0d4479af3410d3c06044d48aAutomatic Updater isc_buffer_t *b, isc_httpdfree_t **freecb,
79cf9524b15ca65f55fd6913e6cf01b5581c588aAutomatic Updater void **freecb_args)
c7d32c0b0ff4c01f0d4479af3410d3c06044d48aAutomatic Updater{
f8e3e03cacd16ffb923a9603fca23a9e1a1fee07Automatic Updater#include "bind9.xsl.h"
c7d32c0b0ff4c01f0d4479af3410d3c06044d48aAutomatic Updater
c7d32c0b0ff4c01f0d4479af3410d3c06044d48aAutomatic Updater UNUSED(url);
c7d32c0b0ff4c01f0d4479af3410d3c06044d48aAutomatic Updater UNUSED(querystring);
c7d32c0b0ff4c01f0d4479af3410d3c06044d48aAutomatic Updater UNUSED(args);
f8e3e03cacd16ffb923a9603fca23a9e1a1fee07Automatic Updater
c7d32c0b0ff4c01f0d4479af3410d3c06044d48aAutomatic Updater *retcode = 200;
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont *retmsg = "OK";
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont *mimetype = "text/xslt+xml";
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont isc_buffer_reinit(b, msg, strlen(msg));
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont isc_buffer_add(b, strlen(msg));
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont *freecb = NULL;
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont *freecb_args = NULL;
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont return (ISC_R_SUCCESS);
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont}
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupontstatic void
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupontshutdown_listener(ns_statschannel_t *listener) {
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont char socktext[ISC_SOCKADDR_FORMATSIZE];
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont isc_sockaddr_format(&listener->address, socktext, sizeof(socktext));
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,NS_LOGMODULE_SERVER,
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont ISC_LOG_NOTICE, "stopping statistics channel on %s",
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont socktext);
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont isc_httpdmgr_shutdown(&listener->httpdmgr);
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont}
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupontstatic isc_boolean_t
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupontclient_ok(const isc_sockaddr_t *fromaddr, void *arg) {
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont ns_statschannel_t *listener = arg;
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont isc_netaddr_t netaddr;
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont char socktext[ISC_SOCKADDR_FORMATSIZE];
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont int match;
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont REQUIRE(listener != NULL);
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont isc_netaddr_fromsockaddr(&netaddr, fromaddr);
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont LOCK(&listener->lock);
c7d32c0b0ff4c01f0d4479af3410d3c06044d48aAutomatic Updater if (dns_acl_match(&netaddr, NULL, listener->acl, &ns_g_server->aclenv,
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont &match, NULL) == ISC_R_SUCCESS && match > 0) {
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont UNLOCK(&listener->lock);
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont return (ISC_TRUE);
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont }
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont UNLOCK(&listener->lock);
bbbf2e27d3a981163dab139497d6b2dc85449db0Tinderbox User
bbbf2e27d3a981163dab139497d6b2dc85449db0Tinderbox User isc_sockaddr_format(fromaddr, socktext, sizeof(socktext));
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont NS_LOGMODULE_SERVER, ISC_LOG_WARNING,
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont "rejected statistics connection from %s", socktext);
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont
dd1ce8b52478fa98c844720af9e77fae2978f18dTinderbox User return (ISC_FALSE);
90f35c2f2a1c660f3b96eec413036d238df395f6Francis Dupont}
static void
destroy_listener(void *arg) {
ns_statschannel_t *listener = arg;
REQUIRE(listener != NULL);
REQUIRE(!ISC_LINK_LINKED(listener, link));
/* We don't to have acquire the lock here since it's already unlinked */
dns_acl_detach(&listener->acl);
DESTROYLOCK(&listener->lock);
isc_mem_putanddetach(&listener->mctx, listener, sizeof(*listener));
}
static isc_result_t
add_listener(ns_server_t *server, ns_statschannel_t **listenerp,
const cfg_obj_t *listen_params, const cfg_obj_t *config,
isc_sockaddr_t *addr, cfg_aclconfctx_t *aclconfctx,
const char *socktext)
{
isc_result_t result;
ns_statschannel_t *listener;
isc_task_t *task = NULL;
isc_socket_t *sock = NULL;
const cfg_obj_t *allow;
dns_acl_t *new_acl = NULL;
listener = isc_mem_get(server->mctx, sizeof(*listener));
if (listener == NULL)
return (ISC_R_NOMEMORY);
listener->httpdmgr = NULL;
listener->address = *addr;
listener->acl = NULL;
listener->mctx = NULL;
ISC_LINK_INIT(listener, link);
result = isc_mutex_init(&listener->lock);
if (result != ISC_R_SUCCESS) {
isc_mem_put(server->mctx, listener, sizeof(*listener));
return (ISC_R_FAILURE);
}
isc_mem_attach(server->mctx, &listener->mctx);
allow = cfg_tuple_get(listen_params, "allow");
if (allow != NULL && cfg_obj_islist(allow)) {
result = cfg_acl_fromconfig(allow, config, ns_g_lctx,
aclconfctx, listener->mctx, 0,
&new_acl);
} else
result = dns_acl_any(listener->mctx, &new_acl);
if (result != ISC_R_SUCCESS)
goto cleanup;
dns_acl_attach(new_acl, &listener->acl);
dns_acl_detach(&new_acl);
result = isc_task_create(ns_g_taskmgr, 0, &task);
if (result != ISC_R_SUCCESS)
goto cleanup;
isc_task_setname(task, "statchannel", NULL);
result = isc_socket_create(ns_g_socketmgr, isc_sockaddr_pf(addr),
isc_sockettype_tcp, &sock);
if (result != ISC_R_SUCCESS)
goto cleanup;
isc_socket_setname(sock, "statchannel", NULL);
#ifndef ISC_ALLOW_MAPPED
isc_socket_ipv6only(sock, ISC_TRUE);
#endif
result = isc_socket_bind(sock, addr);
if (result != ISC_R_SUCCESS)
goto cleanup;
result = isc_httpdmgr_create(server->mctx, sock, task, client_ok,
destroy_listener, listener, ns_g_timermgr,
&listener->httpdmgr);
if (result != ISC_R_SUCCESS)
goto cleanup;
#ifdef HAVE_LIBXML2
isc_httpdmgr_addurl(listener->httpdmgr, "/", render_index, server);
#endif
isc_httpdmgr_addurl(listener->httpdmgr, "/bind9.xsl", render_xsl,
server);
*listenerp = listener;
isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
NS_LOGMODULE_SERVER, ISC_LOG_NOTICE,
"statistics channel listening on %s", socktext);
cleanup:
if (result != ISC_R_SUCCESS) {
if (listener->acl != NULL)
dns_acl_detach(&listener->acl);
DESTROYLOCK(&listener->lock);
isc_mem_putanddetach(&listener->mctx, listener,
sizeof(*listener));
}
if (task != NULL)
isc_task_detach(&task);
if (sock != NULL)
isc_socket_detach(&sock);
return (result);
}
static void
update_listener(ns_server_t *server, ns_statschannel_t **listenerp,
const cfg_obj_t *listen_params, const cfg_obj_t *config,
isc_sockaddr_t *addr, cfg_aclconfctx_t *aclconfctx,
const char *socktext)
{
ns_statschannel_t *listener;
const cfg_obj_t *allow = NULL;
dns_acl_t *new_acl = NULL;
isc_result_t result = ISC_R_SUCCESS;
for (listener = ISC_LIST_HEAD(server->statschannels);
listener != NULL;
listener = ISC_LIST_NEXT(listener, link))
if (isc_sockaddr_equal(addr, &listener->address))
break;
if (listener == NULL) {
*listenerp = NULL;
return;
}
/*
* Now, keep the old access list unless a new one can be made.
*/
allow = cfg_tuple_get(listen_params, "allow");
if (allow != NULL && cfg_obj_islist(allow)) {
result = cfg_acl_fromconfig(allow, config, ns_g_lctx,
aclconfctx, listener->mctx, 0,
&new_acl);
} else
result = dns_acl_any(listener->mctx, &new_acl);
if (result == ISC_R_SUCCESS) {
LOCK(&listener->lock);
dns_acl_detach(&listener->acl);
dns_acl_attach(new_acl, &listener->acl);
dns_acl_detach(&new_acl);
UNLOCK(&listener->lock);
} else {
cfg_obj_log(listen_params, ns_g_lctx, ISC_LOG_WARNING,
"couldn't install new acl for "
"statistics channel %s: %s",
socktext, isc_result_totext(result));
}
*listenerp = listener;
}
isc_result_t
ns_statschannels_configure(ns_server_t *server, const cfg_obj_t *config,
cfg_aclconfctx_t *aclconfctx)
{
ns_statschannel_t *listener, *listener_next;
ns_statschannellist_t new_listeners;
const cfg_obj_t *statschannellist = NULL;
const cfg_listelt_t *element, *element2;
char socktext[ISC_SOCKADDR_FORMATSIZE];
ISC_LIST_INIT(new_listeners);
/*
* Get the list of named.conf 'statistics-channels' statements.
*/
(void)cfg_map_get(config, "statistics-channels", &statschannellist);
/*
* Run through the new address/port list, noting sockets that are
* already being listened on and moving them to the new list.
*
* Identifying duplicate addr/port combinations is left to either
* the underlying config code, or to the bind attempt getting an
* address-in-use error.
*/
if (statschannellist != NULL) {
#ifndef HAVE_LIBXML2
isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
NS_LOGMODULE_SERVER, ISC_LOG_WARNING,
"statistics-channels specified but not effective "
"due to missing XML library");
#endif
for (element = cfg_list_first(statschannellist);
element != NULL;
element = cfg_list_next(element)) {
const cfg_obj_t *statschannel;
const cfg_obj_t *listenercfg = NULL;
statschannel = cfg_listelt_value(element);
(void)cfg_map_get(statschannel, "inet",
&listenercfg);
if (listenercfg == NULL)
continue;
for (element2 = cfg_list_first(listenercfg);
element2 != NULL;
element2 = cfg_list_next(element2)) {
const cfg_obj_t *listen_params;
const cfg_obj_t *obj;
isc_sockaddr_t addr;
listen_params = cfg_listelt_value(element2);
obj = cfg_tuple_get(listen_params, "address");
addr = *cfg_obj_assockaddr(obj);
if (isc_sockaddr_getport(&addr) == 0)
isc_sockaddr_setport(&addr, NS_STATSCHANNEL_HTTPPORT);
isc_sockaddr_format(&addr, socktext,
sizeof(socktext));
isc_log_write(ns_g_lctx,
NS_LOGCATEGORY_GENERAL,
NS_LOGMODULE_SERVER,
ISC_LOG_DEBUG(9),
"processing statistics "
"channel %s",
socktext);
update_listener(server, &listener,
listen_params, config, &addr,
aclconfctx, socktext);
if (listener != NULL) {
/*
* Remove the listener from the old
* list, so it won't be shut down.
*/
ISC_LIST_UNLINK(server->statschannels,
listener, link);
} else {
/*
* This is a new listener.
*/
isc_result_t r;
r = add_listener(server, &listener,
listen_params, config,
&addr, aclconfctx,
socktext);
if (r != ISC_R_SUCCESS) {
cfg_obj_log(listen_params,
ns_g_lctx,
ISC_LOG_WARNING,
"couldn't allocate "
"statistics channel"
" %s: %s",
socktext,
isc_result_totext(r));
}
}
if (listener != NULL)
ISC_LIST_APPEND(new_listeners, listener,
link);
}
}
}
for (listener = ISC_LIST_HEAD(server->statschannels);
listener != NULL;
listener = listener_next) {
listener_next = ISC_LIST_NEXT(listener, link);
ISC_LIST_UNLINK(server->statschannels, listener, link);
shutdown_listener(listener);
}
ISC_LIST_APPENDLIST(server->statschannels, new_listeners, link);
return (ISC_R_SUCCESS);
}
void
ns_statschannels_shutdown(ns_server_t *server) {
ns_statschannel_t *listener;
while ((listener = ISC_LIST_HEAD(server->statschannels)) != NULL) {
ISC_LIST_UNLINK(server->statschannels, listener, link);
shutdown_listener(listener);
}
}