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 *ldapuri = NULL;
40 static char *ldaphost = NULL;
41 static int ldapport = 0;
42 #ifdef HAVE_CYRUS_SASL
43 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
44 static char *sasl_realm = NULL;
45 static char *sasl_authc_id = NULL;
46 static char *sasl_authz_id = NULL;
47 static char *sasl_mech = NULL;
48 static char *sasl_secprops = NULL;
50 static int use_tls = 0;
51 static int ldapadd, replace, not, verbose, contoper, force;
52 static LDAP *ld = NULL;
54 #define LDAPMOD_MAXLINE 4096
56 /* strings found in replog/LDIF entries (mostly lifted from slurpd/slurp.h) */
57 #define T_VERSION_STR "version"
58 #define T_REPLICA_STR "replica"
60 #define T_CHANGETYPESTR "changetype"
61 #define T_ADDCTSTR "add"
62 #define T_MODIFYCTSTR "modify"
63 #define T_DELETECTSTR "delete"
64 #define T_MODRDNCTSTR "modrdn"
65 #define T_MODDNCTSTR "moddn"
66 #define T_RENAMECTSTR "rename"
67 #define T_MODOPADDSTR "add"
68 #define T_MODOPREPLACESTR "replace"
69 #define T_MODOPDELETESTR "delete"
70 #define T_MODSEPSTR "-"
71 #define T_NEWRDNSTR "newrdn"
72 #define T_DELETEOLDRDNSTR "deleteoldrdn"
73 #define T_NEWSUPSTR "newsuperior"
76 static void usage LDAP_P(( const char *prog )) LDAP_GCCATTR((noreturn));
77 static int process_ldif_rec LDAP_P(( char *rbuf, int count ));
78 static void addmodifyop LDAP_P((
79 LDAPMod ***pmodsp, int modop,
81 struct berval *value ));
82 static int domodify LDAP_P((
86 static int dodelete LDAP_P((
88 static int dorename LDAP_P((
93 static char *read_one_record LDAP_P(( FILE *fp ));
96 usage( const char *prog )
99 "Add or modify entries from an LDAP server\n\n"
100 "usage: %s [options]\n"
101 " The list of desired operations are read from stdin or from the file\n"
102 " specified by \"-f file\".\n"
103 "Add or modify options:\n"
104 " -a add values (default%s)\n"
105 " -r replace values\n"
106 " -F force all changes records to be used\n"
109 " -d level set LDAP debugging level to `level'\n"
110 " -D binddn bind DN\n"
111 " -f file read operations from `file'\n"
112 " -h host LDAP server\n"
113 " -I use SASL Interactive mode\n"
114 " -k use Kerberos authentication\n"
115 " -K like -k, but do only step 1 of the Kerberos bind\n"
116 " -M enable Manage DSA IT control (-MM to make critical)\n"
117 " -n show what would be done but don't actually search\n"
118 " -O props SASL security properties\n"
119 " -p port port on LDAP server\n"
120 " -P version procotol version (default: 3)\n"
121 " -Q use SASL Quiet mode\n"
122 " -R realm SASL realm\n"
123 " -U user SASL authentication identity (username)\n"
124 " -v run in verbose mode (diagnostics to standard output)\n"
125 " -w passwd bind passwd (for simple authentication)\n"
126 " -W prompt for bind passwd\n"
127 " -x Simple authentication\n"
128 " -X id SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
129 " -Y mech SASL mechanism\n"
130 " -Z Start TLS request (-ZZ to require successful response)\n"
131 , prog, (strcmp( prog, "ldapadd" ) ? " is to replace" : "") );
133 exit( EXIT_FAILURE );
138 main( int argc, char **argv )
140 char *infile, *rbuf, *start;
142 int rc, i, authmethod, version, want_bindpw, debug, manageDSAit, referrals;
145 if (( prog = strrchr( argv[ 0 ], *LDAP_DIRSEP )) == NULL ) {
151 /* Print usage when no parameters */
152 if( argc < 2 ) usage( prog );
154 ldapadd = ( strcmp( prog, "ldapadd" ) == 0 );
157 not = verbose = want_bindpw = debug = manageDSAit = referrals = 0;
161 while (( i = getopt( argc, argv, "acrf:F" "Cd:D:h:H:IkKMnO:p:P:QRU:vw:WxX:Y:Z" )) != EOF ) {
167 case 'c': /* continuous operation */
170 case 'f': /* read from file */
171 if( infile != NULL ) {
172 fprintf( stderr, "%s: -f previously specified\n" );
175 infile = strdup( optarg );
177 case 'F': /* force all changes records to be used */
180 case 'r': /* default is to replace rather than add values */
189 debug |= atoi( optarg );
191 case 'D': /* bind DN */
192 if( binddn != NULL ) {
193 fprintf( stderr, "%s: -D previously specified\n" );
196 binddn = strdup( optarg );
198 case 'h': /* ldap host */
199 if( ldapuri != NULL ) {
200 fprintf( stderr, "%s: -h incompatible with -H\n" );
203 if( ldaphost != NULL ) {
204 fprintf( stderr, "%s: -h previously specified\n" );
207 ldaphost = strdup( optarg );
209 case 'H': /* ldap URI */
210 if( ldaphost != NULL ) {
211 fprintf( stderr, "%s: -H incompatible with -h\n" );
215 fprintf( stderr, "%s: -H incompatible with -p\n" );
218 if( ldapuri != NULL ) {
219 fprintf( stderr, "%s: -H previously specified\n" );
222 ldapuri = strdup( optarg );
225 #ifdef HAVE_CYRUS_SASL
226 if( version == LDAP_VERSION2 ) {
227 fprintf( stderr, "%s: -I incompatible with version %d\n",
231 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
232 fprintf( stderr, "%s: incompatible previous "
233 "authentication choice\n",
237 authmethod = LDAP_AUTH_SASL;
238 version = LDAP_VERSION3;
239 sasl_flags = LDAP_SASL_INTERACTIVE;
242 fprintf( stderr, "%s: was not compiled with SASL support\n",
244 return( EXIT_FAILURE );
246 case 'k': /* kerberos bind */
247 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
248 if( version > LDAP_VERSION2 ) {
249 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
254 if( authmethod != -1 ) {
255 fprintf( stderr, "%s: -k incompatible with previous "
256 "authentication choice\n", prog );
260 authmethod = LDAP_AUTH_KRBV4;
262 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
266 case 'K': /* kerberos bind, part one only */
267 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
268 if( version > LDAP_VERSION2 ) {
269 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
273 if( authmethod != -1 ) {
274 fprintf( stderr, "%s: incompatible with previous "
275 "authentication choice\n", prog );
279 authmethod = LDAP_AUTH_KRBV41;
281 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
282 return( EXIT_FAILURE );
286 /* enable Manage DSA IT */
287 if( version == LDAP_VERSION2 ) {
288 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
293 version = LDAP_VERSION3;
295 case 'n': /* print deletes, don't actually do them */
299 #ifdef HAVE_CYRUS_SASL
300 if( sasl_secprops != NULL ) {
301 fprintf( stderr, "%s: -O previously specified\n" );
304 if( version == LDAP_VERSION2 ) {
305 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
309 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
310 fprintf( stderr, "%s: incompatible previous "
311 "authentication choice\n", prog );
314 authmethod = LDAP_AUTH_SASL;
315 version = LDAP_VERSION3;
316 sasl_secprops = strdup( optarg );
318 fprintf( stderr, "%s: not compiled with SASL support\n",
320 return( EXIT_FAILURE );
325 fprintf( stderr, "%s: -p previously specified\n" );
328 ldapport = atoi( optarg );
331 switch( atoi(optarg) ) {
333 if( version == LDAP_VERSION3 ) {
334 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
338 version = LDAP_VERSION2;
341 if( version == LDAP_VERSION2 ) {
342 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
346 version = LDAP_VERSION3;
349 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
352 return( EXIT_FAILURE );
355 #ifdef HAVE_CYRUS_SASL
356 if( version == LDAP_VERSION2 ) {
357 fprintf( stderr, "%s: -Q incompatible with version %d\n",
361 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
362 fprintf( stderr, "%s: incompatible previous "
363 "authentication choice\n",
367 authmethod = LDAP_AUTH_SASL;
368 version = LDAP_VERSION3;
369 sasl_flags = LDAP_SASL_QUIET;
372 fprintf( stderr, "%s: not compiled with SASL support\n",
374 return( EXIT_FAILURE );
377 #ifdef HAVE_CYRUS_SASL
378 if( sasl_realm != NULL ) {
379 fprintf( stderr, "%s: -R previously specified\n" );
382 if( version == LDAP_VERSION2 ) {
383 fprintf( stderr, "%s: -R incompatible with version %d\n",
387 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
388 fprintf( stderr, "%s: incompatible previous "
389 "authentication choice\n",
393 authmethod = LDAP_AUTH_SASL;
394 version = LDAP_VERSION3;
395 sasl_realm = strdup( optarg );
397 fprintf( stderr, "%s: not compiled with SASL support\n",
399 return( EXIT_FAILURE );
403 #ifdef HAVE_CYRUS_SASL
404 if( sasl_authc_id != NULL ) {
405 fprintf( stderr, "%s: -U previously specified\n" );
408 if( version == LDAP_VERSION2 ) {
409 fprintf( stderr, "%s: -U incompatible with version %d\n",
413 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
414 fprintf( stderr, "%s: incompatible previous "
415 "authentication choice\n",
419 authmethod = LDAP_AUTH_SASL;
420 version = LDAP_VERSION3;
421 sasl_authc_id = strdup( optarg );
423 fprintf( stderr, "%s: not compiled with SASL support\n",
425 return( EXIT_FAILURE );
428 case 'v': /* verbose mode */
431 case 'w': /* password */
432 passwd.bv_val = strdup( optarg );
436 for( p = optarg; *p == '\0'; p++ ) {
440 passwd.bv_len = strlen( passwd.bv_val );
446 #ifdef HAVE_CYRUS_SASL
447 if( sasl_mech != NULL ) {
448 fprintf( stderr, "%s: -Y previously specified\n" );
451 if( version == LDAP_VERSION2 ) {
452 fprintf( stderr, "%s: -Y incompatible with version %d\n",
456 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
457 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
460 authmethod = LDAP_AUTH_SASL;
461 version = LDAP_VERSION3;
462 sasl_mech = strdup( optarg );
464 fprintf( stderr, "%s: not compiled with SASL support\n",
466 return( EXIT_FAILURE );
470 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
471 fprintf( stderr, "%s: incompatible with previous "
472 "authentication choice\n", prog );
475 authmethod = LDAP_AUTH_SIMPLE;
478 #ifdef HAVE_CYRUS_SASL
479 if( sasl_authz_id != NULL ) {
480 fprintf( stderr, "%s: -X previously specified\n" );
483 if( version == LDAP_VERSION2 ) {
484 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
488 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
489 fprintf( stderr, "%s: -X incompatible with "
490 "authentication choice\n", prog );
493 authmethod = LDAP_AUTH_SASL;
494 version = LDAP_VERSION3;
495 sasl_authz_id = strdup( optarg );
497 fprintf( stderr, "%s: not compiled with SASL support\n",
499 return( EXIT_FAILURE );
504 if( version == LDAP_VERSION2 ) {
505 fprintf( stderr, "%s: -Z incompatible with version %d\n",
509 version = LDAP_VERSION3;
512 fprintf( stderr, "%s: not compiled with TLS support\n",
514 return( EXIT_FAILURE );
518 fprintf( stderr, "%s: unrecongized option -%c\n",
525 version = LDAP_VERSION3;
527 if (authmethod == -1 && version > LDAP_VERSION2) {
528 #ifdef HAVE_CYRUS_SASL
529 authmethod = LDAP_AUTH_SASL;
531 authmethod = LDAP_AUTH_SIMPLE;
535 if ( argc != optind )
538 if ( infile != NULL ) {
539 if (( fp = fopen( infile, "r" )) == NULL ) {
541 return( EXIT_FAILURE );
548 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
549 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
551 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
552 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
558 (void) SIGNAL( SIGPIPE, SIG_IGN );
562 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
564 fprintf( stderr, "ldap_init( %s, %d )\n",
565 ldaphost != NULL ? ldaphost : "<DEFAULT>",
568 ld = ldap_init( ldaphost, ldapport );
572 fprintf( stderr, "ldap_initialize( %s )\n",
573 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
575 (void) ldap_initialize( &ld, ldapuri );
579 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n", rc );
584 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
585 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
587 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
588 referrals ? "on" : "off" );
593 if (version == -1 ) {
597 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
598 != LDAP_OPT_SUCCESS )
600 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
605 if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
607 ldap_perror( ld, "ldap_start_tls" );
608 return( EXIT_FAILURE );
610 fprintf( stderr, "WARNING: could not start TLS\n" );
614 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
615 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
618 if ( authmethod == LDAP_AUTH_SASL ) {
619 #ifdef HAVE_CYRUS_SASL
622 if( sasl_secprops != NULL ) {
623 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
624 (void *) sasl_secprops );
626 if( rc != LDAP_OPT_SUCCESS ) {
628 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
630 return( EXIT_FAILURE );
634 defaults = lutil_sasl_defaults( ld,
641 rc = ldap_sasl_interactive_bind_s( ld, binddn,
642 sasl_mech, NULL, NULL,
643 sasl_flags, lutil_sasl_interact, defaults );
645 if( rc != LDAP_SUCCESS ) {
646 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
647 return( EXIT_FAILURE );
650 fprintf( stderr, "%s: not compiled with SASL support\n",
652 return( EXIT_FAILURE );
656 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
658 ldap_perror( ld, "ldap_bind" );
659 return( EXIT_FAILURE );
670 LDAPControl *ctrls[2];
674 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
675 c.ldctl_value.bv_val = NULL;
676 c.ldctl_value.bv_len = 0;
677 c.ldctl_iscritical = manageDSAit > 1;
679 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
681 if( err != LDAP_OPT_SUCCESS ) {
682 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
683 c.ldctl_iscritical ? "critical " : "" );
684 if( c.ldctl_iscritical ) {
685 exit( EXIT_FAILURE );
691 while (( rc == 0 || contoper ) &&
692 ( rbuf = read_one_record( fp )) != NULL ) {
697 rc = process_ldif_rec( start, count );
700 fprintf( stderr, "ldif_record() = %d\n", rc );
713 process_ldif_rec( char *rbuf, int count )
715 char *line, *dn, *type, *newrdn, *newsup, *p;
716 int rc, linenum, modop, replicaport;
717 int expect_modop, expect_sep, expect_ct, expect_newrdn, expect_newsup;
718 int expect_deleteoldrdn, deleteoldrdn;
719 int saw_replica, use_record, new_entry, delete_entry, got_all;
726 rc = got_all = saw_replica = delete_entry = modop = expect_modop = 0;
727 expect_deleteoldrdn = expect_newrdn = expect_newsup = 0;
728 expect_sep = expect_ct = 0;
734 dn = newrdn = newsup = NULL;
736 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
739 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
745 if ( ldif_parse_line( line, &type, &val.bv_val, &val.bv_len ) < 0 ) {
746 fprintf( stderr, "%s: invalid format (line %d) entry: \"%s\"\n",
747 prog, linenum, dn == NULL ? "" : dn );
748 rc = LDAP_PARAM_ERROR;
753 if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
755 if (( p = strchr( val.bv_val, ':' )) == NULL ) {
759 replicaport = atoi( p );
761 if ( ldaphost != NULL && strcasecmp( val.bv_val, ldaphost ) == 0 &&
762 replicaport == ldapport ) {
765 } else if ( count == 1 && linenum == 1 &&
766 strcasecmp( type, T_VERSION_STR ) == 0 )
768 if( val.bv_len == 0 || atoi(val.bv_val) != 1 ) {
769 fprintf( stderr, "%s: invalid version %s, line %d (ignored)\n",
770 prog, val.bv_val == NULL ? "(null)" : val.bv_val, linenum );
774 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
775 if (( dn = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
777 exit( EXIT_FAILURE );
781 goto end_line; /* skip all lines until we see "dn:" */
786 if ( !use_record && saw_replica ) {
787 printf( "%s: skipping change record for entry: %s\n"
788 "\t(LDAP host/port does not match replica: lines)\n",
792 ber_memfree( val.bv_val );
796 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
797 if ( strcasecmp( val.bv_val, T_MODIFYCTSTR ) == 0 ) {
800 } else if ( strcasecmp( val.bv_val, T_ADDCTSTR ) == 0 ) {
802 } else if ( strcasecmp( val.bv_val, T_MODRDNCTSTR ) == 0
803 || strcasecmp( val.bv_val, T_MODDNCTSTR ) == 0
804 || strcasecmp( val.bv_val, T_RENAMECTSTR ) == 0)
807 } else if ( strcasecmp( val.bv_val, T_DELETECTSTR ) == 0 ) {
808 got_all = delete_entry = 1;
811 "%s: unknown %s \"%s\" (line %d of entry \"%s\")\n",
812 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
813 rc = LDAP_PARAM_ERROR;
816 } else if ( ldapadd ) { /* missing changetype => add */
818 modop = LDAP_MOD_ADD;
820 expect_modop = 1; /* missing changetype => modify */
824 if ( expect_modop ) {
827 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
828 modop = LDAP_MOD_ADD;
830 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
831 modop = LDAP_MOD_REPLACE;
832 addmodifyop( &pmods, modop, val.bv_val, NULL );
834 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
835 modop = LDAP_MOD_DELETE;
836 addmodifyop( &pmods, modop, val.bv_val, NULL );
838 } else { /* no modify op: use default */
839 modop = replace ? LDAP_MOD_REPLACE : LDAP_MOD_ADD;
843 if ( expect_newrdn ) {
844 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
845 if (( newrdn = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
847 exit( EXIT_FAILURE );
849 expect_deleteoldrdn = 1;
852 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
853 prog, T_NEWRDNSTR, type, linenum, dn );
854 rc = LDAP_PARAM_ERROR;
856 } else if ( expect_deleteoldrdn ) {
857 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
858 deleteoldrdn = ( *val.bv_val == '0' ) ? 0 : 1;
859 expect_deleteoldrdn = 0;
863 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
864 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
865 rc = LDAP_PARAM_ERROR;
867 } else if ( expect_newsup ) {
868 if ( strcasecmp( type, T_NEWSUPSTR ) == 0 ) {
869 if (( newsup = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
871 exit( EXIT_FAILURE );
875 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
876 prog, T_NEWSUPSTR, type, linenum, dn );
877 rc = LDAP_PARAM_ERROR;
879 } else if ( got_all ) {
881 "%s: extra lines at end (line %d of entry \"%s\")\n",
883 rc = LDAP_PARAM_ERROR;
885 addmodifyop( &pmods, modop, type, val.bv_val == NULL ? NULL : &val );
890 ber_memfree( val.bv_val );
897 if( version && linenum == 1 ) {
902 if ( delete_entry ) {
904 } else if ( newrdn != NULL ) {
905 rc = dorename( dn, newrdn, newsup, deleteoldrdn );
907 rc = domodify( dn, pmods, new_entry );
910 if ( rc == LDAP_SUCCESS ) {
918 if ( newrdn != NULL ) {
921 if ( pmods != NULL ) {
922 ldap_mods_free( pmods, 1 );
940 modop |= LDAP_MOD_BVALUES;
943 if ( pmods != NULL ) {
944 for ( ; pmods[ i ] != NULL; ++i ) {
945 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
946 pmods[ i ]->mod_op == modop )
953 if ( pmods == NULL || pmods[ i ] == NULL ) {
954 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
955 sizeof( LDAPMod * ))) == NULL )
958 exit( EXIT_FAILURE );
962 pmods[ i + 1 ] = NULL;
964 pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod ));
965 if ( pmods[ i ] == NULL ) {
967 exit( EXIT_FAILURE );
970 pmods[ i ]->mod_op = modop;
971 pmods[ i ]->mod_type = ber_strdup( attr );
972 if ( pmods[ i ]->mod_type == NULL ) {
974 exit( EXIT_FAILURE );
980 if ( pmods[ i ]->mod_bvalues != NULL ) {
981 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
986 pmods[ i ]->mod_bvalues = (struct berval **) ber_memrealloc(
987 pmods[ i ]->mod_bvalues, (j + 2) * sizeof( struct berval * ));
988 if ( pmods[ i ]->mod_bvalues == NULL ) {
989 perror( "ber_realloc" );
990 exit( EXIT_FAILURE );
993 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
994 pmods[ i ]->mod_bvalues[ j ] = ber_bvdup( val );
995 if ( pmods[ i ]->mod_bvalues[ j ] == NULL ) {
996 perror( "ber_bvdup" );
997 exit( EXIT_FAILURE );
1009 int i, j, k, notascii, op;
1012 if ( pmods == NULL ) {
1013 fprintf( stderr, "%s: no attributes to change or add (entry=\"%s\")\n",
1015 return( LDAP_PARAM_ERROR );
1019 for ( i = 0; pmods[ i ] != NULL; ++i ) {
1020 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1021 printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
1022 "replace" : op == LDAP_MOD_ADD ?
1023 "add" : "delete", pmods[ i ]->mod_type );
1024 if ( pmods[ i ]->mod_bvalues != NULL ) {
1025 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
1026 bvp = pmods[ i ]->mod_bvalues[ j ];
1028 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
1029 if ( !isascii( bvp->bv_val[ k ] )) {
1035 printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
1037 printf( "\t%s\n", bvp->bv_val );
1045 printf( "%sadding new entry \"%s\"\n", not ? "!" : "", dn );
1047 printf( "%smodifying entry \"%s\"\n", not ? "!" : "", dn );
1052 i = ldap_add_s( ld, dn, pmods );
1054 i = ldap_modify_s( ld, dn, pmods );
1056 if ( i != LDAP_SUCCESS ) {
1057 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
1058 } else if ( verbose ) {
1059 printf( "modify complete\n" );
1077 printf( "%sdeleting entry \"%s\"\n", not ? "!" : "", dn );
1079 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
1080 ldap_perror( ld, "ldap_delete" );
1081 } else if ( verbose ) {
1082 printf( "delete complete" );
1104 printf( "%smodifying rdn of entry \"%s\"\n", not ? "!" : "", dn );
1106 printf( "\tnew RDN: \"%s\" (%skeep existing values)\n",
1107 newrdn, deleteoldrdn ? "do not " : "" );
1110 if (( rc = ldap_rename2_s( ld, dn, newrdn, newsup, deleteoldrdn ))
1112 ldap_perror( ld, "ldap_modrdn" );
1114 printf( "modrdn completed\n" );
1127 read_one_record( FILE *fp )
1129 char *buf, line[ LDAPMOD_MAXLINE ];
1135 while ( fgets( line, sizeof(line), fp ) != NULL ) {
1136 int len = strlen( line );
1146 if ( lcur + len + 1 > lmax ) {
1147 lmax = LDAPMOD_MAXLINE
1148 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
1150 if (( buf = (char *)realloc( buf, lmax )) == NULL ) {
1151 perror( "realloc" );
1152 exit( EXIT_FAILURE );
1156 strcpy( buf + lcur, line );