xref: /freebsd/stand/lua/password.lua (revision d61897abea6c619bf238b5a81d8ab7eda4e905c0)
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 = {}
360901ba3aSKyle Evans
376c8e9a84SEdward Tomasz Napieralalocal INCORRECT_PASSWORD = "loader: incorrect password"
38a6f1506fSKyle Evans-- Asterisks as a password mask
39a6f1506fSKyle Evanslocal show_password_mask = false
40a6f1506fSKyle Evanslocal twiddle_chars = {"/", "-", "\\", "|"}
41*d61897abSKyle Evanslocal screen_setup = false
42c8518398SKyle Evans
43b5746545SKyle Evans-- Module exports
440901ba3aSKyle Evansfunction password.read(prompt_length)
45aedd6be5SKyle Evans	local str = ""
468620ae01SKyle Evans	local twiddle_pos = 1
47088b4f5fSWarner Losh
48a6f1506fSKyle Evans	local function draw_twiddle()
496c8e9a84SEdward Tomasz Napierala		printc(twiddle_chars[twiddle_pos])
50ed1d0954SKyle Evans		-- Reset cursor to just after the password prompt
51ed1d0954SKyle Evans		screen.setcursor(prompt_length + 2, screen.default_y)
52a6f1506fSKyle Evans		twiddle_pos = (twiddle_pos % #twiddle_chars) + 1
53a6f1506fSKyle Evans	end
54a6f1506fSKyle Evans
55a6f1506fSKyle Evans	-- Space between the prompt and any on-screen feedback
56223e9874SKyle Evans	printc(" ")
57a5e2e5c7SKyle Evans	while true do
58e2df27e3SKyle Evans		local ch = io.getchar()
599f71d421SKyle Evans		if ch == core.KEY_ENTER then
60aedd6be5SKyle Evans			break
61088b4f5fSWarner Losh		end
629f71d421SKyle Evans		if ch == core.KEY_BACKSPACE or ch == core.KEY_DELETE then
63cb4fbe4eSKyle Evans			if #str > 0 then
64a6f1506fSKyle Evans				if show_password_mask then
65223e9874SKyle Evans					printc("\008 \008")
66a6f1506fSKyle Evans				else
67a6f1506fSKyle Evans					draw_twiddle()
68a6f1506fSKyle Evans				end
69cb4fbe4eSKyle Evans				str = str:sub(1, #str - 1)
70088b4f5fSWarner Losh			end
71088b4f5fSWarner Losh		else
72a6f1506fSKyle Evans			if show_password_mask then
73223e9874SKyle Evans				printc("*")
74a6f1506fSKyle Evans			else
75a6f1506fSKyle Evans				draw_twiddle()
76a6f1506fSKyle Evans			end
77aedd6be5SKyle Evans			str = str .. string.char(ch)
78088b4f5fSWarner Losh		end
79a5e2e5c7SKyle Evans	end
80aedd6be5SKyle Evans	return str
81088b4f5fSWarner Loshend
82088b4f5fSWarner Losh
83088b4f5fSWarner Loshfunction password.check()
8411cac431SKyle Evans	-- pwd is optionally supplied if we want to check it
85322a2dddSKyle Evans	local function doPrompt(prompt, pwd)
860901ba3aSKyle Evans		local attempts = 1
870901ba3aSKyle Evans
880901ba3aSKyle Evans		local function clear_incorrect_text_prompt()
896c8e9a84SEdward Tomasz Napierala			printc("\r" .. string.rep(" ", #INCORRECT_PASSWORD))
900901ba3aSKyle Evans		end
910901ba3aSKyle Evans
92*d61897abSKyle Evans		if not screen_setup then
93*d61897abSKyle Evans			screen.clear()
94*d61897abSKyle Evans			screen.defcursor()
95*d61897abSKyle Evans			screen_setup = true
96*d61897abSKyle Evans		end
97*d61897abSKyle Evans
989f71d421SKyle Evans		while true do
996c8e9a84SEdward Tomasz Napierala			if attempts > 1 then
1006c8e9a84SEdward Tomasz Napierala				clear_incorrect_text_prompt()
1016c8e9a84SEdward Tomasz Napierala			end
1020901ba3aSKyle Evans			screen.defcursor()
103223e9874SKyle Evans			printc(prompt)
1040901ba3aSKyle Evans			local read_pwd = password.read(#prompt)
1059f71d421SKyle Evans			if pwd == nil or pwd == read_pwd then
1060901ba3aSKyle Evans				-- Clear the prompt + twiddle
107223e9874SKyle Evans				printc(string.rep(" ", #prompt + 5))
108aedd6be5SKyle Evans				return read_pwd
109088b4f5fSWarner Losh			end
110223e9874SKyle Evans			printc("\n" .. INCORRECT_PASSWORD)
1110901ba3aSKyle Evans			attempts = attempts + 1
112aedd6be5SKyle Evans			loader.delay(3*1000*1000)
113088b4f5fSWarner Losh		end
11411cac431SKyle Evans	end
11511cac431SKyle Evans	local function compare(prompt, pwd)
1169f71d421SKyle Evans		if pwd == nil then
117aedd6be5SKyle Evans			return
11811cac431SKyle Evans		end
119322a2dddSKyle Evans		doPrompt(prompt, pwd)
120088b4f5fSWarner Losh	end
121088b4f5fSWarner Losh
122aedd6be5SKyle Evans	local boot_pwd = loader.getenv("bootlock_password")
1236c8e9a84SEdward Tomasz Napierala	compare("Bootlock password:", boot_pwd)
124088b4f5fSWarner Losh
125aedd6be5SKyle Evans	local geli_prompt = loader.getenv("geom_eli_passphrase_prompt")
1269f71d421SKyle Evans	if geli_prompt ~= nil and geli_prompt:lower() == "yes" then
127322a2dddSKyle Evans		local passphrase = doPrompt("GELI Passphrase:")
128aedd6be5SKyle Evans		loader.setenv("kern.geom.eli.passphrase", passphrase)
12911cac431SKyle Evans	end
13011cac431SKyle Evans
131aedd6be5SKyle Evans	local pwd = loader.getenv("password")
1329f71d421SKyle Evans	if pwd ~= nil then
133aedd6be5SKyle Evans		core.autoboot()
134088b4f5fSWarner Losh	end
1356c8e9a84SEdward Tomasz Napierala	compare("Loader password:", pwd)
136088b4f5fSWarner Loshend
137088b4f5fSWarner Losh
138aedd6be5SKyle Evansreturn password
139