]> git.deb.at Git - pkg/abook.git/blob - contrib/ldap-abook/ldap2abook
58b793624ce7b5cec11740431d75e17dde712717
[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 # 29.08.2005 Tried to fix insecure tempfile handling (untested)
15 #
16 # GPL licensed
17 # Feel free to send me your comments
18 #
19 # I'm not programmer, so REMEMBER:
20 # YOU USE THIS SCRIPT ON YOUR OWN RISK!!!
21 #
22
23 # Change this section to your local settings
24   # Your LDAP host
25   $HOST="ldaphost.com";
26
27   # Base dn to search
28   $BASEDN="\"-b ou=example,dc=com\"";
29
30   # dn which contains email addresses
31   $FINDDN="mail";
32
33   # for example "-D \"cn=admin,dc=com\"" (if needed)
34   $AUTHDN="";
35
36   # ldap password (if needed), or -w to force script
37   # to password prompting
38   $PASS="";
39
40   # use -x if your ldaphost accept simple authentication
41   # or leave empty
42   $SIMPLEAUTH="";
43
44   # Where you want to put results?
45   # In this example results will be putted to
46   # your home directory, to abookFromLDAP file.
47   # If you wish to use abook with this file, simply run:
48   # abook --datafile $HOME/abookFromLDAP
49   $DESTFILE="$ENV{'HOME'}/abookFromLDAP";
50
51   # If you wish to see communiats in english or polish
52   # link comms.pl-en to comms.pl to english or
53   # comms.pl-pl to polish
54
55 # End of configuration
56 ###############################
57 ###############################
58
59 use File::Temp qw/ :mktemp  /;
60 $file = mktemp("/tmp/tmpfileXXXXXXX");
61
62
63 require 'comms.pl';
64
65 system("ldapsearch -h $HOST $SIMPLEAUTH $AUTHDN $PASS \"$FINDDN=*\" $BASEDN -LLL > $file");
66
67 $i=0;
68 open(F1,"<$file") || die "$TMPERR";
69 open(F2,">$DESTFILE") || die "$DESTFILEERR";
70  flock(F1,8);
71  flock(F2,8);
72   while(<F1>){
73      if ($_ =~ m/^gecos/g){
74          s/^ //g;
75          s/^gecos: /name=/g;
76          print F2 "[$i]\n";
77          print F2 $_;
78          $i++;
79      }
80      elsif ($_ =~ m/^mail/g){
81          s/^mail: /email=/g;
82          print F2 "$_\n";
83      }
84   }
85   flock(F2,2);
86   flock(F1,2);
87 close(F2);
88 close(F1);
89
90 unlink($file);
91
92 print "\n$i ";
93 print "$REPORT\n\n";
94
95 #== END