xref: /freebsd-13.1/stand/lua/password.lua (revision 3049d4cc)
1--
2-- SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3--
4-- Copyright (c) 2015 Pedro Souza <[email protected]>
5-- Copyright (C) 2018 Kyle Evans <[email protected]>
6-- All rights reserved.
7--
8-- Redistribution and use in source and binary forms, with or without
9-- modification, are permitted provided that the following conditions
10-- are met:
11-- 1. Redistributions of source code must retain the above copyright
12--    notice, this list of conditions and the following disclaimer.
13-- 2. Redistributions in binary form must reproduce the above copyright
14--    notice, this list of conditions and the following disclaimer in the
15--    documentation and/or other materials provided with the distribution.
16--
17-- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20-- ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26-- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27-- SUCH DAMAGE.
28--
29-- $FreeBSD$
30--
31
32local core = require("core")
33local screen = require("screen")
34
35local password = {}
36-- Asterisks as a password mask
37local show_password_mask = false
38local twiddle_chars = {"/", "-", "\\", "|"}
39
40-- Module exports
41function password.read()
42	local str = ""
43	local n = 0
44	local twiddle_pos = 1
45
46	local function draw_twiddle()
47		loader.printc("  " .. twiddle_chars[twiddle_pos])
48		screen.movecursor(-3, 0)
49		twiddle_pos = (twiddle_pos % #twiddle_chars) + 1
50	end
51
52	-- Space between the prompt and any on-screen feedback
53	loader.printc(" ")
54	while true do
55		local ch = io.getchar()
56		if ch == core.KEY_ENTER then
57			break
58		end
59		if ch == core.KEY_BACKSPACE or ch == core.KEY_DELETE then
60			if n > 0 then
61				n = n - 1
62				if show_password_mask then
63					loader.printc("\008 \008")
64				else
65					draw_twiddle()
66				end
67				str = str:sub(1, n)
68			end
69		else
70			if show_password_mask then
71				loader.printc("*")
72			else
73				draw_twiddle()
74			end
75			str = str .. string.char(ch)
76			n = n + 1
77		end
78	end
79	return str
80end
81
82function password.check()
83	screen.clear()
84	screen.defcursor()
85	-- pwd is optionally supplied if we want to check it
86	local function doPrompt(prompt, pwd)
87		while true do
88			loader.printc(prompt)
89			local read_pwd = password.read()
90			if pwd == nil or pwd == read_pwd then
91				-- Throw an extra newline after password prompt
92				print("")
93				return read_pwd
94			end
95			print("\n\nloader: incorrect password!\n")
96			loader.delay(3*1000*1000)
97		end
98	end
99	local function compare(prompt, pwd)
100		if pwd == nil then
101			return
102		end
103		doPrompt(prompt, pwd)
104	end
105
106	local boot_pwd = loader.getenv("bootlock_password")
107	compare("Boot password: ", boot_pwd)
108
109	local geli_prompt = loader.getenv("geom_eli_passphrase_prompt")
110	if geli_prompt ~= nil and geli_prompt:lower() == "yes" then
111		local passphrase = doPrompt("GELI Passphrase: ")
112		loader.setenv("kern.geom.eli.passphrase", passphrase)
113	end
114
115	local pwd = loader.getenv("password")
116	if pwd ~= nil then
117		core.autoboot()
118	end
119	compare("Password: ", pwd)
120end
121
122return password
123