119
119
logger.addHandler(syslogger)
121
console = logging.StreamHandler()
122
console.setFormatter(logging.Formatter('%(asctime)s %(name)s'
126
logger.addHandler(console)
122
console = logging.StreamHandler()
123
console.setFormatter(logging.Formatter('%(asctime)s %(name)s'
127
logger.addHandler(console)
127
128
logger.setLevel(level)
467
467
section = dict(config.items(client_name))
468
468
client = settings[client_name] = {}
470
# Default copying each value from config to new dict
471
for setting, value in section.iteritems():
472
client[setting] = value
470
client["host"] = section["host"]
474
471
# Reformat values from string types to Python types
475
472
client["approved_by_default"] = config.getboolean(
476
473
client_name, "approved_by_default")
496
493
section["approval_delay"])
497
494
client["approval_duration"] = string_to_delta(
498
495
section["approval_duration"])
496
client["checker_command"] = section["checker"]
497
client["last_approval_request"] = None
498
client["last_checked_ok"] = None
499
client["last_checker_status"] = None
500
if client["enabled"]:
501
client["last_enabled"] = datetime.datetime.utcnow()
502
client["expires"] = (datetime.datetime.utcnow()
505
client["last_enabled"] = None
506
client["expires"] = None
503
def __init__(self, config, name = None):
511
def __init__(self, settings, name = None):
504
512
"""Note: the 'checker' key in 'config' sets the
505
513
'checker_command' attribute and *not* the 'checker'
516
# adding all client settings
517
for setting, value in settings.iteritems():
518
setattr(self, setting, value)
508
520
logger.debug("Creating client %r", self.name)
509
521
# Uppercase and remove spaces from fingerprint for later
510
522
# comparison purposes with return value from the fingerprint()
512
self.fingerprint = config["fingerprint"]
513
524
logger.debug(" Fingerprint: %s", self.fingerprint)
514
self.secret = config["secret"]
515
self.host = config["host"]
516
self.created = datetime.datetime.utcnow()
517
self.enabled = config["enabled"]
518
self.last_approval_request = None
520
self.last_enabled = datetime.datetime.utcnow()
522
self.last_enabled = None
523
self.last_checked_ok = None
524
self.last_checker_status = None
525
self.timeout = config["timeout"]
526
self.extended_timeout = config["extended_timeout"]
527
self.interval = config["interval"]
525
self.created = settings.get("created", datetime.datetime.utcnow())
527
# attributes specific for this server instance
528
528
self.checker = None
529
529
self.checker_initiator_tag = None
530
530
self.disable_initiator_tag = None
532
self.expires = datetime.datetime.utcnow() + self.timeout
535
531
self.checker_callback_tag = None
536
self.checker_command = config["checker"]
537
532
self.current_checker_command = None
538
533
self.approved = None
539
self.approved_by_default = config["approved_by_default"]
540
534
self.approvals_pending = 0
541
self.approval_delay = config["approval_delay"]
542
self.approval_duration = config["approval_duration"]
543
535
self.changedstate = (multiprocessing_manager
544
536
.Condition(multiprocessing_manager
1048
1040
def __init__(self, bus = None, *args, **kwargs):
1050
1042
Client.__init__(self, *args, **kwargs)
1043
self._approvals_pending = 0
1052
1045
self._approvals_pending = 0
1053
1046
# Only now, when this client is initialized, can it show up on
2164
2157
os.dup2(null, sys.stdin.fileno())
2168
# No console logging
2169
logger.removeHandler(console)
2171
2161
# Need to fork before connecting to D-Bus
2173
2163
# Close all input and output, do double fork, etc.
2166
gobject.threads_init()
2176
2168
global main_loop
2177
2169
# From the Avahi example code
2178
2170
DBusGMainLoop(set_as_default=True )
2225
2217
if e.errno != errno.ENOENT:
2219
except EOFError as e:
2220
logger.warning("Could not load persistent state: "
2221
"EOFError: {0}".format(e))
2228
2223
with PGPEngine() as pgp:
2229
for client in clients_data:
2230
client_name = client["name"]
2224
for client_name, client in clients_data.iteritems():
2232
2225
# Decide which value to use after restoring saved state.
2233
2226
# We have three different values: Old config file,
2234
2227
# new config file, and saved state.
2269
2262
client["expires"] = (datetime.datetime
2271
2264
+ client["timeout"])
2273
client["changedstate"] = (multiprocessing_manager
2275
(multiprocessing_manager
2277
client["checker"] = None
2279
new_client = (ClientDBusTransitional.__new__
2280
(ClientDBusTransitional))
2281
tcp_server.clients[client_name] = new_client
2282
new_client.bus = bus
2283
for name, value in client.iteritems():
2284
setattr(new_client, name, value)
2285
client_object_name = unicode(client_name).translate(
2286
{ord("."): ord("_"),
2287
ord("-"): ord("_")})
2288
new_client.dbus_object_path = (dbus.ObjectPath
2290
+ client_object_name))
2291
DBusObjectWithProperties.__init__(new_client,
2296
tcp_server.clients[client_name] = (Client.__new__
2298
for name, value in client.iteritems():
2299
setattr(tcp_server.clients[client_name],
2303
tcp_server.clients[client_name].secret = (
2304
pgp.decrypt(tcp_server.clients[client_name]
2267
client["secret"] = (
2268
pgp.decrypt(client["encrypted_secret"],
2306
2269
client_settings[client_name]
2308
2271
except PGPError:
2309
2272
# If decryption fails, we use secret from new settings
2310
2273
logger.debug("Failed to decrypt {0} old secret"
2311
2274
.format(client_name))
2312
tcp_server.clients[client_name].secret = (
2275
client["secret"] = (
2313
2276
client_settings[client_name]["secret"])
2315
# Create/remove clients based on new changes made to config
2316
for clientname in set(old_client_settings) - set(client_settings):
2317
del tcp_server.clients[clientname]
2318
for clientname in set(client_settings) - set(old_client_settings):
2319
tcp_server.clients[clientname] = (client_class(name = clientname,
2279
# Add/remove clients based on new changes made to config
2280
for client_name in set(old_client_settings) - set(client_settings):
2281
del clients_data[client_name]
2282
for client_name in set(client_settings) - set(old_client_settings):
2283
clients_data[client_name] = client_settings[client_name]
2285
# Create clients all clients
2286
for client_name, client in clients_data.iteritems():
2287
tcp_server.clients[client_name] = client_class(
2288
name = client_name, settings = client)
2324
2290
if not tcp_server.clients:
2325
2291
logger.warning("No clients defined")
2433
2399
if attr not in exclude:
2434
2400
client_dict[attr] = getattr(client, attr)
2436
clients.append(client_dict)
2402
clients[client.name] = client_dict
2437
2403
del client_settings[client.name]["secret"]
2440
with os.fdopen(os.open(stored_state_path,
2441
os.O_CREAT|os.O_WRONLY|os.O_TRUNC,
2442
0600), "wb") as stored_state:
2406
tempfd, tempname = tempfile.mkstemp(suffix=".pickle",
2409
(stored_state_path))
2410
with os.fdopen(tempfd, "wb") as stored_state:
2443
2411
pickle.dump((clients, client_settings), stored_state)
2412
os.rename(tempname, stored_state_path)
2444
2413
except (IOError, OSError) as e:
2445
2414
logger.warning("Could not save persistent state: {0}"
2447
if e.errno not in (errno.ENOENT, errno.EACCES):
2421
if e.errno not in set((errno.ENOENT, errno.EACCES,
2450
2425
# Delete all clients, and settings from config
2451
2426
while tcp_server.clients: