#!/bin/sh
# postinstall for OZSWopenssh
# Creates sshd user/group, imports SMF manifest, generates host keys.

FMRI="svc:/ozone/sshd:default"
SMF_SRC="/opt/ozsw/share/smf/ozone-sshd.xml"
MANIFEST="/var/svc/manifest/ozone/sshd.xml"
SSHDIR="/etc/opt/ozsw/openssh"
KEYGEN="/opt/ozsw/bin/ssh-keygen"

# Create sshd privsep group and user (UID/GID 43 matches Solaris convention)
getent group sshd >/dev/null 2>&1  || groupadd -g 43 sshd
getent passwd sshd >/dev/null 2>&1 || useradd -u 43 -g 43 -c "SSH privsep" \
    -d /var/empty -s /bin/false sshd

# Ensure config directory exists and a default sshd_config is in place
if [ ! -d "$SSHDIR" ]; then
    mkdir -p "$SSHDIR"
fi
if [ ! -f "$SSHDIR/sshd_config" ] && \
   [ -f /opt/ozsw/etc/openssh/sshd_config.default ]; then
    cp /opt/ozsw/etc/openssh/sshd_config.default "$SSHDIR/sshd_config"
fi

# Generate host keys
if [ -x "$KEYGEN" ]; then
    [ -f "$SSHDIR/ssh_host_rsa_key" ]     || \
        "$KEYGEN" -f "$SSHDIR/ssh_host_rsa_key"     -t rsa     -N ''
    [ -f "$SSHDIR/ssh_host_ed25519_key" ] || \
        "$KEYGEN" -f "$SSHDIR/ssh_host_ed25519_key" -t ed25519 -N ''
fi

# Install and import SMF manifest (service starts disabled by default)
if [ -x /usr/sbin/svccfg ] && [ -f "$SMF_SRC" ]; then
    mkdir -p /var/svc/manifest/ozone
    cp "$SMF_SRC" "$MANIFEST"
    /usr/sbin/svccfg import "$MANIFEST"
fi

exit 0
