#!/bin/sh

# This script will be run by 'mkinitramfs' when it creates the image.
# Its job is to decide which files to install, then install them into
# the staging area, where the initramfs is being created.  This
# happens when a new 'linux-image' package is installed, or when the
# administrator runs 'update-initramfs' by hand to update an initramfs
# image.

# The environment contains at least:
#
#  DESTDIR -- The staging directory where the image is being built.

# No initramfs pre-requirements
PREREQ="cryptroot"

prereqs()
{
	echo "$PREREQ"
}

case $1 in
# get pre-requisites
prereqs)
	prereqs
	exit 0
	;;
esac

. /usr/share/initramfs-tools/hook-functions

for d in /usr /usr/local; do
    if [ -d "$d"/lib/mandos ]; then
	prefix="$d"
	break
    fi
done
if [ -z "$prefix" ]; then
    # Mandos not found
    exit 1
fi

for d in /etc/keys/mandos /etc/mandos/keys; do
    if [ -d "$d" ]; then
	keydir="$d"
	break
    fi
done
if [ -z "$keydir" ]; then
    # Mandos key directory not found
    exit 1
fi

set `{ getent passwd _mandos \
    || getent passwd nobody \
    || echo ::65534:65534:::; } \
    | cut --delimiter=: --fields=3,4 --only-delimited \
    --output-delimiter=" "`
mandos_user="$1"
mandos_group="$2"

# The Mandos network client uses the network
auto_add_modules net
# The Mandos network client uses IPv6
force_load ipv6

# These are directories inside the initrd
CONFDIR="/conf/conf.d/mandos"
MANDOSDIR="/lib/mandos"
PLUGINDIR="${MANDOSDIR}/plugins.d"

# Make directories
install --directory --mode=u=rwx,go=rx "${DESTDIR}${CONFDIR}" \
	"${DESTDIR}${MANDOSDIR}"
install --owner=${mandos_user} --group=${mandos_group} --directory \
    --mode=u=rwx "${DESTDIR}${PLUGINDIR}"

# Copy the Mandos plugin runner
copy_exec "$prefix"/lib/mandos/plugin-runner "${MANDOSDIR}"

# Copy the plugins

# Copy the packaged plugins
for file in "$prefix"/lib/mandos/plugins.d/*; do
    base="`basename \"$file\"`"
    # Is this plugin overridden?
    if [ -e "/etc/mandos/plugins.d/$base" ]; then
	continue
    fi
    case "$base" in
	*~|.*|\#*\#|*.dpkg-old|*.dpkg-bak|*.dpkg-new|*.dpkg-divert)
	    : ;;
	"*") echo "W: Mandos client plugin directory is empty." >&2 ;;
	*) copy_exec "$file" "${PLUGINDIR}" ;;
    esac
done

# Copy any user-supplied plugins
for file in /etc/mandos/plugins.d/*; do
    base="`basename \"$file\"`"
    case "$base" in
	*~|.*|\#*\#|*.dpkg-old|*.dpkg-bak|*.dpkg-new|*.dpkg-divert)
	    : ;;
	"*") : ;;
	*) copy_exec "$file" "${PLUGINDIR}" ;;
    esac
done

# GPGME needs /usr/bin/gpg
if [ ! -e "${DESTDIR}/usr/bin/gpg" \
    -a -n "`ls \"${DESTDIR}\"/usr/lib/libgpgme.so* \
		2>/dev/null`" ]; then
    copy_exec /usr/bin/gpg
fi

# Config files
for file in /etc/mandos/plugin-runner.conf; do
    if [ -d "$file" ]; then
	continue
    fi
    cp --archive --sparse=always "$file" "${DESTDIR}${CONFDIR}"
done

if [ ${mandos_user} != 65534 ]; then
    sed --in-place --expression="1i--userid=${mandos_user}" \
	"${DESTDIR}${CONFDIR}/plugin-runner.conf"
fi

if [ ${mandos_group} != 65534 ]; then
    sed --in-place --expression="1i--groupid=${mandos_group}" \
	"${DESTDIR}${CONFDIR}/plugin-runner.conf"
fi

# Key files 
for file in "$keydir"/*; do
    if [ -d "$file" ]; then
	continue
    fi
    cp --archive --sparse=always "$file" "${DESTDIR}${CONFDIR}"
    chown ${mandos_user}:${mandos_group} \
	"${DESTDIR}${CONFDIR}/`basename \"$file\"`"
done

# /lib/mandos/plugin-runner will drop priviliges, but needs access to
# its plugin directory and its config file.  However, since almost all
# files in initrd have been created with umask 027, this opening of
# permissions is needed.
# 
# (The umask is not really intended to affect the files inside the
# initrd; it is intended to affect the initrd.img file itself, since
# it now contains secret key files.  There is, however, no other way
# to set the permission of the initrd.img file without a race
# condition.  This umask is set by "initramfs-tools-hook-conf",
# installed as "/usr/share/initramfs-tools/conf-hooks.d/mandos".)
# 
for full in "${MANDOSDIR}" "${CONFDIR}"; do
    while [ "$full" != "/" ]; do
	chmod a+rX "${DESTDIR}$full"
	full="`dirname \"$full\"`"
    done
done

# Reset some other things to sane permissions which we have
# inadvertently affected with our umask setting.
for dir in / /bin /etc /keyscripts /sbin /scripts /usr /usr/bin; do
    if [ -d "${DESTDIR}$dir" ]; then
	chmod a+rX "${DESTDIR}$dir"
    fi
done
for dir in "${DESTDIR}"/lib* "${DESTDIR}"/usr/lib*; do
    if [ -d "$dir" ]; then
	find "$dir" \! -perm -u+rw,g+r -prune -or -print0 \
	    | xargs --null --no-run-if-empty chmod a+rX
    fi
done
