xref: /freebsd/stand/lua/password.lua (revision 322a2dddba49d04539cc130cd2264a00db45c20d)
1088b4f5fSWarner Losh--
272e39d71SKyle Evans-- SPDX-License-Identifier: BSD-2-Clause-FreeBSD
372e39d71SKyle Evans--
4088b4f5fSWarner Losh-- Copyright (c) 2015 Pedro Souza <pedrosouza@freebsd.org>
521d5bcbeSKyle Evans-- Copyright (C) 2018 Kyle Evans <kevans@FreeBSD.org>
6088b4f5fSWarner Losh-- All rights reserved.
7088b4f5fSWarner Losh--
8088b4f5fSWarner Losh-- Redistribution and use in source and binary forms, with or without
9088b4f5fSWarner Losh-- modification, are permitted provided that the following conditions
10088b4f5fSWarner Losh-- are met:
11088b4f5fSWarner Losh-- 1. Redistributions of source code must retain the above copyright
12088b4f5fSWarner Losh--    notice, this list of conditions and the following disclaimer.
13088b4f5fSWarner Losh-- 2. Redistributions in binary form must reproduce the above copyright
14088b4f5fSWarner Losh--    notice, this list of conditions and the following disclaimer in the
15088b4f5fSWarner Losh--    documentation and/or other materials provided with the distribution.
16088b4f5fSWarner Losh--
17088b4f5fSWarner Losh-- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18088b4f5fSWarner Losh-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19088b4f5fSWarner Losh-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20088b4f5fSWarner Losh-- ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21088b4f5fSWarner Losh-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22088b4f5fSWarner Losh-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23088b4f5fSWarner Losh-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24088b4f5fSWarner Losh-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25088b4f5fSWarner Losh-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26088b4f5fSWarner Losh-- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27088b4f5fSWarner Losh-- SUCH DAMAGE.
28088b4f5fSWarner Losh--
29088b4f5fSWarner Losh-- $FreeBSD$
30088b4f5fSWarner Losh--
31088b4f5fSWarner Losh
32aedd6be5SKyle Evanslocal core = require("core")
33aedd6be5SKyle Evanslocal screen = require("screen")
34088b4f5fSWarner Losh
35aedd6be5SKyle Evanslocal password = {}
36c8518398SKyle Evans
37b5746545SKyle Evans-- Module exports
38088b4f5fSWarner Loshfunction password.read()
39aedd6be5SKyle Evans	local str = ""
40aedd6be5SKyle Evans	local n = 0
41088b4f5fSWarner Losh
42a5e2e5c7SKyle Evans	while true do
43e2df27e3SKyle Evans		local ch = io.getchar()
449f71d421SKyle Evans		if ch == core.KEY_ENTER then
45aedd6be5SKyle Evans			break
46088b4f5fSWarner Losh		end
4711cac431SKyle Evans		-- XXX TODO: Evaluate if we really want this or not, as a
4811cac431SKyle Evans		-- security consideration of sorts
499f71d421SKyle Evans		if ch == core.KEY_BACKSPACE or ch == core.KEY_DELETE then
509f71d421SKyle Evans			if n > 0 then
51aedd6be5SKyle Evans				n = n - 1
52aedd6be5SKyle Evans				-- loader.printc("\008 \008")
53aedd6be5SKyle Evans				str = str:sub(1, n)
54088b4f5fSWarner Losh			end
55088b4f5fSWarner Losh		else
56aedd6be5SKyle Evans			-- loader.printc("*")
57aedd6be5SKyle Evans			str = str .. string.char(ch)
58aedd6be5SKyle Evans			n = n + 1
59088b4f5fSWarner Losh		end
60a5e2e5c7SKyle Evans	end
61aedd6be5SKyle Evans	return str
62088b4f5fSWarner Loshend
63088b4f5fSWarner Losh
64088b4f5fSWarner Loshfunction password.check()
65aedd6be5SKyle Evans	screen.clear()
66aedd6be5SKyle Evans	screen.defcursor()
6711cac431SKyle Evans	-- pwd is optionally supplied if we want to check it
68*322a2dddSKyle Evans	local function doPrompt(prompt, pwd)
699f71d421SKyle Evans		while true do
70aedd6be5SKyle Evans			loader.printc(prompt)
71aedd6be5SKyle Evans			local read_pwd = password.read()
729f71d421SKyle Evans			if pwd == nil or pwd == read_pwd then
7324a1bd54SKyle Evans				-- Throw an extra newline after password prompt
74aedd6be5SKyle Evans				print("")
75aedd6be5SKyle Evans				return read_pwd
76088b4f5fSWarner Losh			end
77aedd6be5SKyle Evans			print("\n\nloader: incorrect password!\n")
78aedd6be5SKyle Evans			loader.delay(3*1000*1000)
79088b4f5fSWarner Losh		end
8011cac431SKyle Evans	end
8111cac431SKyle Evans	local function compare(prompt, pwd)
829f71d421SKyle Evans		if pwd == nil then
83aedd6be5SKyle Evans			return
8411cac431SKyle Evans		end
85*322a2dddSKyle Evans		doPrompt(prompt, pwd)
86088b4f5fSWarner Losh	end
87088b4f5fSWarner Losh
88aedd6be5SKyle Evans	local boot_pwd = loader.getenv("bootlock_password")
89aedd6be5SKyle Evans	compare("Boot password: ", boot_pwd)
90088b4f5fSWarner Losh
91aedd6be5SKyle Evans	local geli_prompt = loader.getenv("geom_eli_passphrase_prompt")
929f71d421SKyle Evans	if geli_prompt ~= nil and geli_prompt:lower() == "yes" then
93*322a2dddSKyle Evans		local passphrase = doPrompt("GELI Passphrase: ")
94aedd6be5SKyle Evans		loader.setenv("kern.geom.eli.passphrase", passphrase)
9511cac431SKyle Evans	end
9611cac431SKyle Evans
97aedd6be5SKyle Evans	local pwd = loader.getenv("password")
989f71d421SKyle Evans	if pwd ~= nil then
99aedd6be5SKyle Evans		core.autoboot()
100088b4f5fSWarner Losh	end
101aedd6be5SKyle Evans	compare("Password: ", pwd)
102088b4f5fSWarner Loshend
103088b4f5fSWarner Losh
104aedd6be5SKyle Evansreturn password
105