Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

can't get chmod to work

by George_Sherston (Vicar)
on Sep 30, 2001 at 21:37 UTC ( #115747=perlquestion: print w/replies, xml ) Need Help??

George_Sherston has asked for the wisdom of the Perl Monks concerning the following question:

Sorry to trouble you with what may be a trivial prob, but I'd be grateful if any monk wd cast a critical eye over the following.

It's a little utility for when I upload my scripts from my win98 machine to my Linux web space. Whilst I'd value comments on the structure of the script (and am even prepared to be told there's already a module to do this), what I'm stumped on is how to get chmod to work.

The simple statement of the problem is, it doesn't work. The file permissions stay the same. And even when I replace $_ with the name of a file in the dir it doesn't work. I know next to nothing about system administration, and it may well be some webspace / OS specific problem - I'd be grateful for any thoughts on that. But if it's in the code, please hit me with a haddock.

Yer tiz:
print $q->header; print $q->start_html; while (<*.pl>) { next if $_ eq ''; print "<B>$_</B><BR>"; $old = $_; open GET, $old; print "opening $old<BR>"; $new = substr($old,0,(length $old) -2) . 'bak'; open PUT, ">$new"; print "opening $new<BR>"; print PUT $_ while <GET>; print "writing $old into $new<BR>"; close GET; close PUT; print "closing $old and $new<BR>"; open GET, "$new"; open PUT, ">$old"; print "reopening $old and $new<BR>"; while (<GET>) { $_ =~ s/c:\\perl\\bin\\perl/\/usr\/bin\/perl/; $_ =~ s/\("DBI:mysql:database=foo"\)/("DBI:mysql:database=foo" +,"bar","baz")/; print PUT $_; } print "altering lines in $new and writing to $old<BR>"; close GET; close PUT; print "closing $old and $new<BR>"; $cnt = chmod 0755, $old; if ($cnt) { print "changing file permissions for $old<BR>"; } else { print "cannot change file perms for $old<BR>"; } print "<BR>"; } print "DONE"; print $q->end_html;

George Sherston

Replies are listed 'Best First'.
Re: can't get chmod to work
by tachyon (Chancellor) on Sep 30, 2001 at 22:25 UTC

    It is likely to be a permissions problem. You check like this:

    # usual syntax chmod 755, $old or die "Can't chmod $old because: $!\n"; # or using the syntax you have and skipping the # useless $cnt temporary variable if (chmod 0755, $old) { print "changed file permissions for $old<BR>"; } else { print "cannot change file perms for $old<BR>System says $!<BR>"; }

    Whenever you do a system call command (open, opendir, unlink, rename, chdir, chmod, etc) the actual error is in $! so it is easy to find what fails how. You are not checking your open's by the way - this is a sure path to frustration. You should always open files like this:

    open FILE, $file or die "Can't open $file for reading, Perl says $!\n" +;




Re: can't get chmod to work
by Zaxo (Archbishop) on Oct 01, 2001 at 02:04 UTC

    I'm sure tachyon's diagnosis is correct. There are two ways to do what you want.

    1. Write a small standalone script to just do the chmod and return status ( 0 = success), make it suid, and call via perl system.
    2. On Apache, arrange for your original cgi script to run suEXEC.

    I prefer the latter, but opinions differ on that. suEXEC demands a sound perl program. Taint mode helps with that.

    After Compline,

Re: can't get chmod to work
by C-Keen (Monk) on Sep 30, 2001 at 22:02 UTC
    Do you have permissions to access the directory your files are in? Maybe that is the problem. Try a ls -l and check.

    Hope this helps,

      I can change file perms manually using my ftp client - wd I be right in assuming that means my scripts shd be able to change them too?

      George Sherston

        Absolutely not. You have user 'you' permissions. Your script will likely run under user 'nobody' and have very limited permissions. Check your system calls as noted below and all will be revealed.

        As for how to solve this do a Super Search for "setuid CGI" in the text. Here is one link SUID?




Re: can't get chmod to work
by mandog (Curate) on Oct 01, 2001 at 06:16 UTC
    umask may be the source of your pain.

    The default permissions of a newly created file are set by umask. If your user process has a different umask than your script process, you may have troubles.

    I wish I could provide a good summery of how umask interacts with parent directory permissions and the many places it can be set, but I don't think I could do a shorter or more accurate job than the man pages or a more knowledgeable monk.

    I reached my current incomplete understanding of umask through the following commands.

    man umask grep -d recurse -i umask /etc | more more <files that seem relevant> man <files that seem relevant>
    ...and searched my ftp server's docs to realize that umask needed to be set in my ftp server's config file.

    Your problem is obviously not ftp related so perhaps:
     perldoc -f umask might help. You might also toss a debugging  system('ls -l'); into your script just after you create your $new file.

    Hope this helps (or is at least relevant...)


Re: can't get chmod to work
by George_Sherston (Vicar) on Oct 02, 2001 at 03:42 UTC
    I'm very grateful to all who took the trouble to answer this. I'm going to try and get my ISP to let me run scripts as / under suEXEC. I'll post my solution here for the benefit of posterity when I have one. Betcha can't wait :)

    George Sherston

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://115747]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2023-09-24 02:47 GMT
Find Nodes?
    Voting Booth?

    No recent polls found