changeset 1181:51d1342e25ad

luanhost password handling
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 20 Feb 2018 19:50:30 -0700
parents 8ca49f5e114d
children 0b55a1af5a44
files src/luan/host/Init.luan src/luan/host/Util.luan src/luan/host/main.luan src/luan/host/run.luan src/luan/modules/IoLuan.java
diffstat 5 files changed, 82 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/host/Init.luan	Wed Feb 14 16:49:56 2018 -0700
+++ b/src/luan/host/Init.luan	Tue Feb 20 19:50:30 2018 -0700
@@ -15,7 +15,7 @@
 
 local dir, domain = ...
 
-Init.password = Io.schemes.file(dir).child("password").read_text()
+Init.password = Luan.do_file(dir.."/info.luan").password or error()
 
 Http.dir = "file:"..dir.."/site"
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan/host/Util.luan	Tue Feb 20 19:50:30 2018 -0700
@@ -0,0 +1,36 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local do_file = Luan.do_file or error()
+local Io = require "luan:Io.luan"
+local String = require "luan:String.luan"
+local lower = String.lower or error()
+local encode = String.encode or error()
+local Hosting = require "luan:host/Hosting.luan"
+
+java()
+local Base64 = require "java:java.util.Base64"
+local MessageDigest = require "java:java.security.MessageDigest"
+
+
+local Util = {}
+
+function Util.read_password(domain)
+	domain = lower(domain)
+	return do_file(Hosting.sites_dir..domain.."/info.luan").password or error()
+end
+
+function Util.write_password(domain,password)
+	local dir = Hosting.sites_dir..lower(domain)
+	Io.schemes.file(dir.."/info.luan").write_text( %>
+return {
+	password = "<%=encode(password)%>"
+}
+<%	)
+	local sha1 = MessageDigest.getInstance("SHA1").digest(password.getBytes())
+	local encoded = Base64.getEncoder().encodeToString(sha1)
+	Io.schemes.file(dir.."/password.nginx").write_text( %>
+admin:{SHA}<%=encoded%>
+<%	)
+end
+
+return Util
--- a/src/luan/host/main.luan	Wed Feb 14 16:49:56 2018 -0700
+++ b/src/luan/host/main.luan	Tue Feb 20 19:50:30 2018 -0700
@@ -16,6 +16,9 @@
 local Logging = require "luan:logging/Logging.luan"
 local logger = Logging.logger "main"
 local WebHandler = require "java:luan.host.WebHandler"
+local Util = require "classpath:luan/host/Util.luan"
+local read_password = Util.read_password or error()
+local write_password = Util.write_password or error()
 
 
 local sites_dir = Io.schemes.file(Hosting.sites_dir)
@@ -48,7 +51,7 @@
 	domain = lower(domain)
 	local dir = sites_dir.child(domain)
 	if dir.exists() then
-		local pwd = dir.child("password").read_text()
+		local pwd = read_password(domain)
 		if pwd ~= password then
 			error "wrong password"
 		end
@@ -102,7 +105,7 @@
 	local dir = sites_dir.child(domain)
 	dir.exists() and error "already exists"
 	dir.mkdir()
-	dir.child("password").write(password)
+	write_password(domain,password)
 	dir = dir.child("site")
 	dir.mkdir()
 	return { name = dir.name(), path = dir.to_string(), children = {} }
@@ -177,7 +180,7 @@
 function fns.change_password(domain,old_password,new_password)
 	local site_dir = get_dir(domain,old_password)
 	site_dir or error "domain not found"
-	site_dir.parent().child("password").write(new_password)
+	write_password(domain,new_password)
 	WebHandler.removeHandler(domain)
 	WebHandler.loadHandler(domain)
 end
--- a/src/luan/host/run.luan	Wed Feb 14 16:49:56 2018 -0700
+++ b/src/luan/host/run.luan	Tue Feb 20 19:50:30 2018 -0700
@@ -1,3 +1,4 @@
+require "luan:logging/init.luan"  -- initialize logging
 local Luan = require "luan:Luan.luan"
 local error = Luan.error
 local do_file = Luan.do_file or error()
@@ -6,15 +7,30 @@
 local print = Io.print or error()
 local String = require "luan:String.luan"
 local Hosting = require "luan:host/Hosting.luan"
-require "luan:logging/init.luan"  -- initialize logging
+local Logging = require "luan:logging/Logging.luan"
+local logger = Logging.logger "run"
 
 
 local here = Io.schemes.file(".").canonical().to_string()
 Hosting.sites_dir = here.."/sites/"
+
+-- tmp
+local Util = require "classpath:luan/host/Util.luan"
+local sites_dir = Io.schemes.file(Hosting.sites_dir)
+for _, site in ipairs(sites_dir.children()) do
+	local password_file = site.child("password")
+	if password_file.exists() then
+		local domain = site.name()
+		local password = password_file.read_text()
+		Util.write_password(domain,password)
+		password_file.delete()
+		logger.info("fixed password for "..domain)
+	end
+end
+
 do_file "classpath:luan/host/main.luan"
 
 
-
 -- web server
 
 java()
--- a/src/luan/modules/IoLuan.java	Wed Feb 14 16:49:56 2018 -0700
+++ b/src/luan/modules/IoLuan.java	Tue Feb 20 19:50:30 2018 -0700
@@ -59,8 +59,8 @@
 		public void close() throws IOException;
 	}
 
-	public static LuanTable textWriter(final PrintStream out) {
-		LuanWriter luanWriter = new LuanWriter() {
+	private static LuanWriter luanWriter(final PrintStream out) {
+		return new LuanWriter() {
 
 			public void write(LuanState luan,Object... args) throws LuanException {
 				for( Object obj : args ) {
@@ -72,11 +72,14 @@
 				out.close();
 			}
 		};
-		return writer(luanWriter);
 	}
 
-	public static LuanTable textWriter(final Writer out) {
-		LuanWriter luanWriter = new LuanWriter() {
+	public static LuanTable textWriter(final PrintStream out) {
+		return writer(luanWriter(out));
+	}
+
+	private static LuanWriter luanWriter(final Writer out) {
+		return new LuanWriter() {
 
 			public void write(LuanState luan,Object... args) throws LuanException, IOException {
 				for( Object obj : args ) {
@@ -88,7 +91,10 @@
 				out.close();
 			}
 		};
-		return writer(luanWriter);
+	}
+
+	public static LuanTable textWriter(final Writer out) {
+		return writer(luanWriter(out));
 	}
 
 	private static LuanTable writer(LuanWriter luanWriter) {
@@ -340,6 +346,12 @@
 			return binaryWriter(new BufferedOutputStream(outputStream()));
 		}
 
+		public void write_text(LuanState luan,Object... args) throws LuanException, IOException {
+			LuanWriter luanWriter = luanWriter(new BufferedWriter(new OutputStreamWriter(outputStream())));
+			luanWriter.write(luan,args);
+			luanWriter.close();
+		}
+
 		@Override public LuanTable table() {
 			LuanTable tbl = super.table();
 			try {
@@ -352,6 +364,9 @@
 				tbl.rawPut( "binary_writer", new LuanJavaFunction(
 					LuanIO.class.getMethod( "binary_writer" ), this
 				) );
+				tbl.rawPut( "write_text", new LuanJavaFunction(
+					LuanIO.class.getMethod( "write_text", LuanState.class, new Object[0].getClass() ), this
+				) );
 			} catch(NoSuchMethodException e) {
 				throw new RuntimeException(e);
 			}