/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:
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
 
            self.logger(u'Checker for client %s (command "%s")'
159
 
                        u' was successful'
160
 
                        % (self.properties[u"Name"], command))
161
 
            self.update()
 
120
            #self.logger(u'Checker for client %s (command "%s")'
 
121
            #            u' was successful'
 
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
    
350
284
    use them as an excuse to shift focus away from this widget.
351
285
    """
352
286
    def keypress(self, (maxcol, maxrow), key):
353
 
        ret = super(ConstrainedListBox, self).keypress((maxcol,
354
 
                                                        maxrow), key)
 
287
        ret = super(ConstrainedListBox, self).keypress((maxcol, maxrow), key)
355
288
        if ret in (u"up", u"down"):
356
289
            return
357
290
        return ret
474
407
        Call this when the widget layout needs to change"""
475
408
        self.uilist = []
476
409
        #self.uilist.append(urwid.ListBox(self.clients))
477
 
        self.uilist.append(urwid.Frame(ConstrainedListBox(self.
478
 
                                                          clients),
 
410
        self.uilist.append(urwid.Frame(ConstrainedListBox(self.clients),
479
411
                                       #header=urwid.Divider(),
480
412
                                       header=None,
481
 
                                       footer=
482
 
                                       urwid.Divider(div_char=
483
 
                                                     self.divider)))
 
413
                                       footer=urwid.Divider(div_char=self.divider)))
484
414
        if self.log_visible:
485
415
            self.uilist.append(self.logbox)
486
416
            pass
549
479
        if path is None:
550
480
            path = client.proxy.object_path
551
481
        self.clients_dict[path] = client
552
 
        self.clients.sort(None, lambda c: c.properties[u"Name"])
 
482
        self.clients.sort(None, lambda c: c.properties[u"name"])
553
483
        self.refresh()
554
484
    
555
485
    def remove_client(self, client, path=None):
671
601
ui = UserInterface()
672
602
try:
673
603
    ui.run()
674
 
except KeyboardInterrupt:
675
 
    ui.screen.stop()
676
604
except Exception, e:
677
605
    ui.log_message(unicode(e))
678
606
    ui.screen.stop()