Removing the bootstrap option from the registry
[re6stnet.git] / re6st-registry
1 #!/usr/bin/env python
2 import argparse, random, select, smtplib, sqlite3, string, socket
3 import subprocess, time, threading, traceback, errno, logging, os, xmlrpclib
4 from SimpleXMLRPCServer import SimpleXMLRPCServer, SimpleXMLRPCRequestHandler
5 from email.mime.text import MIMEText
6 from OpenSSL import crypto
7 from re6st  import utils
8
9 # To generate server ca and key with serial for 2001:db8:42::/48
10 # openssl req -nodes -new -x509 -key ca.key -set_serial 0x120010db80042 -days 365 -out ca.crt
11
12 IPV6_V6ONLY = 26
13 SOL_IPV6 = 41
14
15
16 class RequestHandler(SimpleXMLRPCRequestHandler):
17
18     def _dispatch(self, method, params):
19         return self.server._dispatch(method, (self,) + params)
20
21
22 class SimpleXMLRPCServer4(SimpleXMLRPCServer):
23
24     allow_reuse_address = True
25
26     def address_string(self):
27         # Workaround for http://bugs.python.org/issue6085
28         return self.client_address[0]
29
30
31 class SimpleXMLRPCServer6(SimpleXMLRPCServer4):
32
33     address_family = socket.AF_INET6
34
35     def server_bind(self):
36         self.socket.setsockopt(SOL_IPV6, IPV6_V6ONLY, 1)
37         SimpleXMLRPCServer4.server_bind(self)
38
39
40 class main(object):
41
42     def __init__(self):
43         self.cert_duration = 365 * 86400
44         self.time_out = 86400
45         self.refresh_interval = 600
46         self.last_refresh = time.time()
47
48         utils.setupLog(3)
49
50         # Command line parsing
51         parser = argparse.ArgumentParser(
52                 description='Peer discovery http server for re6stnet')
53         _ = parser.add_argument
54         _('--port', required=True, type=int, help='Port of the host server')
55         _('--db', required=True,
56                 help='Path to database file')
57         _('--ca', required=True,
58                 help='Path to ca.crt file')
59         _('--key', required=True,
60                 help='Path to certificate key')
61         _('--mailhost', required=True,
62                 help='SMTP server mail host')
63         _('--private',
64                 help='VPN IP of the node on which runs the registry')
65         self.config = parser.parse_args()
66
67         if not self.config.private:
68             logging.warning('You have declared no private address'
69                     ', either this is the first start, or you should'
70                     'check you configuration')
71
72         # Database initializing
73         self.db = sqlite3.connect(self.config.db, isolation_level=None)
74         self.db.execute("""CREATE TABLE IF NOT EXISTS peers (
75                         prefix text primary key not null,
76                         address text not null,
77                         date integer default (strftime('%s','now')))""")
78         self.db.execute("CREATE INDEX IF NOT EXISTS peers_ping ON peers(date)")
79         self.db.execute("""CREATE TABLE IF NOT EXISTS tokens (
80                         token text primary key not null,
81                         email text not null,
82                         prefix_len integer not null,
83                         date integer not null)""")
84         try:
85             self.db.execute("""CREATE TABLE vpn (
86                                prefix text primary key not null,
87                                email text,
88                                cert text)""")
89         except sqlite3.OperationalError, e:
90             if e.args[0] != 'table vpn already exists':
91                 raise RuntimeError
92         else:
93             self.db.execute("INSERT INTO vpn VALUES ('',null,null)")
94
95         # Loading certificates
96         with open(self.config.ca) as f:
97             self.ca = crypto.load_certificate(crypto.FILETYPE_PEM, f.read())
98         with open(self.config.key) as f:
99             self.key = crypto.load_privatekey(crypto.FILETYPE_PEM, f.read())
100         # Get vpn network prefix
101         self.network = bin(self.ca.get_serial_number())[3:]
102         logging.info("Network prefix : %s/%u" % (self.network, len(self.network)))
103
104         # Starting server
105         server4 = SimpleXMLRPCServer4(('0.0.0.0', self.config.port), requestHandler=RequestHandler, allow_none=True)
106         server4.register_instance(self)
107         server6 = SimpleXMLRPCServer6(('::', self.config.port), requestHandler=RequestHandler, allow_none=True)
108         server6.register_instance(self)
109
110         # Main loop
111         while True:
112             try:
113                 r, w, e = select.select([server4, server6], [], [])
114             except (OSError, select.error) as e:
115                 if e.args[0] != errno.EINTR:
116                     raise
117             else:
118                 for r in r:
119                     r._handle_request_noblock()
120
121     def requestToken(self, handler, email):
122         while True:
123             # Generating token
124             token = ''.join(random.sample(string.ascii_lowercase, 8))
125             # Updating database
126             try:
127                 self.db.execute("INSERT INTO tokens VALUES (?,?,?,?)", (token, email, 16, int(time.time())))
128                 break
129             except sqlite3.IntegrityError:
130                 pass
131
132         # Creating and sending email
133         s = smtplib.SMTP(self.config.mailhost)
134         me = 'postmaster@re6st.net'
135         msg = MIMEText('Hello world !\nYour token : %s' % (token,))  # XXX
136         msg['Subject'] = '[re6stnet] Token Request'
137         msg['From'] = me
138         msg['To'] = email
139         s.sendmail(me, email, msg.as_string())
140         s.quit()
141
142     def _getPrefix(self, prefix_len):
143         max_len = 128 - len(self.network)
144         assert 0 < prefix_len <= max_len
145         try:
146             prefix, = self.db.execute("""SELECT prefix FROM vpn WHERE length(prefix) <= ? AND cert is null
147                                          ORDER BY length(prefix) DESC""", (prefix_len,)).next()
148         except StopIteration:
149             logging.error('There are no more free /%s prefix available' % (prefix_len,))
150             raise
151         while len(prefix) < prefix_len:
152             self.db.execute("UPDATE vpn SET prefix = ? WHERE prefix = ?", (prefix + '1', prefix))
153             prefix += '0'
154             self.db.execute("INSERT INTO vpn VALUES (?,null,null)", (prefix,))
155         if len(prefix) < max_len or '1' in prefix:
156             return prefix
157         self.db.execute("UPDATE vpn SET cert = 'reserved' WHERE prefix = ?", (prefix,))
158         return self._getPrefix(prefix_len)
159
160     def requestCertificate(self, handler, token, cert_req):
161         try:
162             req = crypto.load_certificate_request(crypto.FILETYPE_PEM, cert_req)
163             with self.db:
164                 try:
165                     token, email, prefix_len, _ = self.db.execute("SELECT * FROM tokens WHERE token = ?", (token,)).next()
166                 except StopIteration:
167                     logging.exception('Bad token (%s) in request' % (token,))
168                     raise
169                 self.db.execute("DELETE FROM tokens WHERE token = ?", (token,))
170
171                 # Get a new prefix
172                 prefix = self._getPrefix(prefix_len)
173
174                 # Create certificate
175                 cert = crypto.X509()
176                 #cert.set_serial_number(serial)
177                 cert.gmtime_adj_notBefore(0)
178                 cert.gmtime_adj_notAfter(self.cert_duration)
179                 cert.set_issuer(self.ca.get_subject())
180                 subject = req.get_subject()
181                 subject.CN = "%u/%u" % (int(prefix, 2), prefix_len)
182                 cert.set_subject(subject)
183                 cert.set_pubkey(req.get_pubkey())
184                 cert.sign(self.key, 'sha1')
185                 cert = crypto.dump_certificate(crypto.FILETYPE_PEM, cert)
186
187                 # Insert certificate into db
188                 self.db.execute("UPDATE vpn SET email = ?, cert = ? WHERE prefix = ?", (email, cert, prefix))
189
190             return cert
191         except:
192             traceback.print_exc()
193             raise
194
195     def getCa(self, handler):
196         return crypto.dump_certificate(crypto.FILETYPE_PEM, self.ca)
197
198     def getPrivateAddress(self, handler):
199         return 'http://[%s]:%u' % (self.config.private, self.config.port)
200
201     def getBootstrapPeer(self, handler, client_prefix):
202         cert, = self.db.execute("SELECT cert FROM vpn WHERE prefix = ?",
203                 (client_prefix,)).next()
204         logging.trace('Getting bootpeer info...')
205         try:
206             prefix, address = self.db.execute("""SELECT prefix, address FROM peers
207                 WHERE prefix != ? ORDER BY random() LIMIT 1""", (client_prefix,)).next()
208         except StopIteration:
209             logging.info('No peer to send for bootstrap')
210             raise
211         logging.trace('Gotten bootpeer info from db')
212         r, w = os.pipe()
213         try:
214             threading.Thread(target=os.write, args=(w, cert)).start()
215             p = subprocess.Popen(('openssl', 'rsautl', '-encrypt', '-certin', '-inkey', '/proc/self/fd/%u' % r),
216                 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
217             logging.info("Sending bootstrap peer (%s, %s)" % (prefix, address))
218             return xmlrpclib.Binary(p.communicate('%s %s' % (prefix, address))[0])
219         finally:
220             os.close(r)
221             os.close(w)
222
223     def declare(self, handler, address):
224         print "declaring new node"
225         client_address, _, _, _ = handler.client_address
226         client_ip = utils.binFromIp(client_address)
227         if client_ip.startswith(self.network):
228             prefix = client_ip[len(self.network):]
229             prefix, = self.db.execute("SELECT prefix FROM vpn WHERE prefix <= ? ORDER BY prefix DESC LIMIT 1", (prefix,)).next()
230             self.db.execute("INSERT OR REPLACE INTO peers (prefix, address) VALUES (?,?)", (prefix, address))
231             return True
232         else:
233             logging.warning("Unauthorized connection from %s which does not start with %s"
234                     % (utils.ipFromBin(client_ip), utils.ipFromBin(self.network.ljust(128, '0'))))
235             return False
236
237     def getPeerList(self, handler, n):
238         assert 0 < n < 1000
239         client_address, _, _, _ = handler.client_address
240         client_ip = utils.binFromIp(client_address)
241         if client_ip.startswith(self.network):
242             if time.time() > self.last_refresh + self.refresh_interval:
243                 print "refreshing peers for dead ones"
244                 self.db.execute("DELETE FROM peers WHERE ( date + ? ) <= CAST (strftime('%s', 'now') AS INTEGER)", (self.time_out,))
245                 self.last_refesh = time.time()
246             print "sending peers"
247             return self.db.execute("SELECT prefix, address FROM peers ORDER BY random() LIMIT ?", (n,)).fetchall()
248         else:
249             logging.warning("Unauthorized connection from %s which does not start with %s"
250                     % (utils.ipFromBin(client_ip), utils.ipFromBin(self.network.ljust(128, '0'))))
251             raise RuntimeError
252
253 if __name__ == "__main__":
254     main()