/usr/share/perl5/Apache/Ocsinventory/Server/System.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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 | ###############################################################################
## OCSINVENTORY-NG
## Copyleft Pascal DANEK 2005
## 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::System;
use Apache::Ocsinventory::Server::System::Config;
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();
}
&check_config();
}
require Exporter;
our @ISA = qw /Exporter/;
our @EXPORT = qw /
_log
_debug
_lock
_unlock
_send_file
/;
our %EXPORT_TAGS = (
'server' => [
qw/
_get_xml_parser_opt
_get_sys_options
_database_connect
_end
_check_deviceid
_log
_debug
_lock
_unlock
_send_file
_inflate
_get_spec_params
_get_spec_params_g
/
]
);
our @EXPORT_OK = (
qw /
_log
_lock
_modules_get_request_handler
_modules_get_pre_inventory_options
_modules_get_post_inventory_options
_modules_get_prolog_readers
_modules_get_prolog_writers
_modules_get_duplicate_handlers
/
);
Exporter::export_ok_tags('server');
use Apache::Ocsinventory::Server::Constants;
sub _get_sys_options{
return 0 if $ENV{OCS_OPT_OPTIONS_NOT_OVERLOADED};
# Wich options enabled ?
#############
# We read the table config looking for the ivalues of these options
my $dbh = $Apache::Ocsinventory::CURRENT_CONTEXT{'DBI_HANDLE'};
my $row;
my $request = $dbh->prepare('SELECT * FROM config');
$request->execute;
# read options defined in ocs GUI
while($row=$request->fetchrow_hashref){
for( keys %CONFIG ){
if( $row->{'NAME'} eq $_){
$ENV{'OCS_OPT_'.$_} = $row->{ $CONFIG{$_}->{type} };
}
}
}
$request->finish;
0;
}
# Try other compress algorythm
sub _inflate{
my @inflate_subs = (
# gzip file content
sub { my $data_ref = shift;
if(my $result = Compress::Zlib::memGunzip( ${$data_ref})){
$Apache::Ocsinventory::CURRENT_CONTEXT{'DEFLATE_SUB'} = \&Compress::Zlib::memGzip;
&_log(321,'system', 'gzip') if $ENV{'OCS_OPT_LOGLEVEL'};
return $result;
}
undef;
},
sub {
my $ref = shift;
if($$ref =~ /^<\?xml/i){
$Apache::Ocsinventory::CURRENT_CONTEXT{'DEFLATE_SUB'} = sub {return $_[0]};
&_log(321,'system', 'not_deflated') if $ENV{'OCS_OPT_LOGLEVEL'};
return $$ref;
}
undef;
}
);
my $data_ref = shift;
my $inflated_ref = shift;
for( @inflate_subs ){
last if( $$inflated_ref = &{$_}($data_ref) );
};
1;
}
# Database connection
sub _database_connect{
my $mode = shift;
my %params;
my ($host, $database, $port, $user, $password);
if($mode eq 'write'){
($host, $database, $port, $user, $password) = ( $ENV{'OCS_DB_HOST'}, $ENV{'OCS_DB_NAME'}, $ENV{'OCS_DB_PORT'},
$ENV{'OCS_DB_USER'}, $Apache::Ocsinventory::CURRENT_CONTEXT{'APACHE_OBJECT'}->dir_config('OCS_DB_PWD') );
}
# Local Mode
elsif($mode eq 'local'){
($host, $port, $user, $password) = ( $ENV{'OCS_DB_HOST'}, $ENV{'OCS_DB_PORT'},
$ENV{'OCS_DB_USER'}, $Apache::Ocsinventory::CURRENT_CONTEXT{'APACHE_OBJECT'}->dir_config('OCS_DB_PWD') );
$database = $ENV{'OCS_DB_LOCAL'}||$ENV{'OCS_DB_NAME'};
}
# Slave mode
elsif($mode eq 'read'){
if($ENV{'OCS_DB_SL_HOST'}){
$host = $ENV{'OCS_DB_SL_HOST'};
$database = $ENV{'OCS_DB_SL_NAME'}||'ocsweb';
$port = $ENV{'OCS_DB_SL_PORT'}||'3306';
$user = $ENV{'OCS_DB_SL_USER'};
$password = $Apache::Ocsinventory::CURRENT_CONTEXT{'APACHE_OBJECT'}->dir_config('OCS_DB_SL_PWD');
}
else{
$host = $ENV{'OCS_DB_HOST'};
$database = $ENV{'OCS_DB_NAME'}||'ocsweb';
$port = $ENV{'OCS_DB_PORT'}||'3306';
$user = $ENV{'OCS_DB_USER'};
$password = $Apache::Ocsinventory::CURRENT_CONTEXT{'APACHE_OBJECT'}->dir_config('OCS_DB_PWD');
}
}
else{
&_log(521,'database_connect', 'invalid_mode') if $ENV{'OCS_OPT_LOGLEVEL'};
return undef;
}
$params{'AutoCommit'} = 0;
$params{'PrintError'} = $ENV{'OCS_OPT_DBI_PRINT_ERROR'};
# Optionnaly a mysql socket different than the client's built in
$params{'mysql_socket'} = $ENV{'OCS_OPT_DBI_MYSQL_SOCKET'} if $ENV{'OCS_OPT_DBI_MYSQL_SOCKET'};
# Connection...
my $dbh = DBI->connect("DBI:mysql:database=$database;host=$host;port=$port", $user, $password, \%params);
$dbh->do("SET NAMES 'utf8'") if($dbh && $ENV{'OCS_OPT_UNICODE_SUPPORT'});
$dbh->do("SET sql_mode='NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'");
return $dbh;
}
sub _check_deviceid{
my $DeviceID = shift;
# If we do not find it
unless(defined($DeviceID)){
return(1);
}
# If it is not conform
unless($DeviceID=~/^.+-\d{4}(?:-\d{2}){5}$/){
return(1);
}
0;
}
sub _lock{
my $device = shift;
my $dbh = $Apache::Ocsinventory::CURRENT_CONTEXT{'DBI_HANDLE'} || shift;
if($dbh->do('INSERT INTO locks(HARDWARE_ID, ID, SINCE) VALUES(?,?,NULL)', {} , $device, $$ )){
$Apache::Ocsinventory::CURRENT_CONTEXT{'LOCK_FL'} = 1
if $device eq ${Apache::Ocsinventory::CURRENT_CONTEXT{'DATABASE_ID'}};
return(0);
}else{
if( $ENV{'OCS_OPT_LOCK_REUSE_TIME'} ){
if( $dbh->do( 'SELECT * FROM locks WHERE HARDWARE_ID=? AND (UNIX_TIMESTAMP()-UNIX_TIMESTAMP(SINCE))>?',
{}, $device, $ENV{'OCS_OPT_LOCK_REUSE_TIME'} ) != '0E0' ) {
&_log(516,'lock', 'reuse_lock') if $ENV{'OCS_OPT_LOGLEVEL'};
if( $dbh->do('UPDATE locks SET ID=? WHERE HARDWARE_ID=?', {}, $$, $device) ){
$Apache::Ocsinventory::CURRENT_CONTEXT{'LOCK_FL'} = 1;
return 0;
}
else{
&_log(516,'lock', 'failed') if $ENV{'OCS_OPT_LOGLEVEL'};
return 1;
}
}
}
return 1;
}
}
sub _unlock{
my $device = shift;
my $dbh = $Apache::Ocsinventory::CURRENT_CONTEXT{'DBI_HANDLE'} || shift;
if($dbh->do('DELETE FROM locks WHERE HARDWARE_ID=? AND ID=?', {}, $device, $$)){
$Apache::Ocsinventory::CURRENT_CONTEXT{'LOCK_FL'} = 0 if $device eq ${Apache::Ocsinventory::CURRENT_CONTEXT{'DATABASE_ID'}};
return(0);
}else{
return(1);
}
}
sub _log{
my $code = shift;
my $phase = shift;
my $comment = shift;
my $DeviceID = $Apache::Ocsinventory::CURRENT_CONTEXT{'DEVICEID'}||'NA';
my $ipaddress = $Apache::Ocsinventory::CURRENT_CONTEXT{'IPADDRESS'}||'??';
our $LOG;
if(!$LOG){
open LOG, '>>'.$ENV{'OCS_OPT_LOGPATH'}.'/activity.log' or die "Failed to open log file : $! ($ENV{'OCS_OPT_LOGPATH'})\n";
# We don't want buffer, so we allways flush the handles
select(LOG);
$|=1;
$LOG = \*LOG;
}
print $LOG localtime().";$$;$code;$DeviceID;$ipaddress;".&_get_http_header('User-agent',$Apache::Ocsinventory::CURRENT_CONTEXT{'APACHE_OBJECT'}).";$phase;".($comment?$comment:"")."\n";
}
# Subroutine called at the end of execution
sub _end{
my $ret = shift;
my $dbh = $Apache::Ocsinventory::CURRENT_CONTEXT{'DBI_HANDLE'};
my $DeviceID = $Apache::Ocsinventory::CURRENT_CONTEXT{'DATABASE_ID'};
#Non-transactionnal table
&_unlock($DeviceID) if $Apache::Ocsinventory::CURRENT_CONTEXT{'LOCK_FL'};
if( $ret==APACHE_SERVER_ERROR ){
&_log(515,'end', 'error') if $ENV{'OCS_OPT_LOGLEVEL'};
$dbh->rollback;
}
elsif( $ret eq APACHE_BAD_REQUEST ){
&_log(515,'end', 'bad_request') if $ENV{'OCS_OPT_LOGLEVEL'};
}
else{
$dbh->commit;
}
close(our $LOG) && undef $LOG if defined $LOG;
$dbh->disconnect;
return $ret;
}
# Retrieve option request handler
sub _modules_get_request_handler{
my $request = shift;
my %search = (
'REQUEST_NAME' => $request
);
my @ret = &_modules_search(\%search, 'HANDLER_REQUEST');
return($ret[0]);
}
# Retrieve options with preinventory handler
sub _modules_get_pre_inventory_options{
return(&_modules_search(undef, 'HANDLER_PRE_INVENTORY'));
}
# Retrieve options with postinventory handler
sub _modules_get_post_inventory_options{
return(&_modules_search(undef, 'HANDLER_POST_INVENTORY'));
}
# Retrieve options with prolog_read
sub _modules_get_prolog_readers{
return(&_modules_search(undef, 'HANDLER_PROLOG_READ'));
}
# Retrieve options with prolog_resp
sub _modules_get_prolog_writers{
return(&_modules_search(undef, 'HANDLER_PROLOG_RESP'));
}
# Retrieve duplicate handlers
sub _modules_get_duplicate_handlers{
return(&_modules_search(undef, 'HANDLER_DUPLICATE'));
}
# Read options structures
sub _modules_search{
# Take a hash ref and return an array
# The hash indicate the desire handler is the second arg
my $search = shift;
my $handler = shift;
my @ret;
my $count;
my $module;
my $search_key;
my $module_key;
for $module (@{$Apache::Ocsinventory::OPTIONS_STRUCTURE}){
$count = 0;
if($search){
for $search_key (keys(%$search)){
if($search_key eq 'REQUEST_NAME'){
$count ++ if defined($module->{$search_key}) and ($module->{$search_key} eq $search->{$search_key});
}elsif($search_key eq 'TYPE'){
$count ++ if defined($module->{$search_key}) and ($module->{$search_key} == $search->{$search_key});
}
}
if($count == keys(%$search)){
push @ret, $module->{$handler} if $module->{$handler};
$count = 0;
}
}else{
push @ret, $module->{$handler} if $module->{$handler};
}
}
if(@ret){
return(@ret);
}else{
return(0);
}
}
sub _get_xml_parser_opt{
my $hash_ref = shift;
$hash_ref->{'SuppressEmpty'} = 1;
$hash_ref->{'ForceArray'} = [];
@{$hash_ref->{'ForceArray'}} = &_get_xml_parser_opt_force_array();
}
sub _get_xml_parser_opt_force_array{
my @ret;
# Core
push @ret, @{$Apache::Ocsinventory::Server::Inventory::XML_PARSER_OPT{'ForceArray'}};
# Options
push @ret, @Apache::Ocsinventory::XMLParseOptForceArray;
for my $module (@{$Apache::Ocsinventory::OPTIONS_STRUCTURE}){
push @ret, @{$module->{'XML_PARSER_OPT'}->{'ForceArray'}} if $module->{'XML_PARSER_OPT'}->{'ForceArray'}=~/ARRAY/;
}
return @ret;
}
#
sub _send_file{
# We want to know if the file is available
my $context = shift;
my $request;
my $row;
my $r = $Apache::Ocsinventory::CURRENT_CONTEXT{'APACHE_OBJECT'};
my $dbh = $Apache::Ocsinventory::CURRENT_CONTEXT{'DBI_HANDLE'};
if($context eq 'deploy'){
my $file = shift;
$request=$dbh->prepare('SELECT CONTENT FROM deploy WHERE NAME=?');
$request->execute($file);
# If not, we return a bad request and log the event
unless($request->rows){
&_log(511,'deploy','no_file') if $ENV{'OCS_OPT_LOGLEVEL'};
return APACHE_BAD_REQUEST;
}else{
# We extract the file and send it
$row = $request->fetchrow_hashref();
# We force this content type to avoid the direct interpretation of, for example, a plain text file
&_set_http_header('Cache-control' => $ENV{'OCS_OPT_PROXY_REVALIDATE_DELAY'},$r);
&_set_http_header('Content-length' => length($row->{'CONTENT'}),$r);
&_set_http_content_type('Application/octet-stream',$r);
&_send_http_headers($r);
$r->print($row->{'CONTENT'});
# We log it
&_log(302,'deploy','file_transmitted') if $ENV{'OCS_OPT_LOGLEVEL'};
return APACHE_OK;
}
}
}
sub _get_spec_params{
my $hardwareId = $Apache::Ocsinventory::CURRENT_CONTEXT{'DATABASE_ID'};
return &_params_from_devices($hardwareId);
}
sub _get_spec_params_g{
my $groups = $Apache::Ocsinventory::CURRENT_CONTEXT{'MEMBER_OF'};
my %result;
for(@$groups){
$result{$_} = {&_params_from_devices($_)} ;
}
return %result;
}
sub _params_from_devices{
my $hardwareId = shift;
my $dbh = $Apache::Ocsinventory::CURRENT_CONTEXT{'DBI_HANDLE'};
my $sth;
my $row;
my %result;
$sth = $dbh->prepare('SELECT NAME,IVALUE,TVALUE FROM devices WHERE HARDWARE_ID=?');
$sth->execute($hardwareId);
while( $row = $sth->fetchrow_hashref()){
if(exists($result{ $row->{'NAME'} })){
if($result{ $row->{'NAME'} } =~ /ARRAY/){
push @{$result{$row->{'NAME'}}},
{ 'IVALUE' => $row->{'IVALUE'}, 'TVALUE' => $row->{'TVALUE'} };
}
else{
my $temp = $result{ $row->{'NAME'} };
$result{ $row->{'NAME'} } = [];
push @{$result{$row->{'NAME'}}}, $temp;
}
}
else{
$result{ $row->{'NAME'} } = {
'IVALUE' => $row->{'IVALUE'},
'TVALUE' => $row->{'TVALUE'}
};
}
}
return %result;
}
1;
|