From fdb2944893dab61db69322dc81498015265a52d3 Mon Sep 17 00:00:00 2001 From: Jash Date: Thu, 19 Nov 2026 10:32:47 +0530 Subject: [PATCH] fix: ONVIF PTZ requests send the SOAP action of the operation in the body refs #6773 --- .../lib/ZoneMinder/Control/ONVIF.pm | 6 +- .../lib/ZoneMinder/Control/Reolink.pm | 4 +- .../ZoneMinder/Control/TapoC520WS_ONVIF.pm | 8 +- scripts/ZoneMinder/t/ptz_soapaction.t | 102 ++++++++++++++++++ 4 files changed, 111 insertions(+), 9 deletions(-) create mode 100644 scripts/ZoneMinder/t/ptz_soapaction.t diff --git a/scripts/ZoneMinder/lib/ZoneMinder/Control/ONVIF.pm b/scripts/ZoneMinder/lib/ZoneMinder/Control/ONVIF.pm index d058fb4716..861adaeccf 100644 --- a/scripts/ZoneMinder/lib/ZoneMinder/Control/ONVIF.pm +++ b/scripts/ZoneMinder/lib/ZoneMinder/Control/ONVIF.pm @@ -751,7 +751,7 @@ sub _auto_stop { '; $self->sendCmd($$self{onvif_path}, $body, - 'http://www.onvif.org/ver20/ptz/wsdl/ContinuousMove'); + 'http://www.onvif.org/ver20/ptz/wsdl/Stop'); } # --- Directional continuous moves ---------------------------------------- @@ -786,7 +786,7 @@ sub moveStop { '; $self->sendCmd($$self{onvif_path}, $body, - 'http://www.onvif.org/ver20/ptz/wsdl/ContinuousMove'); + 'http://www.onvif.org/ver20/ptz/wsdl/Stop'); } sub zoomStop { @@ -803,7 +803,7 @@ sub zoomStop { '; $self->sendCmd($$self{onvif_path}, $body, - 'http://www.onvif.org/ver20/ptz/wsdl/ContinuousMove'); + 'http://www.onvif.org/ver20/ptz/wsdl/Stop'); } # ========================================================================= diff --git a/scripts/ZoneMinder/lib/ZoneMinder/Control/Reolink.pm b/scripts/ZoneMinder/lib/ZoneMinder/Control/Reolink.pm index e6a1686497..6a58995c49 100644 --- a/scripts/ZoneMinder/lib/ZoneMinder/Control/Reolink.pm +++ b/scripts/ZoneMinder/lib/ZoneMinder/Control/Reolink.pm @@ -206,7 +206,7 @@ sub autoStop $sha->add($nonce.$mydate.$password); my $digest = encode_base64($sha->digest,""); my $msg = ''.$username.''.$digest.''.encode_base64($nonce,"").''.$mydate.'000'.($is_zoom ? 'false' : 'true').''.($is_zoom ? 'true' : 'false').''; - my $content_type = 'application/soap+xml; charset=utf-8; action="http://www.onvif.org/ver20/ptz/wsdl/ContinuousMove"'; + my $content_type = 'application/soap+xml; charset=utf-8; action="http://www.onvif.org/ver20/ptz/wsdl/Stop"'; usleep( $autostop ); $self->sendCmd( $cmd, $msg, $content_type ); } @@ -426,7 +426,7 @@ sub moveStop $sha->add($nonce.$mydate.$password); my $digest = encode_base64($sha->digest,""); my $msg =''.$username.''.$digest.''.encode_base64($nonce,"").''.$mydate.'000truefalse'; - my $content_type = 'application/soap+xml; charset=utf-8; action="http://www.onvif.org/ver20/ptz/wsdl/ContinuousMove"'; + my $content_type = 'application/soap+xml; charset=utf-8; action="http://www.onvif.org/ver20/ptz/wsdl/Stop"'; $self->sendCmd( $cmd, $msg, $content_type ); } diff --git a/scripts/ZoneMinder/lib/ZoneMinder/Control/TapoC520WS_ONVIF.pm b/scripts/ZoneMinder/lib/ZoneMinder/Control/TapoC520WS_ONVIF.pm index 514ebf719b..451939e217 100644 --- a/scripts/ZoneMinder/lib/ZoneMinder/Control/TapoC520WS_ONVIF.pm +++ b/scripts/ZoneMinder/lib/ZoneMinder/Control/TapoC520WS_ONVIF.pm @@ -332,7 +332,7 @@ sub autoStop { '; } - my $content_type = 'application/soap+xml; charset=utf-8; action="http://www.onvif.org/ver20/ptz/wsdl/ContinuousMove"'; + my $content_type = 'application/soap+xml; charset=utf-8; action="http://www.onvif.org/ver20/ptz/wsdl/Stop"'; usleep($duration); $self->sendCmd($cmd, $msg_body, $content_type); } @@ -386,7 +386,7 @@ sub moveMap { '; - my $content_type = 'application/soap+xml; charset=utf-8; action="http://www.onvif.org/ver20/ptz/wsdl/ContinuousMove"'; + my $content_type = 'application/soap+xml; charset=utf-8; action="http://www.onvif.org/ver20/ptz/wsdl/AbsoluteMove"'; $self->sendCmd($cmd, $msg_body, $content_type); } @@ -409,7 +409,7 @@ sub moveRel { '; - my $content_type = 'application/soap+xml; charset=utf-8; action="http://www.onvif.org/ver20/ptz/wsdl/ContinuousMove"'; + my $content_type = 'application/soap+xml; charset=utf-8; action="http://www.onvif.org/ver20/ptz/wsdl/RelativeMove"'; $self->sendCmd($cmd, $msg_body, $content_type); } @@ -641,7 +641,7 @@ sub presetHome { ' . $profileToken . ' '; - my $content_type = 'application/soap+xml; charset=utf-8; action="http://www.onvif.org/ver20/ptz/wsdl/GotoPreset"'; + my $content_type = 'application/soap+xml; charset=utf-8; action="http://www.onvif.org/ver20/ptz/wsdl/GotoHomePosition"'; $self->sendCmd( $cmd, $msg_body, $content_type ); } diff --git a/scripts/ZoneMinder/t/ptz_soapaction.t b/scripts/ZoneMinder/t/ptz_soapaction.t new file mode 100644 index 0000000000..30dec09914 --- /dev/null +++ b/scripts/ZoneMinder/t/ptz_soapaction.t @@ -0,0 +1,102 @@ +#!/usr/bin/perl +# +# Every ONVIF SOAP request must name the operation it is performing in the +# SOAP action URI: has to be +# sent with action ".../ptz/wsdl/Stop". Cameras that check the action reject +# the request otherwise, which is how "the camera never stops" happens. +# +# The Control modules cannot be loaded here (their dependencies, e.g. DateTime, +# are not installed in the test environment), so this test reads their source +# as text instead: each file is split into subs, and inside a sub each action +# URI (.../wsdl/, excluding the body's own xmlns declaration) is paired +# with the operation of the closest preceding / element. + +use strict; +use warnings; +use File::Basename qw(basename dirname); +use File::Spec; +use Test::More; + +my $here = dirname(File::Spec->rel2abs(__FILE__)); +my $lib = $ENV{ZM_CONTROL_LIB} + || File::Spec->catdir($here, File::Spec->updir, 'lib', 'ZoneMinder', 'Control'); + +BAIL_OUT("no Control library at $lib") unless -d $lib; + +my @checks; # [ module, sub, op, action ] + +for my $file (sort glob(File::Spec->catfile($lib, '*.pm'))) { + open my $fh, '<', $file or BAIL_OUT("cannot read $file: $!"); + local $/; + my $src = <$fh>; + close $fh; + + (my $module = basename($file)) =~ s/\.pm$//; + + # Locate every sub and the extent of its body. + my @starts; + while ($src =~ /^sub\s+(\w+)/mg) { + push @starts, [ $1, $-[0] ]; + } + + for my $i (0 .. $#starts) { + my ($name, $start) = @{ $starts[$i] }; + my $end = ($i < $#starts) ? $starts[$i + 1][1] : length($src); + my $body = substr($src, $start, $end - $start); + + # Operation elements, with their offset in the sub. + my @ops; + while ($body =~ /<(?:\w+:)?Body\b[^>]*>\s*<([\w:.-]+)/g) { + (my $op = $1) =~ s/^.*://; + push @ops, [ $-[0], $op ]; + } + next unless @ops; + + # Action URIs, skipping the namespace declarations inside the bodies. + my @actions; + while ($body =~ /(http:\/\/www\.onvif\.org\/ver\d+\/\w+\/wsdl\/(\w+))/g) { + my ($pos, $action) = ($-[1], $2); + my $before = substr($body, 0, $pos); + next if $before =~ /xmlns="$/; + push @actions, [ $pos, $action ]; + } + next unless @actions; + + # A sub may send more than one request (autoStop sends a ContinuousMove + # and then a Stop). When the counts line up, pair them in source order -- + # that also covers modules that declare the action before the body. If + # they do not line up, fall back to the nearest operation for each action. + my @pairs; + if (@ops == @actions) { + @pairs = map { [ $ops[$_][1], $actions[$_][1] ] } 0 .. $#ops; + } else { + my %used; + for my $a (@actions) { + my ($pos, $action) = @$a; + my ($best) = sort { abs($a->[0] - $pos) <=> abs($b->[0] - $pos) } + grep { !$used{$_->[0]} } @ops; + next unless $best; + $used{ $best->[0] } = 1; + push @pairs, [ $best->[1], $action ]; + } + } + + push @checks, map { [ $module, $name, $_->[0], $_->[1] ] } @pairs; + } +} + +BAIL_OUT("no SOAP requests found under $lib") unless @checks; + +diag("checking $lib"); +diag(sprintf('%-24s %-20s %-22s %s', 'module', 'sub', 'body operation', 'action')); +for my $c (@checks) { + diag(sprintf('%-24s %-20s %-22s %s', @$c)); +} + +plan tests => scalar(@checks); + +for my $c (@checks) { + my ($module, $name, $op, $action) = @$c; + ok($action eq $op, "$module\::$name sends <$op> with action <$op>") + or diag("$module\::$name body is <$op> but action is <$action>"); +}