#!/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", "remove", "server_state_changed",
# "entry_group_state_changed", "cleanup", and "activate" in the
# "AvahiService" class, and some lines in "main".
# 
# Everything else is
# Copyright © 2008-2011 Teddy Hogeborn
# Copyright © 2008-2011 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, absolute_import, print_function,
                        unicode_literals)

import SocketServer as 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 as configparser
import sys
import re
import os
import signal
import subprocess
import atexit
import stat
import logging
import logging.handlers
import pwd
import contextlib
import struct
import fcntl
import functools
import cPickle as pickle
import multiprocessing

import dbus
import dbus.service
import gobject
import avahi
from dbus.mainloop.glib import DBusGMainLoop
import ctypes
import ctypes.util
import xml.dom.minidom
import inspect

try:
    SO_BINDTODEVICE = socket.SO_BINDTODEVICE
except AttributeError:
    try:
        from IN import SO_BINDTODEVICE
    except ImportError:
        SO_BINDTODEVICE = None


version = "1.3.0"

#logger = logging.getLogger('mandos')
logger = logging.Logger('mandos')
syslogger = (logging.handlers.SysLogHandler
             (facility = logging.handlers.SysLogHandler.LOG_DAEMON,
              address = str("/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
    group: D-Bus Entry Group
    server: D-Bus Server
    bus: dbus.SystemBus()
    """
    def __init__(self, interface = avahi.IF_UNSPEC, name = None,
                 servicetype = None, port = None, TXT = None,
                 domain = "", host = "", max_renames = 32768,
                 protocol = avahi.PROTO_UNSPEC, bus = None):
        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
        self.group = None       # our entry group
        self.server = None
        self.bus = bus
    def rename(self):
        """Derived from the Avahi example code"""
        if self.rename_count >= self.max_renames:
            logger.critical("No suitable Zeroconf service name found"
                            " after %i retries, exiting.",
                            self.rename_count)
            raise AvahiServiceError("Too many renames")
        self.name = unicode(self.server.GetAlternativeServiceName(self.name))
        logger.info("Changing Zeroconf service name to %r ...",
                    self.name)
        syslogger.setFormatter(logging.Formatter
                               ('Mandos (%s) [%%(process)d]:'
                                ' %%(levelname)s: %%(message)s'
                                % self.name))
        self.remove()
        try:
            self.add()
        except dbus.exceptions.DBusException, error:
            logger.critical("DBusException: %s", error)
            self.cleanup()
            os._exit(1)
        self.rename_count += 1
    def remove(self):
        """Derived from the Avahi example code"""
        if self.group is not None:
            self.group.Reset()
    def add(self):
        """Derived from the Avahi example code"""
        if self.group is None:
            self.group = dbus.Interface(
                self.bus.get_object(avahi.DBUS_NAME,
                                    self.server.EntryGroupNew()),
                avahi.DBUS_INTERFACE_ENTRY_GROUP)
            self.group.connect_to_signal('StateChanged',
                                         self
                                         .entry_group_state_changed)
        logger.debug("Adding Zeroconf service '%s' of type '%s' ...",
                     self.name, self.type)
        self.group.AddService(
            self.interface,
            self.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))
        self.group.Commit()
    def entry_group_state_changed(self, state, error):
        """Derived from the Avahi example code"""
        logger.debug("Avahi entry group state change: %i", state)
        
        if state == avahi.ENTRY_GROUP_ESTABLISHED:
            logger.debug("Zeroconf service established.")
        elif state == avahi.ENTRY_GROUP_COLLISION:
            logger.info("Zeroconf service name collision.")
            self.rename()
        elif state == avahi.ENTRY_GROUP_FAILURE:
            logger.critical("Avahi: Error in group state changed %s",
                            unicode(error))
            raise AvahiGroupError("State changed: %s"
                                  % unicode(error))
    def cleanup(self):
        """Derived from the Avahi example code"""
        if self.group is not None:
            self.group.Free()
            self.group = None
    def server_state_changed(self, state):
        """Derived from the Avahi example code"""
        logger.debug("Avahi server state change: %i", state)
        if state == avahi.SERVER_COLLISION:
            logger.error("Zeroconf server name collision")
            self.remove()
        elif state == avahi.SERVER_RUNNING:
            self.add()
    def activate(self):
        """Derived from the Avahi example code"""
        if self.server is None:
            self.server = dbus.Interface(
                self.bus.get_object(avahi.DBUS_NAME,
                                    avahi.DBUS_PATH_SERVER),
                avahi.DBUS_INTERFACE_SERVER)
        self.server.connect_to_signal("StateChanged",
                                 self.server_state_changed)
        self.server_state_changed(self.server.GetState())


class Client(object):
    """A representation of a client host served by this server.
    
    Attributes:
    _approved:   bool(); 'None' if not yet approved/disapproved
    approval_delay: datetime.timedelta(); Time to wait for approval
    approval_duration: datetime.timedelta(); Duration of one approval
    checker:    subprocess.Popen(); a running checker process used
                                    to see if the client lives.
                                    'None' if no process is running.
    checker_callback_tag: a gobject event source tag, or None
    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.
    checker_initiator_tag: a gobject event source tag, or None
    created:    datetime.datetime(); (UTC) object creation
    current_checker_command: string; current running checker_command
    disable_hook:  If set, called by disable() as disable_hook(self)
    disable_initiator_tag: a gobject event source tag, or None
    enabled:    bool()
    fingerprint: string (40 or 32 hexadecimal digits); used to
                 uniquely identify the client
    host:       string; available for use by the checker command
    interval:   datetime.timedelta(); How often to start a new checker
    last_approval_request: datetime.datetime(); (UTC) or None
    last_checked_ok: datetime.datetime(); (UTC) or None
    last_enabled: datetime.datetime(); (UTC)
    name:       string; from the config file, used in log messages and
                        D-Bus identifiers
    secret:     bytestring; sent verbatim (over TLS) to client
    timeout:    datetime.timedelta(); How long from last_checked_ok
                                      until this client is disabled
    runtime_expansions: Allowed attributes for runtime expansion.
    """
    
    runtime_expansions = ("approval_delay", "approval_duration",
                          "created", "enabled", "fingerprint",
                          "host", "interval", "last_checked_ok",
                          "last_enabled", "name", "timeout")
    
    @staticmethod
    def _timedelta_to_milliseconds(td):
        "Convert a datetime.timedelta() to milliseconds"
        return ((td.days * 24 * 60 * 60 * 1000)
                + (td.seconds * 1000)
                + (td.microseconds // 1000))
    
    def timeout_milliseconds(self):
        "Return the 'timeout' attribute in milliseconds"
        return self._timedelta_to_milliseconds(self.timeout)
    
    def interval_milliseconds(self):
        "Return the 'interval' attribute in milliseconds"
        return self._timedelta_to_milliseconds(self.interval)

    def approval_delay_milliseconds(self):
        return self._timedelta_to_milliseconds(self.approval_delay)
    
    def __init__(self, name = None, disable_hook=None, config=None):
        """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("Creating client %r", self.name)
        # Uppercase and remove spaces from fingerprint for later
        # comparison purposes with return value from the fingerprint()
        # function
        self.fingerprint = (config["fingerprint"].upper()
                            .replace(" ", ""))
        logger.debug("  Fingerprint: %s", self.fingerprint)
        if "secret" in config:
            self.secret = config["secret"].decode("base64")
        elif "secfile" in config:
            with open(os.path.expanduser(os.path.expandvars
                                         (config["secfile"])),
                      "rb") as secfile:
                self.secret = secfile.read()
        else:
            raise TypeError("No secret or secfile for client %s"
                            % self.name)
        self.host = config.get("host", "")
        self.created = datetime.datetime.utcnow()
        self.enabled = False
        self.last_approval_request = None
        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
        self._approved = None
        self.approved_by_default = config.get("approved_by_default",
                                              True)
        self.approvals_pending = 0
        self.approval_delay = string_to_delta(
            config["approval_delay"])
        self.approval_duration = string_to_delta(
            config["approval_duration"])
        self.changedstate = multiprocessing_manager.Condition(multiprocessing_manager.Lock())
    
    def send_changedstate(self):
        self.changedstate.acquire()
        self.changedstate.notify_all()
        self.changedstate.release()
        
    def enable(self):
        """Start this client's checker and timeout hooks"""
        if getattr(self, "enabled", False):
            # Already enabled
            return
        self.send_changedstate()
        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))
        # Schedule a disable() when 'timeout' has passed
        self.disable_initiator_tag = (gobject.timeout_add
                                   (self.timeout_milliseconds(),
                                    self.disable))
        self.enabled = True
        # Also start a new checker *right now*.
        self.start_checker()
    
    def disable(self, quiet=True):
        """Disable this client."""
        if not getattr(self, "enabled", False):
            return False
        if not quiet:
            self.send_changedstate()
        if not quiet:
            logger.info("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
        # 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 os.WIFEXITED(condition):
            exitstatus = os.WEXITSTATUS(condition)
            if exitstatus == 0:
                logger.info("Checker for %(name)s succeeded",
                            vars(self))
                self.checked_ok()
            else:
                logger.info("Checker for %(name)s failed",
                            vars(self))
        else:
            logger.warning("Checker for %(name)s crashed?",
                           vars(self))
    
    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))
    
    def need_approval(self):
        self.last_approval_request = datetime.datetime.utcnow()
    
    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 disabled, which is as it
        # should be.
        
        # If a checker exists, make sure it is not a zombie
        try:
            pid, status = os.waitpid(self.checker.pid, os.WNOHANG)
        except (AttributeError, OSError), error:
            if (isinstance(error, OSError)
                and error.errno != errno.ECHILD):
                raise error
        else:
            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(
                    (attr,
                     re.escape(unicode(str(getattr(self, attr, "")),
                                       errors=
                                       'replace')))
                    for attr in
                    self.runtime_expansions)

                try:
                    command = self.checker_command % escaped_attrs
                except TypeError, error:
                    logger.error('Could not format string "%s":'
                                 ' %s', self.checker_command, error)
                    return True # Try again later
            self.current_checker_command = command
            try:
                logger.info("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="/")
                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("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("Stopping checker for %(name)s", vars(self))
        try:
            os.kill(self.checker.pid, signal.SIGTERM)
            #time.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

def dbus_service_property(dbus_interface, signature="v",
                          access="readwrite", byte_arrays=False):
    """Decorators for marking methods of a DBusObjectWithProperties to
    become properties on the D-Bus.
    
    The decorated method will be called with no arguments by "Get"
    and with one argument by "Set".
    
    The parameters, where they are supported, are the same as
    dbus.service.method, except there is only "signature", since the
    type from Get() and the type sent to Set() is the same.
    """
    # Encoding deeply encoded byte arrays is not supported yet by the
    # "Set" method, so we fail early here:
    if byte_arrays and signature != "ay":
        raise ValueError("Byte arrays not supported for non-'ay'"
                         " signature %r" % signature)
    def decorator(func):
        func._dbus_is_property = True
        func._dbus_interface = dbus_interface
        func._dbus_signature = signature
        func._dbus_access = access
        func._dbus_name = func.__name__
        if func._dbus_name.endswith("_dbus_property"):
            func._dbus_name = func._dbus_name[:-14]
        func._dbus_get_args_options = {'byte_arrays': byte_arrays }
        return func
    return decorator


class DBusPropertyException(dbus.exceptions.DBusException):
    """A base class for D-Bus property-related exceptions
    """
    def __unicode__(self):
        return unicode(str(self))


class DBusPropertyAccessException(DBusPropertyException):
    """A property's access permissions disallows an operation.
    """
    pass


class DBusPropertyNotFound(DBusPropertyException):
    """An attempt was made to access a non-existing property.
    """
    pass


class DBusObjectWithProperties(dbus.service.Object):
    """A D-Bus object with properties.

    Classes inheriting from this can use the dbus_service_property
    decorator to expose methods as D-Bus properties.  It exposes the
    standard Get(), Set(), and GetAll() methods on the D-Bus.
    """
    
    @staticmethod
    def _is_dbus_property(obj):
        return getattr(obj, "_dbus_is_property", False)
    
    def _get_all_dbus_properties(self):
        """Returns a generator of (name, attribute) pairs
        """
        return ((prop._dbus_name, prop)
                for name, prop in
                inspect.getmembers(self, self._is_dbus_property))
    
    def _get_dbus_property(self, interface_name, property_name):
        """Returns a bound method if one exists which is a D-Bus
        property with the specified name and interface.
        """
        for name in (property_name,
                     property_name + "_dbus_property"):
            prop = getattr(self, name, None)
            if (prop is None
                or not self._is_dbus_property(prop)
                or prop._dbus_name != property_name
                or (interface_name and prop._dbus_interface
                    and interface_name != prop._dbus_interface)):
                continue
            return prop
        # No such property
        raise DBusPropertyNotFound(self.dbus_object_path + ":"
                                   + interface_name + "."
                                   + property_name)
    
    @dbus.service.method(dbus.PROPERTIES_IFACE, in_signature="ss",
                         out_signature="v")
    def Get(self, interface_name, property_name):
        """Standard D-Bus property Get() method, see D-Bus standard.
        """
        prop = self._get_dbus_property(interface_name, property_name)
        if prop._dbus_access == "write":
            raise DBusPropertyAccessException(property_name)
        value = prop()
        if not hasattr(value, "variant_level"):
            return value
        return type(value)(value, variant_level=value.variant_level+1)
    
    @dbus.service.method(dbus.PROPERTIES_IFACE, in_signature="ssv")
    def Set(self, interface_name, property_name, value):
        """Standard D-Bus property Set() method, see D-Bus standard.
        """
        prop = self._get_dbus_property(interface_name, property_name)
        if prop._dbus_access == "read":
            raise DBusPropertyAccessException(property_name)
        if prop._dbus_get_args_options["byte_arrays"]:
            # The byte_arrays option is not supported yet on
            # signatures other than "ay".
            if prop._dbus_signature != "ay":
                raise ValueError
            value = dbus.ByteArray(''.join(unichr(byte)
                                           for byte in value))
        prop(value)
    
    @dbus.service.method(dbus.PROPERTIES_IFACE, in_signature="s",
                         out_signature="a{sv}")
    def GetAll(self, interface_name):
        """Standard D-Bus property GetAll() method, see D-Bus
        standard.

        Note: Will not include properties with access="write".
        """
        all = {}
        for name, prop in self._get_all_dbus_properties():
            if (interface_name
                and interface_name != prop._dbus_interface):
                # Interface non-empty but did not match
                continue
            # Ignore write-only properties
            if prop._dbus_access == "write":
                continue
            value = prop()
            if not hasattr(value, "variant_level"):
                all[name] = value
                continue
            all[name] = type(value)(value, variant_level=
                                    value.variant_level+1)
        return dbus.Dictionary(all, signature="sv")
    
    @dbus.service.method(dbus.INTROSPECTABLE_IFACE,
                         out_signature="s",
                         path_keyword='object_path',
                         connection_keyword='connection')
    def Introspect(self, object_path, connection):
        """Standard D-Bus method, overloaded to insert property tags.
        """
        xmlstring = dbus.service.Object.Introspect(self, object_path,
                                                   connection)
        try:
            document = xml.dom.minidom.parseString(xmlstring)
            def make_tag(document, name, prop):
                e = document.createElement("property")
                e.setAttribute("name", name)
                e.setAttribute("type", prop._dbus_signature)
                e.setAttribute("access", prop._dbus_access)
                return e
            for if_tag in document.getElementsByTagName("interface"):
                for tag in (make_tag(document, name, prop)
                            for name, prop
                            in self._get_all_dbus_properties()
                            if prop._dbus_interface
                            == if_tag.getAttribute("name")):
                    if_tag.appendChild(tag)
                # Add the names to the return values for the
                # "org.freedesktop.DBus.Properties" methods
                if (if_tag.getAttribute("name")
                    == "org.freedesktop.DBus.Properties"):
                    for cn in if_tag.getElementsByTagName("method"):
                        if cn.getAttribute("name") == "Get":
                            for arg in cn.getElementsByTagName("arg"):
                                if (arg.getAttribute("direction")
                                    == "out"):
                                    arg.setAttribute("name", "value")
                        elif cn.getAttribute("name") == "GetAll":
                            for arg in cn.getElementsByTagName("arg"):
                                if (arg.getAttribute("direction")
                                    == "out"):
                                    arg.setAttribute("name", "props")
            xmlstring = document.toxml("utf-8")
            document.unlink()
        except (AttributeError, xml.dom.DOMException,
                xml.parsers.expat.ExpatError), error:
            logger.error("Failed to override Introspection method",
                         error)
        return xmlstring


class ClientDBus(Client, DBusObjectWithProperties):
    """A Client class using D-Bus
    
    Attributes:
    dbus_object_path: dbus.ObjectPath
    bus: dbus.SystemBus()
    """
    
    runtime_expansions = (Client.runtime_expansions
                          + ("dbus_object_path",))
    
    # dbus.service.Object doesn't use super(), so we can't either.
    
    def __init__(self, bus = None, *args, **kwargs):
        self._approvals_pending = 0
        self.bus = bus
        Client.__init__(self, *args, **kwargs)
        # Only now, when this client is initialized, can it show up on
        # the D-Bus
        client_object_name = unicode(self.name).translate(
            {ord("."): ord("_"),
             ord("-"): ord("_")})
        self.dbus_object_path = (dbus.ObjectPath
                                 ("/clients/" + client_object_name))
        DBusObjectWithProperties.__init__(self, self.bus,
                                          self.dbus_object_path)
        
    def _get_approvals_pending(self):
        return self._approvals_pending
    def _set_approvals_pending(self, value):
        old_value = self._approvals_pending
        self._approvals_pending = value
        bval = bool(value)
        if (hasattr(self, "dbus_object_path")
            and bval is not bool(old_value)):
            dbus_bool = dbus.Boolean(bval, variant_level=1)
            self.PropertyChanged(dbus.String("ApprovalPending"),
                                 dbus_bool)

    approvals_pending = property(_get_approvals_pending,
                                 _set_approvals_pending)
    del _get_approvals_pending, _set_approvals_pending
    
    @staticmethod
    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)
    
    def enable(self):
        oldstate = getattr(self, "enabled", False)
        r = Client.enable(self)
        if oldstate != self.enabled:
            # Emit D-Bus signals
            self.PropertyChanged(dbus.String("Enabled"),
                                 dbus.Boolean(True, variant_level=1))
            self.PropertyChanged(
                dbus.String("LastEnabled"),
                self._datetime_to_dbus(self.last_enabled,
                                       variant_level=1))
        return r
    
    def disable(self, quiet = False):
        oldstate = getattr(self, "enabled", False)
        r = Client.disable(self, quiet=quiet)
        if not quiet and oldstate != self.enabled:
            # Emit D-Bus signal
            self.PropertyChanged(dbus.String("Enabled"),
                                 dbus.Boolean(False, variant_level=1))
        return r
    
    def __del__(self, *args, **kwargs):
        try:
            self.remove_from_connection()
        except LookupError:
            pass
        if hasattr(DBusObjectWithProperties, "__del__"):
            DBusObjectWithProperties.__del__(self, *args, **kwargs)
        Client.__del__(self, *args, **kwargs)
    
    def checker_callback(self, pid, condition, command,
                         *args, **kwargs):
        self.checker_callback_tag = None
        self.checker = None
        # Emit D-Bus signal
        self.PropertyChanged(dbus.String("CheckerRunning"),
                             dbus.Boolean(False, variant_level=1))
        if os.WIFEXITED(condition):
            exitstatus = os.WEXITSTATUS(condition)
            # Emit D-Bus signal
            self.CheckerCompleted(dbus.Int16(exitstatus),
                                  dbus.Int64(condition),
                                  dbus.String(command))
        else:
            # Emit D-Bus signal
            self.CheckerCompleted(dbus.Int16(-1),
                                  dbus.Int64(condition),
                                  dbus.String(command))
        
        return Client.checker_callback(self, pid, condition, command,
                                       *args, **kwargs)
    
    def checked_ok(self, *args, **kwargs):
        r = Client.checked_ok(self, *args, **kwargs)
        # Emit D-Bus signal
        self.PropertyChanged(
            dbus.String("LastCheckedOK"),
            (self._datetime_to_dbus(self.last_checked_ok,
                                    variant_level=1)))
        return r
    
    def need_approval(self, *args, **kwargs):
        r = Client.need_approval(self, *args, **kwargs)
        # Emit D-Bus signal
        self.PropertyChanged(
            dbus.String("LastApprovalRequest"),
            (self._datetime_to_dbus(self.last_approval_request,
                                    variant_level=1)))
        return r
    
    def start_checker(self, *args, **kwargs):
        old_checker = self.checker
        if self.checker is not None:
            old_checker_pid = self.checker.pid
        else:
            old_checker_pid = None
        r = Client.start_checker(self, *args, **kwargs)
        # Only if new checker process was started
        if (self.checker is not None
            and old_checker_pid != self.checker.pid):
            # Emit D-Bus signal
            self.CheckerStarted(self.current_checker_command)
            self.PropertyChanged(
                dbus.String("CheckerRunning"),
                dbus.Boolean(True, variant_level=1))
        return r
    
    def stop_checker(self, *args, **kwargs):
        old_checker = getattr(self, "checker", None)
        r = Client.stop_checker(self, *args, **kwargs)
        if (old_checker is not None
            and getattr(self, "checker", None) is None):
            self.PropertyChanged(dbus.String("CheckerRunning"),
                                 dbus.Boolean(False, variant_level=1))
        return r

    def _reset_approved(self):
        self._approved = None
        return False
    
    def approve(self, value=True):
        self.send_changedstate()
        self._approved = value
        gobject.timeout_add(self._timedelta_to_milliseconds
                            (self.approval_duration),
                            self._reset_approved)
    
    
    ## D-Bus methods, signals & properties
    _interface = "se.bsnet.fukt.Mandos.Client"
    
    ## Signals
    
    # 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
    
    # PropertyChanged - signal
    @dbus.service.signal(_interface, signature="sv")
    def PropertyChanged(self, property, value):
        "D-Bus signal"
        pass
    
    # GotSecret - signal
    @dbus.service.signal(_interface)
    def GotSecret(self):
        """D-Bus signal
        Is sent after a successful transfer of secret from the Mandos
        server to mandos-client
        """
        pass
    
    # Rejected - signal
    @dbus.service.signal(_interface, signature="s")
    def Rejected(self, reason):
        "D-Bus signal"
        pass
    
    # NeedApproval - signal
    @dbus.service.signal(_interface, signature="tb")
    def NeedApproval(self, timeout, default):
        "D-Bus signal"
        return self.need_approval()
    
    ## Methods
    
    # Approve - method
    @dbus.service.method(_interface, in_signature="b")
    def Approve(self, value):
        self.approve(value)
    
    # CheckedOK - method
    @dbus.service.method(_interface)
    def CheckedOK(self):
        return self.checked_ok()
    
    # Enable - method
    @dbus.service.method(_interface)
    def Enable(self):
        "D-Bus method"
        self.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
    @dbus.service.method(_interface)
    def StopChecker(self):
        self.stop_checker()
    
    ## Properties
    
    # ApprovalPending - property
    @dbus_service_property(_interface, signature="b", access="read")
    def ApprovalPending_dbus_property(self):
        return dbus.Boolean(bool(self.approvals_pending))
    
    # ApprovedByDefault - property
    @dbus_service_property(_interface, signature="b",
                           access="readwrite")
    def ApprovedByDefault_dbus_property(self, value=None):
        if value is None:       # get
            return dbus.Boolean(self.approved_by_default)
        self.approved_by_default = bool(value)
        # Emit D-Bus signal
        self.PropertyChanged(dbus.String("ApprovedByDefault"),
                             dbus.Boolean(value, variant_level=1))
    
    # ApprovalDelay - property
    @dbus_service_property(_interface, signature="t",
                           access="readwrite")
    def ApprovalDelay_dbus_property(self, value=None):
        if value is None:       # get
            return dbus.UInt64(self.approval_delay_milliseconds())
        self.approval_delay = datetime.timedelta(0, 0, 0, value)
        # Emit D-Bus signal
        self.PropertyChanged(dbus.String("ApprovalDelay"),
                             dbus.UInt64(value, variant_level=1))
    
    # ApprovalDuration - property
    @dbus_service_property(_interface, signature="t",
                           access="readwrite")
    def ApprovalDuration_dbus_property(self, value=None):
        if value is None:       # get
            return dbus.UInt64(self._timedelta_to_milliseconds(
                    self.approval_duration))
        self.approval_duration = datetime.timedelta(0, 0, 0, value)
        # Emit D-Bus signal
        self.PropertyChanged(dbus.String("ApprovalDuration"),
                             dbus.UInt64(value, variant_level=1))
    
    # Name - property
    @dbus_service_property(_interface, signature="s", access="read")
    def Name_dbus_property(self):
        return dbus.String(self.name)
    
    # Fingerprint - property
    @dbus_service_property(_interface, signature="s", access="read")
    def Fingerprint_dbus_property(self):
        return dbus.String(self.fingerprint)
    
    # Host - property
    @dbus_service_property(_interface, signature="s",
                           access="readwrite")
    def Host_dbus_property(self, value=None):
        if value is None:       # get
            return dbus.String(self.host)
        self.host = value
        # Emit D-Bus signal
        self.PropertyChanged(dbus.String("Host"),
                             dbus.String(value, variant_level=1))
    
    # Created - property
    @dbus_service_property(_interface, signature="s", access="read")
    def Created_dbus_property(self):
        return dbus.String(self._datetime_to_dbus(self.created))
    
    # LastEnabled - property
    @dbus_service_property(_interface, signature="s", access="read")
    def LastEnabled_dbus_property(self):
        if self.last_enabled is None:
            return dbus.String("")
        return dbus.String(self._datetime_to_dbus(self.last_enabled))
    
    # Enabled - property
    @dbus_service_property(_interface, signature="b",
                           access="readwrite")
    def Enabled_dbus_property(self, value=None):
        if value is None:       # get
            return dbus.Boolean(self.enabled)
        if value:
            self.enable()
        else:
            self.disable()
    
    # LastCheckedOK - property
    @dbus_service_property(_interface, signature="s",
                           access="readwrite")
    def LastCheckedOK_dbus_property(self, value=None):
        if value is not None:
            self.checked_ok()
            return
        if self.last_checked_ok is None:
            return dbus.String("")
        return dbus.String(self._datetime_to_dbus(self
                                                  .last_checked_ok))
    
    # LastApprovalRequest - property
    @dbus_service_property(_interface, signature="s", access="read")
    def LastApprovalRequest_dbus_property(self):
        if self.last_approval_request is None:
            return dbus.String("")
        return dbus.String(self.
                           _datetime_to_dbus(self
                                             .last_approval_request))
    
    # Timeout - property
    @dbus_service_property(_interface, signature="t",
                           access="readwrite")
    def Timeout_dbus_property(self, value=None):
        if value is None:       # get
            return dbus.UInt64(self.timeout_milliseconds())
        self.timeout = datetime.timedelta(0, 0, 0, value)
        # Emit D-Bus signal
        self.PropertyChanged(dbus.String("Timeout"),
                             dbus.UInt64(value, variant_level=1))
        if getattr(self, "disable_initiator_tag", None) is None:
            return
        # Reschedule timeout
        gobject.source_remove(self.disable_initiator_tag)
        self.disable_initiator_tag = None
        time_to_die = (self.
                       _timedelta_to_milliseconds((self
                                                   .last_checked_ok
                                                   + self.timeout)
                                                  - datetime.datetime
                                                  .utcnow()))
        if time_to_die <= 0:
            # The timeout has passed
            self.disable()
        else:
            self.disable_initiator_tag = (gobject.timeout_add
                                          (time_to_die, self.disable))
    
    # Interval - property
    @dbus_service_property(_interface, signature="t",
                           access="readwrite")
    def Interval_dbus_property(self, value=None):
        if value is None:       # get
            return dbus.UInt64(self.interval_milliseconds())
        self.interval = datetime.timedelta(0, 0, 0, value)
        # Emit D-Bus signal
        self.PropertyChanged(dbus.String("Interval"),
                             dbus.UInt64(value, variant_level=1))
        if getattr(self, "checker_initiator_tag", None) is None:
            return
        # Reschedule checker run
        gobject.source_remove(self.checker_initiator_tag)
        self.checker_initiator_tag = (gobject.timeout_add
                                      (value, self.start_checker))
        self.start_checker()    # Start one now, too

    # Checker - property
    @dbus_service_property(_interface, signature="s",
                           access="readwrite")
    def Checker_dbus_property(self, value=None):
        if value is None:       # get
            return dbus.String(self.checker_command)
        self.checker_command = value
        # Emit D-Bus signal
        self.PropertyChanged(dbus.String("Checker"),
                             dbus.String(self.checker_command,
                                         variant_level=1))
    
    # CheckerRunning - property
    @dbus_service_property(_interface, signature="b",
                           access="readwrite")
    def CheckerRunning_dbus_property(self, value=None):
        if value is None:       # get
            return dbus.Boolean(self.checker is not None)
        if value:
            self.start_checker()
        else:
            self.stop_checker()
    
    # ObjectPath - property
    @dbus_service_property(_interface, signature="o", access="read")
    def ObjectPath_dbus_property(self):
        return self.dbus_object_path # is already a dbus.ObjectPath
    
    # Secret = property
    @dbus_service_property(_interface, signature="ay",
                           access="write", byte_arrays=True)
    def Secret_dbus_property(self, value):
        self.secret = str(value)
    
    del _interface


class ProxyClient(object):
    def __init__(self, child_pipe, fpr, address):
        self._pipe = child_pipe
        self._pipe.send(('init', fpr, address))
        if not self._pipe.recv():
            raise KeyError()

    def __getattribute__(self, name):
        if(name == '_pipe'):
            return super(ProxyClient, self).__getattribute__(name)
        self._pipe.send(('getattr', name))
        data = self._pipe.recv()
        if data[0] == 'data':
            return data[1]
        if data[0] == 'function':
            def func(*args, **kwargs):
                self._pipe.send(('funcall', name, args, kwargs))
                return self._pipe.recv()[1]
            return func

    def __setattr__(self, name, value):
        if(name == '_pipe'):
            return super(ProxyClient, self).__setattr__(name, value)
        self._pipe.send(('setattr', name, value))


class ClientHandler(socketserver.BaseRequestHandler, object):
    """A class to handle client connections.
    
    Instantiated once for each connection to handle it.
    Note: This will run in its own forked process."""
    
    def handle(self):
        with contextlib.closing(self.server.child_pipe) as child_pipe:
            logger.info("TCP connection from: %s",
                        unicode(self.client_address))
            logger.debug("Pipe FD: %d",
                         self.server.child_pipe.fileno())

            session = (gnutls.connection
                       .ClientSession(self.request,
                                      gnutls.connection
                                      .X509Credentials()))

            # 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.gnutls_priority
            if priority is None:
                priority = "NORMAL"
            (gnutls.library.functions
             .gnutls_priority_set_direct(session._c_object,
                                         priority, None))

            # Start communication using the Mandos protocol
            # Get protocol number
            line = self.request.makefile().readline()
            logger.debug("Protocol version: %r", line)
            try:
                if int(line.strip().split()[0]) > 1:
                    raise RuntimeError
            except (ValueError, IndexError, RuntimeError), error:
                logger.error("Unknown protocol version: %s", error)
                return

            # Start GnuTLS connection
            try:
                session.handshake()
            except gnutls.errors.GNUTLSError, error:
                logger.warning("Handshake failed: %s", error)
                # Do not run session.bye() here: the session is not
                # established.  Just abandon the request.
                return
            logger.debug("Handshake succeeded")

            approval_required = False
            try:
                try:
                    fpr = self.fingerprint(self.peer_certificate
                                           (session))
                except (TypeError, gnutls.errors.GNUTLSError), error:
                    logger.warning("Bad certificate: %s", error)
                    return
                logger.debug("Fingerprint: %s", fpr)

                try:
                    client = ProxyClient(child_pipe, fpr,
                                         self.client_address)
                except KeyError:
                    return
                
                if client.approval_delay:
                    delay = client.approval_delay
                    client.approvals_pending += 1
                    approval_required = True
                
                while True:
                    if not client.enabled:
                        logger.warning("Client %s is disabled",
                                       client.name)
                        if self.server.use_dbus:
                            # Emit D-Bus signal
                            client.Rejected("Disabled")                    
                        return
                    
                    if client._approved or not client.approval_delay:
                        #We are approved or approval is disabled
                        break
                    elif client._approved is None:
                        logger.info("Client %s needs approval",
                                    client.name)
                        if self.server.use_dbus:
                            # Emit D-Bus signal
                            client.NeedApproval(
                                client.approval_delay_milliseconds(),
                                client.approved_by_default)
                    else:
                        logger.warning("Client %s was not approved",
                                       client.name)
                        if self.server.use_dbus:
                            # Emit D-Bus signal
                            client.Rejected("Denied")
                        return
                    
                    #wait until timeout or approved
                    #x = float(client._timedelta_to_milliseconds(delay))
                    time = datetime.datetime.now()
                    client.changedstate.acquire()
                    client.changedstate.wait(float(client._timedelta_to_milliseconds(delay) / 1000))
                    client.changedstate.release()
                    time2 = datetime.datetime.now()
                    if (time2 - time) >= delay:
                        if not client.approved_by_default:
                            logger.warning("Client %s timed out while"
                                           " waiting for approval",
                                           client.name)
                            if self.server.use_dbus:
                                # Emit D-Bus signal
                                client.Rejected("Approval timed out")
                            return
                        else:
                            break
                    else:
                        delay -= time2 - time
                
                sent_size = 0
                while sent_size < len(client.secret):
                    try:
                        sent = session.send(client.secret[sent_size:])
                    except (gnutls.errors.GNUTLSError), error:
                        logger.warning("gnutls send failed")
                        return
                    logger.debug("Sent: %d, remaining: %d",
                                 sent, len(client.secret)
                                 - (sent_size + sent))
                    sent_size += sent

                logger.info("Sending secret to %s", client.name)
                # bump the timeout as if seen
                client.checked_ok()
                if self.server.use_dbus:
                    # Emit D-Bus signal
                    client.GotSecret()
            
            finally:
                if approval_required:
                    client.approvals_pending -= 1
                try:
                    session.bye()
                except (gnutls.errors.GNUTLSError), error:
                    logger.warning("GnuTLS bye failed")
    
    @staticmethod
    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)
    
    @staticmethod
    def fingerprint(openpgp):
        "Convert an OpenPGP bytestring to a hexdigit fingerprint"
        # 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 = ''.join("%02X" % ord(char) for char in fpr)
        return hex_fpr


class MultiprocessingMixIn(object):
    """Like socketserver.ThreadingMixIn, but with multiprocessing"""
    def sub_process_main(self, request, address):
        try:
            self.finish_request(request, address)
        except:
            self.handle_error(request, address)
        self.close_request(request)
            
    def process_request(self, request, address):
        """Start a new process to process the request."""
        multiprocessing.Process(target = self.sub_process_main,
                                args = (request, address)).start()

class MultiprocessingMixInWithPipe(MultiprocessingMixIn, object):
    """ adds a pipe to the MixIn """
    def process_request(self, request, client_address):
        """Overrides and wraps the original process_request().
        
        This function creates a new pipe in self.pipe
        """
        parent_pipe, self.child_pipe = multiprocessing.Pipe()

        super(MultiprocessingMixInWithPipe,
              self).process_request(request, client_address)
        self.child_pipe.close()
        self.add_pipe(parent_pipe)

    def add_pipe(self, parent_pipe):
        """Dummy function; override as necessary"""
        raise NotImplementedError

class IPv6_TCPServer(MultiprocessingMixInWithPipe,
                     socketserver.TCPServer, object):
    """IPv6-capable TCP server.  Accepts 'None' as address and/or port
    
    Attributes:
        enabled:        Boolean; whether this server is activated yet
        interface:      None or a network interface name (string)
        use_ipv6:       Boolean; to use IPv6 or not
    """
    def __init__(self, server_address, RequestHandlerClass,
                 interface=None, use_ipv6=True):
        self.interface = interface
        if use_ipv6:
            self.address_family = socket.AF_INET6
        socketserver.TCPServer.__init__(self, server_address,
                                        RequestHandlerClass)
    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.interface is not None:
            if SO_BINDTODEVICE is None:
                logger.error("SO_BINDTODEVICE does not exist;"
                             " cannot bind to interface %s",
                             self.interface)
            else:
                try:
                    self.socket.setsockopt(socket.SOL_SOCKET,
                                           SO_BINDTODEVICE,
                                           str(self.interface
                                               + '\0'))
                except socket.error, error:
                    if error[0] == errno.EPERM:
                        logger.error("No permission to"
                                     " bind to interface %s",
                                     self.interface)
                    elif error[0] == errno.ENOPROTOOPT:
                        logger.error("SO_BINDTODEVICE not available;"
                                     " cannot bind to interface %s",
                                     self.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.interface:
#                     self.server_address = (self.server_address[0],
#                                            0, # port
#                                            0, # flowinfo
#                                            if_nametoindex
#                                            (self.interface))
            return socketserver.TCPServer.server_bind(self)


class MandosServer(IPv6_TCPServer):
    """Mandos server.
    
    Attributes:
        clients:        set of Client objects
        gnutls_priority GnuTLS priority string
        use_dbus:       Boolean; to emit D-Bus signals or not
    
    Assumes a gobject.MainLoop event loop.
    """
    def __init__(self, server_address, RequestHandlerClass,
                 interface=None, use_ipv6=True, clients=None,
                 gnutls_priority=None, use_dbus=True):
        self.enabled = False
        self.clients = clients
        if self.clients is None:
            self.clients = set()
        self.use_dbus = use_dbus
        self.gnutls_priority = gnutls_priority
        IPv6_TCPServer.__init__(self, server_address,
                                RequestHandlerClass,
                                interface = interface,
                                use_ipv6 = use_ipv6)
    def server_activate(self):
        if self.enabled:
            return socketserver.TCPServer.server_activate(self)
    def enable(self):
        self.enabled = True
    def add_pipe(self, parent_pipe):
        # Call "handle_ipc" for both data and EOF events
        gobject.io_add_watch(parent_pipe.fileno(),
                             gobject.IO_IN | gobject.IO_HUP,
                             functools.partial(self.handle_ipc,
                                               parent_pipe = parent_pipe))
        
    def handle_ipc(self, source, condition, parent_pipe=None,
                   client_object=None):
        condition_names = {
            gobject.IO_IN: "IN",   # There is data to read.
            gobject.IO_OUT: "OUT", # Data can be written (without
                                    # blocking).
            gobject.IO_PRI: "PRI", # There is urgent data to read.
            gobject.IO_ERR: "ERR", # Error condition.
            gobject.IO_HUP: "HUP"  # Hung up (the connection has been
                                    # broken, usually for pipes and
                                    # sockets).
            }
        conditions_string = ' | '.join(name
                                       for cond, name in
                                       condition_names.iteritems()
                                       if cond & condition)
        # error or the other end of multiprocessing.Pipe has closed
        if condition & (gobject.IO_ERR | condition & gobject.IO_HUP):
            return False
        
        # Read a request from the child
        request = parent_pipe.recv()
        command = request[0]
        
        if command == 'init':
            fpr = request[1]
            address = request[2]
            
            for c in self.clients:
                if c.fingerprint == fpr:
                    client = c
                    break
            else:
                logger.warning("Client not found for fingerprint: %s, ad"
                               "dress: %s", fpr, address)
                if self.use_dbus:
                    # Emit D-Bus signal
                    mandos_dbus_service.ClientNotFound(fpr, address[0])
                parent_pipe.send(False)
                return False
            
            gobject.io_add_watch(parent_pipe.fileno(),
                                 gobject.IO_IN | gobject.IO_HUP,
                                 functools.partial(self.handle_ipc,
                                                   parent_pipe = parent_pipe,
                                                   client_object = client))
            parent_pipe.send(True)
            # remove the old hook in favor of the new above hook on same fileno
            return False
        if command == 'funcall':
            funcname = request[1]
            args = request[2]
            kwargs = request[3]
            
            parent_pipe.send(('data', getattr(client_object, funcname)(*args, **kwargs)))

        if command == 'getattr':
            attrname = request[1]
            if callable(client_object.__getattribute__(attrname)):
                parent_pipe.send(('function',))
            else:
                parent_pipe.send(('data', client_object.__getattribute__(attrname)))
        
        if command == 'setattr':
            attrname = request[1]
            value = request[2]
            setattr(client_object, attrname, value)

        return 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('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 == "d":
                delta = datetime.timedelta(value)
            elif suffix == "s":
                delta = datetime.timedelta(0, value)
            elif suffix == "m":
                delta = datetime.timedelta(0, 0, 0, 0, value)
            elif suffix == "h":
                delta = datetime.timedelta(0, 0, 0, 0, 0, value)
            elif suffix == "w":
                delta = datetime.timedelta(0, 0, 0, 0, 0, 0, value)
            else:
                raise ValueError("Unknown suffix %r" % suffix)
        except (ValueError, IndexError), e:
            raise ValueError(*(e.args))
        timevalue += delta
    return timevalue


def if_nametoindex(interface):
    """Call the C function if_nametoindex(), or equivalent
    
    Note: This function cannot accept a unicode string."""
    global if_nametoindex
    try:
        if_nametoindex = (ctypes.cdll.LoadLibrary
                          (ctypes.util.find_library("c"))
                          .if_nametoindex)
    except (OSError, AttributeError):
        logger.warning("Doing if_nametoindex the hard way")
        def if_nametoindex(interface):
            "Get an interface index the hard way, i.e. using fcntl()"
            SIOCGIFINDEX = 0x8933  # From /usr/include/linux/sockios.h
            with contextlib.closing(socket.socket()) as s:
                ifreq = fcntl.ioctl(s, SIOCGIFINDEX,
                                    struct.pack(str("16s16x"),
                                                interface))
            interface_index = struct.unpack(str("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,
                          "%s not a character device"
                          % os.path.devnull)
        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():
    
    ##################################################################
    # Parsing of options, both command line and config file
    
    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("--debuglevel", type="string", metavar="LEVEL",
                      help="Debug level for stdout output")
    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="Do not provide D-Bus"
                      " system bus interface")
    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",
                        "debuglevel": "",
                        }
    
    # 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
    for option in ("debug", "use_dbus", "use_ipv6"):
        server_settings[option] = server_config.getboolean("DEFAULT",
                                                           option)
    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", "debuglevel"):
        value = getattr(options, option)
        if value is not None:
            server_settings[option] = value
    del options
    # Force all strings to be unicode
    for option in server_settings.keys():
        if type(server_settings[option]) is str:
            server_settings[option] = unicode(server_settings[option])
    # Now we have our good server settings in "server_settings"
    
    ##################################################################
    
    # For convenience
    debug = server_settings["debug"]
    debuglevel = server_settings["debuglevel"]
    use_dbus = server_settings["use_dbus"]
    use_ipv6 = server_settings["use_ipv6"]

    if server_settings["servicename"] != "Mandos":
        syslogger.setFormatter(logging.Formatter
                               ('Mandos (%s) [%%(process)d]:'
                                ' %%(levelname)s: %%(message)s'
                                % server_settings["servicename"]))
    
    # Parse config file with clients
    client_defaults = { "timeout": "1h",
                        "interval": "5m",
                        "checker": "fping -q -- %%(host)s",
                        "host": "",
                        "approval_delay": "0s",
                        "approval_duration": "1s",
                        }
    client_config = configparser.SafeConfigParser(client_defaults)
    client_config.read(os.path.join(server_settings["configdir"],
                                    "clients.conf"))
    
    global mandos_dbus_service
    mandos_dbus_service = None
    
    tcp_server = MandosServer((server_settings["address"],
                               server_settings["port"]),
                              ClientHandler,
                              interface=(server_settings["interface"]
                                         or None),
                              use_ipv6=use_ipv6,
                              gnutls_priority=
                              server_settings["priority"],
                              use_dbus=use_dbus)
    if not debug:
        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("nobody").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
    
    if not debug and not debuglevel:
        syslogger.setLevel(logging.WARNING)
        console.setLevel(logging.WARNING)
    if debuglevel:
        level = getattr(logging, debuglevel.upper())
        syslogger.setLevel(level)
        console.setLevel(level)

    if debug:
        # Enable all possible GnuTLS debugging
        
        # "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))
        
        # 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)
    
    # Need to fork before connecting to D-Bus
    if not debug:
        # Close all input and output, do double fork, etc.
        daemon()
    
    global main_loop
    # From the Avahi example code
    DBusGMainLoop(set_as_default=True )
    main_loop = gobject.MainLoop()
    bus = dbus.SystemBus()
    # End of Avahi example code
    if use_dbus:
        try:
            bus_name = dbus.service.BusName("se.bsnet.fukt.Mandos",
                                            bus, do_not_queue=True)
        except dbus.exceptions.NameExistsException, e:
            logger.error(unicode(e) + ", disabling D-Bus")
            use_dbus = False
            server_settings["use_dbus"] = False
            tcp_server.use_dbus = False
    protocol = avahi.PROTO_INET6 if use_ipv6 else avahi.PROTO_INET
    service = AvahiService(name = server_settings["servicename"],
                           servicetype = "_mandos._tcp",
                           protocol = protocol, bus = bus)
    if server_settings["interface"]:
        service.interface = (if_nametoindex
                             (str(server_settings["interface"])))
    
    global multiprocessing_manager
    multiprocessing_manager = multiprocessing.Manager()
    
    client_class = Client
    if use_dbus:
        client_class = functools.partial(ClientDBus, bus = bus)
    def client_config_items(config, section):
        special_settings = {
            "approved_by_default":
                lambda: config.getboolean(section,
                                          "approved_by_default"),
            }
        for name, value in config.items(section):
            try:
                yield (name, special_settings[name]())
            except KeyError:
                yield (name, value)
    
    tcp_server.clients.update(set(
            client_class(name = section,
                         config= dict(client_config_items(
                        client_config, section)))
            for section in client_config.sections()))
    if not tcp_server.clients:
        logger.warning("No clients defined")
        
    if not debug:
        try:
            with pidfile:
                pid = os.getpid()
                pidfile.write(str(pid) + "\n".encode("utf-8"))
            del pidfile
        except IOError:
            logger.error("Could not write to file %r with PID %d",
                         pidfilename, pid)
        except NameError:
            # "pidfile" was never created
            pass
        del pidfilename
        
        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 MandosDBusService(dbus.service.Object):
            """A D-Bus proxy object"""
            def __init__(self):
                dbus.service.Object.__init__(self, bus, "/")
            _interface = "se.bsnet.fukt.Mandos"
            
            @dbus.service.signal(_interface, signature="o")
            def ClientAdded(self, objpath):
                "D-Bus signal"
                pass
            
            @dbus.service.signal(_interface, signature="ss")
            def ClientNotFound(self, fingerprint, address):
                "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 tcp_server.clients)
            
            @dbus.service.method(_interface,
                                 out_signature="a{oa{sv}}")
            def GetAllClientsWithProperties(self):
                "D-Bus method"
                return dbus.Dictionary(
                    ((c.dbus_object_path, c.GetAll(""))
                     for c in tcp_server.clients),
                    signature="oa{sv}")
            
            @dbus.service.method(_interface, in_signature="o")
            def RemoveClient(self, object_path):
                "D-Bus method"
                for c in tcp_server.clients:
                    if c.dbus_object_path == object_path:
                        tcp_server.clients.remove(c)
                        c.remove_from_connection()
                        # Don't signal anything except ClientRemoved
                        c.disable(quiet=True)
                        # Emit D-Bus signal
                        self.ClientRemoved(object_path, c.name)
                        return
                raise KeyError(object_path)
            
            del _interface
        
        mandos_dbus_service = MandosDBusService()
    
    def cleanup():
        "Cleanup function; run on exit"
        service.cleanup()
        
        while tcp_server.clients:
            client = tcp_server.clients.pop()
            if use_dbus:
                client.remove_from_connection()
            client.disable_hook = None
            # Don't signal anything except ClientRemoved
            client.disable(quiet=True)
            if use_dbus:
                # Emit D-Bus signal
                mandos_dbus_service.ClientRemoved(client.dbus_object_path,
                                                  client.name)
    
    atexit.register(cleanup)
    
    for client in tcp_server.clients:
        if use_dbus:
            # Emit D-Bus signal
            mandos_dbus_service.ClientAdded(client.dbus_object_path)
        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("Now listening on address %r, port %d,"
                    " flowinfo %d, scope_id %d"
                    % tcp_server.socket.getsockname())
    else:                       # IPv4
        logger.info("Now listening on address %r, port %d"
                    % tcp_server.socket.getsockname())
    
    #service.interface = tcp_server.socket.getsockname()[3]
    
    try:
        # From the Avahi example code
        try:
            service.activate()
        except dbus.exceptions.DBusException, error:
            logger.critical("DBusException: %s", error)
            cleanup()
            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("Starting main loop")
        main_loop.run()
    except AvahiError, error:
        logger.critical("AvahiError: %s", error)
        cleanup()
        sys.exit(1)
    except KeyboardInterrupt:
        if debug:
            print("", file=sys.stderr)
        logger.debug("Server received KeyboardInterrupt")
    logger.debug("Server exiting")
    # Must run before the D-Bus bus name gets deregistered
    cleanup()

if __name__ == '__main__':
    main()
