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 struct berval passwd = { 0, 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.bv_val = strdup( optarg );
191 for( p = optarg; *p == '\0'; p++ ) {
195 passwd.bv_len = strlen( passwd.bv_val );
198 debug |= atoi( optarg );
200 case 'f': /* read from file */
201 infile = strdup( optarg );
204 ldapport = atoi( optarg );
206 case 'n': /* print adds, don't actually do them */
209 case 'v': /* verbose mode */
213 /* enable Manage DSA IT */
220 switch( atoi(optarg) )
223 version = LDAP_VERSION2;
226 version = LDAP_VERSION3;
229 fprintf( stderr, "protocol version should be 2 or 3\n" );
234 #ifdef HAVE_CYRUS_SASL
236 authmethod = LDAP_AUTH_SASL;
238 fprintf( stderr, "%s was not compiled with SASL support\n",
240 return( EXIT_FAILURE );
244 #ifdef HAVE_CYRUS_SASL
246 authmethod = LDAP_AUTH_SASL;
248 fprintf( stderr, "%s was not compiled with SASL support\n",
250 return( EXIT_FAILURE );
254 #ifdef HAVE_CYRUS_SASL
255 if ( strcasecmp( optarg, "any" ) && strcmp( optarg, "*" ) ) {
256 sasl_mech = strdup( optarg );
258 authmethod = LDAP_AUTH_SASL;
260 fprintf( stderr, "%s was not compiled with SASL support\n",
262 return( EXIT_FAILURE );
266 #ifdef HAVE_CYRUS_SASL
267 sasl_authc_id = strdup( optarg );
268 authmethod = LDAP_AUTH_SASL;
270 fprintf( stderr, "%s was not compiled with SASL support\n",
272 return( EXIT_FAILURE );
276 #ifdef HAVE_CYRUS_SASL
277 sasl_authz_id = strdup( optarg );
278 authmethod = LDAP_AUTH_SASL;
280 fprintf( stderr, "%s was not compiled with SASL support\n",
282 return( EXIT_FAILURE );
289 fprintf( stderr, "%s was not compiled with TLS support\n",
291 return( EXIT_FAILURE );
299 if ( argc != optind )
302 if ( ( authmethod == LDAP_AUTH_KRBV4 ) || ( authmethod ==
303 LDAP_AUTH_KRBV41 ) ) {
304 if( version > LDAP_VERSION2 ) {
305 fprintf( stderr, "Kerberos requires LDAPv2\n" );
306 return( EXIT_FAILURE );
308 version = LDAP_VERSION2;
310 else if ( authmethod == LDAP_AUTH_SASL ) {
311 if( version != -1 && version != LDAP_VERSION3 ) {
312 fprintf( stderr, "SASL requires LDAPv3\n" );
313 return( EXIT_FAILURE );
315 version = LDAP_VERSION3;
319 if( version != -1 && version != LDAP_VERSION3 ) {
320 fprintf(stderr, "manage DSA control requires LDAPv3\n");
323 version = LDAP_VERSION3;
327 if( version != -1 && version != LDAP_VERSION3 ) {
328 fprintf(stderr, "Start TLS requires LDAPv3\n");
331 version = LDAP_VERSION3;
334 if ( infile != NULL ) {
335 if (( fp = fopen( infile, "r" )) == NULL ) {
337 return( EXIT_FAILURE );
344 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
345 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
347 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
348 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
354 (void) SIGNAL( SIGPIPE, SIG_IGN );
358 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
359 perror( "ldap_init" );
360 return( EXIT_FAILURE );
363 /* this seems prudent */
365 int deref = LDAP_DEREF_NEVER;
366 ldap_set_option( ld, LDAP_OPT_DEREF, &deref);
368 /* don't chase referrals */
369 ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
372 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS)
374 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION to %d\n", version );
377 if ( use_tls && ldap_start_tls( ld, NULL, NULL ) != LDAP_SUCCESS ) {
379 ldap_perror( ld, "ldap_start_tls" );
380 return( EXIT_FAILURE );
385 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
386 passwd.bv_len = strlen( passwd.bv_val );
388 if ( authmethod == LDAP_AUTH_SASL ) {
389 #ifdef HAVE_CYRUS_SASL
390 int minssf = 0, maxssf = 0;
392 if ( sasl_integrity > 0 )
394 if ( sasl_integrity > 1 )
396 if ( sasl_privacy > 0 )
397 maxssf = 100000; /* Something big value */
398 if ( sasl_privacy > 1 )
401 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MINSSF,
402 (void *)&minssf ) != LDAP_OPT_SUCCESS ) {
403 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MINSSF"
405 return( EXIT_FAILURE );
407 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MAXSSF,
408 (void *)&maxssf ) != LDAP_OPT_SUCCESS ) {
409 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MINSSF"
411 return( EXIT_FAILURE );
414 rc = ldap_negotiated_sasl_bind_s( ld, binddn, sasl_authc_id,
415 sasl_authz_id, sasl_mech,
416 passwd.bv_len ? &passwd : NULL,
419 if( rc != LDAP_SUCCESS ) {
420 ldap_perror( ld, "ldap_negotiated_sasl_bind_s" );
421 return( EXIT_FAILURE );
424 fprintf( stderr, "%s was not compiled with SASL support\n",
426 return( EXIT_FAILURE );
430 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
432 ldap_perror( ld, "ldap_bind" );
433 return( EXIT_FAILURE );
444 LDAPControl *ctrls[2];
448 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
449 c.ldctl_value.bv_val = NULL;
450 c.ldctl_value.bv_len = 0;
451 c.ldctl_iscritical = manageDSAit > 1;
453 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
455 if( err != LDAP_OPT_SUCCESS ) {
456 fprintf( stderr, "Could not set Manage DSA IT Control\n" );
457 if( c.ldctl_iscritical ) {
458 exit( EXIT_FAILURE );
464 while (( rc == 0 || contoper ) &&
465 ( rbuf = read_one_record( fp )) != NULL ) {
468 * we assume record is ldif/slapd.replog if the first line
469 * has a colon that appears to the left of any equal signs, OR
470 * if the first line consists entirely of digits (an entry id)
475 use_ldif = ( *rbuf == '#' ) ||
476 (( p = strchr( rbuf, ':' )) != NULL &&
477 ( q = strchr( rbuf, '\n' )) != NULL && p < q &&
478 (( q = strchr( rbuf, '=' )) == NULL || p < q ));
483 if ( !use_ldif && ( q = strchr( rbuf, '\n' )) != NULL ) {
484 for ( p = rbuf; p < q; ++p ) {
485 if ( !isdigit( (unsigned char) *p )) {
496 rc = process_ldif_rec( start, count );
498 rc = process_ldapmod_rec( start );
502 fprintf( stderr, "%s() = %d\n",
503 use_ldif ? "ldif_rec" : "ldapmod_rec" , rc );
517 process_ldif_rec( char *rbuf, int count )
519 char *line, *dn, *type, *value, *newrdn, *newsup, *p;
520 int rc, linenum, modop, replicaport;
522 int expect_modop, expect_sep, expect_ct, expect_newrdn, expect_newsup;
523 int expect_deleteoldrdn, deleteoldrdn;
524 int saw_replica, use_record, new_entry, delete_entry, got_all;
530 rc = got_all = saw_replica = delete_entry = modop = expect_modop = 0;
531 expect_deleteoldrdn = expect_newrdn = expect_newsup = 0;
532 expect_sep = expect_ct = 0;
538 dn = newrdn = newsup = NULL;
540 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
543 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
549 if ( ldif_parse_line( line, &type, &value, &vlen ) < 0 ) {
550 fprintf( stderr, "%s: invalid format (line %d) entry: \"%s\"\n",
551 prog, linenum, dn == NULL ? "" : dn );
552 rc = LDAP_PARAM_ERROR;
557 if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
559 if (( p = strchr( value, ':' )) == NULL ) {
563 replicaport = atoi( p );
565 if ( ldaphost != NULL && strcasecmp( value, ldaphost ) == 0 &&
566 replicaport == ldapport ) {
569 } else if ( count == 1 && linenum == 1 &&
570 strcasecmp( type, T_VERSION_STR ) == 0 )
572 if( vlen == 0 || atoi(value) != 1 ) {
573 fprintf( stderr, "%s: invalid version %s, line %d (ignored)\n",
574 prog, value == NULL ? "(null)" : value, linenum );
578 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
579 if (( dn = strdup( value ? value : "" )) == NULL ) {
581 exit( EXIT_FAILURE );
585 goto end_line; /* skip all lines until we see "dn:" */
590 if ( !use_record && saw_replica ) {
591 printf( "%s: skipping change record for entry: %s\n"
592 "\t(LDAP host/port does not match replica: lines)\n",
596 ber_memfree( value );
600 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
601 if ( strcasecmp( value, T_MODIFYCTSTR ) == 0 ) {
604 } else if ( strcasecmp( value, T_ADDCTSTR ) == 0 ) {
606 } else if ( strcasecmp( value, T_MODRDNCTSTR ) == 0
607 || strcasecmp( value, T_MODDNCTSTR ) == 0
608 || strcasecmp( value, T_RENAMECTSTR ) == 0)
611 } else if ( strcasecmp( value, T_DELETECTSTR ) == 0 ) {
612 got_all = delete_entry = 1;
615 "%s: unknown %s \"%s\" (line %d of entry \"%s\")\n",
616 prog, T_CHANGETYPESTR, value, linenum, dn );
617 rc = LDAP_PARAM_ERROR;
620 } else if ( new ) { /* missing changetype => add */
622 modop = LDAP_MOD_ADD;
624 expect_modop = 1; /* missing changetype => modify */
628 if ( expect_modop ) {
631 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
632 modop = LDAP_MOD_ADD;
634 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
635 modop = LDAP_MOD_REPLACE;
636 addmodifyop( &pmods, modop, value, NULL, 0 );
638 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
639 modop = LDAP_MOD_DELETE;
640 addmodifyop( &pmods, modop, value, NULL, 0 );
642 } else { /* no modify op: use default */
643 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
647 if ( expect_newrdn ) {
648 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
649 if (( newrdn = strdup( value )) == NULL ) {
651 exit( EXIT_FAILURE );
653 expect_deleteoldrdn = 1;
656 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
657 prog, T_NEWRDNSTR, type, linenum, dn );
658 rc = LDAP_PARAM_ERROR;
660 } else if ( expect_deleteoldrdn ) {
661 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
662 deleteoldrdn = ( *value == '0' ) ? 0 : 1;
663 expect_deleteoldrdn = 0;
667 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
668 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
669 rc = LDAP_PARAM_ERROR;
671 } else if ( expect_newsup ) {
672 if ( strcasecmp( type, T_NEWSUPSTR ) == 0 ) {
673 if (( newsup = strdup( value )) == NULL ) {
675 exit( EXIT_FAILURE );
679 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
680 prog, T_NEWSUPSTR, type, linenum, dn );
681 rc = LDAP_PARAM_ERROR;
683 } else if ( got_all ) {
685 "%s: extra lines at end (line %d of entry \"%s\")\n",
687 rc = LDAP_PARAM_ERROR;
689 addmodifyop( &pmods, modop, type, value, vlen );
694 ber_memfree( value );
701 if( version && linenum == 1 ) {
706 if ( delete_entry ) {
708 } else if ( newrdn != NULL ) {
709 rc = domodrdn( dn, newrdn, deleteoldrdn );
711 rc = domodify( dn, pmods, new_entry );
714 if ( rc == LDAP_SUCCESS ) {
722 if ( newrdn != NULL ) {
725 if ( pmods != NULL ) {
726 ldap_mods_free( pmods, 1 );
734 process_ldapmod_rec( char *rbuf )
736 char *line, *dn, *p, *q, *attr, *value;
737 int rc, linenum, modop;
746 while ( rc == 0 && rbuf != NULL && *rbuf != '\0' ) {
748 if (( p = strchr( rbuf, '\n' )) == NULL ) {
751 if ( *(p-1) == '\\' ) { /* lines ending in '\' are continued */
752 SAFEMEMCPY( p - 1, p, strlen( p ) + 1 );
760 if ( dn == NULL ) { /* first line contains DN */
761 if (( dn = strdup( line )) == NULL ) {
763 exit( EXIT_FAILURE );
766 if (( p = strchr( line, '=' )) == NULL ) {
768 p = line + strlen( line );
775 *attr != '\0' && isspace( (unsigned char) *attr ); ++attr ) {
776 ; /* skip attribute leading white space */
779 for ( q = p - 1; q > attr && isspace( (unsigned char) *q ); --q ) {
780 *q = '\0'; /* remove attribute trailing white space */
783 if ( value != NULL ) {
784 while ( isspace( (unsigned char) *value )) {
785 ++value; /* skip value leading white space */
787 for ( q = value + strlen( value ) - 1; q > value &&
788 isspace( (unsigned char) *q ); --q ) {
789 *q = '\0'; /* remove value trailing white space */
791 if ( *value == '\0' ) {
797 if ( value == NULL && new ) {
798 fprintf( stderr, "%s: missing value on line %d (attr=\"%s\")\n",
799 prog, linenum, attr );
800 rc = LDAP_PARAM_ERROR;
804 modop = LDAP_MOD_DELETE;
808 modop = LDAP_MOD_ADD;
812 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
815 addmodifyop( &pmods, modop, attr, value,
816 ( value == NULL ) ? 0 : strlen( value ));
825 rc = LDAP_PARAM_ERROR;
826 } else if (( rc = domodify( dn, pmods, new )) == LDAP_SUCCESS ) {
831 if ( pmods != NULL ) {
832 ldap_mods_free( pmods, 1 );
843 addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
850 modop |= LDAP_MOD_BVALUES;
853 if ( pmods != NULL ) {
854 for ( ; pmods[ i ] != NULL; ++i ) {
855 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
856 pmods[ i ]->mod_op == modop ) {
862 if ( pmods == NULL || pmods[ i ] == NULL ) {
863 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
864 sizeof( LDAPMod * ))) == NULL ) {
866 exit( EXIT_FAILURE );
869 pmods[ i + 1 ] = NULL;
870 if (( pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod )))
873 exit( EXIT_FAILURE );
875 pmods[ i ]->mod_op = modop;
876 if (( pmods[ i ]->mod_type = ber_strdup( attr )) == NULL ) {
878 exit( EXIT_FAILURE );
882 if ( value != NULL ) {
884 if ( pmods[ i ]->mod_bvalues != NULL ) {
885 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
889 if (( pmods[ i ]->mod_bvalues =
890 (struct berval **)ber_memrealloc( pmods[ i ]->mod_bvalues,
891 (j + 2) * sizeof( struct berval * ))) == NULL ) {
892 perror( "ber_realloc" );
893 exit( EXIT_FAILURE );
895 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
896 if (( bvp = (struct berval *)ber_memalloc( sizeof( struct berval )))
898 perror( "ber_memalloc" );
899 exit( EXIT_FAILURE );
901 pmods[ i ]->mod_bvalues[ j ] = bvp;
903 if ( valsfromfiles && *value == '/' ) { /* get value from file */
904 if ( fromfile( value, bvp ) < 0 ) {
905 exit( EXIT_FAILURE );
909 if (( bvp->bv_val = (char *)ber_memalloc( vlen + 1 )) == NULL ) {
911 exit( EXIT_FAILURE );
913 SAFEMEMCPY( bvp->bv_val, value, vlen );
914 bvp->bv_val[ vlen ] = '\0';
921 domodify( char *dn, LDAPMod **pmods, int newentry )
923 int i, j, k, notascii, op;
926 if ( pmods == NULL ) {
927 fprintf( stderr, "%s: no attributes to change or add (entry=\"%s\")\n",
929 return( LDAP_PARAM_ERROR );
933 for ( i = 0; pmods[ i ] != NULL; ++i ) {
934 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
935 printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
936 "replace" : op == LDAP_MOD_ADD ?
937 "add" : "delete", pmods[ i ]->mod_type );
938 if ( pmods[ i ]->mod_bvalues != NULL ) {
939 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
940 bvp = pmods[ i ]->mod_bvalues[ j ];
942 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
943 if ( !isascii( bvp->bv_val[ k ] )) {
949 printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
951 printf( "\t%s\n", bvp->bv_val );
959 printf( "%sadding new entry \"%s\"\n", not ? "!" : "", dn );
961 printf( "%smodifying entry \"%s\"\n", not ? "!" : "", dn );
966 i = ldap_add_s( ld, dn, pmods );
968 i = ldap_modify_s( ld, dn, pmods );
970 if ( i != LDAP_SUCCESS ) {
971 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
972 } else if ( verbose ) {
973 printf( "modify complete\n" );
990 printf( "%sdeleting entry \"%s\"\n", not ? "!" : "", dn );
992 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
993 ldap_perror( ld, "ldap_delete" );
994 } else if ( verbose ) {
995 printf( "delete complete" );
1008 domodrdn( char *dn, char *newrdn, int deleteoldrdn )
1013 printf( "%smodifying rdn of entry \"%s\"\n", not ? "!" : "", dn );
1015 printf( "\tnew RDN: \"%s\" (%skeep existing values)\n",
1016 newrdn, deleteoldrdn ? "do not " : "" );
1019 if (( rc = ldap_modrdn2_s( ld, dn, newrdn, deleteoldrdn ))
1021 ldap_perror( ld, "ldap_modrdn" );
1023 printf( "modrdn completed\n" );
1036 fromfile( char *path, struct berval *bv )
1042 if (( fp = fopen( path, "r" )) == NULL ) {
1047 if ( fseek( fp, 0L, SEEK_END ) != 0 ) {
1053 bv->bv_len = ftell( fp );
1055 if (( bv->bv_val = (char *)ber_memalloc( bv->bv_len )) == NULL ) {
1061 if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {
1064 ber_memfree( bv->bv_val );
1069 rlen = fread( bv->bv_val, 1, bv->bv_len, fp );
1073 if ( (unsigned long) rlen != bv->bv_len ) {
1075 ber_memfree( bv->bv_val );
1080 return( bv->bv_len );
1085 read_one_record( FILE *fp )
1087 char *buf, line[ LDAPMOD_MAXLINE ];
1093 while ( fgets( line, sizeof(line), fp ) != NULL ) {
1094 int len = strlen( line );
1104 if ( lcur + len + 1 > lmax ) {
1105 lmax = LDAPMOD_MAXLINE
1106 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
1108 if (( buf = (char *)realloc( buf, lmax )) == NULL ) {
1109 perror( "realloc" );
1110 exit( EXIT_FAILURE );
1114 strcpy( buf + lcur, line );