3 ### $Id: sfa-compnent-server.py
6 # This wrapper implements the SFA Component Interfaces on PLC.
8 # There are several items that need to be done before starting the wrapper
11 # (requirements coming soon)
14 # TCP ports for the component server
18 from optparse import OptionParser
20 from sfacomponent.server.component import Component
21 from sfa.trust.trustedroot import TrustedRootList
22 from sfa.trust.certificate import Keypair, Certificate
23 from sfa.trust.hierarchy import Hierarchy
24 from sfa.util.config import Config
26 # after http://www.erlenstar.demon.co.uk/unix/faq_2.html
28 """Daemonize the current process."""
29 if os.fork() != 0: os._exit(0)
31 if os.fork() != 0: os._exit(0)
33 devnull = os.open(os.devnull, os.O_RDWR)
35 # xxx fixme - this is just to make sure that nothing gets stupidly lost - should use devnull
36 crashlog = os.open('/var/log/sfa.daemon', os.O_RDWR | os.O_APPEND | os.O_CREAT, 0644)
41 # xxx get rid of globals - name consistently CamelCase or under_score
46 # Generate command line parser
47 parser = OptionParser(usage="sfa-component-server [options]")
48 parser.add_option("-v", "--verbose", dest="verbose", action="store_true",
49 help="verbose mode", default=False)
50 parser.add_option("-d", "--daemon", dest="daemon", action="store_true",
51 help="Run as daemon.", default=False)
52 (options, args) = parser.parse_args()
54 hierarchy = Hierarchy()
55 path = hierarchy.basedir
56 key_file = os.path.join(path, "server.key")
57 cert_file = os.path.join(path, "server.cert")
59 # XX TODO: Subject should be the node's hrn
61 if (options.daemon): daemon()
63 if (os.path.exists(key_file)) and (not os.path.exists(cert_file)):
64 # If private key exists and cert doesnt, recreate cert
65 key = Keypair(filename=key_file)
66 cert = Certificate(subject=subject)
67 cert.set_issuer(key=key, subject=subject)
70 cert.save_to_file(cert_file)
72 elif (not os.path.exists(key_file)) or (not os.path.exists(cert_file)):
73 # if no key is specified, then make one up
74 key = Keypair(create=True)
75 key.save_to_file(key_file)
76 cert = Certificate(subject=subject)
77 cert.set_issuer(key=key, subject=subject)
80 cert.save_to_file(cert_file)
82 AuthHierarchy = Hierarchy()
84 TrustedRoots = TrustedRootList(Config().get_trustedroots_dir())
85 component = Component("", component_port, key_file, cert_file)
88 if __name__ == "__main__":