Debian Codegraph @

sadmin/ldif-duplicate-attrs

raw
1 #!/usr/bin/perl
2 # SPDX-License-Identifier: MIT
3 #
4 # Check for multi-value attributes in LDIF file
5 # written by Jan Engelhardt, 2015
6 #
7 use strict;
8 use warnings;
9 use Getopt::Long;
10
11 our @wl = qw(
12 ACL objectClass member equivalentToMe DirXML-Associations
13 groupMembership securityEquals DirXML-PasswordSyncStatus
14 zarafaSendAsPrivilege zarafaAliases
15 departmentNumber
16 );
17 our $do_color = -t 1;
18
19 &main();
20
21 sub main
22 {
23 &Getopt::Long::Configure(qw(bundling));
24 &GetOptions(
25 "C" => \$do_color,
26 );
27
28 my $attrcount = &parse_ldif();
29 &print_stats($attrcount);
30 }
31
32 sub parse_ldif
33 {
34 my $dn;
35 my $attrcount = {};
36
37 while (defined(my $line = <STDIN>)) {
38 chomp($line);
39 if (substr($line, 0, 1) eq "#") {
40 next;
41 }
42 if ($line =~ /^dn:\s*/) {
43 $dn = $';
44 $attrcount->{$dn} = {};
45 }
46 my($key, $value) = &parse_line($line);
47 if (!defined($key) || &whitelisted($key)) {
48 # Blank lines and other unparsable lines get filtered
49 # here.
50 next;
51 }
52 ++$attrcount->{$dn}->{$key};
53 }
54 close(STDIN);
55 return $attrcount;
56 }
57
58 sub parse_line
59 {
60 return (shift(@_) =~ /^([^:]+):+(?:\s*)?(.*)/);
61 }
62
63 sub whitelisted
64 {
65 my $needle = shift @_;
66 return scalar grep { $_ eq $needle } @wl;
67 }
68
69 sub print_stats
70 {
71 my $attrcount = shift @_;
72 my $dncount = {};
73 my $crapdn;
74 my $globattr = {};
75
76 foreach my $dn (keys %$attrcount) {
77 my $count = 0;
78 my $dh = $attrcount->{$dn};
79
80 foreach my $key (keys %$dh) {
81 if ($dh->{$key} < 2) {
82 next;
83 }
84 $count += $dh->{$key};
85 }
86 $dncount->{$dn} = $count;
87 }
88
89 foreach my $dn (sort { $dncount->{$b} <=> $dncount->{$a} }
90 keys %$dncount)
91 {
92 my $dh = $attrcount->{$dn};
93
94 if ($dncount->{$dn} == 0) {
95 next;
96 }
97
98 if ($do_color) {
99 print "\e[1;31m";
100 }
101 print $dncount->{$dn};
102 if ($do_color) {
103 print "\e[0;31m";
104 }
105 print "\t$dn\n";
106 print "\t";
107 foreach my $key (sort {
108 $dh->{$b} <=> $dh->{$a} || $a cmp $b }
109 keys %$dh)
110 {
111 if ($dh->{$key} < 2) {
112 next;
113 }
114 if ($do_color) {
115 print "\e[32m", substr($key, 0, 1), "\e[0m",
116 substr($key, 1);
117 } else {
118 print $key;
119 }
120 print "(", $attrcount->{$dn}->{$key}, ") ";
121 $globattr->{$dn} += $dh->{$key};
122 }
123 print "\n\n";
124 ++$crapdn;
125 }
126 print $crapdn, " DNs\n";
127 }