/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: Björn Påhlsson
  • Date: 2010-09-07 18:41:35 UTC
  • mfrom: (417 mandos)
  • mto: (237.4.3 mandos-release)
  • mto: This revision was merged to the branch mainline in revision 421.
  • Revision ID: belorn@fukt.bsnet.se-20100907184135-hcd2aou8r2trz8cy
merge

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
 
24
3
 
25
4
from __future__ import division, absolute_import, with_statement
26
5
 
51
30
domain = 'se.bsnet.fukt'
52
31
server_interface = domain + '.Mandos'
53
32
client_interface = domain + '.Mandos.Client'
54
 
version = "1.2.1"
 
33
version = "1.0.15"
55
34
 
56
35
# Always run in monochrome mode
57
36
urwid.curses_display.curses.has_colors = lambda : False
123
102
        self.logger = logger
124
103
        
125
104
        self._update_timer_callback_tag = None
126
 
        self._update_timer_callback_lock = 0
127
105
        self.last_checker_failed = False
128
106
        
129
107
        # The widget shown normally
135
113
            *args, **kwargs)
136
114
        self.update()
137
115
        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
 
        
157
116
        self.proxy.connect_to_signal(u"CheckerCompleted",
158
117
                                     self.checker_completed,
159
118
                                     client_interface,
174
133
                                     self.rejected,
175
134
                                     client_interface,
176
135
                                     byte_arrays=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
 
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
191
140
        else:
192
 
            self._update_timer_callback_lock -= 1
193
 
        if old == 0 and self._update_timer_callback_lock:
 
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:
194
147
            self._update_timer_callback_tag = (gobject.timeout_add
195
148
                                               (1000,
196
149
                                                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
200
150
    
201
151
    def checker_completed(self, exitstatus, condition, command):
202
152
        if exitstatus == 0:
203
153
            if self.last_checker_failed:
204
154
                self.last_checker_failed = False
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))
 
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))
209
160
            self.update()
210
161
            return
211
162
        # Checker failed
212
163
        if not self.last_checker_failed:
213
164
            self.last_checker_failed = True
214
 
            self.using_timer(True)
 
165
            self._update_timer_callback_tag = (gobject.timeout_add
 
166
                                               (1000,
 
167
                                                self.update_timer))
215
168
        if os.WIFEXITED(condition):
216
169
            self.logger(u'Checker for client %s (command "%s")'
217
170
                        u' failed with exit code %s'
218
 
                        % (self.properties[u"Name"], command,
 
171
                        % (self.properties[u"name"], command,
219
172
                           os.WEXITSTATUS(condition)))
220
173
        elif os.WIFSIGNALED(condition):
221
174
            self.logger(u'Checker for client %s (command "%s")'
222
175
                        u' was killed by signal %s'
223
 
                        % (self.properties[u"Name"], command,
 
176
                        % (self.properties[u"name"], command,
224
177
                           os.WTERMSIG(condition)))
225
178
        elif os.WCOREDUMP(condition):
226
179
            self.logger(u'Checker for client %s (command "%s")'
227
180
                        u' dumped core'
228
 
                        % (self.properties[u"Name"], command))
 
181
                        % (self.properties[u"name"], command))
229
182
        else:
230
 
            self.logger(u'Checker for client %s completed'
231
 
                        u' mysteriously')
 
183
            self.logger(u'Checker for client %s completed mysteriously')
232
184
        self.update()
233
185
    
234
186
    def checker_started(self, command):
235
187
        #self.logger(u'Client %s started checker "%s"'
236
 
        #            % (self.properties[u"Name"], unicode(command)))
 
188
        #            % (self.properties[u"name"], unicode(command)))
237
189
        pass
238
190
    
239
191
    def got_secret(self):
240
 
        self.last_checker_failed = False
241
192
        self.logger(u'Client %s received its secret'
242
 
                    % self.properties[u"Name"])
 
193
                    % self.properties[u"name"])
243
194
    
244
195
    def need_approval(self, timeout, default):
245
196
        if not default:
247
198
        else:
248
199
            message = u'Client %s will get its secret in %s seconds'
249
200
        self.logger(message
250
 
                    % (self.properties[u"Name"], timeout/1000))
251
 
        self.using_timer(True)
 
201
                    % (self.properties[u"name"], timeout/1000))
252
202
    
253
203
    def rejected(self, reason):
254
204
        self.logger(u'Client %s was rejected; reason: %s'
255
 
                    % (self.properties[u"Name"], reason))
 
205
                    % (self.properties[u"name"], reason))
256
206
    
257
207
    def selectable(self):
