4
4
# Mandos Monitor - Control and monitor the Mandos server
6
# Copyright © 2008-2015 Teddy Hogeborn
7
# Copyright © 2008-2015 Björn Påhlsson
6
# Copyright © 2008-2016 Teddy Hogeborn
7
# Copyright © 2008-2016 Björn Påhlsson
9
9
# This program is free software: you can redistribute it and/or modify
10
10
# it under the terms of the GNU General Public License as published by
64
65
"ApprovalDelay": "Approval Delay",
65
66
"ApprovalDuration": "Approval Duration",
66
67
"Checker": "Checker",
67
"ExtendedTimeout": "Extended Timeout"
68
"ExtendedTimeout": "Extended Timeout",
70
"LastCheckerStatus": "Last Checker Status",
69
72
defaultkeywords = ("Name", "Enabled", "Timeout", "LastCheckedOK")
70
73
domain = "se.recompile"
73
76
server_interface = domain + ".Mandos"
74
77
client_interface = domain + ".Mandos.Client"
82
dbus.OBJECT_MANAGER_IFACE
83
except AttributeError:
84
dbus.OBJECT_MANAGER_IFACE = "org.freedesktop.DBus.ObjectManager"
78
86
def milliseconds_to_string(ms):
79
87
td = datetime.timedelta(0, 0, 0, ms)
275
283
help="Select all clients")
276
284
parser.add_argument("-v", "--verbose", action="store_true",
277
285
help="Print all fields")
286
parser.add_argument("-j", "--dump-json", action="store_true",
287
help="Dump client data in JSON format")
278
288
parser.add_argument("-e", "--enable", action="store_true",
279
289
help="Enable client")
280
290
parser.add_argument("-d", "--disable", action="store_true",
323
333
if has_actions(options) and not (options.client or options.all):
324
334
parser.error("Options require clients names or --all.")
325
335
if options.verbose and has_actions(options):
326
parser.error("--verbose can only be used alone or with"
336
parser.error("--verbose can only be used alone.")
337
if options.dump_json and (options.verbose or has_actions(options)):
338
parser.error("--dump-json can only be used alone.")
328
339
if options.all and not has_actions(options):
329
340
parser.error("--all requires an action.")
331
342
if options.check:
332
343
fail_count, test_count = doctest.testmod()
333
344
sys.exit(os.EX_OK if fail_count == 0 else 1)
342
353
mandos_serv = dbus.Interface(mandos_dbus_objc,
343
354
dbus_interface = server_interface)
355
mandos_serv_object_manager = dbus.Interface(
356
mandos_dbus_objc, dbus_interface = dbus.OBJECT_MANAGER_IFACE)
345
358
#block stderr since dbus library prints to stderr
346
359
null = os.open(os.path.devnull, os.O_RDWR)
352
mandos_clients = mandos_serv.GetAllClientsWithProperties()
365
mandos_clients = { path: ifs_and_props[client_interface]
366
for path, ifs_and_props in
367
mandos_serv_object_manager
368
.GetManagedObjects().items()
369
if client_interface in ifs_and_props }
355
372
os.dup2(stderrcopy, sys.stderr.fileno())
356
373
os.close(stderrcopy)
357
except dbus.exceptions.DBusException:
358
print("Access denied: Accessing mandos server through dbus.",
374
except dbus.exceptions.DBusException as e:
375
print("Access denied: Accessing mandos server through D-Bus: {}"
376
.format(e), file=sys.stderr)
362
379
# Compile dict of (clients: properties) to process
380
397
if not has_actions(options) and clients:
398
if options.verbose or options.dump_json:
382
399
keywords = ("Name", "Enabled", "Timeout", "LastCheckedOK",
383
400
"Created", "Interval", "Host", "Fingerprint",
384
401
"CheckerRunning", "LastEnabled",
385
402
"ApprovalPending", "ApprovedByDefault",
386
403
"LastApprovalRequest", "ApprovalDelay",
387
404
"ApprovalDuration", "Checker",
405
"ExtendedTimeout", "Expires",
390
408
keywords = defaultkeywords
392
print_clients(clients.values(), keywords)
410
if options.dump_json:
411
json.dump({client["Name"]: {key:
413
if isinstance(client[key],
416
for key in keywords }
417
for client in clients.values() },
418
fp = sys.stdout, indent = 4,
419
separators = (',', ': '))
422
print_clients(clients.values(), keywords)
394
424
# Process each client in the list by all selected options
395
425
for client in clients: