/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 Hogeborn
  • Date: 2009-11-09 05:10:35 UTC
  • Revision ID: teddy@fukt.bsnet.se-20091109051035-zcmvuhioo9gjs0cb
* mandos-monitor (MandosClientPropertyCache): Remove conversion of
                                              D-Bus types to native
                                              Python types.
  (MandosClientPropertyCache.type_map): Removed attribute.
  (MandosClientPropertyCache.conversion): Removed method.
  (ConstrainedListBox): New; a ListBox which will keep the focus.
  (UserInterface.__init__): Take new argument "max_log_length".
  (UserInterface.divider, UserInterface.log,
  UserInterface.max_log_length, UserInterface.logbox,
  UserInterface.log_visible, UserInterface.log_wrap): New attributes.
  (UserInterface.rebuild, UserInterface.log_message,
  UserInterface.toggle_log_display, UserInterface.change_log_display):
  New methods.
  (UserInterface.remove_client): Bug fix: Eliminate double client
                                 listing when an empty list gets a
                                 ClientAdded signal.
  (UserInterface.process_input): Add more key translations.

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
from __future__ import division, absolute_import, with_statement
5
5
 
6
6
import sys
7
 
import os
8
7
import signal
9
8
 
10
 
import datetime
11
 
 
12
9
import urwid.curses_display
13
10
import urwid
14
11
 
19
16
 
20
17
import UserList
21
18
 
22
 
import locale
23
 
 
24
 
locale.setlocale(locale.LC_ALL, u'')
25
 
 
26
19
# Some useful constants
27
20
domain = 'se.bsnet.fukt'
28
21
server_interface = domain + '.Mandos'
42
35
    properties and calls a hook function when any of them are
43
36
    changed.
44
37
    """
45
 
    def __init__(self, proxy_object=None, *args, **kwargs):
 
38
    def __init__(self, proxy_object=None, properties=None, *args,
 
39
                 **kwargs):
46
40
        self.proxy = proxy_object # Mandos Client proxy object
47
41
        
48
 
        self.properties = dict()
49
 
        self.proxy.connect_to_signal(u"PropertyChanged",
 
42
        if properties is None:
 
43
            self.properties = dict()
 
44
        else:
 
45
            self.properties = properties
 
46
        self.proxy.connect_to_signal("PropertyChanged",
50
47
                                     self.property_changed,
51
48
                                     client_interface,
52
49
                                     byte_arrays=True)
53
 
 
54
 
        self.properties.update(
55
 
            self.proxy.GetAll(client_interface,
56
 
                              dbus_interface = dbus.PROPERTIES_IFACE))
 
50
        
 
51
        if properties is None:
 
52
            self.properties.update(self.proxy.GetAll(client_interface,
 
53
                                                     dbus_interface =
 
54
                                                     dbus.PROPERTIES_IFACE))
57
55
        super(MandosClientPropertyCache, self).__init__(
58
 
            proxy_object=proxy_object, *args, **kwargs)
 
56
            proxy_object=proxy_object,
 
57
            properties=properties, *args, **kwargs)
59
58
    
60
59
    def property_changed(self, property=None, value=None):
61
60
        """This is called whenever we get a PropertyChanged signal
