/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: 2010-10-03 01:38:05 UTC
  • mfrom: (24.1.170 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20101003013805-ojt18455zatp2ewh
Merge from Björn.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/python
2
2
# -*- mode: python; coding: utf-8 -*-
 
3
 
4
# Mandos Monitor - Control and monitor the Mandos server
 
5
 
6
# Copyright © 2009,2010 Teddy Hogeborn
 
7
# Copyright © 2009,2010 Björn Påhlsson
 
8
 
9
# This program is free software: you can redistribute it and/or modify
 
10
# it under the terms of the GNU General Public License as published by
 
11
# the Free Software Foundation, either version 3 of the License, or
 
12
# (at your option) any later version.
 
13
#
 
14
#     This program is distributed in the hope that it will be useful,
 
15
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
#     GNU General Public License for more details.
 
18
 
19
# You should have received a copy of the GNU General Public License
 
20
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
21
 
22
# Contact the authors at <mandos@fukt.bsnet.se>.
 
23
3
24
 
4
25
from __future__ import division, absolute_import, with_statement
5
26
 
30
51
domain = 'se.bsnet.fukt'
31
52
server_interface = domain + '.Mandos'
32
53
client_interface = domain + '.Mandos.Client'
33
 
version = "1.0.15"
 
54
version = "1.2.1"
34
55
 
35
56
# Always run in monochrome mode
36
57
urwid.curses_display.curses.has_colors = lambda : False
102
123
        self.logger = logger
103
124
        
104
125
        self._update_timer_callback_tag = None
 
126
        self._update_timer_callback_lock = 0
105
127
        self.last_checker_failed = False
106
128
        
107
129
        # The widget shown normally
113
135
            *args, **kwargs)
114
136
        self.update()
115
137
        self.opened = False
 
138
        
 
139
        last_checked_ok = isoformat_to_datetime(self.properties
 
140
                                                [u"LastCheckedOK"])
 
141
        if last_checked_ok is None:
 
142
            self.last_checker_failed = True
 
143
        else:
 
144
            self.last_checker_failed = ((datetime.datetime.utcnow()
 
145
                                         - last_checked_ok)
 
146
                                        > datetime.timedelta
 
147
                                        (milliseconds=
 
148
                                         self.properties
 
149
                                         [u"Interval"]))
 
150
        
 
151
        if self.last_checker_failed:
 
152
            self.using_timer(True)
 
153
        
 
154
        if self.need_approval:
 
155
            self.using_timer(True)
 
156
        
116
157
        self.proxy.connect_to_signal(u"CheckerCompleted",
117
158
                                     self.checker_completed,
118
159
                                     client_interface,
133
174
                                     self.rejected,
134
175
                                     client_interface,
135
176
                                     byte_arrays=True)
136
 
        last_checked_ok = isoformat_to_datetime(self.properties
137
 
                                                ["last_checked_ok"])
138
 
        if last_checked_ok is None:
139
 
            self.last_checker_failed = True
 
177
    
 
178
    def property_changed(self, property=None, value=None):
 
179
        super(self, MandosClientWidget).property_changed(property,
 
180
                                                         value)
 
181
        if property == u"ApprovalPending":
 
182
            using_timer(bool(value))
 
183
        
 
184
    def using_timer(self, flag):
 
185
        """Call this method with True or False when timer should be
 
186
        activated or deactivated.
 
187
        """
 
188
        old = self._update_timer_callback_lock
 
189
        if flag:
 
190
            self._update_timer_callback_lock += 1
140
191
        else:
141
 
            self.last_checker_failed = ((datetime.datetime.utcnow()
142
 
                                         - last_checked_ok)
143
 
                                        > datetime.timedelta
144
 
                                        (milliseconds=
145
 
                                         self.properties["interval"]))
146
 
        if self.last_checker_failed:
 
192
            self._update_timer_callback_lock -= 1
 
193
        if old == 0 and self._update_timer_callback_lock:
147
194
            self._update_timer_callback_tag = (gobject.timeout_add
148
195
                                               (1000,
149
196
                                                self.update_timer))
 
197
        elif old and self._update_timer_callback_lock == 0:
 
198
            gobject.source_remove(self._update_timer_callback_tag)
 
199
            self._update_timer_callback_tag = None
150
200
    
151
201
    def checker_completed(self, exitstatus, condition, command):
152
202
        if exitstatus == 0:
153
203
            if self.last_checker_failed:
154
204
                self.last_checker_failed = False
155
 
                gobject.source_remove(self._update_timer_callback_tag)
156
 
                self._update_timer_callback_tag = None
