0

I'm working on creating my own little Website to manage a Minecraft server as fun project. Now what I would need to accomplish is being able to send commands to the screen in which the server is running.

My approach to this was the following:

<?php
if (isset($_POST['startbutton']))
    {
        exec('sudo screen -S 23971 -X stuff "say hello^M"');
    }
?>

    
<form method="post">
    <button type="submit" name="startbutton">Test</button>
</form>

Now that command line works just fine when i execute it in the terminal itself, but as soon as i try to run it over the Website nothing happens. If i just try to execute

if (isset($_POST['startbutton']))
    {
        echo exec('whoami');
    }
?>

it works just fine as well. I don't know what I am doing wrong.

halfer
  • 19,824
  • 17
  • 99
  • 186
DRock99
  • 15
  • 5
  • Sudo requires a password to be entered by hand. In your PHP you don't have opportunity to do that because there's no terminal. I don't know for sure but that seems like a potential problem – ADyson Jan 25 '21 at 21:25
  • Use `shell_exec` instaed of `exec` so you can see full output from shell. It might help to find the issue. – Cyborg Jan 25 '21 at 23:58

3 Answers3

0

i'm really not sure about it, but try:

<?php
if (isset($_POST['startbutton']))
    {
        exec('sudo screen -S 23971 -X stuff "say hello^M"');
        exec('YOUR SUDO PSW');
    }
?>


<form method="post" action="YOUR PHP PAGE LIKE server.php">
    <button type="submit" name="startbutton">Test</button>
</form>

let me know if it works

Jacopo Mosconi
  • 972
  • 8
  • 22
  • Hi, thank you for the answer. Sadly it doesn't work, the problem seems to lie somewhere else. I have written more about in as an answer to the suggestion below. – DRock99 Jan 26 '21 at 01:40
0

Seems like you are trying to execute a command that needs superuser permissions. In most setups PHP runs under the webserver user and it does not have those permissions. Also you should keep in mind that giving superuser permissions to PHP is a security risk.

My suggestion would be to have a dedicated service, responsible for executing those shell commands. You could then call that service from your PHP script without needing sudo.

Check these for details:

lbrandao
  • 4,144
  • 4
  • 35
  • 43
  • Hi, thanks for the input. I was able to give the www-data user access to using screen, but the problems seems to be that if i start the screen session as e.g root user, it is not accessible for any other user. So it just can't find the screen the command is supposed to be executed in. Do you have any ressources on how to build such a dedicated service which can then be called from the PHP script? – DRock99 Jan 26 '21 at 01:38
  • You can write this dedicated service in PHP or any other language, just make sure it's not started by the web server. Basically this dedicated service would be a server application, always running and responsible for receiving requests and communicating with your minecraft server. You can decide if you want it to be a TCP server, a HTTP server, a WebSocket server... it's up to you. Here is a barebones example of a TCP server in PHP: https://www.php.net/manual/en/sockets.examples.php. You can also take a look at some PHP frameworks that can help you: ReactPHP, Amp or Swoole. – lbrandao Jan 26 '21 at 15:34
0

First of all: Thank you all for your support. I was doing a bit of further research and found a solution for my problem:

I created a new user which I then gave the ownership of /var/www. I then changed the apache2 user from www-data to the new user. Now i just needed to start the screen with the minecraft server as the new user so i can access this screen out of php and I was able to get it to work without having to give any user full root privileges or anything.

DRock99
  • 15
  • 5