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

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