71
71
('Mandos: %(levelname)s: %(message)s'))
72
72
logger.addHandler(syslogger)
74
console = logging.StreamHandler()
75
console.setFormatter(logging.Formatter('%(name)s: %(levelname)s:'
77
logger.addHandler(console)
75
79
class AvahiError(Exception):
76
80
def __init__(self, value):
115
119
self.domain = domain
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))
131
136
self.rename_count += 1
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,
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",
344
353
# Re-run this periodically if run by gobject.timeout_add
558
567
datetime.timedelta(1)
559
568
>>> string_to_delta(u'1w')
560
569
datetime.timedelta(7)
570
>>> string_to_delta('5m 30s')
571
datetime.timedelta(0, 330)
563
suffix=unicode(interval[-1])
564
value=int(interval[:-1])
566
delta = datetime.timedelta(value)
568
delta = datetime.timedelta(0, value)
570
delta = datetime.timedelta(0, 0, 0, 0, value)
572
delta = datetime.timedelta(0, 0, 0, 0, 0, value)
574
delta = datetime.timedelta(0, 0, 0, 0, 0, 0, value)
573
timevalue = datetime.timedelta(0)
574
for s in interval.split():
576
suffix=unicode(s[-1])
579
delta = datetime.timedelta(value)
581
delta = datetime.timedelta(0, value)
583
delta = datetime.timedelta(0, 0, 0, 0, value)
585
delta = datetime.timedelta(0, 0, 0, 0, 0, value)
587
delta = datetime.timedelta(0, 0, 0, 0, 0, 0, value)
590
except (ValueError, IndexError):
577
except (ValueError, IndexError):
582
596
def server_state_changed(state):
654
668
global main_loop_started
655
669
main_loop_started = False
657
parser = OptionParser(version = "Mandos server %s" % version)
671
parser = OptionParser(version = "%%prog %s" % version)
658
672
parser.add_option("-i", "--interface", type="string",
659
673
metavar="IF", help="Bind to interface IF")
660
674
parser.add_option("-a", "--address", type="string",
695
709
server_config = ConfigParser.SafeConfigParser(server_defaults)
696
710
del server_defaults
697
711
server_config.read(os.path.join(options.configdir, "mandos.conf"))
698
server_section = "server"
699
712
# Convert the SafeConfigParser object to a dict
700
server_settings = dict(server_config.items(server_section))
713
server_settings = server_config.defaults()
701
714
# Use getboolean on the boolean config option
702
715
server_settings["debug"] = server_config.getboolean\
703
(server_section, "debug")
704
717
del server_config
706
719
# Override the settings from the config file with command line
727
741
# Parse config file with clients
728
742
client_defaults = { "timeout": "1h",
729
743
"interval": "5m",
730
"checker": "fping -q -- %%(host)s",
744
"checker": "fping -q -- %(host)s",
732
747
client_config = ConfigParser.SafeConfigParser(client_defaults)
733
748
client_config.read(os.path.join(server_settings["configdir"],
751
766
avahi.DBUS_INTERFACE_SERVER )
752
767
# End of Avahi example code
755
console = logging.StreamHandler()
756
# console.setLevel(logging.DEBUG)
757
console.setFormatter(logging.Formatter\
758
('%(levelname)s: %(message)s'))
759
logger.addHandler(console)
763
770
def remove_from_clients(client):
764
771
clients.remove(client)
775
782
logger.critical(u"No clients defined")
786
# Redirect stdin so all checkers get /dev/null
787
null = os.open(os.path.devnull, os.O_NOCTTY | os.O_RDWR)
788
os.dup2(null, sys.stdin.fileno())
793
logger.removeHandler(console)
794
# Close all input and output, do double fork, etc.
781
797
pidfilename = "/var/run/mandos/mandos.pid"