/mandos/trunk

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

« back to all changes in this revision

Viewing changes to mandos-monitor

  • Committer: Björn Påhlsson
  • Date: 2010-09-07 18:48:56 UTC
  • mto: (237.4.3 mandos-release)
  • mto: This revision was merged to the branch mainline in revision 421.
  • Revision ID: belorn@fukt.bsnet.se-20100907184856-waz6cvxbm7ranha2
added the actually plugin file for plymouth

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
57
73
        self.properties.update(
58
74
            self.proxy.GetAll(client_interface,
59
75
                              dbus_interface = dbus.PROPERTIES_IFACE))
60
 
        super(MandosClientPropertyCache, self).__init__(
61
 
            proxy_object=proxy_object, *args, **kwargs)
 
76
 
 
77
        #XXX This break good super behaviour!
 
78
#        super(MandosClientPropertyCache, self).__init__(
 
79
#            *args, **kwargs)
62
80
    
63
81
    def property_changed(self, property=None, value=None):
64
82
        """This is called whenever we get a PropertyChanged signal
83
101
        # Logger
84
102
        self.logger = logger
85
103
        
 
104
        self._update_timer_callback_tag = None
 
105
        self.last_checker_failed = False
 
106
        
86
107
        # The widget shown normally
87
108
        self._text_widget = urwid.Text(u"")
88
109
        # The widget shown when we have focus
104
125
                                     self.got_secret,
105
126
                                     client_interface,
106
127
                                     byte_arrays=True)
 
128
        self.proxy.connect_to_signal(u"NeedApproval",
 
129
                                     self.need_approval,
 
130
                                     client_interface,
 
131
                                     byte_arrays=True)
107
132
        self.proxy.connect_to_signal(u"Rejected",
108
133
                                     self.rejected,
109
134
                                     client_interface,
110
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))
111
150
    
112
151
    def checker_completed(self, exitstatus, condition, command):
113
152
        if exitstatus == 0:
 
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
114
157
            self.logger(u'Checker for client %s (command "%s")'
115
158
                        u' was successful'
116
159
                        % (self.properties[u"name"], command))
 
160
            self.update()
117
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))
118
168
        if os.WIFEXITED(condition):
119
169
            self.logger(u'Checker for client %s (command "%s")'
120
170
                        u' failed with exit code %s'
121
171
                        % (self.properties[u"name"], command,
122
172
                           os.WEXITSTATUS(condition)))
123
 
            return
124
 
        if os.WIFSIGNALED(condition):
 
173
        elif os.WIFSIGNALED(condition):
125
174
            self.logger(u'Checker for client %s (command "%s")'
126
175
                        u' was killed by signal %s'
127
176
                        % (self.properties[u"name"], command,
128
177
                           os.WTERMSIG(condition)))
129
 
            return
130
 
        if os.WCOREDUMP(condition):
 
178
        elif os.WCOREDUMP(condition):
131
179
            self.logger(u'Checker for client %s (command "%s")'
132
180
                        u' dumped core'
133
181
                        % (self.properties[u"name"], command))
134
 
        self.logger(u'Checker for client %s completed mysteriously')
 
182
        else:
 
183
            self.logger(u'Checker for client %s completed mysteriously')
 
184
        self.update()
135
185
    
136
186
    def checker_started(self, command):
137
 
        self.logger(u'Client %s started checker "%s"'
138
 
                    % (self.properties[u"name"], unicode(command)))
 
187
        #self.logger(u'Client %s started checker "%s"'
 
188
        #            % (self.properties[u"name"], unicode(command)))
 
189
        pass
139
190
    
140
191
    def got_secret(self):
141
192
        self.logger(u'Client %s received its secret'
142
193
                    % self.properties[u"name"])
143
194
    
144
 
    def rejected(self):
145
 
        self.logger(u'Client %s was rejected'
146
 
                    % self.properties[u"name"])
 
195
    def need_approval(self, timeout, default):
 
196
        if not default:
 
197
            message = u'Client %s needs approval within %s seconds'
 
198
        else:
 
199
            message = u'Client %s will get its secret in %s seconds'
 
200
        self.logger(message
 
201
                    % (self.properties[u"name"], timeout/1000))
 
202
    
 
203
    def rejected(self, reason):
 
204
        self.logger(u'Client %s was rejected; reason: %s'
 
205
                    % (self.properties[u"name"], reason))
147
206
    
148
207
    def selectable(self):
149
208
        """Make this a "selectable" widget.
