3 * Copyright 1998-1999 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 );
308 else if ( authmethod == LDAP_AUTH_SASL ) {
309 if( version != LDAP_VERSION3 ) {
310 fprintf( stderr, "SASL requires LDAPv3\n" );
311 return( EXIT_FAILURE );
316 if( version != LDAP_VERSION3 ) {
317 fprintf(stderr, "manage DSA control requires LDAPv3\n");
322 if ( infile != NULL ) {
323 if (( fp = fopen( infile, "r" )) == NULL ) {
325 return( EXIT_FAILURE );
332 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
333 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
335 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
336 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
342 (void) SIGNAL( SIGPIPE, SIG_IGN );
346 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
347 perror( "ldap_init" );
348 return( EXIT_FAILURE );
351 /* this seems prudent */
353 int deref = LDAP_DEREF_NEVER;
354 ldap_set_option( ld, LDAP_OPT_DEREF, &deref);
356 /* don't chase referrals */
357 ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
360 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS)
362 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION to %d\n", version );
365 if ( use_tls && ldap_start_tls( ld, NULL, NULL ) != LDAP_SUCCESS ) {
367 ldap_perror( ld, "ldap_start_tls" );
368 return( EXIT_FAILURE );
373 passwd = getpass("Enter LDAP Password: ");
375 if ( authmethod == LDAP_AUTH_SASL ) {
376 #ifdef HAVE_CYRUS_SASL
377 int minssf = 0, maxssf = 0;
379 if ( sasl_integrity > 0 )
381 if ( sasl_integrity > 1 )
383 if ( sasl_privacy > 0 )
384 maxssf = 100000; /* Something big value */
385 if ( sasl_privacy > 1 )
388 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MINSSF,
389 (void *)&minssf ) != LDAP_OPT_SUCCESS ) {
390 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MINSSF"
392 return( EXIT_FAILURE );
394 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MAXSSF,
395 (void *)&maxssf ) != LDAP_OPT_SUCCESS ) {
396 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MINSSF"
398 return( EXIT_FAILURE );
401 if ( ldap_negotiated_sasl_bind_s( ld, binddn, sasl_authc_id,
402 sasl_authz_id, sasl_mech, NULL, NULL, NULL )
404 ldap_perror( ld, "ldap_sasl_bind" );
405 return( EXIT_FAILURE );
408 fprintf( stderr, "%s was not compiled with SASL support\n",
410 return( EXIT_FAILURE );
414 if ( ldap_bind_s( ld, binddn, passwd, authmethod )
416 ldap_perror( ld, "ldap_bind" );
417 return( EXIT_FAILURE );
428 LDAPControl *ctrls[2];
432 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
433 c.ldctl_value.bv_val = NULL;
434 c.ldctl_value.bv_len = 0;
435 c.ldctl_iscritical = manageDSAit > 1;
437 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
439 if( err != LDAP_OPT_SUCCESS ) {
440 fprintf( stderr, "Could not set Manage DSA IT Control\n" );
441 if( c.ldctl_iscritical ) {
442 exit( EXIT_FAILURE );
448 while (( rc == 0 || contoper ) &&
449 ( rbuf = read_one_record( fp )) != NULL ) {
452 * we assume record is ldif/slapd.replog if the first line
453 * has a colon that appears to the left of any equal signs, OR
454 * if the first line consists entirely of digits (an entry id)
459 use_ldif = ( *rbuf == '#' ) ||
460 (( p = strchr( rbuf, ':' )) != NULL &&
461 ( q = strchr( rbuf, '\n' )) != NULL && p < q &&
462 (( q = strchr( rbuf, '=' )) == NULL || p < q ));
467 if ( !use_ldif && ( q = strchr( rbuf, '\n' )) != NULL ) {
468 for ( p = rbuf; p < q; ++p ) {
469 if ( !isdigit( (unsigned char) *p )) {
480 rc = process_ldif_rec( start, count );
482 rc = process_ldapmod_rec( start );
486 fprintf( stderr, "%s() = %d\n",
487 use_ldif ? "ldif_rec" : "ldapmod_rec" , rc );
501 process_ldif_rec( char *rbuf, int count )
503 char *line, *dn, *type, *value, *newrdn, *newsup, *p;
504 int rc, linenum, modop, replicaport;
506 int expect_modop, expect_sep, expect_ct, expect_newrdn, expect_newsup;
507 int expect_deleteoldrdn, deleteoldrdn;
508 int saw_replica, use_record, new_entry, delete_entry, got_all;
514 rc = got_all = saw_replica = delete_entry = modop = expect_modop = 0;
515 expect_deleteoldrdn = expect_newrdn = expect_newsup = 0;
516 expect_sep = expect_ct = 0;
522 dn = newrdn = newsup = NULL;
524 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
527 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
533 if ( ldif_parse_line( line, &type, &value, &vlen ) < 0 ) {
534 fprintf( stderr, "%s: invalid format (line %d) entry: \"%s\"\n",
535 prog, linenum, dn == NULL ? "" : dn );
536 rc = LDAP_PARAM_ERROR;
541 if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
543 if (( p = strchr( value, ':' )) == NULL ) {
547 replicaport = atoi( p );
549 if ( ldaphost != NULL && strcasecmp( value, ldaphost ) == 0 &&
550 replicaport == ldapport ) {
553 } else if ( count == 1 && linenum == 1 &&
554 strcasecmp( type, T_VERSION_STR ) == 0 )
556 if( vlen == 0 || atoi(value) != 1 ) {
557 fprintf( stderr, "%s: invalid version %s, line %d (ignored)\n",
558 prog, value == NULL ? "(null)" : value, linenum );
562 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
563 if (( dn = strdup( value ? value : "" )) == NULL ) {
565 exit( EXIT_FAILURE );
569 goto end_line; /* skip all lines until we see "dn:" */
574 if ( !use_record && saw_replica ) {
575 printf( "%s: skipping change record for entry: %s\n"
576 "\t(LDAP host/port does not match replica: lines)\n",
580 ber_memfree( value );
584 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
585 if ( strcasecmp( value, T_MODIFYCTSTR ) == 0 ) {
588 } else if ( strcasecmp( value, T_ADDCTSTR ) == 0 ) {
590 } else if ( strcasecmp( value, T_MODRDNCTSTR ) == 0
591 || strcasecmp( value, T_MODDNCTSTR ) == 0
592 || strcasecmp( value, T_RENAMECTSTR ) == 0)
595 } else if ( strcasecmp( value, T_DELETECTSTR ) == 0 ) {
596 got_all = delete_entry = 1;
599 "%s: unknown %s \"%s\" (line %d of entry \"%s\")\n",
600 prog, T_CHANGETYPESTR, value, linenum, dn );
601 rc = LDAP_PARAM_ERROR;
604 } else if ( new ) { /* missing changetype => add */
606 modop = LDAP_MOD_ADD;
608 expect_modop = 1; /* missing changetype => modify */
612 if ( expect_modop ) {
615 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
616 modop = LDAP_MOD_ADD;
618 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
619 modop = LDAP_MOD_REPLACE;
620 addmodifyop( &pmods, modop, value, NULL, 0 );
622 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
623 modop = LDAP_MOD_DELETE;
624 addmodifyop( &pmods, modop, value, NULL, 0 );
626 } else { /* no modify op: use default */
627 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
631 if ( expect_newrdn ) {
632 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
633 if (( newrdn = strdup( value )) == NULL ) {
635 exit( EXIT_FAILURE );
637 expect_deleteoldrdn = 1;
640 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
641 prog, T_NEWRDNSTR, type, linenum, dn );
642 rc = LDAP_PARAM_ERROR;
644 } else if ( expect_deleteoldrdn ) {
645 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
646 deleteoldrdn = ( *value == '0' ) ? 0 : 1;
647 expect_deleteoldrdn = 0;
651 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
652 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
653 rc = LDAP_PARAM_ERROR;
655 } else if ( expect_newsup ) {
656 if ( strcasecmp( type, T_NEWSUPSTR ) == 0 ) {
657 if (( newsup = strdup( value )) == NULL ) {
659 exit( EXIT_FAILURE );
663 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
664 prog, T_NEWSUPSTR, type, linenum, dn );
665 rc = LDAP_PARAM_ERROR;
667 } else if ( got_all ) {
669 "%s: extra lines at end (line %d of entry \"%s\")\n",
671 rc = LDAP_PARAM_ERROR;
673 addmodifyop( &pmods, modop, type, value, vlen );
678 ber_memfree( value );
685 if( version && linenum == 1 ) {
690 if ( delete_entry ) {
692 } else if ( newrdn != NULL ) {
693 rc = domodrdn( dn, newrdn, deleteoldrdn );
695 rc = domodify( dn, pmods, new_entry );
698 if ( rc == LDAP_SUCCESS ) {
706 if ( newrdn != NULL ) {
709 if ( pmods != NULL ) {
710 ldap_mods_free( pmods, 1 );
718 process_ldapmod_rec( char *rbuf )
720 char *line, *dn, *p, *q, *attr, *value;
721 int rc, linenum, modop;
730 while ( rc == 0 && rbuf != NULL && *rbuf != '\0' ) {
732 if (( p = strchr( rbuf, '\n' )) == NULL ) {
735 if ( *(p-1) == '\\' ) { /* lines ending in '\' are continued */
736 SAFEMEMCPY( p - 1, p, strlen( p ) + 1 );
744 if ( dn == NULL ) { /* first line contains DN */
745 if (( dn = strdup( line )) == NULL ) {
747 exit( EXIT_FAILURE );
750 if (( p = strchr( line, '=' )) == NULL ) {
752 p = line + strlen( line );
759 *attr != '\0' && isspace( (unsigned char) *attr ); ++attr ) {
760 ; /* skip attribute leading white space */
763 for ( q = p - 1; q > attr && isspace( (unsigned char) *q ); --q ) {
764 *q = '\0'; /* remove attribute trailing white space */
767 if ( value != NULL ) {
768 while ( isspace( (unsigned char) *value )) {
769 ++value; /* skip value leading white space */
771 for ( q = value + strlen( value ) - 1; q > value &&
772 isspace( (unsigned char) *q ); --q ) {
773 *q = '\0'; /* remove value trailing white space */
775 if ( *value == '\0' ) {
781 if ( value == NULL && new ) {
782 fprintf( stderr, "%s: missing value on line %d (attr=\"%s\")\n",
783 prog, linenum, attr );
784 rc = LDAP_PARAM_ERROR;
788 modop = LDAP_MOD_DELETE;
792 modop = LDAP_MOD_ADD;
796 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
799 addmodifyop( &pmods, modop, attr, value,
800 ( value == NULL ) ? 0 : strlen( value ));
809 rc = LDAP_PARAM_ERROR;
810 } else if (( rc = domodify( dn, pmods, new )) == LDAP_SUCCESS ) {
815 if ( pmods != NULL ) {
816 ldap_mods_free( pmods, 1 );
827 addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
834 modop |= LDAP_MOD_BVALUES;
837 if ( pmods != NULL ) {
838 for ( ; pmods[ i ] != NULL; ++i ) {
839 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
840 pmods[ i ]->mod_op == modop ) {
846 if ( pmods == NULL || pmods[ i ] == NULL ) {
847 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
848 sizeof( LDAPMod * ))) == NULL ) {
850 exit( EXIT_FAILURE );
853 pmods[ i + 1 ] = NULL;
854 if (( pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod )))
857 exit( EXIT_FAILURE );
859 pmods[ i ]->mod_op = modop;
860 if (( pmods[ i ]->mod_type = ber_strdup( attr )) == NULL ) {
862 exit( EXIT_FAILURE );
866 if ( value != NULL ) {
868 if ( pmods[ i ]->mod_bvalues != NULL ) {
869 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
873 if (( pmods[ i ]->mod_bvalues =
874 (struct berval **)ber_memrealloc( pmods[ i ]->mod_bvalues,
875 (j + 2) * sizeof( struct berval * ))) == NULL ) {
876 perror( "ber_realloc" );
877 exit( EXIT_FAILURE );
879 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
880 if (( bvp = (struct berval *)ber_memalloc( sizeof( struct berval )))
882 perror( "ber_memalloc" );
883 exit( EXIT_FAILURE );
885 pmods[ i ]->mod_bvalues[ j ] = bvp;
887 if ( valsfromfiles && *value == '/' ) { /* get value from file */
888 if ( fromfile( value, bvp ) < 0 ) {
889 exit( EXIT_FAILURE );
893 if (( bvp->bv_val = (char *)ber_memalloc( vlen + 1 )) == NULL ) {
895 exit( EXIT_FAILURE );
897 SAFEMEMCPY( bvp->bv_val, value, vlen );
898 bvp->bv_val[ vlen ] = '\0';
905 domodify( char *dn, LDAPMod **pmods, int newentry )
907 int i, j, k, notascii, op;
910 if ( pmods == NULL ) {
911 fprintf( stderr, "%s: no attributes to change or add (entry=\"%s\")\n",
913 return( LDAP_PARAM_ERROR );
917 for ( i = 0; pmods[ i ] != NULL; ++i ) {
918 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
919 printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
920 "replace" : op == LDAP_MOD_ADD ?
921 "add" : "delete", pmods[ i ]->mod_type );
922 if ( pmods[ i ]->mod_bvalues != NULL ) {
923 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
924 bvp = pmods[ i ]->mod_bvalues[ j ];
926 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
927 if ( !isascii( bvp->bv_val[ k ] )) {
933 printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
935 printf( "\t%s\n", bvp->bv_val );
943 printf( "%sadding new entry \"%s\"\n", not ? "!" : "", dn );
945 printf( "%smodifying entry \"%s\"\n", not ? "!" : "", dn );
950 i = ldap_add_s( ld, dn, pmods );
952 i = ldap_modify_s( ld, dn, pmods );
954 if ( i != LDAP_SUCCESS ) {
955 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
956 } else if ( verbose ) {
957 printf( "modify complete\n" );
974 printf( "%sdeleting entry \"%s\"\n", not ? "!" : "", dn );
976 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
977 ldap_perror( ld, "ldap_delete" );
978 } else if ( verbose ) {
979 printf( "delete complete" );
992 domodrdn( char *dn, char *newrdn, int deleteoldrdn )
997 printf( "%smodifying rdn of entry \"%s\"\n", not ? "!" : "", dn );
999 printf( "\tnew RDN: \"%s\" (%skeep existing values)\n",
1000 newrdn, deleteoldrdn ? "do not " : "" );
1003 if (( rc = ldap_modrdn2_s( ld, dn, newrdn, deleteoldrdn ))
1005 ldap_perror( ld, "ldap_modrdn" );
1007 printf( "modrdn completed\n" );
1020 fromfile( char *path, struct berval *bv )
1026 if (( fp = fopen( path, "r" )) == NULL ) {
1031 if ( fseek( fp, 0L, SEEK_END ) != 0 ) {
1037 bv->bv_len = ftell( fp );
1039 if (( bv->bv_val = (char *)ber_memalloc( bv->bv_len )) == NULL ) {
1045 if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {
1048 ber_memfree( bv->bv_val );
1053 rlen = fread( bv->bv_val, 1, bv->bv_len, fp );
1057 if ( (unsigned long) rlen != bv->bv_len ) {
1059 ber_memfree( bv->bv_val );
1064 return( bv->bv_len );
1069 read_one_record( FILE *fp )
1071 char *buf, line[ LDAPMOD_MAXLINE ];
1077 while ( fgets( line, sizeof(line), fp ) != NULL ) {
1078 int len = strlen( line );
1088 if ( lcur + len + 1 > lmax ) {
1089 lmax = LDAPMOD_MAXLINE
1090 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
1092 if (( buf = (char *)realloc( buf, lmax )) == NULL ) {
1093 perror( "realloc" );
1094 exit( EXIT_FAILURE );
1098 strcpy( buf + lcur, line );