11
11
# "AvahiService" class, and some lines in "main".
13
13
# Everything else is
14
# Copyright © 2008-2020 Teddy Hogeborn
15
# Copyright © 2008-2020 Björn Påhlsson
14
# Copyright © 2008-2022 Teddy Hogeborn
15
# Copyright © 2008-2022 Björn Påhlsson
17
17
# This file is part of Mandos.
143
143
if sys.version_info < (3, 2):
144
144
configparser.Configparser = configparser.SafeConfigParser
147
147
stored_state_file = "clients.pickle"
149
149
log = logging.getLogger(os.path.basename(sys.argv[0]))
961
961
# key_id() and fingerprint() functions
962
962
client["key_id"] = (section.get("key_id", "").upper()
963
963
.replace(" ", ""))
964
client["fingerprint"] = (section["fingerprint"].upper()
964
client["fingerprint"] = (section.get("fingerprint",
965
966
.replace(" ", ""))
967
if not (client["key_id"] or client["fingerprint"]):
968
log.error("Skipping client %s without key_id or"
969
" fingerprint", client_name)
970
del settings[client_name]
966
972
if "secret" in section:
967
973
client["secret"] = codecs.decode(section["secret"]
968
974
.encode("utf-8"),
1074
1079
def __del__(self):
1077
def init_checker(self):
1078
# Schedule a new checker to be started an 'interval' from now,
1079
# and every interval from then on.
1082
def init_checker(self, randomize_start=False):
1083
# Schedule a new checker to be started a randomly selected
1084
# time (a fraction of 'interval') from now. This spreads out
1085
# the startup of checkers over time when the server is
1080
1087
if self.checker_initiator_tag is not None:
1081
1088
GLib.source_remove(self.checker_initiator_tag)
1089
interval_milliseconds = int(self.interval.total_seconds()
1092
delay_milliseconds = random.randrange(
1093
interval_milliseconds + 1)
1095
delay_milliseconds = interval_milliseconds
1082
1096
self.checker_initiator_tag = GLib.timeout_add(
1083
random.randrange(int(self.interval.total_seconds() * 1000
1086
# Schedule a disable() when 'timeout' has passed
1097
delay_milliseconds, self.start_checker, randomize_start)
1098
delay = datetime.timedelta(0, 0, 0, delay_milliseconds)
1099
# A checker might take up to an 'interval' of time, so we can
1100
# expire at the soonest one interval after a checker was
1101
# started. Since the initial checker is delayed, the expire
1102
# time might have to be extended.
1103
now = datetime.datetime.utcnow()
1104
self.expires = now + delay + self.interval
1105
# Schedule a disable() at expire time
1087
1106
if self.disable_initiator_tag is not None:
1088
1107
GLib.source_remove(self.disable_initiator_tag)
1089
1108
self.disable_initiator_tag = GLib.timeout_add(
1090
int(self.timeout.total_seconds() * 1000), self.disable)
1091
# Also start a new checker *right now*.
1092
self.start_checker()
1109
int((self.expires - now).total_seconds() * 1000),
1094
1112
def checker_callback(self, source, condition, connection,
1138
1156
def need_approval(self):
1139
1157
self.last_approval_request = datetime.datetime.utcnow()
1141
def start_checker(self):
1159
def start_checker(self, start_was_randomized=False):
1142
1160
"""Start a new checker subprocess if one is not running.
1144
1162
If a checker already exists, leave it running and do
1194
1212
GLib.IOChannel.unix_new(pipe[0].fileno()),
1195
1213
GLib.PRIORITY_DEFAULT, GLib.IO_IN,
1196
1214
self.checker_callback, pipe[0], command)
1215
if start_was_randomized:
1216
# We were started after a random delay; Schedule a new
1217
# checker to be started an 'interval' from now, and every
1218
# interval from then on.
1219
now = datetime.datetime.utcnow()
1220
self.checker_initiator_tag = GLib.timeout_add(
1221
int(self.interval.total_seconds() * 1000),
1223
self.expires = max(self.expires, now + self.interval)
1224
# Don't start a new checker again after same random delay
1197
1226
# Re-run this periodically if run by GLib.timeout_add
3710
3740
# default-directory dir)
3711
3741
# (erase-buffer)
3712
3742
# (compilation-mode))
3713
# (let ((inhibit-read-only t))
3714
# (= (process-file-shell-command
3715
# (funcall get-command-line extra)
3716
# nil "*Test*") 0)))
3743
# (let ((process-result
3744
# (let ((inhibit-read-only t))
3745
# (process-file-shell-command
3746
# (funcall get-command-line extra) nil "*Test*"))))
3747
# (and (numberp process-result)
3748
# (= process-result 0))))
3717
3749
# get-command-line:
3718
3750
# (lambda (&optional extra)
3719
3751
# (let ((quoted-script