mirror of
https://github.com/ZoneMinder/zoneminder.git
synced 2026-11-30 04:04:03 -05:00
407 lines
14 KiB
Plaintext
407 lines
14 KiB
Plaintext
# ==========================================================================
|
|
#
|
|
# ZoneMinder Config Module, $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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
#
|
|
# ==========================================================================
|
|
#
|
|
# This module contains the common definitions and functions used by the rest
|
|
# of the ZoneMinder scripts
|
|
#
|
|
package ZoneMinder::Config;
|
|
|
|
use 5.006;
|
|
use strict;
|
|
use warnings;
|
|
|
|
require Exporter;
|
|
require ZoneMinder::Base;
|
|
|
|
our @ISA = qw(Exporter ZoneMinder::Base);
|
|
|
|
use vars qw( %Config );
|
|
# 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_CONFIG = qw( %Config ); # Get populated by BEGIN
|
|
|
|
our %EXPORT_TAGS = (
|
|
functions => [ qw(
|
|
zmConfigLoad
|
|
loadConfigFromDB
|
|
saveConfigToDB
|
|
syncConfigFlags
|
|
) ],
|
|
constants => [ qw(
|
|
ZM_PID
|
|
) ]
|
|
);
|
|
push( @{$EXPORT_TAGS{config}}, @EXPORT_CONFIG );
|
|
push( @{$EXPORT_TAGS{all}}, @{$EXPORT_TAGS{$_}} ) foreach keys %EXPORT_TAGS;
|
|
|
|
our @EXPORT_OK = @{ $EXPORT_TAGS{all} };
|
|
|
|
our @EXPORT = qw();
|
|
|
|
our $VERSION = $ZoneMinder::Base::VERSION;
|
|
|
|
use constant ZM_PID => '@ZM_PID@'; # Path to the ZoneMinder run pid file
|
|
use constant ZM_CONFIG => '@ZM_CONFIG@'; # Path to the ZoneMinder config file
|
|
use constant ZM_CONFIG_SUBDIR => '@ZM_CONFIG_SUBDIR@'; # Path to the ZoneMinder config subfolder
|
|
|
|
use Carp;
|
|
require ZoneMinder::Database;
|
|
|
|
# Load the config from the database into the symbol table
|
|
BEGIN {
|
|
require ZoneMinder::Database;
|
|
|
|
# Process name, value pairs from the main config file first
|
|
process_configfile(ZM_CONFIG);
|
|
|
|
# Search for user created config files. If one or more are found then
|
|
# update the Config hash with those values
|
|
if ( ZM_CONFIG_SUBDIR and -d ZM_CONFIG_SUBDIR ) {
|
|
if ( -R ZM_CONFIG_SUBDIR ) {
|
|
foreach my $filename (glob ZM_CONFIG_SUBDIR.'/*.conf' ) {
|
|
process_configfile($filename);
|
|
}
|
|
} else {
|
|
print(STDERR 'WARNING: ZoneMinder configuration subfolder found but is not readable. Check folder permissions on '.ZM_CONFIG_SUBDIR.".\n");
|
|
}
|
|
}
|
|
|
|
my $dbh = ZoneMinder::Database::zmDbConnect();
|
|
die "Unable to connect to DB. ZM Cannot continue.\n" if !$dbh;
|
|
|
|
my $sql = 'SELECT Name,Value,Type FROM Config';
|
|
my $sth = $dbh->prepare_cached($sql) or croak("Can't prepare '$sql': ".$dbh->errstr());
|
|
my $res = $sth->execute() or croak("Can't execute: ".$sth->errstr());
|
|
while ( my $config = $sth->fetchrow_hashref() ) {
|
|
# A value from zm.conf or conf.d is set by the owner of the box, so it wins over the
|
|
# Config table. A boolean may be written yes/no, true/false or on/off there; same
|
|
# rule as ConfigFileValue() in src/zm_config.cpp and configFileValue() in PHP.
|
|
if ( exists $Config{$config->{Name}} ) {
|
|
if ( $config->{Type} eq 'boolean' ) {
|
|
my $word = lc($Config{$config->{Name}});
|
|
$word =~ s/^\s+|\s+$//g;
|
|
if ( $word eq 'yes' or $word eq 'true' or $word eq 'on' ) {
|
|
$Config{$config->{Name}} = 1;
|
|
} elsif ( $word eq 'no' or $word eq 'false' or $word eq 'off' ) {
|
|
$Config{$config->{Name}} = 0;
|
|
}
|
|
}
|
|
next;
|
|
}
|
|
$Config{$config->{Name}} = $config->{Value};
|
|
}
|
|
$sth->finish();
|
|
|
|
if ( !$Config{ZM_SERVER_ID} ) {
|
|
$Config{ZM_SERVER_ID} = undef;
|
|
$sth = $dbh->prepare_cached('SELECT * FROM Servers WHERE Name=?');
|
|
if ( $Config{ZM_SERVER_NAME} ) {
|
|
$res = $sth->execute($Config{ZM_SERVER_NAME});
|
|
my $result = $sth->fetchrow_hashref();
|
|
$Config{ZM_SERVER_ID} = $$result{Id};
|
|
} elsif ( $Config{ZM_SERVER_HOST} ) {
|
|
$res = $sth->execute($Config{ZM_SERVER_HOST});
|
|
my $result = $sth->fetchrow_hashref();
|
|
$Config{ZM_SERVER_ID} = $$result{Id};
|
|
}
|
|
$sth->finish();
|
|
}
|
|
|
|
# This subroutine must be inside the BEGIN block
|
|
sub process_configfile {
|
|
my $config_file = shift;
|
|
|
|
if ( ! -R $config_file ) {
|
|
print(STDERR "WARNING: ZoneMinder configuration file found but is not readable. Check file permissions on $config_file\n");
|
|
return;
|
|
}
|
|
open( my $CONFIG, '<', $config_file )
|
|
or croak("Can't open config file '$config_file': $!");
|
|
my $logical = '';
|
|
while ( my $str = <$CONFIG> ) {
|
|
$str =~ s/\r?\n\z//;
|
|
# A trailing backslash (with optional whitespace before the newline)
|
|
# means the value continues on the next physical line. Continuation
|
|
# lines have their leading whitespace stripped so users can indent.
|
|
if ( $logical ne '' ) {
|
|
$str =~ s/^[ \t]+//;
|
|
}
|
|
if ( $str =~ s/\\[ \t]*\z// ) {
|
|
$logical .= $str;
|
|
next;
|
|
}
|
|
$logical .= $str;
|
|
my $line = $logical;
|
|
$logical = '';
|
|
next if ( $line =~ /^\s*$/ );
|
|
next if ( $line =~ /^\s*#/ );
|
|
my ( $name, $value ) = $line =~ /^\s*([^=\s]+)\s*=\s*[\'"]*(.*?)[\'"]*\s*$/;
|
|
if ( !$name ) {
|
|
print(STDERR "Warning, bad line in $config_file: $line\n");
|
|
next;
|
|
} # end if
|
|
$name = uc $name;
|
|
#if ( !$ZoneMinder::ConfigData::options_hash{$name} ) {
|
|
#print(STDERR "Warning, unknown config option name $name in $config_file\n");
|
|
#} else {
|
|
#print(STDERR "Warning, known config option name $name in $config_file\n");
|
|
#}
|
|
$Config{$name} = $value;
|
|
} # end while config line
|
|
close($CONFIG);
|
|
} # end sub process_configfile
|
|
|
|
} # end BEGIN
|
|
|
|
# ConfigData holds the definition and help text of every option. Only the functions
|
|
# that work on the Config table itself need it, so it is loaded when they run
|
|
# rather than by every script. Its INIT block does not run when it is required.
|
|
sub loadConfigData {
|
|
require ZoneMinder::ConfigData;
|
|
ZoneMinder::ConfigData::initialiseConfig();
|
|
}
|
|
|
|
sub loadConfigFromDB {
|
|
loadConfigData();
|
|
print('Loading config from DB');
|
|
my $dbh = ZoneMinder::Database::zmDbConnect();
|
|
if ( !$dbh ) {
|
|
print("Error: unable to load options from database: $DBI::errstr\n");
|
|
return(0);
|
|
}
|
|
my $sql = 'SELECT * FROM Config';
|
|
my $sth = $dbh->prepare_cached( $sql )
|
|
or croak( "Can't prepare '$sql': ".$dbh->errstr() );
|
|
my $res = $sth->execute()
|
|
or croak( "Can't execute: ".$sth->errstr() );
|
|
my $option_count = 0;
|
|
while ( my $config = $sth->fetchrow_hashref() ) {
|
|
my ( $name, $value ) = ( $config->{Name}, $config->{Value} );
|
|
my $option = $ZoneMinder::ConfigData::options_hash{$name};
|
|
if ( !$option ) {
|
|
warn( "No option '$name' found, removing.\n" );
|
|
next;
|
|
}
|
|
if ( defined($value) ) {
|
|
if ( $option->{type} == $ZoneMinder::ConfigData::types{boolean} ) {
|
|
# Booleans are 1/0 in the table and in ConfigData; coerce rather than
|
|
# take the column verbatim so an empty or absent value lands on '0'.
|
|
$option->{value} = $value ? '1' : '0';
|
|
} else {
|
|
$option->{value} = $value;
|
|
}
|
|
}
|
|
$option_count++;
|
|
}
|
|
$sth->finish();
|
|
print( " $option_count entries\n" );
|
|
return $option_count;
|
|
} # end sub loadConfigFromDB
|
|
|
|
# ZM_AUTH_HASH_SECRET signs API/login tokens and keys the auth hash and CSRF tokens. The
|
|
# shipped default is public, so replace it, or an empty value, with a random secret the
|
|
# first time the config is saved on this system. refs GHSA-wmcc-x64g-jr84
|
|
sub generateAuthHashSecret {
|
|
loadConfigData();
|
|
my $option = $ZoneMinder::ConfigData::options_hash{ZM_AUTH_HASH_SECRET};
|
|
return if !$option;
|
|
return if defined($option->{value}) and $option->{value} ne ''
|
|
and $option->{value} ne $option->{default};
|
|
|
|
my $bytes;
|
|
if ( open(my $fh, '<:raw', '/dev/urandom') ) {
|
|
read($fh, $bytes, 32);
|
|
close($fh);
|
|
}
|
|
if ( !defined($bytes) or length($bytes) != 32 ) {
|
|
print("Warning: unable to read /dev/urandom, ZM_AUTH_HASH_SECRET is still the default. Please set it.\n");
|
|
return;
|
|
}
|
|
$option->{value} = unpack('H*', $bytes);
|
|
print("Generated a new ZM_AUTH_HASH_SECRET\n");
|
|
}
|
|
|
|
# Set each Config row's Private and System flags from ConfigData, leaving its value
|
|
# alone. The web UI hides and never exports a Private option, and won't edit a
|
|
# System one; a table created from zm_create.sql before it wrote these columns has
|
|
# them all 0, which showed ZM_AUTH_HASH_SECRET to anyone. zmupdate.pl calls this on
|
|
# every update; saveConfigToDB() (zmupdate.pl -f) writes them along with the rest.
|
|
sub syncConfigFlags {
|
|
loadConfigData();
|
|
my $dbh = ZoneMinder::Database::zmDbConnect();
|
|
if ( !$dbh ) {
|
|
print( "Error: unable to update option flags in database: $DBI::errstr\n" );
|
|
return( 0 );
|
|
}
|
|
my $rows = $dbh->selectall_hashref('SELECT Name, Private, `System` FROM Config', 'Name')
|
|
or croak( "Can't read option flags: ".$dbh->errstr() );
|
|
my $sth = $dbh->prepare_cached('UPDATE Config SET Private=?, `System`=? WHERE Name=?')
|
|
or croak( "Can't prepare: ".$dbh->errstr() );
|
|
my $changed = 0;
|
|
foreach my $option ( @ZoneMinder::ConfigData::options ) {
|
|
my $row = $rows->{$option->{name}} or next;
|
|
my ( $private, $system ) = ( $option->{private} ? 1 : 0, $option->{system} ? 1 : 0 );
|
|
next if ( ($row->{Private} // 0) == $private ) and ( ($row->{System} // 0) == $system );
|
|
$sth->execute($private, $system, $option->{name})
|
|
or croak( "Can't update flags of $$option{name}: ".$sth->errstr() );
|
|
$changed++;
|
|
}
|
|
$sth->finish();
|
|
return $changed;
|
|
} # end sub syncConfigFlags
|
|
|
|
sub saveConfigToDB {
|
|
loadConfigData();
|
|
print( 'Saving config to DB ' . @ZoneMinder::ConfigData::options . " entries\n" );
|
|
my $dbh = ZoneMinder::Database::zmDbConnect();
|
|
if ( !$dbh ) {
|
|
print( "Error: unable to save options to database: $DBI::errstr\n" );
|
|
return( 0 );
|
|
}
|
|
|
|
generateAuthHashSecret();
|
|
|
|
my $ac = $dbh->{AutoCommit};
|
|
$dbh->{AutoCommit} = 0;
|
|
|
|
$dbh->do('LOCK TABLE Config WRITE')
|
|
or croak( "Can't lock Config table: " . $dbh->errstr() );
|
|
|
|
my $sql = 'DELETE FROM Config';
|
|
my $res = $dbh->do( $sql )
|
|
or croak( "Can't do '$sql': ".$dbh->errstr() );
|
|
|
|
$sql = 'REPLACE INTO Config SET Id=?, Name=?, Value=?, Type=?, DefaultValue=?, Hint=?, Pattern=?, Format=?, Prompt=?, Help=?, Category=?, Readonly=?, Private=?, `System`=?, Requires=?';
|
|
my $sth = $dbh->prepare_cached( $sql )
|
|
or croak( "Can't prepare '$sql': ".$dbh->errstr() );
|
|
foreach my $option ( @ZoneMinder::ConfigData::options ) {
|
|
#next if ( $option->{category} eq 'hidden' );
|
|
#print( $option->{name}."\n" ) if ( !$option->{category} );
|
|
$option->{db_type} = $option->{type}->{db_type};
|
|
$option->{db_hint} = $option->{type}->{hint};
|
|
$option->{db_pattern} = $option->{type}->{pattern};
|
|
$option->{db_format} = $option->{type}->{format};
|
|
$option->{db_value} = ZoneMinder::ConfigData::dbValue($option->{db_type}, $option->{value});
|
|
$option->{db_default} = ZoneMinder::ConfigData::dbValue($option->{db_type}, $option->{default});
|
|
if ( my $requires = $option->{requires} ) {
|
|
# Requires values are already in stored form, booleans included.
|
|
$option->{db_requires} = join(';', map { "$_->{name}=$_->{value}" } @$requires);
|
|
}
|
|
my $res = $sth->execute(
|
|
$option->{id},
|
|
$option->{name},
|
|
$option->{db_value},
|
|
$option->{db_type},
|
|
$option->{db_default},
|
|
$option->{db_hint},
|
|
$option->{db_pattern},
|
|
$option->{db_format},
|
|
$option->{description},
|
|
$option->{help},
|
|
$option->{category},
|
|
$option->{readonly} ? 1 : 0,
|
|
$option->{private} ? 1 : 0,
|
|
$option->{system} ? 1 : 0,
|
|
$option->{db_requires}
|
|
) or croak("Can't execute when updating config entry $$option{name}: ".$sth->errstr() );
|
|
} # end foreach option
|
|
$sth->finish();
|
|
|
|
$dbh->do('UNLOCK TABLES');
|
|
$dbh->{AutoCommit} = $ac;
|
|
} # end sub saveConfigToDB
|
|
|
|
1;
|
|
__END__
|
|
|
|
=head1 NAME
|
|
|
|
ZoneMinder::Config - ZoneMinder configuration module.
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
use ZoneMinder::Config qw(:all);
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
The ZoneMinder::Config module is used to import the ZoneMinder
|
|
configuration from the database. It will do this at compile time in a BEGIN
|
|
block and require access to the zm.conf file either in the current
|
|
directory or in its defined location in order to determine database access
|
|
details, configuration from this file will also be included. If the :all or
|
|
:config tags are used then this configuration is exported into the
|
|
namespace of the calling program or module.
|
|
|
|
Once the configuration has been imported then configuration variables are
|
|
defined as constants and can be accessed directory by name, e.g.
|
|
|
|
$lang = $Config{ZM_LANG_DEFAULT};
|
|
|
|
=head1 METHODS
|
|
|
|
=over 4
|
|
|
|
=item loadConfigFromDB ();
|
|
|
|
Loads existing configuration from the database (if any) and merges it with
|
|
the definitions held in this module. This results in the merging of any new
|
|
configuration and the removal of any deprecated configuration while
|
|
preserving the existing values of every else.
|
|
|
|
=item saveConfigToDB ();
|
|
|
|
Saves configuration held in memory to the database. The act of loading and
|
|
saving configuration is a convenient way to ensure that the configuration
|
|
held in the database corresponds with the most recent definitions and that
|
|
all components are using the same set of configuration.
|
|
|
|
=back
|
|
|
|
=head2 EXPORT
|
|
|
|
None by default.
|
|
The :constants tag will export the ZM_PID constant which details the location of the zm.pid file
|
|
The :config tag will export all configuration from the database as well as any from the zm.conf file
|
|
The :all tag will export all above symbols.
|
|
|
|
=head1 SEE ALSO
|
|
|
|
http://www.zoneminder.com
|
|
|
|
=head1 AUTHOR
|
|
|
|
Philip Coombes, E<lt>philip.coombes@zoneminder.comE<gt>
|
|
|
|
=head1 COPYRIGHT AND LICENSE
|
|
|
|
Copyright (C) 2001-2008 Philip Coombes
|
|
|
|
Licensed under the GNU General Public License v2 or later; see the COPYING
|
|
file distributed with ZoneMinder for the full text.
|
|
|
|
|
|
=cut
|