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
 
59
43
class MandosClientPropertyCache(object):
 
60
44
    """This wraps a Mandos Client D-Bus proxy object, caches the
 
61
45
    properties and calls a hook function when any of them are
 
 
134
115
                                     client_interface,
 
135
116
                                     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
 
152
118
    def checker_completed(self, exitstatus, condition, command):
 
153
119
        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
 
158
120
            #self.logger(u'Checker for client %s (command "%s")'
 
159
121
            #            u' was successful'
 
160
 
            #            % (self.properties[u"Name"], command))
 
 
122
            #            % (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
 
169
124
        if os.WIFEXITED(condition):
 
170
125
            self.logger(u'Checker for client %s (command "%s")'
 
171
126
                        u' failed with exit code %s'
 
172
 
                        % (self.properties[u"Name"], command,
 
 
127
                        % (self.properties[u"name"], command,
 
173
128
                           os.WEXITSTATUS(condition)))
 
174
 
        elif os.WIFSIGNALED(condition):
 
 
130
        if os.WIFSIGNALED(condition):
 
175
131
            self.logger(u'Checker for client %s (command "%s")'
 
176
132
                        u' was killed by signal %s'
 
177
 
                        % (self.properties[u"Name"], command,
 
 
133
                        % (self.properties[u"name"], command,
 
178
134
                           os.WTERMSIG(condition)))
 
179
 
        elif os.WCOREDUMP(condition):
 
 
136
        if os.WCOREDUMP(condition):
 
180
137
            self.logger(u'Checker for client %s (command "%s")'
 
182
 
                        % (self.properties[u"Name"], command))
 
184
 
            self.logger(u'Checker for client %s completed'
 
 
139
                        % (self.properties[u"name"], command))
 
 
140
        self.logger(u'Checker for client %s completed mysteriously')
 
188
142
    def checker_started(self, command):
 
189
143
        #self.logger(u'Client %s started checker "%s"'
 
190
 
        #            % (self.properties[u"Name"], unicode(command)))
 
 
144
        #            % (self.properties[u"name"], unicode(command)))
 
193
147
    def got_secret(self):
 
194
 
        self.last_checker_failed = False
 
195
148
        self.logger(u'Client %s received its secret'
 
196
 
                    % self.properties[u"Name"])
 
 
149
                    % self.properties[u"name"])
 
198
151
    def need_approval(self, timeout, default):
 
 
202
155
            message = u'Client %s will get its secret in %s seconds'
 
203
156
        self.logger(message
 
204
 
                    % (self.properties[u"Name"], timeout/1000))
 
 
157
                    % (self.properties[u"name"], timeout/1000))
 
206
159
    def rejected(self, reason):
 
207
160
        self.logger(u'Client %s was rejected; reason: %s'
 
208
 
                    % (self.properties[u"Name"], reason))
 
 
161
                    % (self.properties[u"name"], reason))
 
210
163
    def selectable(self):
 
211
164
        """Make this a "selectable" widget.
 
 
237
190
        # Rebuild focus and non-focus widgets using current properties
 
239
192
        # Base part of a client. Name!
 
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?"
 
 
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?"
 
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])
 
 
200
                self._text += u"Seeks approval to send secret. (a)pprove?"
 
263
 
        self._text = "%s%s" % (base, message)
 
 
202
            self._text += (u'%(enabled)s'
 
 
205
                                if self.properties[u"enabled"]
 
265
207
        if not urwid.supports_unicode():
 
266
208
            self._text = self._text.encode("ascii", "replace")
 
267
209
        textlist = [(u"normal", self._text)]
 
 
346
284
    use them as an excuse to shift focus away from this widget.
 
348
286
    def keypress(self, (maxcol, maxrow), key):
 
349
 
        ret = super(ConstrainedListBox, self).keypress((maxcol,
 
 
287
        ret = super(ConstrainedListBox, self).keypress((maxcol, maxrow), key)
 
351
288
        if ret in (u"up", u"down"):
 
 
500
434
        """Toggle visibility of the log buffer."""
 
501
435
        self.log_visible = not self.log_visible
 
503
 
        #self.log_message(u"Log visibility changed to: "
 
504
 
        #                 + unicode(self.log_visible))
 
 
437
        self.log_message(u"Log visibility changed to: "
 
 
438
                         + unicode(self.log_visible))
 
506
440
    def change_log_display(self):
 
507
441
        """Change type of log display.