bzr branch
http://bzr.recompile.se/loggerhead/mandos/trunk
24.1.116
by Björn Påhlsson
added a mandos list client program |
1 |
#!/usr/bin/python
|
240
by Teddy Hogeborn
Merge "mandos-list" from belorn. |
2 |
# -*- mode: python; coding: utf-8 -*-
|
24.1.116
by Björn Påhlsson
added a mandos list client program |
3 |
|
24.1.121
by Björn Påhlsson
mandos-ctl: Added support for all client calls |
4 |
from __future__ import division |
24.1.119
by Björn Påhlsson
Added more method support for mandos clients through mandos-ctl |
5 |
import sys |
24.1.116
by Björn Påhlsson
added a mandos list client program |
6 |
import dbus |
7 |
from optparse import OptionParser |
|
240
by Teddy Hogeborn
Merge "mandos-list" from belorn. |
8 |
import locale |
24.1.121
by Björn Påhlsson
mandos-ctl: Added support for all client calls |
9 |
import datetime |
10 |
import re |
|
240
by Teddy Hogeborn
Merge "mandos-list" from belorn. |
11 |
|
12 |
locale.setlocale(locale.LC_ALL, u'') |
|
24.1.116
by Björn Påhlsson
added a mandos list client program |
13 |
|
14 |
tablewords = { |
|
240
by Teddy Hogeborn
Merge "mandos-list" from belorn. |
15 |
'name': u'Name', |
16 |
'enabled': u'Enabled', |
|
17 |
'timeout': u'Timeout', |
|
18 |
'last_checked_ok': u'Last Successful Check', |
|
19 |
'created': u'Created', |
|
20 |
'interval': u'Interval', |
|
21 |
'host': u'Host', |
|
22 |
'fingerprint': u'Fingerprint', |
|
23 |
'checker_running': u'Check Is Running', |
|
24 |
'last_enabled': u'Last Enabled', |
|
25 |
'checker': u'Checker', |
|
24.1.116
by Björn Påhlsson
added a mandos list client program |
26 |
}
|
412
by Teddy Hogeborn
* mandos-ctl (defaultkeywords): Removed "checker". |
27 |
defaultkeywords = ('name', 'enabled', 'timeout', 'last_checked_ok') |
24.1.130
by Björn Påhlsson
new domain name for d-bus |
28 |
domain = 'se.bsnet.fukt' |
29 |
busname = domain + '.Mandos' |
|
30 |
server_path = '/' |
|
31 |
server_interface = domain + '.Mandos' |
|
32 |
client_interface = domain + '.Mandos.Client' |
|
395
by Teddy Hogeborn
Merge from release branch. |
33 |
version = "1.0.14" |
24.1.118
by Björn Påhlsson
Added enable/disable |
34 |
bus = dbus.SystemBus() |
35 |
mandos_dbus_objc = bus.get_object(busname, server_path) |
|
36 |
mandos_serv = dbus.Interface(mandos_dbus_objc, |
|
37 |
dbus_interface = server_interface) |
|
38 |
mandos_clients = mandos_serv.GetAllClientsWithProperties() |
|
39 |
||
382
by Teddy Hogeborn
* mandos-ctl: Made work again after D-Bus API changes. |
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)) |
|
24.1.121
by Björn Påhlsson
mandos-ctl: Added support for all client calls |
45 |
|
46 |
def milliseconds_to_string(ms): |
|
47 |
td = datetime.timedelta(0, 0, 0, ms) |
|
382
by Teddy Hogeborn
* mandos-ctl: Made work again after D-Bus API changes. |
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 |
})
|
|
24.1.121
by Björn Påhlsson
mandos-ctl: Added support for all client calls |
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" |
|
382
by Teddy Hogeborn
* mandos-ctl: Made work again after D-Bus API changes. |
100 |
if keyword in (u"timeout", u"interval"): |
24.1.121
by Björn Påhlsson
mandos-ctl: Added support for all client calls |
101 |
return milliseconds_to_string(value) |
102 |
return unicode(value) |
|
103 |
||
382
by Teddy Hogeborn
* mandos-ctl: Made work again after D-Bus API changes. |
104 |
# Create format string to print table rows
|
24.1.118
by Björn Påhlsson
Added enable/disable |
105 |
format_string = u' '.join(u'%%-%ds' % |
106 |
max(len(tablewords[key]), |
|
24.1.121
by Björn Påhlsson
mandos-ctl: Added support for all client calls |
107 |
max(len(valuetostring(client[key], key)) |
24.1.118
by Björn Påhlsson
Added enable/disable |
108 |
for client in |
24.1.121
by Björn Påhlsson
mandos-ctl: Added support for all client calls |
109 |
clients)) |
24.1.118
by Björn Påhlsson
Added enable/disable |
110 |
for key in keywords) |
382
by Teddy Hogeborn
* mandos-ctl: Made work again after D-Bus API changes. |
111 |
# Print header line
|
355
by Teddy Hogeborn
* mandos: White-space fixes only. |
112 |
print format_string % tuple(tablewords[key] for key in keywords) |
24.1.121
by Björn Påhlsson
mandos-ctl: Added support for all client calls |
113 |
for client in clients: |
114 |
print format_string % tuple(valuetostring(client[key], key) |
|
24.1.118
by Björn Påhlsson
Added enable/disable |
115 |
for key in keywords) |
116 |
||
24.1.116
by Björn Påhlsson
added a mandos list client program |
117 |
parser = OptionParser(version = "%%prog %s" % version) |
24.1.119
by Björn Påhlsson
Added more method support for mandos clients through mandos-ctl |
118 |
parser.add_option("-a", "--all", action="store_true", |
240
by Teddy Hogeborn
Merge "mandos-list" from belorn. |
119 |
help="Print all fields") |
24.1.119
by Björn Påhlsson
Added more method support for mandos clients through mandos-ctl |
120 |
parser.add_option("-e", "--enable", action="store_true", |
24.1.128
by Björn Påhlsson
fixed a bugg in mandos-ctl + added remove client option |
121 |
help="Enable client") |
24.1.119
by Björn Påhlsson
Added more method support for mandos clients through mandos-ctl |
122 |
parser.add_option("-d", "--disable", action="store_true", |
24.1.128
by Björn Påhlsson
fixed a bugg in mandos-ctl + added remove client option |
123 |
help="disable client") |
24.1.121
by Björn Påhlsson
mandos-ctl: Added support for all client calls |
124 |
parser.add_option("-b", "--bump-timeout", action="store_true", |
24.1.128
by Björn Påhlsson
fixed a bugg in mandos-ctl + added remove client option |
125 |
help="Bump timeout for client") |
24.1.121
by Björn Påhlsson
mandos-ctl: Added support for all client calls |
126 |
parser.add_option("--start-checker", action="store_true", |
24.1.128
by Björn Påhlsson
fixed a bugg in mandos-ctl + added remove client option |
127 |
help="Start checker for client") |
24.1.121
by Björn Påhlsson
mandos-ctl: Added support for all client calls |
128 |
parser.add_option("--stop-checker", action="store_true", |
24.1.128
by Björn Påhlsson
fixed a bugg in mandos-ctl + added remove client option |
129 |
help="Stop checker for client") |
411
by Teddy Hogeborn
More consistent terminology: Clients are no longer "invalid" - they |
130 |
parser.add_option("-V", "--is-enabled", action="store_true", |
131 |
help="Check if client is enabled") |
|
24.1.128
by Björn Påhlsson
fixed a bugg in mandos-ctl + added remove client option |
132 |
parser.add_option("-r", "--remove", action="store_true", |
133 |
help="Remove client") |
|
24.1.121
by Björn Påhlsson
mandos-ctl: Added support for all client calls |
134 |
parser.add_option("-c", "--checker", type="string", |
24.1.128
by Björn Påhlsson
fixed a bugg in mandos-ctl + added remove client option |
135 |
help="Set checker command for client") |
24.1.121
by Björn Påhlsson
mandos-ctl: Added support for all client calls |
136 |
parser.add_option("-t", "--timeout", type="string", |
24.1.128
by Björn Påhlsson
fixed a bugg in mandos-ctl + added remove client option |
137 |
help="Set timeout for client") |
24.1.121
by Björn Påhlsson
mandos-ctl: Added support for all client calls |
138 |
parser.add_option("-i", "--interval", type="string", |
24.1.128
by Björn Påhlsson
fixed a bugg in mandos-ctl + added remove client option |
139 |
help="Set checker interval for client") |
24.1.121
by Björn Påhlsson
mandos-ctl: Added support for all client calls |
140 |
parser.add_option("-H", "--host", type="string", |
24.1.128
by Björn Påhlsson
fixed a bugg in mandos-ctl + added remove client option |
141 |
help="Set host for client") |
24.1.121
by Björn Påhlsson
mandos-ctl: Added support for all client calls |
142 |
parser.add_option("-s", "--secret", type="string", |
24.1.128
by Björn Påhlsson
fixed a bugg in mandos-ctl + added remove client option |
143 |
help="Set password blob (file) for client") |
24.1.119
by Björn Påhlsson
Added more method support for mandos clients through mandos-ctl |
144 |
options, client_names = parser.parse_args() |
145 |
||
291
by Teddy Hogeborn
Merge from Björn: |
146 |
# Compile list of clients to process
|
24.1.119
by Björn Påhlsson
Added more method support for mandos clients through mandos-ctl |
147 |
clients=[] |
148 |
for name in client_names: |
|
149 |
for path, client in mandos_clients.iteritems(): |
|
150 |
if client['name'] == name: |
|
151 |
client_objc = bus.get_object(busname, path) |
|
382
by Teddy Hogeborn
* mandos-ctl: Made work again after D-Bus API changes. |
152 |
clients.append(client_objc) |
24.1.119
by Björn Påhlsson
Added more method support for mandos clients through mandos-ctl |
153 |
break
|
154 |
else: |
|
155 |
print >> sys.stderr, "Client not found on server: %r" % name |
|
156 |
sys.exit(1) |
|
157 |
||
24.1.128
by Björn Påhlsson
fixed a bugg in mandos-ctl + added remove client option |
158 |
if not clients and mandos_clients.values(): |
24.1.116
by Björn Påhlsson
added a mandos list client program |
159 |
keywords = defaultkeywords |
24.1.119
by Björn Påhlsson
Added more method support for mandos clients through mandos-ctl |
160 |
if options.all: |
161 |
keywords = ('name', 'enabled', 'timeout', 'last_checked_ok', |
|
162 |
'created', 'interval', 'host', 'fingerprint', |
|
163 |
'checker_running', 'last_enabled', 'checker') |
|
24.1.121
by Björn Påhlsson
mandos-ctl: Added support for all client calls |
164 |
print_clients(mandos_clients.values()) |
24.1.128
by Björn Påhlsson
fixed a bugg in mandos-ctl + added remove client option |
165 |
|
166 |
# Process each client in the list by all selected options
|
|
24.1.119
by Björn Påhlsson
Added more method support for mandos clients through mandos-ctl |
167 |
for client in clients: |
24.1.128
by Björn Påhlsson
fixed a bugg in mandos-ctl + added remove client option |
168 |
if options.remove: |
169 |
mandos_serv.RemoveClient(client.__dbus_object_path__) |
|
24.1.119
by Björn Påhlsson
Added more method support for mandos clients through mandos-ctl |
170 |
if options.enable: |
382
by Teddy Hogeborn
* mandos-ctl: Made work again after D-Bus API changes. |
171 |
client.Enable(dbus_interface=client_interface) |
24.1.119
by Björn Påhlsson
Added more method support for mandos clients through mandos-ctl |
172 |
if options.disable: |
382
by Teddy Hogeborn
* mandos-ctl: Made work again after D-Bus API changes. |
173 |
client.Disable(dbus_interface=client_interface) |
24.1.121
by Björn Påhlsson
mandos-ctl: Added support for all client calls |
174 |
if options.bump_timeout: |
382
by Teddy Hogeborn
* mandos-ctl: Made work again after D-Bus API changes. |
175 |
client.CheckedOK(dbus_interface=client_interface) |
24.1.121
by Björn Påhlsson
mandos-ctl: Added support for all client calls |
176 |
if options.start_checker: |
382
by Teddy Hogeborn
* mandos-ctl: Made work again after D-Bus API changes. |
177 |
client.StartChecker(dbus_interface=client_interface) |
24.1.121
by Björn Påhlsson
mandos-ctl: Added support for all client calls |
178 |
if options.stop_checker: |
382
by Teddy Hogeborn
* mandos-ctl: Made work again after D-Bus API changes. |
179 |
client.StopChecker(dbus_interface=client_interface) |
411
by Teddy Hogeborn
More consistent terminology: Clients are no longer "invalid" - they |
180 |
if options.is_enabled: |
382
by Teddy Hogeborn
* mandos-ctl: Made work again after D-Bus API changes. |
181 |
sys.exit(0 if client.Get(client_interface, |
182 |
u"enabled", |
|
183 |
dbus_interface=dbus.PROPERTIES_IFACE) |
|
184 |
else 1) |
|
24.1.121
by Björn Påhlsson
mandos-ctl: Added support for all client calls |
185 |
if options.checker: |
382
by Teddy Hogeborn
* mandos-ctl: Made work again after D-Bus API changes. |
186 |
client.Set(client_interface, u"checker", options.checker, |
187 |
dbus_interface=dbus.PROPERTIES_IFACE) |
|
24.1.121
by Björn Påhlsson
mandos-ctl: Added support for all client calls |
188 |
if options.host: |
382
by Teddy Hogeborn
* mandos-ctl: Made work again after D-Bus API changes. |
189 |
client.Set(client_interface, u"host", options.host, |
190 |
dbus_interface=dbus.PROPERTIES_IFACE) |
|
24.1.121
by Björn Påhlsson
mandos-ctl: Added support for all client calls |
191 |
if options.interval: |
382
by Teddy Hogeborn
* mandos-ctl: Made work again after D-Bus API changes. |
192 |
client.Set(client_interface, u"interval", |
193 |
timedelta_to_milliseconds
|
|
194 |
(string_to_delta(options.interval)), |
|
195 |
dbus_interface=dbus.PROPERTIES_IFACE) |
|
24.1.121
by Björn Påhlsson
mandos-ctl: Added support for all client calls |
196 |
if options.timeout: |
382
by Teddy Hogeborn
* mandos-ctl: Made work again after D-Bus API changes. |
197 |
client.Set(client_interface, u"timeout", |
198 |
timedelta_to_milliseconds(string_to_delta |
|
199 |
(options.timeout)), |
|
200 |
dbus_interface=dbus.PROPERTIES_IFACE) |
|
24.1.121
by Björn Påhlsson
mandos-ctl: Added support for all client calls |
201 |
if options.secret: |
382
by Teddy Hogeborn
* mandos-ctl: Made work again after D-Bus API changes. |
202 |
client.Set(client_interface, u"secret", |
203 |
dbus.ByteArray(open(options.secret, u'rb').read()), |
|
204 |
dbus_interface=dbus.PROPERTIES_IFACE) |