- char *line, *dn, *type, *newrdn, *newsup, *p;
- int rc, linenum, modop, replicaport;
- int expect_modop, expect_sep, expect_ct, expect_newrdn, expect_newsup;
- int expect_deleteoldrdn, deleteoldrdn;
- int saw_replica, use_record, new_entry, delete_entry, got_all;
- LDAPMod **pmods;
- int version;
- struct berval val;
- LDAPControl **pctrls;
-
- new_entry = ldapadd;
-
- rc = got_all = saw_replica = delete_entry = modop = expect_modop = 0;
- expect_deleteoldrdn = expect_newrdn = expect_newsup = 0;
- expect_sep = expect_ct = 0;
- linenum = 0;
- version = 0;
- deleteoldrdn = 1;
- use_record = force;
- pmods = NULL;
- pctrls = NULL;
- dn = newrdn = newsup = NULL;
-
- while ( rc == 0 && ( line = ldif_getline( &rbuf )) != NULL ) {
- ++linenum;
-
- if ( expect_sep && strcasecmp( line, T_MODSEPSTR ) == 0 ) {
- expect_sep = 0;
- expect_ct = 1;
- continue;
- }
-
- if ( ldif_parse_line( line, &type, &val.bv_val, &val.bv_len ) < 0 ) {
- fprintf( stderr, _("%s: invalid format (line %d) entry: \"%s\"\n"),
- prog, linenum, dn == NULL ? "" : dn );
- rc = LDAP_PARAM_ERROR;
- break;
- }
-
- if ( dn == NULL ) {
- if ( !use_record && strcasecmp( type, T_REPLICA_STR ) == 0 ) {
- ++saw_replica;
- if (( p = strchr( val.bv_val, ':' )) == NULL ) {
- replicaport = 0;
- } else {
- *p++ = '\0';
- replicaport = atoi( p );
- }
- if ( ldaphost != NULL &&
- strcasecmp( val.bv_val, ldaphost ) == 0 &&
- replicaport == ldapport )
- {
- use_record = 1;
- }
- } else if ( count == 1 && linenum == 1 &&
- strcasecmp( type, T_VERSION_STR ) == 0 )
- {
- if( val.bv_len == 0 || atoi(val.bv_val) != 1 ) {
- fprintf( stderr,
- _("%s: invalid version %s, line %d (ignored)\n"),
- prog, val.bv_val, linenum );
- }
- version++;
-
- } else if ( strcasecmp( type, T_DN_STR ) == 0 ) {
- if (( dn = ber_strdup( val.bv_val )) == NULL ) {
- perror( "strdup" );
- exit( EXIT_FAILURE );
- }
- expect_ct = 1;
- }
- goto end_line; /* skip all lines until we see "dn:" */
- }
-
- if ( expect_ct ) {
- /* Check for "control" tag after dn and before changetype. */
- if (strcasecmp(type, T_CONTROL_STR) == 0) {
- /* Parse and add it to the list of controls */
- rc = parse_ldif_control( line, &pctrls );
- if (rc != 0) {
- fprintf( stderr,
- _("%s: Error processing %s line, line %d: %s\n"),
- prog, T_CONTROL_STR, linenum, ldap_err2string(rc) );
- }
- goto end_line;
- }
-
- expect_ct = 0;
- if ( !use_record && saw_replica ) {
- printf(_("%s: skipping change record for entry: %s\n"),
- prog, dn);
- printf(_("\t(LDAP host/port does not match replica: lines)\n"));
- free( dn );
- ber_memfree( type );
- ber_memfree( val.bv_val );
- return( 0 );
- }
-
- if ( strcasecmp( type, T_CHANGETYPESTR ) == 0 ) {
-#ifdef LIBERAL_CHANGETYPE_MODOP
- /* trim trailing spaces (and log warning ...) */
- int icnt;
- for ( icnt = val.bv_len; --icnt > 0; ) {
- if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) {
- break;
- }
- }
-
- if ( ++icnt != val.bv_len ) {
- fprintf( stderr, _("%s: illegal trailing space after"
- " \"%s: %s\" trimmed (line %d of entry \"%s\")\n"),
- prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
- val.bv_val[icnt] = '\0';
- }
-#endif /* LIBERAL_CHANGETYPE_MODOP */
-
- if ( strcasecmp( val.bv_val, T_MODIFYCTSTR ) == 0 ) {
- new_entry = 0;
- expect_modop = 1;
- } else if ( strcasecmp( val.bv_val, T_ADDCTSTR ) == 0 ) {
- new_entry = 1;
- } else if ( strcasecmp( val.bv_val, T_MODRDNCTSTR ) == 0
- || strcasecmp( val.bv_val, T_MODDNCTSTR ) == 0
- || strcasecmp( val.bv_val, T_RENAMECTSTR ) == 0)
- {
- expect_newrdn = 1;
- } else if ( strcasecmp( val.bv_val, T_DELETECTSTR ) == 0 ) {
- got_all = delete_entry = 1;
- } else {
- fprintf( stderr,
- _("%s: unknown %s \"%s\" (line %d of entry \"%s\")\n"),
- prog, T_CHANGETYPESTR, val.bv_val, linenum, dn );
- rc = LDAP_PARAM_ERROR;
- }
- goto end_line;
- } else if ( ldapadd ) { /* missing changetype => add */
- new_entry = 1;
- modop = LDAP_MOD_ADD;
- } else {
- expect_modop = 1; /* missing changetype => modify */
- }
- }
-
- if ( expect_modop ) {
-#ifdef LIBERAL_CHANGETYPE_MODOP
- /* trim trailing spaces (and log warning ...) */
- int icnt;
- for ( icnt = val.bv_len; --icnt > 0; ) {
- if ( !isspace( (unsigned char) val.bv_val[icnt] ) ) break;
- }
-
- if ( ++icnt != val.bv_len ) {
- fprintf( stderr, _("%s: illegal trailing space after"
- " \"%s: %s\" trimmed (line %d of entry \"%s\")\n"),
- prog, type, val.bv_val, linenum, dn );
- val.bv_val[icnt] = '\0';
- }
-#endif /* LIBERAL_CHANGETYPE_MODOP */
-
- expect_modop = 0;
- expect_sep = 1;
- if ( strcasecmp( type, T_MODOPADDSTR ) == 0 ) {
- modop = LDAP_MOD_ADD;
- goto end_line;
- } else if ( strcasecmp( type, T_MODOPREPLACESTR ) == 0 ) {
- modop = LDAP_MOD_REPLACE;
- addmodifyop( &pmods, modop, val.bv_val, NULL );
- goto end_line;
- } else if ( strcasecmp( type, T_MODOPDELETESTR ) == 0 ) {
- modop = LDAP_MOD_DELETE;
- addmodifyop( &pmods, modop, val.bv_val, NULL );
- goto end_line;
- } else if ( strcasecmp( type, T_MODOPINCREMENTSTR ) == 0 ) {
- modop = LDAP_MOD_INCREMENT;
- addmodifyop( &pmods, modop, val.bv_val, NULL );
- goto end_line;
- } else { /* no modify op: use default */
- modop = ldapadd ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
- }
- }
-
- if ( expect_newrdn ) {
- if ( strcasecmp( type, T_NEWRDNSTR ) == 0 ) {
- if (( newrdn = ber_strdup( val.bv_val )) == NULL ) {
- perror( "strdup" );
- exit( EXIT_FAILURE );
- }
- expect_deleteoldrdn = 1;
- expect_newrdn = 0;
- } else {
- fprintf( stderr, _("%s: expecting \"%s:\" but saw"
- " \"%s:\" (line %d of entry \"%s\")\n"),
- prog, T_NEWRDNSTR, type, linenum, dn );
- rc = LDAP_PARAM_ERROR;
- }
- } else if ( expect_deleteoldrdn ) {
- if ( strcasecmp( type, T_DELETEOLDRDNSTR ) == 0 ) {
- deleteoldrdn = ( *val.bv_val == '0' ) ? 0 : 1;
- expect_deleteoldrdn = 0;
- expect_newsup = 1;
- got_all = 1;
- } else {
- fprintf( stderr, _("%s: expecting \"%s:\" but saw"
- " \"%s:\" (line %d of entry \"%s\")\n"),
- prog, T_DELETEOLDRDNSTR, type, linenum, dn );
- rc = LDAP_PARAM_ERROR;
- }
- } else if ( expect_newsup ) {
- if ( strcasecmp( type, T_NEWSUPSTR ) == 0 ) {
- if (( newsup = ber_strdup( val.bv_val )) == NULL ) {
- perror( "strdup" );
- exit( EXIT_FAILURE );
- }
- expect_newsup = 0;
- } else {
- fprintf( stderr, _("%s: expecting \"%s:\" but saw"
- " \"%s:\" (line %d of entry \"%s\")\n"),
- prog, T_NEWSUPSTR, type, linenum, dn );
- rc = LDAP_PARAM_ERROR;
- }
- } else if ( got_all ) {
- fprintf( stderr,
- _("%s: extra lines at end (line %d of entry \"%s\")\n"),
- prog, linenum, dn );
- rc = LDAP_PARAM_ERROR;
- } else {
- addmodifyop( &pmods, modop, type, &val );
- }