xref: /freebsd/stand/lua/password.lua (revision 9f71d421c89dde4e5a642f4555bcd20558fd91b0)
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
30aedd6be5SKyle Evanslocal core = require("core")
31aedd6be5SKyle Evanslocal screen = require("screen")
32088b4f5fSWarner Losh
33aedd6be5SKyle Evanslocal password = {}
34c8518398SKyle Evans
35b5746545SKyle Evans-- Module exports
36088b4f5fSWarner Loshfunction password.read()
37aedd6be5SKyle Evans	local str = ""
38aedd6be5SKyle Evans	local n = 0
39088b4f5fSWarner Losh
40088b4f5fSWarner Losh	repeat
41aedd6be5SKyle Evans		ch = io.getchar()
42*9f71d421SKyle Evans		if ch == core.KEY_ENTER then
43aedd6be5SKyle 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
47*9f71d421SKyle Evans		if ch == core.KEY_BACKSPACE or ch == core.KEY_DELETE then
48*9f71d421SKyle Evans			if n > 0 then
49aedd6be5SKyle Evans				n = n - 1
50aedd6be5SKyle Evans				-- loader.printc("\008 \008")
51aedd6be5SKyle Evans				str = str:sub(1, n)
52088b4f5fSWarner Losh			end
53088b4f5fSWarner Losh		else
54aedd6be5SKyle Evans			-- loader.printc("*")
55aedd6be5SKyle Evans			str = str .. string.char(ch)
56aedd6be5SKyle Evans			n = n + 1
57088b4f5fSWarner Losh		end
58*9f71d421SKyle Evans	until n == 16
59aedd6be5SKyle Evans	return str
60088b4f5fSWarner Loshend
61088b4f5fSWarner Losh
62088b4f5fSWarner Loshfunction password.check()
63aedd6be5SKyle Evans	screen.clear()
64aedd6be5SKyle Evans	screen.defcursor()
6511cac431SKyle Evans	-- pwd is optionally supplied if we want to check it
6611cac431SKyle Evans	local function do_prompt(prompt, pwd)
67*9f71d421SKyle Evans		while true do
68aedd6be5SKyle Evans			loader.printc(prompt)
69aedd6be5SKyle Evans			local read_pwd = password.read()
70*9f71d421SKyle Evans			if pwd == nil or pwd == read_pwd then
7124a1bd54SKyle Evans				-- Throw an extra newline after password prompt
72aedd6be5SKyle Evans				print("")
73aedd6be5SKyle Evans				return read_pwd
74088b4f5fSWarner Losh			end
75aedd6be5SKyle Evans			print("\n\nloader: incorrect password!\n")
76aedd6be5SKyle Evans			loader.delay(3*1000*1000)
77088b4f5fSWarner Losh		end
7811cac431SKyle Evans	end
7911cac431SKyle Evans	local function compare(prompt, pwd)
80*9f71d421SKyle Evans		if pwd == nil then
81aedd6be5SKyle Evans			return
8211cac431SKyle Evans		end
83aedd6be5SKyle Evans		do_prompt(prompt, pwd)
84088b4f5fSWarner Losh	end
85088b4f5fSWarner Losh
86aedd6be5SKyle Evans	local boot_pwd = loader.getenv("bootlock_password")
87aedd6be5SKyle Evans	compare("Boot password: ", boot_pwd)
88088b4f5fSWarner Losh
89aedd6be5SKyle Evans	local geli_prompt = loader.getenv("geom_eli_passphrase_prompt")
90*9f71d421SKyle Evans	if geli_prompt ~= nil and geli_prompt:lower() == "yes" then
91aedd6be5SKyle Evans		local passphrase = do_prompt("GELI Passphrase: ")
92aedd6be5SKyle Evans		loader.setenv("kern.geom.eli.passphrase", passphrase)
9311cac431SKyle Evans	end
9411cac431SKyle Evans
95aedd6be5SKyle Evans	local pwd = loader.getenv("password")
96*9f71d421SKyle Evans	if pwd ~= nil then
97aedd6be5SKyle Evans		core.autoboot()
98088b4f5fSWarner Losh	end
99aedd6be5SKyle Evans	compare("Password: ", pwd)
100088b4f5fSWarner Loshend
101088b4f5fSWarner Losh
102aedd6be5SKyle Evansreturn password
103