235
240
"""A representation of a client host served by this server.
238
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
240
260
fingerprint: string (40 or 32 hexadecimal digits); used to
241
261
uniquely identify the client
242
secret: bytestring; sent verbatim (over TLS) to client
243
262
host: string; available for use by the checker command
244
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
245
266
last_enabled: datetime.datetime(); (UTC)
247
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
248
270
timeout: datetime.timedelta(); How long from last_checked_ok
249
271
until this client is disabled
250
interval: datetime.timedelta(); How often to start a new checker
251
disable_hook: If set, called by disable() as disable_hook(self)
252
checker: subprocess.Popen(); a running checker process used
253
to see if the client lives.
254
'None' if no process is running.
255
checker_initiator_tag: a gobject event source tag, or None
256
disable_initiator_tag: - '' -
257
checker_callback_tag: - '' -
258
checker_command: string; External command which is run to check if
259
client lives. %() expansions are done at
260
runtime with vars(self) as dict, so that for
261
instance %(name)s can be used in the command.
262
current_checker_command: string; current running checker_command
263
approved_delay: datetime.timedelta(); Time to wait for approval
264
_approved: bool(); 'None' if not yet approved/disapproved
265
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")
269
281
def _timedelta_to_milliseconds(td):
270
282
"Convert a datetime.timedelta() to milliseconds"
791
815
r = Client.checked_ok(self, *args, **kwargs)
792
816
# Emit D-Bus signal
793
817
self.PropertyChanged(
794
dbus.String(u"last_checked_ok"),
818
dbus.String(u"LastCheckedOK"),
795
819
(self._datetime_to_dbus(self.last_checked_ok,
796
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,
799
832
def start_checker(self, *args, **kwargs):
800
833
old_checker = self.checker
801
834
if self.checker is not None:
917
# approved_pending - property
951
# ApprovalPending - property
918
952
@dbus_service_property(_interface, signature=u"b", access=u"read")
919
def approved_pending_dbus_property(self):
953
def ApprovalPending_dbus_property(self):
920
954
return dbus.Boolean(bool(self.approvals_pending))
922
# approved_by_default - property
956
# ApprovedByDefault - property
923
957
@dbus_service_property(_interface, signature=u"b",
924
958
access=u"readwrite")
925
def approved_by_default_dbus_property(self):
926
return dbus.Boolean(self.approved_by_default)
928
# approved_delay - property
929
@dbus_service_property(_interface, signature=u"t",
931
def approved_delay_dbus_property(self):
932
return dbus.UInt64(self.approved_delay_milliseconds())
934
# approved_duration - property
935
@dbus_service_property(_interface, signature=u"t",
937
def approved_duration_dbus_property(self):
938
return dbus.UInt64(self._timedelta_to_milliseconds(
939
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))
942
991
@dbus_service_property(_interface, signature=u"s", access=u"read")
943
def name_dbus_property(self):
992
def Name_dbus_property(self):
944
993
return dbus.String(self.name)
946
# fingerprint - property
995
# Fingerprint - property
947
996
@dbus_service_property(_interface, signature=u"s", access=u"read")
948
def fingerprint_dbus_property(self):
997
def Fingerprint_dbus_property(self):
949
998
return dbus.String(self.fingerprint)
952
1001
@dbus_service_property(_interface, signature=u"s",
953
1002
access=u"readwrite")
954
def host_dbus_property(self, value=None):
1003
def Host_dbus_property(self, value=None):
955
1004
if value is None: # get
956
1005
return dbus.String(self.host)
957
1006
self.host = value
958
1007
# Emit D-Bus signal
959
self.PropertyChanged(dbus.String(u"host"),
1008
self.PropertyChanged(dbus.String(u"Host"),
960
1009
dbus.String(value, variant_level=1))
1011
# Created - property
963
1012
@dbus_service_property(_interface, signature=u"s", access=u"read")
964
def created_dbus_property(self):
1013
def Created_dbus_property(self):
965
1014
return dbus.String(self._datetime_to_dbus(self.created))
967
# last_enabled - property
1016
# LastEnabled - property
968
1017
@dbus_service_property(_interface, signature=u"s", access=u"read")
969
def last_enabled_dbus_property(self):
1018
def LastEnabled_dbus_property(self):
970
1019
if self.last_enabled is None:
971
1020
return dbus.String(u"")
972
1021
return dbus.String(self._datetime_to_dbus(self.last_enabled))
1023
# Enabled - property
975
1024
@dbus_service_property(_interface, signature=u"b",
976
1025
access=u"readwrite")
977
def enabled_dbus_property(self, value=None):
1026
def Enabled_dbus_property(self, value=None):
978
1027
if value is None: # get
979
1028
return dbus.Boolean(self.enabled)
994
1043
return dbus.String(self._datetime_to_dbus(self
995
1044
.last_checked_ok))
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
998
1056
@dbus_service_property(_interface, signature=u"t",
999
1057
access=u"readwrite")
1000
def timeout_dbus_property(self, value=None):
1058
def Timeout_dbus_property(self, value=None):
1001
1059
if value is None: # get
1002
1060
return dbus.UInt64(self.timeout_milliseconds())
1003
1061
self.timeout = datetime.timedelta(0, 0, 0, value)
1004
1062
# Emit D-Bus signal
1005
self.PropertyChanged(dbus.String(u"timeout"),
1063
self.PropertyChanged(dbus.String(u"Timeout"),
1006
1064
dbus.UInt64(value, variant_level=1))
1007
1065
if getattr(self, u"disable_initiator_tag", None) is None:
1022
1080
self.disable_initiator_tag = (gobject.timeout_add
1023
1081
(time_to_die, self.disable))
1025
# interval - property
1083
# Interval - property
1026
1084
@dbus_service_property(_interface, signature=u"t",
1027
1085
access=u"readwrite")
1028
def interval_dbus_property(self, value=None):
1086
def Interval_dbus_property(self, value=None):
1029
1087
if value is None: # get
1030
1088
return dbus.UInt64(self.interval_milliseconds())
1031
1089
self.interval = datetime.timedelta(0, 0, 0, value)
1032
1090
# Emit D-Bus signal
1033
self.PropertyChanged(dbus.String(u"interval"),
1091
self.PropertyChanged(dbus.String(u"Interval"),
1034
1092
dbus.UInt64(value, variant_level=1))
1035
1093
if getattr(self, u"checker_initiator_tag", None) is None:
1040
1098
(value, self.start_checker))
1041
1099
self.start_checker() # Start one now, too
1043
# checker - property
1101
# Checker - property
1044
1102
@dbus_service_property(_interface, signature=u"s",
1045
1103
access=u"readwrite")
1046
def checker_dbus_property(self, value=None):
1104
def Checker_dbus_property(self, value=None):
1047
1105
if value is None: # get
1048
1106
return dbus.String(self.checker_command)
1049
1107
self.checker_command = value
1050
1108
# Emit D-Bus signal
1051
self.PropertyChanged(dbus.String(u"checker"),
1109
self.PropertyChanged(dbus.String(u"Checker"),
1052
1110
dbus.String(self.checker_command,
1053
1111
variant_level=1))
1055
# checker_running - property
1113
# CheckerRunning - property
1056
1114
@dbus_service_property(_interface, signature=u"b",
1057
1115
access=u"readwrite")
1058
def checker_running_dbus_property(self, value=None):
1116
def CheckerRunning_dbus_property(self, value=None):
1059
1117
if value is None: # get
1060
1118
return dbus.Boolean(self.checker is not None)
1189
1247
client.Rejected("Disabled")
1192
if client._approved or not client.approved_delay:
1250
if client._approved or not client.approval_delay:
1193
1251
#We are approved or approval is disabled
1195
1253
elif client._approved is None:
1196
logger.info(u"Client %s need approval",
1254
logger.info(u"Client %s needs approval",
1198
1256
if self.server.use_dbus:
1199
1257
# Emit D-Bus signal
1200
1258
client.NeedApproval(
1201
client.approved_delay_milliseconds(),
1259
client.approval_delay_milliseconds(),
1202
1260
client.approved_by_default)
1204
1262
logger.warning(u"Client %s was not approved",
1206
1264
if self.server.use_dbus:
1207
1265
# Emit D-Bus signal
1208
client.Rejected("Disapproved")
1266
client.Rejected("Denied")
1211
1269
#wait until timeout or approved
1728
1782
tcp_server = MandosServer((server_settings[u"address"],
1729
1783
server_settings[u"port"]),
1731
interface=server_settings[u"interface"],
1785
interface=(server_settings[u"interface"]
1732
1787
use_ipv6=use_ipv6,
1733
1788
gnutls_priority=
1734
1789
server_settings[u"priority"],
1735
1790
use_dbus=use_dbus)
1736
pidfilename = u"/var/run/mandos.pid"
1738
pidfile = open(pidfilename, u"w")
1740
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)
1743
1799
uid = pwd.getpwnam(u"_mandos").pw_uid