=== modified file 'Makefile' --- Makefile 2009-01-10 03:26:15 +0000 +++ Makefile 2009-01-10 21:49:25 +0000 @@ -77,7 +77,7 @@ PLUGINS=plugins.d/password-prompt plugins.d/mandos-client \ plugins.d/usplash plugins.d/splashy plugins.d/askpass-fifo CPROGS=plugin-runner $(PLUGINS) -PROGS=mandos mandos-keygen mandos-list $(CPROGS) +PROGS=mandos mandos-keygen mandos-ctl $(CPROGS) DOCS=mandos.8 plugin-runner.8mandos mandos-keygen.8 \ plugins.d/mandos-client.8mandos \ plugins.d/password-prompt.8mandos mandos.conf.5 \ @@ -164,7 +164,7 @@ --expression='s/^\(VERSION="\)[^"]*"$$/\1$(version)"/' \ $@ -mandos-list: Makefile +mandos-ctl: Makefile $(SED) --in-place \ --expression='s/^\(version = "\)[^"]*"$$/\1$(version)"/' \ $@ === renamed file 'mandos-list' => 'mandos-ctl' --- mandos-list 2008-12-21 19:19:25 +0000 +++ mandos-ctl 2009-01-10 21:45:37 +0000 @@ -1,9 +1,13 @@ #!/usr/bin/python # -*- mode: python; coding: utf-8 -*- +from __future__ import division +import sys import dbus from optparse import OptionParser import locale +import datetime +import re locale.setlocale(locale.LC_ALL, u'') @@ -20,46 +24,164 @@ 'last_enabled': u'Last Enabled', 'checker': u'Checker', } +defaultkeywords = ('name', 'enabled', 'timeout', 'last_checked_ok', + 'checker') busname = 'org.mandos-system.Mandos' -object_path = '/Mandos' -interface = 'org.mandos_system.Mandos' +server_path = '/Mandos' +server_interface = 'org.mandos_system.Mandos' +client_interface = 'org.mandos_system.Mandos.Client' version = "1.0.2" -defaultkeywords = ('name', 'enabled', 'timeout', 'last_checked_ok', - 'checker') - -parser = OptionParser(version = "%%prog %s" % version) -parser.add_option("-a", "--all", action="store_true", default=False, - help="Print all fields") -options = parser.parse_args()[0] -if options.all: - keywords = ('name', 'enabled', 'timeout', 'last_checked_ok', - 'created', 'interval', 'host', 'fingerprint', - 'checker_running', 'last_enabled', 'checker') -else: - keywords = defaultkeywords - bus = dbus.SystemBus() -mandos_dbus_objc = bus.get_object(busname, object_path) +mandos_dbus_objc = bus.get_object(busname, server_path) mandos_serv = dbus.Interface(mandos_dbus_objc, - dbus_interface = interface) + dbus_interface = server_interface) mandos_clients = mandos_serv.GetAllClientsWithProperties() -def valuetostring(x): - if type(x) is dbus.Boolean: - return u"Yes" if x else u"No" +def datetime_to_milliseconds(dt): + "Return the 'timeout' attribute in milliseconds" + return ((dt.days * 24 * 60 * 60 * 1000) + + (dt.seconds * 1000) + + (dt.microseconds // 1000)) + +def milliseconds_to_string(ms): + td = datetime.timedelta(0, 0, 0, ms) + return "%s%02d:%02d:%02d" % (("%dT" % td.days) if td.days else "", # days + td.seconds // 3600, # hours + (td.seconds % 3600) // 60, # minutes + (td.seconds % 60)) # seconds + + +def string_to_delta(interval): + """Parse a string and return a datetime.timedelta + + >>> string_to_delta('7d') + datetime.timedelta(7) + >>> string_to_delta('60s') + datetime.timedelta(0, 60) + >>> string_to_delta('60m') + datetime.timedelta(0, 3600) + >>> string_to_delta('24h') + datetime.timedelta(1) + >>> string_to_delta(u'1w') + datetime.timedelta(7) + >>> string_to_delta('5m 30s') + datetime.timedelta(0, 330) + """ + timevalue = datetime.timedelta(0) + regexp = re.compile("\d+[dsmhw]") + + for s in regexp.findall(interval): + try: + suffix = unicode(s[-1]) + value = int(s[:-1]) + if suffix == u"d": + delta = datetime.timedelta(value) + elif suffix == u"s": + delta = datetime.timedelta(0, value) + elif suffix == u"m": + delta = datetime.timedelta(0, 0, 0, 0, value) + elif suffix == u"h": + delta = datetime.timedelta(0, 0, 0, 0, 0, value) + elif suffix == u"w": + delta = datetime.timedelta(0, 0, 0, 0, 0, 0, value) + else: + raise ValueError + except (ValueError, IndexError): + raise ValueError + timevalue += delta + return timevalue + +def print_clients(clients): + def valuetostring(value, keyword): + if type(value) is dbus.Boolean: + return u"Yes" if value else u"No" + if keyword in ("timeout", "interval"): + return milliseconds_to_string(value) + return unicode(value) + + format_string = u' '.join(u'%%-%ds' % + max(len(tablewords[key]), + max(len(valuetostring(client[key], key)) + for client in + clients)) + for key in keywords) + print format_string % tuple(tablewords[key] for key in keywords) + for client in clients: + print format_string % tuple(valuetostring(client[key], key) + for key in keywords) + +parser = OptionParser(version = "%%prog %s" % version) +parser.add_option("-a", "--all", action="store_true", + help="Print all fields") +parser.add_option("-e", "--enable", action="store_true", + help="Enable specified client") +parser.add_option("-d", "--disable", action="store_true", + help="disable specified client") +parser.add_option("-b", "--bump-timeout", action="store_true", + help="Bump timeout of specified client") +parser.add_option("--start-checker", action="store_true", + help="Start checker for specified client") +parser.add_option("--stop-checker", action="store_true", + help="Stop checker for specified client") +parser.add_option("-v", "--is-valid", action="store_true", + help="Stop checker for specified client") +parser.add_option("-c", "--checker", type="string", + help="Set checker command for specified client") +parser.add_option("-t", "--timeout", type="string", + help="Set timeout for specified client") +parser.add_option("-i", "--interval", type="string", + help="Set checker interval for specified client") +parser.add_option("-H", "--host", type="string", + help="Set host for specified client") +parser.add_option("-s", "--secret", type="string", + help="Set password blob (file) for specified client") +options, client_names = parser.parse_args() + +clients=[] +for name in client_names: + for path, client in mandos_clients.iteritems(): + if client['name'] == name: + client_objc = bus.get_object(busname, path) + clients.append(dbus.Interface(client_objc, + dbus_interface + = client_interface)) + break else: - return unicode(x) - -format_string = u' '.join(u'%%-%ds' - % max(len(tablewords[key]), - max(len(valuetostring(client[key])) - for client - in mandos_clients.itervalues())) - for key in keywords) -print format_string % tuple(tablewords[key] for key in keywords) -for client in mandos_clients.itervalues(): - print format_string % tuple(valuetostring(client[key]) - for key in keywords) - - + print >> sys.stderr, "Client not found on server: %r" % name + sys.exit(1) + +if not clients: + keywords = defaultkeywords + if options.all: + keywords = ('name', 'enabled', 'timeout', 'last_checked_ok', + 'created', 'interval', 'host', 'fingerprint', + 'checker_running', 'last_enabled', 'checker') + print_clients(mandos_clients.values()) + +for client in clients: + if options.enable: + client.Enable() + if options.disable: + client.Disable() + if options.bump_timeout: + client.BumpTimeout() + if options.start_checker: + client.StartChecker() + if options.stop_checker: + client.StopChecker() + if options.is_valid: + sys.exit(0 if client.IsStillValid() else 1) + if options.checker: + client.SetChecker(options.checker) + if options.host: + client.SetHost(options.host) + if options.interval: + client.SetInterval(datetime_to_milliseconds + (string_to_delta(options.interval))) + if options.timeout: + client.SetTimeout(datetime_to_milliseconds + (string_to_delta(options.timeout))) + if options.secret: + client.SetSecret(dbus.ByteArray(open(options.secret, 'rb').read())) +