CGI bin file downloads instead of executing through HTML Browser
Linux - NetworkingThis forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
CGI bin file downloads instead of executing through HTML Browser
I am Embedded Software Engineer, I am new to programming, I wrote the HTML code which is simple form as the code shown below :
<form action="/cgi-bin/gpio.cgi" method="post">
<div><label>Multiplicand 1: <input name="m"></label></div>
<div><label>Multiplicand 2: <input name="n"></label></div>
<div> <input type="submit" value="Multiply" ></div>
</form>
and i wrote the following gpio.cgi code in C langugae to run it on Linux through my HTML : here is the code shown below :
int main() {
char *data ;
long m,n;
printf("%s %c%c\n ", "Content-Type:text/html;charset=iso-8859-1",13,10);
printf("<title>Multiplication results</title>\n");
printf("<h3>Multiplication results</h3>\n");
data=getenv("QUERY_STRING");
if (data==NULL)
printf("<P>Error! Error in passing data from to Script");
else if (sscanf(data,"m=%ld&n=%ld",&m,&n)!=2)
printf("<P>Error! invalid Data. ");
else printf("<P>The Product of %ld and %ld is %ld.",m,n,m*n);
return 0;
}
Than i compiled my above program by typing following command in teminal in Linux:
gcc -o gpio.c gpio.cgi
and i put this gpio.cgi file in /cgi-bin directory the same path as i wrote in HTML code, but when i click Submit button after entering data , my gpio.cgi file gets download instead of execution, while i tested gpio.cgi file independently and it works fine but not with HTML browser.
i did Chmod 777, 755 gpio.cgi for setting file permissions, but nothing worked for it.
There seems to be a number of problems here. One, your webserver seems to be mis-configured, likely the part of the CGI setup is wrong. The C part of the code doesn't build under gcc-6.1.0. Fixing that and adding a debug part so we can see the query string that's being sent:
Code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv) {
char *data ;
long m,n;
printf("%s %c%c\n ", "Content-Type:text/html;charset=iso-8859-1",13,10);
printf("<title>Multiplication results</title>\n");
printf("<h3>Multiplication results</h3>\n");
data=getenv("QUERY_STRING");
/* DEBUG */
printf("<p>QUERY_STRING is: %s</p><br>", data);
if (data==NULL) {
printf("<p>Error! Error in passing data from to Script</p>");
return EXIT_FAILURE;
}
if (sscanf(data,"m=%ld&n=%ld",&m,&n) != 2) {
printf("<p>Error! invalid Data.</p> ");
return EXIT_FAILURE;
}
else {
printf("<p>The Product of %ld and %ld is %ld.</p>", m, n, m*n );
return EXIT_SUCCESS;
}
}
gcc -ggdb -o gpio.cgi gpio.c
env QUERY_STRING="m=30&n=42" ./gpio.cgi
Content-Type:text/html;charset=iso-8859-1
<title>Multiplication results</title>
<h3>Multiplication results</h3>
<p>QUERY_STRING is: m=30&n=42</p><br><p>The Product of 30 and 42 is 1260.</p>
Which since you use the POST method in your form, it leaves the QUERY_STRING blank and puts that in the standard input stream. The data seems to be there as you can see the correct result on command line.
Code:
Multiplication results
QUERY_STRING is:
Error! invalid Data.
...since QUERY_STRING isn't being filled in with POST method. Change "post" to "GET" (<form action="/cgi-bin/gpio.cgi" method="GET">) and:
Code:
Multiplication results
QUERY_STRING is: m=30&n=42
The Product of 30 and 42 is 1260.
It works. You should probably clean up the output headers and do better error checking. As for getting the CGI to run from the webserver, if you're using Apache, check its docs for httpd.conf, something like:
Code:
#
# "/srv/www/cgi-bin" should be changed to whatever your ScriptAliased
# CGI directory exists, if you have that configured.
#
<Directory "/srv/www/cgi-bin" >
AllowOverride None
Options ExecCGI MultiViews
</Directory>
Thank you very much for helping me out. I am using Linux BOA Server, now i am getting another error when I click the submit button, "The CGI was not CGI/1.1 complaint" 502 Bad Gateway. I have done the following command on my gpio.cgi, chmod 755 gpio.cgi, setting file permissions, but still i am getting the same error , please help me.
Odd. I works here with Apache + Firefox. Here's a link with someone with same problem: http://www.friendlyarm.net/forum/topic/1280 My guess is it may be your headers output in the CGI itself. Must be that webserver is more picky than mine. Permissions should be fine. GCC should spit out 755 perms as shown in the example, which is what you want. 777 sets that file to world-writable in addition, which is a security risk. I'd check 1.) the location in your webserver config for the CGI directory matches what you think it is and 2.) change around the headers from "printf("%s %c%c\n ", "Content-Type:text/html;charset=iso-8859-1",13,10);" to something more like:
It's not working still even after making changes as recommended by you and other resources after long digging through google. I have got latest version of Apache2 but i am getting following error during installation :
libapr.1.ld Error 134
Could anyone help me to install the apache working
libapr and libapr-util have to be around for Apache2, since it uses them. If you're installing from packages, make sure to get those and make sure those libraries are in a place Apache can find them (like /usr/lib). If you're building it, build and install those before Apache. Do you mean "/usr/lib/libapr-1.la"? I've never seen a file "libapr.1.ld". "libapr-1.la" should be installed along with the other libapr files. It's a libtool file used for linking the library when using libtool.
I have placed apr and apr-util folders in scrlib folder of the apache package. and when i run the ./configure it got no error and when i run the make command i got following error as i pasted screen the error :
If you have packages available to you, it's better to install those, as it is easier than building stuff yourself. Additionally, you shouldn't build in /usr/httpd. Use /tmp and install into /usr if it's a replacement to an operating system program, or /usr/local if it's an addition. I've never heard of a buffer overflow just from building something ...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.