From 3180cd63f8aaa7ca4bb10103cf6d4e27579bfa84 Mon Sep 17 00:00:00 2001 From: Filippo Squillace Date: Thu, 9 Apr 2015 03:10:15 +0200 Subject: [PATCH] Issue #58: Fix on root access --- lib/core.sh | 15 +++++---------- lib/util.sh | 15 +++++++++++---- tests/test_core.sh | 26 +++++++++++++++++--------- 3 files changed, 33 insertions(+), 23 deletions(-) diff --git a/lib/core.sh b/lib/core.sh index eb89283..5b909fb 100644 --- a/lib/core.sh +++ b/lib/core.sh @@ -150,19 +150,14 @@ function setup_from_file_juju(){ function run_juju_as_root(){ - local main_cmd="${SH[@]}" - [ "$1" != "" ] && main_cmd="$@" - local uid=$UID [ -z $SUDO_UID ] || uid=$SUDO_UID:$SUDO_GID - local cmd=" -mkdir -p ${JUJU_HOME}/${HOME} -mkdir -p /run/lock -${main_cmd} -" + local main_cmd="${SH[@]}" + [ "$1" != "" ] && main_cmd="$(insert_quotes_on_spaces "$@")" + local cmd="mkdir -p ${JUJU_HOME}/${HOME} && mkdir -p /run/lock && ${main_cmd}" - JUJU_ENV=1 ${CHROOT} $JUJU_HOME /usr/bin/bash -c "${cmd}" + JUJU_ENV=1 ${CHROOT} $JUJU_HOME "${SH[@]}" "-c" "${cmd}" # The ownership of the files in JuJu is assigned to the real user [ -z $uid ] || ${CHOWN} -R ${uid} ${JUJU_HOME} @@ -190,7 +185,7 @@ function _run_juju_with_proot(){ if [ "$1" != "" ] then - insert_quotes "${@}" | _run_proot "${proot_args}" "${SH[@]}" + _run_proot "${proot_args}" "${SH[@]}" "-c" "$(insert_quotes_on_spaces "${@}")" else _run_proot "${proot_args}" "${SH[@]}" fi diff --git a/lib/util.sh b/lib/util.sh index 0322138..52e1b24 100644 --- a/lib/util.sh +++ b/lib/util.sh @@ -68,13 +68,20 @@ function ask(){ } -function insert_quotes(){ +function insert_quotes_on_spaces(){ # It inserts quotes between arguments. # Useful to preserve quotes on command # to be used inside sh -c/bash -c C='' - for i in "$@"; do - C="$C \"${i//\"/\\\"}\"" + whitespace="[[:space:]]" + for i in "$@" + do + if [[ $i =~ $whitespace ]] + then + C="$C \"$i\"" + else + C="$C $i" + fi done - echo ${C} + echo $C } diff --git a/tests/test_core.sh b/tests/test_core.sh index bf66e80..0549fff 100755 --- a/tests/test_core.sh +++ b/tests/test_core.sh @@ -105,20 +105,28 @@ function test_run_juju_as_root(){ install_mini_juju CHROOT="sudo $CHROOT" CHOWN="sudo $CHOWN" - SH=("type" "-t" "type") - local output=$(run_juju_as_root) - assertEquals $output "builtin" + local output=$(run_juju_as_root pwd) - assertEquals $output "/" - run_juju_as_root "[ -e /run/lock ]" + assertEquals "$output" "/" + run_juju_as_root [ -e /run/lock ] assertEquals $? 0 - run_juju_as_root "[ -e $HOME ]" + run_juju_as_root [ -e $HOME ] assertEquals $? 0 # test that normal user has ownership of the files created by root - run_juju_as_root "touch /a_root_file" - local output=$(run_juju_as_root "stat -c '%u' /a_root_file") - assertEquals $output "$UID" + run_juju_as_root touch /a_root_file + local output=$(run_juju_as_root stat -c '%u' /a_root_file) + assertEquals "$output" "$UID" + + SH=("sh" "--login" "-c" "type -t type") + local output=$(run_juju_as_root) + assertEquals "$output" "builtin" + SH=("sh" "--login" "-c" "[ -e /run/lock ]") + run_juju_as_root + assertEquals $? 0 + SH=("sh" "--login" "-c" "[ -e $HOME ]") + run_juju_as_root + assertEquals $? 0 } function test_run_juju_as_user(){