/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 16:48:58 UTC
  • mto: (237.4.3 mandos-release)
  • mto: This revision was merged to the branch mainline in revision 421.
  • 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:
40
40
urwid.curses_display.curses.A_UNDERLINE |= (
41
41
    urwid.curses_display.curses.A_BLINK)
42
42
 
43
 
def isoformat_to_datetime(iso):
44
 
    "Parse an ISO 8601 date string to a datetime.datetime()"
45
 
    if not iso:
46
 
        return None
47
 
    d, t = iso.split(u"T", 1)
48
 
    year, month, day = d.split(u"-", 2)
49
 
    hour, minute, second = t.split(u":", 2)
50
 
    second, fraction = divmod(float(second), 1)
51
 
    return datetime.datetime(int(year),
52
 
                             int(month),
53
 
                             int(day),
54
 
                             int(hour),
55
 
                             int(minute),
56
 
                             int(second),           # Whole seconds
57
 
                             int(fraction*1000000)) # Microseconds
58
 
 
59
43
class MandosClientPropertyCache(object):
60
44
    """This wraps a Mandos Client D-Bus proxy object, caches the
61
45
    properties and calls a hook function when any of them are
101
85
        # Logger
102
86
        self.logger = logger
103
87
        
104
 
        self._update_timer_callback_tag = None
105
 
        self.last_checker_failed = False
106
 
        
107
88
        # The widget shown normally
108
89
        self._text_widget = urwid.Text(u"")
109
90
        # The widget shown when we have focus
133
114
                                     self.rejected,
134
115
                                     client_interface,
135
116
                                     byte_arrays=True)
136
 
        last_checked_ok = isoformat_to_datetime(self.properties
137
 
                                                [u"LastCheckedOK"])
138
 
        if last_checked_ok is None:
139
 
            self.last_checker_failed = True
140
 
        else:
141
 
            self.last_checker_failed = ((datetime.datetime.utcnow()
142
 
                                         - last_checked_ok)
143
 
                                        > datetime.timedelta
144
 
                                        (milliseconds=
145
 
                                         self.properties
146
 
                                         [u"Interval"]))
147
 
        if self.last_checker_failed:
148
 
            self._update_timer_callback_tag = (gobject.timeout_add
149
 
                                               (1000,
150
 
                                                self.update_timer))
151
117
    
152
118
    def checker_completed(self, exitstatus, condition, command):
153
119
        if exitstatus == 0:
154
 
            if self.last_checker_failed:
155
 
                self.last_checker_failed = False
156
 
                gobject.source_remove(self._update_timer_callback_tag)
157
 
                self._update_timer_callback_tag = None
158
120
            #self.logger(u'Checker for client %s (command "%s")'
159
121
            #            u' was successful'
160
 
            #            % (self.properties[u"Name"], command))
161
 
            self.update()
 
122
            #            % (self.properties[u"name"], command))
162
123
            return
163
 
        # Checker failed
164
 
        if not self.last_checker_failed:
165
 
            self.last_checker_failed = True
166
 
            self._update_timer_callback_tag = (gobject.timeout_add
167
 
                                               (1000,
168
 
                                                self.update_timer))
169
124
        if os.WIFEXITED(condition):
170
125
            self.logger(u'Checker for client %s (command "%s")'
171
126
                        u' failed with exit code %s'
172
 
                        % (self.properties[u"Name"], command,
 
127
                        % (self.properties[u"name"], command,
173
128
                           os.WEXITSTATUS(condition)))
174
 
        elif os.WIFSIGNALED(condition):
 
129
            return
 
130
        if os.WIFSIGNALED(condition):
175
131
            self.logger(u'Checker for client %s (command "%s")'
176
132
                        u' was killed by signal %s'
177
 
                        % (self.properties[u"Name"], command,
 
133
                        % (self.properties[u"name"], command,
178
134
                           os.WTERMSIG(condition)))
179
 
        elif os.WCOREDUMP(condition):
 
135
            return
 
136
        if os.WCOREDUMP(condition):
180
137
            self.logger(u'Checker for client %s (command "%s")'
181
138
                        u' dumped core'
182
 
                        % (self.properties[u"Name"], command))
183
 
        else:
184
 
            self.logger(u'Checker for client %s completed'
185
 
                        u' mysteriously')
186
 
        self.update()
 
139
                        % (self.properties[u"name"], command))
 
140
        self.logger(u'Checker for client %s completed mysteriously')
187
141
    
188
142
    def checker_started(self, command):
189
143
        #self.logger(u'Client %s started checker "%s"'
190
 
        #            % (self.properties[u"Name"], unicode(command)))
 
144
        #            % (self.properties[u"name"], unicode(command)))
191
145
        pass
192
146
    
193
147
    def got_secret(self):
194
 
        self.last_checker_failed = False
195
148
        self.logger(u'Client %s received its secret'
196
 
                    % self.properties[u"Name"])
 
149
                    % self.properties[u"name"])
197
150
    
198
151
    def need_approval(self, timeout, default):
199
152
        if not default:
201
154
        else:
202
155
            message = u'Client %s will get its secret in %s seconds'
203
156
        self.logger(message
204
 
                    % (self.properties[u"Name"], timeout/1000))
 
157
                    % (self.properties[u"name"], timeout/1000))
205
158
    
206
159
    def rejected(self, reason):
207
160
        self.logger(u'Client %s was rejected; reason: %s'
208
 
                    % (self.properties[u"Name"], reason))
 
161
                    % (self.properties[u"name"], reason))
209
162
    
210
163
    def selectable(self):
211
164
        """Make this a "selectable" widget.
