2
 
# -*- mode: python; coding: utf-8 -*-
 
4
 
from __future__ import division
 
7
 
from optparse import OptionParser
 
12
 
locale.setlocale(locale.LC_ALL, u'')
 
16
 
    'enabled': u'Enabled',
 
17
 
    'timeout': u'Timeout',
 
18
 
    'last_checked_ok': u'Last Successful Check',
 
19
 
    'created': u'Created',
 
20
 
    'interval': u'Interval',
 
22
 
    'fingerprint': u'Fingerprint',
 
23
 
    'checker_running': u'Check Is Running',
 
24
 
    'last_enabled': u'Last Enabled',
 
25
 
    'checker': u'Checker',
 
27
 
defaultkeywords = ('name', 'enabled', 'timeout', 'last_checked_ok')
 
28
 
domain = 'se.bsnet.fukt'
 
29
 
busname = domain + '.Mandos'
 
31
 
server_interface = domain + '.Mandos'
 
32
 
client_interface = domain + '.Mandos.Client'
 
34
 
bus = dbus.SystemBus()
 
35
 
mandos_dbus_objc = bus.get_object(busname, server_path)
 
36
 
mandos_serv = dbus.Interface(mandos_dbus_objc,
 
37
 
                             dbus_interface = server_interface)
 
38
 
mandos_clients = mandos_serv.GetAllClientsWithProperties()
 
40
 
def timedelta_to_milliseconds(td):
 
41
 
    "Convert a datetime.timedelta object to milliseconds"
 
