Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CGI can't write file outside of cgi-bin dir?

Status
Not open for further replies.

pcorchary

MIS
Feb 23, 2002
60
US
Subject sez it all ... why?

This is on Apache 1.3.

I want all CGIs to write a log to another directory, and one CGI needs to create a text-file report in another directory.

I could have a periodic cron script that moves them, but why?

Thanks in advance.
 
It's probably down to permissions. If you running under Linux, then check that the directory is accessible by the webuser (the user that apache runs under, which is usually something like apache, nobody or guest).

In your code try using the test directives (-e [exists], -w [writeable]) to see whether the directory and/or file have the correct permissions to let you access them.

HTH,
Barbie. Leader of Birmingham Perl Mongers
 
Thanks for the response, and greetings from the other side of the pond!

Maybe ... I'm not new to *nix.... I was rather thinking there was some config in my default Mandrake/Apache install ...

Here's what I'm seeing:

$pwd
/var/www
$ ls -la (abbrv)
total 36
drwxrwxr-x 7 apache apache 4096 Mar 4 12:20 ./
drwxr-xr-x 19 root root 4096 Feb 18 19:40 ../
-rw------- 1 web web 1940 Mar 4 17:44 .bash_history
drwxrwxrwx 3 root root 4096 Mar 6 18:44 cgi-bin/
drwxrwxr-x 7 apache apache 4096 Mar 6 10:08 html/

The CGI itself (in:
-rwxr-xr-x 1 root root 14294 Mar 6 16:00 statustest.cgi*

httpd processes run as "apache" (should I change that to nobody like I would under netscape?)

I've tried writting out from Perl CGI to everywhere, including even /tmp. Mostly I was to write to:

$ pwd
/var/$ ls -l (abbrv)
drwxr-xr-x 2 root root 4096 Mar 6 10:41 docs/

This is my Perl sub: ($DOC is set externally)
171 sub save_file {
172 $FILEPATH="/docs/$DOC";
173 open(REPORT,">>$FILEPATH") || &save_error;
174 print REPORT &html_header();
175 print REPORT &page_header_output($TECH);
176
177 print REPORT &table_start();
178 for ( $n=1; $n <= 5; $n++ ) {
179 print REPORT &print_row_output($n);
180 }
181 print REPORT &quot;</table><p>&quot;;
182 print REPORT &quot;<P><textarea name=\&quot;comments\&quot; cols=\&quot;60\&quot; rows=\&quot;
9\&quot; wrap=\&quot;VIRTUAL\&quot;>$formdata{'comments'}</textarea>&quot;;
183
184 print REPORT &html_footer();
185 close(REPORT);
186 }

My error routine is:

161 sub save_error {
162 print &quot;<B>ERROR: something happened when I tried to save the file on the server!</b>&quot;;
163 }


And the error I'm seeing is:

print() on closed filehandle REPORT at /var/ line 174.
print() on closed filehandle REPORT at /var/ line 175.
print() on closed filehandle REPORT at /var/ line 177.
print() on closed filehandle REPORT at /var/ line 179.
print() on closed filehandle REPORT at /var/ line 179.
print() on closed filehandle REPORT at /var/ line 179.
print() on closed filehandle REPORT at /var/ line 179.
print() on closed filehandle REPORT at /var/ line 179.
print() on closed filehandle REPORT at /var/ line 181.
print() on closed filehandle REPORT at /var/ line 182.
print() on closed filehandle REPORT at /var/ line 184.
File &quot;/docs/SR03062002-030558.doc&quot; not found
<br> at /var/ line 198.
 
I agree with Barbie.
My first best guess for the
cause of you problem is permissions. Either the file
you are trying to open is not open to your web daemon
or the directory you are trying to write to is not.

You might try using a simple sub to dump the OS complaint
to the browser... like,

open(HANDLE,&quot;>>/path/to/file&quot;)
or showError(&quot;Failed to open output file, $!&quot;);

sub showError
{
my $gripe = shift;
print &quot;<p>$gripe</p></body></html>\n&quot;;
exit;
}

That way, if the open fails you get immediate notice and
the code exits. That might give you a little better
feedback from your error checking.

'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Sorry for not reply for ages ... its a long story.

Firstly, one problem is this:

172 $FILEPATH=&quot;/docs/$DOC&quot;;

you are specifying a directory &quot;docs&quot; under the root directory, not as you perhaps intended under your website directory. Change to:

$FILEPATH=&quot;../docs/$DOC&quot;;

and you should have a better result.

Secondly, with the following

173 open(REPORT,&quot;>>$FILEPATH&quot;) || &save_error;

You are calling the function save_error(), which doesn't have an exit in, so carries on trying to process the rest of the code.

HTH,
Barbie.
Leader of Birmingham Perl Mongers
 
Even better than using
Code:
$FILEPATH=&quot;../docs/$DOC&quot;;
is using:
Code:
$FILEPATH=&quot;$ENV{'DOCUMENT_ROOT'}/docs/$DOC&quot;;
That way you'll always know that it's going into the docs directory at the root of your web site, even if your cgi directory is in some bizarre place, as some ISPs are wont to do. It pays never to make any assumptions about things like the location of the cgi directory.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top