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"
38 static char *binddn = NULL;
39 static struct berval passwd = { 0, NULL };
40 static char *ldapuri = NULL;
41 static char *ldaphost = NULL;
42 static int ldapport = 0;
43 #ifdef HAVE_CYRUS_SASL
44 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
45 static char *sasl_realm = NULL;
46 static char *sasl_authc_id = NULL;
47 static char *sasl_authz_id = NULL;
48 static char *sasl_mech = NULL;
49 static char *sasl_secprops = NULL;
51 static int use_tls = 0;
52 static int ldapadd, not, verbose, contoper, force;
53 static LDAP *ld = NULL;
55 #define LDAPMOD_MAXLINE 4096
57 /* strings found in replog/LDIF entries (mostly lifted from slurpd/slurp.h) */
58 #define T_VERSION_STR "version"
59 #define T_REPLICA_STR "replica"
61 #define T_CHANGETYPESTR "changetype"
62 #define T_ADDCTSTR "add"
63 #define T_MODIFYCTSTR "modify"
64 #define T_DELETECTSTR "delete"
65 #define T_MODRDNCTSTR "modrdn"
66 #define T_MODDNCTSTR "moddn"
67 #define T_RENAMECTSTR "rename"
68 #define T_MODOPADDSTR "add"
69 #define T_MODOPREPLACESTR "replace"
70 #define T_MODOPDELETESTR "delete"
71 #define T_MODSEPSTR "-"
72 #define T_NEWRDNSTR "newrdn"
73 #define T_DELETEOLDRDNSTR "deleteoldrdn"
74 #define T_NEWSUPSTR "newsuperior"
77 static void usage LDAP_P(( const char *prog )) LDAP_GCCATTR((noreturn));
78 static int process_ldif_rec LDAP_P(( char *rbuf, int count ));
79 static void addmodifyop LDAP_P((
80 LDAPMod ***pmodsp, int modop,
82 struct berval *value ));
83 static int domodify LDAP_P((
87 static int dodelete LDAP_P((
89 static int dorename LDAP_P((
94 static char *read_one_record LDAP_P(( FILE *fp ));
97 usage( const char *prog )
100 "Add or modify entries from an LDAP server\n\n"
101 "usage: %s [options]\n"
102 " The list of desired operations are read from stdin or from the file\n"
103 " specified by \"-f file\".\n"
104 "Add or modify options:\n"
105 " -a add values (default%s)\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 " -H URI LDAP Uniform Resource Indentifier(s)\n"
114 " -I use SASL Interactive mode\n"
115 " -k use Kerberos authentication\n"
116 " -K like -k, but do only step 1 of the Kerberos bind\n"
117 " -M enable Manage DSA IT control (-MM to make critical)\n"
118 " -n show what would be done but don't actually search\n"
119 " -O props SASL security properties\n"
120 " -p port port on LDAP server\n"
121 " -P version procotol version (default: 3)\n"
122 " -Q use SASL Quiet mode\n"
123 " -R realm SASL realm\n"
124 " -U authcid SASL authentication identity\n"
125 " -v run in verbose mode (diagnostics to standard output)\n"
126 " -w passwd bind passwd (for simple authentication)\n"
127 " -W prompt for bind passwd\n"
128 " -x Simple authentication\n"
129 " -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
130 " -Y mech SASL mechanism\n"
131 " -Z Start TLS request (-ZZ to require successful response)\n"
132 , prog, (strcmp( prog, "ldapadd" ) ? " is to replace" : "") );
134 exit( EXIT_FAILURE );
139 main( int argc, char **argv )
141 char *infile, *rbuf, *start;
143 int rc, i, authmethod, version, want_bindpw, debug, manageDSAit, referrals;
146 if (( prog = strrchr( argv[ 0 ], *LDAP_DIRSEP )) == NULL ) {
152 /* Print usage when no parameters */
153 if( argc < 2 ) usage( prog );
155 /* strncmp instead of strcmp since NT binaries carry .exe extension */
156 ldapadd = ( strncmp( prog, "ldapadd", sizeof("ldapadd"-1 ) == 0 );
159 not = verbose = want_bindpw = debug = manageDSAit = referrals = 0;
163 while (( i = getopt( argc, argv, "acrf:F"
164 "Cd:D:h:H:IkKMnO:p:P:QR:U:vw:WxX:Y:Z" )) != EOF )
171 case 'c': /* continuous operation */
174 case 'f': /* read from file */
175 if( infile != NULL ) {
176 fprintf( stderr, "%s: -f previously specified\n", prog );
179 infile = strdup( optarg );
181 case 'F': /* force all changes records to be used */
190 debug |= atoi( optarg );
192 case 'D': /* bind DN */
193 if( binddn != NULL ) {
194 fprintf( stderr, "%s: -D previously specified\n", prog );
197 binddn = strdup( optarg );
199 case 'h': /* ldap host */
200 if( ldapuri != NULL ) {
201 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
204 if( ldaphost != NULL ) {
205 fprintf( stderr, "%s: -h previously specified\n", prog );
208 ldaphost = strdup( optarg );
210 case 'H': /* ldap URI */
211 if( ldaphost != NULL ) {
212 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
216 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
219 if( ldapuri != NULL ) {
220 fprintf( stderr, "%s: -H previously specified\n", prog );
223 ldapuri = strdup( optarg );
226 #ifdef HAVE_CYRUS_SASL
227 if( version == LDAP_VERSION2 ) {
228 fprintf( stderr, "%s: -I incompatible with version %d\n",
232 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
233 fprintf( stderr, "%s: incompatible previous "
234 "authentication choice\n",
238 authmethod = LDAP_AUTH_SASL;
239 version = LDAP_VERSION3;
240 sasl_flags = LDAP_SASL_INTERACTIVE;
243 fprintf( stderr, "%s: was not compiled with SASL support\n",
245 return( EXIT_FAILURE );
247 case 'k': /* kerberos bind */
248 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
249 if( version > LDAP_VERSION2 ) {
250 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
255 if( authmethod != -1 ) {
256 fprintf( stderr, "%s: -k incompatible with previous "
257 "authentication choice\n", prog );
261 authmethod = LDAP_AUTH_KRBV4;
263 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
267 case 'K': /* kerberos bind, part one only */
268 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
269 if( version > LDAP_VERSION2 ) {
270 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
274 if( authmethod != -1 ) {
275 fprintf( stderr, "%s: incompatible with previous "
276 "authentication choice\n", prog );
280 authmethod = LDAP_AUTH_KRBV41;
282 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
283 return( EXIT_FAILURE );
287 /* enable Manage DSA IT */
288 if( version == LDAP_VERSION2 ) {
289 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
294 version = LDAP_VERSION3;
296 case 'n': /* print deletes, don't actually do them */
300 #ifdef HAVE_CYRUS_SASL
301 if( sasl_secprops != NULL ) {
302 fprintf( stderr, "%s: -O previously specified\n", prog );
305 if( version == LDAP_VERSION2 ) {
306 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
310 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
311 fprintf( stderr, "%s: incompatible previous "
312 "authentication choice\n", prog );
315 authmethod = LDAP_AUTH_SASL;
316 version = LDAP_VERSION3;
317 sasl_secprops = strdup( optarg );
319 fprintf( stderr, "%s: not compiled with SASL support\n",
321 return( EXIT_FAILURE );
326 fprintf( stderr, "%s: -p previously specified\n", prog );
329 ldapport = atoi( optarg );
332 switch( atoi(optarg) ) {
334 if( version == LDAP_VERSION3 ) {
335 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
339 version = LDAP_VERSION2;
342 if( version == LDAP_VERSION2 ) {
343 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
347 version = LDAP_VERSION3;
350 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
353 return( EXIT_FAILURE );
356 #ifdef HAVE_CYRUS_SASL
357 if( version == LDAP_VERSION2 ) {
358 fprintf( stderr, "%s: -Q incompatible with version %d\n",
362 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
363 fprintf( stderr, "%s: incompatible previous "
364 "authentication choice\n",
368 authmethod = LDAP_AUTH_SASL;
369 version = LDAP_VERSION3;
370 sasl_flags = LDAP_SASL_QUIET;
373 fprintf( stderr, "%s: not compiled with SASL support\n",
375 return( EXIT_FAILURE );
377 case 'r': /* replace (obsolete) */
381 #ifdef HAVE_CYRUS_SASL
382 if( sasl_realm != NULL ) {
383 fprintf( stderr, "%s: -R previously specified\n", prog );
386 if( version == LDAP_VERSION2 ) {
387 fprintf( stderr, "%s: -R incompatible with version %d\n",
391 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
392 fprintf( stderr, "%s: incompatible previous "
393 "authentication choice\n",
397 authmethod = LDAP_AUTH_SASL;
398 version = LDAP_VERSION3;
399 sasl_realm = strdup( optarg );
401 fprintf( stderr, "%s: not compiled with SASL support\n",
403 return( EXIT_FAILURE );
407 #ifdef HAVE_CYRUS_SASL
408 if( sasl_authc_id != NULL ) {
409 fprintf( stderr, "%s: -U previously specified\n", prog );
412 if( version == LDAP_VERSION2 ) {
413 fprintf( stderr, "%s: -U incompatible with version %d\n",
417 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
418 fprintf( stderr, "%s: incompatible previous "
419 "authentication choice\n",
423 authmethod = LDAP_AUTH_SASL;
424 version = LDAP_VERSION3;
425 sasl_authc_id = strdup( optarg );
427 fprintf( stderr, "%s: not compiled with SASL support\n",
429 return( EXIT_FAILURE );
432 case 'v': /* verbose mode */
435 case 'w': /* password */
436 passwd.bv_val = strdup( optarg );
440 for( p = optarg; *p != '\0'; p++ ) {
444 passwd.bv_len = strlen( passwd.bv_val );
450 #ifdef HAVE_CYRUS_SASL
451 if( sasl_mech != NULL ) {
452 fprintf( stderr, "%s: -Y previously specified\n", prog );
455 if( version == LDAP_VERSION2 ) {
456 fprintf( stderr, "%s: -Y incompatible with version %d\n",
460 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
461 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
464 authmethod = LDAP_AUTH_SASL;
465 version = LDAP_VERSION3;
466 sasl_mech = strdup( optarg );
468 fprintf( stderr, "%s: not compiled with SASL support\n",
470 return( EXIT_FAILURE );
474 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
475 fprintf( stderr, "%s: incompatible with previous "
476 "authentication choice\n", prog );
479 authmethod = LDAP_AUTH_SIMPLE;
482 #ifdef HAVE_CYRUS_SASL
483 if( sasl_authz_id != NULL ) {
484 fprintf( stderr, "%s: -X previously specified\n", prog );
487 if( version == LDAP_VERSION2 ) {
488 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
492 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
493 fprintf( stderr, "%s: -X incompatible with "
494 "authentication choice\n", prog );
497 authmethod = LDAP_AUTH_SASL;
498 version = LDAP_VERSION3;
499 sasl_authz_id = strdup( optarg );
501 fprintf( stderr, "%s: not compiled with SASL support\n",
503 return( EXIT_FAILURE );
508 if( version == LDAP_VERSION2 ) {
509 fprintf( stderr, "%s: -Z incompatible with version %d\n",
513 version = LDAP_VERSION3;
516 fprintf( stderr, "%s: not compiled with TLS support\n",
518 return( EXIT_FAILURE );
522 fprintf( stderr, "%s: unrecognized option -%c\n",
529 version = LDAP_VERSION3;
531 if (authmethod == -1 && version > LDAP_VERSION2) {
532 #ifdef HAVE_CYRUS_SASL
533 authmethod = LDAP_AUTH_SASL;
535 authmethod = LDAP_AUTH_SIMPLE;
539 if ( argc != optind )
542 if ( infile != NULL ) {
543 if (( fp = fopen( infile, "r" )) == NULL ) {
545 return( EXIT_FAILURE );
552 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
553 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
555 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
556 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
562 (void) SIGNAL( SIGPIPE, SIG_IGN );
566 lutil_log_initialize( argc, argv );
570 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
572 fprintf( stderr, "ldap_init( %s, %d )\n",
573 ldaphost != NULL ? ldaphost : "<DEFAULT>",
577 ld = ldap_init( ldaphost, ldapport );
579 perror("ldapsearch: ldap_init");
585 fprintf( stderr, "ldap_initialize( %s )\n",
586 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
589 rc = ldap_initialize( &ld, ldapuri );
590 if( rc != LDAP_SUCCESS ) {
591 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
592 rc, ldap_err2string(rc) );
598 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
599 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
601 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
602 referrals ? "on" : "off" );
607 if (version == -1 ) {
608 version = LDAP_VERSION3;
611 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
612 != LDAP_OPT_SUCCESS )
614 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
619 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
620 ldap_perror( ld, "ldap_start_tls" );
622 return( EXIT_FAILURE );
627 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
628 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
631 if ( authmethod == LDAP_AUTH_SASL ) {
632 #ifdef HAVE_CYRUS_SASL
635 if( sasl_secprops != NULL ) {
636 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
637 (void *) sasl_secprops );
639 if( rc != LDAP_OPT_SUCCESS ) {
641 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
643 return( EXIT_FAILURE );
647 defaults = lutil_sasl_defaults( ld,
654 rc = ldap_sasl_interactive_bind_s( ld, binddn,
655 sasl_mech, NULL, NULL,
656 sasl_flags, lutil_sasl_interact, defaults );
658 if( rc != LDAP_SUCCESS ) {
659 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
660 return( EXIT_FAILURE );
663 fprintf( stderr, "%s: not compiled with SASL support\n",
665 return( EXIT_FAILURE );
669 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
671 ldap_perror( ld, "ldap_bind" );
672 return( EXIT_FAILURE );
683 LDAPControl *ctrls[2];
687 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
688 c.ldctl_value.bv_val = NULL;
689 c.ldctl_value.bv_len = 0;
690 c.ldctl_iscritical = manageDSAit > 1;
692 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
694 if( err != LDAP_OPT_SUCCESS ) {
695 fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
696 c.ldctl_iscritical ? "critical " : "" );
697 if( c.ldctl_iscritical ) {
698 exit( EXIT_FAILURE );
704 while (( rc == 0 || contoper ) &&
705 ( rbuf = read_one_record( fp )) != NULL ) {
710 rc = process_ldif_rec( start, count );
713 fprintf( stderr, "ldif_record() = %d\n", rc );
726 process_ldif_rec( char *rbuf, int count )
728 char *line, *dn, *type, *newrdn, *newsup, *p;
729 int rc, linenum, modop, replicaport;
730 int expect_modop, expect_sep, expect_ct, expect_newrdn, expect_newsup;
731 int expect_deleteoldrdn, deleteoldrdn;
732 int saw_replica, use_record, new_entry, delete_entry, got_all;
739 rc = got_all = saw_replica = delete_entry = modop = expect_modop = 0;
740 expect_deleteoldrdn = expect_newrdn = expect_newsup = 0;
741 expect_sep = expect_ct = 0;
747 dn = newrdn = newsup = NULL;
749 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
752 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
758 if ( ldif_parse_line( line, &type, &val.bv_val, &val.bv_len ) < 0 ) {
759 fprintf( stderr, "%s: invalid format (line %d) entry: \"%s\"\n",
760 prog, linenum, dn == NULL ? "" : dn );
761 rc = LDAP_PARAM_ERROR;
766 if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
768 if (( p = strchr( val.bv_val, ':' )) == NULL ) {
772 replicaport = atoi( p );
774 if ( ldaphost != NULL && strcasecmp( val.bv_val, ldaphost ) == 0 &&
775 replicaport == ldapport ) {
778 } else if ( count == 1 && linenum == 1 &&
779 strcasecmp( type, T_VERSION_STR ) == 0 )
781 if( val.bv_len == 0 || atoi(val.bv_val) != 1 ) {
782 fprintf( stderr, "%s: invalid version %s, line %d (ignored)\n",
783 prog, val.bv_val == NULL ? "(null)" : val.bv_val, linenum );
787 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
788 if (( dn = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
790 exit( EXIT_FAILURE );
794 goto end_line; /* skip all lines until we see "dn:" */
799 if ( !use_record && saw_replica ) {
800 printf( "%s: skipping change record for entry: %s\n"
801 "\t(LDAP host/port does not match replica: lines)\n",
805 ber_memfree( val.bv_val );
809 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
810 #ifdef LIBERAL_CHANGETYPE_MODOP
811 /* trim trailing spaces (and log warning ...) */
814 for ( icnt = val.bv_len; --icnt > 0; ) {
815 if ( !isspace( val.bv_val[icnt] ) ) {
820 if ( ++icnt != val.bv_len ) {
821 fprintf( stderr, "%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n",
822 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
823 val.bv_val[icnt] = '\0';
825 #endif /* LIBERAL_CHANGETYPE_MODOP */
827 if ( strcasecmp( val.bv_val, T_MODIFYCTSTR ) == 0 ) {
830 } else if ( strcasecmp( val.bv_val, T_ADDCTSTR ) == 0 ) {
832 } else if ( strcasecmp( val.bv_val, T_MODRDNCTSTR ) == 0
833 || strcasecmp( val.bv_val, T_MODDNCTSTR ) == 0
834 || strcasecmp( val.bv_val, T_RENAMECTSTR ) == 0)
837 } else if ( strcasecmp( val.bv_val, T_DELETECTSTR ) == 0 ) {
838 got_all = delete_entry = 1;
841 "%s: unknown %s \"%s\" (line %d of entry \"%s\")\n",
842 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
843 rc = LDAP_PARAM_ERROR;
846 } else if ( ldapadd ) { /* missing changetype => add */
848 modop = LDAP_MOD_ADD;
850 expect_modop = 1; /* missing changetype => modify */
854 if ( expect_modop ) {
855 #ifdef LIBERAL_CHANGETYPE_MODOP
856 /* trim trailing spaces (and log warning ...) */
859 for ( icnt = val.bv_len; --icnt > 0; ) {
860 if ( !isspace( val.bv_val[icnt] ) ) {
865 if ( ++icnt != val.bv_len ) {
866 fprintf( stderr, "%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n",
867 prog, type, val.bv_val, linenum, dn );
868 val.bv_val[icnt] = '\0';
870 #endif /* LIBERAL_CHANGETYPE_MODOP */
874 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
875 modop = LDAP_MOD_ADD;
877 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
878 modop = LDAP_MOD_REPLACE;
879 addmodifyop( &pmods, modop, val.bv_val, NULL );
881 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
882 modop = LDAP_MOD_DELETE;
883 addmodifyop( &pmods, modop, val.bv_val, NULL );
885 } else { /* no modify op: use default */
886 modop = ldapadd ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
890 if ( expect_newrdn ) {
891 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
892 if (( newrdn = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
894 exit( EXIT_FAILURE );
896 expect_deleteoldrdn = 1;
899 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
900 prog, T_NEWRDNSTR, type, linenum, dn );
901 rc = LDAP_PARAM_ERROR;
903 } else if ( expect_deleteoldrdn ) {
904 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
905 deleteoldrdn = ( *val.bv_val == '0' ) ? 0 : 1;
906 expect_deleteoldrdn = 0;
910 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
911 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
912 rc = LDAP_PARAM_ERROR;
914 } else if ( expect_newsup ) {
915 if ( strcasecmp( type, T_NEWSUPSTR ) == 0 ) {
916 if (( newsup = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
918 exit( EXIT_FAILURE );
922 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
923 prog, T_NEWSUPSTR, type, linenum, dn );
924 rc = LDAP_PARAM_ERROR;
926 } else if ( got_all ) {
928 "%s: extra lines at end (line %d of entry \"%s\")\n",
930 rc = LDAP_PARAM_ERROR;
932 addmodifyop( &pmods, modop, type, val.bv_val == NULL ? NULL : &val );
937 ber_memfree( val.bv_val );
944 if( version && linenum == 1 ) {
949 if ( delete_entry ) {
951 } else if ( newrdn != NULL ) {
952 rc = dorename( dn, newrdn, newsup, deleteoldrdn );
954 rc = domodify( dn, pmods, new_entry );
957 if ( rc == LDAP_SUCCESS ) {
965 if ( newrdn != NULL ) {
968 if ( pmods != NULL ) {
969 ldap_mods_free( pmods, 1 );
987 modop |= LDAP_MOD_BVALUES;
990 if ( pmods != NULL ) {
991 for ( ; pmods[ i ] != NULL; ++i ) {
992 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
993 pmods[ i ]->mod_op == modop )
1000 if ( pmods == NULL || pmods[ i ] == NULL ) {
1001 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
1002 sizeof( LDAPMod * ))) == NULL )
1004 perror( "realloc" );
1005 exit( EXIT_FAILURE );
1009 pmods[ i + 1 ] = NULL;
1011 pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod ));
1012 if ( pmods[ i ] == NULL ) {
1014 exit( EXIT_FAILURE );
1017 pmods[ i ]->mod_op = modop;
1018 pmods[ i ]->mod_type = ber_strdup( attr );
1019 if ( pmods[ i ]->mod_type == NULL ) {
1021 exit( EXIT_FAILURE );
1025 if ( val != NULL ) {
1027 if ( pmods[ i ]->mod_bvalues != NULL ) {
1028 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
1033 pmods[ i ]->mod_bvalues = (struct berval **) ber_memrealloc(
1034 pmods[ i ]->mod_bvalues, (j + 2) * sizeof( struct berval * ));
1035 if ( pmods[ i ]->mod_bvalues == NULL ) {
1036 perror( "ber_realloc" );
1037 exit( EXIT_FAILURE );
1040 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
1041 pmods[ i ]->mod_bvalues[ j ] = ber_bvdup( val );
1042 if ( pmods[ i ]->mod_bvalues[ j ] == NULL ) {
1043 perror( "ber_bvdup" );
1044 exit( EXIT_FAILURE );
1056 int i, j, k, notascii, op;
1059 if ( pmods == NULL ) {
1060 fprintf( stderr, "%s: no attributes to change or add (entry=\"%s\")\n",
1062 return( LDAP_PARAM_ERROR );
1065 for ( i = 0; pmods[ i ] != NULL; ++i ) {
1066 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1067 if( op == LDAP_MOD_ADD && ( pmods[i]->mod_bvalues == NULL )) {
1069 "%s: attribute \"%s\" has no values (entry=\"%s\")\n",
1070 prog, pmods[i]->mod_type, dn );
1071 return LDAP_PARAM_ERROR;
1076 for ( i = 0; pmods[ i ] != NULL; ++i ) {
1077 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1078 printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
1079 "replace" : op == LDAP_MOD_ADD ?
1080 "add" : "delete", pmods[ i ]->mod_type );
1081 if ( pmods[ i ]->mod_bvalues != NULL ) {
1082 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
1083 bvp = pmods[ i ]->mod_bvalues[ j ];
1085 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
1086 if ( !isascii( bvp->bv_val[ k ] )) {
1092 printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
1094 printf( "\t%s\n", bvp->bv_val );
1102 printf( "%sadding new entry \"%s\"\n", not ? "!" : "", dn );
1104 printf( "%smodifying entry \"%s\"\n", not ? "!" : "", dn );
1109 i = ldap_add_s( ld, dn, pmods );
1111 i = ldap_modify_s( ld, dn, pmods );
1113 if ( i != LDAP_SUCCESS ) {
1114 /* print error message about failed update including DN */
1115 fprintf( stderr, "%s: update failed: %s\n", prog, dn );
1116 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
1117 } else if ( verbose ) {
1118 printf( "modify complete\n" );
1136 printf( "%sdeleting entry \"%s\"\n", not ? "!" : "", dn );
1138 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
1139 fprintf( stderr, "%s: delete failed: %s\n", prog, dn );
1140 ldap_perror( ld, "ldap_delete" );
1141 } else if ( verbose ) {
1142 printf( "delete complete" );
1164 printf( "%smodifying rdn of entry \"%s\"\n", not ? "!" : "", dn );
1166 printf( "\tnew RDN: \"%s\" (%skeep existing values)\n",
1167 newrdn, deleteoldrdn ? "do not " : "" );
1170 if (( rc = ldap_rename2_s( ld, dn, newrdn, newsup, deleteoldrdn ))
1173 fprintf( stderr, "%s: rename failed: %s\n", prog, dn );
1174 ldap_perror( ld, "ldap_modrdn" );
1176 printf( "modrdn completed\n" );
1189 read_one_record( FILE *fp )
1191 char *buf, line[ LDAPMOD_MAXLINE ];
1197 while ( fgets( line, sizeof(line), fp ) != NULL ) {
1198 int len = strlen( line );
1200 if( len < 2 || ( len == 3 && *line == '\r' )) {
1208 if ( lcur + len + 1 > lmax ) {
1209 lmax = LDAPMOD_MAXLINE
1210 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
1212 if (( buf = (char *)realloc( buf, lmax )) == NULL ) {
1213 perror( "realloc" );
1214 exit( EXIT_FAILURE );
1218 strcpy( buf + lcur, line );