xref: /freebsd/stand/lua/password.lua (revision b57465454b35f6e24a6df02a93a27d0136c016d9)
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
30088b4f5fSWarner Loshlocal core = require("core");
31088b4f5fSWarner Loshlocal screen = require("screen");
32088b4f5fSWarner Losh
33c8518398SKyle Evanslocal password = {};
34c8518398SKyle Evans
35*b5746545SKyle Evans-- Module exports
36088b4f5fSWarner Loshfunction password.read()
37088b4f5fSWarner Losh	local str = "";
38088b4f5fSWarner Losh	local n = 0;
39088b4f5fSWarner Losh
40088b4f5fSWarner Losh	repeat
41088b4f5fSWarner Losh		ch = io.getchar();
4224a1bd54SKyle Evans		if (ch == core.KEY_ENTER) then
43088b4f5fSWarner Losh			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
49088b4f5fSWarner Losh				n = n - 1;
50088b4f5fSWarner Losh				-- loader.printc("\008 \008");
5124a1bd54SKyle Evans				str = str:sub(1, n);
52088b4f5fSWarner Losh			end
53088b4f5fSWarner Losh		else
54088b4f5fSWarner Losh			-- loader.printc("*");
55088b4f5fSWarner Losh			str = str .. string.char(ch);
56088b4f5fSWarner Losh			n = n + 1;
57088b4f5fSWarner Losh		end
5824a1bd54SKyle Evans	until (n == 16);
59088b4f5fSWarner Losh	return str;
60088b4f5fSWarner Loshend
61088b4f5fSWarner Losh
62088b4f5fSWarner Loshfunction password.check()
6340bbffdbSKyle Evans	screen.clear();
64088b4f5fSWarner Losh	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
68088b4f5fSWarner Losh			loader.printc(prompt);
6911cac431SKyle 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
7224a1bd54SKyle Evans				print("");
7311cac431SKyle Evans				return read_pwd;
74088b4f5fSWarner Losh			end
75088b4f5fSWarner Losh			print("\n\nloader: incorrect password!\n");
76088b4f5fSWarner Losh			loader.delay(3*1000*1000);
77088b4f5fSWarner Losh		end
7811cac431SKyle Evans	end
7911cac431SKyle Evans	local function compare(prompt, pwd)
8011cac431SKyle Evans		if (pwd == nil) then
8111cac431SKyle Evans			return;
8211cac431SKyle Evans		end
8311cac431SKyle Evans		do_prompt(prompt, pwd);
84088b4f5fSWarner Losh	end
85088b4f5fSWarner Losh
8632a5a33eSKyle Evans	local boot_pwd = loader.getenv("bootlock_password");
87088b4f5fSWarner Losh	compare("Boot password: ", boot_pwd);
88088b4f5fSWarner Losh
89c9594542SKyle Evans	local geli_prompt = loader.getenv("geom_eli_passphrase_prompt");
90c9594542SKyle Evans	if (geli_prompt ~= nil) and (geli_prompt:lower() == "yes") then
9111cac431SKyle Evans		local passphrase = do_prompt("GELI Passphrase: ");
9224a1bd54SKyle Evans		loader.setenv("kern.geom.eli.passphrase", passphrase);
9311cac431SKyle Evans	end
9411cac431SKyle Evans
95088b4f5fSWarner Losh	local pwd = loader.getenv("password");
96088b4f5fSWarner Losh	if (pwd ~= nil) then
97088b4f5fSWarner Losh		core.autoboot();
98088b4f5fSWarner Losh	end
99088b4f5fSWarner Losh	compare("Password: ", pwd);
100088b4f5fSWarner Loshend
101088b4f5fSWarner Losh
10224a1bd54SKyle Evansreturn password;
103