From 724f488ab70598f112ae6e74f62e347b35b0ed29 Mon Sep 17 00:00:00 2001 From: deajan Date: Tue, 7 Feb 2017 20:59:01 +0100 Subject: [PATCH] Quick and dirty sudo prepare script --- dev/tests/sudo_prep.sh | 113 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100755 dev/tests/sudo_prep.sh diff --git a/dev/tests/sudo_prep.sh b/dev/tests/sudo_prep.sh new file mode 100755 index 0000000..18ea5ec --- /dev/null +++ b/dev/tests/sudo_prep.sh @@ -0,0 +1,113 @@ +#!/usr/bin/env bash + +testUser=osyncsudo +testUserHome=/home/osyncsudo + +function SetupSSH { + local remoteUser="${1}" + local homedir="${2}" + + if [ ! -d "$homedir/.ssh" ]; then + mkdir "$homedir/.ssh" + if [ $? != 0 ]; then + echo "Cannot create [$homedir/.ssh]." + exit 1 + fi + fi + + chmod 700 "$homedir/.ssh" + if [ $? != 0 ]; then + echo "Cannot chmod [$homedir/.ssh]." + exit 1 + fi + + chown $testUser "$homedir/.ssh" + if [ $? != 0 ]; then + echo "Cannot chown [$homedir/.ssh]." + exit 1 + fi + + echo -e 'y\n'| ssh-keygen -t rsa -b 2048 -N "" -f "$homedir/.ssh/id_rsa_local" + if ! grep "$(cat $homedir/.ssh/id_rsa_local.pub)" "$homedir/.ssh/authorized_keys"; then + cat "$homedir/.ssh/id_rsa_local.pub" >> "$homedir/.ssh/authorized_keys" + fi + chmod 600 "$homedir/.ssh/authorized_keys" + chown $remoteUser "$homedir/.ssh/authorized_keys" + chown $remoteUser "$homedir/.ssh/id_rsa_local" + chown $remoteUser "$homedir/.ssh/id_rsa_local.pub" + + + # Add localhost to known hosts so self connect works + if [ -z "$(ssh-keygen -F localhost)" ]; then + ssh-keyscan -H localhost >> "$homedir/.ssh/known_hosts" + fi + + if [ -f "$homedir/.ssh/known_hosts" ]; then + chown $remoteUser "$homedir/.ssh/known_hosts" + fi +} + +function PrepareSudoers { + local remoteUser="${1}" + + if [ -f "/etc/sudoers" ]; then + echo "$remoteUser ALL=NOPASSWD:SETENV:/usr/bin/rsync,/usr/bin/bash" >> "/etc/sudoers" + echo "Defaults:$remoteUser !requiretty" >> "/etc/sudoers" + elif [ -f "/usr/local/bin/sudoers" ]; then + echo "$remoteUser ALL=NOPASSWD:SETENV:/usr/local/bin/rsync,/usr/local/bin/bash" >> "/usr/local/etc/sudoers" + echo "Defaults:$remoteUser !requiretty" >> "usr/local/etc/sudoers" + else + echo "No sudoers file found." + echo "copy the following lines to /etc/sudoers (or /usr/local/etc/sudoers) and adjust /usr/bin path to the target system" + echo "$remoteUser ALL=NOPASSWD:SETENV:/usr/bin/rsync,/usr/bin/bash" + echo "Defaults:$remoteUser !requiretty" + fi +} + +function RemoveUser { + local remoteUser="${1}" + + if type rmuser > /dev/null 2>&1; then + rmuser $remoteUser + elif type userdel > /dev/null 2>&1; then + userdel $remoteUser + else + echo "Please remove $remoteUser manually" + fi +} + +function RemoveSudoers { + local remoteUser="${1}" + + if [ -f "/etc/sudoers" ]; then + cp "/etc/sudoers" "/etc/sudoers.old" + grep -v "$remoteUser" "/etc/sudoers.old" > "/etc/sudoers" + elif [ -f "/usr/local/etc/sudoers" ]; then + cp "/usr/local/etc/sudoers" "/usr/local/etc/sudoers.old" + grep -v "$remoteUser" "/usr/local/etc/sudoers.old" > "/usr/local/etc/sudoers" + else + echo "Please remove lines containing $remoteUser from sudoers file manualle" + fi +} + +if [ "$1" == "set" ]; then + echo "Manual creation of $testUser with homedir $testUserHome" + + adduser "$testUser" + + SetupSSH "$testUser" "$testUserHome" + PrepareSudoers "$testUser" + echo "" + echo "Now feel free to run osync sudo test" + echo "SUDO_EXEC=yes osync.sh --initiator=/home/osyncsudo --target=ssh://osyncsudo@localhost:22//root/osync-tests --rsakey=/home/osyncsudo/.ssh/id_rsa_local" + echo "Don't forget to run $0 unset later" + + +elif [ "$1" == "unset" ]; then + RemoveUser "$testUser" + RemoveSudoers "$testUser" +else + echo "usage: $0 [set] [unset]" +fi + +