#!/bin/sh -e
# Copyright @BABOLO (C)2002,2003
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# $Id: up,v 1.10 2003/04/30 00:40:22 babolo Exp $
genclass=sshd
suffix=
inclass=${genclass}
. ${JAILUP_CONF}
keygen=/usr/bin/ssh-keygen
mount=/sbin/mount
kind=`${dirname} "${0}"`
kind=`${basename} "${kind}"`
t=`${basename} "${0}"`
if  [ "${t}" != "${t#*-}" ]; then
    kind="${kind}/-${t#*-}"
fi

if  [ "$#" -lt 3 ]; then
    ${cat} << EOF
# jailup ${kind} @BABOLO V.M %%VERS%%  %%DATE%%
#     Usage:
# jailup sshd dir jailname jailaddr dev
#     where jailname - hostname for jail (see jail(2) hostname)
#           jailaddr - IP addr for jail (see jail(2) ip_number)
#           dev - one of jailup devfull devces
#           dir relative to ${JAILUPDIR} and ${JAILRODIR}
# system sshd used
EOF
    exit
fi

jdir="${1}"
jailsname="${2}"
jailsaddr="${3}"
user=root
group=wheel
port=22
dev="${4:-vn7sXx}"
main_dir="${JAILUPDIR}/${jdir}"
conf_dir="${JAILRODIR}/${jdir}"
umask 022

${mkdir} "${main_dir}" "${conf_dir}/ssh"
${up_mdir} "${main_dir}" ${MTREE}
echo "localtime" | ${up_data} "${conf_dir}/" /etc/

${up_conf} "${conf_dir}/" /etc/ << "EOF"
    host.conf
    hosts
    protocols
    resolv.conf
EOF

${up_shlib} "${main_dir}/usr/lib" << "EOF"
    opie md ssh crypt crypto util z wrap pam c skey
    /usr/lib/pam_opie.so
    /usr/lib/pam_skey.so
    /usr/lib/pam_ssh.so
    /usr/lib/pam_unix.so
    /usr/lib/pam_permit.so
EOF

${up_libexe} "${main_dir}/usr/libexec" << EOF
    ld-elf.so.1
    sshd
EOF

      #####################################
      ####                             ####
      #### Begin of etc fill           ####
      ####                             ####
if  [ ! -e "${conf_dir}/group" ]; then
    ${cat} << "EOF" > "${conf_dir}/group"
wheel:*:0:
sshd:*:22:
nogroup:*:65533:
nobody:*:65534:
EOF
fi

if  [ ! -e "${conf_dir}/master.template" ]; then
    ${cat} << "EOF" > "${conf_dir}/master.template"
root:*:0:0::0:0:Charlie &:/:/nonexistent
sshd:*:22:22::0:0:Secure Shell Daemon:/var/empty:/nonexistent
nobody:*:65534:65534::0:0:Unprivileged user:/nonexistent:/nonexistent
EOF
fi

if  [ ! -e "${conf_dir}/master.passwd" ]; then
    ${cp} -p "${conf_dir}/master.template" "${conf_dir}/master.passwd"
fi

cf="sshd_config"; if  [ -e "${conf_dir}/ssh/${cf}" ]; then cf="${cf}.sample"; fi
${cp} "${TEMPLATE}" "${conf_dir}/ssh/${cf}"
cm="pam.conf"; if  [ -e "${conf_dir}/${cm}" ]; then cm="${cm}.sample"; fi
${cp} "${TEMPLATE}-pam" "${conf_dir}/${cm}"

      ####                             ####
      #### End of etc fill             ####
      ####                             ####
      #####################################

${mtree} -Ueqxp "${main_dir}" -f ${MTREE}
${mtree} -Ueqxp "${conf_dir}" << "EOF"
/set type=file uname=root gname=wheel mode=0644 flags=none
.			type=dir mode=0755
    group
    host.conf
    hosts
    localtime
    protocols
    resolv.conf
    master.passwd	mode=0600
    ssh			type=dir mode=0755
        sshd_config
    ..
..
EOF
${pwd_mkdb} -d "${conf_dir}" -p "${conf_dir}/master.passwd"
[ ! -e "${conf_dir}/ssh/ssh_host_key" ] && ${keygen} -t rsa1 -N "" -f "${conf_dir}/ssh/ssh_host_key"
[ ! -e "${conf_dir}/ssh/ssh_host_rsa_key" ] && ${keygen} -t rsa -N "" -f "${conf_dir}/ssh/ssh_host_rsa_key"
[ ! -e "${conf_dir}/ssh/ssh_host_dsa_key" ] && ${keygen} -t dsa -N "" -f "${conf_dir}/ssh/ssh_host_dsa_key"

${cat} << EOF >> "${genconfig}"
#${genclass}	${inclass}	${jdir}	${jailsname}	${jailsaddr}	0	${user}	${group}	22
EOF
      #####################################
      ####                             ####
      #### Begin of fstab mod          ####
      ####                             ####
${cat} << EOF >> "${etcdir}/fstab"
#/dev/${dev}		${main_dir}/dev	ufs	rw,noexec,noauto 0 0
#${conf_dir}		${main_dir}/etc	null	ro,nodev,noexec	0 0
EOF

echo "#${mount} \"${main_dir}/dev\"" >> "${etcdir}/rc.local"

      ####                             ####
      #### End of fstab mod            ####
      ####                             ####
      #####################################
