env/bin/credentials.sh
2019-09-11 17:44:49 +00:00

110 lines
2.1 KiB
Bash

#!/bin/sh
# env
# Written in 2019 by Lucas
# CC0 1.0 Universal/Public domain - No rights reserved
#
# To the extent possible under law, the author(s) have dedicated all
# copyright and related and neighboring rights to this software to the
# public domain worldwide. This software is distributed without any
# warranty. You should have received a copy of the CC0 Public Domain
# Dedication along with this software. If not, see
# <http://creativecommons.org/publicdomain/zero/1.0/>.
usage() {
cat - <<. >&2
Usage:
${0##*/} [-2pu] service
.
exit 1
}
clip() { xclip -q -r -l 1 -sel clip 2>/dev/null; }
get_user() {
echo user
sekrit get services/"$1"/user | clip
}
get_pass() {
echo pass
sekrit get services/"$1"/pass | clip
}
get_2fa() {
echo 2fa
otpcli_opts=
case $1 in
isnic) otpcli_opts="-H sha512 -d 8"
;;
esac
sekrit get services/"$1"/2fa | otpcli $otpcli_opts | clip
}
get_from_flags() {
user=$1
pass=$2
sfa=$3
s=$4
if [ $user = yes ] && ! sekrit has services/"$s"/user; then
printf "%s: service \"%s\" has no user.\n" \
"${0##*/}" "$s" >&2
exit 1
fi
if [ $pass = yes ] && ! sekrit has services/"$s"/pass; then
printf "%s: service \"%s\" has no pass.\n" \
"${0##*/}" "$s" >&2
exit 1
fi
if [ $sfa = yes ] && ! sekrit has services/"$s"/2fa; then
printf "%s: service \"%s\" has no 2fa.\n" \
"${0##*/}" "$s" >&2
exit 1
fi
[ $user = yes ] && get_user "$s"
[ $pass = yes ] && get_pass "$s"
[ $sfa = yes ] && get_2fa "$s"
}
get_all() {
s=$1
if ! sekrit has services/"$s"/pass; then
printf "%s: Unknown service \"%s\".\n" \
"${0##*/}" "$s" >&2
exit 1
fi
sekrit has services/"$s"/user && get_user "$s"
get_pass "$s"
sekrit has services/"$s"/2fa && get_2fa "$s"
}
sfa=no
pass=no
user=no
while getopts 2pu flag; do
case $flag in
2) sfa=yes
;;
p) pass=yes
;;
u) user=yes
;;
*) usage
;;
esac
done
shift $(($OPTIND - 1))
if [ $# -ne 1 ] || [ -z "$1" ]; then
usage
fi
service=$1
if [ $sfa = yes ] || [ $pass = yes ] || [ $user = yes ]; then
get_from_flags $user $pass $sfa "$service"
else
get_all "$service"
fi