0

Basis of the problem. I have a university assignment requiring me to write a Perl/CGI based website for a phonebook. This part is fine and I'm happy with it, however, I have issues with wrapping the cgi files. I've done it once before no issues but been unable to replicate that success this time doing the same thing.

Basic Perl file to show user ID's:

#!/usr/bin/perl -w

use English;

print "Content-type: text/html";

print "\n";
print "\n";
print "\n";

print "<html>\n";
print "<head><title>IDS.CGI</title></head>\n";
print "<body>\n"; 
print "<p>\nMy User ID is $UID\n</p>";
print "<p>\nMy effective User ID is $EUID\n</p>";
print "<p>\nMy Group ID is $GID\n</p>";
print "<p>\nMy effective Group ID is $EGID\n</p>";
print "\n</body>\n";
print "</html>\n";

Wrapper.C:

#include <stdio.h>
#include <unistd.h>

#define REAL_PATH "ids.pl"

int
main()
{
    execl( REAL_PATH, REAL_PATH, 0 );
    printf( "You should never see this message!\n" );
}

This is throwing an internal server error 500. I've tried my best to debug it including spacing for the headers etc. It runs fine in terminal but not in the web browsers. The servers httpd error log shows that the error of "Premature end of headers". I cannot see how there is a premature end however.

Any help anyone can offer would be greatly appreciated.

Rory Perry
  • 97
  • 1
  • 10
  • This sort of thing is usually because the CGI program is dying, and the *"Premature end of headers"* just means it's not printing anything. Have you checked that both the C executable and the Perl script are executable by the system? I'm unsure why you're running a C program for your CGI. Won't your server handle Perl code directly? I suggest you modify your C program to just print `"Content-type: text/plain\n\nTest succeeded\n";` instead of trying to run your Perl program. That will eliminate the C code if it is successful – Borodin May 04 '16 at 04:00
  • Check out the solution here http://stackoverflow.com/questions/17583341/error-500-premature-end-of-script-headers, e.g. was the permission bits of the cgi program right? – fluter May 04 '16 at 04:01
  • 1
    What makes you think you need that wrapper? –  May 04 '16 at 04:46
  • Definitely not a permissions issue. C Wrapper is required to make the CGI executable as the owner of the file when served from the web server rather than as the 'nobody' default user. – Rory Perry May 04 '16 at 06:09
  • I'd guess it's a path problem, i.e. the C program doesn't _find_ the Perl program. Try `#define REAL_PATH "/full/path/to/ids.pl"`. – PerlDuck May 04 '16 at 10:55

2 Answers2

1

Like any system call, you should always be checking for an error from execl(). Normally you'd look at the return value, but it's not necessary because success will terminate the program.

execl( REAL_PATH, REAL_PATH, 0 );
perror("exec of '"REAL_PATH"' failed");

This uses perror to handle turning errno into a human readable error string and printing it to stderr.

I'd also avoid using #define for string constants, they're awkward to work with. Instead use static const char REAL_PATH[] = "ids.pl" as suggested in this answer.

And I don't understand why you need a C wrapper. Some sort of weird restriction on your web server running interpreted code?

Community
  • 1
  • 1
Schwern
  • 153,029
  • 25
  • 195
  • 336
0

I wasn't compiling the C Wrapper on the actual server therefore getting different machine codes which weren't compatible. Unfortunately, the server denied to compile it originally and I forgot when it did that it had to be compiled on that machine. Doh me!

Rory Perry
  • 97
  • 1
  • 10