xref: /freebsd/stand/lua/password.lua (revision a6f1506f1a4f9dd9bfa65f01e535afcf60ada183)
1088b4f5fSWarner Losh--
272e39d71SKyle Evans-- SPDX-License-Identifier: BSD-2-Clause-FreeBSD
372e39d71SKyle Evans--
4088b4f5fSWarner Losh-- Copyright (c) 2015 Pedro Souza <pedrosouza@freebsd.org>
521d5bcbeSKyle Evans-- Copyright (C) 2018 Kyle Evans <kevans@FreeBSD.org>
6088b4f5fSWarner Losh-- All rights reserved.
7088b4f5fSWarner Losh--
8088b4f5fSWarner Losh-- Redistribution and use in source and binary forms, with or without
9088b4f5fSWarner Losh-- modification, are permitted provided that the following conditions
10088b4f5fSWarner Losh-- are met:
11088b4f5fSWarner Losh-- 1. Redistributions of source code must retain the above copyright
12088b4f5fSWarner Losh--    notice, this list of conditions and the following disclaimer.
13088b4f5fSWarner Losh-- 2. Redistributions in binary form must reproduce the above copyright
14088b4f5fSWarner Losh--    notice, this list of conditions and the following disclaimer in the
15088b4f5fSWarner Losh--    documentation and/or other materials provided with the distribution.
16088b4f5fSWarner Losh--
17088b4f5fSWarner Losh-- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18088b4f5fSWarner Losh-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19088b4f5fSWarner Losh-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20088b4f5fSWarner Losh-- ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21088b4f5fSWarner Losh-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22088b4f5fSWarner Losh-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23088b4f5fSWarner Losh-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24088b4f5fSWarner Losh-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25088b4f5fSWarner Losh-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26088b4f5fSWarner Losh-- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27088b4f5fSWarner Losh-- SUCH DAMAGE.
28088b4f5fSWarner Losh--
29088b4f5fSWarner Losh-- $FreeBSD$
30088b4f5fSWarner Losh--
31088b4f5fSWarner Losh
32aedd6be5SKyle Evanslocal core = require("core")
33aedd6be5SKyle Evanslocal screen = require("screen")
34088b4f5fSWarner Losh
35aedd6be5SKyle Evanslocal password = {}
36*a6f1506fSKyle Evans-- Asterisks as a password mask
37*a6f1506fSKyle Evanslocal show_password_mask = false
38*a6f1506fSKyle Evanslocal twiddle_chars = {"/", "-", "\\", "|"}
39*a6f1506fSKyle Evanslocal twiddle_pos = 1
40c8518398SKyle Evans
41b5746545SKyle Evans-- Module exports
42088b4f5fSWarner Loshfunction password.read()
43aedd6be5SKyle Evans	local str = ""
44aedd6be5SKyle Evans	local n = 0
45088b4f5fSWarner Losh
46*a6f1506fSKyle Evans	twiddle_pos = 1
47*a6f1506fSKyle Evans	local function draw_twiddle()
48*a6f1506fSKyle Evans		loader.printc("  " .. twiddle_chars[twiddle_pos])
49*a6f1506fSKyle Evans		screen.movecursor(-3, -1)
50*a6f1506fSKyle Evans		twiddle_pos = (twiddle_pos % #twiddle_chars) + 1
51*a6f1506fSKyle Evans	end
52*a6f1506fSKyle Evans
53*a6f1506fSKyle Evans	-- Space between the prompt and any on-screen feedback
54*a6f1506fSKyle Evans	loader.printc(" ")
55a5e2e5c7SKyle Evans	while true do
56e2df27e3SKyle Evans		local ch = io.getchar()
579f71d421SKyle Evans		if ch == core.KEY_ENTER then
58aedd6be5SKyle Evans			break
59088b4f5fSWarner Losh		end
609f71d421SKyle Evans		if ch == core.KEY_BACKSPACE or ch == core.KEY_DELETE then
619f71d421SKyle Evans			if n > 0 then
62aedd6be5SKyle Evans				n = n - 1
63*a6f1506fSKyle Evans				if show_password_mask then
64*a6f1506fSKyle Evans					loader.printc("\008 \008")
65*a6f1506fSKyle Evans				else
66*a6f1506fSKyle Evans					draw_twiddle()
67*a6f1506fSKyle Evans				end
68aedd6be5SKyle Evans				str = str:sub(1, n)
69088b4f5fSWarner Losh			end
70088b4f5fSWarner Losh		else
71*a6f1506fSKyle Evans			if show_password_mask then
72*a6f1506fSKyle Evans				loader.printc("*")
73*a6f1506fSKyle Evans			else
74*a6f1506fSKyle Evans				draw_twiddle()
75*a6f1506fSKyle Evans			end
76aedd6be5SKyle Evans			str = str .. string.char(ch)
77aedd6be5SKyle Evans			n = n + 1
78088b4f5fSWarner Losh		end
79a5e2e5c7SKyle Evans	end
80aedd6be5SKyle Evans	return str
81088b4f5fSWarner Loshend
82088b4f5fSWarner Losh
83088b4f5fSWarner Loshfunction password.check()
84aedd6be5SKyle Evans	screen.clear()
85aedd6be5SKyle Evans	screen.defcursor()
8611cac431SKyle Evans	-- pwd is optionally supplied if we want to check it
87322a2dddSKyle Evans	local function doPrompt(prompt, pwd)
889f71d421SKyle Evans		while true do
89aedd6be5SKyle Evans			loader.printc(prompt)
90aedd6be5SKyle Evans			local read_pwd = password.read()
919f71d421SKyle Evans			if pwd == nil or pwd == read_pwd then
9224a1bd54SKyle Evans				-- Throw an extra newline after password prompt
93aedd6be5SKyle Evans				print("")
94aedd6be5SKyle Evans				return read_pwd
95088b4f5fSWarner Losh			end
96aedd6be5SKyle Evans			print("\n\nloader: incorrect password!\n")
97aedd6be5SKyle Evans			loader.delay(3*1000*1000)
98088b4f5fSWarner Losh		end
9911cac431SKyle Evans	end
10011cac431SKyle Evans	local function compare(prompt, pwd)
1019f71d421SKyle Evans		if pwd == nil then
102aedd6be5SKyle Evans			return
10311cac431SKyle Evans		end
104322a2dddSKyle Evans		doPrompt(prompt, pwd)
105088b4f5fSWarner Losh	end
106088b4f5fSWarner Losh
107aedd6be5SKyle Evans	local boot_pwd = loader.getenv("bootlock_password")
108aedd6be5SKyle Evans	compare("Boot password: ", boot_pwd)
109088b4f5fSWarner Losh
110aedd6be5SKyle Evans	local geli_prompt = loader.getenv("geom_eli_passphrase_prompt")
1119f71d421SKyle Evans	if geli_prompt ~= nil and geli_prompt:lower() == "yes" then
112322a2dddSKyle Evans		local passphrase = doPrompt("GELI Passphrase: ")
113aedd6be5SKyle Evans		loader.setenv("kern.geom.eli.passphrase", passphrase)
11411cac431SKyle Evans	end
11511cac431SKyle Evans
116aedd6be5SKyle Evans	local pwd = loader.getenv("password")
1179f71d421SKyle Evans	if pwd ~= nil then
118aedd6be5SKyle Evans		core.autoboot()
119088b4f5fSWarner Losh	end
120aedd6be5SKyle Evans	compare("Password: ", pwd)
121088b4f5fSWarner Loshend
122088b4f5fSWarner Losh
123aedd6be5SKyle Evansreturn password
124