• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 312
  • Last Modified:

CGI program doesn't return anything

I am running thttpd on a (very) cut down version of Red Hat 7. I've written the following C program:

#include <stdio.h>
int main(void)
{
   printf("Content-Type:text/html;charset=iso-8859-1\n\n");
   printf("Hello world...\n");

   return 0;
}


The program works fine when run from the shell and does what it should. I've put it under my www root as /cgi-bin/test.cgi
When I try to access it via my web browser, it is found (no 'document does not exist' error) and is being run as CGI (no 'not a cgi program' error), but just doesn't return anything. I've tried replacing the "\n\n" with "\r\n\r\n" to see if that was the problem but just can't get anything out of it. No errors, nothing.

Does anyone know why this might not work?
Cheers,
--Rob
0
boycy
Asked:
boycy
  • 6
  • 5
  • 2
  • +1
1 Solution
 
ITcrowCommented:
Try:

#include <stdio.h>
int main(void)
{
   printf("Content-Type:text/html\n\n");
   printf("Hello world...\n");

   return 0;
}

May be your header is not completely understood.
0
 
gripeCommented:
It sounds like you haven't configured your cgi directory properly. How are you executing thttpd?

The CGI_PATTERN define or the argument to the '-c' option is relative to the root directory thttpd runs in. By default this directory is the same one you execute it from or you can specify it by using the '-d' option.

As an example, let's say I have my web server at: /home/http
and my cgi directory is at: /home/http/cgi-bin

I will want to start thttpd with the following command to set up the cgi-bin directory:

thttpd -d /home/http -c '/cgi-bin/*'

Note the single quotes (') around cgi-bin to avoid interpolation of the '*' wildcard by the shell.

Could you post the command you're using to start thttpd if this doesn't help?

Thanks
0
 
gripeCommented:
As a side note, there is no problem with your program. I compiled it exactly as you posted and ran it as a CGI with thttpd v2.25b on FreeBSD 4.9. I would suggest a space between 'Content-type:' and your content type information though, as I think this is what the spec calls for. IE:

printf("Content-Type: text/html;charset=iso-8859-1\n\n");
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
boycyAuthor Commented:
ITcrow: nope, that program does exactly the same thing - runs but no output from web server.

gripe: cheers for the spec pointer.
thttpd is being run with the following command:

thttpd -C /etc/thttpd.conf

I'm sure that the configuration for CGI_PATTERN is correct. It's defined in thttpd.conf as follows:
cgipat=/cgi-bin/*

I say I'm sure it's correct because if I change it eg to *.x then I get an error accessing test.cgi saying that it's a cgi program but not allowed to run as cgi.
...

--Rob
0
 
boycyAuthor Commented:
It's definitely a configuration problem: the following bash script 'works', just not when run as CGI...

echo -e "Content-type: text/html\n\n"
echo "Hello world..."
0
 
ITcrowCommented:
Have you tried looking at access log or error log ?
Could you post it here, what is logged in the file when you access the script through web.
0
 
boycyAuthor Commented:
The access log is this:
192.168.1.99 - - [12/Nov/1980:20:19:22 +0000] "GET /cgi-bin/bash.cgi HTTP/1.1" 200 50000 "" "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20040913 Firefox/0.10.1"

I get same result using IE.
The following is the HTTP conversation recorded by ethereal:

GET /cgi-bin/bash.cgi HTTP/1.1
Host: 192.168.1.103
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20040913 Firefox/0.10.1
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive

HTTP/1.0 200 OK


As you can see, all seems fine, but the script just doesn't look like it's actually being run.
Grrrrrrr...

--Rob :D
0
 
ITcrowCommented:
Did you notice the amount of data transfered: 50000
Where as for your CGI it should have been about 25 to 30
192.168.1.99 - - [12/Nov/1980:20:19:22 +0000] "GET /cgi-bin/bash.cgi HTTP/1.1" 200 50000

It is not a good idea to name your script as bash.cgi
As httpd in certain cases will behave as follows:
/cgi-bin/bash.cgi    =    /cgi-bin/bash
Which can confuse it with bash, if the server implementation considers paths first.
0
 
boycyAuthor Commented:
The 50000 confused me too...I have no idea why it says that. There is no output following that message, certainly not 50KBs' worth of data!
I'll rename the script too :)

--Rob
0
 
ITcrowCommented:
I think it just executed your 'bash' shell.
0
 
boycyAuthor Commented:
I don't think it did - I renamed it and still get exactly the same. I also get a content-size of 50000 in the log *whatever* I return. I used a program that just returns without any output and the log reported that as sending 50000.
Something odd is going on...

--Rob
0
 
boycyAuthor Commented:
Ok problem has been solved - The company that manufacture the embedded system I'm working on (and who customised the red hat build) replied to me.
The problem was that the web server was being run in a chroot jail, which I didn't think was a problem as everything was under the www root.
Everything...except the C libraries! And the bash interpreter I assume.

All solved and working like magic now :)
Cheers for your help ITcrow.

--Rob
0
 
ITcrowCommented:
Cool! something to remember while stripping the OS.
0
 
LunchyCommented:
Closed, 125 points refunded.
Lunchy
Friendly Neighbourhood Community Support Admin
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now