70
69
    """
71
70
    
72
71
    def __init__(self, server_proxy_object=None, update_hook=None,
73
 
                 delete_hook=None, logger=None, *args, **kwargs):
 
72
                 delete_hook=None, *args, **kwargs):
74
73
        # Called on update
75
74
        self.update_hook = update_hook
76
75
        # Called on delete
77
76
        self.delete_hook = delete_hook
78
77
        # Mandos Server proxy object
79
78
        self.server_proxy_object = server_proxy_object
80
 
        # Logger
81
 
        self.logger = logger
82
79
        
83
80
        # The widget shown normally
84
 
        self._text_widget = urwid.Text(u"")
 
81
        self._text_widget = urwid.Text("")
85
82
        # The widget shown when we have focus
86
 
        self._focus_text_widget = urwid.Text(u"")
 
83
        self._focus_text_widget = urwid.Text("")
87
84
        super(MandosClientWidget, self).__init__(
88
85
            update_hook=update_hook, delete_hook=delete_hook,
89
86
            *args, **kwargs)
90
87
        self.update()
91
88
        self.opened = False
92
 
        self.proxy.connect_to_signal(u"CheckerCompleted",
93
 
                                     self.checker_completed,
94
 
                                     client_interface,
95
 
                                     byte_arrays=True)
96
 
        self.proxy.connect_to_signal(u"CheckerStarted",
97
 
                                     self.checker_started,
98
 
                                     client_interface,
99
 
                                     byte_arrays=True)
100
 
        self.proxy.connect_to_signal(u"GotSecret",
101
 
                                     self.got_secret,
102
 
                                     client_interface,
103
 
                                     byte_arrays=True)
104
 
        self.proxy.connect_to_signal(u"Rejected",
105
 
                                     self.rejected,
106
 
                                     client_interface,
107
 
                                     byte_arrays=True)
108
 
    
109
 
    def checker_completed(self, exitstatus, condition, command):
110
 
        if exitstatus == 0:
111
 
            self.logger(u'Checker for client %s (command "%s")'
112
 
                        u' was successful'
113
 
                        % (self.properties[u"name"], command))
114
 
            return
115
 
        if os.WIFEXITED(condition):
116
 
            self.logger(u'Checker for client %s (command "%s")'
117
 
                        u' failed with exit code %s'
118
 
                        % (self.properties[u"name"], command,
119
 
                           os.WEXITSTATUS(condition)))
120
 
            return
121
 
        if os.WIFSIGNALED(condition):
122
 
            self.logger(u'Checker for client %s (command "%s")'
123
 
                        u' was killed by signal %s'
124
 
                        % (self.properties[u"name"], command,
125
 
                           os.WTERMSIG(condition)))
126
 
            return
127
 
        if os.WCOREDUMP(condition):
128
 
            self.logger(u'Checker for client %s (command "%s")'
129
 
                        u' dumped core'
130
 
                        % (self.properties[u"name"], command))
131
 
        self.logger(u'Checker for client %s completed mysteriously')
132
 
    
133
 
    def checker_started(self, command):
134
 
        self.logger(u'Client %s started checker "%s"'
135
 
                    % (self.properties[u"name"], unicode(command)))
136
 
    
137
 
    def got_secret(self):
138
 
        self.logger(u'Client %s received its secret'
139
 
                    % self.properties[u"name"])
140
 
    
141
 
    def rejected(self):
142
 
        self.logger(u'Client %s was rejected'
143
 
                    % self.properties[u"name"])
144
89
    
145
90
    def selectable(self):
146
91
        """Make this a "selectable" widget.
170
115
                          }
171
116
        
172
117
        # Rebuild focus and non-focus widgets using current properties
173
 
        self._text = (u'%(name)s: %(enabled)s'
174
 
                      % { u"name": self.properties[u"name"],
175
 
                          u"enabled":
176
 
                              (u"enabled"
177
 
                               if self.properties[u"enabled"]
178
 
                               else u"DISABLED")})
 
118
        self._text = (u'name="%(name)s", enabled=%(enabled)s'
 
119
                      % self.properties)
179
120
        if not urwid.supports_unicode():
180
121
            self._text = self._text.encode("ascii", "replace")
181
 
        textlist = [(u"normal", self._text)]
 
122
        textlist = [(u"normal", u"BLARGH: "), (u"bold", self._text)]
182
123
        self._text_widget.set_text(textlist)
183
124
        self._focus_text_widget.set_text([(with_standout[text[0]],
184
125
                                           text[1])
209
150
            self.proxy.Enable()
210
151
        elif key == u"d" or key == u"-":
211
152
            self.proxy.Disable()
212
 
        elif key == u"r" or key == u"_" or key == u"ctrl k":
 
153
        elif key == u"r" or key == u"_":
213
154
            self.server_proxy_object.RemoveClient(self.proxy
214
155
                                                  .object_path)
215
156
        elif key == u"s":
216
157
            self.proxy.StartChecker()
 
158
        elif key == u"c":
 
159
            self.proxy.StopChecker()
217
160
        elif key == u"S":
218
 
            self.proxy.StopChecker()
219
 
        elif key == u"C":
220
161
            self.proxy.CheckedOK()
221
162
        # xxx
222
163
#         elif key == u"p" or key == "=":
282
223
                ))
