Home | History | Annotate | Line # | Download | only in back-ldif
ldif.c revision 1.1.1.3
      1  1.1.1.2  lukem /*	$NetBSD: ldif.c,v 1.1.1.3 2010/12/12 15:23:08 adam Exp $	*/
      2  1.1.1.2  lukem 
      3      1.1  lukem /* ldif.c - the ldif backend */
      4  1.1.1.3   adam /* OpenLDAP: pkg/ldap/servers/slapd/back-ldif/ldif.c,v 1.48.2.24 2010/04/19 16:53:03 quanah Exp */
      5      1.1  lukem /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
      6      1.1  lukem  *
      7  1.1.1.3   adam  * Copyright 2005-2010 The OpenLDAP Foundation.
      8      1.1  lukem  * All rights reserved.
      9      1.1  lukem  *
     10      1.1  lukem  * Redistribution and use in source and binary forms, with or without
     11      1.1  lukem  * modification, are permitted only as authorized by the OpenLDAP
     12      1.1  lukem  * Public License.
     13      1.1  lukem  *
     14      1.1  lukem  * A copy of this license is available in the file LICENSE in the
     15      1.1  lukem  * top-level directory of the distribution or, alternatively, at
     16      1.1  lukem  * <http://www.OpenLDAP.org/license.html>.
     17      1.1  lukem  */
     18      1.1  lukem /* ACKNOWLEDGEMENTS:
     19      1.1  lukem  * This work was originally developed by Eric Stokes for inclusion
     20      1.1  lukem  * in OpenLDAP Software.
     21      1.1  lukem  */
     22      1.1  lukem 
     23      1.1  lukem #include "portable.h"
     24      1.1  lukem #include <stdio.h>
     25      1.1  lukem #include <ac/string.h>
     26      1.1  lukem #include <sys/types.h>
     27      1.1  lukem #include <sys/stat.h>
     28      1.1  lukem #include <ac/dirent.h>
     29      1.1  lukem #include <fcntl.h>
     30      1.1  lukem #include <ac/errno.h>
     31      1.1  lukem #include <ac/unistd.h>
     32      1.1  lukem #include "slap.h"
     33      1.1  lukem #include "lutil.h"
     34      1.1  lukem #include "config.h"
     35      1.1  lukem 
     36  1.1.1.2  lukem struct ldif_tool {
     37  1.1.1.2  lukem 	Entry	**entries;			/* collected by bi_tool_entry_first() */
     38  1.1.1.2  lukem 	ID		elen;				/* length of entries[] array */
     39  1.1.1.2  lukem 	ID		ecount;				/* number of entries */
     40  1.1.1.2  lukem 	ID		ecurrent;			/* bi_tool_entry_next() position */
     41  1.1.1.2  lukem #	define	ENTRY_BUFF_INCREMENT 500 /* initial entries[] length */
     42  1.1.1.3   adam 	struct berval	*tl_base;
     43  1.1.1.3   adam 	int		tl_scope;
     44  1.1.1.3   adam 	Filter		*tl_filter;
     45  1.1.1.2  lukem };
     46      1.1  lukem 
     47  1.1.1.2  lukem /* Per-database data */
     48      1.1  lukem struct ldif_info {
     49  1.1.1.2  lukem 	struct berval li_base_path;			/* database directory */
     50  1.1.1.2  lukem 	struct ldif_tool li_tool;			/* for slap tools */
     51  1.1.1.2  lukem 	/*
     52  1.1.1.2  lukem 	 * Read-only LDAP requests readlock li_rdwr for filesystem input.
     53  1.1.1.2  lukem 	 * Update requests first lock li_modop_mutex for filesystem I/O,
     54  1.1.1.2  lukem 	 * and then writelock li_rdwr as well for filesystem output.
     55  1.1.1.2  lukem 	 * This allows update requests to do callbacks that acquire
     56  1.1.1.2  lukem 	 * read locks, e.g. access controls that inspect entries.
     57  1.1.1.2  lukem 	 * (An alternative would be recursive read/write locks.)
     58  1.1.1.2  lukem 	 */
     59  1.1.1.2  lukem 	ldap_pvt_thread_mutex_t	li_modop_mutex; /* serialize update requests */
     60  1.1.1.2  lukem 	ldap_pvt_thread_rdwr_t	li_rdwr;	/* no other I/O when writing */
     61      1.1  lukem };
     62      1.1  lukem 
     63      1.1  lukem #ifdef _WIN32
     64      1.1  lukem #define mkdir(a,b)	mkdir(a)
     65  1.1.1.2  lukem #define move_file(from, to) (!MoveFileEx(from, to, MOVEFILE_REPLACE_EXISTING))
     66  1.1.1.2  lukem #else
     67  1.1.1.2  lukem #define move_file(from, to) rename(from, to)
     68      1.1  lukem #endif
     69  1.1.1.2  lukem #define move_dir(from, to) rename(from, to)
     70      1.1  lukem 
     71      1.1  lukem 
     72      1.1  lukem #define LDIF	".ldif"
     73      1.1  lukem #define LDIF_FILETYPE_SEP	'.'			/* LDIF[0] */
     74      1.1  lukem 
     75      1.1  lukem /*
     76      1.1  lukem  * Unsafe/translated characters in the filesystem.
     77      1.1  lukem  *
     78      1.1  lukem  * LDIF_UNSAFE_CHAR(c) returns true if the character c is not to be used
     79  1.1.1.2  lukem  * in relative filenames, except it should accept '\\', '{' and '}' even
     80  1.1.1.2  lukem  * if unsafe.  The value should be a constant expression.
     81      1.1  lukem  *
     82      1.1  lukem  * If '\\' is unsafe, #define LDIF_ESCAPE_CHAR as a safe character.
     83      1.1  lukem  * If '{' and '}' are unsafe, #define IX_FSL/IX_FSR as safe characters.
     84      1.1  lukem  * (Not digits, '-' or '+'.  IX_FSL == IX_FSR is allowed.)
     85      1.1  lukem  *
     86      1.1  lukem  * Characters are escaped as LDIF_ESCAPE_CHAR followed by two hex digits,
     87      1.1  lukem  * except '\\' is replaced with LDIF_ESCAPE_CHAR and {} with IX_FS[LR].
     88      1.1  lukem  * Also some LDIF special chars are hex-escaped.
     89      1.1  lukem  *
     90      1.1  lukem  * Thus an LDIF filename is a valid normalized RDN (or suffix DN)
     91      1.1  lukem  * followed by ".ldif", except with '\\' replaced with LDIF_ESCAPE_CHAR.
     92      1.1  lukem  */
     93      1.1  lukem 
     94      1.1  lukem #ifndef _WIN32
     95      1.1  lukem 
     96      1.1  lukem /*
     97      1.1  lukem  * Unix/MacOSX version.  ':' vs '/' can cause confusion on MacOSX so we
     98      1.1  lukem  * escape both.  We escape them on Unix so both OS variants get the same
     99      1.1  lukem  * filenames.
    100      1.1  lukem  */
    101      1.1  lukem #define LDIF_ESCAPE_CHAR	'\\'
    102      1.1  lukem #define LDIF_UNSAFE_CHAR(c)	((c) == '/' || (c) == ':')
    103      1.1  lukem 
    104      1.1  lukem #else /* _WIN32 */
    105      1.1  lukem 
    106      1.1  lukem /* Windows version - Microsoft's list of unsafe characters, except '\\' */
    107  1.1.1.2  lukem #define LDIF_ESCAPE_CHAR	'^'			/* Not '\\' (unsafe on Windows) */
    108      1.1  lukem #define LDIF_UNSAFE_CHAR(c)	\
    109      1.1  lukem 	((c) == '/' || (c) == ':' || \
    110      1.1  lukem 	 (c) == '<' || (c) == '>' || (c) == '"' || \
    111      1.1  lukem 	 (c) == '|' || (c) == '?' || (c) == '*')
    112      1.1  lukem 
    113      1.1  lukem #endif /* !_WIN32 */
    114      1.1  lukem 
    115      1.1  lukem /*
    116      1.1  lukem  * Left and Right "{num}" prefix to ordered RDNs ("olcDatabase={1}bdb").
    117      1.1  lukem  * IX_DN* are for LDAP RDNs, IX_FS* for their .ldif filenames.
    118      1.1  lukem  */
    119      1.1  lukem #define IX_DNL	'{'
    120      1.1  lukem #define	IX_DNR	'}'
    121      1.1  lukem #ifndef IX_FSL
    122      1.1  lukem #define	IX_FSL	IX_DNL
    123      1.1  lukem #define IX_FSR	IX_DNR
    124      1.1  lukem #endif
    125      1.1  lukem 
    126      1.1  lukem /*
    127      1.1  lukem  * Test for unsafe chars, as well as chars handled specially by back-ldif:
    128      1.1  lukem  * - If the escape char is not '\\', it must itself be escaped.  Otherwise
    129      1.1  lukem  *   '\\' and the escape char would map to the same character.
    130      1.1  lukem  * - Escape the '.' in ".ldif", so the directory for an RDN that actually
    131      1.1  lukem  *   ends with ".ldif" can not conflict with a file of the same name.  And
    132      1.1  lukem  *   since some OSes/programs choke on multiple '.'s, escape all of them.
    133      1.1  lukem  * - If '{' and '}' are translated to some other characters, those
    134      1.1  lukem  *   characters must in turn be escaped when they occur in an RDN.
    135      1.1  lukem  */
    136      1.1  lukem #ifndef LDIF_NEED_ESCAPE
    137      1.1  lukem #define	LDIF_NEED_ESCAPE(c) \
    138      1.1  lukem 	((LDIF_UNSAFE_CHAR(c)) || \
    139      1.1  lukem 	 LDIF_MAYBE_UNSAFE(c, LDIF_ESCAPE_CHAR) || \
    140      1.1  lukem 	 LDIF_MAYBE_UNSAFE(c, LDIF_FILETYPE_SEP) || \
    141      1.1  lukem 	 LDIF_MAYBE_UNSAFE(c, IX_FSL) || \
    142      1.1  lukem 	 (IX_FSR != IX_FSL && LDIF_MAYBE_UNSAFE(c, IX_FSR)))
    143      1.1  lukem #endif
    144      1.1  lukem /*
    145      1.1  lukem  * Helper macro for LDIF_NEED_ESCAPE(): Treat character x as unsafe if
    146      1.1  lukem  * back-ldif does not already treat is specially.
    147      1.1  lukem  */
    148      1.1  lukem #define LDIF_MAYBE_UNSAFE(c, x) \
    149      1.1  lukem 	(!(LDIF_UNSAFE_CHAR(x) || (x) == '\\' || (x) == IX_DNL || (x) == IX_DNR) \
    150      1.1  lukem 	 && (c) == (x))
    151      1.1  lukem 
    152  1.1.1.2  lukem /* Collect other "safe char" tests here, until someone needs a fix. */
    153  1.1.1.2  lukem enum {
    154  1.1.1.2  lukem 	eq_unsafe = LDIF_UNSAFE_CHAR('='),
    155  1.1.1.2  lukem 	safe_filenames = STRLENOF("" LDAP_DIRSEP "") == 1 && !(
    156  1.1.1.2  lukem 		LDIF_UNSAFE_CHAR('-') || /* for "{-1}frontend" in bconfig.c */
    157  1.1.1.2  lukem 		LDIF_UNSAFE_CHAR(LDIF_ESCAPE_CHAR) ||
    158  1.1.1.2  lukem 		LDIF_UNSAFE_CHAR(IX_FSL) || LDIF_UNSAFE_CHAR(IX_FSR))
    159  1.1.1.2  lukem };
    160  1.1.1.2  lukem /* Sanity check: Try to force a compilation error if !safe_filenames */
    161  1.1.1.2  lukem typedef struct {
    162  1.1.1.2  lukem 	int assert_safe_filenames : safe_filenames ? 2 : -2;
    163  1.1.1.2  lukem } assert_safe_filenames[safe_filenames ? 2 : -2];
    164      1.1  lukem 
    165      1.1  lukem 
    166      1.1  lukem static ConfigTable ldifcfg[] = {
    167      1.1  lukem 	{ "directory", "dir", 2, 2, 0, ARG_BERVAL|ARG_OFFSET,
    168      1.1  lukem 		(void *)offsetof(struct ldif_info, li_base_path),
    169      1.1  lukem 		"( OLcfgDbAt:0.1 NAME 'olcDbDirectory' "
    170      1.1  lukem 			"DESC 'Directory for database content' "
    171      1.1  lukem 			"EQUALITY caseIgnoreMatch "
    172      1.1  lukem 			"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
    173      1.1  lukem 	{ NULL, NULL, 0, 0, 0, ARG_IGNORED,
    174      1.1  lukem 		NULL, NULL, NULL, NULL }
    175      1.1  lukem };
    176      1.1  lukem 
    177      1.1  lukem static ConfigOCs ldifocs[] = {
    178      1.1  lukem 	{ "( OLcfgDbOc:2.1 "
    179      1.1  lukem 		"NAME 'olcLdifConfig' "
    180      1.1  lukem 		"DESC 'LDIF backend configuration' "
    181      1.1  lukem 		"SUP olcDatabaseConfig "
    182      1.1  lukem 		"MUST ( olcDbDirectory ) )", Cft_Database, ldifcfg },
    183      1.1  lukem 	{ NULL, 0, NULL }
    184      1.1  lukem };
    185      1.1  lukem 
    186      1.1  lukem 
    187  1.1.1.2  lukem /*
    188  1.1.1.2  lukem  * Handle file/directory names.
    189  1.1.1.2  lukem  */
    190  1.1.1.2  lukem 
    191      1.1  lukem /* Set *res = LDIF filename path for the normalized DN */
    192  1.1.1.2  lukem static int
    193  1.1.1.2  lukem ndn2path( Operation *op, struct berval *dn, struct berval *res, int empty_ok )
    194      1.1  lukem {
    195  1.1.1.2  lukem 	BackendDB *be = op->o_bd;
    196      1.1  lukem 	struct ldif_info *li = (struct ldif_info *) be->be_private;
    197      1.1  lukem 	struct berval *suffixdn = &be->be_nsuffix[0];
    198      1.1  lukem 	const char *start, *end, *next, *p;
    199      1.1  lukem 	char ch, *ptr;
    200      1.1  lukem 	ber_len_t len;
    201      1.1  lukem 	static const char hex[] = "0123456789ABCDEF";
    202      1.1  lukem 
    203      1.1  lukem 	assert( dn != NULL );
    204      1.1  lukem 	assert( !BER_BVISNULL( dn ) );
    205      1.1  lukem 	assert( suffixdn != NULL );
    206      1.1  lukem 	assert( !BER_BVISNULL( suffixdn ) );
    207      1.1  lukem 	assert( dnIsSuffix( dn, suffixdn ) );
    208      1.1  lukem 
    209  1.1.1.2  lukem 	if ( dn->bv_len == 0 && !empty_ok ) {
    210  1.1.1.2  lukem 		return LDAP_UNWILLING_TO_PERFORM;
    211  1.1.1.2  lukem 	}
    212  1.1.1.2  lukem 
    213      1.1  lukem 	start = dn->bv_val;
    214      1.1  lukem 	end = start + dn->bv_len;
    215      1.1  lukem 
    216      1.1  lukem 	/* Room for dir, dirsep, dn, LDIF, "\hexpair"-escaping of unsafe chars */
    217      1.1  lukem 	len = li->li_base_path.bv_len + dn->bv_len + (1 + STRLENOF( LDIF ));
    218      1.1  lukem 	for ( p = start; p < end; ) {
    219      1.1  lukem 		ch = *p++;
    220      1.1  lukem 		if ( LDIF_NEED_ESCAPE( ch ) )
    221      1.1  lukem 			len += 2;
    222      1.1  lukem 	}
    223      1.1  lukem 	res->bv_val = ch_malloc( len + 1 );
    224      1.1  lukem 
    225      1.1  lukem 	ptr = lutil_strcopy( res->bv_val, li->li_base_path.bv_val );
    226      1.1  lukem 	for ( next = end - suffixdn->bv_len; end > start; end = next ) {
    227      1.1  lukem 		/* Set p = start of DN component, next = &',' or start of DN */
    228      1.1  lukem 		while ( (p = next) > start ) {
    229      1.1  lukem 			--next;
    230      1.1  lukem 			if ( DN_SEPARATOR( *next ) )
    231      1.1  lukem 				break;
    232      1.1  lukem 		}
    233      1.1  lukem 		/* Append <dirsep> <p..end-1: RDN or database-suffix> */
    234      1.1  lukem 		for ( *ptr++ = LDAP_DIRSEP[0]; p < end; *ptr++ = ch ) {
    235      1.1  lukem 			ch = *p++;
    236      1.1  lukem 			if ( LDIF_ESCAPE_CHAR != '\\' && ch == '\\' ) {
    237      1.1  lukem 				ch = LDIF_ESCAPE_CHAR;
    238      1.1  lukem 			} else if ( IX_FSL != IX_DNL && ch == IX_DNL ) {
    239      1.1  lukem 				ch = IX_FSL;
    240      1.1  lukem 			} else if ( IX_FSR != IX_DNR && ch == IX_DNR ) {
    241      1.1  lukem 				ch = IX_FSR;
    242      1.1  lukem 			} else if ( LDIF_NEED_ESCAPE( ch ) ) {
    243      1.1  lukem 				*ptr++ = LDIF_ESCAPE_CHAR;
    244      1.1  lukem 				*ptr++ = hex[(ch & 0xFFU) >> 4];
    245      1.1  lukem 				ch = hex[ch & 0x0FU];
    246      1.1  lukem 			}
    247      1.1  lukem 		}
    248      1.1  lukem 	}
    249      1.1  lukem 	ptr = lutil_strcopy( ptr, LDIF );
    250      1.1  lukem 	res->bv_len = ptr - res->bv_val;
    251      1.1  lukem 
    252      1.1  lukem 	assert( res->bv_len <= len );
    253  1.1.1.2  lukem 
    254  1.1.1.2  lukem 	return LDAP_SUCCESS;
    255      1.1  lukem }
    256      1.1  lukem 
    257  1.1.1.2  lukem /*
    258  1.1.1.2  lukem  * *dest = dupbv(<dir + LDAP_DIRSEP>), plus room for <more>-sized filename.
    259  1.1.1.2  lukem  * Return pointer past the dirname.
    260  1.1.1.2  lukem  */
    261  1.1.1.2  lukem static char *
    262  1.1.1.2  lukem fullpath_alloc( struct berval *dest, const struct berval *dir, ber_len_t more )
    263  1.1.1.2  lukem {
    264  1.1.1.2  lukem 	char *s = SLAP_MALLOC( dir->bv_len + more + 2 );
    265  1.1.1.2  lukem 
    266  1.1.1.2  lukem 	dest->bv_val = s;
    267  1.1.1.2  lukem 	if ( s == NULL ) {
    268  1.1.1.2  lukem 		dest->bv_len = 0;
    269  1.1.1.2  lukem 		Debug( LDAP_DEBUG_ANY, "back-ldif: out of memory\n", 0, 0, 0 );
    270  1.1.1.2  lukem 	} else {
    271  1.1.1.2  lukem 		s = lutil_strcopy( dest->bv_val, dir->bv_val );
    272  1.1.1.2  lukem 		*s++ = LDAP_DIRSEP[0];
    273  1.1.1.2  lukem 		*s = '\0';
    274  1.1.1.2  lukem 		dest->bv_len = s - dest->bv_val;
    275  1.1.1.2  lukem 	}
    276  1.1.1.2  lukem 	return s;
    277  1.1.1.2  lukem }
    278  1.1.1.2  lukem 
    279  1.1.1.2  lukem /*
    280  1.1.1.2  lukem  * Append filename to fullpath_alloc() dirname or replace previous filename.
    281  1.1.1.2  lukem  * dir_end = fullpath_alloc() return value.
    282  1.1.1.2  lukem  */
    283  1.1.1.2  lukem #define FILL_PATH(fpath, dir_end, filename) \
    284  1.1.1.2  lukem 	((fpath)->bv_len = lutil_strcopy(dir_end, filename) - (fpath)->bv_val)
    285  1.1.1.2  lukem 
    286  1.1.1.2  lukem 
    287  1.1.1.2  lukem /* .ldif entry filename length <-> subtree dirname length. */
    288  1.1.1.2  lukem #define ldif2dir_len(bv)  ((bv).bv_len -= STRLENOF(LDIF))
    289  1.1.1.2  lukem #define dir2ldif_len(bv)  ((bv).bv_len += STRLENOF(LDIF))
    290  1.1.1.2  lukem /* .ldif entry filename <-> subtree dirname, both with dirname length. */
    291  1.1.1.2  lukem #define ldif2dir_name(bv) ((bv).bv_val[(bv).bv_len] = '\0')
    292  1.1.1.2  lukem #define dir2ldif_name(bv) ((bv).bv_val[(bv).bv_len] = LDIF_FILETYPE_SEP)
    293  1.1.1.2  lukem 
    294  1.1.1.2  lukem /* Get the parent directory path, plus the LDIF suffix overwritten by a \0. */
    295  1.1.1.2  lukem static int
    296  1.1.1.2  lukem get_parent_path( struct berval *dnpath, struct berval *res )
    297  1.1.1.2  lukem {
    298  1.1.1.2  lukem 	ber_len_t i = dnpath->bv_len;
    299  1.1.1.2  lukem 
    300  1.1.1.2  lukem 	while ( i > 0 && dnpath->bv_val[ --i ] != LDAP_DIRSEP[0] ) ;
    301  1.1.1.2  lukem 	if ( res == NULL ) {
    302  1.1.1.2  lukem 		res = dnpath;
    303  1.1.1.2  lukem 	} else {
    304  1.1.1.2  lukem 		res->bv_val = SLAP_MALLOC( i + 1 + STRLENOF(LDIF) );
    305  1.1.1.2  lukem 		if ( res->bv_val == NULL )
    306  1.1.1.2  lukem 			return LDAP_OTHER;
    307  1.1.1.2  lukem 		AC_MEMCPY( res->bv_val, dnpath->bv_val, i );
    308  1.1.1.2  lukem 	}
    309  1.1.1.2  lukem 	res->bv_len = i;
    310  1.1.1.2  lukem 	strcpy( res->bv_val + i, LDIF );
    311  1.1.1.2  lukem 	res->bv_val[i] = '\0';
    312  1.1.1.2  lukem 	return LDAP_SUCCESS;
    313  1.1.1.2  lukem }
    314  1.1.1.2  lukem 
    315  1.1.1.2  lukem /* Make temporary filename pattern for mkstemp() based on dnpath. */
    316  1.1.1.2  lukem static char *
    317  1.1.1.2  lukem ldif_tempname( const struct berval *dnpath )
    318  1.1.1.2  lukem {
    319  1.1.1.2  lukem 	static const char suffix[] = ".XXXXXX";
    320  1.1.1.2  lukem 	ber_len_t len = dnpath->bv_len - STRLENOF( LDIF );
    321  1.1.1.2  lukem 	char *name = SLAP_MALLOC( len + sizeof( suffix ) );
    322  1.1.1.2  lukem 
    323  1.1.1.2  lukem 	if ( name != NULL ) {
    324  1.1.1.2  lukem 		AC_MEMCPY( name, dnpath->bv_val, len );
    325  1.1.1.2  lukem 		strcpy( name + len, suffix );
    326  1.1.1.2  lukem 	}
    327  1.1.1.2  lukem 	return name;
    328  1.1.1.2  lukem }
    329  1.1.1.2  lukem 
    330  1.1.1.2  lukem /*
    331  1.1.1.2  lukem  * Read a file, or stat() it if datap == NULL.  Allocate and fill *datap.
    332  1.1.1.2  lukem  * Return LDAP_SUCCESS, LDAP_NO_SUCH_OBJECT (no such file), or another error.
    333  1.1.1.2  lukem  */
    334  1.1.1.2  lukem static int
    335  1.1.1.2  lukem ldif_read_file( const char *path, char **datap )
    336  1.1.1.2  lukem {
    337  1.1.1.2  lukem 	int rc, fd, len;
    338  1.1.1.2  lukem 	int res = -1;	/* 0:success, <0:error, >0:file too big/growing. */
    339      1.1  lukem 	struct stat st;
    340  1.1.1.2  lukem 	char *data = NULL, *ptr;
    341      1.1  lukem 
    342  1.1.1.2  lukem 	if ( datap == NULL ) {
    343  1.1.1.2  lukem 		res = stat( path, &st );
    344  1.1.1.2  lukem 		goto done;
    345  1.1.1.2  lukem 	}
    346  1.1.1.2  lukem 	fd = open( path, O_RDONLY );
    347  1.1.1.2  lukem 	if ( fd >= 0 ) {
    348  1.1.1.2  lukem 		if ( fstat( fd, &st ) == 0 ) {
    349  1.1.1.2  lukem 			if ( st.st_size > INT_MAX - 2 ) {
    350  1.1.1.2  lukem 				res = 1;
    351  1.1.1.2  lukem 			} else {
    352  1.1.1.2  lukem 				len = st.st_size + 1; /* +1 detects file size > st.st_size */
    353  1.1.1.2  lukem 				*datap = data = ptr = SLAP_MALLOC( len + 1 );
    354  1.1.1.2  lukem 				if ( ptr != NULL ) {
    355  1.1.1.2  lukem 					while ( len && (res = read( fd, ptr, len )) ) {
    356  1.1.1.2  lukem 						if ( res > 0 ) {
    357  1.1.1.2  lukem 							len -= res;
    358  1.1.1.2  lukem 							ptr += res;
    359  1.1.1.2  lukem 						} else if ( errno != EINTR ) {
    360  1.1.1.2  lukem 							break;
    361  1.1.1.2  lukem 						}
    362  1.1.1.2  lukem 					}
    363  1.1.1.2  lukem 					*ptr = '\0';
    364  1.1.1.2  lukem 				}
    365  1.1.1.2  lukem 			}
    366      1.1  lukem 		}
    367  1.1.1.2  lukem 		if ( close( fd ) < 0 )
    368  1.1.1.2  lukem 			res = -1;
    369  1.1.1.2  lukem 	}
    370  1.1.1.2  lukem 
    371  1.1.1.2  lukem  done:
    372  1.1.1.2  lukem 	if ( res == 0 ) {
    373  1.1.1.2  lukem 		Debug( LDAP_DEBUG_TRACE, "ldif_read_file: %s: \"%s\"\n",
    374  1.1.1.2  lukem 			datap ? "read entry file" : "entry file exists", path, 0 );
    375  1.1.1.2  lukem 		rc = LDAP_SUCCESS;
    376  1.1.1.2  lukem 	} else {
    377  1.1.1.2  lukem 		if ( res < 0 && errno == ENOENT ) {
    378  1.1.1.2  lukem 			Debug( LDAP_DEBUG_TRACE, "ldif_read_file: "
    379  1.1.1.2  lukem 				"no entry file \"%s\"\n", path, 0, 0 );
    380  1.1.1.2  lukem 			rc = LDAP_NO_SUCH_OBJECT;
    381  1.1.1.2  lukem 		} else {
    382  1.1.1.2  lukem 			const char *msg = res < 0 ? STRERROR( errno ) : "bad stat() size";
    383  1.1.1.2  lukem 			Debug( LDAP_DEBUG_ANY, "ldif_read_file: %s for \"%s\"\n",
    384  1.1.1.2  lukem 				msg, path, 0 );
    385  1.1.1.2  lukem 			rc = LDAP_OTHER;
    386      1.1  lukem 		}
    387  1.1.1.2  lukem 		if ( data != NULL )
    388  1.1.1.2  lukem 			SLAP_FREE( data );
    389      1.1  lukem 	}
    390  1.1.1.2  lukem 	return rc;
    391      1.1  lukem }
    392      1.1  lukem 
    393      1.1  lukem /*
    394      1.1  lukem  * return nonnegative for success or -1 for error
    395      1.1  lukem  * do not return numbers less than -1
    396      1.1  lukem  */
    397  1.1.1.2  lukem static int
    398  1.1.1.2  lukem spew_file( int fd, const char *spew, int len, int *save_errno )
    399  1.1.1.2  lukem {
    400      1.1  lukem 	int writeres = 0;
    401  1.1.1.2  lukem 
    402      1.1  lukem 	while(len > 0) {
    403      1.1  lukem 		writeres = write(fd, spew, len);
    404      1.1  lukem 		if(writeres == -1) {
    405  1.1.1.2  lukem 			*save_errno = errno;
    406  1.1.1.2  lukem 			if (*save_errno != EINTR)
    407  1.1.1.2  lukem 				break;
    408      1.1  lukem 		}
    409      1.1  lukem 		else {
    410      1.1  lukem 			spew += writeres;
    411      1.1  lukem 			len -= writeres;
    412      1.1  lukem 		}
    413      1.1  lukem 	}
    414      1.1  lukem 	return writeres;
    415      1.1  lukem }
    416      1.1  lukem 
    417  1.1.1.2  lukem /* Write an entry LDIF file.  Create parentdir first if non-NULL. */
    418      1.1  lukem static int
    419  1.1.1.2  lukem ldif_write_entry(
    420  1.1.1.2  lukem 	Operation *op,
    421  1.1.1.2  lukem 	Entry *e,
    422  1.1.1.2  lukem 	const struct berval *path,
    423  1.1.1.2  lukem 	const char *parentdir,
    424  1.1.1.2  lukem 	const char **text )
    425      1.1  lukem {
    426  1.1.1.2  lukem 	int rc = LDAP_OTHER, res, save_errno = 0;
    427  1.1.1.2  lukem 	int fd, entry_length;
    428  1.1.1.2  lukem 	char *entry_as_string, *tmpfname;
    429  1.1.1.2  lukem 
    430  1.1.1.2  lukem 	if ( op->o_abandon )
    431  1.1.1.2  lukem 		return SLAPD_ABANDON;
    432      1.1  lukem 
    433  1.1.1.2  lukem 	if ( parentdir != NULL && mkdir( parentdir, 0750 ) < 0 ) {
    434      1.1  lukem 		save_errno = errno;
    435  1.1.1.2  lukem 		Debug( LDAP_DEBUG_ANY, "ldif_write_entry: %s \"%s\": %s\n",
    436  1.1.1.2  lukem 			"cannot create parent directory",
    437  1.1.1.2  lukem 			parentdir, STRERROR( save_errno ) );
    438  1.1.1.2  lukem 		*text = "internal error (cannot create parent directory)";
    439  1.1.1.2  lukem 		return rc;
    440  1.1.1.2  lukem 	}
    441  1.1.1.2  lukem 
    442  1.1.1.2  lukem 	tmpfname = ldif_tempname( path );
    443  1.1.1.2  lukem 	fd = tmpfname == NULL ? -1 : mkstemp( tmpfname );
    444  1.1.1.2  lukem 	if ( fd < 0 ) {
    445  1.1.1.2  lukem 		save_errno = errno;
    446  1.1.1.2  lukem 		Debug( LDAP_DEBUG_ANY, "ldif_write_entry: %s for \"%s\": %s\n",
    447  1.1.1.2  lukem 			"cannot create file", e->e_dn, STRERROR( save_errno ) );
    448  1.1.1.2  lukem 		*text = "internal error (cannot create file)";
    449      1.1  lukem 
    450      1.1  lukem 	} else {
    451  1.1.1.2  lukem 		ber_len_t dn_len = e->e_name.bv_len;
    452      1.1  lukem 		struct berval rdn;
    453      1.1  lukem 
    454      1.1  lukem 		/* Only save the RDN onto disk */
    455      1.1  lukem 		dnRdn( &e->e_name, &rdn );
    456  1.1.1.2  lukem 		if ( rdn.bv_len != dn_len ) {
    457      1.1  lukem 			e->e_name.bv_val[rdn.bv_len] = '\0';
    458      1.1  lukem 			e->e_name.bv_len = rdn.bv_len;
    459      1.1  lukem 		}
    460      1.1  lukem 
    461  1.1.1.2  lukem 		res = -2;
    462  1.1.1.2  lukem 		ldap_pvt_thread_mutex_lock( &entry2str_mutex );
    463  1.1.1.2  lukem 		entry_as_string = entry2str( e, &entry_length );
    464  1.1.1.2  lukem 		if ( entry_as_string != NULL )
    465  1.1.1.2  lukem 			res = spew_file( fd, entry_as_string, entry_length, &save_errno );
    466  1.1.1.2  lukem 		ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
    467      1.1  lukem 
    468      1.1  lukem 		/* Restore full DN */
    469  1.1.1.2  lukem 		if ( rdn.bv_len != dn_len ) {
    470  1.1.1.2  lukem 			e->e_name.bv_val[rdn.bv_len] = ',';
    471  1.1.1.2  lukem 			e->e_name.bv_len = dn_len;
    472      1.1  lukem 		}
    473      1.1  lukem 
    474  1.1.1.2  lukem 		if ( close( fd ) < 0 && res >= 0 ) {
    475  1.1.1.2  lukem 			res = -1;
    476  1.1.1.2  lukem 			save_errno = errno;
    477  1.1.1.2  lukem 		}
    478      1.1  lukem 
    479  1.1.1.2  lukem 		if ( res >= 0 ) {
    480  1.1.1.2  lukem 			if ( move_file( tmpfname, path->bv_val ) == 0 ) {
    481  1.1.1.2  lukem 				Debug( LDAP_DEBUG_TRACE, "ldif_write_entry: "
    482  1.1.1.2  lukem 					"wrote entry \"%s\"\n", e->e_name.bv_val, 0, 0 );
    483  1.1.1.2  lukem 				rc = LDAP_SUCCESS;
    484      1.1  lukem 			} else {
    485  1.1.1.2  lukem 				save_errno = errno;
    486  1.1.1.2  lukem 				Debug( LDAP_DEBUG_ANY, "ldif_write_entry: "
    487  1.1.1.2  lukem 					"could not put entry file for \"%s\" in place: %s\n",
    488  1.1.1.2  lukem 					e->e_name.bv_val, STRERROR( save_errno ), 0 );
    489  1.1.1.2  lukem 				*text = "internal error (could not put entry file in place)";
    490  1.1.1.2  lukem 			}
    491  1.1.1.2  lukem 		} else if ( res == -1 ) {
    492  1.1.1.2  lukem 			Debug( LDAP_DEBUG_ANY, "ldif_write_entry: %s \"%s\": %s\n",
    493  1.1.1.2  lukem 				"write error to", tmpfname, STRERROR( save_errno ) );
    494  1.1.1.2  lukem 			*text = "internal error (write error to entry file)";
    495      1.1  lukem 		}
    496      1.1  lukem 
    497  1.1.1.2  lukem 		if ( rc != LDAP_SUCCESS ) {
    498      1.1  lukem 			unlink( tmpfname );
    499      1.1  lukem 		}
    500      1.1  lukem 	}
    501      1.1  lukem 
    502  1.1.1.2  lukem 	if ( tmpfname )
    503  1.1.1.2  lukem 		SLAP_FREE( tmpfname );
    504  1.1.1.2  lukem 	return rc;
    505      1.1  lukem }
    506      1.1  lukem 
    507  1.1.1.2  lukem /*
    508  1.1.1.2  lukem  * Read the entry at path, or if entryp==NULL just see if it exists.
    509  1.1.1.2  lukem  * pdn and pndn are the parent's DN and normalized DN, or both NULL.
    510  1.1.1.2  lukem  * Return an LDAP result code.
    511  1.1.1.2  lukem  */
    512  1.1.1.2  lukem static int
    513  1.1.1.2  lukem ldif_read_entry(
    514  1.1.1.2  lukem 	Operation *op,
    515  1.1.1.2  lukem 	const char *path,
    516      1.1  lukem 	struct berval *pdn,
    517  1.1.1.2  lukem 	struct berval *pndn,
    518  1.1.1.2  lukem 	Entry **entryp,
    519  1.1.1.2  lukem 	const char **text )
    520      1.1  lukem {
    521  1.1.1.2  lukem 	int rc;
    522  1.1.1.2  lukem 	Entry *entry;
    523  1.1.1.2  lukem 	char *entry_as_string;
    524  1.1.1.2  lukem 	struct berval rdn;
    525      1.1  lukem 
    526  1.1.1.2  lukem 	/* TODO: Does slapd prevent Abandon of Bind as per rfc4511?
    527  1.1.1.2  lukem 	 * If so we need not check for LDAP_REQ_BIND here.
    528  1.1.1.2  lukem 	 */
    529  1.1.1.2  lukem 	if ( op->o_abandon && op->o_tag != LDAP_REQ_BIND )
    530  1.1.1.2  lukem 		return SLAPD_ABANDON;
    531  1.1.1.2  lukem 
    532  1.1.1.2  lukem 	rc = ldif_read_file( path, entryp ? &entry_as_string : NULL );
    533  1.1.1.2  lukem 
    534  1.1.1.2  lukem 	switch ( rc ) {
    535  1.1.1.2  lukem 	case LDAP_SUCCESS:
    536  1.1.1.2  lukem 		if ( entryp == NULL )
    537  1.1.1.2  lukem 			break;
    538  1.1.1.2  lukem 		*entryp = entry = str2entry( entry_as_string );
    539  1.1.1.2  lukem 		SLAP_FREE( entry_as_string );
    540  1.1.1.2  lukem 		if ( entry == NULL ) {
    541  1.1.1.2  lukem 			rc = LDAP_OTHER;
    542  1.1.1.2  lukem 			if ( text != NULL )
    543  1.1.1.2  lukem 				*text = "internal error (cannot parse some entry file)";
    544  1.1.1.2  lukem 			break;
    545      1.1  lukem 		}
    546  1.1.1.2  lukem 		if ( pdn == NULL || BER_BVISEMPTY( pdn ) )
    547  1.1.1.2  lukem 			break;
    548  1.1.1.2  lukem 		/* Append parent DN to DN from LDIF file */
    549  1.1.1.2  lukem 		rdn = entry->e_name;
    550  1.1.1.2  lukem 		build_new_dn( &entry->e_name, pdn, &rdn, NULL );
    551  1.1.1.2  lukem 		SLAP_FREE( rdn.bv_val );
    552  1.1.1.2  lukem 		rdn = entry->e_nname;
    553  1.1.1.2  lukem 		build_new_dn( &entry->e_nname, pndn, &rdn, NULL );
    554  1.1.1.2  lukem 		SLAP_FREE( rdn.bv_val );
    555  1.1.1.2  lukem 		break;
    556  1.1.1.2  lukem 
    557  1.1.1.2  lukem 	case LDAP_OTHER:
    558  1.1.1.2  lukem 		if ( text != NULL )
    559  1.1.1.2  lukem 			*text = entryp
    560  1.1.1.2  lukem 				? "internal error (cannot read some entry file)"
    561  1.1.1.2  lukem 				: "internal error (cannot stat some entry file)";
    562  1.1.1.2  lukem 		break;
    563      1.1  lukem 	}
    564  1.1.1.2  lukem 
    565  1.1.1.2  lukem 	return rc;
    566      1.1  lukem }
    567      1.1  lukem 
    568  1.1.1.2  lukem /*
    569  1.1.1.2  lukem  * Read the operation's entry, or if entryp==NULL just see if it exists.
    570  1.1.1.2  lukem  * Return an LDAP result code.  May set *text to a message on failure.
    571  1.1.1.2  lukem  * If pathp is non-NULL, set it to the entry filename on success.
    572  1.1.1.2  lukem  */
    573      1.1  lukem static int
    574      1.1  lukem get_entry(
    575      1.1  lukem 	Operation *op,
    576      1.1  lukem 	Entry **entryp,
    577  1.1.1.2  lukem 	struct berval *pathp,
    578  1.1.1.2  lukem 	const char **text )
    579      1.1  lukem {
    580      1.1  lukem 	int rc;
    581      1.1  lukem 	struct berval path, pdn, pndn;
    582      1.1  lukem 
    583  1.1.1.2  lukem 	dnParent( &op->o_req_dn, &pdn );
    584  1.1.1.2  lukem 	dnParent( &op->o_req_ndn, &pndn );
    585  1.1.1.2  lukem 	rc = ndn2path( op, &op->o_req_ndn, &path, 0 );
    586  1.1.1.2  lukem 	if ( rc != LDAP_SUCCESS ) {
    587  1.1.1.2  lukem 		goto done;
    588      1.1  lukem 	}
    589  1.1.1.2  lukem 
    590  1.1.1.2  lukem 	rc = ldif_read_entry( op, path.bv_val, &pdn, &pndn, entryp, text );
    591      1.1  lukem 
    592      1.1  lukem 	if ( rc == LDAP_SUCCESS && pathp != NULL ) {
    593      1.1  lukem 		*pathp = path;
    594      1.1  lukem 	} else {
    595  1.1.1.2  lukem 		SLAP_FREE( path.bv_val );
    596      1.1  lukem 	}
    597  1.1.1.2  lukem  done:
    598      1.1  lukem 	return rc;
    599      1.1  lukem }
    600      1.1  lukem 
    601      1.1  lukem 
    602  1.1.1.2  lukem /*
    603  1.1.1.2  lukem  * RDN-named directory entry, with special handling of "attr={num}val" RDNs.
    604  1.1.1.2  lukem  * For sorting, filename "attr=val.ldif" is truncated to "attr="val\0ldif",
    605  1.1.1.2  lukem  * and filename "attr={num}val.ldif" to "attr={\0um}val.ldif".
    606  1.1.1.2  lukem  * Does not sort escaped chars correctly, would need to un-escape them.
    607  1.1.1.2  lukem  */
    608      1.1  lukem typedef struct bvlist {
    609      1.1  lukem 	struct bvlist *next;
    610  1.1.1.2  lukem 	char *trunc;	/* filename was truncated here */
    611  1.1.1.2  lukem 	int  inum;		/* num from "attr={num}" in filename, or INT_MIN */
    612  1.1.1.2  lukem 	char savech;	/* original char at *trunc */
    613  1.1.1.2  lukem 	/* BVL_NAME(&bvlist) is the filename, allocated after the struct: */
    614  1.1.1.2  lukem #	define BVL_NAME(bvl)     ((char *) ((bvl) + 1))
    615  1.1.1.2  lukem #	define BVL_SIZE(namelen) (sizeof(bvlist) + (namelen) + 1)
    616      1.1  lukem } bvlist;
    617      1.1  lukem 
    618  1.1.1.2  lukem static int
    619  1.1.1.2  lukem ldif_send_entry( Operation *op, SlapReply *rs, Entry *e, int scope )
    620      1.1  lukem {
    621  1.1.1.2  lukem 	int rc = LDAP_SUCCESS;
    622      1.1  lukem 
    623  1.1.1.2  lukem 	if ( scope == LDAP_SCOPE_BASE || scope == LDAP_SCOPE_SUBTREE ) {
    624  1.1.1.2  lukem 		if ( rs == NULL ) {
    625  1.1.1.2  lukem 			/* Save the entry for tool mode */
    626  1.1.1.2  lukem 			struct ldif_tool *tl =
    627  1.1.1.2  lukem 				&((struct ldif_info *) op->o_bd->be_private)->li_tool;
    628  1.1.1.2  lukem 
    629  1.1.1.2  lukem 			if ( tl->ecount >= tl->elen ) {
    630  1.1.1.2  lukem 				/* Allocate/grow entries */
    631  1.1.1.2  lukem 				ID elen = tl->elen ? tl->elen * 2 : ENTRY_BUFF_INCREMENT;
    632  1.1.1.2  lukem 				Entry **entries = (Entry **) SLAP_REALLOC( tl->entries,
    633  1.1.1.2  lukem 					sizeof(Entry *) * elen );
    634  1.1.1.2  lukem 				if ( entries == NULL ) {
    635  1.1.1.2  lukem 					Debug( LDAP_DEBUG_ANY,
    636  1.1.1.2  lukem 						"ldif_send_entry: out of memory\n", 0, 0, 0 );
    637  1.1.1.2  lukem 					rc = LDAP_OTHER;
    638  1.1.1.2  lukem 					goto done;
    639  1.1.1.2  lukem 				}
    640  1.1.1.2  lukem 				tl->elen = elen;
    641  1.1.1.2  lukem 				tl->entries = entries;
    642  1.1.1.2  lukem 			}
    643  1.1.1.2  lukem 			tl->entries[tl->ecount++] = e;
    644  1.1.1.2  lukem 			return rc;
    645      1.1  lukem 		}
    646      1.1  lukem 
    647  1.1.1.2  lukem 		else if ( !get_manageDSAit( op ) && is_entry_referral( e ) ) {
    648  1.1.1.2  lukem 			/* Send a continuation reference.
    649  1.1.1.2  lukem 			 * (ldif_back_referrals() handles baseobject referrals.)
    650  1.1.1.2  lukem 			 * Don't check the filter since it's only a candidate.
    651  1.1.1.2  lukem 			 */
    652  1.1.1.2  lukem 			BerVarray refs = get_entry_referrals( op, e );
    653  1.1.1.2  lukem 			rs->sr_ref = referral_rewrite( refs, &e->e_name, NULL, scope );
    654  1.1.1.2  lukem 			rs->sr_entry = e;
    655  1.1.1.2  lukem 			rc = send_search_reference( op, rs );
    656  1.1.1.2  lukem 			ber_bvarray_free( rs->sr_ref );
    657  1.1.1.2  lukem 			ber_bvarray_free( refs );
    658  1.1.1.2  lukem 			rs->sr_ref = NULL;
    659  1.1.1.2  lukem 			rs->sr_entry = NULL;
    660      1.1  lukem 		}
    661      1.1  lukem 
    662  1.1.1.2  lukem 		else if ( test_filter( op, e, op->ors_filter ) == LDAP_COMPARE_TRUE ) {
    663  1.1.1.2  lukem 			rs->sr_entry = e;
    664  1.1.1.2  lukem 			rs->sr_attrs = op->ors_attrs;
    665  1.1.1.2  lukem 			rs->sr_flags = REP_ENTRY_MODIFIABLE;
    666  1.1.1.2  lukem 			rc = send_search_entry( op, rs );
    667  1.1.1.2  lukem 			rs->sr_entry = NULL;
    668      1.1  lukem 		}
    669      1.1  lukem 	}
    670      1.1  lukem 
    671  1.1.1.2  lukem  done:
    672  1.1.1.2  lukem 	entry_free( e );
    673  1.1.1.2  lukem 	return rc;
    674  1.1.1.2  lukem }
    675      1.1  lukem 
    676  1.1.1.2  lukem /* Read LDIF directory <path> into <listp>.  Set *fname_maxlenp. */
    677  1.1.1.2  lukem static int
    678  1.1.1.2  lukem ldif_readdir(
    679  1.1.1.2  lukem 	Operation *op,
    680  1.1.1.2  lukem 	SlapReply *rs,
    681  1.1.1.2  lukem 	const struct berval *path,
    682  1.1.1.2  lukem 	bvlist **listp,
    683  1.1.1.2  lukem 	ber_len_t *fname_maxlenp )
    684  1.1.1.2  lukem {
    685  1.1.1.2  lukem 	int rc = LDAP_SUCCESS;
    686  1.1.1.2  lukem 	DIR *dir_of_path;
    687      1.1  lukem 
    688  1.1.1.2  lukem 	*listp = NULL;
    689  1.1.1.2  lukem 	*fname_maxlenp = 0;
    690  1.1.1.2  lukem 
    691  1.1.1.2  lukem 	dir_of_path = opendir( path->bv_val );
    692  1.1.1.2  lukem 	if ( dir_of_path == NULL ) {
    693  1.1.1.2  lukem 		int save_errno = errno;
    694  1.1.1.2  lukem 		struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
    695  1.1.1.2  lukem 		int is_rootDSE = (path->bv_len == li->li_base_path.bv_len);
    696  1.1.1.2  lukem 
    697  1.1.1.2  lukem 		/* Absent directory is OK (leaf entry), except the database dir */
    698  1.1.1.2  lukem 		if ( is_rootDSE || save_errno != ENOENT ) {
    699  1.1.1.2  lukem 			Debug( LDAP_DEBUG_ANY,
    700  1.1.1.2  lukem 				"=> ldif_search_entry: failed to opendir \"%s\": %s\n",
    701  1.1.1.2  lukem 				path->bv_val, STRERROR( save_errno ), 0 );
    702  1.1.1.2  lukem 			rc = LDAP_OTHER;
    703  1.1.1.2  lukem 			if ( rs != NULL )
    704  1.1.1.2  lukem 				rs->sr_text =
    705  1.1.1.2  lukem 					save_errno != ENOENT ? "internal error (bad directory)"
    706  1.1.1.2  lukem 					: !is_rootDSE ? "internal error (missing directory)"
    707  1.1.1.2  lukem 					: "internal error (database directory does not exist)";
    708      1.1  lukem 		}
    709  1.1.1.2  lukem 
    710  1.1.1.2  lukem 	} else {
    711  1.1.1.2  lukem 		bvlist *ptr;
    712  1.1.1.2  lukem 		struct dirent *dir;
    713  1.1.1.2  lukem 		int save_errno = 0;
    714  1.1.1.2  lukem 
    715  1.1.1.2  lukem 		while ( (dir = readdir( dir_of_path )) != NULL ) {
    716  1.1.1.2  lukem 			size_t fname_len;
    717      1.1  lukem 			bvlist *bvl, **prev;
    718  1.1.1.2  lukem 			char *trunc, *idxp, *endp, *endp2;
    719      1.1  lukem 
    720  1.1.1.2  lukem 			fname_len = strlen( dir->d_name );
    721  1.1.1.2  lukem 			if ( fname_len < STRLENOF( "x=" LDIF )) /* min filename size */
    722      1.1  lukem 				continue;
    723  1.1.1.2  lukem 			if ( strcmp( dir->d_name + fname_len - STRLENOF(LDIF), LDIF ))
    724      1.1  lukem 				continue;
    725      1.1  lukem 
    726  1.1.1.2  lukem 			if ( *fname_maxlenp < fname_len )
    727  1.1.1.2  lukem 				*fname_maxlenp = fname_len;
    728  1.1.1.2  lukem 
    729  1.1.1.2  lukem 			bvl = SLAP_MALLOC( BVL_SIZE( fname_len ) );
    730  1.1.1.2  lukem 			if ( bvl == NULL ) {
    731  1.1.1.2  lukem 				rc = LDAP_OTHER;
    732  1.1.1.2  lukem 				save_errno = errno;
    733  1.1.1.2  lukem 				break;
    734      1.1  lukem 			}
    735  1.1.1.2  lukem 			strcpy( BVL_NAME( bvl ), dir->d_name );
    736      1.1  lukem 
    737  1.1.1.2  lukem 			/* Make it sortable by ("attr=val" or <preceding {num}, num>) */
    738  1.1.1.2  lukem 			trunc = BVL_NAME( bvl ) + fname_len - STRLENOF( LDIF );
    739  1.1.1.2  lukem 			if ( (idxp = strchr( BVL_NAME( bvl ) + 2, IX_FSL )) != NULL &&
    740  1.1.1.2  lukem 				 (endp = strchr( ++idxp, IX_FSR )) != NULL && endp > idxp &&
    741  1.1.1.2  lukem 				 (eq_unsafe || idxp[-2] == '=' || endp + 1 == trunc) )
    742  1.1.1.2  lukem 			{
    743  1.1.1.2  lukem 				/* attr={n}val or bconfig.c's "pseudo-indexed" attr=val{n} */
    744  1.1.1.2  lukem 				bvl->inum = strtol( idxp, &endp2, 10 );
    745  1.1.1.2  lukem 				if ( endp2 == endp ) {
    746  1.1.1.2  lukem 					trunc = idxp;
    747  1.1.1.2  lukem 					goto truncate;
    748  1.1.1.2  lukem 				}
    749  1.1.1.2  lukem 			}
    750  1.1.1.2  lukem 			bvl->inum = INT_MIN;
    751  1.1.1.2  lukem 		truncate:
    752  1.1.1.2  lukem 			bvl->trunc = trunc;
    753  1.1.1.2  lukem 			bvl->savech = *trunc;
    754  1.1.1.2  lukem 			*trunc = '\0';
    755  1.1.1.2  lukem 
    756  1.1.1.2  lukem 			/* Insertion sort */
    757  1.1.1.2  lukem 			for ( prev = listp; (ptr = *prev) != NULL; prev = &ptr->next ) {
    758  1.1.1.2  lukem 				int cmp = strcmp( BVL_NAME( bvl ), BVL_NAME( ptr ));
    759  1.1.1.2  lukem 				if ( cmp < 0 || (cmp == 0 && bvl->inum < ptr->inum) )
    760      1.1  lukem 					break;
    761      1.1  lukem 			}
    762      1.1  lukem 			*prev = bvl;
    763      1.1  lukem 			bvl->next = ptr;
    764      1.1  lukem 		}
    765      1.1  lukem 
    766  1.1.1.2  lukem 		if ( closedir( dir_of_path ) < 0 ) {
    767  1.1.1.2  lukem 			save_errno = errno;
    768  1.1.1.2  lukem 			rc = LDAP_OTHER;
    769  1.1.1.2  lukem 			if ( rs != NULL )
    770  1.1.1.2  lukem 				rs->sr_text = "internal error (bad directory)";
    771  1.1.1.2  lukem 		}
    772  1.1.1.2  lukem 		if ( rc != LDAP_SUCCESS ) {
    773  1.1.1.2  lukem 			Debug( LDAP_DEBUG_ANY, "ldif_search_entry: %s \"%s\": %s\n",
    774  1.1.1.2  lukem 				"error reading directory", path->bv_val,
    775  1.1.1.2  lukem 				STRERROR( save_errno ) );
    776      1.1  lukem 		}
    777      1.1  lukem 	}
    778  1.1.1.2  lukem 
    779      1.1  lukem 	return rc;
    780      1.1  lukem }
    781      1.1  lukem 
    782  1.1.1.2  lukem /*
    783  1.1.1.2  lukem  * Send an entry, recursively search its children, and free or save it.
    784  1.1.1.2  lukem  * Return an LDAP result code.  Parameters:
    785  1.1.1.2  lukem  *  op, rs  operation and reply.  rs == NULL for slap tools.
    786  1.1.1.2  lukem  *  e       entry to search, or NULL for rootDSE.
    787  1.1.1.2  lukem  *  scope   scope for the part of the search from this entry.
    788  1.1.1.2  lukem  *  path    LDIF filename -- bv_len and non-directory part are overwritten.
    789  1.1.1.2  lukem  */
    790      1.1  lukem static int
    791  1.1.1.2  lukem ldif_search_entry(
    792  1.1.1.2  lukem 	Operation *op,
    793  1.1.1.2  lukem 	SlapReply *rs,
    794  1.1.1.2  lukem 	Entry *e,
    795  1.1.1.2  lukem 	int scope,
    796  1.1.1.2  lukem 	struct berval *path )
    797      1.1  lukem {
    798  1.1.1.2  lukem 	int rc = LDAP_SUCCESS;
    799  1.1.1.2  lukem 	struct berval dn = BER_BVC( "" ), ndn = BER_BVC( "" );
    800  1.1.1.2  lukem 
    801  1.1.1.2  lukem 	if ( scope != LDAP_SCOPE_BASE && e != NULL ) {
    802  1.1.1.2  lukem 		/* Copy DN/NDN since we send the entry with REP_ENTRY_MODIFIABLE,
    803  1.1.1.2  lukem 		 * which bconfig.c seems to need.  (TODO: see config_rename_one.)
    804  1.1.1.2  lukem 		 */
    805  1.1.1.2  lukem 		if ( ber_dupbv( &dn,  &e->e_name  ) == NULL ||
    806  1.1.1.2  lukem 			 ber_dupbv( &ndn, &e->e_nname ) == NULL )
    807  1.1.1.2  lukem 		{
    808  1.1.1.2  lukem 			Debug( LDAP_DEBUG_ANY,
    809  1.1.1.2  lukem 				"ldif_search_entry: out of memory\n", 0, 0, 0 );
    810  1.1.1.2  lukem 			rc = LDAP_OTHER;
    811  1.1.1.2  lukem 			goto done;
    812  1.1.1.2  lukem 		}
    813  1.1.1.2  lukem 	}
    814  1.1.1.2  lukem 
    815  1.1.1.2  lukem 	/* Send the entry if appropriate, and free or save it */
    816  1.1.1.2  lukem 	if ( e != NULL )
    817  1.1.1.2  lukem 		rc = ldif_send_entry( op, rs, e, scope );
    818  1.1.1.2  lukem 
    819  1.1.1.2  lukem 	/* Search the children */
    820  1.1.1.2  lukem 	if ( scope != LDAP_SCOPE_BASE && rc == LDAP_SUCCESS ) {
    821  1.1.1.2  lukem 		bvlist *list, *ptr;
    822  1.1.1.2  lukem 		struct berval fpath;	/* becomes child pathname */
    823  1.1.1.2  lukem 		char *dir_end;	/* will point past dirname in fpath */
    824  1.1.1.2  lukem 
    825  1.1.1.2  lukem 		ldif2dir_len( *path );
    826  1.1.1.2  lukem 		ldif2dir_name( *path );
    827  1.1.1.2  lukem 		rc = ldif_readdir( op, rs, path, &list, &fpath.bv_len );
    828  1.1.1.2  lukem 
    829  1.1.1.2  lukem 		if ( list != NULL ) {
    830  1.1.1.2  lukem 			const char **text = rs == NULL ? NULL : &rs->sr_text;
    831  1.1.1.2  lukem 
    832  1.1.1.2  lukem 			if ( scope == LDAP_SCOPE_ONELEVEL )
    833  1.1.1.2  lukem 				scope = LDAP_SCOPE_BASE;
    834  1.1.1.2  lukem 			else if ( scope == LDAP_SCOPE_SUBORDINATE )
    835  1.1.1.2  lukem 				scope = LDAP_SCOPE_SUBTREE;
    836  1.1.1.2  lukem 
    837  1.1.1.2  lukem 			/* Allocate fpath and fill in directory part */
    838  1.1.1.2  lukem 			dir_end = fullpath_alloc( &fpath, path, fpath.bv_len );
    839  1.1.1.2  lukem 			if ( dir_end == NULL )
    840  1.1.1.2  lukem 				rc = LDAP_OTHER;
    841  1.1.1.2  lukem 
    842  1.1.1.2  lukem 			do {
    843  1.1.1.2  lukem 				ptr = list;
    844  1.1.1.2  lukem 
    845  1.1.1.2  lukem 				if ( rc == LDAP_SUCCESS ) {
    846  1.1.1.2  lukem 					*ptr->trunc = ptr->savech;
    847  1.1.1.2  lukem 					FILL_PATH( &fpath, dir_end, BVL_NAME( ptr ));
    848  1.1.1.2  lukem 
    849  1.1.1.2  lukem 					rc = ldif_read_entry( op, fpath.bv_val, &dn, &ndn,
    850  1.1.1.2  lukem 						&e, text );
    851  1.1.1.2  lukem 					switch ( rc ) {
    852  1.1.1.2  lukem 					case LDAP_SUCCESS:
    853  1.1.1.2  lukem 						rc = ldif_search_entry( op, rs, e, scope, &fpath );
    854  1.1.1.2  lukem 						break;
    855  1.1.1.2  lukem 					case LDAP_NO_SUCH_OBJECT:
    856  1.1.1.2  lukem 						/* Only the search baseDN may produce noSuchObject. */
    857  1.1.1.2  lukem 						rc = LDAP_OTHER;
    858  1.1.1.2  lukem 						if ( rs != NULL )
    859  1.1.1.2  lukem 							rs->sr_text = "internal error "
    860  1.1.1.2  lukem 								"(did someone just remove an entry file?)";
    861  1.1.1.2  lukem 						Debug( LDAP_DEBUG_ANY, "ldif_search_entry: "
    862  1.1.1.2  lukem 							"file listed in parent directory does not exist: "
    863  1.1.1.2  lukem 							"\"%s\"\n", fpath.bv_val, 0, 0 );
    864  1.1.1.2  lukem 						break;
    865  1.1.1.2  lukem 					}
    866  1.1.1.2  lukem 				}
    867  1.1.1.2  lukem 
    868  1.1.1.2  lukem 				list = ptr->next;
    869  1.1.1.2  lukem 				SLAP_FREE( ptr );
    870  1.1.1.2  lukem 			} while ( list != NULL );
    871  1.1.1.2  lukem 
    872  1.1.1.2  lukem 			if ( !BER_BVISNULL( &fpath ) )
    873  1.1.1.2  lukem 				SLAP_FREE( fpath.bv_val );
    874  1.1.1.2  lukem 		}
    875  1.1.1.2  lukem 	}
    876  1.1.1.2  lukem 
    877  1.1.1.2  lukem  done:
    878  1.1.1.2  lukem 	if ( !BER_BVISEMPTY( &dn ) )
    879  1.1.1.2  lukem 		ber_memfree( dn.bv_val );
    880  1.1.1.2  lukem 	if ( !BER_BVISEMPTY( &ndn ) )
    881  1.1.1.2  lukem 		ber_memfree( ndn.bv_val );
    882  1.1.1.2  lukem 	return rc;
    883  1.1.1.2  lukem }
    884  1.1.1.2  lukem 
    885  1.1.1.2  lukem static int
    886  1.1.1.2  lukem search_tree( Operation *op, SlapReply *rs )
    887  1.1.1.2  lukem {
    888  1.1.1.2  lukem 	int rc = LDAP_SUCCESS;
    889  1.1.1.2  lukem 	Entry *e = NULL;
    890      1.1  lukem 	struct berval path;
    891      1.1  lukem 	struct berval pdn, pndn;
    892      1.1  lukem 
    893  1.1.1.2  lukem 	(void) ndn2path( op, &op->o_req_ndn, &path, 1 );
    894  1.1.1.2  lukem 	if ( !BER_BVISEMPTY( &op->o_req_ndn ) ) {
    895  1.1.1.2  lukem 		/* Read baseObject */
    896  1.1.1.2  lukem 		dnParent( &op->o_req_dn, &pdn );
    897  1.1.1.2  lukem 		dnParent( &op->o_req_ndn, &pndn );
    898  1.1.1.2  lukem 		rc = ldif_read_entry( op, path.bv_val, &pdn, &pndn, &e,
    899  1.1.1.2  lukem 			rs == NULL ? NULL : &rs->sr_text );
    900  1.1.1.2  lukem 	}
    901  1.1.1.2  lukem 	if ( rc == LDAP_SUCCESS )
    902  1.1.1.2  lukem 		rc = ldif_search_entry( op, rs, e, op->ors_scope, &path );
    903  1.1.1.2  lukem 
    904      1.1  lukem 	ch_free( path.bv_val );
    905      1.1  lukem 	return rc;
    906      1.1  lukem }
    907      1.1  lukem 
    908      1.1  lukem 
    909  1.1.1.2  lukem /*
    910  1.1.1.2  lukem  * Prepare to create or rename an entry:
    911  1.1.1.2  lukem  * Check that the entry does not already exist.
    912  1.1.1.2  lukem  * Check that the parent entry exists and can have subordinates,
    913  1.1.1.2  lukem  * unless need_dir is NULL or adding the suffix entry.
    914  1.1.1.2  lukem  *
    915  1.1.1.2  lukem  * Return an LDAP result code.  May set *text to a message on failure.
    916  1.1.1.2  lukem  * If success, set *dnpath to LDIF entry path and *need_dir to
    917  1.1.1.2  lukem  * (directory must be created ? dirname : NULL).
    918  1.1.1.2  lukem  */
    919  1.1.1.2  lukem static int
    920  1.1.1.2  lukem ldif_prepare_create(
    921  1.1.1.2  lukem 	Operation *op,
    922  1.1.1.2  lukem 	Entry *e,
    923  1.1.1.2  lukem 	struct berval *dnpath,
    924  1.1.1.2  lukem 	char **need_dir,
    925  1.1.1.2  lukem 	const char **text )
    926      1.1  lukem {
    927  1.1.1.2  lukem 	struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
    928  1.1.1.2  lukem 	struct berval *ndn = &e->e_nname;
    929  1.1.1.2  lukem 	struct berval ppath = BER_BVNULL;
    930  1.1.1.2  lukem 	struct stat st;
    931  1.1.1.2  lukem 	Entry *parent = NULL;
    932  1.1.1.2  lukem 	int rc;
    933  1.1.1.2  lukem 
    934  1.1.1.2  lukem 	if ( op->o_abandon )
    935  1.1.1.2  lukem 		return SLAPD_ABANDON;
    936  1.1.1.2  lukem 
    937  1.1.1.2  lukem 	rc = ndn2path( op, ndn, dnpath, 0 );
    938  1.1.1.2  lukem 	if ( rc != LDAP_SUCCESS ) {
    939  1.1.1.2  lukem 		return rc;
    940  1.1.1.2  lukem 	}
    941  1.1.1.2  lukem 
    942  1.1.1.2  lukem 	if ( stat( dnpath->bv_val, &st ) == 0 ) { /* entry .ldif file */
    943  1.1.1.2  lukem 		rc = LDAP_ALREADY_EXISTS;
    944  1.1.1.2  lukem 
    945  1.1.1.2  lukem 	} else if ( errno != ENOENT ) {
    946  1.1.1.2  lukem 		Debug( LDAP_DEBUG_ANY,
    947  1.1.1.2  lukem 			"ldif_prepare_create: cannot stat \"%s\": %s\n",
    948  1.1.1.2  lukem 			dnpath->bv_val, STRERROR( errno ), 0 );
    949  1.1.1.2  lukem 		rc = LDAP_OTHER;
    950  1.1.1.2  lukem 		*text = "internal error (cannot check entry file)";
    951  1.1.1.2  lukem 
    952  1.1.1.2  lukem 	} else if ( need_dir != NULL ) {
    953  1.1.1.2  lukem 		*need_dir = NULL;
    954  1.1.1.2  lukem 		rc = get_parent_path( dnpath, &ppath );
    955  1.1.1.2  lukem 		/* If parent dir exists, so does parent .ldif:
    956  1.1.1.2  lukem 		 * The directory gets created after and removed before the .ldif.
    957  1.1.1.2  lukem 		 * Except with the database directory, which has no matching entry.
    958  1.1.1.2  lukem 		 */
    959  1.1.1.2  lukem 		if ( rc == LDAP_SUCCESS && stat( ppath.bv_val, &st ) < 0 ) {
    960  1.1.1.2  lukem 			rc = errno == ENOENT && ppath.bv_len > li->li_base_path.bv_len
    961  1.1.1.2  lukem 				? LDAP_NO_SUCH_OBJECT : LDAP_OTHER;
    962  1.1.1.2  lukem 		}
    963  1.1.1.2  lukem 		switch ( rc ) {
    964  1.1.1.2  lukem 		case LDAP_NO_SUCH_OBJECT:
    965  1.1.1.2  lukem 			/* No parent dir, check parent .ldif */
    966  1.1.1.2  lukem 			dir2ldif_name( ppath );
    967  1.1.1.2  lukem 			rc = ldif_read_entry( op, ppath.bv_val, NULL, NULL,
    968  1.1.1.2  lukem 				(op->o_tag != LDAP_REQ_ADD || get_manageDSAit( op )
    969  1.1.1.2  lukem 				 ? &parent : NULL),
    970  1.1.1.2  lukem 				text );
    971  1.1.1.2  lukem 			switch ( rc ) {
    972  1.1.1.2  lukem 			case LDAP_SUCCESS:
    973  1.1.1.2  lukem 				/* Check that parent is not a referral, unless
    974  1.1.1.2  lukem 				 * ldif_back_referrals() already checked.
    975  1.1.1.2  lukem 				 */
    976  1.1.1.2  lukem 				if ( parent != NULL ) {
    977  1.1.1.2  lukem 					int is_ref = is_entry_referral( parent );
    978  1.1.1.2  lukem 					entry_free( parent );
    979  1.1.1.2  lukem 					if ( is_ref ) {
    980  1.1.1.2  lukem 						rc = LDAP_AFFECTS_MULTIPLE_DSAS;
    981  1.1.1.2  lukem 						*text = op->o_tag == LDAP_REQ_MODDN
    982  1.1.1.2  lukem 							? "newSuperior is a referral object"
    983  1.1.1.2  lukem 							: "parent is a referral object";
    984  1.1.1.2  lukem 						break;
    985  1.1.1.2  lukem 					}
    986  1.1.1.2  lukem 				}
    987  1.1.1.2  lukem 				/* Must create parent directory. */
    988  1.1.1.2  lukem 				ldif2dir_name( ppath );
    989  1.1.1.2  lukem 				*need_dir = ppath.bv_val;
    990  1.1.1.2  lukem 				break;
    991  1.1.1.2  lukem 			case LDAP_NO_SUCH_OBJECT:
    992  1.1.1.2  lukem 				*text = op->o_tag == LDAP_REQ_MODDN
    993  1.1.1.2  lukem 					? "newSuperior object does not exist"
    994  1.1.1.2  lukem 					: "parent does not exist";
    995  1.1.1.2  lukem 				break;
    996  1.1.1.2  lukem 			}
    997      1.1  lukem 			break;
    998  1.1.1.2  lukem 		case LDAP_OTHER:
    999  1.1.1.2  lukem 			Debug( LDAP_DEBUG_ANY,
   1000  1.1.1.2  lukem 				"ldif_prepare_create: cannot stat \"%s\" parent dir: %s\n",
   1001  1.1.1.2  lukem 				ndn->bv_val, STRERROR( errno ), 0 );
   1002  1.1.1.2  lukem 			*text = "internal error (cannot stat parent dir)";
   1003  1.1.1.2  lukem 			break;
   1004  1.1.1.2  lukem 		}
   1005  1.1.1.2  lukem 		if ( *need_dir == NULL && ppath.bv_val != NULL )
   1006  1.1.1.2  lukem 			SLAP_FREE( ppath.bv_val );
   1007  1.1.1.2  lukem 	}
   1008  1.1.1.2  lukem 
   1009  1.1.1.2  lukem 	if ( rc != LDAP_SUCCESS ) {
   1010  1.1.1.2  lukem 		SLAP_FREE( dnpath->bv_val );
   1011  1.1.1.2  lukem 		BER_BVZERO( dnpath );
   1012  1.1.1.2  lukem 	}
   1013  1.1.1.2  lukem 	return rc;
   1014      1.1  lukem }
   1015      1.1  lukem 
   1016  1.1.1.2  lukem static int
   1017  1.1.1.2  lukem apply_modify_to_entry(
   1018  1.1.1.2  lukem 	Entry *entry,
   1019  1.1.1.2  lukem 	Modifications *modlist,
   1020  1.1.1.2  lukem 	Operation *op,
   1021  1.1.1.2  lukem 	SlapReply *rs )
   1022      1.1  lukem {
   1023      1.1  lukem 	char textbuf[SLAP_TEXT_BUFLEN];
   1024      1.1  lukem 	int rc = modlist ? LDAP_UNWILLING_TO_PERFORM : LDAP_SUCCESS;
   1025      1.1  lukem 	int is_oc = 0;
   1026      1.1  lukem 	Modification *mods;
   1027      1.1  lukem 
   1028      1.1  lukem 	if (!acl_check_modlist(op, entry, modlist)) {
   1029      1.1  lukem 		return LDAP_INSUFFICIENT_ACCESS;
   1030      1.1  lukem 	}
   1031      1.1  lukem 
   1032      1.1  lukem 	for (; modlist != NULL; modlist = modlist->sml_next) {
   1033      1.1  lukem 		mods = &modlist->sml_mod;
   1034      1.1  lukem 
   1035      1.1  lukem 		if ( mods->sm_desc == slap_schema.si_ad_objectClass ) {
   1036      1.1  lukem 			is_oc = 1;
   1037      1.1  lukem 		}
   1038      1.1  lukem 		switch (mods->sm_op) {
   1039      1.1  lukem 		case LDAP_MOD_ADD:
   1040      1.1  lukem 			rc = modify_add_values(entry, mods,
   1041      1.1  lukem 				   get_permissiveModify(op),
   1042      1.1  lukem 				   &rs->sr_text, textbuf,
   1043      1.1  lukem 				   sizeof( textbuf ) );
   1044      1.1  lukem 			break;
   1045  1.1.1.2  lukem 
   1046      1.1  lukem 		case LDAP_MOD_DELETE:
   1047      1.1  lukem 			rc = modify_delete_values(entry, mods,
   1048      1.1  lukem 				get_permissiveModify(op),
   1049      1.1  lukem 				&rs->sr_text, textbuf,
   1050      1.1  lukem 				sizeof( textbuf ) );
   1051      1.1  lukem 			break;
   1052  1.1.1.2  lukem 
   1053      1.1  lukem 		case LDAP_MOD_REPLACE:
   1054      1.1  lukem 			rc = modify_replace_values(entry, mods,
   1055      1.1  lukem 				 get_permissiveModify(op),
   1056      1.1  lukem 				 &rs->sr_text, textbuf,
   1057      1.1  lukem 				 sizeof( textbuf ) );
   1058      1.1  lukem 			break;
   1059      1.1  lukem 
   1060      1.1  lukem 		case LDAP_MOD_INCREMENT:
   1061      1.1  lukem 			rc = modify_increment_values( entry,
   1062      1.1  lukem 				mods, get_permissiveModify(op),
   1063      1.1  lukem 				&rs->sr_text, textbuf,
   1064      1.1  lukem 				sizeof( textbuf ) );
   1065      1.1  lukem 			break;
   1066      1.1  lukem 
   1067      1.1  lukem 		case SLAP_MOD_SOFTADD:
   1068      1.1  lukem 			mods->sm_op = LDAP_MOD_ADD;
   1069      1.1  lukem 			rc = modify_add_values(entry, mods,
   1070      1.1  lukem 				   get_permissiveModify(op),
   1071      1.1  lukem 				   &rs->sr_text, textbuf,
   1072      1.1  lukem 				   sizeof( textbuf ) );
   1073      1.1  lukem 			mods->sm_op = SLAP_MOD_SOFTADD;
   1074      1.1  lukem 			if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
   1075      1.1  lukem 				rc = LDAP_SUCCESS;
   1076      1.1  lukem 			}
   1077      1.1  lukem 			break;
   1078      1.1  lukem 		}
   1079      1.1  lukem 		if(rc != LDAP_SUCCESS) break;
   1080      1.1  lukem 	}
   1081      1.1  lukem 
   1082  1.1.1.2  lukem 	if ( rc == LDAP_SUCCESS ) {
   1083  1.1.1.2  lukem 		rs->sr_text = NULL; /* Needed at least with SLAP_MOD_SOFTADD */
   1084      1.1  lukem 		if ( is_oc ) {
   1085      1.1  lukem 			entry->e_ocflags = 0;
   1086      1.1  lukem 		}
   1087      1.1  lukem 		/* check that the entry still obeys the schema */
   1088  1.1.1.2  lukem 		rc = entry_schema_check( op, entry, NULL, 0, 0, NULL,
   1089      1.1  lukem 			  &rs->sr_text, textbuf, sizeof( textbuf ) );
   1090      1.1  lukem 	}
   1091      1.1  lukem 
   1092      1.1  lukem 	return rc;
   1093      1.1  lukem }
   1094      1.1  lukem 
   1095  1.1.1.2  lukem 
   1096  1.1.1.2  lukem static int
   1097      1.1  lukem ldif_back_referrals( Operation *op, SlapReply *rs )
   1098      1.1  lukem {
   1099  1.1.1.2  lukem 	struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
   1100  1.1.1.2  lukem 	struct berval path, dn = op->o_req_dn, ndn = op->o_req_ndn;
   1101  1.1.1.2  lukem 	ber_len_t min_dnlen;
   1102  1.1.1.2  lukem 	Entry *entry = NULL, **entryp;
   1103  1.1.1.2  lukem 	BerVarray ref;
   1104  1.1.1.2  lukem 	int rc;
   1105      1.1  lukem 
   1106  1.1.1.2  lukem 	min_dnlen = op->o_bd->be_nsuffix[0].bv_len;
   1107  1.1.1.2  lukem 	if ( min_dnlen == 0 ) {
   1108  1.1.1.2  lukem 		/* Catch root DSE (empty DN), it is not a referral */
   1109  1.1.1.2  lukem 		min_dnlen = 1;
   1110      1.1  lukem 	}
   1111  1.1.1.2  lukem 	if ( ndn2path( op, &ndn, &path, 0 ) != LDAP_SUCCESS ) {
   1112  1.1.1.2  lukem 		return LDAP_SUCCESS;	/* Root DSE again */
   1113      1.1  lukem 	}
   1114      1.1  lukem 
   1115  1.1.1.2  lukem 	entryp = get_manageDSAit( op ) ? NULL : &entry;
   1116      1.1  lukem 	ldap_pvt_thread_rdwr_rlock( &li->li_rdwr );
   1117      1.1  lukem 
   1118  1.1.1.2  lukem 	for (;;) {
   1119  1.1.1.2  lukem 		dnParent( &dn, &dn );
   1120  1.1.1.2  lukem 		dnParent( &ndn, &ndn );
   1121  1.1.1.2  lukem 		rc = ldif_read_entry( op, path.bv_val, &dn, &ndn,
   1122  1.1.1.2  lukem 			entryp, &rs->sr_text );
   1123  1.1.1.2  lukem 		if ( rc != LDAP_NO_SUCH_OBJECT )
   1124  1.1.1.2  lukem 			break;
   1125      1.1  lukem 
   1126  1.1.1.2  lukem 		rc = LDAP_SUCCESS;
   1127  1.1.1.2  lukem 		if ( ndn.bv_len < min_dnlen )
   1128  1.1.1.2  lukem 			break;
   1129  1.1.1.2  lukem 		(void) get_parent_path( &path, NULL );
   1130  1.1.1.2  lukem 		dir2ldif_name( path );
   1131  1.1.1.2  lukem 		entryp = &entry;
   1132  1.1.1.2  lukem 	}
   1133      1.1  lukem 
   1134  1.1.1.2  lukem 	ldap_pvt_thread_rdwr_runlock( &li->li_rdwr );
   1135  1.1.1.2  lukem 	SLAP_FREE( path.bv_val );
   1136      1.1  lukem 
   1137  1.1.1.2  lukem 	if ( entry != NULL ) {
   1138  1.1.1.2  lukem 		if ( is_entry_referral( entry ) ) {
   1139      1.1  lukem 			Debug( LDAP_DEBUG_TRACE,
   1140      1.1  lukem 				"ldif_back_referrals: tag=%lu target=\"%s\" matched=\"%s\"\n",
   1141  1.1.1.2  lukem 				(unsigned long) op->o_tag, op->o_req_dn.bv_val, entry->e_dn );
   1142      1.1  lukem 
   1143  1.1.1.2  lukem 			ref = get_entry_referrals( op, entry );
   1144  1.1.1.2  lukem 			rs->sr_ref = referral_rewrite( ref, &entry->e_name, &op->o_req_dn,
   1145  1.1.1.2  lukem 				op->o_tag == LDAP_REQ_SEARCH ?
   1146  1.1.1.2  lukem 				op->ors_scope : LDAP_SCOPE_DEFAULT );
   1147  1.1.1.2  lukem 			ber_bvarray_free( ref );
   1148  1.1.1.2  lukem 
   1149  1.1.1.2  lukem 			if ( rs->sr_ref != NULL ) {
   1150  1.1.1.2  lukem 				/* send referral */
   1151  1.1.1.2  lukem 				rc = rs->sr_err = LDAP_REFERRAL;
   1152  1.1.1.2  lukem 				rs->sr_matched = entry->e_dn;
   1153  1.1.1.2  lukem 				send_ldap_result( op, rs );
   1154  1.1.1.2  lukem 				ber_bvarray_free( rs->sr_ref );
   1155  1.1.1.2  lukem 				rs->sr_ref = NULL;
   1156  1.1.1.2  lukem 			} else {
   1157      1.1  lukem 				rc = LDAP_OTHER;
   1158  1.1.1.2  lukem 				rs->sr_text = "bad referral object";
   1159      1.1  lukem 			}
   1160      1.1  lukem 			rs->sr_matched = NULL;
   1161      1.1  lukem 		}
   1162      1.1  lukem 
   1163  1.1.1.2  lukem 		entry_free( entry );
   1164      1.1  lukem 	}
   1165      1.1  lukem 
   1166      1.1  lukem 	return rc;
   1167      1.1  lukem }
   1168      1.1  lukem 
   1169      1.1  lukem 
   1170      1.1  lukem /* LDAP operations */
   1171      1.1  lukem 
   1172      1.1  lukem static int
   1173      1.1  lukem ldif_back_bind( Operation *op, SlapReply *rs )
   1174      1.1  lukem {
   1175      1.1  lukem 	struct ldif_info *li;
   1176      1.1  lukem 	Attribute *a;
   1177      1.1  lukem 	AttributeDescription *password = slap_schema.si_ad_userPassword;
   1178      1.1  lukem 	int return_val;
   1179  1.1.1.2  lukem 	Entry *entry = NULL;
   1180      1.1  lukem 
   1181      1.1  lukem 	switch ( be_rootdn_bind( op, rs ) ) {
   1182      1.1  lukem 	case SLAP_CB_CONTINUE:
   1183      1.1  lukem 		break;
   1184      1.1  lukem 
   1185      1.1  lukem 	default:
   1186      1.1  lukem 		/* in case of success, front end will send result;
   1187      1.1  lukem 		 * otherwise, be_rootdn_bind() did */
   1188      1.1  lukem 		return rs->sr_err;
   1189      1.1  lukem 	}
   1190      1.1  lukem 
   1191      1.1  lukem 	li = (struct ldif_info *) op->o_bd->be_private;
   1192      1.1  lukem 	ldap_pvt_thread_rdwr_rlock(&li->li_rdwr);
   1193  1.1.1.2  lukem 	return_val = get_entry(op, &entry, NULL, NULL);
   1194      1.1  lukem 
   1195      1.1  lukem 	/* no object is found for them */
   1196      1.1  lukem 	if(return_val != LDAP_SUCCESS) {
   1197      1.1  lukem 		rs->sr_err = return_val = LDAP_INVALID_CREDENTIALS;
   1198      1.1  lukem 		goto return_result;
   1199      1.1  lukem 	}
   1200      1.1  lukem 
   1201      1.1  lukem 	/* they don't have userpassword */
   1202      1.1  lukem 	if((a = attr_find(entry->e_attrs, password)) == NULL) {
   1203      1.1  lukem 		rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
   1204      1.1  lukem 		return_val = 1;
   1205      1.1  lukem 		goto return_result;
   1206      1.1  lukem 	}
   1207      1.1  lukem 
   1208      1.1  lukem 	/* authentication actually failed */
   1209      1.1  lukem 	if(slap_passwd_check(op, entry, a, &op->oq_bind.rb_cred,
   1210      1.1  lukem 			     &rs->sr_text) != 0) {
   1211      1.1  lukem 		rs->sr_err = LDAP_INVALID_CREDENTIALS;
   1212      1.1  lukem 		return_val = 1;
   1213      1.1  lukem 		goto return_result;
   1214      1.1  lukem 	}
   1215      1.1  lukem 
   1216      1.1  lukem 	/* let the front-end send success */
   1217  1.1.1.2  lukem 	return_val = LDAP_SUCCESS;
   1218      1.1  lukem 
   1219      1.1  lukem  return_result:
   1220      1.1  lukem 	ldap_pvt_thread_rdwr_runlock(&li->li_rdwr);
   1221  1.1.1.2  lukem 	if(return_val != LDAP_SUCCESS)
   1222      1.1  lukem 		send_ldap_result( op, rs );
   1223      1.1  lukem 	if(entry != NULL)
   1224      1.1  lukem 		entry_free(entry);
   1225      1.1  lukem 	return return_val;
   1226      1.1  lukem }
   1227      1.1  lukem 
   1228  1.1.1.2  lukem static int
   1229  1.1.1.2  lukem ldif_back_search( Operation *op, SlapReply *rs )
   1230      1.1  lukem {
   1231      1.1  lukem 	struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
   1232      1.1  lukem 
   1233      1.1  lukem 	ldap_pvt_thread_rdwr_rlock(&li->li_rdwr);
   1234  1.1.1.2  lukem 	rs->sr_err = search_tree( op, rs );
   1235      1.1  lukem 	ldap_pvt_thread_rdwr_runlock(&li->li_rdwr);
   1236      1.1  lukem 	send_ldap_result(op, rs);
   1237      1.1  lukem 
   1238      1.1  lukem 	return rs->sr_err;
   1239      1.1  lukem }
   1240      1.1  lukem 
   1241  1.1.1.2  lukem static int
   1242  1.1.1.2  lukem ldif_back_add( Operation *op, SlapReply *rs )
   1243  1.1.1.2  lukem {
   1244      1.1  lukem 	struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
   1245      1.1  lukem 	Entry * e = op->ora_e;
   1246  1.1.1.2  lukem 	struct berval path;
   1247  1.1.1.2  lukem 	char *parentdir;
   1248      1.1  lukem 	char textbuf[SLAP_TEXT_BUFLEN];
   1249  1.1.1.2  lukem 	int rc;
   1250      1.1  lukem 
   1251  1.1.1.2  lukem 	Debug( LDAP_DEBUG_TRACE, "ldif_back_add: \"%s\"\n", e->e_dn, 0, 0 );
   1252      1.1  lukem 
   1253  1.1.1.2  lukem 	rc = entry_schema_check( op, e, NULL, 0, 1, NULL,
   1254      1.1  lukem 		&rs->sr_text, textbuf, sizeof( textbuf ) );
   1255  1.1.1.2  lukem 	if ( rc != LDAP_SUCCESS )
   1256  1.1.1.2  lukem 		goto send_res;
   1257      1.1  lukem 
   1258  1.1.1.2  lukem 	rc = slap_add_opattrs( op, &rs->sr_text, textbuf, sizeof( textbuf ), 1 );
   1259  1.1.1.2  lukem 	if ( rc != LDAP_SUCCESS )
   1260  1.1.1.2  lukem 		goto send_res;
   1261  1.1.1.2  lukem 
   1262  1.1.1.2  lukem 	ldap_pvt_thread_mutex_lock( &li->li_modop_mutex );
   1263  1.1.1.2  lukem 
   1264  1.1.1.2  lukem 	rc = ldif_prepare_create( op, e, &path, &parentdir, &rs->sr_text );
   1265  1.1.1.2  lukem 	if ( rc == LDAP_SUCCESS ) {
   1266  1.1.1.2  lukem 		ldap_pvt_thread_rdwr_wlock( &li->li_rdwr );
   1267  1.1.1.2  lukem 		rc = ldif_write_entry( op, e, &path, parentdir, &rs->sr_text );
   1268  1.1.1.2  lukem 		ldap_pvt_thread_rdwr_wunlock( &li->li_rdwr );
   1269  1.1.1.2  lukem 
   1270  1.1.1.2  lukem 		SLAP_FREE( path.bv_val );
   1271  1.1.1.2  lukem 		if ( parentdir != NULL )
   1272  1.1.1.2  lukem 			SLAP_FREE( parentdir );
   1273      1.1  lukem 	}
   1274      1.1  lukem 
   1275  1.1.1.2  lukem 	ldap_pvt_thread_mutex_unlock( &li->li_modop_mutex );
   1276      1.1  lukem 
   1277  1.1.1.2  lukem  send_res:
   1278  1.1.1.2  lukem 	rs->sr_err = rc;
   1279  1.1.1.2  lukem 	Debug( LDAP_DEBUG_TRACE, "ldif_back_add: err: %d text: %s\n",
   1280  1.1.1.2  lukem 		rc, rs->sr_text ? rs->sr_text : "", 0 );
   1281  1.1.1.2  lukem 	send_ldap_result( op, rs );
   1282      1.1  lukem 	slap_graduate_commit_csn( op );
   1283      1.1  lukem 	return rs->sr_err;
   1284      1.1  lukem }
   1285      1.1  lukem 
   1286  1.1.1.2  lukem static int
   1287  1.1.1.2  lukem ldif_back_modify( Operation *op, SlapReply *rs )
   1288  1.1.1.2  lukem {
   1289      1.1  lukem 	struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
   1290      1.1  lukem 	Modifications * modlst = op->orm_modlist;
   1291      1.1  lukem 	struct berval path;
   1292      1.1  lukem 	Entry *entry;
   1293  1.1.1.2  lukem 	int rc;
   1294      1.1  lukem 
   1295      1.1  lukem 	slap_mods_opattrs( op, &op->orm_modlist, 1 );
   1296      1.1  lukem 
   1297  1.1.1.2  lukem 	ldap_pvt_thread_mutex_lock( &li->li_modop_mutex );
   1298      1.1  lukem 
   1299  1.1.1.2  lukem 	rc = get_entry( op, &entry, &path, &rs->sr_text );
   1300  1.1.1.2  lukem 	if ( rc == LDAP_SUCCESS ) {
   1301  1.1.1.2  lukem 		rc = apply_modify_to_entry( entry, modlst, op, rs );
   1302  1.1.1.2  lukem 		if ( rc == LDAP_SUCCESS ) {
   1303  1.1.1.2  lukem 			ldap_pvt_thread_rdwr_wlock( &li->li_rdwr );
   1304  1.1.1.2  lukem 			rc = ldif_write_entry( op, entry, &path, NULL, &rs->sr_text );
   1305  1.1.1.2  lukem 			ldap_pvt_thread_rdwr_wunlock( &li->li_rdwr );
   1306      1.1  lukem 		}
   1307      1.1  lukem 
   1308      1.1  lukem 		entry_free( entry );
   1309      1.1  lukem 		SLAP_FREE( path.bv_val );
   1310      1.1  lukem 	}
   1311      1.1  lukem 
   1312  1.1.1.2  lukem 	ldap_pvt_thread_mutex_unlock( &li->li_modop_mutex );
   1313  1.1.1.2  lukem 
   1314  1.1.1.2  lukem 	rs->sr_err = rc;
   1315  1.1.1.2  lukem 	send_ldap_result( op, rs );
   1316      1.1  lukem 	slap_graduate_commit_csn( op );
   1317      1.1  lukem 	return rs->sr_err;
   1318      1.1  lukem }
   1319      1.1  lukem 
   1320  1.1.1.2  lukem static int
   1321  1.1.1.2  lukem ldif_back_delete( Operation *op, SlapReply *rs )
   1322  1.1.1.2  lukem {
   1323      1.1  lukem 	struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
   1324      1.1  lukem 	struct berval path;
   1325  1.1.1.2  lukem 	int rc = LDAP_SUCCESS;
   1326      1.1  lukem 
   1327      1.1  lukem 	if ( BER_BVISEMPTY( &op->o_csn )) {
   1328      1.1  lukem 		struct berval csn;
   1329  1.1.1.3   adam 		char csnbuf[LDAP_PVT_CSNSTR_BUFSIZE];
   1330      1.1  lukem 
   1331      1.1  lukem 		csn.bv_val = csnbuf;
   1332      1.1  lukem 		csn.bv_len = sizeof( csnbuf );
   1333      1.1  lukem 		slap_get_csn( op, &csn, 1 );
   1334      1.1  lukem 	}
   1335      1.1  lukem 
   1336  1.1.1.2  lukem 	ldap_pvt_thread_mutex_lock( &li->li_modop_mutex );
   1337  1.1.1.2  lukem 	ldap_pvt_thread_rdwr_wlock( &li->li_rdwr );
   1338  1.1.1.2  lukem 	if ( op->o_abandon ) {
   1339  1.1.1.2  lukem 		rc = SLAPD_ABANDON;
   1340  1.1.1.2  lukem 		goto done;
   1341  1.1.1.2  lukem 	}
   1342  1.1.1.2  lukem 
   1343  1.1.1.2  lukem 	rc = ndn2path( op, &op->o_req_ndn, &path, 0 );
   1344  1.1.1.2  lukem 	if ( rc != LDAP_SUCCESS ) {
   1345  1.1.1.2  lukem 		goto done;
   1346  1.1.1.2  lukem 	}
   1347      1.1  lukem 
   1348  1.1.1.2  lukem 	ldif2dir_len( path );
   1349  1.1.1.2  lukem 	ldif2dir_name( path );
   1350  1.1.1.2  lukem 	if ( rmdir( path.bv_val ) < 0 ) {
   1351      1.1  lukem 		switch ( errno ) {
   1352      1.1  lukem 		case ENOTEMPTY:
   1353  1.1.1.2  lukem 			rc = LDAP_NOT_ALLOWED_ON_NONLEAF;
   1354      1.1  lukem 			break;
   1355      1.1  lukem 		case ENOENT:
   1356      1.1  lukem 			/* is leaf, go on */
   1357      1.1  lukem 			break;
   1358      1.1  lukem 		default:
   1359  1.1.1.2  lukem 			rc = LDAP_OTHER;
   1360  1.1.1.2  lukem 			rs->sr_text = "internal error (cannot delete subtree directory)";
   1361      1.1  lukem 			break;
   1362      1.1  lukem 		}
   1363      1.1  lukem 	}
   1364      1.1  lukem 
   1365  1.1.1.2  lukem 	if ( rc == LDAP_SUCCESS ) {
   1366  1.1.1.2  lukem 		dir2ldif_name( path );
   1367  1.1.1.2  lukem 		if ( unlink( path.bv_val ) < 0 ) {
   1368  1.1.1.2  lukem 			rc = LDAP_NO_SUCH_OBJECT;
   1369  1.1.1.2  lukem 			if ( errno != ENOENT ) {
   1370  1.1.1.2  lukem 				rc = LDAP_OTHER;
   1371  1.1.1.2  lukem 				rs->sr_text = "internal error (cannot delete entry file)";
   1372      1.1  lukem 			}
   1373      1.1  lukem 		}
   1374      1.1  lukem 	}
   1375      1.1  lukem 
   1376  1.1.1.2  lukem 	if ( rc == LDAP_OTHER ) {
   1377  1.1.1.2  lukem 		Debug( LDAP_DEBUG_ANY, "ldif_back_delete: %s \"%s\": %s\n",
   1378  1.1.1.2  lukem 			"cannot delete", path.bv_val, STRERROR( errno ) );
   1379  1.1.1.2  lukem 	}
   1380  1.1.1.2  lukem 
   1381  1.1.1.2  lukem 	SLAP_FREE( path.bv_val );
   1382  1.1.1.2  lukem  done:
   1383  1.1.1.2  lukem 	ldap_pvt_thread_rdwr_wunlock( &li->li_rdwr );
   1384  1.1.1.2  lukem 	ldap_pvt_thread_mutex_unlock( &li->li_modop_mutex );
   1385  1.1.1.2  lukem 	rs->sr_err = rc;
   1386  1.1.1.2  lukem 	send_ldap_result( op, rs );
   1387      1.1  lukem 	slap_graduate_commit_csn( op );
   1388      1.1  lukem 	return rs->sr_err;
   1389      1.1  lukem }
   1390      1.1  lukem 
   1391      1.1  lukem 
   1392      1.1  lukem static int
   1393      1.1  lukem ldif_move_entry(
   1394      1.1  lukem 	Operation *op,
   1395      1.1  lukem 	Entry *entry,
   1396  1.1.1.2  lukem 	int same_ndn,
   1397  1.1.1.2  lukem 	struct berval *oldpath,
   1398  1.1.1.2  lukem 	const char **text )
   1399      1.1  lukem {
   1400  1.1.1.2  lukem 	struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
   1401      1.1  lukem 	struct berval newpath;
   1402  1.1.1.2  lukem 	char *parentdir = NULL, *trash;
   1403  1.1.1.2  lukem 	int rc, rename_res;
   1404      1.1  lukem 
   1405  1.1.1.2  lukem 	if ( same_ndn ) {
   1406  1.1.1.2  lukem 		rc = LDAP_SUCCESS;
   1407  1.1.1.2  lukem 		newpath = *oldpath;
   1408  1.1.1.2  lukem 	} else {
   1409  1.1.1.2  lukem 		rc = ldif_prepare_create( op, entry, &newpath,
   1410  1.1.1.2  lukem 			op->orr_newSup ? &parentdir : NULL, text );
   1411  1.1.1.2  lukem 	}
   1412      1.1  lukem 
   1413  1.1.1.2  lukem 	if ( rc == LDAP_SUCCESS ) {
   1414  1.1.1.2  lukem 		ldap_pvt_thread_rdwr_wlock( &li->li_rdwr );
   1415  1.1.1.2  lukem 
   1416  1.1.1.2  lukem 		rc = ldif_write_entry( op, entry, &newpath, parentdir, text );
   1417  1.1.1.2  lukem 		if ( rc == LDAP_SUCCESS && !same_ndn ) {
   1418  1.1.1.2  lukem 			trash = oldpath->bv_val; /* will be .ldif file to delete */
   1419  1.1.1.2  lukem 			ldif2dir_len( newpath );
   1420  1.1.1.2  lukem 			ldif2dir_len( *oldpath );
   1421  1.1.1.2  lukem 			/* Move subdir before deleting old entry,
   1422  1.1.1.2  lukem 			 * so .ldif always exists if subdir does.
   1423  1.1.1.2  lukem 			 */
   1424  1.1.1.2  lukem 			ldif2dir_name( newpath );
   1425  1.1.1.2  lukem 			ldif2dir_name( *oldpath );
   1426  1.1.1.2  lukem 			rename_res = move_dir( oldpath->bv_val, newpath.bv_val );
   1427  1.1.1.2  lukem 			if ( rename_res != 0 && errno != ENOENT ) {
   1428  1.1.1.2  lukem 				rc = LDAP_OTHER;
   1429  1.1.1.2  lukem 				*text = "internal error (cannot move this subtree)";
   1430  1.1.1.2  lukem 				trash = newpath.bv_val;
   1431      1.1  lukem 			}
   1432  1.1.1.2  lukem 
   1433  1.1.1.2  lukem 			/* Delete old entry, or if error undo change */
   1434  1.1.1.2  lukem 			for (;;) {
   1435  1.1.1.2  lukem 				dir2ldif_name( newpath );
   1436  1.1.1.2  lukem 				dir2ldif_name( *oldpath );
   1437  1.1.1.2  lukem 				if ( unlink( trash ) == 0 )
   1438  1.1.1.2  lukem 					break;
   1439  1.1.1.2  lukem 				if ( rc == LDAP_SUCCESS ) {
   1440  1.1.1.2  lukem 					/* Prepare to undo change and return failure */
   1441  1.1.1.2  lukem 					rc = LDAP_OTHER;
   1442  1.1.1.2  lukem 					*text = "internal error (cannot move this entry)";
   1443  1.1.1.2  lukem 					trash = newpath.bv_val;
   1444  1.1.1.2  lukem 					if ( rename_res != 0 )
   1445  1.1.1.2  lukem 						continue;
   1446  1.1.1.2  lukem 					/* First move subdirectory back */
   1447  1.1.1.2  lukem 					ldif2dir_name( newpath );
   1448  1.1.1.2  lukem 					ldif2dir_name( *oldpath );
   1449  1.1.1.2  lukem 					if ( move_dir( newpath.bv_val, oldpath->bv_val ) == 0 )
   1450  1.1.1.2  lukem 						continue;
   1451  1.1.1.2  lukem 				}
   1452  1.1.1.2  lukem 				*text = "added new but couldn't delete old entry!";
   1453  1.1.1.2  lukem 				break;
   1454      1.1  lukem 			}
   1455  1.1.1.2  lukem 
   1456  1.1.1.2  lukem 			if ( rc != LDAP_SUCCESS ) {
   1457  1.1.1.2  lukem 				char s[128];
   1458  1.1.1.2  lukem 				snprintf( s, sizeof s, "%s (%s)", *text, STRERROR( errno ));
   1459  1.1.1.2  lukem 				Debug( LDAP_DEBUG_ANY,
   1460  1.1.1.2  lukem 					"ldif_move_entry: %s: \"%s\" -> \"%s\"\n",
   1461  1.1.1.2  lukem 					s, op->o_req_dn.bv_val, entry->e_dn );
   1462      1.1  lukem 			}
   1463      1.1  lukem 		}
   1464  1.1.1.2  lukem 
   1465  1.1.1.2  lukem 		ldap_pvt_thread_rdwr_wunlock( &li->li_rdwr );
   1466  1.1.1.2  lukem 		if ( !same_ndn )
   1467  1.1.1.2  lukem 			SLAP_FREE( newpath.bv_val );
   1468  1.1.1.2  lukem 		if ( parentdir != NULL )
   1469  1.1.1.2  lukem 			SLAP_FREE( parentdir );
   1470      1.1  lukem 	}
   1471      1.1  lukem 
   1472  1.1.1.2  lukem 	return rc;
   1473      1.1  lukem }
   1474      1.1  lukem 
   1475      1.1  lukem static int
   1476  1.1.1.2  lukem ldif_back_modrdn( Operation *op, SlapReply *rs )
   1477      1.1  lukem {
   1478      1.1  lukem 	struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
   1479      1.1  lukem 	struct berval new_dn = BER_BVNULL, new_ndn = BER_BVNULL;
   1480      1.1  lukem 	struct berval p_dn, old_path;
   1481      1.1  lukem 	Entry *entry;
   1482  1.1.1.2  lukem 	int rc, same_ndn;
   1483      1.1  lukem 
   1484      1.1  lukem 	slap_mods_opattrs( op, &op->orr_modlist, 1 );
   1485      1.1  lukem 
   1486  1.1.1.2  lukem 	ldap_pvt_thread_mutex_lock( &li->li_modop_mutex );
   1487      1.1  lukem 
   1488  1.1.1.2  lukem 	rc = get_entry( op, &entry, &old_path, &rs->sr_text );
   1489      1.1  lukem 	if ( rc == LDAP_SUCCESS ) {
   1490      1.1  lukem 		/* build new dn, and new ndn for the entry */
   1491      1.1  lukem 		if ( op->oq_modrdn.rs_newSup != NULL ) {
   1492      1.1  lukem 			p_dn = *op->oq_modrdn.rs_newSup;
   1493      1.1  lukem 		} else {
   1494      1.1  lukem 			dnParent( &entry->e_name, &p_dn );
   1495      1.1  lukem 		}
   1496  1.1.1.2  lukem 		build_new_dn( &new_dn, &p_dn, &op->oq_modrdn.rs_newrdn, NULL );
   1497      1.1  lukem 		dnNormalize( 0, NULL, NULL, &new_dn, &new_ndn, NULL );
   1498  1.1.1.2  lukem 		same_ndn = !ber_bvcmp( &entry->e_nname, &new_ndn );
   1499      1.1  lukem 		ber_memfree_x( entry->e_name.bv_val, NULL );
   1500      1.1  lukem 		ber_memfree_x( entry->e_nname.bv_val, NULL );
   1501      1.1  lukem 		entry->e_name = new_dn;
   1502      1.1  lukem 		entry->e_nname = new_ndn;
   1503      1.1  lukem 
   1504      1.1  lukem 		/* perform the modifications */
   1505      1.1  lukem 		rc = apply_modify_to_entry( entry, op->orr_modlist, op, rs );
   1506      1.1  lukem 		if ( rc == LDAP_SUCCESS )
   1507  1.1.1.2  lukem 			rc = ldif_move_entry( op, entry, same_ndn, &old_path,
   1508  1.1.1.2  lukem 				&rs->sr_text );
   1509      1.1  lukem 
   1510      1.1  lukem 		entry_free( entry );
   1511      1.1  lukem 		SLAP_FREE( old_path.bv_val );
   1512      1.1  lukem 	}
   1513      1.1  lukem 
   1514  1.1.1.2  lukem 	ldap_pvt_thread_mutex_unlock( &li->li_modop_mutex );
   1515      1.1  lukem 	rs->sr_err = rc;
   1516      1.1  lukem 	send_ldap_result( op, rs );
   1517      1.1  lukem 	slap_graduate_commit_csn( op );
   1518      1.1  lukem 	return rs->sr_err;
   1519      1.1  lukem }
   1520      1.1  lukem 
   1521      1.1  lukem 
   1522      1.1  lukem /* Return LDAP_SUCCESS IFF we retrieve the specified entry. */
   1523      1.1  lukem static int
   1524      1.1  lukem ldif_back_entry_get(
   1525      1.1  lukem 	Operation *op,
   1526      1.1  lukem 	struct berval *ndn,
   1527      1.1  lukem 	ObjectClass *oc,
   1528      1.1  lukem 	AttributeDescription *at,
   1529      1.1  lukem 	int rw,
   1530      1.1  lukem 	Entry **e )
   1531      1.1  lukem {
   1532      1.1  lukem 	struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
   1533      1.1  lukem 	struct berval op_dn = op->o_req_dn, op_ndn = op->o_req_ndn;
   1534      1.1  lukem 	int rc;
   1535      1.1  lukem 
   1536      1.1  lukem 	assert( ndn != NULL );
   1537      1.1  lukem 	assert( !BER_BVISNULL( ndn ) );
   1538      1.1  lukem 
   1539      1.1  lukem 	ldap_pvt_thread_rdwr_rlock( &li->li_rdwr );
   1540      1.1  lukem 	op->o_req_dn = *ndn;
   1541      1.1  lukem 	op->o_req_ndn = *ndn;
   1542  1.1.1.2  lukem 	rc = get_entry( op, e, NULL, NULL );
   1543      1.1  lukem 	op->o_req_dn = op_dn;
   1544      1.1  lukem 	op->o_req_ndn = op_ndn;
   1545      1.1  lukem 	ldap_pvt_thread_rdwr_runlock( &li->li_rdwr );
   1546      1.1  lukem 
   1547      1.1  lukem 	if ( rc == LDAP_SUCCESS && oc && !is_entry_objectclass_or_sub( *e, oc ) ) {
   1548      1.1  lukem 		rc = LDAP_NO_SUCH_ATTRIBUTE;
   1549      1.1  lukem 		entry_free( *e );
   1550      1.1  lukem 		*e = NULL;
   1551      1.1  lukem 	}
   1552      1.1  lukem 
   1553      1.1  lukem 	return rc;
   1554      1.1  lukem }
   1555      1.1  lukem 
   1556      1.1  lukem 
   1557      1.1  lukem /* Slap tools */
   1558      1.1  lukem 
   1559  1.1.1.2  lukem static int
   1560  1.1.1.2  lukem ldif_tool_entry_open( BackendDB *be, int mode )
   1561  1.1.1.2  lukem {
   1562  1.1.1.2  lukem 	struct ldif_tool *tl = &((struct ldif_info *) be->be_private)->li_tool;
   1563      1.1  lukem 
   1564  1.1.1.2  lukem 	tl->ecurrent = 0;
   1565  1.1.1.2  lukem 	return 0;
   1566  1.1.1.2  lukem }
   1567      1.1  lukem 
   1568  1.1.1.2  lukem static int
   1569  1.1.1.2  lukem ldif_tool_entry_close( BackendDB *be )
   1570  1.1.1.2  lukem {
   1571  1.1.1.2  lukem 	struct ldif_tool *tl = &((struct ldif_info *) be->be_private)->li_tool;
   1572  1.1.1.2  lukem 	Entry **entries = tl->entries;
   1573  1.1.1.2  lukem 	ID i;
   1574  1.1.1.2  lukem 
   1575  1.1.1.2  lukem 	for ( i = tl->ecount; i--; )
   1576  1.1.1.2  lukem 		if ( entries[i] )
   1577  1.1.1.2  lukem 			entry_free( entries[i] );
   1578  1.1.1.2  lukem 	SLAP_FREE( entries );
   1579  1.1.1.2  lukem 	tl->entries = NULL;
   1580  1.1.1.2  lukem 	tl->ecount = tl->elen = 0;
   1581      1.1  lukem 	return 0;
   1582      1.1  lukem }
   1583      1.1  lukem 
   1584  1.1.1.2  lukem static ID
   1585  1.1.1.2  lukem ldif_tool_entry_next( BackendDB *be )
   1586      1.1  lukem {
   1587  1.1.1.2  lukem 	struct ldif_tool *tl = &((struct ldif_info *) be->be_private)->li_tool;
   1588  1.1.1.2  lukem 
   1589  1.1.1.3   adam 	do {
   1590  1.1.1.3   adam 		Entry *e = tl->entries[ tl->ecurrent ];
   1591  1.1.1.3   adam 
   1592  1.1.1.3   adam 		if ( tl->ecurrent >= tl->ecount ) {
   1593  1.1.1.3   adam 			return NOID;
   1594  1.1.1.3   adam 		}
   1595  1.1.1.3   adam 
   1596  1.1.1.3   adam 		++tl->ecurrent;
   1597  1.1.1.3   adam 
   1598  1.1.1.3   adam 		if ( tl->tl_base && !dnIsSuffixScope( &e->e_nname, tl->tl_base, tl->tl_scope ) ) {
   1599  1.1.1.3   adam 			continue;
   1600  1.1.1.3   adam 		}
   1601  1.1.1.3   adam 
   1602  1.1.1.3   adam 		if ( tl->tl_filter && test_filter( NULL, e, tl->tl_filter  ) != LDAP_COMPARE_TRUE ) {
   1603  1.1.1.3   adam 			continue;
   1604  1.1.1.3   adam 		}
   1605  1.1.1.3   adam 
   1606  1.1.1.3   adam 		break;
   1607  1.1.1.3   adam 	} while ( 1 );
   1608  1.1.1.3   adam 
   1609  1.1.1.3   adam 	return tl->ecurrent;
   1610      1.1  lukem }
   1611      1.1  lukem 
   1612      1.1  lukem static ID
   1613  1.1.1.3   adam ldif_tool_entry_first_x( BackendDB *be, struct berval *base, int scope, Filter *f )
   1614      1.1  lukem {
   1615  1.1.1.2  lukem 	struct ldif_tool *tl = &((struct ldif_info *) be->be_private)->li_tool;
   1616      1.1  lukem 
   1617  1.1.1.3   adam 	tl->tl_base = base;
   1618  1.1.1.3   adam 	tl->tl_scope = scope;
   1619  1.1.1.3   adam 	tl->tl_filter = f;
   1620  1.1.1.3   adam 
   1621  1.1.1.2  lukem 	if ( tl->entries == NULL ) {
   1622      1.1  lukem 		Operation op = {0};
   1623      1.1  lukem 
   1624      1.1  lukem 		op.o_bd = be;
   1625      1.1  lukem 		op.o_req_dn = *be->be_suffix;
   1626      1.1  lukem 		op.o_req_ndn = *be->be_nsuffix;
   1627      1.1  lukem 		op.ors_scope = LDAP_SCOPE_SUBTREE;
   1628  1.1.1.2  lukem 		if ( search_tree( &op, NULL ) != LDAP_SUCCESS ) {
   1629  1.1.1.2  lukem 			tl->ecurrent = tl->ecount; /* fail ldif_tool_entry_next() */
   1630  1.1.1.2  lukem 			return 0; /* fail ldif_tool_entry_get() */
   1631  1.1.1.2  lukem 		}
   1632      1.1  lukem 	}
   1633      1.1  lukem 	return ldif_tool_entry_next( be );
   1634      1.1  lukem }
   1635      1.1  lukem 
   1636  1.1.1.2  lukem static Entry *
   1637  1.1.1.2  lukem ldif_tool_entry_get( BackendDB *be, ID id )
   1638  1.1.1.2  lukem {
   1639  1.1.1.2  lukem 	struct ldif_tool *tl = &((struct ldif_info *) be->be_private)->li_tool;
   1640  1.1.1.2  lukem 	Entry *e = NULL;
   1641      1.1  lukem 
   1642  1.1.1.2  lukem 	--id;
   1643  1.1.1.2  lukem 	if ( id < tl->ecount ) {
   1644  1.1.1.2  lukem 		e = tl->entries[id];
   1645  1.1.1.2  lukem 		tl->entries[id] = NULL;
   1646      1.1  lukem 	}
   1647  1.1.1.2  lukem 	return e;
   1648      1.1  lukem }
   1649      1.1  lukem 
   1650  1.1.1.2  lukem static ID
   1651  1.1.1.2  lukem ldif_tool_entry_put( BackendDB *be, Entry *e, struct berval *text )
   1652  1.1.1.2  lukem {
   1653  1.1.1.2  lukem 	int rc;
   1654  1.1.1.2  lukem 	const char *errmsg = NULL;
   1655  1.1.1.2  lukem 	struct berval path;
   1656  1.1.1.2  lukem 	char *parentdir;
   1657  1.1.1.2  lukem 	Operation op = {0};
   1658      1.1  lukem 
   1659  1.1.1.2  lukem 	op.o_bd = be;
   1660  1.1.1.2  lukem 	rc = ldif_prepare_create( &op, e, &path, &parentdir, &errmsg );
   1661  1.1.1.2  lukem 	if ( rc == LDAP_SUCCESS ) {
   1662  1.1.1.2  lukem 		rc = ldif_write_entry( &op, e, &path, parentdir, &errmsg );
   1663  1.1.1.2  lukem 
   1664  1.1.1.2  lukem 		SLAP_FREE( path.bv_val );
   1665  1.1.1.2  lukem 		if ( parentdir != NULL )
   1666  1.1.1.2  lukem 			SLAP_FREE( parentdir );
   1667  1.1.1.2  lukem 		if ( rc == LDAP_SUCCESS )
   1668  1.1.1.2  lukem 			return 1;
   1669      1.1  lukem 	}
   1670  1.1.1.2  lukem 
   1671  1.1.1.2  lukem 	if ( errmsg == NULL && rc != LDAP_OTHER )
   1672  1.1.1.2  lukem 		errmsg = ldap_err2string( rc );
   1673  1.1.1.2  lukem 	if ( errmsg != NULL )
   1674  1.1.1.2  lukem 		snprintf( text->bv_val, text->bv_len, "%s", errmsg );
   1675  1.1.1.2  lukem 	return NOID;
   1676      1.1  lukem }
   1677      1.1  lukem 
   1678      1.1  lukem 
   1679      1.1  lukem /* Setup */
   1680      1.1  lukem 
   1681      1.1  lukem static int
   1682      1.1  lukem ldif_back_db_init( BackendDB *be, ConfigReply *cr )
   1683      1.1  lukem {
   1684      1.1  lukem 	struct ldif_info *li;
   1685      1.1  lukem 
   1686      1.1  lukem 	li = ch_calloc( 1, sizeof(struct ldif_info) );
   1687      1.1  lukem 	be->be_private = li;
   1688      1.1  lukem 	be->be_cf_ocs = ldifocs;
   1689  1.1.1.2  lukem 	ldap_pvt_thread_mutex_init( &li->li_modop_mutex );
   1690  1.1.1.2  lukem 	ldap_pvt_thread_rdwr_init( &li->li_rdwr );
   1691      1.1  lukem 	SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_ONE_SUFFIX;
   1692      1.1  lukem 	return 0;
   1693      1.1  lukem }
   1694      1.1  lukem 
   1695      1.1  lukem static int
   1696      1.1  lukem ldif_back_db_destroy( Backend *be, ConfigReply *cr )
   1697      1.1  lukem {
   1698      1.1  lukem 	struct ldif_info *li = be->be_private;
   1699      1.1  lukem 
   1700  1.1.1.2  lukem 	ch_free( li->li_base_path.bv_val );
   1701  1.1.1.2  lukem 	ldap_pvt_thread_rdwr_destroy( &li->li_rdwr );
   1702  1.1.1.2  lukem 	ldap_pvt_thread_mutex_destroy( &li->li_modop_mutex );
   1703      1.1  lukem 	free( be->be_private );
   1704      1.1  lukem 	return 0;
   1705      1.1  lukem }
   1706      1.1  lukem 
   1707      1.1  lukem static int
   1708  1.1.1.2  lukem ldif_back_db_open( Backend *be, ConfigReply *cr )
   1709      1.1  lukem {
   1710      1.1  lukem 	struct ldif_info *li = (struct ldif_info *) be->be_private;
   1711      1.1  lukem 	if( BER_BVISEMPTY(&li->li_base_path)) {/* missing base path */
   1712      1.1  lukem 		Debug( LDAP_DEBUG_ANY, "missing base path for back-ldif\n", 0, 0, 0);
   1713      1.1  lukem 		return 1;
   1714      1.1  lukem 	}
   1715      1.1  lukem 	return 0;
   1716      1.1  lukem }
   1717      1.1  lukem 
   1718      1.1  lukem int
   1719  1.1.1.2  lukem ldif_back_initialize( BackendInfo *bi )
   1720      1.1  lukem {
   1721      1.1  lukem 	static char *controls[] = {
   1722      1.1  lukem 		LDAP_CONTROL_MANAGEDSAIT,
   1723      1.1  lukem 		NULL
   1724      1.1  lukem 	};
   1725      1.1  lukem 	int rc;
   1726      1.1  lukem 
   1727      1.1  lukem 	bi->bi_flags |=
   1728      1.1  lukem 		SLAP_BFLAG_INCREMENT |
   1729      1.1  lukem 		SLAP_BFLAG_REFERRALS;
   1730      1.1  lukem 
   1731      1.1  lukem 	bi->bi_controls = controls;
   1732      1.1  lukem 
   1733      1.1  lukem 	bi->bi_open = 0;
   1734      1.1  lukem 	bi->bi_close = 0;
   1735      1.1  lukem 	bi->bi_config = 0;
   1736      1.1  lukem 	bi->bi_destroy = 0;
   1737      1.1  lukem 
   1738      1.1  lukem 	bi->bi_db_init = ldif_back_db_init;
   1739      1.1  lukem 	bi->bi_db_config = config_generic_wrapper;
   1740      1.1  lukem 	bi->bi_db_open = ldif_back_db_open;
   1741      1.1  lukem 	bi->bi_db_close = 0;
   1742      1.1  lukem 	bi->bi_db_destroy = ldif_back_db_destroy;
   1743      1.1  lukem 
   1744      1.1  lukem 	bi->bi_op_bind = ldif_back_bind;
   1745      1.1  lukem 	bi->bi_op_unbind = 0;
   1746      1.1  lukem 	bi->bi_op_search = ldif_back_search;
   1747      1.1  lukem 	bi->bi_op_compare = 0;
   1748      1.1  lukem 	bi->bi_op_modify = ldif_back_modify;
   1749      1.1  lukem 	bi->bi_op_modrdn = ldif_back_modrdn;
   1750      1.1  lukem 	bi->bi_op_add = ldif_back_add;
   1751      1.1  lukem 	bi->bi_op_delete = ldif_back_delete;
   1752      1.1  lukem 	bi->bi_op_abandon = 0;
   1753      1.1  lukem 
   1754      1.1  lukem 	bi->bi_extended = 0;
   1755      1.1  lukem 
   1756      1.1  lukem 	bi->bi_chk_referrals = ldif_back_referrals;
   1757      1.1  lukem 
   1758      1.1  lukem 	bi->bi_connection_init = 0;
   1759      1.1  lukem 	bi->bi_connection_destroy = 0;
   1760      1.1  lukem 
   1761      1.1  lukem 	bi->bi_entry_get_rw = ldif_back_entry_get;
   1762      1.1  lukem 
   1763      1.1  lukem #if 0	/* NOTE: uncomment to completely disable access control */
   1764      1.1  lukem 	bi->bi_access_allowed = slap_access_always_allowed;
   1765      1.1  lukem #endif
   1766      1.1  lukem 
   1767      1.1  lukem 	bi->bi_tool_entry_open = ldif_tool_entry_open;
   1768      1.1  lukem 	bi->bi_tool_entry_close = ldif_tool_entry_close;
   1769  1.1.1.3   adam 	bi->bi_tool_entry_first = backend_tool_entry_first;
   1770  1.1.1.3   adam 	bi->bi_tool_entry_first_x = ldif_tool_entry_first_x;
   1771      1.1  lukem 	bi->bi_tool_entry_next = ldif_tool_entry_next;
   1772      1.1  lukem 	bi->bi_tool_entry_get = ldif_tool_entry_get;
   1773      1.1  lukem 	bi->bi_tool_entry_put = ldif_tool_entry_put;
   1774      1.1  lukem 	bi->bi_tool_entry_reindex = 0;
   1775      1.1  lukem 	bi->bi_tool_sync = 0;
   1776  1.1.1.2  lukem 
   1777      1.1  lukem 	bi->bi_tool_dn2id_get = 0;
   1778      1.1  lukem 	bi->bi_tool_entry_modify = 0;
   1779      1.1  lukem 
   1780      1.1  lukem 	bi->bi_cf_ocs = ldifocs;
   1781      1.1  lukem 
   1782      1.1  lukem 	rc = config_register_schema( ldifcfg, ldifocs );
   1783      1.1  lukem 	if ( rc ) return rc;
   1784      1.1  lukem 	return 0;
   1785      1.1  lukem }
   1786