password.lua revision 329167
1171568Sscottl--
2330449Seadler-- Copyright (c) 2015 Pedro Souza <pedrosouza@freebsd.org>
3330449Seadler-- All rights reserved.
4211095Sdes--
5171568Sscottl-- Redistribution and use in source and binary forms, with or without
6171568Sscottl-- modification, are permitted provided that the following conditions
7171568Sscottl-- are met:
8171568Sscottl-- 1. Redistributions of source code must retain the above copyright
9171568Sscottl--    notice, this list of conditions and the following disclaimer.
10171568Sscottl-- 2. Redistributions in binary form must reproduce the above copyright
11171568Sscottl--    notice, this list of conditions and the following disclaimer in the
12171568Sscottl--    documentation and/or other materials provided with the distribution.
13171568Sscottl--
14171568Sscottl-- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15171568Sscottl-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16171568Sscottl-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17171568Sscottl-- ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18171568Sscottl-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19171568Sscottl-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20171568Sscottl-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21171568Sscottl-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22171568Sscottl-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23171568Sscottl-- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24171568Sscottl-- SUCH DAMAGE.
25171568Sscottl--
26171568Sscottl-- $FreeBSD: head/stand/lua/password.lua 329167 2018-02-12 15:32:00Z imp $
27171568Sscottl--
28171568Sscottl
29171568Sscottllocal password = {};
30171568Sscottl
31171568Sscottllocal core = require("core");
32171568Sscottllocal screen = require("screen");
33171568Sscottl
34171568Sscottlfunction password.read()
35171568Sscottl	local str = "";
36171568Sscottl	local n = 0;
37171568Sscottl
38171568Sscottl	repeat
39171568Sscottl		ch = io.getchar();
40171568Sscottl		if ch == 13 then
41171568Sscottl			break;
42171568Sscottl		end
43171568Sscottl
44171568Sscottl		if ch == 8 then
45171568Sscottl			if n > 0 then
46171568Sscottl				n = n - 1;
47171568Sscottl				-- loader.printc("\008 \008");
48171568Sscottl				str = string.sub(str, 1, n);
49171568Sscottl			end
50185289Sscottl		else
51185289Sscottl			-- loader.printc("*");
52185289Sscottl			str = str .. string.char(ch);
53185289Sscottl			n = n + 1;
54185289Sscottl		end
55185289Sscottl	until n == 16
56171568Sscottl	return str;
57171568Sscottlend
58171568Sscottl
59171568Sscottlfunction password.check()
60171568Sscottl	screen.defcursor();
61171568Sscottl	local function compare(prompt, pwd)
62171568Sscottl		if (pwd == nil) then
63171568Sscottl			return;
64171568Sscottl		end
65171568Sscottl		while true do
66171568Sscottl			loader.printc(prompt);
67171568Sscottl			if (pwd == password.read()) then
68171568Sscottl				break;
69171568Sscottl			end
70171568Sscottl			print("\n\nloader: incorrect password!\n");
71171568Sscottl			loader.delay(3*1000*1000);
72185289Sscottl		end
73171568Sscottl	end
74171568Sscottl
75171568Sscottl	local boot_pwd = loader.getenv("bootlock_password");
76171568Sscottl	compare("Boot password: ", boot_pwd);
77171568Sscottl
78171568Sscottl	local pwd = loader.getenv("password");
79171568Sscottl	if (pwd ~=nil) then
80171568Sscottl		core.autoboot();
81171568Sscottl	end
82171568Sscottl	compare("Password: ", pwd);
83171568Sscottlend
84171568Sscottl
85171568Sscottlreturn password
86171568Sscottl