kasi

POSIX shell password manager that is cryptography program-agnostic
Log | Files | Refs | README | LICENSE

kasi (2279B)


      1 #!/bin/sh -e
      2 
      3 # sets the directory with all your passwords
      4 KASI_DIR=${KASI_DIR:-"$HOME/.kasi/"}
      5 # sets the command to copy into your clipboard (must accept stdin)
      6 KASI_CLIPBOARD=${KASI_CLIPBOARD:-"xsel -i"}
      7 # sets command to use when listing directory structure
      8 KASI_LIST=${KASI_LIST:-"tree"}
      9 # custom command for generating passwords/users
     10 KASI_GEN_PASS=${KASI_GEN_PASS:-"random pass"}
     11 KASI_GEN_USER=${KASI_GEN_USER:-"random user"}
     12 
     13 mkdir -p "$KASI_DIR"
     14 cd "$KASI_DIR"
     15 
     16 # decrypt and print a password from KASI_DIR or print the directory structure
     17 view() {
     18 	if [ -f "$1" ]; then
     19 		$KASI_DECRYPT "$1"
     20 	elif [ -d "${1:-.}" ]; then
     21 		tree "${1:-.}"
     22 	else
     23 		usage
     24 		exit 1
     25 	fi
     26 }
     27 
     28 # view the first line
     29 view_single() {
     30 	[ ! -f "$1" ] && usage && exit 1
     31 
     32 	view "$1" | sed 1q | tr -d \\n
     33 }
     34 
     35 # copy the first line from view to clipboard
     36 copy() {
     37 	[ ! -f "$1" ] && usage && exit 1
     38 
     39 	view_single "$1" | $KASI_CLIPBOARD
     40 }
     41 
     42 # create new or existing password in KASI_DIR
     43 edit() {
     44 	[ -d "$1" ] && usage && exit 1
     45 
     46 	tmpfile="$(mktemp)"
     47 
     48 	if [ "$2" = "generate" ]; then
     49 		$KASI_GEN_PASS >"$tmpfile"
     50 		echo "Username: $($KASI_GEN_USER)" >>"$tmpfile"
     51 	fi
     52 
     53 	# edit a copy of a password if it already exists
     54 	[ -f "$1" ] && $KASI_DECRYPT "$1" > "$tmpfile"
     55 
     56 	${VISUAL:-${EDITOR:-ed}} "$tmpfile"
     57 
     58 	mkdir -p "$(dirname "$1")"
     59 	$KASI_ENCRYPT "$tmpfile" >"$1"
     60 }
     61 
     62 totp_view() {
     63 	[ -d "$1" ] && usage && exit 1
     64 
     65 	secret=$(view "$1" | grep TOTP | cut -d' ' -f 2)
     66 	oathtool -b --totp "$secret"
     67 }
     68 
     69 totp() {
     70 	totp_view "$1" | tr -d '\n' | $KASI_CLIPBOARD
     71 }
     72 
     73 # print usage
     74 usage() {
     75 	printf "Usage: %s [help|copy|edit|generate|view|view_single [file or directory]]\n" "$0"
     76 	printf "\tThe edit and copy command expects a file as an argument,\n"
     77 	printf "\twhile the view command expects a directory as an argument.\n"
     78 	printf "\tThe arguments for view and edit are relative to\n"
     79 	printf "\t\$KASI_DIR which is currently set to %s\n\n" "$KASI_DIR"
     80 
     81 	printf "\tThe view command is the default command if only a file or\n"
     82 	printf "\tdirectory is passed as an argument\n"
     83 }
     84 
     85 option="$1"
     86 case "$option" in
     87 	help) usage ;;
     88 	copy) copy "$2" ;;
     89 	edit) edit "$2" ;;
     90 	generate) edit "$2" generate ;;
     91 	view) view "$2" ;;
     92 	pass) view_single "$2" ;;
     93 	totp_view) totp_view "$2" ;;
     94 	totp) totp "$2" ;;
     95 	*) view "$1" ;;
     96 esac