-from sfa.util.xrn import get_authority, hrn_to_urn
-from sfa.util.sfalogging import logger
-
-from sfa.managers.import_manager import import_manager
-
-# after http://www.erlenstar.demon.co.uk/unix/faq_2.html
-def daemon():
- """Daemonize the current process."""
- if os.fork() != 0: os._exit(0)
- os.setsid()
- if os.fork() != 0: os._exit(0)
- os.umask(0)
- devnull = os.open(os.devnull, os.O_RDWR)
- os.dup2(devnull, 0)
- # xxx fixme - this is just to make sure that nothing gets stupidly lost - should use devnull
- crashlog = os.open('/var/log/httpd/sfa_access_log', os.O_RDWR | os.O_APPEND | os.O_CREAT, 0644)
- os.dup2(crashlog, 1)
- os.dup2(crashlog, 2)
-
-def init_server_key(server_key_file, server_cert_file, config, hierarchy):
-
- hrn = config.SFA_INTERFACE_HRN.lower()
- # check if the server's private key exists. If it doesnt,
- # get the right one from the authorities directory. If it cant be
- # found in the authorities directory, generate a random one
- if not os.path.exists(server_key_file):
- hrn = config.SFA_INTERFACE_HRN.lower()
- hrn_parts = hrn.split(".")
- rel_key_path = hrn
- pkey_filename = hrn+".pkey"
-
- # sub authority's have "." in their hrn. This must
- # be converted to os.path separator
- if len(hrn_parts) > 0:
- rel_key_path = hrn.replace(".", os.sep)
- pkey_filename= hrn_parts[-1]+".pkey"
-
- key_file = os.sep.join([hierarchy.basedir, rel_key_path, pkey_filename])
- if not os.path.exists(key_file):
- # if it doesnt exist then this is probably a fresh interface
- # with no records. Generate a random keypair for now
- logger.debug("server's public key not found in %s" % key_file)
-
- logger.debug("generating a random server key pair")
- key = Keypair(create=True)
- key.save_to_file(server_key_file)
- init_server_cert(hrn, key, server_cert_file, self_signed=True)
-
- else:
- # the pkey was found in the authorites directory. lets
- # copy it to where the server key should be and generate
- # the cert
- key = Keypair(filename=key_file)
- key.save_to_file(server_key_file)
- init_server_cert(hrn, key, server_cert_file)
-
- # If private key exists and cert doesnt, recreate cert
- if (os.path.exists(server_key_file)) and (not os.path.exists(server_cert_file)):
- key = Keypair(filename=server_key_file)
- init_server_cert(hrn, key, server_cert_file)
-