c++ compiled in cgi script with curl not working - c++

I have a code that is 100% fully functional when compiled in g++ and run in terminal. The code uses wget and curl. When I compile it in to a cgi file and have my html webpage call the script then the curl part of the code does not do anything.
What curl is doing is that it is downloading a webpage which is very critical for my program. I have my apache2 server setup, the cgi file is in cgi-bin folder and everything works except the curl doesnt work in the compiled cgi file.
What are some of the ways to fix this? I have tried many things and none of them seem to work. Thank You

It's most likely to be either permissions, paths, or environment variables. Try TEMPORARILY relaxing all permissions - maybe making your curl setuid root and adding in code to check return values and error codes from all your system calls. Try outputting all your environment variables in Terminal and in CGI mode and diffing them.

Related

Need some help getting Apache 2.2.5 to register environmental variables (CentOS 6.10)

I've tried every solution I've found in the last two days on this and other sites. None work.
I'm trying to set two OS level environment variables/commands for the apache user account on centos 6.10. Apache version is 2.2.5. See final paragraph for why I'm having to do this. Alternative solutions are also welcome.
Specifically these commands/variables:
. /opt/rh/python27/enable
export set CLOUDSDK_PYTHON=/opt/rh/python27/root/usr/bin/python2
I have:
Put them in /etc/sysconfig/httpd and /etc/init.d/httpd
Tried various version of these commands
Switched apache to use bash, created .bashrc in /var/www/, gave apache ownership and set permissions correctly and put them in there. I realize this is bad practice, its a test server and I was out of other ideas.
I can't get them to persist beyond a single exec statement in php (where I still have to call both listed commands) or a single su -s /bin/bash apache -c "[command]" execution.
Does anyone know how to get these to persist for the apache user?
Why am I doing this?
I am doing this because Google Cloud Cloud SDK has PHP code that doesn't work so I'm left with either using CURL or passing exec statements, both of which require this environmental variable nonsense for the executing user. To run Google Cloud SDK commands you have to have version 2.7 of python installed, which you can't upgrade to on centos 6.10 without breaking the OS, unless you install it concurrently somewhere else per: http://jhurani.com/linux/2018/07/30/GCSDK-on-centos6.html doing that means I have to set whatever user account is executing code to use the new version of python. Which is apache in this case. Works fine on my normal user account, but I can't get the apache account to respect these changes beyond a single execution, even though everything I read says that it should.
Any help would be appreciated. Thanks for you time and any insight you'd care to share.
Could you try to export the variables in /etc/sysconfig/httpd
export mydocroot=/var/www/html
Then configure the Virtual Host
<VirtualHost *:80>
DocumentRoot ${mydocroot}
</VirtualHost>
Finally restart the service
systemctl restart httpd
In addition, I found that you can also do it through the .httaccess file with SetEnv assuming that it's enabled on the website.
SetEnv SPECIAL_PATH /foo/bin
That's all you need to add the environment variable into the .htaccess file

Django wsgi and file permission

When I run django in localhost via runserver I see that .py files are compiled to .pyc.
When I deploy everything to server using apache and mod_wsgi - should www user has write access to folders with my application? So it will be able to create .pyc
Of course, having write access is not a good idea - if I remove it, will ti slow down app; or compiled python code is cached in memory?
thanks
.pyc files are only used as bytecode cache between invocations of the interpreter; Python will use them if present but has no problem with recompiling the code instead. So to answer your question, you do not need to give the WSGI application write access to the source location.

Setting up .htaccess on host for execution of C++ cgi scripts

I have a cgi script that I know works (as far as the code is concerned), but which cannot be accessed through my website. My hosting provider simply states that I need to edit the .htaccess file, but I have no idea what options/handlers I need to set in order to make the contents of a directory execute like c++.
How is this done?
You can't on this service provider. A quick search of the Bluehost Kb gave this: https://my.bluehost.com/cgi/help/48
Our LINUX web servers have the capability to run CGI scripts in your own "cgi-bin" directory. Scripts may be written in Perl, Python and CGI languages.
Here are some helpful tips to follow when installing scripts:
Upload to your cgi-bin directory to ensure proper file permission settings.
All scripts on our server must have permissions set to 755 (rwx-rx-rx). If you need help in changing script permissions, please see our article about setting file and user permissions.
Upload in ASCII transfer mode (and NOT BINARY mode)
The first line of each script must read: a) #!/usr/bin/perl (for Perl) b) #!/usr/bin/python (for Python)
Ensure the permissions are set to 755
However, there is nothing stopping you just trying just putting your exe in the cgi-bin dir and seeing if it runs, but this probably won't work.
In this case, you'd need to relink any C++ against the local target server, and I doubt that Bluehost would facilitate this -- just too much support hassle for the few $ / month that you pay.

Cannot connect to Oracle Database 10g using a C++ CGI with httpd 2.2

I am writing a CGI program in C++. It is supposed to run on version 2.2 of the Apache HTTP Server and it needs to connect to Oracle Database 10g.
I tried to run this CGI program without httpd and it could connect to the Oracle database successfully, but once I ran it through the httpd server it wrote the following message in error_log.
Error while trying to retrieve text for error ORA-12546
How can I solve this problem?
That generally means that you have encountered an ORA-12546 error, but that your client library doesn't have the actual error messages installed. That's not a problem, you can search for the error numbers here. In your case, it's "TNS:Permission denied".
Does your CGI user have permission to access tnsnames.ora?
Just a guess, but a common cause for this type of thing is that the ORACLE_HOME environment is not set for your CGI program. Apache usually starts up at boot time ans so skips all the /etc/profile type stuff.
You will need to ensure both ORACLE_HOME is set, and depending on your system setup you may also need LD_LIBRARY_PATH updated to include $ORACLE_HOME/lib. You either need your program to set the environment, or have a SetEnv directive in your apache config. Bet way to see if this is the issue is just write a test CGI which dumps all the environment variables.
Now if the CGI is running under mod_perl its a bit trickier because mod_perl tends to be pretty anal about environment security. Think we ended up using the Perl*Something*Require directive to set it up within the perl. Sorry for being vague, was years ago....

Is a cgi different from a console application?

I am having some problems in running cgi on my Apache (Windows, XAMPP), but the exe runs smoothly on the command prompt.
Reading the logs on Apache folder it gives no information about the error. Any ideas about this?
You are a little light on the detail, so I'll just hazzard a guess.
chmod +x app_executable
Also make sure you are outputting the Content-Type: text/html (or whatever you are returning) followed by two newlines.
Is the file in the proper directory? Does Apache have the desired file/directory access rights to that directory? Can you run other CGI scripts from the same directory?
Weird.
I found the problem, it was in a
sprintf("%f", f);
where f wasn't initiated.
This is weird, because it ran normal on my cmd but not on apache
any clues?