/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: teddy at bsnet
  • Date: 2010-08-24 18:18:01 UTC
  • mto: (24.1.154 mandos)
  • mto: This revision was merged to the branch mainline in revision 421.
  • Revision ID: teddy@fukt.bsnet.se-20100824181801-n798wxs117trv8iu
* mandos: Use logging.getLogger() as in the documentation.
* mandos-monitor: Suppress logging from dbus.proxies.

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