310
310
interval: datetime.timedelta(); How often to start a new checker
311
311
last_approval_request: datetime.datetime(); (UTC) or None
312
312
last_checked_ok: datetime.datetime(); (UTC) or None
313
last_checker_status: integer between 0 and 255 reflecting exit status
314
of last checker. -1 reflect crashed checker,
313
last_checker_status: integer between 0 and 255 reflecting exit
314
status of last checker. -1 reflect crashed
316
316
last_enabled: datetime.datetime(); (UTC)
317
317
name: string; from the config file, used in log messages and
318
318
D-Bus identifiers
398
398
self.changedstate = (multiprocessing_manager
399
399
.Condition(multiprocessing_manager
401
self.client_structure = [attr for attr in self.__dict__.iterkeys() if not attr.startswith("_")]
401
self.client_structure = [attr for attr
402
in self.__dict__.iterkeys()
403
if not attr.startswith("_")]
402
404
self.client_structure.append("client_structure")
405
407
for name, t in inspect.getmembers(type(self),
406
lambda obj: isinstance(obj, property)):
407
411
if not name.startswith("_"):
408
412
self.client_structure.append(name)
592
596
self.checker = None
594
# Encrypts a client secret and stores it in a varible encrypted_secret
598
# Encrypts a client secret and stores it in a varible
595
600
def encrypt_secret(self, key):
596
# Encryption-key need to be of a specific size, so we hash inputed key
601
# Encryption-key need to be of a specific size, so we hash
597
603
hasheng = hashlib.sha256()
598
604
hasheng.update(key)
599
605
encryptionkey = hasheng.digest()
601
# Create validation hash so we know at decryption if it was sucessful
607
# Create validation hash so we know at decryption if it was
602
609
hasheng = hashlib.sha256()
603
610
hasheng.update(self.secret)
604
611
validationhash = hasheng.digest()
613
620
# Decrypt a encrypted client secret
614
621
def decrypt_secret(self, key):
615
# Decryption-key need to be of a specific size, so we hash inputed key
622
# Decryption-key need to be of a specific size, so we hash
616
624
hasheng = hashlib.sha256()
617
625
hasheng.update(key)
618
626
encryptionkey = hasheng.digest()
629
637
secret = plain[hasheng.digest_size:]
630
638
hasheng.update(secret)
632
# if validation fails, we use key as new secret. Otherwhise, we use
633
# the decrypted secret
640
# If validation fails, we use key as new secret. Otherwise, we
641
# use the decrypted secret
634
642
if hasheng.digest() == validationhash:
635
643
self.secret = secret
945
953
def __init__(self, bus = None, *args, **kwargs):
955
self._approvals_pending = 0
947
956
Client.__init__(self, *args, **kwargs)
949
self._approvals_pending = 0
959
def add_to_dbus(self):
950
960
# Only now, when this client is initialized, can it show up on
952
962
client_object_name = unicode(self.name).translate(
1922
1932
parser.add_argument("--no-ipv6", action="store_false",
1923
1933
dest="use_ipv6", help="Do not use IPv6")
1924
1934
parser.add_argument("--no-restore", action="store_false",
1925
dest="restore", help="Do not restore stored state",
1936
help="Do not restore stored state",
1928
1939
options = parser.parse_args()
2125
2136
# with exceptions for any special settings as defined above
2126
2137
client_settings = dict((clientname,
2128
(value if setting not in special_settings
2129
else special_settings[setting](clientname)))
2130
for setting, value in client_config.items(clientname)))
2140
setting not in special_settings
2141
else special_settings[setting]
2144
in client_config.items(clientname)))
2131
2145
for clientname in client_config.sections())
2133
2147
old_client_settings = {}
2137
2151
if server_settings["restore"]:
2139
2153
with open(stored_state_path, "rb") as stored_state:
2140
clients_data, old_client_settings = pickle.load(stored_state)
2154
clients_data, old_client_settings = (
2155
pickle.load(stored_state))
2141
2156
os.remove(stored_state_path)
2142
2157
except IOError as e:
2143
logger.warning("Could not load persistant state: {0}".format(e))
2158
logger.warning("Could not load persistant state: {0}"
2144
2160
if e.errno != errno.ENOENT:
2157
2173
# For each value in new config, check if it differs
2158
2174
# from the old config value (Except for the "secret"
2160
if name != "secret" and value != old_client_settings[client_name][name]:
2176
if (name != "secret" and
2177
value != old_client_settings[client_name][name]):
2161
2178
setattr(client, name, value)
2162
2179
except KeyError:
2165
# Clients who has passed its expire date, can still be enabled if its
2166
# last checker was sucessful. Clients who checkers failed before we
2167
# stored it state is asumed to had failed checker during downtime.
2182
# Clients who has passed its expire date, can still be enabled
2183
# if its last checker was sucessful. Clients who checkers
2184
# failed before we stored it state is asumed to had failed
2185
# checker during downtime.
2168
2186
if client["enabled"] and client["last_checked_ok"]:
2169
if ((datetime.datetime.utcnow() - client["last_checked_ok"])
2170
> client["interval"]):
2187
if ((datetime.datetime.utcnow()
2188
- client["last_checked_ok"]) > client["interval"]):
2171
2189
if client["last_checker_status"] != 0:
2172
2190
client["enabled"] = False
2174
client["expires"] = datetime.datetime.utcnow() + client["timeout"]
2192
client["expires"] = (datetime.datetime.utcnow()
2193
+ client["timeout"])
2176
2195
client["changedstate"] = (multiprocessing_manager
2177
2196
.Condition(multiprocessing_manager
2180
new_client = ClientDBusTransitional.__new__(ClientDBusTransitional)
2199
new_client = ClientDBusTransitional.__new__(
2200
ClientDBusTransitional)
2181
2201
tcp_server.clients[client_name] = new_client
2182
2202
new_client.bus = bus
2183
2203
for name, value in client.iteritems():
2184
2204
setattr(new_client, name, value)
2185
client_object_name = unicode(client_name).translate(
2186
{ord("."): ord("_"),
2187
ord("-"): ord("_")})
2188
new_client.dbus_object_path = (dbus.ObjectPath
2189
("/clients/" + client_object_name))
2190
DBusObjectWithProperties.__init__(new_client,
2192
new_client.dbus_object_path)
2205
new_client._approvals_pending = 0
2206
new_client.add_to_dbus()
2194
2208
tcp_server.clients[client_name] = Client.__new__(Client)
2195
2209
for name, value in client.iteritems():
2202
2216
for clientname in set(old_client_settings) - set(client_settings):
2203
2217
del tcp_server.clients[clientname]
2204
2218
for clientname in set(client_settings) - set(old_client_settings):
2205
tcp_server.clients[clientname] = (client_class(name = clientname,
2219
tcp_server.clients[clientname] = client_class(name
2211
2225
if not tcp_server.clients:
2212
2226
logger.warning("No clients defined")
2296
2310
if not (tcp_server.clients or client_settings):
2299
# Store client before exiting. Secrets are encrypted with key based
2300
# on what config file has. If config file is removed/edited, old
2301
# secret will thus be unrecovable.
2313
# Store client before exiting. Secrets are encrypted with key
2314
# based on what config file has. If config file is
2315
# removed/edited, old secret will thus be unrecovable.
2303
2317
for client in tcp_server.clients.itervalues():
2304
client.encrypt_secret(client_settings[client.name]["secret"])
2318
client.encrypt_secret(
2319
client_settings[client.name]["secret"])
2306
2321
client_dict = {}
2308
# A list of attributes that will not be stored when shuting down.
2309
exclude = set(("bus", "changedstate", "secret"))
2323
# A list of attributes that will not be stored when
2325
exclude = set(("bus", "changedstate", "secret"))
2310
2326
for name, typ in inspect.getmembers(dbus.service.Object):
2311
2327
exclude.add(name)
2319
2335
del client_settings[client.name]["secret"]
2322
with os.fdopen(os.open(stored_state_path, os.O_CREAT|os.O_WRONLY|os.O_TRUNC, 0600), "wb") as stored_state:
2338
with os.fdopen(os.open(stored_state_path,
2339
os.O_CREAT|os.O_WRONLY|os.O_TRUNC,
2340
stat.S_IRUSR | stat.S_IWUSR),
2341
"wb") as stored_state:
2323
2342
pickle.dump((clients, client_settings), stored_state)
2324
2343
except IOError as e:
2325
logger.warning("Could not save persistant state: {0}".format(e))
2344
logger.warning("Could not save persistant state: {0}"
2326
2346
if e.errno != errno.ENOENT: