/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-09-09 18:16:14 UTC
  • mfrom: (237.2.35 mandos-empty-device)
  • Revision ID: teddy@fukt.bsnet.se-20100909181614-oanlmvkzsiodbo3c
Merge in branch to interpret an empty device name to mean
"autodetect".

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 
24
24
locale.setlocale(locale.LC_ALL, u'')
25
25
 
26
 
import logging
27
 
logging.getLogger('dbus.proxies').setLevel(logging.CRITICAL)
28
 
 
29
26
# Some useful constants
30
27
domain = 'se.bsnet.fukt'
31
28
server_interface = domain + '.Mandos'
32
29
client_interface = domain + '.Mandos.Client'
33
 
version = "1.0.15"
 
30
version = "1.0.14"
34
31
 
35
32
# Always run in monochrome mode
36
33
urwid.curses_display.curses.has_colors = lambda : False
73
70
        self.properties.update(
74
71
            self.proxy.GetAll(client_interface,
75
72
                              dbus_interface = dbus.PROPERTIES_IFACE))
76
 
 
77
 
        #XXX This break good super behaviour!
78
 
#        super(MandosClientPropertyCache, self).__init__(
79
 
#            *args, **kwargs)
 
73
        super(MandosClientPropertyCache, self).__init__(
 
74
            proxy_object=proxy_object, *args, **kwargs)
80
75
    
81
76
    def property_changed(self, property=None, value=None):
82
77
        """This is called whenever we get a PropertyChanged signal
125
120
                                     self.got_secret,
126
121
                                     client_interface,
127
122
                                     byte_arrays=True)
128
 
        self.proxy.connect_to_signal(u"NeedApproval",
129
 
                                     self.need_approval,
130
 
                                     client_interface,
131
 
                                     byte_arrays=True)
132
123
        self.proxy.connect_to_signal(u"Rejected",
133
124
                                     self.rejected,
134
125
                                     client_interface,
135
126
                                     byte_arrays=True)
136
127
        last_checked_ok = isoformat_to_datetime(self.properties
137
 
                                                [u"LastCheckedOK"])
 
128
                                                ["last_checked_ok"])
138
129
        if last_checked_ok is None:
139
130
            self.last_checker_failed = True
140
131
        else:
142
133
                                         - last_checked_ok)
143
134
                                        > datetime.timedelta
144
135
                                        (milliseconds=
145
 
                                         self.properties
146
 
                                         [u"Interval"]))
 
136
                                         self.properties["interval"]))
147
137
        if self.last_checker_failed:
148
138
            self._update_timer_callback_tag = (gobject.timeout_add
149
139
                                               (1000,
157
147
                self._update_timer_callback_tag = None
158
148
            self.logger(u'Checker for client %s (command "%s")'
159
149
                        u' was successful'
160
 
                        % (self.properties[u"Name"], command))
 
150
                        % (self.properties[u"name"], command))
161
151
            self.update()
162
152
            return
163
153
        # Checker failed
169
159
        if os.WIFEXITED(condition):
170
160
            self.logger(u'Checker for client %s (command "%s")'
171
161
                        u' failed with exit code %s'
172
 
                        % (self.properties[u"Name"], command,
 
162
                        % (self.properties[u"name"], command,
173
163
                           os.WEXITSTATUS(condition)))
174
164
        elif os.WIFSIGNALED(condition):
175
165
            self.logger(u'Checker for client %s (command "%s")'
176
166
                        u' was killed by signal %s'
177
 
                        % (self.properties[u"Name"], command,
 
167
                        % (self.properties[u"name"], command,
178
168
                           os.WTERMSIG(condition)))
179
169
        elif os.WCOREDUMP(condition):
180
170
            self.logger(u'Checker for client %s (command "%s")'
181
171
                        u' dumped core'
182
 
                        % (self.properties[u"Name"], command))
 
172
                        % (self.properties[u"name"], command))
183
173
        else:
184
 
            self.logger(u'Checker for client %s completed'
185
 
                        u' mysteriously')
 
174
            self.logger(u'Checker for client %s completed mysteriously')
186
175
        self.update()
187
176
    
188
177
    def checker_started(self, command):
189
 
        #self.logger(u'Client %s started checker "%s"'
190
 
        #            % (self.properties[u"Name"], unicode(command)))
191
 
        pass
 
178
        self.logger(u'Client %s started checker "%s"'
 
179
                    % (self.properties[u"name"], unicode(command)))
192
180
    
193
181
    def got_secret(self):
194
 
        self.last_checker_failed = False
195
182
        self.logger(u'Client %s received its secret'
196
 
                    % self.properties[u"Name"])
197
 
    
198
 
    def need_approval(self, timeout, default):
199
 
        if not default:
200
 
            message = u'Client %s needs approval within %s seconds'
201
 
        else:
202
 
            message = u'Client %s will get its secret in %s seconds'
203
 
        self.logger(message
204
 
                    % (self.properties[u"Name"], timeout/1000))
205
 
    
206
 
    def rejected(self, reason):
207
 
        self.logger(u'Client %s was rejected; reason: %s'
208
 
                    % (self.properties[u"Name"], reason))
 
183
                    % self.properties[u"name"])
 
184
    
 
185
    def rejected(self):
 
186
        self.logger(u'Client %s was rejected'
 
187
                    % self.properties[u"name"])
209
188
    
210
189
    def selectable(self):
211
190
        """Make this a "selectable" widget.
