From 365c6f5a5e3df9b2444118aa7646129677ff6e4a Mon Sep 17 00:00:00 2001 From: b Date: Sat, 14 Nov 2015 22:22:22 +0000 Subject: [PATCH] Fixed access verification. git-svn-id: svn://botcastle1b/yplom/proxy@8 05ce6ebb-7522-4a6e-a768-0026ae12be9f --- access.1.pl | 44 ++++++++++++++++++++++++++++++++++++-------- proxy.1.pl | 36 +++++++++++++++++++++++++++++++----- 2 files changed, 67 insertions(+), 13 deletions(-) diff --git a/access.1.pl b/access.1.pl index dbbd195..fee2b63 100644 --- a/access.1.pl +++ b/access.1.pl @@ -20,27 +20,55 @@ while () { if ($line =~ /^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)( .*)?$/) { $IP = $1; $accesspath=ACCESS_PATH.$IP; - if (open ($accessfile,"<",$accesspath)) { + if (! (-e $accesspath)) { + print "ERR'n"; + } + elsif (open ($accessfile,"+<",$accesspath)) { + unless (flock ($accessfile, 2)) { + close ($accessfile); + print "ERR\n"; + next; + } $unlocktime=<$accessfile>; $lasttime=<$accessfile>; - close($accessfile); + $unlocktime =~ s/[\r\n]//g; $lasttime =~ s/[\r\n]//g; - if ((abs($accesstime-$unlocktime)>$timeout_unlock) or (abs($accesstime-$unlocktime)>$timeout_inact)){ - print "ERR too long\n"; + if ($unlocktime =~ /^([0-9]+)$/) { + $unlocktime=int($1); + } + else { + close ($accessfile); + print "ERR\n"; + next; + } + + if ($lasttime =~ /^([0-9]+)$/) { + $lasttime=int($1); + } + else { + close ($accessfile); + print "ERR\n"; + next; + } + + if ((abs($accesstime-$unlocktime)>$timeout_unlock) or (abs($accesstime-$lasttime)>$timeout_inact)){ + close ($accessfile); unlink $accesspath; + print "ERR\n"; } else { - print "OK\n"; - if (open ($accessfile,">",$accesspath)) { + if (seek($accessfile, 0, 0)) { print $accessfile "$unlocktime\n$accesstime\n"; - close ($accessfile); + truncate ($accessfile , tell($accessfile)); } + close ($accessfile); + print "OK\n"; } } else { - print "ERR\n"; + print "BH\n"; } } else { diff --git a/proxy.1.pl b/proxy.1.pl index e86d433..236cabb 100755 --- a/proxy.1.pl +++ b/proxy.1.pl @@ -323,22 +323,48 @@ sub access { #kind of doubles the functionality of access.pl but for http return 1; } $accesspath=ACCESS_PATH.$IP; - if (open ($accessfile,"<",$accesspath)) { + if (! (-e $accesspath)) { + return 0; + } + + elsif (open ($accessfile,"+<",$accesspath)) { + unless (flock ($accessfile, 2)) { + close ($accessfile); + return 0; + } $unlocktime=<$accessfile>; $lasttime=<$accessfile>; - close($accessfile); + $unlocktime =~ s/[\r\n]//g; $lasttime =~ s/[\r\n]//g; - if ((abs($accesstime-$unlocktime)>$timeout_unlock) or (abs($accesstime-$unlocktime)>$timeout_inact)){ + if ($unlocktime =~ /^([0-9]+)$/) { + $unlocktime=int($1); + } + else { + close ($accessfile); + return 0; + } + + if ($lasttime =~ /^([0-9]+)$/) { + $lasttime=int($1); + } + else { + close ($accessfile); + return 0; + } + + if ((abs($accesstime-$unlocktime)>$timeout_unlock) or (abs($accesstime-$lasttime)>$timeout_inact)){ + close ($accessfile); unlink $accesspath; return 0; } else { - if (open ($accessfile,">",$accesspath)) { + if (seek($accessfile, 0, 0)) { print $accessfile "$unlocktime\n$accesstime\n"; - close ($accessfile); + truncate ($accessfile , tell($accessfile)); } + close ($accessfile); return 1; } } -- 2.30.2