/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

  • Committer: Teddy Hogeborn
  • Date: 2012-05-05 10:15:01 UTC
  • Revision ID: teddy@recompile.se-20120505101501-czfhh1p0tm3tbvkh
* mandos: White space and other misc. format fixes only.

Show diffs side-by-side

added added

removed removed

Lines of Context:
209
209
        return decrypted_plaintext
210
210
 
211
211
 
212
 
 
213
212
class AvahiError(Exception):
214
213
    def __init__(self, value, *args, **kwargs):
215
214
        self.value = value
244
243
    server: D-Bus Server
245
244
    bus: dbus.SystemBus()
246
245
    """
 
246
    
247
247
    def __init__(self, interface = avahi.IF_UNSPEC, name = None,
248
248
                 servicetype = None, port = None, TXT = None,
249
249
                 domain = "", host = "", max_renames = 32768,
262
262
        self.server = None
263
263
        self.bus = bus
264
264
        self.entry_group_state_changed_match = None
 
265
    
265
266
    def rename(self):
266
267
        """Derived from the Avahi example code"""
267
268
        if self.rename_count >= self.max_renames:
281
282
            self.cleanup()
282
283
            os._exit(1)
283
284
        self.rename_count += 1
 
285
    
284
286
    def remove(self):
285
287
        """Derived from the Avahi example code"""
286
288
        if self.entry_group_state_changed_match is not None:
288
290
            self.entry_group_state_changed_match = None
289
291
        if self.group is not None:
290
292
            self.group.Reset()
 
293
    
291
294
    def add(self):
292
295
        """Derived from the Avahi example code"""
293
296
        self.remove()
310
313
            dbus.UInt16(self.port),
311
314
            avahi.string_array_to_txt_array(self.TXT))
312
315
        self.group.Commit()
 
316
    
313
317
    def entry_group_state_changed(self, state, error):
314
318
        """Derived from the Avahi example code"""
315
319
        logger.debug("Avahi entry group state change: %i", state)
324
328
                            unicode(error))
325
329
            raise AvahiGroupError("State changed: {0!s}"
326
330
                                  .format(error))
 
331
    
327
332
    def cleanup(self):
328
333
        """Derived from the Avahi example code"""
329
334
        if self.group is not None:
334
339
                pass
335
340
            self.group = None
336
341
        self.remove()
 
342
    
337
343
    def server_state_changed(self, state, error=None):
338
344
        """Derived from the Avahi example code"""
339
345
        logger.debug("Avahi server state change: %i", state)
358
364
                logger.debug("Unknown state: %r", state)
359
365
            else:
360
366
                logger.debug("Unknown state: %r: %r", state, error)
 
367
    
361
368
    def activate(self):
362
369
        """Derived from the Avahi example code"""
363
370
        if self.server is None:
385
392
    return ((td.days * 24 * 60 * 60 * 1000)
386
393
            + (td.seconds * 1000)
387
394
            + (td.microseconds // 1000))
388
 
        
 
395
 
389
396
class Client(object):
390
397
    """A representation of a client host served by this server.
391
398
    
458
465
    
459
466
    def approval_delay_milliseconds(self):
460
467
        return timedelta_to_milliseconds(self.approval_delay)
461
 
 
 
468
    
462
469
    @staticmethod
463
470
    def config_parser(config):
464
471
        """Construct a new dict of client settings of this form:
505
512
            client["last_checker_status"] = -2
506
513
        
507
514
        return settings
508
 
        
509
 
        
 
515
    
510
516
    def __init__(self, settings, name = None):
511
 
        """Note: the 'checker' key in 'config' sets the
512
 
        'checker_command' attribute and *not* the 'checker'
513
 
        attribute."""
514
517
        self.name = name
515
518
        # adding all client settings
516
519
        for setting, value in settings.iteritems():
525
528
        else:
526
529
            self.last_enabled = None
527
530
            self.expires = None
528
 
       
 
531
        
529
532
        logger.debug("Creating client %r", self.name)
530
533
        # Uppercase and remove spaces from fingerprint for later
531
534
        # comparison purposes with return value from the fingerprint()
533
536
        logger.debug("  Fingerprint: %s", self.fingerprint)
534
537
        self.created = settings.get("created",
535
538
                                    datetime.datetime.utcnow())
536
 
 
 
539
        
537
540
        # attributes specific for this server instance
538
541
        self.checker = None
539
542
        self.checker_initiator_tag = None
773
776
 
774
777
 
775
778
def dbus_interface_annotations(dbus_interface):
776
 
    """Decorator for marking functions returning interface annotations.
 
779
    """Decorator for marking functions returning interface annotations
777
780
    
778
781
    Usage:
779
782
    
1149
1152
                                        attribute.func_defaults,
1150
1153
                                        attribute.func_closure)))
1151
1154
        # Deprecate all old interfaces
1152
 
        basename="_AlternateDBusNamesMetaclass_interface_annotation{0}"
 
1155
        iname="_AlternateDBusNamesMetaclass_interface_annotation{0}"
1153
1156
        for old_interface_name in old_interface_names:
1154
1157
            @dbus_interface_annotations(old_interface_name)
1155
1158
            def func(self):
1156
1159
                return { "org.freedesktop.DBus.Deprecated": "true" }
1157
1160
            # Find an unused name
1158
 
            for aname in (basename.format(i) for i in
1159
 
                          itertools.count()):
 
1161
            for aname in (iname.format(i) for i in itertools.count()):
1160
1162
                if aname not in attr:
1161
1163
                    attr[aname] = func
1162
1164
                    break
1188
1190
                                 ("/clients/" + client_object_name))
1189
1191
        DBusObjectWithProperties.__init__(self, self.bus,
1190
1192
                                          self.dbus_object_path)
1191
 
        
 
1193
    
1192
1194
    def notifychangeproperty(transform_func,
1193
1195
                             dbus_name, type_func=lambda x: x,
1194
1196
                             variant_level=1):
1217
1219
        
1218
1220
        return property(lambda self: getattr(self, attrname), setter)
1219
1221
    
1220
 
    
1221
1222
    expires = notifychangeproperty(datetime_to_dbus, "Expires")
1222
1223
    approvals_pending = notifychangeproperty(dbus.Boolean,
1223
1224
                                             "ApprovalPending",
1311
1312
                            (self.approval_duration),
1312
1313
                            self._reset_approved)
1313
1314
    
1314
 
    
1315
1315
    ## D-Bus methods, signals & properties
1316
1316
    _interface = "se.recompile.Mandos.Client"
1317
1317
    
2414
2414
                             .format(client_name))
2415
2415
                client["secret"] = (
2416
2416
                    client_settings[client_name]["secret"])
2417
 
 
2418
2417
    
2419
2418
    # Add/remove clients based on new changes made to config
2420
2419
    for client_name in (set(old_client_settings)
2423
2422
    for client_name in (set(client_settings)
2424
2423
                        - set(old_client_settings)):
2425
2424
        clients_data[client_name] = client_settings[client_name]
2426
 
 
 
2425
    
2427
2426
    # Create all client objects
2428
2427
    for client_name, client in clients_data.iteritems():
2429
2428
        tcp_server.clients[client_name] = client_class(
2431
2430
    
2432
2431
    if not tcp_server.clients:
2433
2432
        logger.warning("No clients defined")
2434
 
        
 
2433
    
2435
2434
    if not debug:
2436
2435
        try:
2437
2436
            with pidfile: