xref: /freebsd/stand/lua/password.lua (revision aedd6be5c7c3096828fafa6c1528f3966b9e3aa5)
1088b4f5fSWarner Losh--
2088b4f5fSWarner Losh-- Copyright (c) 2015 Pedro Souza <pedrosouza@freebsd.org>
321d5bcbeSKyle Evans-- Copyright (C) 2018 Kyle Evans <kevans@FreeBSD.org>
4088b4f5fSWarner Losh-- All rights reserved.
5088b4f5fSWarner Losh--
6088b4f5fSWarner Losh-- Redistribution and use in source and binary forms, with or without
7088b4f5fSWarner Losh-- modification, are permitted provided that the following conditions
8088b4f5fSWarner Losh-- are met:
9088b4f5fSWarner Losh-- 1. Redistributions of source code must retain the above copyright
10088b4f5fSWarner Losh--    notice, this list of conditions and the following disclaimer.
11088b4f5fSWarner Losh-- 2. Redistributions in binary form must reproduce the above copyright
12088b4f5fSWarner Losh--    notice, this list of conditions and the following disclaimer in the
13088b4f5fSWarner Losh--    documentation and/or other materials provided with the distribution.
14088b4f5fSWarner Losh--
15088b4f5fSWarner Losh-- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16088b4f5fSWarner Losh-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17088b4f5fSWarner Losh-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18088b4f5fSWarner Losh-- ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19088b4f5fSWarner Losh-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20088b4f5fSWarner Losh-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21088b4f5fSWarner Losh-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22088b4f5fSWarner Losh-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23088b4f5fSWarner Losh-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24088b4f5fSWarner Losh-- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25088b4f5fSWarner Losh-- SUCH DAMAGE.
26088b4f5fSWarner Losh--
27088b4f5fSWarner Losh-- $FreeBSD$
28088b4f5fSWarner Losh--
29088b4f5fSWarner Losh
30*aedd6be5SKyle Evanslocal core = require("core")
31*aedd6be5SKyle Evanslocal screen = require("screen")
32088b4f5fSWarner Losh
33*aedd6be5SKyle Evanslocal password = {}
34c8518398SKyle Evans
35b5746545SKyle Evans-- Module exports
36088b4f5fSWarner Loshfunction password.read()
37*aedd6be5SKyle Evans	local str = ""
38*aedd6be5SKyle Evans	local n = 0
39088b4f5fSWarner Losh
40088b4f5fSWarner Losh	repeat
41*aedd6be5SKyle Evans		ch = io.getchar()
4224a1bd54SKyle Evans		if (ch == core.KEY_ENTER) then
43*aedd6be5SKyle Evans			break
44088b4f5fSWarner Losh		end
4511cac431SKyle Evans		-- XXX TODO: Evaluate if we really want this or not, as a
4611cac431SKyle Evans		-- security consideration of sorts
471504bce3SKyle Evans		if (ch == core.KEY_BACKSPACE) or (ch == core.KEY_DELETE) then
4824a1bd54SKyle Evans			if (n > 0) then
49*aedd6be5SKyle Evans				n = n - 1
50*aedd6be5SKyle Evans				-- loader.printc("\008 \008")
51*aedd6be5SKyle Evans				str = str:sub(1, n)
52088b4f5fSWarner Losh			end
53088b4f5fSWarner Losh		else
54*aedd6be5SKyle Evans			-- loader.printc("*")
55*aedd6be5SKyle Evans			str = str .. string.char(ch)
56*aedd6be5SKyle Evans			n = n + 1
57088b4f5fSWarner Losh		end
58*aedd6be5SKyle Evans	until (n == 16)
59*aedd6be5SKyle Evans	return str
60088b4f5fSWarner Loshend
61088b4f5fSWarner Losh
62088b4f5fSWarner Loshfunction password.check()
63*aedd6be5SKyle Evans	screen.clear()
64*aedd6be5SKyle Evans	screen.defcursor()
6511cac431SKyle Evans	-- pwd is optionally supplied if we want to check it
6611cac431SKyle Evans	local function do_prompt(prompt, pwd)
6724a1bd54SKyle Evans		while (true) do
68*aedd6be5SKyle Evans			loader.printc(prompt)
69*aedd6be5SKyle Evans			local read_pwd = password.read()
7011cac431SKyle Evans			if (not pwd) or (pwd == read_pwd) then
7124a1bd54SKyle Evans				-- Throw an extra newline after password prompt
72*aedd6be5SKyle Evans				print("")
73*aedd6be5SKyle Evans				return read_pwd
74088b4f5fSWarner Losh			end
75*aedd6be5SKyle Evans			print("\n\nloader: incorrect password!\n")
76*aedd6be5SKyle Evans			loader.delay(3*1000*1000)
77088b4f5fSWarner Losh		end
7811cac431SKyle Evans	end
7911cac431SKyle Evans	local function compare(prompt, pwd)
8011cac431SKyle Evans		if (pwd == nil) then
81*aedd6be5SKyle Evans			return
8211cac431SKyle Evans		end
83*aedd6be5SKyle Evans		do_prompt(prompt, pwd)
84088b4f5fSWarner Losh	end
85088b4f5fSWarner Losh
86*aedd6be5SKyle Evans	local boot_pwd = loader.getenv("bootlock_password")
87*aedd6be5SKyle Evans	compare("Boot password: ", boot_pwd)
88088b4f5fSWarner Losh
89*aedd6be5SKyle Evans	local geli_prompt = loader.getenv("geom_eli_passphrase_prompt")
90c9594542SKyle Evans	if (geli_prompt ~= nil) and (geli_prompt:lower() == "yes") then
91*aedd6be5SKyle Evans		local passphrase = do_prompt("GELI Passphrase: ")
92*aedd6be5SKyle Evans		loader.setenv("kern.geom.eli.passphrase", passphrase)
9311cac431SKyle Evans	end
9411cac431SKyle Evans
95*aedd6be5SKyle Evans	local pwd = loader.getenv("password")
96088b4f5fSWarner Losh	if (pwd ~= nil) then
97*aedd6be5SKyle Evans		core.autoboot()
98088b4f5fSWarner Losh	end
99*aedd6be5SKyle Evans	compare("Password: ", pwd)
100088b4f5fSWarner Loshend
101088b4f5fSWarner Losh
102*aedd6be5SKyle Evansreturn password
103