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>.
25
4
from __future__ import division, absolute_import, with_statement
175
134
client_interface,
176
135
byte_arrays=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
136
last_checked_ok = isoformat_to_datetime(self.properties
138
if last_checked_ok is None:
139
self.last_checker_failed = True
192
self._update_timer_callback_lock -= 1
193
if old == 0 and self._update_timer_callback_lock:
141
self.last_checker_failed = ((datetime.datetime.utcnow()
145
self.properties["interval"]))
146
if self.last_checker_failed:
194
147
self._update_timer_callback_tag = (gobject.timeout_add
196
149
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
201
151
def checker_completed(self, exitstatus, condition, command):
202
152
if exitstatus == 0:
203
153
if self.last_checker_failed:
204
154
self.last_checker_failed = False
205
self.using_timer(False)
206
#self.logger(u'Checker for client %s (command "%s")'
208
# % (self.properties[u"Name"], command))
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))
212
163
if not self.last_checker_failed:
213
164
self.last_checker_failed = True
214
self.using_timer(True)
165
self._update_timer_callback_tag = (gobject.timeout_add
215
168
if os.WIFEXITED(condition):
216
169
self.logger(u'Checker for client %s (command "%s")'
217
170
u' failed with exit code %s'
218
% (self.properties[u"Name"], command,
171
% (self.properties[u"name"], command,
219
172
os.WEXITSTATUS(condition)))
220
173
elif os.WIFSIGNALED(condition):
221
174
self.logger(u'Checker for client %s (command "%s")'
222
175
u' was killed by signal %s'
223
% (self.properties[u"Name"], command,
176
% (self.properties[u"name"], command,
224
177
os.WTERMSIG(condition)))
225
178
elif os.WCOREDUMP(condition):
226
179
self.logger(u'Checker for client %s (command "%s")'
228
% (self.properties[u"Name"], command))
181
% (self.properties[u"name"], command))
230
self.logger(u'Checker for client %s completed'
183
self.logger(u'Checker for client %s completed mysteriously')
234
186
def checker_started(self, command):
235
187
#self.logger(u'Client %s started checker "%s"'
236
# % (self.properties[u"Name"], unicode(command)))
188
# % (self.properties[u"name"], unicode(command)))
239
191
def got_secret(self):
240
self.last_checker_failed = False
241
192
self.logger(u'Client %s received its secret'
242
% self.properties[u"Name"])
193
% self.properties[u"name"])
244
195
def need_approval(self, timeout, default):
286
236
# Base part of a client. Name!
287
237
base = (u'%(name)s: '
288
% {u"name": self.properties[u"Name"]})
289
if not self.properties[u"Enabled"]:
238
% {u"name": self.properties[u"name"]})
239
if not self.properties[u"enabled"]:
290
240
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]
307
241
elif self.last_checker_failed:
308
242
timeout = datetime.timedelta(milliseconds
243
= self.properties[u"timeout"])
311
244
last_ok = isoformat_to_datetime(
312
max((self.properties[u"LastCheckedOK"]
313
or self.properties[u"Created"]),
314
self.properties[u"LastEnabled"]))
245
max((self.properties["last_checked_ok"]
246
or self.properties["created"]),
247
self.properties[u"last_enabled"]))
315
248
timer = timeout - (datetime.datetime.utcnow() - last_ok)
316
message = (u'A checker has failed! Time until client'
317
u' gets disabled: %s'
318
% unicode(timer).rsplit(".", 1)[0])
250
message = (u'A checker has failed! Time until client gets diabled: %s'
252
elif self.properties[u"approved_pending"]:
253
if self.properties[u"approved_by_default"]:
254
message = u"Connection established to client. (d)eny?"
256
message = u"Seeks approval to send secret. (a)pprove?"
320
258
message = u"enabled"
321
259
self._text = "%s%s" % (base, message)