4
4
# Mandos Monitor - Control and monitor the Mandos server
 
6
 
# Copyright © 2009-2013 Teddy Hogeborn
 
7
 
# Copyright © 2009-2013 Björn Påhlsson
 
 
6
# Copyright © 2009-2014 Teddy Hogeborn
 
 
7
# Copyright © 2009-2014 Björn Påhlsson
 
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
 
 
163
162
                                         client_interface,
 
164
163
                                         byte_arrays=True))
 
165
 
        #self.logger('Created client {0}'
 
166
 
        #            .format(self.properties["Name"]))
 
 
164
        self.logger('Created client {}'
 
 
165
                    .format(self.properties["Name"]), level=0)
 
168
167
    def using_timer(self, flag):
 
169
168
        """Call this method with True or False when timer should be
 
 
181
180
    def checker_completed(self, exitstatus, condition, command):
 
182
181
        if exitstatus == 0:
 
 
182
            self.logger('Checker for client {} (command "{}")'
 
 
183
                        ' succeeded'.format(self.properties["Name"],
 
186
188
        if os.WIFEXITED(condition):
 
187
 
            self.logger('Checker for client {0} (command "{1}")'
 
188
 
                        ' failed with exit code {2}'
 
 
189
            self.logger('Checker for client {} (command "{}") failed'
 
189
191
                        .format(self.properties["Name"], command,
 
190
192
                                os.WEXITSTATUS(condition)))
 
191
193
        elif os.WIFSIGNALED(condition):
 
192
 
            self.logger('Checker for client {0} (command "{1}") was'
 
193
 
                        ' killed by signal {2}'
 
 
194
            self.logger('Checker for client {} (command "{}") was'
 
 
195
                        ' killed by signal {}'
 
194
196
                        .format(self.properties["Name"], command,
 
195
197
                                os.WTERMSIG(condition)))
 
196
198
        elif os.WCOREDUMP(condition):
 
197
 
            self.logger('Checker for client {0} (command "{1}")'
 
199
 
                        .format(self.properties["Name"], command))
 
 
199
            self.logger('Checker for client {} (command "{}") dumped'
 
 
200
                        ' core'.format(self.properties["Name"],
 
201
 
            self.logger('Checker for client {0} completed'
 
 
203
            self.logger('Checker for client {} completed'
 
203
205
                        .format(self.properties["Name"]))
 
206
208
    def checker_started(self, command):
 
207
 
        """Server signals that a checker started. This could be useful
 
208
 
           to log in the future. """
 
209
 
        #self.logger('Client {0} started checker "{1}"'
 
210
 
        #            .format(self.properties["Name"],
 
 
209
        """Server signals that a checker started."""
 
 
210
        self.logger('Client {} started checker "{}"'
 
 
211
                    .format(self.properties["Name"],
 
214
214
    def got_secret(self):
 
215
 
        self.logger('Client {0} received its secret'
 
 
215
        self.logger('Client {} received its secret'
 
216
216
                    .format(self.properties["Name"]))
 
218
218
    def need_approval(self, timeout, default):
 
220
 
            message = 'Client {0} needs approval within {1} seconds'
 
 
220
            message = 'Client {} needs approval within {} seconds'
 
222
 
            message = 'Client {0} will get its secret in {1} seconds'
 
 
222
            message = 'Client {} will get its secret in {} seconds'
 
223
223
        self.logger(message.format(self.properties["Name"],
 
226
226
    def rejected(self, reason):
 
227
 
        self.logger('Client {0} was rejected; reason: {1}'
 
 
227
        self.logger('Client {} was rejected; reason: {}'
 
228
228
                    .format(self.properties["Name"], reason))
 
230
230
    def selectable(self):
 
 
275
275
                timer = datetime.timedelta()
 
276
276
            if self.properties["ApprovedByDefault"]:
 
277
 
                message = "Approval in {0}. (d)eny?"
 
 
277
                message = "Approval in {}. (d)eny?"
 
279
 
                message = "Denial in {0}. (a)pprove?"
 
 
279
                message = "Denial in {}. (a)pprove?"
 
280
280
            message = message.format(str(timer).rsplit(".", 1)[0])
 
281
281
            self.using_timer(True)
 
282
282
        elif self.properties["LastCheckerStatus"] != 0:
 
 
290
290
                timer = max(expires - datetime.datetime.utcnow(),
 
291
291
                            datetime.timedelta())
 
292
292
            message = ('A checker has failed! Time until client'
 
293
 
                       ' gets disabled: {0}'
 
294
294
                       .format(str(timer).rsplit(".", 1)[0]))
 
295
295
            self.using_timer(True)
 
297
297
            message = "enabled"
 
298
298
            self.using_timer(False)
 
299
 
        self._text = "{0}{1}".format(base, message)
 
 
299
        self._text = "{}{}".format(base, message)
 
301
301
        if not urwid.supports_unicode():
 
302
302
            self._text = self._text.encode("ascii", "replace")
 
 
404
404
    """This is the entire user interface - the whole screen
 
405
405
    with boxes, lists of client widgets, etc.
 
407
 
    def __init__(self, max_log_length=1000):
 
 
407
    def __init__(self, max_log_length=1000, log_level=1):
 
408
408
        DBusGMainLoop(set_as_default=True)
 
410
410
        self.screen = urwid.curses_display.Screen()
 
 
467
469
        self.main_loop = gobject.MainLoop()
 
469
471
    def client_not_found(self, fingerprint, address):
 
470
 
        self.log_message("Client with address {0} and fingerprint"
 
471
 
                         " {1} could not be found"
 
 
472
        self.log_message("Client with address {} and fingerprint {}"
 
 
473
                         " could not be found"
 
472
474
                         .format(address, fingerprint))
 
474
476
    def rebuild(self):
 
 
487
489
            self.uilist.append(self.logbox)
 
488
490
        self.topwidget = urwid.Pile(self.uilist)
 
490
 
    def log_message(self, message):
 
 
492
    def log_message(self, message, level=1):
 
491
493
        """Log message formatted with timestamp"""
 
 
494
        if level < self.log_level:
 
492
496
        timestamp = datetime.datetime.now().isoformat()
 
493
 
        self.log_message_raw(timestamp + ": " + message)
 
 
497
        self.log_message_raw("{}: {}".format(timestamp, message),
 
495
 
    def log_message_raw(self, markup):
 
 
500
    def log_message_raw(self, markup, level=1):
 
496
501
        """Add a log message to the log buffer."""
 
 
502
        if level < self.log_level:
 
497
504
        self.log.append(urwid.Text(markup, wrap=self.log_wrap))
 
498
505
        if (self.max_log_length
 
499
506
            and len(self.log) > self.max_log_length):
 
 
506
513
        """Toggle visibility of the log buffer."""
 
507
514
        self.log_visible = not self.log_visible
 
509
 
        #self.log_message("Log visibility changed to: "
 
510
 
        #                 + str(self.log_visible))
 
 
516
        self.log_message("Log visibility changed to: {}"
 
 
517
                         .format(self.log_visible), level=0)
 
512
519
    def change_log_display(self):
 
513
520
        """Change type of log display.
 
 
518
525
            self.log_wrap = "clip"
 
519
526
        for textwidget in self.log:
 
520
527
            textwidget.set_wrap_mode(self.log_wrap)
 
521
 
        #self.log_message("Wrap mode: " + self.log_wrap)
 
 
528
        self.log_message("Wrap mode: {}".format(self.log_wrap),
 
523
531
    def find_and_remove_client(self, path, name):
 
524
532
        """Find a client by its object path and remove it.