#!/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.5 2003/02/16 20:21:28 babolo Exp $
genclass=tproxy
suffix=
inclass=tproxy
. ${JAILUP_CONF}

if  [ -z "${1}" ]; then
    ${cat} << EOF
#     Usage:
# jailup tproxy dir jailname jailaddr uid gid proxyhost proxyport [port]
#     where dir relative to ${JAILUPDIR} and ${JAILRODIR}
#     default bind port is 80
# tproxy from ports/www/transproxy used
EOF
    exit
fi

jdir="${1}"
jailsname="${2}"
jailsaddr="${3}"
user="${4:-www}"
jgid="${5:-www}"
pxhost="${6}"
pxport="${7}"
port="${8:-80}"
main_dir="${JAILUPDIR}/${jdir}"
conf_dir="${JAILRODIR}/${jdir}"
umask 022

${mkdir} "${main_dir}" "${conf_dir}"
${up_mdir} "${main_dir}" ${MTREE}

${up_data} "${conf_dir}/" /etc/ << "EOF"
    host.conf
    hosts
    resolv.conf
    localtime
EOF

${up_shlib} "${main_dir}/usr/lib" << "EOF"
    c
EOF

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

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

if  [ ! -e "${conf_dir}/master.template" ]; then
    ${cat} << "EOF" > "${conf_dir}/master.template"
root:*:0:0::0:0:Charlie &:/:/nonexistent
nobody:*:65534:65534::0:0:Unprivileged user:/nonexistent:/nonexistent
www:*:80:80::0:0:WWW:/www:/nonexistent
EOF
fi

if  [ ! -e "${conf_dir}/master.passwd" ]; then
    ${cp} -p "${conf_dir}/master.template" "${conf_dir}/master.passwd"
fi
      ####                             ####
      #### 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
    resolv.conf
    localtime
    master.passwd	mode=0600
    master.template	mode=0600
..
EOF
${pwd_mkdb} -d "${conf_dir}" -p "${conf_dir}/master.passwd"

${cat} << EOF >> "${genconfig}"
#${genclass}	${inclass}	${jdir}	${jailsname}	${jailsaddr}	0	${user}	${jgid}	${pxhost}	${pxport}	${port}
EOF

      #####################################
      ####                             ####
      #### Begin of fstab mod          ####
      ####                             ####
${cat} << EOF >> "${etcdir}/fstab"
#${conf_dir}		${main_dir}/etc		null	ro,nodev,noexec	0 0
EOF
      ####                             ####
      #### End of fstab mod            ####
      ####                             ####
      #####################################