157
 
            self.logger(u'Checker for client %s (command "%s")'
158
 
                        u' was successful'
159
 
                        % (self.properties[u"name"], command))
 
205
                self.using_timer(False)
 
206
            #self.logger(u'Checker for client %s (command "%s")'
 
207
            #            u' was successful'
 
208
            #            % (self.properties[u"Name"], command))
160
209
            self.update()
161
210
            return
162
211
        # Checker failed
163
212
        if not self.last_checker_failed:
164
213
            self.last_checker_failed = True
165
 
            self._update_timer_callback_tag = (gobject.timeout_add
166
 
                                               (1000,
167
 
                                                self.update_timer))
 
214
            self.using_timer(True)
168
215
        if os.WIFEXITED(condition):
169
216
            self.logger(u'Checker for client %s (command "%s")'
170
217
                        u' failed with exit code %s'
171
 
                        % (self.properties[u"name"], command,
 
218
                        % (self.properties[u"Name"], command,
172
219
                           os.WEXITSTATUS(condition)))
173
220
        elif os.WIFSIGNALED(condition):
174
221
            self.logger(u'Checker for client %s (command "%s")'
175
222
                        u' was killed by signal %s'
176
 
                        % (self.properties[u"name"], command,
 
223
                        % (self.properties[u"Name"], command,
177
224
                           os.WTERMSIG(condition)))
178
225
        elif os.WCOREDUMP(condition):
179
226
            self.logger(u'Checker for client %s (command "%s")'
180
227
                        u' dumped core'
181
 
                        % (self.properties[u"name"], command))
 
228
                        % (self.properties[u"Name"], command))
182
229
        else:
183
 
            self.logger(u'Checker for client %s completed mysteriously')
 
230
            self.logger(u'Checker for client %s completed'
 
231
                        u' mysteriously')
184
232
        self.update()
185
233
    
186
234
    def checker_started(self, command):
187
235
        #self.logger(u'Client %s started checker "%s"'
188
 
        #            % (self.properties[u"name"], unicode(command)))
 
236
        #            % (self.properties[u"Name"], unicode(command)))
189
237
        pass
190
238
    
191
239
    def got_secret(self):
192
240
        self.last_checker_failed = False
193
241
        self.logger(u'Client %s received its secret'
194
 
                    % self.properties[u"name"])
 
242
                    % self.properties[u"Name"])
195
243
    
196
244
    def need_approval(self, timeout, default):
197
245
        if not default:
199
247
        else:
200
248
            message = u'Client %s will get its secret in %s seconds'
201
249
        self.logger(message
202
 
                    % (self.properties[u"name"], timeout/1000))
 
250
                    % (self.properties[u"Name"], timeout/1000))
 
251
        self.using_timer(True)
203
252
    
204
253
    def rejected(self, reason):
205
254
        self.logger(u'Client %s was rejected; reason: %s'
206
 
                    % (self.properties[u"name"], reason))
 
255
                    % (self.properties[u"Name"], reason))
207
256
    
208
257
    def selectable(self):
