/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-02 18:53:38 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-20100902185338-d1022sv517txn3sb
early commit to ease todays coding

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
71
58
            self.proxy.GetAll(client_interface,
72
59
                              dbus_interface = dbus.PROPERTIES_IFACE))
73
60
        super(MandosClientPropertyCache, self).__init__(
74
 
            proxy_object=proxy_object, *args, **kwargs)
 
61
            *args, **kwargs)
75
62
    
76
63
    def property_changed(self, property=None, value=None):
77
64
        """This is called whenever we get a PropertyChanged signal
96
83
        # Logger
97
84
        self.logger = logger
98
85
        
99
 
        self._update_timer_callback_tag = None
100
 
        self.last_checker_failed = False
101
 
        
102
86
        # The widget shown normally
103
87
        self._text_widget = urwid.Text(u"")
104
88
        # The widget shown when we have focus
120
104
                                     self.got_secret,
121
105
                                     client_interface,
122
106
                                     byte_arrays=True)
 
107
        self.proxy.connect_to_signal(u"NeedApproval",
 
108
                                     self.need_approval,
 
109
                                     client_interface,
 
110
                                     byte_arrays=True)
123
111
        self.proxy.connect_to_signal(u"Rejected",
124
112
                                     self.rejected,
125
113
                                     client_interface,
126
114
                                     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
115
    
142
116
    def checker_completed(self, exitstatus, condition, command):
143
117
        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()
 
118
            #self.logger(u'Checker for client %s (command "%s")'
 
119
            #            u' was successful'
 
120
            #            % (self.properties[u"name"], command))
152
121
            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
122
        if os.WIFEXITED(condition):
160
123
            self.logger(u'Checker for client %s (command "%s")'
161
124
                        u' failed with exit code %s'
162
125
                        % (self.properties[u"name"], command,
163
126
                           os.WEXITSTATUS(condition)))
164
 
        elif os.WIFSIGNALED(condition):
 
127
            return
 
128
        if os.WIFSIGNALED(condition):
165
129
            self.logger(u'Checker for client %s (command "%s")'
166
130
                        u' was killed by signal %s'
167
131
                        % (self.properties[u"name"], command,
168
132
                           os.WTERMSIG(condition)))
169
 
        elif os.WCOREDUMP(condition):
 
133
            return
 
134
        if os.WCOREDUMP(condition):
170
135
            self.logger(u'Checker for client %s (command "%s")'
171
136
                        u' dumped core'
172
137
                        % (self.properties[u"name"], command))
173
 
        else:
174
 
            self.logger(u'Checker for client %s completed mysteriously')
175
 
        self.update()
 
138
        self.logger(u'Checker for client %s completed mysteriously')
176
139
    
177
140
    def checker_started(self, command):
178
 
        self.logger(u'Client %s started checker "%s"'
179
 
                    % (self.properties[u"name"], unicode(command)))
 
141
        #self.logger(u'Client %s started checker "%s"'
 
142
        #            % (self.properties[u"name"], unicode(command)))
 
143
        pass
180
144
    
181
145
    def got_secret(self):
182
146
        self.logger(u'Client %s received its secret'
183
147
                    % self.properties[u"name"])
184
148
    
185
 
    def rejected(self):
186
 
        self.logger(u'Client %s was rejected'
187
 
                    % self.properties[u"name"])
 
149
    def need_approval(self, timeout, default):
 
150
        if not default:
 
151
            message = u'Client %s needs approval within %s seconds'
 
152
        else:
 
153
            message = u'Client %s will get its secret in %s seconds'
 
154
        self.logger(message
 
155
                    % (self.properties[u"name"], timeout/1000))
 
156
    
 
157
    def rejected(self, reason):
 
158
        self.logger(u'Client %s was rejected; reason: %s'
 
159
                    % (self.properties[u"name"], reason))
188
160
    
189
161
    def selectable(self):
190
162
        """Make this a "selectable" widget.
214
186
                          }
215
187
        
216
188
        # Rebuild focus and non-focus widgets using current properties
217
 
        self._text = (u'%(name)s: %(enabled)s%(timer)s'
 
189
        self._text = (u'%(name)s: %(enabled)s'
218
190
                      % { u"name": self.properties[u"name"],
219
191
                          u"enabled":
220
192
                              (u"enabled"
221
193
                               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"")})
 
194
                               else u"DISABLED")})
240
195
        if not urwid.supports_unicode():
241
196
            self._text = self._text.encode("ascii", "replace")
242
197
        textlist = [(u"normal", self._text)]
253
208
        if self.update_hook is not None:
254
209
            self.update_hook()
255
210
    
256
 
    def update_timer(self):
257
 
        "called by gobject"
258
 
        self.update()
259
 
        return True             # Keep calling this
260
 
    
261
211
    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
212
        if self.delete_hook is not None:
266
213
            self.delete_hook(self)
267
214
    
294
241
#             self.proxy.unpause()
295
242
#         elif key == u"RET":
296
243
#             self.open()
 
244
        elif key == u"+":
 
245
            self.proxy.Approve(True)
 
246
        elif key == u"-":
 
247
            self.proxy.Approve(False)
297
248
        else:
298
249
            return key
299
250