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')
9 SERVER_HOST = '127.0.0.1'
11 AUTH_HOST = '127.0.0.1'
17 def verify_callback(preverify_ok, ctx):
21 def __init__(self, hrn, type, id_file, id_key_file, acc_file, cred_file):
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'
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)
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)
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':
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"
52 server = SSL.Connection(self.ctx)
53 server.connect((host,port))
54 peer = self.sec.auth_protocol(server)
63 fp = open('tmp_input.txt', 'r')
64 user_data = fp.readline()
65 call_data = fp.readline()
69 HRN = user_data.split(' ')[0]
70 TYPE = user_data.split(' ')[1].split('\n')[0]
72 ID_FILE = name+'.cert'
73 ID_KEY_FILE = name+'.pkey'
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'
80 message = eval(call_data)
81 server = my_client.connect(SERVER_HOST, SERVER_PORT)
83 server.write(str(message))
84 result = server.read()
86 print 'Performed the call.\n'
88 result = "Error in client data structures.\n"
91 result = "Cred renewed. "+result
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'
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'
102 if __name__=="__main__":
103 print 'Client started.\n'