12
12
locale.setlocale(locale.LC_ALL, u'')
16
'Enabled': u'Enabled',
17
'Timeout': u'Timeout',
18
'LastCheckedOK': u'Last Successful Check',
19
'Created': u'Created',
20
'Interval': u'Interval',
22
'Fingerprint': u'Fingerprint',
23
'CheckerRunning': u'Check Is Running',
24
'LastEnabled': u'Last Enabled',
25
'Checker': u'Checker',
16
'enabled': u'Enabled',
17
'timeout': u'Timeout',
18
'last_checked_ok': u'Last Successful Check',
19
'created': u'Created',
20
'interval': u'Interval',
22
'fingerprint': u'Fingerprint',
23
'checker_running': u'Check Is Running',
24
'last_enabled': u'Last Enabled',
25
'checker': u'Checker',
27
defaultkeywords = ('Name', 'Enabled', 'Timeout', 'LastCheckedOK')
28
domain = 'se.bsnet.fukt'
29
busname = domain + '.Mandos'
31
server_interface = domain + '.Mandos'
32
client_interface = domain + '.Mandos.Client'
35
bus = dbus.SystemBus()
36
mandos_dbus_objc = bus.get_object(busname, server_path)
37
except dbus.exceptions.DBusException:
27
defaultkeywords = ('name', 'enabled', 'timeout', 'last_checked_ok',
29
busname = 'org.mandos-system.Mandos'
30
server_path = '/Mandos'
31
server_interface = 'org.mandos_system.Mandos'
32
client_interface = 'org.mandos_system.Mandos.Client'
35
bus = dbus.SystemBus()
36
mandos_dbus_objc = bus.get_object(busname, server_path)
40
37
mandos_serv = dbus.Interface(mandos_dbus_objc,
41
38
dbus_interface = server_interface)
42
39
mandos_clients = mandos_serv.GetAllClientsWithProperties()
44
def timedelta_to_milliseconds(td):
45
"Convert a datetime.timedelta object to milliseconds"
46
return ((td.days * 24 * 60 * 60 * 1000)
48
+ (td.microseconds // 1000))
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))
50
47
def milliseconds_to_string(ms):
51
48
td = datetime.timedelta(0, 0, 0, ms)
52
return (u"%(days)s%(hours)02d:%(minutes)02d:%(seconds)02d"
53
% { "days": "%dT" % td.days if td.days else "",
54
"hours": td.seconds // 3600,
55
"minutes": (td.seconds % 3600) // 60,
56
"seconds": td.seconds % 60,
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
60
55
def string_to_delta(interval):
123
115
parser.add_option("-a", "--all", action="store_true",
124
116
help="Print all fields")
125
117
parser.add_option("-e", "--enable", action="store_true",
126
help="Enable client")
118
help="Enable specified client")
127
119
parser.add_option("-d", "--disable", action="store_true",
128
help="disable client")
120
help="disable specified client")
129
121
parser.add_option("-b", "--bump-timeout", action="store_true",
130
help="Bump timeout for client")
122
help="Bump timeout of specified client")
131
123
parser.add_option("--start-checker", action="store_true",
132
help="Start checker for client")
124
help="Start checker for specified client")
133
125
parser.add_option("--stop-checker", action="store_true",
134
help="Stop checker for client")
135
parser.add_option("-V", "--is-enabled", action="store_true",
136
help="Check if client is enabled")
137
parser.add_option("-r", "--remove", action="store_true",
138
help="Remove client")
126
help="Stop checker for specified client")
127
parser.add_option("-v", "--is-valid", action="store_true",
128
help="Stop checker for specified client")
139
129
parser.add_option("-c", "--checker", type="string",
140
help="Set checker command for client")
130
help="Set checker command for specified client")
141
131
parser.add_option("-t", "--timeout", type="string",
142
help="Set timeout for client")
132
help="Set timeout for specified client")
143
133
parser.add_option("-i", "--interval", type="string",
144
help="Set checker interval for client")
134
help="Set checker interval for specified client")
145
135
parser.add_option("-H", "--host", type="string",
146
help="Set host for client")
136
help="Set host for specified client")
147
137
parser.add_option("-s", "--secret", type="string",
148
help="Set password blob (file) for client")
149
parser.add_option("-A", "--approve", action="store_true",
150
help="Approve any current client request")
151
parser.add_option("-D", "--deny", action="store_true",
152
help="Deny any current client request")
138
help="Set password blob (file) for specified client")
153
139
options, client_names = parser.parse_args()
155
# Compile list of clients to process
157
142
for name in client_names:
158
143
for path, client in mandos_clients.iteritems():
159
144
if client['name'] == name:
160
145
client_objc = bus.get_object(busname, path)
161
clients.append(client_objc)
146
clients.append(dbus.Interface(client_objc,
164
151
print >> sys.stderr, "Client not found on server: %r" % name
167
if not clients and mandos_clients.values():
168
155
keywords = defaultkeywords
170
keywords = ('Name', 'Enabled', 'Timeout', 'LastCheckedOK',
171
'Created', 'Interval', 'Host', 'Fingerprint',
172
'CheckerRunning', 'LastEnabled', 'Checker')
157
keywords = ('name', 'enabled', 'timeout', 'last_checked_ok',
158
'created', 'interval', 'host', 'fingerprint',
159
'checker_running', 'last_enabled', 'checker')
173
160
print_clients(mandos_clients.values())
175
# Process each client in the list by all selected options
176
162
for client in clients:
178
mandos_serv.RemoveClient(client.__dbus_object_path__)
179
163
if options.enable:
180
client.Enable(dbus_interface=client_interface)
181
165
if options.disable:
182
client.Disable(dbus_interface=client_interface)
183
167
if options.bump_timeout:
184
client.CheckedOK(dbus_interface=client_interface)
185
169
if options.start_checker:
186
client.StartChecker(dbus_interface=client_interface)
170
client.StartChecker()
187
171
if options.stop_checker:
188
client.StopChecker(dbus_interface=client_interface)
189
if options.is_enabled:
190
sys.exit(0 if client.Get(client_interface,
192
dbus_interface=dbus.PROPERTIES_IFACE)
174
sys.exit(0 if client.IsStillValid() else 1)
194
175
if options.checker:
195
client.Set(client_interface, u"Checker", options.checker,
196
dbus_interface=dbus.PROPERTIES_IFACE)
176
client.SetChecker(options.checker)
198
client.Set(client_interface, u"Host", options.host,
199
dbus_interface=dbus.PROPERTIES_IFACE)
178
client.SetHost(options.host)
200
179
if options.interval:
201
client.Set(client_interface, u"Interval",
202
timedelta_to_milliseconds
203
(string_to_delta(options.interval)),
204
dbus_interface=dbus.PROPERTIES_IFACE)
180
client.SetInterval(datetime_to_milliseconds
181
(string_to_delta(options.interval)))
205
182
if options.timeout:
206
client.Set(client_interface, u"Timeout",
207
timedelta_to_milliseconds(string_to_delta
209
dbus_interface=dbus.PROPERTIES_IFACE)
183
client.SetTimeout(datetime_to_milliseconds
184
(string_to_delta(options.timeout)))
210
185
if options.secret:
211
client.Set(client_interface, u"Secret",
212
dbus.ByteArray(open(options.secret, u'rb').read()),
213
dbus_interface=dbus.PROPERTIES_IFACE)
215
client.Approve(dbus.Boolean(True),
216
dbus_interface=client_interface)
218
client.Approve(dbus.Boolean(False),
219
dbus_interface=client_interface)
186
client.SetSecret(dbus.ByteArray(open(options.secret, 'rb').read()))