/mandos/release

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

« back to all changes in this revision

Viewing changes to mandos

  • Committer: Teddy Hogeborn
  • Date: 2008-08-25 01:16:38 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080825011638-zgqej2cafhne05ay
* mandos-keygen: Strip 24-bit checksum of Radix-64 from output to make
                 output strictly base64.

Show diffs side-by-side

added added

removed removed

Lines of Context:
71
71
                        ('Mandos: %(levelname)s: %(message)s'))
72
72
logger.addHandler(syslogger)
73
73
 
 
74
console = logging.StreamHandler()
 
75
console.setFormatter(logging.Formatter('%(name)s: %(levelname)s:'
 
76
                                       ' %(message)s'))
 
77
logger.addHandler(console)
74
78
 
75
79
class AvahiError(Exception):
76
80
    def __init__(self, value):
115
119
        self.domain = domain
116
120
        self.host = host
117
121
        self.rename_count = 0
 
122
        self.max_renames = max_renames
118
123
    def rename(self):
119
124
        """Derived from the Avahi example code"""
120
125
        if self.rename_count >= self.max_renames:
121
126
            logger.critical(u"No suitable service name found after %i"
122
127
                            u" retries, exiting.", rename_count)
123
128
            raise AvahiServiceError("Too many renames")
124
 
        name = server.GetAlternativeServiceName(name)
125
 
        logger.error(u"Changing name to %r ...", name)
 
129
        self.name = server.GetAlternativeServiceName(self.name)
 
130
        logger.info(u"Changing name to %r ...", str(self.name))
126
131
        syslogger.setFormatter(logging.Formatter\
127
132
                               ('Mandos (%s): %%(levelname)s:'
128
 
                               ' %%(message)s' % name))
 
133
                               ' %%(message)s' % self.name))
129
134
        self.remove()
130
135
        self.add()
131
136
        self.rename_count += 1
332
337
            try:
333
338
                logger.info(u"Starting checker %r for %s",
334
339
                            command, self.name)
 
340
                # We don't need to redirect stdout and stderr, since
 
341
                # in normal mode, that is already done by daemon(),
 
342
                # and in debug mode we don't want to.  (Stdin is
 
343
                # always replaced by /dev/null.)
335
344
                self.checker = subprocess.Popen(command,
336
345
                                                close_fds=True,
337
346
                                                shell=True, cwd="/")
338
347
                self.checker_callback_tag = gobject.child_watch_add\
339
348
                                            (self.checker.pid,
340
349
                                             self.checker_callback)
341
 
            except subprocess.OSError, error:
 
350
            except OSError, error:
342
351
                logger.error(u"Failed to start subprocess: %s",
343
352
                             error)
344
353
        # Re-run this periodically if run by gobject.timeout_add
401
410
    gnutls.library.functions.gnutls_openpgp_crt_import\
402
411
                    (crt, ctypes.byref(datum),
403
412
                     gnutls.library.constants.GNUTLS_OPENPGP_FMT_RAW)
 
413
    # Verify the self signature in the key
 
414
    crtverify = ctypes.c_uint();
 
415
    gnutls.library.functions.gnutls_openpgp_crt_verify_self\
 
416
        (crt, ctypes.c_uint(0), ctypes.byref(crtverify))
 
417
    if crtverify.value != 0:
 
418
        gnutls.library.functions.gnutls_openpgp_crt_deinit(crt)
 
419
        raise gnutls.errors.CertificateSecurityError("Verify failed")
404
420
    # New buffer for the fingerprint
405
421
    buffer = ctypes.create_string_buffer(20)
406
422
    buffer_length = ctypes.c_size_t()
558
574
    datetime.timedelta(1)
559
575
    >>> string_to_delta(u'1w')
560
576
    datetime.timedelta(7)
 
577
    >>> string_to_delta('5m 30s')
 
578
    datetime.timedelta(0, 330)
561
579
    """
562
 
    try:
563
 
        suffix=unicode(interval[-1])
564
 
        value=int(interval[:-1])
565
 
        if suffix == u"d":
566
 
            delta = datetime.timedelta(value)
567
 
        elif suffix == u"s":
568
 
            delta = datetime.timedelta(0, value)
569
 
        elif suffix == u"m":
570
 
            delta = datetime.timedelta(0, 0, 0, 0, value)
571
 
        elif suffix == u"h":
572
 
            delta = datetime.timedelta(0, 0, 0, 0, 0, value)
573
 
        elif suffix == u"w":
574
 
            delta = datetime.timedelta(0, 0, 0, 0, 0, 0, value)
575
 
        else:
 
580
    timevalue = datetime.timedelta(0)
 
581
    for s in interval.split():
 
582
        try:
 
583
            suffix=unicode(s[-1])
 
584
            value=int(s[:-1])
 
585
            if suffix == u"d":
 
586
                delta = datetime.timedelta(value)
 
587
            elif suffix == u"s":
 
588
                delta = datetime.timedelta(0, value)
 
589
            elif suffix == u"m":
 
590
                delta = datetime.timedelta(0, 0, 0, 0, value)
 
591
            elif suffix == u"h":
 
592
                delta = datetime.timedelta(0, 0, 0, 0, 0, value)
 
593
            elif suffix == u"w":
 
594
                delta = datetime.timedelta(0, 0, 0, 0, 0, 0, value)
 
595
            else:
 
596
                raise ValueError
 
597
        except (ValueError, IndexError):
576
598
            raise ValueError
577
 
    except (ValueError, IndexError):
578
 
        raise ValueError
579
 
    return delta
 
599
        timevalue += delta
 
600
    return timevalue
580
601
 
581
602
 
582
603
def server_state_changed(state):
695
716
    server_config = ConfigParser.SafeConfigParser(server_defaults)
696
717
    del server_defaults
697
718
    server_config.read(os.path.join(options.configdir, "mandos.conf"))
698
 
    server_section = "server"
699
719
    # Convert the SafeConfigParser object to a dict
700
 
    server_settings = dict(server_config.items(server_section))
 
720
    server_settings = server_config.defaults()
701
721
    # Use getboolean on the boolean config option
702
722
    server_settings["debug"] = server_config.getboolean\
703
 
                               (server_section, "debug")
 
723
                               ("DEFAULT", "debug")
704
724
    del server_config
705
725
    
706
726
    # Override the settings from the config file with command line
717
737
    
718
738
    if not debug:
719
739
        syslogger.setLevel(logging.WARNING)
 
740
        console.setLevel(logging.WARNING)
720
741
    
721
742
    if server_settings["servicename"] != "Mandos":
722
743
        syslogger.setFormatter(logging.Formatter\
727
748
    # Parse config file with clients
728
749
    client_defaults = { "timeout": "1h",
729
750
                        "interval": "5m",
730
 
                        "checker": "fping -q -- %%(host)s",
 
751
                        "checker": "fping -q -- %(host)s",
 
752
                        "host": "",
731
753
                        }
732
754
    client_config = ConfigParser.SafeConfigParser(client_defaults)
733
755
    client_config.read(os.path.join(server_settings["configdir"],
751
773
            avahi.DBUS_INTERFACE_SERVER )
752
774
    # End of Avahi example code
753
775
    
754
 
    if debug:
755
 
        console = logging.StreamHandler()
756
 
        # console.setLevel(logging.DEBUG)
757
 
        console.setFormatter(logging.Formatter\
758
 
                             ('%(levelname)s: %(message)s'))
759
 
        logger.addHandler(console)
760
 
        del console
761
 
    
762
776
    clients = Set()
763
777
    def remove_from_clients(client):
764
778
        clients.remove(client)
775
789
        logger.critical(u"No clients defined")
776
790
        sys.exit(1)
777
791
    
778
 
    if not debug:
 
792
    if debug:
 
793
        # Redirect stdin so all checkers get /dev/null
 
794
        null = os.open(os.path.devnull, os.O_NOCTTY | os.O_RDWR)
 
795
        os.dup2(null, sys.stdin.fileno())
 
796
        if null > 2:
 
797
            os.close(null)
 
798
    else:
 
799
        # No console logging
 
800
        logger.removeHandler(console)
 
801
        # Close all input and output, do double fork, etc.
779
802
        daemon()
780
803
    
781
804
    pidfilename = "/var/run/mandos/mandos.pid"