A friend of mine gave me the below code today and tells me it is exhibiting some strange behaviour. He is using
sysopen to modify a passwd file to edit it in place. His hope is that should a user need to read it, he isn't going to clobber it while they're reading it. I'm not sure how much traffic is involved here, but he is concerned about it, so I didn't ask any questions.
#!/usr/local/bin/perl
use strict;
use warnings;
use Fcntl qw(:DEFAULT :flock);
# sysopen(PASSWD, "./passwd", O_RDWR)
open (PASSWD, "+>>./passwd")
or die "can't open passwd file ($!)";
flock(PASSWD, LOCK_EX);
# or die "can't get lock on passwd file ($!)";
my @temp;
foreach my $readLine (PASSWD) {
chomp $readline;
my (
$name, $passwd, $uid,
$gid, $quota, $comment,
$gcos, $dir, $shell
)
= split /:/, $readLine;
if ($name eq "www") {
$shell =~ s[/bin/bash][/dev/null];
}
my $line = join ':', ($name,$passwd,$uid,$gid,$quota,$comment,$gcos,
+$dir,$shell);
push @temp, $line;
}
truncate (PASSWD, 0);
foreach my $user (@temp) {
print PASSWD "$user\n";
}
close (PASSWD);
He says that what it eventually does is obliterate the file and replace it with the text
PASSWD::::::::
This strikes me as rather bizarre. Now, this is on linux on x86, and perl 5.5. I suggested he use
File::Slurp (/me bows down and worships File::Slurp). However, this script needs to be deployed to > 4000 machines, and installing modules is simply not an option (I have entertained the possibility of just taking the code from the module and putting it in the script, that seems reasonable).
I'm concerned because I can't think of any reason why it would be doing this. Any ideas, Monks?
el dep mas fina
--
Laziness, Impatience, Hubris, and Generosity.