/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« back to all changes in this revision

Viewing changes to mandos-monitor

  • Committer: Björn Påhlsson
  • Date: 2010-09-07 16:48:58 UTC
  • mto: (237.7.1 mandos)
  • mto: This revision was merged to the branch mainline in revision 270.
  • Revision ID: belorn@fukt.bsnet.se-20100907164858-tcg8hkxdj41zizac
mandos server: Added debuglevel that adjust at what level information
               should be reported.
plugin-runner, askpass-fifo, password-prompt, splasy, usplash:
               Using error instead of perror

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 
24
24
locale.setlocale(locale.LC_ALL, u'')
25
25
 
 
26
import logging
 
27
logging.getLogger('dbus.proxies').setLevel(logging.CRITICAL)
 
28
 
26
29
# Some useful constants
27
30
domain = 'se.bsnet.fukt'
28
31
server_interface = domain + '.Mandos'
29
32
client_interface = domain + '.Mandos.Client'
30
 
version = "1.0.14"
 
33
version = "1.0.15"
31
34
 
32
35
# Always run in monochrome mode
33
36
urwid.curses_display.curses.has_colors = lambda : False
37
40
urwid.curses_display.curses.A_UNDERLINE |= (
38
41
    urwid.curses_display.curses.A_BLINK)
39
42
 
40
 
def isoformat_to_datetime(iso):
41
 
    "Parse an ISO 8601 date string to a datetime.datetime()"
42
 
    if not iso:
43
 
        return None
44
 
    d, t = iso.split(u"T", 1)
45
 
    year, month, day = d.split(u"-", 2)
46
 
    hour, minute, second = t.split(u":", 2)
47
 
    second, fraction = divmod(float(second), 1)
48
 
    return datetime.datetime(int(year),
49
 
                             int(month),
50
 
                             int(day),
51
 
                             int(hour),
52
 
                             int(minute),
53
 
                             int(second),           # Whole seconds
54
 
                             int(fraction*1000000)) # Microseconds
55
 
 
56
43
class MandosClientPropertyCache(object):
57
44
    """This wraps a Mandos Client D-Bus proxy object, caches the
58
45
    properties and calls a hook function when any of them are
70
57
        self.properties.update(
71
58
            self.proxy.GetAll(client_interface,
72
59
                              dbus_interface = dbus.PROPERTIES_IFACE))
73
 
        super(MandosClientPropertyCache, self).__init__(
74
 
            proxy_object=proxy_object, *args, **kwargs)
 
60
 
 
61
        #XXX This break good super behaviour!
 
62
#        super(MandosClientPropertyCache, self).__init__(
 
63
#            *args, **kwargs)
75
64
    
76
65
    def property_changed(self, property=None, value=None):
77
66
        """This is called whenever we get a PropertyChanged signal
96
85
        # Logger
97
86
        self.logger = logger
98
87
        
99
 
        self._update_timer_callback_tag = None
100
 
        self.last_checker_failed = False
101
 
        
102
88
        # The widget shown normally
103
89
        self._text_widget = urwid.Text(u"")
104
90
        # The widget shown when we have focus
120
106
                                     self.got_secret,
121
107
                                     client_interface,
122
108
                                     byte_arrays=True)
 
109
        self.proxy.connect_to_signal(u"NeedApproval",
 
110
                                     self.need_approval,
 
111
                                     client_interface,
 
112
                                     byte_arrays=True)
123
113
        self.proxy.connect_to_signal(u"Rejected",
124
114
                                     self.rejected,
125
115
                                     client_interface,
126
116
                                     byte_arrays=True)
127
 
        last_checked_ok = isoformat_to_datetime(self.properties
128
 
                                                ["last_checked_ok"])
129
 
        if last_checked_ok is None:
130
 
            self.last_checker_failed = True
131
 
        else:
132
 
            self.last_checker_failed = ((datetime.datetime.utcnow()
133
 
                                         - last_checked_ok)
134
 
                                        > datetime.timedelta
135
 
                                        (milliseconds=
136
 
                                         self.properties["interval"]))
137
 
        if self.last_checker_failed:
138
 
            self._update_timer_callback_tag = (gobject.timeout_add
139
 
                                               (1000,
140
 
                                                self.update_timer))
141
117
    
142
118
    def checker_completed(self, exitstatus, condition, command):
143
119
        if exitstatus == 0:
144
 
            if self.last_checker_failed:
145
 
                self.last_checker_failed = False
146
 
                gobject.source_remove(self._update_timer_callback_tag)
147
 
                self._update_timer_callback_tag = None
148
 
            self.logger(u'Checker for client %s (command "%s")'
149
 
                        u' was successful'
150
 
                        % (self.properties[u"name"], command))
151
 
            self.update()
 
120
            #self.logger(u'Checker for client %s (command "%s")'
 
121
            #            u' was successful'
 
122
            #            % (self.properties[u"name"], command))
152
123
            return
153
 
        # Checker failed
154
 
        if not self.last_checker_failed:
155
 
            self.last_checker_failed = True
156
 
            self._update_timer_callback_tag = (gobject.timeout_add
157
 
                                               (1000,
158
 
                                                self.update_timer))
159
124
        if os.WIFEXITED(condition):
160
125
            self.logger(u'Checker for client %s (command "%s")'
161
126
                        u' failed with exit code %s'
162
127
                        % (self.properties[u"name"], command,
163
128
                           os.WEXITSTATUS(condition)))
164
 
        elif os.WIFSIGNALED(condition):
 
129
            return
 
130
        if os.WIFSIGNALED(condition):
165
131
            self.logger(u'Checker for client %s (command "%s")'
166
132
                        u' was killed by signal %s'
167
133
                        % (self.properties[u"name"], command,
168
134
                           os.WTERMSIG(condition)))
169
 
        elif os.WCOREDUMP(condition):
 
135
            return
 
136
        if os.WCOREDUMP(condition):
170
137
            self.logger(u'Checker for client %s (command "%s")'
171
138
                        u' dumped core'
172
139
                        % (self.properties[u"name"], command))
173
 
        else:
174
 
            self.logger(u'Checker for client %s completed mysteriously')
175
 
        self.update()
 
140
        self.logger(u'Checker for client %s completed mysteriously')
176
141
    
177
142
    def checker_started(self, command):
178
 
        self.logger(u'Client %s started checker "%s"'
179
 
                    % (self.properties[u"name"], unicode(command)))
 
143
        #self.logger(u'Client %s started checker "%s"'
 
144
        #            % (self.properties[u"name"], unicode(command)))
 
145
        pass
180
146
    
181
147
    def got_secret(self):
182
148
        self.logger(u'Client %s received its secret'
183
149
                    % self.properties[u"name"])
184
150
    
185
 
    def rejected(self):
186
 
        self.logger(u'Client %s was rejected'
187
 
                    % self.properties[u"name"])
 
151
    def need_approval(self, timeout, default):
 
152
        if not default:
 
153
            message = u'Client %s needs approval within %s seconds'
 
154
        else:
 
155
            message = u'Client %s will get its secret in %s seconds'
 
156
        self.logger(message
 
157
                    % (self.properties[u"name"], timeout/1000))
 
158
    
 
159
    def rejected(self, reason):
 
160
        self.logger(u'Client %s was rejected; reason: %s'
 
161
                    % (self.properties[u"name"], reason))
188
162
    
189
163
    def selectable(self):
