/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: Teddy Hogeborn
  • Date: 2016-03-19 22:27:06 UTC
  • mfrom: (342 release)
  • mto: (237.7.594 trunk)
  • mto: This revision was merged to the branch mainline in revision 343.
  • Revision ID: teddy@recompile.se-20160319222706-n61m90vqve42k1vc
Merge from release branch

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-2015 Teddy Hogeborn
7
 
# Copyright © 2009-2015 Björn Påhlsson
 
6
# Copyright © 2009-2016 Teddy Hogeborn
 
7
# Copyright © 2009-2016 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
 
try:
43
 
    import gobject
44
 
except ImportError:
45
 
    from gi.repository import GObject as gobject
 
42
from gi.repository import GLib
46
43
 
47
44
import dbus
48
45
 
60
57
domain = 'se.recompile'
61
58
server_interface = domain + '.Mandos'
62
59
client_interface = domain + '.Mandos.Client'
63
 
version = "1.6.9"
 
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"
64
66
 
65
67
def isoformat_to_datetime(iso):
66
68
    "Parse an ISO 8601 date string to a datetime.datetime()"
105
107
        It updates the changed properties in the "properties" dict.
106
108
        """
107
109
        # Update properties dict with new value
108
 
        self.properties.update(properties)
 
110
        if interface == client_interface:
 
111
            self.properties.update(properties)
109
112
    
110
113
    def delete(self):
111
114
        self.property_changed_match.remove()
166
169
        """
167
170
        if flag and self._update_timer_callback_tag is None:
168
171
            # Will update the shown timer value every second
169
 
            self._update_timer_callback_tag = (gobject.timeout_add
 
172
            self._update_timer_callback_tag = (GLib.timeout_add
170
173
                                               (1000,
171
174
                                                self.update_timer))
172
175
        elif not (flag or self._update_timer_callback_tag is None):
173
 
            gobject.source_remove(self._update_timer_callback_tag)
 
176
            GLib.source_remove(self._update_timer_callback_tag)
174
177
            self._update_timer_callback_tag = None
175
178
    
176
 
    def checker_completed(self, exitstatus, signal, command):
 
179
    def checker_completed(self, exitstatus, condition, command):
177
180
        if exitstatus == 0:
178
181
            self.logger('Checker for client {} (command "{}")'
179
182
                        ' succeeded'.format(self.properties["Name"],
181
184
            self.update()
182
185
            return
183
186
        # Checker failed
184
 
        if exitstatus >= 0:
 
187
        if os.WIFEXITED(condition):
185
188
            self.logger('Checker for client {} (command "{}") failed'
186
189
                        ' with exit code {}'
187
190
                        .format(self.properties["Name"], command,
188
 
                                exitstatus))
189
 
        elif signal != 0:
 
191
                                os.WEXITSTATUS(condition)))
 
192
        elif os.WIFSIGNALED(condition):
190
193
            self.logger('Checker for client {} (command "{}") was'
191
194
                        ' killed by signal {}'
192
195
                        .format(self.properties["Name"], command,
193
 
                                signal))
194
 
        else:
195
 
            self.logger('Checker for client {} completed'
196
 
                        ' mysteriously'
197
 
                        .format(self.properties["Name"]))
 
196
                                os.WTERMSIG(condition)))
198
197
        self.update()
199
198
    
200
199
    def checker_started(self, command):
307
306
            self.update_hook()
308
307
    
309
308
    def update_timer(self):
310
 
        """called by gobject. Will indefinitely loop until
311
 
        gobject.source_remove() on tag is called"""
 
309
        """called by GLib. Will indefinitely loop until
 
310
        GLib.source_remove() on tag is called
 
311
        """
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
 
            gobject.source_remove(self._update_timer_callback_tag)
 
317
            GLib.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()
463
463
                              "q: Quit  ?: Help"))
464
464
        
465
465
        self.busname = domain + '.Mandos'
466
 
        self.main_loop = gobject.MainLoop()
 
466
        self.main_loop = GLib.MainLoop()
467
467
    
468
468
    def client_not_found(self, fingerprint, address):
469
469
        self.log_message("Client with address {} and fingerprint {}"
525
525
        self.log_message("Wrap mode: {}".format(self.log_wrap),
526
526
                         level=0)
527
527
    
528
 
    def find_and_remove_client(self, path, name):
 
528
    def find_and_remove_client(self, path, interfaces):
529
529
        """Find a client by its object path and remove it.
530
530
        
531
 
        This is connected to the ClientRemoved signal from the
 
531
        This is connected to the InterfacesRemoved signal from the
532
532
        Mandos server object."""
 
533
        if client_interface not in interfaces:
 
534
            # Not a Mandos client object; ignore
 
535
            return
533
536
        try:
534
537
            client = self.clients_dict[path]
535
538
        except KeyError:
536
539
            # not found?
537
 
            self.log_message("Unknown client {!r} ({!r}) removed"
538
 
                             .format(name, path))
 
540
            self.log_message("Unknown client {!r} removed"
 
541
                             .format(path))
539
542
            return
540
543
        client.delete()
541
544
    
542
 
    def add_new_client(self, path):
 
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
543
554
        client_proxy_object = self.bus.get_object(self.busname, path)
544
555
        self.add_client(MandosClientWidget(server_proxy_object
545
556
                                           =self.mandos_serv,
550
561
                                           delete_hook
551
562
                                           =self.remove_client,
552
563
                                           logger
553
 
                                           =self.log_message),
 
564
                                           =self.log_message,
 
565
                                           properties
 
566
                                           = dict(ifs_and_props[
 
567
                                               client_interface])),
554
568
                        path=path)
555
569
    
556
570
    def add_client(self, client, path=None):
591
605
            mandos_clients = dbus.Dictionary()
592
606
        
593
607
        (self.mandos_serv
594
 
         .connect_to_signal("ClientRemoved",
 
608
         .connect_to_signal("InterfacesRemoved",
595
609
                            self.find_and_remove_client,
596
 
                            dbus_interface=server_interface,
 
610
                            dbus_interface
 
611
                            = dbus.OBJECT_MANAGER_IFACE,
597
612
                            byte_arrays=True))
598
613
        (self.mandos_serv
599
 
         .connect_to_signal("ClientAdded",
 
614
         .connect_to_signal("InterfacesAdded",
600
615
                            self.add_new_client,
601
 
                            dbus_interface=server_interface,
 
616
                            dbus_interface
 
617
                            = dbus.OBJECT_MANAGER_IFACE,
602
618
                            byte_arrays=True))
603
619
        (self.mandos_serv
604
620
         .connect_to_signal("ClientNotFound",
622
638
                            path=path)
623
639
        
624
640
        self.refresh()
625
 
        self._input_callback_tag = (gobject.io_add_watch
 
641
        self._input_callback_tag = (GLib.io_add_watch
626
642
                                    (sys.stdin.fileno(),
627
 
                                     gobject.IO_IN,
 
643
                                     GLib.IO_IN,
628
644
                                     self.process_input))
629
645
        self.main_loop.run()
630
646
        # Main loop has finished, we should close everything now
631
 
        gobject.source_remove(self._input_callback_tag)
 
647
        GLib.source_remove(self._input_callback_tag)
632
648
        self.screen.stop()
633
649
    
634
650
    def stop(self):