2
2
# -*- mode: python; coding: utf-8 -*-
4
# Mandos Monitor - Control and monitor the Mandos server
6
# Copyright © 2009,2010 Teddy Hogeborn
7
# Copyright © 2009,2010 Björn Påhlsson
9
# This program is free software: you can redistribute it and/or modify
10
# it under the terms of the GNU General Public License as published by
11
# the Free Software Foundation, either version 3 of the License, or
12
# (at your option) any later version.
14
# This program is distributed in the hope that it will be useful,
15
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
# GNU General Public License for more details.
19
# You should have received a copy of the GNU General Public License
20
# along with this program. If not, see <http://www.gnu.org/licenses/>.
22
# Contact the authors at <mandos@fukt.bsnet.se>.
4
25
from __future__ import division, absolute_import, with_statement
134
175
client_interface,
135
176
byte_arrays=True)
136
last_checked_ok = isoformat_to_datetime(self.properties
138
if last_checked_ok is None:
139
self.last_checker_failed = True
178
def property_changed(self, property=None, value=None):
179
super(self, MandosClientWidget).property_changed(property,
181
if property == u"ApprovalPending":
182
using_timer(bool(value))
184
def using_timer(self, flag):
185
"""Call this method with True or False when timer should be
186
activated or deactivated.
188
old = self._update_timer_callback_lock
190
self._update_timer_callback_lock += 1
141
self.last_checker_failed = ((datetime.datetime.utcnow()
145
self.properties["interval"]))
146
if self.last_checker_failed:
192
self._update_timer_callback_lock -= 1
193
if old == 0 and self._update_timer_callback_lock:
147
194
self._update_timer_callback_tag = (gobject.timeout_add
149
196
self.update_timer))
197
elif old and self._update_timer_callback_lock == 0:
198
gobject.source_remove(self._update_timer_callback_tag)
199
self._update_timer_callback_tag = None
151
201
def checker_completed(self, exitstatus, condition, command):
152
202
if exitstatus == 0:
153
203
if self.last_checker_failed:
154
204
self.last_checker_failed = False
155
gobject.source_remove(self._update_timer_callback_tag)
156
self._update_timer_callback_tag = None
157
self.logger(u'Checker for client %s (command "%s")'
159
% (self.properties[u"name"], command))
205
self.using_timer(False)
206
#self.logger(u'Checker for client %s (command "%s")'
208
# % (self.properties[u"Name"], command))
163
212
if not self.last_checker_failed:
164
213
self.last_checker_failed = True
165
self._update_timer_callback_tag = (gobject.timeout_add
214
self.using_timer(True)
168
215
if os.WIFEXITED(condition):
169
216
self.logger(u'Checker for client %s (command "%s")'
170
217
u' failed with exit code %s'
171
% (self.properties[u"name"], command,
218
% (self.properties[u"Name"], command,
172
219
os.WEXITSTATUS(condition)))
173
220
elif os.WIFSIGNALED(condition):
174
221
self.logger(u'Checker for client %s (command "%s")'
175
222
u' was killed by signal %s'
176
% (self.properties[u"name"], command,
223
% (self.properties[u"Name"], command,
177
224
os.WTERMSIG(condition)))
178
225
elif os.WCOREDUMP(condition):
179
226
self.logger(u'Checker for client %s (command "%s")'
181
% (self.properties[u"name"], command))
228
% (self.properties[u"Name"], command))
183
self.logger(u'Checker for client %s completed mysteriously')
230
self.logger(u'Checker for client %s completed'
186
234
def checker_started(self, command):
187
235
#self.logger(u'Client %s started checker "%s"'
188
# % (self.properties[u"name"], unicode(command)))
236
# % (self.properties[u"Name"], unicode(command)))
191
239
def got_secret(self):
240
self.last_checker_failed = False
192
241
self.logger(u'Client %s received its secret'
193
% self.properties[u"name"])
242
% self.properties[u"Name"])
195
244
def need_approval(self, timeout, default):
236
286
# Base part of a client. Name!
237
287
base = (u'%(name)s: '
238
% {u"name": self.properties[u"name"]})
239
if not self.properties[u"enabled"]:
288
% {u"name": self.properties[u"Name"]})
289
if not self.properties[u"Enabled"]:
240
290
message = u"DISABLED"
291
elif self.properties[u"ApprovalPending"]:
292
timeout = datetime.timedelta(milliseconds
295
last_approval_request = isoformat_to_datetime(
296
self.properties[u"LastApprovalRequest"])
297
if last_approval_request is not None:
298
timer = timeout - (datetime.datetime.utcnow()
299
- last_approval_request)
301
timer = datetime.timedelta()
302
if self.properties[u"ApprovedByDefault"]:
303
message = u"Approval in %s. (d)eny?"
305
message = u"Denial in %s. (a)pprove?"
306
message = message % unicode(timer).rsplit(".", 1)[0]
241
307
elif self.last_checker_failed:
242
308
timeout = datetime.timedelta(milliseconds
243
= self.properties[u"timeout"])
244
311
last_ok = isoformat_to_datetime(
245
max((self.properties["last_checked_ok"]
246
or self.properties["created"]),
247
self.properties[u"last_enabled"]))
312
max((self.properties[u"LastCheckedOK"]
313
or self.properties[u"Created"]),
314
self.properties[u"LastEnabled"]))
248
315
timer = timeout - (datetime.datetime.utcnow() - last_ok)
249
message = (u'A checker has failed! Time until client gets diabled: %s'
316
message = (u'A checker has failed! Time until client'
317
u' gets disabled: %s'
250
318
% unicode(timer).rsplit(".", 1)[0])
251
elif self.properties[u"approved_pending"]:
252
if self.properties[u"approved_by_default"]:
253
message = u"Connection established to client. (d)eny?"
255
message = u"Seeks approval to send secret. (a)pprove?"
257
320
message = u"enabled"
258
321
self._text = "%s%s" % (base, message)