/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to mandos-ctl

  • Committer: Teddy Hogeborn
  • Date: 2015-07-06 20:14:45 UTC
  • Revision ID: teddy@recompile.se-20150706201445-kq3o6qozigmiqsp9
plugin-runner.xml (EXAMPLE): Use the /usr/lib/<arch> directory.
                             Also explain used options in order.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
4
4
# Mandos Monitor - Control and monitor the Mandos server
5
5
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
8
8
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
39
39
import os
40
40
import collections
41
41
import doctest
42
 
import json
43
42
 
44
43
import dbus
45
44
 
73
72
server_path = "/"
74
73
server_interface = domain + ".Mandos"
75
74
client_interface = domain + ".Mandos.Client"
76
 
version = "1.7.10"
77
 
 
78
 
 
79
 
try:
80
 
    dbus.OBJECT_MANAGER_IFACE
81
 
except AttributeError:
82
 
    dbus.OBJECT_MANAGER_IFACE = "org.freedesktop.DBus.ObjectManager"
 
75
version = "1.6.9"
 
76
 
83
77
 
84
78
def milliseconds_to_string(ms):
85
79
    td = datetime.timedelta(0, 0, 0, ms)
281
275
                        help="Select all clients")
282
276
    parser.add_argument("-v", "--verbose", action="store_true",
283
277
                        help="Print all fields")
284
 
    parser.add_argument("-j", "--dump-json", action="store_true",
285
 
                        help="Dump client data in JSON format")
286
278
    parser.add_argument("-e", "--enable", action="store_true",
287
279
                        help="Enable client")
288
280
    parser.add_argument("-d", "--disable", action="store_true",
331
323
    if has_actions(options) and not (options.client or options.all):
332
324
        parser.error("Options require clients names or --all.")
333
325
    if options.verbose and has_actions(options):
334
 
        parser.error("--verbose can only be used alone.")
335
 
    if options.dump_json and (options.verbose or has_actions(options)):
336
 
        parser.error("--dump-json can only be used alone.")
 
326
        parser.error("--verbose can only be used alone or with"
 
327
                     " --all.")
337
328
    if options.all and not has_actions(options):
338
329
        parser.error("--all requires an action.")
339
 
    
 
330
 
340
331
    if options.check:
341
332
        fail_count, test_count = doctest.testmod()
342
333
        sys.exit(os.EX_OK if fail_count == 0 else 1)
350
341
    
351
342
    mandos_serv = dbus.Interface(mandos_dbus_objc,
352
343
                                 dbus_interface = server_interface)
353
 
    mandos_serv_object_manager = dbus.Interface(
354
 
        mandos_dbus_objc, dbus_interface = dbus.OBJECT_MANAGER_IFACE)
355
344
    
356
345
    #block stderr since dbus library prints to stderr
357
346
    null = os.open(os.path.devnull, os.O_RDWR)
360
349
    os.close(null)
361
350
    try:
362
351
        try:
363
 
            mandos_clients = { path: ifs_and_props[client_interface]
364
 
                               for path, ifs_and_props in
365
 
                               mandos_serv_object_manager
366
 
                               .GetManagedObjects().items()
367
 
                               if client_interface in ifs_and_props }
 
352
            mandos_clients = mandos_serv.GetAllClientsWithProperties()
368
353
        finally:
369
354
            #restore stderr
370
355
            os.dup2(stderrcopy, sys.stderr.fileno())
371
356
            os.close(stderrcopy)
372
 
    except dbus.exceptions.DBusException as e:
373
 
        print("Access denied: Accessing mandos server through D-Bus: {}"
374
 
              .format(e), file=sys.stderr)
 
357
    except dbus.exceptions.DBusException:
 
358
        print("Access denied: Accessing mandos server through dbus.",
 
359
              file=sys.stderr)
375
360
        sys.exit(1)
376
361
    
377
362
    # Compile dict of (clients: properties) to process
393
378
                sys.exit(1)
394
379
    
395
380
    if not has_actions(options) and clients:
396
 
        if options.verbose or options.dump_json:
 
381
        if options.verbose:
397
382
            keywords = ("Name", "Enabled", "Timeout", "LastCheckedOK",
398
383
                        "Created", "Interval", "Host", "Fingerprint",
399
384
                        "CheckerRunning", "LastEnabled",
404
389
        else:
405
390
            keywords = defaultkeywords
406
391
        
407
 
        if options.dump_json:
408
 
            json.dump({client["Name"]: {key: client[key]
409
 
                                        for key in keywords }
410
 
                       for client in clients.values() },
411
 
                      fp = sys.stdout, indent = 4,
412
 
                      separators = (',', ': '))
413
 
            print()
414
 
        else:
415
 
            print_clients(clients.values(), keywords)
 
392
        print_clients(clients.values(), keywords)
416
393
    else:
417
394
        # Process each client in the list by all selected options
418
395
        for client in clients: