#!/usr/bin/perl # # pdb2ldif # # Perl program to convert Palm Address Book (AddressDB.pdb) files # to .ldif files, which can be easily imported into an LDAP # directory with the following command: # # cat | ldapadd -w -D # # or even more easy: # # pdb2ldif --pdb= | ldapadd -w -D # # The program takes --pdb= as the PDB file and directs output to . # If you're very crafty, you can probably do a lot more with unix pipes, # but this should be acceptable for most people. # # Requires p5-Palm libraries (available seperately) # Getopt is also required, but should have been part of your Perl distro. # # Y2K issues: Good until 2038, they you'll need a 64-bit processor :-) # # Author: Geoff Silver # Copyright: US Linux Networks, LLC (c) 2001 # License: GNU General Public License (GPL) # Written: 06 February 2001 # Last Modified: 19 April 2001 # Current Version: 1.1 # For Updates: http://uslinux.net/software # # Special Thanks to Jeroen van Pelt for # the list of patches/improvements that have become version 1.1! # # Special Notes: # -Phone fields labels are automatically recognized, so that the phone # numbers and email addresses end up in the correct fields. # -Custom fields are NOT transferred (but you could do it if you've set # up your LDAP server to support them). # -The LDAP dn (distinguished name) is 'mail=$n,dc=$host', where $n is # the number of seconds since the epoch (01/01/1970). # -Phone numbers are translated to XXX-XXX-XXXX format # -Multi-line addresses and notes are converted to a single line, and # line breaks (\n's) are converted to strings ($'s). # -See below for more PDB <---> LDAP <---> English field descriptions. # ###################################################################### $VERSION = '1.1'; use Palm::PDB; use Palm::Address; use Getopt::Long; &GetOptions ( "pdb=s" => \$addressbook, "dc=s" => \$dc, "help" => \$help, "morehelp" => \$morehelp ); if ($morehelp) { print " Author: Geoff Silver Copyright: US Linux Networks, LLC (c) 2001 License: GNU General Public License (GPL) Written: 06 February 2001 Last Modified: 19 April 2001 Current Version: $VERSION For Updates: http://uslinux.net/software Syntax: pdb2ldif [--option1=value1 --option2=value2 ... ] Example: pdb2ldif --pdb=AddressDB.pdb --dc=uslinux Options: --pdb - Specifies the .PDB address book input file --dc - Specifies the LDAP DC to append to the DN --help - Prints the basic help menu --morehelp - Prints this detailed help & caveats Requires: p5-Palm libraries (available seperately) Getopt (should have been part of your Perl distribution) Y2K issues: Good until 2038, they you'll need a 64-bit processor :-) Special Notes: Phone fields are defined as follows: Palm Work Phone is LDAP telephonenumber Palm Home Phone is LDAP homephone Palm Cellular is LDAP mobile Palm Fax is LDAP facsimiletelephonenumber Palm E-mail is LDAP mail Custom fields are NOT transferred (but you could do it if you've set up your LDAP server to support them). The LDAP 'DN' (distinguished name) is 'mail=,dc=$host'. You may change this in the code, but we find this is generally an acceptable unique identifier. Phone numbers are translated to XXX-XXX-XXXX format Multi-line addresses and notes are converted to a single line, and line breaks (\\n's) are converted to strings (\$'s). Formats: LDIF PDB English -------------------- -------------- ----------------------- dn time,dc=\$dc time in milliseconds cn first name + last name sn name last name givenname firstName first name o company company telephonenumber phone[1|2|3|4|5] work phone homephone phone[1|2|3|4|5] home phone facsimiletelephonenumber phone[1|2|3|4|5] fax mobile phone[1|2|3|4|5] cellular mail phone[1|2|3|4|5] e-mail postaladdress address street address l city city st state state postalcode zipCode zip code c country country title title title description note additional notes "; exit(0); } if ($help) { print " Author: Geoff Silver Copyright: US Linux Networks, LLC (c) 2001 License: GNU General Public License (GPL) Written: 06 February 2001 Last Modified: 19 April 2001 Current Version: $VERSION For Updates: http://uslinux.net/software Syntax: pdb2ldif [--option1=value1 --option2=value2 ... ] Example: pdb2ldif --pdb=AddressDB.pdb --dc=uslinux Options: --pdb - Specifies the .PDB address book input file --dc - Specifies the LDAP DC to append to the DN --help - Prints this basic help menu --morehelp - Prints detailed help information/caveats "; exit(0); } if (($addressbook eq "") || ($dc eq "")) { print "Syntax is 'pdb2ldif --pdb=[addressbook.pdb]' --dc=[LDAP dc for dn]\n"; exit(1); } $pdb = new Palm::PDB; $pdb->Load($addressbook); my $categories = $pdb->{appinfo}{categories}; ####################################################################### # LDIF PDB English # -------------------- -------------- ----------------------- # dn time,dc=\$dc # time in milliseconds # cn first name + last name # sn name last name # givenname firstName first name # o company company # telephonenumber phone[1|2|3|4|5] work phone # homephone phone[1|2|3|4|5] home phone # facsimiletelephonenumber phone[1|2|3|4|5] fax # mobile phone[1|2|3|4|5] cellular # mail phone[1|2|3|4|5] e-mail # postaladdress address street address # l city city # st state state # postalcode zipCode zip code # c country country # title title title # description note additional notes # ####################################################################### foreach $record (@{$pdb->{"records"}}) { my $sn = $record->{fields}{name}; my $givenname = $record->{fields}{firstName}; my $rcategory = $record->{category}; my $o = $record->{fields}{company}; my @phone_fields = (phone1,phone2,phone3,phone4,phone5); my $phone_field = ""; if ($rcategory eq 6){ foreach $phone_field (@phone_fields) { if ($record->{phoneLabel}{$phone_field} eq "0" && $record->{fields}{$phone_field} ne "") { $telephonenumber = $record->{fields}{$phone_field}; } if ($record->{phoneLabel}{$phone_field} eq "1" && $record->{fields}{$phone_field} ne "") { $homephone = $record->{fields}{$phone_field}; } if ($record->{phoneLabel}{$phone_field} eq "2" && $record->{fields}{$phone_field} ne "") { $facsimiletelephonenumber = $record->{fields}{$phone_field}; } if ($record->{phoneLabel}{$phone_field} eq "7" && $record->{fields}{$phone_field} ne "") { $mobile= $record->{fields}{$phone_field}; } if ($record->{phoneLabel}{$phone_field} eq "4" && $record->{fields}{$phone_field} ne "") { $mail = $record->{fields}{$phone_field}; } } my $postaladdress = $record->{fields}{address}; my $l = $record->{fields}{city}; my $st = $record->{fields}{state}; my $postalcode = $record->{fields}{zipCode}; my $c = $record->{fields}{country}; my $title = $record->{fields}{title}; my $description = $record->{fields}{note}; # my $custom1 = $record->{fields}{custom1}; # Website # my $custom2 = $record->{fields}{custom2}; # my $custom3 = $record->{fields}{custom3}; # my $custom4 = $record->{fields}{custom4}; sleep 1; # Make sure we don't use the same "fake" unique ID twice. my $date=`date +%s`; chop($date); # my $dn = "mail=$date,dc=$dc"; if ($givenname ne "" && $sn ne "") { $cn = "$givenname $sn"; } else { $cn = ""; } my $dn = "cn=$cn,ou=abook,dc=$dc"; $postaladdress=~s/\n/\$/g; # Translate \n's to $'s $description=~s/\n/\$/g; # Translate \n's to $'s $telephonenumber=~s/\(//g; # Get rid of ('s $telephonenumber=~s/\)/-/g; # Translate )'s to hyphens $telephonenumber=~s/\s/-/g; # Translate spaces to hyphens $telephonenumber=~s/-{2,}/-/g; # Get rid of multiple -'s $facsimiletelephonenumber=~s/\(//g; $facsimiletelephonenumber=~s/\)/-/g; $facsimiletelephonenumber=~s/\s/-/g; $facsimiletelephonenumber=~s/-{2,}/-/g; $mobile=~s/\(//g; $mobile=~s/\)/-/g; $mobile=~s/\s/-/g; $mobile=~s/-{2,}/-/g; $homephone=~s/\(//g; $homephone=~s/\)/-/g; $homephone=~s/\s/-/g; $homephone=~s/-{2,}/-/g; if ($dn ne "") { print "dn: $dn\n"; } if ($cn ne "") { print "cn: $cn\n"; } if ($sn ne "") { print "sn: $sn\n"; } if ($givenname ne "") { print "givenname: $givenname\n"; } if ($o ne "") { print "o: $o\n"; } if ($telephonenumber ne "") { print "telephonenumber: $telephonenumber\n"; } if ($homephone ne "") { print "homephone: $homephone\n"; } if ($facsimiletelephonenumber ne "") { print "facsimiletelephonenumber: $facsimiletelephonenumber\n"; } if ($mobile ne "") { print "mobile: $mobile\n"; } if ($mail ne "") { print "mail: $mail\n"; } if ($postaladdress ne "") { print "postaladdress: $postaladdress\n"; } if ($l ne "") { print "l: $l\n"; } if ($st ne "") { print "st: $st\n"; } if ($postalcode ne ""){ print "postalcode: $postalcode\n"; } if ($c ne "") { print "c: $c\n"; } if ($title ne "") { print "title: $title\n"; } if ($description ne "") { print "description: $description\n"; } print "objectclass: abookPerson\n"; print "\n"; $dn = ""; $cn = ""; $sn = ""; $givenname = ""; $o = ""; $telephonenumber = ""; $homephone = ""; $facsimiletelephonenumber = ""; $mobile = ""; $mail = ""; $postaladdress = ""; $l = ""; $st = ""; $postalcode = ""; $c = ""; $title = ""; $description = ""; } }