171
230
                          u"bold-underline-blink":
172
231
                              u"bold-underline-blink-standout",
173
232
                          }
174
 
        
 
233
 
175
234
        # Rebuild focus and non-focus widgets using current properties
176
 
        self._text = (u'%(name)s: %(enabled)s'
177
 
                      % { u"name": self.properties[u"name"],
178
 
                          u"enabled":
179
 
                              (u"enabled"
180
 
                               if self.properties[u"enabled"]
181
 
                               else u"DISABLED")})
 
235
 
 
236
        # Base part of a client. Name!
 
237
        base = (u'%(name)s: '
 
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)
 
249
 
 
250
            message = (u'A checker has failed! Time until client gets diabled: %s'
 
251
                           % unicode(timer))
 
252
        elif self.properties[u"approved_pending"]:
 
253
            if self.properties[u"approved_by_default"]:
 
254
                message = u"Connection established to client. (d)eny?"
 
255
            else:
 
256
                message = u"Seeks approval to send secret. (a)pprove?"
 
257
        else:
 
258
            message = u"enabled"
 
259
        self._text = "%s%s" % (base, message)
 
260
            
182
261
        if not urwid.supports_unicode():
183
262
            self._text = self._text.encode("ascii", "replace")
184
263
        textlist = [(u"normal", self._text)]
195
274
        if self.update_hook is not None:
196
275
            self.update_hook()
197
276
    
 
277
    def update_timer(self):
 
278
        "called by gobject"
 
279
        self.update()
 
280
        return True             # Keep calling this
 
281
    
198
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
199
286
        if self.delete_hook is not None:
200
287
            self.delete_hook(self)
201
288
    
208
295
    def keypress(self, (maxcol,), key):
209
296
        """Handle keys.
210
297
        This overrides the method from urwid.FlowWidget"""
211
 
        if key == u"e" or key == u"+":
212
 
            self.proxy.Enable()
213
 
        elif key == u"d" or key == u"-":
214
 
            self.proxy.Disable()
 
298
        if key == u"+":
 
299
            self.proxy.Enable(dbus_interface = client_interface)
 
300
        elif key == u"-":
 
301
            self.proxy.Disable(dbus_interface = client_interface)
 
302
        elif key == u"a":
 
303
            self.proxy.Approve(dbus.Boolean(True, variant_level=1),
 
304
                               dbus_interface = client_interface)
 
305
        elif key == u"d":
 
306
            self.proxy.Approve(dbus.Boolean(False, variant_level=1),
 
307
                                  dbus_interface = client_interface)
215
308
        elif key == u"r" or key == u"_" or key == u"ctrl k":
216
309
            self.server_proxy_object.RemoveClient(self.proxy
217
310
                                                  .object_path)
218
311
        elif key == u"s":
219
 
            self.proxy.StartChecker()
 
312
            self.proxy.StartChecker(dbus_interface = client_interface)
220
313
        elif key == u"S":
221
 
            self.proxy.StopChecker()
 
314
            self.proxy.StopChecker(dbus_interface = client_interface)
222
315
        elif key == u"C":
223
 
            self.proxy.CheckedOK()
 
316
            self.proxy.CheckedOK(dbus_interface = client_interface)
224
317
        # xxx
225
318
#         elif key == u"p" or key == "=":
226
319
#             self.proxy.pause()
228
321
#             self.proxy.unpause()
229
322
#         elif key == u"RET":
230
323
#             self.open()
 
324
#        elif key == u"+":
 
325
#            self.proxy.Approve(True)
 
326
#        elif key == u"-":
 
327
#            self.proxy.Approve(False)
231
328
        else:
232
329
            return key
233
330
    
525
622
                self.log_message_raw((u"bold",
526
623
                                      u"  "
527
624
                                      .join((u"Clients:",
528
 
                                             u"e: Enable",
529
 
                                             u"d: Disable",
 
625
                                             u"+: Enable",
 
626
                                             u"-: Disable",
530
627
                                             u"r: Remove",
531
628
                                             u"s: Start new checker",
532
629
                                             u"S: Stop checker",
533
 
                                             u"C: Checker OK"))))
 
630
                                             u"C: Checker OK",
 
631
                                             u"a: Approve",
 
632
                                             u"d: Deny"))))
534
633
                self.refresh()
535
634
            elif key == u"tab":
536
635
                if self.topwidget.get_focus() is self.logbox: