1

I have a script to copy permissions which works fine, being the interesting line this one:

...
bash commands.sh $PAM_USER "medium" /bin/{ls,rm,bash,nano,mkdir,kill,ln,ps,grep,sed} /usr/bin/{vim,touch,java,python,gcc,awk,make}
...

commands.sh has a loop which reads through all permissions listed and copies them to a specified directory. Problem comes when I try to do the same thing reading the permissions from a config file.

My config file contains the following:

MEDIUM_PERMISSIONS_BIN=/bin/{ls,rm,bash,nano,mkdir,kill,ln,ps,grep,sed}
MEDIUM_PERMISSIONS_USR_BIN=/usr/bin/{vim,touch,java,python,gcc,awk,make}
MEDIUM_ENVIROMENT_LIFESPAN=midnight
MEDIUM_PERSISTENT_HOME=true

Note: the last two configurations are for something different, for now I'm trying to read the first two lines properly.

I add the config file to the previous script and try to execute the following:

source /etc/users/config.cfg
...
 bash commands.sh $PAM_USER "medium" $MEDIUM_PERMISSIONS_BIN $MEDIUM_PERMISSIONS_USR_BIN
...

The problem is that the loop reads it as such:

loop: /bin/{ls,rm,bash,nano,mkdir,kill,ln,ps,grep,sed}
loop: /usr/bin/{vim,touch,java,python,gcc,awk,make}

when previously it used to read it as /bin/ls, /bin/rm, /bin/rm and so on, hence the loop working with the first version as it is able to find those commands, but not on the second version sisnce the output specified previously is incorrect.

I've read through the sed and awk commands but I'm quite new to this and can't get them to work.

How can I read the permissions from my config file as if I was introducing them directly like in the first version?

Thanks

edit: commands.sh code:

#!/bin/bash

CHROOT="/users/$2/$1"
if [ ! -d "$CHROOT/home/$1" ]; then
    mkdir -p "$CHROOT/home/$1"
fi

for i in $( ldd $* | grep -v dynamic | cut -d " " -f 3 | sed 's/://' | sort | uniq )
do
    cp -v --parents $i $CHROOT
done

#ARCH amd64
if [ -f /lib64/ld-linux-x86-64.so.2 ]; then
    cp --parents /lib64/ld-linux-x86-64.so.2 /$CHROOT
fi

#ARCH i386
if [ -f /lib/ld-linux.so.2 ]; then
    cp --parents /lib/ld-linux.so.2 /$CHROOT
fi

clear
cd

Comment: I removed all the debugging echoes. This code is pretty much a copy from a code you can find here: https://linuxconfig.org/how-to-automatically-chroot-jail-selected-ssh-user-logins

  • 1
    Please edit your question and show us the commands.sh script. We need to see the code to understand how it reads whatever it reads. – terdon Dec 3 '18 at 11:22
  • @terdon done. I should have included it in my original post, sorry. Anyways, your post seems to work perfectly and also explaining how it works. Much appreciated :) – sartox Dec 3 '18 at 13:55
  • Yeah, I guessed how you were reading it after leaving the comment :) – terdon Dec 3 '18 at 13:57
1

You are trying to define an array variable but are using the format for a string:

## This will define a string variable
$ MEDIUM_PERMISSIONS_BIN=/bin/{ls,rm,bash,nano,mkdir,kill,ln,ps,grep,sed}
$ echo $MEDIUM_PERMISSIONS_BIN
/bin/{ls,rm,bash,nano,mkdir,kill,ln,ps,grep,sed}

## The parentheses tell the shell this is an array
$ MEDIUM_PERMISSIONS_BIN=(/bin/{ls,rm,bash,nano,mkdir,kill,ln,ps,grep,sed})
$ echo $MEDIUM_PERMISSIONS_BIN  ## the 1st element of the array
/bin/ls
$ echo ${MEDIUM_PERMISSIONS_BIN[@]} ## the entire array
/bin/ls /bin/rm /bin/bash /bin/nano /bin/mkdir /bin/kill /bin/ln /bin/ps /bin/grep /bin/sed

So just change your config file to:

MEDIUM_PERMISSIONS_BIN=(/bin/{ls,rm,bash,nano,mkdir,kill,ln,ps,grep,sed})
MEDIUM_PERMISSIONS_USR_BIN=(/usr/bin/{vim,touch,java,python,gcc,awk,make})
MEDIUM_ENVIROMENT_LIFESPAN=midnight
MEDIUM_PERSISTENT_HOME=true

And then, in your script, use "${MEDIUM_PERMISSIONS_BIN[@]}".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.