From 8d9dda24536e676d7d2199843e1b617971d0d3bf Mon Sep 17 00:00:00 2001 From: Filippo Squillace Date: Sat, 4 Apr 2015 16:29:55 +0200 Subject: [PATCH 1/5] Issue #58: Fix the command execution under CLI --- lib/core.sh | 2 +- lib/util.sh | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/core.sh b/lib/core.sh index 8580c1e..eb89283 100644 --- a/lib/core.sh +++ b/lib/core.sh @@ -190,7 +190,7 @@ function _run_juju_with_proot(){ if [ "$1" != "" ] then - _run_proot "${proot_args}" "${@}" + insert_quotes "${@}" | _run_proot "${proot_args}" "${SH[@]}" else _run_proot "${proot_args}" "${SH[@]}" fi diff --git a/lib/util.sh b/lib/util.sh index 9f411a3..0322138 100644 --- a/lib/util.sh +++ b/lib/util.sh @@ -67,3 +67,14 @@ function ask(){ fi } + +function insert_quotes(){ +# 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//\"/\\\"}\"" + done + echo ${C} +} From 0a0aac52f51cc105940d41bb709628f770ec0162 Mon Sep 17 00:00:00 2001 From: Filippo Squillace Date: Sat, 4 Apr 2015 16:32:40 +0200 Subject: [PATCH 2/5] Fix the test_all.sh --- tests/test_all.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/test_all.sh b/tests/test_all.sh index 684e6fd..48f9262 100755 --- a/tests/test_all.sh +++ b/tests/test_all.sh @@ -1,5 +1,7 @@ - +tests_succeded=true for tst in $(ls $(dirname $0)/test_* | grep -v $(basename $0)) do - $tst + $tst || tests_succeded=false done + +$tests_succeded From 4214206da07b40d307aa8f073a0a0e0490b4ecb8 Mon Sep 17 00:00:00 2001 From: Filippo Squillace Date: Sat, 4 Apr 2015 16:34:30 +0200 Subject: [PATCH 3/5] Issue #58: Update travis for install AUR packages --- .travis.yml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 66f859f..1d79584 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,13 +5,12 @@ install: - juju -f echo 'Installing juju' - sed -i -e "s/#Server/Server/" ~/.juju/etc/pacman.d/mirrorlist - juju -f pacman --noconfirm -Syy + - juju -f pacman --noconfirm -S base-devel script: - SKIP_ROOT_TESTS=1 ./tests/test_all.sh -# Test on installing package from AUR (issue #58) - #- juju -f pacman --noconfirm -S base-devel - #- juju -f yaourt --noconfirm -S tcptraceroute - #- juju -f tcptraceroute localhost +# Test on installing package from AUR + - juju -f yaourt --noconfirm -S tcptraceroute # Test on installing package from official repo - juju -f pacman --noconfirm -S sysstat - juju -f iostat From 3180cd63f8aaa7ca4bb10103cf6d4e27579bfa84 Mon Sep 17 00:00:00 2001 From: Filippo Squillace Date: Thu, 9 Apr 2015 03:10:15 +0200 Subject: [PATCH 4/5] 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(){ From fa440287fe12c16b756e953c3b6f0c96958e8360 Mon Sep 17 00:00:00 2001 From: Filippo Squillace Date: Thu, 9 Apr 2015 03:16:10 +0200 Subject: [PATCH 5/5] Ensure to have assign ownership to the user on root access --- lib/core.sh | 7 +++++++ tests/test_util.sh | 8 ++++++++ 2 files changed, 15 insertions(+) diff --git a/lib/core.sh b/lib/core.sh index 5b909fb..5b5a8c1 100644 --- a/lib/core.sh +++ b/lib/core.sh @@ -157,10 +157,17 @@ function run_juju_as_root(){ [ "$1" != "" ] && main_cmd="$(insert_quotes_on_spaces "$@")" local cmd="mkdir -p ${JUJU_HOME}/${HOME} && mkdir -p /run/lock && ${main_cmd}" + trap - QUIT EXIT ABRT KILL TERM INT + trap "[ -z $uid ] || ${CHOWN} -R ${uid} ${JUJU_HOME}" EXIT QUIT ABRT KILL TERM INT + JUJU_ENV=1 ${CHROOT} $JUJU_HOME "${SH[@]}" "-c" "${cmd}" + local ret=$? # The ownership of the files in JuJu is assigned to the real user [ -z $uid ] || ${CHOWN} -R ${uid} ${JUJU_HOME} + + trap - QUIT EXIT ABRT KILL TERM INT + return $? } function _run_proot(){ diff --git a/tests/test_util.sh b/tests/test_util.sh index f0a5447..c10a095 100755 --- a/tests/test_util.sh +++ b/tests/test_util.sh @@ -52,4 +52,12 @@ function test_ask(){ assertEquals $? 1 } +function test_insert_quotes_on_spaces(){ + local actual=$(insert_quotes_on_spaces this is "a test") + assertEquals "this is \"a test\"" "$actual" + + local actual=$(insert_quotes_on_spaces this is 'a test') + assertEquals "this is \"a test\"" "$actual" +} + source $(dirname $0)/shunit2