xref: /freebsd/stand/lua/password.lua (revision c851839897bd827c373c86aef1b6b9d0f1cc538a)
1088b4f5fSWarner Losh--
2088b4f5fSWarner Losh-- Copyright (c) 2015 Pedro Souza <pedrosouza@freebsd.org>
3088b4f5fSWarner Losh-- All rights reserved.
4088b4f5fSWarner Losh--
5088b4f5fSWarner Losh-- Redistribution and use in source and binary forms, with or without
6088b4f5fSWarner Losh-- modification, are permitted provided that the following conditions
7088b4f5fSWarner Losh-- are met:
8088b4f5fSWarner Losh-- 1. Redistributions of source code must retain the above copyright
9088b4f5fSWarner Losh--    notice, this list of conditions and the following disclaimer.
10088b4f5fSWarner Losh-- 2. Redistributions in binary form must reproduce the above copyright
11088b4f5fSWarner Losh--    notice, this list of conditions and the following disclaimer in the
12088b4f5fSWarner Losh--    documentation and/or other materials provided with the distribution.
13088b4f5fSWarner Losh--
14088b4f5fSWarner Losh-- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15088b4f5fSWarner Losh-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16088b4f5fSWarner Losh-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17088b4f5fSWarner Losh-- ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18088b4f5fSWarner Losh-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19088b4f5fSWarner Losh-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20088b4f5fSWarner Losh-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21088b4f5fSWarner Losh-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22088b4f5fSWarner Losh-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23088b4f5fSWarner Losh-- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24088b4f5fSWarner Losh-- SUCH DAMAGE.
25088b4f5fSWarner Losh--
26088b4f5fSWarner Losh-- $FreeBSD$
27088b4f5fSWarner Losh--
28088b4f5fSWarner Losh
29088b4f5fSWarner Loshlocal core = require("core");
30088b4f5fSWarner Loshlocal screen = require("screen");
31088b4f5fSWarner Losh
32*c8518398SKyle Evanslocal password = {};
33*c8518398SKyle Evans
34088b4f5fSWarner Loshfunction password.read()
35088b4f5fSWarner Losh	local str = "";
36088b4f5fSWarner Losh	local n = 0;
37088b4f5fSWarner Losh
38088b4f5fSWarner Losh	repeat
39088b4f5fSWarner Losh		ch = io.getchar();
4024a1bd54SKyle Evans		if (ch == core.KEY_ENTER) then
41088b4f5fSWarner Losh			break;
42088b4f5fSWarner Losh		end
4311cac431SKyle Evans		-- XXX TODO: Evaluate if we really want this or not, as a
4411cac431SKyle Evans		-- security consideration of sorts
451504bce3SKyle Evans		if (ch == core.KEY_BACKSPACE) or (ch == core.KEY_DELETE) then
4624a1bd54SKyle Evans			if (n > 0) then
47088b4f5fSWarner Losh				n = n - 1;
48088b4f5fSWarner Losh				-- loader.printc("\008 \008");
4924a1bd54SKyle Evans				str = str:sub(1, n);
50088b4f5fSWarner Losh			end
51088b4f5fSWarner Losh		else
52088b4f5fSWarner Losh			-- loader.printc("*");
53088b4f5fSWarner Losh			str = str .. string.char(ch);
54088b4f5fSWarner Losh			n = n + 1;
55088b4f5fSWarner Losh		end
5624a1bd54SKyle Evans	until (n == 16);
57088b4f5fSWarner Losh	return str;
58088b4f5fSWarner Loshend
59088b4f5fSWarner Losh
60088b4f5fSWarner Loshfunction password.check()
6140bbffdbSKyle Evans	screen.clear();
62088b4f5fSWarner Losh	screen.defcursor();
6311cac431SKyle Evans	-- pwd is optionally supplied if we want to check it
6411cac431SKyle Evans	local function do_prompt(prompt, pwd)
6524a1bd54SKyle Evans		while (true) do
66088b4f5fSWarner Losh			loader.printc(prompt);
6711cac431SKyle Evans			local read_pwd = password.read();
6811cac431SKyle Evans			if (not pwd) or (pwd == read_pwd) then
6924a1bd54SKyle Evans				-- Throw an extra newline after password prompt
7024a1bd54SKyle Evans				print("");
7111cac431SKyle Evans				return read_pwd;
72088b4f5fSWarner Losh			end
73088b4f5fSWarner Losh			print("\n\nloader: incorrect password!\n");
74088b4f5fSWarner Losh			loader.delay(3*1000*1000);
75088b4f5fSWarner Losh		end
7611cac431SKyle Evans	end
7711cac431SKyle Evans	local function compare(prompt, pwd)
7811cac431SKyle Evans		if (pwd == nil) then
7911cac431SKyle Evans			return;
8011cac431SKyle Evans		end
8111cac431SKyle Evans		do_prompt(prompt, pwd);
82088b4f5fSWarner Losh	end
83088b4f5fSWarner Losh
8432a5a33eSKyle Evans	local boot_pwd = loader.getenv("bootlock_password");
85088b4f5fSWarner Losh	compare("Boot password: ", boot_pwd);
86088b4f5fSWarner Losh
87c9594542SKyle Evans	local geli_prompt = loader.getenv("geom_eli_passphrase_prompt");
88c9594542SKyle Evans	if (geli_prompt ~= nil) and (geli_prompt:lower() == "yes") then
8911cac431SKyle Evans		local passphrase = do_prompt("GELI Passphrase: ");
9024a1bd54SKyle Evans		loader.setenv("kern.geom.eli.passphrase", passphrase);
9111cac431SKyle Evans	end
9211cac431SKyle Evans
93088b4f5fSWarner Losh	local pwd = loader.getenv("password");
94088b4f5fSWarner Losh	if (pwd ~= nil) then
95088b4f5fSWarner Losh		core.autoboot();
96088b4f5fSWarner Losh	end
97088b4f5fSWarner Losh	compare("Password: ", pwd);
98088b4f5fSWarner Loshend
99088b4f5fSWarner Losh
10024a1bd54SKyle Evansreturn password;
101