colsw
colsw

Reputation: 3326

Executing commands by visiting webpage?

I run an Ubuntu server, I have two gameservers on this which need semi-regular updates.

They are run under a gameserver-specific account named 'gs', in screens.

I have a 'runupdate.sh' script which contains the following line:

/var/gs/steamcmd/one.sh;/var/gs/steamcmd/two.sh; (one line because windows CR/LF was throwing errors)

one.sh/two.sh are very similar scripts, the contents of one.sh are:

screen -r tf2_1 -X quit;
/var/gs/steamcmd/steamcmd.sh +runscript /var/gs/steamcmd/update_tf2_1.txt;
screen -d -m -S tf2_1 /var/gs/tf2/1/srcds_run -game tf -port 27015 +sv_pure 2 +map cp_badlands +maxplayers 24;

these scripts work perfectly when run as the 'gs' user.

for access purposes I would like the gameservers to always be run in screens assigned to the 'gs' account.

currently, I would like a php script to run when I visit a webpage on www.mysite.com/admin/restartservers12345 which will execure the 'runupdate.sh' script and issue a restart of the servers while updating them, my updateservers12345.html document currently looks like:

<!DOCTYPE html>

<?php
echo exec('/var/gs/runupdate.sh')
?>

<html>
<style>
p {
    text-align: center;
    color:white;
    font: bold 60px arial, sans-serif;
}
</style>
<body style="background-color: rgb(60,60,60)">

<p>Server Updating...</p>

</body>
</html>

the issue is that this script seems to be run as 'www-data' or the otherwise default Apache account, which means it cannot access the gameserver screens running on 'gs'.

currently I would like to have a box onscreen into shich people need to type the password for the gs account, and then hit enter, after which a script like

exec(echo $textboxpassword | su -s - gs -c '/var/gs/runupdate.sh')

will run, effectively running that script as the 'gs' account.

currently when trying to run that command through ssh I get an error su: must be run from a terminal and I can't seem to confirm if any of that php code is being executed properly on my server.

any help would be much appreciated. thank you.

Upvotes: 4

Views: 635

Answers (1)

Evgeniy Kuzmin
Evgeniy Kuzmin

Reputation: 2462

you should use "sudo" instead of "su"

Add user www-data to sudoers list, you even can allow only single command '/var/gs/runupdate.sh' to execute from www-data as sudoer.

/etc/sudoers.d/www-data

www-data ALL=(ALL) NOPASSWD:  /var/gs/runupdate.sh

Update 1:

You can use

sudo -u gs bash -c '/var/gs/runupdate.sh'

And it will execute runupdate.sh from gs user

You can allow execution without password by follow lines at /etc/sudoers.d/www-data

www-data ALL=(gs) NOPASSWD:  /var/gs/runupdate.sh

Upvotes: 2

Related Questions