error when accessing a uart device from a cgi python script - cgi

I'm trying to access a UART device from my /dev folder to control a robot. I'm trying to do this from the Internet using a cgi python script. The code works fine when I execute it on the command line but when it executes through the server I get a 500 error. I'm pretty sure this is some sort of permissions problem but I don't know how to solve it. If anyone knows what going on here or how I could fix it, the help would be much appreciated.
I can post my python code if anyone would liked to see it but I don't think the problem is in the code, but rather in the execution of the code.
Thanks in advance,
Michael,

Maybe solotion for you can be checking group of device file:
adam#sabayon ~ $ ls -lash /dev/ttyS*
0 crw-rw---- 1 root uucp 4, 64 03-10 13:20 /dev/ttyS0
0 crw-rw---- 1 root uucp 4, 65 03-10 13:20 /dev/ttyS1
0 crw-rw---- 1 root uucp 4, 66 03-10 13:20 /dev/ttyS2
0 crw-rw---- 1 root uucp 4, 67 03-10 13:20 /dev/ttyS3
For me it is uucp. And add user which start your cgi script to this group (for my system uucp). But you have to remember that all other cgi scripts started by this user will be also allowed to access UART.
EDIT:
For apache webserver it looks like suEXEC can be solution for you.
The suEXEC feature provides Apache users the ability to run CGI and
SSI programs under user IDs different from the user ID of the calling
web server. Normally, when a CGI or SSI program executes, it runs as
the same user who is running the web server.
So it looks like you can create new user for your cgi script configure it correctly and than use it with your script by apache.
Here is documentation for suEXEC:
http://httpd.apache.org/docs/2.0/suexec.html
I never used it, but I hope it will help.

Related

Apache Custom Module permission issue with calling Libipset

