This file is indexed.

/usr/bin/zmtrigger.pl is in zoneminder 1.26.5-1ubuntu3.

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

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
#!/usr/bin/perl -wT
#
# ==========================================================================
#
# ZoneMinder External Trigger Script, $Date: 2008-07-25 10:48:16 +0100 (Fri, 25 Jul 2008) $, $Revision: 2612 $
# 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# ==========================================================================
#
# This script is used to trigger and cancel alarms from external connections
# using an arbitrary text based format
#
# ==========================================================================
use strict;
use bytes;

# ==========================================================================
#
# User config
#
# ==========================================================================

use constant MAX_CONNECT_DELAY => 10;
use constant MONITOR_RELOAD_INTERVAL => 300;
use constant SELECT_TIMEOUT => 0.25;

# ==========================================================================
#
# Channel/Connection Modules
#
# ==========================================================================

# Include from system perl paths only
use ZoneMinder;
use ZoneMinder::Trigger::Channel::Inet;
use ZoneMinder::Trigger::Channel::Unix;
use ZoneMinder::Trigger::Channel::Serial;
use ZoneMinder::Trigger::Connection;

my @connections;
push( @connections, ZoneMinder::Trigger::Connection->new( name=>"Chan1", channel=>ZoneMinder::Trigger::Channel::Inet->new( port=>6802 ), mode=>"rw" ) );
push( @connections, ZoneMinder::Trigger::Connection->new( name=>"Chan2", channel=>ZoneMinder::Trigger::Channel::Unix->new( path=>ZM_PATH_SOCKS.'/zmtrigger.sock' ), mode=>"rw" ) );
#push( @connections, ZoneMinder::Trigger::Connection->new( name=>"Chan3", channel=>ZoneMinder::Trigger::Channel::File->new( path=>'/tmp/zmtrigger.out' ), mode=>"w" ) );
push( @connections, ZoneMinder::Trigger::Connection->new( name=>"Chan4", channel=>ZoneMinder::Trigger::Channel::Serial->new( path=>'/dev/ttyS0' ), mode=>"rw" ) );

# ==========================================================================
#
# Don't change anything from here on down
#
# ==========================================================================

use DBI;
#use Socket;
use Data::Dumper;
use POSIX qw( EINTR );
use Time::HiRes qw( usleep );

$| = 1;

$ENV{PATH}  = '/bin:/usr/bin';
$ENV{SHELL} = '/bin/sh' if exists $ENV{SHELL};
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};

logInit();
logSetSignal();

Info( "Trigger daemon starting\n" );

my $dbh = zmDbConnect();

my $base_rin = '';
foreach my $connection ( @connections )
{
	Info( "Opening connection '$connection->{name}'\n" );
	$connection->open();
}

my @in_select_connections = grep { $_->input() && $_->selectable() } @connections;
my @in_poll_connections = grep { $_->input() && !$_->selectable() } @connections;
my @out_connections = grep { $_->output() } @connections;

foreach my $connection ( @in_select_connections )
{
	vec( $base_rin, $connection->fileno(), 1 ) = 1;
}

my %spawned_connections;
my %monitors;

my $monitor_reload_time = 0;

