changed port to 12346
[sfa.git] / sfacomponent / server / sfa-component-server.py
1 #!/usr/bin/python
2 #
3 ### $Id: sfa-compnent-server.py 
4 ### $URL:
5  
6 # This wrapper implements the SFA Component Interfaces on PLC.
7 #
8 # There are several items that need to be done before starting the wrapper
9 # server.
10 #
11 #   (requirements coming soon)
12 ##
13
14 # TCP ports for the component server
15 component_port=12346
16
17 import os, os.path
18 from optparse import OptionParser
19
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
25
26 # after http://www.erlenstar.demon.co.uk/unix/faq_2.html
27 def daemon():
28     """Daemonize the current process."""
29     if os.fork() != 0: os._exit(0)
30     os.setsid()
31     if os.fork() != 0: os._exit(0)
32     os.umask(0)
33     devnull = os.open(os.devnull, os.O_RDWR)
34     os.dup2(devnull, 0)
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)
37     os.dup2(crashlog, 1)
38     os.dup2(crashlog, 2)
39
40 def main():
41     # xxx get rid of globals - name consistently CamelCase or under_score
42     global AuthHierarchy
43     global TrustedRoots
44     global component_port
45
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()
53
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")
58    
59     # XX TODO: Subject should be the node's hrn    
60     subject = "component" 
61     if (options.daemon):  daemon()
62
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)
68         cert.set_pubkey(key)
69         cert.sign()
70         cert.save_to_file(cert_file)
71
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)
78         cert.set_pubkey(key)
79         cert.sign()
80         cert.save_to_file(cert_file)
81
82     AuthHierarchy = Hierarchy()
83
84     TrustedRoots = TrustedRootList(Config().get_trustedroots_dir())
85     component = Component("", component_port, key_file, cert_file)
86     component.start()
87
88 if __name__ == "__main__":
89     main()