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_s( 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 );
389 if ( authmethod == LDAP_AUTH_SASL ) {
390 #ifdef HAVE_CYRUS_SASL
391 int minssf = 0, maxssf = 0;
393 if ( sasl_integrity > 0 )
395 if ( sasl_integrity > 1 )
397 if ( sasl_privacy > 0 )
398 maxssf = 100000; /* Something big value */
399 if ( sasl_privacy > 1 )
402 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MINSSF,
403 (void *)&minssf ) != LDAP_OPT_SUCCESS ) {
404 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MINSSF"
406 return( EXIT_FAILURE );
408 if ( ldap_set_option( ld, LDAP_OPT_X_SASL_MAXSSF,
409 (void *)&maxssf ) != LDAP_OPT_SUCCESS ) {
410 fprintf( stderr, "Could not set LDAP_OPT_X_SASL_MINSSF"
412 return( EXIT_FAILURE );
415 rc = ldap_negotiated_sasl_bind_s( ld, binddn, sasl_authc_id,
416 sasl_authz_id, sasl_mech,
417 passwd.bv_len ? &passwd : NULL,
420 if( rc != LDAP_SUCCESS ) {
421 ldap_perror( ld, "ldap_negotiated_sasl_bind_s" );
422 return( EXIT_FAILURE );
425 fprintf( stderr, "%s was not compiled with SASL support\n",
427 return( EXIT_FAILURE );
431 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
433 ldap_perror( ld, "ldap_bind" );
434 return( EXIT_FAILURE );
445 LDAPControl *ctrls[2];
449 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
450 c.ldctl_value.bv_val = NULL;
451 c.ldctl_value.bv_len = 0;
452 c.ldctl_iscritical = manageDSAit > 1;
454 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
456 if( err != LDAP_OPT_SUCCESS ) {
457 fprintf( stderr, "Could not set Manage DSA IT Control\n" );
458 if( c.ldctl_iscritical ) {
459 exit( EXIT_FAILURE );
465 while (( rc == 0 || contoper ) &&
466 ( rbuf = read_one_record( fp )) != NULL ) {
469 * we assume record is ldif/slapd.replog if the first line
470 * has a colon that appears to the left of any equal signs, OR
471 * if the first line consists entirely of digits (an entry id)
476 use_ldif = ( *rbuf == '#' ) ||
477 (( p = strchr( rbuf, ':' )) != NULL &&
478 ( q = strchr( rbuf, '\n' )) != NULL && p < q &&
479 (( q = strchr( rbuf, '=' )) == NULL || p < q ));
484 if ( !use_ldif && ( q = strchr( rbuf, '\n' )) != NULL ) {
485 for ( p = rbuf; p < q; ++p ) {
486 if ( !isdigit( (unsigned char) *p )) {
497 rc = process_ldif_rec( start, count );
499 rc = process_ldapmod_rec( start );
503 fprintf( stderr, "%s() = %d\n",
504 use_ldif ? "ldif_rec" : "ldapmod_rec" , rc );
518 process_ldif_rec( char *rbuf, int count )
520 char *line, *dn, *type, *value, *newrdn, *newsup, *p;
521 int rc, linenum, modop, replicaport;
523 int expect_modop, expect_sep, expect_ct, expect_newrdn, expect_newsup;
524 int expect_deleteoldrdn, deleteoldrdn;
525 int saw_replica, use_record, new_entry, delete_entry, got_all;
531 rc = got_all = saw_replica = delete_entry = modop = expect_modop = 0;
532 expect_deleteoldrdn = expect_newrdn = expect_newsup = 0;
533 expect_sep = expect_ct = 0;
539 dn = newrdn = newsup = NULL;
541 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
544 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
550 if ( ldif_parse_line( line, &type, &value, &vlen ) < 0 ) {
551 fprintf( stderr, "%s: invalid format (line %d) entry: \"%s\"\n",
552 prog, linenum, dn == NULL ? "" : dn );
553 rc = LDAP_PARAM_ERROR;
558 if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
560 if (( p = strchr( value, ':' )) == NULL ) {
564 replicaport = atoi( p );
566 if ( ldaphost != NULL && strcasecmp( value, ldaphost ) == 0 &&
567 replicaport == ldapport ) {
570 } else if ( count == 1 && linenum == 1 &&
571 strcasecmp( type, T_VERSION_STR ) == 0 )
573 if( vlen == 0 || atoi(value) != 1 ) {
574 fprintf( stderr, "%s: invalid version %s, line %d (ignored)\n",
575 prog, value == NULL ? "(null)" : value, linenum );
579 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
580 if (( dn = strdup( value ? value : "" )) == NULL ) {
582 exit( EXIT_FAILURE );
586 goto end_line; /* skip all lines until we see "dn:" */
591 if ( !use_record && saw_replica ) {
592 printf( "%s: skipping change record for entry: %s\n"
593 "\t(LDAP host/port does not match replica: lines)\n",
597 ber_memfree( value );
601 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
602 if ( strcasecmp( value, T_MODIFYCTSTR ) == 0 ) {
605 } else if ( strcasecmp( value, T_ADDCTSTR ) == 0 ) {
607 } else if ( strcasecmp( value, T_MODRDNCTSTR ) == 0
608 || strcasecmp( value, T_MODDNCTSTR ) == 0
609 || strcasecmp( value, T_RENAMECTSTR ) == 0)
612 } else if ( strcasecmp( value, T_DELETECTSTR ) == 0 ) {
613 got_all = delete_entry = 1;
616 "%s: unknown %s \"%s\" (line %d of entry \"%s\")\n",
617 prog, T_CHANGETYPESTR, value, linenum, dn );
618 rc = LDAP_PARAM_ERROR;
621 } else if ( new ) { /* missing changetype => add */
623 modop = LDAP_MOD_ADD;
625 expect_modop = 1; /* missing changetype => modify */
629 if ( expect_modop ) {
632 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
633 modop = LDAP_MOD_ADD;
635 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
636 modop = LDAP_MOD_REPLACE;
637 addmodifyop( &pmods, modop, value, NULL, 0 );
639 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
640 modop = LDAP_MOD_DELETE;
641 addmodifyop( &pmods, modop, value, NULL, 0 );
643 } else { /* no modify op: use default */
644 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
648 if ( expect_newrdn ) {
649 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
650 if (( newrdn = strdup( value )) == NULL ) {
652 exit( EXIT_FAILURE );
654 expect_deleteoldrdn = 1;
657 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
658 prog, T_NEWRDNSTR, type, linenum, dn );
659 rc = LDAP_PARAM_ERROR;
661 } else if ( expect_deleteoldrdn ) {
662 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
663 deleteoldrdn = ( *value == '0' ) ? 0 : 1;
664 expect_deleteoldrdn = 0;
668 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
669 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
670 rc = LDAP_PARAM_ERROR;
672 } else if ( expect_newsup ) {
673 if ( strcasecmp( type, T_NEWSUPSTR ) == 0 ) {
674 if (( newsup = strdup( value )) == NULL ) {
676 exit( EXIT_FAILURE );
680 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
681 prog, T_NEWSUPSTR, type, linenum, dn );
682 rc = LDAP_PARAM_ERROR;
684 } else if ( got_all ) {
686 "%s: extra lines at end (line %d of entry \"%s\")\n",
688 rc = LDAP_PARAM_ERROR;
690 addmodifyop( &pmods, modop, type, value, vlen );
695 ber_memfree( value );
702 if( version && linenum == 1 ) {
707 if ( delete_entry ) {
709 } else if ( newrdn != NULL ) {
710 rc = domodrdn( dn, newrdn, deleteoldrdn );
712 rc = domodify( dn, pmods, new_entry );
715 if ( rc == LDAP_SUCCESS ) {
723 if ( newrdn != NULL ) {
726 if ( pmods != NULL ) {
727 ldap_mods_free( pmods, 1 );
735 process_ldapmod_rec( char *rbuf )
737 char *line, *dn, *p, *q, *attr, *value;
738 int rc, linenum, modop;
747 while ( rc == 0 && rbuf != NULL && *rbuf != '\0' ) {
749 if (( p = strchr( rbuf, '\n' )) == NULL ) {
752 if ( *(p-1) == '\\' ) { /* lines ending in '\' are continued */
753 SAFEMEMCPY( p - 1, p, strlen( p ) + 1 );
761 if ( dn == NULL ) { /* first line contains DN */
762 if (( dn = strdup( line )) == NULL ) {
764 exit( EXIT_FAILURE );
767 if (( p = strchr( line, '=' )) == NULL ) {
769 p = line + strlen( line );
776 *attr != '\0' && isspace( (unsigned char) *attr ); ++attr ) {
777 ; /* skip attribute leading white space */
780 for ( q = p - 1; q > attr && isspace( (unsigned char) *q ); --q ) {
781 *q = '\0'; /* remove attribute trailing white space */
784 if ( value != NULL ) {
785 while ( isspace( (unsigned char) *value )) {
786 ++value; /* skip value leading white space */
788 for ( q = value + strlen( value ) - 1; q > value &&
789 isspace( (unsigned char) *q ); --q ) {
790 *q = '\0'; /* remove value trailing white space */
792 if ( *value == '\0' ) {
798 if ( value == NULL && new ) {
799 fprintf( stderr, "%s: missing value on line %d (attr=\"%s\")\n",
800 prog, linenum, attr );
801 rc = LDAP_PARAM_ERROR;
805 modop = LDAP_MOD_DELETE;
809 modop = LDAP_MOD_ADD;
813 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
816 addmodifyop( &pmods, modop, attr, value,
817 ( value == NULL ) ? 0 : strlen( value ));
826 rc = LDAP_PARAM_ERROR;
827 } else if (( rc = domodify( dn, pmods, new )) == LDAP_SUCCESS ) {
832 if ( pmods != NULL ) {
833 ldap_mods_free( pmods, 1 );
844 addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
851 modop |= LDAP_MOD_BVALUES;
854 if ( pmods != NULL ) {
855 for ( ; pmods[ i ] != NULL; ++i ) {
856 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
857 pmods[ i ]->mod_op == modop ) {
863 if ( pmods == NULL || pmods[ i ] == NULL ) {
864 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
865 sizeof( LDAPMod * ))) == NULL ) {
867 exit( EXIT_FAILURE );
870 pmods[ i + 1 ] = NULL;
871 if (( pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod )))
874 exit( EXIT_FAILURE );
876 pmods[ i ]->mod_op = modop;
877 if (( pmods[ i ]->mod_type = ber_strdup( attr )) == NULL ) {
879 exit( EXIT_FAILURE );
883 if ( value != NULL ) {
885 if ( pmods[ i ]->mod_bvalues != NULL ) {
886 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
890 if (( pmods[ i ]->mod_bvalues =
891 (struct berval **)ber_memrealloc( pmods[ i ]->mod_bvalues,
892 (j + 2) * sizeof( struct berval * ))) == NULL ) {
893 perror( "ber_realloc" );
894 exit( EXIT_FAILURE );
896 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
897 if (( bvp = (struct berval *)ber_memalloc( sizeof( struct berval )))
899 perror( "ber_memalloc" );
900 exit( EXIT_FAILURE );
902 pmods[ i ]->mod_bvalues[ j ] = bvp;
904 if ( valsfromfiles && *value == '/' ) { /* get value from file */
905 if ( fromfile( value, bvp ) < 0 ) {
906 exit( EXIT_FAILURE );
910 if (( bvp->bv_val = (char *)ber_memalloc( vlen + 1 )) == NULL ) {
912 exit( EXIT_FAILURE );
914 SAFEMEMCPY( bvp->bv_val, value, vlen );
915 bvp->bv_val[ vlen ] = '\0';
922 domodify( char *dn, LDAPMod **pmods, int newentry )
924 int i, j, k, notascii, op;
927 if ( pmods == NULL ) {
928 fprintf( stderr, "%s: no attributes to change or add (entry=\"%s\")\n",
930 return( LDAP_PARAM_ERROR );
934 for ( i = 0; pmods[ i ] != NULL; ++i ) {
935 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
936 printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
937 "replace" : op == LDAP_MOD_ADD ?
938 "add" : "delete", pmods[ i ]->mod_type );
939 if ( pmods[ i ]->mod_bvalues != NULL ) {
940 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
941 bvp = pmods[ i ]->mod_bvalues[ j ];
943 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
944 if ( !isascii( bvp->bv_val[ k ] )) {
950 printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
952 printf( "\t%s\n", bvp->bv_val );
960 printf( "%sadding new entry \"%s\"\n", not ? "!" : "", dn );
962 printf( "%smodifying entry \"%s\"\n", not ? "!" : "", dn );
967 i = ldap_add_s( ld, dn, pmods );
969 i = ldap_modify_s( ld, dn, pmods );
971 if ( i != LDAP_SUCCESS ) {
972 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
973 } else if ( verbose ) {
974 printf( "modify complete\n" );
991 printf( "%sdeleting entry \"%s\"\n", not ? "!" : "", dn );
993 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
994 ldap_perror( ld, "ldap_delete" );
995 } else if ( verbose ) {
996 printf( "delete complete" );
1009 domodrdn( char *dn, char *newrdn, int deleteoldrdn )
1014 printf( "%smodifying rdn of entry \"%s\"\n", not ? "!" : "", dn );
1016 printf( "\tnew RDN: \"%s\" (%skeep existing values)\n",
1017 newrdn, deleteoldrdn ? "do not " : "" );
1020 if (( rc = ldap_modrdn2_s( ld, dn, newrdn, deleteoldrdn ))
1022 ldap_perror( ld, "ldap_modrdn" );
1024 printf( "modrdn completed\n" );
1037 fromfile( char *path, struct berval *bv )
1043 if (( fp = fopen( path, "r" )) == NULL ) {
1048 if ( fseek( fp, 0L, SEEK_END ) != 0 ) {
1054 bv->bv_len = ftell( fp );
1056 if (( bv->bv_val = (char *)ber_memalloc( bv->bv_len )) == NULL ) {
1062 if ( fseek( fp, 0L, SEEK_SET ) != 0 ) {
1065 ber_memfree( bv->bv_val );
1070 rlen = fread( bv->bv_val, 1, bv->bv_len, fp );
1074 if ( (unsigned long) rlen != bv->bv_len ) {
1076 ber_memfree( bv->bv_val );
1081 return( bv->bv_len );
1086 read_one_record( FILE *fp )
1088 char *buf, line[ LDAPMOD_MAXLINE ];
1094 while ( fgets( line, sizeof(line), fp ) != NULL ) {
1095 int len = strlen( line );
1105 if ( lcur + len + 1 > lmax ) {
1106 lmax = LDAPMOD_MAXLINE
1107 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
1109 if (( buf = (char *)realloc( buf, lmax )) == NULL ) {
1110 perror( "realloc" );
1111 exit( EXIT_FAILURE );
1115 strcpy( buf + lcur, line );