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