/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: 2015-05-31 20:12:27 UTC
  • mfrom: (749.1.2 multiprocessing-exec)
  • Revision ID: teddy@recompile.se-20150531201227-u3n1z1ito5siqh7c
Merge change to use multiprocessing module to run checkers.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
4
4
# Mandos Monitor - Control and monitor the Mandos server
5
5
6
 
# Copyright © 2009-2016 Teddy Hogeborn
7
 
# Copyright © 2009-2016 Björn Påhlsson
 
6
# Copyright © 2009-2015 Teddy Hogeborn
 
7
# Copyright © 2009-2015 Björn Påhlsson
8
8
9
9
# This program is free software: you can redistribute it and/or modify
10
10
# it under the terms of the GNU General Public License as published by
39
39
import urwid
40
40
 
41
41
from dbus.mainloop.glib import DBusGMainLoop
42
 
from gi.repository import GLib
 
42
try:
 
43
    import gobject
 
44
except ImportError:
 
45
    from gi.repository import GObject as gobject
43
46
 
44
47
import dbus
45
48
 
57
60
domain = 'se.recompile'
58
61
server_interface = domain + '.Mandos'
59
62
client_interface = domain + '.Mandos.Client'
60
 
version = "1.7.7"
61
 
 
62
 
try:
63
 
    dbus.OBJECT_MANAGER_IFACE
64
 
except AttributeError:
65
 
    dbus.OBJECT_MANAGER_IFACE = "org.freedesktop.DBus.ObjectManager"
 
63
version = "1.6.9"
66
64
 
67
65
def isoformat_to_datetime(iso):
68
66
    "Parse an ISO 8601 date string to a datetime.datetime()"
107
105
        It updates the changed properties in the "properties" dict.
108
106
        """
109
107
        # Update properties dict with new value
110
 
        if interface == client_interface:
111
 
            self.properties.update(properties)
 
108
        self.properties.update(properties)
112
109
    
113
110
    def delete(self):
114
111
        self.property_changed_match.remove()
169
166
        """
170
167
        if flag and self._update_timer_callback_tag is None:
171
168
            # Will update the shown timer value every second
172
 
            self._update_timer_callback_tag = (GLib.timeout_add
 
169
            self._update_timer_callback_tag = (gobject.timeout_add
173
170
                                               (1000,
174
171
                                                self.update_timer))
175
172
        elif not (flag or self._update_timer_callback_tag is None):
176
 
            GLib.source_remove(self._update_timer_callback_tag)
 
173
            gobject.source_remove(self._update_timer_callback_tag)
177
174
            self._update_timer_callback_tag = None
178
175
    
179
 
    def checker_completed(self, exitstatus, condition, command):
 
176
    def checker_completed(self, exitstatus, signal, command):
180
177
        if exitstatus == 0:
181
178
            self.logger('Checker for client {} (command "{}")'
182
179
                        ' succeeded'.format(self.properties["Name"],
184
181
            self.update()
185
182
            return
186
183
        # Checker failed
187
 
        if os.WIFEXITED(condition):
 
184
        if exitstatus >= 0:
188
185
            self.logger('Checker for client {} (command "{}") failed'
189
186
                        ' with exit code {}'
190
187
                        .format(self.properties["Name"], command,
191
 
                                os.WEXITSTATUS(condition)))
192
 
        elif os.WIFSIGNALED(condition):
 
188
                                exitstatus))
 
189
        elif signal != 0:
193
190
            self.logger('Checker for client {} (command "{}") was'
194
191
                        ' killed by signal {}'
195
192
                        .format(self.properties["Name"], command,
196
 
                                os.WTERMSIG(condition)))
 
193
                                signal))
 
194
        else:
 
195
            self.logger('Checker for client {} completed'
 
196
                        ' mysteriously'
 
197
                        .format(self.properties["Name"]))
197
198
        self.update()
198
199
    
199
200
    def checker_started(self, command):
306
307
            self.update_hook()
307
308
    
308
309
    def update_timer(self):
309
 
        """called by GLib. Will indefinitely loop until
310
 
        GLib.source_remove() on tag is called
311
 
        """
 
310
        """called by gobject. Will indefinitely loop until
 
311
        gobject.source_remove() on tag is called"""
312
312
        self.update()
313
313
        return True             # Keep calling this
314
314
    
315
315
    def delete(self, **kwargs):
316
316
        if self._update_timer_callback_tag is not None:
317
 
            GLib.source_remove(self._update_timer_callback_tag)
 
317
            gobject.source_remove(self._update_timer_callback_tag)
318
318
            self._update_timer_callback_tag = None
319
319
        for match in self.match_objects:
320
320
            match.remove()
333
333
        """Handle keys.
334
334
        This overrides the method from urwid.FlowWidget"""
335
335
        if key == "+":
336
 
            self.proxy.Set(client_interface, "Enabled",
337
 
                           dbus.Boolean(True), ignore_reply = True,
338
 
                           dbus_interface = dbus.PROPERTIES_IFACE)
 
336
            self.proxy.Enable(dbus_interface = client_interface,
 
337
                              ignore_reply=True)
339
338
        elif key == "-":
340
 
            self.proxy.Set(client_interface, "Enabled", False,
341
 
                           ignore_reply = True,
342
 
                           dbus_interface = dbus.PROPERTIES_IFACE)
 