190
164
        """Make this a "selectable" widget.
212
186
                          u"bold-underline-blink":
213
187
                              u"bold-underline-blink-standout",
214
188
                          }
215
 
        
 
189
 
216
190
        # Rebuild focus and non-focus widgets using current properties
217
 
        self._text = (u'%(name)s: %(enabled)s%(timer)s'
218
 
                      % { u"name": self.properties[u"name"],
219
 
                          u"enabled":
220
 
                              (u"enabled"
221
 
                               if self.properties[u"enabled"]
222
 
                               else u"DISABLED"),
223
 
                          u"timer": (unicode(datetime.timedelta
224
 
                                             (milliseconds =
225
 
                                              self.properties
226
 
                                              [u"timeout"])
227
 
                                             - (datetime.datetime
228
 
                                                .utcnow()
229
 
                                                - isoformat_to_datetime
230
 
                                                (max((self.properties
231
 
                                                 ["last_checked_ok"]
232
 
                                                 or
233
 
                                                 self.properties
234
 
                                                 ["created"]),
235
 
                                                    self.properties[u"last_enabled"]))))
236
 
                                     if (self.last_checker_failed
237
 
                                         and self.properties
238
 
                                         [u"enabled"])
239
 
                                     else u"")})
 
191
 
 
192
        # Base part of a client. Name!
 
193
        self._text = (u'%(name)s: '
 
194
                      % {u"name": self.properties[u"name"]})
 
195
 
 
196
        if self.properties[u"approved_pending"]:
 
197
            if self.properties[u"approved_by_default"]:
 
198
                self._text += u"Connection established to client. (d)eny?"
 
199
            else:
 
200
                self._text += u"Seeks approval to send secret. (a)pprove?"
 
201
        else:
 
202
            self._text += (u'%(enabled)s'
 
203
                           % {u"enabled":
 
204
                               (u"enabled"
 
205
                                if self.properties[u"enabled"]
 
206
                                else u"DISABLED")})
240
207
        if not urwid.supports_unicode():
241
208
            self._text = self._text.encode("ascii", "replace")
242
209
        textlist = [(u"normal", self._text)]
253
220
        if self.update_hook is not None:
254
221
            self.update_hook()
255
222
    
256
 
    def update_timer(self):
257
 
        "called by gobject"
258
 
        self.update()
259
 
        return True             # Keep calling this
260
 
    
261
223
    def delete(self):
262
 
        if self._update_timer_callback_tag is not None:
263
 
            gobject.source_remove(self._update_timer_callback_tag)
264
 
            self._update_timer_callback_tag = None
265
224
        if self.delete_hook is not None:
266
225
            self.delete_hook(self)
267
226
    
274
233
    def keypress(self, (maxcol,), key):
275
234
        """Handle keys.
276
235
        This overrides the method from urwid.FlowWidget"""
277
 
        if key == u"e" or key == u"+":
278
 
            self.proxy.Enable()
279
 
        elif key == u"d" or key == u"-":
280
 
            self.proxy.Disable()
 
236
        if key == u"+":
 
237
            self.proxy.Enable(dbus_interface = client_interface)
 
238
        elif key == u"-":
 
239
            self.proxy.Disable(dbus_interface = client_interface)
 
240
        elif key == u"a":
 
241
            self.proxy.Approve(dbus.Boolean(True, variant_level=1),
 
242
                               dbus_interface = client_interface)
 
243
        elif key == u"d":
 
244
            self.proxy.Approve(dbus.Boolean(False, variant_level=1),
 
245
                                  dbus_interface = client_interface)
281
246
        elif key == u"r" or key == u"_" or key == u"ctrl k":
282
247
            self.server_proxy_object.RemoveClient(self.proxy
283
248
                                                  .object_path)
284
249
        elif key == u"s":
285
 
            self.proxy.StartChecker()
 
250
            self.proxy.StartChecker(dbus_interface = client_interface)
286
251
        elif key == u"S":
287
 
            self.proxy.StopChecker()
 
252
            self.proxy.StopChecker(dbus_interface = client_interface)
288
253
        elif key == u"C":
289
 
            self.proxy.CheckedOK()
 
254
            self.proxy.CheckedOK(dbus_interface = client_interface)
290
255
        # xxx
291
256
#         elif key == u"p" or key == "=":
292
257
#             self.proxy.pause()
294
259
#             self.proxy.unpause()
295
260
#         elif key == u"RET":
296
261
#             self.open()
 
262
#        elif key == u"+":
 
263
#            self.proxy.Approve(True)
 
264
#        elif key == u"-":
 
265
#            self.proxy.Approve(False)
297
266
        else:
298
267
            return key
299
268
    
591
560
                self.log_message_raw((u"bold",
592
561
                                      u"  "
593
562
                                      .join((u"Clients:",
594
 
                                             u"e: Enable",
595
 
                                             u"d: Disable",
 
563
                                             u"+: Enable",
 
564
                                             u"-: Disable",
596
565
                                             u"r: Remove",
597
566
                                             u"s: Start new checker",
598
567
                                             u"S: Stop checker",
599
 
                                             u"C: Checker OK"))))
 
568
                                             u"C: Checker OK",
 
569
                                             u"a: Approve",
 
570
                                             u"d: Deny"))))
600
571
                self.refresh()
601
572
            elif key == u"tab":
602
573
                if self.topwidget.get_focus() is self.logbox: