1-- 2-- SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3-- 4-- Copyright (c) 2015 Pedro Souza <pedrosouza@freebsd.org> 5-- Copyright (C) 2018 Kyle Evans <kevans@FreeBSD.org> 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 twiddle_pos = 1 46 47 local function draw_twiddle() 48 printc(twiddle_chars[twiddle_pos]) 49 -- Reset cursor to just after the password prompt 50 screen.setcursor(prompt_length + 2, screen.default_y) 51 twiddle_pos = (twiddle_pos % #twiddle_chars) + 1 52 end 53 54 -- Space between the prompt and any on-screen feedback 55 printc(" ") 56 while true do 57 local ch = io.getchar() 58 if ch == core.KEY_ENTER then 59 break 60 end 61 if ch == core.KEY_BACKSPACE or ch == core.KEY_DELETE then 62 if #str > 0 then 63 if show_password_mask then 64 printc("\008 \008") 65 else 66 draw_twiddle() 67 end 68 str = str:sub(1, #str - 1) 69 end 70 else 71 if show_password_mask then 72 printc("*") 73 else 74 draw_twiddle() 75 end 76 str = str .. string.char(ch) 77 end 78 end 79 return str 80end 81 82function password.check() 83 screen.clear() 84 screen.defcursor() 85 -- pwd is optionally supplied if we want to check it 86 local function doPrompt(prompt, pwd) 87 local attempts = 1 88 89 local function clear_incorrect_text_prompt() 90 printc("\r" .. string.rep(" ", #INCORRECT_PASSWORD)) 91 end 92 93 while true do 94 if attempts > 1 then 95 clear_incorrect_text_prompt() 96 end 97 screen.defcursor() 98 printc(prompt) 99 local read_pwd = password.read(#prompt) 100 if pwd == nil or pwd == read_pwd then 101 -- Clear the prompt + twiddle 102 printc(string.rep(" ", #prompt + 5)) 103 return read_pwd 104 end 105 printc("\n" .. INCORRECT_PASSWORD) 106 attempts = attempts + 1 107 loader.delay(3*1000*1000) 108 end 109 end 110 local function compare(prompt, pwd) 111 if pwd == nil then 112 return 113 end 114 doPrompt(prompt, pwd) 115 end 116 117 local boot_pwd = loader.getenv("bootlock_password") 118 compare("Bootlock password:", boot_pwd) 119 120 local geli_prompt = loader.getenv("geom_eli_passphrase_prompt") 121 if geli_prompt ~= nil and geli_prompt:lower() == "yes" then 122 local passphrase = doPrompt("GELI Passphrase:") 123 loader.setenv("kern.geom.eli.passphrase", passphrase) 124 end 125 126 local pwd = loader.getenv("password") 127 if pwd ~= nil then 128 core.autoboot() 129 end 130 compare("Loader password:", pwd) 131end 132 133return password 134