This file is indexed.

/usr/bin/zmpkg.pl is in zoneminder 1.29.0+dfsg-1ubuntu2.

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
#!/usr/bin/perl -wT
#
# ==========================================================================
#
# ZoneMinder Package Control Script, $Date$, $Revision$
# 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.
#
# ==========================================================================

=head1 NAME

zmpkg.pl - ZoneMinder Package Control Script

=head1 SYNOPSIS

 zmpkg.pl {start|stop|restart|status|logrot|'state'|version}

=head1 DESCRIPTION

This script is used to start and stop the ZoneMinder package primarily to
allow command line control for automatic restart on reboot (see zm script)

=cut
use strict;
use bytes;

# ==========================================================================
#
# Don't change anything below here
#
# ==========================================================================

# Include from system perl paths only
use ZoneMinder;
use DBI;
use POSIX;
use Time::HiRes qw/gettimeofday/;
use autouse 'Pod::Usage'=>qw(pod2usage);

# Detaint our environment
$ENV{PATH}  = '/bin:/usr/bin';
$ENV{SHELL} = '/bin/sh' if exists $ENV{SHELL};
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
my $store_state=""; # PP - will remember state name passed

logInit();

my $command = $ARGV[0]||'';
if ( $command eq 'version' ) {
    print ZoneMinder::Base::ZM_VERSION . "\n";
    exit(0);
}

my $state;

my $dbh;

if ( !$command || $command !~ /^(?:start|stop|restart|status|logrot|version)$/ )
{
    if ( $command )
    {
        $dbh = zmDbConnect();
        # Check to see if it's a valid run state
        my $sql = 'select * from States where Name = ?';
        my $sth = $dbh->prepare_cached( $sql )
            or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
        my $res = $sth->execute( $command )
            or Fatal( "Can't execute: ".$sth->errstr() );
        if ( $state = $sth->fetchrow_hashref() )
        {
            $state->{Name} = $command;
            $state->{Definitions} = [];
            foreach( split( /,/, $state->{Definition} ) )
            {
                my ( $id, $function, $enabled ) = split( /:/, $_ );
                push( @{$state->{Definitions}},
                      { Id=>$id, Function=>$function, Enabled=>$enabled }
                );
            }
 	    $store_state=$command; # PP - Remember the name that was passed to search in DB
            $command = 'state';
        }
        else
        {
            $command = undef;
        }
    }
    if ( !$command )
    {
        pod2usage(-exitstatus => -1);
    }
}
$dbh = zmDbConnect() if ! $dbh;
# PP - Sane state check
isActiveSanityCheck();

# Move to the right place
chdir( $Config{ZM_PATH_WEB} )
    or Fatal( "Can't chdir to '".$Config{ZM_PATH_WEB}."': $!" );

my $dbg_id = "";

Info( "Command: $command\n" );

my $retval = 0;

if ( $command eq "state" )
{
    Info( "Updating DB: $state->{Name}\n" );
    my $sql = $Config{ZM_SERVER_ID} ? 'SELECT * FROM Monitors WHERE ServerId=? ORDER BY Id ASC' : 'SELECT * FROM Monitors ORDER BY Id ASC';
    my $sth = $dbh->prepare_cached( $sql )
        or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
    my $res = $sth->execute( $Config{ZM_SERVER_ID} ? $Config{ZM_SERVER_ID}: () )
        or Fatal( "Can't execute: ".$sth->errstr() );
    while( my $monitor = $sth->fetchrow_hashref() )
    {
        foreach my $definition ( @{$state->{Definitions}} )
        {
            if ( $monitor->{Id} =~ /^$definition->{Id}$/ )
            {
                $monitor->{NewFunction} = $definition->{Function};
                $monitor->{NewEnabled} = $definition->{Enabled};
            }
        }
        #next if ( !$monitor->{NewFunction} );
        $monitor->{NewFunction} = 'None'
            if ( !$monitor->{NewFunction} );
        $monitor->{NewEnabled} = 0
            if ( !$monitor->{NewEnabled} );
        if ( $monitor->{Function} ne $monitor->{NewFunction}
             || $monitor->{Enabled} ne $monitor->{NewEnabled}
        )
        {
            my $sql = "update Monitors set Function = ?, Enabled = ? where Id = ?";
            my $sth = $dbh->prepare_cached( $sql )
                or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
            my $res = $sth->execute( $monitor->{NewFunction}, $monitor->{NewEnabled}, $monitor->{Id} )
                or Fatal( "Can't execute: ".$sth->errstr() );
        }
    }
    $sth->finish();
      
    # PP - Now mark a specific state as active
    resetStates();
    Info ("Marking $store_state as Enabled");
    $sql = "update States set IsActive = '1' where Name = ?";
    $sth = $dbh->prepare_cached( $sql )
         or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
    $res = $sth->execute( $store_state )
         or Fatal( "Can't execute: ".$sth->errstr() );

    # PP - zero out other states isActive
    $command = "restart";
}

