#!/bin/sh

set -e

verbose=0
create=0
detach=0
nsdir=${XDG_RUNTIME_DIR:-/run/user/$(id -u)}/ns
nstmpl="ns-XXXX"
nsuser="wgs"
nsnet="172.16.0.0/12"

usage() {
	echo "usage: $(basename $0) [-vclk] [-n name] [cmd [arg…]]" >&2
}

logset() {
	[ "${verbose:-0}" -gt 0 ] && printf '[ ] %s ' "$*" >&2 || true
}

logchk() {
	[ "${verbose:-0}" -gt 0 ] && printf '\r[[1;32mx[0m]\n' >&2 || true
}

logdie() {
	[ "${verbose:-0}" -gt 0 ] && printf '\r[[1;31m![0m]\n' >&2
	ns_cleanup $nsname
	exit 1
}

nslist() {
	[ -d /run/netns ] && ls -1 /run/netns
}

nspid() { # 2nd PID is unshare(1)'s first child, aka PID 1
	ip netns pids $nsname|head -n2|tail -n1
}

nscwd() {
	readlink -f /proc/$(nspid)/cwd
}

nsip() {
	ns_exec ip -4 -o addr show "if"|awk '{print $4}'
}

ns_cleanup() {
	nsname="$1"
	if=$(ip -o link show|awk "/netns $nsname/{print \$2}"|cut -d@ -f1)
	if [ -n "$if" ]; then
		net=$(ip -o -4 addr show dev "$if"|awk '{print $4}')
		doas iptables -t nat -D POSTROUTING -s "$net" -j MASQUERADE
	fi
	ip netns pids $nsname | xargs -r kill -9
	doas ip netns del $nsname
	doas umount -Rq $nsdir/$nsname
	rmdir "$nsdir/$nsname"
}

ns_mkfs() {
	dir=$1
	logset "Create rootfs structure"

	mkdir -p $dir
	doas mount -t tmpfs tmpfs "$dir"
	for d in bin etc lib home/$nsuser tmp mnt dev dev/pts dev/shm sys proc; do
		mkdir -p $dir/$d
	done
	for d in null zero full random urandom tty; do
	    touch "$dir/dev/$d"
	    doas mount --bind "/dev/$d" "$dir/dev/$d"
	done
	logchk

	logset "Generate system files"
	cp -r /usr/share/terminfo $dir/etc/
	install -D /etc/ssl/certs/ca-certificates.crt $dir/etc/ssl/certs/ca-certificates.crt
	echo "$nsname" > $dir/etc/hostname
	echo "hosts: files dns" > $dir/etc/nsswitch.conf
	echo "127.0.0.1 localhost" > $dir/etc/hosts
	printf 'nameserver %s\n' $(resolvectl dns|cut -d: -f2)|sort|uniq > $dir/etc/resolv.conf
	printf "export PS1='(%s) $ '" $nsname > $dir/etc/profile
	printf "%s:x:0:0::/home/%s:/bin/sh" $nsuser $nsuser > $dir/etc/passwd
	printf "%s:*::0:::::" $nsuser > $dir/etc/shadow
	printf "world:x:0:%s\n" $nsuser > $dir/etc/group
	ln -s ../proc/mounts "$dir/etc/mtab"
	ln -sf pts/ptmx "$dir/dev/ptmx"
	ln -sf / "$dir/usr"
	logchk

	cat <<-EOF > "$dir/etc/rc"
	#!/bin/sh
	/bin/mount -t sysfs sys /sys
	/bin/mount -t tmpfs tmpfs /dev/shm
	/bin/mount -t devpts devpts /dev/pts -o newinstance,ptmxmode=0666
	/bin/hostname $nsname
	EOF
	chmod +x $dir/etc/rc

	logset "Compile /bin/init"
	cc -x c - -o $dir/bin/init <<-EOF
	#include <signal.h>
	#include <unistd.h>
	#include <sys/wait.h>
	int main() {
	  sigset_t set; int signum;
	  if (getpid()!=1) return 1;
	  sigfillset(&set); sigprocmask(SIG_BLOCK, &set, 0); 
	  if (fork()>0) while(!sigwait(&set, &signum)) wait(NULL);
	  sigprocmask(SIG_UNBLOCK, &set, 0); setsid(); setpgid(0, 0);
	  return execve("/etc/rc", (char *[]){"rc", 0}, (char *[]){0});
	}
	EOF
	logchk
}

