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"
40 static char *binddn = NULL;
41 static struct berval passwd = { 0, NULL };
42 static char *ldapuri = NULL;
43 static char *ldaphost = NULL;
44 static int ldapport = 0;
45 #ifdef HAVE_CYRUS_SASL
46 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
47 static char *sasl_realm = NULL;
48 static char *sasl_authc_id = NULL;
49 static char *sasl_authz_id = NULL;
50 static char *sasl_mech = NULL;
51 static char *sasl_secprops = NULL;
53 static int use_tls = 0;
54 static int ldapadd, not, verbose, contoper, force;
55 static LDAP *ld = NULL;
57 #define LDAPMOD_MAXLINE 4096
59 /* strings found in replog/LDIF entries (mostly lifted from slurpd/slurp.h) */
60 #define T_VERSION_STR "version"
61 #define T_REPLICA_STR "replica"
63 #define T_CONTROL_STR "control"
64 #define T_CHANGETYPESTR "changetype"
65 #define T_ADDCTSTR "add"
66 #define T_MODIFYCTSTR "modify"
67 #define T_DELETECTSTR "delete"
68 #define T_MODRDNCTSTR "modrdn"
69 #define T_MODDNCTSTR "moddn"
70 #define T_RENAMECTSTR "rename"
71 #define T_MODOPADDSTR "add"
72 #define T_MODOPREPLACESTR "replace"
73 #define T_MODOPDELETESTR "delete"
74 #define T_MODSEPSTR "-"
75 #define T_NEWRDNSTR "newrdn"
76 #define T_DELETEOLDRDNSTR "deleteoldrdn"
77 #define T_NEWSUPSTR "newsuperior"
80 static void usage LDAP_P(( const char *prog )) LDAP_GCCATTR((noreturn));
81 static int process_ldif_rec LDAP_P(( char *rbuf, int count ));
82 static int parse_ldif_control LDAP_P(( char *line, LDAPControl ***pctrls ));
83 static void addmodifyop LDAP_P((
84 LDAPMod ***pmodsp, int modop,
86 struct berval *value ));
87 static int domodify LDAP_P((
92 static int dodelete LDAP_P((
94 LDAPControl **pctrls ));
95 static int dorename LDAP_P((
100 LDAPControl **pctrls ));
101 static char *read_one_record LDAP_P(( FILE *fp ));
104 usage( const char *prog )
107 "Add or modify entries from an LDAP server\n\n"
108 "usage: %s [options]\n"
109 " The list of desired operations are read from stdin or from the file\n"
110 " specified by \"-f file\".\n"
111 "Add or modify options:\n"
112 " -a add values (default%s)\n"
113 " -c continuous operation mode (do not stop on errors)\n"
114 " -F force all changes records to be used\n"
115 " -S file write skipped modifications to `file'\n"
118 " -d level set LDAP debugging level to `level'\n"
119 " -D binddn bind DN\n"
120 " -e [!]<ctrl>[=<ctrlparam>] general controls (! indicates criticality)\n"
121 " [!]authzid=<authzid> (\"dn:<dn>\" or \"u:<user>\")\n"
122 " [!]manageDSAit (alternate form, see -M)\n"
124 " -f file read operations from `file'\n"
125 " -h host LDAP server\n"
126 " -H URI LDAP Uniform Resource Indentifier(s)\n"
127 " -I use SASL Interactive mode\n"
128 " -k use Kerberos authentication\n"
129 " -K like -k, but do only step 1 of the Kerberos bind\n"
130 " -M enable Manage DSA IT control (-MM to make critical)\n"
131 " -n show what would be done but don't actually update\n"
132 " -O props SASL security properties\n"
133 " -p port port on LDAP server\n"
134 " -P version procotol version (default: 3)\n"
135 " -Q use SASL Quiet mode\n"
136 " -R realm SASL realm\n"
137 " -U authcid SASL authentication identity\n"
138 " -v run in verbose mode (diagnostics to standard output)\n"
139 " -w passwd bind passwd (for simple authentication)\n"
140 " -W prompt for bind passwd\n"
141 " -x Simple authentication\n"
142 " -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
143 " -y file Read passwd from file\n"
144 " -Y mech SASL mechanism\n"
145 " -Z Start TLS request (-ZZ to require successful response)\n"
146 , prog, (strcmp( prog, "ldapadd" ) ? " is to replace" : "") );
148 exit( EXIT_FAILURE );
153 main( int argc, char **argv )
155 char *infile, *rejfile, *rbuf, *start, *rejbuf = NULL;
157 char *matched_msg = NULL, *error_msg = NULL;
158 int rc, retval, i, authmethod, version, want_bindpw;
159 int debug, manageDSAit, noop, referrals;
161 char *pw_file = NULL;
162 char *control, *cvalue;
163 char *authzid = NULL;
166 prog = lutil_progname( "ldapmodify", argc, argv );
168 /* Print usage when no parameters */
169 if( argc < 2 ) usage( prog );
171 /* strncmp instead of strcmp since NT binaries carry .exe extension */
172 ldapadd = ( strncmp( prog, "ldapadd", sizeof("ldapadd")-1 ) == 0 );
176 not = verbose = want_bindpw = debug = manageDSAit = noop = referrals = 0;
180 while (( i = getopt( argc, argv, "acrf:E:F"
181 "Cd:D:e:h:H:IkKMnO:p:P:QR:S:U:vw:WxX:y:Y:Z" )) != EOF )
188 case 'c': /* continuous operation */
191 case 'E': /* modify controls */
192 if( version == LDAP_VERSION2 ) {
193 fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
198 /* should be extended to support comma separated list of
199 * [!]key[=value] parameters, e.g. -E !foo,bar=567
204 if( optarg[0] == '!' ) {
209 control = ber_strdup( optarg );
210 if ( (cvalue = strchr( control, '=' )) != NULL ) {
213 fprintf( stderr, "Invalid modify control name: %s\n", control );
216 case 'f': /* read from file */
217 if( infile != NULL ) {
218 fprintf( stderr, "%s: -f previously specified\n", prog );
221 infile = ber_strdup( optarg );
223 case 'F': /* force all changes records to be used */
232 debug |= atoi( optarg );
234 case 'D': /* bind DN */
235 if( binddn != NULL ) {
236 fprintf( stderr, "%s: -D previously specified\n", prog );
239 binddn = ber_strdup( optarg );
241 case 'e': /* general controls */
242 if( version == LDAP_VERSION2 ) {
243 fprintf( stderr, "%s: -e incompatible with LDAPv%d\n",
248 /* should be extended to support comma separated list of
249 * [!]key[=value] parameters, e.g. -e !foo,bar=567
254 if( optarg[0] == '!' ) {
259 control = ber_strdup( optarg );
260 if ( (cvalue = strchr( control, '=' )) != NULL ) {
264 if ( strcasecmp( control, "authzid" ) == 0 ) {
265 if( authzid != NULL ) {
266 fprintf( stderr, "authzid control previously specified\n");
269 if( cvalue == NULL ) {
270 fprintf( stderr, "authzid: control value expected\n" );
275 fprintf( stderr, "authzid: must be marked critical\n" );
280 assert( authzid == NULL );
283 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
285 fprintf( stderr, "manageDSAit control previously specified\n");
288 if( cvalue != NULL ) {
289 fprintf( stderr, "manageDSAit: no control value expected\n" );
294 manageDSAit = 1 + crit;
296 } else if ( strcasecmp( control, "noop" ) == 0 ) {
298 fprintf( stderr, "noop control previously specified\n");
301 if( cvalue != NULL ) {
302 fprintf( stderr, "noop: no control value expected\n" );
310 fprintf( stderr, "Invalid general control name: %s\n", control );
315 case 'h': /* ldap host */
316 if( ldapuri != NULL ) {
317 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
320 if( ldaphost != NULL ) {
321 fprintf( stderr, "%s: -h previously specified\n", prog );
324 ldaphost = ber_strdup( optarg );
326 case 'H': /* ldap URI */
327 if( ldaphost != NULL ) {
328 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
332 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
335 if( ldapuri != NULL ) {
336 fprintf( stderr, "%s: -H previously specified\n", prog );
339 ldapuri = ber_strdup( optarg );
342 #ifdef HAVE_CYRUS_SASL
343 if( version == LDAP_VERSION2 ) {
344 fprintf( stderr, "%s: -I incompatible with version %d\n",
348 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
349 fprintf( stderr, "%s: incompatible previous "
350 "authentication choice\n",
354 authmethod = LDAP_AUTH_SASL;
355 version = LDAP_VERSION3;
356 sasl_flags = LDAP_SASL_INTERACTIVE;
359 fprintf( stderr, "%s: was not compiled with SASL support\n",
361 return( EXIT_FAILURE );
363 case 'k': /* kerberos bind */
364 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
365 if( version > LDAP_VERSION2 ) {
366 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
371 if( authmethod != -1 ) {
372 fprintf( stderr, "%s: -k incompatible with previous "
373 "authentication choice\n", prog );
377 authmethod = LDAP_AUTH_KRBV4;
379 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
383 case 'K': /* kerberos bind, part one only */
384 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
385 if( version > LDAP_VERSION2 ) {
386 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
390 if( authmethod != -1 ) {
391 fprintf( stderr, "%s: incompatible with previous "
392 "authentication choice\n", prog );
396 authmethod = LDAP_AUTH_KRBV41;
398 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
399 return( EXIT_FAILURE );
403 /* enable Manage DSA IT */
404 if( version == LDAP_VERSION2 ) {
405 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
410 version = LDAP_VERSION3;
412 case 'n': /* print deletes, don't actually do them */
416 #ifdef HAVE_CYRUS_SASL
417 if( sasl_secprops != NULL ) {
418 fprintf( stderr, "%s: -O previously specified\n", prog );
421 if( version == LDAP_VERSION2 ) {
422 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
426 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
427 fprintf( stderr, "%s: incompatible previous "
428 "authentication choice\n", prog );
431 authmethod = LDAP_AUTH_SASL;
432 version = LDAP_VERSION3;
433 sasl_secprops = ber_strdup( optarg );
435 fprintf( stderr, "%s: not compiled with SASL support\n",
437 return( EXIT_FAILURE );
442 fprintf( stderr, "%s: -p previously specified\n", prog );
445 ldapport = atoi( optarg );
448 switch( atoi(optarg) ) {
450 if( version == LDAP_VERSION3 ) {
451 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
455 version = LDAP_VERSION2;
458 if( version == LDAP_VERSION2 ) {
459 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
463 version = LDAP_VERSION3;
466 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
469 return( EXIT_FAILURE );
473 #ifdef HAVE_CYRUS_SASL
474 if( version == LDAP_VERSION2 ) {
475 fprintf( stderr, "%s: -Q incompatible with version %d\n",
479 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
480 fprintf( stderr, "%s: incompatible previous "
481 "authentication choice\n",
485 authmethod = LDAP_AUTH_SASL;
486 version = LDAP_VERSION3;
487 sasl_flags = LDAP_SASL_QUIET;
490 fprintf( stderr, "%s: not compiled with SASL support\n",
492 return( EXIT_FAILURE );
494 case 'r': /* replace (obsolete) */
498 #ifdef HAVE_CYRUS_SASL
499 if( sasl_realm != NULL ) {
500 fprintf( stderr, "%s: -R previously specified\n", prog );
503 if( version == LDAP_VERSION2 ) {
504 fprintf( stderr, "%s: -R incompatible with version %d\n",
508 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
509 fprintf( stderr, "%s: incompatible previous "
510 "authentication choice\n",
514 authmethod = LDAP_AUTH_SASL;
515 version = LDAP_VERSION3;
516 sasl_realm = ber_strdup( optarg );
518 fprintf( stderr, "%s: not compiled with SASL support\n",
520 return( EXIT_FAILURE );
523 case 'S': /* skipped modifications to file */
524 if( rejfile != NULL ) {
525 fprintf( stderr, "%s: -S previously specified\n", prog );
528 rejfile = ber_strdup( optarg );
531 #ifdef HAVE_CYRUS_SASL
532 if( sasl_authc_id != NULL ) {
533 fprintf( stderr, "%s: -U previously specified\n", prog );
536 if( version == LDAP_VERSION2 ) {
537 fprintf( stderr, "%s: -U incompatible with version %d\n",
541 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
542 fprintf( stderr, "%s: incompatible previous "
543 "authentication choice\n",
547 authmethod = LDAP_AUTH_SASL;
548 version = LDAP_VERSION3;
549 sasl_authc_id = ber_strdup( optarg );
551 fprintf( stderr, "%s: not compiled with SASL support\n",
553 return( EXIT_FAILURE );
556 case 'v': /* verbose mode */
559 case 'w': /* password */
560 passwd.bv_val = ber_strdup( optarg );
564 for( p = optarg; *p != '\0'; p++ ) {
568 passwd.bv_len = strlen( passwd.bv_val );
577 #ifdef HAVE_CYRUS_SASL
578 if( sasl_mech != NULL ) {
579 fprintf( stderr, "%s: -Y previously specified\n", prog );
582 if( version == LDAP_VERSION2 ) {
583 fprintf( stderr, "%s: -Y incompatible with version %d\n",
587 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
588 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
591 authmethod = LDAP_AUTH_SASL;
592 version = LDAP_VERSION3;
593 sasl_mech = ber_strdup( optarg );
595 fprintf( stderr, "%s: not compiled with SASL support\n",
597 return( EXIT_FAILURE );
601 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
602 fprintf( stderr, "%s: incompatible with previous "
603 "authentication choice\n", prog );
606 authmethod = LDAP_AUTH_SIMPLE;
609 #ifdef HAVE_CYRUS_SASL
610 if( sasl_authz_id != NULL ) {
611 fprintf( stderr, "%s: -X previously specified\n", prog );
614 if( version == LDAP_VERSION2 ) {
615 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
619 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
620 fprintf( stderr, "%s: -X incompatible with "
621 "authentication choice\n", prog );
624 authmethod = LDAP_AUTH_SASL;
625 version = LDAP_VERSION3;
626 sasl_authz_id = ber_strdup( optarg );
628 fprintf( stderr, "%s: not compiled with SASL support\n",
630 return( EXIT_FAILURE );
635 if( version == LDAP_VERSION2 ) {
636 fprintf( stderr, "%s: -Z incompatible with version %d\n",
640 version = LDAP_VERSION3;
643 fprintf( stderr, "%s: not compiled with TLS support\n",
645 return( EXIT_FAILURE );
649 fprintf( stderr, "%s: unrecognized option -%c\n",
656 version = LDAP_VERSION3;
658 if (authmethod == -1 && version > LDAP_VERSION2) {
659 #ifdef HAVE_CYRUS_SASL
660 authmethod = LDAP_AUTH_SASL;
662 authmethod = LDAP_AUTH_SIMPLE;
666 if ( argc != optind )
669 if ( rejfile != NULL ) {
670 if (( rejfp = fopen( rejfile, "w" )) == NULL ) {
672 return( EXIT_FAILURE );
678 if ( infile != NULL ) {
679 if (( fp = fopen( infile, "r" )) == NULL ) {
681 return( EXIT_FAILURE );
688 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
689 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
691 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
692 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
698 (void) SIGNAL( SIGPIPE, SIG_IGN );
702 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
704 fprintf( stderr, "ldap_init( %s, %d )\n",
705 ldaphost != NULL ? ldaphost : "<DEFAULT>",
709 ld = ldap_init( ldaphost, ldapport );
711 perror("ldapmodify: ldap_init");
717 fprintf( stderr, "ldap_initialize( %s )\n",
718 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
721 rc = ldap_initialize( &ld, ldapuri );
722 if( rc != LDAP_SUCCESS ) {
723 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
724 rc, ldap_err2string(rc) );
730 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
731 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
733 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
734 referrals ? "on" : "off" );
739 if (version == -1 ) {
740 version = LDAP_VERSION3;
743 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
744 != LDAP_OPT_SUCCESS )
746 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
751 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
752 ldap_perror( ld, "ldap_start_tls" );
754 return( EXIT_FAILURE );
758 if ( pw_file || want_bindpw ) {
760 rc = lutil_get_filed_password( pw_file, &passwd );
761 if( rc ) return EXIT_FAILURE;
763 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
764 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
768 if ( authmethod == LDAP_AUTH_SASL ) {
769 #ifdef HAVE_CYRUS_SASL
772 if( sasl_secprops != NULL ) {
773 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
774 (void *) sasl_secprops );
776 if( rc != LDAP_OPT_SUCCESS ) {
778 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
780 return( EXIT_FAILURE );
784 defaults = lutil_sasl_defaults( ld,
791 rc = ldap_sasl_interactive_bind_s( ld, binddn,
792 sasl_mech, NULL, NULL,
793 sasl_flags, lutil_sasl_interact, defaults );
795 if( rc != LDAP_SUCCESS ) {
796 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
797 return( EXIT_FAILURE );
800 fprintf( stderr, "%s: not compiled with SASL support\n",
802 return( EXIT_FAILURE );
806 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
808 ldap_perror( ld, "ldap_bind" );
809 return( EXIT_FAILURE );
818 if ( authzid || manageDSAit || noop ) {
819 int err, crit=0, i=0;
821 LDAPControl *ctrls[4];
824 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
825 c[i].ldctl_value.bv_val = authzid;
826 c[i].ldctl_value.bv_len = strlen( authzid );
827 c[i].ldctl_iscritical = 1;
829 if( c[i].ldctl_iscritical ) crit++;
835 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
836 c[i].ldctl_value.bv_val = NULL;
837 c[i].ldctl_value.bv_len = 0;
838 c[i].ldctl_iscritical = manageDSAit > 1;
840 if( c[i].ldctl_iscritical ) crit++;
846 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
847 c[i].ldctl_value.bv_val = NULL;
848 c[i].ldctl_value.bv_len = 0;
849 c[i].ldctl_iscritical = noop > 1;
851 if( c[i].ldctl_iscritical ) crit++;
856 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
858 if( err != LDAP_OPT_SUCCESS ) {
859 fprintf( stderr, "Could not set %scontrols\n",
860 crit ? "critical " : "" );
869 while (( rc == 0 || contoper ) &&
870 ( rbuf = read_one_record( fp )) != NULL ) {
876 len = strlen( rbuf );
877 if (( rejbuf = (char *)ber_memalloc( len+1 )) == NULL ) {
879 exit( EXIT_FAILURE );
881 memcpy( rejbuf, rbuf, len+1 );
884 rc = process_ldif_rec( start, count );
889 fprintf(rejfp, "# Error: %s (%d)", ldap_err2string(rc), rc);
891 ldap_get_option(ld, LDAP_OPT_MATCHED_DN, &matched_msg);
892 if ( matched_msg != NULL && *matched_msg != '\0' ) {
893 fprintf( rejfp, ", matched DN: %s", matched_msg );
896 ldap_get_option(ld, LDAP_OPT_ERROR_STRING, &error_msg);
897 if ( error_msg != NULL && *error_msg != '\0' ) {
898 fprintf( rejfp, ", additional info: %s", error_msg );
900 fprintf( rejfp, "\n%s\n", rejbuf );
911 if ( rejfp != NULL ) {
920 process_ldif_rec( char *rbuf, int count )
922 char *line, *dn, *type, *newrdn, *newsup, *p;
923 int rc, linenum, modop, replicaport;
924 int expect_modop, expect_sep, expect_ct, expect_newrdn, expect_newsup;
925 int expect_deleteoldrdn, deleteoldrdn;
926 int saw_replica, use_record, new_entry, delete_entry, got_all;
930 LDAPControl **pctrls;
934 rc = got_all = saw_replica = delete_entry = modop = expect_modop = 0;
935 expect_deleteoldrdn = expect_newrdn = expect_newsup = 0;
936 expect_sep = expect_ct = 0;
943 dn = newrdn = newsup = NULL;
945 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
948 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
954 if ( ldif_parse_line( line, &type, &val.bv_val, &val.bv_len ) < 0 ) {
955 fprintf( stderr, "%s: invalid format (line %d) entry: \"%s\"\n",
956 prog, linenum, dn == NULL ? "" : dn );
957 rc = LDAP_PARAM_ERROR;
962 if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
964 if (( p = strchr( val.bv_val, ':' )) == NULL ) {
968 replicaport = atoi( p );
970 if ( ldaphost != NULL && strcasecmp( val.bv_val, ldaphost ) == 0 &&
971 replicaport == ldapport ) {
974 } else if ( count == 1 && linenum == 1 &&
975 strcasecmp( type, T_VERSION_STR ) == 0 )
977 if( val.bv_len == 0 || atoi(val.bv_val) != 1 ) {
978 fprintf( stderr, "%s: invalid version %s, line %d (ignored)\n",
979 prog, val.bv_val, linenum );
983 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
984 if (( dn = ber_strdup( val.bv_val )) == NULL ) {
986 exit( EXIT_FAILURE );
990 goto end_line; /* skip all lines until we see "dn:" */
995 /* Check for "control" tag after dn and before changetype. */
996 if (strcasecmp(type, T_CONTROL_STR) == 0) {
997 /* Parse and add it to the list of controls */
998 rc = parse_ldif_control( line, &pctrls );
1000 fprintf( stderr, "%s: Error processing %s line, line %d: %s\n",
1001 prog, T_CONTROL_STR, linenum, ldap_err2string(rc) );
1007 if ( !use_record && saw_replica ) {
1008 printf( "%s: skipping change record for entry: %s\n"
1009 "\t(LDAP host/port does not match replica: lines)\n",
1012 ber_memfree( type );
1013 ber_memfree( val.bv_val );
1017 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
1018 #ifdef LIBERAL_CHANGETYPE_MODOP
1019 /* trim trailing spaces (and log warning ...) */
1022 for ( icnt = val.bv_len; --icnt > 0; ) {
1023 if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
1028 if ( ++icnt != val.bv_len ) {
1029 fprintf( stderr, "%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n",
1030 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
1031 val.bv_val[icnt] = '\0';
1033 #endif /* LIBERAL_CHANGETYPE_MODOP */
1035 if ( strcasecmp( val.bv_val, T_MODIFYCTSTR ) == 0 ) {
1038 } else if ( strcasecmp( val.bv_val, T_ADDCTSTR ) == 0 ) {
1040 } else if ( strcasecmp( val.bv_val, T_MODRDNCTSTR ) == 0
1041 || strcasecmp( val.bv_val, T_MODDNCTSTR ) == 0
1042 || strcasecmp( val.bv_val, T_RENAMECTSTR ) == 0)
1045 } else if ( strcasecmp( val.bv_val, T_DELETECTSTR ) == 0 ) {
1046 got_all = delete_entry = 1;
1049 "%s: unknown %s \"%s\" (line %d of entry \"%s\")\n",
1050 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
1051 rc = LDAP_PARAM_ERROR;
1054 } else if ( ldapadd ) { /* missing changetype => add */
1056 modop = LDAP_MOD_ADD;
1058 expect_modop = 1; /* missing changetype => modify */
1062 if ( expect_modop ) {
1063 #ifdef LIBERAL_CHANGETYPE_MODOP
1064 /* trim trailing spaces (and log warning ...) */
1067 for ( icnt = val.bv_len; --icnt > 0; ) {
1068 if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
1073 if ( ++icnt != val.bv_len ) {
1074 fprintf( stderr, "%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n",
1075 prog, type, val.bv_val, linenum, dn );
1076 val.bv_val[icnt] = '\0';
1078 #endif /* LIBERAL_CHANGETYPE_MODOP */
1082 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
1083 modop = LDAP_MOD_ADD;
1085 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
1086 modop = LDAP_MOD_REPLACE;
1087 addmodifyop( &pmods, modop, val.bv_val, NULL );
1089 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
1090 modop = LDAP_MOD_DELETE;
1091 addmodifyop( &pmods, modop, val.bv_val, NULL );
1093 } else { /* no modify op: use default */
1094 modop = ldapadd ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
1098 if ( expect_newrdn ) {
1099 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
1100 if (( newrdn = ber_strdup( val.bv_val )) == NULL ) {
1102 exit( EXIT_FAILURE );
1104 expect_deleteoldrdn = 1;
1107 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
1108 prog, T_NEWRDNSTR, type, linenum, dn );
1109 rc = LDAP_PARAM_ERROR;
1111 } else if ( expect_deleteoldrdn ) {
1112 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
1113 deleteoldrdn = ( *val.bv_val == '0' ) ? 0 : 1;
1114 expect_deleteoldrdn = 0;
1118 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
1119 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
1120 rc = LDAP_PARAM_ERROR;
1122 } else if ( expect_newsup ) {
1123 if ( strcasecmp( type, T_NEWSUPSTR ) == 0 ) {
1124 if (( newsup = ber_strdup( val.bv_val )) == NULL ) {
1126 exit( EXIT_FAILURE );
1130 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
1131 prog, T_NEWSUPSTR, type, linenum, dn );
1132 rc = LDAP_PARAM_ERROR;
1134 } else if ( got_all ) {
1136 "%s: extra lines at end (line %d of entry \"%s\")\n",
1137 prog, linenum, dn );
1138 rc = LDAP_PARAM_ERROR;
1140 addmodifyop( &pmods, modop, type, &val );
1144 ber_memfree( type );
1145 ber_memfree( val.bv_val );
1148 if( linenum == 0 ) {
1152 if( version && linenum == 1 ) {
1156 /* If default controls are set (as with -M option) and controls are
1157 specified in the LDIF file, we must add the default controls to
1158 the list of controls sent with the ldap operation.
1162 LDAPControl **defctrls = NULL; /* Default server controls */
1163 LDAPControl **newctrls = NULL;
1164 ldap_get_option(ld, LDAP_OPT_SERVER_CONTROLS, &defctrls);
1166 int npc=0; /* Number of LDIF controls */
1167 int ndefc=0; /* Number of default controls */
1168 while (pctrls[npc]) /* Count LDIF controls */
1170 while (defctrls[ndefc]) /* Count default controls */
1172 newctrls = ber_memrealloc(pctrls, (npc+ndefc+1)*sizeof(LDAPControl*));
1173 if (newctrls == NULL)
1174 rc = LDAP_NO_MEMORY;
1178 for (i=npc; i<npc+ndefc; i++) {
1179 pctrls[i] = ldap_control_dup(defctrls[i-npc]);
1180 if (pctrls[i] == NULL) {
1181 rc = LDAP_NO_MEMORY;
1185 pctrls[npc+ndefc] = NULL;
1186 ldap_controls_free(defctrls); /* Must be freed by library */
1194 if ( delete_entry ) {
1195 rc = dodelete( dn, pctrls );
1196 } else if ( newrdn != NULL ) {
1197 rc = dorename( dn, newrdn, newsup, deleteoldrdn, pctrls );
1199 rc = domodify( dn, pmods, pctrls, new_entry );
1202 if ( rc == LDAP_SUCCESS ) {
1210 if ( newrdn != NULL ) {
1213 if ( pmods != NULL ) {
1214 ldap_mods_free( pmods, 1 );
1217 if (pctrls != NULL) {
1218 ldap_controls_free( pctrls );
1224 /* Parse an LDIF control line of the form
1225 control: oid [true/false] [: value] or
1226 control: oid [true/false] [:: base64-value] or
1227 control: oid [true/false] [:< url]
1228 The control is added to the list of controls in *ppctrls.
1231 parse_ldif_control( char *line,
1232 LDAPControl ***ppctrls )
1235 int criticality = 0; /* Default is false if not present */
1238 ber_len_t value_len = 0;
1240 char *s, *oidStart, *pcolon;
1241 LDAPControl *newctrl = NULL;
1242 LDAPControl **pctrls = NULL;
1247 s = line + strlen(T_CONTROL_STR); /* Skip over "control" */
1248 pcolon = s; /* Save this position for later */
1249 if (*s++ != ':') /* Make sure colon follows */
1250 return ( LDAP_PARAM_ERROR );
1251 while (*s && isspace(*s)) s++; /* Skip white space before OID */
1253 /* OID should come next. Validate and extract it. */
1255 return ( LDAP_PARAM_ERROR );
1257 while (isdigit(*s) || *s == '.') s++; /* OID should be digits or . */
1259 return ( LDAP_PARAM_ERROR ); /* OID was not present */
1260 if (*s) { /* End of OID should be space or NULL */
1262 return ( LDAP_PARAM_ERROR ); /* else OID contained invalid chars */
1263 *s++ = 0; /* Replace space with null to terminate */
1267 oid = ber_strdup(oidStart);
1269 return ( LDAP_NO_MEMORY );
1271 /* Optional Criticality field is next. */
1272 while (*s && isspace(*s)) s++; /* Skip white space before criticality */
1273 if (strncasecmp(s, "true", 4) == 0) {
1277 else if (strncasecmp(s, "false", 5) == 0) {
1282 /* Optional value field is next */
1283 while (*s && isspace(*s)) s++; /* Skip white space before value */
1285 if (*s != ':') { /* If value is present, must start with : */
1286 rc = LDAP_PARAM_ERROR;
1290 /* Shift value down over OID and criticality so it's in the form
1292 control:: base64-value
1294 Then we can use ldif_parse_line to extract and decode the value
1296 while ( (*pcolon++ = *s++) != 0) /* Shift value */
1298 rc = ldif_parse_line(line, &type, &val, &value_len);
1299 if (type) ber_memfree(type); /* Don't need this field*/
1301 rc = LDAP_PARAM_ERROR;
1306 /* Create a new LDAPControl structure. */
1307 newctrl = (LDAPControl *)ber_memalloc(sizeof(LDAPControl));
1308 if ( newctrl == NULL ) {
1309 rc = LDAP_NO_MEMORY;
1312 newctrl->ldctl_oid = oid;
1314 newctrl->ldctl_iscritical = criticality;
1315 newctrl->ldctl_value.bv_len = value_len;
1316 newctrl->ldctl_value.bv_val = val;
1319 /* Add the new control to the passed-in list of controls. */
1322 while ( pctrls[i] ) /* Count the # of controls passed in */
1325 /* Allocate 1 more slot for the new control and 1 for the NULL. */
1326 pctrls = (LDAPControl **)ber_memrealloc(pctrls, (i+2)*(sizeof(LDAPControl *)));
1327 if (pctrls == NULL) {
1328 rc = LDAP_NO_MEMORY;
1331 pctrls[i] = newctrl;
1338 if (newctrl->ldctl_oid)
1339 ber_memfree(newctrl->ldctl_oid);
1340 if (newctrl->ldctl_value.bv_val)
1341 ber_memfree(newctrl->ldctl_value.bv_val);
1342 ber_memfree(newctrl);
1358 struct berval *val )
1364 modop |= LDAP_MOD_BVALUES;
1367 if ( pmods != NULL ) {
1368 for ( ; pmods[ i ] != NULL; ++i ) {
1369 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
1370 pmods[ i ]->mod_op == modop )
1377 if ( pmods == NULL || pmods[ i ] == NULL ) {
1378 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
1379 sizeof( LDAPMod * ))) == NULL )
1381 perror( "realloc" );
1382 exit( EXIT_FAILURE );
1386 pmods[ i + 1 ] = NULL;
1388 pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod ));
1389 if ( pmods[ i ] == NULL ) {
1391 exit( EXIT_FAILURE );
1394 pmods[ i ]->mod_op = modop;
1395 pmods[ i ]->mod_type = ber_strdup( attr );
1396 if ( pmods[ i ]->mod_type == NULL ) {
1398 exit( EXIT_FAILURE );
1402 if ( val != NULL ) {
1404 if ( pmods[ i ]->mod_bvalues != NULL ) {
1405 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
1410 pmods[ i ]->mod_bvalues = (struct berval **) ber_memrealloc(
1411 pmods[ i ]->mod_bvalues, (j + 2) * sizeof( struct berval * ));
1412 if ( pmods[ i ]->mod_bvalues == NULL ) {
1413 perror( "ber_realloc" );
1414 exit( EXIT_FAILURE );
1417 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
1418 pmods[ i ]->mod_bvalues[ j ] = ber_bvdup( val );
1419 if ( pmods[ i ]->mod_bvalues[ j ] == NULL ) {
1420 perror( "ber_bvdup" );
1421 exit( EXIT_FAILURE );
1431 LDAPControl **pctrls,
1434 int i, j, k, notascii, op;
1437 if ( pmods == NULL ) {
1438 fprintf( stderr, "%s: no attributes to change or add (entry=\"%s\")\n",
1440 return( LDAP_PARAM_ERROR );
1443 for ( i = 0; pmods[ i ] != NULL; ++i ) {
1444 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1445 if( op == LDAP_MOD_ADD && ( pmods[i]->mod_bvalues == NULL )) {
1447 "%s: attribute \"%s\" has no values (entry=\"%s\")\n",
1448 prog, pmods[i]->mod_type, dn );
1449 return LDAP_PARAM_ERROR;
1454 for ( i = 0; pmods[ i ] != NULL; ++i ) {
1455 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1456 printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
1457 "replace" : op == LDAP_MOD_ADD ?
1458 "add" : "delete", pmods[ i ]->mod_type );
1459 if ( pmods[ i ]->mod_bvalues != NULL ) {
1460 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
1461 bvp = pmods[ i ]->mod_bvalues[ j ];
1463 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
1464 if ( !isascii( bvp->bv_val[ k ] )) {
1470 printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
1472 printf( "\t%s\n", bvp->bv_val );
1480 printf( "%sadding new entry \"%s\"\n", not ? "!" : "", dn );
1482 printf( "%smodifying entry \"%s\"\n", not ? "!" : "", dn );
1487 i = ldap_add_ext_s( ld, dn, pmods, pctrls, NULL );
1489 i = ldap_modify_ext_s( ld, dn, pmods, pctrls, NULL );
1491 if ( i != LDAP_SUCCESS ) {
1492 /* print error message about failed update including DN */
1493 fprintf( stderr, "%s: update failed: %s\n", prog, dn );
1494 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
1495 } else if ( verbose ) {
1496 printf( "modify complete\n" );
1511 LDAPControl **pctrls )
1515 printf( "%sdeleting entry \"%s\"\n", not ? "!" : "", dn );
1517 if (( rc = ldap_delete_ext_s( ld, dn, pctrls, NULL )) != LDAP_SUCCESS ) {
1518 fprintf( stderr, "%s: delete failed: %s\n", prog, dn );
1519 ldap_perror( ld, "ldap_delete" );
1520 } else if ( verbose ) {
1521 printf( "delete complete" );
1539 LDAPControl **pctrls )
1544 printf( "%smodifying rdn of entry \"%s\"\n", not ? "!" : "", dn );
1546 printf( "\tnew RDN: \"%s\" (%skeep existing values)\n",
1547 newrdn, deleteoldrdn ? "do not " : "" );
1550 if (( rc = ldap_rename_s( ld, dn, newrdn, newsup, deleteoldrdn, pctrls, NULL ))
1552 fprintf( stderr, "%s: rename failed: %s\n", prog, dn );
1553 ldap_perror( ld, "ldap_modrdn" );
1555 printf( "modrdn completed\n" );
1568 read_one_record( FILE *fp )
1570 char *buf, line[ LDAPMOD_MAXLINE ];
1576 while ( fgets( line, sizeof(line), fp ) != NULL ) {
1577 int len = strlen( line );
1579 if( len < 2 || ( len == 2 && *line == '\r' )) {
1587 if ( lcur + len + 1 > lmax ) {
1588 lmax = LDAPMOD_MAXLINE
1589 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
1591 if (( buf = (char *)ber_memrealloc( buf, lmax )) == NULL ) {
1592 perror( "realloc" );
1593 exit( EXIT_FAILURE );
1597 strcpy( buf + lcur, line );