bytecode77
bytecode77

Reputation: 14820

Limit bash script to its owner

I have different users in /home. Each user has a start.sh script and a start.conf for configuration. Also start.sh in linked to /bin/nameoftheuser to make typing easier (This adds more complexity to the question).

I want to limit the script to the user that works with it. What I currently do is this:

cd
if [ -f start.conf ]; then
    . start.conf
else
    echo "start.conf not found"
    exit 2
fi

if [ $(whoami) != $SCRIPTUSER ]; then
    echo "You are logged in with $(whoami). Only $SCRIPTUSER is allowed to use this script"
    exit 2
fi

$SCRIPTUSER is defined in start.conf. But when I'm logged in with the wrong user and I want to use start.sh, the wrong configuration file gets included so the second part of the code doesn't catch the mistake.

In the end, each user should limit their start.sh script excluselive to them and others should not be able to call it by accident


One idea could be comparing the owner of $0 to the current user. But when there is a symbolic link in /bin/nameoftheuser, the symbolic link is owned by root.

Including the configuration file and reading what user is allowed to use it and compare $(whoami) to it, does also not satisfy. This is because a different user could call it and include their own start.conf

Upvotes: 1

Views: 37

Answers (2)

Nick Burns
Nick Burns

Reputation: 192

This will find the owner of the symbolic link

owner=$(stat --format '%U' $(readlink -f /bin/nameoftheuser))

Upvotes: 1

hek2mgl
hek2mgl

Reputation: 157967

Why don't you simply use file permissions?

chmod 700 script.sh

That's it.

Upvotes: 4

Related Questions