1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
# --
# Kernel/System/AuthSession/FS.pm - provides session filesystem backend
# Copyright (C) 2001-2007 OTRS GmbH, http://otrs.org/
# --
# $Id: FS.pm,v 1.23 2007/06/27 12:09:56 martin Exp $
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (GPL). If you
# did not receive this file, see http://www.gnu.org/licenses/gpl.txt.
# --
package Kernel::System::AuthSession::FS;
use strict;
use warnings;
use Digest::MD5;
use MIME::Base64;
use Kernel::System::Encode;
use vars qw($VERSION);
$VERSION = '$Revision: 1.23 $';
$VERSION =~ s/^\$.*:\W(.*)\W.+?$/$1/;
sub new {
my $Type = shift;
my %Param = @_;
# allocate new hash for object
my $Self = {};
bless ($Self, $Type);
# check needed objects
foreach (qw(LogObject ConfigObject DBObject TimeObject)) {
$Self->{$_} = $Param{$_} || die "No $_!";
}
# encode object
$Self->{EncodeObject} = Kernel::System::Encode->new(%Param);
# get more common params
$Self->{SessionSpool} = $Self->{ConfigObject}->Get('SessionDir');
$Self->{SystemID} = $Self->{ConfigObject}->Get('SystemID');
# Debug 0=off 1=on
$Self->{Debug} = 0;
return $Self;
}
sub CheckSessionID {
my $Self = shift;
my %Param = @_;
my $SessionID = $Param{SessionID};
my $RemoteAddr = $ENV{REMOTE_ADDR} || 'none';
# set default message
$Self->{CheckSessionIDMessage} = "SessionID is invalid!!!";
# session id check
my %Data = $Self->GetSessionIDData(SessionID => $SessionID);
if (!$Data{UserID} || !$Data{UserLogin}) {
$Self->{CheckSessionIDMessage} = "SessionID invalid! Need user data!";
$Self->{LogObject}->Log(
Priority => 'notice',
Message => "SessionID: '$SessionID' is invalid!!!",
);
return;
}
# remote ip check
if ( $Data{UserRemoteAddr} ne $RemoteAddr &&
$Self->{ConfigObject}->Get('SessionCheckRemoteIP') ) {
$Self->{LogObject}->Log(
Priority => 'notice',
Message => "RemoteIP of '$SessionID' ($Data{UserRemoteAddr}) is different with the ".
"request IP ($RemoteAddr). Don't grant access!!!",
);
# delete session id if it isn't the same remote ip?
if ($Self->{ConfigObject}->Get('SessionDeleteIfNotRemoteID')) {
$Self->RemoveSessionID(SessionID => $SessionID);
}
return;
}
# check session idle time
my $MaxSessionIdleTime = $Self->{ConfigObject}->Get('SessionMaxIdleTime');
if ( ($Self->{TimeObject}->SystemTime() - $MaxSessionIdleTime) >= $Data{UserLastRequest} ) {
$Self->{CheckSessionIDMessage} = 'Session has timed out. Please log in again.';
$Self->{LogObject}->Log(
Priority => 'notice',
Message => "SessionID ($SessionID) idle timeout (". int(($Self->{TimeObject}->SystemTime() - $Data{UserLastRequest})/(60*60))
."h)! Don't grant access!!!",
);
# delete session id if too old?
if ($Self->{ConfigObject}->Get('SessionDeleteIfTimeToOld')) {
$Self->RemoveSessionID(SessionID => $SessionID);
}
return;
}
# check session time
my $MaxSessionTime = $Self->{ConfigObject}->Get('SessionMaxTime');
if ( ($Self->{TimeObject}->SystemTime() - $MaxSessionTime) >= $Data{UserSessionStart} ) {
$Self->{CheckSessionIDMessage} = 'Session has timed out. Please log in again.';
$Self->{LogObject}->Log(
Priority => 'notice',
Message => "SessionID ($SessionID) too old (". int(($Self->{TimeObject}->SystemTime() - $Data{UserSessionStart})/(60*60))
."h)! Don't grant access!!!",
);
# delete session id if too old?
if ($Self->{ConfigObject}->Get('SessionDeleteIfTimeToOld')) {
$Self->RemoveSessionID(SessionID => $SessionID);
}
return;
}
return 1;
}
sub CheckSessionIDMessage {
my $Self = shift;
my %Param = @_;
return $Self->{CheckSessionIDMessage} || '';
}
sub GetSessionIDData {
my $Self = shift;
my %Param = @_;
my $SessionID = $Param{SessionID} || '';
my $Strg = '';
my %Data;
# check session id
if (!$SessionID) {
$Self->{LogObject}->Log(Priority => 'error', Message => "Got no SessionID!!");
return;
}
# read data
open (SESSION, "< $Self->{SessionSpool}/$SessionID")
|| warn "Can't open $Self->{SessionSpool}/$SessionID: $!\n";
while (<SESSION>) {
chomp;
# split data
my @PaarData = split(/:/, $_);
if (defined($PaarData[1])) {
$Data{$PaarData[0]} = decode_base64($PaarData[1]);
$Self->{EncodeObject}->Encode(\$Data{$PaarData[0]});
}
else {
$Data{$PaarData[0]} = '';
}
# Debug
if ($Self->{Debug}) {
$Self->{LogObject}->Log(
Priority => 'debug',
Message => "GetSessionIDData: '$PaarData[0]:".decode_base64($PaarData[1])."'",
);
}
}
close (SESSION);
return %Data;
}
sub CreateSessionID {
my $Self = shift;
my %Param = @_;
# REMOTE_ADDR
my $RemoteAddr = $ENV{REMOTE_ADDR} || 'none';
# HTTP_USER_AGENT
my $RemoteUserAgent = $ENV{HTTP_USER_AGENT} || 'none';
# create SessionID
my $md5 = Digest::MD5->new();
$md5->add(
($Self->{TimeObject}->SystemTime() . int(rand(999999999)) . $Self->{SystemID}) . $RemoteAddr . $RemoteUserAgent
);
my $SessionID = $Self->{SystemID} . $md5->hexdigest;
# data 2 strg
my $DataToStore = '';
foreach (keys %Param) {
if (defined($Param{$_})) {
$Self->{EncodeObject}->EncodeOutput(\$Param{$_});
$DataToStore .= "$_:". encode_base64($Param{$_}, '') ."\n";
}
}
$DataToStore .= "UserSessionStart:". encode_base64($Self->{TimeObject}->SystemTime(), '') ."\n";
$DataToStore .= "UserRemoteAddr:". encode_base64($RemoteAddr, '') ."\n";
$DataToStore .= "UserRemoteUserAgent:". encode_base64($RemoteUserAgent, '') ."\n";
# store SessionID + data
if (open (SESSION, ">> $Self->{SessionSpool}/$SessionID")) {
print SESSION "$DataToStore";
close (SESSION);
return $SessionID;
}
else {
$Self->{LogObject}->Log(
Priority => 'error',
Message => "Can't create $Self->{SessionSpool}/$SessionID: $!",
);
return;
}
}
sub RemoveSessionID {
my $Self = shift;
my %Param = @_;
my $SessionID = $Param{SessionID};
# delete fs file
if (unlink("$Self->{SessionSpool}/$SessionID")) {
# log event
$Self->{LogObject}->Log(
Priority => 'notice',
Message => "Removed SessionID $Param{SessionID}."
);
return 1;
}
else {
# log event
$Self->{LogObject}->Log(
Priority => 'error',
Message => "Can't delete file $Self->{SessionSpool}/$SessionID: $!"
);
return 0;
}
}
sub UpdateSessionID {
my $Self = shift;
my %Param = @_;
my $Key = defined($Param{Key}) ? $Param{Key} : '';
my $Value = defined($Param{Value}) ? $Param{Value} : '';
my $SessionID = $Param{SessionID};
# check needed stuff
if (!$SessionID) {
$Self->{LogObject}->Log(
Priority => 'error',
Message => "Need SessionID!",
);
return;
}
my %SessionData = $Self->GetSessionIDData(SessionID => $SessionID);
# update the value
$SessionData{$Key} = $Value;
# set new data sting
my $NewDataToStore = '';
foreach (keys %SessionData) {
$Self->{EncodeObject}->EncodeOutput(\$SessionData{$_});
$NewDataToStore .= "$_:".encode_base64($SessionData{$_}, '')."\n";
# Debug
if ($Self->{Debug}) {
$Self->{LogObject}->Log(
Priority => 'debug',
Message => "UpdateSessionID: $_=$SessionData{$_}",
);
}
}
# update fs file
if (open (SESSION, "> $Self->{SessionSpool}/$SessionID")) {
print SESSION $NewDataToStore;
close (SESSION);
return 1;
}
else {
$Self->{LogObject}->Log(
Priority => 'error',
Message => "Can't write $Self->{SessionSpool}/$SessionID: $!",
);
return;
}
}
sub GetAllSessionIDs {
my $Self = shift;
my %Param = @_;
my @SessionIDs = ();
# read data
my @List = glob("$Self->{SessionSpool}/$Self->{SystemID}*");
foreach my $SessionID (@List) {
$SessionID =~ s!^.*/!!;
push (@SessionIDs, $SessionID);
}
return @SessionIDs;
}
sub CleanUp {
my $Self = shift;
my %Param = @_;
# delete fs files
my @SessionIDs = $Self->GetAllSessionIDs();
foreach (@SessionIDs) {
if (!$Self->RemoveSessionID(SessionID => $_)) {
return;
}
}
return 1;
}
1;
|