| 1 | # -*- Mode: perl -*- | ||
| 2 | # | ||
| 3 | # $Id: File.pm,v 0.1 2001/04/22 17:57:04 ram Exp $ | ||
| 4 | # | ||
| 5 | # Copyright (c) 1998-2001, Raphael Manfredi | ||
| 6 | # Copyright (c) 2000-2001, Christophe Dehaudt | ||
| 7 | # | ||
| 8 | # You may redistribute only under the terms of the Artistic License, | ||
| 9 | # as specified in the README file that comes with the distribution. | ||
| 10 | # | ||
| 11 | # HISTORY | ||
| 12 | # $Log: File.pm,v $ | ||
| 13 | # Revision 0.1 2001/04/22 17:57:04 ram | ||
| 14 | # Baseline for first Alpha release. | ||
| 15 | # | ||
| 16 | # $EndLog$ | ||
| 17 | # | ||
| 18 | |||
| 19 | use strict; | ||
| 20 | |||
| 21 | package CGI::MxScreen::Session::Medium::File; | ||
| 22 | |||
| 23 | # | ||
| 24 | # Session storage is a file, indexed by a session ID. | ||
| 25 | # | ||
| 26 | # In the generated HTML, the session is identified by two parameters: | ||
| 27 | # | ||
| 28 | # _mxscreen_session the session ID | ||
| 29 | # _mxscreen_token a random token, saved with context | ||
| 30 | # | ||
| 31 | # The random token is generated each time the context is saved to the file. | ||
| 32 | # It is used at retrieve time to validate the session ID: noone could guess | ||
| 33 | # both the session ID and the associated token at the same time. | ||
| 34 | # | ||
| 35 | # Note that since we generate a token each time we save, using "Back" from | ||
| 36 | # the browser to resubmit an old form is bound to fail, since the token | ||
| 37 | # will have changed. | ||
| 38 | # | ||
| 39 | # It is up to the user to ensure that old session files are removed from the | ||
| 40 | # file system, according to a suitable session expiring policy. | ||
| 41 | # | ||
| 42 | |||
| 43 | require CGI::MxScreen::Session::Medium; | ||
| 44 | use vars qw(@ISA); | ||
| 45 | @ISA = qw(CGI::MxScreen::Session::Medium); | ||
| 46 | |||
| 47 | use Carp::Datum; | ||
| 48 | use Getargs::Long; | ||
| 49 | use Log::Agent; | ||
| 50 | use Fcntl; | ||
| 51 | use File::Basename; | ||
| 52 | use File::Path; | ||
| 53 | |||
| 54 | require LockFile::Simple; | ||
| 55 | require CGI; | ||
| 56 | |||
| 57 | use constant MX_SESSION_ID => "_mxscreen_session"; | ||
| 58 | use constant MX_TOKEN => "_mxscreen_token"; | ||
| 59 | |||
| 60 | # | ||
| 61 | # ->make | ||
| 62 | # | ||
| 63 | # Creation routine. | ||
| 64 | # | ||
| 65 | # Arguments: | ||
| 66 | # -directory root directory where sessions are stored | ||
| 67 | # -nfs whether stored data are on a network filesystem | ||
| 68 | # -max_hold maximum expected lock holding time, for stale lock detection | ||
| 69 | # | ||
| 70 | sub make { | ||
| 71 | DFEATURE my $f_; | ||
| 72 | my $self = bless {}, shift; | ||
| 73 | my $MAX_HOLD = 60; # If CGI takes more than that, uh! | ||
| 74 | |||
| 75 | my ($directory, $nfs, $max) = xgetargs(@_, | ||
| 76 | -directory => 's', | ||
| 77 | -nfs => ['i', 0], | ||
| 78 | -max_hold => ['i', $MAX_HOLD], | ||
| 79 | ); | ||
| 80 | |||
| 81 | $self->{directory} = $directory; | ||
| 82 | $self->{shared} = $nfs; | ||
| 83 | $self->{lockmgr} = LockFile::Simple->make( | ||
| 84 | -delay => 1, | ||
| 85 | -hold => $max, | ||
| 86 | -max => $max, # After that, will be stale | ||
| 87 | -nfs => $nfs, | ||
| 88 | -stale => 1, | ||
| 89 | ); | ||
| 90 | |||
| 91 | return DVAL $self; | ||
| 92 | } | ||
| 93 | |||
| 94 | # | ||
| 95 | # Attribute access | ||
| 96 | # | ||
| 97 | |||
| 98 | sub directory { $_[0]->{directory} } | ||
| 99 | sub shared { $_[0]->{shared} } | ||
| 100 | sub lockmgr { $_[0]->{lockmgr} } | ||
| 101 | |||
| 102 | # | ||
| 103 | # ->_id_to_path | ||
| 104 | # | ||
| 105 | # Convert a session ID into a path. | ||
| 106 | # | ||
| 107 | # Because traditional UNIX filesystems handle huge directories "inefficiently", | ||
| 108 | # we hash the session files under 2 extra layers. | ||
| 109 | # | ||
| 110 | sub _id_to_path { | ||
| 111 | DFEATURE my $f_; | ||
| 112 | my $self = shift; | ||
| 113 | my ($id) = @_; | ||
| 114 | |||
| 115 | require Digest::MD5; | ||
| 116 | my $hash = Digest::MD5::md5_base64($id); | ||
| 117 | $hash =~ tr|+/=|-_.|; | ||
| 118 | |||
| 119 | my $path = $self->{directory} . '/' . | ||
| 120 | join("/", split(//, substr($hash, 0, 2))) . "/$id"; | ||
| 121 | $path = $1 if $path =~ /^(.*)$/; # untaint | ||
| 122 | |||
| 123 | return DVAL $path; | ||
| 124 | } | ||
| 125 | |||
| 126 | # | ||
| 127 | # ->session_id -- defined | ||
| 128 | # | ||
| 129 | # Retrieve session ID from the CGI environment. | ||
| 130 | # For a file, the session ID is stored in _mxscreen_session | ||
| 131 | # | ||
| 132 | sub session_id { | ||
| 133 | DFEATURE my $f_; | ||
| 134 | my $self = shift; | ||
| 135 | |||
| 136 | my $id = CGI::param(MX_SESSION_ID); | ||
| 137 | CGI::delete(MX_SESSION_ID); # Invisible to end-user | ||
| 138 | |||
| 139 | return DVAL $id; | ||
| 140 | } | ||
| 141 | |||
| 142 | # | ||
| 143 | # ->is_available -- defined | ||
| 144 | # | ||
| 145 | # Look whether ID is free to use as a session ID. | ||
| 146 | # If it is free, atomically reserve it. | ||
| 147 | # | ||
| 148 | # Returns true if ID is OK for use, false if it's not available. | ||
| 149 | # | ||
| 150 | sub is_available { | ||
| 151 | DFEATURE my $f_; | ||
| 152 | my $self = shift; | ||
| 153 | my ($id) = @_; | ||
| 154 | |||
| 155 | # | ||
| 156 | # Check that no session bearing the same ID is still recorded within | ||
| 157 | # the filesystem. | ||
| 158 | # | ||
| 159 | |||
| 160 | my $path = $self->_id_to_path($id); | ||
| 161 | my $lockmgr = $self->lockmgr; | ||
| 162 | |||
| 163 | # | ||
| 164 | # Create all the missing directories that lead to the session path, | ||
| 165 | # before attempting to lock the file. | ||
| 166 | # | ||
| 167 | |||
| 168 | my $dir = dirname $path; | ||
| 169 | mkpath($dir) unless -d $dir; | ||
| 170 | my $lock = $lockmgr->lock($path); | ||
| 171 | |||
| 172 | if (-f $path) { | ||
| 173 | $lock->release if defined $lock; | ||
| 174 | logtrc 'info', "$path already exists for session $id"; | ||
| 175 | return DVAL undef; | ||
| 176 | } | ||
| 177 | |||
| 178 | # | ||
| 179 | # Create empty file to reserve the entry. | ||
| 180 | # | ||
| 181 | |||
| 182 | local *FILE; | ||
| 183 | open(FILE, ">$path") || logerr "can't create $path: $!"; | ||
| 184 | close FILE; | ||
| 185 | |||
| 186 | $lock->release if defined $lock; | ||
| 187 | |||
| 188 | return DVAL 1; # Session ID reserved, and OK to use | ||
| 189 | } | ||
| 190 | |||
| 191 | # | ||
| 192 | # ->retrieve -- defined | ||
| 193 | # | ||
| 194 | # Retrieve context by session ID. | ||
| 195 | # | ||
| 196 | sub retrieve { | ||
| 197 | DFEATURE my $f_; | ||
| 198 | my $self = shift; | ||
| 199 | my ($id) = @_; | ||
| 200 | |||
| 201 | DREQUIRE defined $self->serializer, "already called set_serializer()"; | ||
| 202 | |||
| 203 | my $path = $self->_id_to_path($id); | ||
| 204 | my $lockmgr = $self->{lockmgr}; | ||
| 205 | my $lock = $lockmgr->lock($path); | ||
| 206 | |||
| 207 | my $array = $self->_retrieve_locked($path); | ||
| 208 | $lock->release if defined $lock; | ||
| 209 | |||
| 210 | return DVAL undef unless defined $array; | ||
| 211 | |||
| 212 | my $token = CGI::param(MX_TOKEN); | ||
| 213 | CGI::delete(MX_TOKEN); # Invisible to end-user | ||
| 214 | |||
| 215 | if ($array->[0] ne $token) { | ||
| 216 | logerr "context token mismatch"; | ||
| 217 | return DVAL undef; | ||
| 218 | } | ||
| 219 | |||
| 220 | return DVAL $array->[1]; | ||
| 221 | } | ||
| 222 | |||
| 223 | # | ||
| 224 | # ->_retrieve_locked | ||
| 225 | # | ||
| 226 | # retrieve context from (locked) file. | ||
| 227 | # | ||
| 228 | sub _retrieve_locked { | ||
| 229 | DFEATURE my $f_; | ||
| 230 | my $self = shift; | ||
| 231 | my ($path) = @_; | ||
| 232 | |||
| 233 | my $serializer = $self->serializer; | ||
| 234 | |||
| 235 | local *FILE; | ||
| 236 | unless (sysopen(FILE, $path, O_RDONLY)) { | ||
| 237 | logerr "can't open session file $path: $!"; | ||
| 238 | return DVAL undef; | ||
| 239 | } | ||
| 240 | my $frozen; | ||
| 241 | binmode FILE; | ||
| 242 | unless (sysread(FILE, $frozen, -s(FILE))) { | ||
| 243 | logerr "can't read session file $path: $!"; | ||
| 244 | close FILE; | ||
| 245 | return DVAL undef; | ||
| 246 | } | ||
| 247 | close FILE; | ||
| 248 | unless (length $frozen) { | ||
| 249 | logwarn "no data in session file $path, session expired"; | ||
| 250 | return undef; | ||
| 251 | } | ||
| 252 | |||
| 253 | return DVAL $serializer->deserialize($frozen); | ||
| 254 | } | ||
| 255 | |||
| 256 | # | ||
| 257 | # ->store -- defined | ||
| 258 | # | ||
| 259 | # Store context by session ID. | ||
| 260 | # | ||
| 261 | # Returns hash of (parameter => value) to be generated in the HTML | ||
| 262 | # to identify the session. | ||
| 263 | # | ||
| 264 | sub store { | ||
| 265 | DFEATURE my $f_; | ||
| 266 | my $self = shift; | ||
| 267 | my ($id, $context) = @_; | ||
| 268 | |||
| 269 | DREQUIRE defined $self->serializer, "already called set_serializer()"; | ||
| 270 | |||
| 271 | my $path = $self->_id_to_path($id); | ||
| 272 | my $lockmgr = $self->{lockmgr}; | ||
| 273 | my $lock = $lockmgr->lock($path); | ||
| 274 | |||
| 275 | # | ||
| 276 | # We're storing the context, with a random key pre-pended to validate | ||
| 277 | # the tupple session-ID/random-key at retrieve time. This prevents | ||
| 278 | # forging a session ID. | ||
| 279 | # | ||
| 280 | # We use the _generate_session_id feature from our parent to get | ||
| 281 | # a random token. | ||
| 282 | # | ||
| 283 | |||
| 284 | my $store = [$self->_generate_session_id, $context]; | ||
| 285 | |||
| 286 | $self->_store_locked($path, $store); | ||
| 287 | $lock->release if defined $lock; | ||
| 288 | |||
| 289 | # | ||
| 290 | # Return the hidden parameters to generate in the HTML output. | ||
| 291 | # | ||
| 292 | |||
| 293 | my $ret = { | ||
| 294 | &MX_SESSION_ID => $id, | ||
| 295 | &MX_TOKEN => $store->[0], | ||
| 296 | }; | ||
| 297 | |||
| 298 | return DVAL $ret; | ||
| 299 | } | ||
| 300 | |||
| 301 | # | ||
| 302 | # ->_store_locked | ||
| 303 | # | ||
| 304 | # Store context into (locked) file. | ||
| 305 | # | ||
| 306 | sub _store_locked { | ||
| 307 | DFEATURE my $f_; | ||
| 308 | my $self = shift; | ||
| 309 | my ($path, $context) = @_; | ||
| 310 | |||
| 311 | my $serializer = $self->serializer; | ||
| 312 | |||
| 313 | local *FILE; | ||
| 314 | unless (sysopen(FILE, $path, O_WRONLY|O_CREAT|O_TRUNC)) { | ||
| 315 | logerr "can't create session file $path: $!"; | ||
| 316 | return DVOID; | ||
| 317 | } | ||
| 318 | my $frozen = $serializer->serialize($context); | ||
| 319 | binmode FILE; | ||
| 320 | unless (syswrite(FILE, $frozen)) { | ||
| 321 | logerr "can't write to session file $path: $!"; | ||
| 322 | close FILE; | ||
| 323 | return DVOID; | ||
| 324 | } | ||
| 325 | unless (close FILE) { | ||
| 326 | logerr "can't flush session file $path: $!"; | ||
| 327 | return DVOID; | ||
| 328 | } | ||
| 329 | |||
| 330 | return DVOID; | ||
| 331 | } | ||
| 332 | |||
| 333 | 1; | ||
| 334 | |||
| 335 | =head1 NAME | ||
| 336 | |||
| 337 | CGI::MxScreen::Session::Medium::File - File session medium | ||
| 338 | |||
| 339 | =head1 SYNOPSIS | ||
| 340 | |||
| 341 | # Not meant to be used directly | ||
| 342 | |||
| 343 | =head1 DESCRIPTION | ||
| 344 | |||
| 345 | This saves the session within a file on the server side. Session | ||
| 346 | files are created under a set of directories, to avoid having directories | ||
| 347 | with two many files in them, which is inefficient on most filesystems | ||
| 348 | (reiserfs excepted). An MD5 hash of the session ID is taken to compute two | ||
| 349 | sub directories, resulting in session files being stored as: | ||
| 350 | |||
| 351 | W/N/192.168.0.3-987001261-28947 | ||
| 352 | k/5/192.168.0.3-987177890-16666 | ||
| 353 | |||
| 354 | You can see the pattern used for session ids: the IP address of the client | ||
| 355 | making the request, a timestamp indicating the start of the session, and | ||
| 356 | the PID of the process. However, this is used only when the targetted | ||
| 357 | session ID is free, otherwise, a random cryptic session ID is generated. | ||
| 358 | |||
| 359 | Because sessions will use disk space, you need an expiration policy. | ||
| 360 | If someone attempts to continue a session that has been removed, | ||
| 361 | C<CGI::MxScreen> will return an error, and that someone will have to | ||
| 362 | restart the whole session, thereby potentially loosing all the work | ||
| 363 | already done. | ||
| 364 | |||
| 365 | On my web server, I use the following command to expire sessions after | ||
| 366 | a week of inactivity: | ||
| 367 | |||
| 368 | cd /var/tmp/web-sessions | ||
| 369 | find . -type f -atime +7 | xargs rm -f | ||
| 370 | |||
| 371 | The creation routine takes the following arguments: | ||
| 372 | |||
| 373 | =over 4 | ||
| 374 | |||
| 375 | =item C<-directory> => I<path> | ||
| 376 | |||
| 377 | Mandatory argument, giving the root directory where sessions are saved. | ||
| 378 | |||
| 379 | =item C<-max_hold> => I<seconds> | ||
| 380 | |||
| 381 | Optional argument, defining the timeout after which a lock is declared stale. | ||
| 382 | Defaults is 60 seconds. | ||
| 383 | |||
| 384 | =item C<-nfs> => I<flag> | ||
| 385 | |||
| 386 | Optional, tells whether session files are read from a local or a network | ||
| 387 | filesystem (e.g. NFS). Assumes local filesystem by default. | ||
| 388 | |||
| 389 | =back | ||
| 390 | |||
| 391 | You can configure this session medium in the configuration file as | ||
| 392 | explained in L<CGI::MxScreen::Config> by saying: | ||
| 393 | |||
| 394 | $mx_medium = ["+File", -directory => "/var/tmp/www-sessions"]; | ||
| 395 | |||
| 396 | You can further say: | ||
| 397 | |||
| 398 | $mx_serializer = ["+Storable", -compress => 1]; | ||
| 399 | |||
| 400 | to store sessions in compressed forms, although I advise you not to, | ||
| 401 | for performance reasons. Actually, you should look at | ||
| 402 | L<CGI::MxScreen::Session::Medium::Raw_File> if you want fast session files. | ||
| 403 | |||
| 404 | |||
| 405 | =head1 AUTHOR | ||
| 406 | |||
| 407 | Raphael Manfredi F<E<lt>Raphael_Manfredi@pobox.comE<gt>> | ||
| 408 | |||
| 409 | =head1 SEE ALSO | ||
| 410 | |||
| 411 | CGI::MxScreen::Session::Medium::Browser(3), | ||
| 412 | CGI::MxScreen::Session::Medium::Raw_File(3). | ||
| 413 | |||
| 414 | =cut | ||
| 415 |