#!/bin/bash

ENO_INVAL=2
ENO_PERM=1

discover_tools_dir=/usr/lib/biometric-authentication/discover-tools
discover_list=`ls ${discover_tools_dir} 2>/dev/null`
drv_list=""

device_type="all"
write_flag="false"
short_flag="false"

function help()
{
	echo "Discover biometric device, need root authority"
	echo "Usage:	biometric-device-discover [option]"
	echo ""
	echo "Options:"
	echo "	-t	device type (\"serial\", \"usb\", \"pcie\" or \"all\")"
	echo "	-w	Write the discover results to configuration file"
	echo "	-h	Show this help"
}

while getopts "t:wsh" arg #选项后面的冒号表示该选项需要参数
do
		case $arg in
			t)
				device_type=${OPTARG}

				case ${device_type} in
					all)
						;;
					serial)
						;;
					usb)
						;;
					pcie)
						;;
					*)
						echo "Unkonw device type: ${device_type}"
						exit ${ENO_INVAL}
						;;
				esac

				echo "Device type is ${device_type}" #参数存在$optarg中
				;;
			w)
				echo "Write the discover results to configuration file"
				write_flag="true"
				;;
			s)
				echo "Use short output"
				short_flag="true"
				;;
			h)
				help
				exit 0
				;;
			?)  #当有不认识的选项的时候arg为?
				#echo "Unkonw argument ${OPTARG}"
				help >&2
				exit ${ENO_INVAL}
				;;
		esac
done

user=`whoami`
if [ "${user}X" != "rootX" ];
then
	echo "Discover biometric device need root authority" >&2
	exit ${ENO_NOAUTH}
fi

if [ "${device_type}X" = "allX" ];
then
	device_type_list="serial usb pcie"
else
	device_type_list=${device_type}
fi

for device_type in ${device_type_list}; do
	drv_list=""

	for bio_drv in ${discover_list}; do
		if [[ "${bio_drv}" =~ "${device_type}-" ]]; then
			drv_list="${drv_list} ${bio_drv##${device_type}-}"
		fi
	done

	drv_list=${drv_list## } # 移除前导空格
	if [ "${drv_list}X" != "X" ]; then
		echo "${device_type} driver [${drv_list}] has discover tools"
	fi


	for driver in ${drv_list}; do
		# 过滤 driver 中的危险字符，仅允许字母、数字、下划线、连字符、点号，
		# 防止 ${driver} 拼入命令路径时被 shell 二次解析造成命令注入
		if ! [[ "${driver}" =~ ^[A-Za-z0-9_.-]+$ ]]; then
			echo "Skip invalid driver name: ${driver}" >&2
			continue
		fi

		dev_node_list=`/usr/lib/biometric-authentication/discover-tools/${device_type}-${driver} 2>/dev/null`
		echo -n "  Discover ${driver} device: "

		if [ "${dev_node_list}X" != "X" ]; then
			if [ ${write_flag} = "true" ]; then
				biometric-config-tool add-driver -f -d ${driver} /usr/lib/biometric-authentication/drivers/${driver}.so
			fi

		for dev_node in ${dev_node_list}; do
			# 过滤设备节点中的命令/路径特殊字符，防命令注入与路径穿越(../)
			# 仅允许字母、数字、下划线、点号、冒号、连字符、斜杠，
			# 并显式拒绝包含 ".." 的路径
			if ! [[ "${dev_node}" =~ ^[/a-zA-Z0-9_.:-]+$ ]] || [[ "${dev_node}" == *".."* ]]; then
				echo "Skip invalid device node: ${dev_node}" >&2
				continue
			fi

			echo "${dev_node}, enable ${driver} driver"
				if [ ${write_flag} == "true" ]; then
					if [ ${device_type} == "serial" ]; then
						biometric-config-tool set-key -f ${driver} Path ${dev_node}
					fi
					biometric-config-tool enable-driver ${driver}
				fi
			done
		else
			echo -n "not found"
			if [ ${write_flag} == "true" -a ${device_type} == "serial" ]; then
				echo ", remove ${driver} driver"
				biometric-config-tool remove-driver -i ${driver}
			else
				echo
			fi
		fi
	done
done




