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
|
2011-12-09 18:09:29 +01:00
|
|
|
import bind10_config
|
2011-12-09 16:04:51 +01:00
|
|
|
from isc.dns import *
|
|
|
|
from isc.config.ccsession import *
|
|
|
|
from isc.cc import SessionError, SessionTimeout
|
|
|
|
import isc.util.process
|
|
|
|
|
|
|
|
from isc.log_messages.ddns_messages import *
|
|
|
|
|
2011-12-09 18:09:29 +01:00
|
|
|
from optparse import OptionParser, OptionValueError
|
|
|
|
import os
|
|
|
|
import signal
|
|
|
|
|
2011-12-09 16:04:51 +01:00
|
|
|
isc.log.init("b10-ddns")
|
|
|
|
logger = isc.log.Logger("ddns")
|
2011-12-09 18:09:29 +01:00
|
|
|
|
|
|
|
DATA_PATH = bind10_config.DATA_PATH
|
|
|
|
if "B10_FROM_BUILD" in os.environ:
|
|
|
|
DATA_PATH = DATA_PATH + "/src/bin/ddns"
|
|
|
|
SPECFILE_LOCATION = DATA_PATH + "/ddns.spec"
|
|
|
|
|
2011-12-09 16:04:51 +01:00
|
|
|
|
|
|
|
isc.util.process.rename()
|
|
|
|
|
|
|
|
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
|
|
|
self._handle()
|
|
|
|
|
|
|
|
def _handle(self):
|
|
|
|
'''
|
|
|
|
Handle a DDNS update.
|
|
|
|
This should be called from the initializer, and should contain the
|
|
|
|
logic for doing the checks, handling the update, and responding with
|
|
|
|
the result.
|
|
|
|
'''
|
|
|
|
pass
|
|
|
|
|
2011-12-09 16:04:51 +01:00
|
|
|
class DDNSServer:
|
|
|
|
def __init__(self):
|
2011-12-15 14:47:09 +01:00
|
|
|
'''
|
|
|
|
Initialize the DDNS Server.
|
|
|
|
This sets up a ModuleCCSession for the BIND 10 system.
|
|
|
|
'''
|
2011-12-09 18:09:29 +01:00
|
|
|
self._cc = isc.config.ModuleCCSession(SPECFILE_LOCATION,
|
|
|
|
self.config_handler,
|
|
|
|
self.command_handler)
|
|
|
|
self._config_data = self._cc.get_full_config()
|
|
|
|
self._cc.start()
|
2011-12-15 14:47:09 +01:00
|
|
|
self._shutdown = False
|
2011-12-09 18:09:29 +01:00
|
|
|
|
|
|
|
def config_handler(self, new_config):
|
|
|
|
'''Update config data.'''
|
|
|
|
answer = create_answer(0)
|
|
|
|
return answer
|
|
|
|
|
|
|
|
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.
|
|
|
|
'''
|
2011-12-09 18:09:29 +01:00
|
|
|
if cmd == "shutdown":
|
|
|
|
logger.info(DDNS_RECEIVED_SHUTDOWN_COMMAND)
|
|
|
|
self.shutdown()
|
|
|
|
answer = create_answer(0)
|
|
|
|
else:
|
|
|
|
answer = create_answer(1, "Unknown command:" + str(cmd))
|
|
|
|
return answer
|
|
|
|
|
2011-12-09 16:04:51 +01:00
|
|
|
def shutdown(self):
|
2011-12-15 14:47:09 +01:00
|
|
|
'''
|
|
|
|
Shut down the server. Perform any cleanup that is necessary.
|
|
|
|
Currently, this only sets the internal _shutdown value to true,
|
|
|
|
so the main loop in run() stops.
|
|
|
|
'''
|
|
|
|
self._shutdown = True
|
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-15 14:47:09 +01:00
|
|
|
while not self._shutdown:
|
2011-12-09 18:09:29 +01:00
|
|
|
self._cc.check_command(False)
|
2011-12-09 16:04:51 +01:00
|
|
|
|
|
|
|
ddns_server = None
|
|
|
|
|
|
|
|
def signal_handler(signal, frame):
|
2011-12-15 14:47:09 +01:00
|
|
|
'''
|
|
|
|
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-09 16:04:51 +01:00
|
|
|
if ddns_server is not None:
|
|
|
|
ddns_server.shutdown()
|
|
|
|
|
|
|
|
def set_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")
|
|
|
|
|
|
|
|
if '__main__' == __name__:
|
|
|
|
try:
|
|
|
|
parser = OptionParser()
|
|
|
|
set_cmd_options(parser)
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
VERBOSE_MODE = options.verbose
|
|
|
|
|
|
|
|
ddns_server = DDNSServer()
|
2011-12-15 14:47:09 +01:00
|
|
|
set_signal_handler()
|
2011-12-09 16:04:51 +01:00
|
|
|
ddns_server.run()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
logger.INFO(DDNS_STOPPED_BY_KEYBOARD)
|
|
|
|
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)
|
|
|
|
|
|
|
|
if ddns_server:
|
|
|
|
ddns_server.shutdown()
|
|
|
|
|