| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
Debconf::Config - Debconf meta-configuration module |
| 6 |
|
| 7 |
=cut |
| 8 |
|
| 9 |
package Debconf::Config; |
| 10 |
use warnings; |
| 11 |
use strict; |
| 12 |
use Debconf::Question; |
| 13 |
use Debconf::Gettext; |
| 14 |
use Debconf::Priority qw(priority_valid priority_list); |
| 15 |
use Debconf::Log qw(warn); |
| 16 |
use Debconf::Db; |
| 17 |
|
| 18 |
use fields qw(config templates frontend frontend_forced priority terse reshow |
| 19 |
admin_email log debug nowarnings smileys sigils |
| 20 |
noninteractive_seen c_values); |
| 21 |
our $config=fields::new('Debconf::Config'); |
| 22 |
|
| 23 |
our @config_files=("/etc/debconf.conf", "/usr/share/debconf/debconf.conf"); |
| 24 |
if ($ENV{DEBCONF_SYSTEMRC}) { |
| 25 |
unshift @config_files, $ENV{DEBCONF_SYSTEMRC}; |
| 26 |
} else { |
| 27 |
# I don't use $ENV{HOME} because it can be a bit untrustworthy if |
| 28 |
# set by programs like sudo, and that proved to be confusing |
| 29 |
unshift @config_files, ((getpwuid($>))[7])."/.debconfrc"; |
| 30 |
} |
| 31 |
|
| 32 |
=head1 DESCRIPTION |
| 33 |
|
| 34 |
This package holds configuration values for debconf. It supplies defaults, |
| 35 |
and allows them to be overridden by values from the command line, the |
| 36 |
environment, the config file, and values pulled out of the debconf database. |
| 37 |
|
| 38 |
=head1 METHODS |
| 39 |
|
| 40 |
=over 4 |
| 41 |
|
| 42 |
=item load |
| 43 |
|
| 44 |
This class method reads and parses a config file. The config file format is |
| 45 |
a series of stanzas; the first stanza configures debconf as a whole, and |
| 46 |
then each of the rest sets up a database driver. This lacks the glorious |
| 47 |
nested bindish beauty of Wichert's original idea, but it captures the |
| 48 |
essence of it. It will load from a set of standard locations unless a file |
| 49 |
to load is specified as the first parameter. |
| 50 |
|
| 51 |
If a hash of parameters are passed, those parameters are used as the defaults |
| 52 |
for *every* database driver that is loaded up. Practically, setting |
| 53 |
(readonly => "true") is the only use of this. |
| 54 |
|
| 55 |
=cut |
| 56 |
|
| 57 |
# Turns a chunk of text into a hash. Returns number of fields |
| 58 |
# that were processed. Also handles env variable expansion. |
| 59 |
sub _hashify ($$) { |
| 60 |
my $text=shift; |
| 61 |
my $hash=shift; |
| 62 |
|
| 63 |
$text =~ s/\$\{([^}]+)\}/$ENV{$1}/eg; |
| 64 |
|
| 65 |
my %ret; |
| 66 |
my $i; |
| 67 |
foreach my $line (split /\n/, $text) { |
| 68 |
next if $line=~/^\s*#/; # comment |
| 69 |
next if $line=~/^\s*$/; # blank |
| 70 |
$line=~s/^\s+//; |
| 71 |
$line=~s/\s+$//; |
| 72 |
$i++; |
| 73 |
my ($key, $value)=split(/\s*:\s*/, $line, 2); |
| 74 |
$key=~tr/-/_/; |
| 75 |
die "Parse error" unless defined $key and length $key; |
| 76 |
$hash->{lc($key)}=$value; |
| 77 |
} |
| 78 |
return $i; |
| 79 |
} |
| 80 |
|
| 81 |
# Processes an environment variable that encodes a reference to an existing |
| 82 |
# db, or the parameters to set up a new db. Returns the db. Additional |
| 83 |
# parameters will be used as defaults if a new driver is set up. At least a |
| 84 |
# name default should always be passed. Returns the db name. |
| 85 |
sub _env_to_driver { |
| 86 |
my $value=shift; |
| 87 |
|
| 88 |
my ($name, $options) = $value =~ m/^(\w+)(?:{(.*)})?$/; |
| 89 |
return unless $name; |
| 90 |
|
| 91 |
return $name if Debconf::DbDriver->driver($name); |
| 92 |
|
| 93 |
my %hash = @_; # defaults from params |
| 94 |
$hash{driver} = $name; |
| 95 |
|
| 96 |
if (defined $options) { |
| 97 |
# And add any other name:value name:value pairs, |
| 98 |
# default name is `filename' for convienence. |
| 99 |
foreach (split ' ', $options) { |
| 100 |
if (/^(\w+):(.*)/) { |
| 101 |
$hash{$1}=$2; |
| 102 |
} |
| 103 |
else { |
| 104 |
$hash{filename}=$_; |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
return Debconf::Db->makedriver(%hash)->{name}; |
| 109 |
} |
| 110 |
|
| 111 |
sub load { |
| 112 |
my $class=shift; |
| 113 |
my $cf=shift; |
| 114 |
my @defaults=@_; |
| 115 |
|
| 116 |
if (! $cf) { |
| 117 |
for my $file (@config_files) { |
| 118 |
$file = "$ENV{DPKG_ROOT}$file" if exists $ENV{DPKG_ROOT}; |
| 119 |
if (-e $file) { |
| 120 |
$cf = $file; |
| 121 |
last; |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
die "No config file found" unless $cf; |
| 126 |
|
| 127 |
open (my $debconf_config, "<", $cf) or die "$cf: $!\n"; |
| 128 |
local $/="\n\n"; # read a stanza at a time |
| 129 |
|
| 130 |
# Read global options stanza. |
| 131 |
1 until _hashify(<$debconf_config>, $config) || eof $debconf_config; |
| 132 |
|
| 133 |
# Verify that all options are sane. |
| 134 |
if (! exists $config->{config}) { |
| 135 |
print STDERR "debconf: ".gettext("Config database not specified in config file.")."\n"; |
| 136 |
exit(1); |
| 137 |
} |
| 138 |
if (! exists $config->{templates}) { |
| 139 |
print STDERR "debconf: ".gettext("Template database not specified in config file.")."\n"; |
| 140 |
exit(1); |
| 141 |
} |
| 142 |
|
| 143 |
if (exists $config->{sigils} || exists $config->{smileys}) { |
| 144 |
print STDERR "debconf: ".gettext("The Sigils and Smileys options in the config file are no longer used. Please remove them.")."\n"; |
| 145 |
} |
| 146 |
|
| 147 |
# Now read in each database driver, and set it up. |
| 148 |
while (<$debconf_config>) { |
| 149 |
my %config=(@defaults); |
| 150 |
if (exists $ENV{DEBCONF_DB_REPLACE}) { |
| 151 |
$config{readonly} = "true"; |
| 152 |
} |
| 153 |
if (exists $ENV{DPKG_ROOT}) { |
| 154 |
$config{root} = $ENV{DPKG_ROOT}; |
| 155 |
} |
| 156 |
next unless _hashify($_, \%config); |
| 157 |
eval { |
| 158 |
Debconf::Db->makedriver(%config); |
| 159 |
}; |
| 160 |
if ($@) { |
| 161 |
print STDERR "debconf: ".sprintf(gettext("Problem setting up the database defined by stanza %s of %s."),$., $cf)."\n"; |
| 162 |
die $@; |
| 163 |
} |
| 164 |
} |
| 165 |
close $debconf_config; |
| 166 |
|
| 167 |
# DEBCONF_DB_REPLACE bypasses the normal databases. We do still need |
| 168 |
# to set up the normal databases anyway so that the template |
| 169 |
# database is available, but we load them all read-only above. |
| 170 |
if (exists $ENV{DEBCONF_DB_REPLACE}) { |
| 171 |
$config->{config} = _env_to_driver($ENV{DEBCONF_DB_REPLACE}, |
| 172 |
name => "_ENV_REPLACE"); |
| 173 |
# Unfortunately a read-only template database isn't always |
| 174 |
# good enough, so we need to stack a throwaway one in front |
| 175 |
# of it just in case anything tries to register new |
| 176 |
# templates. There is no provision yet for keeping this |
| 177 |
# database around after debconf exits. |
| 178 |
Debconf::Db->makedriver( |
| 179 |
driver => "Pipe", |
| 180 |
name => "_ENV_REPLACE_templates", |
| 181 |
infd => "none", |
| 182 |
outfd => "none", |
| 183 |
); |
| 184 |
my @template_stack = ("_ENV_REPLACE_templates", $config->{templates}); |
| 185 |
Debconf::Db->makedriver( |
| 186 |
driver => "Stack", |
| 187 |
name => "_ENV_stack_templates", |
| 188 |
stack => join(", ", @template_stack), |
| 189 |
); |
| 190 |
$config->{templates} = "_ENV_stack_templates"; |
| 191 |
} |
| 192 |
|
| 193 |
# Allow environment overriding of primary database driver |
| 194 |
my @finalstack = ($config->{config}); |
| 195 |
if (exists $ENV{DEBCONF_DB_OVERRIDE}) { |
| 196 |
unshift @finalstack, _env_to_driver($ENV{DEBCONF_DB_OVERRIDE}, |
| 197 |
name => "_ENV_OVERRIDE"); |
| 198 |
} |
| 199 |
if (exists $ENV{DEBCONF_DB_FALLBACK}) { |
| 200 |
push @finalstack, _env_to_driver($ENV{DEBCONF_DB_FALLBACK}, |
| 201 |
name => "_ENV_FALLBACK", |
| 202 |
readonly => "true"); |
| 203 |
} |
| 204 |
if (@finalstack > 1) { |
| 205 |
Debconf::Db->makedriver( |
| 206 |
driver => "Stack", |
| 207 |
name => "_ENV_stack", |
| 208 |
stack => join(", ", @finalstack), |
| 209 |
); |
| 210 |
$config->{config} = "_ENV_stack"; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
=item getopt |
| 215 |
|
| 216 |
This class method parses command line options in @ARGV with GetOptions from |
| 217 |
Getopt::Long. Many meta configuration items can be overridden with command |
| 218 |
line options. |
| 219 |
|
| 220 |
The first parameter should be basic usage text for the program in |
| 221 |
question. Usage text for the globally supported options will be prepended |
| 222 |
to this if usage help must be printed. |
| 223 |
|
| 224 |
If any additonal parameters are passed to this function, they are also |
| 225 |
passed to GetOptions. This can be used to handle additional options. |
| 226 |
|
| 227 |
=cut |
| 228 |
|
| 229 |
sub getopt { |
| 230 |
my $class=shift; |
| 231 |
my $usage=shift; |
| 232 |
|
| 233 |
my $showusage=sub { # closure |
| 234 |
print STDERR $usage."\n"; |
| 235 |
print STDERR gettext(<<EOF); |
| 236 |
-f, --frontend Specify debconf frontend to use. |
| 237 |
-p, --priority Specify minimum priority question to show. |
| 238 |
--terse Enable terse mode. |
| 239 |
EOF |
| 240 |
exit 1; |
| 241 |
}; |
| 242 |
|
| 243 |
# don't load big Getopt::Long unless really necessary. |
| 244 |
return unless grep { $_ =~ /^-/ } @ARGV; |
| 245 |
|
| 246 |
require Getopt::Long; |
| 247 |
Getopt::Long::Configure('bundling'); |
| 248 |
Getopt::Long::GetOptions( |
| 249 |
'frontend|f=s', sub { shift; $class->frontend(shift); $config->frontend_forced(1) }, |
| 250 |
'priority|p=s', sub { shift; $class->priority(shift) }, |
| 251 |
'terse', sub { $config->{terse} = 'true' }, |
| 252 |
'help|h', $showusage, |
| 253 |
@_, |
| 254 |
) || $showusage->(); |
| 255 |
} |
| 256 |
|
| 257 |
=item frontend |
| 258 |
|
| 259 |
The frontend to use. Looks at first the value of DEBIAN_FRONTEND, second the |
| 260 |
config file, third the database, and if all of those fail, defaults to the |
| 261 |
dialog frontend. |
| 262 |
|
| 263 |
If a value is passed to this function, it changes it temporarily (for |
| 264 |
the lifetime of the program) to override what's in the database or config |
| 265 |
file. |
| 266 |
|
| 267 |
=cut |
| 268 |
|
| 269 |
sub frontend { |
| 270 |
my $class=shift; |
| 271 |
|
| 272 |
return $ENV{DEBIAN_FRONTEND} if exists $ENV{DEBIAN_FRONTEND}; |
| 273 |
$config->{frontend}=shift if @_; |
| 274 |
return $config->{frontend} if exists $config->{frontend}; |
| 275 |
|
| 276 |
my $ret='dialog'; |
| 277 |
my $question=Debconf::Question->get('debconf/frontend'); |
| 278 |
if ($question) { |
| 279 |
$ret=lcfirst($question->value) || $ret; |
| 280 |
} |
| 281 |
return $ret; |
| 282 |
} |
| 283 |
|
| 284 |
=item frontend_forced |
| 285 |
|
| 286 |
Whether the frontend was forced set on the command line or in the |
| 287 |
environment. |
| 288 |
|
| 289 |
=cut |
| 290 |
|
| 291 |
sub frontend_forced { |
| 292 |
my ($class, $val) = @_; |
| 293 |
$config->{frontend_forced} = $val |
| 294 |
if defined $val || exists $ENV{DEBIAN_FRONTEND}; |
| 295 |
return $config->{frontend_forced} ? 1 : 0; |
| 296 |
} |
| 297 |
|
| 298 |
=item priority |
| 299 |
|
| 300 |
The lowest priority of questions you want to see. Looks at first the value |
| 301 |
of DEBIAN_PRIORITY, second the config file, third the database, and if all |
| 302 |
of those fail, defaults to "high". |
| 303 |
|
| 304 |
If a value is passed to this function, it changes it temporarily (for |
| 305 |
the lifetime of the program) to override what's in the database or config |
| 306 |
file. |
| 307 |
|
| 308 |
=cut |
| 309 |
|
| 310 |
sub priority { |
| 311 |
my $class=shift; |
| 312 |
return $ENV{DEBIAN_PRIORITY} if exists $ENV{DEBIAN_PRIORITY}; |
| 313 |
if (@_) { |
| 314 |
my $newpri=shift; |
| 315 |
if (! priority_valid($newpri)) { |
| 316 |
warn(sprintf(gettext("Ignoring invalid priority \"%s\""), $newpri)); |
| 317 |
warn(sprintf(gettext("Valid priorities are: %s"), join(" ", priority_list()))); |
| 318 |
} |
| 319 |
else { |
| 320 |
$config->{priority}=$newpri; |
| 321 |
} |
| 322 |
} |
| 323 |
return $config->{priority} if exists $config->{priority}; |
| 324 |
|
| 325 |
my $ret='high'; |
| 326 |
my $question=Debconf::Question->get('debconf/priority'); |
| 327 |
if ($question) { |
| 328 |
$ret=$question->value || $ret; |
| 329 |
} |
| 330 |
return $ret; |
| 331 |
} |
| 332 |
|
| 333 |
=item terse |
| 334 |
|
| 335 |
The behavior in terse mode varies by frontend. Changes to terse mode are |
| 336 |
not persistant across debconf invocations. |
| 337 |
|
| 338 |
=cut |
| 339 |
|
| 340 |
sub terse { |
| 341 |
my $class=shift; |
| 342 |
return $ENV{DEBCONF_TERSE} if exists $ENV{DEBCONF_TERSE}; |
| 343 |
$config->{terse}=$_[0] if @_; |
| 344 |
return $config->{terse} if exists $config->{terse}; |
| 345 |
return 'false'; |
| 346 |
} |
| 347 |
|
| 348 |
=item nowarnings |
| 349 |
|
| 350 |
Set to disable warnings. |
| 351 |
|
| 352 |
=cut |
| 353 |
|
| 354 |
sub nowarnings { |
| 355 |
my $class=shift; |
| 356 |
return $ENV{DEBCONF_NOWARNINGS} if exists $ENV{DEBCONF_NOWARNINGS}; |
| 357 |
$config->{nowarnings}=$_[0] if @_; |
| 358 |
return $config->{nowarnings} if exists $config->{nowarnings}; |
| 359 |
return 'false'; |
| 360 |
} |
| 361 |
|
| 362 |
=item debug |
| 363 |
|
| 364 |
Returns debconf's debug regex. This is pulled out of the config file, |
| 365 |
and may be overridden by DEBCONF_DEBUG in the environment. |
| 366 |
|
| 367 |
=cut |
| 368 |
|
| 369 |
sub debug { |
| 370 |
my $class=shift; |
| 371 |
return $ENV{DEBCONF_DEBUG} if exists $ENV{DEBCONF_DEBUG}; |
| 372 |
return $config->{debug} if exists $config->{debug}; |
| 373 |
return ''; |
| 374 |
} |
| 375 |
|
| 376 |
=item admin_email |
| 377 |
|
| 378 |
Returns an email address to use to send notes to. This is pulled out of the |
| 379 |
config file, and may be overridden by the DEBCONF_ADMIN_MAIL environment |
| 380 |
variable. If neither is set, it defaults to root. |
| 381 |
|
| 382 |
=cut |
| 383 |
|
| 384 |
sub admin_email { |
| 385 |
my $class=shift; |
| 386 |
return $ENV{DEBCONF_ADMIN_EMAIL} if exists $ENV{DEBCONF_ADMIN_EMAIL}; |
| 387 |
return $config->{admin_email} if exists $config->{admin_email}; |
| 388 |
return 'root'; |
| 389 |
} |
| 390 |
|
| 391 |
=item noninteractive_seen |
| 392 |
|
| 393 |
Set to cause the seen flag to be set for questions asked in the |
| 394 |
noninteractive frontend. |
| 395 |
|
| 396 |
=cut |
| 397 |
|
| 398 |
sub noninteractive_seen { |
| 399 |
my $class=shift; |
| 400 |
return $ENV{DEBCONF_NONINTERACTIVE_SEEN} if exists $ENV{DEBCONF_NONINTERACTIVE_SEEN}; |
| 401 |
return $config->{noninteractive_seen} if exists $config->{noninteractive_seen}; |
| 402 |
return 'false'; |
| 403 |
} |
| 404 |
|
| 405 |
=item c_values |
| 406 |
|
| 407 |
Set to true to display "coded" values from Choices-C fields instead of the |
| 408 |
descriptive values from other fields for select and multiselect templates. |
| 409 |
|
| 410 |
=cut |
| 411 |
|
| 412 |
sub c_values { |
| 413 |
my $class=shift; |
| 414 |
return $ENV{DEBCONF_C_VALUES} if exists $ENV{DEBCONF_C_VALUES}; |
| 415 |
return $config->{c_values} if exists $config->{c_values}; |
| 416 |
return 'false'; |
| 417 |
} |
| 418 |
|
| 419 |
=back |
| 420 |
|
| 421 |
=head1 FIELDS |
| 422 |
|
| 423 |
Other fields can be accessed and set by calling class methods. |
| 424 |
|
| 425 |
=cut |
| 426 |
|
| 427 |
sub AUTOLOAD { |
| 428 |
(my $field = our $AUTOLOAD) =~ s/.*://; |
| 429 |
my $class=shift; |
| 430 |
|
| 431 |
return $config->{$field}=shift if @_; |
| 432 |
return $config->{$field} if defined $config->{$field}; |
| 433 |
return ''; |
| 434 |
} |
| 435 |
|
| 436 |
=head1 AUTHOR |
| 437 |
|
| 438 |
Joey Hess <[email protected]> |
| 439 |
|
| 440 |
=cut |
| 441 |
|
| 442 |
1 |