/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

  • Committer: Björn Påhlsson
  • Date: 2010-09-01 13:04:23 UTC
  • mto: (237.7.1 mandos)
  • mto: This revision was merged to the branch mainline in revision 270.
  • Revision ID: belorn@fukt.bsnet.se-20100901130423-bkn4j4vukasa1d27
New graphical boot supported: Plymouth

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
7
8
import signal
8
9
 
 
10
import datetime
 
11
 
9
12
import urwid.curses_display
10
13
import urwid
11
14
 
16
19
 
17
20
import UserList
18
21
 
 
22
import locale
 
23
 
 
24
locale.setlocale(locale.LC_ALL, u'')
 
25
 
19
26
# Some useful constants
20
27
domain = 'se.bsnet.fukt'
21
28
server_interface = domain + '.Mandos'
35
42
    properties and calls a hook function when any of them are
36
43
    changed.
37
44
    """
38
 
    def __init__(self, proxy_object=None, properties=None, *args,
39
 
                 **kwargs):
 
45
    def __init__(self, proxy_object=None, *args, **kwargs):
40
46
        self.proxy = proxy_object # Mandos Client proxy object
41
47
        
42
 
        if properties is None:
43
 
            self.properties = dict()
44
 
        else:
45
 
            self.properties = properties
46
 
        self.proxy.connect_to_signal("PropertyChanged",
 
48
        self.properties = dict()
 
49
        self.proxy.connect_to_signal(u"PropertyChanged",
47
50
                                     self.property_changed,
48
51
                                     client_interface,
49
52
                                     byte_arrays=True)
50
 
        
51
 
        if properties is None:
52
 
            self.properties.update(self.proxy.GetAll(client_interface,
53
 
                                                     dbus_interface =
54
 
                                                     dbus.PROPERTIES_IFACE))
 
53
 
 
54
        self.properties.update(
 
55
            self.proxy.GetAll(client_interface,
 
56
                              dbus_interface = dbus.PROPERTIES_IFACE))
55
57
        super(MandosClientPropertyCache, self).__init__(
56
 
            proxy_object=proxy_object,
57
 
            properties=properties, *args, **kwargs)
 
58
            proxy_object=proxy_object, *args, **kwargs)
58
59
    
59
60
    def property_changed(self, property=None, value=None):
60
61
        """This is called whenever we get a PropertyChanged signal
69
70
    """
70
71
    
71
72
    def __init__(self, server_proxy_object=None, update_hook=None,
72
 
                 delete_hook=None, *args, **kwargs):
 
73
                 delete_hook=None, logger=None, *args, **kwargs):
73
74
        # Called on update
74
75
        self.update_hook = update_hook
75
76
        # Called on delete
76
77
        self.delete_hook = delete_hook
77
78
        # Mandos Server proxy object
78
79
        self.server_proxy_object = server_proxy_object
 
80
        # Logger
 
81
        self.logger = logger
79
82
        
80
83
        # The widget shown normally
81
 
        self._text_widget = urwid.Text("")
 
84
        self._text_widget = urwid.Text(u"")
82
85
        # The widget shown when we have focus
83
 
        self._focus_text_widget = urwid.Text("")
 
86
        self._focus_text_widget = urwid.Text(u"")
84
87
        super(MandosClientWidget, self).__init__(
85
88
            update_hook=update_hook, delete_hook=delete_hook,
86
89
            *args, **kwargs)
87
90
        self.update()
88
91
        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"])
89
144
    
90
145
    def selectable(self):
91
146
        """Make this a "selectable" widget.
115
170
                          }
116
171
        
117
172
        # Rebuild focus and non-focus widgets using current properties
118
 
        self._text = (u'name="%(name)s", enabled=%(enabled)s'
119
 
                      % self.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")})
120
179
        if not urwid.supports_unicode():
121
180
            self._text = self._text.encode("ascii", "replace")
122
 
        textlist = [(u"normal", u"BLARGH: "), (u"bold", self._text)]
 
181
        textlist = [(u"normal", self._text)]
123
182
        self._text_widget.set_text(textlist)
124
183
        self._focus_text_widget.set_text([(with_standout[text[0]],
125
184
                                           text[1])
150
209
            self.proxy.Enable()
151
210
        elif key == u"d" or key == u"-":
152
211
            self.proxy.Disable()
153
 
        elif key == u"r" or key == u"_":
 
212
        elif key == u"r" or key == u"_" or key == u"ctrl k":
154
213
            self.server_proxy_object.RemoveClient(self.proxy
155
214
                                                  .object_path)
156
215
        elif key == u"s":
223
282
                ))
224
283
        
225
284
        if urwid.supports_unicode():
226
 
            #self.divider = u"─" # \u2500
227
 
            self.divider = u"━" # \u2501
 
285
            self.divider = u"─" # \u2500
 
286
            #self.divider = u"━" # \u2501
228
287
        else:
229
288
            #self.divider = u"-" # \u002d
230
289
            self.divider = u"_" # \u005f
250
309
        self.log_wrap = u"any"
251
310
        
252
311
        self.rebuild()
253
 
        self.log_message((u"bold",
254
 
                          u"Mandos Monitor version " + version))
255
 
        self.log_message((u"bold",
256
 
                          u"q: Quit  ?: Help"))
 
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"))
257
316
        
258
317
        self.busname = domain + '.Mandos'
259
318
        self.main_loop = gobject.MainLoop()
270
329
            mandos_clients = dbus.Dictionary()
271
330
        
272
331
        (self.mandos_serv
273
 
         .connect_to_signal("ClientRemoved",
 
332
         .connect_to_signal(u"ClientRemoved",
274
333
                            self.find_and_remove_client,
275
334
                            dbus_interface=server_interface,
276
335
                            byte_arrays=True))
277
336
        (self.mandos_serv
278
 
         .connect_to_signal("ClientAdded",
 
337
         .connect_to_signal(u"ClientAdded",
279
338
                            self.add_new_client,
280
339
                            dbus_interface=server_interface,
281
340
                            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))
282
346
        for path, client in mandos_clients.iteritems():
283
347
            client_proxy_object = self.bus.get_object(self.busname,
284
348
                                                      path)
290
354
                                               update_hook
291
355
                                               =self.refresh,
292
356
                                               delete_hook
293
 
                                               =self.remove_client),
 
357
                                               =self.remove_client,
 
358
                                               logger
 
359
                                               =self.log_message),
294
360
                            path=path)
295
361
    
 
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
    
296
367
    def rebuild(self):
297
368
        """This rebuilds the User Interface.
298
369
        Call this when the widget layout needs to change"""
307
378
            pass
308
379
        self.topwidget = urwid.Pile(self.uilist)
309
380
    
310
 
    def log_message(self, markup):
 
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):
311
386
        """Add a log message to the log buffer."""
312
387
        self.log.append(urwid.Text(markup, wrap=self.log_wrap))
313
388
        if (self.max_log_length
314
389
            and len(self.log) > self.max_log_length):
315
390
            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()
316
394
    
317
395
    def toggle_log_display(self):
318
396
        """Toggle visibility of the log buffer."""
344
422
            return
345
423
        self.remove_client(client, path)
346
424
    
347
 
    def add_new_client(self, path, properties):
 
425
    def add_new_client(self, path):
348
426
        client_proxy_object = self.bus.get_object(self.busname, path)
349
427
        self.add_client(MandosClientWidget(server_proxy_object
350
428
                                           =self.mandos_serv,
351
429
                                           proxy_object
352
430
                                           =client_proxy_object,
353
 
                                           properties=properties,
354
431
                                           update_hook
355
432
                                           =self.refresh,
356
433
                                           delete_hook
357
 
                                           =self.remove_client),
 
434
                                           =self.remove_client,
 
435
                                           logger
 
436
                                           =self.log_message),
358
437
                        path=path)
359
438
    
360
439
    def add_client(self, client, path=None):
429
508
            elif key == u"w" or key == u"i":
430
509
                self.change_log_display()
431
510
                self.refresh()
432
 
            elif key == u"?" or key == u"f1":
 
511
            elif key == u"?" or key == u"f1" or key == u"esc":
433
512
                if not self.log_visible:
434
513
                    self.log_visible = True
435
514
                    self.rebuild()
436
 
                self.log_message((u"bold",
437
 
                                  u"  ".join((u"q: Quit",
438
 
                                              u"?: Help",
439
 
                                              u"l: Log window toggle",
440
 
                                              u"TAB: Switch window",
441
 
                                              u"w: Wrap (log)"))))
442
 
                self.log_message((u"bold",
443
 
                                  u"  ".join((u"Clients:",
444
 
                                              u"e: Enable",
445
 
                                              u"d: Disable",
446
 
                                              u"r: Remove",
447
 
                                              u"s: Start new checker",
448
 
                                              u"S: Stop checker",
449
 
                                              u"C: Checker OK"))))
 
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"))))
450
531
                self.refresh()
451
532
            elif key == u"tab":
452
533
                if self.topwidget.get_focus() is self.logbox:
480
561
ui = UserInterface()
481
562
try:
482
563
    ui.run()
483
 
except:
 
564
except Exception, e:
 
565
    ui.log_message(unicode(e))
484
566
    ui.screen.stop()
485
567
    raise