# CGI/perl

## SPo0n

what group must my user be in to enable execution of perl or cgi scripts? It all works fine from /home/httpd/cgi-bin or /home/httpd/perl but when i try with my user it either displays the text, tries to download the file or gives a 500 internal server error with "Unexpected end of headers" or similar error. 

Ideas please  :Smile: 

----------

## slartibartfasz

just to clarifiy things:

it works when u execute a perl programm from the console and the same script does not work when u execute it via apache, right?

probably your apache is not set up correctly - look at the apache configfiles (/etc/apache/conf/apache.conf and commonapache.conf) and read carefully everything concerning CGI therein - make sure that apache has proper permissions on the scripts. u can get more details by setting the apache loglevel up one or two notches and do a 'tail -f /var/log/apache/error_log' while calling the script. make sure that u restart apache after every change u make to the confs.

----------

## SPo0n

I've already made the correct changes to the apache config - and triple checked.

```
bash-2.05b# tail -f /var/log/apache/error_log 

[Tue May 13 17:16:37 2003] [error] (13)Permission denied: exec of /home/httpd/cgi-bin/index.cgi failed

[Tue May 13 17:16:37 2003] [error] [client 192.168.0.5] Premature end of script headers: /home/httpd/cgi-bin/index.cgi

[Tue May 13 17:16:39 2003] [error] (13)Permission denied: exec of /home/httpd/cgi-bin/index.cgi failed

[Tue May 13 17:16:39 2003] [error] [client 192.168.0.5] Premature end of script headers: /home/httpd/cgi-bin/index.cgi

[Tue May 13 17:16:40 2003] [error] (13)Permission denied: exec of /home/httpd/cgi-bin/index.cgi failed

[Tue May 13 17:16:40 2003] [error] [client 192.168.0.5] Premature end of script headers: /home/httpd/cgi-bin/index.cgi

[Tue May 13 17:20:29 2003] [error] (2)No such file or directory: exec of /home/httpd/cgi-bin/index.cgi failed

[Tue May 13 17:20:29 2003] [error] [client 192.168.0.5] Premature end of script headers: /home/httpd/cgi-bin/index.cgi

[Tue May 13 17:20:31 2003] [error] (2)No such file or directory: exec of /home/httpd/cgi-bin/index.cgi failed

[Tue May 13 17:20:31 2003] [error] [client 192.168.0.5] Premature end of script headers: /home/httpd/cgi-bin/index.cgi

```

I forgot to chmod the file (created a new script to test, moved the old one to another file) which was causing exec errors. 

The last line is the error i keep getting, i've really lost hope on how to fix it  :Sad: 

it works fine from the command line

```
bash-2.05b# ./index.cgi 

Unquoted string "left" may clash with future reserved word at ./index.cgi line 10.

Content-Type: text/html; charset=ISO-8859-1

<?xml version="1.0" encoding="iso-8859-1"?>

<!DOCTYPE html

        PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US"><head><title>hello world</title>

</head><body><h1 align="left">This page is located at http://localhost</h1></body></html>bash-2.05b# 

```

Here's the source of the file if it's of interest: 

```
#!/usr/bin/perl -w

use CGI; #load CGI

$x = new CGI; #new CGI object

$url = $x->url(-base => 1);

print   $x->header, #create http header

        $x->start_html('hello world'), #start html

        $x->h1({-align=>left},"This page is located at $url"), #<h1>

        $x->end_html; #end html

exit(0);

```

What can i do? Is there something i'm not running, or haven't enabled?

Edit: well what a suprise, i switched to my other tab after posting this, just to check the error again.. and it worked (BUT i didn't change anything!!!!). But it still isn't working at all for normal users...

----------

## slartibartfasz

what do u mean with 'normal users'? command line? apache? some yes some no? root only?

and u should always 'use strict;' in your scripts!

----------

## SPo0n

yes i know i should use strict but that was a quick script i knocked up to test

By normal users i mean any user - aka my user named jamie. I want to be able to use both shell and apache perl/cgi.

----------

## slartibartfasz

ok: it works for apache, it works for root but for no one else.

what it the errormessage if anyone else tries to execute it? double check the permissions on the file or 'chmod o+x file'. if this still doesnt help - compare the output of the 'env' command for jamie and root.

----------

## SPo0n

it seems to let me execute it as jamie (the file in /home/httpd/cgi-bin) from the shell. no errors at all, exactly the same output as if root did it.

Even if i su root it gives me the env stuff for jamie, rather than root, how can i get around that?

----------

## splooge

su - jamie

edit: I think I meant su - root.  =)  that should bring over the new environment stuffs.

