env/bin/credentials.sh

134 lines
2.3 KiB
Bash
Raw Normal View History

2019-09-11 04:11:40 +02:00
#!/bin/sh
2019-12-08 19:14:46 +01:00
# credentials
2020-05-03 16:47:27 +02:00
# Written in 2019-2020 by Lucas
2019-09-11 04:11:40 +02:00
# 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/>.
2019-12-05 11:58:10 +01:00
usage()
{
2019-12-28 01:12:31 +01:00
cat - <<. >&2
Usage:
${0##*/} [-2pu] service
${0##*/} -r key
.
2019-09-11 04:11:40 +02:00
exit 1
}
2019-12-05 11:58:10 +01:00
err()
{
printf "%s: %s\n" "${0##*/}" "$*" >&2
exit 1
}
2019-09-11 04:11:40 +02:00
2019-12-05 11:58:10 +01:00
clip()
{
xclip -q -r -l 1 -sel clip 2>/dev/null
2019-09-11 04:11:40 +02:00
}
2019-12-05 11:58:10 +01:00
get_user()
{
printf user
2020-05-25 17:18:06 +02:00
sekrit cp "services/$1/user" && printf "\n"
2019-09-11 04:11:40 +02:00
}
2019-12-05 11:58:10 +01:00
get_pass()
{
printf pass
2020-05-25 17:18:06 +02:00
sekrit cp "services/$1/pass" && printf "\n"
2019-12-05 11:58:10 +01:00
}
get_2fa()
{
printf 2fa
2019-09-11 04:11:40 +02:00
otpcli_opts=
case $1 in
isnic) otpcli_opts="-H sha512 -d 8"
;;
esac
2019-12-05 11:58:10 +01:00
sekrit get "services/$1/2fa" | otpcli $otpcli_opts | clip && printf "\n"
2019-09-11 04:11:40 +02:00
}
get_service()
2019-12-05 11:58:10 +01:00
{
service=$1
user=$2
pass=$3
sfa=$4
2019-09-11 04:11:40 +02:00
if [ $user = no ] && [ $pass = no ] && [ $sfa = no ]; then
sekrit has "services/$service/pass" ||
2020-05-03 16:47:27 +02:00
err "unknown service $service"
pass=yes
2020-05-03 16:47:27 +02:00
sekrit has "services/$service/user" && user=yes
sekrit has "services/$service/2fa" && sfa=yes
fi
if [ $user = check ]; then
2020-05-03 16:47:27 +02:00
sekrit has "services/$service/user" ||
err "service $service has no user"
user=yes
2019-12-16 14:50:59 +01:00
fi
if [ $pass = check ]; then
2020-05-03 16:47:27 +02:00
sekrit has "services/$service/pass" ||
err "service $service has no pass"
pass=yes
2019-12-16 14:50:59 +01:00
fi
if [ $sfa = check ]; then
2020-05-03 16:47:27 +02:00
sekrit has "services/$service/2fa" ||
err "service $service has no 2fa"
sfa=yes
2019-12-16 14:50:59 +01:00
fi
2019-09-11 04:11:40 +02:00
2019-12-05 11:58:10 +01:00
[ $user = yes ] && get_user "$service"
[ $pass = yes ] && get_pass "$service"
[ $sfa = yes ] && get_2fa "$service"
2019-09-11 04:11:40 +02:00
}
2019-12-28 01:12:31 +01:00
get_raw()
{
key=$1
2020-05-03 16:47:27 +02:00
sekrit has "$key" || err "unknown key $key"
2019-12-28 01:12:31 +01:00
printf "%s" "$key"
sekrit get "$key" | clip && printf "\n"
}
2019-09-11 04:11:40 +02:00
sfa=no
pass=no
2019-12-28 01:12:31 +01:00
raw=no
2019-09-11 04:11:40 +02:00
user=no
2019-12-28 01:12:31 +01:00
while getopts 2pru flag; do
2019-09-11 04:11:40 +02:00
case $flag in
2) sfa=check
2019-09-11 04:11:40 +02:00
;;
p) pass=check
2019-09-11 04:11:40 +02:00
;;
2019-12-28 01:12:31 +01:00
r) raw=yes
;;
u) user=check
2019-09-11 04:11:40 +02:00
;;
*) usage
;;
esac
done
2020-05-03 16:47:27 +02:00
shift $((OPTIND - 1))
2019-09-11 04:11:40 +02:00
2019-12-05 11:58:10 +01:00
[ $# -eq 1 ] && [ -n "$1" ] || usage
key=$1
2019-09-11 04:11:40 +02:00
2019-12-28 01:12:31 +01:00
if [ $raw = yes ]; then
if [ $sfa != no ] || [ $pass != no ] || [ $user != no ]; then
2020-05-03 16:47:27 +02:00
err "-r is mutually exclusive with -2pu"
2019-12-28 01:12:31 +01:00
fi
get_raw "$key"
else
get_service "$key" $user $pass $sfa
fi