This file is indexed.

/usr/share/perl5/Apache/Ocsinventory/Server/Inventory/Data.pm is in ocsinventory-server 2.0.5-1.1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
###############################################################################
## OCSINVENTORY-NG 
## Copyleft Pascal DANEK 2008
## Web : http://www.ocsinventory-ng.org
##
## This code is open source and may be copied and modified as long as the source
## code is always made freely available.
## Please refer to the General Public Licence http://www.gnu.org/ or Licence.txt
################################################################################
package Apache::Ocsinventory::Server::Inventory::Data;

use strict;

require Exporter;

our @ISA = qw /Exporter/;

our @EXPORT = qw / 
  _init_map 
  _get_bind_values 
  _has_changed 
  _get_parser_ForceArray 
/;

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

sub _init_map{
  my ($sectionsMeta, $sectionsList) = @_;
  my $section;
  my @bind_num;
  my $field;
  my $fields_string;
  my $field_index;

  # Parse every section
  for $section (keys(%DATA_MAP)){
    $field_index = 0;
    # Field array (from data_map field hash keys), filtered fields and cached fields
    $sectionsMeta->{$section}->{field_arrayref} = [];
    $sectionsMeta->{$section}->{field_filtered} = [];
    $sectionsMeta->{$section}->{field_cached} = {};
    ##############################################
    #Don't process the sections that are use for capacities special inventory
    next if $DATA_MAP{$section}->{capacities};
    # Don't process the non-auto-generated sections
    next if !$DATA_MAP{$section}->{auto};
    $sectionsMeta->{$section}->{multi} = 1 if $DATA_MAP{$section}->{multi};
    $sectionsMeta->{$section}->{mask} = $DATA_MAP{$section}->{mask};
    $sectionsMeta->{$section}->{delOnReplace} = 1 if $DATA_MAP{$section}->{delOnReplace};
    $sectionsMeta->{$section}->{writeDiff} = 1 if $DATA_MAP{$section}->{writeDiff};
    $sectionsMeta->{$section}->{cache} = 1 if $DATA_MAP{$section}->{cache};
    $sectionsMeta->{$section}->{mandatory} = 1 if $DATA_MAP{$section}->{mandatory};
    $sectionsMeta->{$section}->{auto} = 1 if $DATA_MAP{$section}->{auto};
    $sectionsMeta->{$section}->{name} = $section;
    # $sectionsMeta->{$section}->{hasChanged} is set while inventory update
     
    # Parse fields of the current section
    for $field ( keys(%{$DATA_MAP{$section}->{fields}} ) ){
      if(!$DATA_MAP{$section}->{fields}->{$field}->{noSql}){
        push @{$sectionsMeta->{$section}->{field_arrayref}}, $field;
        $sectionsMeta->{$section}->{noSql} = 1 unless $sectionsMeta->{$section}->{noSql};
      }
      if($DATA_MAP{$section}->{fields}->{$field}->{filter}){
        next unless $ENV{OCS_OPT_INVENTORY_FILTER_ENABLED};
        push @{$sectionsMeta->{$section}->{field_filtered}}, $field;
        $sectionsMeta->{$section}->{filter} = 1 unless $sectionsMeta->{$section}->{filter};
      }
      if($DATA_MAP{$section}->{fields}->{$field}->{cache}){
        next unless $ENV{OCS_OPT_INVENTORY_CACHE_ENABLED};
        $sectionsMeta->{$section}->{field_cached}->{$field} = $field_index;
        $sectionsMeta->{$section}->{cache} = 1 unless $sectionsMeta->{$section}->{cache};
      }
      if(defined $DATA_MAP{$section}->{fields}->{$field}->{fallback}){
        $sectionsMeta->{$section}->{fields}->{$field}->{fallback} = $DATA_MAP{$section}->{fields}->{$field}->{fallback};
      }

      if(defined $DATA_MAP{$section}->{fields}->{$field}->{type}){
        $sectionsMeta->{$section}->{fields}->{$field}->{type} = $DATA_MAP{$section}->{fields}->{$field}->{type};
      }     
 
      $field_index++;      
    }
    # Build the "DBI->prepare" sql insert string 
    $fields_string = join ',', ('HARDWARE_ID', @{$sectionsMeta->{$section}->{field_arrayref}});
    $sectionsMeta->{$section}->{sql_insert_string} = "INSERT INTO $section($fields_string) VALUES(";
    for(0..@{$sectionsMeta->{$section}->{field_arrayref}}){
      push @bind_num, '?';
    }
    
    $sectionsMeta->{$section}->{sql_insert_string}.= (join ',', @bind_num).')';
    @bind_num = ();
    # Build the "DBI->prepare" sql select string 
    $sectionsMeta->{$section}->{sql_select_string} = "SELECT ID,$fields_string FROM $section 
      WHERE HARDWARE_ID=? ORDER BY ".$DATA_MAP{$section}->{sortBy};
    # Build the "DBI->prepare" sql deletion string 
    $sectionsMeta->{$section}->{sql_delete_string} = "DELETE FROM $section WHERE HARDWARE_ID=? AND ID=?";
    # to avoid many "keys"
    push @$sectionsList, $section;
  }

  #Special treatment for hardware section
  $sectionsMeta->{'hardware'} = &_get_hardware_fields; 
  push @$sectionsList, 'hardware';

}

sub _get_bind_values{
  my ($refXml, $sectionMeta, $arrayToFeed) = @_;
  
  my ($bind_value, $xmlvalue, $xmlfield);

  for my $field ( @{ $sectionMeta->{field_arrayref} } ) {
    if(defined($refXml->{$field}) && !defined($sectionMeta->{fields}->{$field}->{type}) && $refXml->{$field} ne '' && $refXml->{$field} ne '??' && $refXml->{$field}!~/^N\/?A$/) {
      $bind_value = $refXml->{$field}
    }
    else{
       if( defined $sectionMeta->{fields}->{$field}->{fallback} ){
         $bind_value = $sectionMeta->{fields}->{$field}->{fallback};
         &_log( 000, 'fallback', "$field:".$sectionMeta->{fields}->{$field}->{fallback}) if $ENV{'OCS_OPT_LOGLEVEL'}>1;
       }
       else{
         &_log( 000, 'generic-fallback', "$field:".$sectionMeta->{fields}->{$field}->{fallback}) if $ENV{'OCS_OPT_LOGLEVEL'}>1;
         $bind_value = '';
       }
    }

    # We have to substitute the value with the ID matching "type_section_field.name" if the field is tagged "type".
    # It allows to support different DB structures
    if(defined $sectionMeta->{fields}->{$field}->{type}) {
      $xmlfield = $field;
      $xmlfield =~ s/_ID//g;    #We delete the _ID pattern to be in concordance with XML 

      if(defined $sectionMeta->{fields}->{$field}->{fallback}) {
        $bind_value = _get_type_id($sectionMeta->{name}, $xmlfield, $sectionMeta->{fields}->{$field}->{fallback} );
        &_log( 000, 'fallback', "$field:".$sectionMeta->{fields}->{$field}->{fallback}) if $ENV{'OCS_OPT_LOGLEVEL'}>1;

       } else {  #No fallback for this field
        $xmlvalue = $refXml->{$xmlfield};
        $bind_value = _get_type_id($sectionMeta->{name}, $xmlfield, $xmlvalue);
      }
    }
    push @$arrayToFeed, $bind_value;
  }
}

sub _get_parser_ForceArray{
  my $arrayRef = shift;
  for my $section (keys(%DATA_MAP)){
    unless ($DATA_MAP{$section}->{capacities}) {
     # Feed the multilines section array in order to parse xml correctly
     push @{ $arrayRef }, uc $section if $DATA_MAP{$section}->{multi};
    }
  }
}

sub _has_changed{
  my $section = shift;
  my $result = $Apache::Ocsinventory::CURRENT_CONTEXT{'XML_ENTRY'};
  
  # Don't use inventory diff if section mask is
  return 1 if $DATA_MAP{$section}->{mask}==0;
   
  # Check checksum to know if section has changed
  if( defined($result->{CONTENT}->{HARDWARE}->{CHECKSUM}) ){
    return $DATA_MAP{$section}->{mask} & $result->{CONTENT}->{HARDWARE}->{CHECKSUM};
  }
  else{
    &_log( 524, 'inventory', "$section (no checksum)") if $ENV{'OCS_OPT_LOGLEVEL'};
    return 1;
  }
}

sub _get_type_id {
#TODO: create it if needed
# Type table structure
# CREATE TABLE type_${section}_$field (
#   ID INTEGER NOT NULL auto_increment,
#   NAME VARCHAR(255))
# ENGINE=INNODB, DEFAULT CHARSET=UTF8 ;
# For migration, add the following line :
# ALTER TABLE $section CHANGE COLUMN $field $field_ID INTEGER NOT NULL ;

  my ($section, $field, $value) = @_ ;
  
  my $dbh = $Apache::Ocsinventory::CURRENT_CONTEXT{'DBI_HANDLE'} ;
  
  my ($id, $existsReq, $createSql);

  my $table_name = 'type_'.lc $section.'_'.lc $field ;

  if ($value eq '') {  #Value from XML is empty 
    $existsReq = $dbh->prepare("SELECT ID FROM $table_name WHERE NAME IS NULL") ;
    $createSql = "INSERT INTO $table_name(NAME) VALUES(NULL)" ;
    $existsReq->execute() ;
  } else {
    $existsReq = $dbh->prepare("SELECT ID FROM $table_name WHERE NAME=?") ;
    $createSql = "INSERT INTO $table_name(NAME) VALUES(?)" ;
    $existsReq->execute($value) ;
  }

  # It exists
  if($existsReq->rows){
    my $row = $existsReq->fetchrow_hashref() ;
    $id = $row->{ID} ;
  }
  # It does not exist
  else{

    if ($value eq '') {
      $dbh->do($createSql) && $existsReq->execute();
    } else { 
      $dbh->do($createSql, {}, $value) && $existsReq->execute($value);
    }

    my $row = $existsReq->fetchrow_hashref() ;
    $id = $row->{ID} ;
  }

  return $id ;
}

sub _get_hardware_fields {
  my $sectionMeta = {};
  my $field;
  my $field_index=0;  #Variable for feeding in _cache function

  #We only get cache fields 
  for $field ( keys(%{$DATA_MAP{'hardware'}->{fields}} ) ){

    if($DATA_MAP{'hardware'}->{fields}->{$field}->{cache}){
      next unless $ENV{OCS_OPT_INVENTORY_CACHE_ENABLED};
      $sectionMeta->{field_cached}->{$field}=$field_index;
      $field_index++;
    }
  }
  return $sectionMeta;
}

1;