File: Inventory.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 (212 lines) | stat: -rw-r--r-- 7,225 bytes parent folder | download
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
###############################################################################
## 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::Inventory;

use strict;

BEGIN{
	if($ENV{'OCS_MODPERL_VERSION'} == 1){
		require Apache::Ocsinventory::Server::Modperl1;
		Apache::Ocsinventory::Server::Modperl1->import();
	}elsif($ENV{'OCS_MODPERL_VERSION'} == 2){
		require Apache::Ocsinventory::Server::Modperl2;
		Apache::Ocsinventory::Server::Modperl2->import();
	}
}

require Exporter;

our @ISA = qw /Exporter/;

our @EXPORT = qw /_inventory_handler/;

use Apache::Ocsinventory::Server::Constants;
use Apache::Ocsinventory::Server::System qw / :server /;

use Apache::Ocsinventory::Server::Communication;
use Apache::Ocsinventory::Server::Communication::Session;
use Apache::Ocsinventory::Server::Duplicate;

use Apache::Ocsinventory::Server::Inventory::Data;
use Apache::Ocsinventory::Server::Inventory::Capacities;
use Apache::Ocsinventory::Server::Inventory::Export;
use Apache::Ocsinventory::Server::Inventory::Update;
use Apache::Ocsinventory::Server::Inventory::Filter;
use Apache::Ocsinventory::Server::Inventory::Update::AccountInfos;

our %XML_PARSER_OPT = (
	'ForceArray' => []
);

# Remanent data
my ( %SECTIONS, @SECTIONS );

&_init_map( \%SECTIONS, \@SECTIONS );
&_get_parser_ForceArray( $XML_PARSER_OPT{ForceArray} );

sub _context{
  my $dbh = $Apache::Ocsinventory::CURRENT_CONTEXT{'DBI_HANDLE'};
  
  if(!$Apache::Ocsinventory::CURRENT_CONTEXT{'EXIST_FL'}){
    $dbh->do('INSERT INTO hardware(DEVICEID) VALUES(?)', {}, $Apache::Ocsinventory::CURRENT_CONTEXT{'DEVICEID'}) or return(1);
    my $request = $dbh->prepare('SELECT ID FROM hardware WHERE DEVICEID=?');
    unless($request->execute($Apache::Ocsinventory::CURRENT_CONTEXT{'DEVICEID'})){
      &_log(518,'inventory','id_error') if $ENV{'OCS_OPT_LOGLEVEL'};
      return(1);
    }
    my $row = $request->fetchrow_hashref;
    $Apache::Ocsinventory::CURRENT_CONTEXT{'DATABASE_ID'} = $row->{'ID'};
  }
  return(0);
}

sub _inventory_handler{
  
  # Call to preinventory handlers
  if( &_pre_options() == INVENTORY_STOP ){
    &_log(107,'inventory','stopped_by_module') if $ENV{'OCS_OPT_LOGLEVEL'};
    return APACHE_FORBIDDEN;
  }

  return APACHE_SERVER_ERROR if &_context();
  
  my $dbh = $Apache::Ocsinventory::CURRENT_CONTEXT{'DBI_HANDLE'};
  
  # Lock device
  if(&_lock($Apache::Ocsinventory::CURRENT_CONTEXT{'DATABASE_ID'})){
    &_log( 516, 'inventory', 'device_locked');
    return(APACHE_FORBIDDEN);
  }
  
  # Check prolog
  if( !check_session( \%Apache::Ocsinventory::CURRENT_CONTEXT ) ){
    &_log( 114, 'inventory', 'no_session');
    if( !$Apache::Ocsinventory::CURRENT_CONTEXT{'IS_TRUSTED'} && $ENV{OCS_OPT_INVENTORY_SESSION_ONLY} ){
      &_log( 115, 'inventory', 'refused');
      return(APACHE_FORBIDDEN);
    }
  }

  #Inventory incoming
  &_log(104,'inventory','incoming') if $ENV{'OCS_OPT_LOGLEVEL'};
  
  # Put the inventory in the database
  return APACHE_SERVER_ERROR if &_update_inventory( \%SECTIONS, \@SECTIONS );
  
  #Committing inventory
  $dbh->commit;
  #Call to post inventory handlers
  &_post_options();
  
  #############
  # Manage several questions, including duplicates
  &_post_inventory();
  
  # That's all
  &_log(101,'inventory','transmitted') if $ENV{'OCS_OPT_LOGLEVEL'};
  return APACHE_OK;
}

sub _post_inventory{
  my $request;
  my $row;
  my $red;
  my $accountkey;
  my %elements;

  my @accountFields = _get_account_fields();
  
  my $deviceId = $Apache::Ocsinventory::CURRENT_CONTEXT{'DATABASE_ID'};
  my $result = $Apache::Ocsinventory::CURRENT_CONTEXT{'XML_ENTRY'};
  my $dbh = $Apache::Ocsinventory::CURRENT_CONTEXT{'DBI_HANDLE'};

  &_generate_ocs_file();
  &kill_session( \%Apache::Ocsinventory::CURRENT_CONTEXT );
  
  $red = &_duplicate_main();
  # We verify accountinfo diff if the machine was already in the database
  if($Apache::Ocsinventory::CURRENT_CONTEXT{'EXIST_FL'} or $red){
    # We put back the account infos to the agent if necessary
    $request = $dbh->prepare('SELECT * FROM accountinfo WHERE HARDWARE_ID=?');
    $request->execute($deviceId);
    if($row = $request->fetchrow_hashref()){
      my $up = 0;
      # Compare the account infos with the user's ones
      for $accountkey (@accountFields){
        for(@{$result->{CONTENT}->{ACCOUNTINFO}}){
          if($_->{KEYNAME} eq $accountkey){
            utf8::encode($_->{KEYVALUE});    #We encode string to be able to make comparison if string is UTF8
            $up=1,last if($_->{KEYVALUE} ne $row->{$accountkey});
          }
        }
      }
      # If there is something new in the table
      if(
          !exists($result->{CONTENT}->{ACCOUNTINFO})
          ||
          @accountFields != @{$result->{CONTENT}->{ACCOUNTINFO}}
      ) {
          $up = 1 
      }
      
      if($up){
        # we write the xml data
        $elements{'RESPONSE'} = [ 'ACCOUNT_UPDATE' ];
        for(@accountFields){
          push @{$elements{'ACCOUNTINFO'}}, { 'KEYNAME' => [ $_ ], 'KEYVALUE' => [ $row->{$_} ] };
        }
        $request->finish;
        # send the update to the client
        &_send_response(\%elements);
        return;
      }else{
        $request->finish;
        &_send_response({'RESPONSE' => [ 'NO_ACCOUNT_UPDATE' ]});
        return;
      }
      
    }else{
      # There is a problem. The device MUST be present in the table
      &_log(509,'postinventory','no_account_infos') if $ENV{'OCS_OPT_LOGLEVEL'};
      $request->finish;
      $elements{'RESPONSE'} = [ 'ACCOUNT_UPDATE' ];
      for(@{$result->{CONTENT}->{ACCOUNTINFO}}){
        if($_->{KEYNAME} eq 'TAG'){
          push @{$elements{'ACCOUNTINFO'}}, { 'KEYNAME' => [$_->{KEYNAME}], 'KEYVALUE' => [ 'LOST' ] };
        }else{
          push @{$elements{'ACCOUNTINFO'}}, { 'KEYNAME' => [ $_->{KEYNAME} ], 'KEYVALUE' => [ $_->{KEYVALUE} ] };
        }
      }
      # call accountinfo to insert agent values in addition to the LOST value in TAG
      &_accountinfo("lost");
      # send the update to the client with TAG=LOST
      &_send_response(\%elements);
      return;
    }
  }else{
    &_send_response({'RESPONSE' => [ 'NO_ACCOUNT_UPDATE' ]});
    return;
  }	
  0;
}

1;