1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/* ldapmodrdn.c - generic program to modify an entry's RDN using LDAP */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <locale.h>
#include <ctype.h>
#include <lber.h>
#include <ldap.h>
#include <locale.h>
#include "ldaptool.h"
static int contoper, remove_oldrdn;
static LDAP *ld;
static int domodrdn( LDAP *ld, char *dn, char *rdn, char *newsuperior,
int remove_oldrdn);
static void options_callback( int option, char *optarg );
static void usage( void )
{
fprintf(stderr, gettext("usage: %s [options] [dn newrdn [newsuperior]]\n"), ldaptool_progname);
fprintf( stderr, gettext("options:\n"));
ldaptool_common_usage( 0 );
fprintf( stderr, gettext(" -c\t\tcontinuous mode\n") );
fprintf( stderr, gettext(" -r\t\tremove old RDN from the entries\n"));
fprintf( stderr, gettext(" -f file\tread changes from `file'\n") );
exit(LDAP_PARAM_ERROR );
}
int
main(int argc, char **argv )
{
char *myname, *entrydn, *rdn, buf[ 4096 ];
int rc, havedn, deref, optind;
char * L_newParent = NULL;
int haverdn = 0;
int L_protoVersion = LDAP_VERSION3;
char *locale = setlocale(LC_ALL, "");
textdomain(TEXT_DOMAIN);
ldaplogconfigf(NULL);
contoper = remove_oldrdn = 0;
if ((myname = strrchr(argv[0], '/')) == NULL)
myname = argv[0];
else
++myname;
optind = ldaptool_process_args( argc, argv, "cr", 0, options_callback);
if ( optind == -1 ) {
usage();
}
if ( ldaptool_fp == NULL ) {
ldaptool_fp = stdin;
}
havedn = 0;
if (argc - optind == 3) /* accept as arguments: dn rdn newsuperior */
{
if (( L_newParent = strdup( argv[argc - 1] )) == NULL )
{
perror( "strdup" );
exit( LDAP_NO_MEMORY );
}
if (( rdn = strdup( argv[argc - 2] )) == NULL )
{
perror( "strdup" );
exit( LDAP_NO_MEMORY );
}
if (( entrydn = strdup( argv[argc - 3] )) == NULL )
{
perror( "strdup" );
exit( LDAP_NO_MEMORY );
}
++havedn;
}
else if (argc - optind == 2) /* accept as arguments: dn rdn */
{
if (( rdn = strdup( argv[argc - 1] )) == NULL )
{
perror( "strdup" );
exit( LDAP_NO_MEMORY );
}
if (( entrydn = strdup( argv[argc - 2] )) == NULL )
{
perror( "strdup" );
exit( 1 );
}
++havedn;
}
else if ( argc - optind != 0 )
{
fprintf( stderr, gettext("%s: invalid number of arguments, only two or three allowed\n"), myname);
usage();
exit( 1 );
}
ld = ldaptool_ldap_init (0);
if ( !ldaptool_not ) {
deref = LDAP_DEREF_NEVER; /* this seems prudent */
ldap_set_option( ld, LDAP_OPT_DEREF, &deref );
}
ldaptool_bind( ld );
rc = 0;
if (havedn)
{
rc = domodrdn(ld, entrydn, rdn, L_newParent, remove_oldrdn);
}
else while ( (rc == 0 || contoper) &&
(fgets(buf, sizeof(buf), ldaptool_fp) != NULL) )
{
/*
* The format of the file is one of the following:
* dn
* rdn
* newsuperior
* <blank lines...>
* OR
* dn
* rdn
* <blank lines...>
* both types of sequences can be found in the file
*/
if ( (strlen(buf) == 1) && (ldaptool_fp == stdin) )
break;
buf[ strlen( buf ) - 1 ] = '\0'; /* remove nl */
if ( *buf != '\0' ) /* blank lines optional, skip */
{
if ( haverdn ) /* first type of sequence */
{
if (( L_newParent = strdup( buf )) == NULL )
{
perror( "strdup" );
exit( LDAP_NO_MEMORY );
}
if ( L_newParent && (L_protoVersion == LDAP_VERSION) )
{
printf( gettext("LDAP Server is V2: <newsuperior> argument is ignored...\n") );
L_newParent = NULL;
}
rc = domodrdn(ld, entrydn, rdn, L_newParent, remove_oldrdn);
haverdn = 0;
}
else if ( havedn ) /* have DN, get RDN */
{
if (( rdn = strdup( buf )) == NULL )
{
perror( "strdup" );
exit( LDAP_NO_MEMORY );
}
havedn = 0;
++haverdn;
}
else if ( !havedn ) /* don't have DN yet */
{
if (( entrydn = strdup( buf )) == NULL)
{
perror( "strdup" );
exit( LDAP_NO_MEMORY );
}
++havedn;
}
}
else
{
printf(gettext("kex: new line %d\n"), rc);
if ( haverdn ) /* second type of sequence */
{
rc = domodrdn(ld, entrydn, rdn, NULL, remove_oldrdn);
haverdn = 0;
}
}
}
if ( (rc == 0 || contoper) && haverdn ) /* second type of sequence */
{
rc = domodrdn(ld, entrydn, rdn, NULL, remove_oldrdn);
haverdn = 0;
}
ldaptool_cleanup( ld );
exit( rc );
}
static void
options_callback( int option, char *optarg )
{
switch( option ) {
case 'c': /* continuous operation mode */
++contoper;
break;
case 'r': /* remove old RDN */
++remove_oldrdn;
break;
default:
usage();
}
}
static int
domodrdn( LDAP *ld, char *dn, char *rdn, char *newsuperior, int remove_oldrdn )
{
int rc = LDAP_SUCCESS;
if ( ldaptool_verbose )
printf( gettext("new RDN: %1$s (%2$skeep existing values)\n"),
rdn, remove_oldrdn ? "do not " : "" );
printf( gettext("%1$srenaming entry %2$s\n"),
ldaptool_not ? "!" : "", dn );
if ( !ldaptool_not )
{
rc = ldap_rename_s( ld, dn, rdn, newsuperior, remove_oldrdn, NULL, NULL );
if ( rc != LDAP_SUCCESS )
fprintf(stderr, gettext("ldap_rename_s: %s\n"), ldap_err2string(rc));
else if ( ldaptool_verbose )
printf( gettext("rename completed\n") );
}
putchar('\n');
return( rc );
}
|