Commit 8ea1ddfc authored by Julien Muchembled's avatar Julien Muchembled Committed by Joanne Hugé

registry: clean-up

parent 99cf2adc
...@@ -88,7 +88,8 @@ class RegistryServer(object): ...@@ -88,7 +88,8 @@ class RegistryServer(object):
"prefix TEXT PRIMARY KEY NOT NULL", "prefix TEXT PRIMARY KEY NOT NULL",
"email TEXT", "email TEXT",
"cert TEXT") "cert TEXT")
self.db.execute("INSERT OR IGNORE INTO cert VALUES ('',null,null)") if not self.db.execute("SELECT 1 FROM cert LIMIT 1").fetchone():
self.db.execute("INSERT INTO cert VALUES ('',null,null)")
utils.sqliteCreateTable(self.db, "crl", utils.sqliteCreateTable(self.db, "crl",
"serial INTEGER PRIMARY KEY NOT NULL", "serial INTEGER PRIMARY KEY NOT NULL",
# Expiration date of revoked certificate. # Expiration date of revoked certificate.
...@@ -388,20 +389,25 @@ class RegistryServer(object): ...@@ -388,20 +389,25 @@ class RegistryServer(object):
def newPrefix(self, prefix_len): def newPrefix(self, prefix_len):
max_len = 128 - len(self.network) max_len = 128 - len(self.network)
assert 0 < prefix_len <= max_len assert 0 < prefix_len <= max_len
q = self.db.execute
while True:
try: try:
prefix, = self.db.execute("""SELECT prefix FROM cert WHERE length(prefix) <= ? AND cert is null prefix, = q(
ORDER BY length(prefix) DESC""", (prefix_len,)).next() "SELECT prefix FROM cert"
" WHERE length(prefix) <= ? AND cert is null"
" ORDER BY length(prefix) DESC",
(prefix_len,)).next()
except StopIteration: except StopIteration:
logging.error('No more free /%u prefix available', prefix_len) logging.error('No more free /%u prefix available', prefix_len)
raise raise
while len(prefix) < prefix_len: while len(prefix) < prefix_len:
self.db.execute("UPDATE cert SET prefix = ? WHERE prefix = ?", (prefix + '1', prefix)) q("UPDATE cert SET prefix = ? WHERE prefix = ?",
(prefix + '1', prefix))
prefix += '0' prefix += '0'
self.db.execute("INSERT INTO cert VALUES (?,null,null)", (prefix,)) q("INSERT INTO cert VALUES (?,null,null)", (prefix,))
if len(prefix) < max_len or '1' in prefix: if len(prefix) < max_len or '1' in prefix:
return prefix return prefix
self.db.execute("UPDATE cert SET cert = 'reserved' WHERE prefix = ?", (prefix,)) q("UPDATE cert SET cert = 'reserved' WHERE prefix = ?", (prefix,))
return self.newPrefix(prefix_len)
@rpc @rpc
def requestCertificate(self, token, req): def requestCertificate(self, token, req):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment