xref: /freebsd/stand/lua/password.lua (revision a6f1506f1a4f9dd9bfa65f01e535afcf60ada183)
1--
2-- SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3--
4-- Copyright (c) 2015 Pedro Souza <pedrosouza@freebsd.org>
5-- Copyright (C) 2018 Kyle Evans <kevans@FreeBSD.org>
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 = {"/", "-", "\\", "|"}
39local twiddle_pos = 1
40
41-- Module exports
42function password.read()
43	local str = ""
44	local n = 0
45
46	twiddle_pos = 1
47	local function draw_twiddle()
48		loader.printc("  " .. twiddle_chars[twiddle_pos])
49		screen.movecursor(-3, -1)
50		twiddle_pos = (twiddle_pos % #twiddle_chars) + 1
51	end
52
53	-- Space between the prompt and any on-screen feedback
54	loader.printc(" ")
55	while true do
56		local ch = io.getchar()
57		if ch == core.KEY_ENTER then
58			break
59		end
60		if ch == core.KEY_BACKSPACE or ch == core.KEY_DELETE then
61			if n > 0 then
62				n = n - 1
63				if show_password_mask then
64					loader.printc("\008 \008")
65				else
66					draw_twiddle()
67				end
68				str = str:sub(1, n)
69			end
70		else
71			if show_password_mask then
72				loader.printc("*")
73			else
74				draw_twiddle()
75			end
76			str = str .. string.char(ch)
77			n = n + 1
78		end
79	end
80	return str
81end
82
83function password.check()
84	screen.clear()
85	screen.defcursor()
86	-- pwd is optionally supplied if we want to check it
87	local function doPrompt(prompt, pwd)
88		while true do
89			loader.printc(prompt)
90			local read_pwd = password.read()
91			if pwd == nil or pwd == read_pwd then
92				-- Throw an extra newline after password prompt
93				print("")
94				return read_pwd
95			end
96			print("\n\nloader: incorrect password!\n")
97			loader.delay(3*1000*1000)
98		end
99	end
100	local function compare(prompt, pwd)
101		if pwd == nil then
102			return
103		end
104		doPrompt(prompt, pwd)
105	end
106
107	local boot_pwd = loader.getenv("bootlock_password")
108	compare("Boot password: ", boot_pwd)
109
110	local geli_prompt = loader.getenv("geom_eli_passphrase_prompt")
111	if geli_prompt ~= nil and geli_prompt:lower() == "yes" then
112		local passphrase = doPrompt("GELI Passphrase: ")
113		loader.setenv("kern.geom.eli.passphrase", passphrase)
114	end
115
116	local pwd = loader.getenv("password")
117	if pwd ~= nil then
118		core.autoboot()
119	end
120	compare("Password: ", pwd)
121end
122
123return password
124