24
20
'last_enabled': u'Last Enabled',
25
21
'checker': u'Checker',
23
busname = 'org.mandos-system.Mandos'
24
server_path = '/Mandos'
25
server_interface = 'org.mandos_system.Mandos'
26
client_interface = 'org.mandos_system.Mandos.Client'
27
28
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'
35
30
bus = dbus.SystemBus()
36
31
mandos_dbus_objc = bus.get_object(busname, server_path)
37
32
mandos_serv = dbus.Interface(mandos_dbus_objc,
38
33
dbus_interface = server_interface)
39
34
mandos_clients = mandos_serv.GetAllClientsWithProperties()
41
def datetime_to_milliseconds(dt):
42
"Return the 'timeout' attribute in milliseconds"
43
return ((dt.days * 24 * 60 * 60 * 1000)
45
+ (dt.microseconds // 1000))
47
def milliseconds_to_string(ms):
48
td = datetime.timedelta(0, 0, 0, ms)
49
return "%s%02d:%02d:%02d" % (("%dT" % td.days) if td.days else "", # days
50
td.seconds // 3600, # hours
51
(td.seconds % 3600) // 60, # minutes
52
(td.seconds % 60)) # seconds
55
def string_to_delta(interval):
56
"""Parse a string and return a datetime.timedelta
58
>>> string_to_delta('7d')
60
>>> string_to_delta('60s')
61
datetime.timedelta(0, 60)
62
>>> string_to_delta('60m')
63
datetime.timedelta(0, 3600)
64
>>> string_to_delta('24h')
66
>>> string_to_delta(u'1w')
68
>>> string_to_delta('5m 30s')
69
datetime.timedelta(0, 330)
71
timevalue = datetime.timedelta(0)
72
regexp = re.compile("\d+[dsmhw]")
74
for s in regexp.findall(interval):
76
suffix = unicode(s[-1])
79
delta = datetime.timedelta(value)
81
delta = datetime.timedelta(0, value)
83
delta = datetime.timedelta(0, 0, 0, 0, value)
85
delta = datetime.timedelta(0, 0, 0, 0, 0, value)
87
delta = datetime.timedelta(0, 0, 0, 0, 0, 0, value)
90
except (ValueError, IndexError):
95
def print_clients(clients):
96
def valuetostring(value, keyword):
97
if type(value) is dbus.Boolean:
98
return u"Yes" if value else u"No"
99
if keyword in ("timeout", "interval"):
100
return milliseconds_to_string(value)
101
return unicode(value)
36
def Getclientbyname(name):
38
for cli in mandos_clients.iteritems():
39
if cli[1]['name'] == name:
41
if client_path is None:
42
print "Client not found on server"
45
client_objc = bus.get_object(busname, client_path)
46
return dbus.Interface(client_objc, dbus_interface = client_interface)
50
client = Getclientbyname(options.enable)
51
if client is not None:
55
client = Getclientbyname(options.disable)
56
if client is not None:
61
if type(x) is dbus.Boolean:
62
return u"Yes" if x else u"No"
103
65
format_string = u' '.join(u'%%-%ds' %
104
66
max(len(tablewords[key]),
105
max(len(valuetostring(client[key], key))
67
max(len(valuetostring(client[key]))
69
mandos_clients.itervalues()))
108
70
for key in keywords)
109
71
print format_string % tuple(tablewords[key] for key in keywords)
110
for client in clients:
111
print format_string % tuple(valuetostring(client[key], key)
72
for client in mandos_clients.itervalues():
73
print format_string % tuple(valuetostring(client[key])
112
74
for key in keywords)
114
77
parser = OptionParser(version = "%%prog %s" % version)
115
parser.add_option("-a", "--all", action="store_true",
78
parser.add_option("-a", "--all", action="store_true", default=False,
116
79
help="Print all fields")
117
parser.add_option("-e", "--enable", action="store_true",
118
help="Enable client")
119
parser.add_option("-d", "--disable", action="store_true",
120
help="disable client")
121
parser.add_option("-b", "--bump-timeout", action="store_true",
122
help="Bump timeout for client")
123
parser.add_option("--start-checker", action="store_true",
124
help="Start checker for client")
125
parser.add_option("--stop-checker", action="store_true",
126
help="Stop checker for client")
127
parser.add_option("-V", "--is-valid", action="store_true",
128
help="Check if client is still valid")
129
parser.add_option("-r", "--remove", action="store_true",
130
help="Remove client")
131
parser.add_option("-c", "--checker", type="string",
132
help="Set checker command for client")
133
parser.add_option("-t", "--timeout", type="string",
134
help="Set timeout for client")
135
parser.add_option("-i", "--interval", type="string",
136
help="Set checker interval for client")
137
parser.add_option("-H", "--host", type="string",
138
help="Set host for client")
139
parser.add_option("-s", "--secret", type="string",
140
help="Set password blob (file) for client")
141
options, client_names = parser.parse_args()
143
# Compile list of clients to process
145
for name in client_names:
146
for path, client in mandos_clients.iteritems():
147
if client['name'] == name:
148
client_objc = bus.get_object(busname, path)
149
clients.append(dbus.Interface(client_objc,
154
print >> sys.stderr, "Client not found on server: %r" % name
157
if not clients and mandos_clients.values():
80
parser.add_option("-e", "--enable", type="string",
81
help="Enable specified client")
82
parser.add_option("-d", "--disable", type="string",
83
help="disable specified client")
84
options = parser.parse_args()[0]
90
keywords = ('name', 'enabled', 'timeout', 'last_checked_ok',
91
'created', 'interval', 'host', 'fingerprint',
92
'checker_running', 'last_enabled', 'checker')
158
94
keywords = defaultkeywords
160
keywords = ('name', 'enabled', 'timeout', 'last_checked_ok',
161
'created', 'interval', 'host', 'fingerprint',
162
'checker_running', 'last_enabled', 'checker')
163
print_clients(mandos_clients.values())
165
# Process each client in the list by all selected options
166
for client in clients:
168
mandos_serv.RemoveClient(client.__dbus_object_path__)
173
if options.bump_timeout:
175
if options.start_checker:
176
client.StartChecker()
177
if options.stop_checker:
180
sys.exit(0 if client.IsStillValid() else 1)
182
client.SetChecker(options.checker)
184
client.SetHost(options.host)
186
client.SetInterval(datetime_to_milliseconds
187
(string_to_delta(options.interval)))
189
client.SetTimeout(datetime_to_milliseconds
190
(string_to_delta(options.timeout)))
192
client.SetSecret(dbus.ByteArray(open(options.secret, 'rb').read()))