#!/bin/bash -ue

<< MIT-LICENSE
Copyright (c) 2023 Jianjun Liu<jianjunliu@126.com> https://www.pkglinux.top

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
MIT-LICENSE

CHKPASS_VERBOSE=""
CHKPASS_PASS=false
CHKPASS_ALGO=false
CHKPASS_SALT=false
CHKPASS_CHCK=false

chkpass-usage()
{
	# Display Help
	echo "Authenticate username against the shadow file.";
	echo
	echo "Syntax: $0 [ -h|v|V|p|a|s|c ] [ --help|verbose|version|pass|hash|algo|algorithm|salt|chk|chck|check ]"
	echo "options:"
	echo "h     Print this help."
	echo "v     Verbose mode."
	echo "V     Print software version and exit."
	echo "p     Print out original password string."
	echo "a     Print out password hashing algorithm."
	echo "s     Print out password hashing salt."
	echo "c     Pring out hashed password string."
	echo
}

for arg in "$@"; do
	shift
	case "$arg" in
		"--help")	set -- "$@" "-h" ;;
		"--verbose")	set -- "$@" "-v" ;;
		"--version")	set -- "$@" "-V" ;;
		"--pass")	set -- "$@" "-p" ;;
		"--hash")	set -- "$@" "-p" ;;
		"--algo")	set -- "$@" "-a" ;;
		"--algorithm")	set -- "$@" "-a" ;;
		"--salt")	set -- "$@" "-s" ;;
		"--chk")	set -- "$@" "-c" ;;
		"--chck")	set -- "$@" "-c" ;;
		"--check")	set -- "$@" "-c" ;;
		*)		set -- "$@" "$arg"
	esac
done

while getopts ':hvVpasc' OPTION; do
	case "$OPTION" in
		h)	# display usage
			chkpass-usage
			exit;;
		v)	# verbose mode
			CHKPASS_VERBOSE="-v"
			;;
		V)	# display version
			echo "2022Q2"
			exit;;
		p)	# hash string
			CHKPASS_PASS=true ;;
		a)	# algorithm
			CHKPASS_ALGO=true ;;
		s)	# salt
			CHKPASS_SALT=true ;;
		c)	# check 
			CHKPASS_CHCK=true ;;
		\?)	# Invalid option
	                echo "Error: Invalid option"
			exit;;
	esac
done

shift "$(($OPTIND -1))"

[ "$#" -lt 1 ] && chkpass-usage && exit 1;

[ ${UID} -ne 0 ] && echo "$0 will not be performed when not running as root" && exit 1;

chkpass-hash()
{
	echo $(getent passwd $1) | cut -d: -f2;
}

chkpass-algo()
{
	echo $(chkpass-hash $1 | cut -d$ -f2);
}

chkpass-salt()
{
	echo $(chkpass-hash $1 | cut -d$ -f3);
}

[ -z "$(chkpass-hash $1)" ] && echo "Empty hash string." && exit 1;

$CHKPASS_PASS && echo $(chkpass-hash $1);

$CHKPASS_ALGO && echo $(chkpass-algo $1);

$CHKPASS_SALT && echo $(chkpass-salt $1);

if [ "$#" -gt 1 ]; then
	CHKPASS_HASH=$(echo $2 | openssl passwd -$(chkpass-algo $1) -salt $(chkpass-salt $1) -stdin);
else
	CHKPASS_HASH=$(openssl passwd -$(chkpass-algo $1) -salt $(chkpass-salt $1));
fi

$CHKPASS_CHCK && echo $CHKPASS_HASH;

[ "$CHKPASS_HASH" = "$(chkpass-hash $1)" ] && exit 0 || echo "Not matched!";

exit 1
