/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: 2010-03-27 18:50:53 UTC
  • mfrom: (24.1.146 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20100327185053-ht5xs10afawgx0sq
Merge from Björn.

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
from __future__ import division
4
5
import sys
5
6
import dbus
6
7
from optparse import OptionParser
7
8
import locale
 
9
import datetime
 
10
import re
8
11
 
9
12
locale.setlocale(locale.LC_ALL, u'')
10
13
 
21
24
    'last_enabled': u'Last Enabled',
22
25
    'checker': u'Checker',
23
26
    }
24
 
defaultkeywords = ('name', 'enabled', 'timeout', 'last_checked_ok',
25
 
                   'checker')
26
 
busname = 'org.mandos-system.Mandos'
27
 
server_path = '/Mandos'
28
 
server_interface = 'org.mandos_system.Mandos'
29
 
client_interface = 'org.mandos_system.Mandos.Client'
30
 
version = "1.0.2"
31
 
 
 
27
defaultkeywords = ('name', 'enabled', 'timeout', 'last_checked_ok')
 
28
domain = 'se.bsnet.fukt'
 
29
busname = domain + '.Mandos'
 
30
server_path = '/'
 
31
server_interface = domain + '.Mandos'
 
32
client_interface = domain + '.Mandos.Client'
 
33
version = "1.0.14"
32
34
bus = dbus.SystemBus()
33
35
mandos_dbus_objc = bus.get_object(busname, server_path)
34
36
mandos_serv = dbus.Interface(mandos_dbus_objc,
35
37
                             dbus_interface = server_interface)
36
38
mandos_clients = mandos_serv.GetAllClientsWithProperties()
37
39
 
38
 
def print_clients():
39
 
    def valuetostring(x):
40
 
        if type(x) is dbus.Boolean:
41
 
            return u"Yes" if x else u"No"
42
 
        else:
43
 
            return unicode(x)
 
40
def timedelta_to_milliseconds(td):
 
41
    "Convert a datetime.timedelta object to milliseconds"
 
42
    return ((td.days * 24 * 60 * 60 * 1000)
 
43
            + (td.seconds * 1000)
 
44
            + (td.microseconds // 1000))
 
45
 
 
46
def milliseconds_to_string(ms):
 
47
    td = datetime.timedelta(0, 0, 0, ms)
 
48
    return (u"%(days)s%(hours)02d:%(minutes)02d:%(seconds)02d"
 
49
            % { "days": "%dT" % td.days if td.days else "",
 
50
                "hours": td.seconds // 3600,
 
51
                "minutes": (td.seconds % 3600) // 60,
 
52
                "seconds": td.seconds % 60,
 
53
                })
 
54
 
 
55
 
 
56
def string_to_delta(interval):
 
57
    """Parse a string and return a datetime.timedelta
 
58
 
 
59
    >>> string_to_delta('7d')
 
60
    datetime.timedelta(7)
 
61
    >>> string_to_delta('60s')
 
62
    datetime.timedelta(0, 60)
 
63
    >>> string_to_delta('60m')
 
64
    datetime.timedelta(0, 3600)
 
65
    >>> string_to_delta('24h')
 
66
    datetime.timedelta(1)
 
67
    >>> string_to_delta(u'1w')
 
68
    datetime.timedelta(7)
 
69
    >>> string_to_delta('5m 30s')
 
70
    datetime.timedelta(0, 330)
 
71
    """
 
72
    timevalue = datetime.timedelta(0)
 
73
    regexp = re.compile("\d+[dsmhw]")
 
74
    
 
75
    for s in regexp.findall(interval):
 
76
        try:
 
77
            suffix = unicode(s[-1])
 
78
            value = int(s[:-1])
 
79
            if suffix == u"d":
 
80
                delta = datetime.timedelta(value)
 
81
            elif suffix == u"s":
 
82
                delta = datetime.timedelta(0, value)
 
83
            elif suffix == u"m":
 
84
                delta = datetime.timedelta(0, 0, 0, 0, value)
 
85
            elif suffix == u"h":
 
86
                delta = datetime.timedelta(0, 0, 0, 0, 0, value)
 
87
            elif suffix == u"w":
 
88
                delta = datetime.timedelta(0, 0, 0, 0, 0, 0, value)
 
89
            else:
 
90
                raise ValueError
 
91
        except (ValueError, IndexError):
 
92
            raise ValueError
 
93
        timevalue += delta
 
94
    return timevalue
 
95
 
 
96
def print_clients(clients):
 
97
    def valuetostring(value, keyword):
 
98
        if type(value) is dbus.Boolean:
 
99
            return u"Yes" if value else u"No"
 
100
        if keyword in (u"timeout", u"interval"):
 
101
            return milliseconds_to_string(value)
 
102
        return unicode(value)
 
103
    
 
104
    # Create format string to print table rows
44
105
    format_string = u' '.join(u'%%-%ds' %
45
106
                              max(len(tablewords[key]),
46
 
                                  max(len(valuetostring(client[key]))
 
107
                                  max(len(valuetostring(client[key], key))
47
108
                                      for client in
48
 
                                      mandos_clients.itervalues()))
 
109
                                      clients))
49
110
                              for key in keywords)
50
 
    print format_string % tuple(tablewords[key] for key in keywords) 
51
 
    for client in mandos_clients.itervalues():
52
 
        print format_string % tuple(valuetostring(client[key])
 
111
    # Print header line
 
112
    print format_string % tuple(tablewords[key] for key in keywords)
 
113
    for client in clients:
 
114
        print format_string % tuple(valuetostring(client[key], key)
53
115
                                    for key in keywords)
54
116
 
55
117
parser = OptionParser(version = "%%prog %s" % version)
56
118
parser.add_option("-a", "--all", action="store_true",
57
119
                  help="Print all fields")
58
120
parser.add_option("-e", "--enable", action="store_true",
59
 
                  help="Enable specified client")
 
121
                  help="Enable client")
60
122
parser.add_option("-d", "--disable", action="store_true",
61
 
                  help="disable specified client")
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")
 
123
                  help="disable client")
 
