=== modified file 'mandos-ctl' --- mandos-ctl 2014-10-05 20:08:58 +0000 +++ mandos-ctl 2015-04-02 21:56:15 +0000 @@ -3,8 +3,8 @@ # # Mandos Monitor - Control and monitor the Mandos server # -# Copyright © 2008-2014 Teddy Hogeborn -# Copyright © 2008-2014 Björn Påhlsson +# Copyright © 2008-2015 Teddy Hogeborn +# Copyright © 2008-2015 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 @@ -64,8 +64,8 @@ "ApprovalDelay": "Approval Delay", "ApprovalDuration": "Approval Duration", "Checker": "Checker", - "ExtendedTimeout" : "Extended Timeout" - } + "ExtendedTimeout": "Extended Timeout" +} defaultkeywords = ("Name", "Enabled", "Timeout", "LastCheckedOK") domain = "se.recompile" busname = domain + ".Mandos" @@ -74,14 +74,14 @@ client_interface = domain + ".Mandos.Client" version = "1.6.9" + def milliseconds_to_string(ms): td = datetime.timedelta(0, 0, 0, ms) - return ("{days}{hours:02}:{minutes:02}:{seconds:02}" - .format(days = "{}T".format(td.days) if td.days else "", - hours = td.seconds // 3600, - minutes = (td.seconds % 3600) // 60, - seconds = td.seconds % 60, - )) + return ("{days}{hours:02}:{minutes:02}:{seconds:02}".format( + days = "{}T".format(td.days) if td.days else "", + hours = td.seconds // 3600, + minutes = (td.seconds % 3600) // 60, + seconds = td.seconds % 60)) def rfc3339_duration_to_delta(duration): @@ -125,7 +125,7 @@ token_end = Token(re.compile(r"$"), None, frozenset()) token_second = Token(re.compile(r"(\d+)S"), datetime.timedelta(seconds=1), - frozenset((token_end,))) + frozenset((token_end, ))) token_minute = Token(re.compile(r"(\d+)M"), datetime.timedelta(minutes=1), frozenset((token_second, token_end))) @@ -147,7 +147,7 @@ frozenset((token_month, token_end))) token_week = Token(re.compile(r"(\d+)W"), datetime.timedelta(weeks=1), - frozenset((token_end,))) + frozenset((token_end, ))) token_duration = Token(re.compile(r"P"), None, frozenset((token_year, token_month, token_day, token_time, @@ -155,7 +155,7 @@ # Define starting values value = datetime.timedelta() # Value so far found_token = None - followers = frozenset((token_duration,)) # Following valid tokens + followers = frozenset((token_duration, )) # Following valid tokens s = duration # String left to parse # Loop until end token is found while found_token is not token_end: @@ -223,6 +223,7 @@ value += datetime.timedelta(0, 0, 0, int(num)) return value + def print_clients(clients, keywords): def valuetostring(value, keyword): if type(value) is dbus.Boolean: @@ -234,19 +235,18 @@ # Create format string to print table rows format_string = " ".join("{{{key}:{width}}}".format( - width = max(len(tablewords[key]), - max(len(valuetostring(client[key], - key)) - for client in - clients)), - key = key) for key in keywords) + width = max(len(tablewords[key]), + max(len(valuetostring(client[key], key)) + for client in clients)), + key = key) + for key in keywords) # Print header line print(format_string.format(**tablewords)) for client in clients: - print(format_string.format(**{ key: - valuetostring(client[key], - key) - for key in keywords })) + print(format_string.format(**{ + key: valuetostring(client[key], key) + for key in keywords })) + def has_actions(options): return any((options.enable, @@ -268,6 +268,7 @@ options.approve, options.deny)) + def main(): parser = argparse.ArgumentParser() parser.add_argument("--version", action="version", @@ -338,8 +339,7 @@ bus = dbus.SystemBus() mandos_dbus_objc = bus.get_object(busname, server_path) except dbus.exceptions.DBusException: - print("Could not connect to Mandos server", - file=sys.stderr) + print("Could not connect to Mandos server", file=sys.stderr) sys.exit(1) mandos_serv = dbus.Interface(mandos_dbus_objc, @@ -382,11 +382,10 @@ if not has_actions(options) and clients: if options.verbose: - keywords = ("Name", "Enabled", "Timeout", - "LastCheckedOK", "Created", "Interval", - "Host", "Fingerprint", "CheckerRunning", - "LastEnabled", "ApprovalPending", - "ApprovedByDefault", + keywords = ("Name", "Enabled", "Timeout", "LastCheckedOK", + "Created", "Interval", "Host", "Fingerprint", + "CheckerRunning", "LastEnabled", + "ApprovalPending", "ApprovedByDefault", "LastApprovalRequest", "ApprovalDelay", "ApprovalDuration", "Checker", "ExtendedTimeout") @@ -397,16 +396,19 @@ else: # Process each client in the list by all selected options for client in clients: + def set_client_prop(prop, value): """Set a Client D-Bus property""" client.Set(client_interface, prop, value, dbus_interface=dbus.PROPERTIES_IFACE) + def set_client_prop_ms(prop, value): """Set a Client D-Bus property, converted from a string to milliseconds.""" set_client_prop(prop, string_to_delta(value).total_seconds() * 1000) + if options.remove: mandos_serv.RemoveClient(client.__dbus_object_path__) if options.enable: @@ -456,5 +458,6 @@ client.Approve(dbus.Boolean(False), dbus_interface=client_interface) + if __name__ == "__main__": main()