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