40
40
urwid.curses_display.curses.A_UNDERLINE |= (
 
41
41
    urwid.curses_display.curses.A_BLINK)
 
 
43
def isoformat_to_datetime(iso):
 
 
44
    "Parse an ISO 8601 date string to a datetime.datetime()"
 
 
47
    d, t = iso.split(u"T", 1)
 
 
48
    year, month, day = d.split(u"-", 2)
 
 
49
    hour, minute, second = t.split(u":", 2)
 
 
50
    second, fraction = divmod(float(second), 1)
 
 
51
    return datetime.datetime(int(year),
 
 
56
                             int(second),           # Whole seconds
 
 
57
                             int(fraction*1000000)) # Microseconds
 
43
59
class MandosClientPropertyCache(object):
 
44
60
    """This wraps a Mandos Client D-Bus proxy object, caches the
 
45
61
    properties and calls a hook function when any of them are
 
 
115
134
                                     client_interface,
 
116
135
                                     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
 
 
141
            self.last_checker_failed = ((datetime.datetime.utcnow()
 
 
145
                                         self.properties["interval"]))
 
 
146
        if self.last_checker_failed:
 
 
147
            self._update_timer_callback_tag = (gobject.timeout_add
 
118
151
    def checker_completed(self, exitstatus, condition, command):
 
119
152
        if exitstatus == 0:
 
120
 
            #self.logger(u'Checker for client %s (command "%s")'
 
122
 
            #            % (self.properties[u"name"], command))
 
 
153
            if self.last_checker_failed:
 
 
154
                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))
 
 
163
        if not self.last_checker_failed:
 
 
164
            self.last_checker_failed = True
 
 
165
            self._update_timer_callback_tag = (gobject.timeout_add
 
124
168
        if os.WIFEXITED(condition):
 
125
169
            self.logger(u'Checker for client %s (command "%s")'
 
126
170
                        u' failed with exit code %s'
 
127
171
                        % (self.properties[u"name"], command,
 
128
172
                           os.WEXITSTATUS(condition)))
 
130
 
        if os.WIFSIGNALED(condition):
 
 
173
        elif os.WIFSIGNALED(condition):
 
131
174
            self.logger(u'Checker for client %s (command "%s")'
 
132
175
                        u' was killed by signal %s'
 
133
176
                        % (self.properties[u"name"], command,
 
134
177
                           os.WTERMSIG(condition)))
 
136
 
        if os.WCOREDUMP(condition):
 
 
178
        elif os.WCOREDUMP(condition):
 
137
179
            self.logger(u'Checker for client %s (command "%s")'
 
139
181
                        % (self.properties[u"name"], command))
 
140
 
        self.logger(u'Checker for client %s completed mysteriously')
 
 
183
            self.logger(u'Checker for client %s completed mysteriously')
 
142
186
    def checker_started(self, command):
 
143
187
        #self.logger(u'Client %s started checker "%s"'
 
 
190
234
        # Rebuild focus and non-focus widgets using current properties
 
192
236
        # Base part of a client. Name!
 
193
 
        self._text = (u'%(name)s: '
 
 
237
        base = (u'%(name)s: '
 
194
238
                      % {u"name": self.properties[u"name"]})
 
 
239
        if not self.properties[u"enabled"]:
 
 
240
            message = u"DISABLED"
 
 
241
        elif self.last_checker_failed:
 
 
242
            timeout = datetime.timedelta(milliseconds
 
 
243
                                         = self.properties[u"timeout"])
 
 
244
            last_ok = isoformat_to_datetime(
 
 
245
                max((self.properties["last_checked_ok"]
 
 
246
                     or self.properties["created"]),
 
 
247
                    self.properties[u"last_enabled"]))
 
 
248
            timer = timeout - (datetime.datetime.utcnow() - last_ok)
 
196
 
        if self.properties[u"approved_pending"]:
 
 
250
            message = (u'A checker has failed! Time until client gets diabled: %s'
 
 
252
        elif self.properties[u"approved_pending"]:
 
197
253
            if self.properties[u"approved_by_default"]:
 
198
 
                self._text += u"Connection established to client. (d)eny?"
 
 
254
                message = u"Connection established to client. (d)eny?"
 
200
 
                self._text += u"Seeks approval to send secret. (a)pprove?"
 
 
256
                message = u"Seeks approval to send secret. (a)pprove?"
 
202
 
            self._text += (u'%(enabled)s'
 
205
 
                                if self.properties[u"enabled"]
 
 
259
        self._text = "%s%s" % (base, message)
 
207
261
        if not urwid.supports_unicode():
 
208
262
            self._text = self._text.encode("ascii", "replace")
 
209
263
        textlist = [(u"normal", self._text)]
 
 
220
274
        if self.update_hook is not None:
 
221
275
            self.update_hook()
 
 
277
    def update_timer(self):
 
 
280
        return True             # Keep calling this
 
223
282
    def delete(self):
 
 
283
        if self._update_timer_callback_tag is not None:
 
 
284
            gobject.source_remove(self._update_timer_callback_tag)
 
 
285
            self._update_timer_callback_tag = None
 
224
286
        if self.delete_hook is not None:
 
225
287
            self.delete_hook(self)