1 # debconf -- lintian check script -*- perl -*-
3 # Copyright (C) 2001 Colin Watson
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, you can find it on the World Wide
17 # Web at http://www.gnu.org/copyleft/gpl.html, or write to the Free
18 # Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21 package Maemian::debconf;
25 use Maemian::Relation;
28 # From debconf-devel(7), section 'THE TEMPLATES FILE', up to date with debconf
29 # version 1.5.24. Added indices for cdebconf (indicates sort order for
30 # choices); debconf doesn't support it, but it ignores it, which is safe
32 my %template_fields = map { $_ => 1 }
33 qw(template type choices indices default description);
35 # From debconf-devel(7), section 'THE TEMPLATES FILE', up to date with debconf
37 my %valid_types = map { $_ => 1 }
48 # From debconf-devel(7), section 'THE DEBCONF PROTOCOL' under 'INPUT', up to
49 # date with debconf version 1.5.24.
50 my %valid_priorities = map { $_ => 1 }
51 qw(low medium high critical);
53 # All the packages that provide debconf functionality. Anything using debconf
54 # needs to have dependencies that satisfy one of these.
55 my @debconfs = qw(debconf debconf-2.0 cdebconf cdebconf-udeb libdebconfclient0
56 libdebconfclient0-udeb);
67 my $usesmultiselect='';
69 if ($type eq 'source') {
70 my $binaries = $info->field('binary');
72 my @files = map { "$_.templates" } split /,\s+/, $binaries;
73 push @files, "templates";
75 foreach my $file (@files) {
76 my $templates_file = "debfiles/$file";
78 $binary =~ s/\.?templates$//;
79 # Single binary package (so @files contains "templates" and
80 # "binary.templates")?
81 if (!$binary and $#files == 1) {
85 if (-f $templates_file) {
86 my @templates = read_dpkg_control($templates_file, "templates file");
88 foreach my $template (@templates) {
89 if (exists $template->{template} and exists $template->{_choices}) {
90 tag "template-uses-unsplit-choices",
91 "$binary - $template->{template}";
97 # The remainder of the checks are for binary packages, so we exit now
101 if (open(PREINST, '<', "control/preinst")) {
103 s/\#.*//; # Not perfect for Perl, but should be OK
104 if (m,/usr/share/debconf/confmodule, or
105 m/(?:Debconf|Debian::DebConf)::Client::ConfModule/) {
113 if (-f "control/config") {
116 if (-f "control/templates") {
120 # This still misses packages that use debconf only in the postrm. Packages
121 # that ask debconf questions in the postrm should load the confmodule in the
122 # postinst so that debconf can register their templates.
123 return unless $seenconfig or $seentemplates or $usespreinst;
125 # parse depends info for later checks
127 # Consider every package to depend on itself.
129 if (defined $info->field('version')) {
130 $_ = $info->field('version');
131 $version = "$pkg (= $_)";
134 my (%dependencies, @alldeps);
136 for my $field (qw(depends pre-depends)) {
137 if (defined $info->field($field)) {
138 $_ = $info->field($field);
139 $_ .= ", $version" if defined $version;
141 $dependencies{$field} = Maemian::Relation->new($_);
143 push @alldeps, $version;
144 $dependencies{$field} = Maemian::Relation->new($version);
148 my $alldependencies = Maemian::Relation->new(join ', ', @alldeps);
150 # See if the package depends on dbconfig-common. Packages that do are allowed
151 # to have a config file with no templates, since they use the dbconfig-common
153 my $usesdbconfig = $alldependencies->implies('dbconfig-common');
155 # Check that both debconf control area files are present.
156 if ($seenconfig and not $seentemplates and not $usesdbconfig) {
157 tag "no-debconf-templates", "";
158 } elsif ($seentemplates and not $seenconfig and not $usespreinst and $type ne 'udeb') {
159 tag "no-debconf-config", "";
162 if ($seenconfig and not -x "control/config") {
163 tag "debconf-config-not-executable", "";
166 # First check that templates look valid
167 if ($seentemplates) {
168 open(TMPL, '<', "control/templates")
169 or fail("Can't open control/templates: $!");
174 my $name = 'unknown';
176 foreach my $line (split "\n", $_) {
177 if ($line =~ s/^([-_.A-Za-z0-9]+):\s*(.+)//) {
179 $name = $2 if ($1 eq 'Template');
183 foreach (keys %fields) {
184 if ($fields{$_} > 1) {
186 tag "duplicate-fields-in-templates", "$name $_";
187 # Templates file is corrupted, no need to report
196 # Lots of template checks.
198 my @templates = $seentemplates ? read_dpkg_control("control/templates", "templates file") : ();
199 my %potential_db_abuse;
202 foreach my $template (@templates) {
205 if (not exists $template->{template}) {
206 tag "no-template-name", "";
207 $template->{template} = 'no-template-name';
209 push @templates_seen, $template->{template};
210 if ($template->{template}!~m|[A-Za-z0-9.+-](?:/[A-Za-z0-9.+-])|) {
211 tag "malformed-template-name", "$template->{template}";
215 if (not exists $template->{type}) {
216 tag "no-template-type", "$template->{template}";
217 } elsif (not $valid_types{$template->{type}}) {
218 tag "unknown-template-type", "$template->{type}";
219 } elsif ($template->{type} eq 'select') {
221 } elsif ($template->{type} eq 'multiselect') {
224 } elsif ($template->{type} eq 'boolean') {
225 tag "boolean-template-has-bogus-default",
226 "$template->{template} $template->{default}"
227 if defined $template->{default}
228 and $template->{default} ne 'true'
229 and $template->{default} ne 'false';
232 if ($template->{choices} && ($template->{choices} !~ /^\s*$/)) {
233 my $nrchoices = count_choices ($template->{choices});
234 for my $key (keys %$template) {
235 if ($key =~ /^choices-/) {
236 if (! $template->{$key} || ($template->{$key} =~ /^\s*$/)) {
237 tag "empty-translated-choices", "$template->{template} $key";
239 if (count_choices ($template->{$key}) != $nrchoices) {
240 tag "mismatch-translated-choices", "$template->{template} $key";
244 if ($template->{choices} =~ /^\s*(yes\s*,\s*no|no\s*,\s*yes)\s*$/i) {
245 tag "select-with-boolean-choices", "$template->{template}";
249 if ($isselect and not exists $template->{choices}) {
250 tag "select-without-choices", "$template->{template}";
253 if (not exists $template->{description}) {
254 tag "no-template-description", "$template->{template}";
255 } elsif ($template->{description}=~m/^\s*(.*?)\s*?\n\s*\1\s*$/) {
256 # Check for duplication. Should all this be folded into the
257 # description checks?
258 tag "duplicate-long-description-in-template",
259 "$template->{template}";
263 foreach my $field (sort keys %$template) {
264 # Tests on translations
265 my ($mainfield, $lang) = split m/-/, $field, 2;
267 $languages{$lang}{$mainfield}=1;
269 unless ($template_fields{$mainfield}) { # Ignore language codes here
270 tag "unknown-field-in-templates", "$template->{template} $field";
274 if ($template->{template} && $template->{type}) {
275 $potential_db_abuse{$template->{template}} = 1
276 if (($template->{type} eq "note") or ($template->{type} eq "text"));
279 # Check the description against the best practices in the Developer's
280 # Reference, but skip all templates where the short description contains
281 # the string "for internal use".
282 my ($short, $extended);
283 if (defined $template->{description}) {
284 $template->{description} =~ m/^([^\n]*)\n(.*)$/s;
285 ($short, $extended) = ($1, $2);
286 unless (defined $short) {
287 $short = $template->{description};
290 ($short, $extended) = ('', '');
292 my $type = $template->{type} || '';
293 unless ($short =~ /for internal use/i) {
294 my $isprompt = grep { $_ eq $type } qw(string password);
295 my $isselect = grep { $_ eq $type } qw(select multiselect);
297 if ($short && ($short !~ m/:$/ || $short =~ m/^(what|who|when|where|which|how)/i)) {
298 tag "malformed-prompt-in-templates", $template->{template};
302 if ($short =~ /^(Please|Cho+se|Enter|Select|Specify|Give)/) {
303 tag "using-imperative-form-in-templates", $template->{template};
306 if ($type eq 'boolean') {
307 if ($short !~ /\?/) {
308 tag "malformed-question-in-templates", $template->{template};
311 if (defined ($extended) && $extended =~ /[^\?]\?(\s+|$)/) {
312 tag "using-question-in-extended-description-in-templates", $template->{template};
314 if ($type eq 'note') {
315 if ($short =~ /[.?;:]$/) {
316 tag "malformed-title-in-templates", $template->{template};
319 if (length ($short) > 75) {
320 tag "too-long-short-description-in-templates", $template->{template};
322 if (defined $template->{description}) {
323 if ($template->{description} =~ /(\A|\s)(I|[Mm]y|[Ww]e|[Oo]ur|[Oo]urs|mine|myself|ourself|me|us)(\Z|\s)/) {
324 tag "using-first-person-in-templates", $template->{template};
326 if ($template->{description} =~ /[ \'\"]yes[ \'\",;.]/i and $type eq 'boolean') {
327 tag "making-assumptions-about-interfaces-in-templates", $template->{template};
331 # Check whether the extended description is too long.
334 for my $string (split ("\n", $extended)) {
335 while (length ($string) > 80) {
336 my $pos = rindex ($string, ' ', 80);
338 $pos = index ($string, ' ');
343 $string = substr ($string, $pos + 1);
350 tag "too-long-extended-description-in-templates", $template->{template};
356 # Check the maintainer scripts.
358 my $config_calls_db_input;
361 my %template_aliases;
362 for my $file (qw(config prerm postrm preinst postinst)) {
363 my $potential_makedev = {};
364 if (open(IN, '<', "control/$file")) {
365 my $usesconfmodule='';
366 my $obsoleteconfmodule='';
371 # Only check scripts.
373 unless ($fl && $fl =~ /^\#!/) {
379 s/#.*//; # Not perfect for Perl, but should be OK
386 if (m,(?:\.|source)\s+/usr/share/debconf/confmodule, ||
387 m/(use|require)\s+Debconf::Client::ConfModule/) {
390 if (not $obsoleteconfmodule and
391 m,(/usr/share/debconf/confmodule\.sh|
392 Debian::DebConf::Client::ConfModule),x) {
393 tag "loads-obsolete-confmodule", "$file:$. $1";
395 $obsoleteconfmodule=1;
397 if ($file eq 'config' and m/db_input/) {
398 $config_calls_db_input = 1;
400 if ($file eq 'postinst' and not $db_input and m/db_input/
401 and not $config_calls_db_input) {
403 tag "postinst-uses-db-input", ""
404 unless $type eq 'udeb';
408 $potential_makedev->{$.} = 1;
410 if (m/^\s*(?:db_input|db_text)\s+[\"\']?(\S+?)[\"\']?\s+(\S+)\s/) {
411 my ($priority, $template) = ($1, $2);
412 $templates_used{$template} = 1;
413 if ($priority !~ /^\$\S+$/) {
414 tag "unknown-debconf-priority", "$file:$. $1"
415 unless ($valid_priorities{$priority});
416 tag "possible-debconf-note-abuse", "$file:$. $template"
417 if ($potential_db_abuse{$template}
418 and (not ($potential_makedev->{($. - 1)} and ($priority eq "low")))
419 and ($priority =~ /^(low|medium)$/));
422 if (m/^\s*(?:db_get|db_set(?:title)?)\s+[\"\']?(\S+?)[\"\']?(\s|\Z)/) {
423 $templates_used{$1} = 1;
425 # Try to handle Perl somewhat.
426 if (m/^\s*(?:.*=\s*get|set)\s*\(\s*[\"\'](\S+?)[\"\']/) {
427 $templates_used{$1} = 1;
429 if (m/^\s*db_register\s+[\"\']?(\S+?)[\"\']?\s+(\S+)\s/) {
430 my ($template, $question) = ($1, $2);
431 push @{$template_aliases{$template}}, $question;
433 if (not $isdefault and m/db_fset.*isdefault/) {
435 tag "isdefault-flag-is-deprecated", "$file";
438 if (not $db_purge and m/db_purge/) { # TODO: Perl?
443 if ($file eq 'postinst' or $file eq 'config') {
444 unless ($usesconfmodule) {
445 tag "$file-does-not-load-confmodule", ""
446 unless ($type eq 'udeb' || ($file eq 'postinst' && !$seenconfig));
450 if ($file eq 'postrm') {
452 tag "postrm-does-not-purge-debconf", "";
457 } elsif ($file eq 'postinst') {
458 tag "$file-does-not-load-confmodule", ""
459 unless ($type eq 'udeb' || !$seenconfig);
460 } elsif ($file eq 'postrm') {
461 tag "postrm-does-not-purge-debconf", ""
462 unless ($type eq 'udeb');
466 foreach my $template (@templates_seen) {
467 $template =~ s/\s+\Z//;
471 if ($templates_used{$template}) {
474 foreach my $alias (@{$template_aliases{$template}}) {
475 if ($templates_used{$alias}) {
482 unless ($used or $pkg eq "debconf" or $type eq 'udeb') {
483 tag "unused-debconf-template", $template
484 unless $template =~ m,^shared/packages-(wordlist|ispell)$,
485 or $template =~ m,/languages$,;
489 # Check that the right dependencies are in the control file. Accept any
490 # package that might provide debconf functionality.
492 my $anydebconf = join (' | ', @debconfs);
494 unless ($dependencies{'pre-depends'}->implies($anydebconf)) {
495 tag "missing-debconf-dependency-for-preinst", ""
496 unless $type eq 'udeb';
499 unless ($alldependencies->implies($anydebconf) or $usesdbconfig) {
500 tag "missing-debconf-dependency", "";
504 # Now make sure that no scripts are using debconf as a registry.
505 # Unfortunately this requires us to unpack to level 2 and grep all the
506 # scripts in the package.
507 # the following checks is ignored if the package being checked is debconf
510 return 0 if ($pkg eq "debconf") || ($type eq 'udeb');
512 foreach my $filename (sort keys %{$info->scripts}) {
513 open(IN, '<', "unpacked/$filename") or fail("cannot open $filename: $!");
515 s/#.*//; # Not perfect for Perl, but should be OK
516 if (m,/usr/share/debconf/confmodule, or
517 m/(?:Debconf|Debian::DebConf)::Client::ConfModule/) {
518 tag "debconf-is-not-a-registry", "$filename";
527 # -----------------------------------
529 # Count the number of choices. Splitting code copied from debconf 1.5.8
530 # (Debconf::Question).
535 for my $chunk (split /(\\[, ]|,\s+)/, $choices) {
536 if ($chunk =~ /^\\([, ])$/) {
538 } elsif ($chunk =~ /^,\s+$/) {
539 push (@items, $item);
545 push (@items, $item) if $item ne '';
546 return scalar (@items);
552 # indent-tabs-mode: t
553 # cperl-indent-level: 4
555 # vim: syntax=perl ts=8