Debian Codegraph @

sadmin/ldif-leading-spaces

raw
1 #!/usr/bin/perl
2 # SPDX-License-Identifier: MIT
3 #
4 # Detect questionable values for LDAP attributes
5 # written by Jan Engelhardt, 2015
6 #
7 use strict;
8 use warnings;
9 use Encode;
10 use Getopt::Long;
11 use MIME::Base64;
12
13 our @wl = qw(
14 zarafaUniqueID
15 );
16 our $check_for_spaces = 1;
17
18 &main();
19
20 sub main
21 {
22 my $dn;
23 my $dn_printed = 0;
24 my $dncount = 0;
25
26 while (defined(my $line = <STDIN>)) {
27 chomp($line);
28 if (substr($line, 0, 1) eq "#") {
29 next;
30 }
31 if ($line =~ /^dn:\s*/) {
32 $dn = $';
33 $dn_printed = 0;
34 next;
35 }
36 my($key, $is_encoded, $value) = &parse_line($line);
37 if (!defined($key) || &whitelisted($key)) {
38 next;
39 }
40 if ($is_encoded) {
41 $value = decode_base64($value);
42 }
43 if ($check_for_spaces && $value =~ /^\s|\s$/) {
44 if (!$dn_printed) {
45 print "\ndn: $dn\n";
46 $dn_printed = 1;
47 ++$dncount;
48 }
49 print "$key(", length($key), "): $value\n";
50 }
51 }
52 close(STDIN);
53 print "\n$dncount DNs total\n";
54 }
55
56 sub parse_line
57 {
58 my @r = (shift(@_) =~ /^([^:]+)(:+)(?:\s*)?(.*)/);
59 if (defined($r[1])) {
60 $r[1] = length($r[1]) >= 2 ? 1 : 0;
61 }
62 return @r;
63 }
64
65 sub whitelisted
66 {
67 my $needle = shift @_;
68 return scalar grep { $_ eq $needle } @wl;
69 }