$! = undef;
my $rin = '';
my $win = $rin;
my $ein = $win;
my $timeout = SELECT_TIMEOUT;
my %actions;
while( 1 )
{
	$rin = $base_rin;
	# Add the file descriptors of any spawned connections
	foreach my $fileno ( keys(%spawned_connections) )
	{
		vec( $rin, $fileno, 1 ) = 1;
	}

	my $nfound = select( my $rout = $rin, undef, my $eout = $ein, $timeout );
	if ( $nfound > 0 )
	{
		Debug( "Got input from $nfound connections\n" );
		foreach my $connection ( @in_select_connections )
		{
			if ( vec( $rout, $connection->fileno(), 1 ) )
			{
				Debug( "Got input from connection ".$connection->name()." (".$connection->fileno().")\n" );
				if ( $connection->spawns() )
				{
					my $new_connection = $connection->accept();
					$spawned_connections{$new_connection->fileno()} = $new_connection;
					Debug( "Added new spawned connection (".$new_connection->fileno()."), ".int(keys(%spawned_connections))." spawned connections\n" );
				}
				else
				{
					my $messages = $connection->getMessages();
					if ( defined($messages) )
					{
						foreach my $message ( @$messages )
						{
							handleMessage( $connection, $message );
						}
					}
				}
			}
		}
		foreach my $connection ( values(%spawned_connections) )
		{
			if ( vec( $rout, $connection->fileno(), 1 ) )
			{
				Debug( "Got input from spawned connection ".$connection->name()." (".$connection->fileno().")\n" );
				my $messages = $connection->getMessages();
				if ( defined($messages) )
				{
					foreach my $message ( @$messages )
					{
						handleMessage( $connection, $message );
					}
				}
				else
				{
					delete( $spawned_connections{$connection->fileno()} );
					Debug( "Removed spawned connection (".$connection->fileno()."), ".int(keys(%spawned_connections))." spawned connections\n" );
					$connection->close();
				}
			}
		}
	}
	elsif ( $nfound < 0 )
	{
		if ( $! == EINTR )
		{
			# Do nothing
		}
		else
		{
			Fatal( "Can't select: $!" );
		}
	}

	# Check polled connections
	foreach my $connection ( @in_poll_connections )
	{
		my $messages = $connection->getMessages();
		if ( defined($messages) )
		{
			foreach my $message ( @$messages )
			{
				handleMessage( $connection, $message );
			}
		}
	}

	# Check for alarms that might have happened
	my @out_messages;
	foreach my $monitor ( values(%monitors) )
	{
		my ( $state, $last_event ) = zmMemRead( $monitor, [ "shared_data:state", "shared_data:last_event" ] );

		#print( "$monitor->{Id}: S:$state, LE:$last_event\n" );
		#print( "$monitor->{Id}: mS:$monitor->{LastState}, mLE:$monitor->{LastEvent}\n" );
		if ( $state == STATE_ALARM || $state == STATE_ALERT ) # In alarm state
		{
			if ( !defined($monitor->{LastEvent}) || ($last_event != $monitor->{LastEvent}) ) # A new event
			{
				push( @out_messages, $monitor->{Id}."|on|".time()."|".$last_event );
			}
			else # The same one as last time, so ignore it
			{
				# Do nothing
			}
		}
		elsif ( ($state == STATE_IDLE && $monitor->{LastState} != STATE_IDLE) || ($state == STATE_TAPE && $monitor->{LastState} != STATE_TAPE) ) # Out of alarm state
		{
			push( @out_messages, $monitor->{Id}."|off|".time()."|".$last_event );
		}
		elsif ( defined($monitor->{LastEvent}) && ($last_event != $monitor->{LastEvent}) ) # We've missed a whole event
		{
			push( @out_messages, $monitor->{Id}."|on|".time()."|".$last_event );
			push( @out_messages, $monitor->{Id}."|off|".time()."|".$last_event );
		}
		$monitor->{LastState} = $state;
		$monitor->{LastEvent} = $last_event;
	}
	foreach my $connection ( @out_connections )
	{
		if ( $connection->canWrite() )
		{
			$connection->putMessages( \@out_messages );
		}
	}
	foreach my $connection ( values(%spawned_connections) )
	{
		if ( $connection->canWrite() )
		{
			$connection->putMessages( \@out_messages );
		}
	}

	Debug( "Checking for timed actions\n" ) if ( int(keys(%actions)) );
	my $now = time();
	foreach my $action_time ( sort( grep { $_ < $now } keys( %actions ) ) )
	{
		Info( "Found actions expiring at $action_time\n" );
		foreach my $action ( @{$actions{$action_time}} )
		{
			my $connection = $action->{connection};
			my $message = $action->{message};
			Info( "Found action '$message'\n" );
			handleMessage( $connection, $message );
		}
		delete( $actions{$action_time} );
	}

    # Allow connections to do their own timed actions
	foreach my $connection ( @connections )
	{
		my $messages = $connection->timedActions();
		if ( defined($messages) )
		{
			foreach my $message ( @$messages )
			{
				handleMessage( $connection, $message );
			}
		}
	}
	foreach my $connection ( values(%spawned_connections) )
	{
		my $messages = $connection->timedActions();
		if ( defined($messages) )
		{
			foreach my $message ( @$messages )
			{
				handleMessage( $connection, $message );
			}
		}
	}

	# If necessary reload monitors
	if ( (time() - $monitor_reload_time) > MONITOR_RELOAD_INTERVAL )
	{
	    foreach my $monitor ( values(%monitors) )
        {
            # Free up any used memory handle
            zmMemInvalidate( $monitor );
        }
		loadMonitors();
	}
}
Info( "Trigger daemon exiting\n" );
exit;

