From 9b4e269bea4db2e75d3d33757a53d2ab89bf05bf Mon Sep 17 00:00:00 2001 From: Steven Barth Date: Sun, 30 Mar 2008 19:12:16 +0000 Subject: [PATCH] * Added reboot page * Added SSH-Keys page * ffluci.template: Removed a debugging statement --- src/ffluci/controller/admin/system.lua | 50 ++++++++++++++++++------ src/ffluci/fs.lua | 10 +++-- src/ffluci/http.lua | 10 +++-- src/ffluci/sys.lua | 21 ++++++++-- src/ffluci/template.lua | 21 ++++++---- src/ffluci/view/admin_system/editor.htm | 2 +- src/ffluci/view/admin_system/passwd.htm | 33 ++++++++++++---- src/ffluci/view/admin_system/reboot.htm | 4 ++ src/ffluci/view/admin_system/sshkeys.htm | 23 +++++++++++ 9 files changed, 136 insertions(+), 38 deletions(-) create mode 100644 src/ffluci/view/admin_system/reboot.htm create mode 100644 src/ffluci/view/admin_system/sshkeys.htm diff --git a/src/ffluci/controller/admin/system.lua b/src/ffluci/controller/admin/system.lua index 5e3204153..455424eb8 100644 --- a/src/ffluci/controller/admin/system.lua +++ b/src/ffluci/controller/admin/system.lua @@ -9,19 +9,21 @@ menu = { descr = "System", order = 20, entries = { - {action = "passwd", descr = "Passwort"}, + {action = "passwd", descr = "Passwort ändern"}, + {action = "sshkeys", descr = "SSH-Schlüssel"}, + {action = "reboot", descr = "Neu starten"}, } } function action_editor() - local file = ffluci.http.formvalue("file") + local file = ffluci.http.formvalue("file", "") local data = ffluci.http.formvalue("data") local err = nil local msg = nil - local stat = nil + local stat = true if file and data then - stat, err = pcall(ffluci.fs.writefile, file, data) + stat, err = ffluci.fs.writefile(file, data) end if not stat then @@ -30,11 +32,9 @@ function action_editor() msg = table.concat(err, " ") end - local stat, cnt = pcall(ffluci.fs.readfile, fname) - if stat and cnt then + local cnt, err = ffluci.fs.readfile(file) + if cnt then cnt = ffluci.util.pcdata(cnt) - else - cnt = nil end ffluci.template.render("admin_system/editor", {fn=file, cnt=cnt, msg=msg}) end @@ -42,12 +42,38 @@ end function action_passwd() local p1 = ffluci.http.formvalue("pwd1") local p2 = ffluci.http.formvalue("pwd2") - local msg = nil - local cm + local stat = nil if p1 or p2 then - msg = ffluci.sys.user.setpasswd("root", p1, p2) + if p1 == p2 then + stat = ffluci.sys.user.setpasswd("root", p1) + else + stat = 10 + end + end + + ffluci.template.render("admin_system/passwd", {stat=stat}) +end + +function action_reboot() + ffluci.template.render("admin_system/reboot") + ffluci.sys.reboot() +end + +function action_sshkeys() + local file = "/etc/dropbear/authorized_keys" + local data = ffluci.http.formvalue("data") + local stat = nil + local err = nil + + if data then + stat, err = ffluci.fs.writefile(file, data) + end + + local cnt = ffluci.fs.readfile(file) + if cnt then + cnt = ffluci.util.pcdata(cnt) end - ffluci.template.render("admin_system/passwd", {msg=msg}) + ffluci.template.render("admin_system/sshkeys", {cnt=cnt, msg=err}) end \ No newline at end of file diff --git a/src/ffluci/fs.lua b/src/ffluci/fs.lua index e262caa3c..897308c10 100644 --- a/src/ffluci/fs.lua +++ b/src/ffluci/fs.lua @@ -40,7 +40,7 @@ function readfile(filename) local fp, err = io.open(filename) if fp == nil then - error(err) + return nil, err end local data = fp:read("*a") @@ -55,7 +55,7 @@ function readfilel(filename) local data = {} if fp == nil then - error(err) + return nil, err end while true do @@ -71,11 +71,15 @@ end -- Writes given data to a file function writefile(filename, data) local fp, err = io.open(filename, "w") + if fp == nil then - error(err) + return nil, err end + fp:write(data) fp:close() + + return true end -- Returns the file modification date/time of "path" diff --git a/src/ffluci/http.lua b/src/ffluci/http.lua index 81076233b..b7ce92ffa 100644 --- a/src/ffluci/http.lua +++ b/src/ffluci/http.lua @@ -38,20 +38,24 @@ end -- Asks the browser to redirect to "url" -function redirect(url) +function redirect(url, qs) + if qs then + url = url .. "?" .. qs + end + status(302, "Found") print("Location: " .. url .. "\n") end -- Same as redirect but accepts category, module and action for internal use -function request_redirect(category, module, action) +function request_redirect(category, module, action, ...) category = category or "public" module = module or "index" action = action or "index" local pattern = os.getenv("SCRIPT_NAME") .. "/%s/%s/%s" - redirect(pattern:format(category, module, action)) + redirect(pattern:format(category, module, action), ...) end diff --git a/src/ffluci/sys.lua b/src/ffluci/sys.lua index 97a926b0b..532324d4b 100644 --- a/src/ffluci/sys.lua +++ b/src/ffluci/sys.lua @@ -38,6 +38,11 @@ function loadavg() return loadavg:match("^(.-) (.-) (.-) (.-) (.-)$") end +-- Reboots the system +function reboot() + return os.execute("reboot >/dev/null 2>&1") +end + group = {} group.getgroup = posix.getgroup @@ -70,8 +75,16 @@ user = {} user.getuser = posix.getpasswd -- Changes the user password of given user -function user.setpasswd(user, pwd1, pwd2) - local cmd = "(echo '"..pwd1.."';sleep 1;echo '"..pwd2.."')|" - cmd = cmd .. "passwd "..user.." 2>&1" - return ffluci.util.exec(cmd) +function user.setpasswd(user, pwd) + if pwd then + pwd = pwd:gsub("'", "") + end + + if user then + user = user:gsub("'", "") + end + + local cmd = "(echo '"..pwd.."';sleep 1;echo '"..pwd.."')|" + cmd = cmd .. "passwd '"..user.."' >/dev/null 2>&1" + return os.execute(cmd) end \ No newline at end of file diff --git a/src/ffluci/template.lua b/src/ffluci/template.lua index 2bc015081..52bebbcf4 100644 --- a/src/ffluci/template.lua +++ b/src/ffluci/template.lua @@ -120,10 +120,6 @@ function compile(template) template = string.dump(tf) end - c = c or 1 - ffluci.fs.writefile("/tmp/"..tostring(c), template) - c = c+1 - return template end @@ -179,9 +175,14 @@ function Template.__init__(self, name) -- Build if there is no compiled file or if compiled file is outdated if ((commt == nil) and not (tplmt == nil)) or (not (commt == nil) and not (tplmt == nil) and commt < tplmt) then - local compiled = compile(ffluci.fs.readfile(sourcefile)) - ffluci.fs.writefile(compiledfile, compiled) - self.template, err = loadstring(compiled) + local source + source, err = ffluci.fs.readfile(sourcefile) + + if source then + local compiled = compile(source) + ffluci.fs.writefile(compiledfile, compiled) + self.template, err = loadstring(compiled) + end else self.template, err = loadfile(compiledfile) end @@ -190,7 +191,11 @@ function Template.__init__(self, name) self.template, err = loadfile(self.compiledfile) elseif compiler_mode == "memory" then - self.template, err = loadstring(compile(ffluci.fs.readfile(sourcefile))) + local source + source, err = ffluci.fs.readfile(sourcefile) + if source then + self.template, err = loadstring(compile(source)) + end end diff --git a/src/ffluci/view/admin_system/editor.htm b/src/ffluci/view/admin_system/editor.htm index d4b3302ef..0215c91df 100644 --- a/src/ffluci/view/admin_system/editor.htm +++ b/src/ffluci/view/admin_system/editor.htm @@ -1,7 +1,7 @@ <%+header%>

