Blame SOURCES/pkcs11-switch.sh

5831fa
#!/bin/sh
5831fa
5831fa
# Paths, names and functions definitions
5831fa
NSSDB="/etc/pki/nssdb/"
5831fa
COOLKEY_NAME="CoolKey PKCS #11 Module"
5831fa
COOLKEY_LIBRARY="libcoolkeypk11.so"
5831fa
OPENSC_NAME="OpenSC PKCS #11 Module"
5831fa
OPENSC_LIBRARY="opensc-pkcs11.so"
5831fa
5831fa
add_module() {
5831fa
	NAME="$1"
5831fa
	LIBRARY="$2"
5831fa
	modutil -add "$NAME" -dbdir "$NSSDB" -libfile "$LIBRARY"
5831fa
}
5831fa
remove_module() {
5831fa
	NAME="$1"
5831fa
	modutil -delete "$NAME" -dbdir "$NSSDB" -force
5831fa
}
5831fa
5831fa
# Parse arguments. If wrong, print usage
5831fa
TARGET="$1"
5831fa
if [ "$TARGET" = "" ]; then
5831fa
	# Print currently installed module
5831fa
	PRINT_CURRENT="1"
5831fa
elif [ "$TARGET" = "opensc" ] || [ "$TARGET" = "coolkey" ]; then
5831fa
	: # Correct arguments
5831fa
else
5831fa
	echo "Simple tool to switch between OpenSC and Coolkey PKCS#11 modules in main NSS DB."
5831fa
	echo "Usage: $0 [coolkey|opensc]"
5831fa
	echo "    [coolkey|opensc]   says which of the modules should be used."
5831fa
	echo "                       The other one will be removed from database."
5831fa
	echo
5831fa
	echo "    If there is no argument specified, prints the current module in NSS DB"
5831fa
	exit 255
5831fa
fi
5831fa
5831fa
if [ ! -x /usr/bin/modutil ]; then
5831fa
	echo "The modutil is not installed. Please install package nss-util"
5831fa
	exit 255
5831fa
fi
5831fa
5831fa
# Find the current library in NSS DB
5831fa
CURRENT="" # none
5831fa
LIBS=$(modutil -rawlist -dbdir "$NSSDB" | grep "^library=")
5831fa
if echo "$LIBS" | grep "$COOLKEY_NAME" > /dev/null; then
5831fa
	CURRENT="coolkey"
5831fa
fi
5831fa
if echo "$LIBS" | grep "$OPENSC_NAME" > /dev/null; then
5831fa
	if [ -n "$CURRENT" ]; then
5831fa
		CURRENT="opensc coolkey"
5831fa
		echo "There are both modules in NSS DB, which is not recommended."
5831fa
		echo "I will remove the other."
5831fa
	else
5831fa
		CURRENT="opensc"
5831fa
	fi
5831fa
fi
5831fa
5831fa
if [ "$PRINT_CURRENT" = "1" ]; then
5831fa
	echo "$CURRENT"
5831fa
	exit 0
5831fa
fi
5831fa
5831fa
# Do we need to change something?
5831fa
if [ "$CURRENT" = "$TARGET" ]; then
5831fa
	echo "The requested module is already in the NSS DB"
5831fa
	exit 0
5831fa
fi
5831fa
5831fa
# Do the actual change
5831fa
if [ "$TARGET" = "opensc" ]; then
5831fa
	add_module "$OPENSC_NAME" "$OPENSC_LIBRARY"
5831fa
	remove_module "$COOLKEY_NAME"
5831fa
fi
5831fa
if [ "$TARGET" = "coolkey" ]; then
5831fa
	add_module "$COOLKEY_NAME" "$COOLKEY_LIBRARY"
5831fa
	remove_module "$OPENSC_NAME"
5831fa
fi