Trouble with permissions when creating files

K

Kishore

Hi..

I am having trouble with permissions when creating files throught the
perl program.

When I run the perl program through http, it creates files with
permissions 0600.. but if I run the same program through shell, the
files are created with 0644 permission.

I am not sure if this is the correct group to request help from.

here is the code I am using..

use Fcntl ':flock'; # import LOCK_* constants
open(LOG, ">>$LOG_FILE") or die "Cannot open logfile '$LOG_FILE' for
writing: $!";
flock(LOG, LOCK_EX);
seek(LOG, 0, 2);
print LOG "this is the line \n";
flock(LOG, LOCK_UN);
close(LOG);

Thanks in advance for your help.
Kishore.
 
G

Gunnar Hjalmarsson

Kishore said:
I am having trouble with permissions when creating files throught the
perl program.

When I run the perl program through http, it creates files with
permissions 0600.. but if I run the same program through shell, the
files are created with 0644 permission.

Set umask to 022 before opening the file.
use Fcntl ':flock'; # import LOCK_* constants
open(LOG, ">>$LOG_FILE") or die "Cannot open logfile '$LOG_FILE' for
writing: $!";
flock(LOG, LOCK_EX);
seek(LOG, 0, 2);

AFAIK, using seek that way is redundant when you append to a file
using '>>'.
print LOG "this is the line \n";
flock(LOG, LOCK_UN);

That's also redundant. The file gets unlocked when you close the
filehandle.
 
B

Bob Walton

Kishore said:
Hi..

I am having trouble with permissions when creating files throught the
perl program.

When I run the perl program through http, it creates files with
permissions 0600.. but if I run the same program through shell, the
files are created with 0644 permission.

I am not sure if this is the correct group to request help from. ....

Kishore.

You need to read up on CGI and the specifics of how your web server runs
CGI scripts. This problem has nothing to do with Perl, in the sense
that if your CGI script were written in C, Fortran, Befunge, or any
other language, you would have the same problem. You might consider
asking in a CGI newsgroup, but you really really should read up first.
Hint: When your web server runs your script, it is running as a
different user, with different priviledges and default permissions.
 
G

Gregory Toomey

Kishore said:
Hi..

I am having trouble with permissions when creating files throught the
perl program.

When I run the perl program through http, it creates files with
permissions 0600.. but if I run the same program through shell, the
files are created with 0644 permission.

I am not sure if this is the correct group to request help from.

If you are using Apache then you could be running under a cgi wrapper. This
sets userid & permissions to "sane" values before running the Perl script
as cgi. Setting the umask may solve the problem.

See http://httpd.apache.org/docs/suexec.html

gtoomey
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top