/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: Teddy Hogeborn
  • Date: 2012-05-01 20:48:00 UTC
  • Revision ID: teddy@recompile.se-20120501204800-0wkzdkcd3epzci45
* mandos (main): Simplify and shorten code selecting user and group ID
                 to switch to.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
#     GNU General Public License for more details.
18
18
19
19
# You should have received a copy of the GNU General Public License
20
 
# along with this program.  If not, see
21
 
# <http://www.gnu.org/licenses/>.
 
20
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
21
23
22
# Contact the authors at <mandos@recompile.se>.
24
23
26
25
from __future__ import (division, absolute_import, print_function,
27
26
                        unicode_literals)
28
27
 
29
 
from future_builtins import *
30
 
 
31
28
import sys
32
29
import os
33
30
import signal
175
172
                                         self.rejected,
176
173
                                         client_interface,
177
174
                                         byte_arrays=True))
178
 
        #self.logger('Created client {0}'
179
 
        #            .format(self.properties["Name"]))
 
175
        #self.logger('Created client %s' % (self.properties["Name"]))
180
176
    
181
177
    def property_changed(self, property=None, value=None):
182
178
        super(self, MandosClientWidget).property_changed(property,
185
181
            using_timer(bool(value))
186
182
        if property == "LastCheckerStatus":
187
183
            using_timer(value != 0)
188
 
            #self.logger('Checker for client {0} (command "{1}") was '
189
 
            #            ' successful'.format(self.properties["Name"],
190
 
            #                                 command))
 
184
            #self.logger('Checker for client %s (command "%s")'
 
185
            #            ' was successful'
 
186
            #            % (self.properties["Name"], command))
191
187
    
192
188
    def using_timer(self, flag):
193
189
        """Call this method with True or False when timer should be
213
209
            return
214
210
        # Checker failed
215
211
        if os.WIFEXITED(condition):
216
 
            self.logger('Checker for client {0} (command "{1}")'
217
 
                        ' failed with exit code {2}'
218
 
                        .format(self.properties["Name"], command,
219
 
                                os.WEXITSTATUS(condition)))
 
212
            self.logger('Checker for client %s (command "%s")'
 
213
                        ' failed with exit code %s'
 
214
                        % (self.properties["Name"], command,
 
215
                           os.WEXITSTATUS(condition)))
220
216
        elif os.WIFSIGNALED(condition):
221
 
            self.logger('Checker for client {0} (command "{1}") was'
222
 
                        ' killed by signal {2}'
223
 
                        .format(self.properties["Name"], command,
224
 
                                os.WTERMSIG(condition)))
 
217
            self.logger('Checker for client %s (command "%s")'
 
218
                        ' was killed by signal %s'
 
219
                        % (self.properties["Name"], command,
 
220
                           os.WTERMSIG(condition)))
225
221
        elif os.WCOREDUMP(condition):
226
 
            self.logger('Checker for client {0} (command "{1}")'
 
222
            self.logger('Checker for client %s (command "%s")'
227
223
                        ' dumped core'
228
 
                        .format(self.properties["Name"], command))
 
224
                        % (self.properties["Name"], command))
229
225
        else:
230
 
            self.logger('Checker for client {0} completed'
231
 
                        ' mysteriously'
232
 
                        .format(self.properties["Name"]))
 
226
            self.logger('Checker for client %s completed'
 
227
                        ' mysteriously')
233
228
        self.update()
234
229
    
235
230
    def checker_started(self, command):
236
231
        """Server signals that a checker started. This could be useful
237
232
           to log in the future. """
238
 
        #self.logger('Client {0} started checker "{1}"'
239
 
        #            .format(self.properties["Name"],
240
 
        #                    unicode(command)))
 
233
        #self.logger('Client %s started checker "%s"'
 
234
        #            % (self.properties["Name"], unicode(command)))
241
235
        pass
242
236
    
243
237
    def got_secret(self):
244
 
        self.logger('Client {0} received its secret'
245
 
                    .format(self.properties["Name"]))
 
238
        self.logger('Client %s received its secret'
 
239
                    % self.properties["Name"])
246
240
    
247
241
    def need_approval(self, timeout, default):
248
242
        if not default:
249
 
            message = 'Client {0} needs approval within {1} seconds'
 
243
            message = 'Client %s needs approval within %s seconds'
250
244
        else:
251
 
            message = 'Client {0} will get its secret in {1} seconds'
252
 
        self.logger(message.format(self.properties["Name"],
253
 
                                   timeout/1000))
 
245
            message = 'Client %s will get its secret in %s seconds'
 
246
        self.logger(message
 
247
                    % (self.properties["Name"], timeout/1000))
254
248
        self.using_timer(True)
255
249
    
256
250
    def rejected(self, reason):
257
 
        self.logger('Client {0} was rejected; reason: {1}'
258
 
                    .format(self.properties["Name"], reason))
 
251
        self.logger('Client %s was rejected; reason: %s'
 
252
                    % (self.properties["Name"], reason))
259
253
    
260
254
    def selectable(self):
261
255
        """Make this a "selectable" widget.
287
281
        # Rebuild focus and non-focus widgets using current properties
288
282
 
289
283
        # Base part of a client. Name!
290
 
        base = '{name}: '.format(name=self.properties["Name"])
 
284
        base = ('%(name)s: '
 
285
                      % {"name": self.properties["Name"]})
291
286
        if not self.properties["Enabled"]:
292
287
            message = "DISABLED"
293
288
        elif self.properties["ApprovalPending"]:
302
297
            else:
303
298
                timer = datetime.timedelta()
304
299
            if self.properties["ApprovedByDefault"]:
305
 
                message = "Approval in {0}. (d)eny?"
 
300
                message = "Approval in %s. (d)eny?"
306
301
            else:
307
 
                message = "Denial in {0}. (a)pprove?"
308
 
            message = message.format(unicode(timer).rsplit(".", 1)[0])
 
302
                message = "Denial in %s. (a)pprove?"
 
303
            message = message % unicode(timer).rsplit(".", 1)[0]
309
304
        elif self.properties["LastCheckerStatus"] != 0:
310
 
            # When checker has failed, show timer until client expires
 
305
            # When checker has failed, print a timer until client expires
311
306
            expires = self.properties["Expires"]
312
307
            if expires == "":
313
308
                timer = datetime.timedelta(0)
314
309
            else:
315
 
                expires = (datetime.datetime.strptime
316
 
                           (expires, '%Y-%m-%dT%H:%M:%S.%f'))
 
310
                expires = datetime.datetime.strptime(expires,
 
311
                                                     '%Y-%m-%dT%H:%M:%S.%f')
317
312
                timer = expires - datetime.datetime.utcnow()
318
313
            message = ('A checker has failed! Time until client'
319
 
                       ' gets disabled: {0}'
320
 
                       .format(unicode(timer).rsplit(".", 1)[0]))
 
314
                       ' gets disabled: %s'
 
315
                           % unicode(timer).rsplit(".", 1)[0])
321
316
        else:
322
317
            message = "enabled"
323
 
        self._text = "{0}{1}".format(base, message)
 
318
        self._text = "%s%s" % (base, message)
324
319
            
325
320
        if not urwid.supports_unicode():
326
321
            self._text = self._text.encode("ascii", "replace")
491
486
        self.main_loop = gobject.MainLoop()
492
487
    
493
488
    def client_not_found(self, fingerprint, address):
494
 
        self.log_message("Client with address {0} and fingerprint"
495
 
                         " {1} could not be found"
496
 
                         .format(address, fingerprint))
 
489
        self.log_message(("Client with address %s and fingerprint %s"
 
490
                          " could not be found" % (address,
 
491
                                                    fingerprint)))
497
492
    
498
493
    def rebuild(self):
499
494
        """This rebuilds the User Interface.
552
547
            client = self.clients_dict[path]
553
548
        except KeyError:
554
549
            # not found?
555
 
            self.log_message("Unknown client {0!r} ({1!r}) removed"
556
 
                             .format(name, path))
 
550
            self.log_message("Unknown client %r (%r) removed", name,
 
551
                             path)
557
552
            return
558
553
        client.delete()
559
554