# Check if we are running systemd and if we have been called by the system
if ( $command =~ /^(start|stop|restart)$/ )
{
    # We have to detaint to keep perl from complaining
    $command = $1;


    if ( systemdRunning() && !calledBysystem() ) {
        qx(/usr/bin/zmsystemctl.pl $command);
        $command = "";
    }
}

if ( $command =~ /^(?:stop|restart)$/ )
{
    my $status = runCommand( "zmdc.pl check" );

    if ( $status eq "running" )
    {
        runCommand( "zmdc.pl shutdown" );
        zmMemTidy();
    }
    else
    {
        $retval = 1;
    }
}

#runCommand( "zmupdate.pl -f" );

if ( $command =~ /^(?:start|restart)$/ )
{
    my $status = runCommand( "zmdc.pl check" );

    if ( $status eq "stopped" )
    {
        if ( $Config{ZM_DYN_DB_VERSION}
             and ( $Config{ZM_DYN_DB_VERSION} ne ZM_VERSION )
        )
        {
            Fatal( "Version mismatch, system is version ".ZM_VERSION
                   .", database is ".$Config{ZM_DYN_DB_VERSION}
                   .", please run zmupdate.pl to update."
            );
            exit( -1 );
        }

        # Recreate the temporary directory if it's been wiped
        verifyFolder("/tmp/zm");

        # Recreate the run directory if it's been wiped
        verifyFolder("/var/run/zm");

        # Recreate the sock directory if it's been wiped
        verifyFolder("/var/run/zm");

        zmMemTidy();
        runCommand( "zmdc.pl startup" );

		Info( "Starting up services" . ( $Config{ZM_SERVER_ID} ? " for server $Config{ZM_SERVER_ID}\n" : "\n" ) );
        my $sql = $Config{ZM_SERVER_ID} ? 'SELECT * FROM Monitors WHERE ServerId=?' : 'SELECT * FROM Monitors';
        my $sth = $dbh->prepare_cached( $sql )
            or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
        my $res = $sth->execute( $Config{ZM_SERVER_ID} ? $Config{ZM_SERVER_ID} : () )
            or Fatal( "Can't execute: ".$sth->errstr() );
        while( my $monitor = $sth->fetchrow_hashref() )
        {
            if ( $monitor->{Function} ne 'None' )
            {
                if ( $monitor->{Type} eq 'Local' )
                {
                    runCommand( "zmdc.pl start zmc -d $monitor->{Device}" );
                }
                else
                {
                    runCommand( "zmdc.pl start zmc -m $monitor->{Id}" );
                }
                if ( $monitor->{Function} ne 'Monitor' )
                {
                    if ( $Config{ZM_OPT_FRAME_SERVER} )
                    {
                        runCommand( "zmdc.pl start zmf -m $monitor->{Id}" );
                    }
                    runCommand( "zmdc.pl start zma -m $monitor->{Id}" );
                }
                if ( $Config{ZM_OPT_CONTROL} )
                {
                    if ( $monitor->{Function} eq 'Modect' || $monitor->{Function} eq 'Mocord' )
                    {
                        if ( $monitor->{Controllable} && $monitor->{TrackMotion} )
                        {
                            runCommand( "zmdc.pl start zmtrack.pl -m $monitor->{Id}" );
                        }
                    }
                }
            }
        }
        $sth->finish();

        # This is now started unconditionally
        runCommand( "zmdc.pl start zmfilter.pl" );
        if ( $Config{ZM_RUN_AUDIT} )
        {
            runCommand( "zmdc.pl start zmaudit.pl -c" );
        }
        if ( $Config{ZM_OPT_TRIGGERS} )
        {
            runCommand( "zmdc.pl start zmtrigger.pl" );
        }
        if ( $Config{ZM_OPT_X10} )
        {
            runCommand( "zmdc.pl start zmx10.pl -c start" );
        }
        runCommand( "zmdc.pl start zmwatch.pl" );
        if ( $Config{ZM_CHECK_FOR_UPDATES} )
        {
            runCommand( "zmdc.pl start zmupdate.pl -c" );
        }
    }
    else
    {
        $retval = 1;
    }
}

