Forum Moderators: coopster & phranque

Message Too Old, No Replies

File::Copy throwing "permission denied"

         

csdude55

7:44 pm on Oct 29, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm a little rusty in my Perl, forgive me if this is an obvious question :-/

I'm having an issue with one script where files are disappearing during processing, and I can't find exactly why. So I want to copy the files to a backup at the very beginning of the script, just in case.

I wrote a test script:

# test.cgi
#!/usr/bin/perl

use CGI::Carp qw(fatalsToBrowser);
use File::Copy;

if (-e "/home/example/data/copy.txt") {
copy("/home/example/data/copy.txt","/home/example/data/copy2.dat") or die "Copy failed: $!";
}
print "Done";
exit;


and it throws the following error:

Copy failed: Permission denied at blah blah blah

The /home/example/data directory is set to 0755, and copy.txt is 0777. Both test.cgi and copy.txt are owned by root root.

I originally tried it with copy.txt in the cgi-bin with test.cgi and had the same error, so I moved it to /data/ where other files (all 0777) are regularly modified and updated with no problem. So the issue shouldn't be the directory, at least.

Any suggestions?

csdude55

6:24 am on Oct 30, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



FWIW, I installed File::Copy::Recursive and use rcopy(), but it throws the same error. This is becoming super frustrating :-/

robzilla

10:07 am on Oct 30, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Both test.cgi and copy.txt are owned by root root.

And the cgi script is executed by... what user? Not root, presumably? And who owns the folder?

csdude55

5:55 pm on Oct 30, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I was running the script via browser, so I guess that's "public"? I'm not 100% sure on that one, it's not something I've had to think about in 25 years! LOL

I figured out the problem, though. I had to change all permissions to 777 instead of 755.

On my old server, root/root could do anything so I had all writable directories set to 755. I'm not sure if there's a setting somewhere on my new server, or if it's related to the Perl update that came along with the transition, but at some point I guess this changed.

robzilla

6:39 pm on Oct 30, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Root can do anything but if your script does not run as root and the folder has 755 permissions (i.e. read + execute, but not write) and root/root as owner/group then of course that non-root user will not have write access.