/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-16 20:30:35 UTC
  • Revision ID: teddy@recompile.se-20150716203035-z7qjwm270o2vrjah
Assume the C11 language (ISO 9899:2011), when compiling C code.

* Makefile (LANGUAGE): Changed to "-std=gnu11".

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
 
65
64
    "ApprovalDelay": "Approval Delay",
66
65
    "ApprovalDuration": "Approval Duration",
67
66
    "Checker": "Checker",
68
 
    "ExtendedTimeout": "Extended Timeout",
69
 
    "Expires": "Expires",
70
 
    "LastCheckerStatus": "Last Checker Status",
 
67
    "ExtendedTimeout": "Extended Timeout"
71
68
}
72
69
defaultkeywords = ("Name", "Enabled", "Timeout", "LastCheckedOK")
73
70
domain = "se.recompile"
75
72
server_path = "/"
76
73
server_interface = domain + ".Mandos"
77
74
client_interface = domain + ".Mandos.Client"
78
 
version = "1.7.10"
79
 
 
80
 
 
81
 
try:
82
 
    dbus.OBJECT_MANAGER_IFACE
83
 
except AttributeError:
84
 
    dbus.OBJECT_MANAGER_IFACE = "org.freedesktop.DBus.ObjectManager"
 
75
version = "1.6.9"
 
76
 
85
77
 
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"
 
327
                     " --all.")
339
328
    if options.all and not has_actions(options):
340
329
        parser.error("--all requires an action.")
341
 
    
 
330
 
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)
352
341
    
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)
357
344
    
358
345
    #block stderr since dbus library prints to stderr
359
346
    null = os.open(os.path.devnull, os.O_RDWR)
362
349
    os.close(null)
363
350
    try:
364
351
        try:
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()
370
353
        finally:
371
354
            #restore stderr
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.",
 
359
              file=sys.stderr)
377
360
        sys.exit(1)
378
361
    
379
362
    # Compile dict of (clients: properties) to process
395
378
                sys.exit(1)
396
379
    
397
380
    if not has_actions(options) and clients:
398
 
        if options.verbose or options.dump_json:
 
381
        if options.verbose:
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",
406
 
                        "LastCheckerStatus")
 
388
                        "ExtendedTimeout")
407
389
        else:
408
390
            keywords = defaultkeywords
409
391
        
410
 
        if options.dump_json:
411
 
            json.dump({client["Name"]: {key:
412
 
                                        bool(client[key])
413
 
                                        if isinstance(client[key],
414
 
                                                      dbus.Boolean)
415
 
                                        else client[key]
416
 
                                        for key in keywords }
417
 
                       for client in clients.values() },
418
 
                      fp = sys.stdout, indent = 4,
419
 
                      separators = (',', ': '))
420
 
            print()
421
 
        else:
422
 
            print_clients(clients.values(), keywords)
 
392
        print_clients(clients.values(), keywords)
423
393
    else:
424
394
        # Process each client in the list by all selected options
425
395
        for client in clients: