3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 /* ldapmodify.c - generic program to modify or add entries using LDAP */
12 #include <ac/stdlib.h>
15 #include <ac/signal.h>
16 #include <ac/string.h>
17 #include <ac/unistd.h>
19 #ifdef HAVE_SYS_STAT_H
23 #ifdef HAVE_SYS_FILE_H
33 #include "ldap_defaults.h"
36 static char *binddn = NULL;
37 static struct berval passwd = { 0, NULL};
38 static char *ldaphost = NULL;
39 static int ldapport = 0;
40 #ifdef HAVE_CYRUS_SASL
41 static char *sasl_authc_id = NULL;
42 static char *sasl_authz_id = NULL;
43 static char *sasl_mech = NULL;
44 static int sasl_integrity = 0;
45 static int sasl_privacy = 0;
47 static int use_tls = 0;
48 static int ldapadd, replace, not, verbose, contoper, force;
51 #define LDAPMOD_MAXLINE 4096
53 /* strings found in replog/LDIF entries (mostly lifted from slurpd/slurp.h) */
54 #define T_VERSION_STR "version"
55 #define T_REPLICA_STR "replica"
57 #define T_CHANGETYPESTR "changetype"
58 #define T_ADDCTSTR "add"
59 #define T_MODIFYCTSTR "modify"
60 #define T_DELETECTSTR "delete"
61 #define T_MODRDNCTSTR "modrdn"
62 #define T_MODDNCTSTR "moddn"
63 #define T_RENAMECTSTR "rename"
64 #define T_MODOPADDSTR "add"
65 #define T_MODOPREPLACESTR "replace"
66 #define T_MODOPDELETESTR "delete"
67 #define T_MODSEPSTR "-"
68 #define T_NEWRDNSTR "newrdn"
69 #define T_DELETEOLDRDNSTR "deleteoldrdn"
70 #define T_NEWSUPSTR "newsuperior"
73 static void usage LDAP_P(( const char *prog )) LDAP_GCCATTR((noreturn));
74 static int process_ldif_rec LDAP_P(( char *rbuf, int count ));
75 static void addmodifyop LDAP_P(( LDAPMod ***pmodsp, int modop, char *attr,
76 char *value, int vlen ));
77 static int domodify LDAP_P(( char *dn, LDAPMod **pmods, int newentry ));
78 static int dodelete LDAP_P(( char *dn ));
79 static int domodrdn LDAP_P(( char *dn, char *newrdn, int deleteoldrdn ));
80 static char *read_one_record LDAP_P(( FILE *fp ));
83 usage( const char *prog )
86 "Add or modify entries from an LDAP server\n\n"
87 "usage: %s [options]\n"
88 " The list of desired operations are read from stdin or from the file\n"
89 " specified by \"-f file\".\n"
91 " -a\t\tadd values (default%s)\n"
92 " -b\t\tread values from files (for binary attributes)\n"
93 " -c\t\tcontinuous operation\n"
94 " -d level\tset LDAP debugging level to `level'\n"
96 " -E\t\trequest SASL privacy (-EE to make it critical)\n"
97 " -f file\t\tperform sequence of operations listed in file\n"
98 " -F\t\tforce all changes records to be used\n"
99 " -h host\t\tLDAP server\n"
100 " -I\t\trequest SASL integrity checking (-II to make it\n"
102 " -k\t\tuse Kerberos authentication\n"
103 " -K\t\tlike -k, but do only step 1 of the Kerberos bind\n"
104 " -M\t\tenable Manage DSA IT control (-MM to make it critical)\n"
105 " -n\t\tprint changes, don't actually do them\n"
106 " -p port\t\tport on LDAP server\n"
107 " -r\t\treplace values\n"
108 " -U user\t\tSASL authentication identity (username)\n"
109 " -v\t\tverbose mode\n"
110 " -w passwd\tbind password (for Simple authentication)\n"
111 " -X id\t\tSASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
112 " -Y mech\t\tSASL mechanism\n"
113 " -Z\t\tissue Start TLS request (-ZZ to require successful response)\n"
114 , prog, (strcmp( prog, "ldapadd" ) ? " is to replace" : "") );
115 exit( EXIT_FAILURE );
120 main( int argc, char **argv )
122 char *infile, *rbuf, *start;
124 int rc, i, authmethod, version, want_bindpw, debug, manageDSAit;
127 if (( prog = strrchr( argv[ 0 ], *LDAP_DIRSEP )) == NULL ) {
133 /* Print usage when no parameters */
134 if( argc < 2 ) usage( prog );
136 ldapadd = ( strcmp( prog, "ldapadd" ) == 0 );
139 not = verbose = want_bindpw = debug = manageDSAit = 0;
140 authmethod = LDAP_AUTH_SIMPLE;
143 while (( i = getopt( argc, argv, "acD:d:EFf:h:IKkMnP:p:rtU:vWw:X:Y:Z" )) != EOF ) {
148 case 'c': /* continuous operation */
151 case 'r': /* default is to replace rather than add values */
154 case 'k': /* kerberos bind */
155 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
156 authmethod = LDAP_AUTH_KRBV4;
158 fprintf( stderr, "%s was not compiled with Kerberos support\n", argv[0] );
159 return( EXIT_FAILURE );
162 case 'K': /* kerberos bind, part 1 only */
163 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
164 authmethod = LDAP_AUTH_KRBV41;
166 fprintf( stderr, "%s was not compiled with Kerberos support\n", argv[0] );
167 return( EXIT_FAILURE );
170 case 'F': /* force all changes records to be used */
173 case 'h': /* ldap host */
174 ldaphost = strdup( optarg );
176 case 'D': /* bind DN */
177 binddn = strdup( optarg );
179 case 'w': /* password */
180 passwd.bv_val = strdup( optarg );
184 for( p = optarg; *p == '\0'; p++ ) {
188 passwd.bv_len = strlen( passwd.bv_val );
191 debug |= atoi( optarg );
193 case 'f': /* read from file */
194 infile = strdup( optarg );
197 ldapport = atoi( optarg );
199 case 'n': /* print adds, don't actually do them */
202 case 'v': /* verbose mode */
206 /* enable Manage DSA IT */
213 switch( atoi(optarg) )
216 version = LDAP_VERSION2;
219 version = LDAP_VERSION3;
222 fprintf( stderr, "protocol version should be 2 or 3\n" );
227 #ifdef HAVE_CYRUS_SASL
229 authmethod = LDAP_AUTH_SASL;
231 fprintf( stderr, "%s was not compiled with SASL support\n",
233 return( EXIT_FAILURE );
237 #ifdef HAVE_CYRUS_SASL
239 authmethod = LDAP_AUTH_SASL;
241 fprintf( stderr, "%s was not compiled with SASL support\n",
243 return( EXIT_FAILURE );
247 #ifdef HAVE_CYRUS_SASL
248 if ( strcasecmp( optarg, "any" ) && strcmp( optarg, "*" ) ) {
249 sasl_mech = strdup( optarg );
251 authmethod = LDAP_AUTH_SASL;
253 fprintf( stderr, "%s was not compiled with SASL support\n",
255 return( EXIT_FAILURE );
259 #ifdef HAVE_CYRUS_SASL
260 sasl_authc_id = strdup( optarg );
261 authmethod = LDAP_AUTH_SASL;
263 fprintf( stderr, "%s was not compiled with SASL support\n",
265 return( EXIT_FAILURE );
269 #ifdef HAVE_CYRUS_SASL
270 sasl_authz_id = strdup( optarg );
271 authmethod = LDAP_AUTH_SASL;
273 fprintf( stderr, "%s was not compiled with SASL support\n",
275 return( EXIT_FAILURE );
282 fprintf( stderr, "%s was not compiled with TLS support\n",
284 return( EXIT_FAILURE );
292 if ( argc != optind )
295 if ( ( authmethod == LDAP_AUTH_KRBV4 ) || ( authmethod ==
296 LDAP_AUTH_KRBV41 ) ) {
297 if( version > LDAP_VERSION2 ) {
298 fprintf( stderr, "Kerberos requires LDAPv2\n" );
299 return( EXIT_FAILURE );
301 version = LDAP_VERSION2;
303 else if ( authmethod == LDAP_AUTH_SASL ) {
304 if( version != -1 && version != LDAP_VERSION3 ) {
305 fprintf( stderr, "SASL requires LDAPv3\n" );
306 return( EXIT_FAILURE );
308 version = LDAP_VERSION3;
312 if( version != -1 && version != LDAP_VERSION3 ) {
313 fprintf(stderr, "manage DSA control requires LDAPv3\n");
316 version = LDAP_VERSION3;
320 if( version != -1 && version != LDAP_VERSION3 ) {
321 fprintf(stderr, "Start TLS requires LDAPv3\n");
324 version = LDAP_VERSION3;
327 if ( infile != NULL ) {
328 if (( fp = fopen( infile, "r" )) == NULL ) {
330 return( EXIT_FAILURE );
337 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
338 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
340 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
341 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
347 (void) SIGNAL( SIGPIPE, SIG_IGN );
351 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
352 perror( "ldap_init" );
353 return( EXIT_FAILURE );
356 /* don't chase referrals */
357 ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
359 if (version == -1 ) {
363 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
364 != LDAP_OPT_SUCCESS )
366 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
370 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
372 ldap_perror( ld, "ldap_start_tls" );
373 return( EXIT_FAILURE );
378 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
379 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
382 if ( authmethod == LDAP_AUTH_SASL ) {
383 #ifdef HAVE_CYRUS_SASL
384 int minssf = 0, maxssf = 0;
386 if ( sasl_integrity > 0 )
388 if ( sasl_integrity > 1 )
390 if ( sasl_privacy > 0 )
391 maxssf = 100000; /* Something big value */
392 if ( sasl_privacy > 1 )
395 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MINSSF,
396 (void *)&minssf ) != LDAP_OPT_SUCCESS ) {
397 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MINSSF"
399 return( EXIT_FAILURE );
401 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MAXSSF,
402 (void *)&maxssf ) != LDAP_OPT_SUCCESS ) {
403 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MINSSF"
405 return( EXIT_FAILURE );
408 rc = ldap_negotiated_sasl_bind_s( ld, binddn, sasl_authc_id,
409 sasl_authz_id, sasl_mech,
410 passwd.bv_len ? &passwd : NULL,
413 if( rc != LDAP_SUCCESS ) {
414 ldap_perror( ld, "ldap_negotiated_sasl_bind_s" );
415 return( EXIT_FAILURE );
418 fprintf( stderr, "%s was not compiled with SASL support\n",
420 return( EXIT_FAILURE );
424 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
426 ldap_perror( ld, "ldap_bind" );
427 return( EXIT_FAILURE );
438 LDAPControl *ctrls[2];
442 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
443 c.ldctl_value.bv_val = NULL;
444 c.ldctl_value.bv_len = 0;
445 c.ldctl_iscritical = manageDSAit > 1;
447 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
449 if( err != LDAP_OPT_SUCCESS ) {
450 fprintf( stderr, "Could not set Manage DSA IT Control\n" );
451 if( c.ldctl_iscritical ) {
452 exit( EXIT_FAILURE );
458 while (( rc == 0 || contoper ) &&
459 ( rbuf = read_one_record( fp )) != NULL ) {
464 rc = process_ldif_rec( start, count );
467 fprintf( stderr, "ldif_record() = %d\n", rc );
480 process_ldif_rec( char *rbuf, int count )
482 char *line, *dn, *type, *value, *newrdn, *newsup, *p;
483 int rc, linenum, modop, replicaport;
485 int expect_modop, expect_sep, expect_ct, expect_newrdn, expect_newsup;
486 int expect_deleteoldrdn, deleteoldrdn;
487 int saw_replica, use_record, new_entry, delete_entry, got_all;
493 rc = got_all = saw_replica = delete_entry = modop = expect_modop = 0;
494 expect_deleteoldrdn = expect_newrdn = expect_newsup = 0;
495 expect_sep = expect_ct = 0;
501 dn = newrdn = newsup = NULL;
503 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
506 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
512 if ( ldif_parse_line( line, &type, &value, &vlen ) < 0 ) {
513 fprintf( stderr, "%s: invalid format (line %d) entry: \"%s\"\n",
514 prog, linenum, dn == NULL ? "" : dn );
515 rc = LDAP_PARAM_ERROR;
520 if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
522 if (( p = strchr( value, ':' )) == NULL ) {
526 replicaport = atoi( p );
528 if ( ldaphost != NULL && strcasecmp( value, ldaphost ) == 0 &&
529 replicaport == ldapport ) {
532 } else if ( count == 1 && linenum == 1 &&
533 strcasecmp( type, T_VERSION_STR ) == 0 )
535 if( vlen == 0 || atoi(value) != 1 ) {
536 fprintf( stderr, "%s: invalid version %s, line %d (ignored)\n",
537 prog, value == NULL ? "(null)" : value, linenum );
541 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
542 if (( dn = strdup( value ? value : "" )) == NULL ) {
544 exit( EXIT_FAILURE );
548 goto end_line; /* skip all lines until we see "dn:" */
553 if ( !use_record && saw_replica ) {
554 printf( "%s: skipping change record for entry: %s\n"
555 "\t(LDAP host/port does not match replica: lines)\n",
559 ber_memfree( value );
563 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
564 if ( strcasecmp( value, T_MODIFYCTSTR ) == 0 ) {
567 } else if ( strcasecmp( value, T_ADDCTSTR ) == 0 ) {
569 } else if ( strcasecmp( value, T_MODRDNCTSTR ) == 0
570 || strcasecmp( value, T_MODDNCTSTR ) == 0
571 || strcasecmp( value, T_RENAMECTSTR ) == 0)
574 } else if ( strcasecmp( value, T_DELETECTSTR ) == 0 ) {
575 got_all = delete_entry = 1;
578 "%s: unknown %s \"%s\" (line %d of entry \"%s\")\n",
579 prog, T_CHANGETYPESTR, value, linenum, dn );
580 rc = LDAP_PARAM_ERROR;
583 } else if ( ldapadd ) { /* missing changetype => add */
585 modop = LDAP_MOD_ADD;
587 expect_modop = 1; /* missing changetype => modify */
591 if ( expect_modop ) {
594 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
595 modop = LDAP_MOD_ADD;
597 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
598 modop = LDAP_MOD_REPLACE;
599 addmodifyop( &pmods, modop, value, NULL, 0 );
601 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
602 modop = LDAP_MOD_DELETE;
603 addmodifyop( &pmods, modop, value, NULL, 0 );
605 } else { /* no modify op: use default */
606 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
610 if ( expect_newrdn ) {
611 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
612 if (( newrdn = strdup( value )) == NULL ) {
614 exit( EXIT_FAILURE );
616 expect_deleteoldrdn = 1;
619 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
620 prog, T_NEWRDNSTR, type, linenum, dn );
621 rc = LDAP_PARAM_ERROR;
623 } else if ( expect_deleteoldrdn ) {
624 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
625 deleteoldrdn = ( *value == '0' ) ? 0 : 1;
626 expect_deleteoldrdn = 0;
630 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
631 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
632 rc = LDAP_PARAM_ERROR;
634 } else if ( expect_newsup ) {
635 if ( strcasecmp( type, T_NEWSUPSTR ) == 0 ) {
636 if (( newsup = strdup( value )) == NULL ) {
638 exit( EXIT_FAILURE );
642 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
643 prog, T_NEWSUPSTR, type, linenum, dn );
644 rc = LDAP_PARAM_ERROR;
646 } else if ( got_all ) {
648 "%s: extra lines at end (line %d of entry \"%s\")\n",
650 rc = LDAP_PARAM_ERROR;
652 addmodifyop( &pmods, modop, type, value, vlen );
657 ber_memfree( value );
664 if( version && linenum == 1 ) {
669 if ( delete_entry ) {
671 } else if ( newrdn != NULL ) {
672 rc = domodrdn( dn, newrdn, deleteoldrdn );
674 rc = domodify( dn, pmods, new_entry );
677 if ( rc == LDAP_SUCCESS ) {
685 if ( newrdn != NULL ) {
688 if ( pmods != NULL ) {
689 ldap_mods_free( pmods, 1 );
697 addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
704 modop |= LDAP_MOD_BVALUES;
707 if ( pmods != NULL ) {
708 for ( ; pmods[ i ] != NULL; ++i ) {
709 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
710 pmods[ i ]->mod_op == modop )
717 if ( pmods == NULL || pmods[ i ] == NULL ) {
718 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
719 sizeof( LDAPMod * ))) == NULL )
722 exit( EXIT_FAILURE );
726 pmods[ i + 1 ] = NULL;
728 pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod ));
729 if ( pmods[ i ] == NULL ) {
731 exit( EXIT_FAILURE );
734 pmods[ i ]->mod_op = modop;
735 pmods[ i ]->mod_type = ber_strdup( attr );
736 if ( pmods[ i ]->mod_type == NULL ) {
738 exit( EXIT_FAILURE );
742 if ( value != NULL ) {
744 if ( pmods[ i ]->mod_bvalues != NULL ) {
745 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
750 pmods[ i ]->mod_bvalues = (struct berval **) ber_memrealloc(
751 pmods[ i ]->mod_bvalues, (j + 2) * sizeof( struct berval * ));
752 if ( pmods[ i ]->mod_bvalues == NULL ) {
753 perror( "ber_realloc" );
754 exit( EXIT_FAILURE );
757 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
758 bvp = (struct berval *)ber_memalloc( sizeof( struct berval ));
760 perror( "ber_memalloc" );
761 exit( EXIT_FAILURE );
763 pmods[ i ]->mod_bvalues[ j ] = bvp;
766 bvp->bv_val = (char *)ber_memalloc( vlen + 1 );
767 if ( bvp->bv_val == NULL ) {
769 exit( EXIT_FAILURE );
772 SAFEMEMCPY( bvp->bv_val, value, vlen );
773 bvp->bv_val[ vlen ] = '\0';
779 domodify( char *dn, LDAPMod **pmods, int newentry )
781 int i, j, k, notascii, op;
784 if ( pmods == NULL ) {
785 fprintf( stderr, "%s: no attributes to change or add (entry=\"%s\")\n",
787 return( LDAP_PARAM_ERROR );
791 for ( i = 0; pmods[ i ] != NULL; ++i ) {
792 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
793 printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
794 "replace" : op == LDAP_MOD_ADD ?
795 "add" : "delete", pmods[ i ]->mod_type );
796 if ( pmods[ i ]->mod_bvalues != NULL ) {
797 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
798 bvp = pmods[ i ]->mod_bvalues[ j ];
800 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
801 if ( !isascii( bvp->bv_val[ k ] )) {
807 printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
809 printf( "\t%s\n", bvp->bv_val );
817 printf( "%sadding new entry \"%s\"\n", not ? "!" : "", dn );
819 printf( "%smodifying entry \"%s\"\n", not ? "!" : "", dn );
824 i = ldap_add_s( ld, dn, pmods );
826 i = ldap_modify_s( ld, dn, pmods );
828 if ( i != LDAP_SUCCESS ) {
829 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
830 } else if ( verbose ) {
831 printf( "modify complete\n" );
848 printf( "%sdeleting entry \"%s\"\n", not ? "!" : "", dn );
850 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
851 ldap_perror( ld, "ldap_delete" );
852 } else if ( verbose ) {
853 printf( "delete complete" );
866 domodrdn( char *dn, char *newrdn, int deleteoldrdn )
871 printf( "%smodifying rdn of entry \"%s\"\n", not ? "!" : "", dn );
873 printf( "\tnew RDN: \"%s\" (%skeep existing values)\n",
874 newrdn, deleteoldrdn ? "do not " : "" );
877 if (( rc = ldap_modrdn2_s( ld, dn, newrdn, deleteoldrdn ))
879 ldap_perror( ld, "ldap_modrdn" );
881 printf( "modrdn completed\n" );
894 read_one_record( FILE *fp )
896 char *buf, line[ LDAPMOD_MAXLINE ];
902 while ( fgets( line, sizeof(line), fp ) != NULL ) {
903 int len = strlen( line );
913 if ( lcur + len + 1 > lmax ) {
914 lmax = LDAPMOD_MAXLINE
915 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
917 if (( buf = (char *)realloc( buf, lmax )) == NULL ) {
919 exit( EXIT_FAILURE );
923 strcpy( buf + lcur, line );