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 add values (default%s)\n"
104 " -r replace values\n"
105 " -F force all changes records to be used\n"
108 " -d level set LDAP debugging level to `level'\n"
109 " -D binddn bind DN\n"
110 " -f file read operations from `file'\n"
111 " -h host LDAP server\n"
112 " -I use SASL Interactive mode\n"
113 " -k use Kerberos authentication\n"
114 " -K like -k, but do only step 1 of the Kerberos bind\n"
115 " -M enable Manage DSA IT control (-MM to make critical)\n"
116 " -n show what would be done but don't actually search\n"
117 " -O props SASL security properties\n"
118 " -p port port on LDAP server\n"
119 " -P version procotol version (default: 3)\n"
120 " -Q use SASL Quiet mode\n"
121 " -R realm SASL realm\n"
122 " -U user SASL authentication identity (username)\n"
123 " -v run in verbose mode (diagnostics to standard output)\n"
124 " -w passwd bind passwd (for simple authentication)\n"
125 " -W prompt for bind passwd\n"
126 " -x Simple authentication\n"
127 " -X id SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
128 " -Y mech SASL mechanism\n"
129 " -Z Start TLS request (-ZZ to require successful response)\n"
130 , prog, (strcmp( prog, "ldapadd" ) ? " is to replace" : "") );
132 exit( EXIT_FAILURE );
137 main( int argc, char **argv )
139 char *infile, *rbuf, *start;
141 int rc, i, authmethod, version, want_bindpw, debug, manageDSAit, referrals;
144 if (( prog = strrchr( argv[ 0 ], *LDAP_DIRSEP )) == NULL ) {
150 /* Print usage when no parameters */
151 if( argc < 2 ) usage( prog );
153 ldapadd = ( strcmp( prog, "ldapadd" ) == 0 );
156 not = verbose = want_bindpw = debug = manageDSAit = referrals = 0;
160 while (( i = getopt( argc, argv, "acrf:F" "Cd:D:h:IkKMnO:p:P:QRU:vw:WxX:Y:Z" )) != EOF ) {
166 case 'c': /* continuous operation */
169 case 'f': /* read from file */
170 if( infile != NULL ) {
171 fprintf( stderr, "%s: -f previously specified\n" );
174 infile = strdup( optarg );
176 case 'F': /* force all changes records to be used */
179 case 'r': /* default is to replace rather than add values */
188 debug |= atoi( optarg );
190 case 'D': /* bind DN */
191 if( binddn != NULL ) {
192 fprintf( stderr, "%s: -D previously specified\n" );
195 binddn = strdup( optarg );
197 case 'h': /* ldap host */
198 if( ldaphost != NULL ) {
199 fprintf( stderr, "%s: -h previously specified\n" );
202 ldaphost = strdup( optarg );
205 #ifdef HAVE_CYRUS_SASL
206 if( version == LDAP_VERSION2 ) {
207 fprintf( stderr, "%s: -I incompatible with version %d\n",
211 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
212 fprintf( stderr, "%s: incompatible previous "
213 "authentication choice\n",
217 authmethod = LDAP_AUTH_SASL;
218 version = LDAP_VERSION3;
219 sasl_flags = LDAP_SASL_INTERACTIVE;
222 fprintf( stderr, "%s: was not compiled with SASL support\n",
224 return( EXIT_FAILURE );
226 case 'k': /* kerberos bind */
227 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
228 if( version > LDAP_VERSION2 ) {
229 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
234 if( authmethod != -1 ) {
235 fprintf( stderr, "%s: -k incompatible with previous "
236 "authentication choice\n", prog );
240 authmethod = LDAP_AUTH_KRBV4;
242 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
246 case 'K': /* kerberos bind, part one only */
247 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
248 if( version > LDAP_VERSION2 ) {
249 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
253 if( authmethod != -1 ) {
254 fprintf( stderr, "%s: incompatible with previous "
255 "authentication choice\n", prog );
259 authmethod = LDAP_AUTH_KRBV41;
261 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
262 return( EXIT_FAILURE );
266 /* enable Manage DSA IT */
267 if( version == LDAP_VERSION2 ) {
268 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
273 version = LDAP_VERSION3;
275 case 'n': /* print deletes, don't actually do them */
279 #ifdef HAVE_CYRUS_SASL
280 if( sasl_secprops != NULL ) {
281 fprintf( stderr, "%s: -O previously specified\n" );
284 if( version == LDAP_VERSION2 ) {
285 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
289 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
290 fprintf( stderr, "%s: incompatible previous "
291 "authentication choice\n", prog );
294 authmethod = LDAP_AUTH_SASL;
295 version = LDAP_VERSION3;
296 sasl_secprops = strdup( optarg );
298 fprintf( stderr, "%s: not compiled with SASL support\n",
300 return( EXIT_FAILURE );
305 fprintf( stderr, "%s: -p previously specified\n" );
308 ldapport = atoi( optarg );
311 switch( atoi(optarg) ) {
313 if( version == LDAP_VERSION3 ) {
314 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
318 version = LDAP_VERSION2;
321 if( version == LDAP_VERSION2 ) {
322 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
326 version = LDAP_VERSION3;
329 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
332 return( EXIT_FAILURE );
335 #ifdef HAVE_CYRUS_SASL
336 if( version == LDAP_VERSION2 ) {
337 fprintf( stderr, "%s: -Q incompatible with version %d\n",
341 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
342 fprintf( stderr, "%s: incompatible previous "
343 "authentication choice\n",
347 authmethod = LDAP_AUTH_SASL;
348 version = LDAP_VERSION3;
349 sasl_flags = LDAP_SASL_QUIET;
352 fprintf( stderr, "%s: not compiled with SASL support\n",
354 return( EXIT_FAILURE );
357 #ifdef HAVE_CYRUS_SASL
358 if( sasl_realm != NULL ) {
359 fprintf( stderr, "%s: -R previously specified\n" );
362 if( version == LDAP_VERSION2 ) {
363 fprintf( stderr, "%s: -R incompatible with version %d\n",
367 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
368 fprintf( stderr, "%s: incompatible previous "
369 "authentication choice\n",
373 authmethod = LDAP_AUTH_SASL;
374 version = LDAP_VERSION3;
375 sasl_realm = strdup( optarg );
377 fprintf( stderr, "%s: not compiled with SASL support\n",
379 return( EXIT_FAILURE );
383 #ifdef HAVE_CYRUS_SASL
384 if( sasl_authc_id != NULL ) {
385 fprintf( stderr, "%s: -U previously specified\n" );
388 if( version == LDAP_VERSION2 ) {
389 fprintf( stderr, "%s: -U incompatible with version %d\n",
393 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
394 fprintf( stderr, "%s: incompatible previous "
395 "authentication choice\n",
399 authmethod = LDAP_AUTH_SASL;
400 version = LDAP_VERSION3;
401 sasl_authc_id = strdup( optarg );
403 fprintf( stderr, "%s: not compiled with SASL support\n",
405 return( EXIT_FAILURE );
408 case 'v': /* verbose mode */
411 case 'w': /* password */
412 passwd.bv_val = strdup( optarg );
416 for( p = optarg; *p == '\0'; p++ ) {
420 passwd.bv_len = strlen( passwd.bv_val );
426 #ifdef HAVE_CYRUS_SASL
427 if( sasl_mech != NULL ) {
428 fprintf( stderr, "%s: -Y previously specified\n" );
431 if( version == LDAP_VERSION2 ) {
432 fprintf( stderr, "%s: -Y incompatible with version %d\n",
436 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
437 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
440 authmethod = LDAP_AUTH_SASL;
441 version = LDAP_VERSION3;
442 sasl_mech = strdup( optarg );
444 fprintf( stderr, "%s: not compiled with SASL support\n",
446 return( EXIT_FAILURE );
450 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
451 fprintf( stderr, "%s: incompatible with previous "
452 "authentication choice\n", prog );
455 authmethod = LDAP_AUTH_SIMPLE;
458 #ifdef HAVE_CYRUS_SASL
459 if( sasl_authz_id != NULL ) {
460 fprintf( stderr, "%s: -X previously specified\n" );
463 if( version == LDAP_VERSION2 ) {
464 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
468 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
469 fprintf( stderr, "%s: -X incompatible with "
470 "authentication choice\n", prog );
473 authmethod = LDAP_AUTH_SASL;
474 version = LDAP_VERSION3;
475 sasl_authz_id = strdup( optarg );
477 fprintf( stderr, "%s: not compiled with SASL support\n",
479 return( EXIT_FAILURE );
484 if( version == LDAP_VERSION2 ) {
485 fprintf( stderr, "%s: -Z incompatible with version %d\n",
489 version = LDAP_VERSION3;
492 fprintf( stderr, "%s: not compiled with TLS support\n",
494 return( EXIT_FAILURE );
498 fprintf( stderr, "%s: unrecongized option -%c\n",
505 version = LDAP_VERSION3;
507 if (authmethod == -1 && version > LDAP_VERSION2) {
508 #ifdef HAVE_CYRUS_SASL
509 authmethod = LDAP_AUTH_SASL;
511 authmethod = LDAP_AUTH_SIMPLE;
515 if ( argc != optind )
518 if ( infile != NULL ) {
519 if (( fp = fopen( infile, "r" )) == NULL ) {
521 return( EXIT_FAILURE );
528 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
529 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
531 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
532 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
538 (void) SIGNAL( SIGPIPE, SIG_IGN );
542 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
543 perror( "ldap_init" );
544 return( EXIT_FAILURE );
548 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
549 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
551 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
552 referrals ? "on" : "off" );
557 if (version == -1 ) {
561 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
562 != LDAP_OPT_SUCCESS )
564 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
569 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
571 ldap_perror( ld, "ldap_start_tls" );
572 return( EXIT_FAILURE );
574 fprintf( stderr, "WARNING: could not start TLS\n" );
578 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
579 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
582 if ( authmethod == LDAP_AUTH_SASL ) {
583 #ifdef HAVE_CYRUS_SASL
586 if( sasl_secprops != NULL ) {
587 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
588 (void *) sasl_secprops );
590 if( rc != LDAP_OPT_SUCCESS ) {
592 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
594 return( EXIT_FAILURE );
598 defaults = lutil_sasl_defaults( ld,
605 rc = ldap_sasl_interactive_bind_s( ld, binddn,
606 sasl_mech, NULL, NULL,
607 sasl_flags, lutil_sasl_interact, defaults );
609 if( rc != LDAP_SUCCESS ) {
610 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
611 return( EXIT_FAILURE );
614 fprintf( stderr, "%s: not compiled with SASL support\n",
616 return( EXIT_FAILURE );
620 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
622 ldap_perror( ld, "ldap_bind" );
623 return( EXIT_FAILURE );
634 LDAPControl *ctrls[2];
638 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
639 c.ldctl_value.bv_val = NULL;
640 c.ldctl_value.bv_len = 0;
641 c.ldctl_iscritical = manageDSAit > 1;
643 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
645 if( err != LDAP_OPT_SUCCESS ) {
646 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
647 c.ldctl_iscritical ? "critical " : "" );
648 if( c.ldctl_iscritical ) {
649 exit( EXIT_FAILURE );
655 while (( rc == 0 || contoper ) &&
656 ( rbuf = read_one_record( fp )) != NULL ) {
661 rc = process_ldif_rec( start, count );
664 fprintf( stderr, "ldif_record() = %d\n", rc );
677 process_ldif_rec( char *rbuf, int count )
679 char *line, *dn, *type, *newrdn, *newsup, *p;
680 int rc, linenum, modop, replicaport;
681 int expect_modop, expect_sep, expect_ct, expect_newrdn, expect_newsup;
682 int expect_deleteoldrdn, deleteoldrdn;
683 int saw_replica, use_record, new_entry, delete_entry, got_all;
690 rc = got_all = saw_replica = delete_entry = modop = expect_modop = 0;
691 expect_deleteoldrdn = expect_newrdn = expect_newsup = 0;
692 expect_sep = expect_ct = 0;
698 dn = newrdn = newsup = NULL;
700 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
703 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
709 if ( ldif_parse_line( line, &type, &val.bv_val, &val.bv_len ) < 0 ) {
710 fprintf( stderr, "%s: invalid format (line %d) entry: \"%s\"\n",
711 prog, linenum, dn == NULL ? "" : dn );
712 rc = LDAP_PARAM_ERROR;
717 if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
719 if (( p = strchr( val.bv_val, ':' )) == NULL ) {
723 replicaport = atoi( p );
725 if ( ldaphost != NULL && strcasecmp( val.bv_val, ldaphost ) == 0 &&
726 replicaport == ldapport ) {
729 } else if ( count == 1 && linenum == 1 &&
730 strcasecmp( type, T_VERSION_STR ) == 0 )
732 if( val.bv_len == 0 || atoi(val.bv_val) != 1 ) {
733 fprintf( stderr, "%s: invalid version %s, line %d (ignored)\n",
734 prog, val.bv_val == NULL ? "(null)" : val.bv_val, linenum );
738 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
739 if (( dn = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
741 exit( EXIT_FAILURE );
745 goto end_line; /* skip all lines until we see "dn:" */
750 if ( !use_record && saw_replica ) {
751 printf( "%s: skipping change record for entry: %s\n"
752 "\t(LDAP host/port does not match replica: lines)\n",
756 ber_memfree( val.bv_val );
760 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
761 if ( strcasecmp( val.bv_val, T_MODIFYCTSTR ) == 0 ) {
764 } else if ( strcasecmp( val.bv_val, T_ADDCTSTR ) == 0 ) {
766 } else if ( strcasecmp( val.bv_val, T_MODRDNCTSTR ) == 0
767 || strcasecmp( val.bv_val, T_MODDNCTSTR ) == 0
768 || strcasecmp( val.bv_val, T_RENAMECTSTR ) == 0)
771 } else if ( strcasecmp( val.bv_val, T_DELETECTSTR ) == 0 ) {
772 got_all = delete_entry = 1;
775 "%s: unknown %s \"%s\" (line %d of entry \"%s\")\n",
776 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
777 rc = LDAP_PARAM_ERROR;
780 } else if ( ldapadd ) { /* missing changetype => add */
782 modop = LDAP_MOD_ADD;
784 expect_modop = 1; /* missing changetype => modify */
788 if ( expect_modop ) {
791 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
792 modop = LDAP_MOD_ADD;
794 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
795 modop = LDAP_MOD_REPLACE;
796 addmodifyop( &pmods, modop, val.bv_val, NULL );
798 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
799 modop = LDAP_MOD_DELETE;
800 addmodifyop( &pmods, modop, val.bv_val, NULL );
802 } else { /* no modify op: use default */
803 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
807 if ( expect_newrdn ) {
808 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
809 if (( newrdn = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
811 exit( EXIT_FAILURE );
813 expect_deleteoldrdn = 1;
816 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
817 prog, T_NEWRDNSTR, type, linenum, dn );
818 rc = LDAP_PARAM_ERROR;
820 } else if ( expect_deleteoldrdn ) {
821 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
822 deleteoldrdn = ( *val.bv_val == '0' ) ? 0 : 1;
823 expect_deleteoldrdn = 0;
827 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
828 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
829 rc = LDAP_PARAM_ERROR;
831 } else if ( expect_newsup ) {
832 if ( strcasecmp( type, T_NEWSUPSTR ) == 0 ) {
833 if (( newsup = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
835 exit( EXIT_FAILURE );
839 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
840 prog, T_NEWSUPSTR, type, linenum, dn );
841 rc = LDAP_PARAM_ERROR;
843 } else if ( got_all ) {
845 "%s: extra lines at end (line %d of entry \"%s\")\n",
847 rc = LDAP_PARAM_ERROR;
849 addmodifyop( &pmods, modop, type, val.bv_val == NULL ? NULL : &val );
854 ber_memfree( val.bv_val );
861 if( version && linenum == 1 ) {
866 if ( delete_entry ) {
868 } else if ( newrdn != NULL ) {
869 rc = dorename( dn, newrdn, newsup, deleteoldrdn );
871 rc = domodify( dn, pmods, new_entry );
874 if ( rc == LDAP_SUCCESS ) {
882 if ( newrdn != NULL ) {
885 if ( pmods != NULL ) {
886 ldap_mods_free( pmods, 1 );
904 modop |= LDAP_MOD_BVALUES;
907 if ( pmods != NULL ) {
908 for ( ; pmods[ i ] != NULL; ++i ) {
909 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
910 pmods[ i ]->mod_op == modop )
917 if ( pmods == NULL || pmods[ i ] == NULL ) {
918 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
919 sizeof( LDAPMod * ))) == NULL )
922 exit( EXIT_FAILURE );
926 pmods[ i + 1 ] = NULL;
928 pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod ));
929 if ( pmods[ i ] == NULL ) {
931 exit( EXIT_FAILURE );
934 pmods[ i ]->mod_op = modop;
935 pmods[ i ]->mod_type = ber_strdup( attr );
936 if ( pmods[ i ]->mod_type == NULL ) {
938 exit( EXIT_FAILURE );
944 if ( pmods[ i ]->mod_bvalues != NULL ) {
945 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
950 pmods[ i ]->mod_bvalues = (struct berval **) ber_memrealloc(
951 pmods[ i ]->mod_bvalues, (j + 2) * sizeof( struct berval * ));
952 if ( pmods[ i ]->mod_bvalues == NULL ) {
953 perror( "ber_realloc" );
954 exit( EXIT_FAILURE );
957 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
958 pmods[ i ]->mod_bvalues[ j ] = ber_bvdup( val );
959 if ( pmods[ i ]->mod_bvalues[ j ] == NULL ) {
960 perror( "ber_bvdup" );
961 exit( EXIT_FAILURE );
973 int i, j, k, notascii, op;
976 if ( pmods == NULL ) {
977 fprintf( stderr, "%s: no attributes to change or add (entry=\"%s\")\n",
979 return( LDAP_PARAM_ERROR );
983 for ( i = 0; pmods[ i ] != NULL; ++i ) {
984 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
985 printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
986 "replace" : op == LDAP_MOD_ADD ?
987 "add" : "delete", pmods[ i ]->mod_type );
988 if ( pmods[ i ]->mod_bvalues != NULL ) {
989 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
990 bvp = pmods[ i ]->mod_bvalues[ j ];
992 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
993 if ( !isascii( bvp->bv_val[ k ] )) {
999 printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
1001 printf( "\t%s\n", bvp->bv_val );
1009 printf( "%sadding new entry \"%s\"\n", not ? "!" : "", dn );
1011 printf( "%smodifying entry \"%s\"\n", not ? "!" : "", dn );
1016 i = ldap_add_s( ld, dn, pmods );
1018 i = ldap_modify_s( ld, dn, pmods );
1020 if ( i != LDAP_SUCCESS ) {
1021 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
1022 } else if ( verbose ) {
1023 printf( "modify complete\n" );
1041 printf( "%sdeleting entry \"%s\"\n", not ? "!" : "", dn );
1043 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
1044 ldap_perror( ld, "ldap_delete" );
1045 } else if ( verbose ) {
1046 printf( "delete complete" );
1068 printf( "%smodifying rdn of entry \"%s\"\n", not ? "!" : "", dn );
1070 printf( "\tnew RDN: \"%s\" (%skeep existing values)\n",
1071 newrdn, deleteoldrdn ? "do not " : "" );
1074 if (( rc = ldap_rename2_s( ld, dn, newrdn, newsup, deleteoldrdn ))
1076 ldap_perror( ld, "ldap_modrdn" );
1078 printf( "modrdn completed\n" );
1091 read_one_record( FILE *fp )
1093 char *buf, line[ LDAPMOD_MAXLINE ];
1099 while ( fgets( line, sizeof(line), fp ) != NULL ) {
1100 int len = strlen( line );
1110 if ( lcur + len + 1 > lmax ) {
1111 lmax = LDAPMOD_MAXLINE
1112 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
1114 if (( buf = (char *)realloc( buf, lmax )) == NULL ) {
1115 perror( "realloc" );
1116 exit( EXIT_FAILURE );
1120 strcpy( buf + lcur, line );