Ejemplo n.º 1
0
int
ldap_back_map_attrs(
		struct ldapmap *at_map,
		AttributeName *an,
		int remap,
		char ***mapped_attrs,
		void *memctx )
{
	int i, j;
	char **na;
	struct berval mapped;

	if ( an == NULL ) {
		*mapped_attrs = NULL;
		return LDAP_SUCCESS;
	}

	for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ )
		/*  */ ;

	na = (char **)ber_memcalloc_x( i + 1, sizeof(char *), memctx );
	if ( na == NULL ) {
		*mapped_attrs = NULL;
		return LDAP_NO_MEMORY;
	}

	for ( i = j = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
		ldap_back_map( at_map, &an[i].an_name, &mapped, remap );
		if ( !BER_BVISNULL( &mapped ) && !BER_BVISEMPTY( &mapped ) ) {
			na[j++] = mapped.bv_val;
		}
	}
	if ( j == 0 && i != 0 ) {
		na[j++] = LDAP_NO_ATTRS;
	}
	na[j] = NULL;

	*mapped_attrs = na;
	return LDAP_SUCCESS;
}
Ejemplo n.º 2
0
RETCODE
backsql_BindRowAsStrings_x( SQLHSTMT sth, BACKSQL_ROW_NTS *row, void *ctx )
{
	RETCODE		rc;

	if ( row == NULL ) {
		return SQL_ERROR;
	}

#ifdef BACKSQL_TRACE
	Debug( LDAP_DEBUG_TRACE, "==> backsql_BindRowAsStrings()\n", 0, 0, 0 );
#endif /* BACKSQL_TRACE */
	
	rc = SQLNumResultCols( sth, &row->ncols );
	if ( rc != SQL_SUCCESS ) {
#ifdef BACKSQL_TRACE
		Debug( LDAP_DEBUG_TRACE, "backsql_BindRowAsStrings(): "
			"SQLNumResultCols() failed:\n", 0, 0, 0 );
#endif /* BACKSQL_TRACE */
		
		backsql_PrintErrors( SQL_NULL_HENV, SQL_NULL_HDBC, sth, rc );

	} else {
		SQLCHAR		colname[ 64 ];
		SQLSMALLINT	name_len, col_type, col_scale, col_null;
		UDWORD		col_prec;
		int		i;

#ifdef BACKSQL_TRACE
		Debug( LDAP_DEBUG_TRACE, "backsql_BindRowAsStrings: "
			"ncols=%d\n", (int)row->ncols, 0, 0 );
#endif /* BACKSQL_TRACE */

		row->col_names = (BerVarray)ber_memcalloc_x( row->ncols + 1, 
				sizeof( struct berval ), ctx );
		if ( row->col_names == NULL ) {
			goto nomem;
		}

		row->col_prec = (UDWORD *)ber_memcalloc_x( row->ncols,
				sizeof( UDWORD ), ctx );
		if ( row->col_prec == NULL ) {
			goto nomem;
		}

		row->col_type = (SQLSMALLINT *)ber_memcalloc_x( row->ncols,
				sizeof( SQLSMALLINT ), ctx );
		if ( row->col_type == NULL ) {
			goto nomem;
		}

		row->cols = (char **)ber_memcalloc_x( row->ncols + 1, 
				sizeof( char * ), ctx );
		if ( row->cols == NULL ) {
			goto nomem;
		}

		row->value_len = (SQLINTEGER *)ber_memcalloc_x( row->ncols,
				sizeof( SQLINTEGER ), ctx );
		if ( row->value_len == NULL ) {
			goto nomem;
		}

		if ( 0 ) {
nomem:
			ber_memfree_x( row->col_names, ctx );
			row->col_names = NULL;
			ber_memfree_x( row->col_prec, ctx );
			row->col_prec = NULL;
			ber_memfree_x( row->col_type, ctx );
			row->col_type = NULL;
			ber_memfree_x( row->cols, ctx );
			row->cols = NULL;
			ber_memfree_x( row->value_len, ctx );
			row->value_len = NULL;

			Debug( LDAP_DEBUG_ANY, "backsql_BindRowAsStrings: "
				"out of memory\n", 0, 0, 0 );

			return LDAP_NO_MEMORY;
		}

		for ( i = 0; i < row->ncols; i++ ) {
			SQLSMALLINT	TargetType;

			rc = SQLDescribeCol( sth, (SQLSMALLINT)(i + 1), &colname[ 0 ],
					(SQLUINTEGER)( sizeof( colname ) - 1 ),
					&name_len, &col_type,
					&col_prec, &col_scale, &col_null );
			/* FIXME: test rc? */

			ber_str2bv_x( (char *)colname, 0, 1,
					&row->col_names[ i ], ctx );
#ifdef BACKSQL_TRACE
			Debug( LDAP_DEBUG_TRACE, "backsql_BindRowAsStrings: "
				"col_name=%s, col_prec[%d]=%d\n",
				colname, (int)(i + 1), (int)col_prec );
#endif /* BACKSQL_TRACE */
			if ( col_type != SQL_CHAR && col_type != SQL_VARCHAR )
			{
				col_prec = MAX_ATTR_LEN;
			}

			row->cols[ i ] = (char *)ber_memcalloc_x( col_prec + 1,
					sizeof( char ), ctx );
			row->col_prec[ i ] = col_prec;
			row->col_type[ i ] = col_type;

			/*
			 * ITS#3386, ITS#3113 - 20070308
			 * Note: there are many differences between various DPMS and ODBC
			 * Systems; some support SQL_C_BLOB, SQL_C_BLOB_LOCATOR.  YMMV:
			 * This has only been tested on Linux/MySQL/UnixODBC
			 * For BINARY-type Fields (BLOB, etc), read the data as BINARY
			 */
			if ( BACKSQL_IS_BINARY( col_type ) ) {
#ifdef BACKSQL_TRACE
				Debug( LDAP_DEBUG_TRACE, "backsql_BindRowAsStrings: "
					"col_name=%s, col_type[%d]=%d: reading binary data\n",
					colname, (int)(i + 1), (int)col_type);
#endif /* BACKSQL_TRACE */
				TargetType = SQL_C_BINARY;

			} else {
				/* Otherwise read it as Character data */
#ifdef BACKSQL_TRACE
				Debug( LDAP_DEBUG_TRACE, "backsql_BindRowAsStrings: "
					"col_name=%s, col_type[%d]=%d: reading character data\n",
					colname, (int)(i + 1), (int)col_type);
#endif /* BACKSQL_TRACE */
				TargetType = SQL_C_CHAR;
			}

			rc = SQLBindCol( sth, (SQLUSMALLINT)(i + 1),
				 TargetType,
				 (SQLPOINTER)row->cols[ i ],
				 col_prec + 1,
				 &row->value_len[ i ] );

			/* FIXME: test rc? */
		}

		BER_BVZERO( &row->col_names[ i ] );
		row->cols[ i ] = NULL;
	}

#ifdef BACKSQL_TRACE
	Debug( LDAP_DEBUG_TRACE, "<== backsql_BindRowAsStrings()\n", 0, 0, 0 );
#endif /* BACKSQL_TRACE */

	return rc;
}
Ejemplo n.º 3
0
int
ldap_back_map_attrs(
		Operation *op,
		struct ldapmap *at_map,
		AttributeName *an,
		int remap,
		char ***mapped_attrs )
{
	int i, x, j;
	char **na;
	struct berval mapped;

	if ( an == NULL && op->o_bd->be_extra_anlist == NULL ) {
		*mapped_attrs = NULL;
		return LDAP_SUCCESS;
	}

	i = 0;
	if ( an != NULL ) {
		for ( ; !BER_BVISNULL( &an[i].an_name ); i++ )
			/*  */ ;
	}

	x = 0;
	if ( op->o_bd->be_extra_anlist != NULL ) {
		for ( ; !BER_BVISNULL( &op->o_bd->be_extra_anlist[x].an_name ); x++ )
			/*  */ ;
	}

	assert( i > 0 || x > 0 );
	
	na = (char **)ber_memcalloc_x( i + x + 1, sizeof(char *), op->o_tmpmemctx );
	if ( na == NULL ) {
		*mapped_attrs = NULL;
		return LDAP_NO_MEMORY;
	}

	j = 0;
	if ( i > 0 ) {
		for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) {
			ldap_back_map( at_map, &an[i].an_name, &mapped, remap );
			if ( !BER_BVISNULL( &mapped ) && !BER_BVISEMPTY( &mapped ) ) {
				na[j++] = mapped.bv_val;
			}
		}
	}

	if ( x > 0 ) {
		for ( x = 0; !BER_BVISNULL( &op->o_bd->be_extra_anlist[x].an_name ); x++ ) {
			if ( op->o_bd->be_extra_anlist[x].an_desc &&
				ad_inlist( op->o_bd->be_extra_anlist[x].an_desc, an ) )
			{
				continue;
			}

			ldap_back_map( at_map, &op->o_bd->be_extra_anlist[x].an_name, &mapped, remap );
			if ( !BER_BVISNULL( &mapped ) && !BER_BVISEMPTY( &mapped ) ) {
				na[j++] = mapped.bv_val;
			}
		}
	}

	if ( j == 0 && ( i > 0 || x > 0 ) ) {
		na[j++] = LDAP_NO_ATTRS;
	}
	na[j] = NULL;

	*mapped_attrs = na;

	return LDAP_SUCCESS;
}
Ejemplo n.º 4
0
/*
 * ldap_parse_ldif_record_x() will convert an LDIF record read with ldif_read_record()
 * into an array of LDAPMod* and an array of LDAPControl*, suitable for passing
 * directly to any other LDAP API function that takes LDAPMod** and LDAPControl**
 * arguments, such as ldap_modify_s().
 *
 * rbuf - the ldif record buffer returned from ldif_read_record - rbuf.bv_val must be
 *        writable - will use ldif_getline to read from it
 * linenum - the ldif line number returned from ldif_read_record
 *         - used for logging errors (e.g. error at line N)
 * lr - holds the data to return
 * errstr - a string used for logging (usually the program name e.g. "ldapmodify"
 * flags - 0 or some combination of LDIF_DEFAULT_ADD LDIF_ENTRIES_ONLY LDIF_NO_CONTROLS
 * ctx is the memory allocation context - if NULL, use the standard memory allocator
 */
int
ldap_parse_ldif_record_x(
	struct berval *rbuf,
	unsigned long linenum,
	LDIFRecord *lr,
	const char *errstr,
	unsigned int flags,
	void *ctx )
{
	char	*line, *dn;
	int		rc, modop;
	int		expect_modop, expect_sep;
	int		ldapadd, new_entry, delete_entry, got_all;
	LDAPMod	**pmods;
	int version;
	LDAPControl **pctrls;
	int i, j, k, idn, nmods;
	struct berval **bvl, bv;

	assert( lr != NULL );
	assert( rbuf != NULL );
	memset( lr, 0, sizeof(LDIFRecord) );
	lr->lr_ctx = ctx; /* save memory context for later */
	ldapadd = flags & LDIF_DEFAULT_ADD;
	new_entry = ldapadd;

	rc = got_all = delete_entry = modop = expect_modop = 0;
	expect_sep = 0;
	version = 0;
	pmods = NULL;
	pctrls = NULL;
	dn = NULL;

	lr->lr_lines = ldif_countlines( rbuf->bv_val );
	lr->lr_btype = ber_memcalloc_x( 1, (lr->lr_lines+1)*2*sizeof(struct berval)+lr->lr_lines, ctx );
	if ( !lr->lr_btype )
		return LDAP_NO_MEMORY;

	lr->lr_vals = lr->lr_btype+lr->lr_lines+1;
	lr->lr_freeval = (char *)(lr->lr_vals+lr->lr_lines+1);
	i = -1;

	while ( rc == 0 && ( line = ldif_getline( &rbuf->bv_val )) != NULL ) {
		int freev;

		if ( *line == '\n' || *line == '\0' ) {
			break;
		}

		++i;

		if ( line[0] == '-' && !line[1] ) {
			BER_BVZERO( lr->lr_btype+i );
			lr->lr_freeval[i] = 0;
			continue;
		}
	
		if ( ( rc = ldif_parse_line2( line, lr->lr_btype+i, lr->lr_vals+i, &freev ) ) < 0 ) {
			fprintf( stderr, _("%s: invalid format (line %lu) entry: \"%s\"\n"),
				errstr, linenum+i, dn == NULL ? "" : dn );
			rc = LDAP_PARAM_ERROR;
			goto leave;
		}
		lr->lr_freeval[i] = freev;

		if ( dn == NULL ) {
			if ( linenum+i == 1 && BV_CASEMATCH( lr->lr_btype+i, &BV_VERSION )) {
				/* lutil_atoi() introduces a dependence of libldap
				 * on liblutil; we only allow version 1 by now (ITS#6654)
				 */
#if 0
				int	v;
				if( lr->lr_vals[i].bv_len == 0 || lutil_atoi( &v, lr->lr_vals[i].bv_val) != 0 || v != 1 )
#endif
				static const struct berval version1 = { 1, "1" };
				if ( lr->lr_vals[i].bv_len != version1.bv_len || strncmp( lr->lr_vals[i].bv_val, version1.bv_val, version1.bv_len ) != 0 )
				{
					fprintf( stderr,
						_("%s: invalid version %s, line %lu (ignored)\n"),
						errstr, lr->lr_vals[i].bv_val, linenum );
				}
				version++;

			} else if ( BV_CASEMATCH( lr->lr_btype+i, &BV_DN )) {
				lr->lr_dn = lr->lr_vals[i];
				dn = lr->lr_dn.bv_val; /* primarily for logging */
				idn = i;
			}
			/* skip all lines until we see "dn:" */
		}
	}

	/* check to make sure there was a dn: line */
	if ( !dn ) {
		rc = 0;
		goto leave;
	}

	lr->lr_lines = i+1;

	if( lr->lr_lines == 0 ) {
		rc = 0;
		goto leave;
	}

	if( version && lr->lr_lines == 1 ) {
		rc = 0;
		goto leave;
	}

	i = idn+1;
	/* Check for "control" tag after dn and before changetype. */
	if ( BV_CASEMATCH( lr->lr_btype+i, &BV_CONTROL )) {
		/* Parse and add it to the list of controls */
		if ( !( flags & LDIF_NO_CONTROLS ) ) {
			rc = parse_ldif_control( lr->lr_vals+i, &pctrls );
			if (rc != 0) {
				fprintf( stderr,
						 _("%s: Error processing %s line, line %lu: %s\n"),
						 errstr, BV_CONTROL.bv_val, linenum+i, ldap_err2string(rc) );
			}
		}
		i++;
		if ( i>= lr->lr_lines ) {
short_input:
			fprintf( stderr,
				_("%s: Expecting more input after %s line, line %lu\n"),
				errstr, lr->lr_btype[i-1].bv_val, linenum+i );
			
			rc = LDAP_PARAM_ERROR;
			goto leave;
		}
	}

	/* Check for changetype */
	if ( BV_CASEMATCH( lr->lr_btype+i, &BV_CHANGETYPE )) {
#ifdef LIBERAL_CHANGETYPE_MODOP
		/* trim trailing spaces (and log warning ...) */
		int icnt;
		for ( icnt = lr->lr_vals[i].bv_len; --icnt > 0; ) {
			if ( !isspace( (unsigned char) lr->lr_vals[i].bv_val[icnt] ) ) {
				break;
			}
		}

		if ( ++icnt != lr->lr_vals[i].bv_len ) {
			fprintf( stderr, _("%s: illegal trailing space after"
				" \"%s: %s\" trimmed (line %lu, entry \"%s\")\n"),
				errstr, BV_CHANGETYPE.bv_val, lr->lr_vals[i].bv_val, linenum+i, dn );
			lr->lr_vals[i].bv_val[icnt] = '\0';
		}
#endif /* LIBERAL_CHANGETYPE_MODOP */

		/* if LDIF_ENTRIES_ONLY, then either the changetype must be add, or
		   there must be no changetype, and the flag LDIF_DEFAULT_ADD must be set */
		if ( flags & LDIF_ENTRIES_ONLY ) {
			if ( !( BV_CASEMATCH( lr->lr_vals+i, &BV_ADDCT )) ) {
				ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
									_("%s: skipping LDIF record beginning at line %lu: "
									  "changetype '%.*s' found but entries only was requested\n"),
									errstr, linenum,
									(int)lr->lr_vals[i].bv_len,
									(const char *)lr->lr_vals[i].bv_val );
				goto leave;
			}
		}

		if ( BV_CASEMATCH( lr->lr_vals+i, &BV_MODIFYCT )) {
			new_entry = 0;
			expect_modop = 1;
		} else if ( BV_CASEMATCH( lr->lr_vals+i, &BV_ADDCT )) {
			new_entry = 1;
			modop = LDAP_MOD_ADD;
		} else if ( BV_CASEMATCH( lr->lr_vals+i, &BV_MODRDNCT )
			|| BV_CASEMATCH( lr->lr_vals+i, &BV_MODDNCT )
			|| BV_CASEMATCH( lr->lr_vals+i, &BV_RENAMECT ))
		{
			i++;
			if ( i >= lr->lr_lines )
				goto short_input;
			if ( !BV_CASEMATCH( lr->lr_btype+i, &BV_NEWRDN )) {
				fprintf( stderr, _("%s: expecting \"%s:\" but saw"
					" \"%s:\" (line %lu, entry \"%s\")\n"),
					errstr, BV_NEWRDN.bv_val, lr->lr_btype[i].bv_val, linenum+i, dn );
				rc = LDAP_PARAM_ERROR;
				goto leave;
			}
			lr->lrop_newrdn = lr->lr_vals[i];
			i++;
			if ( i >= lr->lr_lines )
				goto short_input;
			if ( !BV_CASEMATCH( lr->lr_btype+i, &BV_DELETEOLDRDN )) {
				fprintf( stderr, _("%s: expecting \"%s:\" but saw"
					" \"%s:\" (line %lu, entry \"%s\")\n"),
					errstr, BV_DELETEOLDRDN.bv_val, lr->lr_btype[i].bv_val, linenum+i, dn );
				rc = LDAP_PARAM_ERROR;
				goto leave;
			}
			lr->lrop_delold = ( lr->lr_vals[i].bv_val[0] == '0' ) ? 0 : 1;
			i++;
			if ( i < lr->lr_lines ) {
				if ( !BV_CASEMATCH( lr->lr_btype+i, &BV_NEWSUP )) {
					fprintf( stderr, _("%s: expecting \"%s:\" but saw"
						" \"%s:\" (line %lu, entry \"%s\")\n"),
						errstr, BV_NEWSUP.bv_val, lr->lr_btype[i].bv_val, linenum+i, dn );
					rc = LDAP_PARAM_ERROR;
					goto leave;
				}
				lr->lrop_newsup = lr->lr_vals[i];
				i++;
			}
			got_all = 1;
		} else if ( BV_CASEMATCH( lr->lr_vals+i, &BV_DELETECT )) {
			got_all = delete_entry = 1;
		} else {
			fprintf( stderr,
				_("%s:  unknown %s \"%s\" (line %lu, entry \"%s\")\n"),
				errstr, BV_CHANGETYPE.bv_val, lr->lr_vals[i].bv_val, linenum+i, dn );
			rc = LDAP_PARAM_ERROR;
			goto leave;
		}
		i++;
	} else if ( ldapadd ) {		/*  missing changetype => add */
		new_entry = 1;
		modop = LDAP_MOD_ADD;
	} else {
		/* if LDIF_ENTRIES_ONLY, then either the changetype must be add, or
		   there must be no changetype, and the flag LDIF_DEFAULT_ADD must be set */
		if ( flags & LDIF_ENTRIES_ONLY ) {
			ber_pvt_log_printf( LDAP_DEBUG_ANY, ldif_debug,
								_("%s: skipping LDIF record beginning at line %lu: "
								  "no changetype found but entries only was requested and "
								  "the default setting for missing changetype is modify\n"),
								errstr, linenum );
			goto leave;
		}
		expect_modop = 1;	/* missing changetype => modify */
	}

	if ( got_all ) {
		if ( i < lr->lr_lines ) {
			fprintf( stderr,
				_("%s: extra lines at end (line %lu, entry \"%s\")\n"),
				errstr, linenum+i, dn );
			rc = LDAP_PARAM_ERROR;
			goto leave;
		}
		goto doit;
	}

	nmods = lr->lr_lines - i;
	idn = i;

	if ( new_entry ) {
		int fv;

		/* Make sure all attributes with multiple values are contiguous */
		for (; i<lr->lr_lines; i++) {
			for (j=i+1; j<lr->lr_lines; j++) {
				if ( !lr->lr_btype[j].bv_val ) {
					fprintf( stderr,
						_("%s: missing attributeDescription (line %lu, entry \"%s\")\n"),
						errstr, linenum+j, dn );
					rc = LDAP_PARAM_ERROR;
					goto leave;
				}
				if ( BV_CASEMATCH( lr->lr_btype+i, lr->lr_btype+j )) {
					nmods--;
					/* out of order, move intervening attributes down */
					if ( j != i+1 ) {
						bv = lr->lr_vals[j];
						fv = lr->lr_freeval[j];
						for (k=j; k>i; k--) {
							lr->lr_btype[k] = lr->lr_btype[k-1];
							lr->lr_vals[k] = lr->lr_vals[k-1];
							lr->lr_freeval[k] = lr->lr_freeval[k-1];
						}
						k++;
						lr->lr_btype[k] = lr->lr_btype[i];
						lr->lr_vals[k] = bv;
						lr->lr_freeval[k] = fv;
					}
					i++;
				}
			}
		}
		/* Allocate space for array of mods, array of pointers to mods,
		 * and array of pointers to values, allowing for NULL terminators
		 * for the pointer arrays...
		 */
		lr->lr_lm = ber_memalloc_x( nmods * sizeof(LDAPMod) +
			(nmods+1) * sizeof(LDAPMod*) +
			(lr->lr_lines + nmods - idn) * sizeof(struct berval *), ctx );
		pmods = (LDAPMod **)(lr->lr_lm+nmods);
		bvl = (struct berval **)(pmods+nmods+1);

		j = 0;
		k = -1;
		BER_BVZERO(&bv);
		for (i=idn; i<lr->lr_lines; i++) {
			if ( BV_CASEMATCH( lr->lr_btype+i, &BV_DN )) {
				fprintf( stderr, _("%s: attributeDescription \"%s\":"
					" (possible missing newline"
						" after line %lu, entry \"%s\"?)\n"),
					errstr, lr->lr_btype[i].bv_val, linenum+i - 1, dn );
			}
			if ( !BV_CASEMATCH( lr->lr_btype+i, &bv )) {
				bvl[k++] = NULL;
				bv = lr->lr_btype[i];
				lr->lr_lm[j].mod_op = LDAP_MOD_ADD | LDAP_MOD_BVALUES;
				lr->lr_lm[j].mod_type = bv.bv_val;
				lr->lr_lm[j].mod_bvalues = bvl+k;
				pmods[j] = lr->lr_lm+j;
				j++;
			}
			bvl[k++] = lr->lr_vals+i;
		}
		bvl[k] = NULL;
		pmods[j] = NULL;
		goto doit;
	}

	lr->lr_mops = ber_memalloc_x( lr->lr_lines+1, ctx );
	lr->lr_mops[lr->lr_lines] = M_SEP;
	lr->lr_mops[i-1] = M_SEP;

	for ( ; i<lr->lr_lines; i++ ) {
		if ( expect_modop ) {
#ifdef LIBERAL_CHANGETYPE_MODOP
			/* trim trailing spaces (and log warning ...) */
		    int icnt;
		    for ( icnt = lr->lr_vals[i].bv_len; --icnt > 0; ) {
				if ( !isspace( (unsigned char) lr->lr_vals[i].bv_val[icnt] ) ) break;
			}
    
			if ( ++icnt != lr->lr_vals[i].bv_len ) {
				fprintf( stderr, _("%s: illegal trailing space after"
					" \"%s: %s\" trimmed (line %lu, entry \"%s\")\n"),
					errstr, type, lr->lr_vals[i].bv_val, linenum+i, dn );
				lr->lr_vals[i].bv_val[icnt] = '\0';
			}
#endif /* LIBERAL_CHANGETYPE_MODOP */

			expect_modop = 0;
			expect_sep = 1;
			if ( BV_CASEMATCH( lr->lr_btype+i, &BV_MODOPADD )) {
				modop = LDAP_MOD_ADD;
				lr->lr_mops[i] = M_SEP;
				nmods--;
			} else if ( BV_CASEMATCH( lr->lr_btype+i, &BV_MODOPREPLACE )) {
			/* defer handling these since they might have no values.
			 * Use the BVALUES flag to signal that these were
			 * deferred. If values are provided later, this
			 * flag will be switched off.
			 */
				modop = LDAP_MOD_REPLACE;
				lr->lr_mops[i] = modop | LDAP_MOD_BVALUES;
				lr->lr_btype[i] = lr->lr_vals[i];
			} else if ( BV_CASEMATCH( lr->lr_btype+i, &BV_MODOPDELETE )) {
				modop = LDAP_MOD_DELETE;
				lr->lr_mops[i] = modop | LDAP_MOD_BVALUES;
				lr->lr_btype[i] = lr->lr_vals[i];
			} else if ( BV_CASEMATCH( lr->lr_btype+i, &BV_MODOPINCREMENT )) {
				modop = LDAP_MOD_INCREMENT;
				lr->lr_mops[i] = M_SEP;
				nmods--;
			} else {	/* no modify op: invalid LDIF */
				fprintf( stderr, _("%s: modify operation type is missing at"
					" line %lu, entry \"%s\"\n"),
					errstr, linenum+i, dn );
				rc = LDAP_PARAM_ERROR;
				goto leave;
			}
			bv = lr->lr_vals[i];
		} else if ( expect_sep && BER_BVISEMPTY( lr->lr_btype+i )) {
			lr->lr_mops[i] = M_SEP;
			expect_sep = 0;
			expect_modop = 1;
			nmods--;
		} else {
			if ( !BV_CASEMATCH( lr->lr_btype+i, &bv )) {
				fprintf( stderr, _("%s: wrong attributeType at"
					" line %lu, entry \"%s\"\n"),
					errstr, linenum+i, dn );
				rc = LDAP_PARAM_ERROR;
				goto leave;
			}
			lr->lr_mops[i] = modop;
			/* If prev op was deferred and matches this type,
			 * clear the flag
			 */
			if ( (lr->lr_mops[i-1] & LDAP_MOD_BVALUES)
				&& BV_CASEMATCH( lr->lr_btype+i, lr->lr_btype+i-1 ))
			{
				lr->lr_mops[i-1] = M_SEP;
				nmods--;
			}
		}
	}

	/* Allocate space for array of mods, array of pointers to mods,
	 * and array of pointers to values, allowing for NULL terminators
	 * for the pointer arrays...
	 */
	lr->lr_lm = ber_memalloc_x( nmods * sizeof(LDAPMod) +
		(nmods+1) * sizeof(LDAPMod*) +
		(lr->lr_lines + nmods - idn) * sizeof(struct berval *), ctx );
	pmods = (LDAPMod **)(lr->lr_lm+nmods);
	bvl = (struct berval **)(pmods+nmods+1);

	j = 0;
	k = -1;
	BER_BVZERO(&bv);
	lr->lr_mops[idn-1] = M_SEP;
	for (i=idn; i<lr->lr_lines; i++) {
		if ( lr->lr_mops[i] == M_SEP )
			continue;
		if ( lr->lr_mops[i] != lr->lr_mops[i-1] || !BV_CASEMATCH( lr->lr_btype+i, &bv )) {
			bvl[k++] = NULL;
			bv = lr->lr_btype[i];
			lr->lr_lm[j].mod_op = lr->lr_mops[i] | LDAP_MOD_BVALUES;
			lr->lr_lm[j].mod_type = bv.bv_val;
			if ( lr->lr_mops[i] & LDAP_MOD_BVALUES ) {
				lr->lr_lm[j].mod_bvalues = NULL;
			} else {
				lr->lr_lm[j].mod_bvalues = bvl+k;
			}
			pmods[j] = lr->lr_lm+j;
			j++;
		}
		bvl[k++] = lr->lr_vals+i;
	}
	bvl[k] = NULL;
	pmods[j] = NULL;

doit:
	/* first, set the common fields */
	lr->lr_ctrls = pctrls;
	/* next, set the op */
	if ( delete_entry ) {
		lr->lr_op = LDAP_REQ_DELETE;
	} else if ( lr->lrop_newrdn.bv_val != NULL ) {
		lr->lr_op = LDAP_REQ_MODDN;
	} else {
		/* for now, either add or modify */
		lr->lrop_mods = pmods;
		if ( new_entry ) {
			lr->lr_op = LDAP_REQ_ADD;
		} else {
			lr->lr_op = LDAP_REQ_MODIFY;
		}
	}

leave:
	if ( rc != LDAP_SUCCESS ) {
		ldap_ldif_record_done( lr );
	}

	return( rc );
}