PERL can't open file for logging (world writable directory Windows XP Home/ Active Perl / Apache)

P

PGPS

Hi,
1. I have a perl program which logs user inputs. It was working fine on
Linux system.

2. I moved my entire work onto my home PC with XP-Home/Active
Perl/Apache and running the program as a cgi-script. It was working
fine and was able to perform logs as usual, but I was unable to view
it.

3. Apache woudn't display .html files if they are in cgi-bin directory.
I had to move it out to htdocs and change the path of the file in PERL
program to point to the file in htdocs.

4. With this setup, I get an error "Cannot open file" in the error logs
of Apache. I can view the file on the internet, but the program is not
able to open to save the logs.

Can someone tell me what's going on. And how to fix it. Either at
Apache end (to be able to view .html files in cgi-bin directory) OR on
the PERL end, where I want to be able to make the file world writable
and perform logs.

Thanks.
 
G

Gunnar Hjalmarsson

PGPS said:
1. I have a perl program which logs user inputs. It was working fine on
Linux system.

2. I moved my entire work onto my home PC with XP-Home/Active
Perl/Apache and running the program as a cgi-script. It was working
fine and was able to perform logs as usual, but I was unable to view
it.

3. Apache woudn't display .html files if they are in cgi-bin directory.

That's how it should be.
I had to move it out to htdocs and change the path of the file in PERL
program to point to the file in htdocs.

4. With this setup, I get an error "Cannot open file" in the error logs
of Apache. I can view the file on the internet, but the program is not
able to open to save the logs.

Can someone tell me what's going on. And how to fix it.

Not based on what you've told us.
Either at
Apache end (to be able to view .html files in cgi-bin directory) OR on
the PERL end, where I want to be able to make the file world writable
and perform logs.

I think there is some fundamental misconception here. The fact that you
cannot read files with your browser when they are located in the cgi-bin
is unrelated to whether they can be opened for writing. Neither of these
things is Perl related.

Nevertheless, if you post some Perl code, somebody may be able to put
you in the right direction.
 
S

Sisyphus

..
..
4. With this setup, I get an error "Cannot open file" in the error logs
of Apache.

Can you extend that error message to include the name of the file that perl
couldn't open (in case it's not the file you think it was), and to include
the contents of $!.

Cheers,
Rob
 
P

PGPS

X-No-Archive:Yes

Thanks. I put the same question in Apache groups and I got a response
saying that "That's how it should be".

My question is: "What's the solution to get it working". Since, on the
Linux machine, on which I just had a user login (on someone else's
server), I could get it working. So, the configuration was changed. The
problem is not just HTML, I would like all the depended filies like
*.js, *.jpg, *.css, *.gif to be in the same directory as the program.

Anyway, the above is a Apache configuration problem. Below is my PERL
problem.

Below is the code:

open(LOGFILE, ">>/logs.html");
print LOGFILE "Some stuff here\n";
close(LOGFILE);

The error in the apache access logs is something to the effect that
"Cannot open file" and it points to the line number in the PERL program
which has this line:

open(LOGFILE, ">>/logs.html");

DIRECTORY STRUCTURE
[htdocs]
logs.html
[cgi-bin]
[program1]
prog1.cgi

htdocs and cgi-bin directories are in the same parent directory.
htdocs has logs.html file
cgi-bin has a folder called program1 and program1 folder has prog1.cgi



Thanks.
 
S

Sisyphus

..
..
The error in the apache access logs is something to the effect that
"Cannot open file" and it points to the line number in the PERL program
which has this line:

open(LOGFILE, ">>/logs.html");

Beats me as to why that error appears in the apache logs.

I could understand it if the line in question was:

open(LOGFILE, ">>/logs.html") or die "Cannot open file";

in which case I would recommend changing it to:

open(LOGFILE, ">>/logs.html") or die "Cannot open file: $!";

(In fact, change it to that even if the code *does* read exactly as you
posted.)

The "$!" bit should provide some hint as to why the failure is occurring.

Cheers,
Rob
 
T

Tad McClellan

PGPS said:
X-No-Archive:Yes


Why do you want to (attempt to) set no archive?

(it does not work when you include it in the body like that, it
needs to be in the headers.
)

I would like all the depended filies like
*.js, *.jpg, *.css, *.gif to be in the same directory as the program.


