LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 07-16-2016, 03:16 AM   #1
haroonrl
LQ Newbie
 
Registered: Jul 2016
Posts: 8

Rep: Reputation: Disabled
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.

please help me i am really stucked with it
 
Old 07-16-2016, 08:50 PM   #2
jayjwa
Member
 
Registered: Jul 2003
Location: NY
Distribution: Slackware, Termux
Posts: 770

Rep: Reputation: 242Reputation: 242Reputation: 242
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;
  }
}
With a full page:

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>CGI Test Form </title>
	<meta name="generator" content="Bluefish 2.2.4" >
	<meta name="author" content="jayjwa" >
	<meta name="date" content="2016-07-16T20:33:08-0400" >
	<meta name="copyright" content="GPL">
	<meta name="keywords" content="test">
	<meta name="description" content="A CGI test form">
	<meta name="ROBOTS" content="NOINDEX, NOFOLLOW">
	<meta http-equiv="content-type" content="text/html; charset=UTF-8">
	<meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8">
	<meta http-equiv="content-style-type" content="text/css">
	<meta http-equiv="expires" content="0">
	<meta >
</head>

<body>
<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>
</body>

</html>
I get on the command line:

Code:
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>
 
Old 07-17-2016, 04:02 AM   #3
haroonrl
LQ Newbie
 
Registered: Jul 2016
Posts: 8

Original Poster
Rep: Reputation: Disabled
Error The CGI was not CGI/1.1 compliant

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.

Thanks in advacnce
 
Old 07-17-2016, 11:58 AM   #4
jayjwa
Member
 
Registered: Jul 2003
Location: NY
Distribution: Slackware, Termux
Posts: 770

Rep: Reputation: 242Reputation: 242Reputation: 242
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:

Code:
printf("Content-type: text/html\r\n");
printf("\r\n");
Full:

Code:
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv) {
  char *data ;
  long m,n;

  printf("Content-Type: text/html\r\n");
  printf("\r\n");
  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;
  }
}
Which still works here. I'm sorry I don't have that webserver setup, so it's kind of hard to predict if it will like that output or not.
 
Old 07-17-2016, 12:28 PM   #5
haroonrl
LQ Newbie
 
Registered: Jul 2016
Posts: 8

Original Poster
Rep: Reputation: Disabled
Apache2 server installatio error

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

Thanks in advance
 
Old 07-17-2016, 08:30 PM   #6
jayjwa
Member
 
Registered: Jul 2003
Location: NY
Distribution: Slackware, Termux
Posts: 770

Rep: Reputation: 242Reputation: 242Reputation: 242
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.
 
Old 07-18-2016, 04:54 AM   #7
haroonrl
LQ Newbie
 
Registered: Jul 2016
Posts: 8

Original Poster
Rep: Reputation: Disabled
Apache installation apr error

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 :

