]> git.deb.at Git - pkg/abook.git/blob - contrib/ldap-abook/ldap2abook
c86de4f85beca354426dea903f6f1eb2b0e58c0d
[pkg/abook.git] / contrib / ldap-abook / ldap2abook
1 #!/usr/bin/perl
2 #
3 # This is very simple method to get data from LDAP and
4 # converts it to abook readable format.
5 # Script get only first email of any person. This is, probably,
6 # primary address.
7 # If you have better/simpler method - send me, please.
8 #
9 # You MUST have ldap-utils installed on your system to
10 # use this script. Without ldapsearch script does not work!
11 #
12 # Author: Mariusz Balewski <M.Balewski@wp.pl>
13 # 03.06.2004
14 #
15 # GPL licensed
16 # Feel free to send me your comments
17 #
18 # I'm not programmer, so REMEMBER:
19 # YOU USE THIS SCRIPT ON YOUR OWN RISK!!!
20 #
21
22 # Change this section to your local settings
23   # Your LDAP host
24   $HOST="ldaphost.com";
25
26   # Base dn to search
27   $BASEDN="\"-b ou=example,dc=com\"";
28
29   # dn which contains email addresses
30   $FINDDN="mail";
31
32   # for example "-D \"cn=admin,dc=com\"" (if needed)
33   $AUTHDN="";
34
35   # ldap password (if needed), or -w to force script
36   # to password prompting
37   $PASS="";
38
39   # use -x if your ldaphost accept simple authentication
40   # or leave empty
41   $SIMPLEAUTH="";
42
43   # Where you want to put results?
44   # In this example results will be putted to
45   # your home directory, to abookFromLDAP file.
46   # If you wish to use abook with this file, simply run:
47   # abook --datafile $HOME/abookFromLDAP
48   $DESTFILE="$ENV{'HOME'}/abookFromLDAP";
49
50   # If you wish to see communiats in english or polish
51   # link comms.pl-en to comms.pl to english or
52   # comms.pl-pl to polish
53
54 # End of configuration
55 ###############################
56 ###############################
57
58 require 'comms.pl';
59
60 system("ldapsearch -h $HOST $SIMPLEAUTH $AUTHDN $PASS \"$FINDDN=*\" $BASEDN -LLL > /tmp/ldap2abook.tmp");
61
62 $i=0;
63 open(F1,"</tmp/ldap2abook.tmp") || die "$TMPERR";
64 open(F2,">$DESTFILE") || die "$DESTFILEERR";
65  flock(F1,8);
66  flock(F2,8);
67   while(<F1>){
68      if ($_ =~ m/^gecos/g){
69          s/^ //g;
70          s/^gecos: /name=/g;
71          print F2 "[$i]\n";
72          print F2 $_;
73          $i++;
74      }
75      elsif ($_ =~ m/^mail/g){
76          s/^mail: /email=/g;
77          print F2 "$_\n";
78      }
79   }
80   flock(F2,2);
81   flock(F1,2);
82 close(F2);
83 close(F1);
84
85 system("rm /tmp/ldap2abook.tmp");
86
87 print "\n$i ";
88 print "$REPORT\n\n";
89
90 #== END