#!/bin/bash
# Created by Ben Okopnik on Thu Mar 22 22:50:21 CDT 2007

[ "$UID" -eq 0 ] || { echo "You need to run this as root."; exit 1; }

# If a number has been specified as a command-line arg, use it; otherwise,
# create 20 accounts.
if [ -n "$1" ]
then
    if ! [[ "$1" =~ ^[0-9]+$ ]] || [ "$1" -le 0 -o "$1" -gt 100 ]
    then
        echo "If used, the # of accounts to create must be 1-100. Exiting..."
        exit 1
    fi
fi

# Default to 20 accounts unless some other number has been specified
number=${1:-20}

source .chrootrc

echo "Creating the basic dir structure"
mkdir -p $dir/{bin,dev,etc,lib,proc,tmp,var}
mkdir -p $dir/usr/{bin,lib,local,sbin,share}
mkdir -p $dir/usr/local/share
mkdir -p $dir/var/log

echo "Creating devices in $dir/dev"
mkdir $dir/dev/pts
mknod -m 666 $dir/dev/null c 1 3
mknod -m 666 $dir/dev/zero c 1 5
mknod -m 666 $dir/dev/full c 1 7
mknod -m 655 $dir/dev/urandom c 1 9
mknod -m 666 $dir/dev/ptyp0 c 2 0
mknod -m 666 $dir/dev/ptyp1 c 2 1
mknod -m 666 $dir/dev/ptyp2 c 2 2
mknod -m 666 $dir/dev/ptyp3 c 2 3
mknod -m 666 $dir/dev/ttyp0 c 3 0
mknod -m 666 $dir/dev/ttyp1 c 3 1
mknod -m 666 $dir/dev/ttyp2 c 3 2
mknod -m 666 $dir/dev/ttyp3 c 3 3
mknod -m 666 $dir/dev/tty c 5 0
mknod -m 666 $dir/dev/ptmx c 5 2

# Create the 'lastlog' file
touch $dir/var/log/lastlog

echo "Copying the basic toolkit [this takes a while]"
cp -a /etc/ssh $dir/etc/ssh
# cp -a /bin/{bash,cat,chmod,cp,date,ln,ls,more,mv,rm} $dir/bin
# cp -a /usr/bin/{clear,env,groups,id,last,perl,perldoc,a2p,pod2man,cpan,splain} $dir/usr/bin
cp -a /usr/sbin/sshd $dir/usr/sbin/
cp -a /usr/lib/perl* $dir/usr/lib
cp -a /usr/lib/man-db $dir/usr/lib
cp -a /usr/share/perl* $dir/usr/share
cp -a /usr/local/share/perl* $dir/usr/local/share
ln -s $dir/bin/bash $dir/bin/sh
echo "echo chroot$dir" > $dir/bin/hostname
chmod +x $dir/bin/hostname

echo "Installing the required libs for the toolkit progs"
# Different versions of 'ldd' produce several variations in output.
# Debian's includes the paths to the libraries, so deciding what to copy
# where is easy; if yours does not, you'll have to check if it exists in
# the standard paths and copy it to the appropriate place in the chroot
# tree.
for lib in `ldd $dir/bin/* $dir/usr/bin/* $dir/usr/sbin/*|\
	perl -walne'print $1 if m#(\S*/lib\S+)#'|sort -u`
do
	# Extract the original lib directory
	d=${lib%/*}
	# Cut the leading / from the above
	ld=${d#/}
	# Check if the dir exists in the chroot and create one if not
	[ -d "$ld" ] || mkdir -p $dir/$ld
	[ -e ${lib#/} ] || cp $lib $dir/$ld
done

# Create nsswitch.conf for SSH...
for n in passwd group shadow networks protocols services ethers rpc
do
	printf "%-15s%s\n" $n: files >> $dir/etc/nsswitch.conf
done	
printf "%-15s%s\n" hosts: "files dns" >> $dir/etc/nsswitch.conf

# ...and copy the appropriate libs
cp /lib/libnss_{files,dns}.so.2 $dir/lib

echo "Creating the user files"
cat <<! > $dir/etc/profile
PATH=/bin:/usr/bin:/sbin:/usr/sbin
PS1='\$USER@\`hostname\`:\$PWD\\$ '
export PATH PS1
umask 0022
!

cat <<! > $dir/etc/passwd
root:x:0:0:Admin user:/:/bin/bash
sshd:x:111:65534::/var/run/sshd:/bin/false
nobody:x:65534:65534:nobody:/nonexistent:/bin/sh
!

cat <<! > $dir/etc/group
shadow:x:42:
utmp:x:43:
ssh:x:109:
nogroup:x:65534:
!

cat <<! > $dir/etc/shadow
root:!:14384:0:99999:7:::
nobody:!:13592:0:99999:7:::
sshd:!:13592:0:99999:7:::
!

# Note: users' UIDs will start at 1001; the first account manually added
# after this (using 'chroot-add-user') will have a UID of 1000; all
# subsequent UIDs will go up from the max user UID. This places the
# teacher's account near the top of the passwd file, which makes it easier
# to edit.
for n in `seq $number`
do
	# Create the home directory
	mkdir -p $dir/home/student$n; chown $n:$n $dir/home/student$n
	# Populate the ~/.bash_profile
	echo -e "PATH=/bin:/sbin:/usr/bin:/usr/sbin\nLANG=C\nLC_ALL=C\nPAGER=/usr/bin/less\nexport PATH LANG LC_ALL PAGER" > $dir/home/student$n/.bash_profile

	# Add reasonable entries to /etc/{passwd,group,shadow}
	N=$((1000 + $n))
	echo "student$n:x:$N:$N:Student account:/home/student$n:/bin/bash" >> $dir/etc/passwd
	echo "student$n:x:$N:" >> $dir/etc/group
	# Obvious password for students
	echo "student$n:\$6\$dZ3bUzrS\$dltz7ogy5QMS4glTilclPGBh7ots09Sgs5KroTa0EucLnhsmSIHBJU5coCnUw1hJI2WQlhc7/kaIGXJH90i3m0:14384:0:99999:7:::" >> $dir/etc/shadow
done

chmod 600 $dir/etc/shadow

echo "Configuring the SSH environment"
# Change the IP, the PAM, and the PrivSep notes in etc/ssh/sshd_config
sed -i -e "s/#ListenAddress 0.0.0.0/ListenAddress $ip/" -e "s/UsePAM yes/UsePAM no/" \
	-e "s/UsePrivilegeSeparation yes/UsePrivilegeSeparation no/" $dir/etc/ssh/sshd_config

ifconfig eth0:chroot $ip down
ifconfig eth0:chroot $ip netmask $netmask
[ -z "`mount | grep $dir`" ] && {
	mount -tproc proc $dir/proc
	mount -tdevpts devpts $dir/dev/pts 
}