Why would you like all the dependent files to be in the same
directory as the program?

The error in the apache access logs is something to the effect that
^^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^^^^^^

Please post the exact text of any messages that you are getting.

The devil is in the details when debugging programs...



Have you seen the Posting Guidelines that are posted here frequently?


[snip TOFU]
 
J

J. Gleixner

PGPS wrote:
[...]
open(LOGFILE, ">>/logs.html");

You have a file in your root ( '/' ) directory that's writable by
everyone??? Use the full path to the file.
 
M

Mumia W. (reading news)

Sisyphus said:
.
.
Can you extend that error message to include the name of the file that perl
couldn't open (in case it's not the file you think it was), and to include
the contents of $!.

Cheers,
Rob


[...] I put the same question in Apache groups and I got a response
saying that "That's how it should be".

My question is: "What's the solution to get it working". Since, on the
Linux machine, on which I just had a user login (on someone else's
server), I could get it working. So, the configuration was changed. The
problem is not just HTML, I would like all the depended filies like
*.js, *.jpg, *.css, *.gif to be in the same directory as the program.

Anyway, the above is a Apache configuration problem. Below is my PERL
problem.

Below is the code:

open(LOGFILE, ">>/logs.html");
print LOGFILE "Some stuff here\n";
close(LOGFILE);

The error in the apache access logs is something to the effect that
"Cannot open file" and it points to the line number in the PERL program
which has this line:

open(LOGFILE, ">>/logs.html");

Why are you trying to write in the root directory? Does your web-server
has permission to write there?
DIRECTORY STRUCTURE
[htdocs]
logs.html
[cgi-bin]
[program1]
prog1.cgi

htdocs and cgi-bin directories are in the same parent directory.
htdocs has logs.html file
cgi-bin has a folder called program1 and program1 folder has prog1.cgi



Thanks.

1) The /.../htdocs directory almost certainly has different permissions
than the / (root) directory.

2) Often, web-servers don't have permission to write into their own
..html files because an intruder who manages to break into the web-server
can easily deface the website. You might have to change the permissions
on /.../htdocs/ and deal with the consequences.
 
P

PGPS

Thank you all. I have been using relative path for my log files. When I
used the absolute path, it worked. The confusion started with my
changing all other paths to relative paths (images, .css etc.) and it
worked fine, except for the log files.

Thanks.
Joe said:
PGPS said:
open(LOGFILE, ">>/logs.html");

DIRECTORY STRUCTURE
[htdocs]
logs.html
[cgi-bin]
[program1]
prog1.cgi

That open() statement is definitely wrong.

my $file = "$ENV{DOCUMENT_ROOT}/logs.html"; # Use full path name
open(LOGFILE, ">>$file") or die "Cannot append to $file: $!\n";

To access anything in htdocs, you need to either use a hardcoded
absolute pathname (such as "/var/www/htdocs/logs.html") or use
an ENV variable (such as what I've shown above).

Remember, when the web server receives a request for "/logs.html"
(such as from <a href="/logs.html">Log files</a>), it converts that
string to a file name by using ServerRoot and/or DocumentRoot.
Your CGI program needs to do the same thing; it cannot simply
use the string "/logs.html" as it and expect it to work.

-Joe
 
P

PGPS

X-No-Archive: YES
Thank you all. I have been using relative path for my log files. When I
used the absolute path, it worked. The confusion started with my
changing all other paths to relative paths (images, .css etc.) and it
worked fine, except for the log files.

Thanks.
Joe said:
PGPS said:
open(LOGFILE, ">>/logs.html");

DIRECTORY STRUCTURE
[htdocs]
logs.html
[cgi-bin]
[program1]
prog1.cgi

That open() statement is definitely wrong.

my $file = "$ENV{DOCUMENT_ROOT}/logs.html"; # Use full path name
open(LOGFILE, ">>$file") or die "Cannot append to $file: $!\n";

To access anything in htdocs, you need to either use a hardcoded
absolute pathname (such as "/var/www/htdocs/logs.html") or use
an ENV variable (such as what I've shown above).

Remember, when the web server receives a request for "/logs.html"
(such as from <a href="/logs.html">Log files</a>), it converts that
string to a file name by using ServerRoot and/or DocumentRoot.
Your CGI program needs to do the same thing; it cannot simply
use the string "/logs.html" as it and expect it to work.

-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top