modules/admin-full: merge system/password, system/sshkeys and service/dropbear into...
[project/luci.git] / modules / admin-full / luasrc / model / cbi / admin_system / admin.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2011 Jo-Philipp Wich <xm@subsignal.org>
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11         http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14 ]]--
15
16 local fs = require "nixio.fs"
17
18 m = Map("system", translate("Router Password"),
19         translate("Changes the administrator password for accessing the device"))
20
21 s = m:section(TypedSection, "_dummy", "")
22 s.addremove = false
23 s.anonymous = true
24
25 pw1 = s:option(Value, "pw1", translate("Password"))
26 pw1.password = true
27
28 pw2 = s:option(Value, "pw2", translate("Confirmation"))
29 pw2.password = true
30
31 function s.cfgsections()
32         return { "_pass" }
33 end
34
35 function m.on_commit(map)
36         local v1 = pw1:formvalue("_pass")
37         local v2 = pw2:formvalue("_pass")
38
39         if v1 and v2 and #v1 > 0 and #v2 > 0 then
40                 if v1 == v2 then
41                         if luci.sys.user.setpasswd("root", v1) == 0 then
42                                 m.message = translate("Password successfully changed!")
43                         else
44                                 m.message = translate("Unknown Error, password not changed!")
45                         end
46                 else
47                         m.message = translate("Given password confirmation did not match, password not changed!")
48                 end
49         end
50 end
51
52
53
54 m2 = Map("dropbear", translate("SSH Access"),
55         translate("Dropbear offers <abbr title=\"Secure Shell\">SSH</abbr> network shell access and an integrated <abbr title=\"Secure Copy\">SCP</abbr> server"))
56
57 s = m2:section(TypedSection, "dropbear", translate("Dropbear Instance"))
58 s.anonymous = true
59 s.addremove = true
60
61
62 ni = s:option(Value, "Interface", translate("Interface"),
63         translate("Listen only on the given interface or, if unspecified, on all"))
64
65 ni.template    = "cbi/network_netlist"
66 ni.nocreate    = true
67 ni.unspecified = true
68
69
70 pt = s:option(Value, "Port", translate("Port"),
71         translate("Specifies the listening port of this <em>Dropbear</em> instance"))
72
73 pt.datatype = "port"
74 pt.default  = 22
75
76
77 pa = s:option(Flag, "PasswordAuth", translate("Password authentication"),
78         translate("Allow <abbr title=\"Secure Shell\">SSH</abbr> password authentication"))
79
80 pa.enabled  = "on"
81 pa.disabled = "off"
82 pa.default  = pa.enabled
83 pa.rmempty  = false
84
85
86 ra = s:option(Flag, "RootPasswordAuth", translate("Allow root logins with password"),
87         translate("Allow the <em>root</em> user to login with password"))
88
89 ra.enabled  = "on"
90 ra.disabled = "off"
91 ra.default  = ra.enabled
92
93
94 gp = s:option(Flag, "GatewayPorts", translate("Gateway ports"),
95         translate("Allow remote hosts to connect to local SSH forwarded ports"))
96
97 gp.enabled  = "on"
98 gp.disabled = "off"
99 gp.default  = gp.disabled
100
101
102 s2 = m2:section(TypedSection, "_dummy", translate("SSH-Keys"),
103         translate("Here you can paste public SSH-Keys (one per line) for SSH public-key authentication."))
104 s2.addremove = false
105 s2.anonymous = true
106 s2.template  = "cbi/tblsection"
107
108 function s2.cfgsections()
109         return { "_keys" }
110 end
111
112 keys = s2:option(TextValue, "_data", "")
113 keys.wrap    = "off"
114 keys.rows    = 3
115 keys.rmempty = false
116
117 function keys.cfgvalue()
118         return fs.readfile("/etc/dropbear/authorized_keys") or ""
119 end
120
121 function keys.write(self, section, value)
122         if value then
123                 fs.writefile("/etc/dropbear/authorized_keys", value:gsub("\r\n", "\n"))
124         end
125 end
126
127 return m, m2