(no commit message)
[sfa.git] / gui / JavaApplication1 / client_keiko / clientstub.py
1 #!/usr/bin/python
2
3 import os, sys
4 from M2Crypto import SSL
5 sys.path.append('/home/soners/work/geni/rpc/util')
6 sys.path.append('/home/soners/work/geni/rpc/util/sec')
7 from sec import *
8
9 SERVER_HOST = '127.0.0.1' 
10 SERVER_PORT = 8002
11 AUTH_HOST = '127.0.0.1' 
12 AUTH_PORT = 8002
13
14 renew_res1 = 0
15 renew_res2 = 0
16
17 def verify_callback(preverify_ok, ctx):
18     return 1
19
20 class GENIClient():
21     def __init__(self, hrn, type, id_file, id_key_file, acc_file, cred_file):
22         self.hrn = hrn
23         self.type = type
24         #check if the certificate and the private key exists, terminate if not
25         if not os.path.exists(id_file) or not os.path.exists(id_key_file) :
26             print 'The certificate or the private key does not exist.\n'
27             os.exit(1)
28         #check the acc and cred files
29         if not os.path.exists(acc_file) or not is_valid_chain(acc_file):
30             open(acc_file, 'w').write('ANONYM')
31         if not os.path.exists(cred_file) or not is_valid_chain(cred_file):
32             open(cred_file, 'w').write('NO_CRED')
33         #initialize the security system
34         self.sec = Sec('client',  id_file, id_key_file, acc_file, cred_file)
35         #ssl parameters
36         self.ctx = SSL.Context()
37         self.ctx.load_cert(self.sec.id_file,self.sec.id_key_file)
38         self.ctx.set_verify(SSL.verify_peer | SSL.verify_fail_if_no_peer_cert, depth=9, callback=verify_callback)    
39     
40     def connect(self, host, port):    
41         #if the acc and cred needs renewal then do call to authority
42         if self.type == 'user' or self.type == 'slice' or self.type == 'SA': 
43             reg_type = 'slice'
44         else:
45             reg_type ='component'
46         renew_res1 = renew_cert('accounting', '.', reg_type, self.hrn, None, None, (AUTH_HOST, AUTH_PORT), self.sec)
47         renew_res2 = renew_cert('credential', '.', reg_type, self.hrn, None, None, (AUTH_HOST, AUTH_PORT), self.sec)
48         if renew_res1 == None:
49             print "There is no certificate in the directory "+"./\n"
50             os.exit(0)
51         #connect to server
52         server = SSL.Connection(self.ctx)
53         server.connect((host,port))
54         peer = self.sec.auth_protocol(server)
55         if peer:
56             return server
57         else:
58             return None
59         
60 def main():
61     try:
62         #read the input file
63         fp = open('tmp_input.txt', 'r')
64         user_data = fp.readline()
65         call_data = fp.readline()
66         print 'Read file.\n'
67         
68         #client related info
69         HRN = user_data.split(' ')[0]
70         TYPE = user_data.split(' ')[1].split('\n')[0]
71         name = get_leaf(HRN)
72         ID_FILE = name+'.cert'
73         ID_KEY_FILE = name+'.pkey'
74         ACC_FILE = 'acc_file'
75         CRED_FILE = 'cred_file'
76         my_client = GENIClient(HRN, TYPE, ID_FILE, ID_KEY_FILE, ACC_FILE, CRED_FILE)
77         print 'Constructed client.\n'
78         
79         #operation call
80         message = eval(call_data)
81         server = my_client.connect(SERVER_HOST, SERVER_PORT)
82         if server:
83             server.write(str(message))
84             result = server.read() 
85             server.close()
86             print 'Performed the call.\n'
87         else:
88             result = "Error in client data structures.\n"
89             
90         if renew_res2 == 1:
91             result = "Cred renewed. "+result
92         if renew_res1 == 1:
93             result = "Acc renewed. "+result
94         #write result to output file
95         open('tmp_output.txt','w').write(result)
96         print 'Written to file.\n'
97     except:
98         #write result to output file
99         open('tmp_output.txt','w').write("An error occurred in client stub.\n")
100         print 'Exception occurred.\n'
101         
102 if __name__=="__main__":
103     print 'Client started.\n'
104     main()