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 *ldaphost = NULL;
40 static int ldapport = 0;
41 #ifdef HAVE_CYRUS_SASL
42 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
43 static char *sasl_realm = NULL;
44 static char *sasl_authc_id = NULL;
45 static char *sasl_authz_id = NULL;
46 static char *sasl_mech = NULL;
47 static char *sasl_secprops = NULL;
49 static int use_tls = 0;
50 static int ldapadd, replace, not, verbose, contoper, force;
53 #define LDAPMOD_MAXLINE 4096
55 /* strings found in replog/LDIF entries (mostly lifted from slurpd/slurp.h) */
56 #define T_VERSION_STR "version"
57 #define T_REPLICA_STR "replica"
59 #define T_CHANGETYPESTR "changetype"
60 #define T_ADDCTSTR "add"
61 #define T_MODIFYCTSTR "modify"
62 #define T_DELETECTSTR "delete"
63 #define T_MODRDNCTSTR "modrdn"
64 #define T_MODDNCTSTR "moddn"
65 #define T_RENAMECTSTR "rename"
66 #define T_MODOPADDSTR "add"
67 #define T_MODOPREPLACESTR "replace"
68 #define T_MODOPDELETESTR "delete"
69 #define T_MODSEPSTR "-"
70 #define T_NEWRDNSTR "newrdn"
71 #define T_DELETEOLDRDNSTR "deleteoldrdn"
72 #define T_NEWSUPSTR "newsuperior"
75 static void usage LDAP_P(( const char *prog )) LDAP_GCCATTR((noreturn));
76 static int process_ldif_rec LDAP_P(( char *rbuf, int count ));
77 static void addmodifyop LDAP_P((
78 LDAPMod ***pmodsp, int modop,
80 struct berval *value ));
81 static int domodify LDAP_P((
85 static int dodelete LDAP_P((
87 static int dorename LDAP_P((
92 static char *read_one_record LDAP_P(( FILE *fp ));
95 usage( const char *prog )
98 "Add or modify entries from an LDAP server\n\n"
99 "usage: %s [options]\n"
100 " The list of desired operations are read from stdin or from the file\n"
101 " specified by \"-f file\".\n"
102 "Add or modify options:\n"
103 " -a\t\tadd values (default%s)\n"
104 " -r\t\treplace values\n"
105 " -F\t\tforce all changes records to be used\n"
108 " -c\t\tcontinuous operation (ignore errors)\n"
109 " -C\t\tchase referrals\n"
110 " -d level\tset LDAP debugging level to `level'\n"
111 " -D dn\t\tbind DN\n"
112 " -f file\t\tread operations from `file'\n"
113 " -h host\t\tLDAP server\n"
114 " -I\t\tuse SASL Interactive mode\n"
115 " -k\t\tuse Kerberos authentication\n"
116 " -K\t\tlike -k, but do only step 1 of the Kerberos bind\n"
117 " -M\t\tenable Manage DSA IT control (-MM to make it critical)\n"
118 " -n\t\tprint changes, don't actually do them\n"
119 " -O secprops\tSASL security properties\n"
120 " -p port\t\tport on LDAP server\n"
121 " -Q\t\tuse SASL Quiet mode\n"
122 " -R realm\tSASL realm\n"
123 " -U user\t\tSASL authentication identity (username)\n"
124 " -v\t\tverbose mode\n"
125 " -w passwd\tbind password (for Simple authentication)\n"
126 " -X id\t\tSASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
127 " -Y mech\t\tSASL mechanism\n"
128 " -Z\t\tissue Start TLS request (-ZZ to require successful response)\n"
129 , prog, (strcmp( prog, "ldapadd" ) ? " is to replace" : "") );
131 exit( EXIT_FAILURE );
136 main( int argc, char **argv )
138 char *infile, *rbuf, *start;
140 int rc, i, authmethod, version, want_bindpw, debug, manageDSAit, referrals;
143 if (( prog = strrchr( argv[ 0 ], *LDAP_DIRSEP )) == NULL ) {
149 /* Print usage when no parameters */
150 if( argc < 2 ) usage( prog );
152 ldapadd = ( strcmp( prog, "ldapadd" ) == 0 );
155 not = verbose = want_bindpw = debug = manageDSAit = referrals = 0;
159 while (( i = getopt( argc, argv, "acrf:F" "Cd:D:h:IkKMnO:p:P:QRU:vw:WxX:Y:Z" )) != EOF ) {
165 case 'c': /* continuous operation */
168 case 'f': /* read from file */
169 if( infile != NULL ) {
170 fprintf( stderr, "%s: -f previously specified\n" );
173 infile = strdup( optarg );
175 case 'F': /* force all changes records to be used */
178 case 'r': /* default is to replace rather than add values */
187 debug |= atoi( optarg );
189 case 'D': /* bind DN */
190 if( binddn != NULL ) {
191 fprintf( stderr, "%s: -D previously specified\n" );
194 binddn = strdup( optarg );
196 case 'h': /* ldap host */
197 if( ldaphost != NULL ) {
198 fprintf( stderr, "%s: -h previously specified\n" );
201 ldaphost = strdup( optarg );
204 #ifdef HAVE_CYRUS_SASL
205 if( version == LDAP_VERSION2 ) {
206 fprintf( stderr, "%s: -I incompatible with version %d\n",
210 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
211 fprintf( stderr, "%s: incompatible previous "
212 "authentication choice\n",
216 authmethod = LDAP_AUTH_SASL;
217 version = LDAP_VERSION3;
218 sasl_flags = LDAP_SASL_INTERACTIVE;
221 fprintf( stderr, "%s: was not compiled with SASL support\n",
223 return( EXIT_FAILURE );
225 case 'k': /* kerberos bind */
226 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
227 if( version > LDAP_VERSION2 ) {
228 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
233 if( authmethod != -1 ) {
234 fprintf( stderr, "%s: -k incompatible with previous "
235 "authentication choice\n", prog );
239 authmethod = LDAP_AUTH_KRBV4;
241 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
245 case 'K': /* kerberos bind, part one only */
246 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
247 if( version > LDAP_VERSION2 ) {
248 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
252 if( authmethod != -1 ) {
253 fprintf( stderr, "%s: incompatible with previous "
254 "authentication choice\n", prog );
258 authmethod = LDAP_AUTH_KRBV41;
260 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
261 return( EXIT_FAILURE );
265 /* enable Manage DSA IT */
266 if( version == LDAP_VERSION2 ) {
267 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
272 version = LDAP_VERSION3;
274 case 'n': /* print deletes, don't actually do them */
278 #ifdef HAVE_CYRUS_SASL
279 if( sasl_secprops != NULL ) {
280 fprintf( stderr, "%s: -O previously specified\n" );
283 if( version == LDAP_VERSION2 ) {
284 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
288 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
289 fprintf( stderr, "%s: incompatible previous "
290 "authentication choice\n", prog );
293 authmethod = LDAP_AUTH_SASL;
294 version = LDAP_VERSION3;
295 sasl_secprops = strdup( optarg );
297 fprintf( stderr, "%s: not compiled with SASL support\n",
299 return( EXIT_FAILURE );
304 fprintf( stderr, "%s: -p previously specified\n" );
307 ldapport = atoi( optarg );
310 switch( atoi(optarg) ) {
312 if( version == LDAP_VERSION3 ) {
313 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
317 version = LDAP_VERSION2;
320 if( version == LDAP_VERSION2 ) {
321 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
325 version = LDAP_VERSION3;
328 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
331 return( EXIT_FAILURE );
334 #ifdef HAVE_CYRUS_SASL
335 if( version == LDAP_VERSION2 ) {
336 fprintf( stderr, "%s: -Q incompatible with version %d\n",
340 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
341 fprintf( stderr, "%s: incompatible previous "
342 "authentication choice\n",
346 authmethod = LDAP_AUTH_SASL;
347 version = LDAP_VERSION3;
348 sasl_flags = LDAP_SASL_QUIET;
351 fprintf( stderr, "%s: was not compiled with SASL support\n",
353 return( EXIT_FAILURE );
356 #ifdef HAVE_CYRUS_SASL
357 if( sasl_realm != NULL ) {
358 fprintf( stderr, "%s: -R previously specified\n" );
361 if( version == LDAP_VERSION2 ) {
362 fprintf( stderr, "%s: -R incompatible with version %d\n",
366 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
367 fprintf( stderr, "%s: incompatible previous "
368 "authentication choice\n",
372 authmethod = LDAP_AUTH_SASL;
373 version = LDAP_VERSION3;
374 sasl_realm = strdup( optarg );
376 fprintf( stderr, "%s: was not compiled with SASL support\n",
378 return( EXIT_FAILURE );
382 #ifdef HAVE_CYRUS_SASL
383 if( sasl_authc_id != NULL ) {
384 fprintf( stderr, "%s: -U previously specified\n" );
387 if( version == LDAP_VERSION2 ) {
388 fprintf( stderr, "%s: -U incompatible with version %d\n",
392 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
393 fprintf( stderr, "%s: incompatible previous "
394 "authentication choice\n",
398 authmethod = LDAP_AUTH_SASL;
399 version = LDAP_VERSION3;
400 sasl_authc_id = strdup( optarg );
402 fprintf( stderr, "%s: was not compiled with SASL support\n",
404 return( EXIT_FAILURE );
407 case 'v': /* verbose mode */
410 case 'w': /* password */
411 passwd.bv_val = strdup( optarg );
415 for( p = optarg; *p == '\0'; p++ ) {
419 passwd.bv_len = strlen( passwd.bv_val );
425 #ifdef HAVE_CYRUS_SASL
426 if( sasl_mech != NULL ) {
427 fprintf( stderr, "%s: -Y previously specified\n" );
430 if( version == LDAP_VERSION2 ) {
431 fprintf( stderr, "%s: -Y incompatible with version %d\n",
435 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
436 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
439 authmethod = LDAP_AUTH_SASL;
440 version = LDAP_VERSION3;
441 sasl_mech = strdup( optarg );
443 fprintf( stderr, "%s: was not compiled with SASL support\n",
445 return( EXIT_FAILURE );
449 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
450 fprintf( stderr, "%s: incompatible with previous "
451 "authentication choice\n", prog );
454 authmethod = LDAP_AUTH_SIMPLE;
457 #ifdef HAVE_CYRUS_SASL
458 if( sasl_authz_id != NULL ) {
459 fprintf( stderr, "%s: -X previously specified\n" );
462 if( version == LDAP_VERSION2 ) {
463 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
467 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
468 fprintf( stderr, "%s: -X incompatible with "
469 "authentication choice\n", prog );
472 authmethod = LDAP_AUTH_SASL;
473 version = LDAP_VERSION3;
474 sasl_authz_id = strdup( optarg );
476 fprintf( stderr, "%s: not compiled with SASL support\n",
478 return( EXIT_FAILURE );
483 if( version == LDAP_VERSION2 ) {
484 fprintf( stderr, "%s -Z incompatible with version %d\n",
488 version = LDAP_VERSION3;
491 fprintf( stderr, "%s: not compiled with TLS support\n",
493 return( EXIT_FAILURE );
497 fprintf( stderr, "%s: unrecongized option -%c\n",
504 version = LDAP_VERSION3;
506 if (authmethod == -1 && version > LDAP_VERSION2) {
507 #ifdef HAVE_CYRUS_SASL
508 authmethod = LDAP_AUTH_SASL;
510 authmethod = LDAP_AUTH_SIMPLE;
514 if ( argc != optind )
517 if ( infile != NULL ) {
518 if (( fp = fopen( infile, "r" )) == NULL ) {
520 return( EXIT_FAILURE );
527 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
528 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
530 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
531 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
537 (void) SIGNAL( SIGPIPE, SIG_IGN );
541 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
542 perror( "ldap_init" );
543 return( EXIT_FAILURE );
547 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
548 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
550 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
551 referrals ? "on" : "off" );
556 if (version == -1 ) {
560 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
561 != LDAP_OPT_SUCCESS )
563 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
568 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
570 ldap_perror( ld, "ldap_start_tls" );
571 return( EXIT_FAILURE );
573 fprintf( stderr, "WARNING: could not start TLS\n" );
577 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
578 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
581 if ( authmethod == LDAP_AUTH_SASL ) {
582 #ifdef HAVE_CYRUS_SASL
585 if( sasl_secprops != NULL ) {
586 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
587 (void *) sasl_secprops );
589 if( rc != LDAP_OPT_SUCCESS ) {
591 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
593 return( EXIT_FAILURE );
597 defaults = lutil_sasl_defaults( ld,
604 rc = ldap_sasl_interactive_bind_s( ld, binddn,
605 sasl_mech, NULL, NULL,
606 sasl_flags, lutil_sasl_interact, defaults );
608 if( rc != LDAP_SUCCESS ) {
609 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
610 return( EXIT_FAILURE );
613 fprintf( stderr, "%s was not compiled with SASL support\n",
615 return( EXIT_FAILURE );
619 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
621 ldap_perror( ld, "ldap_bind" );
622 return( EXIT_FAILURE );
633 LDAPControl *ctrls[2];
637 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
638 c.ldctl_value.bv_val = NULL;
639 c.ldctl_value.bv_len = 0;
640 c.ldctl_iscritical = manageDSAit > 1;
642 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
644 if( err != LDAP_OPT_SUCCESS ) {
645 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
646 c.ldctl_iscritical ? "critical " : "" );
647 if( c.ldctl_iscritical ) {
648 exit( EXIT_FAILURE );
654 while (( rc == 0 || contoper ) &&
655 ( rbuf = read_one_record( fp )) != NULL ) {
660 rc = process_ldif_rec( start, count );
663 fprintf( stderr, "ldif_record() = %d\n", rc );
676 process_ldif_rec( char *rbuf, int count )
678 char *line, *dn, *type, *newrdn, *newsup, *p;
679 int rc, linenum, modop, replicaport;
680 int expect_modop, expect_sep, expect_ct, expect_newrdn, expect_newsup;
681 int expect_deleteoldrdn, deleteoldrdn;
682 int saw_replica, use_record, new_entry, delete_entry, got_all;
689 rc = got_all = saw_replica = delete_entry = modop = expect_modop = 0;
690 expect_deleteoldrdn = expect_newrdn = expect_newsup = 0;
691 expect_sep = expect_ct = 0;
697 dn = newrdn = newsup = NULL;
699 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
702 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
708 if ( ldif_parse_line( line, &type, &val.bv_val, &val.bv_len ) < 0 ) {
709 fprintf( stderr, "%s: invalid format (line %d) entry: \"%s\"\n",
710 prog, linenum, dn == NULL ? "" : dn );
711 rc = LDAP_PARAM_ERROR;
716 if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
718 if (( p = strchr( val.bv_val, ':' )) == NULL ) {
722 replicaport = atoi( p );
724 if ( ldaphost != NULL && strcasecmp( val.bv_val, ldaphost ) == 0 &&
725 replicaport == ldapport ) {
728 } else if ( count == 1 && linenum == 1 &&
729 strcasecmp( type, T_VERSION_STR ) == 0 )
731 if( val.bv_len == 0 || atoi(val.bv_val) != 1 ) {
732 fprintf( stderr, "%s: invalid version %s, line %d (ignored)\n",
733 prog, val.bv_val == NULL ? "(null)" : val.bv_val, linenum );
737 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
738 if (( dn = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
740 exit( EXIT_FAILURE );
744 goto end_line; /* skip all lines until we see "dn:" */
749 if ( !use_record && saw_replica ) {
750 printf( "%s: skipping change record for entry: %s\n"
751 "\t(LDAP host/port does not match replica: lines)\n",
755 ber_memfree( val.bv_val );
759 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
760 if ( strcasecmp( val.bv_val, T_MODIFYCTSTR ) == 0 ) {
763 } else if ( strcasecmp( val.bv_val, T_ADDCTSTR ) == 0 ) {
765 } else if ( strcasecmp( val.bv_val, T_MODRDNCTSTR ) == 0
766 || strcasecmp( val.bv_val, T_MODDNCTSTR ) == 0
767 || strcasecmp( val.bv_val, T_RENAMECTSTR ) == 0)
770 } else if ( strcasecmp( val.bv_val, T_DELETECTSTR ) == 0 ) {
771 got_all = delete_entry = 1;
774 "%s: unknown %s \"%s\" (line %d of entry \"%s\")\n",
775 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
776 rc = LDAP_PARAM_ERROR;
779 } else if ( ldapadd ) { /* missing changetype => add */
781 modop = LDAP_MOD_ADD;
783 expect_modop = 1; /* missing changetype => modify */
787 if ( expect_modop ) {
790 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
791 modop = LDAP_MOD_ADD;
793 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
794 modop = LDAP_MOD_REPLACE;
795 addmodifyop( &pmods, modop, val.bv_val, NULL );
797 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
798 modop = LDAP_MOD_DELETE;
799 addmodifyop( &pmods, modop, val.bv_val, NULL );
801 } else { /* no modify op: use default */
802 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
806 if ( expect_newrdn ) {
807 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
808 if (( newrdn = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
810 exit( EXIT_FAILURE );
812 expect_deleteoldrdn = 1;
815 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
816 prog, T_NEWRDNSTR, type, linenum, dn );
817 rc = LDAP_PARAM_ERROR;
819 } else if ( expect_deleteoldrdn ) {
820 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
821 deleteoldrdn = ( *val.bv_val == '0' ) ? 0 : 1;
822 expect_deleteoldrdn = 0;
826 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
827 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
828 rc = LDAP_PARAM_ERROR;
830 } else if ( expect_newsup ) {
831 if ( strcasecmp( type, T_NEWSUPSTR ) == 0 ) {
832 if (( newsup = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
834 exit( EXIT_FAILURE );
838 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
839 prog, T_NEWSUPSTR, type, linenum, dn );
840 rc = LDAP_PARAM_ERROR;
842 } else if ( got_all ) {
844 "%s: extra lines at end (line %d of entry \"%s\")\n",
846 rc = LDAP_PARAM_ERROR;
848 addmodifyop( &pmods, modop, type, val.bv_val == NULL ? NULL : &val );
853 ber_memfree( val.bv_val );
860 if( version && linenum == 1 ) {
865 if ( delete_entry ) {
867 } else if ( newrdn != NULL ) {
868 rc = dorename( dn, newrdn, newsup, deleteoldrdn );
870 rc = domodify( dn, pmods, new_entry );
873 if ( rc == LDAP_SUCCESS ) {
881 if ( newrdn != NULL ) {
884 if ( pmods != NULL ) {
885 ldap_mods_free( pmods, 1 );
903 modop |= LDAP_MOD_BVALUES;
906 if ( pmods != NULL ) {
907 for ( ; pmods[ i ] != NULL; ++i ) {
908 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
909 pmods[ i ]->mod_op == modop )
916 if ( pmods == NULL || pmods[ i ] == NULL ) {
917 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
918 sizeof( LDAPMod * ))) == NULL )
921 exit( EXIT_FAILURE );
925 pmods[ i + 1 ] = NULL;
927 pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod ));
928 if ( pmods[ i ] == NULL ) {
930 exit( EXIT_FAILURE );
933 pmods[ i ]->mod_op = modop;
934 pmods[ i ]->mod_type = ber_strdup( attr );
935 if ( pmods[ i ]->mod_type == NULL ) {
937 exit( EXIT_FAILURE );
943 if ( pmods[ i ]->mod_bvalues != NULL ) {
944 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
949 pmods[ i ]->mod_bvalues = (struct berval **) ber_memrealloc(
950 pmods[ i ]->mod_bvalues, (j + 2) * sizeof( struct berval * ));
951 if ( pmods[ i ]->mod_bvalues == NULL ) {
952 perror( "ber_realloc" );
953 exit( EXIT_FAILURE );
956 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
957 pmods[ i ]->mod_bvalues[ j ] = ber_bvdup( val );
958 if ( pmods[ i ]->mod_bvalues[ j ] == NULL ) {
959 perror( "ber_bvdup" );
960 exit( EXIT_FAILURE );
972 int i, j, k, notascii, op;
975 if ( pmods == NULL ) {
976 fprintf( stderr, "%s: no attributes to change or add (entry=\"%s\")\n",
978 return( LDAP_PARAM_ERROR );
982 for ( i = 0; pmods[ i ] != NULL; ++i ) {
983 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
984 printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
985 "replace" : op == LDAP_MOD_ADD ?
986 "add" : "delete", pmods[ i ]->mod_type );
987 if ( pmods[ i ]->mod_bvalues != NULL ) {
988 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
989 bvp = pmods[ i ]->mod_bvalues[ j ];
991 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
992 if ( !isascii( bvp->bv_val[ k ] )) {
998 printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
1000 printf( "\t%s\n", bvp->bv_val );
1008 printf( "%sadding new entry \"%s\"\n", not ? "!" : "", dn );
1010 printf( "%smodifying entry \"%s\"\n", not ? "!" : "", dn );
1015 i = ldap_add_s( ld, dn, pmods );
1017 i = ldap_modify_s( ld, dn, pmods );
1019 if ( i != LDAP_SUCCESS ) {
1020 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
1021 } else if ( verbose ) {
1022 printf( "modify complete\n" );
1040 printf( "%sdeleting entry \"%s\"\n", not ? "!" : "", dn );
1042 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
1043 ldap_perror( ld, "ldap_delete" );
1044 } else if ( verbose ) {
1045 printf( "delete complete" );
1067 printf( "%smodifying rdn of entry \"%s\"\n", not ? "!" : "", dn );
1069 printf( "\tnew RDN: \"%s\" (%skeep existing values)\n",
1070 newrdn, deleteoldrdn ? "do not " : "" );
1073 if (( rc = ldap_rename2_s( ld, dn, newrdn, newsup, deleteoldrdn ))
1075 ldap_perror( ld, "ldap_modrdn" );
1077 printf( "modrdn completed\n" );
1090 read_one_record( FILE *fp )
1092 char *buf, line[ LDAPMOD_MAXLINE ];
1098 while ( fgets( line, sizeof(line), fp ) != NULL ) {
1099 int len = strlen( line );
1109 if ( lcur + len + 1 > lmax ) {
1110 lmax = LDAPMOD_MAXLINE
1111 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
1113 if (( buf = (char *)realloc( buf, lmax )) == NULL ) {
1114 perror( "realloc" );
1115 exit( EXIT_FAILURE );
1119 strcpy( buf + lcur, line );