I'm working on an apache module that can check the libipset API to test if an IP is in a list. This is being used as a backup firewall for proxied connections.
I've managed to get everything working up until the C script calls type = ipset_type_get(session, cmd);. After testing, I believe the main problem is that libipset requires higher permissions. I'm not getting a permission error, just a null value. However, when I run the C script directly using apache as the user, I can get it to work when I grant sudo privileges to apache for the script.
I've tried 1 and 2 in the answers here and they've both failed. Is there any other way to force root for the ipset API call?
This action might need cap_net_admin.
If using systemd to control the process, you can add it like this:
[Service]
...
CapabilityBoundingSet=CAP_NET_ADMIN
Another approach would be to set the binary executable's capabilities.
setcap cap_net_admin=ep /usr/sbin/apache2
If using apparmour, you coould instead set up a profile for apache and include the line
capability net_admin,
in the file ( /etc/apparmor.d/usr.sbin.apache2 )
( see here : https://serverfault.com/questions/932410/enabling-apparmor-for-apache2-in-ubuntu-18-04 )

Why does JavaBridge work but JavaBridgeTemplate621 not

I am trying to install a PHP Java Bridge as per the instructions on http://php-java-bridge.sourceforge.net/pjb/index.php on Windows 7
I have PHP working fine on via IIS installed at c:\php
I copied the JavaBridge.war to c:\php which proved to get a good result from the test (running from c:\php)
java -classpath JavaBridge.war TestInstallation
I installed Tomcat 7 and copied the JavaBridge.war to webapps, which the service extract automatically and I am able to get the expected result from
http://localhost:8080/JavaBridge/
All Ok Great!
However when I do the same with the JavaBrigdeTemplate621.war, the browser
http://localhost:8080/JavaBridgeTemplate621/
returns an error
HTTP Status 500 - php.java.bridge.http.FCGIConnectException: Could not connect to server
I have not included the whole page as its very verbose, but the last root cause entry is
root cause
java.io.IOException: PHP not found. Please install php-cgi. PHP test command was: [php-cgi, -v]
php.java.bridge.Util$Process.start(Util.java:1145)
php.java.servlet.fastcgi.FCGIProcess.start(FCGIProcess.java:68)
php.java.bridge.http.NPChannelFactory.doBind(NPChannelFactory.java:94)
php.java.bridge.http.FCGIConnectionFactory.runFcgi(FCGIConnectionFactory.java:88)
php.java.bridge.http.FCGIConnectionFactory$1.run(FCGIConnectionFactory.java:109)
I've tried restarting IIS as well as Tomcat several times
What am I missing (It's Hot and it's Friday ....)
I notice that
\webapps\JavaBridgeTemplate621\WEB-INF\cgi\x86-windows
has a text file called php-cgi.exe.MISSING.README.txt
where as
\webapps\JavaBridge\WEB-INF\cgi\x86-windows
has php-cgi.exe, php5ts.dll and php.ini
Is there another manual step required to get those in there not documented in the instructions?
Not reading the instructions clearly enough. I missed that you should copy the automatically created folder (from war) into the IIS web apps folder. I was trying use Tomcat as the HTTP server instead of IIS. Once I copied them to IIS and accessed via default port 80 (not 8080) it worked fine.
Hope my conversation with myself is of benefit to someone else

Apache taking 20sec to start running CGI script

Any ideas as to why my Apache/2.0.49 server always waits 20 seconds from receiving a request that is executed using a cgi-bin script to starting to run that script?
The server responds immediately to normal HTTP requests that only use static files, but always takes 20 seconds to respond to cgi-bin requests. I've used tcpdump to time the arrival of the request and printed the time at the beginning of the script to determine that the delay is between those two events.
I can't see anything in the configuration that relates to 20 seconds. The server runs Red Hat Linux 3.3.3-7 & I'm pretty sure that it used to respond to cgi-scripts immediately, but am unsure when it started being slow & what might have changed to cause that.
Thanks in advance for any help/suggestions you may be able to provide.
We had a similar problem with our Apache server running PHP but only with a specific client, in the end it turned out the issue was that the PHP was trying to fetch a file from a remote server but didn’t have connectivity, after 20 seconds the PHP “gave up” and the flow was completed. You should try to search your code for something similar.

Weird~Apache can not find the actually existing "bash" to execute my cgi file~

May be it's too easy for you to answer.
My problem is about cgi and apache web server.
Make it simple, I have a html "form.html" containing a form in it. To access it, typing "127.0.0.1/form.html" in browser.
After clicking "submit" in this html file, it is supposed to adress to "127.0.0.1\cgi-bin\cginame.cgi", the content of "cginame.cgi" is as below:
#!/bin/bash
if [ $REQUEST_METHOD="GET" ]
then
data=$QUERY_STRING
else
data='cat'
fi
java mortcal $data
"mortcal" is a java program calculating and return a HTML page containing results to user.
I'm using apache 2.2 and ubuntu 10.04.
The problem is when I click the "submit" button in "form.html", I got these in error log:
[Sat Sep 24 15:00:20 2011] [error] (2)No such file or directory: exec of '/usr/lib/cgi-bin/mortcgi.cgi' failed
[Sat Sep 24 15:00:20 2011] [error] [client 127.0.0.1] Premature end of script headers: mortcgi.cgi
I know it's because apache can not find "/bin/bash" to execute the cgi file. But I do have "/bin/bash".
It's so weird. Please help me out. Thank you in advance.
To execute CGI scripts, you need to configure Apache to allow this, and your script has to follow the HTTP protocol by sending back data in the right format, and right permissions, and on and on and on.
Here's a great tutorial with an example: http://httpd.apache.org/docs/2.2/howto/cgi.html
... however, I need to say: running a java program from within a shell script via Apache is a bad idea, in general. Each request loads the java runtime engine (JRE), runs the program, then unloads it. There are issues with environment, file ownership and so on -- all of this is why there are application servers like tomcat for java. So if you're just trying something, that's fine. If you're thinking this is a good way to get something done in a professional production environment, I would reconsider.
As noted, this seems like a poor way to do things, but:
Do the script file permissions allow execution for the web server user?
Are you using any security framework such as selinux which would apply additional restrictions?
I checked my configuration files. They are ok. So I kept searching on the web and finally I saw this:
"If you've copied over the script from a Windows machine, you may be being tripped up by ^M at end of line. You can use cat -v /usr/lib/cgi-bin/printenv.pl | head -1 to verify that there isn't a ^M at the end of the line. "
I did copy my cgi file from windows! I forgot to mention it because I did not think it's a big deal.
Now I have removed the ^M by typing this" :%s/^V^M//g in vi. This problem is resolved. Thanks very much for your answer, Mr.Harrison and Dark Falcon, Thank you all.

CakePHP app working remotely but not on local machine

I have a CakePHP app which I have been developing on a remote server. Everything is working fine on the remote server.
I'm now trying to install it on a machine with a fresh install of XAMPP. Cake is timing out. Apache is working - other things, such as phpMyAdmin, work fine. I am running Apache through port 8000, as IIS is using port 80. The OS is Windows Server 2003.
When trying to access the app, it times out, with the following error:
Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\cake\libs\debugger.php on line 247
I have turned URL rewriting off, that hasn't solved the problem.
I have been trying to track down the source of the problem by echoing things and then exiting the script in the cake core. I found that the script was timing out (with the same error) when the components were trying to load. I commented out my components array in app_controller, and the script ran a little further.
Now, I have tracked it down this far:
Dispatcher::dispatch();
Dispatcher::_invoke();
Controller::constructClasses();
Controller::loadModel();
ClassRegistry::init(); //called on line 635 of Controller in the "else" block of the if (PHP5) statement
In ClassRegistry::init(), the script times out on line 141, whic is as follows:
${$class} =& new $class($settings);
I have no idea where to go from here! Help much appreciated.
I got your problem.
To solve this you should do tw0 things.
1) Try to change the debug level 2 in the core.php from the config adn then change it to 0.
2) plz enable this line from the config.
line number 57 in core.php.
Configure::write('App.baseUrl', env('SCRIPT_NAME'));
regards,
archit.
Well, after following the problem a bit further, I found that the problem was coming up in Model::construct. It turns out that the hostname in my database.php file was wrong! I would have expected a "could not connect to the database..." error.