1 #!/usr/bin/perl
4 # Copyright (C) 2005 Guillaume Delecourt <guillaume.delecourt@opensides.be>
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 #
20 #
22 use Net::LDAP;
23 use Getopt::Std;
24 use Net::LDAP::Schema;
25 use Net::LDAP::LDIF;
27 # Variables a config
28 $admin="cn=ldapadmin,dc=example,dc=be";
29 $password="";
30 $peopleou="ou=People,dc=example,dc=be";
31 $base="dc=example,dc=be";
32 $scope="one"; # par defaut
33 $dump_file="myldaptree.ldif";
34 $server="localhost";
37 my %Options;
39 my $ok = getopts('?', \%Options);
41 #Verifying if help is needed
42 if ( (!$ok) || (@ARGV < 1) || ($Options{'?'}) ) {
43 &help();
44 }
46 print "We backup the whole tree before every operation\n";
47 &dump();
49 $comm=$ARGV[0];
51 if($comm eq "del" && @ARGV >1 )
52 {
53 print "You asked to delete attribute : ";
54 $i=1;
55 while($ARGV[$i] ne "")
56 {
57 print $ARGV[$i]." ";
58 $i++;
59 }
60 print "\n";
61 $ldap = Net::LDAP->new($server);
62 $ldap->bind($admin,password=>$password);
65 print "ldap connection" .$ldap;
67 $mesg = $ldap->search(filter=>"(objectClass=*)",base=>$peopleou,scope=>$scope);
68 @entries = $mesg->entries;
70 foreach $entry (@entries) {
71 $i=1;
72 print $entry->dn()."\n";
73 while($ARGV[$i] ne "")
74 {
75 if($ARGV[$i] eq "obj"){$obj=1;$i++;next}
76 if($obj==1)
77 {
78 $mesg = $ldap->modify($entry->dn(), delete => {"ObjectClass"=>"$ARGV[$i]"});
79 print "\t objectClass: ".$ARGV[$i];
80 }
81 else
82 {
83 $mesg = $ldap->modify($entry->dn(), delete => [$ARGV[$i]]);
84 print "\t attribut: ".$ARGV[$i];
85 }
86 $obj=0;
87 $i++;
88 }
90 print "\n";
91 }
92 $ldap->unbind;
93 exit(0);
94 }
95 elsif($comm eq "gosa" && @ARGV ==1)
96 {
97 print "Add GOsa attribute for the following users\n";
98 print "---------------------------------------------\n";
99 $ldap = Net::LDAP->new($server);
100 $ldap->bind($admin,password=>$password);
101 $mesg = $ldap->search(filter=>"&(!(objectClass~=gosaAccount))", base=>$peopleou,scope=>$scope);
102 @entries = $mesg->entries;
104 foreach $entry (@entries) {
105 $mesg = $ldap->modify($entry->dn(), add => { "ObjectClass" => "gosaAccount"});
106 $mesg = $ldap->modify($entry->dn(), add => { "ObjectClass" => "organizationalPerson"});
107 $mesg = $ldap->modify($entry->dn(), add => { "ObjectClass" => "Person"});
108 print $entry->dn();
109 print "\n";
110 }
111 $ldap->unbind;
112 exit(0);
113 }
114 elsif($comm eq "modif" && @ARGV >1)
115 {
116 print "Modifications asked\n";
117 print "------------------------\n";
118 $ldap = Net::LDAP->new($server);
119 $ldap->bind($admin,password=>$password);
121 $mesg = $ldap->search(filter=>"(objectClass=*)",base=>$peopleou,scope=>$scope);
122 @entries = $mesg->entries;
123 foreach $entry (@entries) {
124 $mesg = $ldap->modify($entry->dn(), replace => { "$ARGV[1]" => "$ARGV[2]" } );
125 print $entry->dn()."\n\tattribut $ARGV[1] modifié avec la valeur $ARGV[2]\n";
126 }
127 $ldap->unbind;
128 exit(0);
129 }
130 elsif($comm eq "dump" && @ARGV ==1)
131 {
132 &dump();
133 }
134 else
135 {
136 &help();
137 }
139 sub help()
140 {
141 print_banner;
142 print "Usage: $0 [-?] command\n";
143 print "\t-? show this help message\n";
144 print "\tgosa -> add GOsa attributes for the whole the people branch !\n";
145 print "\tdel attribut -> Remove an attribute for the whole people branch !\n";
146 print "\tmodif <attribute> <attribute value> -> to modify the attribute\n";
147 print "\tdump to dump the whole ldap tree\n";
148 exit (1);
149 }
151 sub dump()
152 {
153 $ldap = Net::LDAP->new($server) or die "$@";
154 $ldap->bind($admin,password=>$password);
155 my $ldif = Net::LDAP::LDIF->new($dump_file,'w') ;
156 $mesg = $ldap->search (
157 base => "$base",
158 filter => "(objectclass=*)"
159 );
160 $ldif->write_entry($mesg->entries) ;
161 $ldap->unbind;
162 }