31 users online (1 members and 30 guests)  


  Results 1 to 1 of 1

Related

  1. table row values    Forum: HTML Forum
    Replies: 1
  2. adding up my values    Forum: Javascript Forum
    Replies: 1
  1. #1
    alex_fetbroyt's Avatar
    New User

    Status
    Offline
    Join Date
    Jun 2006
    Posts
    1

    Hashes lose values.

    Hi everybody.
    I write a web authorization server for RADIUS server on FreeBSD. I almost done it but there is one problem. I put intermediate variables of login sessions into hashes but this hashes lost values which I put into.
    For example, in POINT_1(see code) I put into the hash %UserPassword a password of a user but in POINT_2(see code) this hash does not contain the password which I put in POINT_1.
    What is a problem?
    How to fix it?
    This is code:

    #!/usr/bin/perl
    use strict;
    use POSIX ":sys_wait_h";
    use Time::Local;
    use Class::Struct;
    use threads;
    use threads::shared;
    use Socket;
    use IO::File;
    use HTTP:aemon;
    use HTTP::Status;
    use Authen::Radius;
    use POSIX qw(setsid);
    # Program Answers
    my $Error = 'Error.';
    # HTTP Server Settings
    my $HTTPHost = '81.20.162.11';
    my $HTTPPort = '88';
    my $HTTPRootAddress = 'http://r-wifi.proxy-service.ru:88/';
    # RADIUS Server Settings
    my $RADIUSHost = '127.0.0.1';
    my $RADIUSAccessPort = '1812';
    my $RADIUSAccountingPort = '1813';
    my $RADIUSSecret = 'local';
    my $RADIUSDictionary = '/usr/local/share/Authen-Radius/dictionary';
    my $RADIUSRefreshTime = 10;
    # SessionID
    my %SessionID : shared = ();
    for(my $i = 1000; $i < 4000; $i++)
    {
    # Zero means that a session is not taken.
    # One means that a session is taken.
    $SessionID{$i} = 0;
    }
    # Session Info
    my %UserPassword : shared = ();
    my %UserRadiusSessionID : shared = ();
    my %UserIPAddress : shared = ();
    my %UserIPPort : shared = ();
    my %UserStartTime : shared = ();
    # Settings
    my %IPAddressURL = ();
    # Daemonize the program
    #&Daemonize;
    # Run web-server with supporting of threads
    sub zombie_reaper
    {
    while(waitpid(-1, WNOHANG) > 0)
    {
    }

    $SIG{CHLD} = \&zombie_reaper;
    }
    $SIG{CHLD} = \&zombie_reaper;
    # Run Threads
    my $OutUserThread = threads->new(\&OutUser);
    # ReStart FireWall
    system("/etc/rc.d/ipfw restart");
    my $HTTPServer = HTTP:aemon->new(LocalAddr => $HTTPHost, LocalPort => $HTTPPort) or die($Error . "\n");
    while(my $Connection = $HTTPServer->accept)
    {
    my $child_pid = fork();
    print($child_pid . "\n");
    if($child_pid)
    {
    next;
    }
    elsif(defined($child_pid))
    {
    while(my $Request = $Connection->get_request)
    {
    #if($HTTPServer->url eq $HTTPRootAddress)
    if(1)
    {
    #print($Request->as_string);

    if($Request->method eq 'GET')
    {
    if(GetHost($Request->as_string) eq "$HTTPHost:$HTTPPort")
    {
    if($Request->url->path eq "/")
    {
    print("Direct Answer\n");
    $Connection->send_response(InsertIntoResponse(ReadPage("./htm/loginpage.htm"), "$HTTPHost:$HTTPPort"));
    }
    else
    {
    if($Request->url->path eq "/top.jpg" or $Request->url->path eq "/login/top.jpg" or $Request->url->path eq "/on/top.jpg" or $Request->url->path eq "/off/top.jpg")
    {
    print("top\n");
    $Connection->send_file_response("./htm/top.jpg");
    }
    elsif($Request->url->path eq "/label.jpg" or $Request->url->path eq "/login/label.jpg" or $Request->url->path eq "/on/label.jpg" or $Request->url->path eq "/off/label.jpg")
    {
    print("label\n");
    $Connection->send_file_response("./htm/label.jpg");
    }
    }
    }
    else
    {
    print("Cache IP\n");
    $IPAddressURL{GetRemoteIPAddress($Connection)} = "http://" . GetHost($Request->as_string) . substr($Request->url, 0, length($Request));
    print(GetRemoteIPAddress($Connection) . " - http://" . GetHost($Request->as_string) . substr($Request->url, 0, length($Request)));
    $Connection->send_redirect("http://$HTTPHost:$HTTPPort"]http://$HTTPHost:$HTTPPort");
    }
    }

    if($Request->method eq 'POST')
    {
    if($Request->url->path eq '/login/')
    {
    my $Login = GetLogin($Request->as_string);
    my $Password = GetPassword($Request->as_string);

    if(IsUserExists($Login, $Password) eq 1)
    {
    if(!exists($UserPassword{$Login}))
    {
    print("No User\n");
    my $Response = InsertIntoResponse(InsertIntoResponse(InsertIntoResponse(ReadPage("./htm/onpage.htm"), "$HTTPHost:$HTTPPort"), $Login), $Password);

    $Connection->send_response($Response);
    }
    else
    {
    print("Yes User\n");
    my $Response = InsertIntoResponse(InsertIntoResponse(InsertIntoResponse(InsertIntoResponse(ReadPage("./htm/offpage.htm"), "|"), "$HTTPHost:$HTTPPort"), $Login), $Password);

    $Connection->send_response($Response);
    }
    }
    else
    {
    print("Not Exists\n");
    $Connection->send_response(ReadPage("./htm/usernotexistspage.htm"));
    }
    }

    if($Request->url->path eq '/on/')
    {
    my $Login = GetLogin($Request->as_string);
    my $Password = GetPassword($Request->as_string);

    if(IsUserExists($Login, $Password) eq 1)
    {
    if(!exists($UserPassword{$Login}))
    {
    my $IPAddress = GetRemoteIPAddress($Connection);
    my $IPPort = GetRemoteIPPort($Connection);
    my $ID = FreeSessionID();
    $SessionID{$ID} = 1;
    $UserRadiusSessionID{$Login} = $ID;
    if(StartSession($Login, $IPAddress, $IPPort) eq 5)
    {
    #POINT_1
    $UserPassword{$Login} = $Password;
    $UserIPAddress{$Login} = $IPAddress;
    $UserIPPort{$Login} = $IPPort;
    $UserStartTime{$Login} = time();

    # Run FireWall Script Here
    system("./add.sh $ID $IPAddress");

    print($Login . "\n");
    print($UserPassword{$Login} . "\n");
    print(GetRemoteIPAddress($Connection) . "\n");

    if(!exists($IPAddressURL{GetRemoteIPAddress($Connection)}))
    {
    $Connection->send_response(InsertIntoResponse(InsertIntoResponse(InsertIntoResponse(InsertIntoResponse(ReadPage("./htm/offpage.htm"), "|"), "$HTTPHost:$HTTPPort"), $Login), $Password));
    }
    else
    {
    $Connection->send_response(InsertIntoResponse(InsertIntoResponse(InsertIntoResponse(InsertIntoResponse(ReadPage("./htm/offpage.htm"), $IPAddressURL{GetRemoteIPAddress($Connection)}), "$HTTPHost:$HTTPPort"), $Login), $Password));
    delete($IPAddressURL{GetRemoteIPAddress($Connection)});
    }
    }
    }
    else
    {
    $Connection->send_response(ReadPage("./htm/alreadyconnectedpage.htm"));
    }
    }
    else
    {
    $Connection->send_response(ReadPage("./htm/usernotexistspage.htm"));
    }
    }

    if($Request->url->path eq '/off/')
    {
    my $Login = GetLogin($Request->as_string);
    my $Password = GetPassword($Request->as_string);

    if(IsUserExists($Login, $Password) eq 1)
    {
    POINT_2
    print($Login . "\n");
    print($UserPassword{$Login} . "\n");

    if(exists($UserPassword{$Login}))
    {
    if(StopSession($Login, $UserIPAddress{$Login}, $UserIPPort{$Login}, GetSessionTime($Login), GetInBytes($Login, "last"), GetOutBytes($Login, "last")) eq 5)
    {
    delete($UserPassword{$Login});
    my $ID = $UserRadiusSessionID{$Login};
    $SessionID{$ID} = 0;
    delete($UserRadiusSessionID{$Login});
    delete($UserIPAddress{$Login});
    delete($UserIPPort{$Login});
    delete($UserStartTime{$Login});

    # Run FireWall Script Here
    system("./delete.sh $ID");

    $Connection->send_response(InsertIntoResponse(InsertIntoResponse(InsertIntoResponse(ReadPage("./htm/onpage.htm"), "$HTTPHost:$HTTPPort"), $Login), $Password));
    }
    }
    else
    {
    $Connection->send_response(ReadPage("./htm/alreadydisconnectedpage.htm"));
    }
    }
    else
    {
    $Connection->send_response(ReadPage("./htm/usernotexistspage.htm"));
    }
    }
    }
    }
    }
    }
    else
    {
    die($Error . "\n");
    }
    }
    continue
    {
    $Connection->close();
    undef($Connection);
    }
    sub GetLogin
    {
    my $Request = shift;
    my $Pos = index($Request, 'login=') + length('login=');
    my $Login = substr($Request, $Pos, index($Request, '&') - $Pos);
    return $Login;
    }
    sub GetPassword
    {
    my $Request = shift;
    my $Pos = index($Request, 'password=') + length('password=');
    my $Password = substr($Request, $Pos, length($Request) - $Pos - 1);
    return $Password;
    }
    sub IsUserExists
    {
    my $Login = shift;
    my $Password = shift;
    my $Radius = Authen::Radius->new(Host => $RADIUSHost . ":" . $RADIUSAccessPort, Secret => $RADIUSSecret) or die($Error . "\n");
    $Radius->load_dictionary($RADIUSDictionary);
    my $AuthResult = $Radius->check_pwd($Login, $Password);
    return $AuthResult;
    }
    sub InsertIntoResponse
    {
    my $Response = shift;
    my $Data = shift;
    my $Pos = index($Response, '~');
    my $NewResponse = substr($Response, 0, $Pos) . $Data . substr($Response, $Pos + length('~'), length($Response) - $Pos - length('~'));
    return $NewResponse;
    }
    sub StartSession
    {
    my $Login = shift;
    my $IPAddress = shift;
    my $IPPort = shift;
    my $Radius = Authen::Radius->new(Host => $RADIUSHost . ":" . $RADIUSAccountingPort, Secret => $RADIUSSecret) or die($Error . "\n");
    $Radius->load_dictionary($RADIUSDictionary);
    $Radius->add_attributes(
    {Name => 'User-Name', Value => $Login},
    {Name => 'Service-Type', Value => 'Framed-User'},
    {Name => 'NAS-IP-Address', Value => $HTTPHost},
    {Name => 'NAS-Port', Value => $HTTPPort},
    {Name => 'NAS-Port-Type', Value => 'Async'},
    {Name => 'Acct-Session-ID', Value => $UserRadiusSessionID{$Login}},
    {Name => 'Acct-Status-Type', Value => 'Start'}
    );
    $Radius->send_packet(ACCOUNTING_REQUEST);
    my $Receive = $Radius->recv_packet();
    return $Receive;
    }
    sub StopSession
    {
    my $Login = shift;
    my $IPAddress = shift;
    my $IPPort = shift;
    my $SessionTime = shift;
    my $InBytes = shift;
    my $OutBytes = shift;
    my $Radius = Authen::Radius->new(Host => $RADIUSHost . ":" . $RADIUSAccountingPort, Secret => $RADIUSSecret) or die($Error . "\n");
    $Radius->load_dictionary($RADIUSDictionary);
    $Radius->add_attributes(
    {Name => 'User-Name', Value => $Login},
    {Name => 'Service-Type', Value => 'Framed-User'},
    {Name => 'NAS-IP-Address', Value => $HTTPHost},
    {Name => 'NAS-Port', Value => $HTTPPort},
    {Name => 'NAS-Port-Type', Value => 'Async'},
    {Name => 'Acct-Session-ID', Value => $UserRadiusSessionID{$Login}},
    {Name => 'Acct-Status-Type', Value => 'Stop'},
    {Name => 'Acct-Delay-Time', Value => 0},
    {Name => 'Acct-Session-Time', Value => $SessionTime},
    {Name => 'Acct-Input-Octets', Value => $InBytes},
    {Name => 'Acct-Output-Octets', Value => $OutBytes}
    );
    $Radius->send_packet(ACCOUNTING_REQUEST);
    my $Receive = $Radius->recv_packet();
    return $Receive;
    }
    sub UpdateSession
    {
    my $Login = shift;
    my $IPAddress = shift;
    my $IPPort = shift;
    my $SessionTime = shift;
    my $InBytes = shift;
    my $OutBytes = shift;
    my $Radius = Authen::Radius->new(Host => $RADIUSHost . ":" . $RADIUSAccountingPort, Secret => $RADIUSSecret) or die($Error . "\n");
    $Radius->load_dictionary($RADIUSDictionary);
    $Radius->add_attributes(
    {Name => 'User-Name', Value => $Login},
    {Name => 'Service-Type', Value => 'Framed-User'},
    {Name => 'NAS-IP-Address', Value => $HTTPHost},
    {Name => 'NAS-Port', Value => $HTTPPort},
    {Name => 'NAS-Port-Type', Value => 'Async'},
    {Name => 'Acct-Session-ID', Value => $UserRadiusSessionID{$Login}},
    {Name => 'Acct-Status-Type', Value => 'Alive'},
    {Name => 'Acct-Delay-Time', Value => 0},
    {Name => 'Acct-Session-Time', Value => $SessionTime},
    {Name => 'Acct-Input-Octets', Value => $InBytes},
    {Name => 'Acct-Output-Octets', Value => $OutBytes}
    );
    $Radius->send_packet(ACCOUNTING_REQUEST);
    my $Receive = $Radius->recv_packet();
    return $Receive;
    }
    sub GetRemoteIPAddress
    {
    my $Socket = shift;
    my $RemoteIPInfo = getpeername($Socket);
    (my $RemoteIPPort, my $RemoteIPAddress) = unpack_sockaddr_in($RemoteIPInfo);
    $RemoteIPAddress = inet_ntoa($RemoteIPAddress);
    return $RemoteIPAddress;
    }
    sub GetRemoteIPPort
    {
    my $Socket = shift;
    my $RemoteIPInfo = getpeername($Socket);
    (my $RemoteIPPort, my $RemoteIPAddress) = unpack_sockaddr_in($RemoteIPInfo);
    $RemoteIPAddress = inet_ntoa($RemoteIPAddress);
    return $RemoteIPPort;
    }
    sub FreeSessionID
    {
    while(my ($ID, $State) = each(%SessionID))
    {
    if($State eq 0)
    {
    return $ID;
    }
    }
    }
    sub GetSessionTime
    {
    my $Login = shift;
    my $TimeInterval = time() - $UserStartTime{$Login};
    return $TimeInterval;
    }
    sub GetInBytes
    {
    my $Login = shift;
    my $FileName = shift;
    my @Splits;
    my $ID = $UserRadiusSessionID{$Login};
    system("./$FileName.sh $ID");
    my $Input = IO::File->new("./$FileName.txt") or die($Error . "\n");
    while(defined(my $Line = $Input->getline()))
    {
    @Splits = split(" ", $Line);
    }
    $Input->close();
    return $Splits[2];
    }
    sub GetOutBytes
    {
    my $Login = shift;
    my $FileName = shift;
    my @Splits;
    my $ID = $UserRadiusSessionID{$Login};
    system("./$FileName.sh $ID");
    my $Input = IO::File->new("./$FileName.txt") or die($Error . "\n");
    while(defined(my $Line = $Input->getline()))
    {
    @Splits = split(" ", $Line);
    }
    $Input->close();
    return $Splits[1];
    }
    sub OutUser
    {
    while(1)
    {
    print("OutUser\n");

    while(my ($Login, $Password) = each(%UserPassword))
    {
    if(IsUserExists($Login, $Password) eq 1)
    {
    print("Update\n");
    UpdateSession($Login, $UserIPAddress{$Login}, $UserIPPort{$Login}, GetSessionTime($Login), GetInBytes($Login, "current"), GetOutBytes($Login, "current"));
    }
    else
    {
    print("Stop\n");
    StopSession($Login, $UserIPAddress{$Login}, $UserIPPort{$Login}, GetSessionTime($Login), GetInBytes($Login, "current"), GetOutBytes($Login, "current"));

    delete($UserPassword{$Login});
    my $ID = $UserRadiusSessionID{$Login};
    $SessionID{$ID} = 0;
    delete($UserRadiusSessionID{$Login});
    delete($UserIPAddress{$Login});
    delete($UserIPPort{$Login});
    delete($UserStartTime{$Login});

    # Run FireWall Script Here
    system("./delete.sh $ID");
    }
    }

    sleep($RADIUSRefreshTime);
    }
    }
    sub Daemonize
    {
    #chdir('/');
    open(STDIN, '/dev/null');
    open(STDOUT, '>>/dev/null');
    #open(STDERR, '>>/dev/null');
    my $pid = fork();
    exit if $pid;
    setsid;
    umask(0);
    }
    sub GetHost
    {
    my $Request = shift;
    my $Pos = index($Request, 'Host: ') + length('Host: ');
    my $Host = substr($Request, $Pos, length($Request) - $Pos - 1);
    $Pos = index($Host, "\n") + length("\n");
    $Host = substr($Host, 0, $Pos - 1);
    return $Host;
    }
    sub ReadPage
    {
    my $FileName = shift;
    my $Content = "";
    my $Input = IO::File->new($FileName);
    while(defined(my $Line = $Input->getline()))
    {
    $Content = $Content . $Line;
    }
    $Input->close();
    return $Content;
    }

    Thanks.Bye.
    Last edited by HTML; 02-05-2007 at 11:29 AM.



Tags for this Thread