/usr/share/perl5/Sendpage/KeesConf.pm is in sendpage-common 1.0.3-1.
This file is owned by root:root, with mode 0o644.
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 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 | #
# KeesConf.pm implements a quick-and-dirty configfile parser
#
# $Id: KeesConf.pm 316 2008-01-03 20:21:19Z keescook $
#
# Copyright (C) 2000-2004 Kees Cook
# kees@outflux.net, http://outflux.net/
#
# 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.
# http://www.gnu.org/copyleft/gpl.html
package Sendpage::KeesConf;
use Carp;
=head1 NAME
Sendpage::KeesConf - implements a configuration file reader
=head1 SYNOPSIS
use Sendpage::KeesConf;
$config = Sendpage::KeesConf->new();
$config->define("variable", { DEFAULT => "setting" });
$config->file("config.cfg");
$setting=$config->get("variable");
=head1 DESCRIPTION
I have borrowed VERY heavily from Andy Wardley's (abw@cre.canon.co.uk)
C<AppConfig> tool, which can be found on CPAN (http://cpan.perl.org)
but I found it not dynamic enough for multi-instance variable defaults.
As a result, I wrote this massively trimmed-down version for my use.
The following methods are available:
=over 4
=cut
# off-limits chars in section names are : @ =
#
# -Kees
# argument count types
$ARGCOUNT_NONE = 0;
$ARGCOUNT_ONE = 1;
$ARGCOUNT_LIST = 2;
#$ARGCOUNT_HASH = 3;
=item $config = Sendpage::KeesConf->new();
The constructor doesn't take an arguement, but it should in the future.
=cut
sub new {
my $proto = shift;
my $class = ref($proto) || $proto;
my $self = {};
# get our args
my $config = shift;
$self->{DEFAULTS} = undef;
$self->{KNOWN} = undef;
bless($self,$class);
return $self;
}
=item $config->forget();
This call will make $config forget about any variables it has loaded.
It does NOT forget C<define>d variables, just instantiated ones via C<file>.
=cut
# forget all configurations
sub dump {
my $self = shift;
$self->{KNOWN} = undef;
$self->{SECTIONS} = undef;
}
=item $config->define($name, $options);
This will define a variable by the name of $name.
$options can contain:
=over 4
=item ARGCOUNT
What type of variable this should be. Default value is "1". The available
types are:
=over 4
=item 0
Boolean (true/false, yes/no, 1/0)
=item 1
Scalar (any string)
=item 2
List (an array of strings)
=back
=item DEFAULT
The default value the variable should have if it is not overridden during
the call to C<file>. The DEFAULT must be the same data type as ARGCOUNT.
The default DEFAULT is the string "<unset>".
=item UNSET
set this to 1 if you want the default value to be undefined. This is
a hack to get around the default DEFAULT.
=back
=cut
# define a variable
sub define {
my $self = shift;
my ($name, $vars)=@_;
my $default;
$self->{DEFAULTS}->{$name}->{ARGCOUNT}=defined($vars->{ARGCOUNT}) ?
$vars->{ARGCOUNT} : $ARGCOUNT_ONE;
if ($self->{DEFAULTS}->{$name}->{ARGCOUNT} == $ARGCOUNT_LIST) {
$self->{DEFAULTS}->{$name}->{DEFAULT}= defined($vars->{DEFAULT}) ?
$vars->{DEFAULT} : undef ;
}
else {
$self->{DEFAULTS}->{$name}->{DEFAULT}= defined($vars->{DEFAULT}) ?
$vars->{DEFAULT} : "<unset>";
}
undef $self->{DEFAULTS}->{$name}->{DEFAULT}
if (defined($vars->{UNSET}));
# warn "'$name' defined with '".$self->{DEFAULTS}->{$name}->{ARGCOUNT}.
# "' and '".$self->{DEFAULTS}->{$name}->{DEFAULT}."'\n";
}
=item $config->instance_exists($name);
This tests to see if there is a section loaded named $name
=cut
# check to see if a section exists in the KNOWN space
sub instance_exists {
my ($self,$name)=@_;
#warn "\tchecking for instance: '$name'\n";
my(%hash, $thing);
foreach $thing (@{ $self->{SECTIONS} }) {
$hash{$thing}=1;
#warn "\t\tI have: '$thing'\n";
}
return defined($hash{$name});
}
=item $var=$config->ifset($name);
This call will search for the variable named $name. If it is not found,
it will return undef. If the value exists, it will return the value.
This is a way to call "get" without having a default passed through.
=cut
sub ifset {
my $self = shift;
my ($whole)=@_;
return ($self->exists($whole) ? $self->get($whole) : undef);
}
=item $var=$config->exists($name);
This call will search for the variable named $name. If it is not found,
it will return false. If the value exists, it will return true. This is
a way for the user to find out if they will get a "default" on a call
to "get".
=cut
# return a variable or default for that variable
sub exists {
my $self = shift;
my ($whole)=@_;
return (defined($self->{KNOWN}->{$whole}));
}
=item $var=$config->fallbackget($name,$quiet);
This call will search for the variable named $name. If it is not found,
the section portion will be removed, and retried for a sectionless "get" call.
That way, global variables can be overridden by section-specific variables.
If "SECTION:Instance@name" does not exist, "name" will be tried.
=cut
sub fallbackget {
my $self = shift;
my ($whole,$quiet)=@_;
my ($class,$instance,$name,$var);
#warn "trying '$whole'...\n";
$var=$self->get($whole,1);
if (!defined($var)) {
($class,$instance,$name)=$self->breakdown($whole);
#warn "now trying '$name'...\n";
$var=$self->get($name,$quiet);
}
return $var;
}
=item $var=$config->get($name);
This call will search for the variable named $name. If it is not found,
it will fall back to the default for the section. Sections are explained
in more detail later.
=cut
# return a variable or default for that variable
sub get {
my $self = shift;
my ($whole,$quiet)=@_;
my ($name,$class,$instance,$var,@parts);
# Vars can be in CLASS:Instance@variable format
# knowns use the entire name,
# defaults use CLASS:variable format
undef $name;
#warn "asking for '$whole'\n";
$value=$self->{KNOWN}->{$whole};
if (!defined($value)) {
# save our original value
$name=$whole;
($class,$instance,$name)=$self->breakdown($name);
# reduce our variable to just class/var
$whole=sprintf("%s$name",$class ? "$class:" : "");
#warn "getting default for '$whole'\n";
my $def=$self->{DEFAULTS}->{$whole};
if (!defined($def) && $class) {
$def=$self->{DEFAULTS}->{"$class:"};
}
if (defined($def)) {
# getting classed default
#warn "found default for '$whole'\n";
$value=$def->{DEFAULT};
}
}
if (!defined($value) && !$quiet) {
croak "'$whole' not defined";
}
return $value;
}
=item $config->instances($class);
Returns an array of the names of all the variables in the class $class.
=cut
sub instances {
my $self = shift;
my($class)=@_;
my @array=sort @{ $self->{SECTIONS} };
grep(s/^${class}://, @array);
}
=item $config->file('program.cfg');
Loads variables from the named file. Syntax for this file is:
[SECTION:INSTANCE]
VARIABLE1 = VALUE1
VARIABLE2 = VALUE2
.
.
.
If VARIABLE is an array, VALUE is loaded using commas (,) as the
list separator. The variable will be available under the name of the
section. For example, to see VALUE2, it would be accessed as:
$config->get("SECTION:INSTANCE\@VARIABLE2");
Notice, that "=", ":", and "@" are all not allowed in section or
variable names.
=cut
# load variables from a file
sub file {
my $self=shift;
my $filename=shift;
my (@lines,@merged,$line);
# for parsing, I prefer this methodology:
# 1) strip all lines starting with a "#"
# 2) join any lines that have a "\" as the last character
# 3) drop any blank lines
# 4) parse, one line at a time
open(FILE,"<$filename") || die "Cannot read '$filename'\n";
@lines=grep(!/^\s*#/,<FILE>); # drop any lines starting with #
close(FILE);
# merge any line with a trailing \
undef @merged;
undef $line;
while ($#lines>=0) {
$line=shift @lines;
chomp($line); # drop crs
while ($line =~ /\\$/ && $#lines>=0) {
$line.=shift @lines;
}
push(@merged,$line);
undef $line;
}
@lines=grep(!/^\s*$/,@merged); # drop any blank lines
my $section="";
foreach $line (@lines) {
#warn "saw line '$line'\n";
my ($token,$value)=split(/=/,$line,2);
# drop any white-space surrounding the token
$token=~s/^\s*//;
$token=~s/\s*$//;
if ($token =~ /^\[([^\]]+)\]/) {
$section=$1;
# drop any white-space surrounding the section
$section=~s/^\s*//;
$section=~s/\s*$//;
# clean up section name (no @s)
$section=~s/\@//g;
#warn "saw section '$section'\n";
if ($self->instance_exists($section)) {
$main::log->do('warning',
"section '$section' already defined -- merging!");
}
else {
push(@{ $self->{SECTIONS} },$section);
}
$section.="\@";
next;
}
# drop any white-space surrounding the value
$value=~s/^\s*//;
$value=~s/\s*$//;
# drop any quotes (not really syntax-smart, ya know?)
$value=~s/^"//;
$value=~s/"$//;
# add our section header
$token="${section}${token}";
#warn "token: '$token' value: '$value'\n";
# now our token/values are "clean". Let's insert them
# into our various structures
#warn "Checking on defaults for '$token'\n";
my ($class,$instance,$name)=$self->breakdown($token);
#warn "got '$class' : '$instance' \@ '$name'\n";
# reduce our variable to just class/var
my $whole=sprintf("%s$name",$class ? "$class:" : "");
#warn "Checking on defaults for '$whole'\n";
my $def=$self->{DEFAULTS}->{$whole};
if (!defined($def) && $class) {
$def=$self->{DEFAULTS}->{"$class:"};
#warn "tried '$class:'\n";
}
if (defined($def)) {
if ($def->{ARGCOUNT} == $ARGCOUNT_NONE) {
if ($value=~/^[ty1]/i) {
$self->{KNOWN}->{$token}=1;
#warn "stored '$token' as '1'\n";
}
elsif ($value =~ /^[fn0]/i) {
$self->{KNOWN}->{$token}=0;
#warn "stored '$token' as '0'\n";
}
else {
$main::log->do('warning',
"value for '$token' not true/false, yes/no, 1/0");
}
}
elsif ($def->{ARGCOUNT} == $ARGCOUNT_ONE) {
$self->{KNOWN}->{$token}=$value;
#warn "stored '$token' as '$value'\n";
}
elsif ($def->{ARGCOUNT} == $ARGCOUNT_LIST) {
#warn "adding to '$token'\n";
my @parts=split(/[\s,]+/,$value);
my $item;
foreach $item (@parts) {
# drop white space
$item=~s/^\s*//;
$item=~s/\s*$//;
#warn "\t'$item'\n";
push(@{$self->{KNOWN}->{$token}},$item);
}
}
else {
$main::log->do('warning',
"default for '$whole' has strange ARGCOUNT");
}
}
else {
$main::log->do('warning',"unknown variable '$token' found in file '$filename'");
}
}
return 1;
}
# "dangerous" hack to set a variable
sub set {
my($self,$var,$value)=@_;
$self->{KNOWN}->{$var}=$value;
}
# breakdown a variable name into class, instance, and variable
#
# input string: "CLASS:INSTANCE@NAME" where "CLASS:" is optional
# and "INSTANCE@" is optional
#
sub breakdown {
my $self=shift;
my ($name)=@_;
my (@parts,$class,$instance);
# strip off the class, if it exists
@parts=split(/:/,$name,2);
$class=$parts[0];
if ($class eq $name) {
undef $class;
}
else {
#warn "class: '$class'\n";
$name=$parts[1];
}
# strip off the instance if it exists
@parts=split(/\@/,$name,2);
$instance=$parts[0];
if ($instance eq $name) {
undef $instance;
}
else {
#warn "instance: '$instance'\n";
$name=$parts[1];
}
return ($class,$instance,$name);
}
1;
__END__
=back
Sections can be defined (and loaded) so that defaults can pass back
to a defined section default. For example, lets say that you have
several modems, and most of them have different settings. You can define
all the modem variables like so:
$config->define("modem:baud",{ DEFAULT => 9600 });
$config->define("modem:flowctl",{ DEFAULT => "hardware" });
Then, when you load them, let's say the config file has:
[modem:sportster]
baud = 115200
[modem:hayes]
The baud rate for the sportster will come back as 115200, but the hayes
will fall back during a C<get> call, and find the default for the modem
section: 9600. Both fallback to have "flowctl" as "hardware":
# returns specific value 115200
$config->get("modem:sportster\@baud");
# returns default value 9600
$config->get("modem:hayes\@baud");
# both return default value "hardware"
$config->get("modem:sportster\@flowctl");
$config->get("modem:hayes\@flowctl");
=head1 CAVEATS
=over 4
=item character limitations
As mentioned above, variable names (and section names) cannot have the
characters ":", "@", or "=" in them.
=item default defaults
There should be a way to pass default defaults into C<new>. That would
be handy, and could eliminate the need for the UNSET option in C<define>.
=back
=head1 AUTHOR
Kees Cook <kees@outflux.net>
=head1 SEE ALSO
perl(1), sendpage(1), Sendpage::KeesLog(3), Sendpage::Modem(3),
Sendpage::PagingCentral(3), Sendpage::PageQueue(3), Sendpage::Page(3),
Sendpage::Recipient(3), Sendpage::Queue(3)
=head1 COPYRIGHT
Copyright 2000 Kees Cook.
This library is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
=cut
|