/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 at bsnet
  • Date: 2010-09-14 18:22:03 UTC
  • Revision ID: teddy@fukt.bsnet.se-20100914182203-w26c64tdthto4jg2
* mandos-monitor.xml: New.
* Makefile (PROGS): Add "mandos-monitor".
  (DOCS): Add "mandos-monitor.xml".
  (mandos-monitor.8, mandos-monitor.8.xhtml, mandos-monitor): New.
  (install-server): Install "mandos-ctl" and "mandos-monitor" too.
  (uninstall-server): Remove "mandos-ctl" and "mandos-monitor" too.
* mandos-monitor: Don't log uninteresting messages.

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
 
45
24
locale.setlocale(locale.LC_ALL, u'')
46
25
 
47
26
import logging
48
 
logging.getLogger(u'dbus.proxies').setLevel(logging.CRITICAL)
 
27
logging.getLogger('dbus.proxies').setLevel(logging.CRITICAL)
49
28
 
50
29
# Some useful constants
51
 
domain = u'se.bsnet.fukt'
52
 
server_interface = domain + u'.Mandos'
53
 
client_interface = domain + u'.Mandos.Client'
54
 
version = u"1.2.3"
 
30
domain = 'se.bsnet.fukt'
 
31
server_interface = domain + '.Mandos'
 
32
client_interface = domain + '.Mandos.Client'
 
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
 
        
 
116
        self.proxy.connect_to_signal(u"CheckerCompleted",
 
117
                                     self.checker_completed,
 
118
                                     client_interface,
 
119
                                     byte_arrays=True)
 
120
        self.proxy.connect_to_signal(u"CheckerStarted",
 
121
                                     self.checker_started,
 
122
                                     client_interface,
 
123
                                     byte_arrays=True)
 
124
        self.proxy.connect_to_signal(u"GotSecret",
 
125
                                     self.got_secret,
 
126
                                     client_interface,
 
127
                                     byte_arrays=True)
 
128
        self.proxy.connect_to_signal(u"NeedApproval",
 
129
                                     self.need_approval,
 
130
                                     client_interface,
 
131
                                     byte_arrays=True)
 
132
        self.proxy.connect_to_signal(u"Rejected",
 
133
                                     self.rejected,
 
134
                                     client_interface,
 
135
                                     byte_arrays=True)
139
136
        last_checked_ok = isoformat_to_datetime(self.properties
140
137
                                                [u"LastCheckedOK"])
141
138
        if last_checked_ok is None:
147
144
                                        (milliseconds=
148
145
                                         self.properties
149
146
                                         [u"Interval"]))
150
 
        
151
147
        if self.last_checker_failed:
152
 
            self.using_timer(True)
153
 
        
154
 
        if self.need_approval:
155
 
            self.using_timer(True)
156
 
        
157
 
        self.proxy.connect_to_signal(u"CheckerCompleted",
158
 
                                     self.checker_completed,
159
 
                                     client_interface,
160
 
                                     byte_arrays=True)
161
 
        self.proxy.connect_to_signal(u"CheckerStarted",
162
 
                                     self.checker_started,
163
 
                                     client_interface,
164
 
                                     byte_arrays=True)
165
 
        self.proxy.connect_to_signal(u"GotSecret",
166
 
                                     self.got_secret,
167
 
                                     client_interface,
168
 
                                     byte_arrays=True)
169
 
        self.proxy.connect_to_signal(u"NeedApproval",
170
 
                                     self.need_approval,
171
 
                                     client_interface,
172
 
                                     byte_arrays=True)
