| 1 | package CGI::Session::Driver::file; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | # $Id$ | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 24 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 24 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 24 | ▶ | use File::Spec; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 24 | ▶ | use Fcntl qw( :DEFAULT :flock :mode ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 24 | ▶ | use CGI::Session::Driver; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 24 | ▶ | use vars qw( $FileName $NoFlock $UMask $NO_FOLLOW ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 24 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | # keep historical behavior | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 24 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 24 | *FileName = \$CGI::Session::File::FileName; | |||||||||||||||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 24 | @CGI::Session::Driver::file::ISA = ( "CGI::Session::Driver" ); | |||||||||||||||||||||||||||||||||||||||||
| 22 | 24 | $CGI::Session::Driver::file::VERSION = '4.43'; | |||||||||||||||||||||||||||||||||||||||||
| 23 | 24 | $FileName = "cgisess_%s"; | |||||||||||||||||||||||||||||||||||||||||
| 24 | 24 | $NoFlock = 0; | |||||||||||||||||||||||||||||||||||||||||
| 25 | 24 | $UMask = 0660; | |||||||||||||||||||||||||||||||||||||||||
| 26 | 24 | ▶ | $NO_FOLLOW = eval { O_NOFOLLOW } || 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
eval {
do {
131072
}
} || 0
MC/DC0%0/1
eval {
do {
131072
}
} || 0
eval {
do {
131072
}
}
Truth table50%1/2
A
A= eval {
do {
131072
}
}
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 48 | ▶ | sub init { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=6 SCAR=22.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 48 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 30 | 48 | ▶ | $self->{Directory} ||= File::Spec->tmpdir(); | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"Directory"} ||= "File::Spec"->tmpdir
MC/DC0%0/2
$self->{"Directory"} ||= "File::Spec"->tmpdir
$self->{"Directory"}
"File::Spec"->tmpdir
Truth table66%2/3
A ||= B
A= $self->{"Directory"}B= "File::Spec"->tmpdir
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 48 | ▶ | unless ( -d $self->{Directory} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | require File::Path; | |||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | unless ( File::Path::mkpath($self->{Directory}) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | return $self->set_error( "init(): couldn't create directory path: $!" ); | |||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 48 | ▶ | $self->{NoFlock} = $NoFlock unless exists $self->{NoFlock}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 48 | ▶ | $self->{UMask} = $UMask unless exists $self->{UMask}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 48 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 87 | ▶ | sub _file { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_file: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 87 | my ($self,$sid) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 47 | 87 | my $id = $sid; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 87 | $id =~ s|\\|/|g; | |||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 87 | ▶ | if ($id =~ m|/|) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | { | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | return $self->set_error( "_file(): Session ids cannot contain \\ or / chars: $sid" ); | |||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 87 | return File::Spec->catfile($self->{Directory}, sprintf( $FileName, $sid )); | |||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 34 | ▶ | sub retrieve { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
retrieve: called CC=8 SCAR=22.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 34 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 60 | 34 | my ($sid) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 34 | my $path = $self->_file($sid); | |||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 34 | ▶ | return 0 unless -e $path; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | # make certain our filehandle goes away when we fall out of scope | ||||||||||||||||||||||||||||||||||||||||||
| 67 | 20 | local *FH; | |||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 20 | ▶ | if (-l $path) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | unlink($path) or | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | return $self->set_error("retrieve(): '$path' appears to be a symlink and I couldn't remove it: $!"); | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | return 0; # we deleted this so we have no hope of getting back anything | |||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | 20 | ▶ | sysopen(FH, $path, O_RDONLY | $NO_FOLLOW ) || return $self->set_error( "retrieve(): couldn't open '$path': $!" ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | 20 | ▶ | $self->{NoFlock} || flock(FH, LOCK_SH) or return $self->set_error( "retrieve(): couldn't lock '$path': $!" ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"NoFlock"} or flock FH, 1
MC/DC0%0/2
$self->{"NoFlock"} or flock FH, 1
$self->{"NoFlock"}
flock FH, 1
Truth table33%1/3
A or B
A= $self->{"NoFlock"}B= flock FH, 1
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 20 | my $rv = ""; | |||||||||||||||||||||||||||||||||||||||||
| 79 | 20 | ▶ | while ( <FH> ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 69 | $rv .= $_; | |||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 20 | close(FH); | |||||||||||||||||||||||||||||||||||||||||
| 83 | 20 | return $rv; | |||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | 35 | ▶ | sub store { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
store: called CC=10 SCAR=24.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 35 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 35 | my ($sid, $datastr) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 35 | my $path = $self->_file($sid); | |||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | # make certain our filehandle goes away when we fall out of scope | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 35 | local *FH; | |||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 35 | my $mode = O_WRONLY|$NO_FOLLOW; | |||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | # kill symlinks when we spot them | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 35 | ▶ | if (-l $path) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 1 | ▶ | unlink($path) or | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | return $self->set_error("store(): '$path' appears to be a symlink and I couldn't remove it: $!"); | ||||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 35 | ▶ | $mode = O_RDWR|O_CREAT|O_EXCL unless -e $path; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 35 | ▶ | sysopen(FH, $path, $mode, $self->{UMask}) or return $self->set_error( "store(): couldn't open '$path': $!" ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | # sanity check to make certain we're still ok | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 35 | ▶ | if (-l $path) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | return $self->set_error("store(): '$path' is a symlink, check for malicious processes"); | |||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | # prevent race condition (RT#17949) | ||||||||||||||||||||||||||||||||||||||||||
| 115 | 35 | ▶ | $self->{NoFlock} || flock(FH, LOCK_EX) or return $self->set_error( "store(): couldn't lock '$path': $!" ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"NoFlock"} or flock FH, 2
MC/DC0%0/2
$self->{"NoFlock"} or flock FH, 2
$self->{"NoFlock"}
flock FH, 2
Truth table33%1/3
A or B
A= $self->{"NoFlock"}B= flock FH, 2
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | 35 | ▶ | truncate(FH, 0) or return $self->set_error( "store(): couldn't truncate '$path': $!" ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | 35 | print FH $datastr; | |||||||||||||||||||||||||||||||||||||||||
| 119 | 35 | ▶ | close(FH) or return $self->set_error( "store(): couldn't close '$path': $!" ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 35 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | 16 | ▶ | sub remove { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
remove: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 16 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 126 | 16 | my ($sid) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 127 | 16 | my $path = $self -> _file($sid); | |||||||||||||||||||||||||||||||||||||||||
| 128 | 16 | ▶ | unlink($path) or return $self->set_error( "remove(): couldn't unlink '$path': $!" ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 16 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | 1 | ▶ | sub traverse { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
traverse: called CC=10 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 135 | 1 | my ($coderef) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | 1 | ▶ | unless ( $coderef && ref($coderef) && (ref $coderef eq 'CODE') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$coderef and ref $coderef and ref $coderef eq "CODE"
$coderef and ref $coderef
MC/DC0%0/3
$coderef and ref $coderef and ref $coderef eq "CODE"
$coderef
ref $coderef
ref $coderef eq "CODE"
Truth table25%1/4
A and B and C
A= $coderefB= ref $coderefC= ref $coderef eq "CODE"
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | croak "traverse(): usage error"; | |||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | opendir( DIRHANDLE, $self->{Directory} ) | ||||||||||||||||||||||||||||||||||||||||||
| 142 | 1 | ▶ | or return $self->set_error( "traverse(): couldn't open $self->{Directory}, " . $! ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | 1 | my $filename_pattern = $FileName; | |||||||||||||||||||||||||||||||||||||||||
| 145 | 1 | $filename_pattern =~ s/\./\\./g; | |||||||||||||||||||||||||||||||||||||||||
| 146 | 1 | $filename_pattern =~ s/\%s/(\.\+)/g; | |||||||||||||||||||||||||||||||||||||||||
| 147 | 1 | ▶ | while ( my $filename = readdir(DIRHANDLE) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 148 | 53 | ▶ | next if $filename =~ m/^\.\.?$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | 51 | my $full_path = File::Spec->catfile($self->{Directory}, $filename); | |||||||||||||||||||||||||||||||||||||||||
| 150 | 51 | ▶ | my $mode = (stat($full_path))[2] | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | or return $self->set_error( "traverse(): stat failed for $full_path: " . $! ); | ||||||||||||||||||||||||||||||||||||||||||
| 152 | 51 | ▶ | next if S_ISDIR($mode); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | 50 | ▶ | if ( $filename =~ /^$filename_pattern$/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 1 | $coderef->($1); | |||||||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||
| 157 | 1 | closedir( DIRHANDLE ); | |||||||||||||||||||||||||||||||||||||||||
| 158 | 1 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | 48 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | 48 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | 24 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | __END__; | ||||||||||||||||||||||||||||||||||||||||||