Merge branch 'master' of https://git.erp5.org/repos/vifibnet
[re6stnet.git] / utils.py
1 import argparse, time
2 from OpenSSL import crypto
3
4
5 def log(message, verbose_level):
6 if config.verbose >= verbose_level:
7 print time.strftime("%d-%m-%Y %H:%M:%S : " + message)
8
9 def ipFromBin(prefix):
10 prefix = hex(int(prefix, 2))[2:]
11 ip = ''
12 for i in xrange(0, len(prefix) - 1, 4):
13 ip += prefix[i:i+4] + ':'
14 return ip.rstrip(':')
15
16 def ipFromPrefix(vifibnet, prefix, prefix_len):
17 prefix = bin(int(prefix))[2:].rjust(prefix_len, '0')
18 ip_t = (vifibnet + prefix).ljust(128, '0')
19 return ipFromBin(ip_t)
20
21 def networkFromCa(ca_path):
22 # Get network prefix from ca.crt
23 with open(ca_path, 'r') as f:
24 ca = crypto.load_certificate(crypto.FILETYPE_PEM, f.read())
25 return bin(ca.get_serial_number())[3:]
26
27 def ipFromCert(network, cert_path):
28 # Get ip from cert.crt
29 with open(cert_path, 'r') as f:
30 cert = crypto.load_certificate(crypto.FILETYPE_PEM, f.read())
31 subject = cert.get_subject()
32 prefix, prefix_len = subject.CN.split('/')
33 return ipFromPrefix(network, prefix, int(prefix_len))
34
35 def ovpnArgs(optional_args, ca_path, cert_path)
36 # Treat openvpn arguments
37 if optional_args[0] == "--":
38 del optional_args[0]
39 optional_args.append('--ca')
40 optional_args.append(config.ca)
41 optional_args.append('--cert')
42 optional_args.append(config.cert)
43 return optional_args
44
45
46
47
48
49
50
51
52
53
54
55