258
208
        """Make this a "selectable" widget.
285
235
 
286
236
        # Base part of a client. Name!
287
237
        base = (u'%(name)s: '
288
 
                      % {u"name": self.properties[u"Name"]})
289
 
        if not self.properties[u"Enabled"]:
 
238
                      % {u"name": self.properties[u"name"]})
 
239
        if not self.properties[u"enabled"]:
290
240
            message = u"DISABLED"
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]
307
241
        elif self.last_checker_failed:
308
242
            timeout = datetime.timedelta(milliseconds
309
 
                                         = self.properties
310
 
                                         [u"Timeout"])
 
243
                                         = self.properties[u"timeout"])
311
244
            last_ok = isoformat_to_datetime(
312
 
                max((self.properties[u"LastCheckedOK"]
313
 
                     or self.properties[u"Created"]),
314
 
                    self.properties[u"LastEnabled"]))
 
245
                max((self.properties["last_checked_ok"]
 
246
                     or self.properties["created"]),
 
247
                    self.properties[u"last_enabled"]))
315
248
            timer = timeout - (datetime.datetime.utcnow() - last_ok)
316
 
            message = (u'A checker has failed! Time until client'
317
 
                       u' gets disabled: %s'
318
 
                           % unicode(timer).rsplit(".", 1)[0])
 
249
 
 
250
            message = (u'A checker has failed! Time until client gets diabled: %s'
 
251
                           % unicode(timer))
 
252
        elif self.properties[u"approved_pending"]:
 
253
            if self.properties[u"approved_by_default"]:
 
254
                message = u"Connection established to client. (d)eny?"
 
255
            else:
 
256
                message = u"Seeks approval to send secret. (a)pprove?"
319
257
        else:
320
258
            message = u"enabled"
321
259
        self._text = "%s%s" % (base, message)
367
305
        elif key == u"d":
368
306
            self.proxy.Approve(dbus.Boolean(False, variant_level=1),
369
307
                                  dbus_interface = client_interface)
370
 
        elif key == u"R" or key == u"_" or key == u"ctrl k":
 
308
        elif key == u"r" or key == u"_" or key == u"ctrl k":
371
309
            self.server_proxy_object.RemoveClient(self.proxy
372
310
                                                  .object_path)
373
311
        elif key == u"s":
383
321
#             self.proxy.unpause()
384
322
#         elif key == u"RET":
385
323
#             self.open()
 
324
#        elif key == u"+":
 
325
#            self.proxy.Approve(True)
 
326
#        elif key == u"-":
 
327
#            self.proxy.Approve(False)
386
328
        else:
387
329
            return key
388
330
    
404
346
    use them as an excuse to shift focus away from this widget.
405
347
    """
406
348
    def keypress(self, (maxcol, maxrow), key):
407
 
        ret = super(ConstrainedListBox, self).keypress((maxcol,
408
 
                                                        maxrow), key)
 
349
        ret = super(ConstrainedListBox, self).keypress((maxcol, maxrow), key)
409
350
        if ret in (u"up", u"down"):
410
351
            return
411
352
        return ret
528
469
        Call this when the widget layout needs to change"""
529
470
        self.uilist = []
530
471
        #self.uilist.append(urwid.ListBox(self.clients))
531
 
        self.uilist.append(urwid.Frame(ConstrainedListBox(self.
532
 
                                                          clients),
 
472
        self.uilist.append(urwid.Frame(ConstrainedListBox(self.clients),
533
473
                                       #header=urwid.Divider(),
534
474
                                       header=None,
535
 
                                       footer=
536
 
                                       urwid.Divider(div_char=
537
 
                                                     self.divider)))
 
475
                                       footer=urwid.Divider(div_char=self.divider)))
538
476
        if self.log_visible:
539
477
            self.uilist.append(self.logbox)
540
478
            pass
558
496
        """Toggle visibility of the log buffer."""
559
497
        self.log_visible = not self.log_visible
560
498
        self.rebuild()
561
 
        #self.log_message(u"Log visibility changed to: "
562
 
        #                 + unicode(self.log_visible))
 
499
        self.log_message(u"Log visibility changed to: "
 
500
                         + unicode(self.log_visible))
563
501
    
564
502
    def change_log_display(self):
565
503
        """Change type of log display.
570
508
            self.log_wrap = u"clip"
571
509
        for textwidget in self.log:
572
510
            textwidget.set_wrap_mode(self.log_wrap)
573
 
        #self.log_message(u"Wrap mode: " + self.log_wrap)
 
511
        self.log_message(u"Wrap mode: " + self.log_wrap)
574
512
    
575
513
    def find_and_remove_client(self, path, name):
576
514
        """Find an client from its object path and remove it.
603
541
        if path is None:
604
542
            path = client.proxy.object_path
605
543
        self.clients_dict[path] = client
606
 
        self.clients.sort(None, lambda c: c.properties[u"Name"])
 
544
        self.clients.sort(None, lambda c: c.properties[u"name"])
607
545
        self.refresh()
608
546
    
609
547
    def remove_client(self, client, path=None):
686
624
                                      .join((u"Clients:",
687
625
                                             u"+: Enable",
688
626
                                             u"-: Disable",
689
 
                                             u"R: Remove",
 
627
                                             u"r: Remove",
690
628
                                             u"s: Start new checker",
691
629
                                             u"S: Stop checker",
692
630
                                             u"C: Checker OK",
725
663
ui = UserInterface()
726
664
try:
727
665
    ui.run()
728
 
except KeyboardInterrupt:
729
 
    ui.screen.stop()
730
666
except Exception, e:
731
667
    ui.log_message(unicode(e))
732
668
    ui.screen.stop()