283
224
        
284
225
        if urwid.supports_unicode():
285
 
            self.divider = u"─" # \u2500
286
 
            #self.divider = u"━" # \u2501
 
226
            #self.divider = u"─" # \u2500
 
227
            self.divider = u"━" # \u2501
287
228
        else:
288
229
            #self.divider = u"-" # \u002d
289
230
            self.divider = u"_" # \u005f
309
250
        self.log_wrap = u"any"
310
251
        
311
252
        self.rebuild()
312
 
        self.log_message_raw((u"bold",
313
 
                              u"Mandos Monitor version " + version))
314
 
        self.log_message_raw((u"bold",
315
 
                              u"q: Quit  ?: Help"))
 
253
        self.log_message(u"Message")
 
254
        self.log_message(u"Message0 Message1 Message2 Message3 Message4 Message5 Message6 Message7 Message8 Message9")
 
255
        self.log_message(u"Message10 Message11 Message12 Message13 Message14 Message15 Message16 Message17 Message18 Message19")
 
256
        self.log_message(u"Message20 Message21 Message22 Message23 Message24 Message25 Message26 Message27 Message28 Message29")
316
257
        
317
258
        self.busname = domain + '.Mandos'
318
259
        self.main_loop = gobject.MainLoop()
329
270
            mandos_clients = dbus.Dictionary()
330
271
        
331
272
        (self.mandos_serv
332
 
         .connect_to_signal(u"ClientRemoved",
 
273
         .connect_to_signal("ClientRemoved",
333
274
                            self.find_and_remove_client,
334
275
                            dbus_interface=server_interface,
335
276
                            byte_arrays=True))
336
277
        (self.mandos_serv
337
 
         .connect_to_signal(u"ClientAdded",
 
278
         .connect_to_signal("ClientAdded",
338
279
                            self.add_new_client,
339
280
                            dbus_interface=server_interface,
340
281
                            byte_arrays=True))
341
 
        (self.mandos_serv
342
 
         .connect_to_signal(u"ClientNotFound",
343
 
                            self.client_not_found,
344
 
                            dbus_interface=server_interface,
345
 
                            byte_arrays=True))
346
282
        for path, client in mandos_clients.iteritems():
347
283
            client_proxy_object = self.bus.get_object(self.busname,
348
284
                                                      path)
354
290
                                               update_hook
355
291
                                               =self.refresh,
356
292
                                               delete_hook
357
 
                                               =self.remove_client,
358
 
                                               logger
359
 
                                               =self.log_message),
 
293
                                               =self.remove_client),
360
294
                            path=path)
361
295
    
362
 
    def client_not_found(self, fingerprint, address):
363
 
        self.log_message((u"Client with address %s and fingerprint %s"
364
 
                          u" could not be found" % (address,
365
 
                                                    fingerprint)))
366
 
    
367
296
    def rebuild(self):
368
297
        """This rebuilds the User Interface.
369
298
        Call this when the widget layout needs to change"""
378
307
            pass
379
308
        self.topwidget = urwid.Pile(self.uilist)
380
309
    
381
 
    def log_message(self, message):
382
 
        timestamp = datetime.datetime.now().isoformat()
383
 
        self.log_message_raw(timestamp + u": " + message)
384
 
    
385
 
    def log_message_raw(self, markup):
 
310
    def log_message(self, markup):
386
311
        """Add a log message to the log buffer."""
387
312
        self.log.append(urwid.Text(markup, wrap=self.log_wrap))
388
313
        if (self.max_log_length
389
314
            and len(self.log) > self.max_log_length):
390
315
            del self.log[0:len(self.log)-self.max_log_length-1]
391
 
        self.logbox.set_focus(len(self.logbox.body.contents),
392
 
                              coming_from=u"above")
393
 
        self.refresh()
394
316
    
395
317
    def toggle_log_display(self):
396
318
        """Toggle visibility of the log buffer."""
422
344
            return
423
345
        self.remove_client(client, path)
424
346
    
425
 
    def add_new_client(self, path):
 
347
    def add_new_client(self, path, properties):
426
348
        client_proxy_object = self.bus.get_object(self.busname, path)
