Solved

CGI program doesn't return anything

Posted on 2004-10-28
286 Views
Last Modified: 2013-12-25
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
Question by:boycy
    14 Comments
     
    LVL 5

    Expert Comment

    by:ITcrow
    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
     
    LVL 13

    Expert Comment

    by:gripe
    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
     
    LVL 13

    Expert Comment

    by:gripe
    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
     
    LVL 6

    Author Comment

    by:boycy
    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
     
    LVL 6

    Author Comment

    by:boycy
    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
     
    LVL 5

    Expert Comment

    by:ITcrow
    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
     
    LVL 6

    Author Comment

    by:boycy
    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
     
    LVL 5

    Expert Comment

    by:ITcrow
    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
     
    LVL 6

    Author Comment

    by:boycy
    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
     
    LVL 5

    Expert Comment

    by:ITcrow
    I think it just executed your 'bash' shell.
    0
     
    LVL 6

    Author Comment

    by:boycy
    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
     
    LVL 6

    Author Comment

    by:boycy
    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
     
    LVL 5

    Expert Comment

    by:ITcrow
    Cool! something to remember while stripping the OS.
    0
     
    LVL 2

    Accepted Solution

    by:
    Closed, 125 points refunded.
    Lunchy
    Friendly Neighbourhood Community Support Admin
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Suggested Solutions

    Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
    Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

    875 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now