240
240
"""A representation of a client host served by this server.
243
name: string; from the config file, used in log messages and
243
_approved: bool(); 'None' if not yet approved/disapproved
244
approval_delay: datetime.timedelta(); Time to wait for approval
245
approval_duration: datetime.timedelta(); Duration of one approval
246
checker: subprocess.Popen(); a running checker process used
247
to see if the client lives.
248
'None' if no process is running.
249
checker_callback_tag: a gobject event source tag, or None
250
checker_command: string; External command which is run to check
251
if client lives. %() expansions are done at
252
runtime with vars(self) as dict, so that for
253
instance %(name)s can be used in the command.
254
checker_initiator_tag: a gobject event source tag, or None
255
created: datetime.datetime(); (UTC) object creation
256
current_checker_command: string; current running checker_command
257
disable_hook: If set, called by disable() as disable_hook(self)
258
disable_initiator_tag: a gobject event source tag, or None
245
260
fingerprint: string (40 or 32 hexadecimal digits); used to
246
261
uniquely identify the client
247
secret: bytestring; sent verbatim (over TLS) to client
248
262
host: string; available for use by the checker command
249
created: datetime.datetime(); (UTC) object creation
263
interval: datetime.timedelta(); How often to start a new checker
264
last_approval_request: datetime.datetime(); (UTC) or None
265
last_checked_ok: datetime.datetime(); (UTC) or None
250
266
last_enabled: datetime.datetime(); (UTC)
252
last_checked_ok: datetime.datetime(); (UTC) or None
267
name: string; from the config file, used in log messages and
269
secret: bytestring; sent verbatim (over TLS) to client
253
270
timeout: datetime.timedelta(); How long from last_checked_ok
254
271
until this client is disabled
255
interval: datetime.timedelta(); How often to start a new checker
256
disable_hook: If set, called by disable() as disable_hook(self)
257
checker: subprocess.Popen(); a running checker process used
258
to see if the client lives.
259
'None' if no process is running.
260
checker_initiator_tag: a gobject event source tag, or None
261
disable_initiator_tag: - '' -
262
checker_callback_tag: - '' -
263
checker_command: string; External command which is run to check if
264
client lives. %() expansions are done at
265
runtime with vars(self) as dict, so that for
266
instance %(name)s can be used in the command.
267
current_checker_command: string; current running checker_command
268
approved_delay: datetime.timedelta(); Time to wait for approval
269
_approved: bool(); 'None' if not yet approved/disapproved
270
approved_duration: datetime.timedelta(); Duration of one approval
272
runtime_expansions: Allowed attributes for runtime expansion.
275
runtime_expansions = (u"approval_delay", u"approval_duration",
276
u"created", u"enabled", u"fingerprint",
277
u"host", u"interval", u"last_checked_ok",
278
u"last_enabled", u"name", u"timeout")
274
281
def _timedelta_to_milliseconds(td):
275
282
"Convert a datetime.timedelta() to milliseconds"
796
815
r = Client.checked_ok(self, *args, **kwargs)
797
816
# Emit D-Bus signal
798
817
self.PropertyChanged(
799
dbus.String(u"last_checked_ok"),
818
dbus.String(u"LastCheckedOK"),
800
819
(self._datetime_to_dbus(self.last_checked_ok,
801
820
variant_level=1)))
823
def need_approval(self, *args, **kwargs):
824
r = Client.need_approval(self, *args, **kwargs)
826
self.PropertyChanged(
827
dbus.String(u"LastApprovalRequest"),
828
(self._datetime_to_dbus(self.last_approval_request,
804
832
def start_checker(self, *args, **kwargs):
805
833
old_checker = self.checker
806
834
if self.checker is not None:
922
# approved_pending - property
951
# ApprovalPending - property
923
952
@dbus_service_property(_interface, signature=u"b", access=u"read")
924
def approved_pending_dbus_property(self):
953
def ApprovalPending_dbus_property(self):
925
954
return dbus.Boolean(bool(self.approvals_pending))
927
# approved_by_default - property
956
# ApprovedByDefault - property
928
957
@dbus_service_property(_interface, signature=u"b",
929
958
access=u"readwrite")
930
def approved_by_default_dbus_property(self):
931
return dbus.Boolean(self.approved_by_default)
933
# approved_delay - property
934
@dbus_service_property(_interface, signature=u"t",
936
def approved_delay_dbus_property(self):
937
return dbus.UInt64(self.approved_delay_milliseconds())
939
# approved_duration - property
940
@dbus_service_property(_interface, signature=u"t",
942
def approved_duration_dbus_property(self):
943
return dbus.UInt64(self._timedelta_to_milliseconds(
944
self.approved_duration))
959
def ApprovedByDefault_dbus_property(self, value=None):
960
if value is None: # get
961
return dbus.Boolean(self.approved_by_default)
962
self.approved_by_default = bool(value)
964
self.PropertyChanged(dbus.String(u"ApprovedByDefault"),
965
dbus.Boolean(value, variant_level=1))
967
# ApprovalDelay - property
968
@dbus_service_property(_interface, signature=u"t",
970
def ApprovalDelay_dbus_property(self, value=None):
971
if value is None: # get
972
return dbus.UInt64(self.approval_delay_milliseconds())
973
self.approval_delay = datetime.timedelta(0, 0, 0, value)
975
self.PropertyChanged(dbus.String(u"ApprovalDelay"),
976
dbus.UInt64(value, variant_level=1))
978
# ApprovalDuration - property
979
@dbus_service_property(_interface, signature=u"t",
981
def ApprovalDuration_dbus_property(self, value=None):
982
if value is None: # get
983
return dbus.UInt64(self._timedelta_to_milliseconds(
984
self.approval_duration))
985
self.approval_duration = datetime.timedelta(0, 0, 0, value)
987
self.PropertyChanged(dbus.String(u"ApprovalDuration"),
988
dbus.UInt64(value, variant_level=1))
947
991
@dbus_service_property(_interface, signature=u"s", access=u"read")
948
def name_dbus_property(self):
992
def Name_dbus_property(self):
949
993
return dbus.String(self.name)
951
# fingerprint - property
995
# Fingerprint - property
952
996
@dbus_service_property(_interface, signature=u"s", access=u"read")
953
def fingerprint_dbus_property(self):
997
def Fingerprint_dbus_property(self):
954
998
return dbus.String(self.fingerprint)
957
1001
@dbus_service_property(_interface, signature=u"s",
958
1002
access=u"readwrite")
959
def host_dbus_property(self, value=None):
1003
def Host_dbus_property(self, value=None):
960
1004
if value is None: # get
961
1005
return dbus.String(self.host)
962
1006
self.host = value
963
1007
# Emit D-Bus signal
964
self.PropertyChanged(dbus.String(u"host"),
1008
self.PropertyChanged(dbus.String(u"Host"),
965
1009
dbus.String(value, variant_level=1))
1011
# Created - property
968
1012
@dbus_service_property(_interface, signature=u"s", access=u"read")
969
def created_dbus_property(self):
1013
def Created_dbus_property(self):
970
1014
return dbus.String(self._datetime_to_dbus(self.created))
972
# last_enabled - property
1016
# LastEnabled - property
973
1017
@dbus_service_property(_interface, signature=u"s", access=u"read")
974
def last_enabled_dbus_property(self):
1018
def LastEnabled_dbus_property(self):
975
1019
if self.last_enabled is None:
976
1020
return dbus.String(u"")
977
1021
return dbus.String(self._datetime_to_dbus(self.last_enabled))
1023
# Enabled - property
980
1024
@dbus_service_property(_interface, signature=u"b",
981
1025
access=u"readwrite")
982
def enabled_dbus_property(self, value=None):
1026
def Enabled_dbus_property(self, value=None):
983
1027
if value is None: # get
984
1028
return dbus.Boolean(self.enabled)
999
1043
return dbus.String(self._datetime_to_dbus(self
1000
1044
.last_checked_ok))
1002
# timeout - property
1046
# LastApprovalRequest - property
1047
@dbus_service_property(_interface, signature=u"s", access=u"read")
1048
def LastApprovalRequest_dbus_property(self):
1049
if self.last_approval_request is None:
1050
return dbus.String(u"")
1051
return dbus.String(self.
1052
_datetime_to_dbus(self
1053
.last_approval_request))
1055
# Timeout - property
1003
1056
@dbus_service_property(_interface, signature=u"t",
1004
1057
access=u"readwrite")
1005
def timeout_dbus_property(self, value=None):
1058
def Timeout_dbus_property(self, value=None):
1006
1059
if value is None: # get
1007
1060
return dbus.UInt64(self.timeout_milliseconds())
1008
1061
self.timeout = datetime.timedelta(0, 0, 0, value)
1009
1062
# Emit D-Bus signal
1010
self.PropertyChanged(dbus.String(u"timeout"),
1063
self.PropertyChanged(dbus.String(u"Timeout"),
1011
1064
dbus.UInt64(value, variant_level=1))
1012
1065
if getattr(self, u"disable_initiator_tag", None) is None:
1027
1080
self.disable_initiator_tag = (gobject.timeout_add
1028
1081
(time_to_die, self.disable))
1030
# interval - property
1083
# Interval - property
1031
1084
@dbus_service_property(_interface, signature=u"t",
1032
1085
access=u"readwrite")
1033
def interval_dbus_property(self, value=None):
1086
def Interval_dbus_property(self, value=None):
1034
1087
if value is None: # get
1035
1088
return dbus.UInt64(self.interval_milliseconds())
1036
1089
self.interval = datetime.timedelta(0, 0, 0, value)
1037
1090
# Emit D-Bus signal
1038
self.PropertyChanged(dbus.String(u"interval"),
1091
self.PropertyChanged(dbus.String(u"Interval"),
1039
1092
dbus.UInt64(value, variant_level=1))
1040
1093
if getattr(self, u"checker_initiator_tag", None) is None:
1045
1098
(value, self.start_checker))
1046
1099
self.start_checker() # Start one now, too
1048
# checker - property
1101
# Checker - property
1049
1102
@dbus_service_property(_interface, signature=u"s",
1050
1103
access=u"readwrite")
1051
def checker_dbus_property(self, value=None):
1104
def Checker_dbus_property(self, value=None):
1052
1105
if value is None: # get
1053
1106
return dbus.String(self.checker_command)
1054
1107
self.checker_command = value
1055
1108
# Emit D-Bus signal
1056
self.PropertyChanged(dbus.String(u"checker"),
1109
self.PropertyChanged(dbus.String(u"Checker"),
1057
1110
dbus.String(self.checker_command,
1058
1111
variant_level=1))
1060
# checker_running - property
1113
# CheckerRunning - property
1061
1114
@dbus_service_property(_interface, signature=u"b",
1062
1115
access=u"readwrite")
1063
def checker_running_dbus_property(self, value=None):
1116
def CheckerRunning_dbus_property(self, value=None):
1064
1117
if value is None: # get
1065
1118
return dbus.Boolean(self.checker is not None)
1194
1247
client.Rejected("Disabled")
1197
if client._approved or not client.approved_delay:
1250
if client._approved or not client.approval_delay:
1198
1251
#We are approved or approval is disabled
1200
1253
elif client._approved is None:
1201
logger.info(u"Client %s need approval",
1254
logger.info(u"Client %s needs approval",
1203
1256
if self.server.use_dbus:
1204
1257
# Emit D-Bus signal
1205
1258
client.NeedApproval(
1206
client.approved_delay_milliseconds(),
1259
client.approval_delay_milliseconds(),
1207
1260
client.approved_by_default)
1209
1262
logger.warning(u"Client %s was not approved",
1211
1264
if self.server.use_dbus:
1212
1265
# Emit D-Bus signal
1213
client.Rejected("Disapproved")
1266
client.Rejected("Denied")
1216
1269
#wait until timeout or approved
1733
1782
tcp_server = MandosServer((server_settings[u"address"],
1734
1783
server_settings[u"port"]),
1736
interface=server_settings[u"interface"],
1785
interface=(server_settings[u"interface"]
1737
1787
use_ipv6=use_ipv6,
1738
1788
gnutls_priority=
1739
1789
server_settings[u"priority"],
1740
1790
use_dbus=use_dbus)
1741
pidfilename = u"/var/run/mandos.pid"
1743
pidfile = open(pidfilename, u"w")
1745
logger.error(u"Could not open file %r", pidfilename)
1792
pidfilename = u"/var/run/mandos.pid"
1794
pidfile = open(pidfilename, u"w")
1796
logger.error(u"Could not open file %r", pidfilename)
1748
1799
uid = pwd.getpwnam(u"_mandos").pw_uid