File: Session.pm

package info (click to toggle)
ocsinventory-server 2.5%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,088 kB
  • sloc: php: 27,462; perl: 8,241; sh: 1,680; sql: 1,355; xml: 1,041; makefile: 34
file content (130 lines) | stat: -rw-r--r-- 4,572 bytes parent folder | download | duplicates (3)
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
###############################################################################
## Copyright 2005-2016 OCSInventory-NG/OCSInventory-Server contributors.
## See the Contributors file for more details about them.
## 
## This file is part of OCSInventory-NG/OCSInventory-ocsreports.
##
## OCSInventory-NG/OCSInventory-Server is free software: you can redistribute
## it and/or modify it under the terms of the GNU General Public License as
## published by the Free Software Foundation, either version 2 of the License,
## or (at your option) any later version.
##
## OCSInventory-NG/OCSInventory-Server is distributed in the hope that it
## will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
## of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with OCSInventory-NG/OCSInventory-ocsreports. if not, write to the
## Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
## MA 02110-1301, USA.
################################################################################
package Apache::Ocsinventory::Server::Communication::Session;

use strict;

use Apache::Ocsinventory::Server::System(qw/ :server /);

require Exporter;

our @ISA = qw /Exporter/;

our @EXPORT = qw /
  start_session
  check_session
  kill_session
/;

sub start_session{
  my $current_context = shift;
  my $dbh = $current_context->{DBI_HANDLE};
  my $deviceId = $current_context->{DEVICEID}; 
  
  clean_sessions( $current_context );
  
  # Trying to start session
  if( !$dbh->do( 'INSERT INTO prolog_conntrack(DEVICEID,PID,TIMESTAMP) VALUES(?,?,UNIX_TIMESTAMP())', {}, $deviceId, $$) ){
    &_log(525,'session', 'failed') if $ENV{'OCS_OPT_LOGLEVEL'};
    return 0;
  }
  &_log(311,'session', 'started') if $ENV{'OCS_OPT_LOGLEVEL'};
  return 1;
}

sub clean_sessions{
  my $current_context = shift;
  my $dbh = $current_context->{DBI_HANDLE};
  
  # To avoid race conditions
  if( !$dbh->do("INSERT INTO engine_mutex(NAME, PID, TAG) VALUES('SESSION',?,'CLEAN')", {}, $$) ){
    &_log(315,'session',"already handled") if $ENV{'OCS_OPT_LOGLEVEL'};
    return;
  }
  
  # We have to make it every SESSION_CLEAN_TIME seconds
  my $check_clean = $dbh->prepare('SELECT UNIX_TIMESTAMP()-IVALUE AS IVALUE FROM engine_persistent WHERE NAME="SESSION_CLEAN_DATE"');
  if($check_clean->execute() && $check_clean->rows()){
    my $row = $check_clean->fetchrow_hashref();
    if($row->{IVALUE}< $ENV{OCS_OPT_SESSION_CLEAN_TIME} ){
      $dbh->do('DELETE FROM engine_mutex WHERE PID=? AND NAME="SESSION" AND TAG="CLEAN"', {}, $$ );
      return;
    }
  }
  &_log(314,'session', "clean(check)") if $ENV{'OCS_OPT_LOGLEVEL'};
  # Delete old sessions
  $dbh->do('INSERT INTO engine_persistent(NAME,IVALUE) VALUES("SESSION_CLEAN_DATE", UNIX_TIMESTAMP())')
    if($dbh->do('UPDATE engine_persistent SET IVALUE=UNIX_TIMESTAMP() WHERE NAME="SESSION_CLEAN_DATE"')==0E0);
    
  my $cleaned = $dbh->do('DELETE FROM prolog_conntrack WHERE UNIX_TIMESTAMP()-TIMESTAMP>?', {}, $ENV{OCS_OPT_SESSION_CLEAN_TIME} );
  
  $dbh->do('DELETE FROM engine_mutex WHERE PID=? AND NAME="SESSION" AND TAG="CLEAN"', {}, $$ );
  
  &_log(316,'session', "clean($cleaned)") if $cleaned && ($cleaned!=0E0) && $ENV{'OCS_OPT_LOGLEVEL'};
}

sub check_session {
  my $current_context = shift;
  my $dbh = $current_context->{DBI_HANDLE};
  my $deviceId = $current_context->{DEVICEID};
  
  unless( $ENV{OCS_OPT_SESSION_VALIDITY_TIME} ){
    &_log(317,'session', 'always_true') if $ENV{'OCS_OPT_LOGLEVEL'};
    return 1;
  }
  
  my $check = $dbh->do('SELECT DEVICEID FROM prolog_conntrack WHERE DEVICEID=? AND (UNIX_TIMESTAMP()-TIMESTAMP<?)',
               {}, $deviceId, $ENV{OCS_OPT_SESSION_VALIDITY_TIME});
  if(!$check){
    &_log(526,'session', 'error') if $ENV{'OCS_OPT_LOGLEVEL'};
    return 0;
  }
  elsif($check==0E0){
    &_log(318,'session', 'missing') if $ENV{'OCS_OPT_LOGLEVEL'};
    return 0;
  }
  else{
    &_log(319,'session', 'found') if $ENV{'OCS_OPT_LOGLEVEL'};
    return 1;
  }
}

sub kill_session{
  my $current_context = shift;
  my $dbh = $current_context->{DBI_HANDLE};
  my $deviceId = $current_context->{DEVICEID};
  
  my $code = $dbh->do('DELETE FROM prolog_conntrack WHERE DEVICEID=?', {}, $deviceId);
  if(!$code){
    &_log(527,'session', 'error') if $ENV{'OCS_OPT_LOGLEVEL'};
    return 0;
  }
  elsif( $code != 0E0 ){
    &_log(320,'session', 'end') if $ENV{'OCS_OPT_LOGLEVEL'};
    return 1;
  }
  else{
    return 0;
  }
}

1;