remote.lua 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 site = require "gluon.site"
  14. local f_keys = Form(translate("SSH keys"), translate("You can provide your SSH keys here (one per line):"), 'keys')
  15. local s = f_keys:section(Section)
  16. local keys = s:option(TextValue, "keys")
  17. keys.wrap = "off"
  18. keys.rows = 5
  19. keys.default = fs.readfile("/etc/dropbear/authorized_keys") or ""
  20. function keys:write(value)
  21. value = util.trim(value:gsub("\r", ""))
  22. if value ~= "" then
  23. fs.writefile("/etc/dropbear/authorized_keys", value .. "\n")
  24. else
  25. fs.remove("/etc/dropbear/authorized_keys")
  26. end
  27. end
  28. local config = site.config_mode.remote_login
  29. if not config.show_password_form(false) then
  30. -- password login is disabled in site.conf
  31. return f_keys
  32. end
  33. local min_password_length = config.min_password_length(12)
  34. local mintype = 'minlength(' .. min_password_length .. ')'
  35. local length_hint
  36. if min_password_length > 1 then
  37. length_hint = translatef("%u characters min.", min_password_length)
  38. end
  39. local f_password = Form(translate("Password"), translate(
  40. "Alternatively, you can set a password to access your node. Please choose a "
  41. .. "secure password you don't use anywhere else.<br /><br />If you set an empty "
  42. .. "password, login via password will be disabled. This is the default."
  43. ), 'password'
  44. )
  45. f_password.reset = false
  46. local s = f_password:section(Section)
  47. local pw1 = s:option(Value, "pw1", translate("Password"))
  48. pw1.password = true
  49. pw1.optional = true
  50. pw1.datatype = mintype
  51. function pw1.cfgvalue()
  52. return ''
  53. end
  54. local pw2 = s:option(Value, "pw2", translate("Confirmation"), length_hint)
  55. pw2.password = true
  56. pw2.optional = true
  57. pw2.datatype = mintype
  58. function pw2.cfgvalue()
  59. return ''
  60. end
  61. local function set_password(password)
  62. local inr, inw = nixio.pipe()
  63. local pid = nixio.fork()
  64. if pid < 0 then
  65. return false
  66. elseif pid == 0 then
  67. inw:close()
  68. local null = nixio.open('/dev/null', 'w')
  69. nixio.dup(null, nixio.stderr)
  70. nixio.dup(null, nixio.stdout)
  71. if null:fileno() > 2 then
  72. null:close()
  73. end
  74. nixio.dup(inr, nixio.stdin)
  75. inr:close()
  76. nixio.execp('passwd')
  77. os.exit(127)
  78. end
  79. inr:close()
  80. inw:write(string.format('%s\n%s\n', password, password))
  81. inw:close()
  82. local wpid, status, code = nixio.waitpid(pid)
  83. return wpid and status == 'exited' and code == 0
  84. end
  85. function f_password:write()
  86. if pw1.data ~= pw2.data then
  87. f_password.errmessage = translate("The password and the confirmation differ.")
  88. return
  89. end
  90. local pw = pw1.data
  91. if pw ~= nil and #pw > 0 then
  92. if set_password(pw) then
  93. f_password.message = translate("Password changed.")
  94. else
  95. f_password.errmessage = translate("Unable to change the password.")
  96. end
  97. else
  98. -- We don't check the return code here as the error 'password for root is already locked' is normal...
  99. os.execute('passwd -l root >/dev/null')
  100. f_password.message = translate("Password removed.")
  101. end
  102. end
  103. return f_keys, f_password