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