summaryrefslogtreecommitdiffstats
path: root/lib/Lintian/Deb822.pm
blob: c153415332e3d3e22029172700499f660f68a086 (plain)
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
# Copyright (C) 1998 Christian Schwarz
# Copyright (C) 2020 Felix Lechner
#
# 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, you can find it on the World Wide
# Web at https://www.gnu.org/copyleft/gpl.html, or write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1301, USA.

package Lintian::Deb822;

use v5.20;
use warnings;
use utf8;

use Const::Fast;
use Path::Tiny;
use Syntax::Keyword::Try;
use Unicode::UTF8 qw(encode_utf8);

use Lintian::Deb822::Constants qw(:constants);
use Lintian::Deb822::Section;

const my $EMPTY => q{};
const my $NUMBER_SIGN => q{#};

use Moo;
use namespace::clean;

=encoding utf-8

=head1 NAME

Lintian::Deb822 -- A deb822 control file

=head1 SYNOPSIS

 use Lintian::Deb822;

=head1 DESCRIPTION

Represents a paragraph in a Deb822 control file.

=head1 INSTANCE METHODS

=over 4

=item sections

Array of Deb822::Section objects in order of their original appearance.

=item positions

Line positions

=cut

has sections => (is => 'rw', default => sub { [] });
has positions => (is => 'rw', default => sub { [] });

=item first_mention

=cut

sub first_mention {
    my ($self, $name) = @_;

    my $earliest;

    # empty when field not present
    $earliest ||= $_->value($name) for @{$self->sections};

    return ($earliest // $EMPTY);
}

=item last_mention

=cut

sub last_mention {
    my ($self, $name) = @_;

    my $latest;

    for my $section (@{$self->sections}) {

        # empty when field not present
        $latest = $section->value($name)
          if $section->declares($name);
    }

    return ($latest // $EMPTY);
}

=item read_file

=cut

sub read_file {
    my ($self, $path, $flags) = @_;

    my $contents = path($path)->slurp_utf8;

    return $self->parse_string($contents, $flags);
}

=item parse_string

=cut

sub parse_string {
    my ($self, $contents, $flags) = @_;

    my (@paragraphs, @positions);

    try {
        @paragraphs= parse_dpkg_control_string($contents, $flags,\@positions);

    } catch {
        # ignore syntax errors here
        die map { encode_utf8($_) } $@
          unless $@ =~ /syntax error/;
    }

    my $index = 0;
    for my $paragraph (@paragraphs) {

        my $section = Lintian::Deb822::Section->new;
        $section->verbatim($paragraph);
        $section->positions($positions[$index]);

        push(@{$self->sections}, $section);

    } continue {
        $index++;
    }

    return @{$self->sections};
}

=back

=head1 FUNCTIONS

=head2 Debian control parsers

At first glance, this module appears to contain several debian control
parsers.  In practise, there is only one real parser
(L</visit_dpkg_paragraph_string>) - the rest are convenience functions around
it.

=over 4

=item read_dpkg_control(FILE[, FLAGS[, LINES]])

This is a convenience function to ease using L</parse_dpkg_control>
with paths to files (rather than open handles).  The first argument
must be the path to a FILE, which should be read as a debian control
file.  If the file is empty, an empty list is returned.

Otherwise, this behaves like:

 use autodie;
 
 open(my $fd, '<:encoding(UTF-8)', FILE); # or '<'
 my @p = parse_dpkg_control($fd, FLAGS, LINES);
 close($fd);
 return @p;

This goes without saying that may fail with any of the messages that
L</parse_dpkg_control(HANDLE[, FLAGS[, LINES]])> do.  It can also emit
autodie exceptions if open or close fails.

=cut

sub read_dpkg_control {
    my ($file, $flags, $field_starts) = @_;

    open(my $handle, '<:utf8_strict', $file)
      or die encode_utf8("Cannot open $file");

    local $/ = undef;
    my $string = <$handle>;
    close $handle;

    my @result;

    my $visitor = sub {
        my ($paragraph, $line) = @_;

        push(@result, $paragraph);
        push(@{$field_starts}, $line) if defined $field_starts;
    };

    visit_dpkg_paragraph_string($visitor, $string, $flags);

    return @result;
}

=item read_dpkg_control_lc(FILE[, FLAGS[, LINES]])

=cut

sub read_dpkg_control_lc {
    my ($file, $flags, $field_starts) = @_;

    my @result = read_dpkg_control($file, $flags, $field_starts);

    lowercase_field_names(\@result);
    lowercase_field_names($field_starts);

    return @result;
}

=item parse_dpkg_control_string(STRING[, FLAGS[, LINES]])

Reads debian control data from STRING and returns a list of
paragraphs in it.  A paragraph is represented via a hashref, which
maps (lower cased) field names to their values.

FLAGS (if given) is a bitmask of the I<DCTRL_*> constants.  Please
refer to L</CONSTANTS> for the list of constants and their meaning.
The default value for FLAGS is 0.

If LINES is given, it should be a reference to an empty list.  On
return, LINES will be populated with a hashref for each paragraph (in
the same order as the returned list).  Each hashref will also have a
special key "I<START-OF-PARAGRAPH>" that gives the line number of the
first field in that paragraph.  These hashrefs will map the field name
of the given paragraph to the line number where the field name
appeared.

This is a convenience sub around L</visit_dpkg_paragraph> and can
therefore produce the same errors as it.  Please see
L</visit_dpkg_paragraph> for the finer semantics of how the
control file is parsed.

NB: parse_dpkg_control does I<not> close the handle for the caller.

=cut

sub parse_dpkg_control_string {
    my ($string, $flags, $field_starts) = @_;
    my @result;

    my $c = sub {
        my ($para, $line) = @_;

        push(@result, $para);
        push(@{$field_starts}, $line)
          if defined $field_starts;
    };

    visit_dpkg_paragraph_string($c, $string, $flags);

    return @result;
}

=item parse_dpkg_control_string_lc(STRING[, FLAGS[, LINES]])

=cut

sub parse_dpkg_control_string_lc {
    my ($string, $flags, $field_starts) = @_;

    my @result = parse_dpkg_control_string($string, $flags, $field_starts);

    lowercase_field_names(\@result);
    lowercase_field_names($field_starts);

    return @result;
}

=item lowercase_field_names

=cut

sub lowercase_field_names {
    my ($arrayref) = @_;

    return
      unless $arrayref;

    for my $paragraph (@{$arrayref}) {

        # magic marker should only appear in field starts
        my @fields = grep { $_ ne 'START-OF-PARAGRAPH' } keys %{$paragraph};
        my @mixedcase = grep { $_ ne lc } @fields;

        for my $old (@mixedcase) {
            $paragraph->{lc $old} = $paragraph->{$old};
            delete $paragraph->{$old};
        }
    }

    return;
}

=item visit_dpkg_paragraph_string (CODE, STRING[, FLAGS])

Reads debian control data from STRING and passes each paragraph to
CODE.  A paragraph is represented via a hashref, which maps (lower
cased) field names to their values.

FLAGS (if given) is a bitmask of the I<DCTRL_*> constants.  Please
refer to L</CONSTANTS> for the list of constants and their meaning.
The default value for FLAGS is 0.

If the file is empty (i.e. it contains no paragraphs), the method will
contain an I<empty> list.  The deb822 contents may be inside a
I<signed> PGP message with a signature.

visit_dpkg_paragraph will require the PGP headers to be correct (if
present) and require that the entire file is covered by the signature.
However, it will I<not> validate the signature (in fact, the contents
of the PGP SIGNATURE part can be empty).  The signature should be
validated separately.

visit_dpkg_paragraph will pass paragraphs to CODE as they are
completed.  If CODE can process the paragraphs as they are seen, very
large control files can be processed without keeping all the
paragraphs in memory.

As a consequence of how the file is parsed, CODE may be passed a
number of (valid) paragraphs before parsing is stopped due to a syntax
error.

NB: visit_dpkg_paragraph does I<not> close the handle for the caller.

CODE is expected to be a callable reference (e.g. a sub) and will be
invoked as the following:

=over 4

=item CODE->(PARA, LINE_NUMBERS)

The first argument, PARA, is a hashref to the most recent paragraph
parsed.  The second argument, LINE_NUMBERS, is a hashref mapping each
of the field names to the line number where the field name appeared.
LINE_NUMBERS will also have a special key "I<START-OF-PARAGRAPH>" that
gives the line number of the first field in that paragraph.

The return value of CODE is ignored.

If the CODE invokes die (or similar) the error is propagated to the
caller.

=back


I<On syntax errors>, visit_dpkg_paragraph will call die with the
following string:

  "syntax error at line %d: %s\n"

Where %d is the line number of the issue and %s is one of:

=over

=item Duplicate field %s

The field appeared twice in the paragraph.

=item Continuation line outside a paragraph (maybe line %d should be " .")

A continuation line appears outside a paragraph - usually caused by an
unintended empty line before it.

=item Whitespace line not allowed (possibly missing a ".")

An empty continuation line was found.  This usually means that a
period is missing to denote an "empty line" in (e.g.) the long
description of a package.

=item Cannot parse line "%s"

Generic error containing the text of the line that confused the
parser.  Note that all non-printables in %s will be replaced by
underscores.

=item Comments are not allowed

A comment line appeared and FLAGS contained DCTRL_NO_COMMENTS.

=item PGP signature seen before start of signed message

A "BEGIN PGP SIGNATURE" header is seen and a "BEGIN PGP MESSAGE" has
not been seen yet.

=item Two PGP signatures (first one at line %d)

Two "BEGIN PGP SIGNATURE" headers are seen in the same file.

=item Unexpected %s header

A valid PGP header appears (e.g. "BEGIN PUBLIC KEY BLOCK").

=item Malformed PGP header

An invalid or malformed PGP header appears.

=item Expected at most one signed message (previous at line %d)

Two "BEGIN PGP MESSAGE" headers appears in the same message.

=item End of file but expected an "END PGP SIGNATURE" header

The file ended after a "BEGIN PGP SIGNATURE" header without being
followed by an "END PGP SIGNATURE".

=item PGP MESSAGE header must be first content if present

The file had content before PGP MESSAGE.

=item Data after the PGP SIGNATURE

The file had data after the PGP SIGNATURE block ended.

=item End of file before "BEGIN PGP SIGNATURE"

The file had a "BEGIN PGP MESSAGE" header, but no signature was
present.

=back

=cut

sub visit_dpkg_paragraph_string {
    my ($code, $string, $flags) = @_;
    $flags//=0;
    my $field_starts = {};
    my $section = {};
    my $open_section = 0;
    my $last_tag;
    my $debconf = $flags & DCTRL_DEBCONF_TEMPLATE;
    my $signed = 0;
    my $signature = 0;

    my @lines = split(/\n/, $string);

    my $position = 1;

    my $line;
    while (defined($line = shift @lines)) {
        chomp $line;

        if (substr($line, 0, 1) eq $NUMBER_SIGN) {
            next
              unless $flags & DCTRL_NO_COMMENTS;
            die encode_utf8("No comments allowed (line $position).\n");
        }

        # empty line?
        if ($line eq $EMPTY || (!$debconf && $line =~ /^\s*$/)) {
            if ($open_section) { # end of current section
                 # pass the current section to the handler
                $code->($section, $field_starts);
                $section = {};
                $field_starts = {};
                $open_section = 0;
            }
        }
        # pgp sig? Be strict here (due to #696230)
        # According to http://tools.ietf.org/html/rfc4880#section-6.2
        # The header MUST start at the beginning of the line and MUST NOT have
        # any other text (except whitespace) after the header.
        elsif ($line =~ m/^-----BEGIN PGP SIGNATURE-----[ \r\t]*$/)
        { # skip until end of signature
            my $saw_end = 0;

            die encode_utf8("PGP signature before message (line $position).\n")
              unless $signed;

            die encode_utf8(
"Found two PGP signatures (line $signature and line $position).\n"
            )if $signature;

            $signature = $position;
            while (defined($line = shift @lines)) {
                if ($line =~ /^-----END PGP SIGNATURE-----[ \r\t]*$/) {
                    $saw_end = 1;
                    last;
                }
            }continue {
                ++$position;
            }

            # The "at line X" may seem a little weird, but it keeps the
            # message format identical.
            die encode_utf8("Cannot find END PGP SIGNATURE header.\n")
              unless $saw_end;
        }
        # other pgp control?
        elsif ($line =~ /^-----(?:BEGIN|END) PGP/) {
            # At this point it could be a malformed PGP header or one
            # of the following valid headers (RFC4880):
            #  * BEGIN PGP MESSAGE
            #    - Possibly a signed Debian CTRL, so okay (for now)
            #  * BEGIN PGP {PUBLIC,PRIVATE} KEY BLOCK
            #    - Valid header, but not a Debian CTRL file.
            #  * BEGIN PGP MESSAGE, PART X{,/Y}
            #    - Valid, but we don't support partial messages, so
            #      bail on those.

            unless ($line =~ /^-----BEGIN PGP SIGNED MESSAGE-----[ \r\t]*$/) {
                # Not a (full) PGP MESSAGE; reject.

                my $key = qr/(?:BEGIN|END) PGP (?:PUBLIC|PRIVATE) KEY BLOCK/;
                my $msgpart = qr{BEGIN PGP MESSAGE, PART \d+(?:/\d+)?};
                my $msg
                  = qr/(?:BEGIN|END) PGP (?:(?:COMPRESSED|ENCRYPTED) )?MESSAGE/;

                if ($line =~ /^-----($key|$msgpart|$msg)-----[ \r\t]*$/) {
                    die encode_utf8(
                        "Unexpected $1 header (line $position).\n");
                }

                die encode_utf8("Malformed PGP header (line $position).\n");

            } else {
                die encode_utf8(
"Multiple PGP messages (line $signed and line $position).\n"
                )if $signed;

                # NB: If you remove this, keep in mind that it may
                # allow two paragraphs to merge.  Consider:
                #
                # Field-P1: some-value
                # -----BEGIN PGP SIGNATURE-----
                #
                # Field-P2: another value
                #
                # At the time of writing: If $open_section is
                # true, it will remain so until the empty line
                # after the PGP header.
                die encode_utf8(
                    "Expected PGP MESSAGE header (line $position).\n")
                  if $last_tag;

                $signed = $position;
            }

            # skip until the next blank line
            while (defined($line = shift @lines)) {
                last
                  if $line =~ /^\s*$/;
            }continue {
                ++$position;
            }
        }
       # did we see a signature already?  We allow all whitespace/comment lines
       # outside the signature.
        elsif ($signature) {
            # Accept empty lines after the signature.
            next
              if $line =~ /^\s*$/;

            # NB: If you remove this, keep in mind that it may allow
            # two paragraphs to merge.  Consider:
            #
            # Field-P1: some-value
            # -----BEGIN PGP SIGNATURE-----
            # [...]
            # -----END PGP SIGNATURE-----
            # Field-P2: another value
            #
            # At the time of writing: If $open_section is true, it
            # will remain so until the empty line after the PGP
            # header.
            die encode_utf8("Data after PGP SIGNATURE (line $position).\n");
        }
        # new empty field?
        elsif ($line =~ /^([^: \t]+):\s*$/) {
            $field_starts->{'START-OF-PARAGRAPH'} = $position
              unless $open_section;
            $open_section = 1;

            my $tag = $1;
            $section->{$tag} = $EMPTY;
            $field_starts->{$tag} = $position;

            $last_tag = $tag;
        }
        # new field?
        elsif ($line =~ /^([^: \t]+):\s*(.*)$/) {
            $field_starts->{'START-OF-PARAGRAPH'} = $position
              unless $open_section;
            $open_section = 1;

            # Policy: Horizontal whitespace (spaces and tabs) may occur
            # immediately before or after the value and is ignored there.
            my $tag = $1;
            my $value = $2;

            # trim right
            $value =~ s/\s+$//;

            if (exists $section->{$tag}) {
                # Policy: A paragraph must not contain more than one instance
                # of a particular field name.
                die encode_utf8("Duplicate field $tag (line $position).\n");
            }
            $value =~ s/#.*$//
              if $flags & DCTRL_COMMENTS_AT_EOL;
            $section->{$tag} = $value;
            $field_starts->{$tag} = $position;

            $last_tag = $tag;
        }

        # continued field?
        elsif ($line =~ /^([ \t].*\S.*)$/) {
            die encode_utf8(
"Continuation line not in paragraph (line $position). Missing a dot on the previous line?\n"
            )unless $open_section;

            # Policy: Many fields' values may span several lines; in this case
            # each continuation line must start with a space or a tab.  Any
            # trailing spaces or tabs at the end of individual lines of a
            # field value are ignored.
            my $value = $1;

            # trim right
            $value =~ s/\s+$//;

            $value =~ s/#.*$//
              if $flags & DCTRL_COMMENTS_AT_EOL;
            $section->{$last_tag} .= "\n" . $value;
        }
        # None of the above => syntax error
        else {

            die encode_utf8(
                "Unexpected whitespace (line $position). Missing a dot?\n")
              if $line =~ /^\s+$/;

            # Replace non-printables and non-space characters with
            # "_" - just in case.
            $line =~ s/[^[:graph:][:space:]]/_/g;

            die encode_utf8("Cannot parse line $position: $line\n");
        }

    }continue {
        ++$position;
    }

    # pass the last section (if not already done).
    $code->($section, $field_starts)
      if $open_section;

    # Given the API, we cannot use this check to prevent any
    # paragraphs from being emitted to the code argument, so we might
    # as well just do this last.

    die encode_utf8("Cannot find BEGIN PGP SIGNATURE\n.")
      if $signed && !$signature;

    return;
}

=back

=head1 AUTHOR

Originally written Christian Schwarz and many other people.

Moo version by Felix Lechner <felix.lechner@lease-up.com> for Lintian.

=head1 SEE ALSO

lintian(1)

=cut

1;

# Local Variables:
# indent-tabs-mode: nil
# cperl-indent-level: 4
# End:
# vim: syntax=perl sw=4 sts=4 sr et