#!/bin/sh

COMMANDS="[ a2p addr2line apply apropos asa awk b64decode b64encode banner basename bc bdes c2ph cal calendar captoinfo cat chgrp chmod cksum clear cmp co col colrm column comm compress cp cpio csplit ctags cut date dc dd deroff diff diff3 dirname du echo ed egrep enc2xs encrypt env ex expand expr false fgrep file file2c find fmt fold fpr fsplit fstat ftp gencat getcap getconf getent getopt gprof grep groups gunzip gzcat gzexe gzip gzsig h2ph h2xs head help hexdump htpasswd ident indent info infocmp infokey infotocap join jot keynote kill lam leave less lesskey lex ln lndir locate lock logname look lorder ls m4 make makeinfo md5 merge mesg mg mkdep mkdir mklocale mktemp more mv nice od page pagesize passwd paste patch pax pgrep pkill pod2html pod2latex pod2man pod2text pod2usage podchecker podselect pr printenv printf psed pwd quota readlink renice reset rev rksh rm rmdir rpcgen rs s2p scp script sdiff sed sftp sha1 sha256 shar size sleep slogin sort spell split sqlite3 ssh ssh-add ssh-agent ssh-keygen ssh-keyscan stat strings sum sync tail tar tee telnet test texi2dvi texi2pdf texindex tftp time tmux touch tr true tset tsort ul uncompress unexpand unifdef uniq units unvis uudecode uuencode vi view vis wc what whatis whereis which whois xargs yacc yes zcat zcmp zdiff zegrep zfgrep zforce zgrep zless zmore znew mysql"

LOCALCMDS="irssi"

#############################################################################################################

echo 'creating devices and mandatory directories'
rm -rf /chroot/dev /chroot/usr/share/misc /chroot/etc /chroot/usr/libdata/perl5
mkdir -p /chroot/dev /chroot/tmp /chroot/var/run /chroot/usr/share/misc /chroot/etc /chroot/etc
cd /chroot/dev
/dev/MAKEDEV std ptm pty
rm console klog kmem ksyms mem xf86
chmod 2777 /chroot/tmp
mkdir -p /chroot/var/run
cp -fp /var/run/dev.db /chroot/var/run
cp -fp /usr/share/misc/terminfo.db /chroot/usr/share/misc
cp -fp /etc/resolv.conf /etc/services /etc/hosts /etc/localtime /etc/pwd.db /chroot/etc
cp -rfp /usr/libdata/perl5 /chroot/usr/libdata/
cp -fp /etc/motd /chroot/etc/

#############################################################################################################

echo 'creating binaries directories'
rm -rf /chroot/bin /chroot/usr/local/bin
mkdir -p /chroot/bin /chroot/usr/local/bin
for COM in ${COMMANDS}
do
	cp -p `which ${COM}` /chroot/bin
done
cd /chroot/bin
rm -f sh
ln -s rksh sh
for COM in ${LOCALCMDS}
do
	cp -p `which ${COM}` /chroot/usr/local/bin
done

#############################################################################################################

echo 'creating libraries directories'
rm -rf /chroot/usr/lib /chroot/usr/local/lib /chroot/usr/libexec /chroot/usr/local/libexec
mkdir -p /chroot/usr/lib /chroot/usr/local/lib /chroot/usr/libexec /chroot/usr/local/libexec
for CMD in /chroot/bin/* /chroot/usr/local/bin/*; do ldd ${CMD} |grep \\.so; done 2>/dev/null |awk '{print $7}'|sort -u | while read LIB
do
	cp -p ${LIB} /chroot${LIB}
done

for USER in `grep ^chroot /etc/group|awk -F ':' '{print $4}'|sed 's/,/ /g'`
do
	HM=/chroot/`grep ^${USER}: /etc/passwd | awk -F ':' '{print $6}'`
	chpass -s /bin/rksh ${USER}
	mkdir -p ${HM}/.ssh
	touch ${HM}/.ssh/authorized_keys
	chown ${USER}:users ${HM}
	rm -f ${HM}/.profile
	echo "
PATH=/bin:/usr/local/bin
export PATH HOME TERM
" > ${HM}/.profile
	chown root:wheel ${HM}/.profile
	chmod 444 ${HM}/.profile
	chmod 700 ${HM}
	echo "Restrictions set for ${USER}"
done

#############################################################################################################


