1#!/usr/bin/perl
2# Nearly complete clone of Umich ldapmodrdn program
3#
4# c.ridd@isode.com
5#
6# $Id: ldapmodrdn,v 1.1 2003/06/09 12:29:42 gbarr Exp $
7#
8# $Log: ldapmodrdn,v $
9# Revision 1.1  2003/06/09 12:29:42  gbarr
10# Depend in MakeMaker to fixup the #! line of installed scripts
11#
12# Revision 1.2  2000/07/30 21:03:50  gbarr
13# *** empty log message ***
14#
15# Revision 1.1  1999/01/11 08:39:12  cjr
16# Initial revision
17#
18#
19
20use strict;
21use Carp;
22use Net::LDAP;
23use vars qw($opt_n $opt_v $opt_r $opt_c $opt_d $opt_D $opt_w $opt_h $opt_p
24	    $opt_3);
25use Getopt::Std;
26
27die "Usage: $0 [options] dn rdn\
28where:\
29    dn          Distinguished names of entry to modify\
30    rdn         New Relative Distinguished name of entry\
31options:\
32    -n          show what would be done but don\'t actually change entries\
33    -v          run in verbose mode (diagnostics to standard output)\
34    -r          remove old RDN values from the entry\
35    -c          continue after any modrdn errors\
36    -d level    set LDAP debugging level to \'level\'\
37    -D binddn   bind dn\
38    -w passwd   bind passwd (for simple authentication)\
39    -h host     ldap server\
40    -p port     port on ldap server\
41    -3          connect using LDAPv3, otherwise use LDAPv2\n" unless @ARGV;
42
43getopts('nvcd:D:w:h:p:3');
44
45$opt_h = 'nameflow.dante.net' unless $opt_h;
46
47my %newargs;
48
49$newargs{port} = $opt_p if $opt_p;
50$newargs{debug} = $opt_d if $opt_d;
51
52dumpargs("new", $opt_h, \%newargs) if ($opt_n || $opt_v);
53my $ldap;
54
55unless ($opt_n) {
56    $ldap = Net::LDAP->new($opt_h, %newargs) or die $@;
57}
58
59#
60# Bind as the desired version, falling back if required to v2
61#
62my %bindargs;
63$bindargs{dn} = $opt_D if $opt_D;
64$bindargs{password} = $opt_w if $opt_w;
65$bindargs{version} = $opt_3 ? 3 : 2;
66
67if ($bindargs{version} == 3) {
68    dumpargs("bind", undef, \%bindargs) if ($opt_n || $opt_v);
69    unless ($opt_n) {
70	$ldap->bind(%bindargs) or $bindargs{version} = 2;
71    }
72}
73
74if ($bindargs{version} == 2) {
75    dumpargs("bind", undef, \%bindargs) if ($opt_n || $opt_v);
76    unless ($opt_n) {
77	$ldap->bind(%bindargs) or die $@;
78    }
79}
80
81my %modargs;
82$modargs{dn} = $ARGV[0];
83$modargs{newrdn} = $ARGV[1];
84$modargs{deleteoldrdn} = $opt_r ? 1 : 0;
85
86if ($opt_n || $opt_v) {
87    dumpargs("moddn",undef,\%modargs);
88}
89unless ($opt_n) {
90    $ldap->moddn(%modargs) or die $@;
91}
92
93if ($opt_n || $opt_v) {
94    print "unbind()\n";
95}
96unless ($opt_n) {
97    $ldap->unbind() or die $@;
98}
99
100sub dumpargs {
101    my ($cmd,$s,$rh) = @_;
102    my @t;
103    push @t, "'$s'" if $s;
104    map {
105	my $value = $$rh{$_};
106	if (ref($value) eq 'ARRAY') {
107	    push @t, "$_ => [" . join(", ", @$value) . "]";
108	} else {
109	    push @t, "$_ => '$value'";
110	}
111    } keys(%$rh);
112    print "$cmd(", join(", ", @t), ")\n";
113}
114