/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 15:29:22 UTC
  • Revision ID: teddy@recompile.se-20150531152922-o1xv6qr3hbj1twm0
mandos: Use codecs.open() and print() for PID file.

Use more Pythonic code, closer to the Python 3 style.

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-2014 Teddy Hogeborn
 
7
# Copyright © 2009-2014 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.9"
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
176
    def checker_completed(self, exitstatus, condition, command):
194
191
                        ' killed by signal {}'
195
192
                        .format(self.properties["Name"], command,
196
193
                                os.WTERMSIG(condition)))
 
194
        elif os.WCOREDUMP(condition):
 
195
            self.logger('Checker for client {} (command "{}") dumped'
 
196
                        ' core'.format(self.properties["Name"],
 
197
                                       command))
 
198
        else:
 
199
            self.logger('Checker for client {} completed'
 
200
                        ' mysteriously'
 
201
                        .format(self.properties["Name"]))
197
202
        self.update()
198
203
    
199
204
    def checker_started(self, command):
306
311
            self.update_hook()
307
312
    
308
313
    def update_timer(self):
309
 
        """called by GLib. Will indefinitely loop until
310
 
        GLib.source_remove() on tag is called
311
 
        """
 
314
        """called by gobject. Will indefinitely loop until
 
315
        gobject.source_remove() on tag is called"""
312
316
        self.update()
313
317
        return True             # Keep calling this
314
318
    
315
319
    def delete(self, **kwargs):
316
320
        if self._update_timer_callback_tag is not None:
317
 
            GLib.source_remove(self._update_timer_callback_tag)
 
321
            gobject.source_remove(self._update_timer_callback_tag)
318
322
            self._update_timer_callback_tag = None
319
323
        for match in self.match_objects:
320
324
            match.remove()
333
337
        """Handle keys.
334
338
        This overrides the method from urwid.FlowWidget"""
335
339
        if key == "+":
336
 
            self.proxy.Set(client_interface, "Enabled",
337
 
                           dbus.Boolean(True), ignore_reply = True,
338
 
                           dbus_interface = dbus.PROPERTIES_IFACE)
 
340
            self.proxy.Enable(dbus_interface = client_interface,
 
341
                              ignore_reply=True)
339
342
        elif key == "-":
340
 
            self.proxy.Set(client_interface, "Enabled", False,
341
 
                           ignore_reply = True,
342
 
                           dbus_interface = dbus.PROPERTIES_IFACE)
 
343
            self.proxy.Disable(dbus_interface = client_interface,
 
344
                               ignore_reply=True)
343
345
        elif key == "a":
344
346
            self.proxy.Approve(dbus.Boolean(True, variant_level=1),
345
347
                               dbus_interface = client_interface,
353
355
                                                  .object_path,
354
356
                                                  ignore_reply=True)
355
357
        elif key == "s":
356
 
            self.proxy.Set(client_interface, "CheckerRunning",
357
 
                           dbus.Boolean(True), ignore_reply = True,
358
 
                           dbus_interface = dbus.PROPERTIES_IFACE)
 
358
            self.proxy.StartChecker(dbus_interface = client_interface,
 
359
                                    ignore_reply=True)
359
360
        elif key == "S":
360
 
            self.proxy.Set(client_interface, "CheckerRunning",
361
 
                           dbus.Boolean(False), ignore_reply = True,
362
 
                           dbus_interface = dbus.PROPERTIES_IFACE)
 
361
            self.proxy.StopChecker(dbus_interface = client_interface,
 
362
                                   ignore_reply=True)
363
363
        elif key == "C":
364
364
            self.proxy.CheckedOK(dbus_interface = client_interface,
365
365
                                 ignore_reply=True)
463
463
                              "q: Quit  ?: Help"))
464
464
        
465
465
        self.busname = domain + '.Mandos'
466
 
        self.main_loop = GLib.MainLoop()
 
466
        self.main_loop = gobject.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, interfaces):
 
528
    def find_and_remove_client(self, path, name):
529
529
        """Find a client by its object path and remove it.
530
530
        
531
 
        This is connected to the InterfacesRemoved signal from the
 
531
        This is connected to the ClientRemoved signal from the
532
532
        Mandos server object."""
533
 
        if client_interface not in interfaces:
534
 
            # Not a Mandos client object; ignore
535
 
            return
536
533
        try:
537
534
            client = self.clients_dict[path]
538
535
        except KeyError:
539
536
            # not found?
540
 
            self.log_message("Unknown client {!r} removed"
541
 
                             .format(path))
 
537
            self.log_message("Unknown client {!r} ({!r}) removed"
 
538
                             .format(name, path))
542
539
            return
543
540
        client.delete()
544
541
    
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
 
542
    def add_new_client(self, path):
554
543
        client_proxy_object = self.bus.get_object(self.busname, path)
555
544
        self.add_client(MandosClientWidget(server_proxy_object
556
545
                                           =self.mandos_serv,
561
550
                                           delete_hook
562
551
                                           =self.remove_client,
563
552
                                           logger
564
 
                                           =self.log_message,
565
 
                                           properties
566
 
                                           = dict(ifs_and_props[
567
 
                                               client_interface])),
 
553
                                           =self.log_message),
568
554
                        path=path)
569
555
    
570
556
    def add_client(self, client, path=None):
605
591
            mandos_clients = dbus.Dictionary()
606
592
        
607
593
        (self.mandos_serv
608
 
         .connect_to_signal("InterfacesRemoved",
 
594
         .connect_to_signal("ClientRemoved",
609
595
                            self.find_and_remove_client,
610
 
                            dbus_interface
611
 
                            = dbus.OBJECT_MANAGER_IFACE,
 
596
                            dbus_interface=server_interface,
612
597
                            byte_arrays=True))
613
598
        (self.mandos_serv
614
 
         .connect_to_signal("InterfacesAdded",
 
599
         .connect_to_signal("ClientAdded",
615
600
                            self.add_new_client,
616
 
                            dbus_interface
617
 
                            = dbus.OBJECT_MANAGER_IFACE,
 
601
                            dbus_interface=server_interface,
618
602
                            byte_arrays=True))
619
603
        (self.mandos_serv
620
604
         .connect_to_signal("ClientNotFound",
638
622
                            path=path)
639
623
        
640
624
        self.refresh()
641
 
        self._input_callback_tag = (GLib.io_add_watch
 
625
        self._input_callback_tag = (gobject.io_add_watch
642
626
                                    (sys.stdin.fileno(),
643
 
                                     GLib.IO_IN,
 
627
                                     gobject.IO_IN,
644
628
                                     self.process_input))
645
629
        self.main_loop.run()
646
630
        # Main loop has finished, we should close everything now
647
 
        GLib.source_remove(self._input_callback_tag)
 
631
        gobject.source_remove(self._input_callback_tag)
648
632
        self.screen.stop()
649
633
    
650
634
    def stop(self):