3 * Copyright 1998-2002 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
33 #include "lutil_ldap.h"
35 #include "ldap_defaults.h"
39 static char *binddn = NULL;
40 static struct berval passwd = { 0, NULL };
41 static char *ldapuri = NULL;
42 static char *ldaphost = NULL;
43 static int ldapport = 0;
44 #ifdef HAVE_CYRUS_SASL
45 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
46 static char *sasl_realm = NULL;
47 static char *sasl_authc_id = NULL;
48 static char *sasl_authz_id = NULL;
49 static char *sasl_mech = NULL;
50 static char *sasl_secprops = NULL;
52 static int use_tls = 0;
53 static int ldapadd, not, verbose, contoper, force;
54 static LDAP *ld = NULL;
56 #define LDAPMOD_MAXLINE 4096
58 /* strings found in replog/LDIF entries (mostly lifted from slurpd/slurp.h) */
59 #define T_VERSION_STR "version"
60 #define T_REPLICA_STR "replica"
62 #define T_CHANGETYPESTR "changetype"
63 #define T_ADDCTSTR "add"
64 #define T_MODIFYCTSTR "modify"
65 #define T_DELETECTSTR "delete"
66 #define T_MODRDNCTSTR "modrdn"
67 #define T_MODDNCTSTR "moddn"
68 #define T_RENAMECTSTR "rename"
69 #define T_MODOPADDSTR "add"
70 #define T_MODOPREPLACESTR "replace"
71 #define T_MODOPDELETESTR "delete"
72 #define T_MODSEPSTR "-"
73 #define T_NEWRDNSTR "newrdn"
74 #define T_DELETEOLDRDNSTR "deleteoldrdn"
75 #define T_NEWSUPSTR "newsuperior"
78 static void usage LDAP_P(( const char *prog )) LDAP_GCCATTR((noreturn));
79 static int process_ldif_rec LDAP_P(( char *rbuf, int count ));
80 static void addmodifyop LDAP_P((
81 LDAPMod ***pmodsp, int modop,
83 struct berval *value ));
84 static int domodify LDAP_P((
88 static int dodelete LDAP_P((
90 static int dorename LDAP_P((
95 static char *read_one_record LDAP_P(( FILE *fp ));
98 usage( const char *prog )
101 "Add or modify entries from an LDAP server\n\n"
102 "usage: %s [options]\n"
103 " The list of desired operations are read from stdin or from the file\n"
104 " specified by \"-f file\".\n"
105 "Add or modify options:\n"
106 " -a add values (default%s)\n"
107 " -c continuous operation mode (do not stop on errors)\n"
108 " -F force all changes records to be used\n"
109 " -S file write skipped modifications to `file'\n"
112 " -d level set LDAP debugging level to `level'\n"
113 " -D binddn bind DN\n"
114 " -e [!]<ctrl>[=<ctrlparam>] general controls (! indicates criticality)\n"
115 " [!]manageDSAit (alternate form, see -M)\n"
117 " -f file read operations from `file'\n"
118 " -h host LDAP server\n"
119 " -H URI LDAP Uniform Resource Indentifier(s)\n"
120 " -I use SASL Interactive mode\n"
121 " -k use Kerberos authentication\n"
122 " -K like -k, but do only step 1 of the Kerberos bind\n"
123 " -M enable Manage DSA IT control (-MM to make critical)\n"
124 " -n show what would be done but don't actually update\n"
125 " -O props SASL security properties\n"
126 " -p port port on LDAP server\n"
127 " -P version procotol version (default: 3)\n"
128 " -Q use SASL Quiet mode\n"
129 " -R realm SASL realm\n"
130 " -U authcid SASL authentication identity\n"
131 " -v run in verbose mode (diagnostics to standard output)\n"
132 " -w passwd bind passwd (for simple authentication)\n"
133 " -W prompt for bind passwd\n"
134 " -x Simple authentication\n"
135 " -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
136 " -y file Read passwd from file\n"
137 " -Y mech SASL mechanism\n"
138 " -Z Start TLS request (-ZZ to require successful response)\n"
139 , prog, (strcmp( prog, "ldapadd" ) ? " is to replace" : "") );
141 exit( EXIT_FAILURE );
146 main( int argc, char **argv )
148 char *infile, *rejfile, *rbuf, *start, *rejbuf = NULL;
150 char *matched_msg = NULL, *error_msg = NULL;
151 int rc, i, authmethod, version, want_bindpw, debug, manageDSAit, noop, referrals;
153 char *pw_file = NULL;
154 char *control, *cvalue;
157 prog = lutil_progname( "ldapmodify", argc, argv );
159 /* Print usage when no parameters */
160 if( argc < 2 ) usage( prog );
162 /* strncmp instead of strcmp since NT binaries carry .exe extension */
163 ldapadd = ( strncmp( prog, "ldapadd", sizeof("ldapadd")-1 ) == 0 );
167 not = verbose = want_bindpw = debug = manageDSAit = noop = referrals = 0;
171 while (( i = getopt( argc, argv, "acrf:E:F"
172 "Cd:D:e:h:H:IkKMnO:p:P:QR:S:U:vw:WxX:y:Y:Z" )) != EOF )
179 case 'c': /* continuous operation */
182 case 'E': /* modify controls */
183 if( version == LDAP_VERSION2 ) {
184 fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
189 /* should be extended to support comma separated list of
190 * [!]key[=value] parameters, e.g. -E !foo,bar=567
195 if( optarg[0] == '!' ) {
200 control = strdup( optarg );
201 if ( (cvalue = strchr( control, '=' )) != NULL ) {
204 fprintf( stderr, "Invalid modify control name: %s\n", control );
207 case 'f': /* read from file */
208 if( infile != NULL ) {
209 fprintf( stderr, "%s: -f previously specified\n", prog );
212 infile = strdup( optarg );
214 case 'F': /* force all changes records to be used */
223 debug |= atoi( optarg );
225 case 'D': /* bind DN */
226 if( binddn != NULL ) {
227 fprintf( stderr, "%s: -D previously specified\n", prog );
230 binddn = strdup( optarg );
232 case 'e': /* general controls */
233 if( version == LDAP_VERSION2 ) {
234 fprintf( stderr, "%s: -e incompatible with LDAPv%d\n",
239 /* should be extended to support comma separated list of
240 * [!]key[=value] parameters, e.g. -e !foo,bar=567
245 if( optarg[0] == '!' ) {
250 control = strdup( optarg );
251 if ( (cvalue = strchr( control, '=' )) != NULL ) {
255 if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
256 if( cvalue != NULL ) {
257 fprintf( stderr, "manageDSAit: no control value expected" );
262 manageDSAit = 1 + crit;
266 } else if ( strcasecmp( control, "noop" ) == 0 ) {
267 if( cvalue != NULL ) {
268 fprintf( stderr, "noop: no control value expected" );
278 fprintf( stderr, "Invalid general control name: %s\n", control );
282 case 'h': /* ldap host */
283 if( ldapuri != NULL ) {
284 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
287 if( ldaphost != NULL ) {
288 fprintf( stderr, "%s: -h previously specified\n", prog );
291 ldaphost = strdup( optarg );
293 case 'H': /* ldap URI */
294 if( ldaphost != NULL ) {
295 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
299 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
302 if( ldapuri != NULL ) {
303 fprintf( stderr, "%s: -H previously specified\n", prog );
306 ldapuri = strdup( optarg );
309 #ifdef HAVE_CYRUS_SASL
310 if( version == LDAP_VERSION2 ) {
311 fprintf( stderr, "%s: -I incompatible with version %d\n",
315 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
316 fprintf( stderr, "%s: incompatible previous "
317 "authentication choice\n",
321 authmethod = LDAP_AUTH_SASL;
322 version = LDAP_VERSION3;
323 sasl_flags = LDAP_SASL_INTERACTIVE;
326 fprintf( stderr, "%s: was not compiled with SASL support\n",
328 return( EXIT_FAILURE );
330 case 'k': /* kerberos bind */
331 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
332 if( version > LDAP_VERSION2 ) {
333 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
338 if( authmethod != -1 ) {
339 fprintf( stderr, "%s: -k incompatible with previous "
340 "authentication choice\n", prog );
344 authmethod = LDAP_AUTH_KRBV4;
346 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
350 case 'K': /* kerberos bind, part one only */
351 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
352 if( version > LDAP_VERSION2 ) {
353 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
357 if( authmethod != -1 ) {
358 fprintf( stderr, "%s: incompatible with previous "
359 "authentication choice\n", prog );
363 authmethod = LDAP_AUTH_KRBV41;
365 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
366 return( EXIT_FAILURE );
370 /* enable Manage DSA IT */
371 if( version == LDAP_VERSION2 ) {
372 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
377 version = LDAP_VERSION3;
379 case 'n': /* print deletes, don't actually do them */
383 #ifdef HAVE_CYRUS_SASL
384 if( sasl_secprops != NULL ) {
385 fprintf( stderr, "%s: -O previously specified\n", prog );
388 if( version == LDAP_VERSION2 ) {
389 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
393 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
394 fprintf( stderr, "%s: incompatible previous "
395 "authentication choice\n", prog );
398 authmethod = LDAP_AUTH_SASL;
399 version = LDAP_VERSION3;
400 sasl_secprops = strdup( optarg );
402 fprintf( stderr, "%s: not compiled with SASL support\n",
404 return( EXIT_FAILURE );
409 fprintf( stderr, "%s: -p previously specified\n", prog );
412 ldapport = atoi( optarg );
415 switch( atoi(optarg) ) {
417 if( version == LDAP_VERSION3 ) {
418 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
422 version = LDAP_VERSION2;
425 if( version == LDAP_VERSION2 ) {
426 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
430 version = LDAP_VERSION3;
433 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
436 return( EXIT_FAILURE );
439 #ifdef HAVE_CYRUS_SASL
440 if( version == LDAP_VERSION2 ) {
441 fprintf( stderr, "%s: -Q incompatible with version %d\n",
445 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
446 fprintf( stderr, "%s: incompatible previous "
447 "authentication choice\n",
451 authmethod = LDAP_AUTH_SASL;
452 version = LDAP_VERSION3;
453 sasl_flags = LDAP_SASL_QUIET;
456 fprintf( stderr, "%s: not compiled with SASL support\n",
458 return( EXIT_FAILURE );
460 case 'r': /* replace (obsolete) */
464 #ifdef HAVE_CYRUS_SASL
465 if( sasl_realm != NULL ) {
466 fprintf( stderr, "%s: -R previously specified\n", prog );
469 if( version == LDAP_VERSION2 ) {
470 fprintf( stderr, "%s: -R incompatible with version %d\n",
474 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
475 fprintf( stderr, "%s: incompatible previous "
476 "authentication choice\n",
480 authmethod = LDAP_AUTH_SASL;
481 version = LDAP_VERSION3;
482 sasl_realm = strdup( optarg );
484 fprintf( stderr, "%s: not compiled with SASL support\n",
486 return( EXIT_FAILURE );
489 case 'S': /* skipped modifications to file */
490 if( rejfile != NULL ) {
491 fprintf( stderr, "%s: -S previously specified\n", prog );
494 rejfile = strdup( optarg );
497 #ifdef HAVE_CYRUS_SASL
498 if( sasl_authc_id != NULL ) {
499 fprintf( stderr, "%s: -U previously specified\n", prog );
502 if( version == LDAP_VERSION2 ) {
503 fprintf( stderr, "%s: -U incompatible with version %d\n",
507 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
508 fprintf( stderr, "%s: incompatible previous "
509 "authentication choice\n",
513 authmethod = LDAP_AUTH_SASL;
514 version = LDAP_VERSION3;
515 sasl_authc_id = strdup( optarg );
517 fprintf( stderr, "%s: not compiled with SASL support\n",
519 return( EXIT_FAILURE );
522 case 'v': /* verbose mode */
525 case 'w': /* password */
526 passwd.bv_val = strdup( optarg );
530 for( p = optarg; *p != '\0'; p++ ) {
534 passwd.bv_len = strlen( passwd.bv_val );
543 #ifdef HAVE_CYRUS_SASL
544 if( sasl_mech != NULL ) {
545 fprintf( stderr, "%s: -Y previously specified\n", prog );
548 if( version == LDAP_VERSION2 ) {
549 fprintf( stderr, "%s: -Y incompatible with version %d\n",
553 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
554 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
557 authmethod = LDAP_AUTH_SASL;
558 version = LDAP_VERSION3;
559 sasl_mech = strdup( optarg );
561 fprintf( stderr, "%s: not compiled with SASL support\n",
563 return( EXIT_FAILURE );
567 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
568 fprintf( stderr, "%s: incompatible with previous "
569 "authentication choice\n", prog );
572 authmethod = LDAP_AUTH_SIMPLE;
575 #ifdef HAVE_CYRUS_SASL
576 if( sasl_authz_id != NULL ) {
577 fprintf( stderr, "%s: -X previously specified\n", prog );
580 if( version == LDAP_VERSION2 ) {
581 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
585 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
586 fprintf( stderr, "%s: -X incompatible with "
587 "authentication choice\n", prog );
590 authmethod = LDAP_AUTH_SASL;
591 version = LDAP_VERSION3;
592 sasl_authz_id = strdup( optarg );
594 fprintf( stderr, "%s: not compiled with SASL support\n",
596 return( EXIT_FAILURE );
601 if( version == LDAP_VERSION2 ) {
602 fprintf( stderr, "%s: -Z incompatible with version %d\n",
606 version = LDAP_VERSION3;
609 fprintf( stderr, "%s: not compiled with TLS support\n",
611 return( EXIT_FAILURE );
615 fprintf( stderr, "%s: unrecognized option -%c\n",
622 version = LDAP_VERSION3;
624 if (authmethod == -1 && version > LDAP_VERSION2) {
625 #ifdef HAVE_CYRUS_SASL
626 authmethod = LDAP_AUTH_SASL;
628 authmethod = LDAP_AUTH_SIMPLE;
632 if ( argc != optind )
635 if ( rejfile != NULL ) {
636 if (( rejfp = fopen( rejfile, "w" )) == NULL ) {
638 return( EXIT_FAILURE );
644 if ( infile != NULL ) {
645 if (( fp = fopen( infile, "r" )) == NULL ) {
647 return( EXIT_FAILURE );
654 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
655 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
657 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
658 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
664 (void) SIGNAL( SIGPIPE, SIG_IGN );
668 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
670 fprintf( stderr, "ldap_init( %s, %d )\n",
671 ldaphost != NULL ? ldaphost : "<DEFAULT>",
675 ld = ldap_init( ldaphost, ldapport );
677 perror("ldapmodify: ldap_init");
683 fprintf( stderr, "ldap_initialize( %s )\n",
684 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
687 rc = ldap_initialize( &ld, ldapuri );
688 if( rc != LDAP_SUCCESS ) {
689 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
690 rc, ldap_err2string(rc) );
696 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
697 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
699 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
700 referrals ? "on" : "off" );
705 if (version == -1 ) {
706 version = LDAP_VERSION3;
709 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
710 != LDAP_OPT_SUCCESS )
712 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
717 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
718 ldap_perror( ld, "ldap_start_tls" );
720 return( EXIT_FAILURE );
724 if ( pw_file || want_bindpw ) {
726 rc = lutil_get_filed_password( pw_file, &passwd );
727 if( rc ) return EXIT_FAILURE;
729 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
730 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
734 if ( authmethod == LDAP_AUTH_SASL ) {
735 #ifdef HAVE_CYRUS_SASL
738 if( sasl_secprops != NULL ) {
739 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
740 (void *) sasl_secprops );
742 if( rc != LDAP_OPT_SUCCESS ) {
744 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
746 return( EXIT_FAILURE );
750 defaults = lutil_sasl_defaults( ld,
757 rc = ldap_sasl_interactive_bind_s( ld, binddn,
758 sasl_mech, NULL, NULL,
759 sasl_flags, lutil_sasl_interact, defaults );
761 if( rc != LDAP_SUCCESS ) {
762 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
763 return( EXIT_FAILURE );
766 fprintf( stderr, "%s: not compiled with SASL support\n",
768 return( EXIT_FAILURE );
772 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
774 ldap_perror( ld, "ldap_bind" );
775 return( EXIT_FAILURE );
784 if ( manageDSAit || noop ) {
787 LDAPControl *ctrls[3];
792 c1.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
793 c1.ldctl_value.bv_val = NULL;
794 c1.ldctl_value.bv_len = 0;
795 c1.ldctl_iscritical = manageDSAit > 1;
802 c2.ldctl_oid = LDAP_CONTROL_NOOP;
803 c2.ldctl_value.bv_val = NULL;
804 c2.ldctl_value.bv_len = 0;
805 c2.ldctl_iscritical = noop > 1;
808 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
810 if( err != LDAP_OPT_SUCCESS ) {
811 fprintf( stderr, "Could not set %scontrols\n",
812 (c1.ldctl_iscritical || c2.ldctl_iscritical)
813 ? "critical " : "" );
814 if ( c1.ldctl_iscritical && c2.ldctl_iscritical ) {
821 while (( rc == 0 || contoper ) &&
822 ( rbuf = read_one_record( fp )) != NULL ) {
828 len = strlen( rbuf );
829 if (( rejbuf = (char *)malloc( len+1 )) == NULL ) {
831 exit( EXIT_FAILURE );
833 memcpy( rejbuf, rbuf, len+1 );
836 rc = process_ldif_rec( start, count );
839 fprintf(rejfp, "# Error: %s (%d)", ldap_err2string(rc), rc);
841 ldap_get_option(ld, LDAP_OPT_MATCHED_DN, &matched_msg);
842 if ( matched_msg != NULL && *matched_msg != '\0' ) {
843 fprintf( rejfp, ", matched DN: %s", matched_msg );
846 ldap_get_option(ld, LDAP_OPT_ERROR_STRING, &error_msg);
847 if ( error_msg != NULL && *error_msg != '\0' ) {
848 fprintf( rejfp, ", additional info: %s", error_msg );
850 fprintf( rejfp, "\n%s\n", rejbuf );
861 if ( rejfp != NULL ) {
870 process_ldif_rec( char *rbuf, int count )
872 char *line, *dn, *type, *newrdn, *newsup, *p;
873 int rc, linenum, modop, replicaport;
874 int expect_modop, expect_sep, expect_ct, expect_newrdn, expect_newsup;
875 int expect_deleteoldrdn, deleteoldrdn;
876 int saw_replica, use_record, new_entry, delete_entry, got_all;
883 rc = got_all = saw_replica = delete_entry = modop = expect_modop = 0;
884 expect_deleteoldrdn = expect_newrdn = expect_newsup = 0;
885 expect_sep = expect_ct = 0;
891 dn = newrdn = newsup = NULL;
893 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
896 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
902 if ( ldif_parse_line( line, &type, &val.bv_val, &val.bv_len ) < 0 ) {
903 fprintf( stderr, "%s: invalid format (line %d) entry: \"%s\"\n",
904 prog, linenum, dn == NULL ? "" : dn );
905 rc = LDAP_PARAM_ERROR;
910 if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
912 if (( p = strchr( val.bv_val, ':' )) == NULL ) {
916 replicaport = atoi( p );
918 if ( ldaphost != NULL && strcasecmp( val.bv_val, ldaphost ) == 0 &&
919 replicaport == ldapport ) {
922 } else if ( count == 1 && linenum == 1 &&
923 strcasecmp( type, T_VERSION_STR ) == 0 )
925 if( val.bv_len == 0 || atoi(val.bv_val) != 1 ) {
926 fprintf( stderr, "%s: invalid version %s, line %d (ignored)\n",
927 prog, val.bv_val == NULL ? "(null)" : val.bv_val, linenum );
931 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
932 if (( dn = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
934 exit( EXIT_FAILURE );
938 goto end_line; /* skip all lines until we see "dn:" */
943 if ( !use_record && saw_replica ) {
944 printf( "%s: skipping change record for entry: %s\n"
945 "\t(LDAP host/port does not match replica: lines)\n",
949 ber_memfree( val.bv_val );
953 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
954 #ifdef LIBERAL_CHANGETYPE_MODOP
955 /* trim trailing spaces (and log warning ...) */
958 for ( icnt = val.bv_len; --icnt > 0; ) {
959 if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
964 if ( ++icnt != val.bv_len ) {
965 fprintf( stderr, "%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n",
966 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
967 val.bv_val[icnt] = '\0';
969 #endif /* LIBERAL_CHANGETYPE_MODOP */
971 if ( strcasecmp( val.bv_val, T_MODIFYCTSTR ) == 0 ) {
974 } else if ( strcasecmp( val.bv_val, T_ADDCTSTR ) == 0 ) {
976 } else if ( strcasecmp( val.bv_val, T_MODRDNCTSTR ) == 0
977 || strcasecmp( val.bv_val, T_MODDNCTSTR ) == 0
978 || strcasecmp( val.bv_val, T_RENAMECTSTR ) == 0)
981 } else if ( strcasecmp( val.bv_val, T_DELETECTSTR ) == 0 ) {
982 got_all = delete_entry = 1;
985 "%s: unknown %s \"%s\" (line %d of entry \"%s\")\n",
986 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
987 rc = LDAP_PARAM_ERROR;
990 } else if ( ldapadd ) { /* missing changetype => add */
992 modop = LDAP_MOD_ADD;
994 expect_modop = 1; /* missing changetype => modify */
998 if ( expect_modop ) {
999 #ifdef LIBERAL_CHANGETYPE_MODOP
1000 /* trim trailing spaces (and log warning ...) */
1003 for ( icnt = val.bv_len; --icnt > 0; ) {
1004 if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
1009 if ( ++icnt != val.bv_len ) {
1010 fprintf( stderr, "%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n",
1011 prog, type, val.bv_val, linenum, dn );
1012 val.bv_val[icnt] = '\0';
1014 #endif /* LIBERAL_CHANGETYPE_MODOP */
1018 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
1019 modop = LDAP_MOD_ADD;
1021 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
1022 modop = LDAP_MOD_REPLACE;
1023 addmodifyop( &pmods, modop, val.bv_val, NULL );
1025 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
1026 modop = LDAP_MOD_DELETE;
1027 addmodifyop( &pmods, modop, val.bv_val, NULL );
1029 } else { /* no modify op: use default */
1030 modop = ldapadd ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
1034 if ( expect_newrdn ) {
1035 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
1036 if (( newrdn = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
1038 exit( EXIT_FAILURE );
1040 expect_deleteoldrdn = 1;
1043 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
1044 prog, T_NEWRDNSTR, type, linenum, dn );
1045 rc = LDAP_PARAM_ERROR;
1047 } else if ( expect_deleteoldrdn ) {
1048 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
1049 deleteoldrdn = ( *val.bv_val == '0' ) ? 0 : 1;
1050 expect_deleteoldrdn = 0;
1054 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
1055 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
1056 rc = LDAP_PARAM_ERROR;
1058 } else if ( expect_newsup ) {
1059 if ( strcasecmp( type, T_NEWSUPSTR ) == 0 ) {
1060 if (( newsup = strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
1062 exit( EXIT_FAILURE );
1066 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
1067 prog, T_NEWSUPSTR, type, linenum, dn );
1068 rc = LDAP_PARAM_ERROR;
1070 } else if ( got_all ) {
1072 "%s: extra lines at end (line %d of entry \"%s\")\n",
1073 prog, linenum, dn );
1074 rc = LDAP_PARAM_ERROR;
1076 addmodifyop( &pmods, modop, type, val.bv_val == NULL ? NULL : &val );
1080 ber_memfree( type );
1081 ber_memfree( val.bv_val );
1084 if( linenum == 0 ) {
1088 if( version && linenum == 1 ) {
1093 if ( delete_entry ) {
1094 rc = dodelete( dn );
1095 } else if ( newrdn != NULL ) {
1096 rc = dorename( dn, newrdn, newsup, deleteoldrdn );
1098 rc = domodify( dn, pmods, new_entry );
1101 if ( rc == LDAP_SUCCESS ) {
1109 if ( newrdn != NULL ) {
1112 if ( pmods != NULL ) {
1113 ldap_mods_free( pmods, 1 );
1125 struct berval *val )
1131 modop |= LDAP_MOD_BVALUES;
1134 if ( pmods != NULL ) {
1135 for ( ; pmods[ i ] != NULL; ++i ) {
1136 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
1137 pmods[ i ]->mod_op == modop )
1144 if ( pmods == NULL || pmods[ i ] == NULL ) {
1145 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
1146 sizeof( LDAPMod * ))) == NULL )
1148 perror( "realloc" );
1149 exit( EXIT_FAILURE );
1153 pmods[ i + 1 ] = NULL;
1155 pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod ));
1156 if ( pmods[ i ] == NULL ) {
1158 exit( EXIT_FAILURE );
1161 pmods[ i ]->mod_op = modop;
1162 pmods[ i ]->mod_type = ber_strdup( attr );
1163 if ( pmods[ i ]->mod_type == NULL ) {
1165 exit( EXIT_FAILURE );
1169 if ( val != NULL ) {
1171 if ( pmods[ i ]->mod_bvalues != NULL ) {
1172 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
1177 pmods[ i ]->mod_bvalues = (struct berval **) ber_memrealloc(
1178 pmods[ i ]->mod_bvalues, (j + 2) * sizeof( struct berval * ));
1179 if ( pmods[ i ]->mod_bvalues == NULL ) {
1180 perror( "ber_realloc" );
1181 exit( EXIT_FAILURE );
1184 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
1185 pmods[ i ]->mod_bvalues[ j ] = ber_bvdup( val );
1186 if ( pmods[ i ]->mod_bvalues[ j ] == NULL ) {
1187 perror( "ber_bvdup" );
1188 exit( EXIT_FAILURE );
1200 int i, j, k, notascii, op;
1203 if ( pmods == NULL ) {
1204 fprintf( stderr, "%s: no attributes to change or add (entry=\"%s\")\n",
1206 return( LDAP_PARAM_ERROR );
1209 for ( i = 0; pmods[ i ] != NULL; ++i ) {
1210 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1211 if( op == LDAP_MOD_ADD && ( pmods[i]->mod_bvalues == NULL )) {
1213 "%s: attribute \"%s\" has no values (entry=\"%s\")\n",
1214 prog, pmods[i]->mod_type, dn );
1215 return LDAP_PARAM_ERROR;
1220 for ( i = 0; pmods[ i ] != NULL; ++i ) {
1221 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1222 printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
1223 "replace" : op == LDAP_MOD_ADD ?
1224 "add" : "delete", pmods[ i ]->mod_type );
1225 if ( pmods[ i ]->mod_bvalues != NULL ) {
1226 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
1227 bvp = pmods[ i ]->mod_bvalues[ j ];
1229 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
1230 if ( !isascii( bvp->bv_val[ k ] )) {
1236 printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
1238 printf( "\t%s\n", bvp->bv_val );
1246 printf( "%sadding new entry \"%s\"\n", not ? "!" : "", dn );
1248 printf( "%smodifying entry \"%s\"\n", not ? "!" : "", dn );
1253 i = ldap_add_s( ld, dn, pmods );
1255 i = ldap_modify_s( ld, dn, pmods );
1257 if ( i != LDAP_SUCCESS ) {
1258 /* print error message about failed update including DN */
1259 fprintf( stderr, "%s: update failed: %s\n", prog, dn );
1260 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
1261 } else if ( verbose ) {
1262 printf( "modify complete\n" );
1280 printf( "%sdeleting entry \"%s\"\n", not ? "!" : "", dn );
1282 if (( rc = ldap_delete_s( ld, dn )) != LDAP_SUCCESS ) {
1283 fprintf( stderr, "%s: delete failed: %s\n", prog, dn );
1284 ldap_perror( ld, "ldap_delete" );
1285 } else if ( verbose ) {
1286 printf( "delete complete" );
1308 printf( "%smodifying rdn of entry \"%s\"\n", not ? "!" : "", dn );
1310 printf( "\tnew RDN: \"%s\" (%skeep existing values)\n",
1311 newrdn, deleteoldrdn ? "do not " : "" );
1314 if (( rc = ldap_rename2_s( ld, dn, newrdn, newsup, deleteoldrdn ))
1317 fprintf( stderr, "%s: rename failed: %s\n", prog, dn );
1318 ldap_perror( ld, "ldap_modrdn" );
1320 printf( "modrdn completed\n" );
1333 read_one_record( FILE *fp )
1335 char *buf, line[ LDAPMOD_MAXLINE ];
1341 while ( fgets( line, sizeof(line), fp ) != NULL ) {
1342 int len = strlen( line );
1344 if( len < 2 || ( len == 2 && *line == '\r' )) {
1352 if ( lcur + len + 1 > lmax ) {
1353 lmax = LDAPMOD_MAXLINE
1354 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
1356 if (( buf = (char *)realloc( buf, lmax )) == NULL ) {
1357 perror( "realloc" );
1358 exit( EXIT_FAILURE );
1362 strcpy( buf + lcur, line );