173
 
        self.proxy.connect_to_signal(u"Rejected",
174
 
                                     self.rejected,
175
 
                                     client_interface,
176
 
                                     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
191
 
        else:
192
 
            self._update_timer_callback_lock -= 1
193
 
        if old == 0 and self._update_timer_callback_lock:
194
148
            self._update_timer_callback_tag = (gobject.timeout_add
195
149
                                               (1000,
196
150
                                                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
151
    
201
152
    def checker_completed(self, exitstatus, condition, command):
202
153
        if exitstatus == 0:
203
154
            if self.last_checker_failed:
204
155
                self.last_checker_failed = False
205
 
                self.using_timer(False)
 
156
                gobject.source_remove(self._update_timer_callback_tag)
 
157
                self._update_timer_callback_tag = None
206
158
            #self.logger(u'Checker for client %s (command "%s")'
207
159
            #            u' was successful'
208
160
            #            % (self.properties[u"Name"], command))
211
163
        # Checker failed
212
164
        if not self.last_checker_failed:
213
165
            self.last_checker_failed = True
214
 
            self.using_timer(True)
 
166
            self._update_timer_callback_tag = (gobject.timeout_add
 
167
                                               (1000,
 
168
                                                self.update_timer))
215
169
        if os.WIFEXITED(condition):
216
170
            self.logger(u'Checker for client %s (command "%s")'
217
171
                        u' failed with exit code %s'
248
202
            message = u'Client %s will get its secret in %s seconds'
249
203
        self.logger(message
250
204
                    % (self.properties[u"Name"], timeout/1000))
251
 
        self.using_timer(True)
252
205
    
253
206
    def rejected(self, reason):
254
207
        self.logger(u'Client %s was rejected; reason: %s'
259
212
        This overrides the method from urwid.FlowWidget."""
260
213
        return True
261
214
    
262
 
    def rows(self, maxcolrow, focus=False):
 
215
    def rows(self, (maxcol,), focus=False):
263
216
        """How many rows this widget will occupy might depend on
264
217
        whether we have focus or not.
265
218
        This overrides the method from urwid.FlowWidget"""
266
 
        return self.current_widget(focus).rows(maxcolrow, focus=focus)
 
219
        return self.current_widget(focus).rows((maxcol,), focus=focus)
267
220
    
268
221
    def current_widget(self, focus=False):
269
222
        if focus or self.opened:
289
242
        if not self.properties[u"Enabled"]:
290
243
            message = u"DISABLED"
291
244
        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
245
            if self.properties[u"ApprovedByDefault"]:
303
 
                message = u"Approval in %s. (d)eny?"
 
246
                message = u"Connection established to client. (d)eny?"
304
247
            else:
305
 
                message = u"Denial in %s. (a)pprove?"
306
 
            message = message % unicode(timer).rsplit(".", 1)[0]
 
248
                message = u"Seeks approval to send secret. (a)pprove?"
307
249
        elif self.last_checker_failed:
308
250
            timeout = datetime.timedelta(milliseconds
309
251
                                         = self.properties
314
256
                    self.properties[u"LastEnabled"]))
315
257
            timer = timeout - (datetime.datetime.utcnow() - last_ok)
316
258
            message = (u'A checker has failed! Time until client'
317
 
                       u' gets disabled: %s'
 
259
                       u' gets diabled: %s'
318
260
                           % unicode(timer).rsplit(".", 1)[0])
319
261
        else:
320
262
            message = u"enabled"
321
 
        self._text = u"%s%s" % (base, message)
 
263
        self._text = "%s%s" % (base, message)
322
264
            
323
265
        if not urwid.supports_unicode():
324
 
            self._text = self._text.encode(u"ascii", u"replace")
 
266
            self._text = self._text.encode("ascii", "replace")
325
267
        textlist = [(u"normal", self._text)]
326
268
        self._text_widget.set_text(textlist)
327
269
        self._focus_text_widget.set_text([(with_standout[text[0]],
331
273
                                          for text in textlist])
332
274
        self._widget = self._text_widget
333
275
        self._focus_widget = urwid.AttrWrap(self._focus_text_widget,
334
 
                                            u"standout")
 
276
                                            "standout")
335
277
        # Run update hook, if any
336
278
        if self.update_hook is not None:
337
279
            self.update_hook()
348
290
        if self.delete_hook is not None:
349
291
            self.delete_hook(self)
350
292
    
351
 
    def render(self, maxcolrow, focus=False):
 
293
    def render(self, (maxcol,), focus=False):
352
294
        """Render differently if we have focus.
353
295
        This overrides the method from urwid.FlowWidget"""
354
 
        return self.current_widget(focus).render(maxcolrow,
 
296
        return self.current_widget(focus).render((maxcol,),
355
297
                                                 focus=focus)
356
298
    
357
 
    def keypress(self, maxcolrow, key):
 
299
    def keypress(self, (maxcol,), key):
358
300
        """Handle keys.
359
301
        This overrides the method from urwid.FlowWidget"""
360
302
        if key == u"+":
367
309
        elif key == u"d":
368
310
            self.proxy.Approve(dbus.Boolean(False, variant_level=1),
369
311
                                  dbus_interface = client_interface)
370
 
        elif key == u"R" or key == u"_" or key == u"ctrl k":
 
312
        elif key == u"r" or key == u"_" or key == u"ctrl k":
371
313
            self.server_proxy_object.RemoveClient(self.proxy
372
314
                                                  .object_path)
373
315
        elif key == u"s":
403
345
    "down" key presses, thus not allowing any containing widgets to
404
346
    use them as an excuse to shift focus away from this widget.
405
347
    """
406
 
    def keypress(self, maxcolrow, key):
407
 
        ret = super(ConstrainedListBox, self).keypress(maxcolrow, key)
 
348
    def keypress(self, (maxcol, maxrow), key):
 
349
        ret = super(ConstrainedListBox, self).keypress((maxcol,
 
350
                                                        maxrow), key)
408
351
        if ret in (u"up", u"down"):
409
352
            return
410
353
        return ret
685
628
                                      .join((u"Clients:",
686
629
                                             u"+: Enable",
687
630
                                             u"-: Disable",
688
 
                                             u"R: Remove",
 
631
                                             u"r: Remove",
689
632
                                             u"s: Start new checker",
690
633
                                             u"S: Stop checker",
691
634
                                             u"C: Checker OK",