Code:
root@harroon-Inspiron:/usr/httpd-2.4.23# make
Making all in srclib
make[1]: Entering directory `/usr/httpd-2.4.23/srclib'
Making all in apr
make[2]: Entering directory `/usr/httpd-2.4.23/srclib/apr'
make[3]: Entering directory `/usr/httpd-2.4.23/srclib/apr'
/bin/bash /usr/httpd-2.4.23/srclib/apr/libtool --silent --mode=link gcc -g -O2 -pthread   -DHAVE_CONFIG_H  -DLINUX -D_REENTRANT -D_GNU_SOURCE -D_LARGEFILE64_SOURCE   -I./include -I/usr/httpd-2.4.23/srclib/apr/include/arch/unix -I./include/arch/unix -I/usr/httpd-2.4.23/srclib/apr/include/arch/unix -I/usr/httpd-2.4.23/srclib/apr/include -I/usr/httpd-2.4.23/srclib/apr/include/private -I/usr/httpd-2.4.23/srclib/apr/include/private   -version-info 5:2:5    -o libapr-1.la -rpath /usr/local/apache2/lib encoding/apr_escape.lo passwd/apr_getpass.lo strings/apr_cpystrn.lo strings/apr_fnmatch.lo strings/apr_snprintf.lo strings/apr_strings.lo strings/apr_strnatcmp.lo strings/apr_strtok.lo tables/apr_hash.lo tables/apr_skiplist.lo tables/apr_tables.lo atomic/unix/builtins.lo atomic/unix/ia32.lo atomic/unix/mutex.lo atomic/unix/ppc.lo atomic/unix/s390.lo atomic/unix/solaris.lo dso/unix/dso.lo file_io/unix/buffer.lo file_io/unix/copy.lo file_io/unix/dir.lo file_io/unix/fileacc.lo file_io/unix/filedup.lo file_io/unix/filepath.lo file_io/unix/filepath_util.lo file_io/unix/filestat.lo file_io/unix/flock.lo file_io/unix/fullrw.lo file_io/unix/mktemp.lo file_io/unix/open.lo file_io/unix/pipe.lo file_io/unix/readwrite.lo file_io/unix/seek.lo file_io/unix/tempdir.lo locks/unix/global_mutex.lo locks/unix/proc_mutex.lo locks/unix/thread_cond.lo locks/unix/thread_mutex.lo locks/unix/thread_rwlock.lo memory/unix/apr_pools.lo misc/unix/charset.lo misc/unix/env.lo misc/unix/errorcodes.lo misc/unix/getopt.lo misc/unix/otherchild.lo misc/unix/rand.lo misc/unix/start.lo misc/unix/version.lo mmap/unix/common.lo mmap/unix/mmap.lo network_io/unix/inet_ntop.lo network_io/unix/inet_pton.lo network_io/unix/multicast.lo network_io/unix/sendrecv.lo network_io/unix/sockaddr.lo network_io/unix/socket_util.lo network_io/unix/sockets.lo network_io/unix/sockopt.lo poll/unix/epoll.lo poll/unix/kqueue.lo poll/unix/poll.lo poll/unix/pollcb.lo poll/unix/pollset.lo poll/unix/port.lo poll/unix/select.lo poll/unix/z_asio.lo random/unix/apr_random.lo random/unix/sha2.lo random/unix/sha2_glue.lo shmem/unix/shm.lo support/unix/waitio.lo threadproc/unix/proc.lo threadproc/unix/procsup.lo threadproc/unix/signals.lo threadproc/unix/thread.lo threadproc/unix/threadpriv.lo time/unix/time.lo time/unix/timestr.lo user/unix/groupinfo.lo user/unix/userinfo.lo   -lrt -lcrypt  -lpthread -ldl
*** buffer overflow detected ***: ar terminated
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x68e4e)[0x400a7e4e]
/lib/i386-linux-gnu/libc.so.6(__fortify_fail+0x6b)[0x4013a85b]
/lib/i386-linux-gnu/libc.so.6(+0xfa6ea)[0x401396ea]
/lib/i386-linux-gnu/libc.so.6(+0xf9e48)[0x40138e48]
/lib/i386-linux-gnu/libc.so.6(_IO_default_xsputn+0x8e)[0x400afc0e]
/lib/i386-linux-gnu/libc.so.6(_IO_padn+0xc9)[0x400a3f29]
/lib/i386-linux-gnu/libc.so.6(_IO_vfprintf+0x3033)[0x400856d3]
/lib/i386-linux-gnu/libc.so.6(__vsprintf_chk+0xb1)[0x40138f01]
/lib/i386-linux-gnu/libc.so.6(__sprintf_chk+0x2f)[0x40138e2f]
ar[0x804dce3]
ar[0x804f8d3]
ar[0x8052c48]
ar[0x804be07]
ar[0x804a37e]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0x40058a83]
ar[0x804a456]
======= Memory map: ========
08048000-080a7000 r-xp 00000000 08:05 690270     /usr/local/bin/ar
080a7000-080a8000 r--p 0005e000 08:05 690270     /usr/local/bin/ar
080a8000-080a9000 rw-p 0005f000 08:05 690270     /usr/local/bin/ar
09c22000-09c85000 rw-p 00000000 00:00 0          [heap]
40000000-40020000 r-xp 00000000 08:05 1836729    /lib/i386-linux-gnu/ld-2.19.so
40020000-40021000 r--p 0001f000 08:05 1836729    /lib/i386-linux-gnu/ld-2.19.so
40021000-40022000 rw-p 00020000 08:05 1836729    /lib/i386-linux-gnu/ld-2.19.so
40022000-40024000 r--p 00000000 00:00 0          [vvar]
40024000-40026000 r-xp 00000000 00:00 0          [vdso]
40026000-40029000 rw-p 00000000 00:00 0 
4003f000-401e7000 r-xp 00000000 08:05 1836680    /lib/i386-linux-gnu/libc-2.19.so
401e7000-401e9000 r--p 001a8000 08:05 1836680    /lib/i386-linux-gnu/libc-2.19.so
401e9000-401ea000 rw-p 001aa000 08:05 1836680    /lib/i386-linux-gnu/libc-2.19.so
401ea000-401ee000 rw-p 00000000 00:00 0 
40205000-40221000 r-xp 00000000 08:05 1832607    /lib/i386-linux-gnu/libgcc_s.so.1
40221000-40222000 rw-p 0001b000 08:05 1832607    /lib/i386-linux-gnu/libgcc_s.so.1
bfa82000-bfaa3000 rw-p 00000000 00:00 0          [stack]
/usr/httpd-2.4.23/srclib/apr/libtool: line 1723: 14070 Aborted                 ar cru .libs/libapr-1.a encoding/apr_escape.o passwd/apr_getpass.o strings/apr_cpystrn.o strings/apr_fnmatch.o strings/apr_snprintf.o strings/apr_strings.o strings/apr_strnatcmp.o strings/apr_strtok.o tables/apr_hash.o tables/apr_skiplist.o tables/apr_tables.o atomic/unix/builtins.o atomic/unix/ia32.o atomic/unix/mutex.o atomic/unix/ppc.o atomic/unix/s390.o atomic/unix/solaris.o dso/unix/dso.o file_io/unix/buffer.o file_io/unix/copy.o file_io/unix/dir.o file_io/unix/fileacc.o file_io/unix/filedup.o file_io/unix/filepath.o file_io/unix/filepath_util.o file_io/unix/filestat.o file_io/unix/flock.o file_io/unix/fullrw.o file_io/unix/mktemp.o file_io/unix/open.o file_io/unix/pipe.o file_io/unix/readwrite.o file_io/unix/seek.o file_io/unix/tempdir.o locks/unix/global_mutex.o locks/unix/proc_mutex.o locks/unix/thread_cond.o locks/unix/thread_mutex.o locks/unix/thread_rwlock.o memory/unix/apr_pools.o misc/unix/charset.o misc/unix/env.o misc/unix/errorcodes.o misc/unix/getopt.o misc/unix/otherchild.o misc/unix/rand.o misc/unix/start.o misc/unix/version.o mmap/unix/common.o mmap/unix/mmap.o network_io/unix/inet_ntop.o network_io/unix/inet_pton.o network_io/unix/multicast.o network_io/unix/sendrecv.o network_io/unix/sockaddr.o network_io/unix/socket_util.o network_io/unix/sockets.o network_io/unix/sockopt.o poll/unix/epoll.o poll/unix/kqueue.o poll/unix/poll.o poll/unix/pollcb.o poll/unix/pollset.o poll/unix/port.o poll/unix/select.o poll/unix/z_asio.o random/unix/apr_random.o random/unix/sha2.o random/unix/sha2_glue.o shmem/unix/shm.o support/unix/waitio.o threadproc/unix/proc.o threadproc/unix/procsup.o threadproc/unix/signals.o threadproc/unix/thread.o threadproc/unix/threadpriv.o time/unix/time.o time/unix/timestr.o user/unix/groupinfo.o user/unix/userinfo.o
make[3]: *** [libapr-1.la] Error 134
make[3]: Leaving directory `/usr/httpd-2.4.23/srclib/apr'
make[2]: *** [all-recursive] Error 1
make[2]: Leaving directory `/usr/httpd-2.4.23/srclib/apr'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/usr/httpd-2.4.23/srclib'
make: *** [all-recursive] Error 1
root@harroon-Inspiron:/usr/httpd-2.4.23# ^C

please help me out ..
 
Old 07-21-2016, 10:12 PM   #8
jayjwa
Member
 
Registered: Jul 2003
Location: NY
Distribution: Slackware, Termux
Posts: 770

Rep: Reputation: 242Reputation: 242Reputation: 242
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 ...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
facing issue while executing cgi-bin script in RHEL 6 anas_lko Linux - Server 1 03-02-2012 06:17 AM
Apache2 cgi-bin perl - My browser just wants to download the .pl files keenboy Linux - Server 4 05-19-2010 09:42 AM
Permissions of web server folders(cgi-bin, var/www/html) niner710 Linux - Newbie 14 05-14-2008 10:54 AM
cgi script downloads - instead of executing sathiyamoorthy Linux - Server 1 04-23-2008 08:24 AM
Can not access cgi-bin from browser. Spreegem Linux - Software 6 11-07-2005 07:22 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 05:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration