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