/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to server.py

  • Committer: Teddy Hogeborn
  • Date: 2008-08-04 21:25:55 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080804212555-rm7xxjze65f8avy3
* server.py: Cosmetic changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
# This program is partly derived from an example program for an Avahi
7
7
# service publisher, downloaded from
8
8
# <http://avahi.org/wiki/PythonPublishExample>.  This includes the
9
 
# methods "add" and "remove" in the "AvahiService" class, the
10
 
# "server_state_changed" and "entry_group_state_changed" functions,
11
 
# and some lines in "main".
 
9
# following functions: "AvahiService.add", "AvahiService.remove",
 
10
# "server_state_changed", "entry_group_state_changed", and some lines
 
11
# in "main".
12
12
13
13
# Everything else is
14
14
# Copyright © 2007-2008 Teddy Hogeborn & Björn Påhlsson
61
61
from dbus.mainloop.glib import DBusGMainLoop
62
62
import ctypes
63
63
 
 
64
# Brief description of the operation of this program:
 
65
 
66
# This server announces itself as a Zeroconf service.  Connecting
 
67
# clients use the TLS protocol, with the unusual quirk that this
 
68
# server program acts as a TLS "client" while a connecting client acts
 
69
# as a TLS "server".  The client (acting as a TLS "server") must
 
70
# supply an OpenPGP certificate, and the fingerprint of this
 
71
# certificate is used by this server to look up (in a list read from a
 
72
# file at start time) which binary blob to give the client.  No other
 
73
# authentication or authorization is done by this server.
 
74
 
64
75
 
65
76
logger = logging.Logger('mandos')
66
77
syslogger = logging.handlers.SysLogHandler\
528
539
                in6addr_any = "::"
529
540
                self.server_address = (in6addr_any,
530
541
                                       self.server_address[1])
531
 
            elif not self.server_address[1]:
 
542
            elif self.server_address[1] is None:
532
543
                self.server_address = (self.server_address[0],
533
544
                                       0)
534
 
#                 if self.settings["interface"]:
535
 
#                     self.server_address = (self.server_address[0],
536
 
#                                            0, # port
537
 
#                                            0, # flowinfo
538
 
#                                            if_nametoindex
539
 
#                                            (self.settings
540
 
#                                             ["interface"]))
541
545
            return super(type(self), self).server_bind()
542
546
 
543
547
 
623
627
    return if_nametoindex(interface)
624
628
 
625
629
 
626
 
def daemon(nochdir = False, noclose = False):
 
630
def daemon(nochdir, noclose):
627
631
    """See daemon(3).  Standard BSD Unix function.
628
632
    This should really exist as os.daemon, but it doesn't (yet)."""
629
633
    if os.fork():
631
635
    os.setsid()
632
636
    if not nochdir:
633
637
        os.chdir("/")
634
 
    if os.fork():
635
 
        sys.exit()
636
638
    if not noclose:
637
639
        # Close all standard open file descriptors
638
640
        null = os.open(os.path.devnull, os.O_NOCTTY | os.O_RDWR)
659
661
                      help="Port number to receive requests on")
660
662
    parser.add_option("--check", action="store_true", default=False,
661
663
                      help="Run self-test")
662
 
    parser.add_option("--debug", action="store_true",
 
664
    parser.add_option("--debug", action="store_true", default=False,
663
665
                      help="Debug mode; run in foreground and log to"
664
666
                      " terminal")
665
667
    parser.add_option("--priority", type="string", help="GnuTLS"
690
692
    # Parse config file for server-global settings
691
693
    server_config = ConfigParser.SafeConfigParser(server_defaults)
692
694
    del server_defaults
693
 
    server_config.read(os.path.join(options.configdir, "mandos.conf"))
 
695
    server_config.read(os.path.join(options.configdir, "server.conf"))
694
696
    server_section = "server"
695
697
    # Convert the SafeConfigParser object to a dict
696
698
    server_settings = dict(server_config.items(server_section))
760
762
                       for section in client_config.sections()))
761
763
    
762
764
    if not debug:
763
 
        daemon()
764
 
 
765
 
    pidfilename = "/var/run/mandos/mandos.pid"
766
 
    pid = os.getpid()
767
 
    try:
768
 
        pidfile = open(pidfilename, "w")
769
 
        pidfile.write(str(pid) + "\n")
770
 
        pidfile.close()
771
 
        del pidfile
772
 
    except IOError, err:
773
 
        logger.error("Could not write %s file with PID %d",
774
 
                     pidfilename, os.getpid())
 
765
        daemon(False, False)
775
766
    
776
767
    def cleanup():
777
768
        "Cleanup function; run on exit"