#!/bin/bash
#
# Wrapper around OpenSSL for a PKINIT-compatible CA.
#
# Wrapper around OpenSSL operations for a CA that issues tickets with the
# proper extensions for PKINIT.  This assumes a local CA on a Kerberos KDC
# machine and hardcodes the paths that are used by the Puppet configuration.
#
# Written by Russ Allbery <eagle@eyrie.org>
# Copyright 2015, 2017 Russ Allbery <eagle@eyrie.org>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

set -e

# Keep things from being world-readable by default.
umask 077

# Ensure we have a sane PATH.
export PATH=/bin:/usr/bin

# Where the OpenSSL extension configurations are located.
extpath=/etc/krb5kdc

# Where the root CA is located.
capath=/etc/krb5kdc/ca

# Where the Kerberos KDC certificate should be stored.
kdcpath=/var/lib/krb5kdc

# Local Kerberos realm.
REALM=EYRIE.ORG

# Do the work.
case "$1" in
    (init)
        mkdir -p "$capath"
        chown root:root "$capath"
        chmod 700 "$capath"
        openssl genrsa -out "$capath"/cakey.pem 2048
        openssl req -key "$capath"/cakey.pem -new -x509 \
            -out "$capath"/cacert.pem -subj "/CN=$REALM CA" -days 3650
        ;;

    (issue-kdc)
        mkdir -p "$kdcpath"
        chown root:root "$kdcpath"
        chmod 700 "$kdcpath"
        openssl genrsa -out "$kdcpath"/kdckey.pem 2048
        openssl req -new -out "$kdcpath"/kdc.req -key "$kdcpath"/kdckey.pem \
            -subj "/CN=$REALM" 
        REALM="$REALM" openssl x509 -req -in "$kdcpath"/kdc.req         \
            -CAkey "$capath"/cakey.pem -CA "$capath"/cacert.pem         \
            -out "$kdcpath"/kdc.pem -days 365                           \
            -extfile "$extpath"/extensions.kdc -extensions kdc_cert     \
            -CAcreateserial
        rm "$kdcpath"/kdc.req
        ;;

    (issue-client)
        if [ -z "$2" ]; then
            echo 'No client principal to generate given' >&2
            exit 1
        fi
        if [[ "$2" =~ '@' ]]; then
            echo 'Do not include the realm in the client principal' >&2
            exit 1
        fi
        openssl genrsa -out clientkey.pem 2048
        openssl req -new -key clientkey.pem -out client.req \
                -subj "/CN=$2@$REALM"
        REALM="$REALM" CLIENT="$2" openssl x509                             \
            -CAkey "$capath"/cakey.pem -CA "$capath"/cacert.pem -req        \
            -in client.req -extensions client_cert                          \
            -extfile "$extpath"/extensions.client -days 365 -out client.pem
        rm client.req
        ;;

    (help|--help)
        cat <<EOF
Usage: pkinit-ca init
       pkinit-ca issue-kdc
       pkinit-ca issue-client <principal>

Issues a certificate suitable for use with PKINIT.  The init action creates
the root CA certificate, the issue-kdc action creates a certificate suitable
for use by the Kerberos KDC, and the issue-client action creates a client
certificate tied to the given Kerberos principal.

The generated certificate and key will be put in the standard system
locations for init and issue-kdc (/etc/krb5kdc/ca and /var/lib/krb5kdc,
respectively).  For issue-client, both will be written to the current working
directory, as client.pem and clientkey.pem.
EOF
        exit 0
        ;;

    (*)
        echo "Unknown operation: $1" >&2
        exit 1
        ;;
esac
exit 0

# Documentation.  Use a hack to hide this from the shell.  Because of the
# above exit line, this should never be executed.
DOCS=<<__END_OF_DOCS__

=for stopwords
Allbery KDC OpenSSL PKINIT hardcodes issue-kdc pkinit-ca

=head1 NAME

pkinit-ca - Wrapper around OpenSSL for a PKINIT-compatible CA

=head1 SYNOPSIS

B<pkinit-ca> help

B<pkinit-ca> [B<--help>] init

B<pkinit-ca> issue-kdc

B<pkinit-ca> issue-client I<principal>

=head1 DESCRIPTION

Wrapper around OpenSSL operations for a CA that issues tickets with the proper
extensions for PKINIT.  This assumes a local CA on a Kerberos KDC machine and
hardcodes the paths that are used by the Puppet configuration.

This script will normally have to be run as root so that it can put files in
the appropriate paths.

Supported operations:

=over 4

=item init

Create the initial CA for the given realm and store the certificate in
F</etc/krb5kdc/ca>.  The resulting F<cacert.pem> file will need to be put on
all clients, normally by loading it into Puppet.

=item issue-kdc

Creates a certificate for a Kerberos KDC in the local realm, storing it in
F<kdc.pem> in F</var/lib/krb5kdc>, with a key stored in F<kdckey.pem> in the
same directory.

=item issue-client

Creates a certificate for a client principal, given as the first argument.
Stores the certificate as F<client.pem> and the key as F<clientkey.pem> in the
current directory.

=back

=head1 OPTIONS

Running C<pkinit-ca --help> or C<pkinit-ca help> will show a summary of the
usage.

=head1 BUGS

The realm of C<EYRIE.ORG> is currently hard-coded in the script.

=head1 AUTHOR

Russ Allbery <eagle@eyrie.org>

=head1 SEE ALSO

openssl(1), kdc.conf(5)

=cut

__END_OF_DOCS__
