2021-02-15 15:16:49 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# creates trash email for registration for online-services
|
|
|
|
# email is hashed from domain of service + login-username from linux-system
|
|
|
|
# or only unhashed service-domain
|
|
|
|
# this created email-address is added to LDAP target email als dcMailAlias
|
|
|
|
# also added to pass passwordmanager of user
|
|
|
|
|
2021-02-15 15:27:28 +01:00
|
|
|
[ -e ${MSC_BASE}/defaults.conf ] && . ${MSC_BASE}/defaults.conf
|
|
|
|
|
|
|
|
LDAP_HOST=${LDAP_HOST_DEFAULT}
|
|
|
|
BIND_DN=${LDAP_ADMIN_BIND_DN}
|
|
|
|
PASS_ID=${PASS_ID_LDAP_ADMIN}
|
|
|
|
OWN_DOMAIN=${TRASHMAIL_OWN_DOMAIN_DEFAULT}
|
|
|
|
TARGET_MAIL=${TRASHMAIL_TARGET_MAIL_DEFAULT}
|
|
|
|
HASHED_DEFAULT=${TRASHMAIL_HASHED_DEFAULT}
|
2021-03-01 13:42:52 +01:00
|
|
|
HASHLENGTH_DEFAULT=8
|
|
|
|
PWOPTS_DEFAULT="-c -n -s"
|
|
|
|
PWLENGTH_DEFAULT=50
|
2021-02-15 15:16:49 +01:00
|
|
|
|
2021-11-18 00:47:59 +01:00
|
|
|
usage () {
|
2021-03-03 06:20:28 +01:00
|
|
|
cat << EOF
|
|
|
|
|
|
|
|
usage: ${0} <OPTIONS> <URL> [<PWLENGTH>]
|
|
|
|
|
|
|
|
URL: https://target.domain.tld:8080/path/to/site | target.domain.tld
|
|
|
|
PWLENGTH: Integer, count characters in generated password
|
|
|
|
|
|
|
|
OPTIONS:
|
2021-03-21 00:59:58 +01:00
|
|
|
--delete delete trashmail from LDAP
|
2021-03-03 06:20:28 +01:00
|
|
|
-d include domain from <URL> in username: 8ee948ae.target.domain.tld@mydomain.tld
|
|
|
|
-f|--force force overwrite existing entry in pass
|
|
|
|
-h|--hashed create hash from domain and linux-login-user (${USER})
|
|
|
|
--help show this help/usage
|
|
|
|
-l|--length <INTEGER>|full cut hashed part of username to <INTGER> characters from start (default: ${HASHLENGHT_DEFAULT})
|
|
|
|
-n|--not-hashed do not hash username from targetdomain and linux-login-user
|
2021-10-24 12:05:30 +02:00
|
|
|
-t|--target-mail <TARGET_MAIL> Emailadress for which trashmail is created
|
2021-03-03 06:20:28 +01:00
|
|
|
-w|--full-hash do not cut hash (same as »-l full«)
|
|
|
|
-y|--symbols same option as in pwgen. Include at least one special character in the password.
|
|
|
|
|
|
|
|
|
|
|
|
EOF
|
|
|
|
|
|
|
|
}
|
2021-02-15 15:16:49 +01:00
|
|
|
|
2021-03-03 17:35:43 +01:00
|
|
|
# TODO: how exit main-shell from error in subshell???
|
2021-10-24 12:05:30 +02:00
|
|
|
getopt -u -o dfhl:nt:wy --long delete,force,hashed,help,not-hashed,hashlength:,whole-hash,symblols,target-mail: -- "$@" || exit $?
|
|
|
|
set -- $(getopt -u -o dfhl:nt:wy --long delete,force,hashed,help,not-hashed,hashlength:,whole-hash,symblols,target-mail: -- "$@"|| exit $?)
|
2021-03-03 17:35:43 +01:00
|
|
|
|
|
|
|
echo @: $@
|
2021-02-15 15:16:49 +01:00
|
|
|
while : ; do
|
|
|
|
case $1 in
|
2021-03-21 00:59:58 +01:00
|
|
|
--delete)
|
|
|
|
ACTION=delete
|
|
|
|
shift
|
|
|
|
;;
|
2021-03-01 13:42:52 +01:00
|
|
|
-d)
|
|
|
|
INCLUDE_DOMAIN=true
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-f|--force)
|
|
|
|
FORCE=true
|
2021-02-15 15:16:49 +01:00
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-h|--hashed)
|
|
|
|
HASHED=true
|
|
|
|
shift
|
|
|
|
;;
|
2021-03-03 06:20:28 +01:00
|
|
|
--help)
|
2021-11-18 00:47:59 +01:00
|
|
|
usage
|
2021-03-03 06:20:28 +01:00
|
|
|
exit 0
|
|
|
|
;;
|
2021-03-01 13:42:52 +01:00
|
|
|
-l|--hashlength)
|
|
|
|
HASHLENGTH=$2
|
|
|
|
shift; shift;
|
|
|
|
;;
|
|
|
|
-n|--not-hashed)
|
|
|
|
HASHED=false
|
|
|
|
shift
|
|
|
|
;;
|
2021-10-24 12:05:30 +02:00
|
|
|
-t|--target-mail)
|
|
|
|
TARGET_MAIL=$2
|
|
|
|
shift; shift;
|
|
|
|
;;
|
2021-03-01 13:42:52 +01:00
|
|
|
-w|--full-hash)
|
|
|
|
HASHLENGTH=full
|
|
|
|
shift;
|
|
|
|
;;
|
|
|
|
-y)
|
|
|
|
PWOPTS="${PWOPTS} -y"
|
2021-02-15 15:16:49 +01:00
|
|
|
shift
|
|
|
|
;;
|
|
|
|
--)
|
|
|
|
shift
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
*)
|
2021-03-03 06:20:28 +01:00
|
|
|
echo wrong argument $1 >&2
|
2021-11-18 00:47:59 +01:00
|
|
|
usage
|
2021-03-03 06:20:28 +01:00
|
|
|
exit 1
|
2021-02-15 15:16:49 +01:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2021-03-01 13:42:52 +01:00
|
|
|
# Urlparsing inspired by: https://gist.github.com/joshisa/297b0bc1ec0dcdda0d1625029711fa24
|
|
|
|
# Referenced and tweaked from http://stackoverflow.com/questions/6174220/parse-url-in-shell-script#6174447
|
|
|
|
|
2021-02-15 15:16:49 +01:00
|
|
|
URL=$1
|
2021-03-01 13:42:52 +01:00
|
|
|
protocol=$(echo "$1" | grep "://" | sed -e's,^\(.*://\).*,\1,g')
|
|
|
|
# Remove the protocol
|
|
|
|
url_no_protocol=$(echo "${1/$protocol/}")
|
|
|
|
# Use tr: Make the protocol lower-case for easy string compare
|
|
|
|
protocol=$(echo "$protocol" | tr '[:upper:]' '[:lower:]')
|
|
|
|
|
2021-02-15 15:16:49 +01:00
|
|
|
|
2021-03-01 13:42:52 +01:00
|
|
|
# Extract the user and password (if any)
|
|
|
|
# cut 1: Remove the path part to prevent @ in the querystring from breaking the next cut
|
|
|
|
# rev: Reverse string so cut -f1 takes the (reversed) rightmost field, and -f2- is what we want
|
|
|
|
# cut 2: Remove the host:port
|
|
|
|
# rev: Undo the first rev above
|
|
|
|
userpass=$(echo "$url_no_protocol" | grep "@" | cut -d"/" -f1 | rev | cut -d"@" -f2- | rev)
|
|
|
|
pass=$(echo "$userpass" | grep ":" | cut -d":" -f2)
|
|
|
|
if [ -n "$pass" ]; then
|
|
|
|
user=$(echo "$userpass" | grep ":" | cut -d":" -f1)
|
|
|
|
else
|
|
|
|
user="$userpass"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Extract the host
|
|
|
|
hostport=$(echo "${url_no_protocol/$userpass@/}" | cut -d"/" -f1)
|
|
|
|
host=$(echo "$hostport" | cut -d":" -f1)
|
|
|
|
port=$(echo "$hostport" | grep ":" | cut -d":" -f2)
|
|
|
|
path=$(echo "$url_no_protocol" | grep "/" | cut -d"/" -f2-)
|
|
|
|
|
|
|
|
echo "Create a trashmail-address for ${host}" >&2
|
2021-02-15 15:16:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
if ${HASHED:-$HASHED_DEFAULT}; then
|
2021-03-01 13:42:52 +01:00
|
|
|
case $HASHLENGTH in
|
|
|
|
full)
|
|
|
|
TRASHUSER="$(echo ${host}${USER}|md5sum -|awk '{print $1}')"
|
|
|
|
;;
|
|
|
|
[0-9]|[0-9][0-9])
|
|
|
|
TRASHUSER="$(echo ${host}${USER}|md5sum -|awk '{print $1}'|cut -c-${HASHLENGTH})"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
TRASHUSER="$(echo ${host}${USER}|md5sum -|awk '{print $1}'|cut -c-${HASHLENGTH_DEFAULT})"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
if ${INCLUDE_DOMAIN:-false} ;then
|
|
|
|
$INCLUDE_DOMAIN && TRASHUSER=${TRASHUSER}.${host}
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
2021-02-15 15:16:49 +01:00
|
|
|
else
|
2021-03-01 13:42:52 +01:00
|
|
|
TRASHUSER=${host}
|
2021-02-15 15:16:49 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
TRASHMAIL=${TRASHUSER}@${OWN_DOMAIN}
|
|
|
|
echo $TRASHMAIL
|
|
|
|
|
2021-03-01 13:42:52 +01:00
|
|
|
PASS_ENTRY="${PASS_PREFIX%/}${PASS_PREFIX:+/}${host}/${TRASHMAIL}"
|
2021-02-15 15:16:49 +01:00
|
|
|
|
2021-03-21 00:59:58 +01:00
|
|
|
case ${ACTION:-create} in
|
|
|
|
create)
|
2021-03-01 13:42:52 +01:00
|
|
|
#set -x
|
|
|
|
echo "Add new trashmail to LDAP"
|
2021-02-15 15:16:49 +01:00
|
|
|
cat << EOF |ldapmodify -Z -H ldap://${LDAP_HOST} -D ${BIND_DN} -x -w $(pass ${PASS_ID}|head -n 1)
|
|
|
|
dn: dcSubMailAddress=${TARGET_MAIL},ou=mailaccounts,dc=schuerz,dc=at
|
|
|
|
changetype: modify
|
|
|
|
add: dcMailAlternateAddress
|
|
|
|
dcMailAlternateAddress: ${TRASHMAIL}
|
|
|
|
EOF
|
|
|
|
|
|
|
|
if [ $? -gt 0 ]; then
|
|
|
|
echo pass find ${PASS_ENTRY}
|
|
|
|
pass find ${TRASHMAIL}|grep -v "Search Terms"
|
|
|
|
case $? in
|
|
|
|
1)
|
|
|
|
# returncode 1 from grep means, no line selected. so no entry exists, create new one
|
2021-03-01 13:42:52 +01:00
|
|
|
echo "Entry not found --> create new pass-entry"
|
2021-02-15 15:16:49 +01:00
|
|
|
CREATE=true
|
|
|
|
;;
|
|
|
|
0)
|
|
|
|
if ${FORCE-false} ; then
|
2021-03-01 13:42:52 +01:00
|
|
|
echo "Entry found but enforced to overwrite"
|
2021-02-15 15:16:49 +01:00
|
|
|
CREATE=true
|
|
|
|
else
|
2021-03-01 13:42:52 +01:00
|
|
|
echo
|
2021-02-15 15:16:49 +01:00
|
|
|
echo "Current password for ${PASS_ENTRY} is $(pass ${PASS_ENTRY} |head -n1)."
|
|
|
|
read -p "Overwrite? [Y|n]: "
|
|
|
|
echo ""
|
|
|
|
case $REPLY in
|
|
|
|
y|Y)
|
|
|
|
CREATE=true
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
CREATE=false
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Something went wrong"
|
|
|
|
exit 2
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
else
|
|
|
|
CREATE=true
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ${CREATE-false}; then
|
|
|
|
cat << EOF |pass insert -m ${PASS_ENTRY}
|
2021-03-01 13:42:52 +01:00
|
|
|
$(pwgen ${PWOPTS_DEFAULT} ${PWOPTS} ${2:-$PWLENGTH_DEFAULT} 1)
|
2021-02-15 15:16:49 +01:00
|
|
|
email: ${TRASHMAIL}
|
|
|
|
login: ${TRASHUSER}
|
|
|
|
url: ${URL}
|
2021-03-01 13:42:52 +01:00
|
|
|
comment: trashemail autogenerated md5-hash from »${host}${USER}« cut to ${HASHLENGTH}. Delete email, when account deleted!!!
|
2021-10-31 10:17:49 +01:00
|
|
|
targetmail for this trashmail is ${TARGET_MAIL}
|
2021-02-15 15:16:49 +01:00
|
|
|
EOF
|
|
|
|
fi
|
2021-03-01 13:42:52 +01:00
|
|
|
#set +x
|
|
|
|
#pass git commit "${PASS_ENTRY}"
|
|
|
|
pass -c ${PASS_ENTRY}
|
2021-03-21 00:59:58 +01:00
|
|
|
;;
|
|
|
|
delete)
|
|
|
|
|
|
|
|
cat << EOF |ldapmodify -Z -H ldap://${LDAP_HOST} -D ${BIND_DN} -x -w $(pass ${PASS_ID}|head -n 1)
|
|
|
|
dn: dcSubMailAddress=${TARGET_MAIL},ou=mailaccounts,dc=schuerz,dc=at
|
|
|
|
changetype: modify
|
|
|
|
delete: dcMailAlternateAddress
|
|
|
|
dcMailAlternateAddress: ${TRASHMAIL}
|
|
|
|
EOF
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo action $ACTION not known
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|