logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Coding & Scripting Corner View previous topic :: View next topic
Reply to topic   Topic: Tutorial for a HTML PHP Solution that Calls Another Program?
Author
redapplesonly



Joined: 24 Feb 2022
Posts: 3

PostPosted: Fri 25 Feb '22 21:58    Post subject: Tutorial for a HTML PHP Solution that Calls Another Program? Reply with quote

Hello all,

I have a general question: Can anyone recommend a tutorial for a HTTP/PHP website that calls another program when the user clicks a button?

Some details: I have an Ubuntu 16.04 server, on which runs some custom Java code that I've written for my colleagues. (There are 5 of us.) My colleagues want to use the code, but don't want to go through the hassle of running the code, which involves logging in/su'ing to an account/editing a file/submitting the file to the code/watching for output/etc. Its a pain.

A solution I've like to give them is a simple webpage, hosted on the Ubuntu. The page has a button; they click the button, and everything is automated.

I've installed Apache2 2.4.41 for this purpose. And I have a primitive website that has the button. But I need a PHP script that activates when that button is pushed. The script would have to run my Java code, then post the text-based output.

A wrinkle here is that I'm guessing the PHP script would run as user 'www-data'. But my Java code was installed for user 'pete'. So the PHP script has to be able to do a
Code:
sudo -u pete /home/pete/runJavaProgram.exe
or something.

I realize this is a fairly open-ended post. I apologize for that; I'm new to HTTP and PHP, and any wisdom you can share with me is wildly appreciated! Thank you.
Back to top
tangent
Moderator


Joined: 16 Aug 2020
Posts: 346
Location: UK

PostPosted: Mon 28 Feb '22 16:18    Post subject: Reply with quote

There are any number of methods to solve this problem (your favourite search engine is your friend), depending on how refined and secure a solution you want to create.

However, the simplest option I can think of is to start with a basic HTML form statement, and a CGI script. You could then enhance things with PHP rather than CGI should you want to.

You're running on Ubuntu, so enabling basic CGI in Apache is straightforward. Equally, you can configure the sudoers file so the Apache user www-data can run your Java program as pete (see below).
    a) As root, enable CGI in the default Ubuntu location (/usr/lib/cgi-bin)
    Code:
    # a2enmod cgid
    # apache2ctl restart

    b) Create a test index.html file (say /var/www/html/test/index.html) with the following, e.g.
    Code:
    <form action="/cgi-bin/test" method="post">
        <input type="submit" name="action" value="Click me" />
    </form>

    c) Create a suitable CGI test script, e.g.
    Code:
    # cat > /usr/lib/cgi-bin/test  <<'EOF'
    #!/bin/sh

    echo "Content-Type: text/html\n\n";

    echo "<html> <body>\n";
    /usr/bin/sudo -u pete /home/pete/runJavaProgram.exe | sed -e 's/$/<br>/'
    echo "</body> </html>\n";
    EOF

    Note the sudo command to run your Java program as pete. Also the use of sed to insert line breaks into the output content.

    d) Make the script executable
    Code:
    # chmod 755 /usr/lib/cgi-bin/test

    e) As root, edit the sudoers extras file (/etc/sudoers.d/extras), and add the following line to allow the Apache www-data account to run your Java program as pete.

    Code:
    www-data ALL=(pete) NOPASSWD: /home/pete/runJavaProgram.exe

    f) Point a browser to your above test index page, e.g. http://localhost/test, and click the "Click me" button.
The CGI script should run and you'll hopefully see the output from your Java program.

This solution should be adequate for an internal network.
Back to top
redapplesonly



Joined: 24 Feb 2022
Posts: 3

PostPosted: Mon 28 Feb '22 22:11    Post subject: Reply with quote

@tangent

Thanks, Tangent! I've studied your notes, and I'm eager to try your solution. Unfortunately, I need to do other work stuff at the moment. But I can clear my schedule tomorrow to work on this. Thank you so much! Smile
Back to top
redapplesonly



Joined: 24 Feb 2022
Posts: 3

PostPosted: Tue 01 Mar '22 18:17    Post subject: Reply with quote

