Issue #58: Fix on root access

This commit is contained in:
Filippo Squillace 2015-04-09 03:10:15 +02:00
parent 4214206da0
commit 3180cd63f8
3 changed files with 33 additions and 23 deletions

View file

@ -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

View file

@ -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
}

View file

@ -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(){