209
258
        """Make this a "selectable" widget.
236
285
 
237
286
        # Base part of a client. Name!
238
287
        base = (u'%(name)s: '
239
 
                      % {u"name": self.properties[u"name"]})
240
 
        if not self.properties[u"enabled"]:
 
288
                      % {u"name": self.properties[u"Name"]})
 
289
        if not self.properties[u"Enabled"]:
241
290
            message = u"DISABLED"
242
 
        elif self.properties[u"approved_pending"]:
243
 
            if self.properties[u"approved_by_default"]:
244
 
                message = u"Connection established to client. (d)eny?"
245
 
            else:
246
 
                message = u"Seeks approval to send secret. (a)pprove?"
 
291
        elif self.properties[u"ApprovalPending"]:
 
292
            timeout = datetime.timedelta(milliseconds
 
293
                                         = self.properties
 
294
                                         [u"ApprovalDelay"])
 
295
            last_approval_request = isoformat_to_datetime(
 
296
                self.properties[u"LastApprovalRequest"])
 
297
            if last_approval_request is not None:
 
298
                timer = timeout - (datetime.datetime.utcnow()
 
299
                                   - last_approval_request)
 
300
            else:
 
301
                timer = datetime.timedelta()
 
302
            if self.properties[u"ApprovedByDefault"]:
 
303
                message = u"Approval in %s. (d)eny?"
 
304
            else:
 
305
                message = u"Denial in %s. (a)pprove?"
 
306
            message = message % unicode(timer).rsplit(".", 1)[0]
247
307
        elif self.last_checker_failed:
248
308
            timeout = datetime.timedelta(milliseconds
249
 
                                         = self.properties[u"timeout"])
 
309
                                         = self.properties
 
310
                                         [u"Timeout"])
250
311
            last_ok = isoformat_to_datetime(
251
 
                max((self.properties["last_checked_ok"]
252
 
                     or self.properties["created"]),
253
 
                    self.properties[u"last_enabled"]))
 
312
                max((self.properties[u"LastCheckedOK"]
 
313
                     or self.properties[u"Created"]),
 
314
                    self.properties[u"LastEnabled"]))
254
315
            timer = timeout - (datetime.datetime.utcnow() - last_ok)
255
 
            message = (u'A checker has failed! Time until client gets diabled: %s'
 
316
            message = (u'A checker has failed! Time until client'
 
317
                       u' gets disabled: %s'
256
318
                           % unicode(timer).rsplit(".", 1)[0])
257
319
        else:
258
320
            message = u"enabled"
305
367
        elif key == u"d":
306
368
            self.proxy.Approve(dbus.Boolean(False, variant_level=1),
307
369
                                  dbus_interface = client_interface)
308
 
        elif key == u"r" or key == u"_" or key == u"ctrl k":
 
370
        elif key == u"R" or key == u"_" or key == u"ctrl k":
309
371
            self.server_proxy_object.RemoveClient(self.proxy
310
372
                                                  .object_path)
311
373
        elif key == u"s":
321
383
#             self.proxy.unpause()
322
384
#         elif key == u"RET":
323
385
#             self.open()
324
 
#        elif key == u"+":
325
 
#            self.proxy.Approve(True)
326
 
#        elif key == u"-":
327
 
#            self.proxy.Approve(False)
328
386
        else:
329
387
            return key
330
388
    
346
404
    use them as an excuse to shift focus away from this widget.
347
405
    """
348
406
    def keypress(self, (maxcol, maxrow), key):
349
 
        ret = super(ConstrainedListBox, self).keypress((maxcol, maxrow), key)
 
407
        ret = super(ConstrainedListBox, self).keypress((maxcol,
 
408
                                                        maxrow), key)
350
409
        if ret in (u"up", u"down"):
351
410
            return
352
411
        return ret
469
528
        Call this when the widget layout needs to change"""
470
529
        self.uilist = []
471
530
        #self.uilist.append(urwid.ListBox(self.clients))
472
 
        self.uilist.append(urwid.Frame(ConstrainedListBox(self.clients),
 
531
        self.uilist.append(urwid.Frame(ConstrainedListBox(self.
 
532
                                                          clients),
473
533
                                       #header=urwid.Divider(),
474
534
                                       header=None,
475
 
                                       footer=urwid.Divider(div_char=self.divider)))
 
535
                                       footer=
 
536
                                       urwid.Divider(div_char=
 
537
                                                     self.divider)))
476
538
        if self.log_visible:
477
539
            self.uilist.append(self.logbox)
478
540
            pass
496
558
        """Toggle visibility of the log buffer."""
497
559
        self.log_visible = not self.log_visible
498
560
        self.rebuild()
499
 
        self.log_message(u"Log visibility changed to: "
500
 
                         + unicode(self.log_visible))
 
561
        #self.log_message(u"Log visibility changed to: "
 
562
        #                 + unicode(self.log_visible))
501
563
    
502
564
    def change_log_display(self):
503
565
        """Change type of log display.
508
570
            self.log_wrap = u"clip"
509
571
        for textwidget in self.log:
510
572
            textwidget.set_wrap_mode(self.log_wrap)
511
 
        self.log_message(u"Wrap mode: " + self.log_wrap)
 
573
        #self.log_message(u"Wrap mode: " + self.log_wrap)
512
574
    
513
575
    def find_and_remove_client(self, path, name):
514
576
        """Find an client from its object path and remove it.
541
603
        if path is None:
542
604
            path = client.proxy.object_path
543
605
        self.clients_dict[path] = client
544
 
        self.clients.sort(None, lambda c: c.properties[u"name"])
 
606
        self.clients.sort(None, lambda c: c.properties[u"Name"])
545
607
        self.refresh()
546
608
    
547
609
    def remove_client(self, client, path=None):
624
686
                                      .join((u"Clients:",
625
687
                                             u"+: Enable",
626
688
                                             u"-: Disable",
627
 
                                             u"r: Remove",
 
689
                                             u"R: Remove",
628
690
                                             u"s: Start new checker",
629
691
                                             u"S: Stop checker",
630
692
                                             u"C: Checker OK",