/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

* mandos: Use all new builtins.
* mandos-ctl: - '' -
* mandos-monitor: - '' -

Show diffs side-by-side

added added

removed removed

Lines of Context:
572
572
        if getattr(self, "enabled", False):
573
573
            # Already enabled
574
574
            return
 
575
        self.send_changedstate()
575
576
        self.expires = datetime.datetime.utcnow() + self.timeout
576
577
        self.enabled = True
577
578
        self.last_enabled = datetime.datetime.utcnow()
578
579
        self.init_checker()
579
 
        self.send_changedstate()
580
580
    
581
581
    def disable(self, quiet=True):
582
582
        """Disable this client."""
583
583
        if not getattr(self, "enabled", False):
584
584
            return False
585
585
        if not quiet:
 
586
            self.send_changedstate()
 
587
        if not quiet:
586
588
            logger.info("Disabling client %s", self.name)
587
 
        if getattr(self, "disable_initiator_tag", None) is not None:
 
589
        if getattr(self, "disable_initiator_tag", False):
588
590
            gobject.source_remove(self.disable_initiator_tag)
589
591
            self.disable_initiator_tag = None
590
592
        self.expires = None
591
 
        if getattr(self, "checker_initiator_tag", None) is not None:
 
593
        if getattr(self, "checker_initiator_tag", False):
592
594
            gobject.source_remove(self.checker_initiator_tag)
593
595
            self.checker_initiator_tag = None
594
596
        self.stop_checker()
595
597
        self.enabled = False
596
 
        if not quiet:
597
 
            self.send_changedstate()
598
598
        # Do not run this again if called by a gobject.timeout_add
599
599
        return False
600
600
    
604
604
    def init_checker(self):
605
605
        # Schedule a new checker to be started an 'interval' from now,
606
606
        # and every interval from then on.
607
 
        if self.checker_initiator_tag is not None:
608
 
            gobject.source_remove(self.checker_initiator_tag)
609
607
        self.checker_initiator_tag = (gobject.timeout_add
610
608
                                      (self.interval_milliseconds(),
611
609
                                       self.start_checker))
612
610
        # Schedule a disable() when 'timeout' has passed
613
 
        if self.disable_initiator_tag is not None:
614
 
            gobject.source_remove(self.disable_initiator_tag)
615
611
        self.disable_initiator_tag = (gobject.timeout_add
616
612
                                   (self.timeout_milliseconds(),
617
613
                                    self.disable))
648
644
            timeout = self.timeout
649
645
        if self.disable_initiator_tag is not None:
650
646
            gobject.source_remove(self.disable_initiator_tag)
651
 
            self.disable_initiator_tag = None
652
647
        if getattr(self, "enabled", False):
653
648
            self.disable_initiator_tag = (gobject.timeout_add
654
649
                                          (timedelta_to_milliseconds
1340
1335
        return False
1341
1336
    
1342
1337
    def approve(self, value=True):
 
1338
        self.send_changedstate()
1343
1339
        self.approved = value
1344
1340
        gobject.timeout_add(timedelta_to_milliseconds
1345
1341
                            (self.approval_duration),
1346
1342
                            self._reset_approved)
1347
 
        self.send_changedstate()
1348
1343
    
1349
1344
    ## D-Bus methods, signals & properties
1350
1345
    _interface = "se.recompile.Mandos.Client"
1534
1529
    def Timeout_dbus_property(self, value=None):
1535
1530
        if value is None:       # get
1536
1531
            return dbus.UInt64(self.timeout_milliseconds())
1537
 
        old_timeout = self.timeout
1538
1532
        self.timeout = datetime.timedelta(0, 0, 0, value)
1539
 
        # Reschedule disabling
 
1533
        # Reschedule timeout
1540
1534
        if self.enabled:
1541
1535
            now = datetime.datetime.utcnow()
1542
 
            self.expires += self.timeout - old_timeout
1543
 
            if self.expires <= now:
 
1536
            time_to_die = timedelta_to_milliseconds(
 
1537
                (self.last_checked_ok + self.timeout) - now)
 
1538
            if time_to_die <= 0:
1544
1539
                # The timeout has passed
1545
1540
                self.disable()
1546
1541
            else:
 
1542
                self.expires = (now +
 
1543
                                datetime.timedelta(milliseconds =
 
1544
                                                   time_to_die))
1547
1545
                if (getattr(self, "disable_initiator_tag", None)
1548
1546
                    is None):
1549
1547
                    return
1550
1548
                gobject.source_remove(self.disable_initiator_tag)
1551
 
                self.disable_initiator_tag = (
1552
 
                    gobject.timeout_add(
1553
 
                        timedelta_to_milliseconds(self.expires - now),
1554
 
                        self.disable))
 
1549
                self.disable_initiator_tag = (gobject.timeout_add
 
1550
                                              (time_to_die,
 
1551
                                               self.disable))
1555
1552
    
1556
1553
    # ExtendedTimeout - property
1557
1554
    @dbus_service_property(_interface, signature="t",
1745
1742
                    #wait until timeout or approved
1746
1743
                    time = datetime.datetime.now()
1747
1744
                    client.changedstate.acquire()
1748
 
                    client.changedstate.wait(
1749
 
                        float(timedelta_to_milliseconds(delay)
1750
 
                              / 1000))
 
1745
                    (client.changedstate.wait
 
1746
                     (float(client.timedelta_to_milliseconds(delay)
 
1747
                            / 1000)))
1751
1748
                    client.changedstate.release()
1752
1749
                    time2 = datetime.datetime.now()
1753
1750
                    if (time2 - time) >= delay: