Mercurial Hosting > luan
annotate src/luan/host/main.luan @ 1396:a5f61890ad84
add check_postgres_password
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 10 Sep 2019 13:41:22 -0600 |
parents | 8fe777ba5045 |
children | 5b8f76e26ab7 |
rev | line source |
---|---|
1353
8d95711f6615
replace java() with require "java"
Franklin Schmidt <fschmidt@gmail.com>
parents:
1277
diff
changeset
|
1 require "java" |
1135 | 2 local Luan = require "luan:Luan.luan" |
3 local error = Luan.error | |
1164 | 4 local type = Luan.type or error() |
1135 | 5 local ipairs = Luan.ipairs or error() |
6 local try = Luan.try or error() | |
7 local Io = require "luan:Io.luan" | |
8 local Rpc = require "luan:Rpc.luan" | |
9 local Thread = require "luan:Thread.luan" | |
10 local String = require "luan:String.luan" | |
11 local literal = String.literal or error() | |
12 local lower = String.lower or error() | |
13 local matches = String.matches or error() | |
14 local Hosting = require "luan:host/Hosting.luan" | |
15 local Logging = require "luan:logging/Logging.luan" | |
16 local logger = Logging.logger "main" | |
1182 | 17 local WebHandler = Hosting.WebHandler or error() |
1181
51d1342e25ad
luanhost password handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1164
diff
changeset
|
18 local Util = require "classpath:luan/host/Util.luan" |
51d1342e25ad
luanhost password handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1164
diff
changeset
|
19 local read_password = Util.read_password or error() |
1394
8fe777ba5045
change postgres password
Franklin Schmidt <fschmidt@gmail.com>
parents:
1353
diff
changeset
|
20 local set_password = Util.set_password or error() |
1396
a5f61890ad84
add check_postgres_password
Franklin Schmidt <fschmidt@gmail.com>
parents:
1394
diff
changeset
|
21 local set_postgres_password = Util.set_postgres_password or error() |
a5f61890ad84
add check_postgres_password
Franklin Schmidt <fschmidt@gmail.com>
parents:
1394
diff
changeset
|
22 local check_postgres_password = Util.check_postgres_password or error() |
1135 | 23 |
24 | |
25 local sites_dir = Io.schemes.file(Hosting.sites_dir) | |
26 | |
27 sites_dir.mkdir() | |
28 | |
29 local function delete_unused(file) | |
30 if file.is_directory() then | |
31 if file.name() == "local" then | |
32 return false | |
33 end | |
34 local all_deleted = true | |
35 for _,child in ipairs(file.children()) do | |
36 all_deleted = delete_unused(child) and all_deleted | |
37 end | |
38 if not all_deleted then | |
39 return false | |
40 end | |
41 end | |
42 file.delete() | |
43 return true | |
44 end | |
45 | |
46 | |
47 local fns = Rpc.functions | |
48 | |
49 local function get_dir(domain,password) | |
1164 | 50 type(domain)=="string" or error() |
51 type(password)=="string" or error() | |
1135 | 52 domain = lower(domain) |
53 local dir = sites_dir.child(domain) | |
54 if dir.exists() then | |
1181
51d1342e25ad
luanhost password handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1164
diff
changeset
|
55 local pwd = read_password(domain) |
1135 | 56 if pwd ~= password then |
57 error "wrong password" | |
58 end | |
59 return dir.child("site") | |
60 else | |
61 return nil | |
62 end | |
63 end | |
64 | |
65 function fns.get(domain,password) | |
66 local site_dir = get_dir(domain,password) | |
67 if site_dir == nil then | |
68 return nil | |
69 end | |
70 | |
71 local children, file_info | |
72 | |
73 function children(dir) | |
74 if dir.name() == "local" then | |
75 return {} | |
76 end | |
77 local rtn = {} | |
78 for _,child in ipairs(dir.children()) do | |
79 local info = file_info(child) | |
80 if info ~= nil then | |
81 rtn[info.name] = info | |
82 end | |
83 end | |
84 return rtn | |
85 end | |
86 | |
87 function file_info(file) | |
88 local info = { name = file.name(), path = file.to_string() } | |
89 if file.is_directory() then | |
90 info.children = children(file) | |
91 elseif file.is_file() then | |
92 info.checksum = file.checksum() | |
93 else | |
94 return nil | |
95 end | |
96 return info | |
97 end | |
98 | |
99 return file_info(site_dir) | |
100 end | |
101 | |
102 function fns.create(domain,password) | |
1164 | 103 type(domain)=="string" or error() |
104 type(password)=="string" or error() | |
1135 | 105 domain = lower(domain) |
106 local dir = sites_dir.child(domain) | |
107 dir.exists() and error "already exists" | |
1396
a5f61890ad84
add check_postgres_password
Franklin Schmidt <fschmidt@gmail.com>
parents:
1394
diff
changeset
|
108 check_postgres_password(domain,password) |
1135 | 109 dir.mkdir() |
1394
8fe777ba5045
change postgres password
Franklin Schmidt <fschmidt@gmail.com>
parents:
1353
diff
changeset
|
110 set_password(domain,password) |
1135 | 111 dir = dir.child("site") |
112 dir.mkdir() | |
113 return { name = dir.name(), path = dir.to_string(), children = {} } | |
114 end | |
115 | |
116 local function security(site_dir,file) | |
117 matches( file.to_string(), "^"..literal(site_dir.to_string()) ) or error "security violation" | |
118 end | |
119 | |
120 function fns.copy_file(domain,password,dir,name,content) | |
121 local site_dir = get_dir(domain,password) | |
122 site_dir or error "domain not found" | |
123 local file = Io.schemes.file(dir).child(name) | |
124 security(site_dir,file) | |
125 file.delete() | |
126 file.write(content) | |
127 end | |
128 | |
129 function fns.mkdir(domain,password,dir,name) | |
130 local site_dir = get_dir(domain,password) | |
131 site_dir or error "domain not found" | |
132 local file = Io.schemes.file(dir).child(name) | |
133 security(site_dir,file) | |
134 file.mkdir() | |
135 return { name = file.name(), path = file.to_string(), children = {} } | |
136 end | |
137 | |
138 function fns.delete_unused(domain,password,path) | |
139 local site_dir = get_dir(domain,password) | |
140 site_dir or error "domain not found" | |
141 local file = Io.schemes.file(path) | |
142 security(site_dir,file) | |
143 return delete_unused(file) | |
144 end | |
145 | |
146 function fns.update_handler(domain,password) | |
147 local site_dir = get_dir(domain,password) | |
148 site_dir or error "domain not found" | |
149 domain = lower(domain) | |
150 WebHandler.removeHandler(domain) | |
151 WebHandler.loadHandler(domain) | |
152 end | |
153 | |
154 function fns.delete(domain,password) | |
155 local site_dir = get_dir(domain,password) | |
156 site_dir or error "domain not found" | |
157 site_dir.parent().delete() | |
158 domain = lower(domain) | |
159 WebHandler.removeHandler(domain) | |
160 end | |
161 | |
162 function fns.exists(domain) | |
1164 | 163 type(domain)=="string" or error() |
1135 | 164 domain = lower(domain) |
165 return sites_dir.child(domain).exists() | |
166 end | |
167 | |
168 function fns.change_domain(old_domain,new_domain,password) | |
169 local old_dir = get_dir(old_domain,password) | |
170 old_dir or error "domain not found" | |
171 old_dir = old_dir.parent() | |
1164 | 172 type(new_domain)=="string" or error() |
1135 | 173 new_domain = lower(new_domain) |
174 local new_dir = sites_dir.child(new_domain) | |
175 new_dir.exists() and error "new_domain already exists" | |
176 WebHandler.removeHandler(old_domain) | |
177 old_dir.rename_to(new_dir.to_string()) | |
178 WebHandler.removeHandler(old_domain) | |
179 WebHandler.loadHandler(new_domain) | |
180 end | |
181 | |
182 function fns.change_password(domain,old_password,new_password) | |
183 local site_dir = get_dir(domain,old_password) | |
184 site_dir or error "domain not found" | |
1394
8fe777ba5045
change postgres password
Franklin Schmidt <fschmidt@gmail.com>
parents:
1353
diff
changeset
|
185 set_password(domain,new_password) |
1396
a5f61890ad84
add check_postgres_password
Franklin Schmidt <fschmidt@gmail.com>
parents:
1394
diff
changeset
|
186 set_postgres_password(domain,new_password) |
1135 | 187 WebHandler.removeHandler(domain) |
188 WebHandler.loadHandler(domain) | |
189 end | |
190 | |
191 fns.call = WebHandler.callSite | |
192 | |
193 Thread.fork(Rpc.serve) |