171
except dbus.exceptions.DBusException, error:
172
except dbus.exceptions.DBusException as error:
172
173
logger.critical("DBusException: %s", error)
175
176
self.rename_count += 1
176
177
def remove(self):
177
178
"""Derived from the Avahi example code"""
179
if self.entry_group_state_changed_match is not None:
180
self.entry_group_state_changed_match.remove()
181
self.entry_group_state_changed_match = None
178
182
if self.group is not None:
179
183
self.group.Reset()
181
185
"""Derived from the Avahi example code"""
182
187
if self.group is None:
183
188
self.group = dbus.Interface(
184
189
self.bus.get_object(avahi.DBUS_NAME,
185
190
self.server.EntryGroupNew()),
186
191
avahi.DBUS_INTERFACE_ENTRY_GROUP)
187
self.group.connect_to_signal('StateChanged',
189
.entry_group_state_changed)
192
self.entry_group_state_changed_match = (
193
self.group.connect_to_signal(
194
'StateChanged', self .entry_group_state_changed))
190
195
logger.debug("Adding Zeroconf service '%s' of type '%s' ...",
191
196
self.name, self.type)
192
197
self.group.AddService(
215
220
def cleanup(self):
216
221
"""Derived from the Avahi example code"""
217
222
if self.group is not None:
225
except (dbus.exceptions.UnknownMethodException,
226
dbus.exceptions.DBusException) as e:
219
228
self.group = None
220
def server_state_changed(self, state):
230
def server_state_changed(self, state, error=None):
221
231
"""Derived from the Avahi example code"""
222
232
logger.debug("Avahi server state change: %i", state)
223
if state == avahi.SERVER_COLLISION:
224
logger.error("Zeroconf server name collision")
233
bad_states = { avahi.SERVER_INVALID:
234
"Zeroconf server invalid",
235
avahi.SERVER_REGISTERING: None,
236
avahi.SERVER_COLLISION:
237
"Zeroconf server name collision",
238
avahi.SERVER_FAILURE:
239
"Zeroconf server failure" }
240
if state in bad_states:
241
if bad_states[state] is not None:
243
logger.error(bad_states[state])
245
logger.error(bad_states[state] + ": %r", error)
226
247
elif state == avahi.SERVER_RUNNING:
251
logger.debug("Unknown state: %r", state)
253
logger.debug("Unknown state: %r: %r", state, error)
228
254
def activate(self):
229
255
"""Derived from the Avahi example code"""
230
256
if self.server is None:
231
257
self.server = dbus.Interface(
232
258
self.bus.get_object(avahi.DBUS_NAME,
233
avahi.DBUS_PATH_SERVER),
259
avahi.DBUS_PATH_SERVER,
260
follow_name_owner_changes=True),
234
261
avahi.DBUS_INTERFACE_SERVER)
235
262
self.server.connect_to_signal("StateChanged",
236
263
self.server_state_changed)
736
777
("/clients/" + client_object_name))
737
778
DBusObjectWithProperties.__init__(self, self.bus,
738
779
self.dbus_object_path)
780
def _set_expires(self, value):
781
old_value = getattr(self, "_expires", None)
782
self._expires = value
783
if hasattr(self, "dbus_object_path") and old_value != value:
784
dbus_time = (self._datetime_to_dbus(self._expires,
786
self.PropertyChanged(dbus.String("Expires"),
788
expires = property(lambda self: self._expires, _set_expires)
740
791
def _get_approvals_pending(self):
741
792
return self._approvals_pending
960
1012
def ApprovedByDefault_dbus_property(self, value=None):
961
1013
if value is None: # get
962
1014
return dbus.Boolean(self.approved_by_default)
1015
old_value = self.approved_by_default
963
1016
self.approved_by_default = bool(value)
964
1017
# Emit D-Bus signal
965
self.PropertyChanged(dbus.String("ApprovedByDefault"),
966
dbus.Boolean(value, variant_level=1))
1018
if old_value != self.approved_by_default:
1019
self.PropertyChanged(dbus.String("ApprovedByDefault"),
1020
dbus.Boolean(value, variant_level=1))
968
1022
# ApprovalDelay - property
969
1023
@dbus_service_property(_interface, signature="t",
971
1025
def ApprovalDelay_dbus_property(self, value=None):
972
1026
if value is None: # get
973
1027
return dbus.UInt64(self.approval_delay_milliseconds())
1028
old_value = self.approval_delay
974
1029
self.approval_delay = datetime.timedelta(0, 0, 0, value)
975
1030
# Emit D-Bus signal
976
self.PropertyChanged(dbus.String("ApprovalDelay"),
977
dbus.UInt64(value, variant_level=1))
1031
if old_value != self.approval_delay:
1032
self.PropertyChanged(dbus.String("ApprovalDelay"),
1033
dbus.UInt64(value, variant_level=1))
979
1035
# ApprovalDuration - property
980
1036
@dbus_service_property(_interface, signature="t",
983
1039
if value is None: # get
984
1040
return dbus.UInt64(self._timedelta_to_milliseconds(
985
1041
self.approval_duration))
1042
old_value = self.approval_duration
986
1043
self.approval_duration = datetime.timedelta(0, 0, 0, value)
987
1044
# Emit D-Bus signal
988
self.PropertyChanged(dbus.String("ApprovalDuration"),
989
dbus.UInt64(value, variant_level=1))
1045
if old_value != self.approval_duration:
1046
self.PropertyChanged(dbus.String("ApprovalDuration"),
1047
dbus.UInt64(value, variant_level=1))
991
1049
# Name - property
992
1050
@dbus_service_property(_interface, signature="s", access="read")
1039
1097
if value is not None:
1040
1098
self.checked_ok()
1042
if self.last_checked_ok is None:
1043
return dbus.String("")
1044
return dbus.String(self._datetime_to_dbus(self
1100
return self._datetime_to_dbus(self.last_checked_ok)
1102
# Expires - property
1103
@dbus_service_property(_interface, signature="s", access="read")
1104
def Expires_dbus_property(self):
1105
return self._datetime_to_dbus(self.expires)
1047
1107
# LastApprovalRequest - property
1048
1108
@dbus_service_property(_interface, signature="s", access="read")
1049
1109
def LastApprovalRequest_dbus_property(self):
1050
if self.last_approval_request is None:
1051
return dbus.String("")
1052
return dbus.String(self.
1053
_datetime_to_dbus(self
1054
.last_approval_request))
1110
return self._datetime_to_dbus(self.last_approval_request)
1056
1112
# Timeout - property
1057
1113
@dbus_service_property(_interface, signature="t",
1059
1115
def Timeout_dbus_property(self, value=None):
1060
1116
if value is None: # get
1061
1117
return dbus.UInt64(self.timeout_milliseconds())
1118
old_value = self.timeout
1062
1119
self.timeout = datetime.timedelta(0, 0, 0, value)
1063
1120
# Emit D-Bus signal
1064
self.PropertyChanged(dbus.String("Timeout"),
1065
dbus.UInt64(value, variant_level=1))
1121
if old_value != self.timeout:
1122
self.PropertyChanged(dbus.String("Timeout"),
1123
dbus.UInt64(value, variant_level=1))
1066
1124
if getattr(self, "disable_initiator_tag", None) is None:
1068
1126
# Reschedule timeout
1069
1127
gobject.source_remove(self.disable_initiator_tag)
1070
1128
self.disable_initiator_tag = None
1071
1130
time_to_die = (self.
1072
1131
_timedelta_to_milliseconds((self
1073
1132
.last_checked_ok
1078
1137
# The timeout has passed
1140
self.expires = (datetime.datetime.utcnow()
1141
+ datetime.timedelta(milliseconds = time_to_die))
1081
1142
self.disable_initiator_tag = (gobject.timeout_add
1082
1143
(time_to_die, self.disable))
1145
# ExtendedTimeout - property
1146
@dbus_service_property(_interface, signature="t",
1148
def ExtendedTimeout_dbus_property(self, value=None):
1149
if value is None: # get
1150
return dbus.UInt64(self.extended_timeout_milliseconds())
1151
old_value = self.extended_timeout
1152
self.extended_timeout = datetime.timedelta(0, 0, 0, value)
1154
if old_value != self.extended_timeout:
1155
self.PropertyChanged(dbus.String("ExtendedTimeout"),
1156
dbus.UInt64(value, variant_level=1))
1084
1158
# Interval - property
1085
1159
@dbus_service_property(_interface, signature="t",
1086
1160
access="readwrite")
1087
1161
def Interval_dbus_property(self, value=None):
1088
1162
if value is None: # get
1089
1163
return dbus.UInt64(self.interval_milliseconds())
1164
old_value = self.interval
1090
1165
self.interval = datetime.timedelta(0, 0, 0, value)
1091
1166
# Emit D-Bus signal
1092
self.PropertyChanged(dbus.String("Interval"),
1093
dbus.UInt64(value, variant_level=1))
1167
if old_value != self.interval:
1168
self.PropertyChanged(dbus.String("Interval"),
1169
dbus.UInt64(value, variant_level=1))
1094
1170
if getattr(self, "checker_initiator_tag", None) is None:
1096
1172
# Reschedule checker run
1105
1181
def Checker_dbus_property(self, value=None):
1106
1182
if value is None: # get
1107
1183
return dbus.String(self.checker_command)
1184
old_value = self.checker_command
1108
1185
self.checker_command = value
1109
1186
# Emit D-Bus signal
1110
self.PropertyChanged(dbus.String("Checker"),
1111
dbus.String(self.checker_command,
1187
if old_value != self.checker_command:
1188
self.PropertyChanged(dbus.String("Checker"),
1189
dbus.String(self.checker_command,
1114
1192
# CheckerRunning - property
1115
1193
@dbus_service_property(_interface, signature="b",
1205
1283
if int(line.strip().split()[0]) > 1:
1206
1284
raise RuntimeError
1207
except (ValueError, IndexError, RuntimeError), error:
1285
except (ValueError, IndexError, RuntimeError) as error:
1208
1286
logger.error("Unknown protocol version: %s", error)
1211
1289
# Start GnuTLS connection
1213
1291
session.handshake()
1214
except gnutls.errors.GNUTLSError, error:
1292
except gnutls.errors.GNUTLSError as error:
1215
1293
logger.warning("Handshake failed: %s", error)
1216
1294
# Do not run session.bye() here: the session is not
1217
1295
# established. Just abandon the request.
1673
1752
##################################################################
1674
1753
# Parsing of options, both command line and config file
1676
parser = optparse.OptionParser(version = "%%prog %s" % version)
1677
parser.add_option("-i", "--interface", type="string",
1678
metavar="IF", help="Bind to interface IF")
1679
parser.add_option("-a", "--address", type="string",
1680
help="Address to listen for requests on")
1681
parser.add_option("-p", "--port", type="int",
1682
help="Port number to receive requests on")
1683
parser.add_option("--check", action="store_true",
1684
help="Run self-test")
1685
parser.add_option("--debug", action="store_true",
1686
help="Debug mode; run in foreground and log to"
1688
parser.add_option("--debuglevel", type="string", metavar="LEVEL",
1689
help="Debug level for stdout output")
1690
parser.add_option("--priority", type="string", help="GnuTLS"
1691
" priority string (see GnuTLS documentation)")
1692
parser.add_option("--servicename", type="string",
1693
metavar="NAME", help="Zeroconf service name")
1694
parser.add_option("--configdir", type="string",
1695
default="/etc/mandos", metavar="DIR",
1696
help="Directory to search for configuration"
1698
parser.add_option("--no-dbus", action="store_false",
1699
dest="use_dbus", help="Do not provide D-Bus"
1700
" system bus interface")
1701
parser.add_option("--no-ipv6", action="store_false",
1702
dest="use_ipv6", help="Do not use IPv6")
1703
options = parser.parse_args()[0]
1755
parser = argparse.ArgumentParser()
1756
parser.add_argument("-v", "--version", action="version",
1757
version = "%%(prog)s %s" % version,
1758
help="show version number and exit")
1759
parser.add_argument("-i", "--interface", metavar="IF",
1760
help="Bind to interface IF")
1761
parser.add_argument("-a", "--address",
1762
help="Address to listen for requests on")
1763
parser.add_argument("-p", "--port", type=int,
1764
help="Port number to receive requests on")
1765
parser.add_argument("--check", action="store_true",
1766
help="Run self-test")
1767
parser.add_argument("--debug", action="store_true",
1768
help="Debug mode; run in foreground and log"
1770
parser.add_argument("--debuglevel", metavar="LEVEL",
1771
help="Debug level for stdout output")
1772
parser.add_argument("--priority", help="GnuTLS"
1773
" priority string (see GnuTLS documentation)")
1774
parser.add_argument("--servicename",
1775
metavar="NAME", help="Zeroconf service name")
1776
parser.add_argument("--configdir",
1777
default="/etc/mandos", metavar="DIR",
1778
help="Directory to search for configuration"
1780
parser.add_argument("--no-dbus", action="store_false",
1781
dest="use_dbus", help="Do not provide D-Bus"
1782
" system bus interface")
1783
parser.add_argument("--no-ipv6", action="store_false",
1784
dest="use_ipv6", help="Do not use IPv6")
1785
options = parser.parse_args()
1705
1787
if options.check: