Blame SOURCES/libexec-generate-server-cert.sh

adf540
#!/bin/bash
adf540
# Author: Jan Vcelak <jvcelak@redhat.com>
adf540
adf540
set -e
adf540
adf540
# default options
adf540
adf540
CERTDB_DIR=/etc/openldap/certs
adf540
CERT_NAME="OpenLDAP Server"
adf540
PASSWORD_FILE=
adf540
HOSTNAME_FQDN="$(hostname --fqdn)"
adf540
ALT_NAMES=
adf540
ONCE=0
adf540
adf540
# internals
adf540
adf540
RANDOM_SOURCE=/dev/urandom
adf540
CERT_RANDOM_BYTES=256
adf540
CERT_KEY_TYPE=rsa
adf540
CERT_KEY_SIZE=1024
adf540
CERT_VALID_MONTHS=12
adf540
adf540
# parse arguments
adf540
adf540
usage() {
adf540
	printf "usage: generate-server-cert.sh [-d certdb-dir] [-n cert-name]\n" >&2
adf540
	printf "                               [-p password-file] [-h hostnames]\n" >&2
adf540
	printf "                               [-a dns-alt-names] [-o]\n" >&2
adf540
	exit 1
adf540
}
adf540
adf540
while getopts "d:n:p:h:a:o" opt; do
adf540
	case "$opt" in
adf540
	d)
adf540
		CERTDB_DIR="$OPTARG"
adf540
		;;
adf540
	n)
adf540
		CERT_NAME="$OPTARG"
adf540
		;;
adf540
	p)
adf540
		PASSWORD_FILE="$OPTARG"
adf540
		;;
adf540
	h)
adf540
		HOSTNAME_FQDN="$OPTARG"
adf540
		;;
adf540
	a)
adf540
		ALT_NAMES="$OPTARG"
adf540
		;;
adf540
	o)
adf540
		ONCE=1
adf540
		;;
adf540
	\?)
adf540
		usage
adf540
		;;
adf540
	esac
adf540
done
adf540
adf540
[ "$OPTIND" -le "$#" ] && usage
adf540
adf540
# generated options
adf540
adf540
ONCE_FILE="$CERTDB_DIR/.slapd-leave"
adf540
PASSWORD_FILE="${PASSWORD_FILE:-${CERTDB_DIR}/password}"
adf540
ALT_NAMES="${ALT_NAMES:-${HOSTNAME_FQDN},localhost,localhost.localdomain}"
adf540
adf540
# verify target location
adf540
adf540
if [ "$ONCE" -eq 1 -a -f "$ONCE_FILE" ]; then
adf540
	printf "Skipping certificate generating, '%s' exists.\n" "$ONCE_FILE" >&2
adf540
	exit 0
adf540
fi
adf540
adf540
if ! certutil -d "$CERTDB_DIR" -U &>/dev/null; then
adf540
	printf "Directory '%s' is not a valid certificate database.\n" "$CERTDB_DIR" >&2
adf540
	exit 1
adf540
fi
adf540
adf540
printf "Creating new server certificate in '%s'.\n" "$CERTDB_DIR" >&2
adf540
adf540
if [ ! -r "$PASSWORD_FILE" ]; then
adf540
	printf "Password file '%s' is not readable.\n" "$PASSWORD_FILE" >&2
adf540
	exit 1
adf540
fi
adf540
adf540
if certutil -d "$CERTDB_DIR" -L -a -n "$CERT_NAME" &>/dev/null; then
adf540
	printf "Certificate '%s' already exists in the certificate database.\n" "$CERT_NAME" >&2
adf540
	exit 1
adf540
fi
adf540
adf540
# generate server certificate (self signed)
adf540
adf540
adf540
CERT_RANDOM=$(mktemp --tmpdir=/var/run/openldap)
adf540
dd if=$RANDOM_SOURCE bs=$CERT_RANDOM_BYTES count=1 of=$CERT_RANDOM &>/dev/null
adf540
adf540
certutil -d "$CERTDB_DIR" -f "$PASSWORD_FILE" -z "$CERT_RANDOM" \
adf540
	-S -x -n "$CERT_NAME" \
adf540
	-s "CN=$HOSTNAME_FQDN" \
adf540
	-t TC,, \
adf540
	-k $CERT_KEY_TYPE -g $CERT_KEY_SIZE \
adf540
	-v $CERT_VALID_MONTHS \
adf540
	-8 "$ALT_NAMES" \
adf540
	&>/dev/null
adf540
adf540
rm -f $CERT_RANDOM
adf540
adf540
# tune permissions
adf540
adf540
if [ "$(id -u)" -eq 0 ]; then
adf540
	chgrp ldap "$PASSWORD_FILE"
adf540
	chmod g+r "$PASSWORD_FILE"
adf540
else
adf540
	printf "WARNING: The server requires read permissions on the password file in order to\n" >&2
adf540
	printf "         load it's private key from the certificate database.\n" >&2
adf540
fi
adf540
adf540
touch "$ONCE_FILE"
adf540
exit 0