@tangent

So I implemented your solution this morning, but I'm afraid something is still amiss. May I review my configuration? I did all of the following:

(A) Enabled CGI and restarted Apache

(B) Set up a simple index.html page:

Code:
<!DOCTYPE html>
<html>
    <body>
          <h2>My Test Webpage</h2>
          <form action="/cgi-bin/test" method="post">
                <input type="submit" name="action" value="Click me" />
          </form>
    </body>
</html>


(C) I created the following CGI-script in /usr/lib/cgi-bin/test:

Code:
#!/bin/sh

echo "Content-Type: text/html\n\n";

echo "<html> <body>\n";
/usr/bin/sudo -u pete /home/pete/testScript.sh | sed -e 's/$/<br>/'
echo "</body> </html>\n";


And set all the following permissions:

Code:
pete@myUbuntu:/var/www/html$ ls -l /usr/lib/cgi-bin/test
-rwxrwxr-x 1 www-data TestGroup 170 Mar  1 10:32 /usr/lib/cgi-bin/test
pete@myUbuntu:/var/www/html$


Where group TestGroup contains users pete and www-data. Note the chmod 775 permissions.

(D) The CGI-Script points to another script owned by user pete. This is the central problem with which I am wrestling: how to get the webpage (user www-data) to run a script owned by another user (user pete) For proof-of-concept purposes, that script is very simple right now:

Code:
pete@myUbuntu:/var/www/html$ more /home/pete/testScript.sh
#!/bin/sh
echo "Hey hey hey!!!"
pete@myUbuntu:/var/www/html$ ls -l /home/pete/testScript.sh
-rwxrwxr-x 1 oete TestGroup 33 Mar  1 10:11 /home/pete/testScript.sh
pete@myUbuntu:/var/www/html$


Very simple. The test script is owned by user pete and allows access by group TestGroup.

(E) As a last step, I edited the sudoers file to include this:

Code:
# User privilege specification
root     ALL=(ALL:ALL) ALL
www-data ALL=(ALL:ALL) ALL
pete     ALL=(ALL:ALL) ALL


I can tighten that later, but for now, I just want to get the server working.

Okay: When everything is in place, and I websurf to my server, I see the simple HTML page. I click the "Click Me" button. This appears on the webpage:

Code:
Network Error (tcp_error)

A communication error occurred: ""
The Web Server may be down, too busy, or experiencing other problems preventing it from responding to requests. You may wish to try again at a later time.


But the real clue is listed in /var/log/apache2/error.log:

Code:
sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper


I've seen this error before; its caused by this line in the CGI script:

Code:
/usr/bin/sudo -u pete /home/pete/testScript.sh | sed -e 's/$/<br>/'


Specifically, user www-data tries to run "sudo -u pete _______" and Ubuntu doesn't think this should be allowed. When I remove the above command, the error doesn't appear... but /home/pete/testScript.sh doesn't run, either.

Do you see what I'm doing wrong? Sorry for the long post; its a weird problem.
Back to top
tangent
Moderator


Joined: 16 Aug 2020
Posts: 346
Location: UK

PostPosted: Tue 01 Mar '22 22:13    Post subject: Reply with quote

As you say the clue to your problem is in the Apache error log, showing that sudo is requesting a password. Believe the problem is your entry in the sudoers file, which needs the NOPASSWRD tag adding to the www-data entry. There is no terminal associated with the Apache process in which the password could be entered, and moreover, the www-data account isn't interactive and equally has no password.

As listed above, suggest you try this line instead (with testScript.sh rather than runJavaProgram.exe)
Code:
www-data ALL=(pete) NOPASSWD: /home/pete/testScript.sh

The www-data account only needs to switch user to pete rather than root (ALL); hence the pete in brackets. You equally only want it to run the one command rather than ALL. Finally, for this Apache functionality, there's no need to grant pete any sudo privilege per se.

Does adding NOPASSWD solve the problem?
Back to top


Reply to topic   Topic: Tutorial for a HTML PHP Solution that Calls Another Program? View previous topic :: View next topic
Post new topic   Forum Index -> Coding & Scripting Corner