124
parser.add_option("-b", "--bump-timeout", action="store_true",
 
125
                  help="Bump timeout for client")
 
126
parser.add_option("--start-checker", action="store_true",
 
127
                  help="Start checker for client")
 
128
parser.add_option("--stop-checker", action="store_true",
 
129
                  help="Stop checker for client")
 
130
parser.add_option("-V", "--is-enabled", action="store_true",
 
131
                  help="Check if client is enabled")
 
132
parser.add_option("-r", "--remove", action="store_true",
 
133
                  help="Remove client")
 
134
parser.add_option("-c", "--checker", type="string",
 
135
                  help="Set checker command for client")
 
136
parser.add_option("-t", "--timeout", type="string",
 
137
                  help="Set timeout for client")
 
138
parser.add_option("-i", "--interval", type="string",
 
139
                  help="Set checker interval for client")
 
140
parser.add_option("-H", "--host", type="string",
 
141
                  help="Set host for client")
 
142
parser.add_option("-s", "--secret", type="string",
 
143
                  help="Set password blob (file) for client")
68
144
options, client_names = parser.parse_args()
69
145
 
 
146
# Compile list of clients to process
70
147
clients=[]
71
148
for name in client_names:
72
149
    for path, client in mandos_clients.iteritems():
73
150
        if client['name'] == name:
74
151
            client_objc = bus.get_object(busname, path)
75
 
            clients.append(dbus.Interface(client_objc,
76
 
                                          dbus_interface
77
 
                                          = client_interface))
 
152
            clients.append(client_objc)
78
153
            break
79
154
    else:
80
155
        print >> sys.stderr, "Client not found on server: %r" % name
81
156
        sys.exit(1)
82
157
 
83
 
if not clients:
 
158
if not clients and mandos_clients.values():
84
159
    keywords = defaultkeywords
85
160
    if options.all:
86
161
        keywords = ('name', 'enabled', 'timeout', 'last_checked_ok',
87
162
                    'created', 'interval', 'host', 'fingerprint',
88
163
                    'checker_running', 'last_enabled', 'checker')
89
 
    print_clients()
 
164
    print_clients(mandos_clients.values())
90
165
 
 
166
# Process each client in the list by all selected options
91
167
for client in clients:
 
168
    if options.remove:
 
169
        mandos_serv.RemoveClient(client.__dbus_object_path__)
92
170
    if options.enable:
93
 
        client.Enable()
 
171
        client.Enable(dbus_interface=client_interface)
94
172
    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
 
 
 
173
        client.Disable(dbus_interface=client_interface)
 
174
    if options.bump_timeout:
 
175
        client.CheckedOK(dbus_interface=client_interface)
 
176
    if options.start_checker:
 
177
        client.StartChecker(dbus_interface=client_interface)
 
178
    if options.stop_checker:
 
179
        client.StopChecker(dbus_interface=client_interface)
 
180
    if options.is_enabled:
 
181
        sys.exit(0 if client.Get(client_interface,
 
182
                                 u"enabled",
 
183
                                 dbus_interface=dbus.PROPERTIES_IFACE)
 
184
                 else 1)
 
185
    if options.checker:
 
186
        client.Set(client_interface, u"checker", options.checker,
 
187
                   dbus_interface=dbus.PROPERTIES_IFACE)
 
188
    if options.host:
 
189
        client.Set(client_interface, u"host", options.host,
 
190
                   dbus_interface=dbus.PROPERTIES_IFACE)
 
191
    if options.interval:
 
192
        client.Set(client_interface, u"interval",
 
193
                   timedelta_to_milliseconds
 
194
                   (string_to_delta(options.interval)),
 
195
                   dbus_interface=dbus.PROPERTIES_IFACE)
 
196
    if options.timeout:
 
197
        client.Set(client_interface, u"timeout",
 
198
                   timedelta_to_milliseconds(string_to_delta
 
199
                                             (options.timeout)),
 
200
                   dbus_interface=dbus.PROPERTIES_IFACE)
 
201
    if options.secret:
 
202
        client.Set(client_interface, u"secret",
 
203
                   dbus.ByteArray(open(options.secret, u'rb').read()),
 
204
                   dbus_interface=dbus.PROPERTIES_IFACE)