/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« back to all changes in this revision

Viewing changes to mandos-monitor

merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
urwid.curses_display.curses.A_UNDERLINE |= (
41
41
    urwid.curses_display.curses.A_BLINK)
42
42
 
 
43
def isoformat_to_datetime(iso):
 
44
    "Parse an ISO 8601 date string to a datetime.datetime()"
 
45
    if not iso:
 
46
        return None
 
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),
 
52
                             int(month),
 
53
                             int(day),
 
54
                             int(hour),
 
55
                             int(minute),
 
56
                             int(second),           # Whole seconds
 
57
                             int(fraction*1000000)) # Microseconds
 
58
 
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
85
101
        # Logger
86
102
        self.logger = logger
87
103
        
 
104
        self._update_timer_callback_tag = None
 
105
        self.last_checker_failed = False
 
106
        
88
107
        # The widget shown normally
89
108
        self._text_widget = urwid.Text(u"")
90
109
        # The widget shown when we have focus
114
133
                                     self.rejected,
115
134
                                     client_interface,
116
135
                                     byte_arrays=True)
 
136
        last_checked_ok = isoformat_to_datetime(self.properties
 
137
                                                ["last_checked_ok"])
 
138
        if last_checked_ok is None:
 
139
            self.last_checker_failed = True
 
140
        else:
 
141
            self.last_checker_failed = ((datetime.datetime.utcnow()
 
142
                                         - last_checked_ok)
 
143
                                        > datetime.timedelta
 
144
                                        (milliseconds=
 
145
                                         self.properties["interval"]))
 
146
        if self.last_checker_failed:
 
147
            self._update_timer_callback_tag = (gobject.timeout_add
 
148
                                               (1000,
 
149
                                                self.update_timer))
117
150
    
118
151
    def checker_completed(self, exitstatus, condition, command):
119
152
        if exitstatus == 0:
120
 
            #self.logger(u'Checker for client %s (command "%s")'
121
 
            #            u' was successful'
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")'
 
158
                        u' was successful'
 
159
                        % (self.properties[u"name"], command))
 
160
            self.update()
123
161
            return
 
162
        # Checker failed
 
163
        if not self.last_checker_failed:
 
164
            self.last_checker_failed = True
 
165
            self._update_timer_callback_tag = (gobject.timeout_add
 
166
                                               (1000,
 
167
                                                self.update_timer))
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)))
129
 
            return
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)))
135
 
            return
136
 
        if os.WCOREDUMP(condition):
 
178
        elif os.WCOREDUMP(condition):
137
179
            self.logger(u'Checker for client %s (command "%s")'
138
180
                        u' dumped core'
139
181
                        % (self.properties[u"name"], command))
140
 
        self.logger(u'Checker for client %s completed mysteriously')
 
182
        else:
 
183
            self.logger(u'Checker for client %s completed mysteriously')
 
184
        self.update()
141
185
    
142
186
    def checker_started(self, command):
143
187
        #self.logger(u'Client %s started checker "%s"'
145
189
        pass
146
190
    
147
191
    def got_secret(self):
 
192
        self.last_checker_failed = False
148
193
        self.logger(u'Client %s received its secret'
149
194
                    % self.properties[u"name"])
150
195
    
190
235
        # Rebuild focus and non-focus widgets using current properties
191
236
 
192
237
        # Base part of a client. Name!
193
 
        self._text = (u'%(name)s: '
 
238
        base = (u'%(name)s: '
194
239
                      % {u"name": self.properties[u"name"]})
195
 
 
196
 
        if self.properties[u"approved_pending"]:
 
240
        if not self.properties[u"enabled"]:
 
241
            message = u"DISABLED"
 
242
        elif self.properties[u"approved_pending"]:
197
243
            if self.properties[u"approved_by_default"]:
198
 
                self._text += u"Connection established to client. (d)eny?"
 
244
                message = u"Connection established to client. (d)eny?"
199
245
            else:
200
 
                self._text += u"Seeks approval to send secret. (a)pprove?"
 
246
                message = u"Seeks approval to send secret. (a)pprove?"
 
247
        elif self.last_checker_failed:
 
248
            timeout = datetime.timedelta(milliseconds
 
249
                                         = self.properties[u"timeout"])
 
250
            last_ok = isoformat_to_datetime(
 
251
                max((self.properties["last_checked_ok"]
 
252
                     or self.properties["created"]),
 
253
                    self.properties[u"last_enabled"]))
 
254
            timer = timeout - (datetime.datetime.utcnow() - last_ok)
 
255
            message = (u'A checker has failed! Time until client gets diabled: %s'
 
256
                           % unicode(timer).rsplit(".", 1)[0])
201
257
        else:
202
 
            self._text += (u'%(enabled)s'
203
 
                           % {u"enabled":
204
 
                               (u"enabled"
205
 
                                if self.properties[u"enabled"]
206
 
                                else u"DISABLED")})
 
258
            message = u"enabled"
 
259
        self._text = "%s%s" % (base, message)
 
260
            
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()
222
276
    
 
277
    def update_timer(self):
 
278
        "called by gobject"
 
279
        self.update()
 
280
        return True             # Keep calling this
 
281
    
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)
226
288
    
601
663
ui = UserInterface()
602
664
try:
603
665
    ui.run()
 
666
except KeyboardInterrupt:
 
667
    ui.screen.stop()
604
668
except Exception, e:
605
669
    ui.log_message(unicode(e))
606
670
    ui.screen.stop()