* Added reboot page

* Added SSH-Keys page
* ffluci.template: Removed a debugging statement
This commit is contained in:
Steven Barth 2008-03-30 19:12:16 +00:00
parent eee2804791
commit 9b4e269bea
9 changed files with 136 additions and 38 deletions

View file

@ -9,19 +9,21 @@ menu = {
descr = "System", descr = "System",
order = 20, order = 20,
entries = { entries = {
{action = "passwd", descr = "Passwort"}, {action = "passwd", descr = "Passwort ändern"},
{action = "sshkeys", descr = "SSH-Schlüssel"},
{action = "reboot", descr = "Neu starten"},
} }
} }
function action_editor() function action_editor()
local file = ffluci.http.formvalue("file") local file = ffluci.http.formvalue("file", "")
local data = ffluci.http.formvalue("data") local data = ffluci.http.formvalue("data")
local err = nil local err = nil
local msg = nil local msg = nil
local stat = nil local stat = true
if file and data then if file and data then
stat, err = pcall(ffluci.fs.writefile, file, data) stat, err = ffluci.fs.writefile(file, data)
end end
if not stat then if not stat then
@ -30,11 +32,9 @@ function action_editor()
msg = table.concat(err, " ") msg = table.concat(err, " ")
end end
local stat, cnt = pcall(ffluci.fs.readfile, fname) local cnt, err = ffluci.fs.readfile(file)
if stat and cnt then if cnt then
cnt = ffluci.util.pcdata(cnt) cnt = ffluci.util.pcdata(cnt)
else
cnt = nil
end end
ffluci.template.render("admin_system/editor", {fn=file, cnt=cnt, msg=msg}) ffluci.template.render("admin_system/editor", {fn=file, cnt=cnt, msg=msg})
end end
@ -42,12 +42,38 @@ end
function action_passwd() function action_passwd()
local p1 = ffluci.http.formvalue("pwd1") local p1 = ffluci.http.formvalue("pwd1")
local p2 = ffluci.http.formvalue("pwd2") local p2 = ffluci.http.formvalue("pwd2")
local msg = nil local stat = nil
local cm
if p1 or p2 then 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 end
ffluci.template.render("admin_system/passwd", {msg=msg}) 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/sshkeys", {cnt=cnt, msg=err})
end end

View file

@ -40,7 +40,7 @@ function readfile(filename)
local fp, err = io.open(filename) local fp, err = io.open(filename)
if fp == nil then if fp == nil then
error(err) return nil, err
end end
local data = fp:read("*a") local data = fp:read("*a")
@ -55,7 +55,7 @@ function readfilel(filename)
local data = {} local data = {}
if fp == nil then if fp == nil then
error(err) return nil, err
end end
while true do while true do
@ -71,11 +71,15 @@ end
-- Writes given data to a file -- Writes given data to a file
function writefile(filename, data) function writefile(filename, data)
local fp, err = io.open(filename, "w") local fp, err = io.open(filename, "w")
if fp == nil then if fp == nil then
error(err) return nil, err
end end
fp:write(data) fp:write(data)
fp:close() fp:close()
return true
end end
-- Returns the file modification date/time of "path" -- Returns the file modification date/time of "path"

View file

@ -38,20 +38,24 @@ end
-- Asks the browser to redirect to "url" -- Asks the browser to redirect to "url"
function redirect(url) function redirect(url, qs)
if qs then
url = url .. "?" .. qs
end
status(302, "Found") status(302, "Found")
print("Location: " .. url .. "\n") print("Location: " .. url .. "\n")
end end
-- Same as redirect but accepts category, module and action for internal use -- 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" category = category or "public"
module = module or "index" module = module or "index"
action = action or "index" action = action or "index"
local pattern = os.getenv("SCRIPT_NAME") .. "/%s/%s/%s" local pattern = os.getenv("SCRIPT_NAME") .. "/%s/%s/%s"
redirect(pattern:format(category, module, action)) redirect(pattern:format(category, module, action), ...)
end end

View file

@ -38,6 +38,11 @@ function loadavg()
return loadavg:match("^(.-) (.-) (.-) (.-) (.-)$") return loadavg:match("^(.-) (.-) (.-) (.-) (.-)$")
end end
-- Reboots the system
function reboot()
return os.execute("reboot >/dev/null 2>&1")
end
group = {} group = {}
group.getgroup = posix.getgroup group.getgroup = posix.getgroup
@ -70,8 +75,16 @@ user = {}
user.getuser = posix.getpasswd user.getuser = posix.getpasswd
-- Changes the user password of given user -- Changes the user password of given user
function user.setpasswd(user, pwd1, pwd2) function user.setpasswd(user, pwd)
local cmd = "(echo '"..pwd1.."';sleep 1;echo '"..pwd2.."')|" if pwd then
cmd = cmd .. "passwd "..user.." 2>&1" pwd = pwd:gsub("'", "")
return ffluci.util.exec(cmd) 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 end

View file

@ -120,10 +120,6 @@ function compile(template)
template = string.dump(tf) template = string.dump(tf)
end end
c = c or 1
ffluci.fs.writefile("/tmp/"..tostring(c), template)
c = c+1
return template return template
end end
@ -179,9 +175,14 @@ function Template.__init__(self, name)
-- Build if there is no compiled file or if compiled file is outdated -- Build if there is no compiled file or if compiled file is outdated
if ((commt == nil) and not (tplmt == nil)) if ((commt == nil) and not (tplmt == nil))
or (not (commt == nil) and not (tplmt == nil) and commt < tplmt) then or (not (commt == nil) and not (tplmt == nil) and commt < tplmt) then
local compiled = compile(ffluci.fs.readfile(sourcefile)) local source
ffluci.fs.writefile(compiledfile, compiled) source, err = ffluci.fs.readfile(sourcefile)
self.template, err = loadstring(compiled)
if source then
local compiled = compile(source)
ffluci.fs.writefile(compiledfile, compiled)
self.template, err = loadstring(compiled)
end
else else
self.template, err = loadfile(compiledfile) self.template, err = loadfile(compiledfile)
end end
@ -190,7 +191,11 @@ function Template.__init__(self, name)
self.template, err = loadfile(self.compiledfile) self.template, err = loadfile(self.compiledfile)
elseif compiler_mode == "memory" then 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 end

View file

@ -1,7 +1,7 @@
<%+header%> <%+header%>
<h1><%:texteditor Texteditor%></h1> <h1><%:texteditor Texteditor%></h1>
<form method="post" action="<%=controller%>/admin/system/editor"> <form method="post" action="<%=controller%>/admin/system/editor">
<div><%:file Datei%>: <input type="text" name="file" size="30" value="<%=fn%>" /> <div><%:file Datei%>: <input type="text" name="file" size="30" value="<%=(fn or '')%>" />
<% if msg then %><span class="error"><%:error Fehler%>: <%=msg%></span><% end %></div> <% if msg then %><span class="error"><%:error Fehler%>: <%=msg%></span><% end %></div>
<br /> <br />
<div><textarea style="width: 100%" rows="20" name="data"><%=(cnt or '')%></textarea></div> <div><textarea style="width: 100%" rows="20" name="data"><%=(cnt or '')%></textarea></div>

View file

@ -1,14 +1,33 @@
<%+header%> <%+header%>
<h1><%:system System%></h1> <h1><%:system System%></h1>
<h2><%:changepw Passwort ändern%></h2> <h2><%:passwd Passwort ändern%></h2>
<div><br /> <div><br />
<% if msg then %> <% if stat then %>
<code><%=msg%></code> <% if stat == 0 then %>
<% else %> <code><%:password_changed Passwort erfolgreich geändert!%></code>
<% elseif stat == 10 then %>
<code class="error"><%:password_nomatch Passwörter stimmen nicht überein! %></code>
<% else %>
<code class="error"><%:unknown_error Unbekannter Fehler!%></code>
<% end %>
<% end %>
<% if not stat or stat == 10 then %>
<form method="post" action="<%=controller%>/admin/system/passwd"> <form method="post" action="<%=controller%>/admin/system/passwd">
<input type="password" name="pwd1" /> <%:password Passwort%><br /> <fieldset class="cbi-section-node">
<input type="password" name="pwd2" /> <%:confirmation Bestätigung%><br /> <div class="cbi-value clear">
<input type="submit" value="<%:save Speichern%>" /> <div class="cbi-value-title left"><%:password Passwort%></div>
<div class="cbi-value-field"><input type="password" name="pwd1" /></div>
</div>
<div class="cbi-value clear">
<div class="cbi-value-title left"><%:confirmation Bestätigung%></div>
<div class="cbi-value-field"><input type="password" name="pwd2" /></div>
</div>
<br />
<div>
<input type="submit" value="<%:save Speichern%>" />
<input type="reset" value="<%:reset Zurücksetzen%>" />
</div>
</fieldset>
</form> </form>
<% end %> <% end %>
</div> </div>

View file

@ -0,0 +1,4 @@
<%+header%>
<h1><%:system System%></h1>
<h2><%:reboot Neu starten%></h2>
<%+footer%>

View file

@ -0,0 +1,23 @@
<%+header%>
<h1><%:system System%></h1>
<h2><%:sshkeys SSH-Schlüssel%></h2>
<br />
<div><%:sshkeys_descr Hier können öffentliche SSH-Schlüssel (einer pro Zeile)
zur Authentifizierung abgelegt werden.%></div>
<br />
<form method="post" action="<%=controller%>/admin/system/sshkeys">
<fieldset class="cbi-section-node">
<div><textarea style="width: 100%" rows="10" name="data"><%=(cnt or '')%></textarea></div>
<br />
<div>
<input type="submit" value="<%:save Speichern%>" />
<input type="reset" value="<%:reset Zurücksetzen%>" />
</div>
<% if msg then %><br /><div class="error"><%:error Fehler%>: <%=msg%></div><% end %>
</fieldset>
</form>
<%+footer%>