5 # This tool dumps an LDAP database into a directory on your hard disk.
6 # Every LDAP object will become an ldif file, every (sub)tree will become
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2, or (at your option)
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
20 # Maximilian Wilhelm <max@rfc2324.org>
21 # -- Fri, 18 Feb 2005 17:11:14 +0100
27 use Net::LDAP::Util qw( ldap_error_name ldap_error_text );
31 END {ldap_disconnect()}
36 $ldap{server} = "localhost"; # LDAP-Server
37 $ldap{basedn} = ""; # LDAP Base-DN
38 $ldap{admindn} = ""; # LDAP bind DN (if required)
39 $ldap{adminpw} = ""; # password for ldap bind (if required)
40 $ldap{conn} = undef; # Space for LDAP conn-ID
41 $ldap{filter} = ""; # basic search filter
43 # Specify the directory to which the LDAP database will be dumped
44 my $ldif_base_dir = ".";
47 ### End Configuration ###
50 # Connect to the LDAP server
54 $ldap{conn} = Net::LDAP->new($ldap{server} )
55 or die "Unable to connect to server \"$ldap{server}\": $! ";
63 my $result = $ldap{conn}->bind( $ldap{admindn}, password => $ldap{adminpw} );
65 if ( $result->code ) {
66 print( "Could not bind to ldap server! "
67 . ldap_error_name( $result->code ) . ": "
68 . ldap_error_text( $result->code ) . "\n" );
74 # Close LDAP Connection
79 $ldap{conn}->unbind();
87 # Find subtrees / entries
94 $result = $ldap{conn}->search( base => "$base",
95 filter => "(objectClass=*)",
100 if ( $result->count != 0 ) {
101 foreach my $entry ( $result->all_entries ) {
104 get_values( $entry );
106 if ( has_children( $dn ) ) {
117 # Save the object at the given $dn into a file
125 my @dnArr = split(/,/, $dn);
126 my $dnPath = "$ldif_base_dir";
128 # Get the *directory* path
129 for ( my $n = scalar(@dnArr)-1; $n > 0; $n-- ) {
130 $dnPath .= "/$dnArr[$n]";
133 # Check if the directory we will be writing to, does exist
134 # and create if not there.
135 if ( ! -d "$dnPath") {
136 mkpath( ["$dnPath"] ) or die "Cannot create directory $dnPath\n";
139 # Add last element (becomes file name) and add .ldif
140 $dnPath = $dnPath . "/$dnArr[0].ldif";
142 print "Writing: $dnPath\n";
143 my $ldif = Net::LDAP::LDIF->new( "$dnPath" ,"w" ) or die "Cannot open $dnPath\n";
144 $ldif->write_entry( $entry );
147 # Net::LDAP::LDIF->new( \*STDOUT,"w" )->write( $entry );
155 # Check if entry has children (true) or is a leaf (false)
161 my $entry = $ldap{conn}->search( base => "$dn",
162 filter => "(objectClass=*)",
166 return ( $entry->count > 0 );
169 warn "has_children => This should never happen";
175 # Check for server and basedn
176 if ( ! $ldap{server} ) {
177 print STDERR "Error: No LDAP server specified!\n";
181 if ( ! $ldap{basedn} ) {
182 print STDERR "Error: No LDAP Base DN specified!\n";
190 ldap_bind() if ( $ldap{admin_dn} );
192 search_one $ldap{basedn};