#!/usr/bin/env python import string import random import web from web import form from urllib import urlopen from urlparse import urlparse from contextlib import closing db = web.database(dbn='mysql', user='mysecrets', pw='horsebatteries', db='mysecrets') urls = ( '/secret/api/(.*)', 'api', '/secret/(.*)', 'index' ) app = web.application(urls, globals()) render = web.template.render('templates/') create = form.Form( form.Textbox('base_url', description="domain"), form.Textbox('username'), form.Password('password'), ) def trash(id): if db.select('passwords', where='id = "'+id+'"'): if not db.select('trash', where='id = "'+id+'"'): db.query('INSERT INTO trash SELECT * FROM passwords WHERE id = "'+id+'"') orig = db.select('passwords', where='id = "'+id+'"')[0] new = db.select('trash', where='id = "'+id+'"')[0] if orig == new: db.delete('passwords', where='id = "'+id+'"') return True; return False; def get_domain(base_url): with closing(urlopen('https://mxr.mozilla.org/mozilla/source/netwerk/dns/src/effective_tld_names.dat?raw=1')) as tldFile: tlds = [line.strip() for line in tldFile if line[0] not in "/\n"] urlElements = base_url.split('.') for i in range(-len(urlElements),0): lastIElements = urlElements[i:] candidate = ".".join(lastIElements) wildcardCandidate = ".".join(["*"]+lastIElements[1:]) exceptionCandidate = "!"+candidate if (exceptionCandidate in tlds): return ".".join(urlElements[i:]) if (candidate in tlds or wildcardCandidate in tlds): return ".".join(urlElements[i-1:]) return base_url def mkpass(size=10): validChars = string.ascii_letters + string.digits validChars = validChars.strip("oO01l") return string.join([random.choice(validChars) for x in range(size)],"") def get_pair_from_url(domain): a = db.select('passwords', where='base_url LIKE "%'+domain+'%"', order='id DESC') if not len(a) > 0: a = db.select('passwords', where='base_url LIKE "%'+get_domain(domain)+'%"', order='id DESC') return a def get_generated_from_url(domain): gen = db.select('generated', where='base_url LIKE "%'+get_domain(domain)+'%"', order='id DESC') while not len(gen) > 0: db.insert('generated', base_url = domain, password = mkpass()) gen = db.select('generated', where='base_url LIKE "%'+domain+'%"') return gen class index: def GET(self, method): if method == 'del': i = web.input() result = trash(i.id) if result: body = "id: " + i.id + " deleted." if not result: body = "id: " + i.id + " not deleted." return render.page('Deleted ' + i.id, body) if method == 'new': i = web.input() f = create() body = [] domain = i.base_url body.append('
' + generated[0].password + '
\n') body.append(''+i.username+', '+i.password+'
' return render.page('Created', body) if __name__ == "__main__": web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr) app.run()