/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: 2014-07-25 23:39:49 UTC
  • mto: This revision was merged to the branch mainline in revision 724.
  • Revision ID: teddy@recompile.se-20140725233949-rcg0bbj7q3uweh6p
Use the new .total_seconds() method on datetime.timedelta objects.

* mandos (timedelta_to_milliseconds): Removed.  All callers changed to
                                      use ".total_seconds() * 1000".
  (Client.timeout_milliseconds): - '' -
  (Client.extended_timeout_milliseconds): - '' -
  (Client.interval_milliseconds): - '' -
  (Client.approval_delay_milliseconds): - '' -
* mandos-ctl (timedelta_to_milliseconds): - '' -

Show diffs side-by-side

added added

removed removed

Lines of Context:
401
401
        return ret
402
402
 
403
403
 
404
 
def timedelta_to_milliseconds(td):
405
 
    "Convert a datetime.timedelta() to milliseconds"
406
 
    return ((td.days * 24 * 60 * 60 * 1000)
407
 
            + (td.seconds * 1000)
408
 
            + (td.microseconds // 1000))
409
 
 
410
 
 
411
404
class Client(object):
412
405
    """A representation of a client host served by this server.
413
406
    
468
461
                        "enabled": "True",
469
462
                        }
470
463
    
471
 
    def timeout_milliseconds(self):
472
 
        "Return the 'timeout' attribute in milliseconds"
473
 
        return timedelta_to_milliseconds(self.timeout)
474
 
    
475
 
    def extended_timeout_milliseconds(self):
476
 
        "Return the 'extended_timeout' attribute in milliseconds"
477
 
        return timedelta_to_milliseconds(self.extended_timeout)
478
 
    
479
 
    def interval_milliseconds(self):
480
 
        "Return the 'interval' attribute in milliseconds"
481
 
        return timedelta_to_milliseconds(self.interval)
482
 
    
483
 
    def approval_delay_milliseconds(self):
484
 
        return timedelta_to_milliseconds(self.approval_delay)
485
 
    
486
464
    @staticmethod
487
465
    def config_parser(config):
488
466
        """Construct a new dict of client settings of this form:
625
603
        if self.checker_initiator_tag is not None:
626
604
            gobject.source_remove(self.checker_initiator_tag)
627
605
        self.checker_initiator_tag = (gobject.timeout_add
628
 
                                      (self.interval_milliseconds(),
 
606
                                      (int(self.interval
 
607
                                           .total_seconds() * 1000),
629
608
                                       self.start_checker))
630
609
        # Schedule a disable() when 'timeout' has passed
631
610
        if self.disable_initiator_tag is not None:
632
611
            gobject.source_remove(self.disable_initiator_tag)
633
612
        self.disable_initiator_tag = (gobject.timeout_add
634
 
                                   (self.timeout_milliseconds(),
635
 
                                    self.disable))
 
613
                                      (int(self.timeout
 
614
                                           .total_seconds() * 1000),
 
615
                                       self.disable))
636
616
        # Also start a new checker *right now*.
637
617
        self.start_checker()
638
618
    
669
649
            self.disable_initiator_tag = None
670
650
        if getattr(self, "enabled", False):
671
651
            self.disable_initiator_tag = (gobject.timeout_add
672
 
                                          (timedelta_to_milliseconds
673
 
                                           (timeout), self.disable))
 
652
                                          (int(timeout.total_seconds()
 
653
                                               * 1000), self.disable))
674
654
            self.expires = datetime.datetime.utcnow() + timeout
675
655
    
676
656
    def need_approval(self):
1305
1285
    approval_delay = notifychangeproperty(dbus.UInt64,
1306
1286
                                          "ApprovalDelay",
1307
1287
                                          type_func =
1308
 
                                          timedelta_to_milliseconds)
 
1288
                                          lambda td: td.total_seconds()
 
1289
                                          * 1000)
1309
1290
    approval_duration = notifychangeproperty(
1310
1291
        dbus.UInt64, "ApprovalDuration",
1311
 
        type_func = timedelta_to_milliseconds)
 
1292
        type_func = lambda td: td.total_seconds() * 1000)
1312
1293
    host = notifychangeproperty(dbus.String, "Host")
1313
1294
    timeout = notifychangeproperty(dbus.UInt64, "Timeout",
1314
 
                                   type_func =
1315
 
                                   timedelta_to_milliseconds)
 
1295
                                   type_func = lambda td:
 
1296
                                       td.total_seconds() * 1000)
1316
1297
    extended_timeout = notifychangeproperty(
1317
1298
        dbus.UInt64, "ExtendedTimeout",
1318
 
        type_func = timedelta_to_milliseconds)
 
1299
        type_func = lambda td: td.total_seconds() * 1000)
