21
24
'last_enabled': u'Last Enabled',
22
25
'checker': u'Checker',
24
defaultkeywords = ('name', 'enabled', 'timeout', 'last_checked_ok',
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'
27
defaultkeywords = ('name', 'enabled', 'timeout', 'last_checked_ok')
28
domain = 'se.bsnet.fukt'
29
busname = domain + '.Mandos'
31
server_interface = domain + '.Mandos'
32
client_interface = domain + '.Mandos.Client'
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()
40
if type(x) is dbus.Boolean:
41
return u"Yes" if x else u"No"
40
def timedelta_to_milliseconds(td):
41
"Convert a datetime.timedelta object to milliseconds"
42
return ((td.days * 24 * 60 * 60 * 1000)
44
+ (td.microseconds // 1000))
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,
56
def string_to_delta(interval):
57
"""Parse a string and return a datetime.timedelta
59
>>> string_to_delta('7d')
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')
67
>>> string_to_delta(u'1w')
69
>>> string_to_delta('5m 30s')
70
datetime.timedelta(0, 330)
72
timevalue = datetime.timedelta(0)
73
regexp = re.compile("\d+[dsmhw]")
75
for s in regexp.findall(interval):
77
suffix = unicode(s[-1])
80
delta = datetime.timedelta(value)
82
delta = datetime.timedelta(0, value)
84
delta = datetime.timedelta(0, 0, 0, 0, value)
86
delta = datetime.timedelta(0, 0, 0, 0, 0, value)
88
delta = datetime.timedelta(0, 0, 0, 0, 0, 0, value)
91
except (ValueError, IndexError):
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)
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))
48
mandos_clients.itervalues()))
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])
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)
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()
146
# Compile list of clients to process
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,
152
clients.append(client_objc)
80
155
print >> sys.stderr, "Client not found on server: %r" % name
158
if not clients and mandos_clients.values():
84
159
keywords = defaultkeywords
86
161
keywords = ('name', 'enabled', 'timeout', 'last_checked_ok',
87
162
'created', 'interval', 'host', 'fingerprint',
88
163
'checker_running', 'last_enabled', 'checker')
164
print_clients(mandos_clients.values())
166
# Process each client in the list by all selected options
91
167
for client in clients:
169
mandos_serv.RemoveClient(client.__dbus_object_path__)
92
170
if options.enable:
171
client.Enable(dbus_interface=client_interface)
94
172
if options.disable:
96
if options.bumptimeout:
98
if options.startchecker:
100
if options.stopchecker:
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,
183
dbus_interface=dbus.PROPERTIES_IFACE)
186
client.Set(client_interface, u"checker", options.checker,
187
dbus_interface=dbus.PROPERTIES_IFACE)
189
client.Set(client_interface, u"host", options.host,
190
dbus_interface=dbus.PROPERTIES_IFACE)
192
client.Set(client_interface, u"interval",
193
timedelta_to_milliseconds
194
(string_to_delta(options.interval)),
195
dbus_interface=dbus.PROPERTIES_IFACE)
197
client.Set(client_interface, u"timeout",
198
timedelta_to_milliseconds(string_to_delta
200
dbus_interface=dbus.PROPERTIES_IFACE)
202
client.Set(client_interface, u"secret",
203
dbus.ByteArray(open(options.secret, u'rb').read()),
204
dbus_interface=dbus.PROPERTIES_IFACE)