dotfiles/.local/bin/chroot-gen

59 lines
1.6 KiB
Plaintext
Raw Permalink Normal View History

2023-12-14 08:27:07 +00:00
#!/bin/bash
# Adapted from LinuxConfig.org
# GNU GPL v3.0+
2024-08-05 20:46:41 +00:00
# cmds build into bash: echo, cd
2023-12-14 09:52:57 +00:00
CMDS="bash clear mkdir touch mv rm ls grep cat vim"
2023-12-14 08:27:07 +00:00
USER=$1
2023-12-14 08:50:20 +00:00
CMDS=($CMDS) # convert to array
2023-12-14 09:06:15 +00:00
# Create user if not exist
2023-12-14 08:27:07 +00:00
if ! id "$USER" 2&>/dev/null; then
echo 'INFO: User not found'
echo 'Creating...'
2024-08-05 21:01:39 +00:00
useradd --create-home --shell /usr/bin/bash $USER
2023-12-14 09:06:15 +00:00
echo "Password for $USER:"
passwd $USER
2023-12-14 08:27:07 +00:00
fi
2023-12-14 09:06:15 +00:00
CHROOT=$(eval echo ~$USER)
2023-12-14 09:45:44 +00:00
mkdir -p $CHROOT$CHROOT # make new home dir
chown $USER:$USER $CHROOT$CHROOT
2023-12-14 09:52:57 +00:00
echo "PS1='\[\e[38;5;202m\][\[\e[38;5;45m\]\h\[\e[38;5;202m\]:\[\e[38;5;40m\]\W\[\e[38;5;202m\]]\\$ \[\e[0m\]'" > $CHROOT$CHROOT/.bashrc
2023-12-14 09:45:44 +00:00
2023-12-14 09:31:31 +00:00
# ChrootDirectory requires these permissions
chown root:root $CHROOT
chmod 745 $CHROOT
2023-12-14 09:15:12 +00:00
printf "Match User $USER\n ChrootDirectory $CHROOT" > /etc/ssh/sshd_config.d/70-$USER.conf
2023-12-14 09:06:15 +00:00
2023-12-14 09:45:44 +00:00
# cp user and group into chroot
mkdir -p $CHROOT/etc/
cat /etc/passwd | grep $USER > $CHROOT/etc/passwd
cat /etc/group | grep $USER > $CHROOT/etc/group
2023-12-14 08:27:07 +00:00
# cp in all commands and dependencies
for cmd in "${CMDS[@]}"; do
for dep in $( ldd $(which $cmd) | grep -v dynamic | cut -d " " -f 3 | sed 's/://' | sort | uniq ); do
cp --parents $dep $CHROOT
done
cp --parents $(which $cmd) $CHROOT
done
SHELL=$(cat /etc/passwd | grep $USER | tr ":" "\n" | tail -n 1)
if [ ! -f $CHROOT$SHELL ]; then
2023-12-14 09:36:15 +00:00
echo "WARN: You didn't add the shell specified in /etc/passwd for \"$USER\": ($SHELL)"
2023-12-14 08:27:07 +00:00
fi
# ARCH amd64
if [ -f /lib64/ld-linux-x86-64.so.2 ]; then
cp --parents /lib64/ld-linux-x86-64.so.2 /$CHROOT
fi
# ARCH i386
if [ -f /lib/ld-linux.so.2 ]; then
cp --parents /lib/ld-linux.so.2 /$CHROOT
fi
2023-12-14 09:32:30 +00:00
systemctl restart sshd