comparison src/luan/host/Util.luan @ 1611:f67f972bd648

make postgres.luan optional
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 15 May 2021 17:24:07 -0600
parents 04615093b19d
children cf9dfead83a3
comparison
equal deleted inserted replaced
1610:94ea190714dd 1611:f67f972bd648
1 local Luan = require "luan:Luan.luan" 1 local Luan = require "luan:Luan.luan"
2 local error = Luan.error 2 local error = Luan.error
3 local do_file = Luan.do_file or error() 3 local do_file = Luan.do_file or error()
4 local ipairs = Luan.ipairs or error() 4 local ipairs = Luan.ipairs or error()
5 local stringify = Luan.stringify or error() 5 local stringify = Luan.stringify or error()
6 local Package = require "luan:Package.luan"
6 local Io = require "luan:Io.luan" 7 local Io = require "luan:Io.luan"
7 local String = require "luan:String.luan" 8 local String = require "luan:String.luan"
8 local lower = String.lower or error() 9 local lower = String.lower or error()
9 local format = String.format or error() 10 local format = String.format or error()
10 local to_binary = String.to_binary or error() 11 local to_binary = String.to_binary or error()
24 function Util.read_password(domain) 25 function Util.read_password(domain)
25 domain = lower(domain) 26 domain = lower(domain)
26 return do_file(Hosted.sites_dir..domain.."/info.luan").password or error() 27 return do_file(Hosted.sites_dir..domain.."/info.luan").password or error()
27 end 28 end
28 29
29 local function basic_authentication(dir,password)
30 local sha1 = digest_message("SHA1",to_binary(password))
31 local encoded = base64_encode(sha1)
32 local file = Io.schemes.file(dir.."/password.nginx")
33 file.delete()
34 file.write_text("admin:{SHA}"..encoded.."\n")
35 end
36
37 local function digest_authentication(dir,password)
38 local s = "admin:Restricted:"..password
39 local md5 = digest_message("MD5",to_binary(s))
40 md5 = {bytes(md5,1,#md5)}
41 local encoded = ""
42 for _, n in ipairs(md5) do
43 encoded = encoded..format("%02x",n)
44 end
45 local file = Io.schemes.file(dir.."/password.nginx")
46 file.delete()
47 file.write_text("admin:Restricted:"..encoded.."\n")
48 end
49
50 function Util.set_password(domain,password) 30 function Util.set_password(domain,password)
51 local dir = Hosted.sites_dir..lower(domain) 31 local dir = Hosted.sites_dir..lower(domain)
52 local file = Io.schemes.file(dir.."/info.luan") 32 local file = Io.schemes.file(dir.."/info.luan")
53 file.delete() 33 file.delete()
54 file.write_text("return "..stringify{password=password}.."\n") 34 file.write_text("return "..stringify{password=password}.."\n")
55 digest_authentication(dir,password)
56 end 35 end
57 36
58 local fn = Luan.load_file("file:postgres.luan") or error() 37 local pg_admin = Package.load("file:postgres.luan")
59 local pg_admin = fn()
60 38
61 function Util.set_postgres_password(domain,password) 39 function Util.set_postgres_password(domain,password)
62 if pg_admin == nil then 40 if pg_admin == false then
63 return 41 return
64 end 42 end
65 local db = database(pg_admin) 43 local db = database(pg_admin)
66 local exists = db.query("select rolname from pg_roles where rolname=?",domain).results() ~= nil; 44 local exists = db.query("select rolname from pg_roles where rolname=?",domain).results() ~= nil;
67 --logger.info("exists "..exists) 45 --logger.info("exists "..exists)
70 end 48 end
71 db.close() 49 db.close()
72 end 50 end
73 51
74 function Util.check_postgres_password(domain,password) 52 function Util.check_postgres_password(domain,password)
75 if pg_admin == nil then 53 if pg_admin == false then
76 return 54 return
77 end 55 end
78 local db = database(pg_admin) 56 local db = database(pg_admin)
79 local exists = db.query("select rolname from pg_roles where rolname=?",domain).results() ~= nil; 57 local exists = db.query("select rolname from pg_roles where rolname=?",domain).results() ~= nil;
80 db.close() 58 db.close()