339
            self.proxy.Disable(dbus_interface = client_interface,
 
340
                               ignore_reply=True)
343
341
        elif key == "a":
344
342
            self.proxy.Approve(dbus.Boolean(True, variant_level=1),
345
343
                               dbus_interface = client_interface,
353
351
                                                  .object_path,
354
352
                                                  ignore_reply=True)
355
353
        elif key == "s":
356
 
            self.proxy.Set(client_interface, "CheckerRunning",
357
 
                           dbus.Boolean(True), ignore_reply = True,
358
 
                           dbus_interface = dbus.PROPERTIES_IFACE)
 
354
            self.proxy.StartChecker(dbus_interface = client_interface,
 
355
                                    ignore_reply=True)
359
356
        elif key == "S":
360
 
            self.proxy.Set(client_interface, "CheckerRunning",
361
 
                           dbus.Boolean(False), ignore_reply = True,
362
 
                           dbus_interface = dbus.PROPERTIES_IFACE)
 
357
            self.proxy.StopChecker(dbus_interface = client_interface,
 
358
                                   ignore_reply=True)
363
359
        elif key == "C":
364
360
            self.proxy.CheckedOK(dbus_interface = client_interface,
365
361
                                 ignore_reply=True)
463
459
                              "q: Quit  ?: Help"))
464
460
        
465
461
        self.busname = domain + '.Mandos'
466
 
        self.main_loop = GLib.MainLoop()
 
462
        self.main_loop = gobject.MainLoop()
467
463
    
468
464
    def client_not_found(self, fingerprint, address):
469
465
        self.log_message("Client with address {} and fingerprint {}"
525
521
        self.log_message("Wrap mode: {}".format(self.log_wrap),
526
522
                         level=0)
527
523
    
528
 
    def find_and_remove_client(self, path, interfaces):
 
524
    def find_and_remove_client(self, path, name):
529
525
        """Find a client by its object path and remove it.
530
526
        
531
 
        This is connected to the InterfacesRemoved signal from the
 
527
        This is connected to the ClientRemoved signal from the
532
528
        Mandos server object."""
533
 
        if client_interface not in interfaces:
534
 
            # Not a Mandos client object; ignore
535
 
            return
536
529
        try:
537
530
            client = self.clients_dict[path]
538
531
        except KeyError:
539
532
            # not found?
540
 
            self.log_message("Unknown client {!r} removed"
541
 
                             .format(path))
 
533
            self.log_message("Unknown client {!r} ({!r}) removed"
 
534
                             .format(name, path))
542
535
            return
543
536
        client.delete()
544
537
    
545
 
    def add_new_client(self, path, ifs_and_props):
546
 
        """Find a client by its object path and remove it.
547
 
        
548
 
        This is connected to the InterfacesAdded signal from the
549
 
        Mandos server object.
550
 
        """
551
 
        if client_interface not in ifs_and_props:
552
 
            # Not a Mandos client object; ignore
553
 
            return
 
538
    def add_new_client(self, path):
554
539
        client_proxy_object = self.bus.get_object(self.busname, path)
555
540
        self.add_client(MandosClientWidget(server_proxy_object
556
541
                                           =self.mandos_serv,
561
546
                                           delete_hook
562
547
                                           =self.remove_client,
563
548
                                           logger
564
 
                                           =self.log_message,
565
 
                                           properties
566
 
                                           = dict(ifs_and_props[
567
 
                                               client_interface])),
 
549
                                           =self.log_message),
568
550
                        path=path)
569
551
    
570
552
    def add_client(self, client, path=None):
605
587
            mandos_clients = dbus.Dictionary()
606
588
        
607
589
        (self.mandos_serv
608
 
         .connect_to_signal("InterfacesRemoved",
 
590
         .connect_to_signal("ClientRemoved",
609
591
                            self.find_and_remove_client,
610
 
                            dbus_interface
611
 
                            = dbus.OBJECT_MANAGER_IFACE,
 
592
                            dbus_interface=server_interface,
612
593
                            byte_arrays=True))
613
594
        (self.mandos_serv
614
 
         .connect_to_signal("InterfacesAdded",
 
595
         .connect_to_signal("ClientAdded",
615
596
                            self.add_new_client,
616
 
                            dbus_interface
617
 
                            = dbus.OBJECT_MANAGER_IFACE,
 
597
                            dbus_interface=server_interface,
618
598
                            byte_arrays=True))
619
599
        (self.mandos_serv
620
600
         .connect_to_signal("ClientNotFound",
638
618
                            path=path)
639
619
        
640
620
        self.refresh()
641
 
        self._input_callback_tag = (GLib.io_add_watch
 
621
        self._input_callback_tag = (gobject.io_add_watch
642
622
                                    (sys.stdin.fileno(),
643
 
                                     GLib.IO_IN,
 
623
                                     gobject.IO_IN,
644
624
                                     self.process_input))
645
625
        self.main_loop.run()
646
626
        # Main loop has finished, we should close everything now
647
 
        GLib.source_remove(self._input_callback_tag)
 
627
        gobject.source_remove(self._input_callback_tag)
648
628
        self.screen.stop()
649
629
    
650
630
    def stop(self):