remote.lua 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. --[[
  2. Copyright 2008 Steven Barth <steven@midlink.org>
  3. Copyright 2011 Jo-Philipp Wich <xm@subsignal.org>
  4. Copyright 2013 Nils Schneider <nils@nilsschneider.net>
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. ]]--
  10. local nixio = require "nixio"
  11. local fs = require "nixio.fs"
  12. local util = require "gluon.util"
  13. local f_keys = Form(translate("SSH keys"), translate("You can provide your SSH keys here (one per line):"), 'keys')
  14. local s = f_keys:section(Section)
  15. local keys = s:option(TextValue, "keys")
  16. keys.wrap = "off"
  17. keys.rows = 5
  18. keys.default = fs.readfile("/etc/dropbear/authorized_keys") or ""
  19. function keys:write(value)
  20. value = util.trim(value:gsub("\r", ""))
  21. if value ~= "" then
  22. fs.writefile("/etc/dropbear/authorized_keys", value .. "\n")
  23. else
  24. fs.remove("/etc/dropbear/authorized_keys")
  25. end
  26. end
  27. local f_password = Form(translate("Password"),
  28. translate(
  29. "Alternatively, you can set a password to access you node. Please choose a secure password you don't use anywhere else.<br /><br />"
  30. .. "If you set an empty password, login via password will be disabled. This is the default."
  31. ), 'password'
  32. )
  33. f_password.reset = false
  34. local s = f_password:section(Section)
  35. local pw1 = s:option(Value, "pw1", translate("Password"))
  36. pw1.password = true
  37. function pw1.cfgvalue()
  38. return ''
  39. end
  40. local pw2 = s:option(Value, "pw2", translate("Confirmation"))
  41. pw2.password = true
  42. function pw2.cfgvalue()
  43. return ''
  44. end
  45. local function set_password(password)
  46. local inr, inw = nixio.pipe()
  47. local pid = nixio.fork()
  48. if pid < 0 then
  49. return false
  50. elseif pid == 0 then
  51. inw:close()
  52. local null = nixio.open('/dev/null', 'w')
  53. nixio.dup(null, nixio.stderr)
  54. nixio.dup(null, nixio.stdout)
  55. if null:fileno() > 2 then
  56. null:close()
  57. end
  58. nixio.dup(inr, nixio.stdin)
  59. inr:close()
  60. nixio.execp('passwd')
  61. os.exit(127)
  62. end
  63. inr:close()
  64. inw:write(string.format('%s\n%s\n', password, password))
  65. inw:close()
  66. local wpid, status, code = nixio.waitpid(pid)
  67. return wpid and status == 'exited' and code == 0
  68. end
  69. function f_password:write()
  70. if pw1.data ~= pw2.data then
  71. f_password.errmessage = translate("The password and the confirmation differ.")
  72. return
  73. end
  74. local pw = pw1.data
  75. if #pw > 0 then
  76. if set_password(pw) then
  77. f_password.message = translate("Password changed.")
  78. else
  79. f_password.errmessage = translate("Unable to change the password.")
  80. end
  81. else
  82. -- We don't check the return code here as the error 'password for root is already locked' is normal...
  83. os.execute('passwd -l root >/dev/null')
  84. f_password.message = translate("Password removed.")
  85. end
  86. end
  87. return f_keys, f_password