sub loadMonitors
{
	Debug( "Loading monitors\n" );
	$monitor_reload_time = time();

	my %new_monitors = ();

	my $sql = "select * from Monitors where find_in_set( Function, 'Modect,Mocord,Nodect' )";
	my $sth = $dbh->prepare_cached( $sql ) or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
	my $res = $sth->execute() or Fatal( "Can't execute: ".$sth->errstr() );
	while( my $monitor = $sth->fetchrow_hashref() )
	{
		next if ( !zmMemVerify( $monitor ) ); # Check shared memory ok

		if ( defined($monitors{$monitor->{Id}}->{LastState}) )
		{
			$monitor->{LastState} = $monitors{$monitor->{Id}}->{LastState};
		}
		else
		{
			$monitor->{LastState} = zmGetMonitorState( $monitor );
		}
		if ( defined($monitors{$monitor->{Id}}->{LastEvent}) )
		{
			$monitor->{LastEvent} = $monitors{$monitor->{Id}}->{LastEvent};
		}
		else
		{
			$monitor->{LastEvent} = zmGetLastEvent( $monitor );
		}
		$new_monitors{$monitor->{Id}} = $monitor;
	}
	%monitors = %new_monitors;
}

sub handleMessage
{
	my $connection = shift;
	my $message = shift;

	my ( $id, $action, $score, $cause, $text, $showtext ) = split( /\|/, $message );
	$score = 0 if ( !defined($score) );
	$cause = "" if ( !defined($cause) );
	$text = "" if ( !defined($text) );

	my $monitor = $monitors{$id};
	if ( !$monitor )
	{
		Warning( "Can't find monitor '$id' for message '$message'\n" );
		return;
	}
	Debug( "Found monitor for id '$id'\n" );

	next if ( !zmMemVerify( $monitor ) );

	Debug( "Handling action '$action'\n" );
	if ( $action =~ /^(enable|disable)(?:\+(\d+))?$/ )
	{
		my $state = $1;
		my $delay = $2;
		if ( $state eq "enable" )
		{
			zmMonitorEnable( $monitor );
		}
		else
		{
			zmMonitorDisable( $monitor );
		}
		# Force a reload
		$monitor_reload_time = 0;
		Info( "Set monitor to $state\n" );
		if ( $delay )
		{
			my $action_time = time()+$delay;
			my $action_text = $id."|".(($state eq "enable")?"disable":"enable");
			my $action_array = $actions{$action_time};
			if ( !$action_array )
			{
				$action_array = $actions{$action_time} = [];
			}
			push( @$action_array, { connection=>$connection, message=>$action_text } );
			Debug( "Added timed event '$action_text', expires at $action_time (+$delay secs)\n" );
		}
	}
	elsif ( $action =~ /^(on|off)(?:\+(\d+))?$/ )
	{
		next if ( !$monitor->{Enabled} );

		my $trigger = $1;
		my $delay = $2;
		my $trigger_data;
		if ( $trigger eq "on" )
		{
			zmTriggerEventOn( $monitor, $score, $cause, $text );
		    zmTriggerShowtext( $monitor, $showtext ) if defined($showtext);
		    Info( "Trigger '$trigger' '$cause'\n" );
		}
		elsif ( $trigger eq "off" )
		{
            my $last_event = zmGetLastEvent( $monitor );
			zmTriggerEventOff( $monitor );
		    zmTriggerShowtext( $monitor, $showtext ) if defined($showtext);
		    Info( "Trigger '$trigger'\n" );
            # Wait til it's finished
            while( zmInAlarm( $monitor ) && ($last_event == zmGetLastEvent( $monitor )) )
            {
                # Tenth of a second
                usleep( 100000 );
            }
			zmTriggerEventCancel( $monitor );
		}
		else
		{
		    Info( "Trigger '$trigger'\n" );
			zmTriggerEventCancel( $monitor );
		}
		if ( $delay )
		{
			my $action_time = time()+$delay;
			#my $action_text = $id."|cancel|0|".$cause."|".$text;
			my $action_text = $id."|cancel";
			my $action_array = $actions{$action_time};
			if ( !$action_array )
			{
				$action_array = $actions{$action_time} = [];
			}
			push( @$action_array, { connection=>$connection, message=>$action_text } );
			Debug( "Added timed event '$action_text', expires at $action_time (+$delay secs)\n" );
		}
	}
	elsif( $action eq "cancel" )
	{
		zmTriggerEventCancel( $monitor );
		zmTriggerShowtext( $monitor, $showtext ) if defined($showtext);
		Info( "Cancelled event\n" );
	}
	elsif( $action eq "show" )
	{
		zmTriggerShowtext( $monitor, $showtext );
		Info( "Updated show text to '$showtext'\n" );
	}
	else
	{
		Error( "Unrecognised action '$action' in message '$message'\n" );
	}
}