Blame SOURCES/pkcs11-switch.sh

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