ns_install() {
	dir=$1
	logset "Boostrap utilities"
	# https://pubs.opengroup.org/onlinepubs/9799919799/idx/utilities.html
	exe="admin alias ar asa at awk basename batch bc bg c17 cal"
	exe="$exe cat cd cflow chgrp chmod chown cksum cmp comm command"
	exe="$exe compress cp crontab csplit ctags cut cxref date dd"
	exe="$exe delta df diff dirname du echo ed env ex expand expr"
	exe="$exe false fc fg file find fold fuser gencat get getconf"
	exe="$exe getopts gettext grep hash head iconv id ipcrm ipcs"
	exe="$exe jobs join kill lex link ln locale localedef logger"
	exe="$exe logname lp ls m4 mailx make man mesg mkdir mkfifo more"
	exe="$exe msgfmt mv newgrp ngettext nice nl nm nohup od paste"
	exe="$exe patch pathchk pax pr printf prs ps pwd read readlink"
	exe="$exe realpath renice rm rmdel rmdir sact sccs sed sh sleep"
	exe="$exe sort split strings strip stty tabs tail talk tee test"
	exe="$exe time timeout touch tput tr true tsort tty type ulimit"
	exe="$exe umask unalias uname uncompress unexpand unget uniq"
	exe="$exe unlink uucp uudecode uuencode uustat uux val vi wait"
	exe="$exe wc what who write xargs xgettext yacc zcat"

	# Missing from the above list
	exe="$exe mksh bash busybox mount umount mountpoint"
	exe="$exe tar gzip bzip2 xz zstd less"

	# Network utils
	exe="$exe hostname ip ping host dig curl ssh tcpdump"

	which $exe|xargs cp -t $dir/bin
	ldd $(readlink -f `which $exe`) 2>/dev/null \
		|awk '/=>/{print $3}/^\s+\//{print $1}' \
		|sed 's,^/usr,,'|sort|uniq|cpio --quiet -updL $dir
	logchk
}

ns_boot() {
	ns=$1
	logset "Start init process"
	unshare -rf -Uuimpn --mount-proc --kill-child -R $ns -- /bin/init &
	doas ip netns attach $nsname $!

	[ -z "$(nspid)" ] && logdie "Init process failed to start"
	logchk
}

ns_network() {
	ns=$1
	pid=$(nspid)
	nsif="$nsname"
	logset "Setup network"

	# Grab a random prefix from the given netblock to handle
	# communication between host and namespace (through NAT)
	pfx=20
	net=$(ipcalc -nb "$nsnet" $pfx|awk '/Network/{print $2}'|shuf|head -n1)
	min=$(ipcalc -nb $net|awk '/HostMin/{print $2}')
	max=$(ipcalc -nb $net|awk '/HostMax/{print $2}')
	doas ip link add $nsif type veth peer ${nsif}-peer
	doas ip link set $nsif up
	doas ip addr add "$min/$pfx" dev $nsif
	doas sysctl -qw net.ipv4.ip_forward=1
	doas iptables -t nat -A POSTROUTING -s $net -j MASQUERADE

	# Move 2nd pair of veth inside the namespace
	doas ip link set ${nsif}-peer netns $nsname
	nsenter -t "$(nspid)" -r --wd=$ns --preserve-credentials -U -u -p -i -n -m -- env -i /bin/sh <<-EOF
	/bin/ip link set lo up
	/bin/ip link set ${nsif}-peer name if
	/bin/ip link set if up
	/bin/ip address add ${max}/${pfx} dev if
	/bin/ip route add default via ${min}
	EOF
	logchk
}

ns_pop() {
	# Generate random nsname if not provided
	: ${nsname:=ns-$(tr -cd a-z </dev/urandom|dd status=none bs=1 count=5)}

	mkdir -p   "$nsdir/$nsname"
	trap "ns_cleanup $nsname" EXIT INT TERM QUIT

	ns_mkfs    "$nsdir/$nsname"
	ns_install "$nsdir/$nsname"
	ns_boot    "$nsdir/$nsname"
	ns_network "$nsdir/$nsname"
	trap - EXIT
}

ns_exec() {
	pid=$(nspid)
	cwd=$(nscwd)
	nshome=$(head -n1 $cwd/etc/passwd|cut -d: -f 6)
	nsenter -t $pid -r --wd=$cwd --preserve-credentials -U -u -p -i -n -m -- \
		env -i -C $nshome \
			PATH=/bin \
			USER=$nsuser \
			HOME=$nshome \
			TERM="${TERM:-vt100}" \
			LESS="${LESS:-R}" \
			PAGER="${PAGER:-less}" \
			EDITOR="${EDITOR:-vi}" \
			VISUAL="${VISUAL:-vi}" \
			"$@"
}

ns_info() {
	nsname="$1"
	[ -z "$(nspid)" ] && exit 1
	cat <<-EOF
	name: $nsname
	user: $(cut -d: -f1 < $(nscwd)/etc/passwd)
	root: $(nscwd)
	init: $(nspid)
	ipv4: $(nsip)
	EOF
}

while getopts "hvcdli:k:n:u:" OPT; do
	case $OPT in
	d) detach=1;;
	n) nsname="$OPTARG";;
	u) nsuser="$OPTARG";;
	v) verbose=1;;
	c) create=1;; 
	i) ns_info "$OPTARG"; exit 0;;
	k) ns_cleanup "$OPTARG"; exit 0;;
	l) nslist; exit 0;;
	h) usage; exit 0;;
	*) usage; exit 1;;
	esac
done
shift $((OPTIND-1))

# Calling ns(1) without argument will attach to the first available
# namespace. If no namespace is available, a new one will be spun up.
if [ "$create" -eq 1 -o -z "$(nslist)" ]; then
	ns_pop
	[ "$detach" -eq 1 ] && { echo $nsname; exit 0; }
fi

[ -z "$nsname" ] && nsname=$(nslist|head -n1)

# Pop a shell or whatever args provided
ns_exec ${@:-mksh -l}
