mirror of
https://github.com/ZoneMinder/zoneminder.git
synced 2026-11-30 20:26:05 -05:00
Two places in ZoneMinder run a command the operator typed: a filter with "Execute command", and the scheduled tasks added for #4957. Both run it as the web user through a shell, which is close to shell access on the server. Until now the only control over that was who may edit, and the answer was anyone with System rights. ZM_ALLOWED_COMMANDS names the programs that may be run, comma separated. Empty is the default and places no restriction, so an upgrade changes nothing and the filters people already have keep working; setting it narrows both features at once. Only the program is matched, because an exact whole-command match could never work for filters: zmfilter.pl appends the event path to whatever the filter stored, so no two runs have the same command line. Arguments are therefore free, which means the program being on the list has to be the whole of the guarantee -- so while a list is in force a command may not contain the characters that start another program: semicolon, pipe, ampersand, backtick and dollar-paren. Without that, "/usr/local/bin/backup.sh; anything" would pass on the strength of its first word. Redirection is deliberately allowed; it starts no program, and refusing it would break the ordinary "... >> /var/log/mine.log". Refused in three places, because each answers a different question. The filter and scheduled task editors refuse on save, so the operator is told at the time rather than by a log line days later. zmfilter.pl refuses at the moment it runs, because a row can reach the database without passing through the web ui and because the setting may have been narrowed since. And cronRebuild leaves a newly disallowed task out of the crontab, so tightening the setting takes jobs away rather than waiting for somebody to edit each one. The rule exists twice, in web/includes/Command.php and ZoneMinder::General, since one is PHP and the other Perl. Rather than hope they stay in step, tests/php/test_allowed_commands.php puts every case to both and fails if they disagree. That earned itself immediately: both split the command on whitespace before handling quotes, so a program path containing a space was cut in half and compared as the wrong string. The cross-check found it in both at once. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
880 lines
25 KiB
Perl
880 lines
25 KiB
Perl
package ZoneMinder::General;
|
|
|
|
use 5.006;
|
|
use strict;
|
|
use warnings;
|
|
|
|
require Exporter;
|
|
require ZoneMinder::Base;
|
|
require ZoneMinder::Storage;
|
|
|
|
our @ISA = qw(Exporter ZoneMinder::Base);
|
|
|
|
# Items to export into callers namespace by default. Note: do not export
|
|
# names by default without a very good reason. Use EXPORT_OK instead.
|
|
# Do not simply export all your public functions/methods/constants.
|
|
|
|
# This allows declaration use ZoneMinder ':all';
|
|
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
|
|
# will save memory.
|
|
our %EXPORT_TAGS = (
|
|
functions => [ qw(
|
|
commandIsAllowed
|
|
commandProgram
|
|
executeShellCommand
|
|
findDbCommand
|
|
getCmdFormat
|
|
runCommand
|
|
setFileOwner
|
|
createEventPath
|
|
createEvent
|
|
makePath
|
|
jsonEncode
|
|
jsonDecode
|
|
jsonLoad
|
|
systemStatus
|
|
packageControl
|
|
daemonControl
|
|
parseNameEqualsValueToHash
|
|
hash_diff
|
|
acquireExclusiveLock
|
|
lockHolderPid
|
|
startedInteractively
|
|
) ]
|
|
);
|
|
push( @{$EXPORT_TAGS{all}}, @{$EXPORT_TAGS{$_}} ) foreach keys %EXPORT_TAGS;
|
|
|
|
our @EXPORT_OK = ( @{ $EXPORT_TAGS{all} } );
|
|
|
|
our @EXPORT = qw();
|
|
|
|
our $VERSION = $ZoneMinder::Base::VERSION;
|
|
|
|
# ==========================================================================
|
|
#
|
|
# General Utility Functions
|
|
#
|
|
# ==========================================================================
|
|
|
|
use ZoneMinder::Config qw(:all);
|
|
use ZoneMinder::Logger qw(:all);
|
|
use ZoneMinder::Database qw(:all);
|
|
|
|
use POSIX;
|
|
use Fcntl qw(:flock O_WRONLY O_CREAT);
|
|
|
|
# The pid recorded in a lock file, or undef when it is empty or unreadable.
|
|
# Whether this process was run by hand rather than started by the daemon
|
|
# manager. zmdc.pl reopens STDIN on /dev/null for everything it starts, so a
|
|
# terminal on STDIN means a person typed this.
|
|
#
|
|
# Used to skip the startup delays that only make sense when the whole system is
|
|
# coming up at once. A run from cron or a unit file has no terminal either and
|
|
# still waits, which is the right way round: those are the unattended cases the
|
|
# delay is for.
|
|
sub startedInteractively {
|
|
return -t STDIN ? 1 : 0;
|
|
}
|
|
|
|
sub lockHolderPid {
|
|
my ($path) = @_;
|
|
my $rfh;
|
|
return undef unless open($rfh, '<', $path);
|
|
my $line = <$rfh>;
|
|
close($rfh);
|
|
return undef unless defined $line;
|
|
chomp $line;
|
|
# The capture also untaints it, since callers log it from taint checked scripts.
|
|
return ($line =~ /^(\d+)$/) ? $1 : undef;
|
|
}
|
|
|
|
# Take an exclusive, non blocking lock on $path, creating it if needed, and
|
|
# record the holder's pid in it. The lock lives for as long as the caller keeps
|
|
# the returned handle, so store it somewhere that outlives the work it guards.
|
|
# It is released when the handle goes out of scope or the process exits,
|
|
# including on a crash, which a lock built out of a pid file or the presence of
|
|
# a socket cannot manage.
|
|
#
|
|
# Returns ($fh, $status, $pid):
|
|
# ($fh, 'ok', $$) the lock is ours
|
|
# (undef, 'held', $pid) someone else has it, and who, when the file says so
|
|
# (undef, 'error', undef) it could not be opened at all, already logged
|
|
#
|
|
# Callers need those two failures kept apart. A lock file left behind by another
|
|
# user cannot be opened for write at all, and reporting that as a duplicate
|
|
# server sends the operator hunting for a process that does not exist.
|
|
sub acquireExclusiveLock {
|
|
my ($path) = @_;
|
|
|
|
# O_CREAT without O_TRUNC on purpose. Truncating at open would throw away the
|
|
# current holder's pid before we know whether we can have the lock, and that
|
|
# pid is the one useful thing the loser has to report. 0666 because whoever
|
|
# runs the daemon next has to be able to open it for write: the socket
|
|
# directory belongs to the web user, but these daemons get run by hand as root
|
|
# often enough that a root owned 0644 lock file would wedge that monitor for
|
|
# good.
|
|
my $fh;
|
|
if (!sysopen($fh, $path, O_WRONLY|O_CREAT, 0666)) {
|
|
Error("Can't open lock file $path: $!");
|
|
return (undef, 'error', undef);
|
|
}
|
|
# umask takes the group and other write bits back off the mode above, so set
|
|
# them again. Harmlessly fails when we did not create it.
|
|
chmod(0666, $path);
|
|
|
|
if (!flock($fh, LOCK_EX|LOCK_NB)) {
|
|
my $pid = lockHolderPid($path);
|
|
close($fh);
|
|
return (undef, 'held', $pid);
|
|
}
|
|
|
|
# Ours now, so say who we are and let the next process name us rather than
|
|
# leaving someone to run fuser over the file.
|
|
truncate($fh, 0);
|
|
seek($fh, 0, 0);
|
|
syswrite($fh, "$$\n");
|
|
return ($fh, 'ok', $$);
|
|
}
|
|
|
|
# For running general shell commands
|
|
sub executeShellCommand {
|
|
my $command = shift;
|
|
my $output = qx($command);
|
|
$output = '' if !defined($output);
|
|
chomp($output);
|
|
my $status = $? >> 8;
|
|
if ($status) {
|
|
Warning("Failed Command: $command Status: $status Output: $output");
|
|
} elsif (logDebugging()) {
|
|
Debug("Command: $command Status: $status Output: $output");
|
|
}
|
|
return $status;
|
|
}
|
|
|
|
# Map legacy MySQL command names to MariaDB-native equivalents.
|
|
# MariaDB is deprecating the mysql-prefixed names in favour of its own.
|
|
my %DB_COMMAND_MAP = (
|
|
'mysql' => 'mariadb',
|
|
'mysqldump' => 'mariadb-dump',
|
|
);
|
|
my %_db_command_cache;
|
|
|
|
sub findDbCommand {
|
|
my $legacy_name = shift;
|
|
return $_db_command_cache{$legacy_name} if exists $_db_command_cache{$legacy_name};
|
|
|
|
my $maria_name = $DB_COMMAND_MAP{$legacy_name};
|
|
if ($maria_name) {
|
|
my $path = qx(command -v $maria_name 2>/dev/null);
|
|
chomp($path);
|
|
if ($path) {
|
|
Debug("Using '$maria_name' for database command '$legacy_name'");
|
|
$_db_command_cache{$legacy_name} = $maria_name;
|
|
return $maria_name;
|
|
}
|
|
}
|
|
|
|
Debug("Using legacy '$legacy_name' for database command");
|
|
$_db_command_cache{$legacy_name} = $legacy_name;
|
|
return $legacy_name;
|
|
}
|
|
|
|
sub getCmdFormat {
|
|
Debug('Testing valid shell syntax');
|
|
|
|
my ( $name ) = getpwuid( $> );
|
|
if ( $name eq $Config{ZM_WEB_USER} ) {
|
|
Debug("Running as '$name', su commands not needed");
|
|
return '';
|
|
}
|
|
|
|
my $null_command = 'true';
|
|
|
|
my $prefix = 'sudo -u '.$Config{ZM_WEB_USER}.' ';
|
|
my $suffix = '';
|
|
my $command = $prefix.$null_command.$suffix;
|
|
Debug("Testing \"$command\"");
|
|
my $output = qx($command 2>&1);
|
|
my $status = $? >> 8;
|
|
$output //= $!;
|
|
|
|
if ( !$status ) {
|
|
Debug("Test ok, using format \"$prefix<command>$suffix\"");
|
|
return( $prefix, $suffix );
|
|
} else {
|
|
chomp( $output );
|
|
Debug("Test failed, '$output'");
|
|
|
|
$prefix = 'su -s /bin/sh '.$Config{ZM_WEB_USER}.q` -c '`;
|
|
$suffix = q`'`;
|
|
$command = $prefix.$null_command.$suffix;
|
|
Debug("Testing \"$command\"");
|
|
my $output = qx($command 2>&1);
|
|
my $status = $? >> 8;
|
|
$output //= $!;
|
|
|
|
if ( !$status ) {
|
|
Debug("Test ok, using format \"$prefix<command>$suffix\"");
|
|
return( $prefix, $suffix );
|
|
} else {
|
|
chomp($output);
|
|
Debug("Test failed, '$output'");
|
|
|
|
$prefix = 'su '.$Config{ZM_WEB_USER}.' -c \'';
|
|
$suffix = '\'';
|
|
$command = $prefix.$null_command.$suffix;
|
|
Debug("Testing \"$command\"");
|
|
$output = qx($command 2>&1);
|
|
$status = $? >> 8;
|
|
$output //= $!;
|
|
|
|
if ( !$status ) {
|
|
Debug("Test ok, using format \"$prefix<command>$suffix\"");
|
|
return( $prefix, $suffix );
|
|
} else {
|
|
chomp($output);
|
|
Debug("Test failed, '$output'");
|
|
}
|
|
}
|
|
}
|
|
Error('Unable to find valid su syntax');
|
|
exit -1;
|
|
} # end sub getCmdFormat
|
|
|
|
our $testedShellSyntax = 0;
|
|
our ( $cmdPrefix, $cmdSuffix );
|
|
|
|
# For running ZM daemons etc
|
|
sub runCommand {
|
|
if ( !$testedShellSyntax ) {
|
|
# Determine the appropriate syntax for the su command
|
|
( $cmdPrefix, $cmdSuffix ) = getCmdFormat();
|
|
$testedShellSyntax = !undef;
|
|
}
|
|
|
|
my $command = shift;
|
|
$command = $Config{ZM_PATH_BIN}.'/'.$command;
|
|
if ( $cmdPrefix ) {
|
|
$command = $cmdPrefix.$command.$cmdSuffix;
|
|
}
|
|
Debug("Command: $command");
|
|
my $output = qx($command);
|
|
my $status = $? >> 8;
|
|
chomp($output);
|
|
if ( $status || logDebugging() ) {
|
|
if ( $status ) {
|
|
Error("Unable to run \"$command\", output is \"$output\", status is $status");
|
|
} else {
|
|
Debug("Output: $output");
|
|
}
|
|
}
|
|
return $output;
|
|
} # end sub runCommand
|
|
|
|
sub createEventPath {
|
|
my $event = shift;
|
|
my $eventPath = $event->Path();
|
|
$event->createPath();
|
|
$event->createIdFile();
|
|
$event->createLinkPath();
|
|
|
|
return $eventPath;
|
|
}
|
|
|
|
use Data::Dumper;
|
|
|
|
our $_setFileOwner = undef;
|
|
our ( $_ownerUid, $_ownerGid );
|
|
|
|
sub _checkProcessOwner {
|
|
if ( !defined($_setFileOwner) ) {
|
|
my ( $processOwner ) = getpwuid( $> );
|
|
if ( $processOwner ne $Config{ZM_WEB_USER} ) {
|
|
# Not running as web user, so should be root in which case chown
|
|
# the temporary directory
|
|
( my $ownerName, my $ownerPass, $_ownerUid, $_ownerGid )
|
|
= getpwnam( $Config{ZM_WEB_USER} )
|
|
or Fatal( "Can't get user details for web user '"
|
|
.$Config{ZM_WEB_USER}."': $!"
|
|
);
|
|
$_setFileOwner = 1;
|
|
} else {
|
|
$_setFileOwner = 0;
|
|
}
|
|
}
|
|
return $_setFileOwner;
|
|
}
|
|
|
|
sub setFileOwner {
|
|
my $file = shift;
|
|
|
|
if ( _checkProcessOwner() ) {
|
|
chown($_ownerUid, $_ownerGid, $file)
|
|
or Fatal("Can't change ownership of file '$file' to '"
|
|
.$Config{ZM_WEB_USER}.':'.$Config{ZM_WEB_GROUP}."': $!"
|
|
);
|
|
}
|
|
}
|
|
|
|
our $_hasImageInfo = undef;
|
|
|
|
sub _checkForImageInfo {
|
|
if ( !defined($_hasImageInfo) ) {
|
|
my $result = eval {
|
|
require Image::Info;
|
|
Image::Info->import();
|
|
};
|
|
$_hasImageInfo = $@?0:1;
|
|
}
|
|
return $_hasImageInfo;
|
|
}
|
|
|
|
sub createEvent {
|
|
my $event = shift;
|
|
|
|
Debug('Creating event');
|
|
#print( Dumper( $event )."\n" );
|
|
|
|
_checkForImageInfo();
|
|
|
|
my $dbh = zmDbConnect();
|
|
|
|
if ( $event->{monitor} ) {
|
|
$event->{MonitorId} = $event->{monitor}->{Id};
|
|
} elsif ( $event->{MonitorId} ) {
|
|
my $sql = "select * from Monitors where Id = ?";
|
|
my $sth = $dbh->prepare_cached( $sql )
|
|
or Fatal( "Can't prepare sql '$sql': ".$dbh->errstr() );
|
|
my $res = $sth->execute( $event->{MonitorId} )
|
|
or Fatal( "Can't execute sql '$sql': ".$sth->errstr() );
|
|
$event->{monitor} = $sth->fetchrow_hashref()
|
|
or Fatal( "Unable to create event, can't load monitor with id '"
|
|
.$event->{MonitorId}."'"
|
|
);
|
|
$sth->finish();
|
|
} else {
|
|
Fatal( "Unable to create event, no monitor or monitor id supplied" );
|
|
}
|
|
$event->{Name} = "New Event" unless( $event->{Name} );
|
|
$event->{Frames} = int(@{$event->{frames}});
|
|
$event->{TotScore} = $event->{MaxScore} = 0;
|
|
|
|
my $lastTimestamp = 0.0;
|
|
foreach my $frame ( @{$event->{frames}} ) {
|
|
if ( !$event->{Width} ) {
|
|
if ( $_hasImageInfo ) {
|
|
my $imageInfo = Image::Info::image_info( $frame->{imagePath} );
|
|
if ( $imageInfo->{error} ) {
|
|
Error( "Unable to extract image info from '"
|
|
.$frame->{imagePath}."': ".$imageInfo->{error}
|
|
);
|
|
} else {
|
|
( $event->{Width}, $event->{Height} ) = Image::Info::dim( $imageInfo );
|
|
}
|
|
}
|
|
}
|
|
$frame->{Type} = $frame->{Score}>0?'Alarm':'Normal' unless( $frame->{Type} );
|
|
$frame->{Delta} = $lastTimestamp?($frame->{TimeStamp}-$lastTimestamp):0.0;
|
|
$event->{StartDateTime} = $frame->{TimeStamp} unless ( $event->{StartDateTime} );
|
|
$event->{TotScore} += $frame->{Score};
|
|
$event->{MaxScore} = $frame->{Score} if ( $frame->{Score} > $event->{MaxScore} );
|
|
$event->{AlarmFrames}++ if ( $frame->{Type} eq 'Alarm' );
|
|
$event->{EndDateTime} = $frame->{TimeStamp};
|
|
$lastTimestamp = $frame->{TimeStamp};
|
|
}
|
|
$event->{Width} = $event->{monitor}->{Width} unless( $event->{Width} );
|
|
$event->{Height} = $event->{monitor}->{Height} unless( $event->{Height} );
|
|
$event->{AvgScore} = $event->{TotScore}/int($event->{AlarmFrames});
|
|
$event->{Length} = $event->{EndDateTime} - $event->{StartDateTime};
|
|
|
|
my %formats = (
|
|
StartDateTime => 'from_unixtime(?)',
|
|
EndDateTime => 'from_unixtime(?)',
|
|
);
|
|
|
|
my ( @fields, @formats, @values );
|
|
while ( my ( $field, $value ) = each( %$event ) ) {
|
|
next unless $field =~ /^[A-Z]/;
|
|
push( @fields, $field );
|
|
push( @formats, ($formats{$field} or '?') );
|
|
push( @values, $event->{$field} );
|
|
}
|
|
|
|
my $sql = "INSERT INTO Events (".join(',',@fields)
|
|
.") VALUES (".join(',',@formats).")"
|
|
;
|
|
my $sth = $dbh->prepare_cached( $sql )
|
|
or Fatal( "Can't prepare sql '$sql': ".$dbh->errstr() );
|
|
my $res = $sth->execute( @values )
|
|
or Fatal( "Can't execute sql '$sql': ".$sth->errstr() );
|
|
# Portable across DBD::mysql and DBD::MariaDB; see ZoneMinder::Object.
|
|
$event->{Id} = $dbh->last_insert_id(undef, undef, undef, undef);
|
|
Info( "Created event ".$event->{Id} );
|
|
|
|
if ( $event->{EndDateTime} ) {
|
|
$event->{Name} = $event->{monitor}->{EventPrefix}.$event->{Id}
|
|
if ( $event->{Name} eq 'New Event' );
|
|
my $sql = "update Events set Name = ? where Id = ?";
|
|
my $sth = $dbh->prepare_cached( $sql )
|
|
or Fatal( "Can't prepare sql '$sql': ".$dbh->errstr() );
|
|
my $res = $sth->execute( $event->{Name}, $event->{Id} )
|
|
or Fatal( "Can't execute sql '$sql': ".$sth->errstr() );
|
|
}
|
|
|
|
my $eventPath = createEventPath( $event );
|
|
|
|
my %frameFormats = (
|
|
TimeStamp => 'from_unixtime(?)',
|
|
);
|
|
my $frameId = 1;
|
|
foreach my $frame ( @{$event->{frames}} ) {
|
|
$frame->{EventId} = $event->{Id};
|
|
$frame->{FrameId} = $frameId++;
|
|
|
|
my ( @fields, @formats, @values );
|
|
while ( my ( $field, $value ) = each( %$frame ) ) {
|
|
next unless $field =~ /^[A-Z]/;
|
|
push( @fields, $field );
|
|
push( @formats, ($frameFormats{$field} or '?') );
|
|
push( @values, $frame->{$field} );
|
|
}
|
|
|
|
my $sql = "insert into Frames (".join(',',@fields)
|
|
.") values (".join(',',@formats).")"
|
|
;
|
|
my $sth = $dbh->prepare_cached( $sql )
|
|
or Fatal( "Can't prepare sql '$sql': ".$dbh->errstr() );
|
|
my $res = $sth->execute( @values )
|
|
or Fatal( "Can't execute sql '$sql': ".$sth->errstr() );
|
|
#$frame->{FrameId} = $dbh->{mysql_insertid};
|
|
if ( $frame->{imagePath} ) {
|
|
$frame->{capturePath} = sprintf(
|
|
"%s/%0".$Config{ZM_EVENT_IMAGE_DIGITS}
|
|
."d-capture.jpg"
|
|
, $eventPath
|
|
, $frame->{FrameId}
|
|
);
|
|
rename( $frame->{imagePath}, $frame->{capturePath} )
|
|
or Fatal( "Can't copy ".$frame->{imagePath}
|
|
." to ".$frame->{capturePath}.": $!"
|
|
);
|
|
setFileOwner( $frame->{capturePath} );
|
|
if ( $event->{SaveJPEGs} > 1 ) {
|
|
$frame->{analysePath} = sprintf(
|
|
"%s/%0".$Config{ZM_EVENT_IMAGE_DIGITS}
|
|
."d-analyse.jpg"
|
|
, $eventPath
|
|
, $frame->{FrameId}
|
|
);
|
|
link( $frame->{capturePath}, $frame->{analysePath} )
|
|
or Fatal( "Can't link ".$frame->{capturePath}
|
|
." to ".$frame->{analysePath}.": $!"
|
|
);
|
|
setFileOwner( $frame->{analysePath} );
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
sub addEventImage {
|
|
my $event = shift;
|
|
my $frame = shift;
|
|
|
|
# TBD
|
|
}
|
|
|
|
sub updateEvent {
|
|
my $event = shift;
|
|
|
|
if ( !$event->{EventId} ) {
|
|
Error( "Unable to update event, no event id supplied" );
|
|
return( 0 );
|
|
}
|
|
|
|
my $dbh = zmDbConnect();
|
|
|
|
$event->{Name} = $event->{monitor}->{EventPrefix}.$event->{Id}
|
|
if ( $event->{Name} eq 'New Event' );
|
|
|
|
my %formats = (
|
|
StartDateTime => 'from_unixtime(?)',
|
|
EndDateTime => 'from_unixtime(?)',
|
|
);
|
|
|
|
my ( @values, @sets );
|
|
while ( my ( $field, $value ) = each( %$event ) ) {
|
|
next if ( $field eq 'Id' );
|
|
push( @values, $event->{$field} );
|
|
push( @sets, $field." = ".($formats{$field} or '?') );
|
|
}
|
|
my $sql = "update Events set ".join(',',@sets)." where Id = ?";
|
|
push( @values, $event->{Id} );
|
|
|
|
my $sth = $dbh->prepare_cached( $sql )
|
|
or Fatal( "Can't prepare sql '$sql': ".$dbh->errstr() );
|
|
my $res = $sth->execute( @values )
|
|
or Fatal( "Can't execute sql '$sql': ".$sth->errstr() );
|
|
}
|
|
|
|
sub makePath {
|
|
my $path = shift;
|
|
my $root = shift;
|
|
$root = (( $path =~ m|^/| )?'':'.' ) unless( $root );
|
|
|
|
Debug( "Creating path '$path' in $root'\n" );
|
|
my @parts = split( '/', $path );
|
|
my $fullPath = $root;
|
|
foreach my $dir ( @parts ) {
|
|
$fullPath .= '/'.$dir;
|
|
if ( !-d $fullPath ) {
|
|
if ( -e $fullPath ) {
|
|
Fatal( "Can't create '$fullPath', already exists as non directory" );
|
|
} else {
|
|
Debug( "Creating '$fullPath'\n" );
|
|
mkdir( $fullPath, 0755 ) or Fatal( "Can't mkdir '$fullPath': $!" );
|
|
setFileOwner( $fullPath );
|
|
}
|
|
}
|
|
}
|
|
return( $fullPath );
|
|
}
|
|
|
|
our $testedJSON = 0;
|
|
our $hasJSONAny = 0;
|
|
|
|
sub _testJSON {
|
|
return if $testedJSON;
|
|
my $result = eval {
|
|
require JSON::MaybeXS;
|
|
JSON::MaybeXS->import();
|
|
};
|
|
$testedJSON = 1;
|
|
$hasJSONAny = 1 if $result;
|
|
}
|
|
|
|
sub _getJSONType {
|
|
my $value = shift;
|
|
return 'null' unless defined($value);
|
|
return 'integer' if $value =~ /^\d+$/;
|
|
return 'double' if $value =~ /^\d+$/;
|
|
return 'hash' if ref($value) eq 'HASH';
|
|
return 'array' if ref($value) eq 'ARRAY';
|
|
return 'string';
|
|
}
|
|
|
|
sub jsonEncode {
|
|
my $value = shift;
|
|
|
|
_testJSON();
|
|
if ($hasJSONAny) {
|
|
my $string = eval {
|
|
JSON::MaybeXS->encode_json($value);
|
|
};
|
|
Error('Unable to encode object to JSON: '.$@) unless $string;
|
|
return $string;
|
|
}
|
|
|
|
my $type = _getJSONType($value);
|
|
if ($type eq 'integer' || $type eq 'double') {
|
|
return '"'.$value.'"';
|
|
} elsif ($type eq 'boolean') {
|
|
return $value ? 'true' : 'false';
|
|
} elsif ( $type eq 'string' ) {
|
|
$value =~ s|(["\\/])|\\$1|g;
|
|
$value =~ s|\r?\n|\n|g;
|
|
return '"'.$value.'"';
|
|
} elsif ( $type eq 'null' ) {
|
|
return 'null';
|
|
} elsif ( $type eq 'array' ) {
|
|
return '['.join( ',', map { jsonEncode( $_ ) } @$value ).']';
|
|
} elsif ( $type eq 'hash' ) {
|
|
my $result = '{';
|
|
while ( my ( $subKey=>$subValue ) = each(%$value) ) {
|
|
$result .= ',' if ( $result ne '{' );
|
|
$result .= '"'.$subKey.'":'.jsonEncode($subValue);
|
|
}
|
|
return $result.'}';
|
|
} else {
|
|
Error("Unexpected type '$type'");
|
|
return '';
|
|
}
|
|
}
|
|
|
|
sub jsonDecode {
|
|
my $value = shift;
|
|
|
|
_testJSON();
|
|
if ( $hasJSONAny ) {
|
|
my $object = eval { JSON::MaybeXS->decode_json($value) };
|
|
Fatal("Unable to decode JSON string '$value': $@") unless $object;
|
|
return $object;
|
|
}
|
|
|
|
my $comment = 0;
|
|
my $unescape = 0;
|
|
my $out = '';
|
|
my @chars = split(//, $value);
|
|
for ( my $i = 0; $i < @chars; $i++ ) {
|
|
if ( !$comment ) {
|
|
if ( $chars[$i] eq ':' ) {
|
|
$out .= '=>';
|
|
} else {
|
|
$out .= $chars[$i];
|
|
}
|
|
} elsif ( !$unescape ) {
|
|
if ( $chars[$i] eq '\\' ) {
|
|
$unescape = 1;
|
|
} else {
|
|
$out .= $chars[$i];
|
|
}
|
|
} else {
|
|
if ( $chars[$i] ne '/' ) {
|
|
$out .= '\\';
|
|
}
|
|
$out .= $chars[$i];
|
|
$unescape = 0;
|
|
}
|
|
if ( $chars[$i] eq '"' ) {
|
|
$comment = !$comment;
|
|
}
|
|
}
|
|
$out =~ s/=>true/=>1/g;
|
|
$out =~ s/=>false/=>0/g;
|
|
$out =~ s/=>null/=>undef/g;
|
|
$out =~ s/`/'/g;
|
|
$out =~ s/qx/qq/g;
|
|
( $out ) = $out =~ m/^(\{.+\})$/; # Detaint and check it's a valid object syntax
|
|
my $result = eval $out;
|
|
Fatal($@) if $@;
|
|
return $result;
|
|
}
|
|
|
|
sub jsonLoad {
|
|
my $file = shift;
|
|
my $json = undef;
|
|
eval {
|
|
require File::Slurp;
|
|
my $contents = File::Slurp::read_file($file);
|
|
if (!$contents) {
|
|
Error("No contents for $file");
|
|
return $json;
|
|
}
|
|
require JSON::MaybeXS;
|
|
$json = JSON::MaybeXS::decode_json($contents);
|
|
};
|
|
Error($@) if $@;
|
|
return $json;
|
|
}
|
|
|
|
sub parseNameEqualsValueToHash {
|
|
my %settings;
|
|
foreach my $line ( split ( /\r?\n/, $_[0] ) ) {
|
|
next if ! $line;
|
|
next if ! ( $line =~ /=/ );
|
|
my ($name, $value ) = split('=', $line);
|
|
$value =~ s/^'//;
|
|
$value =~ s/'$//;
|
|
$settings{$name} = defined $value ? $value : '';
|
|
}
|
|
return wantarray ? %settings : \%settings;
|
|
}
|
|
|
|
sub hash_diff {
|
|
# assumes keys of second hash are all in the first hash
|
|
my ( $settings, $defaults ) = @_;
|
|
my %updates;
|
|
my %defaults = %{$defaults} if $defaults;
|
|
|
|
foreach my $setting ( keys %{$settings} ) {
|
|
next if ! exists $defaults{$setting};
|
|
|
|
if ((ref $$settings{$setting}) ne (ref $defaults{$setting})) {
|
|
Warning("Different refs for $setting");
|
|
next;
|
|
}
|
|
|
|
if (ref $$settings{$setting} eq 'HASH') {
|
|
my %u = hash_diff($$settings{$setting}, $defaults{$setting});
|
|
if (%u) {
|
|
$updates{$setting} = \%u;
|
|
}
|
|
} elsif (
|
|
($$settings{$setting} and ! $defaults{$setting})
|
|
or
|
|
(!$$settings{$setting} and $defaults{$setting})
|
|
or
|
|
(
|
|
($$settings{$setting} and $defaults{$setting} and (
|
|
$$settings{$setting} ne $defaults{$setting}))
|
|
)
|
|
) {
|
|
$updates{$setting} = $defaults{$setting};
|
|
}
|
|
} # end foreach setting
|
|
foreach my $key (keys %{$defaults}) {
|
|
if (!exists $$settings{$key} and defined($$defaults{$key})) {
|
|
$updates{$key} = $$defaults{$key};
|
|
}
|
|
}
|
|
return %updates;
|
|
}
|
|
|
|
sub packageControl {
|
|
my $command = shift;
|
|
my $string = $Config{ZM_PATH_BIN}.'/zmpkg.pl '.$command;
|
|
$string .= ' 2>/dev/null >&- <&- >/dev/null';
|
|
executeShellCommand($string);
|
|
}
|
|
|
|
sub daemonControl {
|
|
my ($command, $daemon, $args) = @_;
|
|
my $string = $Config{ZM_PATH_BIN}.'/zmdc.pl '.$command;
|
|
if ( $daemon ) {
|
|
$string .= ' ' . $daemon;
|
|
if ( $args ) {
|
|
$string .= ' ' . $args;
|
|
}
|
|
}
|
|
#$string .= ' 2>/dev/null >&- <&- >/dev/null';
|
|
executeShellCommand($string);
|
|
}
|
|
|
|
sub systemStatus {
|
|
my $command = $Config{ZM_PATH_BIN}.'/zmdc.pl check';
|
|
my $output = qx($command);
|
|
my $status = $? >> 8;
|
|
if ( $status || logDebugging() ) {
|
|
$output = '' if !defined($output);
|
|
chomp($output);
|
|
Debug("Command: $command Output: $output");
|
|
}
|
|
return $output;
|
|
}
|
|
|
|
# --- Which commands a filter or scheduled task may run ----------------------
|
|
#
|
|
# ZM_ALLOWED_COMMANDS narrows "Execute command" and the scheduled tasks to a
|
|
# list the administrator chose. Empty, the default and what every existing
|
|
# install has, places no restriction and keeps the behaviour ZoneMinder has
|
|
# always had.
|
|
#
|
|
# The same rule lives in web/includes/Command.php, because the web ui refuses
|
|
# a command when it is saved and this refuses one when it is about to run --
|
|
# and a row can reach the database without passing through the web ui.
|
|
# tests/php/test_allowed_commands.php checks the two against each other.
|
|
|
|
# The characters that let one command start another. Refused outright while a
|
|
# list is in force: a program being on the list says nothing about what else
|
|
# runs alongside it if the command can chain. Redirection is deliberately not
|
|
# here; it starts no program.
|
|
our @COMMAND_CHAINING = (';', '|', '&', '`', '$(');
|
|
|
|
# The program a command invokes: its first word, with any surrounding quotes
|
|
# removed. undef when there is not one. Only the program is matched, so
|
|
# arguments may vary -- which they must, since zmfilter.pl appends the event
|
|
# path to whatever the filter has stored.
|
|
sub commandProgram {
|
|
my $command = shift;
|
|
return undef if !defined $command;
|
|
$command =~ s/^\s+|\s+$//g;
|
|
return undef if $command eq '';
|
|
|
|
# A quoted path may contain spaces, so the quoted run has to be taken before
|
|
# splitting on whitespace -- splitting first would cut
|
|
# "/usr/local/bin/my script.sh" in half and compare the wrong thing.
|
|
if ($command =~ /^(["'])/) {
|
|
my $quote = $1;
|
|
# An unterminated quote is not a program name we can read.
|
|
return undef if $command !~ /^\Q$quote\E([^\Q$quote\E]*)\Q$quote\E/;
|
|
my $program = $1;
|
|
return ($program eq '') ? undef : $program;
|
|
}
|
|
|
|
my ($program) = split(/\s+/, $command, 2);
|
|
return undef if !defined $program or $program eq '';
|
|
return ($program eq '') ? undef : $program;
|
|
}
|
|
|
|
# May $command be run, given the configured list? $allowed is the raw setting;
|
|
# an empty one means no restriction.
|
|
sub commandIsAllowed {
|
|
my ($command, $allowed) = @_;
|
|
$allowed = '' if !defined $allowed;
|
|
|
|
my @list = grep { $_ ne '' } map { my $e = $_; $e =~ s/^\s+|\s+$//g; $e } split(/,/, $allowed);
|
|
return 1 if !@list;
|
|
|
|
return 0 if !defined $command;
|
|
foreach my $chain (@COMMAND_CHAINING) {
|
|
return 0 if index($command, $chain) >= 0;
|
|
}
|
|
|
|
my $program = commandProgram($command);
|
|
return 0 if !defined $program;
|
|
return (grep { $_ eq $program } @list) ? 1 : 0;
|
|
}
|
|
|
|
1;
|
|
__END__
|
|
# Below is stub documentation for your module. You'd better edit it!
|
|
|
|
=head1 NAME
|
|
|
|
ZoneMinder::General - Utility Functions for ZoneMinder
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
use ZoneMinder::General;
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
This module contains the common definitions and functions used by the rest
|
|
of the ZoneMinder scripts
|
|
|
|
=head2 EXPORT
|
|
|
|
functions => [ qw(
|
|
executeShellCommand
|
|
findDbCommand
|
|
getCmdFormat
|
|
runCommand
|
|
setFileOwner
|
|
createEventPath
|
|
createEvent
|
|
makePath
|
|
jsonEncode
|
|
jsonDecode
|
|
packageControl
|
|
daemonControl
|
|
systemStatus
|
|
parseNameEqualsValueToHash
|
|
hash_diff
|
|
acquireExclusiveLock
|
|
lockHolderPid
|
|
) ]
|
|
|
|
|
|
=head1 AUTHOR
|
|
|
|
Philip Coombes, E<lt>philip.coombes@zoneminder.comE<gt>
|
|
|
|
=head1 COPYRIGHT AND LICENSE
|
|
|
|
Copyright (C) 2001-2008 Philip Coombes
|
|
|
|
This program 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.
|
|
|
|
This program 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 this program; if not, write to the Free Software
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
=cut
|