2011-12-09 16:04:51 +01:00
|
|
|
#!@PYTHON@
|
|
|
|
|
2011-12-14 21:34:28 -08:00
|
|
|
# Copyright (C) 2011 Internet Systems Consortium.
|
2011-12-09 16:04:51 +01: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 SYSTEMS CONSORTIUM
|
|
|
|
# DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
|
|
|
|
# INTERNET SYSTEMS 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.
|
|
|
|
|
|
|
|
|
|
|
|
import sys; sys.path.append ('@@PYTHONPATH@@')
|
|
|
|
import isc
|
2012-05-30 00:28:36 -07:00
|
|
|
from isc.acl.dns import REQUEST_LOADER
|
2011-12-09 18:09:29 +01:00
|
|
|
import bind10_config
|
2011-12-09 16:04:51 +01:00
|
|
|
from isc.dns import *
|
2012-05-25 16:31:22 -07:00
|
|
|
import isc.ddns.session
|
2012-05-29 21:32:00 -07:00
|
|
|
from isc.ddns.zone_config import ZoneConfig
|
2012-06-05 00:31:40 -07:00
|
|
|
from isc.ddns.logger import ClientFormatter, ZoneFormatter
|
2011-12-09 16:04:51 +01:00
|
|
|
from isc.config.ccsession import *
|
2012-06-05 00:31:40 -07:00
|
|
|
from isc.cc import SessionError, SessionTimeout, ProtocolError
|
2011-12-09 16:04:51 +01:00
|
|
|
import isc.util.process
|
2012-01-25 11:04:55 +01:00
|
|
|
import isc.util.cio.socketsession
|
2012-06-05 00:31:40 -07:00
|
|
|
from isc.notify.notify_out import ZONE_NEW_DATA_READY_CMD
|
2012-05-29 14:51:12 -07:00
|
|
|
import isc.server_common.tsig_keyring
|
2012-06-07 15:02:55 -07:00
|
|
|
from isc.server_common.dns_tcp import DNSTCPContext
|
2012-05-29 21:32:00 -07:00
|
|
|
from isc.datasrc import DataSourceClient
|
2011-12-29 19:43:42 +01:00
|
|
|
import select
|
2012-01-19 10:52:11 +01:00
|
|
|
import errno
|
2011-12-09 16:04:51 +01:00
|
|
|
|
|
|
|
from isc.log_messages.ddns_messages import *
|
|
|
|
|
2011-12-09 18:09:29 +01:00
|
|
|
from optparse import OptionParser, OptionValueError
|
|
|
|
import os
|
2012-01-20 10:31:17 +01:00
|
|
|
import os.path
|
2011-12-09 18:09:29 +01:00
|
|
|
import signal
|
2012-01-20 10:31:17 +01:00
|
|
|
import socket
|
2011-12-09 18:09:29 +01:00
|
|
|
|
2011-12-09 16:04:51 +01:00
|
|
|
isc.log.init("b10-ddns")
|
|
|
|
logger = isc.log.Logger("ddns")
|
2012-01-19 10:52:34 +01:00
|
|
|
TRACE_BASIC = logger.DBGLVL_TRACE_BASIC
|
2011-12-09 18:09:29 +01:00
|
|
|
|
2012-05-29 21:32:00 -07:00
|
|
|
# Well known path settings. We need to define
|
|
|
|
# SPECFILE_LOCATION: ddns configuration spec file
|
|
|
|
# SOCKET_FILE: Unix domain socket file to communicate with b10-auth
|
|
|
|
# AUTH_SPECFILE_LOCATION: b10-auth configuration spec file (tentatively
|
|
|
|
# necessarily for sqlite3-only-and-older-datasrc-API stuff). This should be
|
|
|
|
# gone once we migrate to the new API and start using generalized config.
|
|
|
|
#
|
2012-05-16 10:53:30 +05:30
|
|
|
# If B10_FROM_SOURCE is set in the environment, we use data files
|
|
|
|
# from a directory relative to that, otherwise we use the ones
|
|
|
|
# installed on the system
|
2011-12-20 14:44:27 +01:00
|
|
|
if "B10_FROM_SOURCE" in os.environ:
|
2012-05-29 21:32:00 -07:00
|
|
|
SPECFILE_PATH = os.environ["B10_FROM_SOURCE"] + "/src/bin/ddns"
|
2012-05-16 10:53:30 +05:30
|
|
|
else:
|
|
|
|
PREFIX = "@prefix@"
|
|
|
|
DATAROOTDIR = "@datarootdir@"
|
2012-05-29 21:32:00 -07:00
|
|
|
SPECFILE_PATH = "@datadir@/@PACKAGE@".replace("${datarootdir}", DATAROOTDIR)
|
|
|
|
SPECFILE_PATH = SPECFILE_PATH.replace("${prefix}", PREFIX)
|
2012-05-16 10:53:30 +05:30
|
|
|
|
2012-01-25 10:27:14 +01:00
|
|
|
if "B10_FROM_BUILD" in os.environ:
|
2012-05-29 21:32:00 -07:00
|
|
|
AUTH_SPECFILE_PATH = os.environ["B10_FROM_BUILD"] + "/src/bin/auth"
|
2012-01-25 10:27:14 +01:00
|
|
|
if "B10_FROM_SOURCE_LOCALSTATEDIR" in os.environ:
|
2012-05-29 21:32:00 -07:00
|
|
|
SOCKET_FILE_PATH = os.environ["B10_FROM_SOURCE_LOCALSTATEDIR"]
|
2012-01-25 10:27:14 +01:00
|
|
|
else:
|
2012-05-29 21:32:00 -07:00
|
|
|
SOCKET_FILE_PATH = os.environ["B10_FROM_BUILD"]
|
|
|
|
else:
|
|
|
|
SOCKET_FILE_PATH = bind10_config.DATA_PATH
|
|
|
|
AUTH_SPECFILE_PATH = SPECFILE_PATH
|
|
|
|
|
|
|
|
SPECFILE_LOCATION = SPECFILE_PATH + "/ddns.spec"
|
|
|
|
SOCKET_FILE = SOCKET_FILE_PATH + '/ddns_socket'
|
|
|
|
AUTH_SPECFILE_LOCATION = AUTH_SPECFILE_PATH + '/auth.spec'
|
2011-12-09 16:04:51 +01:00
|
|
|
|
|
|
|
isc.util.process.rename()
|
|
|
|
|
2012-06-05 00:31:40 -07:00
|
|
|
# Cooperating modules
|
|
|
|
XFROUT_MODULE_NAME = 'Xfrout'
|
|
|
|
|
2011-12-09 16:04:51 +01:00
|
|
|
class DDNSConfigError(Exception):
|
2011-12-15 14:47:09 +01:00
|
|
|
'''An exception indicating an error in updating ddns configuration.
|
2011-12-09 16:04:51 +01:00
|
|
|
|
2011-12-14 21:34:28 -08:00
|
|
|
This exception is raised when the ddns process encounters an error in
|
2011-12-09 16:04:51 +01:00
|
|
|
handling configuration updates. Not all syntax error can be caught
|
|
|
|
at the module-CC layer, so ddns needs to (explicitly or implicitly)
|
|
|
|
validate the given configuration data itself. When it finds an error
|
|
|
|
it raises this exception (either directly or by converting an exception
|
|
|
|
from other modules) as a unified error in configuration.
|
2011-12-15 14:47:09 +01:00
|
|
|
'''
|
2011-12-09 16:04:51 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
class DDNSSessionError(Exception):
|
2011-12-14 21:34:28 -08:00
|
|
|
'''An exception raised for some unexpected events during a ddns session.
|
2011-12-09 16:04:51 +01:00
|
|
|
'''
|
|
|
|
pass
|
|
|
|
|
2011-12-12 14:35:14 +01:00
|
|
|
class DDNSSession:
|
2011-12-15 14:47:09 +01:00
|
|
|
'''Class to handle one DDNS update'''
|
2011-12-14 16:43:14 -08:00
|
|
|
|
2011-12-12 14:35:14 +01:00
|
|
|
def __init__(self):
|
2011-12-15 14:47:09 +01:00
|
|
|
'''Initialize a DDNS Session'''
|
2011-12-12 14:35:14 +01:00
|
|
|
pass
|
|
|
|
|
2012-01-20 10:31:17 +01:00
|
|
|
def clear_socket():
|
|
|
|
'''
|
|
|
|
Removes the socket file, if it exists.
|
|
|
|
'''
|
|
|
|
if os.path.exists(SOCKET_FILE):
|
|
|
|
os.remove(SOCKET_FILE)
|
|
|
|
|
2012-05-29 21:32:00 -07:00
|
|
|
def get_datasrc_client(cc_session):
|
|
|
|
'''Return data source client for update requests.
|
|
|
|
|
|
|
|
This is supposed to have a very short lifetime and should soon be replaced
|
|
|
|
with generic data source configuration framework. Based on that
|
|
|
|
observation we simply hardcode everything except the SQLite3 database file,
|
|
|
|
which will be retrieved from the auth server configuration (this behavior
|
|
|
|
will also be deprecated). When something goes wrong with it this function
|
|
|
|
still returns a dummy client so that the caller doesn't have to bother
|
|
|
|
to handle the error (which would also have to be replaced anyway).
|
|
|
|
The caller will subsequently call its find_zone method via an update
|
|
|
|
session object, which will result in an exception, and then result in
|
|
|
|
a SERVFAIL response.
|
|
|
|
|
|
|
|
Once we are ready for introducing the general framework, the whole
|
|
|
|
function will simply be removed.
|
|
|
|
|
|
|
|
'''
|
2012-06-01 15:01:24 -07:00
|
|
|
HARDCODED_DATASRC_CLASS = RRClass.IN()
|
|
|
|
file, is_default = cc_session.get_remote_config_value("Auth",
|
|
|
|
"database_file")
|
|
|
|
# See xfrout.py:get_db_file() for this trick:
|
|
|
|
if is_default and "B10_FROM_BUILD" in os.environ:
|
|
|
|
file = os.environ["B10_FROM_BUILD"] + "/bind10_zones.sqlite3"
|
|
|
|
datasrc_config = '{ "database_file": "' + file + '"}'
|
2012-05-29 21:32:00 -07:00
|
|
|
try:
|
|
|
|
return HARDCODED_DATASRC_CLASS, DataSourceClient('sqlite3',
|
|
|
|
datasrc_config)
|
|
|
|
except isc.datasrc.Error as ex:
|
|
|
|
class DummyDataSourceClient:
|
|
|
|
def __init__(self, ex):
|
|
|
|
self.__ex = ex
|
|
|
|
def find_zone(self, zone_name):
|
|
|
|
raise isc.datasrc.Error(self.__ex)
|
|
|
|
return HARDCODED_DATASRC_CLASS, DummyDataSourceClient(ex)
|
|
|
|
|
2011-12-09 16:04:51 +01:00
|
|
|
class DDNSServer:
|
2011-12-15 10:42:54 -08:00
|
|
|
def __init__(self, cc_session=None):
|
2011-12-15 14:47:09 +01:00
|
|
|
'''
|
|
|
|
Initialize the DDNS Server.
|
|
|
|
This sets up a ModuleCCSession for the BIND 10 system.
|
2011-12-15 16:16:50 +01:00
|
|
|
Parameters:
|
2011-12-15 10:42:54 -08:00
|
|
|
cc_session: If None (default), a new ModuleCCSession will be set up.
|
2011-12-15 16:16:50 +01:00
|
|
|
If specified, the given session will be used. This is
|
|
|
|
mainly used for testing.
|
2011-12-15 14:47:09 +01:00
|
|
|
'''
|
2011-12-15 16:16:50 +01:00
|
|
|
if cc_session is not None:
|
|
|
|
self._cc = cc_session
|
|
|
|
else:
|
|
|
|
self._cc = isc.config.ModuleCCSession(SPECFILE_LOCATION,
|
|
|
|
self.config_handler,
|
|
|
|
self.command_handler)
|
|
|
|
|
2012-05-30 00:28:36 -07:00
|
|
|
# Initialize configuration with defaults. Right now 'zones' is the
|
|
|
|
# only configuration, so we simply directly set it here.
|
2011-12-09 18:09:29 +01:00
|
|
|
self._config_data = self._cc.get_full_config()
|
2012-05-30 00:28:36 -07:00
|
|
|
self._zone_config = self.__update_zone_config(
|
|
|
|
self._cc.get_default_value('zones'))
|
2011-12-09 18:09:29 +01:00
|
|
|
self._cc.start()
|
2012-05-29 21:32:00 -07:00
|
|
|
|
2012-05-30 00:28:36 -07:00
|
|
|
# Get necessary configurations from remote modules.
|
2012-05-29 21:32:00 -07:00
|
|
|
self._cc.add_remote_config(AUTH_SPECFILE_LOCATION)
|
2012-05-29 15:26:44 -07:00
|
|
|
isc.server_common.tsig_keyring.init_keyring(self._cc)
|
2012-05-29 21:32:00 -07:00
|
|
|
|
2011-12-15 14:47:09 +01:00
|
|
|
self._shutdown = False
|
2012-01-19 10:52:34 +01:00
|
|
|
# List of the session receivers where we get the requests
|
|
|
|
self._socksession_receivers = {}
|
2012-01-20 10:31:17 +01:00
|
|
|
clear_socket()
|
|
|
|
self._listen_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
|
|
|
self._listen_socket.bind(SOCKET_FILE)
|
|
|
|
self._listen_socket.listen(16)
|
2011-12-09 18:09:29 +01:00
|
|
|
|
2012-05-29 15:48:16 -07:00
|
|
|
# Create reusable resources
|
|
|
|
self.__request_msg = Message(Message.PARSE)
|
|
|
|
self.__response_renderer = MessageRenderer()
|
|
|
|
|
2012-06-07 15:47:21 -07:00
|
|
|
# The following attribute(s) are essentially private, but defined as
|
|
|
|
# "protected" so that test code can customize/inspect them.
|
|
|
|
# They should not be overridden/referenced for any other purposes.
|
2012-05-29 14:51:12 -07:00
|
|
|
#
|
|
|
|
# DDNS Protocol handling class.
|
2012-05-25 16:31:22 -07:00
|
|
|
self._UpdateSessionClass = isc.ddns.session.UpdateSession
|
2012-06-07 18:14:06 -07:00
|
|
|
# Outstanding TCP context: fileno=>(context_obj, dst)
|
2012-06-07 15:47:21 -07:00
|
|
|
self._tcp_ctxs = {}
|
2012-05-25 16:31:22 -07:00
|
|
|
|
2012-06-05 00:31:40 -07:00
|
|
|
class InternalError(Exception):
|
2012-05-25 16:31:22 -07:00
|
|
|
'''Exception for internal errors in an update session.
|
|
|
|
|
|
|
|
This exception is expected to be caught within the server class,
|
|
|
|
only used for controling the code flow.
|
|
|
|
|
|
|
|
'''
|
|
|
|
pass
|
|
|
|
|
2011-12-09 18:09:29 +01:00
|
|
|
def config_handler(self, new_config):
|
|
|
|
'''Update config data.'''
|
2012-05-30 00:28:36 -07:00
|
|
|
try:
|
|
|
|
if 'zones' in new_config:
|
|
|
|
self._zone_config = \
|
|
|
|
self.__update_zone_config(new_config['zones'])
|
|
|
|
return create_answer(0)
|
|
|
|
except Exception as ex:
|
|
|
|
# We catch any exception here. That includes any syntax error
|
|
|
|
# against the configuration spec. The config interface is too
|
|
|
|
# complicated and it's not clear how much validation is performed
|
|
|
|
# there, so, while assuming it's unlikely to happen, we act
|
|
|
|
# proactively.
|
2012-05-30 14:16:56 -07:00
|
|
|
logger.error(DDNS_CONFIG_HANDLER_ERROR, ex)
|
2012-05-30 00:28:36 -07:00
|
|
|
return create_answer(1, "Failed to handle new configuration: " +
|
|
|
|
str(ex))
|
|
|
|
|
|
|
|
def __update_zone_config(self, new_zones_config):
|
|
|
|
'''Handle zones configuration update.'''
|
|
|
|
new_zones = {}
|
|
|
|
for zone_config in new_zones_config:
|
|
|
|
origin = Name(zone_config['origin'])
|
|
|
|
rrclass = RRClass(zone_config['class'])
|
|
|
|
update_acl = zone_config['update_acl']
|
|
|
|
new_zones[(origin, rrclass)] = REQUEST_LOADER.load(update_acl)
|
|
|
|
return new_zones
|
2011-12-09 18:09:29 +01:00
|
|
|
|
|
|
|
def command_handler(self, cmd, args):
|
2011-12-15 14:47:09 +01:00
|
|
|
'''
|
|
|
|
Handle a CC session command, as sent from bindctl or other
|
|
|
|
BIND 10 modules.
|
|
|
|
'''
|
2012-01-24 13:03:30 +01:00
|
|
|
# TODO: Handle exceptions and turn them to an error response
|
2011-12-09 18:09:29 +01:00
|
|
|
if cmd == "shutdown":
|
|
|
|
logger.info(DDNS_RECEIVED_SHUTDOWN_COMMAND)
|
2011-12-19 11:56:33 +01:00
|
|
|
self.trigger_shutdown()
|
2011-12-09 18:09:29 +01:00
|
|
|
answer = create_answer(0)
|
|
|
|
else:
|
2011-12-15 16:16:50 +01:00
|
|
|
answer = create_answer(1, "Unknown command: " + str(cmd))
|
2011-12-09 18:09:29 +01:00
|
|
|
return answer
|
|
|
|
|
2011-12-19 11:56:33 +01:00
|
|
|
def trigger_shutdown(self):
|
|
|
|
'''Initiate a shutdown sequence.
|
|
|
|
|
|
|
|
This method is expected to be called in various ways including
|
|
|
|
in the middle of a signal handler, and is designed to be as simple
|
|
|
|
as possible to minimize side effects. Actual shutdown will take
|
|
|
|
place in a normal control flow.
|
|
|
|
|
2011-12-15 14:47:09 +01:00
|
|
|
'''
|
2011-12-16 18:21:45 +01:00
|
|
|
logger.info(DDNS_SHUTDOWN)
|
2011-12-19 11:56:33 +01:00
|
|
|
self._shutdown = True
|
|
|
|
|
|
|
|
def shutdown_cleanup(self):
|
|
|
|
'''
|
|
|
|
Perform any cleanup that is necessary when shutting down the server.
|
|
|
|
Do NOT call this to initialize shutdown, use trigger_shutdown().
|
|
|
|
|
2012-02-01 12:42:12 +01:00
|
|
|
Currently, it only causes the ModuleCCSession to send a message that
|
|
|
|
this module is stopping.
|
2011-12-19 11:56:33 +01:00
|
|
|
'''
|
2012-02-02 14:45:31 +01:00
|
|
|
self._cc.send_stopping()
|
2011-12-09 16:04:51 +01:00
|
|
|
|
2012-01-03 15:56:05 +01:00
|
|
|
def accept(self):
|
|
|
|
"""
|
|
|
|
Accept another connection and create the session receiver.
|
|
|
|
"""
|
2012-01-24 13:46:08 +01:00
|
|
|
try:
|
2012-06-01 15:05:32 -07:00
|
|
|
(sock, remote_addr) = self._listen_socket.accept()
|
2012-01-24 13:46:08 +01:00
|
|
|
fileno = sock.fileno()
|
|
|
|
logger.debug(TRACE_BASIC, DDNS_NEW_CONN, fileno,
|
2012-06-01 15:05:32 -07:00
|
|
|
remote_addr if remote_addr else '<anonymous address>')
|
2012-01-25 11:04:55 +01:00
|
|
|
receiver = isc.util.cio.socketsession.SocketSessionReceiver(sock)
|
2012-01-24 13:46:08 +01:00
|
|
|
self._socksession_receivers[fileno] = (sock, receiver)
|
2012-01-25 11:04:55 +01:00
|
|
|
except (socket.error, isc.util.cio.socketsession.SocketSessionError) \
|
2012-01-24 13:46:08 +01:00
|
|
|
as e:
|
|
|
|
# These exceptions mean the connection didn't work, but we can
|
|
|
|
# continue with the rest
|
|
|
|
logger.error(DDNS_ACCEPT_FAILURE, e)
|
2012-01-03 15:56:05 +01:00
|
|
|
|
2012-05-29 14:51:12 -07:00
|
|
|
def __check_request_tsig(self, msg, req_data):
|
|
|
|
'''TSIG checker for update requests.
|
|
|
|
|
|
|
|
This is a helper method for handle_request() below. It examines
|
|
|
|
the given update request message to see if it contains a TSIG RR,
|
|
|
|
and verifies the signature if it does. It returs the TSIG context
|
|
|
|
used for the verification, or None if the request doesn't contain
|
|
|
|
a TSIG. If the verification fails it simply raises an exception
|
|
|
|
as handle_request() assumes it should succeed.
|
|
|
|
|
|
|
|
'''
|
|
|
|
tsig_record = msg.get_tsig_record()
|
|
|
|
if tsig_record is None:
|
|
|
|
return None
|
|
|
|
tsig_ctx = TSIGContext(tsig_record.get_name(),
|
|
|
|
tsig_record.get_rdata().get_algorithm(),
|
2012-05-29 15:26:44 -07:00
|
|
|
isc.server_common.tsig_keyring.get_keyring())
|
2012-05-29 14:51:12 -07:00
|
|
|
tsig_error = tsig_ctx.verify(tsig_record, req_data)
|
|
|
|
if tsig_error != TSIGError.NOERROR:
|
2012-06-05 17:04:53 -07:00
|
|
|
raise self.InternalError("Failed to verify request's TSIG: " +
|
|
|
|
str(tsig_error))
|
2012-05-29 14:51:12 -07:00
|
|
|
return tsig_ctx
|
|
|
|
|
2012-05-25 16:31:22 -07:00
|
|
|
def handle_request(self, req_session):
|
2012-01-03 19:15:43 +01:00
|
|
|
"""
|
2012-01-19 10:52:34 +01:00
|
|
|
This is the place where the actual DDNS processing is done. Other
|
|
|
|
methods are either subroutines of this method or methods doing the
|
|
|
|
uninteresting "accounting" stuff, like accepting socket,
|
|
|
|
initialization, etc.
|
2012-01-03 19:15:43 +01:00
|
|
|
|
|
|
|
It is called with the request being session as received from
|
2012-01-19 10:52:34 +01:00
|
|
|
SocketSessionReceiver, i.e. tuple
|
2012-01-03 19:15:43 +01:00
|
|
|
(socket, local_address, remote_address, data).
|
2012-05-25 16:31:22 -07:00
|
|
|
|
2012-05-30 11:58:22 -07:00
|
|
|
In general, this method doesn't propagate exceptions outside the
|
|
|
|
method. Most of protocol or system errors will result in an error
|
|
|
|
response to the update client or dropping the update request.
|
|
|
|
The update session class should also ensure this. Critical exceptions
|
|
|
|
such as memory allocation failure will be propagated, however, and
|
|
|
|
will subsequently terminate the server process.
|
|
|
|
|
2012-05-30 12:31:13 -07:00
|
|
|
Return: True if a response to the request is successfully sent;
|
|
|
|
False otherwise. The return value wouldn't be useful for the server
|
|
|
|
itself; it's provided mainly for testing purposes.
|
|
|
|
|
2012-01-03 19:15:43 +01:00
|
|
|
"""
|
2012-05-25 16:31:22 -07:00
|
|
|
# give tuple elements intuitive names
|
|
|
|
(sock, local_addr, remote_addr, req_data) = req_session
|
|
|
|
|
|
|
|
# The session sender (b10-auth) should have made sure that this is
|
|
|
|
# a validly formed DNS message of OPCODE being UPDATE, and if it's
|
|
|
|
# TSIG signed, its key is known to the system and the signature is
|
|
|
|
# valid. Messages that don't meet these should have been resopnded
|
|
|
|
# or dropped by the sender, so if such error is detected we treat it
|
|
|
|
# as an internal error and don't bother to respond.
|
|
|
|
try:
|
2012-05-29 15:48:16 -07:00
|
|
|
self.__request_msg.clear(Message.PARSE)
|
2012-06-01 23:12:26 -07:00
|
|
|
# specify PRESERVE_ORDER as we need to handle each RR separately.
|
|
|
|
self.__request_msg.from_wire(req_data, Message.PRESERVE_ORDER)
|
2012-05-29 15:48:16 -07:00
|
|
|
if self.__request_msg.get_opcode() != Opcode.UPDATE():
|
2012-06-05 17:04:53 -07:00
|
|
|
raise self.InternalError('Update request has unexpected '
|
|
|
|
'opcode: ' +
|
|
|
|
str(self.__request_msg.get_opcode()))
|
2012-05-29 15:48:16 -07:00
|
|
|
tsig_ctx = self.__check_request_tsig(self.__request_msg, req_data)
|
2012-05-25 16:31:22 -07:00
|
|
|
except Exception as ex:
|
2012-05-29 14:51:12 -07:00
|
|
|
logger.error(DDNS_REQUEST_PARSE_FAIL, ex)
|
2012-05-25 16:31:22 -07:00
|
|
|
return False
|
2012-01-24 13:03:30 +01:00
|
|
|
|
2012-05-29 21:32:00 -07:00
|
|
|
# Let an update session object handle the request. Note: things around
|
|
|
|
# ZoneConfig will soon be substantially revised. For now we don't
|
|
|
|
# bother to generalize it.
|
|
|
|
datasrc_class, datasrc_client = get_datasrc_client(self._cc)
|
2012-05-30 00:28:36 -07:00
|
|
|
zone_cfg = ZoneConfig([], datasrc_class, datasrc_client,
|
|
|
|
self._zone_config)
|
2012-05-29 15:48:16 -07:00
|
|
|
update_session = self._UpdateSessionClass(self.__request_msg,
|
2012-05-29 21:32:00 -07:00
|
|
|
remote_addr, zone_cfg)
|
2012-05-25 16:31:22 -07:00
|
|
|
result, zname, zclass = update_session.handle()
|
2012-05-29 14:51:12 -07:00
|
|
|
|
|
|
|
# If the request should be dropped, we're done; otherwise, send the
|
|
|
|
# response generated by the session object.
|
|
|
|
if result == isc.ddns.session.UPDATE_DROP:
|
|
|
|
return False
|
2012-05-25 16:31:22 -07:00
|
|
|
msg = update_session.get_message()
|
2012-05-29 15:48:16 -07:00
|
|
|
self.__response_renderer.clear()
|
2012-05-29 14:51:12 -07:00
|
|
|
if tsig_ctx is not None:
|
2012-05-29 15:48:16 -07:00
|
|
|
msg.to_wire(self.__response_renderer, tsig_ctx)
|
2012-05-29 14:51:12 -07:00
|
|
|
else:
|
2012-05-29 15:48:16 -07:00
|
|
|
msg.to_wire(self.__response_renderer)
|
2012-06-04 18:16:09 -07:00
|
|
|
|
2012-06-05 00:31:40 -07:00
|
|
|
ret = self.__send_response(sock, self.__response_renderer.get_data(),
|
|
|
|
remote_addr)
|
|
|
|
if result == isc.ddns.session.UPDATE_SUCCESS:
|
|
|
|
self.__notify_update(zname, zclass)
|
|
|
|
return ret
|
2012-06-04 18:16:09 -07:00
|
|
|
|
|
|
|
def __send_response(self, sock, data, dest):
|
|
|
|
'''Send DDNS response to the client.
|
|
|
|
|
|
|
|
Right now, this is a straightforward subroutine of handle_request(),
|
|
|
|
but is intended to be extended evetually so that it can handle more
|
|
|
|
comlicated operations for TCP (which requires asynchronous write).
|
|
|
|
Further, when we support multiple requests over a single TCP
|
|
|
|
connection, this method may even be shared by multiple methods.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
sock: (python socket) the socket to which the response should be sent.
|
|
|
|
data: (binary) the response data
|
|
|
|
dest: (python socket address) the destion address to which the response
|
|
|
|
should be sent.
|
|
|
|
|
|
|
|
Return: True if the send operation succeds; otherwise False.
|
|
|
|
|
|
|
|
'''
|
2012-05-30 12:19:20 -07:00
|
|
|
try:
|
2012-06-07 15:02:55 -07:00
|
|
|
if sock.proto == socket.IPPROTO_UDP:
|
|
|
|
sock.sendto(data, dest)
|
|
|
|
else:
|
|
|
|
tcp_ctx = DNSTCPContext(sock)
|
2012-06-07 15:47:21 -07:00
|
|
|
send_result = tcp_ctx.send(data)
|
|
|
|
if send_result == DNSTCPContext.SENDING:
|
2012-06-07 18:14:06 -07:00
|
|
|
self._tcp_ctxs[sock.fileno] = (tcp_ctx, dest)
|
2012-06-07 15:47:21 -07:00
|
|
|
elif send_result == DNSTCPContext.CLOSED:
|
|
|
|
raise socket.error("socket error in TCP send")
|
|
|
|
else:
|
|
|
|
tcp_ctx.close()
|
2012-05-30 12:19:20 -07:00
|
|
|
except socket.error as ex:
|
2012-06-07 18:14:06 -07:00
|
|
|
logger.warn(DDNS_RESPONSE_SOCKET_ERROR, ClientFormatter(dest), ex)
|
2012-05-30 12:19:20 -07:00
|
|
|
return False
|
2012-05-25 16:31:22 -07:00
|
|
|
|
|
|
|
return True
|
2012-01-03 19:15:43 +01:00
|
|
|
|
2012-06-05 00:31:40 -07:00
|
|
|
def __notify_update(self, zname, zclass):
|
|
|
|
'''Notify other modules of the update.
|
|
|
|
|
|
|
|
Note that we use blocking communication here. While the internal
|
|
|
|
communication bus is generally expected to be pretty responsive and
|
|
|
|
error free, notable delay can still occur, and in worse cases timeouts
|
|
|
|
or connection reset can happen. In these cases, even if the trouble
|
|
|
|
is temporary, the update service will be suspended for a while.
|
|
|
|
For a longer term we'll need to switch to asynchronous communication,
|
|
|
|
but for now we rely on the blocking operation.
|
|
|
|
|
|
|
|
Note also that we directly refer to the "protected" member of
|
|
|
|
ccsession (_cc._session) rather than creating a separate channel.
|
|
|
|
It's probably not the best practice, but hopefully we can introduce
|
|
|
|
a cleaner way when we support asynchronous communication.
|
|
|
|
At the moment we prefer the brevity with the use of internal channel
|
|
|
|
of the cc session.
|
|
|
|
|
|
|
|
'''
|
|
|
|
param = {'zone_name': zname.to_text(), 'zone_class': zclass.to_text()}
|
|
|
|
msg = create_command(ZONE_NEW_DATA_READY_CMD, param)
|
|
|
|
modname = XFROUT_MODULE_NAME
|
|
|
|
try:
|
|
|
|
seq = self._cc._session.group_sendmsg(msg, modname)
|
|
|
|
answer, _ = self._cc._session.group_recvmsg(False, seq)
|
|
|
|
rcode, error_msg = parse_answer(answer)
|
|
|
|
except (SessionTimeout, SessionError, ProtocolError) as ex:
|
|
|
|
rcode = 1
|
|
|
|
error_msg = str(ex)
|
|
|
|
if rcode == 0:
|
|
|
|
logger.debug(TRACE_BASIC, DDNS_UPDATE_NOTIFY, modname,
|
|
|
|
ZoneFormatter(zname, zclass))
|
|
|
|
else:
|
|
|
|
logger.error(DDNS_UPDATE_NOTIFY_FAIL, modname,
|
|
|
|
ZoneFormatter(zname, zclass), error_msg)
|
|
|
|
|
2012-01-19 10:52:34 +01:00
|
|
|
def handle_session(self, fileno):
|
2012-01-03 17:28:29 +01:00
|
|
|
"""
|
2012-01-23 17:05:57 +01:00
|
|
|
Handle incoming session on the socket with given fileno.
|
2012-01-03 17:28:29 +01:00
|
|
|
"""
|
2012-01-19 10:52:34 +01:00
|
|
|
logger.debug(TRACE_BASIC, DDNS_SESSION, fileno)
|
|
|
|
(socket, receiver) = self._socksession_receivers[fileno]
|
2012-01-03 19:15:43 +01:00
|
|
|
try:
|
2012-01-19 10:52:34 +01:00
|
|
|
self.handle_request(receiver.pop())
|
2012-01-25 11:04:55 +01:00
|
|
|
except isc.util.cio.socketsession.SocketSessionError as se:
|
2012-01-03 19:15:43 +01:00
|
|
|
# No matter why this failed, the connection is in unknown, possibly
|
2012-01-19 10:52:34 +01:00
|
|
|
# broken state. So, we close the socket and remove the receiver.
|
|
|
|
del self._socksession_receivers[fileno]
|
2012-01-23 17:05:16 +01:00
|
|
|
socket.close()
|
|
|
|
logger.warn(DDNS_DROP_CONN, fileno, se)
|
2012-01-03 17:28:29 +01:00
|
|
|
|
2011-12-09 16:04:51 +01:00
|
|
|
def run(self):
|
2011-12-15 14:47:09 +01:00
|
|
|
'''
|
|
|
|
Get and process all commands sent from cfgmgr or other modules.
|
|
|
|
This loops waiting for events until self.shutdown() has been called.
|
|
|
|
'''
|
2011-12-09 18:09:29 +01:00
|
|
|
logger.info(DDNS_RUNNING)
|
2011-12-29 19:43:42 +01:00
|
|
|
cc_fileno = self._cc.get_socket().fileno()
|
|
|
|
listen_fileno = self._listen_socket.fileno()
|
2011-12-15 14:47:09 +01:00
|
|
|
while not self._shutdown:
|
2012-01-24 13:03:30 +01:00
|
|
|
# In this event loop, we propagate most of exceptions, which will
|
|
|
|
# subsequently kill the process. We expect the handling functions
|
|
|
|
# to catch their own exceptions which they can recover from
|
|
|
|
# (malformed packets, lost connections, etc). The rationale behind
|
|
|
|
# this is they know best which exceptions are recoverable there
|
|
|
|
# and an exception may be recoverable somewhere, but not elsewhere.
|
2011-12-29 19:43:42 +01:00
|
|
|
|
2012-01-19 10:52:11 +01:00
|
|
|
try:
|
|
|
|
(reads, writes, exceptions) = \
|
|
|
|
select.select([cc_fileno, listen_fileno] +
|
2012-06-07 17:41:03 -07:00
|
|
|
list(self._socksession_receivers.keys()),
|
|
|
|
list(self._tcp_ctxs.keys()), [])
|
2012-01-19 10:52:11 +01:00
|
|
|
except select.error as se:
|
|
|
|
# In case it is just interrupted, we continue like nothing
|
|
|
|
# happened
|
|
|
|
if se.args[0] == errno.EINTR:
|
|
|
|
(reads, writes, exceptions) = ([], [], [])
|
|
|
|
else:
|
|
|
|
raise
|
2011-12-29 19:43:42 +01:00
|
|
|
for fileno in reads:
|
|
|
|
if fileno == cc_fileno:
|
2011-12-29 19:48:12 +01:00
|
|
|
self._cc.check_command(True)
|
2011-12-29 19:43:42 +01:00
|
|
|
elif fileno == listen_fileno:
|
|
|
|
self.accept()
|
|
|
|
else:
|
2012-01-19 10:52:34 +01:00
|
|
|
self.handle_session(fileno)
|
2012-06-07 17:41:03 -07:00
|
|
|
for fileno in writes:
|
2012-06-07 18:14:06 -07:00
|
|
|
ctx = self._tcp_ctxs[fileno]
|
|
|
|
result = ctx[0].send_ready()
|
2012-06-07 17:41:03 -07:00
|
|
|
if result != DNSTCPContext.SENDING:
|
2012-06-07 18:14:06 -07:00
|
|
|
if result == DNSTCPContext.CLOSED:
|
|
|
|
logger.warn(DDNS_RESPONSE_TCP_SOCKET_ERROR,
|
|
|
|
ClientFormatter(ctx[1]))
|
|
|
|
ctx[0].close()
|
2012-06-07 17:41:03 -07:00
|
|
|
del self._tcp_ctxs[fileno]
|
2011-12-19 11:56:33 +01:00
|
|
|
self.shutdown_cleanup()
|
2011-12-16 18:21:45 +01:00
|
|
|
logger.info(DDNS_STOPPED)
|
2011-12-09 16:04:51 +01:00
|
|
|
|
2011-12-15 16:16:50 +01:00
|
|
|
def create_signal_handler(ddns_server):
|
2011-12-15 14:47:09 +01:00
|
|
|
'''
|
2011-12-15 16:16:50 +01:00
|
|
|
This creates a signal_handler for use in set_signal_handler, which
|
|
|
|
shuts down the given DDNSServer (or any object that has a shutdown()
|
|
|
|
method)
|
2011-12-15 14:47:09 +01:00
|
|
|
'''
|
2011-12-15 16:16:50 +01:00
|
|
|
def signal_handler(signal, frame):
|
|
|
|
'''
|
|
|
|
Handler for process signals. Since only signals to shut down are sent
|
|
|
|
here, the actual signal is not checked and the server is simply shut
|
|
|
|
down.
|
|
|
|
'''
|
2011-12-19 11:56:33 +01:00
|
|
|
ddns_server.trigger_shutdown()
|
2011-12-15 16:16:50 +01:00
|
|
|
return signal_handler
|
2011-12-09 16:04:51 +01:00
|
|
|
|
2011-12-15 16:16:50 +01:00
|
|
|
def set_signal_handler(signal_handler):
|
2011-12-15 14:47:09 +01:00
|
|
|
'''
|
|
|
|
Sets the signal handler(s).
|
|
|
|
'''
|
2011-12-09 16:04:51 +01:00
|
|
|
signal.signal(signal.SIGTERM, signal_handler)
|
|
|
|
signal.signal(signal.SIGINT, signal_handler)
|
|
|
|
|
|
|
|
def set_cmd_options(parser):
|
2011-12-15 14:47:09 +01:00
|
|
|
'''
|
|
|
|
Helper function to set command-line options
|
|
|
|
'''
|
2011-12-09 16:04:51 +01:00
|
|
|
parser.add_option("-v", "--verbose", dest="verbose", action="store_true",
|
|
|
|
help="display more about what is going on")
|
|
|
|
|
2011-12-16 18:21:45 +01:00
|
|
|
def main(ddns_server=None):
|
|
|
|
'''
|
|
|
|
The main function.
|
|
|
|
Parameters:
|
|
|
|
ddns_server: If None (default), a DDNSServer object is initialized.
|
|
|
|
If specified, the given DDNSServer will be used. This is
|
|
|
|
mainly used for testing.
|
|
|
|
cc_session: If None (default), a new ModuleCCSession will be set up.
|
|
|
|
If specified, the given session will be used. This is
|
|
|
|
mainly used for testing.
|
|
|
|
'''
|
2011-12-09 16:04:51 +01:00
|
|
|
try:
|
|
|
|
parser = OptionParser()
|
|
|
|
set_cmd_options(parser)
|
|
|
|
(options, args) = parser.parse_args()
|
2011-12-15 16:16:50 +01:00
|
|
|
if options.verbose:
|
|
|
|
print("[b10-ddns] Warning: -v verbose option is ignored at this point.")
|
2011-12-09 16:04:51 +01:00
|
|
|
|
2011-12-16 18:21:45 +01:00
|
|
|
if ddns_server is None:
|
|
|
|
ddns_server = DDNSServer()
|
2011-12-15 16:16:50 +01:00
|
|
|
set_signal_handler(create_signal_handler(ddns_server))
|
2011-12-09 16:04:51 +01:00
|
|
|
ddns_server.run()
|
|
|
|
except KeyboardInterrupt:
|
2011-12-15 16:16:50 +01:00
|
|
|
logger.info(DDNS_STOPPED_BY_KEYBOARD)
|
2011-12-09 16:04:51 +01:00
|
|
|
except SessionError as e:
|
|
|
|
logger.error(DDNS_CC_SESSION_ERROR, str(e))
|
|
|
|
except ModuleCCSessionError as e:
|
|
|
|
logger.error(DDNS_MODULECC_SESSION_ERROR, str(e))
|
|
|
|
except DDNSConfigError as e:
|
|
|
|
logger.error(DDNS_CONFIG_ERROR, str(e))
|
|
|
|
except SessionTimeout as e:
|
|
|
|
logger.error(DDNS_CC_SESSION_TIMEOUT_ERROR)
|
2011-12-16 18:21:45 +01:00
|
|
|
except Exception as e:
|
|
|
|
logger.error(DDNS_UNCAUGHT_EXCEPTION, type(e).__name__, str(e))
|
2012-01-20 10:31:17 +01:00
|
|
|
clear_socket()
|
2011-12-16 18:21:45 +01:00
|
|
|
|
|
|
|
if '__main__' == __name__:
|
|
|
|
main()
|