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()
 
 
147
        if self.last_checker_failed:
 
 
148
            self._update_timer_callback_tag = (gobject.timeout_add
 
118
152
    def checker_completed(self, exitstatus, condition, command):
 
119
153
        if exitstatus == 0:
 
 
154
            if self.last_checker_failed:
 
 
155
                self.last_checker_failed = False
 
 
156
                gobject.source_remove(self._update_timer_callback_tag)
 
 
157
                self._update_timer_callback_tag = None
 
120
158
            #self.logger(u'Checker for client %s (command "%s")'
 
121
159
            #            u' was successful'
 
122
 
            #            % (self.properties[u"name"], command))
 
 
160
            #            % (self.properties[u"Name"], command))
 
 
164
        if not self.last_checker_failed:
 
 
165
            self.last_checker_failed = True
 
 
166
            self._update_timer_callback_tag = (gobject.timeout_add
 
124
169
        if os.WIFEXITED(condition):
 
125
170
            self.logger(u'Checker for client %s (command "%s")'
 
126
171
                        u' failed with exit code %s'
 
127
 
                        % (self.properties[u"name"], command,
 
 
172
                        % (self.properties[u"Name"], command,
 
128
173
                           os.WEXITSTATUS(condition)))
 
130
 
        if os.WIFSIGNALED(condition):
 
 
174
        elif os.WIFSIGNALED(condition):
 
131
175
            self.logger(u'Checker for client %s (command "%s")'
 
132
176
                        u' was killed by signal %s'
 
133
 
                        % (self.properties[u"name"], command,
 
 
177
                        % (self.properties[u"Name"], command,
 
134
178
                           os.WTERMSIG(condition)))
 
136
 
        if os.WCOREDUMP(condition):
 
 
179
        elif os.WCOREDUMP(condition):
 
137
180
            self.logger(u'Checker for client %s (command "%s")'
 
139
 
                        % (self.properties[u"name"], command))
 
140
 
        self.logger(u'Checker for client %s completed mysteriously')
 
 
182
                        % (self.properties[u"Name"], command))
 
 
184
            self.logger(u'Checker for client %s completed'
 
142
188
    def checker_started(self, command):
 
143
189
        #self.logger(u'Client %s started checker "%s"'
 
144
 
        #            % (self.properties[u"name"], unicode(command)))
 
 
190
        #            % (self.properties[u"Name"], unicode(command)))
 
147
193
    def got_secret(self):
 
 
194
        self.last_checker_failed = False
 
148
195
        self.logger(u'Client %s received its secret'
 
149
 
                    % self.properties[u"name"])
 
 
196
                    % self.properties[u"Name"])
 
151
198
    def need_approval(self, timeout, default):
 
 
155
202
            message = u'Client %s will get its secret in %s seconds'
 
156
203
        self.logger(message
 
157
 
                    % (self.properties[u"name"], timeout/1000))
 
 
204
                    % (self.properties[u"Name"], timeout/1000))
 
159
206
    def rejected(self, reason):
 
160
207
        self.logger(u'Client %s was rejected; reason: %s'
 
161
 
                    % (self.properties[u"name"], reason))
 
 
208
                    % (self.properties[u"Name"], reason))
 
163
210
    def selectable(self):
 
164
211
        """Make this a "selectable" widget.
 
 
190
237
        # Rebuild focus and non-focus widgets using current properties
 
192
239
        # Base part of a client. Name!
 
193
 
        self._text = (u'%(name)s: '
 
194
 
                      % {u"name": self.properties[u"name"]})
 
196
 
        if self.properties[u"approved_pending"]:
 
197
 
            if self.properties[u"approved_by_default"]:
 
198
 
                self._text += u"Connection established to client. (d)eny?"
 
 
240
        base = (u'%(name)s: '
 
 
241
                      % {u"name": self.properties[u"Name"]})
 
 
242
        if not self.properties[u"Enabled"]:
 
 
243
            message = u"DISABLED"
 
 
244
        elif self.properties[u"ApprovalPending"]:
 
 
245
            if self.properties[u"ApprovedByDefault"]:
 
 
246
                message = u"Connection established to client. (d)eny?"
 
200
 
                self._text += u"Seeks approval to send secret. (a)pprove?"
 
 
248
                message = u"Seeks approval to send secret. (a)pprove?"
 
 
249
        elif self.last_checker_failed:
 
 
250
            timeout = datetime.timedelta(milliseconds
 
 
253
            last_ok = isoformat_to_datetime(
 
 
254
                max((self.properties[u"LastCheckedOK"]
 
 
255
                     or self.properties[u"Created"]),
 
 
256
                    self.properties[u"LastEnabled"]))
 
 
257
            timer = timeout - (datetime.datetime.utcnow() - last_ok)
 
 
258
            message = (u'A checker has failed! Time until client'
 
 
260
                           % unicode(timer).rsplit(".", 1)[0])
 
202
 
            self._text += (u'%(enabled)s'
 
205
 
                                if self.properties[u"enabled"]
 
 
263
        self._text = "%s%s" % (base, message)
 
207
265
        if not urwid.supports_unicode():
 
208
266
            self._text = self._text.encode("ascii", "replace")
 
209
267
        textlist = [(u"normal", self._text)]
 
 
284
346
    use them as an excuse to shift focus away from this widget.
 
286
348
    def keypress(self, (maxcol, maxrow), key):
 
287
 
        ret = super(ConstrainedListBox, self).keypress((maxcol, maxrow), key)
 
 
349
        ret = super(ConstrainedListBox, self).keypress((maxcol,
 
288
351
        if ret in (u"up", u"down"):
 
 
434
500
        """Toggle visibility of the log buffer."""
 
435
501
        self.log_visible = not self.log_visible
 
437
 
        self.log_message(u"Log visibility changed to: "
 
438
 
                         + unicode(self.log_visible))
 
 
503
        #self.log_message(u"Log visibility changed to: "
 
 
504
        #                 + unicode(self.log_visible))
 
440
506
    def change_log_display(self):
 
441
507
        """Change type of log display.