#!/usr/bin/perl
#
# ldap2bugzilla - This script updates the "realname" component of Bugzilla 2.x
# profiles against what is found on an LDAP server.  This script uses the perl
# Net::LDAP CPAN module.  This script needs to run on your local Bugzilla
# server.  This script does not create any Bugzilla accounts.  Redistribution
# is intended as a re-useable, cron-friendly "cook book" example.
#
# This script was created February, 2001 by Danny Howard, of Tellme Networks.
# The author can be contacted by e-mailing dannyman@toldme.com.
# 
# Tellme Open Source License
# 
# Permission is hereby granted under the copyrights of Tellme, free of charge,
# to any person obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to the
# following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

# Set your LDAP server, and base DN
my $ldap_serv = "ldap.your.com";
my $ldap_base = "dc=your,dc=com";

# The root of your Bugzilla distribution.
my $bugz_dir = '/usr/local/bugzilla';

# We use this thing to queue up our Bugzilla database updates, otherwise we
# screw up our original SELECT -> FetchSQLData() loop.
my %updates;

use Net::LDAP;

# Bugzilla
chdir $bugz_dir;
require "globals.pl";
ConnectToDatabase();

# LDAP
my $conn = Net::LDAP->new($ldap_serv) or die "LDAP->new: $ldap_serv: $!\n";
$conn->bind;

SendSQL("SELECT userid, login_name, realname FROM profiles;");

while( my ( $buid, $bemail, $bcn ) = (FetchSQLData()) ) {
	# Search for a cn (common name) in LDAP that has mail= the Bugme
	# login_name
	my $ldap = $conn->search( base => $ldap_base, attrs => ['cn'],
	    filter => "(mail=$bemail)" );
	my $entry = $ldap->pop_entry; # Search could contain multiple entries.
	if( $entry && $entry->get_value('cn') ne $bcn ) {
		# Print out if you are doing anything.  Since this script only
		# outputs stuff when it actually changes something, it is
		# pretty cron-friendly. :)
		print "$bcn -> " . $entry->get_value('cn') . "\n";
		$updates{$buid} = SqlQuote($entry->get_value('cn'));
		# Otherwise we screw up FetchSQLData()
	}
}

# And then, run the Bugzilla database updates.
foreach $k (keys %updates) {
	SendSQL("UPDATE profiles SET realname = $updates{$k} WHERE userid = $k;");
}
