/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

Added more method support for mandos clients through mandos-ctl

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/python
2
2
# -*- mode: python; coding: utf-8 -*-
3
3
 
 
4
import sys
4
5
import dbus
5
6
from optparse import OptionParser
6
7
import locale
20
21
    'last_enabled': u'Last Enabled',
21
22
    'checker': u'Checker',
22
23
    }
 
24
defaultkeywords = ('name', 'enabled', 'timeout', 'last_checked_ok',
 
25
                   'checker')
23
26
busname = 'org.mandos-system.Mandos'
24
27
server_path = '/Mandos'
25
28
server_interface = 'org.mandos_system.Mandos'
26
29
client_interface = 'org.mandos_system.Mandos.Client'
27
30
version = "1.0.2"
28
 
defaultkeywords = ('name', 'enabled', 'timeout', 'last_checked_ok',
29
 
                   'checker')
 
31
 
30
32
bus = dbus.SystemBus()
31
33
mandos_dbus_objc = bus.get_object(busname, server_path)
32
34
mandos_serv = dbus.Interface(mandos_dbus_objc,
33
35
                             dbus_interface = server_interface)
34
36
mandos_clients = mandos_serv.GetAllClientsWithProperties()
35
37
 
36
 
def Getclientbyname(name):
37
 
    client_path = None
38
 
    for cli in mandos_clients.iteritems():
39
 
        if cli[1]['name'] == name:
40
 
            client_path = cli[0]
41
 
    if client_path is None:
42
 
        print "Client not found on server"
43
 
        return
44
 
 
45
 
    client_objc = bus.get_object(busname, client_path)
46
 
    return dbus.Interface(client_objc, dbus_interface = client_interface)
47
 
 
48
 
    
49
 
def Enable(options):
50
 
    client = Getclientbyname(options.enable)
51
 
    if client is not None:
52
 
        client.Enable()
53
 
 
54
 
def Disable(options):
55
 
    client = Getclientbyname(options.disable)
56
 
    if client is not None:
57
 
        client.Disable()
58
 
 
59
 
def List(foo):
 
38
def print_clients():
60
39
    def valuetostring(x):
61
40
        if type(x) is dbus.Boolean:
62
41
            return u"Yes" if x else u"No"
73
52
        print format_string % tuple(valuetostring(client[key])
74
53
                                    for key in keywords)
75
54
 
76
 
action = List
77
55
parser = OptionParser(version = "%%prog %s" % version)
78
 
parser.add_option("-a", "--all", action="store_true", default=False,
 
56
parser.add_option("-a", "--all", action="store_true",
79
57
                  help="Print all fields")
80
 
parser.add_option("-e", "--enable", type="string",
 
58
parser.add_option("-e", "--enable", action="store_true",
81
59
                  help="Enable specified client")
82
 
parser.add_option("-d", "--disable", type="string",
 
60
parser.add_option("-d", "--disable", action="store_true",
83
61
                  help="disable specified client")
84
 
options = parser.parse_args()[0]
85
 
if options.enable:
86
 
    action = Enable
87
 
elif options.disable:
88
 
    action = Disable
89
 
elif options.all:
90
 
    keywords = ('name', 'enabled', 'timeout', 'last_checked_ok',
91
 
                'created', 'interval', 'host', 'fingerprint',
92
 
                'checker_running', 'last_enabled', 'checker')
93
 
else:
 
62
parser.add_option("-b", "--bumptimeout", action="store_true",
 
63
                  help="Bump timeout of specified client")
 
64
parser.add_option("-s", "--startchecker", action="store_true",
 
65
                  help="Start checker for specified client")
 
66
parser.add_option("-S", "--stopchecker", action="store_true",
 
67
                  help="Stop checker for specified client")
 
68
options, client_names = parser.parse_args()
 
69
 
 
70
clients=[]
 
71
for name in client_names:
 
72
    for path, client in mandos_clients.iteritems():
 
73
        if client['name'] == name:
 
74
            client_objc = bus.get_object(busname, path)
 
75
            clients.append(dbus.Interface(client_objc,
 
76
                                          dbus_interface
 
77
                                          = client_interface))
 
78
            break
 
79
    else:
 
80
        print >> sys.stderr, "Client not found on server: %r" % name
 
81
        sys.exit(1)
 
82
 
 
83
if not clients:
94
84
    keywords = defaultkeywords
95
 
 
96
 
action(options)
97
 
 
98
 
    
 
85
    if options.all:
 
86
        keywords = ('name', 'enabled', 'timeout', 'last_checked_ok',
 
87
                    'created', 'interval', 'host', 'fingerprint',
 
88
                    'checker_running', 'last_enabled', 'checker')
 
89
    print_clients()
 
90
 
 
91
for client in clients:
 
92
    if options.enable:
 
93
        client.Enable()
 
94
    if options.disable:
 
95
        client.Disable()
 
96
    if options.bumptimeout:
 
97
        client.BumpTimeout()
 
98
    if options.startchecker:
 
99
        client.StartChecker()
 
100
    if options.stopchecker:
 
101
        client.StopChecker()
 
102