From ee5d8db0cdc4ec50988c18d2ad85d1610b1ee1a7 Mon Sep 17 00:00:00 2001 From: stan Date: Mon, 16 May 2011 22:54:37 +0000 Subject: [PATCH] Moved the runCommand function into the library plus shell detection. Should have been there anyway. git-svn-id: http://svn.zoneminder.com/svn/zm/trunk@3343 e3e1d417-86f3-4887-817a-d78f3d33393f --- scripts/ZoneMinder/lib/ZoneMinder/General.pm | 109 +++++++++++++++++++ scripts/zmpkg.pl.in | 99 ----------------- 2 files changed, 109 insertions(+), 99 deletions(-) diff --git a/scripts/ZoneMinder/lib/ZoneMinder/General.pm b/scripts/ZoneMinder/lib/ZoneMinder/General.pm index a44964d10..f2828621c 100644 --- a/scripts/ZoneMinder/lib/ZoneMinder/General.pm +++ b/scripts/ZoneMinder/lib/ZoneMinder/General.pm @@ -43,6 +43,8 @@ our @ISA = qw(Exporter ZoneMinder::Base); our %EXPORT_TAGS = ( 'functions' => [ qw( executeShellCommand + getCmdFormat + runCommand getEventPath deleteEventFiles makePath @@ -67,6 +69,7 @@ use ZoneMinder::Debug qw(:all); use POSIX; +# For running general shell commands sub executeShellCommand( $ ) { my $command = shift; @@ -81,6 +84,112 @@ sub executeShellCommand( $ ) return( $status ); } +sub getCmdFormat() +{ + Debug( "Testing valid shell syntax\n" ); + + my ( $name ) = getpwuid( $> ); + if ( $name eq ZM_WEB_USER ) + { + Debug( "Running as '$name', su commands not needed\n" ); + return( "" ); + } + + my $null_command = "true"; + + my $prefix = "sudo -u ".ZM_WEB_USER." "; + my $suffix = ""; + my $command = $prefix.$null_command.$suffix; + Debug( "Testing \"$command\"\n" ); + $command .= " > /dev/null 2>&1"; + my $output = qx($command); + my $status = $? >> 8; + if ( !$status ) + { + Debug( "Test ok, using format \"$prefix$suffix\"\n" ); + return( $prefix, $suffix ); + } + else + { + chomp( $output ); + Debug( "Test failed, '$output'\n" ); + + $prefix = "su ".ZM_WEB_USER." --shell=/bin/sh --command='"; + $suffix = "'"; + $command = $prefix.$null_command.$suffix; + Debug( "Testing \"$command\"\n" ); + my $output = qx($command); + my $status = $? >> 8; + if ( !$status ) + { + Debug( "Test ok, using format \"$prefix$suffix\"\n" ); + return( $prefix, $suffix ); + } + else + { + chomp( $output ); + Debug( "Test failed, '$output'\n" ); + + $prefix = "su ".ZM_WEB_USER." -c '"; + $suffix = "'"; + $command = $prefix.$null_command.$suffix; + Debug( "Testing \"$command\"\n" ); + $output = qx($command); + $status = $? >> 8; + if ( !$status ) + { + Debug( "Test ok, using format \"$prefix$suffix\"\n" ); + return( $prefix, $suffix ); + } + else + { + chomp( $output ); + Debug( "Test failed, '$output'\n" ); + } + } + } + Error( "Unable to find valid 'su' syntax\n" ); + exit( -1 ); +} + +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 = ZM_PATH_BIN."/".$command; + if ( $cmdPrefix ) + { + $command = $cmdPrefix.$command.$cmdSuffix; + } + Debug( "Command: $command\n" ); + my $output = qx($command); + my $status = $? >> 8; + chomp( $output ); + if ( $status || zmDbgLevel() > 0 ) + { + if ( $status ) + { + Error( "Unable to run \"$command\", output is \"$output\"\n" ); + exit( -1 ); + } + else + { + Debug( "Output: $output\n" ); + } + } + return( $output ); +} + sub getEventPath( $ ) { my $event = shift; diff --git a/scripts/zmpkg.pl.in b/scripts/zmpkg.pl.in index bd846a543..e1e37d144 100644 --- a/scripts/zmpkg.pl.in +++ b/scripts/zmpkg.pl.in @@ -101,10 +101,6 @@ Info( "Command: $command\n" ); my $retval = 0; -# Determine the appropriate syntax for the su command - -my ( $cmd_prefix, $cmd_suffix ) = getCmdFormat(); - if ( $command eq "state" ) { Info( "Updating DB: $state->{Name}\n" ); @@ -239,98 +235,3 @@ if ( $command eq "logrot" ) } exit( $retval ); - -sub getCmdFormat -{ - Debug( "Testing valid shell syntax\n" ); - - my ( $name ) = getpwuid( $> ); - if ( $name eq ZM_WEB_USER ) - { - Debug( "Running as '$name', su commands not needed\n" ); - return( "" ); - } - - my $null_command = "true"; - - my $prefix = "sudo -u ".ZM_WEB_USER." "; - my $suffix = ""; - my $command = $prefix.$null_command.$suffix; - Debug( "Testing \"$command\"\n" ); - $command .= " > /dev/null 2>&1"; - my $output = qx($command); - my $status = $? >> 8; - if ( !$status ) - { - Debug( "Test ok, using format \"$prefix$suffix\"\n" ); - return( $prefix, $suffix ); - } - else - { - chomp( $output ); - Debug( "Test failed, '$output'\n" ); - - $prefix = "su ".ZM_WEB_USER." --shell=/bin/sh --command='"; - $suffix = "'"; - $command = $prefix.$null_command.$suffix; - Debug( "Testing \"$command\"\n" ); - my $output = qx($command); - my $status = $? >> 8; - if ( !$status ) - { - Debug( "Test ok, using format \"$prefix$suffix\"\n" ); - return( $prefix, $suffix ); - } - else - { - chomp( $output ); - Debug( "Test failed, '$output'\n" ); - - $prefix = "su ".ZM_WEB_USER." -c '"; - $suffix = "'"; - $command = $prefix.$null_command.$suffix; - Debug( "Testing \"$command\"\n" ); - $output = qx($command); - $status = $? >> 8; - if ( !$status ) - { - Debug( "Test ok, using format \"$prefix$suffix\"\n" ); - return( $prefix, $suffix ); - } - else - { - chomp( $output ); - Debug( "Test failed, '$output'\n" ); - } - } - } - Error( "Unable to find valid 'su' syntax\n" ); - exit( -1 ); -} - -sub runCommand -{ - my $command = shift; - $command = ZM_PATH_BIN."/".$command; - if ( $cmd_prefix ) - { - $command = $cmd_prefix.$command.$cmd_suffix; - } - Debug( "Command: $command\n" ); - my $output = qx($command); - my $status = $? >> 8; - chomp( $output ); - if ( $status || DBG_LEVEL > 0 ) - { - if ( $status ) - { - Error( "Unable to run \"$command\", output is \"$output\"\n" ); - exit( -1 ); - } - else - { - Debug( "Output: $output\n" ); - } - } - return( $output ); -}