]> git.sur5r.net Git - openldap/blob - clients/tools/ldapmodrdn.c
Disable anonymous -C for LDAP update tools
[openldap] / clients / tools / ldapmodrdn.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /* ldapmodrdn.c - generic program to modify an entry's RDN using LDAP.
7  *
8  * Support for MODIFYDN REQUEST V3 (newSuperior) by:
9  * 
10  * Copyright 1999, Juan C. Gomez, All rights reserved.
11  * This software is not subject to any license of Silicon Graphics 
12  * Inc. or Purdue University.
13  *
14  * Redistribution and use in source and binary forms are permitted
15  * without restriction or fee of any kind as long as this notice
16  * is preserved.
17  *
18  */
19
20 #include "portable.h"
21
22 #include <stdio.h>
23
24 #include <ac/stdlib.h>
25
26 #include <ac/ctype.h>
27 #include <ac/string.h>
28 #include <ac/unistd.h>
29
30 #include <ldap.h>
31 #include "lutil.h"
32 #include "lutil_ldap.h"
33 #include "ldap_defaults.h"
34
35 #include "common.h"
36
37
38 static char     *newSuperior = NULL;
39 static int   remove_old_RDN = 0;
40
41
42 static int domodrdn(
43     LDAP        *ld,
44     char        *dn,
45     char        *rdn,
46     char        *newSuperior,
47     int         remove );       /* flag: remove old RDN */
48
49 void
50 usage( void )
51 {
52         fprintf( stderr,
53 "Rename LDAP entries\n\n"
54 "usage: %s [options] [dn rdn]\n"
55 "       dn rdn: If given, rdn will replace the RDN of the entry specified by DN\n"
56 "               If not given, the list of modifications is read from stdin or\n"
57 "               from the file specified by \"-f file\" (see man page).\n"
58 "Rename options:\n"
59 "  -r         remove old RDN\n"
60 "  -s newsup  new superior entry\n"
61                  , prog );
62         tool_common_usage();
63         exit( EXIT_FAILURE );
64 }
65
66
67 const char options[] = "rs:"
68         "cd:D:e:f:h:H:IkKMnO:p:P:QR:U:vVw:WxX:y:Y:Z";
69
70 int
71 handle_private_option( int i )
72 {
73         switch ( i ) {
74 #if 0
75                 int crit;
76                 char *control, *cvalue;
77         case 'E': /* modrdn controls */
78                 if( protocol == LDAP_VERSION2 ) {
79                         fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
80                                 prog, version );
81                         exit( EXIT_FAILURE );
82                 }
83
84                 /* should be extended to support comma separated list of
85                  *      [!]key[=value] parameters, e.g.  -E !foo,bar=567
86                  */
87
88                 crit = 0;
89                 cvalue = NULL;
90                 if( optarg[0] == '!' ) {
91                         crit = 1;
92                         optarg++;
93                 }
94
95                 control = strdup( optarg );
96                 if ( (cvalue = strchr( control, '=' )) != NULL ) {
97                         *cvalue++ = '\0';
98                 }
99                 fprintf( stderr, "Invalid modrdn control name: %s\n", control );
100                 usage();
101 #endif
102
103         case 'r':       /* remove old RDN */
104             remove_old_RDN++;
105             break;
106
107         case 's':       /* newSuperior */
108                 if( protocol == LDAP_VERSION2 ) {
109                         fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
110                                 prog, protocol );
111                         exit( EXIT_FAILURE );
112                 }
113             newSuperior = strdup( optarg );
114             protocol = LDAP_VERSION3;
115             break;
116
117         default:
118                 return 0;
119         }
120         return 1;
121 }
122
123
124 int
125 main(int argc, char **argv)
126 {
127     char                *entrydn = NULL, *rdn = NULL, buf[ 4096 ];
128     FILE                *fp;
129     LDAP                *ld;
130         int             rc, retval, havedn;
131
132     prog = lutil_progname( "ldapmodrdn", argc, argv );
133
134         tool_args( argc, argv );
135
136     havedn = 0;
137     if (argc - optind == 2) {
138         if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
139             perror( "strdup" );
140             return( EXIT_FAILURE );
141         }
142         if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
143             perror( "strdup" );
144             return( EXIT_FAILURE );
145         }
146         ++havedn;
147     } else if ( argc - optind != 0 ) {
148         fprintf( stderr, "%s: invalid number of arguments (%d), "
149                 "only two allowed\n", prog, argc-optind );
150         usage();
151     }
152
153     if ( infile != NULL ) {
154         if (( fp = fopen( infile, "r" )) == NULL ) {
155             perror( infile );
156             return( EXIT_FAILURE );
157         }
158     } else {
159         fp = stdin;
160     }
161
162         ld = tool_conn_setup( 0, 0 );
163
164         if ( pw_file || want_bindpw ) {
165                 if ( pw_file ) {
166                         rc = lutil_get_filed_password( pw_file, &passwd );
167                         if( rc ) return EXIT_FAILURE;
168                 } else {
169                         passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
170                         passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
171                 }
172         }
173
174         tool_bind( ld );
175
176         if ( authzid || manageDSAit || noop )
177                 tool_server_controls( ld, NULL, 0 );
178
179     retval = rc = 0;
180     if (havedn)
181         retval = domodrdn( ld, entrydn, rdn, newSuperior, remove_old_RDN );
182     else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
183         if ( *buf != '\0' ) {   /* blank lines optional, skip */
184             buf[ strlen( buf ) - 1 ] = '\0';    /* remove nl */
185
186             if ( havedn ) {     /* have DN, get RDN */
187                 if (( rdn = strdup( buf )) == NULL ) {
188                     perror( "strdup" );
189                     return( EXIT_FAILURE );
190                 }
191                 rc = domodrdn(ld, entrydn, rdn, newSuperior, remove_old_RDN );
192                 if ( rc != 0 )
193                         retval = rc;
194                 havedn = 0;
195             } else if ( !havedn ) {     /* don't have DN yet */
196                 if (( entrydn = strdup( buf )) == NULL ) {
197                     perror( "strdup" );
198                     return( EXIT_FAILURE );
199                 }
200                 ++havedn;
201             }
202         }
203     }
204
205     ldap_unbind( ld );
206
207     return( retval );
208 }
209
210 static int domodrdn(
211     LDAP        *ld,
212     char        *dn,
213     char        *rdn,
214     char        *newSuperior,
215     int         remove ) /* flag: remove old RDN */
216 {
217         int rc, code, id;
218         char *matcheddn=NULL, *text=NULL, **refs=NULL;
219         LDAPMessage *res;
220
221     if ( verbose ) {
222                 printf( "Renaming \"%s\"\n", dn );
223                 printf( "\tnew rdn=\"%s\" (%s old rdn)\n",
224                         rdn, remove ? "delete" : "keep" );
225                 if( newSuperior != NULL ) {
226                         printf("\tnew parent=\"%s\"\n", newSuperior);
227                 }
228         }
229
230         if( not ) return LDAP_SUCCESS;
231
232         rc = ldap_rename( ld, dn, rdn, newSuperior, remove,
233                 NULL, NULL, &id );
234
235         if ( rc != LDAP_SUCCESS ) {
236                 fprintf( stderr, "%s: ldap_rename: %s (%d)\n",
237                         prog, ldap_err2string( rc ), rc );
238                 return rc;
239         }
240
241         rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ALL, NULL, &res );
242         if ( rc < 0 ) {
243                 ldap_perror( ld, "ldapmodrdn: ldap_result" );
244                 return rc;
245         }
246
247         rc = ldap_parse_result( ld, res, &code, &matcheddn, &text, &refs, NULL, 1 );
248
249         if( rc != LDAP_SUCCESS ) {
250                 fprintf( stderr, "%s: ldap_parse_result: %s (%d)\n",
251                         prog, ldap_err2string( rc ), rc );
252                 return rc;
253         }
254
255         if( verbose || code != LDAP_SUCCESS ||
256                 (matcheddn && *matcheddn) || (text && *text) || (refs && *refs) )
257         {
258                 printf( "Rename Result: %s (%d)\n",
259                         ldap_err2string( code ), code );
260
261                 if( text && *text ) {
262                         printf( "Additional info: %s\n", text );
263                 }
264
265                 if( matcheddn && *matcheddn ) {
266                         printf( "Matched DN: %s\n", matcheddn );
267                 }
268
269                 if( refs ) {
270                         int i;
271                         for( i=0; refs[i]; i++ ) {
272                                 printf("Referral: %s\n", refs[i] );
273                         }
274                 }
275         }
276
277         ber_memfree( text );
278         ber_memfree( matcheddn );
279         ber_memvfree( (void **) refs );
280
281         return code;
282 }