remote.lua 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. --[[
  2. LuCI - Lua Configuration Interface
  3. Copyright 2008 Steven Barth <steven@midlink.org>
  4. Copyright 2011 Jo-Philipp Wich <xm@subsignal.org>
  5. Copyright 2013 Nils Schneider <nils@nilsschneider.net>
  6. Licensed under the Apache License, Version 2.0 (the "License");
  7. you may not use this file except in compliance with the License.
  8. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. $Id$
  11. ]]--
  12. local fs = require "nixio.fs"
  13. local f_keys = SimpleForm('keys', translate("SSH keys"), translate("You can provide your SSH keys here (one per line):"))
  14. f_keys.hidden = { submit_keys = '1' }
  15. local keys
  16. keys = f_keys:field(TextValue, "keys", "")
  17. keys.wrap = "off"
  18. keys.rows = 5
  19. keys.rmempty = true
  20. function keys.cfgvalue()
  21. return fs.readfile("/etc/dropbear/authorized_keys") or ""
  22. end
  23. function keys.write(self, section, value)
  24. if not f_keys:formvalue('submit_keys') then return end
  25. fs.writefile("/etc/dropbear/authorized_keys", value:gsub("\r\n", "\n"):trim() .. "\n")
  26. end
  27. function keys.remove(self, section)
  28. if not f_keys:formvalue('submit_keys') then return end
  29. fs.remove("/etc/dropbear/authorized_keys")
  30. end
  31. local f_password = SimpleForm('password', translate("Password"),
  32. translate(
  33. "Alternatively, you can set a password to access you node. Please choose a secure password you don't use anywhere else.<br /><br />"
  34. .. "If you set an empty password, login via password will be disabled. This is the default."
  35. )
  36. )
  37. f_password.hidden = { submit_password = '1' }
  38. f_password.reset = false
  39. local pw1 = f_password:field(Value, "pw1", translate("Password"))
  40. pw1.password = true
  41. function pw1.cfgvalue()
  42. return ''
  43. end
  44. local pw2 = f_password:field(Value, "pw2", translate("Confirmation"))
  45. pw2.password = true
  46. function pw2.cfgvalue()
  47. return ''
  48. end
  49. function f_password:handle(state, data)
  50. if not f_password:formvalue('submit_password') then return end
  51. if data.pw1 ~= data.pw2 then
  52. f_password.errmessage = translate("The password and the confirmation differ.")
  53. return
  54. end
  55. if data.pw1 and #data.pw1 > 0 then
  56. if luci.sys.user.setpasswd('root', data.pw1) == 0 then
  57. f_password.message = translate("Password changed.")
  58. else
  59. f_password.errmessage = translate("Unable to change the password.")
  60. end
  61. else
  62. -- We don't check the return code here as the error 'password for root is already locked' is normal...
  63. os.execute('passwd -l root >/dev/null')
  64. f_password.message = translate("Password removed.")
  65. end
  66. end
  67. return f_keys, f_password