js-home.org, 12.08.2020
 
 

4.4.1 XCmailEin Adressbuch in LDAP
Da mein altes Adressbuch in XCmail existierte, wollte ich das natürlich relativ einfach nach LDAP bringen. Folgendes Script hat das dann gemacht:

xcmail2ldif.php
#!/usr/local/bin/php
<?php

$names=array();

function strconv($s)
{
	$r=utf8_encode($s);
	if ($r!=$s)
		$r=": ".base64_encode($r);
	else
		$r=" ".$r;
	return $r."\r\n";
}
function convert($address)
{
	global $names;
	if (sizeof($address)>0)
	{
		if (strlen($address["LASTNAME"])==0)
			$address["LASTNAME"]=$address["EMAIL"];
		if ($names[$address["LASTNAME"]."_".$address["FIRSTNAME"]]>0)
		{
		}
		else
		{
			echo "dn:".strconv("cn=".trim($address["LASTNAME"]." ".$address["FIRSTNAME"]).",ou=people,o=js-home.org");
			echo "objectclass: top\r\n";
			echo "objectclass: person\r\n";
			echo "objectclass: organizationalPerson\r\n";

			if ($address["LASTNAME"]) echo "sn:".strconv($address["LASTNAME"]);
			echo "cn:".strconv(trim($address["LASTNAME"]." ".$address["FIRSTNAME"]));

			echo "\r\n";

			$names[$address["LASTNAME"]."_".$address["FIRSTNAME"]]=0;
		}
		$names[$address["LASTNAME"]."_".$address["FIRSTNAME"]]++;
		$extra="cn=address_".$names[$address["LASTNAME"]."_".$address["FIRSTNAME"]].",";
		echo "dn:".strconv($extra."cn=".trim($address["LASTNAME"]." ".$address["FIRSTNAME"]).",ou=people,o=js-home.org");
		echo "objectclass: top\r\n";
		echo "objectclass: person\r\n";
		echo "objectclass: organizationalPerson\r\n";
		echo "objectclass: inetOrgPerson\r\n";
		echo "objectclass: mozillaOrgPerson\r\n";

		if ($address["FIRSTNAME"]) echo "givenName:".strconv($address["FIRSTNAME"]);
		if ($address["LASTNAME"]) echo "sn:".strconv($address["LASTNAME"]);
		echo "cn:".strconv(trim($address["LASTNAME"]." ".$address["FIRSTNAME"]));
		echo "mail:".strconv($address["EMAIL"]);
		if ($address["WORKPHONE"]) echo "telephoneNumber: ".$address["WORKPHONE"]."\r\n";
		if ($address["FAX"]) echo "facsimileTelephoneNumber: ".$address["FAX"]."\r\n";

		if ($address["MOBIL"]) echo "mobile: ".$address["MOBIL"]."\r\n";
		if ($address["ADDRESS"]) echo "homePostalAddress:".strconv($address["ADDRESS"]);
		if ($address["CITY"]) echo "mozillaHomeLocalityName:".strconv($address["CITY"]);
		if ($address["STATE"]) echo "mozillaHomeState:".strconv($address["STATE"]);
		if ($address["ZIP"]) echo "mozillaHomePostalCode:".strconv($address["ZIP"]);
		if ($address["COUNTRY"]) echo "mozillaHomeCountryName:".strconv($address["COUNTRY"]);

		if ($address["ADDRESS"]) echo "postalAddress:".strconv($address["ADDRESS"]);
		if ($address["CITY"]) echo "l:".strconv($address["CITY"]);
		if ($address["STATE"]) echo "st:".strconv($address["STATE"]);
		if ($address["ZIP"]) echo "postalCode:".strconv($address["ZIP"]);
		if ($address["COUNTRY"]) echo "c:".strconv($address["COUNTRY"]);

		if ($address["ALIAS"]) echo "mozillaNickname:".strconv($address["ALIAS"]);
		if ($address["URL"]) echo "mozillaWorkUrl:".strconv($address["URL"]);
		if ($address["URL"]) echo "mozillaHomeUrl:".strconv($address["URL"]);

		if ($address["NOTE"]) echo "description:".strconv($address["NOTE"]);
		echo "\r\n";
	}
}

if ($addr=file($argv[1]))
{
	echo "dn: ou=people,o=js-home.org\r\n";
	echo "ou: people\r\n";
	echo "objectClass: top\r\n";
	echo "objectClass: organizationalUnit\r\n";
	echo "\r\n";

	$address=array();
	foreach($addr as $l)
	{
		if (ereg("^ADDRESS.*",$l,$res))
		{
			convert($address);
			$address=array();
		}
		else if (ereg("\.([A-Z]+):(.+)",$l,$res))
		{
			$address[$res[1]]=trim(str_replace("\\n"," ",str_replace(",","_",trim($res[2]))));
		}
	}
}

convert($address);

?>


Es erzeugt auch gleich die ou=people. Als Argument gibt man den Dateinamen des XCmail-Adressbuchs an (~/.XCmail/addressbook) und auf der Standard-Ausgabe bekommt man den ldif-Code, den man dem ldapadd übergibt.

Es kümmert sich um die UTF-8-Sachen, das evtl. nötige base64 und den 2. Doppelpunkt und ersetzt außerdem Leerzeichen durch Unterstriche, wo sie nicht erlaubt sind.

Das Script ist in PHP beschrieben.




All actions are logged, copyright © JS