]> git.sur5r.net Git - openldap/blob - clients/tools/ldapmodrdn.c
360bb20c2b20504d988a6bdde773c1b488c9dc5c
[openldap] / clients / tools / ldapmodrdn.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-1999 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/signal.h>
28 #include <ac/string.h>
29 #include <ac/unistd.h>
30
31 #include <lber.h>
32 #include <ldap.h>
33
34 static char     *binddn = NULL;
35 static char     *passwd = NULL;
36 static char     *ldaphost = NULL;
37 static int      ldapport = 0;
38 #ifdef HAVE_CYRUS_SASL
39 static char     *sasl_authc_id = NULL;
40 static char     *sasl_authz_id = NULL;
41 static char     *sasl_mech = NULL;
42 static int      sasl_integrity = 0;
43 static int      sasl_privacy = 0;
44 #endif
45 static int      use_tls = 0;
46 static int      not, verbose, contoper;
47 static LDAP     *ld;
48
49 static int domodrdn(
50     LDAP        *ld,
51     char        *dn,
52     char        *rdn,
53     char        *newSuperior,
54     int         remove );       /* flag: remove old RDN */
55
56 static void
57 usage( const char *s )
58 {
59         fprintf( stderr,
60 "Rename LDAP entries\n\n"
61 "usage: %s [options] [dn rdn]\n"
62 "       dn rdn: If given, rdn will replace the RDN of the entry specified by DN\n"
63 "               If not given, the list of modifications is read from stdin or\n"
64 "               from the file specified by \"-f file\" (see man page).\n"
65 "options:\n"
66 "       -c\t\tcontinuous operation mode (do not stop on errors)\n"
67 "       -d level\tset LDAP debugging level to `level'\n"
68 "       -D binddn\tbind DN\n"
69 "       -E\t\trequest SASL privacy (-EE to make it critical)\n"
70 "       -f file\t\tdo renames listed in `file'\n"
71 "       -h host\t\tLDAP server\n"
72 "       -I\t\trequest SASL integrity checking (-II to make it\n"
73 "               \tcritical)\n"
74 "       -k\t\tuse Kerberos authentication\n"
75 "       -K\t\tlike -k, but do only step 1 of the Kerberos bind\n"
76 "       -M\t\tenable Manage DSA IT control (-MM to make it critical)\n"
77 "       -n\t\tshow what would be done but don't actually do it\n"
78 "       -p port\t\tport on LDAP server\n"
79 "       -P version\tprocotol version (2 or 3)\n"
80 "       -r\t\tremove old RDN\n"
81 "       -s newsuperior\tnew superior entry\n"
82 "       -U user\t\tSASL authentication identity (username)\n"
83 "       -v\t\trun in verbose mode (diagnostics to standard output)\n"
84 "       -w passwd\tbind passwd (for simple authentication)\n"
85 "       -W\t\tprompt for bind passwd\n"
86 "       -X id\t\tSASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
87 "       -Y mech\t\tSASL mechanism\n"
88 "       -Z\t\trequest the use of TLS (-ZZ to make it critical)\n"
89 ,               s );
90
91         exit( EXIT_FAILURE );
92 }
93
94 int
95 main(int argc, char **argv)
96 {
97     char                *myname,*infile, *entrydn = NULL, *rdn = NULL, buf[ 4096 ];
98     FILE                *fp;
99         int             rc, i, remove, havedn, authmethod, version, want_bindpw, debug, manageDSAit;
100     char        *newSuperior=NULL;
101
102     infile = NULL;
103     not = contoper = verbose = remove = want_bindpw = debug = manageDSAit = 0;
104     authmethod = LDAP_AUTH_SIMPLE;
105         version = -1;
106
107     myname = (myname = strrchr(argv[0], '/')) == NULL ? argv[0] : ++myname;
108
109     while (( i = getopt( argc, argv, "cD:d:Ef:h:IKkMnP:p:rs:U:vWw:X:Y:Z" )) != EOF ) {
110         switch( i ) {
111         case 'k':       /* kerberos bind */
112 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
113                 authmethod = LDAP_AUTH_KRBV4;
114 #else
115                 fprintf( stderr, "%s was not compiled with Kerberos support\n", argv[0] );
116                 return( EXIT_FAILURE );
117 #endif
118                 break;
119         case 'K':       /* kerberos bind, part one only */
120 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
121                 authmethod = LDAP_AUTH_KRBV41;
122 #else
123                 fprintf( stderr, "%s was not compiled with Kerberos support\n", argv[0] );
124                 return( EXIT_FAILURE );
125 #endif
126                 break;
127         case 'c':       /* continuous operation mode */
128             ++contoper;
129             break;
130         case 'h':       /* ldap host */
131             ldaphost = strdup( optarg );
132             break;
133         case 'D':       /* bind DN */
134             binddn = strdup( optarg );
135             break;
136         case 's':       /* newSuperior */
137             newSuperior = strdup( optarg );
138             version = LDAP_VERSION3;    /* This option => force V3 */
139             break;
140         case 'w':       /* password */
141             passwd = strdup( optarg );
142                 {
143                         char* p;
144
145                         for( p = optarg; *p == '\0'; p++ ) {
146                                 *p = '*';
147                         }
148                 }
149             break;
150         case 'd':
151             debug |= atoi( optarg );
152             break;
153         case 'f':       /* read from file */
154             infile = strdup( optarg );
155             break;
156         case 'p':
157             ldapport = atoi( optarg );
158             break;
159         case 'n':       /* print adds, don't actually do them */
160             ++not;
161             break;
162         case 'v':       /* verbose mode */
163             verbose++;
164             break;
165         case 'r':       /* remove old RDN */
166             remove++;
167             break;
168         case 'M':
169                 /* enable Manage DSA IT */
170                 manageDSAit++;
171                 break;
172         case 'W':
173                 want_bindpw++;
174                 break;
175         case 'P':
176                 switch( atoi(optarg) )
177                 {
178                 case 2:
179                         version = LDAP_VERSION2;
180                         break;
181                 case 3:
182                         version = LDAP_VERSION3;
183                         break;
184                 default:
185                         fprintf( stderr, "protocol version should be 2 or 3\n" );
186                         usage( argv[0] );
187                         return( EXIT_FAILURE );
188                 }
189                 break;
190         case 'I':
191 #ifdef HAVE_CYRUS_SASL
192                 sasl_integrity++;
193                 authmethod = LDAP_AUTH_SASL;
194 #else
195                 fprintf( stderr, "%s was not compiled with SASL support\n",
196                         argv[0] );
197                 return( EXIT_FAILURE );
198 #endif
199                 break;
200         case 'E':
201 #ifdef HAVE_CYRUS_SASL
202                 sasl_privacy++;
203                 authmethod = LDAP_AUTH_SASL;
204 #else
205                 fprintf( stderr, "%s was not compiled with SASL support\n",
206                         argv[0] );
207                 return( EXIT_FAILURE );
208 #endif
209                 break;
210         case 'Y':
211 #ifdef HAVE_CYRUS_SASL
212                 if ( strcasecmp( optarg, "any" ) && strcmp( optarg, "*" ) ) {
213                         sasl_mech = strdup( optarg );
214                 }
215                 authmethod = LDAP_AUTH_SASL;
216 #else
217                 fprintf( stderr, "%s was not compiled with SASL support\n",
218                         argv[0] );
219                 return( EXIT_FAILURE );
220 #endif
221                 break;
222         case 'U':
223 #ifdef HAVE_CYRUS_SASL
224                 sasl_authc_id = strdup( optarg );
225                 authmethod = LDAP_AUTH_SASL;
226 #else
227                 fprintf( stderr, "%s was not compiled with SASL support\n",
228                         argv[0] );
229                 return( EXIT_FAILURE );
230 #endif
231                 break;
232         case 'X':
233 #ifdef HAVE_CYRUS_SASL
234                 sasl_authz_id = strdup( optarg );
235                 authmethod = LDAP_AUTH_SASL;
236 #else
237                 fprintf( stderr, "%s was not compiled with SASL support\n",
238                         argv[0] );
239                 return( EXIT_FAILURE );
240 #endif
241                 break;
242         case 'Z':
243 #ifdef HAVE_TLS
244                 use_tls++;
245 #else
246                 fprintf( stderr, "%s was not compiled with TLS support\n",
247                         argv[0] );
248                 return( EXIT_FAILURE );
249 #endif
250                 break;
251         default:
252             usage( argv[0] );
253             return( EXIT_FAILURE );
254         }
255     }
256
257         if ( ( authmethod == LDAP_AUTH_KRBV4 ) || ( authmethod ==
258                         LDAP_AUTH_KRBV41 ) ) {
259                 if( version > LDAP_VERSION2 ) {
260                         fprintf( stderr, "Kerberos requires LDAPv2\n" );
261                         return( EXIT_FAILURE );
262                 }
263                 version = LDAP_VERSION2;
264         }
265         else if ( authmethod == LDAP_AUTH_SASL ) {
266                 if( version != -1 || version != LDAP_VERSION3 ) {
267                         fprintf( stderr, "SASL requires LDAPv3\n" );
268                         return( EXIT_FAILURE );
269                 }
270                 version = LDAP_VERSION3;
271         }
272
273         if( manageDSAit ) {
274                 if( version != -1 || version != LDAP_VERSION3 ) {
275                         fprintf(stderr, "manage DSA control requires LDAPv3\n");
276                         return EXIT_FAILURE;
277                 }
278                 version = LDAP_VERSION3;
279         }
280
281         if( use_tls ) {
282                 if( version != -1 || version != LDAP_VERSION3 ) {
283                         fprintf(stderr, "Start TLS requires LDAPv3\n");
284                         return EXIT_FAILURE;
285                 }
286                 version = LDAP_VERSION3;
287         }
288
289     if (newSuperior != NULL) {
290                 if (version == LDAP_VERSION2) {
291                         fprintf( stderr,
292                                 "%s: version conflict!, -s newSuperior requires LDAPv3\n",
293                                 myname);
294                         usage( argv[0] );
295                         return( EXIT_FAILURE );
296                 }
297                 version = LDAP_VERSION3;
298     }
299     
300     havedn = 0;
301     if (argc - optind == 2) {
302         if (( rdn = strdup( argv[argc - 1] )) == NULL ) {
303             perror( "strdup" );
304             return( EXIT_FAILURE );
305         }
306         if (( entrydn = strdup( argv[argc - 2] )) == NULL ) {
307             perror( "strdup" );
308             return( EXIT_FAILURE );
309         }
310         ++havedn;
311     } else if ( argc - optind != 0 ) {
312         fprintf( stderr, "%s: invalid number of arguments, only two allowed\n", myname);
313         usage( argv[0] );
314         return( EXIT_FAILURE );
315     }
316
317     if ( infile != NULL ) {
318         if (( fp = fopen( infile, "r" )) == NULL ) {
319             perror( infile );
320             return( EXIT_FAILURE );
321         }
322     } else {
323         fp = stdin;
324     }
325
326         if ( debug ) {
327                 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
328                         fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
329                 }
330                 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
331                         fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
332                 }
333         }
334
335 #ifdef SIGPIPE
336         (void) SIGNAL( SIGPIPE, SIG_IGN );
337 #endif
338
339     if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
340         perror( "ldap_init" );
341         return( EXIT_FAILURE );
342     }
343
344         /* this seems prudent */
345         {
346                 int deref = LDAP_DEREF_NEVER;
347                 ldap_set_option( ld, LDAP_OPT_DEREF, &deref);
348         }
349         /* don't chase referrals */
350         ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
351
352
353         if (version != -1 &&
354                 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS)
355         {
356                 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
357         }
358
359         if ( use_tls && ldap_start_tls( ld, NULL, NULL ) != LDAP_SUCCESS ) {
360                 if ( use_tls > 1 ) {
361                         ldap_perror( ld, "ldap_start_tls" );
362                         return( EXIT_FAILURE );
363                 }
364         }
365
366         if (want_bindpw)
367                 passwd = getpass("Enter LDAP Password: ");
368
369         if ( authmethod == LDAP_AUTH_SASL ) {
370 #ifdef HAVE_CYRUS_SASL
371                 int     minssf = 0, maxssf = 0;
372
373                 if ( sasl_integrity > 0 )
374                         maxssf = 1;
375                 if ( sasl_integrity > 1 )
376                         minssf = 1;
377                 if ( sasl_privacy > 0 )
378                         maxssf = 100000; /* Something big value */
379                 if ( sasl_privacy > 1 )
380                         minssf = 56;
381                 
382                 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MINSSF,
383                         (void *)&minssf ) != LDAP_OPT_SUCCESS ) {
384                         fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MINSSF"
385                                 "%d\n", minssf);
386                         return( EXIT_FAILURE );
387                 }
388                 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MAXSSF,
389                         (void *)&maxssf ) != LDAP_OPT_SUCCESS ) {
390                         fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MAXSSF"
391                                 "%d\n", maxssf);
392                         return( EXIT_FAILURE );
393                 }
394                 
395                 if ( ldap_negotiated_sasl_bind_s( ld, binddn, sasl_authc_id,
396                                 sasl_authz_id, sasl_mech, NULL, NULL, NULL )
397                                         != LDAP_SUCCESS ) {
398                         ldap_perror( ld, "ldap_sasl_bind" );
399                         return( EXIT_FAILURE );
400                 }
401 #else
402                 fprintf( stderr, "%s was not compiled with SASL support\n",
403                         argv[0] );
404                 return( EXIT_FAILURE );
405 #endif
406         }
407         else {
408                 if ( ldap_bind_s( ld, binddn, passwd, authmethod )
409                                 != LDAP_SUCCESS ) {
410                         ldap_perror( ld, "ldap_bind" );
411                         return( EXIT_FAILURE );
412                 }
413         }
414
415         if ( manageDSAit ) {
416                 int err;
417                 LDAPControl c;
418                 LDAPControl *ctrls[2];
419                 ctrls[0] = &c;
420                 ctrls[1] = NULL;
421
422                 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
423                 c.ldctl_value.bv_val = NULL;
424                 c.ldctl_value.bv_len = 0;
425                 c.ldctl_iscritical = manageDSAit > 1;
426
427                 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
428
429                 if( err != LDAP_OPT_SUCCESS ) {
430                         fprintf( stderr, "Could not set Manage DSA IT Control\n" );
431                         if( c.ldctl_iscritical ) {
432                                 exit( EXIT_FAILURE );
433                         }
434                 }
435         }
436
437     rc = 0;
438     if (havedn)
439         rc = domodrdn( ld, entrydn, rdn, newSuperior, remove );
440     else while ((rc == 0 || contoper) && fgets(buf, sizeof(buf), fp) != NULL) {
441         if ( *buf != '\0' ) {   /* blank lines optional, skip */
442             buf[ strlen( buf ) - 1 ] = '\0';    /* remove nl */
443
444             if ( havedn ) {     /* have DN, get RDN */
445                 if (( rdn = strdup( buf )) == NULL ) {
446                     perror( "strdup" );
447                     return( EXIT_FAILURE );
448                 }
449                 rc = domodrdn(ld, entrydn, rdn, newSuperior, remove );
450                 havedn = 0;
451             } else if ( !havedn ) {     /* don't have DN yet */
452                 if (( entrydn = strdup( buf )) == NULL ) {
453                     perror( "strdup" );
454                     return( EXIT_FAILURE );
455                 }
456                 ++havedn;
457             }
458         }
459     }
460
461     ldap_unbind( ld );
462
463         /* UNREACHABLE */
464         return( rc );
465 }
466
467 static int domodrdn(
468     LDAP        *ld,
469     char        *dn,
470     char        *rdn,
471     char        *newSuperior,
472     int         remove ) /* flag: remove old RDN */
473 {
474     int i;
475
476     if ( verbose ) {
477                 printf( "Renaming \"%s\"\n", dn );
478                 printf( "\tnew rdn=\"%s\" (%s old rdn)\n",
479                         rdn, remove ? "delete" : "keep" );
480                 if( newSuperior != NULL ) {
481                         printf("\tnew parent=\"%s\"\n", newSuperior);
482                 }
483         }
484
485     if ( !not ) {
486         i = ldap_rename2_s( ld, dn, rdn, newSuperior, remove );
487         if ( i != LDAP_SUCCESS ) {
488             ldap_perror( ld, "ldap_rename2_s" );
489         } else if ( verbose ) {
490             printf( "modrdn complete\n" );
491         }
492     } else {
493         i = LDAP_SUCCESS;
494     }
495
496     return( i );
497 }