427
349
        self.add_client(MandosClientWidget(server_proxy_object
428
350
                                           =self.mandos_serv,
429
351
                                           proxy_object
430
352
                                           =client_proxy_object,
 
353
                                           properties=properties,
431
354
                                           update_hook
432
355
                                           =self.refresh,
433
356
                                           delete_hook
434
 
                                           =self.remove_client,
435
 
                                           logger
436
 
                                           =self.log_message),
 
357
                                           =self.remove_client),
437
358
                        path=path)
438
359
    
439
360
    def add_client(self, client, path=None):
508
429
            elif key == u"w" or key == u"i":
509
430
                self.change_log_display()
510
431
                self.refresh()
511
 
            elif key == u"?" or key == u"f1" or key == u"esc":
512
 
                if not self.log_visible:
513
 
                    self.log_visible = True
514
 
                    self.rebuild()
515
 
                self.log_message_raw((u"bold",
516
 
                                      u"  ".
517
 
                                      join((u"q: Quit",
518
 
                                            u"?: Help",
519
 
                                            u"l: Log window toggle",
520
 
                                            u"TAB: Switch window",
521
 
                                            u"w: Wrap (log)"))))
522
 
                self.log_message_raw((u"bold",
523
 
                                      u"  "
524
 
                                      .join((u"Clients:",
525
 
                                             u"e: Enable",
526
 
                                             u"d: Disable",
527
 
                                             u"r: Remove",
528
 
                                             u"s: Start new checker",
529
 
                                             u"S: Stop checker",
530
 
                                             u"C: Checker OK"))))
 
432
            elif key == u"?" or key == u"f1":
 
433
                self.log_message(u"Help!")
531
434
                self.refresh()
532
435
            elif key == u"tab":
533
436
                if self.topwidget.get_focus() is self.logbox:
535
438
                else:
536
439
                    self.topwidget.set_focus(self.logbox)
537
440
                self.refresh()
538
 
            #elif (key == u"end" or key == u"meta >" or key == u"G"
539
 
            #      or key == u">"):
540
 
            #    pass            # xxx end-of-buffer
541
 
            #elif (key == u"home" or key == u"meta <" or key == u"g"
542
 
            #      or key == u"<"):
543
 
            #    pass            # xxx beginning-of-buffer
544
 
            #elif key == u"ctrl e" or key == u"$":
545
 
            #    pass            # xxx move-end-of-line
546
 
            #elif key == u"ctrl a" or key == u"^":
547
 
            #    pass            # xxx move-beginning-of-line
548
 
            #elif key == u"ctrl b" or key == u"meta (" or key == u"h":
549
 
            #    pass            # xxx left
550
 
            #elif key == u"ctrl f" or key == u"meta )" or key == u"l":
551
 
            #    pass            # xxx right
552
 
            #elif key == u"a":
553
 
            #    pass            # scroll up log
554
 
            #elif key == u"z":
555
 
            #    pass            # scroll down log
 
441
            elif (key == u"end" or key == u"meta >" or key == u"G"
 
442
                  or key == u">"):
 
443
                pass            # xxx end-of-buffer
 
444
            elif (key == u"home" or key == u"meta <" or key == u"g"
 
445
                  or key == u"<"):
 
446
                pass            # xxx beginning-of-buffer
 
447
            elif key == u"ctrl e" or key == u"$":
 
448
                pass            # xxx move-end-of-line
 
449
            elif key == u"ctrl a" or key == u"^":
 
450
                pass            # xxx move-beginning-of-line
 
451
            elif key == u"ctrl b" or key == u"meta (" or key == u"h":
 
452
                pass            # xxx left
 
453
            elif key == u"ctrl f" or key == u"meta )" or key == u"l":
 
454
                pass            # xxx right
 
455
            elif key == u"a":
 
456
                pass            # scroll up log
 
457
            elif key == u"z":
 
458
                pass            # scroll down log
556
459
            elif self.topwidget.selectable():
557
460
                self.topwidget.keypress(self.size, key)
558
461
                self.refresh()
561
464
ui = UserInterface()
562
465
try:
563
466
    ui.run()
564
 
except Exception, e:
565
 
    ui.log_message(unicode(e))
 
467
except:
566
468
    ui.screen.stop()
567
469
    raise