24
24
'last_enabled': u'Last Enabled',
25
25
'checker': u'Checker',
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)
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'
35
bus = dbus.SystemBus()
36
mandos_dbus_objc = bus.get_object(busname, server_path)
37
except dbus.exceptions.DBusException:
37
40
mandos_serv = dbus.Interface(mandos_dbus_objc,
38
41
dbus_interface = server_interface)
39
42
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))
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))
47
50
def milliseconds_to_string(ms):
48
51
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
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,
55
60
def string_to_delta(interval):
115
122
parser.add_option("-a", "--all", action="store_true",
116
123
help="Print all fields")
117
124
parser.add_option("-e", "--enable", action="store_true",
118
help="Enable specified client")
125
help="Enable client")
119
126
parser.add_option("-d", "--disable", action="store_true",
120
help="disable specified client")
127
help="disable client")
121
128
parser.add_option("-b", "--bump-timeout", action="store_true",
122
help="Bump timeout of specified client")
129
help="Bump timeout for client")
123
130
parser.add_option("--start-checker", action="store_true",
124
help="Start checker for specified client")
131
help="Start checker for client")
125
132
parser.add_option("--stop-checker", action="store_true",
126
help="Stop checker for specified client")
127
parser.add_option("-V", "--is-valid", action="store_true",
128
help="Check if specified client is still valid")
129
parser.add_option("-Q", "--quit", action="store_true",
130
help="Stops the running mandos server")
133
help="Stop checker for client")
134
parser.add_option("-V", "--is-enabled", action="store_true",
135
help="Check if client is enabled")
136
parser.add_option("-r", "--remove", action="store_true",
137
help="Remove client")
131
138
parser.add_option("-c", "--checker", type="string",
132
help="Set checker command for specified client")
139
help="Set checker command for client")
133
140
parser.add_option("-t", "--timeout", type="string",
134
help="Set timeout for specified client")
141
help="Set timeout for client")
135
142
parser.add_option("-i", "--interval", type="string",
136
help="Set checker interval for specified client")
143
help="Set checker interval for client")
137
144
parser.add_option("-H", "--host", type="string",
138
help="Set host for specified client")
145
help="Set host for client")
139
146
parser.add_option("-s", "--secret", type="string",
140
help="Set password blob (file) for specified client")
147
help="Set password blob (file) for client")
148
parser.add_option("-A", "--approve", action="store_true",
149
help="Approve any current client request")
150
parser.add_option("-D", "--deny", action="store_true",
151
help="Deny any current client request")
141
152
options, client_names = parser.parse_args()
154
# Compile list of clients to process
144
156
for name in client_names:
145
157
for path, client in mandos_clients.iteritems():
146
158
if client['name'] == name:
147
159
client_objc = bus.get_object(busname, path)
148
clients.append(dbus.Interface(client_objc,
160
clients.append(client_objc)
153
163
print >> sys.stderr, "Client not found on server: %r" % name
166
if not clients and mandos_clients.values():
157
167
keywords = defaultkeywords
159
169
keywords = ('name', 'enabled', 'timeout', 'last_checked_ok',
160
170
'created', 'interval', 'host', 'fingerprint',
161
171
'checker_running', 'last_enabled', 'checker')
162
172
print_clients(mandos_clients.values())
174
# Process each client in the list by all selected options
164
175
for client in clients:
177
mandos_serv.RemoveClient(client.__dbus_object_path__)
165
178
if options.enable:
179
client.Enable(dbus_interface=client_interface)
167
180
if options.disable:
181
client.Disable(dbus_interface=client_interface)
169
182
if options.bump_timeout:
183
client.CheckedOK(dbus_interface=client_interface)
171
184
if options.start_checker:
172
client.StartChecker()
185
client.StartChecker(dbus_interface=client_interface)
173
186
if options.stop_checker:
176
sys.exit(0 if client.IsStillValid() else 1)
187
client.StopChecker(dbus_interface=client_interface)
188
if options.is_enabled:
189
sys.exit(0 if client.Get(client_interface,
191
dbus_interface=dbus.PROPERTIES_IFACE)
177
193
if options.checker:
178
client.SetChecker(options.checker)
194
client.Set(client_interface, u"checker", options.checker,
195
dbus_interface=dbus.PROPERTIES_IFACE)
180
client.SetHost(options.host)
197
client.Set(client_interface, u"host", options.host,
198
dbus_interface=dbus.PROPERTIES_IFACE)
181
199
if options.interval:
182
client.SetInterval(datetime_to_milliseconds
183
(string_to_delta(options.interval)))
200
client.Set(client_interface, u"interval",
201
timedelta_to_milliseconds
202
(string_to_delta(options.interval)),
203
dbus_interface=dbus.PROPERTIES_IFACE)
184
204
if options.timeout:
185
client.SetTimeout(datetime_to_milliseconds
186
(string_to_delta(options.timeout)))
205
client.Set(client_interface, u"timeout",
206
timedelta_to_milliseconds(string_to_delta
208
dbus_interface=dbus.PROPERTIES_IFACE)
187
209
if options.secret:
188
client.SetSecret(dbus.ByteArray(open(options.secret, 'rb').read()))
210
client.Set(client_interface, u"secret",
211
dbus.ByteArray(open(options.secret, u'rb').read()),
212
dbus_interface=dbus.PROPERTIES_IFACE)
214
client.Approve(dbus.Boolean(True), dbus_interface=client_interface)
216
client.Approve(dbus.Boolean(False), dbus_interface=client_interface)