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