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 );
284 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
286 fprintf( stderr, "manageDSAit control previously specified\n");
289 if( cvalue != NULL ) {
290 fprintf( stderr, "manageDSAit: no control value expected\n" );
295 manageDSAit = 1 + crit;
298 } else if ( strcasecmp( control, "noop" ) == 0 ) {
300 fprintf( stderr, "noop control previously specified\n");
303 if( cvalue != NULL ) {
304 fprintf( stderr, "noop: no control value expected\n" );
313 fprintf( stderr, "Invalid general control name: %s\n", control );
317 case 'h': /* ldap host */
318 if( ldapuri != NULL ) {
319 fprintf( stderr, "%s: -h incompatible with -H\n", prog );
322 if( ldaphost != NULL ) {
323 fprintf( stderr, "%s: -h previously specified\n", prog );
326 ldaphost = ber_strdup( optarg );
328 case 'H': /* ldap URI */
329 if( ldaphost != NULL ) {
330 fprintf( stderr, "%s: -H incompatible with -h\n", prog );
334 fprintf( stderr, "%s: -H incompatible with -p\n", prog );
337 if( ldapuri != NULL ) {
338 fprintf( stderr, "%s: -H previously specified\n", prog );
341 ldapuri = ber_strdup( optarg );
344 #ifdef HAVE_CYRUS_SASL
345 if( version == LDAP_VERSION2 ) {
346 fprintf( stderr, "%s: -I incompatible with version %d\n",
350 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
351 fprintf( stderr, "%s: incompatible previous "
352 "authentication choice\n",
356 authmethod = LDAP_AUTH_SASL;
357 version = LDAP_VERSION3;
358 sasl_flags = LDAP_SASL_INTERACTIVE;
361 fprintf( stderr, "%s: was not compiled with SASL support\n",
363 return( EXIT_FAILURE );
365 case 'k': /* kerberos bind */
366 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
367 if( version > LDAP_VERSION2 ) {
368 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
373 if( authmethod != -1 ) {
374 fprintf( stderr, "%s: -k incompatible with previous "
375 "authentication choice\n", prog );
379 authmethod = LDAP_AUTH_KRBV4;
381 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
385 case 'K': /* kerberos bind, part one only */
386 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
387 if( version > LDAP_VERSION2 ) {
388 fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
392 if( authmethod != -1 ) {
393 fprintf( stderr, "%s: incompatible with previous "
394 "authentication choice\n", prog );
398 authmethod = LDAP_AUTH_KRBV41;
400 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
401 return( EXIT_FAILURE );
405 /* enable Manage DSA IT */
406 if( version == LDAP_VERSION2 ) {
407 fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
412 version = LDAP_VERSION3;
414 case 'n': /* print deletes, don't actually do them */
418 #ifdef HAVE_CYRUS_SASL
419 if( sasl_secprops != NULL ) {
420 fprintf( stderr, "%s: -O previously specified\n", prog );
423 if( version == LDAP_VERSION2 ) {
424 fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
428 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
429 fprintf( stderr, "%s: incompatible previous "
430 "authentication choice\n", prog );
433 authmethod = LDAP_AUTH_SASL;
434 version = LDAP_VERSION3;
435 sasl_secprops = ber_strdup( optarg );
437 fprintf( stderr, "%s: not compiled with SASL support\n",
439 return( EXIT_FAILURE );
444 fprintf( stderr, "%s: -p previously specified\n", prog );
447 ldapport = atoi( optarg );
450 switch( atoi(optarg) ) {
452 if( version == LDAP_VERSION3 ) {
453 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
457 version = LDAP_VERSION2;
460 if( version == LDAP_VERSION2 ) {
461 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
465 version = LDAP_VERSION3;
468 fprintf( stderr, "%s: protocol version should be 2 or 3\n",
471 return( EXIT_FAILURE );
474 #ifdef HAVE_CYRUS_SASL
475 if( version == LDAP_VERSION2 ) {
476 fprintf( stderr, "%s: -Q incompatible with version %d\n",
480 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
481 fprintf( stderr, "%s: incompatible previous "
482 "authentication choice\n",
486 authmethod = LDAP_AUTH_SASL;
487 version = LDAP_VERSION3;
488 sasl_flags = LDAP_SASL_QUIET;
491 fprintf( stderr, "%s: not compiled with SASL support\n",
493 return( EXIT_FAILURE );
495 case 'r': /* replace (obsolete) */
499 #ifdef HAVE_CYRUS_SASL
500 if( sasl_realm != NULL ) {
501 fprintf( stderr, "%s: -R previously specified\n", prog );
504 if( version == LDAP_VERSION2 ) {
505 fprintf( stderr, "%s: -R incompatible with version %d\n",
509 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
510 fprintf( stderr, "%s: incompatible previous "
511 "authentication choice\n",
515 authmethod = LDAP_AUTH_SASL;
516 version = LDAP_VERSION3;
517 sasl_realm = ber_strdup( optarg );
519 fprintf( stderr, "%s: not compiled with SASL support\n",
521 return( EXIT_FAILURE );
524 case 'S': /* skipped modifications to file */
525 if( rejfile != NULL ) {
526 fprintf( stderr, "%s: -S previously specified\n", prog );
529 rejfile = ber_strdup( optarg );
532 #ifdef HAVE_CYRUS_SASL
533 if( sasl_authc_id != NULL ) {
534 fprintf( stderr, "%s: -U previously specified\n", prog );
537 if( version == LDAP_VERSION2 ) {
538 fprintf( stderr, "%s: -U incompatible with version %d\n",
542 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
543 fprintf( stderr, "%s: incompatible previous "
544 "authentication choice\n",
548 authmethod = LDAP_AUTH_SASL;
549 version = LDAP_VERSION3;
550 sasl_authc_id = ber_strdup( optarg );
552 fprintf( stderr, "%s: not compiled with SASL support\n",
554 return( EXIT_FAILURE );
557 case 'v': /* verbose mode */
560 case 'w': /* password */
561 passwd.bv_val = ber_strdup( optarg );
565 for( p = optarg; *p != '\0'; p++ ) {
569 passwd.bv_len = strlen( passwd.bv_val );
578 #ifdef HAVE_CYRUS_SASL
579 if( sasl_mech != NULL ) {
580 fprintf( stderr, "%s: -Y previously specified\n", prog );
583 if( version == LDAP_VERSION2 ) {
584 fprintf( stderr, "%s: -Y incompatible with version %d\n",
588 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
589 fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
592 authmethod = LDAP_AUTH_SASL;
593 version = LDAP_VERSION3;
594 sasl_mech = ber_strdup( optarg );
596 fprintf( stderr, "%s: not compiled with SASL support\n",
598 return( EXIT_FAILURE );
602 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
603 fprintf( stderr, "%s: incompatible with previous "
604 "authentication choice\n", prog );
607 authmethod = LDAP_AUTH_SIMPLE;
610 #ifdef HAVE_CYRUS_SASL
611 if( sasl_authz_id != NULL ) {
612 fprintf( stderr, "%s: -X previously specified\n", prog );
615 if( version == LDAP_VERSION2 ) {
616 fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
620 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
621 fprintf( stderr, "%s: -X incompatible with "
622 "authentication choice\n", prog );
625 authmethod = LDAP_AUTH_SASL;
626 version = LDAP_VERSION3;
627 sasl_authz_id = ber_strdup( optarg );
629 fprintf( stderr, "%s: not compiled with SASL support\n",
631 return( EXIT_FAILURE );
636 if( version == LDAP_VERSION2 ) {
637 fprintf( stderr, "%s: -Z incompatible with version %d\n",
641 version = LDAP_VERSION3;
644 fprintf( stderr, "%s: not compiled with TLS support\n",
646 return( EXIT_FAILURE );
650 fprintf( stderr, "%s: unrecognized option -%c\n",
657 version = LDAP_VERSION3;
659 if (authmethod == -1 && version > LDAP_VERSION2) {
660 #ifdef HAVE_CYRUS_SASL
661 authmethod = LDAP_AUTH_SASL;
663 authmethod = LDAP_AUTH_SIMPLE;
667 if ( argc != optind )
670 if ( rejfile != NULL ) {
671 if (( rejfp = fopen( rejfile, "w" )) == NULL ) {
673 return( EXIT_FAILURE );
679 if ( infile != NULL ) {
680 if (( fp = fopen( infile, "r" )) == NULL ) {
682 return( EXIT_FAILURE );
689 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
690 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
692 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
693 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
699 (void) SIGNAL( SIGPIPE, SIG_IGN );
703 if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
705 fprintf( stderr, "ldap_init( %s, %d )\n",
706 ldaphost != NULL ? ldaphost : "<DEFAULT>",
710 ld = ldap_init( ldaphost, ldapport );
712 perror("ldapmodify: ldap_init");
718 fprintf( stderr, "ldap_initialize( %s )\n",
719 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
722 rc = ldap_initialize( &ld, ldapuri );
723 if( rc != LDAP_SUCCESS ) {
724 fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
725 rc, ldap_err2string(rc) );
731 if( ldap_set_option( ld, LDAP_OPT_REFERRALS,
732 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
734 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
735 referrals ? "on" : "off" );
740 if (version == -1 ) {
741 version = LDAP_VERSION3;
744 if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
745 != LDAP_OPT_SUCCESS )
747 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
752 if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
753 ldap_perror( ld, "ldap_start_tls" );
755 return( EXIT_FAILURE );
759 if ( pw_file || want_bindpw ) {
761 rc = lutil_get_filed_password( pw_file, &passwd );
762 if( rc ) return EXIT_FAILURE;
764 passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
765 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
769 if ( authmethod == LDAP_AUTH_SASL ) {
770 #ifdef HAVE_CYRUS_SASL
773 if( sasl_secprops != NULL ) {
774 rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
775 (void *) sasl_secprops );
777 if( rc != LDAP_OPT_SUCCESS ) {
779 "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
781 return( EXIT_FAILURE );
785 defaults = lutil_sasl_defaults( ld,
792 rc = ldap_sasl_interactive_bind_s( ld, binddn,
793 sasl_mech, NULL, NULL,
794 sasl_flags, lutil_sasl_interact, defaults );
796 if( rc != LDAP_SUCCESS ) {
797 ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
798 return( EXIT_FAILURE );
801 fprintf( stderr, "%s: not compiled with SASL support\n",
803 return( EXIT_FAILURE );
807 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
809 ldap_perror( ld, "ldap_bind" );
810 return( EXIT_FAILURE );
819 if ( authzid || manageDSAit || noop ) {
820 int err, crit=0, i=0;
822 LDAPControl *ctrls[4];
825 c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
826 c[i].ldctl_value.bv_val = authzid;
827 c[i].ldctl_value.bv_len = strlen( authzid );
828 c[i].ldctl_iscritical = 1;
830 if( c[i].ldctl_iscritical ) crit++;
836 c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
837 c[i].ldctl_value.bv_val = NULL;
838 c[i].ldctl_value.bv_len = 0;
839 c[i].ldctl_iscritical = manageDSAit > 1;
841 if( c[i].ldctl_iscritical ) crit++;
847 c[i].ldctl_oid = LDAP_CONTROL_NOOP;
848 c[i].ldctl_value.bv_val = NULL;
849 c[i].ldctl_value.bv_len = 0;
850 c[i].ldctl_iscritical = noop > 1;
852 if( c[i].ldctl_iscritical ) crit++;
857 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
859 if( err != LDAP_OPT_SUCCESS ) {
860 fprintf( stderr, "Could not set %scontrols\n",
861 crit ? "critical " : "" );
870 while (( rc == 0 || contoper ) &&
871 ( rbuf = read_one_record( fp )) != NULL ) {
877 len = strlen( rbuf );
878 if (( rejbuf = (char *)ber_memalloc( len+1 )) == NULL ) {
880 exit( EXIT_FAILURE );
882 memcpy( rejbuf, rbuf, len+1 );
885 rc = process_ldif_rec( start, count );
890 fprintf(rejfp, "# Error: %s (%d)", ldap_err2string(rc), rc);
892 ldap_get_option(ld, LDAP_OPT_MATCHED_DN, &matched_msg);
893 if ( matched_msg != NULL && *matched_msg != '\0' ) {
894 fprintf( rejfp, ", matched DN: %s", matched_msg );
897 ldap_get_option(ld, LDAP_OPT_ERROR_STRING, &error_msg);
898 if ( error_msg != NULL && *error_msg != '\0' ) {
899 fprintf( rejfp, ", additional info: %s", error_msg );
901 fprintf( rejfp, "\n%s\n", rejbuf );
912 if ( rejfp != NULL ) {
921 process_ldif_rec( char *rbuf, int count )
923 char *line, *dn, *type, *newrdn, *newsup, *p;
924 int rc, linenum, modop, replicaport;
925 int expect_modop, expect_sep, expect_ct, expect_newrdn, expect_newsup;
926 int expect_deleteoldrdn, deleteoldrdn;
927 int saw_replica, use_record, new_entry, delete_entry, got_all;
931 LDAPControl **pctrls;
935 rc = got_all = saw_replica = delete_entry = modop = expect_modop = 0;
936 expect_deleteoldrdn = expect_newrdn = expect_newsup = 0;
937 expect_sep = expect_ct = 0;
944 dn = newrdn = newsup = NULL;
946 while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
949 if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
955 if ( ldif_parse_line( line, &type, &val.bv_val, &val.bv_len ) < 0 ) {
956 fprintf( stderr, "%s: invalid format (line %d) entry: \"%s\"\n",
957 prog, linenum, dn == NULL ? "" : dn );
958 rc = LDAP_PARAM_ERROR;
963 if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
965 if (( p = strchr( val.bv_val, ':' )) == NULL ) {
969 replicaport = atoi( p );
971 if ( ldaphost != NULL && strcasecmp( val.bv_val, ldaphost ) == 0 &&
972 replicaport == ldapport ) {
975 } else if ( count == 1 && linenum == 1 &&
976 strcasecmp( type, T_VERSION_STR ) == 0 )
978 if( val.bv_len == 0 || atoi(val.bv_val) != 1 ) {
979 fprintf( stderr, "%s: invalid version %s, line %d (ignored)\n",
980 prog, val.bv_val == NULL ? "(null)" : val.bv_val, linenum );
984 } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
985 if (( dn = ber_strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
987 exit( EXIT_FAILURE );
991 goto end_line; /* skip all lines until we see "dn:" */
996 /* Check for "control" tag after dn and before changetype. */
997 if (strcasecmp(type, T_CONTROL_STR) == 0) {
998 /* Parse and add it to the list of controls */
999 rc = parse_ldif_control( line, &pctrls );
1001 fprintf( stderr, "%s: Error processing %s line, line %d: %s\n",
1002 prog, T_CONTROL_STR, linenum, ldap_err2string(rc) );
1008 if ( !use_record && saw_replica ) {
1009 printf( "%s: skipping change record for entry: %s\n"
1010 "\t(LDAP host/port does not match replica: lines)\n",
1013 ber_memfree( type );
1014 ber_memfree( val.bv_val );
1018 if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
1019 #ifdef LIBERAL_CHANGETYPE_MODOP
1020 /* trim trailing spaces (and log warning ...) */
1023 for ( icnt = val.bv_len; --icnt > 0; ) {
1024 if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
1029 if ( ++icnt != val.bv_len ) {
1030 fprintf( stderr, "%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n",
1031 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
1032 val.bv_val[icnt] = '\0';
1034 #endif /* LIBERAL_CHANGETYPE_MODOP */
1036 if ( strcasecmp( val.bv_val, T_MODIFYCTSTR ) == 0 ) {
1039 } else if ( strcasecmp( val.bv_val, T_ADDCTSTR ) == 0 ) {
1041 } else if ( strcasecmp( val.bv_val, T_MODRDNCTSTR ) == 0
1042 || strcasecmp( val.bv_val, T_MODDNCTSTR ) == 0
1043 || strcasecmp( val.bv_val, T_RENAMECTSTR ) == 0)
1046 } else if ( strcasecmp( val.bv_val, T_DELETECTSTR ) == 0 ) {
1047 got_all = delete_entry = 1;
1050 "%s: unknown %s \"%s\" (line %d of entry \"%s\")\n",
1051 prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
1052 rc = LDAP_PARAM_ERROR;
1055 } else if ( ldapadd ) { /* missing changetype => add */
1057 modop = LDAP_MOD_ADD;
1059 expect_modop = 1; /* missing changetype => modify */
1063 if ( expect_modop ) {
1064 #ifdef LIBERAL_CHANGETYPE_MODOP
1065 /* trim trailing spaces (and log warning ...) */
1068 for ( icnt = val.bv_len; --icnt > 0; ) {
1069 if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
1074 if ( ++icnt != val.bv_len ) {
1075 fprintf( stderr, "%s: illegal trailing space after \"%s: %s\" trimmed (line %d of entry \"%s\")\n",
1076 prog, type, val.bv_val, linenum, dn );
1077 val.bv_val[icnt] = '\0';
1079 #endif /* LIBERAL_CHANGETYPE_MODOP */
1083 if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
1084 modop = LDAP_MOD_ADD;
1086 } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
1087 modop = LDAP_MOD_REPLACE;
1088 addmodifyop( &pmods, modop, val.bv_val, NULL );
1090 } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
1091 modop = LDAP_MOD_DELETE;
1092 addmodifyop( &pmods, modop, val.bv_val, NULL );
1094 } else { /* no modify op: use default */
1095 modop = ldapadd ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
1099 if ( expect_newrdn ) {
1100 if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
1101 if (( newrdn = ber_strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
1103 exit( EXIT_FAILURE );
1105 expect_deleteoldrdn = 1;
1108 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
1109 prog, T_NEWRDNSTR, type, linenum, dn );
1110 rc = LDAP_PARAM_ERROR;
1112 } else if ( expect_deleteoldrdn ) {
1113 if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
1114 deleteoldrdn = ( *val.bv_val == '0' ) ? 0 : 1;
1115 expect_deleteoldrdn = 0;
1119 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
1120 prog, T_DELETEOLDRDNSTR, type, linenum, dn );
1121 rc = LDAP_PARAM_ERROR;
1123 } else if ( expect_newsup ) {
1124 if ( strcasecmp( type, T_NEWSUPSTR ) == 0 ) {
1125 if (( newsup = ber_strdup( val.bv_val ? val.bv_val : "" )) == NULL ) {
1127 exit( EXIT_FAILURE );
1131 fprintf( stderr, "%s: expecting \"%s:\" but saw \"%s:\" (line %d of entry \"%s\")\n",
1132 prog, T_NEWSUPSTR, type, linenum, dn );
1133 rc = LDAP_PARAM_ERROR;
1135 } else if ( got_all ) {
1137 "%s: extra lines at end (line %d of entry \"%s\")\n",
1138 prog, linenum, dn );
1139 rc = LDAP_PARAM_ERROR;
1141 addmodifyop( &pmods, modop, type, val.bv_val == NULL ? NULL : &val );
1145 ber_memfree( type );
1146 ber_memfree( val.bv_val );
1149 if( linenum == 0 ) {
1153 if( version && linenum == 1 ) {
1157 /* If default controls are set (as with -M option) and controls are
1158 specified in the LDIF file, we must add the default controls to
1159 the list of controls sent with the ldap operation.
1163 LDAPControl **defctrls = NULL; /* Default server controls */
1164 LDAPControl **newctrls = NULL;
1165 ldap_get_option(ld, LDAP_OPT_SERVER_CONTROLS, &defctrls);
1167 int npc=0; /* Number of LDIF controls */
1168 int ndefc=0; /* Number of default controls */
1169 while (pctrls[npc]) /* Count LDIF controls */
1171 while (defctrls[ndefc]) /* Count default controls */
1173 newctrls = ber_memrealloc(pctrls, (npc+ndefc+1)*sizeof(LDAPControl*));
1174 if (newctrls == NULL)
1175 rc = LDAP_NO_MEMORY;
1179 for (i=npc; i<npc+ndefc; i++) {
1180 pctrls[i] = ldap_control_dup(defctrls[i-npc]);
1181 if (pctrls[i] == NULL) {
1182 rc = LDAP_NO_MEMORY;
1186 pctrls[npc+ndefc] = NULL;
1187 ldap_controls_free(defctrls); /* Must be freed by library */
1195 if ( delete_entry ) {
1196 rc = dodelete( dn, pctrls );
1197 } else if ( newrdn != NULL ) {
1198 rc = dorename( dn, newrdn, newsup, deleteoldrdn, pctrls );
1200 rc = domodify( dn, pmods, pctrls, new_entry );
1203 if ( rc == LDAP_SUCCESS ) {
1211 if ( newrdn != NULL ) {
1214 if ( pmods != NULL ) {
1215 ldap_mods_free( pmods, 1 );
1218 if (pctrls != NULL) {
1219 ldap_controls_free( pctrls );
1225 /* Parse an LDIF control line of the form
1226 control: oid [true/false] [: value] or
1227 control: oid [true/false] [:: base64-value] or
1228 control: oid [true/false] [:< url]
1229 The control is added to the list of controls in *ppctrls.
1232 parse_ldif_control( char *line,
1233 LDAPControl ***ppctrls )
1236 int criticality = 0; /* Default is false if not present */
1239 ber_len_t value_len = 0;
1241 char *s, *oidStart, *pcolon;
1242 LDAPControl *newctrl = NULL;
1243 LDAPControl **pctrls = NULL;
1248 s = line + strlen(T_CONTROL_STR); /* Skip over "control" */
1249 pcolon = s; /* Save this position for later */
1250 if (*s++ != ':') /* Make sure colon follows */
1251 return ( LDAP_PARAM_ERROR );
1252 while (*s && isspace(*s)) s++; /* Skip white space before OID */
1254 /* OID should come next. Validate and extract it. */
1256 return ( LDAP_PARAM_ERROR );
1258 while (isdigit(*s) || *s == '.') s++; /* OID should be digits or . */
1260 return ( LDAP_PARAM_ERROR ); /* OID was not present */
1261 if (*s) { /* End of OID should be space or NULL */
1263 return ( LDAP_PARAM_ERROR ); /* else OID contained invalid chars */
1264 *s++ = 0; /* Replace space with null to terminate */
1268 oid = ber_strdup(oidStart);
1270 return ( LDAP_NO_MEMORY );
1272 /* Optional Criticality field is next. */
1273 while (*s && isspace(*s)) s++; /* Skip white space before criticality */
1274 if (strncasecmp(s, "true", 4) == 0) {
1278 else if (strncasecmp(s, "false", 5) == 0) {
1283 /* Optional value field is next */
1284 while (*s && isspace(*s)) s++; /* Skip white space before value */
1286 if (*s != ':') { /* If value is present, must start with : */
1287 rc = LDAP_PARAM_ERROR;
1291 /* Shift value down over OID and criticality so it's in the form
1293 control:: base64-value
1295 Then we can use ldif_parse_line to extract and decode the value
1297 while ( (*pcolon++ = *s++) != 0) /* Shift value */
1299 rc = ldif_parse_line(line, &type, &val, &value_len);
1300 if (type) ber_memfree(type); /* Don't need this field*/
1302 rc = LDAP_PARAM_ERROR;
1307 /* Create a new LDAPControl structure. */
1308 newctrl = (LDAPControl *)ber_memalloc(sizeof(LDAPControl));
1309 if ( newctrl == NULL ) {
1310 rc = LDAP_NO_MEMORY;
1313 newctrl->ldctl_oid = oid;
1315 newctrl->ldctl_iscritical = criticality;
1316 newctrl->ldctl_value.bv_len = value_len;
1317 newctrl->ldctl_value.bv_val = val;
1320 /* Add the new control to the passed-in list of controls. */
1323 while ( pctrls[i] ) /* Count the # of controls passed in */
1326 /* Allocate 1 more slot for the new control and 1 for the NULL. */
1327 pctrls = (LDAPControl **)ber_memrealloc(pctrls, (i+2)*(sizeof(LDAPControl *)));
1328 if (pctrls == NULL) {
1329 rc = LDAP_NO_MEMORY;
1332 pctrls[i] = newctrl;
1339 if (newctrl->ldctl_oid)
1340 ber_memfree(newctrl->ldctl_oid);
1341 if (newctrl->ldctl_value.bv_val)
1342 ber_memfree(newctrl->ldctl_value.bv_val);
1343 ber_memfree(newctrl);
1359 struct berval *val )
1365 modop |= LDAP_MOD_BVALUES;
1368 if ( pmods != NULL ) {
1369 for ( ; pmods[ i ] != NULL; ++i ) {
1370 if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
1371 pmods[ i ]->mod_op == modop )
1378 if ( pmods == NULL || pmods[ i ] == NULL ) {
1379 if (( pmods = (LDAPMod **)ber_memrealloc( pmods, (i + 2) *
1380 sizeof( LDAPMod * ))) == NULL )
1382 perror( "realloc" );
1383 exit( EXIT_FAILURE );
1387 pmods[ i + 1 ] = NULL;
1389 pmods[ i ] = (LDAPMod *)ber_memcalloc( 1, sizeof( LDAPMod ));
1390 if ( pmods[ i ] == NULL ) {
1392 exit( EXIT_FAILURE );
1395 pmods[ i ]->mod_op = modop;
1396 pmods[ i ]->mod_type = ber_strdup( attr );
1397 if ( pmods[ i ]->mod_type == NULL ) {
1399 exit( EXIT_FAILURE );
1403 if ( val != NULL ) {
1405 if ( pmods[ i ]->mod_bvalues != NULL ) {
1406 for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
1411 pmods[ i ]->mod_bvalues = (struct berval **) ber_memrealloc(
1412 pmods[ i ]->mod_bvalues, (j + 2) * sizeof( struct berval * ));
1413 if ( pmods[ i ]->mod_bvalues == NULL ) {
1414 perror( "ber_realloc" );
1415 exit( EXIT_FAILURE );
1418 pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
1419 pmods[ i ]->mod_bvalues[ j ] = ber_bvdup( val );
1420 if ( pmods[ i ]->mod_bvalues[ j ] == NULL ) {
1421 perror( "ber_bvdup" );
1422 exit( EXIT_FAILURE );
1432 LDAPControl **pctrls,
1435 int i, j, k, notascii, op;
1438 if ( pmods == NULL ) {
1439 fprintf( stderr, "%s: no attributes to change or add (entry=\"%s\")\n",
1441 return( LDAP_PARAM_ERROR );
1444 for ( i = 0; pmods[ i ] != NULL; ++i ) {
1445 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1446 if( op == LDAP_MOD_ADD && ( pmods[i]->mod_bvalues == NULL )) {
1448 "%s: attribute \"%s\" has no values (entry=\"%s\")\n",
1449 prog, pmods[i]->mod_type, dn );
1450 return LDAP_PARAM_ERROR;
1455 for ( i = 0; pmods[ i ] != NULL; ++i ) {
1456 op = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
1457 printf( "%s %s:\n", op == LDAP_MOD_REPLACE ?
1458 "replace" : op == LDAP_MOD_ADD ?
1459 "add" : "delete", pmods[ i ]->mod_type );
1460 if ( pmods[ i ]->mod_bvalues != NULL ) {
1461 for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
1462 bvp = pmods[ i ]->mod_bvalues[ j ];
1464 for ( k = 0; (unsigned long) k < bvp->bv_len; ++k ) {
1465 if ( !isascii( bvp->bv_val[ k ] )) {
1471 printf( "\tNOT ASCII (%ld bytes)\n", bvp->bv_len );
1473 printf( "\t%s\n", bvp->bv_val );
1481 printf( "%sadding new entry \"%s\"\n", not ? "!" : "", dn );
1483 printf( "%smodifying entry \"%s\"\n", not ? "!" : "", dn );
1488 i = ldap_add_ext_s( ld, dn, pmods, pctrls, NULL );
1490 i = ldap_modify_ext_s( ld, dn, pmods, pctrls, NULL );
1492 if ( i != LDAP_SUCCESS ) {
1493 /* print error message about failed update including DN */
1494 fprintf( stderr, "%s: update failed: %s\n", prog, dn );
1495 ldap_perror( ld, newentry ? "ldap_add" : "ldap_modify" );
1496 } else if ( verbose ) {
1497 printf( "modify complete\n" );
1512 LDAPControl **pctrls )
1516 printf( "%sdeleting entry \"%s\"\n", not ? "!" : "", dn );
1518 if (( rc = ldap_delete_ext_s( ld, dn, pctrls, NULL )) != LDAP_SUCCESS ) {
1519 fprintf( stderr, "%s: delete failed: %s\n", prog, dn );
1520 ldap_perror( ld, "ldap_delete" );
1521 } else if ( verbose ) {
1522 printf( "delete complete" );
1540 LDAPControl **pctrls )
1545 printf( "%smodifying rdn of entry \"%s\"\n", not ? "!" : "", dn );
1547 printf( "\tnew RDN: \"%s\" (%skeep existing values)\n",
1548 newrdn, deleteoldrdn ? "do not " : "" );
1551 if (( rc = ldap_rename_s( ld, dn, newrdn, newsup, deleteoldrdn, pctrls, NULL ))
1553 fprintf( stderr, "%s: rename failed: %s\n", prog, dn );
1554 ldap_perror( ld, "ldap_modrdn" );
1556 printf( "modrdn completed\n" );
1569 read_one_record( FILE *fp )
1571 char *buf, line[ LDAPMOD_MAXLINE ];
1577 while ( fgets( line, sizeof(line), fp ) != NULL ) {
1578 int len = strlen( line );
1580 if( len < 2 || ( len == 2 && *line == '\r' )) {
1588 if ( lcur + len + 1 > lmax ) {
1589 lmax = LDAPMOD_MAXLINE
1590 * (( lcur + len + 1 ) / LDAPMOD_MAXLINE + 1 );
1592 if (( buf = (char *)ber_memrealloc( buf, lmax )) == NULL ) {
1593 perror( "realloc" );
1594 exit( EXIT_FAILURE );
1598 strcpy( buf + lcur, line );