summaryrefslogtreecommitdiffstats
path: root/contrib/moco-ldap-check.pl
blob: 59f515bf2aaa5c576be861330dea30261104fc51 (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
#!/usr/bin/perl -wT
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is the Bugzilla Bug Tracking System.
#
# The Initial Developer of the Original Code is the Mozilla
# Foundation. Portions created by Mozilla are
# Copyright (C) 2011 Mozilla Foundation. All Rights Reserved.
#
# Contributor(s): Byron Jones <glob@mozilla.com>

use strict;

use lib qw(.);

use Net::LDAP;
use XMLRPC::Lite;
use HTTP::Cookies;
use LWP::UserAgent;
use Term::ReadKey;
$| = 1;

#
#
#

print STDERR <<EOF;
This script cross-checks members of the Bugzilla mozilla-corporation group
with Mozilla's LDAP repository.

To run this script you need:
  - a bugzilla.mozilla.org admin account
  - a Mozilla LDAP account

EOF

my $bugzillaLogin = get_text('bl', 'Bugzilla Login: ');
my $bugzillaPassword = get_text('bp', 'Bugzilla Password: ', 1);
my $ldapLogin = get_text('ll', 'LDAP Login: ');
my $ldapPassword = get_text('lp', 'LDAP Password: ', 1);

sub get_text {
    my($switch, $prompt, $password) = @_;

    for (my $i = 0; $i <= $#ARGV; $i++) {
        if ($ARGV[$i] eq "-$switch") {
            return $ARGV[$i + 1];
        }
    }

    print STDERR $prompt;
    my $response = '';
    ReadMode 4;
    my $ch;
    while(1) {
        1 while (not defined ($ch = ReadKey(-1)));
        exit if $ch eq "\3";
        last if $ch =~ /[\r\n]/;
        if ($ch =~ /[\b\x7F]/) {
            next if $response eq '';
            chop $response;
            print "\b \b";
            next;
        }
        if ($ch eq "\025") {
            my $len = length($response);
            print(("\b" x $len) . (" " x $len) . ("\b" x $len));
            $response = '';
            next;
        }
        next if ord($ch) < 32;
        $response .= $ch;
        print STDERR $password ? '*' : $ch;
    }
    ReadMode 0;
    print STDERR "\n";
    return $response;
}
END {
    ReadMode 0;
}

#
# get list of users in mo-co group
#

my %bugzilla;
{
    my $cookie_jar = HTTP::Cookies->new(file => "cookies.txt", autosave => 1);
    my $proxy = XMLRPC::Lite->proxy(
        'https://bugzilla.mozilla.org/xmlrpc.cgi',
        'cookie_jar' => $cookie_jar);
    my $response;

    print STDERR "Logging in to Bugzilla...\n";
    $response = $proxy->call(
        'User.login',
        {
            login => $bugzillaLogin,
            password => $bugzillaPassword,
            remember => 1,
        }
    );
    if ($response->fault) {
        my ($package, $filename, $line) = caller;
        die $response->faultstring . "\n";
    }

    my $ua = LWP::UserAgent->new();
    $ua->cookie_jar($cookie_jar);
    $response = $ua->get('https://bugzilla.mozilla.org/editusers.cgi?' .
        'action=list&matchvalue=login_name&matchstr=&matchtype=substr&' .
        'grouprestrict=1&groupid=42');
    if (!$response->is_success) {
        die $response->status_line;
    }

    print STDERR "Getting user list from Bugzilla...\n";
    my $content = $response->content;
    while (
        $content =~ m#
            <td([^>]*)>[^<]+
            <a\shref="editusers[^"]+">([^<]+)</a>[^<]+
            </td>[^<]+
            <td[^>]*>([^<]+)</td>
        #gx
    ) {
        my ($class, $email, $name) = ($1, $2, $3);
        next if $class =~ /bz_inactive/;
        $email =~ s/(^\s+|\s+$)//g;
        $email =~ s/&#64;/@/;
        next unless $email =~ /@/;
        $name =~ s/(^\s+|\s+$)//g;
        $bugzilla{lc $email} = $name;
    }
}

#
# build list of current mo-co bugmail accounts
#

my %ldap;
{
    print STDERR "Logging into LDAP...\n";
    my $ldap = Net::LDAP->new('addressbook.mozilla.com',
        scheme => 'ldaps', onerror => 'die') or die "$@";
    $ldap->bind("mail=$ldapLogin,o=com,dc=mozilla", password => $ldapPassword);
    my $result = $ldap->search(
        base => 'o=com,dc=mozilla',
        scope => 'sub',
        filter => '(mail=*)',
        attrs => ['mail', 'bugzillaEmail', 'emailAlias', 'cn', 'employeeType'],
    );
    print STDERR "Getting user list from LDAP...\n";
    foreach my $entry ($result->entries) {
        my ($name, $bugMail, $mail, $type) =
            map { $entry->get_value($_) || '' }
            qw(cn bugzillaEmail mail employeeType);
        next if $type eq 'DISABLED';
        $mail = lc $mail;
        $ldap{$mail}{name} = $name;
        $ldap{$mail}{bugMail} = lc $bugMail;
        $ldap{$mail}{alias} = {};
        foreach my $alias (
            @{$entry->get_value('emailAlias', asref => 1) || []}
        ) {
            $ldap{$mail}{alias}{lc $alias} = 1;
        }
    }
}

#
# cross-check
#

my @invalid;
foreach my $bugzilla (sort keys %bugzilla) {
    # check for matching bugmail entry
    my $exists = 0;
    foreach my $mail (sort keys %ldap) {
        next unless $ldap{$mail}{bugMail} eq $bugzilla;
        $exists = 1;
        last;
    }
    next if $exists;

    # check for matching mail
    $exists = 0;
    foreach my $mail (sort keys %ldap) {
        next unless $mail eq $bugzilla;
        $exists = 1;
        last;
    }
    next if $exists;

    # check for matching email alias
    $exists = 0;
    foreach my $mail (sort keys %ldap) {
        next unless exists $ldap{$mail}{alias}{$bugzilla};
        $exists = 1;
        last;
    }
    next if $exists;

    push @invalid, $bugzilla;
}

my $max_length = 0;
foreach my $email (@invalid) {
    $max_length = length($email) if length($email) > $max_length;
}
foreach my $email (@invalid) {
    printf "%-${max_length}s %s\n", $email, $bugzilla{$email};
}