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
34 #include "ldap_defaults.h"
37 static char *binddn = NULL;
38 static char *passwd = NULL;
39 static char *ldaphost = NULL;
40 static int ldapport = 0;
41 #ifdef HAVE_CYRUS_SASL
42 static char *sasl_authc_id = NULL;
43 static char *sasl_authz_id = NULL;
44 static char *sasl_mech = NULL;
45 static int sasl_integrity = 0;
46 static int sasl_privacy = 0;
48 static int use_tls = 0;
49 static int new, replace, not, verbose, contoper, force, valsfromfiles;
52 #define LDAPMOD_MAXLINE 4096
54 /* strings found in replog/LDIF entries (mostly lifted from slurpd/slurp.h) */
55 #define T_VERSION_STR "version"
56 #define T_REPLICA_STR "replica"
58 #define T_CHANGETYPESTR "changetype"
59 #define T_ADDCTSTR "add"
60 #define T_MODIFYCTSTR "modify"
61 #define T_DELETECTSTR "delete"
62 #define T_MODRDNCTSTR "modrdn"
63 #define T_MODDNCTSTR "moddn"
64 #define T_RENAMECTSTR "rename"
65 #define T_MODOPADDSTR "add"
66 #define T_MODOPREPLACESTR "replace"
67 #define T_MODOPDELETESTR "delete"
68 #define T_MODSEPSTR "-"
69 #define T_NEWRDNSTR "newrdn"
70 #define T_DELETEOLDRDNSTR "deleteoldrdn"
71 #define T_NEWSUPSTR "newsuperior"
74 static void usage LDAP_P(( const char *prog )) LDAP_GCCATTR((noreturn));
75 static int process_ldapmod_rec LDAP_P(( char *rbuf ));
76 static int process_ldif_rec LDAP_P(( char *rbuf, int count ));
77 static void addmodifyop LDAP_P(( LDAPMod ***pmodsp, int modop, char *attr,
78 char *value, int vlen ));
79 static int domodify LDAP_P(( char *dn, LDAPMod **pmods, int newentry ));
80 static int dodelete LDAP_P(( char *dn ));
81 static int domodrdn LDAP_P(( char *dn, char *newrdn, int deleteoldrdn ));
82 static int fromfile LDAP_P(( char *path, struct berval *bv ));
83 static char *read_one_record LDAP_P(( FILE *fp ));
86 usage( const char *prog )
89 "Add or modify entries from an LDAP server\n\n"
90 "usage: %s [options]\n"
91 " The list of desired operations are read from stdin or from the file\n"
92 " specified by \"-f file\".\n"
94 " -a\t\tadd values (default%s)\n"
95 " -b\t\tread values from files (for binary attributes)\n"
96 " -c\t\tcontinuous operation\n"
97 " -d level\tset LDAP debugging level to `level'\n"
99 " -E\t\trequest SASL privacy (-EE to make it critical)\n"
100 " -f file\t\tperform sequence of operations listed in file\n"
101 " -F\t\tforce all changes records to be used\n"
102 " -h host\t\tLDAP server\n"
103 " -I\t\trequest SASL integrity checking (-II to make it\n"
105 " -k\t\tuse Kerberos authentication\n"
106 " -K\t\tlike -k, but do only step 1 of the Kerberos bind\n"
107 " -M\t\tenable Manage DSA IT control (-MM to make it critical)\n"
108 " -n\t\tprint adds, don't actually do them\n"
109 " -p port\t\tport on LDAP server\n"
110 " -r\t\treplace values\n"
111 " -U user\t\tSASL authentication identity (username)\n"
112 " -v\t\tverbose mode\n"
113 " -w passwd\tbind password (for Simple authentication)\n"
114 " -X id\t\tSASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
115 " -Y mech\t\tSASL mechanism\n"
116 " -Z\t\trequest the use of TLS (-ZZ to make it critical)\n"
117 , prog, (strcmp( prog, "ldapadd" ) ? " is to replace" : "") );
118 exit( EXIT_FAILURE );
123 main( int argc, char **argv )
125 char *infile, *rbuf, *start, *p, *q;
127 int rc, i, use_ldif, authmethod, version, want_bindpw, debug, manageDSAit;
130 if (( prog = strrchr( argv[ 0 ], *LDAP_DIRSEP )) == NULL ) {
136 /* Print usage when no parameters */
140 new = ( strcmp( prog, "ldapadd" ) == 0 );
143 not = verbose = valsfromfiles = want_bindpw = debug = manageDSAit = 0;
144 authmethod = LDAP_AUTH_SIMPLE;
147 while (( i = getopt( argc, argv, "abcD:d:EFf:h:IKkMnP:p:rtU:vWw:X:Y:Z" )) != EOF ) {
152 case 'b': /* read values from files (for binary attributes) */
155 case 'c': /* continuous operation */
158 case 'r': /* default is to replace rather than add values */
161 case 'k': /* kerberos bind */
162 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
163 authmethod = LDAP_AUTH_KRBV4;
165 fprintf( stderr, "%s was not compiled with Kerberos support\n", argv[0] );
166 return( EXIT_FAILURE );
169 case 'K': /* kerberos bind, part 1 only */
170 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
171 authmethod = LDAP_AUTH_KRBV41;
173 fprintf( stderr, "%s was not compiled with Kerberos support\n", argv[0] );
174 return( EXIT_FAILURE );
177 case 'F': /* force all changes records to be used */
180 case 'h': /* ldap host */
181 ldaphost = strdup( optarg );
183 case 'D': /* bind DN */
184 binddn = strdup( optarg );
186 case 'w': /* password */
187 passwd = strdup( optarg );
191 for( p = optarg; *p == '\0'; p++ ) {
197 debug |= atoi( optarg );
199 case 'f': /* read from file */
200 infile = strdup( optarg );
203 ldapport = atoi( optarg );
205 case 'n': /* print adds, don't actually do them */
208 case 'v': /* verbose mode */
212 /* enable Manage DSA IT */
219 switch( atoi(optarg) )
222 version = LDAP_VERSION2;
225 version = LDAP_VERSION3;
228 fprintf( stderr, "protocol version should be 2 or 3\n" );
233 #ifdef HAVE_CYRUS_SASL
235 authmethod = LDAP_AUTH_SASL;
237 fprintf( stderr, "%s was not compiled with SASL support\n",
239 return( EXIT_FAILURE );
243 #ifdef HAVE_CYRUS_SASL
245 authmethod = LDAP_AUTH_SASL;
247 fprintf( stderr, "%s was not compiled with SASL support\n",
249 return( EXIT_FAILURE );
253 #ifdef HAVE_CYRUS_SASL
254 if ( strcasecmp( optarg, "any" ) && strcmp( optarg, "*" ) ) {
255 sasl_mech = strdup( optarg );
257 authmethod = LDAP_AUTH_SASL;
259 fprintf( stderr, "%s was not compiled with SASL support\n",
261 return( EXIT_FAILURE );
265 #ifdef HAVE_CYRUS_SASL
266 sasl_authc_id = strdup( optarg );
267 authmethod = LDAP_AUTH_SASL;
269 fprintf( stderr, "%s was not compiled with SASL support\n",
271 return( EXIT_FAILURE );
275 #ifdef HAVE_CYRUS_SASL
276 sasl_authz_id = strdup( optarg );
277 authmethod = LDAP_AUTH_SASL;
279 fprintf( stderr, "%s was not compiled with SASL support\n",
281 return( EXIT_FAILURE );
288 fprintf( stderr, "%s was not compiled with TLS support\n",
290 return( EXIT_FAILURE );
298 if ( argc != optind )
301 if ( ( authmethod == LDAP_AUTH_KRBV4 ) || ( authmethod ==
302 LDAP_AUTH_KRBV41 ) ) {
303 if( version > LDAP_VERSION2 ) {
304 fprintf( stderr, "Kerberos requires LDAPv2\n" );
305 return( EXIT_FAILURE );
307 version = LDAP_VERSION2;
309 else if ( authmethod == LDAP_AUTH_SASL ) {
310 if( version != -1 && version != LDAP_VERSION3 ) {
311 fprintf( stderr, "SASL requires LDAPv3\n" );
312 return( EXIT_FAILURE );
314 version = LDAP_VERSION3;
318 if( version != -1 && version != LDAP_VERSION3 ) {
319 fprintf(stderr, "manage DSA control requires LDAPv3\n");
322 version = LDAP_VERSION3;
326 if( version != -1 && version != LDAP_VERSION3 ) {
327 fprintf(stderr, "Start TLS requires LDAPv3\n");
330 version = LDAP_VERSION3;
333 if ( infile != NULL ) {
334 if (( fp = fopen( infile, "r" )) == NULL ) {
336 return( EXIT_FAILURE );
343 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
344 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
346 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
347 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
353 (void) SIGNAL( SIGPIPE, SIG_IGN );
357 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
358 perror( "ldap_init" );
359 return( EXIT_FAILURE );
362 /* this seems prudent */
364 int deref = LDAP_DEREF_NEVER;
365 ldap_set_option( ld, LDAP_OPT_DEREF, &deref);
367 /* don't chase referrals */
368 ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
371 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS)
373 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION to %d\n", version );
376 if ( use_tls && ldap_start_tls( ld, NULL, NULL ) != LDAP_SUCCESS ) {
378 ldap_perror( ld, "ldap_start_tls" );
379 return( EXIT_FAILURE );
384 passwd = getpass("Enter LDAP Password: ");
386 if ( authmethod == LDAP_AUTH_SASL ) {
387 #ifdef HAVE_CYRUS_SASL
388 int minssf = 0, maxssf = 0;
390 if ( sasl_integrity > 0 )
392 if ( sasl_integrity > 1 )
394 if ( sasl_privacy > 0 )
395 maxssf = 100000; /* Something big value */
396 if ( sasl_privacy > 1 )
399 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MINSSF,
400 (void *)&minssf ) != LDAP_OPT_SUCCESS ) {
401 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MINSSF"
403 return( EXIT_FAILURE );
405 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MAXSSF,
406 (void *)&maxssf ) != LDAP_OPT_SUCCESS ) {
407 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MINSSF"
409 return( EXIT_FAILURE );
412 if ( ldap_negotiated_sasl_bind_s( ld, binddn, sasl_authc_id,
413 sasl_authz_id, sasl_mech, NULL, NULL, NULL )
415 ldap_perror( ld, "ldap_sasl_bind" );
416 return( EXIT_FAILURE );
419 fprintf( stderr, "%s was not compiled with SASL support\n",
421 return( EXIT_FAILURE );
425 if ( ldap_bind_s( ld, binddn, passwd, authmethod )
427 ldap_perror( ld, "ldap_bind" );
428 return( EXIT_FAILURE );
439 LDAPControl *ctrls[2];
443 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
444 c.ldctl_value.bv_val = NULL;
445 c.ldctl_value.bv_len = 0;
446 c.ldctl_iscritical = manageDSAit > 1;
448 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
450 if( err != LDAP_OPT_SUCCESS ) {
451 fprintf( stderr, "Could not set Manage DSA IT Control\n" );
452 if( c.ldctl_iscritical ) {
453 exit( EXIT_FAILURE );
459 while (( rc == 0 || contoper ) &&
460 ( rbuf = read_one_record( fp )) != NULL ) {
463 * we assume record is ldif/slapd.replog if the first line
464 * has a colon that appears to the left of any equal signs, OR
465 * if the first line consists entirely of digits (an entry id)
470 use_ldif = ( *rbuf == '#' ) ||
471 (( p = strchr( rbuf, ':' )) != NULL &&
472 ( q = strchr( rbuf, '\n' )) != NULL && p < q &&
473 (( q = strchr( rbuf, '=' )) == NULL || p < q ));
478 if ( !use_ldif && ( q = strchr( rbuf, '\n' )) != NULL ) {
479 for ( p = rbuf; p < q; ++p ) {
480 if ( !isdigit( (unsigned char) *p )) {
491 rc = process_ldif_rec( start, count );
493 rc = process_ldapmod_rec( start );
497 fprintf( stderr, "%s() = %d\n",
498 use_ldif ? "ldif_rec" : "ldapmod_rec" , rc );
512 process_ldif_rec( char *rbuf, int count )
514 char *line, *dn, *type, *value, *newrdn, *newsup, *p;
515 int rc, linenum, modop, replicaport;
517 int expect_modop, expect_sep, expect_ct, expect_newrdn, expect_newsup;
518 int expect_deleteoldrdn, deleteoldrdn;
519 int saw_replica, use_record, new_entry, delete_entry, got_all;
525 rc = got_all = saw_replica = delete_entry = modop = expect_modop = 0;
526 expect_deleteoldrdn = expect_newrdn = expect_newsup = 0;
527 expect_sep = expect_ct = 0;
533 dn = newrdn = newsup = NULL;
535 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
538 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
544 if ( ldif_parse_line( line, &type, &value, &vlen ) < 0 ) {
545 fprintf( stderr, "%s: invalid format (line %d) entry: \"%s\"\n",
546 prog, linenum, dn == NULL ? "" : dn );
547 rc = LDAP_PARAM_ERROR;
552 if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
554 if (( p = strchr( value, ':' )) == NULL ) {
558 replicaport = atoi( p );
560 if ( ldaphost != NULL && strcasecmp( value, ldaphost ) == 0 &&
561 replicaport == ldapport ) {
564 } else if ( count == 1 && linenum == 1 &&
565 strcasecmp( type, T_VERSION_STR ) == 0 )
567 if( vlen == 0 || atoi(value) != 1 ) {
568 fprintf( stderr, "%s: invalid version %s, line %d (ignored)\n",
569 prog, value == NULL ? "(null)" : value, linenum );
573 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
574 if (( dn = strdup( value ? value : "" )) == NULL ) {
576 exit( EXIT_FAILURE );
580 goto end_line; /* skip all lines until we see "dn:" */
585 if ( !use_record && saw_replica ) {
586 printf( "%s: skipping change record for entry: %s\n"
587 "\t(LDAP host/port does not match replica: lines)\n",
591 ber_memfree( value );
595 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
596 if ( strcasecmp( value, T_MODIFYCTSTR ) == 0 ) {
599 } else if ( strcasecmp( value, T_ADDCTSTR ) == 0 ) {
601 } else if ( strcasecmp( value, T_MODRDNCTSTR ) == 0
602 || strcasecmp( value, T_MODDNCTSTR ) == 0
603 || strcasecmp( value, T_RENAMECTSTR ) == 0)
606 } else if ( strcasecmp( value, T_DELETECTSTR ) == 0 ) {
607 got_all = delete_entry = 1;
610 "%s: unknown %s \"%s\" (line %d of entry \"%s\")\n",
611 prog, T_CHANGETYPESTR, value, linenum, dn );
612 rc = LDAP_PARAM_ERROR;
615 } else if ( new ) { /* missing changetype => add */
617 modop = LDAP_MOD_ADD;
619 expect_modop = 1; /* missing changetype => modify */
623 if ( expect_modop ) {
626 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
627 modop = LDAP_MOD_ADD;
629 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
630 modop = LDAP_MOD_REPLACE;
631 addmodifyop( &pmods, modop, value, NULL, 0 );
633 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
634 modop = LDAP_MOD_DELETE;
635 addmodifyop( &pmods, modop, value, NULL, 0 );
637 } else { /* no modify op: use default */
638 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
642 if ( expect_newrdn ) {
643 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
644 if (( newrdn = strdup( value )) == NULL ) {
646 exit( EXIT_FAILURE );
648 expect_deleteoldrdn = 1;
651 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
652 prog, T_NEWRDNSTR, type, linenum, dn );
653 rc = LDAP_PARAM_ERROR;
655 } else if ( expect_deleteoldrdn ) {
656 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
657 deleteoldrdn = ( *value == '0' ) ? 0 : 1;
658 expect_deleteoldrdn = 0;
662 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
663 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
664 rc = LDAP_PARAM_ERROR;
666 } else if ( expect_newsup ) {
667 if ( strcasecmp( type, T_NEWSUPSTR ) == 0 ) {
668 if (( newsup = strdup( value )) == NULL ) {
670 exit( EXIT_FAILURE );
674 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
675 prog, T_NEWSUPSTR, type, linenum, dn );
676 rc = LDAP_PARAM_ERROR;
678 } else if ( got_all ) {
680 "%s: extra lines at end (line %d of entry \"%s\")\n",
682 rc = LDAP_PARAM_ERROR;
684 addmodifyop( &pmods, modop, type, value, vlen );
689 ber_memfree( value );
696 if( version && linenum == 1 ) {
701 if ( delete_entry ) {
703 } else if ( newrdn != NULL ) {
704 rc = domodrdn( dn, newrdn, deleteoldrdn );
706 rc = domodify( dn, pmods, new_entry );
709 if ( rc == LDAP_SUCCESS ) {
717 if ( newrdn != NULL ) {
720 if ( pmods != NULL ) {
721 ldap_mods_free( pmods, 1 );
729 process_ldapmod_rec( char *rbuf )
731 char *line, *dn, *p, *q, *attr, *value;
732 int rc, linenum, modop;
741 while ( rc == 0 && rbuf != NULL && *rbuf != '\0' ) {
743 if (( p = strchr( rbuf, '\n' )) == NULL ) {
746 if ( *(p-1) == '\\' ) { /* lines ending in '\' are continued */
747 SAFEMEMCPY( p - 1, p, strlen( p ) + 1 );
755 if ( dn == NULL ) { /* first line contains DN */
756 if (( dn = strdup( line )) == NULL ) {
758 exit( EXIT_FAILURE );
761 if (( p = strchr( line, '=' )) == NULL ) {
763 p = line + strlen( line );
770 *attr != '\0' && isspace( (unsigned char) *attr ); ++attr ) {
771 ; /* skip attribute leading white space */
774 for ( q = p - 1; q > attr && isspace( (unsigned char) *q ); --q ) {
775 *q = '\0'; /* remove attribute trailing white space */
778 if ( value != NULL ) {
779 while ( isspace( (unsigned char) *value )) {
780 ++value; /* skip value leading white space */
782 for ( q = value + strlen( value ) - 1; q > value &&
783 isspace( (unsigned char) *q ); --q ) {
784 *q = '\0'; /* remove value trailing white space */
786 if ( *value == '\0' ) {
792 if ( value == NULL && new ) {
793 fprintf( stderr, "%s: missing value on line %d (attr=\"%s\")\n",
794 prog, linenum, attr );
795 rc = LDAP_PARAM_ERROR;
799 modop = LDAP_MOD_DELETE;
803 modop = LDAP_MOD_ADD;
807 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
810 addmodifyop( &pmods, modop, attr, value,
811 ( value == NULL ) ? 0 : strlen( value ));
820 rc = LDAP_PARAM_ERROR;
821 } else if (( rc = domodify( dn, pmods, new )) == LDAP_SUCCESS ) {
826 if ( pmods != NULL ) {
827 ldap_mods_free( pmods, 1 );
838 addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
845 modop |= LDAP_MOD_BVALUES;
848 if ( pmods != NULL ) {
849 for ( ; pmods[ i ] != NULL; ++i ) {
850 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
851 pmods[ i ]->mod_op == modop ) {
857 if ( pmods == NULL || pmods[ i ] == NULL ) {
858 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
859 sizeof( LDAPMod * ))) == NULL ) {
861 exit( EXIT_FAILURE );
864 pmods[ i + 1 ] = NULL;
865 if (( pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod )))
868 exit( EXIT_FAILURE );
870 pmods[ i ]->mod_op = modop;
871 if (( pmods[ i ]->mod_type = ber_strdup( attr )) == NULL ) {
873 exit( EXIT_FAILURE );
877 if ( value != NULL ) {
879 if ( pmods[ i ]->mod_bvalues != NULL ) {
880 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
884 if (( pmods[ i ]->mod_bvalues =
885 (struct berval **)ber_memrealloc( pmods[ i ]->mod_bvalues,
886 (j + 2) * sizeof( struct berval * ))) == NULL ) {
887 perror( "ber_realloc" );
888 exit( EXIT_FAILURE );
890 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
891 if (( bvp = (struct berval *)ber_memalloc( sizeof( struct berval )))
893 perror( "ber_memalloc" );
894 exit( EXIT_FAILURE );
896 pmods[ i ]->mod_bvalues[ j ] = bvp;
898 if ( valsfromfiles && *value == '/' ) { /* get value from file */
899 if ( fromfile( value, bvp ) < 0 ) {
900 exit( EXIT_FAILURE );
904 if (( bvp->bv_val = (char *)ber_memalloc( vlen + 1 )) == NULL ) {
906 exit( EXIT_FAILURE );
908 SAFEMEMCPY( bvp->bv_val, value, vlen );
909 bvp->bv_val[ vlen ] = '\0';
916 domodify( char *dn, LDAPMod **pmods, int newentry )
918 int i, j, k, notascii, op;
921 if ( pmods == NULL ) {
922 fprintf( stderr, "%s: no attributes to change or add (entry=\"%s\")\n",
924 return( LDAP_PARAM_ERROR );
928 for ( i = 0; pmods[ i ] != NULL; ++i ) {
929 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
930 printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
931 "replace" : op == LDAP_MOD_ADD ?
932 "add" : "delete", pmods[ i ]->mod_type );
933 if ( pmods[ i ]->mod_bvalues != NULL ) {
934 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
935 bvp = pmods[ i ]->mod_bvalues[ j ];
937 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
938 if ( !isascii( bvp->bv_val[ k ] )) {
944 printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
946 printf( "\t%s\n", bvp->bv_val );
954 printf( "%sadding new entry \"%s\"\n", not ? "!" : "", dn );
956 printf( "%smodifying entry \"%s\"\n", not ? "!" : "", dn );
961 i = ldap_add_s( ld, dn, pmods );
963 i = ldap_modify_s( ld, dn, pmods );
965 if ( i != LDAP_SUCCESS ) {
966 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
967 } else if ( verbose ) {
968 printf( "modify complete\n" );
985 printf( "%sdeleting entry \"%s\"\n", not ? "!" : "", dn );
987 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
988 ldap_perror( ld, "ldap_delete" );
989 } else if ( verbose ) {
990 printf( "delete complete" );
1003 domodrdn( char *dn, char *newrdn, int deleteoldrdn )
1008 printf( "%smodifying rdn of entry \"%s\"\n", not ? "!" : "", dn );
1010 printf( "\tnew RDN: \"%s\" (%skeep existing values)\n",
1011 newrdn, deleteoldrdn ? "do not " : "" );
1014 if (( rc = ldap_modrdn2_s( ld, dn, newrdn, deleteoldrdn ))
1016 ldap_perror( ld, "ldap_modrdn" );
1018 printf( "modrdn completed\n" );
1031 fromfile( char *path, struct berval *bv )
1037 if (( fp = fopen( path, "r" )) == NULL ) {
1042 if ( fseek( fp, 0L, SEEK_END ) != 0 ) {
1048 bv->bv_len = ftell( fp );
1050 if (( bv->bv_val = (char *)ber_memalloc( bv->bv_len )) == NULL ) {
1056 if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {
1059 ber_memfree( bv->bv_val );
1064 rlen = fread( bv->bv_val, 1, bv->bv_len, fp );
1068 if ( (unsigned long) rlen != bv->bv_len ) {
1070 ber_memfree( bv->bv_val );
1075 return( bv->bv_len );
1080 read_one_record( FILE *fp )
1082 char *buf, line[ LDAPMOD_MAXLINE ];
1088 while ( fgets( line, sizeof(line), fp ) != NULL ) {
1089 int len = strlen( line );
1099 if ( lcur + len + 1 > lmax ) {
1100 lmax = LDAPMOD_MAXLINE
1101 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
1103 if (( buf = (char *)realloc( buf, lmax )) == NULL ) {
1104 perror( "realloc" );
1105 exit( EXIT_FAILURE );
1109 strcpy( buf + lcur, line );