<%:texteditor Texteditor%>

-
<%:file Datei%>: +
<%:file Datei%>: <% if msg then %><%:error Fehler%>: <%=msg%><% end %>

diff --git a/src/ffluci/view/admin_system/passwd.htm b/src/ffluci/view/admin_system/passwd.htm index 3458fef92..441753d83 100644 --- a/src/ffluci/view/admin_system/passwd.htm +++ b/src/ffluci/view/admin_system/passwd.htm @@ -1,14 +1,33 @@ <%+header%>

<%:system System%>

-

<%:changepw Passwort ändern%>

+

<%:passwd Passwort ändern%>


-<% if msg then %> - <%=msg%> -<% else %> +<% if stat then %> + <% if stat == 0 then %> + <%:password_changed Passwort erfolgreich geändert!%> + <% elseif stat == 10 then %> + <%:password_nomatch Passwörter stimmen nicht überein! %> + <% else %> + <%:unknown_error Unbekannter Fehler!%> + <% end %> +<% end %> +<% if not stat or stat == 10 then %> - <%:password Passwort%>
- <%:confirmation Bestätigung%>
- +
+
+
<%:password Passwort%>
+
+
+
+
<%:confirmation Bestätigung%>
+
+
+
+
+ + +
+
<% end %>
diff --git a/src/ffluci/view/admin_system/reboot.htm b/src/ffluci/view/admin_system/reboot.htm new file mode 100644 index 000000000..a81464409 --- /dev/null +++ b/src/ffluci/view/admin_system/reboot.htm @@ -0,0 +1,4 @@ +<%+header%> +

<%:system System%>

+

<%:reboot Neu starten%>

+<%+footer%> \ No newline at end of file diff --git a/src/ffluci/view/admin_system/sshkeys.htm b/src/ffluci/view/admin_system/sshkeys.htm new file mode 100644 index 000000000..1e1cc24ce --- /dev/null +++ b/src/ffluci/view/admin_system/sshkeys.htm @@ -0,0 +1,23 @@ +<%+header%> +

<%:system System%>

+

<%:sshkeys SSH-Schlüssel%>

+ +
+ +
<%:sshkeys_descr Hier können öffentliche SSH-Schlüssel (einer pro Zeile) + zur Authentifizierung abgelegt werden.%>
+ +
+ +
+
+
+
+
+ + +
+ <% if msg then %>
<%:error Fehler%>: <%=msg%>
<% end %> +
+
+<%+footer%> \ No newline at end of file -- 2.25.1