/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: 2016-08-25 16:17:23 UTC
  • Revision ID: teddy@recompile.se-20160825161723-wsbn6sxyk29qgmsv
PEP8 compliance: mandos-ctl

* mandos-ctl: Add PEP8 compliance (as per the "pycodestyle" tool).

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.10"
 
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()
333
333
        """Handle keys.
334
334
        This overrides the method from urwid.FlowWidget"""
335
335
        if key == "+":
336
 
            self.proxy.Enable(dbus_interface = client_interface,
337
 
                              ignore_reply=True)
 
336
            self.proxy.Set(client_interface, "Enabled",
 
337
                           dbus.Boolean(True), ignore_reply = True,
 
338
                           dbus_interface = dbus.PROPERTIES_IFACE)
338
339
        elif key == "-":
339
 
            self.proxy.Disable(dbus_interface = client_interface,
340
 
                               ignore_reply=True)
 
340
            self.proxy.Set(client_interface, "Enabled", False,
 
341
                           ignore_reply = True,
 
342
                           dbus_interface = dbus.PROPERTIES_IFACE)
341
343
        elif key == "a":
342
344
            self.proxy.Approve(dbus.Boolean(True, variant_level=1),
343
345
                               dbus_interface = client_interface,
351
353
                                                  .object_path,
352
354
                                                  ignore_reply=True)
353
355
        elif key == "s":
354
 
            self.proxy.StartChecker(dbus_interface = client_interface,
355
 
                                    ignore_reply=True)
 
356
            self.proxy.Set(client_interface, "CheckerRunning",
 
357
                           dbus.Boolean(True), ignore_reply = True,
 
358
                           dbus_interface = dbus.PROPERTIES_IFACE)
356
359
        elif key == "S":
357
 
            self.proxy.StopChecker(dbus_interface = client_interface,
358
 
                                   ignore_reply=True)
 
360
            self.proxy.Set(client_interface, "CheckerRunning",
 
361
                           dbus.Boolean(False), ignore_reply = True,
 
362
                           dbus_interface = dbus.PROPERTIES_IFACE)
359
363
        elif key == "C":
360
364
            self.proxy.CheckedOK(dbus_interface = client_interface,
361
365
                                 ignore_reply=True)
459
463
                              "q: Quit  ?: Help"))
460
464
        
461
465
        self.busname = domain + '.Mandos'
462
 
        self.main_loop = gobject.MainLoop()
 
466
        self.main_loop = GLib.MainLoop()
463
467
    
464
468
    def client_not_found(self, fingerprint, address):
465
469
        self.log_message("Client with address {} and fingerprint {}"
521
525
        self.log_message("Wrap mode: {}".format(self.log_wrap),
522
526
                         level=0)
523
527
    
524
 
    def find_and_remove_client(self, path, name):
 
528
    def find_and_remove_client(self, path, interfaces):
525
529
        """Find a client by its object path and remove it.
526
530
        
527
 
        This is connected to the ClientRemoved signal from the
 
531
        This is connected to the InterfacesRemoved signal from the
528
532
        Mandos server object."""
 
533
        if client_interface not in interfaces:
 
534
            # Not a Mandos client object; ignore
 
535
            return
529
536
        try:
530
537
            client = self.clients_dict[path]
531
538
        except KeyError:
532
539
            # not found?
533
 
            self.log_message("Unknown client {!r} ({!r}) removed"
534
 
                             .format(name, path))
 
540
            self.log_message("Unknown client {!r} removed"
 
541
                             .format(path))
535
542
            return
536
543
        client.delete()
537
544
    
538
 
    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
539
554
        client_proxy_object = self.bus.get_object(self.busname, path)
540
555
        self.add_client(MandosClientWidget(server_proxy_object
541
556
                                           =self.mandos_serv,
546
561
                                           delete_hook
547
562
                                           =self.remove_client,
548
563
                                           logger
549
 
                                           =self.log_message),
 
564
                                           =self.log_message,
 
565
                                           properties
 
566
                                           = dict(ifs_and_props[
 
567
                                               client_interface])),
550
568
                        path=path)
551
569
    
552
570
    def add_client(self, client, path=None):
587
605
            mandos_clients = dbus.Dictionary()
588
606
        
589
607
        (self.mandos_serv
590
 
         .connect_to_signal("ClientRemoved",
 
608
         .connect_to_signal("InterfacesRemoved",
591
609
                            self.find_and_remove_client,
592
 
                            dbus_interface=server_interface,
 
610
                            dbus_interface
 
611
                            = dbus.OBJECT_MANAGER_IFACE,
593
612
                            byte_arrays=True))
594
613
        (self.mandos_serv
595
 
         .connect_to_signal("ClientAdded",
 
614
         .connect_to_signal("InterfacesAdded",
596
615
                            self.add_new_client,
597
 
                            dbus_interface=server_interface,
 
616
                            dbus_interface
 
617
                            = dbus.OBJECT_MANAGER_IFACE,
598
618
                            byte_arrays=True))
599
619
        (self.mandos_serv
600
620
         .connect_to_signal("ClientNotFound",
618
638
                            path=path)
619
639
        
620
640
        self.refresh()
621
 
        self._input_callback_tag = (gobject.io_add_watch
 
641
        self._input_callback_tag = (GLib.io_add_watch
622
642
                                    (sys.stdin.fileno(),
623
 
                                     gobject.IO_IN,
 
643
                                     GLib.IO_IN,
624
644
                                     self.process_input))
625
645
        self.main_loop.run()
626
646
        # Main loop has finished, we should close everything now
627
 
        gobject.source_remove(self._input_callback_tag)
 
647
        GLib.source_remove(self._input_callback_tag)
628
648
        self.screen.stop()
629
649
    
630
650
    def stop(self):