237
190
        # Rebuild focus and non-focus widgets using current properties
238
191
 
239
192
        # Base part of a client. Name!
240
 
        base = (u'%(name)s: '
241
 
                      % {u"name": self.properties[u"Name"]})
242
 
        if not self.properties[u"Enabled"]:
243
 
            message = u"DISABLED"
244
 
        elif self.properties[u"ApprovalPending"]:
245
 
            if self.properties[u"ApprovedByDefault"]:
246
 
                message = u"Connection established to client. (d)eny?"
 
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?"
247
199
            else:
248
 
                message = u"Seeks approval to send secret. (a)pprove?"
249
 
        elif self.last_checker_failed:
250
 
            timeout = datetime.timedelta(milliseconds
251
 
                                         = self.properties
252
 
                                         [u"Timeout"])
253
 
            last_ok = isoformat_to_datetime(
254
 
                max((self.properties[u"LastCheckedOK"]
255
 
                     or self.properties[u"Created"]),
256
 
                    self.properties[u"LastEnabled"]))
257
 
            timer = timeout - (datetime.datetime.utcnow() - last_ok)
258
 
            message = (u'A checker has failed! Time until client'
259
 
                       u' gets diabled: %s'
260
 
                           % unicode(timer).rsplit(".", 1)[0])
 
200
                self._text += u"Seeks approval to send secret. (a)pprove?"
261
201
        else:
262
 
            message = u"enabled"
263
 
        self._text = "%s%s" % (base, message)
264
 
            
 
202
            self._text += (u'%(enabled)s'
 
203
                           % {u"enabled":
 
204
                               (u"enabled"
 
205
                                if self.properties[u"enabled"]
 
206
                                else u"DISABLED")})
265
207
        if not urwid.supports_unicode():
266
208
            self._text = self._text.encode("ascii", "replace")
267
209
        textlist = [(u"normal", self._text)]
278
220
        if self.update_hook is not None:
279
221
            self.update_hook()
280
222
    
281
 
    def update_timer(self):
282
 
        "called by gobject"
283
 
        self.update()
284
 
        return True             # Keep calling this
285
 
    
286
223
    def delete(self):
287
 
        if self._update_timer_callback_tag is not None:
288
 
            gobject.source_remove(self._update_timer_callback_tag)
289
 
            self._update_timer_callback_tag = None
290
224
        if self.delete_hook is not None:
291
225
            self.delete_hook(self)
292
226
    
325
259
#             self.proxy.unpause()
326
260
#         elif key == u"RET":
327
261
#             self.open()
 
262
#        elif key == u"+":
 
263
#            self.proxy.Approve(True)
 
264
#        elif key == u"-":
 
265
#            self.proxy.Approve(False)
328
266
        else:
329
267
            return key
330
268
    
346
284
    use them as an excuse to shift focus away from this widget.
347
285
    """
348
286
    def keypress(self, (maxcol, maxrow), key):
349
 
        ret = super(ConstrainedListBox, self).keypress((maxcol,
350
 
                                                        maxrow), key)
 
287
        ret = super(ConstrainedListBox, self).keypress((maxcol, maxrow), key)
351
288
        if ret in (u"up", u"down"):
352
289
            return
353
290
        return ret
470
407
        Call this when the widget layout needs to change"""
471
408
        self.uilist = []
472
409
        #self.uilist.append(urwid.ListBox(self.clients))
473
 
        self.uilist.append(urwid.Frame(ConstrainedListBox(self.
474
 
                                                          clients),
 
410
        self.uilist.append(urwid.Frame(ConstrainedListBox(self.clients),
475
411
                                       #header=urwid.Divider(),
476
412
                                       header=None,
477
 
                                       footer=
478
 
                                       urwid.Divider(div_char=
479
 
                                                     self.divider)))
 
413
                                       footer=urwid.Divider(div_char=self.divider)))
480
414
        if self.log_visible:
481
415
            self.uilist.append(self.logbox)
482
416
            pass
500
434
        """Toggle visibility of the log buffer."""
501
435
        self.log_visible = not self.log_visible
502
436
        self.rebuild()
503
 
        #self.log_message(u"Log visibility changed to: "
504
 
        #                 + unicode(self.log_visible))
 
437
        self.log_message(u"Log visibility changed to: "
 
438
                         + unicode(self.log_visible))
505
439
    
506
440
    def change_log_display(self):
507
441
        """Change type of log display.
512
446
            self.log_wrap = u"clip"
513
447
        for textwidget in self.log:
514
448
            textwidget.set_wrap_mode(self.log_wrap)
515
 
        #self.log_message(u"Wrap mode: " + self.log_wrap)
 
449
        self.log_message(u"Wrap mode: " + self.log_wrap)
516
450
    
517
451
    def find_and_remove_client(self, path, name):
518
452
        """Find an client from its object path and remove it.
545
479
        if path is None:
546
480
            path = client.proxy.object_path
547
481
        self.clients_dict[path] = client
548
 
        self.clients.sort(None, lambda c: c.properties[u"Name"])
 
482
        self.clients.sort(None, lambda c: c.properties[u"name"])
549
483
        self.refresh()
550
484
    
551
485
    def remove_client(self, client, path=None):
667
601
ui = UserInterface()
668
602
try:
669
603
    ui.run()
670
 
except KeyboardInterrupt:
671
 
    ui.screen.stop()
672
604
except Exception, e:
673
605
    ui.log_message(unicode(e))
674
606
    ui.screen.stop()