I have no idea if this is the problem or not: but have you tried setting the permissions to be world executable?  That might help (though it might be insecure, but your script looks pretty safe ;p)

chmod xx5 /path/to/script

alternatively:

chmod o+x /path/to/script

I'm probably way off here but whatever.   =)

----------

## SPo0n

i already tried o+x and still get the 500 internal server error  :Sad: 

I guess i'll have to edit my scripts as root and use them from httpd's folder

----------

## slartibartfasz

 *Quote:*   

> But it still isn't working at all for normal users...

 

 *SPo0n wrote:*   

> it seems to let me execute it as jamie (the file in /home/httpd/cgi-bin) from the shell. no errors at all, exactly the same output as if root did it.

 

aehm - where is the problem now? sorry if i'm a little bit slow but u say it works in apache, it works as root and it works for jamie...  :Question: 

----------

## splooge

Yeah apparently I have a huge reading comprehension problem this time of the morning ...

    # ScriptAlias: This controls which directories contain server scripts.

    # ScriptAliases are essentially the same as Aliases, except that

    # documents in the realname directory are treated as applications and

    # run by the server when requested rather than as documents sent to the cli$

    # The same rules about trailing "/" apply to ScriptAlias directives as to

    # Alias.

    #

    ScriptAlias /cgi-bin/ /home/httpd/cgi-bin/

Does that help at all?

----------

## SPo0n

 *slartibartfasz wrote:*   

>  *Quote:*   But it still isn't working at all for normal users... 
> 
>  *SPo0n wrote:*   it seems to let me execute it as jamie (the file in /home/httpd/cgi-bin) from the shell. no errors at all, exactly the same output as if root did it. 
> 
> aehm - where is the problem now? sorry if i'm a little bit slow but u say it works in apache, it works as root and it works for jamie... 

 

no the problem is it doesn't work in apache for my regular user.

i'll check (again) that i have the script alias thing in my apache config(s).

----------

## DefconAlpha

You could always try re-emerging apache and mod_perl at the same time again. My apache always ran scripts right out of the box when i freshly emerged it. I never got a bunch of user cgi directories working, but i did have the main cgi-bin working just lovely... (even with python too :P )

You will definitely want to run the etc-update and overwrite your old file with the new one, so back up your existing configs and see if that works.

Also, give the output of `ls -l /path/to/script'

It will help a little bit... Oh, is apache running as nobody or apache? if the script isn't readable by the apache process it won't be executed at all...

Hope this helps...

----------

## slartibartfasz

 *SPo0n wrote:*   

> no the problem is it doesn't work in apache for my regular user

 

ah - we are getting closer now  :Wink:  - ok: apache doesnt usually change how it works on behalf of the client that connects. it will allways execute scripts as 'apache' (or as whatever u set it to in the config file).

i see only three potential causes for the error:

1) the browser u use for your normal user sends not the same requests as the one for root --> your problem is the browser and NOT apache NOR perl

2) apache itself does different things with connections from jamie --> there must be some setting in the apache config (or the CGI.pm) that causes this behaviour

3) u start apache once with root and then with jamie and it doesnt switch to another user (as specified in the configs) --> permission and config problem

hope that helps u - i'm getting myself a beer - its getting late  :Cool: 

----------

## SPo0n

Ok umereged and recompiling the lot (apache, mod_php, mod_perl, CGI)

i was thinking of doing this before  :Smile: 

----------

## jimlynch11

i too am having problems.

i have a fresh emerge of apache, mod_perl, & CGI, yet i get a 500 server error when trying to run a simple perl_test.cgi script through lynx

----------

## jimlynch11

update...i got the cgi to work, and after some problems, the ikonboard installer.cgi script worked fine...but now the ikonboard.cgi script which it created wont work, with a 500 server error again. any ideas? i know its a server problem because thats the same message i got before installer.cgi started working magically.

----------

## uzik

I think you have a bug in your perl, not in your apache.

Sounds like it's a permissions problem.

----------

## jimlynch11

yeah, i looked at the ikonboard.cgi file last night and compared it side by side with the working one, and for some reason, there was a space inserted between the header and the rest of the script in the working one, but not in the ikonboard.cgi file...once i inserted a space into that file's header, all was right with the world...thanks for the help

----------

