1-- 2-- SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3-- 4-- Copyright (c) 2015 Pedro Souza <[email protected]> 5-- Copyright (C) 2018 Kyle Evans <[email protected]> 6-- All rights reserved. 7-- 8-- Redistribution and use in source and binary forms, with or without 9-- modification, are permitted provided that the following conditions 10-- are met: 11-- 1. Redistributions of source code must retain the above copyright 12-- notice, this list of conditions and the following disclaimer. 13-- 2. Redistributions in binary form must reproduce the above copyright 14-- notice, this list of conditions and the following disclaimer in the 15-- documentation and/or other materials provided with the distribution. 16-- 17-- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20-- ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26-- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27-- SUCH DAMAGE. 28-- 29-- $FreeBSD$ 30-- 31 32local core = require("core") 33local screen = require("screen") 34 35local password = {} 36 37local INCORRECT_PASSWORD = "loader: incorrect password!" 38-- Asterisks as a password mask 39local show_password_mask = false 40local twiddle_chars = {"/", "-", "\\", "|"} 41 42-- Module exports 43function password.read(prompt_length) 44 local str = "" 45 local n = 0 46 local twiddle_pos = 1 47 48 local function draw_twiddle() 49 loader.printc(" " .. twiddle_chars[twiddle_pos]) 50 -- Reset cursor to just after the password prompt 51 screen.setcursor(prompt_length + 2, screen.default_y) 52 twiddle_pos = (twiddle_pos % #twiddle_chars) + 1 53 end 54 55 -- Space between the prompt and any on-screen feedback 56 loader.printc(" ") 57 while true do 58 local ch = io.getchar() 59 if ch == core.KEY_ENTER then 60 break 61 end 62 if ch == core.KEY_BACKSPACE or ch == core.KEY_DELETE then 63 if n > 0 then 64 n = n - 1 65 if show_password_mask then 66 loader.printc("\008 \008") 67 else 68 draw_twiddle() 69 end 70 str = str:sub(1, n) 71 end 72 else 73 if show_password_mask then 74 loader.printc("*") 75 else 76 draw_twiddle() 77 end 78 str = str .. string.char(ch) 79 n = n + 1 80 end 81 end 82 return str 83end 84 85function password.check() 86 screen.clear() 87 screen.defcursor() 88 -- pwd is optionally supplied if we want to check it 89 local function doPrompt(prompt, pwd) 90 local attempts = 1 91 92 local function clear_incorrect_text_prompt() 93 loader.printc("\n") 94 loader.printc(string.rep(" ", #INCORRECT_PASSWORD)) 95 end 96 97 while true do 98 screen.defcursor() 99 loader.printc(prompt) 100 local read_pwd = password.read(#prompt) 101 if pwd == nil or pwd == read_pwd then 102 -- Clear the prompt + twiddle 103 loader.printc(string.rep(" ", #prompt + 5)) 104 if attempts > 1 then 105 clear_incorrect_text_prompt() 106 end 107 return read_pwd 108 end 109 loader.printc("\n" .. INCORRECT_PASSWORD) 110 attempts = attempts + 1 111 loader.delay(3*1000*1000) 112 end 113 end 114 local function compare(prompt, pwd) 115 if pwd == nil then 116 return 117 end 118 doPrompt(prompt, pwd) 119 end 120 121 local boot_pwd = loader.getenv("bootlock_password") 122 compare("Boot password: ", boot_pwd) 123 124 local geli_prompt = loader.getenv("geom_eli_passphrase_prompt") 125 if geli_prompt ~= nil and geli_prompt:lower() == "yes" then 126 local passphrase = doPrompt("GELI Passphrase: ") 127 loader.setenv("kern.geom.eli.passphrase", passphrase) 128 end 129 130 local pwd = loader.getenv("password") 131 if pwd ~= nil then 132 core.autoboot() 133 end 134 compare("Password: ", pwd) 135end 136 137return password 138