/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
 
                                                ["last_checked_ok"])
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["interval"]))
146
 
        if self.last_checker_failed:
147
 
            self._update_timer_callback_tag = (gobject.timeout_add
148
 
                                               (1000,
149
 
                                                self.update_timer))
150
117
    
151
118
    def checker_completed(self, exitstatus, condition, command):
152
119
        if exitstatus == 0:
153
 
            if self.last_checker_failed:
154
 
                self.last_checker_failed = False
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))
160
 
            self.update()
 
120
            #self.logger(u'Checker for client %s (command "%s")'
 
121
            #            u' was successful'
 
122
            #            % (self.properties[u"name"], command))
161
123
            return
162
 
        # Checker failed
163
 
        if not self.last_checker_failed:
164
 
            self.last_checker_failed = True
165
 
            self._update_timer_callback_tag = (gobject.timeout_add
166
 
                                               (1000,
167
 
                                                self.update_timer))
168
124
        if os.WIFEXITED(condition):
169
125
            self.logger(u'Checker for client %s (command "%s")'
170
126
                        u' failed with exit code %s'
171
127
                        % (self.properties[u"name"], command,
172
128
                           os.WEXITSTATUS(condition)))
173
 
        elif os.WIFSIGNALED(condition):
 
129
            return
 
130
        if os.WIFSIGNALED(condition):
174
131
            self.logger(u'Checker for client %s (command "%s")'
175
132
                        u' was killed by signal %s'
176
133
                        % (self.properties[u"name"], command,
177
134
                           os.WTERMSIG(condition)))
178
 
        elif os.WCOREDUMP(condition):
 
135
            return
 
136
        if os.WCOREDUMP(condition):
179
137
            self.logger(u'Checker for client %s (command "%s")'
180
138
                        u' dumped core'
181
139
                        % (self.properties[u"name"], command))
182
 
        else:
183
 
            self.logger(u'Checker for client %s completed mysteriously')
184
 
        self.update()
 
140
        self.logger(u'Checker for client %s completed mysteriously')
185
141
    
186
142
    def checker_started(self, command):
187
143
        #self.logger(u'Client %s started checker "%s"'
234
190
        # Rebuild focus and non-focus widgets using current properties
235
191
 
236
192
        # Base part of a client. Name!
237
 
        base = (u'%(name)s: '
 
193
        self._text = (u'%(name)s: '
238
194
                      % {u"name": self.properties[u"name"]})
239
 
        if not self.properties[u"enabled"]:
240
 
            message = u"DISABLED"
241
 
        elif self.last_checker_failed:
242
 
            timeout = datetime.timedelta(milliseconds
243
 
                                         = self.properties[u"timeout"])
244
 
            last_ok = isoformat_to_datetime(
245
 
                max((self.properties["last_checked_ok"]
246
 
                     or self.properties["created"]),
247
 
                    self.properties[u"last_enabled"]))
248
 
            timer = timeout - (datetime.datetime.utcnow() - last_ok)
249
195
 
250
 
            message = (u'A checker has failed! Time until client gets diabled: %s'
251
 
                           % unicode(timer))
252
 
        elif self.properties[u"approved_pending"]:
 
196
        if self.properties[u"approved_pending"]:
253
197
            if self.properties[u"approved_by_default"]:
254
 
                message = u"Connection established to client. (d)eny?"
 
198
                self._text += u"Connection established to client. (d)eny?"
255
199
            else:
256
 
                message = u"Seeks approval to send secret. (a)pprove?"
 
200
                self._text += u"Seeks approval to send secret. (a)pprove?"
257
201
        else:
258
 
            message = u"enabled"
259
 
        self._text = "%s%s" % (base, message)
260
 
            
 
202
            self._text += (u'%(enabled)s'
 
203
                           % {u"enabled":
 
204
                               (u"enabled"
 
205
                                if self.properties[u"enabled"]
 
206
                                else u"DISABLED")})
261
207
        if not urwid.supports_unicode():
262
208
            self._text = self._text.encode("ascii", "replace")
263
209
        textlist = [(u"normal", self._text)]
274
220
        if self.update_hook is not None:
275
221
            self.update_hook()
276
222
    
277
 
    def update_timer(self):
278
 
        "called by gobject"
279
 
        self.update()
280
 
        return True             # Keep calling this
281
 
    
282
223
    def delete(self):
283
 
        if self._update_timer_callback_tag is not None:
284
 
            gobject.source_remove(self._update_timer_callback_tag)
285
 
            self._update_timer_callback_tag = None
286
224
        if self.delete_hook is not None:
287
225
            self.delete_hook(self)
288
226