1319
1300
    interval = notifychangeproperty(dbus.UInt64,
1320
1301
                                    "Interval",
1321
1302
                                    type_func =
1322
 
                                    timedelta_to_milliseconds)
 
1303
                                    lambda td: td.total_seconds()
 
1304
                                    * 1000)
1323
1305
    checker_command = notifychangeproperty(dbus.String, "Checker")
1324
1306
    
1325
1307
    del notifychangeproperty
1368
1350
    
1369
1351
    def approve(self, value=True):
1370
1352
        self.approved = value
1371
 
        gobject.timeout_add(timedelta_to_milliseconds
1372
 
                            (self.approval_duration),
1373
 
                            self._reset_approved)
 
1353
        gobject.timeout_add(int(self.approval_duration.total_seconds()
 
1354
                                * 1000), self._reset_approved)
1374
1355
        self.send_changedstate()
1375
1356
    
1376
1357
    ## D-Bus methods, signals & properties
1479
1460
                           access="readwrite")
1480
1461
    def ApprovalDelay_dbus_property(self, value=None):
1481
1462
        if value is None:       # get
1482
 
            return dbus.UInt64(self.approval_delay_milliseconds())
 
1463
            return dbus.UInt64(self.approval_delay.total_seconds()
 
1464
                               * 1000)
1483
1465
        self.approval_delay = datetime.timedelta(0, 0, 0, value)
1484
1466
    
1485
1467
    # ApprovalDuration - property
1487
1469
                           access="readwrite")
1488
1470
    def ApprovalDuration_dbus_property(self, value=None):
1489
1471
        if value is None:       # get
1490
 
            return dbus.UInt64(timedelta_to_milliseconds(
1491
 
                    self.approval_duration))
 
1472
            return dbus.UInt64(self.approval_duration.total_seconds()
 
1473
                               * 1000)
1492
1474
        self.approval_duration = datetime.timedelta(0, 0, 0, value)
1493
1475
    
1494
1476
    # Name - property
1560
1542
                           access="readwrite")
1561
1543
    def Timeout_dbus_property(self, value=None):
1562
1544
        if value is None:       # get
1563
 
            return dbus.UInt64(self.timeout_milliseconds())
 
1545
            return dbus.UInt64(self.timeout.total_seconds() * 1000)
1564
1546
        old_timeout = self.timeout
1565
1547
        self.timeout = datetime.timedelta(0, 0, 0, value)
1566
1548
        # Reschedule disabling
1577
1559
                gobject.source_remove(self.disable_initiator_tag)
1578
1560
                self.disable_initiator_tag = (
1579
1561
                    gobject.timeout_add(
1580
 
                        timedelta_to_milliseconds(self.expires - now),
1581
 
                        self.disable))
 
1562
                        int((self.expires - now).total_seconds()
 
1563
                            * 1000), self.disable))
1582
1564
    
1583
1565
    # ExtendedTimeout - property
1584
1566
    @dbus_service_property(_interface, signature="t",
1585
1567
                           access="readwrite")
1586
1568
    def ExtendedTimeout_dbus_property(self, value=None):
1587
1569
        if value is None:       # get
1588
 
            return dbus.UInt64(self.extended_timeout_milliseconds())
 
1570
            return dbus.UInt64(self.extended_timeout.total_seconds()
 
1571
                               * 1000)
1589
1572
        self.extended_timeout = datetime.timedelta(0, 0, 0, value)
1590
1573
    
1591
1574
    # Interval - property
1593
1576
                           access="readwrite")
1594
1577
    def Interval_dbus_property(self, value=None):
1595
1578
        if value is None:       # get
1596
 
            return dbus.UInt64(self.interval_milliseconds())
 
1579
            return dbus.UInt64(self.interval.total_seconds() * 1000)
1597
1580
        self.interval = datetime.timedelta(0, 0, 0, value)
1598
1581
        if getattr(self, "checker_initiator_tag", None) is None:
1599
1582
            return
1759
1742
                        if self.server.use_dbus:
1760
1743
                            # Emit D-Bus signal
1761
1744
                            client.NeedApproval(
1762
 
                                client.approval_delay_milliseconds(),
1763
 
                                client.approved_by_default)
 
1745
                                client.approval_delay.total_seconds()
 
1746
                                * 1000, client.approved_by_default)
1764
1747
                    else:
1765
1748
                        logger.warning("Client %s was not approved",
1766
1749
                                       client.name)
1772
1755
                    #wait until timeout or approved
1773
1756
                    time = datetime.datetime.now()
1774
1757
                    client.changedstate.acquire()
1775
 
                    client.changedstate.wait(
1776
 
                        float(timedelta_to_milliseconds(delay)
1777
 
                              / 1000))
 
1758
                    client.changedstate.wait(delay.total_seconds())
1778
1759
                    client.changedstate.release()
1779
1760
                    time2 = datetime.datetime.now()
1780
1761
                    if (time2 - time) >= delay: