maddy

Fork https://github.com/foxcpp/maddy

git clone git://git.lin.moe/go/maddy.git

  1/*
  2Maddy Mail Server - Composable all-in-one email server.
  3Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors
  4
  5This program is free software: you can redistribute it and/or modify
  6it under the terms of the GNU General Public License as published by
  7the Free Software Foundation, either version 3 of the License, or
  8(at your option) any later version.
  9
 10This program is distributed in the hope that it will be useful,
 11but WITHOUT ANY WARRANTY; without even the implied warranty of
 12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13GNU General Public License for more details.
 14
 15You should have received a copy of the GNU General Public License
 16along with this program.  If not, see <https://www.gnu.org/licenses/>.
 17*/
 18
 19package clitools
 20
 21import (
 22	"bufio"
 23	"errors"
 24	"fmt"
 25	"os"
 26)
 27
 28var stdinScanner = bufio.NewScanner(os.Stdin)
 29
 30func Confirmation(prompt string, def bool) bool {
 31	selection := "y/N"
 32	if def {
 33		selection = "Y/n"
 34	}
 35
 36	fmt.Fprintf(os.Stderr, "%s [%s]: ", prompt, selection)
 37	if !stdinScanner.Scan() {
 38		fmt.Fprintln(os.Stderr, stdinScanner.Err())
 39		return false
 40	}
 41
 42	switch stdinScanner.Text() {
 43	case "Y", "y":
 44		return true
 45	case "N", "n":
 46		return false
 47	default:
 48		return def
 49	}
 50}
 51
 52func readPass(tty *os.File, output []byte) ([]byte, error) {
 53	cursor := output[0:1]
 54	readen := 0
 55	for {
 56		n, err := tty.Read(cursor)
 57		if n != 1 {
 58			return nil, errors.New("ReadPassword: invalid read size when not in canonical mode")
 59		}
 60		if err != nil {
 61			return nil, errors.New("ReadPassword: " + err.Error())
 62		}
 63		if cursor[0] == '\n' {
 64			break
 65		}
 66		// Esc or Ctrl+D or Ctrl+C.
 67		if cursor[0] == '\x1b' || cursor[0] == '\x04' || cursor[0] == '\x03' {
 68			return nil, errors.New("ReadPassword: prompt rejected")
 69		}
 70		if cursor[0] == '\x7F' /* DEL */ {
 71			if readen != 0 {
 72				readen--
 73				cursor = output[readen : readen+1]
 74			}
 75			continue
 76		}
 77
 78		if readen == cap(output) {
 79			return nil, errors.New("ReadPassword: too long password")
 80		}
 81
 82		readen++
 83		cursor = output[readen : readen+1]
 84	}
 85
 86	return output[0:readen], nil
 87}
 88
 89func ReadPassword(prompt string) (string, error) {
 90	termios, err := TurnOnRawIO(os.Stdin)
 91	hiddenPass := true
 92	if err != nil {
 93		hiddenPass = false
 94		fmt.Fprintln(os.Stderr, "Failed to disable terminal output:", err)
 95	}
 96
 97	// There is no meaningful way to handle error here.
 98	//nolint:errcheck
 99	defer TcSetAttr(os.Stdin.Fd(), &termios)
100
101	fmt.Fprintf(os.Stderr, "%s: ", prompt)
102
103	if hiddenPass {
104		buf := make([]byte, 512)
105		buf, err = readPass(os.Stdin, buf)
106		if err != nil {
107			return "", err
108		}
109		fmt.Println()
110
111		return string(buf), nil
112	}
113	if !stdinScanner.Scan() {
114		return "", stdinScanner.Err()
115	}
116
117	return stdinScanner.Text(), nil
118}