xref: /freebsd/stand/lua/password.lua (revision 088b4f5f323e73e5eb54b0e409b064ff5d46d221)
1*088b4f5fSWarner Losh--
2*088b4f5fSWarner Losh-- Copyright (c) 2015 Pedro Souza <pedrosouza@freebsd.org>
3*088b4f5fSWarner Losh-- All rights reserved.
4*088b4f5fSWarner Losh--
5*088b4f5fSWarner Losh-- Redistribution and use in source and binary forms, with or without
6*088b4f5fSWarner Losh-- modification, are permitted provided that the following conditions
7*088b4f5fSWarner Losh-- are met:
8*088b4f5fSWarner Losh-- 1. Redistributions of source code must retain the above copyright
9*088b4f5fSWarner Losh--    notice, this list of conditions and the following disclaimer.
10*088b4f5fSWarner Losh-- 2. Redistributions in binary form must reproduce the above copyright
11*088b4f5fSWarner Losh--    notice, this list of conditions and the following disclaimer in the
12*088b4f5fSWarner Losh--    documentation and/or other materials provided with the distribution.
13*088b4f5fSWarner Losh--
14*088b4f5fSWarner Losh-- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*088b4f5fSWarner Losh-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*088b4f5fSWarner Losh-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*088b4f5fSWarner Losh-- ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*088b4f5fSWarner Losh-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*088b4f5fSWarner Losh-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*088b4f5fSWarner Losh-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*088b4f5fSWarner Losh-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*088b4f5fSWarner Losh-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*088b4f5fSWarner Losh-- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*088b4f5fSWarner Losh-- SUCH DAMAGE.
25*088b4f5fSWarner Losh--
26*088b4f5fSWarner Losh-- $FreeBSD$
27*088b4f5fSWarner Losh--
28*088b4f5fSWarner Losh
29*088b4f5fSWarner Loshlocal password = {};
30*088b4f5fSWarner Losh
31*088b4f5fSWarner Loshlocal core = require("core");
32*088b4f5fSWarner Loshlocal screen = require("screen");
33*088b4f5fSWarner Losh
34*088b4f5fSWarner Loshfunction password.read()
35*088b4f5fSWarner Losh	local str = "";
36*088b4f5fSWarner Losh	local n = 0;
37*088b4f5fSWarner Losh
38*088b4f5fSWarner Losh	repeat
39*088b4f5fSWarner Losh		ch = io.getchar();
40*088b4f5fSWarner Losh		if ch == 13 then
41*088b4f5fSWarner Losh			break;
42*088b4f5fSWarner Losh		end
43*088b4f5fSWarner Losh
44*088b4f5fSWarner Losh		if ch == 8 then
45*088b4f5fSWarner Losh			if n > 0 then
46*088b4f5fSWarner Losh				n = n - 1;
47*088b4f5fSWarner Losh				-- loader.printc("\008 \008");
48*088b4f5fSWarner Losh				str = string.sub(str, 1, n);
49*088b4f5fSWarner Losh			end
50*088b4f5fSWarner Losh		else
51*088b4f5fSWarner Losh			-- loader.printc("*");
52*088b4f5fSWarner Losh			str = str .. string.char(ch);
53*088b4f5fSWarner Losh			n = n + 1;
54*088b4f5fSWarner Losh		end
55*088b4f5fSWarner Losh	until n == 16
56*088b4f5fSWarner Losh	return str;
57*088b4f5fSWarner Loshend
58*088b4f5fSWarner Losh
59*088b4f5fSWarner Loshfunction password.check()
60*088b4f5fSWarner Losh	screen.defcursor();
61*088b4f5fSWarner Losh	local function compare(prompt, pwd)
62*088b4f5fSWarner Losh		if (pwd == nil) then
63*088b4f5fSWarner Losh			return;
64*088b4f5fSWarner Losh		end
65*088b4f5fSWarner Losh		while true do
66*088b4f5fSWarner Losh			loader.printc(prompt);
67*088b4f5fSWarner Losh			if (pwd == password.read()) then
68*088b4f5fSWarner Losh				break;
69*088b4f5fSWarner Losh			end
70*088b4f5fSWarner Losh			print("\n\nloader: incorrect password!\n");
71*088b4f5fSWarner Losh			loader.delay(3*1000*1000);
72*088b4f5fSWarner Losh		end
73*088b4f5fSWarner Losh	end
74*088b4f5fSWarner Losh
75*088b4f5fSWarner Losh	local boot_pwd = loader.getenv("bootlock_password");
76*088b4f5fSWarner Losh	compare("Boot password: ", boot_pwd);
77*088b4f5fSWarner Losh
78*088b4f5fSWarner Losh	local pwd = loader.getenv("password");
79*088b4f5fSWarner Losh	if (pwd ~=nil) then
80*088b4f5fSWarner Losh		core.autoboot();
81*088b4f5fSWarner Losh	end
82*088b4f5fSWarner Losh	compare("Password: ", pwd);
83*088b4f5fSWarner Loshend
84*088b4f5fSWarner Losh
85*088b4f5fSWarner Loshreturn password
86