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
try: q = self.db.execute
prefix, = self.db.execute("""SELECT prefix FROM cert WHERE length(prefix) <= ? AND cert is null while True:
ORDER BY length(prefix) DESC""", (prefix_len,)).next() try:
except StopIteration: prefix, = q(
logging.error('No more free /%u prefix available', prefix_len) "SELECT prefix FROM cert"
raise " WHERE length(prefix) <= ? AND cert is null"
while len(prefix) < prefix_len: " ORDER BY length(prefix) DESC",
self.db.execute("UPDATE cert SET prefix = ? WHERE prefix = ?", (prefix + '1', prefix)) (prefix_len,)).next()
prefix += '0' except StopIteration:
self.db.execute("INSERT INTO cert VALUES (?,null,null)", (prefix,)) logging.error('No more free /%u prefix available', prefix_len)
if len(prefix) < max_len or '1' in prefix: raise
return prefix while len(prefix) < prefix_len:
self.db.execute("UPDATE cert SET cert = 'reserved' WHERE prefix = ?", (prefix,)) q("UPDATE cert SET prefix = ? WHERE prefix = ?",
return self.newPrefix(prefix_len) (prefix + '1', prefix))
prefix += '0'
q("INSERT INTO cert VALUES (?,null,null)", (prefix,))
if len(prefix) < max_len or '1' in prefix:
return prefix
q("UPDATE cert SET cert = 'reserved' WHERE prefix = ?", (prefix,))
@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