233
212
                          u"bold-underline-blink":
234
213
                              u"bold-underline-blink-standout",
235
214
                          }
236
 
 
 
215
        
237
216
        # Rebuild focus and non-focus widgets using current properties
238
 
 
239
 
        # 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?"
247
 
            else:
248
 
                message = u"Seeks approval to send secret. (a)pprove?"
249
 
        elif self.last_checker_failed:
250
 
            timeout = datetime.timedelta(milliseconds
251
 
                                         = self.properties
252
 
                                         [u"Timeout"])
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'
259
 
                       u' gets diabled: %s'
260
 
                           % unicode(timer).rsplit(".", 1)[0])
261
 
        else:
262
 
            message = u"enabled"
263
 
        self._text = "%s%s" % (base, message)
264
 
            
 
217
        self._text = (u'%(name)s: %(enabled)s%(timer)s'
 
218
                      % { u"name": self.properties[u"name"],
 
219
                          u"enabled":
 
220
                              (u"enabled"
 
221
                               if self.properties[u"enabled"]
 
222
                               else u"DISABLED"),
 
223
                          u"timer": (unicode(datetime.timedelta
 
224
                                             (milliseconds =
 
225
                                              self.properties
 
226
                                              [u"timeout"])
 
227
                                             - (datetime.datetime
 
228
                                                .utcnow()
 
229
                                                - isoformat_to_datetime
 
230
                                                (max((self.properties
 
231
                                                 ["last_checked_ok"]
 
232
                                                 or
 
233
                                                 self.properties
 
234
                                                 ["created"]),
 
235
                                                    self.properties[u"last_enabled"]))))
 
236
                                     if (self.last_checker_failed
 
237
                                         and self.properties
 
238
                                         [u"enabled"])
 
239
                                     else u"")})
265
240
        if not urwid.supports_unicode():
266
241
            self._text = self._text.encode("ascii", "replace")
267
242
        textlist = [(u"normal", self._text)]
299
274
    def keypress(self, (maxcol,), key):
300
275
        """Handle keys.
301
276
        This overrides the method from urwid.FlowWidget"""
302
 
        if key == u"+":
303
 
            self.proxy.Enable(dbus_interface = client_interface)
304
 
        elif key == u"-":
305
 
            self.proxy.Disable(dbus_interface = client_interface)
306
 
        elif key == u"a":
307
 
            self.proxy.Approve(dbus.Boolean(True, variant_level=1),
308
 
                               dbus_interface = client_interface)
309
 
        elif key == u"d":
310
 
            self.proxy.Approve(dbus.Boolean(False, variant_level=1),
311
 
                                  dbus_interface = client_interface)
 
277
        if key == u"e" or key == u"+":
 
278
            self.proxy.Enable()
 
279
        elif key == u"d" or key == u"-":
 
280
            self.proxy.Disable()
312
281
        elif key == u"r" or key == u"_" or key == u"ctrl k":
313
282
            self.server_proxy_object.RemoveClient(self.proxy
314
283
                                                  .object_path)
315
284
        elif key == u"s":
316
 
            self.proxy.StartChecker(dbus_interface = client_interface)
 
285
            self.proxy.StartChecker()
317
286
        elif key == u"S":
318
 
            self.proxy.StopChecker(dbus_interface = client_interface)
 
287
            self.proxy.StopChecker()
319
288
        elif key == u"C":
320
 
            self.proxy.CheckedOK(dbus_interface = client_interface)
 
289
            self.proxy.CheckedOK()
321
290
        # xxx
322
291
#         elif key == u"p" or key == "=":
323
292
#             self.proxy.pause()
325
294
#             self.proxy.unpause()
326
295
#         elif key == u"RET":
327
296
#             self.open()
328
 
#        elif key == u"+":
329
 
#            self.proxy.Approve(True)
330
 
#        elif key == u"-":
331
 
#            self.proxy.Approve(False)
332
297
        else:
333
298
            return key
334
299
    
350
315
    use them as an excuse to shift focus away from this widget.
351
316
    """
352
317
    def keypress(self, (maxcol, maxrow), key):
353
 
        ret = super(ConstrainedListBox, self).keypress((maxcol,
354
 
                                                        maxrow), key)
 
318
        ret = super(ConstrainedListBox, self).keypress((maxcol, maxrow), key)
355
319
        if ret in (u"up", u"down"):
356
320
            return
357
321
        return ret
474
438
        Call this when the widget layout needs to change"""
475
439
        self.uilist = []
476
440
        #self.uilist.append(urwid.ListBox(self.clients))
477
 
        self.uilist.append(urwid.Frame(ConstrainedListBox(self.
478
 
                                                          clients),
 
441
        self.uilist.append(urwid.Frame(ConstrainedListBox(self.clients),
479
442
                                       #header=urwid.Divider(),
480
443
                                       header=None,
481
 
                                       footer=
482
 
                                       urwid.Divider(div_char=
483
 
                                                     self.divider)))
 
444
                                       footer=urwid.Divider(div_char=self.divider)))
484
445
        if self.log_visible:
485
446
            self.uilist.append(self.logbox)
486
447
            pass
549
510
        if path is None:
550
511
            path = client.proxy.object_path
551
512
        self.clients_dict[path] = client
552
 
        self.clients.sort(None, lambda c: c.properties[u"Name"])
 
513
        self.clients.sort(None, lambda c: c.properties[u"name"])
553
514
        self.refresh()
554
515
    
555
516
    def remove_client(self, client, path=None):
630
591
                self.log_message_raw((u"bold",
631
592
                                      u"  "
632
593
                                      .join((u"Clients:",
633
 
                                             u"+: Enable",
634
 
                                             u"-: Disable",
 
594
                                             u"e: Enable",
 
595
                                             u"d: Disable",
635
596
                                             u"r: Remove",
636
597
                                             u"s: Start new checker",
637
598
                                             u"S: Stop checker",
638
 
                                             u"C: Checker OK",
639
 
                                             u"a: Approve",
640
 
                                             u"d: Deny"))))
 
599
                                             u"C: Checker OK"))))
641
600
                self.refresh()
642
601
            elif key == u"tab":
643
602
                if self.topwidget.get_focus() is self.logbox:
671
630
ui = UserInterface()
672
631
try:
673
632
    ui.run()
674
 
except KeyboardInterrupt:
675
 
    ui.screen.stop()
676
633
except Exception, e:
677
634
    ui.log_message(unicode(e))
678
635
    ui.screen.stop()