#!/usr/bin/python
# -*- mode: python; coding: utf-8 -*-
# 
# Mandos server - give out binary blobs to connecting clients.
# 
# This program is partly derived from an example program for an Avahi
# service publisher, downloaded from
# <http://avahi.org/wiki/PythonPublishExample>.  This includes the
# methods "add" and "remove" in the "AvahiService" class, the
# "server_state_changed" and "entry_group_state_changed" functions,
# and some lines in "main".
# 
# Everything else is
# Copyright © 2008,2009 Teddy Hogeborn
# Copyright © 2008,2009 Björn Påhlsson
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
#     This program is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see
# <http://www.gnu.org/licenses/>.
# 
# Contact the authors at <mandos@fukt.bsnet.se>.
# 

from __future__ import division, with_statement, absolute_import

import SocketServer
import socket
import optparse
import datetime
import errno
import gnutls.crypto
import gnutls.connection
import gnutls.errors
import gnutls.library.functions
import gnutls.library.constants
import gnutls.library.types
import ConfigParser
import sys
import re
import os
import signal
from sets import Set
import subprocess
import atexit
import stat
import logging
import logging.handlers
import pwd
from contextlib import closing

import dbus
import dbus.service
import gobject
import avahi
from dbus.mainloop.glib import DBusGMainLoop
import ctypes
import ctypes.util

version = "1.0.8"

logger = logging.Logger('mandos')
syslogger = (logging.handlers.SysLogHandler
             (facility = logging.handlers.SysLogHandler.LOG_DAEMON,
              address = "/dev/log"))
syslogger.setFormatter(logging.Formatter
                       ('Mandos [%(process)d]: %(levelname)s:'
                        ' %(message)s'))
logger.addHandler(syslogger)

console = logging.StreamHandler()
console.setFormatter(logging.Formatter('%(name)s [%(process)d]:'
                                       ' %(levelname)s: %(message)s'))
logger.addHandler(console)

class AvahiError(Exception):
    def __init__(self, value, *args, **kwargs):
        self.value = value
        super(AvahiError, self).__init__(value, *args, **kwargs)
    def __unicode__(self):
        return unicode(repr(self.value))

class AvahiServiceError(AvahiError):
    pass

class AvahiGroupError(AvahiError):
    pass


class AvahiService(object):
    """An Avahi (Zeroconf) service.
    Attributes:
    interface: integer; avahi.IF_UNSPEC or an interface index.
               Used to optionally bind to the specified interface.
    name: string; Example: 'Mandos'
    type: string; Example: '_mandos._tcp'.
                  See <http://www.dns-sd.org/ServiceTypes.html>
    port: integer; what port to announce
    TXT: list of strings; TXT record for the service
    domain: string; Domain to publish on, default to .local if empty.
    host: string; Host to publish records for, default is localhost
    max_renames: integer; maximum number of renames
    rename_count: integer; counter so we only rename after collisions
                  a sensible number of times
    """
    def __init__(self, interface = avahi.IF_UNSPEC, name = None,
                 servicetype = None, port = None, TXT = None,
                 domain = "", host = "", max_renames = 32768,
                 protocol = avahi.PROTO_UNSPEC):
        self.interface = interface
        self.name = name
        self.type = servicetype
        self.port = port
        self.TXT = TXT if TXT is not None else []
        self.domain = domain
        self.host = host
        self.rename_count = 0
        self.max_renames = max_renames
        self.protocol = protocol
    def rename(self):
        """Derived from the Avahi example code"""
        if self.rename_count >= self.max_renames:
            logger.critical(u"No suitable Zeroconf service name found"
                            u" after %i retries, exiting.",
                            self.rename_count)
            raise AvahiServiceError(u"Too many renames")
        self.name = server.GetAlternativeServiceName(self.name)
        logger.info(u"Changing Zeroconf service name to %r ...",
                    str(self.name))
        syslogger.setFormatter(logging.Formatter
                               ('Mandos (%s): %%(levelname)s:'
                                ' %%(message)s' % self.name))
        self.remove()
        self.add()
        self.rename_count += 1
    def remove(self):
        """Derived from the Avahi example code"""
        if group is not None:
            group.Reset()
    def add(self):
        """Derived from the Avahi example code"""
        global group
        if group is None:
            group = dbus.Interface(bus.get_object
                                   (avahi.DBUS_NAME,
                                    server.EntryGroupNew()),
                                   avahi.DBUS_INTERFACE_ENTRY_GROUP)
            group.connect_to_signal('StateChanged',
                                    entry_group_state_changed)
        logger.debug(u"Adding Zeroconf service '%s' of type '%s' ...",
                     service.name, service.type)
        group.AddService(
                self.interface,         # interface
                self.protocol,          # protocol
                dbus.UInt32(0),         # flags
                self.name, self.type,
                self.domain, self.host,
                dbus.UInt16(self.port),
                avahi.string_array_to_txt_array(self.TXT))
        group.Commit()

# From the Avahi example code:
group = None                            # our entry group
# End of Avahi example code


def _datetime_to_dbus(dt, variant_level=0):
    """Convert a UTC datetime.datetime() to a D-Bus type."""
    return dbus.String(dt.isoformat(), variant_level=variant_level)


class Client(dbus.service.Object):
    """A representation of a client host served by this server.
    Attributes:
    name:       string; from the config file, used in log messages and
                        D-Bus identifiers
    fingerprint: string (40 or 32 hexadecimal digits); used to
                 uniquely identify the client
    secret:     bytestring; sent verbatim (over TLS) to client
    host:       string; available for use by the checker command
    created:    datetime.datetime(); (UTC) object creation
    last_enabled: datetime.datetime(); (UTC)
    enabled:    bool()
    last_checked_ok: datetime.datetime(); (UTC) or None
    timeout:    datetime.timedelta(); How long from last_checked_ok
                                      until this client is invalid
    interval:   datetime.timedelta(); How often to start a new checker
    disable_hook:  If set, called by disable() as disable_hook(self)
    checker:    subprocess.Popen(); a running checker process used
                                    to see if the client lives.
                                    'None' if no process is running.
    checker_initiator_tag: a gobject event source tag, or None
    disable_initiator_tag:    - '' -
    checker_callback_tag:  - '' -
    checker_command: string; External command which is run to check if
                     client lives.  %() expansions are done at
                     runtime with vars(self) as dict, so that for
                     instance %(name)s can be used in the command.
    current_checker_command: string; current running checker_command
    use_dbus: bool(); Whether to provide D-Bus interface and signals
    dbus_object_path: dbus.ObjectPath ; only set if self.use_dbus
    """
    def timeout_milliseconds(self):
        "Return the 'timeout' attribute in milliseconds"
        return ((self.timeout.days * 24 * 60 * 60 * 1000)
                + (self.timeout.seconds * 1000)
                + (self.timeout.microseconds // 1000))
    
    def interval_milliseconds(self):
        "Return the 'interval' attribute in milliseconds"
        return ((self.interval.days * 24 * 60 * 60 * 1000)
                + (self.interval.seconds * 1000)
                + (self.interval.microseconds // 1000))
    
    def __init__(self, name = None, disable_hook=None, config=None,
                 use_dbus=True):
        """Note: the 'checker' key in 'config' sets the
        'checker_command' attribute and *not* the 'checker'
        attribute."""
        self.name = name
        if config is None:
            config = {}
        logger.debug(u"Creating client %r", self.name)
        self.use_dbus = False   # During __init__
        # Uppercase and remove spaces from fingerprint for later
        # comparison purposes with return value from the fingerprint()
        # function
        self.fingerprint = (config["fingerprint"].upper()
                            .replace(u" ", u""))
        logger.debug(u"  Fingerprint: %s", self.fingerprint)
        if "secret" in config:
            self.secret = config["secret"].decode(u"base64")
        elif "secfile" in config:
            with closing(open(os.path.expanduser
                              (os.path.expandvars
                               (config["secfile"])))) as secfile:
                self.secret = secfile.read()
        else:
            raise TypeError(u"No secret or secfile for client %s"
                            % self.name)
        self.host = config.get("host", "")
        self.created = datetime.datetime.utcnow()
        self.enabled = False
        self.last_enabled = None
        self.last_checked_ok = None
        self.timeout = string_to_delta(config["timeout"])
        self.interval = string_to_delta(config["interval"])
        self.disable_hook = disable_hook
        self.checker = None
        self.checker_initiator_tag = None
        self.disable_initiator_tag = None
        self.checker_callback_tag = None
        self.checker_command = config["checker"]
        self.current_checker_command = None
        self.last_connect = None
        # Only now, when this client is initialized, can it show up on
        # the D-Bus
        self.use_dbus = use_dbus
        if self.use_dbus:
            self.dbus_object_path = (dbus.ObjectPath
                                     ("/clients/"
                                      + self.name.replace(".", "_")))
            dbus.service.Object.__init__(self, bus,
                                         self.dbus_object_path)
    
    def enable(self):
        """Start this client's checker and timeout hooks"""
        self.last_enabled = datetime.datetime.utcnow()
        # Schedule a new checker to be started an 'interval' from now,
        # and every interval from then on.
        self.checker_initiator_tag = (gobject.timeout_add
                                      (self.interval_milliseconds(),
                                       self.start_checker))
        # Also start a new checker *right now*.
        self.start_checker()
        # Schedule a disable() when 'timeout' has passed
        self.disable_initiator_tag = (gobject.timeout_add
                                   (self.timeout_milliseconds(),
                                    self.disable))
        self.enabled = True
        if self.use_dbus:
            # Emit D-Bus signals
            self.PropertyChanged(dbus.String(u"enabled"),
                                 dbus.Boolean(True, variant_level=1))
            self.PropertyChanged(dbus.String(u"last_enabled"),
                                 (_datetime_to_dbus(self.last_enabled,
                                                    variant_level=1)))
    
    def disable(self):
        """Disable this client."""
        if not getattr(self, "enabled", False):
            return False
        logger.info(u"Disabling client %s", self.name)
        if getattr(self, "disable_initiator_tag", False):
            gobject.source_remove(self.disable_initiator_tag)
            self.disable_initiator_tag = None
        if getattr(self, "checker_initiator_tag", False):
            gobject.source_remove(self.checker_initiator_tag)
            self.checker_initiator_tag = None
        self.stop_checker()
        if self.disable_hook:
            self.disable_hook(self)
        self.enabled = False
        if self.use_dbus:
            # Emit D-Bus signal
            self.PropertyChanged(dbus.String(u"enabled"),
                                 dbus.Boolean(False, variant_level=1))
        # Do not run this again if called by a gobject.timeout_add
        return False
    
    def __del__(self):
        self.disable_hook = None
        self.disable()
    
    def checker_callback(self, pid, condition, command):
        """The checker has completed, so take appropriate actions."""
        self.checker_callback_tag = None
        self.checker = None
        if self.use_dbus:
            # Emit D-Bus signal
            self.PropertyChanged(dbus.String(u"checker_running"),
                                 dbus.Boolean(False, variant_level=1))
        if os.WIFEXITED(condition):
            exitstatus = os.WEXITSTATUS(condition)
            if exitstatus == 0:
                logger.info(u"Checker for %(name)s succeeded",
                            vars(self))
                self.checked_ok()
            else:
                logger.info(u"Checker for %(name)s failed",
                            vars(self))
            if self.use_dbus:
                # Emit D-Bus signal
                self.CheckerCompleted(dbus.Int16(exitstatus),
                                      dbus.Int64(condition),
                                      dbus.String(command))
        else:
            logger.warning(u"Checker for %(name)s crashed?",
                           vars(self))
            if self.use_dbus:
                # Emit D-Bus signal
                self.CheckerCompleted(dbus.Int16(-1),
                                      dbus.Int64(condition),
                                      dbus.String(command))
    
    def checked_ok(self):
        """Bump up the timeout for this client.
        This should only be called when the client has been seen,
        alive and well.
        """
        self.last_checked_ok = datetime.datetime.utcnow()
        gobject.source_remove(self.disable_initiator_tag)
        self.disable_initiator_tag = (gobject.timeout_add
                                      (self.timeout_milliseconds(),
                                       self.disable))
        if self.use_dbus:
            # Emit D-Bus signal
            self.PropertyChanged(
                dbus.String(u"last_checked_ok"),
                (_datetime_to_dbus(self.last_checked_ok,
                                   variant_level=1)))
    
    def start_checker(self):
        """Start a new checker subprocess if one is not running.
        If a checker already exists, leave it running and do
        nothing."""
        # The reason for not killing a running checker is that if we
        # did that, then if a checker (for some reason) started
        # running slowly and taking more than 'interval' time, the
        # client would inevitably timeout, since no checker would get
        # a chance to run to completion.  If we instead leave running
        # checkers alone, the checker would have to take more time
        # than 'timeout' for the client to be declared invalid, which
        # is as it should be.
        
        # If a checker exists, make sure it is not a zombie
        if self.checker is not None:
            pid, status = os.waitpid(self.checker.pid, os.WNOHANG)
            if pid:
                logger.warning("Checker was a zombie")
                gobject.source_remove(self.checker_callback_tag)
                self.checker_callback(pid, status,
                                      self.current_checker_command)
        # Start a new checker if needed
        if self.checker is None:
            try:
                # In case checker_command has exactly one % operator
                command = self.checker_command % self.host
            except TypeError:
                # Escape attributes for the shell
                escaped_attrs = dict((key, re.escape(str(val)))
                                     for key, val in
                                     vars(self).iteritems())
                try:
                    command = self.checker_command % escaped_attrs
                except TypeError, error:
                    logger.error(u'Could not format string "%s":'
                                 u' %s', self.checker_command, error)
                    return True # Try again later
                self.current_checker_command = command
            try:
                logger.info(u"Starting checker %r for %s",
                            command, self.name)
                # We don't need to redirect stdout and stderr, since
                # in normal mode, that is already done by daemon(),
                # and in debug mode we don't want to.  (Stdin is
                # always replaced by /dev/null.)
                self.checker = subprocess.Popen(command,
                                                close_fds=True,
                                                shell=True, cwd="/")
                if self.use_dbus:
                    # Emit D-Bus signal
                    self.CheckerStarted(command)
                    self.PropertyChanged(
                        dbus.String("checker_running"),
                        dbus.Boolean(True, variant_level=1))
                self.checker_callback_tag = (gobject.child_watch_add
                                             (self.checker.pid,
                                              self.checker_callback,
                                              data=command))
                # The checker may have completed before the gobject
                # watch was added.  Check for this.
                pid, status = os.waitpid(self.checker.pid, os.WNOHANG)
                if pid:
                    gobject.source_remove(self.checker_callback_tag)
                    self.checker_callback(pid, status, command)
            except OSError, error:
                logger.error(u"Failed to start subprocess: %s",
                             error)
        # Re-run this periodically if run by gobject.timeout_add
        return True
    
    def stop_checker(self):
        """Force the checker process, if any, to stop."""
        if self.checker_callback_tag:
            gobject.source_remove(self.checker_callback_tag)
            self.checker_callback_tag = None
        if getattr(self, "checker", None) is None:
            return
        logger.debug(u"Stopping checker for %(name)s", vars(self))
        try:
            os.kill(self.checker.pid, signal.SIGTERM)
            #os.sleep(0.5)
            #if self.checker.poll() is None:
            #    os.kill(self.checker.pid, signal.SIGKILL)
        except OSError, error:
            if error.errno != errno.ESRCH: # No such process
                raise
        self.checker = None
        if self.use_dbus:
            self.PropertyChanged(dbus.String(u"checker_running"),
                                 dbus.Boolean(False, variant_level=1))
    
    def still_valid(self):
        """Has the timeout not yet passed for this client?"""
        if not getattr(self, "enabled", False):
            return False
        now = datetime.datetime.utcnow()
        if self.last_checked_ok is None:
            return now < (self.created + self.timeout)
        else:
            return now < (self.last_checked_ok + self.timeout)
    
    ## D-Bus methods & signals
    _interface = u"se.bsnet.fukt.Mandos.Client"
    
    # CheckedOK - method
    CheckedOK = dbus.service.method(_interface)(checked_ok)
    CheckedOK.__name__ = "CheckedOK"
    
    # CheckerCompleted - signal
    @dbus.service.signal(_interface, signature="nxs")
    def CheckerCompleted(self, exitcode, waitstatus, command):
        "D-Bus signal"
        pass
    
    # CheckerStarted - signal
    @dbus.service.signal(_interface, signature="s")
    def CheckerStarted(self, command):
        "D-Bus signal"
        pass
    
    # GetAllProperties - method
    @dbus.service.method(_interface, out_signature="a{sv}")
    def GetAllProperties(self):
        "D-Bus method"
        return dbus.Dictionary({
                dbus.String("name"):
                    dbus.String(self.name, variant_level=1),
                dbus.String("fingerprint"):
                    dbus.String(self.fingerprint, variant_level=1),
                dbus.String("host"):
                    dbus.String(self.host, variant_level=1),
                dbus.String("created"):
                    _datetime_to_dbus(self.created, variant_level=1),
                dbus.String("last_enabled"):
                    (_datetime_to_dbus(self.last_enabled,
                                       variant_level=1)
                     if self.last_enabled is not None
                     else dbus.Boolean(False, variant_level=1)),
                dbus.String("enabled"):
                    dbus.Boolean(self.enabled, variant_level=1),
                dbus.String("last_checked_ok"):
                    (_datetime_to_dbus(self.last_checked_ok,
                                       variant_level=1)
                     if self.last_checked_ok is not None
                     else dbus.Boolean (False, variant_level=1)),
                dbus.String("timeout"):
                    dbus.UInt64(self.timeout_milliseconds(),
                                variant_level=1),
                dbus.String("interval"):
                    dbus.UInt64(self.interval_milliseconds(),
                                variant_level=1),
                dbus.String("checker"):
                    dbus.String(self.checker_command,
                                variant_level=1),
                dbus.String("checker_running"):
                    dbus.Boolean(self.checker is not None,
                                 variant_level=1),
                dbus.String("object_path"):
                    dbus.ObjectPath(self.dbus_object_path,
                                    variant_level=1)
                }, signature="sv")
    
    # IsStillValid - method
    IsStillValid = (dbus.service.method(_interface, out_signature="b")
                    (still_valid))
    IsStillValid.__name__ = "IsStillValid"
    
    # PropertyChanged - signal
    @dbus.service.signal(_interface, signature="sv")
    def PropertyChanged(self, property, value):
        "D-Bus signal"
        pass
    
    # SetChecker - method
    @dbus.service.method(_interface, in_signature="s")
    def SetChecker(self, checker):
        "D-Bus setter method"
        self.checker_command = checker
        # Emit D-Bus signal
        self.PropertyChanged(dbus.String(u"checker"),
                             dbus.String(self.checker_command,
                                         variant_level=1))
    
    # SetHost - method
    @dbus.service.method(_interface, in_signature="s")
    def SetHost(self, host):
        "D-Bus setter method"
        self.host = host
        # Emit D-Bus signal
        self.PropertyChanged(dbus.String(u"host"),
                             dbus.String(self.host, variant_level=1))
    
    # SetInterval - method
    @dbus.service.method(_interface, in_signature="t")
    def SetInterval(self, milliseconds):
        self.interval = datetime.timedelta(0, 0, 0, milliseconds)
        # Emit D-Bus signal
        self.PropertyChanged(dbus.String(u"interval"),
                             (dbus.UInt64(self.interval_milliseconds(),
                                          variant_level=1)))
    
    # SetSecret - method
    @dbus.service.method(_interface, in_signature="ay",
                         byte_arrays=True)
    def SetSecret(self, secret):
        "D-Bus setter method"
        self.secret = str(secret)
    
    # SetTimeout - method
    @dbus.service.method(_interface, in_signature="t")
    def SetTimeout(self, milliseconds):
        self.timeout = datetime.timedelta(0, 0, 0, milliseconds)
        # Emit D-Bus signal
        self.PropertyChanged(dbus.String(u"timeout"),
                             (dbus.UInt64(self.timeout_milliseconds(),
                                          variant_level=1)))
    
    # Enable - method
    Enable = dbus.service.method(_interface)(enable)
    Enable.__name__ = "Enable"
    
    # StartChecker - method
    @dbus.service.method(_interface)
    def StartChecker(self):
        "D-Bus method"
        self.start_checker()
    
    # Disable - method
    @dbus.service.method(_interface)
    def Disable(self):
        "D-Bus method"
        self.disable()
    
    # StopChecker - method
    StopChecker = dbus.service.method(_interface)(stop_checker)
    StopChecker.__name__ = "StopChecker"
    
    del _interface


def peer_certificate(session):
    "Return the peer's OpenPGP certificate as a bytestring"
    # If not an OpenPGP certificate...
    if (gnutls.library.functions
        .gnutls_certificate_type_get(session._c_object)
        != gnutls.library.constants.GNUTLS_CRT_OPENPGP):
        # ...do the normal thing
        return session.peer_certificate
    list_size = ctypes.c_uint(1)
    cert_list = (gnutls.library.functions
                 .gnutls_certificate_get_peers
                 (session._c_object, ctypes.byref(list_size)))
    if not bool(cert_list) and list_size.value != 0:
        raise gnutls.errors.GNUTLSError("error getting peer"
                                        " certificate")
    if list_size.value == 0:
        return None
    cert = cert_list[0]
    return ctypes.string_at(cert.data, cert.size)


def fingerprint(openpgp):
    "Convert an OpenPGP bytestring to a hexdigit fingerprint string"
    # New GnuTLS "datum" with the OpenPGP public key
    datum = (gnutls.library.types
             .gnutls_datum_t(ctypes.cast(ctypes.c_char_p(openpgp),
                                         ctypes.POINTER
                                         (ctypes.c_ubyte)),
                             ctypes.c_uint(len(openpgp))))
    # New empty GnuTLS certificate
    crt = gnutls.library.types.gnutls_openpgp_crt_t()
    (gnutls.library.functions
     .gnutls_openpgp_crt_init(ctypes.byref(crt)))
    # Import the OpenPGP public key into the certificate
    (gnutls.library.functions
     .gnutls_openpgp_crt_import(crt, ctypes.byref(datum),
                                gnutls.library.constants
                                .GNUTLS_OPENPGP_FMT_RAW))
    # Verify the self signature in the key
    crtverify = ctypes.c_uint()
    (gnutls.library.functions
     .gnutls_openpgp_crt_verify_self(crt, 0, ctypes.byref(crtverify)))
    if crtverify.value != 0:
        gnutls.library.functions.gnutls_openpgp_crt_deinit(crt)
        raise gnutls.errors.CertificateSecurityError("Verify failed")
    # New buffer for the fingerprint
    buf = ctypes.create_string_buffer(20)
    buf_len = ctypes.c_size_t()
    # Get the fingerprint from the certificate into the buffer
    (gnutls.library.functions
     .gnutls_openpgp_crt_get_fingerprint(crt, ctypes.byref(buf),
                                         ctypes.byref(buf_len)))
    # Deinit the certificate
    gnutls.library.functions.gnutls_openpgp_crt_deinit(crt)
    # Convert the buffer to a Python bytestring
    fpr = ctypes.string_at(buf, buf_len.value)
    # Convert the bytestring to hexadecimal notation
    hex_fpr = u''.join(u"%02X" % ord(char) for char in fpr)
    return hex_fpr


class TCP_handler(SocketServer.BaseRequestHandler, object):
    """A TCP request handler class.
    Instantiated by IPv6_TCPServer for each request to handle it.
    Note: This will run in its own forked process."""
    
    def handle(self):
        logger.info(u"TCP connection from: %s",
                    unicode(self.client_address))
        session = (gnutls.connection
                   .ClientSession(self.request,
                                  gnutls.connection
                                  .X509Credentials()))
        
        line = self.request.makefile().readline()
        logger.debug(u"Protocol version: %r", line)
        try:
            if int(line.strip().split()[0]) > 1:
                raise RuntimeError
        except (ValueError, IndexError, RuntimeError), error:
            logger.error(u"Unknown protocol version: %s", error)
            return
        
        # Note: gnutls.connection.X509Credentials is really a generic
        # GnuTLS certificate credentials object so long as no X.509
        # keys are added to it.  Therefore, we can use it here despite
        # using OpenPGP certificates.
        
        #priority = ':'.join(("NONE", "+VERS-TLS1.1", "+AES-256-CBC",
        #                     "+SHA1", "+COMP-NULL", "+CTYPE-OPENPGP",
        #                     "+DHE-DSS"))
        # Use a fallback default, since this MUST be set.
        priority = self.server.settings.get("priority", "NORMAL")
        (gnutls.library.functions
         .gnutls_priority_set_direct(session._c_object,
                                     priority, None))
        
        try:
            session.handshake()
        except gnutls.errors.GNUTLSError, error:
            logger.warning(u"Handshake failed: %s", error)
            # Do not run session.bye() here: the session is not
            # established.  Just abandon the request.
            return
        logger.debug(u"Handshake succeeded")
        try:
            fpr = fingerprint(peer_certificate(session))
        except (TypeError, gnutls.errors.GNUTLSError), error:
            logger.warning(u"Bad certificate: %s", error)
            session.bye()
            return
        logger.debug(u"Fingerprint: %s", fpr)
        
        for c in self.server.clients:
            if c.fingerprint == fpr:
                client = c
                break
        else:
            logger.warning(u"Client not found for fingerprint: %s",
                           fpr)
            session.bye()
            return
        # Have to check if client.still_valid(), since it is possible
        # that the client timed out while establishing the GnuTLS
        # session.
        if not client.still_valid():
            logger.warning(u"Client %(name)s is invalid",
                           vars(client))
            session.bye()
            return
        ## This won't work here, since we're in a fork.
        # client.checked_ok()
        sent_size = 0
        while sent_size < len(client.secret):
            sent = session.send(client.secret[sent_size:])
            logger.debug(u"Sent: %d, remaining: %d",
                         sent, len(client.secret)
                         - (sent_size + sent))
            sent_size += sent
        session.bye()


class IPv6_TCPServer(SocketServer.ForkingMixIn,
                     SocketServer.TCPServer, object):
    """IPv6-capable TCP server.  Accepts 'None' as address and/or port
    Attributes:
        settings:       Server settings
        clients:        Set() of Client objects
        enabled:        Boolean; whether this server is activated yet
    """
    address_family = socket.AF_INET6
    def __init__(self, *args, **kwargs):
        if "settings" in kwargs:
            self.settings = kwargs["settings"]
            del kwargs["settings"]
        if "clients" in kwargs:
            self.clients = kwargs["clients"]
            del kwargs["clients"]
        if "use_ipv6" in kwargs:
            if not kwargs["use_ipv6"]:
                self.address_family = socket.AF_INET
            del kwargs["use_ipv6"]
        self.enabled = False
        super(IPv6_TCPServer, self).__init__(*args, **kwargs)
    def server_bind(self):
        """This overrides the normal server_bind() function
        to bind to an interface if one was specified, and also NOT to
        bind to an address or port if they were not specified."""
        if self.settings["interface"]:
            # 25 is from /usr/include/asm-i486/socket.h
            SO_BINDTODEVICE = getattr(socket, "SO_BINDTODEVICE", 25)
            try:
                self.socket.setsockopt(socket.SOL_SOCKET,
                                       SO_BINDTODEVICE,
                                       self.settings["interface"])
            except socket.error, error:
                if error[0] == errno.EPERM:
                    logger.error(u"No permission to"
                                 u" bind to interface %s",
                                 self.settings["interface"])
                else:
                    raise
        # Only bind(2) the socket if we really need to.
        if self.server_address[0] or self.server_address[1]:
            if not self.server_address[0]:
                if self.address_family == socket.AF_INET6:
                    any_address = "::" # in6addr_any
                else:
                    any_address = socket.INADDR_ANY
                self.server_address = (any_address,
                                       self.server_address[1])
            elif not self.server_address[1]:
                self.server_address = (self.server_address[0],
                                       0)
#                 if self.settings["interface"]:
#                     self.server_address = (self.server_address[0],
#                                            0, # port
#                                            0, # flowinfo
#                                            if_nametoindex
#                                            (self.settings
#                                             ["interface"]))
            return super(IPv6_TCPServer, self).server_bind()
    def server_activate(self):
        if self.enabled:
            return super(IPv6_TCPServer, self).server_activate()
    def enable(self):
        self.enabled = True


def string_to_delta(interval):
    """Parse a string and return a datetime.timedelta
    
    >>> string_to_delta('7d')
    datetime.timedelta(7)
    >>> string_to_delta('60s')
    datetime.timedelta(0, 60)
    >>> string_to_delta('60m')
    datetime.timedelta(0, 3600)
    >>> string_to_delta('24h')
    datetime.timedelta(1)
    >>> string_to_delta(u'1w')
    datetime.timedelta(7)
    >>> string_to_delta('5m 30s')
    datetime.timedelta(0, 330)
    """
    timevalue = datetime.timedelta(0)
    for s in interval.split():
        try:
            suffix = unicode(s[-1])
            value = int(s[:-1])
            if suffix == u"d":
                delta = datetime.timedelta(value)
            elif suffix == u"s":
                delta = datetime.timedelta(0, value)
            elif suffix == u"m":
                delta = datetime.timedelta(0, 0, 0, 0, value)
            elif suffix == u"h":
                delta = datetime.timedelta(0, 0, 0, 0, 0, value)
            elif suffix == u"w":
                delta = datetime.timedelta(0, 0, 0, 0, 0, 0, value)
            else:
                raise ValueError
        except (ValueError, IndexError):
            raise ValueError
        timevalue += delta
    return timevalue


def server_state_changed(state):
    """Derived from the Avahi example code"""
    if state == avahi.SERVER_COLLISION:
        logger.error(u"Zeroconf server name collision")
        service.remove()
    elif state == avahi.SERVER_RUNNING:
        service.add()


def entry_group_state_changed(state, error):
    """Derived from the Avahi example code"""
    logger.debug(u"Avahi state change: %i", state)
    
    if state == avahi.ENTRY_GROUP_ESTABLISHED:
        logger.debug(u"Zeroconf service established.")
    elif state == avahi.ENTRY_GROUP_COLLISION:
        logger.warning(u"Zeroconf service name collision.")
        service.rename()
    elif state == avahi.ENTRY_GROUP_FAILURE:
        logger.critical(u"Avahi: Error in group state changed %s",
                        unicode(error))
        raise AvahiGroupError(u"State changed: %s" % unicode(error))

def if_nametoindex(interface):
    """Call the C function if_nametoindex(), or equivalent"""
    global if_nametoindex
    try:
        if_nametoindex = (ctypes.cdll.LoadLibrary
                          (ctypes.util.find_library("c"))
                          .if_nametoindex)
    except (OSError, AttributeError):
        if "struct" not in sys.modules:
            import struct
        if "fcntl" not in sys.modules:
            import fcntl
        def if_nametoindex(interface):
            "Get an interface index the hard way, i.e. using fcntl()"
            SIOCGIFINDEX = 0x8933  # From /usr/include/linux/sockios.h
            with closing(socket.socket()) as s:
                ifreq = fcntl.ioctl(s, SIOCGIFINDEX,
                                    struct.pack("16s16x", interface))
            interface_index = struct.unpack("I", ifreq[16:20])[0]
            return interface_index
    return if_nametoindex(interface)


def daemon(nochdir = False, noclose = False):
    """See daemon(3).  Standard BSD Unix function.
    This should really exist as os.daemon, but it doesn't (yet)."""
    if os.fork():
        sys.exit()
    os.setsid()
    if not nochdir:
        os.chdir("/")
    if os.fork():
        sys.exit()
    if not noclose:
        # Close all standard open file descriptors
        null = os.open(os.path.devnull, os.O_NOCTTY | os.O_RDWR)
        if not stat.S_ISCHR(os.fstat(null).st_mode):
            raise OSError(errno.ENODEV,
                          "/dev/null not a character device")
        os.dup2(null, sys.stdin.fileno())
        os.dup2(null, sys.stdout.fileno())
        os.dup2(null, sys.stderr.fileno())
        if null > 2:
            os.close(null)


def main():
    parser = optparse.OptionParser(version = "%%prog %s" % version)
    parser.add_option("-i", "--interface", type="string",
                      metavar="IF", help="Bind to interface IF")
    parser.add_option("-a", "--address", type="string",
                      help="Address to listen for requests on")
    parser.add_option("-p", "--port", type="int",
                      help="Port number to receive requests on")
    parser.add_option("--check", action="store_true",
                      help="Run self-test")
    parser.add_option("--debug", action="store_true",
                      help="Debug mode; run in foreground and log to"
                      " terminal")
    parser.add_option("--priority", type="string", help="GnuTLS"
                      " priority string (see GnuTLS documentation)")
    parser.add_option("--servicename", type="string", metavar="NAME",
                      help="Zeroconf service name")
    parser.add_option("--configdir", type="string",
                      default="/etc/mandos", metavar="DIR",
                      help="Directory to search for configuration"
                      " files")
    parser.add_option("--no-dbus", action="store_false",
                      dest="use_dbus",
                      help=optparse.SUPPRESS_HELP) # XXX: Not done yet
    parser.add_option("--no-ipv6", action="store_false",
                      dest="use_ipv6", help="Do not use IPv6")
    options = parser.parse_args()[0]
    
    if options.check:
        import doctest
        doctest.testmod()
        sys.exit()
    
    # Default values for config file for server-global settings
    server_defaults = { "interface": "",
                        "address": "",
                        "port": "",
                        "debug": "False",
                        "priority":
                        "SECURE256:!CTYPE-X.509:+CTYPE-OPENPGP",
                        "servicename": "Mandos",
                        "use_dbus": "True",
                        "use_ipv6": "True",
                        }
    
    # Parse config file for server-global settings
    server_config = ConfigParser.SafeConfigParser(server_defaults)
    del server_defaults
    server_config.read(os.path.join(options.configdir, "mandos.conf"))
    # Convert the SafeConfigParser object to a dict
    server_settings = server_config.defaults()
    # Use the appropriate methods on the non-string config options
    server_settings["debug"] = server_config.getboolean("DEFAULT",
                                                        "debug")
    server_settings["use_dbus"] = server_config.getboolean("DEFAULT",
                                                           "use_dbus")
    server_settings["use_ipv6"] = server_config.getboolean("DEFAULT",
                                                           "use_ipv6")
    if server_settings["port"]:
        server_settings["port"] = server_config.getint("DEFAULT",
                                                       "port")
    del server_config
    
    # Override the settings from the config file with command line
    # options, if set.
    for option in ("interface", "address", "port", "debug",
                   "priority", "servicename", "configdir",
                   "use_dbus", "use_ipv6"):
        value = getattr(options, option)
        if value is not None:
            server_settings[option] = value
    del options
    # Now we have our good server settings in "server_settings"
    
    # For convenience
    debug = server_settings["debug"]
    use_dbus = server_settings["use_dbus"]
    use_dbus = False            # XXX: Not done yet
    use_ipv6 = server_settings["use_ipv6"]
    
    if not debug:
        syslogger.setLevel(logging.WARNING)
        console.setLevel(logging.WARNING)
    
    if server_settings["servicename"] != "Mandos":
        syslogger.setFormatter(logging.Formatter
                               ('Mandos (%s): %%(levelname)s:'
                                ' %%(message)s'
                                % server_settings["servicename"]))
    
    # Parse config file with clients
    client_defaults = { "timeout": "1h",
                        "interval": "5m",
                        "checker": "fping -q -- %%(host)s",
                        "host": "",
                        }
    client_config = ConfigParser.SafeConfigParser(client_defaults)
    client_config.read(os.path.join(server_settings["configdir"],
                                    "clients.conf"))
    
    clients = Set()
    tcp_server = IPv6_TCPServer((server_settings["address"],
                                 server_settings["port"]),
                                TCP_handler,
                                settings=server_settings,
                                clients=clients, use_ipv6=use_ipv6)
    pidfilename = "/var/run/mandos.pid"
    try:
        pidfile = open(pidfilename, "w")
    except IOError:
        logger.error("Could not open file %r", pidfilename)
    
    try:
        uid = pwd.getpwnam("_mandos").pw_uid
        gid = pwd.getpwnam("_mandos").pw_gid
    except KeyError:
        try:
            uid = pwd.getpwnam("mandos").pw_uid
            gid = pwd.getpwnam("mandos").pw_gid
        except KeyError:
            try:
                uid = pwd.getpwnam("nobody").pw_uid
                gid = pwd.getpwnam("nogroup").pw_gid
            except KeyError:
                uid = 65534
                gid = 65534
    try:
        os.setgid(gid)
        os.setuid(uid)
    except OSError, error:
        if error[0] != errno.EPERM:
            raise error
    
    # Enable all possible GnuTLS debugging
    if debug:
        # "Use a log level over 10 to enable all debugging options."
        # - GnuTLS manual
        gnutls.library.functions.gnutls_global_set_log_level(11)
        
        @gnutls.library.types.gnutls_log_func
        def debug_gnutls(level, string):
            logger.debug("GnuTLS: %s", string[:-1])
        
        (gnutls.library.functions
         .gnutls_global_set_log_function(debug_gnutls))
    
    global service
    protocol = avahi.PROTO_INET6 if use_ipv6 else avahi.PROTO_INET
    service = AvahiService(name = server_settings["servicename"],
                           servicetype = "_mandos._tcp",
                           protocol = protocol)
    if server_settings["interface"]:
        service.interface = (if_nametoindex
                             (server_settings["interface"]))
    
    global main_loop
    global bus
    global server
    # From the Avahi example code
    DBusGMainLoop(set_as_default=True )
    main_loop = gobject.MainLoop()
    bus = dbus.SystemBus()
    server = dbus.Interface(bus.get_object(avahi.DBUS_NAME,
                                           avahi.DBUS_PATH_SERVER),
                            avahi.DBUS_INTERFACE_SERVER)
    # End of Avahi example code
    if use_dbus:
        bus_name = dbus.service.BusName(u"se.bsnet.fukt.Mandos", bus)
    
    clients.update(Set(Client(name = section,
                              config
                              = dict(client_config.items(section)),
                              use_dbus = use_dbus)
                       for section in client_config.sections()))
    if not clients:
        logger.warning(u"No clients defined")
    
    if debug:
        # Redirect stdin so all checkers get /dev/null
        null = os.open(os.path.devnull, os.O_NOCTTY | os.O_RDWR)
        os.dup2(null, sys.stdin.fileno())
        if null > 2:
            os.close(null)
    else:
        # No console logging
        logger.removeHandler(console)
        # Close all input and output, do double fork, etc.
        daemon()
    
    try:
        pid = os.getpid()
        pidfile.write(str(pid) + "\n")
        pidfile.close()
        del pidfile
    except IOError:
        logger.error(u"Could not write to file %r with PID %d",
                     pidfilename, pid)
    except NameError:
        # "pidfile" was never created
        pass
    del pidfilename
    
    def cleanup():
        "Cleanup function; run on exit"
        global group
        # From the Avahi example code
        if not group is None:
            group.Free()
            group = None
        # End of Avahi example code
        
        while clients:
            client = clients.pop()
            client.disable_hook = None
            client.disable()
    
    atexit.register(cleanup)
    
    if not debug:
        signal.signal(signal.SIGINT, signal.SIG_IGN)
    signal.signal(signal.SIGHUP, lambda signum, frame: sys.exit())
    signal.signal(signal.SIGTERM, lambda signum, frame: sys.exit())
    
    if use_dbus:
        class MandosServer(dbus.service.Object):
            """A D-Bus proxy object"""
            def __init__(self):
                dbus.service.Object.__init__(self, bus, "/")
            _interface = u"se.bsnet.fukt.Mandos"
            
            @dbus.service.signal(_interface, signature="oa{sv}")
            def ClientAdded(self, objpath, properties):
                "D-Bus signal"
                pass
            
            @dbus.service.signal(_interface, signature="os")
            def ClientRemoved(self, objpath, name):
                "D-Bus signal"
                pass
            
            @dbus.service.method(_interface, out_signature="ao")
            def GetAllClients(self):
                "D-Bus method"
                return dbus.Array(c.dbus_object_path for c in clients)
            
            @dbus.service.method(_interface, out_signature="a{oa{sv}}")
            def GetAllClientsWithProperties(self):
                "D-Bus method"
                return dbus.Dictionary(
                    ((c.dbus_object_path, c.GetAllProperties())
                     for c in clients),
                    signature="oa{sv}")
            
            @dbus.service.method(_interface, in_signature="o")
            def RemoveClient(self, object_path):
                "D-Bus method"
                for c in clients:
                    if c.dbus_object_path == object_path:
                        clients.remove(c)
                        # Don't signal anything except ClientRemoved
                        c.use_dbus = False
                        c.disable()
                        # Emit D-Bus signal
                        self.ClientRemoved(object_path, c.name)
                        return
                raise KeyError
            
            del _interface
        
        mandos_server = MandosServer()
    
    for client in clients:
        if use_dbus:
            # Emit D-Bus signal
            mandos_server.ClientAdded(client.dbus_object_path,
                                      client.GetAllProperties())
        client.enable()
    
    tcp_server.enable()
    tcp_server.server_activate()
    
    # Find out what port we got
    service.port = tcp_server.socket.getsockname()[1]
    if use_ipv6:
        logger.info(u"Now listening on address %r, port %d,"
                    " flowinfo %d, scope_id %d"
                    % tcp_server.socket.getsockname())
    else:                       # IPv4
        logger.info(u"Now listening on address %r, port %d"
                    % tcp_server.socket.getsockname())
    
    #service.interface = tcp_server.socket.getsockname()[3]
    
    try:
        # From the Avahi example code
        server.connect_to_signal("StateChanged", server_state_changed)
        try:
            server_state_changed(server.GetState())
        except dbus.exceptions.DBusException, error:
            logger.critical(u"DBusException: %s", error)
            sys.exit(1)
        # End of Avahi example code
        
        gobject.io_add_watch(tcp_server.fileno(), gobject.IO_IN,
                             lambda *args, **kwargs:
                             (tcp_server.handle_request
                              (*args[2:], **kwargs) or True))
        
        logger.debug(u"Starting main loop")
        main_loop.run()
    except AvahiError, error:
        logger.critical(u"AvahiError: %s", error)
        sys.exit(1)
    except KeyboardInterrupt:
        if debug:
            print >> sys.stderr
        logger.debug("Server received KeyboardInterrupt")
    logger.debug("Server exiting")

if __name__ == '__main__':
    main()
