From a993b8b34d3f4bd1ff1cbd3ea189b8eba3e8f39c Mon Sep 17 00:00:00 2001 From: pvogt09 <50047961+pvogt09@users.noreply.github.com> Date: Fri, 3 Apr 2020 12:06:59 +0200 Subject: [PATCH 1/3] fixes #3217 by checking for existing pihole group Signed-off-by: pvogt09 <50047961+pvogt09@users.noreply.github.com> --- automated install/basic-install.sh | 41 ++++++++++++++++++++++++++---- test/test_automated_install.py | 41 ++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 5 deletions(-) diff --git a/automated install/basic-install.sh b/automated install/basic-install.sh index 14c68250..4474d37e 100755 --- a/automated install/basic-install.sh +++ b/automated install/basic-install.sh @@ -1770,18 +1770,49 @@ create_pihole_user() { printf " %b %s..." "${INFO}" "${str}" # If the user pihole exists, if id -u pihole &> /dev/null; then - # just show a success - printf "%b %b %s\\n" "${OVER}" "${TICK}" "${str}" + # if group exists + if getent group pihole; then + # just show a success + printf "%b %b %s\\n" "${OVER}" "${TICK}" "${str}" + else + local str="Checking for group 'pihole'" + printf " %b %s..." "${INFO}" "${str}" + local str="Creating group 'pihole'" + # if group can be created + if groupadd pihole; then + printf "%b %b %s\\n" "${OVER}" "${TICK}" "${str}" + local str="Adding user 'pihole' to group 'pihole'" + printf " %b %s..." "${INFO}" "${str}" + # if pihole user can be added to group pihole + if usermod -g pihole pihole; then + printf "%b %b %s\\n" "${OVER}" "${TICK}" "${str}" + else + printf "%b %b %s\\n" "${OVER}" "${CROSS}" "${str}" + fi + else + printf "%b %b %s\\n" "${OVER}" "${CROSS}" "${str}" + fi + fi # Otherwise, else printf "%b %b %s" "${OVER}" "${CROSS}" "${str}" local str="Creating user 'pihole'" printf "%b %b %s..." "${OVER}" "${INFO}" "${str}" # create her with the useradd command - if useradd -r -s /usr/sbin/nologin pihole; then - printf "%b %b %s\\n" "${OVER}" "${TICK}" "${str}" + if getent group pihole; then + # add primary group pihole as it already exists + if useradd -r --no-user-group -g pihole -s /usr/sbin/nologin pihole; then + printf "%b %b %s\\n" "${OVER}" "${TICK}" "${str}" + else + printf "%b %b %s\\n" "${OVER}" "${CROSS}" "${str}" + fi else - printf "%b %b %s\\n" "${OVER}" "${CROSS}" "${str}" + # add user pihole with default group settings + if useradd -r -s /usr/sbin/nologin pihole; then + printf "%b %b %s\\n" "${OVER}" "${TICK}" "${str}" + else + printf "%b %b %s\\n" "${OVER}" "${CROSS}" "${str}" + fi fi fi } diff --git a/test/test_automated_install.py b/test/test_automated_install.py index c0bd1ebe..c4ab24e3 100644 --- a/test/test_automated_install.py +++ b/test/test_automated_install.py @@ -92,6 +92,47 @@ def test_setupVars_saved_to_file(Pihole): assert "{}={}".format(k, v) in output +def test_pihole_user_group_creation(Pihole): + ''' + check user creation works if user or group already exist + ''' + # normal situation where neither user or group exist + user_create = Pihole.run(''' + source /opt/pihole/basic-install.sh + create_pihole_user + ''') + expected_stdout = tick_box + ' Creating user \'pihole\'' + assert expected_stdout in user_create.stdout + # situation where both user and group already exist + user_create = Pihole.run(''' + source /opt/pihole/basic-install.sh + create_pihole_user + ''') + expected_stdout = tick_box + ' Checking for user \'pihole\'' + assert expected_stdout in user_create.stdout + # situation where only group and no user exists + Pihole.run('su --shell /bin/bash --command "userdel -r pihole" -p root') + user_create = Pihole.run(''' + source /opt/pihole/basic-install.sh + create_pihole_user + ''') + expected_stdout = tick_box + ' Creating user \'pihole\'' + assert expected_stdout in user_create.stdout + # situation where only user and no group exists + Pihole.run('su --shell /bin/bash --command "userdel -r pihole" -p root') + Pihole.run('su --shell /bin/bash --command "groupdel pihole" -p root') + Pihole.run('su --shell /bin/bash --command "groupadd pihole_dummy" -p root') + Pihole.run('su --shell /bin/bash --command "useradd -r --no-user-group -g pihole_dummy -s /usr/sbin/nologin pihole" -p root') + user_create = Pihole.run(''' + source /opt/pihole/basic-install.sh + create_pihole_user + ''') + expected_stdout = tick_box + ' Creating group \'pihole\'' + assert expected_stdout in user_create.stdout + expected_stdout = tick_box + ' Adding user \'pihole\' to group \'pihole\'' + assert expected_stdout in user_create.stdout + + def test_configureFirewall_firewalld_running_no_errors(Pihole): ''' confirms firewalld rules are applied when firewallD is running From 25c5661c1bc20c0cd568f375d85adbb5943be47e Mon Sep 17 00:00:00 2001 From: pvogt09 <50047961+pvogt09@users.noreply.github.com> Date: Fri, 3 Apr 2020 19:22:30 +0200 Subject: [PATCH 2/3] fix stickler errors Signed-off-by: pvogt09 <50047961+pvogt09@users.noreply.github.com> --- test/test_automated_install.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/test/test_automated_install.py b/test/test_automated_install.py index c4ab24e3..fef9eb99 100644 --- a/test/test_automated_install.py +++ b/test/test_automated_install.py @@ -96,6 +96,7 @@ def test_pihole_user_group_creation(Pihole): ''' check user creation works if user or group already exist ''' + sudo_cmd = 'su --shell /bin/bash --command "{0}" -p root' # normal situation where neither user or group exist user_create = Pihole.run(''' source /opt/pihole/basic-install.sh @@ -111,7 +112,7 @@ def test_pihole_user_group_creation(Pihole): expected_stdout = tick_box + ' Checking for user \'pihole\'' assert expected_stdout in user_create.stdout # situation where only group and no user exists - Pihole.run('su --shell /bin/bash --command "userdel -r pihole" -p root') + Pihole.run(sudo_cmd.format('userdel -r pihole')) user_create = Pihole.run(''' source /opt/pihole/basic-install.sh create_pihole_user @@ -119,10 +120,13 @@ def test_pihole_user_group_creation(Pihole): expected_stdout = tick_box + ' Creating user \'pihole\'' assert expected_stdout in user_create.stdout # situation where only user and no group exists - Pihole.run('su --shell /bin/bash --command "userdel -r pihole" -p root') - Pihole.run('su --shell /bin/bash --command "groupdel pihole" -p root') - Pihole.run('su --shell /bin/bash --command "groupadd pihole_dummy" -p root') - Pihole.run('su --shell /bin/bash --command "useradd -r --no-user-group -g pihole_dummy -s /usr/sbin/nologin pihole" -p root') + Pihole.run(sudo_cmd.format('userdel -r pihole')) + Pihole.run(sudo_cmd.format('groupdel pihole')) + Pihole.run(sudo_cmd.format('groupadd pihole_dummy')) + useradd_dummy = ( + 'useradd -r --no-user-group -g pihole_dummy ' + + '-s /usr/sbin/nologin pihole') + Pihole.run(sudo_cmd.format(useradd_dummy)) user_create = Pihole.run(''' source /opt/pihole/basic-install.sh create_pihole_user From 40ac3e7eb746c1891294c60a20ed2cf7f97e6432 Mon Sep 17 00:00:00 2001 From: pvogt09 <50047961+pvogt09@users.noreply.github.com> Date: Fri, 3 Apr 2020 20:07:44 +0200 Subject: [PATCH 3/3] remove pihole group during uninstall Signed-off-by: pvogt09 <50047961+pvogt09@users.noreply.github.com> --- automated install/basic-install.sh | 4 ++-- automated install/uninstall.sh | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/automated install/basic-install.sh b/automated install/basic-install.sh index 4474d37e..1070a7a3 100755 --- a/automated install/basic-install.sh +++ b/automated install/basic-install.sh @@ -1771,7 +1771,7 @@ create_pihole_user() { # If the user pihole exists, if id -u pihole &> /dev/null; then # if group exists - if getent group pihole; then + if getent group pihole > /dev/null 2>&1; then # just show a success printf "%b %b %s\\n" "${OVER}" "${TICK}" "${str}" else @@ -1799,7 +1799,7 @@ create_pihole_user() { local str="Creating user 'pihole'" printf "%b %b %s..." "${OVER}" "${INFO}" "${str}" # create her with the useradd command - if getent group pihole; then + if getent group pihole > /dev/null 2>&1; then # add primary group pihole as it already exists if useradd -r --no-user-group -g pihole -s /usr/sbin/nologin pihole; then printf "%b %b %s\\n" "${OVER}" "${TICK}" "${str}" diff --git a/automated install/uninstall.sh b/automated install/uninstall.sh index 01ce9c39..5dab2136 100755 --- a/automated install/uninstall.sh +++ b/automated install/uninstall.sh @@ -188,6 +188,14 @@ removeNoPurge() { echo -e " ${CROSS} Unable to remove 'pihole' user" fi fi + # If the pihole group exists, then remove + if getent group "pihole" &> /dev/null; then + if ${SUDO} groupdel pihole 2> /dev/null; then + echo -e " ${TICK} Removed 'pihole' group" + else + echo -e " ${CROSS} Unable to remove 'pihole' group" + fi + fi echo -e "\\n We're sorry to see you go, but thanks for checking out Pi-hole! If you need help, reach out to us on Github, Discourse, Reddit or Twitter