if ( $command eq "status" )
{
    my $status = runCommand( "zmdc.pl check" );

    print( STDOUT $status."\n" );
}

if ( $command eq "logrot" )
{
    runCommand( "zmdc.pl logrot" );
}

exit( $retval );

# PP - Make sure isActive is on and only one
sub isActiveSanityCheck
{

	Info ("Sanity checking States table...");
	$dbh = zmDbConnect() if ! $dbh;
	
	# PP - First, make sure default exists and there is only one
	my $sql = "select Name from States where Name = 'default'";
        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() );

	if ($sth->rows != 1) # PP - no row, or too many rows. Either case is an error
	{
		Info( "Fixing States table - either no default state or duplicate default states" );
		$sql = "delete from States where Name = 'default'";
       		$sth = $dbh->prepare_cached( $sql )
             		or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
                $res = $sth->execute()
               		or Fatal( "Can't execute: ".$sth->errstr() );
		$sql = "insert into States (Name,Definition,IsActive) VALUES ('default','','1');";
       		$sth = $dbh->prepare_cached( $sql )
               		or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
                $res = $sth->execute()
                	or Fatal( "Can't execute: ".$sth->errstr() );
	}	


	# PP - Now make sure no two states have IsActive=1
        $sql = "select Name from States where IsActive = '1'";
        $sth = $dbh->prepare_cached( $sql )
                or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
        $res = $sth->execute()
                or Fatal( "Can't execute: ".$sth->errstr() );

        if ( $sth->rows != 1 )
	{
		Info( "Fixing States table so only one run state is active" );
		resetStates();
		$sql = "update States set IsActive='1' WHERE Name='default'";
        	$sth = $dbh->prepare_cached( $sql )
        	        or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
                $res = $sth->execute()
               		or Fatal( "Can't execute: ".$sth->errstr() );


	}
}


# PP - zeroes out isActive for all states
sub resetStates
{
        $dbh = zmDbConnect() if ! $dbh;
        my $sql = "update States set IsActive = '0'";
        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() );

}

sub systemdRunning
{
    my $result = 0;

    my $output = qx(ps -o comm="" -p 1);
    chomp( $output );

    if ($output =~ /systemd/) {
        $result = 1;
    }

    return $result;
}

sub calledBysystem
{
    my $result = 0;
    my $ppid = getppid();

    my $output = qx(ps -o comm="" -p $ppid);
    chomp( $output );

    if ($output =~ /^(?:systemd|init)$/) {
        $result = 1;
    }

    return $result;
}

sub verifyFolder
{
    my $folder = shift;

        # Recreate the temporary directory if it's been wiped
        if ( !-e $folder )
        {
            Debug( "Recreating directory '$folder'" );
            mkdir( "$folder", 0774 )
                or Fatal( "Can't create missing temporary directory '$folder': $!" );
            my ( $runName ) = getpwuid( $> );
            if ( $runName ne $Config{ZM_WEB_USER} )
            {
                # Not running as web user, so should be root in which case
                # chown the directory
                my ( $webName, $webPass, $webUid, $webGid ) = getpwnam( $Config{ZM_WEB_USER} )
                    or Fatal( "Can't get user details for web user '"
                              .$Config{ZM_WEB_USER}."': $!"
                       );
                chown( $webUid, $webGid, "$folder" )
                    or Fatal( "Can't change ownership of directory '$folder' to '"
                              .$Config{ZM_WEB_USER}.":".$Config{ZM_WEB_GROUP}."': $!"
                       );
            }
        }
}
__END__