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
32 #include "lutil_ldap.h"
34 #include "ldap_defaults.h"
37 static char *binddn = NULL;
38 static struct berval passwd = { 0, NULL };
39 static char *ldapuri = NULL;
40 static char *ldaphost = NULL;
41 static int ldapport = 0;
42 #ifdef HAVE_CYRUS_SASL
43 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
44 static char *sasl_realm = NULL;
45 static char *sasl_authc_id = NULL;
46 static char *sasl_authz_id = NULL;
47 static char *sasl_mech = NULL;
48 static char *sasl_secprops = NULL;
50 static int use_tls = 0;
51 static int ldapadd, replace, not, verbose, contoper, force;
52 static LDAP *ld = NULL;
54 #define LDAPMOD_MAXLINE 4096
56 /* strings found in replog/LDIF entries (mostly lifted from slurpd/slurp.h) */
57 #define T_VERSION_STR "version"
58 #define T_REPLICA_STR "replica"
60 #define T_CHANGETYPESTR "changetype"
61 #define T_ADDCTSTR "add"
62 #define T_MODIFYCTSTR "modify"
63 #define T_DELETECTSTR "delete"
64 #define T_MODRDNCTSTR "modrdn"
65 #define T_MODDNCTSTR "moddn"
66 #define T_RENAMECTSTR "rename"
67 #define T_MODOPADDSTR "add"
68 #define T_MODOPREPLACESTR "replace"
69 #define T_MODOPDELETESTR "delete"
70 #define T_MODSEPSTR "-"
71 #define T_NEWRDNSTR "newrdn"
72 #define T_DELETEOLDRDNSTR "deleteoldrdn"
73 #define T_NEWSUPSTR "newsuperior"
76 static void usage LDAP_P(( const char *prog )) LDAP_GCCATTR((noreturn));
77 static int process_ldif_rec LDAP_P(( char *rbuf, int count ));
78 static void addmodifyop LDAP_P((
79 LDAPMod ***pmodsp, int modop,
81 struct berval *value ));
82 static int domodify LDAP_P((
86 static int dodelete LDAP_P((
88 static int dorename LDAP_P((
93 static char *read_one_record LDAP_P(( FILE *fp ));
96 usage( const char *prog )
99 "Add or modify entries from an LDAP server\n\n"
100 "usage: %s [options]\n"
101 " The list of desired operations are read from stdin or from the file\n"
102 " specified by \"-f file\".\n"
103 "Add or modify options:\n"
104 " -a add values (default%s)\n"
105 " -r replace values\n"
106 " -F force all changes records to be used\n"
109 " -d level set LDAP debugging level to `level'\n"
110 " -D binddn bind DN\n"
111 " -f file read operations from `file'\n"
112 " -h host LDAP server\n"
113 " -H URI LDAP Uniform Resource Indentifier(s)\n"
114 " -I use SASL Interactive mode\n"
115 " -k use Kerberos authentication\n"
116 " -K like -k, but do only step 1 of the Kerberos bind\n"
117 " -M enable Manage DSA IT control (-MM to make critical)\n"
118 " -n show what would be done but don't actually search\n"
119 " -O props SASL security properties\n"
120 " -p port port on LDAP server\n"
121 " -P version procotol version (default: 3)\n"
122 " -Q use SASL Quiet mode\n"
123 " -R realm SASL realm\n"
124 " -U user SASL authentication identity (username)\n"
125 " -v run in verbose mode (diagnostics to standard output)\n"
126 " -w passwd bind passwd (for simple authentication)\n"
127 " -W prompt for bind passwd\n"
128 " -x Simple authentication\n"
129 " -X id SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
130 " -Y mech SASL mechanism\n"
131 " -Z Start TLS request (-ZZ to require successful response)\n"
132 , prog, (strcmp( prog, "ldapadd" ) ? " is to replace" : "") );
134 exit( EXIT_FAILURE );
139 main( int argc, char **argv )
141 char *infile, *rbuf, *start;
143 int rc, i, authmethod, version, want_bindpw, debug, manageDSAit, referrals;
146 if (( prog = strrchr( argv[ 0 ], *LDAP_DIRSEP )) == NULL ) {
152 /* Print usage when no parameters */
153 if( argc < 2 ) usage( prog );
155 ldapadd = ( strcmp( prog, "ldapadd" ) == 0 );
158 not = verbose = want_bindpw = debug = manageDSAit = referrals = 0;
162 while (( i = getopt( argc, argv, "acrf:F" "Cd:D:h:H:IkKMnO:p:P:QRU:vw:WxX:Y:Z" )) != EOF ) {
168 case 'c': /* continuous operation */
171 case 'f': /* read from file */
172 if( infile != NULL ) {
173 fprintf( stderr, "%s: -f previously specified\n" );
176 infile = strdup( optarg );
178 case 'F': /* force all changes records to be used */
181 case 'r': /* default is to replace rather than add values */
190 debug |= atoi( optarg );
192 case 'D': /* bind DN */
193 if( binddn != NULL ) {
194 fprintf( stderr, "%s: -D previously specified\n" );
197 binddn = strdup( optarg );
199 case 'h': /* ldap host */
200 if( ldapuri != NULL ) {
201 fprintf( stderr, "%s: -h incompatible with -H\n" );
204 if( ldaphost != NULL ) {
205 fprintf( stderr, "%s: -h previously specified\n" );
208 ldaphost = strdup( optarg );
210 case 'H': /* ldap URI */
211 if( ldaphost != NULL ) {
212 fprintf( stderr, "%s: -H incompatible with -h\n" );
216 fprintf( stderr, "%s: -H incompatible with -p\n" );
219 if( ldapuri != NULL ) {
220 fprintf( stderr, "%s: -H previously specified\n" );
223 ldapuri = strdup( optarg );
226 #ifdef HAVE_CYRUS_SASL
227 if( version == LDAP_VERSION2 ) {
228 fprintf( stderr, "%s: -I incompatible with version %d\n",
232 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
233 fprintf( stderr, "%s: incompatible previous "
234 "authentication choice\n",
238 authmethod = LDAP_AUTH_SASL;
239 version = LDAP_VERSION3;
240 sasl_flags = LDAP_SASL_INTERACTIVE;
243 fprintf( stderr, "%s: was not compiled with SASL support\n",
245 return( EXIT_FAILURE );
247 case 'k': /* kerberos bind */
248 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
249 if( version > LDAP_VERSION2 ) {
250 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
255 if( authmethod != -1 ) {
256 fprintf( stderr, "%s: -k incompatible with previous "
257 "authentication choice\n", prog );
261 authmethod = LDAP_AUTH_KRBV4;
263 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
267 case 'K': /* kerberos bind, part one only */
268 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
269 if( version > LDAP_VERSION2 ) {
270 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
274 if( authmethod != -1 ) {
275 fprintf( stderr, "%s: incompatible with previous "
276 "authentication choice\n", prog );
280 authmethod = LDAP_AUTH_KRBV41;
282 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
283 return( EXIT_FAILURE );
287 /* enable Manage DSA IT */
288 if( version == LDAP_VERSION2 ) {
289 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
294 version = LDAP_VERSION3;
296 case 'n': /* print deletes, don't actually do them */
300 #ifdef HAVE_CYRUS_SASL
301 if( sasl_secprops != NULL ) {
302 fprintf( stderr, "%s: -O previously specified\n" );
305 if( version == LDAP_VERSION2 ) {
306 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
310 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
311 fprintf( stderr, "%s: incompatible previous "
312 "authentication choice\n", prog );
315 authmethod = LDAP_AUTH_SASL;
316 version = LDAP_VERSION3;
317 sasl_secprops = strdup( optarg );
319 fprintf( stderr, "%s: not compiled with SASL support\n",
321 return( EXIT_FAILURE );
326 fprintf( stderr, "%s: -p previously specified\n" );
329 ldapport = atoi( optarg );
332 switch( atoi(optarg) ) {
334 if( version == LDAP_VERSION3 ) {
335 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
339 version = LDAP_VERSION2;
342 if( version == LDAP_VERSION2 ) {
343 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
347 version = LDAP_VERSION3;
350 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
353 return( EXIT_FAILURE );
356 #ifdef HAVE_CYRUS_SASL
357 if( version == LDAP_VERSION2 ) {
358 fprintf( stderr, "%s: -Q incompatible with version %d\n",
362 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
363 fprintf( stderr, "%s: incompatible previous "
364 "authentication choice\n",
368 authmethod = LDAP_AUTH_SASL;
369 version = LDAP_VERSION3;
370 sasl_flags = LDAP_SASL_QUIET;
373 fprintf( stderr, "%s: not compiled with SASL support\n",
375 return( EXIT_FAILURE );
378 #ifdef HAVE_CYRUS_SASL
379 if( sasl_realm != NULL ) {
380 fprintf( stderr, "%s: -R previously specified\n" );
383 if( version == LDAP_VERSION2 ) {
384 fprintf( stderr, "%s: -R incompatible with version %d\n",
388 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
389 fprintf( stderr, "%s: incompatible previous "
390 "authentication choice\n",
394 authmethod = LDAP_AUTH_SASL;
395 version = LDAP_VERSION3;
396 sasl_realm = strdup( optarg );
398 fprintf( stderr, "%s: not compiled with SASL support\n",
400 return( EXIT_FAILURE );
404 #ifdef HAVE_CYRUS_SASL
405 if( sasl_authc_id != NULL ) {
406 fprintf( stderr, "%s: -U previously specified\n" );
409 if( version == LDAP_VERSION2 ) {
410 fprintf( stderr, "%s: -U incompatible with version %d\n",
414 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
415 fprintf( stderr, "%s: incompatible previous "
416 "authentication choice\n",
420 authmethod = LDAP_AUTH_SASL;
421 version = LDAP_VERSION3;
422 sasl_authc_id = strdup( optarg );
424 fprintf( stderr, "%s: not compiled with SASL support\n",
426 return( EXIT_FAILURE );
429 case 'v': /* verbose mode */
432 case 'w': /* password */
433 passwd.bv_val = strdup( optarg );
437 for( p = optarg; *p == '\0'; p++ ) {
441 passwd.bv_len = strlen( passwd.bv_val );
447 #ifdef HAVE_CYRUS_SASL
448 if( sasl_mech != NULL ) {
449 fprintf( stderr, "%s: -Y previously specified\n" );
452 if( version == LDAP_VERSION2 ) {
453 fprintf( stderr, "%s: -Y incompatible with version %d\n",
457 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
458 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
461 authmethod = LDAP_AUTH_SASL;
462 version = LDAP_VERSION3;
463 sasl_mech = strdup( optarg );
465 fprintf( stderr, "%s: not compiled with SASL support\n",
467 return( EXIT_FAILURE );
471 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
472 fprintf( stderr, "%s: incompatible with previous "
473 "authentication choice\n", prog );
476 authmethod = LDAP_AUTH_SIMPLE;
479 #ifdef HAVE_CYRUS_SASL
480 if( sasl_authz_id != NULL ) {
481 fprintf( stderr, "%s: -X previously specified\n" );
484 if( version == LDAP_VERSION2 ) {
485 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
489 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
490 fprintf( stderr, "%s: -X incompatible with "
491 "authentication choice\n", prog );
494 authmethod = LDAP_AUTH_SASL;
495 version = LDAP_VERSION3;
496 sasl_authz_id = strdup( optarg );
498 fprintf( stderr, "%s: not compiled with SASL support\n",
500 return( EXIT_FAILURE );
505 if( version == LDAP_VERSION2 ) {
506 fprintf( stderr, "%s: -Z incompatible with version %d\n",
510 version = LDAP_VERSION3;
513 fprintf( stderr, "%s: not compiled with TLS support\n",
515 return( EXIT_FAILURE );
519 fprintf( stderr, "%s: unrecongized option -%c\n",
526 version = LDAP_VERSION3;
528 if (authmethod == -1 && version > LDAP_VERSION2) {
529 #ifdef HAVE_CYRUS_SASL
530 authmethod = LDAP_AUTH_SASL;
532 authmethod = LDAP_AUTH_SIMPLE;
536 if ( argc != optind )
539 if ( infile != NULL ) {
540 if (( fp = fopen( infile, "r" )) == NULL ) {
542 return( EXIT_FAILURE );
549 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
550 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
552 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
553 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
559 (void) SIGNAL( SIGPIPE, SIG_IGN );
563 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
565 fprintf( stderr, "ldap_init( %s, %d )\n",
566 ldaphost != NULL ? ldaphost : "<DEFAULT>",
569 ld = ldap_init( ldaphost, ldapport );
573 fprintf( stderr, "ldap_initialize( %s )\n",
574 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
576 (void) ldap_initialize( &ld, ldapuri );
580 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
581 rc, ldap_err2string(rc) );
586 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
587 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
589 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
590 referrals ? "on" : "off" );
595 if (version == -1 ) {
599 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
600 != LDAP_OPT_SUCCESS )
602 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
607 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
609 ldap_perror( ld, "ldap_start_tls" );
610 return( EXIT_FAILURE );
612 fprintf( stderr, "WARNING: could not start TLS\n" );
616 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
617 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
620 if ( authmethod == LDAP_AUTH_SASL ) {
621 #ifdef HAVE_CYRUS_SASL
624 if( sasl_secprops != NULL ) {
625 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
626 (void *) sasl_secprops );
628 if( rc != LDAP_OPT_SUCCESS ) {
630 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
632 return( EXIT_FAILURE );
636 defaults = lutil_sasl_defaults( ld,
643 rc = ldap_sasl_interactive_bind_s( ld, binddn,
644 sasl_mech, NULL, NULL,
645 sasl_flags, lutil_sasl_interact, defaults );
647 if( rc != LDAP_SUCCESS ) {
648 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
649 return( EXIT_FAILURE );
652 fprintf( stderr, "%s: not compiled with SASL support\n",
654 return( EXIT_FAILURE );
658 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
660 ldap_perror( ld, "ldap_bind" );
661 return( EXIT_FAILURE );
672 LDAPControl *ctrls[2];
676 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
677 c.ldctl_value.bv_val = NULL;
678 c.ldctl_value.bv_len = 0;
679 c.ldctl_iscritical = manageDSAit > 1;
681 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
683 if( err != LDAP_OPT_SUCCESS ) {
684 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
685 c.ldctl_iscritical ? "critical " : "" );
686 if( c.ldctl_iscritical ) {
687 exit( EXIT_FAILURE );
693 while (( rc == 0 || contoper ) &&
694 ( rbuf = read_one_record( fp )) != NULL ) {
699 rc = process_ldif_rec( start, count );
702 fprintf( stderr, "ldif_record() = %d\n", rc );
715 process_ldif_rec( char *rbuf, int count )
717 char *line, *dn, *type, *newrdn, *newsup, *p;
718 int rc, linenum, modop, replicaport;
719 int expect_modop, expect_sep, expect_ct, expect_newrdn, expect_newsup;
720 int expect_deleteoldrdn, deleteoldrdn;
721 int saw_replica, use_record, new_entry, delete_entry, got_all;
728 rc = got_all = saw_replica = delete_entry = modop = expect_modop = 0;
729 expect_deleteoldrdn = expect_newrdn = expect_newsup = 0;
730 expect_sep = expect_ct = 0;
736 dn = newrdn = newsup = NULL;
738 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
741 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
747 if ( ldif_parse_line( line, &type, &val.bv_val, &val.bv_len ) < 0 ) {
748 fprintf( stderr, "%s: invalid format (line %d) entry: \"%s\"\n",
749 prog, linenum, dn == NULL ? "" : dn );
750 rc = LDAP_PARAM_ERROR;
755 if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
757 if (( p = strchr( val.bv_val, ':' )) == NULL ) {
761 replicaport = atoi( p );
763 if ( ldaphost != NULL && strcasecmp( val.bv_val, ldaphost ) == 0 &&
764 replicaport == ldapport ) {
767 } else if ( count == 1 && linenum == 1 &&
768 strcasecmp( type, T_VERSION_STR ) == 0 )
770 if( val.bv_len == 0 || atoi(val.bv_val) != 1 ) {
771 fprintf( stderr, "%s: invalid version %s, line %d (ignored)\n",
772 prog, val.bv_val == NULL ? "(null)" : val.bv_val, linenum );
776 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
777 if (( dn = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
779 exit( EXIT_FAILURE );
783 goto end_line; /* skip all lines until we see "dn:" */
788 if ( !use_record && saw_replica ) {
789 printf( "%s: skipping change record for entry: %s\n"
790 "\t(LDAP host/port does not match replica: lines)\n",
794 ber_memfree( val.bv_val );
798 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
799 if ( strcasecmp( val.bv_val, T_MODIFYCTSTR ) == 0 ) {
802 } else if ( strcasecmp( val.bv_val, T_ADDCTSTR ) == 0 ) {
804 } else if ( strcasecmp( val.bv_val, T_MODRDNCTSTR ) == 0
805 || strcasecmp( val.bv_val, T_MODDNCTSTR ) == 0
806 || strcasecmp( val.bv_val, T_RENAMECTSTR ) == 0)
809 } else if ( strcasecmp( val.bv_val, T_DELETECTSTR ) == 0 ) {
810 got_all = delete_entry = 1;
813 "%s: unknown %s \"%s\" (line %d of entry \"%s\")\n",
814 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
815 rc = LDAP_PARAM_ERROR;
818 } else if ( ldapadd ) { /* missing changetype => add */
820 modop = LDAP_MOD_ADD;
822 expect_modop = 1; /* missing changetype => modify */
826 if ( expect_modop ) {
829 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
830 modop = LDAP_MOD_ADD;
832 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
833 modop = LDAP_MOD_REPLACE;
834 addmodifyop( &pmods, modop, val.bv_val, NULL );
836 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
837 modop = LDAP_MOD_DELETE;
838 addmodifyop( &pmods, modop, val.bv_val, NULL );
840 } else { /* no modify op: use default */
841 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
845 if ( expect_newrdn ) {
846 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
847 if (( newrdn = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
849 exit( EXIT_FAILURE );
851 expect_deleteoldrdn = 1;
854 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
855 prog, T_NEWRDNSTR, type, linenum, dn );
856 rc = LDAP_PARAM_ERROR;
858 } else if ( expect_deleteoldrdn ) {
859 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
860 deleteoldrdn = ( *val.bv_val == '0' ) ? 0 : 1;
861 expect_deleteoldrdn = 0;
865 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
866 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
867 rc = LDAP_PARAM_ERROR;
869 } else if ( expect_newsup ) {
870 if ( strcasecmp( type, T_NEWSUPSTR ) == 0 ) {
871 if (( newsup = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
873 exit( EXIT_FAILURE );
877 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
878 prog, T_NEWSUPSTR, type, linenum, dn );
879 rc = LDAP_PARAM_ERROR;
881 } else if ( got_all ) {
883 "%s: extra lines at end (line %d of entry \"%s\")\n",
885 rc = LDAP_PARAM_ERROR;
887 addmodifyop( &pmods, modop, type, val.bv_val == NULL ? NULL : &val );
892 ber_memfree( val.bv_val );
899 if( version && linenum == 1 ) {
904 if ( delete_entry ) {
906 } else if ( newrdn != NULL ) {
907 rc = dorename( dn, newrdn, newsup, deleteoldrdn );
909 rc = domodify( dn, pmods, new_entry );
912 if ( rc == LDAP_SUCCESS ) {
920 if ( newrdn != NULL ) {
923 if ( pmods != NULL ) {
924 ldap_mods_free( pmods, 1 );
942 modop |= LDAP_MOD_BVALUES;
945 if ( pmods != NULL ) {
946 for ( ; pmods[ i ] != NULL; ++i ) {
947 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
948 pmods[ i ]->mod_op == modop )
955 if ( pmods == NULL || pmods[ i ] == NULL ) {
956 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
957 sizeof( LDAPMod * ))) == NULL )
960 exit( EXIT_FAILURE );
964 pmods[ i + 1 ] = NULL;
966 pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod ));
967 if ( pmods[ i ] == NULL ) {
969 exit( EXIT_FAILURE );
972 pmods[ i ]->mod_op = modop;
973 pmods[ i ]->mod_type = ber_strdup( attr );
974 if ( pmods[ i ]->mod_type == NULL ) {
976 exit( EXIT_FAILURE );
982 if ( pmods[ i ]->mod_bvalues != NULL ) {
983 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
988 pmods[ i ]->mod_bvalues = (struct berval **) ber_memrealloc(
989 pmods[ i ]->mod_bvalues, (j + 2) * sizeof( struct berval * ));
990 if ( pmods[ i ]->mod_bvalues == NULL ) {
991 perror( "ber_realloc" );
992 exit( EXIT_FAILURE );
995 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
996 pmods[ i ]->mod_bvalues[ j ] = ber_bvdup( val );
997 if ( pmods[ i ]->mod_bvalues[ j ] == NULL ) {
998 perror( "ber_bvdup" );
999 exit( EXIT_FAILURE );
1011 int i, j, k, notascii, op;
1014 if ( pmods == NULL ) {
1015 fprintf( stderr, "%s: no attributes to change or add (entry=\"%s\")\n",
1017 return( LDAP_PARAM_ERROR );
1021 for ( i = 0; pmods[ i ] != NULL; ++i ) {
1022 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1023 printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
1024 "replace" : op == LDAP_MOD_ADD ?
1025 "add" : "delete", pmods[ i ]->mod_type );
1026 if ( pmods[ i ]->mod_bvalues != NULL ) {
1027 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
1028 bvp = pmods[ i ]->mod_bvalues[ j ];
1030 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
1031 if ( !isascii( bvp->bv_val[ k ] )) {
1037 printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
1039 printf( "\t%s\n", bvp->bv_val );
1047 printf( "%sadding new entry \"%s\"\n", not ? "!" : "", dn );
1049 printf( "%smodifying entry \"%s\"\n", not ? "!" : "", dn );
1054 i = ldap_add_s( ld, dn, pmods );
1056 i = ldap_modify_s( ld, dn, pmods );
1058 if ( i != LDAP_SUCCESS ) {
1059 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
1060 } else if ( verbose ) {
1061 printf( "modify complete\n" );
1079 printf( "%sdeleting entry \"%s\"\n", not ? "!" : "", dn );
1081 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
1082 ldap_perror( ld, "ldap_delete" );
1083 } else if ( verbose ) {
1084 printf( "delete complete" );
1106 printf( "%smodifying rdn of entry \"%s\"\n", not ? "!" : "", dn );
1108 printf( "\tnew RDN: \"%s\" (%skeep existing values)\n",
1109 newrdn, deleteoldrdn ? "do not " : "" );
1112 if (( rc = ldap_rename2_s( ld, dn, newrdn, newsup, deleteoldrdn ))
1114 ldap_perror( ld, "ldap_modrdn" );
1116 printf( "modrdn completed\n" );
1129 read_one_record( FILE *fp )
1131 char *buf, line[ LDAPMOD_MAXLINE ];
1137 while ( fgets( line, sizeof(line), fp ) != NULL ) {
1138 int len = strlen( line );
1148 if ( lcur + len + 1 > lmax ) {
1149 lmax = LDAPMOD_MAXLINE
1150 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
1152 if (( buf = (char *)realloc( buf, lmax )) == NULL ) {
1153 perror( "realloc" );
1154 exit( EXIT_FAILURE );
1158 strcpy( buf + lcur, line );