42
 
    return ((td.days * 24 * 60 * 60 * 1000)
 
44
 
            + (td.microseconds // 1000))
 
46
 
def milliseconds_to_string(ms):
 
47
 
    td = datetime.timedelta(0, 0, 0, ms)
 
48
 
    return (u"%(days)s%(hours)02d:%(minutes)02d:%(seconds)02d"
 
49
 
            % { "days": "%dT" % td.days if td.days else "",
 
50
 
                "hours": td.seconds // 3600,
 
51
 
                "minutes": (td.seconds % 3600) // 60,
 
52
 
                "seconds": td.seconds % 60,
 
56
 
def string_to_delta(interval):
 
57
 
    """Parse a string and return a datetime.timedelta
 
59
 
    >>> string_to_delta('7d')
 
61
 
    >>> string_to_delta('60s')
 
62
 
    datetime.timedelta(0, 60)
 
63
 
    >>> string_to_delta('60m')
 
64
 
    datetime.timedelta(0, 3600)
 
65
 
    >>> string_to_delta('24h')
 
67
 
    >>> string_to_delta(u'1w')
 
69
 
    >>> string_to_delta('5m 30s')
 
70
 
    datetime.timedelta(0, 330)
 
72
 
    timevalue = datetime.timedelta(0)
 
73
 
    regexp = re.compile("\d+[dsmhw]")
 
75
 
    for s in regexp.findall(interval):
 
77
 
            suffix = unicode(s[-1])
 
80
 
                delta = datetime.timedelta(value)
 
82
 
                delta = datetime.timedelta(0, value)
 
84
 
                delta = datetime.timedelta(0, 0, 0, 0, value)
 
86
 
                delta = datetime.timedelta(0, 0, 0, 0, 0, value)
 
88
 
                delta = datetime.timedelta(0, 0, 0, 0, 0, 0, value)
 
91
 
        except (ValueError, IndexError):
 
96
 
def print_clients(clients):
 
97
 
    def valuetostring(value, keyword):
 
98
 
        if type(value) is dbus.Boolean:
 
99
 
            return u"Yes" if value else u"No"
 
100
 
        if keyword in (u"timeout", u"interval"):
 
101
 
            return milliseconds_to_string(value)
 
102
 
        return unicode(value)
 
104
 
    # Create format string to print table rows
 
105
 
    format_string = u' '.join(u'%%-%ds' %
 
106
 
                              max(len(tablewords[key]),
 
107
 
                                  max(len(valuetostring(client[key], key))
 
112
 
    print format_string % tuple(tablewords[key] for key in keywords)
 
113
 
    for client in clients:
 
114
 
        print format_string % tuple(valuetostring(client[key], key)
 
117
 
parser = OptionParser(version = "%%prog %s" % version)
 
118
 
parser.add_option("-a", "--all", action="store_true",
 
119
 
                  help="Print all fields")
 
120
 
parser.add_option("-e", "--enable", action="store_true",
 
121
 
                  help="Enable client")
 
122
 
parser.add_option("-d", "--disable", action="store_true",
 
123
 
                  help="disable client")
 
124
 
parser.add_option("-b", "--bump-timeout", action="store_true",
 
125
 
                  help="Bump timeout for client")
 
126
 
parser.add_option("--start-checker", action="store_true",
 
127
 
                  help="Start checker for client")
 
128
 
parser.add_option("--stop-checker", action="store_true",
 
129
 
                  help="Stop checker for client")
 
130
 
parser.add_option("-V", "--is-enabled", action="store_true",
 
131
 
                  help="Check if client is enabled")
 
132
 
parser.add_option("-r", "--remove", action="store_true",
 
133
 
                  help="Remove client")
 
134
 
parser.add_option("-c", "--checker", type="string",
 
135
 
                  help="Set checker command for client")
 
136
 
parser.add_option("-t", "--timeout", type="string",
 
137
 
                  help="Set timeout for client")
 
138
 
parser.add_option("-i", "--interval", type="string",
 
139
 
                  help="Set checker interval for client")
 
140
 
parser.add_option("-H", "--host", type="string",
 
141
 
                  help="Set host for client")
 
142
 
parser.add_option("-s", "--secret", type="string",
 
143
 
                  help="Set password blob (file) for client")
 
144
 
options, client_names = parser.parse_args()
 
146
 
# Compile list of clients to process
 
148
 
for name in client_names:
 
149
 
    for path, client in mandos_clients.iteritems():
 
150
 
        if client['name'] == name:
 
151
 
            client_objc = bus.get_object(busname, path)
 
152
 
            clients.append(client_objc)
 
155
 
        print >> sys.stderr, "Client not found on server: %r" % name
 
158
 
if not clients and mandos_clients.values():
 
159
 
    keywords = defaultkeywords
 
161
 
        keywords = ('name', 'enabled', 'timeout', 'last_checked_ok',
 
162
 
                    'created', 'interval', 'host', 'fingerprint',
 
163
 
                    'checker_running', 'last_enabled', 'checker')
 
164
 
    print_clients(mandos_clients.values())
 
166
 
# Process each client in the list by all selected options
 
167
 
for client in clients:
 
169
 
        mandos_serv.RemoveClient(client.__dbus_object_path__)
 
171
 
        client.Enable(dbus_interface=client_interface)
 
173
 
        client.Disable(dbus_interface=client_interface)
 
174
 
    if options.bump_timeout:
 
175
 
        client.CheckedOK(dbus_interface=client_interface)
 
176
 
    if options.start_checker:
 
177
 
        client.StartChecker(dbus_interface=client_interface)
 
178
 
    if options.stop_checker:
 
179
 
        client.StopChecker(dbus_interface=client_interface)
 
180
 
    if options.is_enabled:
 
181
 
        sys.exit(0 if client.Get(client_interface,
 
183
 
                                 dbus_interface=dbus.PROPERTIES_IFACE)
 
186
 
        client.Set(client_interface, u"checker", options.checker,
 
187
 
                   dbus_interface=dbus.PROPERTIES_IFACE)
 
189
 
        client.Set(client_interface, u"host", options.host,
 
190
 
                   dbus_interface=dbus.PROPERTIES_IFACE)
 
192
 
        client.Set(client_interface, u"interval",
 
193
 
                   timedelta_to_milliseconds
 
194
 
                   (string_to_delta(options.interval)),
 
195
 
                   dbus_interface=dbus.PROPERTIES_IFACE)
 
197
 
        client.Set(client_interface, u"timeout",
 
198
 
                   timedelta_to_milliseconds(string_to_delta
 
200
 
                   dbus_interface=dbus.PROPERTIES_IFACE)
 
202
 
        client.Set(client_interface, u"secret",
 
203
 
                   dbus.ByteArray(open(options.secret, u'rb').read()),
 
204
 
                   dbus_interface=dbus.PROPERTIES_IFACE)