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