Home | History | Annotate | Line # | Download | only in back-ldif
ldif.c revision 1.1
      1  1.1  lukem /* ldif.c - the ldif backend */
      2  1.1  lukem /* $OpenLDAP: pkg/ldap/servers/slapd/back-ldif/ldif.c,v 1.48.2.14 2008/04/21 18:53:52 quanah Exp $ */
      3  1.1  lukem /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
      4  1.1  lukem  *
      5  1.1  lukem  * Copyright 2005-2008 The OpenLDAP Foundation.
      6  1.1  lukem  * All rights reserved.
      7  1.1  lukem  *
      8  1.1  lukem  * Redistribution and use in source and binary forms, with or without
      9  1.1  lukem  * modification, are permitted only as authorized by the OpenLDAP
     10  1.1  lukem  * Public License.
     11  1.1  lukem  *
     12  1.1  lukem  * A copy of this license is available in the file LICENSE in the
     13  1.1  lukem  * top-level directory of the distribution or, alternatively, at
     14  1.1  lukem  * <http://www.OpenLDAP.org/license.html>.
     15  1.1  lukem  */
     16  1.1  lukem /* ACKNOWLEDGEMENTS:
     17  1.1  lukem  * This work was originally developed by Eric Stokes for inclusion
     18  1.1  lukem  * in OpenLDAP Software.
     19  1.1  lukem  */
     20  1.1  lukem 
     21  1.1  lukem #include "portable.h"
     22  1.1  lukem #include <stdio.h>
     23  1.1  lukem #include <ac/string.h>
     24  1.1  lukem #include <sys/types.h>
     25  1.1  lukem #include <sys/stat.h>
     26  1.1  lukem #include <ac/dirent.h>
     27  1.1  lukem #include <fcntl.h>
     28  1.1  lukem #include <ac/errno.h>
     29  1.1  lukem #include <ac/unistd.h>
     30  1.1  lukem #include "slap.h"
     31  1.1  lukem #include "lutil.h"
     32  1.1  lukem #include "config.h"
     33  1.1  lukem 
     34  1.1  lukem typedef struct enumCookie {
     35  1.1  lukem 	Operation *op;
     36  1.1  lukem 	SlapReply *rs;
     37  1.1  lukem 	Entry **entries;
     38  1.1  lukem 	int elen;
     39  1.1  lukem 	int eind;
     40  1.1  lukem } enumCookie;
     41  1.1  lukem 
     42  1.1  lukem struct ldif_info {
     43  1.1  lukem 	struct berval li_base_path;
     44  1.1  lukem 	enumCookie li_tool_cookie;
     45  1.1  lukem 	ID li_tool_current;
     46  1.1  lukem 	ldap_pvt_thread_rdwr_t  li_rdwr;
     47  1.1  lukem };
     48  1.1  lukem 
     49  1.1  lukem #ifdef _WIN32
     50  1.1  lukem #define mkdir(a,b)	mkdir(a)
     51  1.1  lukem #endif
     52  1.1  lukem 
     53  1.1  lukem 
     54  1.1  lukem #define LDIF	".ldif"
     55  1.1  lukem #define LDIF_FILETYPE_SEP	'.'			/* LDIF[0] */
     56  1.1  lukem 
     57  1.1  lukem /*
     58  1.1  lukem  * Unsafe/translated characters in the filesystem.
     59  1.1  lukem  *
     60  1.1  lukem  * LDIF_UNSAFE_CHAR(c) returns true if the character c is not to be used
     61  1.1  lukem  * in relative filenames, except it should accept '\\' even if unsafe and
     62  1.1  lukem  * need not reject '{' and '}'.  The value should be a constant expression.
     63  1.1  lukem  *
     64  1.1  lukem  * If '\\' is unsafe, #define LDIF_ESCAPE_CHAR as a safe character.
     65  1.1  lukem  *
     66  1.1  lukem  * If '{' and '}' are unsafe, #define IX_FSL/IX_FSR as safe characters.
     67  1.1  lukem  * (Not digits, '-' or '+'.  IX_FSL == IX_FSR is allowed.)
     68  1.1  lukem  *
     69  1.1  lukem  * Characters are escaped as LDIF_ESCAPE_CHAR followed by two hex digits,
     70  1.1  lukem  * except '\\' is replaced with LDIF_ESCAPE_CHAR and {} with IX_FS[LR].
     71  1.1  lukem  * Also some LDIF special chars are hex-escaped.
     72  1.1  lukem  *
     73  1.1  lukem  * Thus an LDIF filename is a valid normalized RDN (or suffix DN)
     74  1.1  lukem  * followed by ".ldif", except with '\\' replaced with LDIF_ESCAPE_CHAR.
     75  1.1  lukem  */
     76  1.1  lukem 
     77  1.1  lukem #ifndef _WIN32
     78  1.1  lukem 
     79  1.1  lukem /*
     80  1.1  lukem  * Unix/MacOSX version.  ':' vs '/' can cause confusion on MacOSX so we
     81  1.1  lukem  * escape both.  We escape them on Unix so both OS variants get the same
     82  1.1  lukem  * filenames.
     83  1.1  lukem  */
     84  1.1  lukem #define LDIF_ESCAPE_CHAR	'\\'
     85  1.1  lukem #define LDIF_UNSAFE_CHAR(c)	((c) == '/' || (c) == ':')
     86  1.1  lukem 
     87  1.1  lukem #else /* _WIN32 */
     88  1.1  lukem 
     89  1.1  lukem /* Windows version - Microsoft's list of unsafe characters, except '\\' */
     90  1.1  lukem #define LDIF_ESCAPE_CHAR	'^'
     91  1.1  lukem #define LDIF_UNSAFE_CHAR(c)	\
     92  1.1  lukem 	((c) == '/' || (c) == ':' || \
     93  1.1  lukem 	 (c) == '<' || (c) == '>' || (c) == '"' || \
     94  1.1  lukem 	 (c) == '|' || (c) == '?' || (c) == '*')
     95  1.1  lukem 
     96  1.1  lukem #endif /* !_WIN32 */
     97  1.1  lukem 
     98  1.1  lukem /*
     99  1.1  lukem  * Left and Right "{num}" prefix to ordered RDNs ("olcDatabase={1}bdb").
    100  1.1  lukem  * IX_DN* are for LDAP RDNs, IX_FS* for their .ldif filenames.
    101  1.1  lukem  */
    102  1.1  lukem #define IX_DNL	'{'
    103  1.1  lukem #define	IX_DNR	'}'
    104  1.1  lukem #ifndef IX_FSL
    105  1.1  lukem #define	IX_FSL	IX_DNL
    106  1.1  lukem #define IX_FSR	IX_DNR
    107  1.1  lukem #endif
    108  1.1  lukem 
    109  1.1  lukem /*
    110  1.1  lukem  * Test for unsafe chars, as well as chars handled specially by back-ldif:
    111  1.1  lukem  * - If the escape char is not '\\', it must itself be escaped.  Otherwise
    112  1.1  lukem  *   '\\' and the escape char would map to the same character.
    113  1.1  lukem  * - Escape the '.' in ".ldif", so the directory for an RDN that actually
    114  1.1  lukem  *   ends with ".ldif" can not conflict with a file of the same name.  And
    115  1.1  lukem  *   since some OSes/programs choke on multiple '.'s, escape all of them.
    116  1.1  lukem  * - If '{' and '}' are translated to some other characters, those
    117  1.1  lukem  *   characters must in turn be escaped when they occur in an RDN.
    118  1.1  lukem  */
    119  1.1  lukem #ifndef LDIF_NEED_ESCAPE
    120  1.1  lukem #define	LDIF_NEED_ESCAPE(c) \
    121  1.1  lukem 	((LDIF_UNSAFE_CHAR(c)) || \
    122  1.1  lukem 	 LDIF_MAYBE_UNSAFE(c, LDIF_ESCAPE_CHAR) || \
    123  1.1  lukem 	 LDIF_MAYBE_UNSAFE(c, LDIF_FILETYPE_SEP) || \
    124  1.1  lukem 	 LDIF_MAYBE_UNSAFE(c, IX_FSL) || \
    125  1.1  lukem 	 (IX_FSR != IX_FSL && LDIF_MAYBE_UNSAFE(c, IX_FSR)))
    126  1.1  lukem #endif
    127  1.1  lukem /*
    128  1.1  lukem  * Helper macro for LDIF_NEED_ESCAPE(): Treat character x as unsafe if
    129  1.1  lukem  * back-ldif does not already treat is specially.
    130  1.1  lukem  */
    131  1.1  lukem #define LDIF_MAYBE_UNSAFE(c, x) \
    132  1.1  lukem 	(!(LDIF_UNSAFE_CHAR(x) || (x) == '\\' || (x) == IX_DNL || (x) == IX_DNR) \
    133  1.1  lukem 	 && (c) == (x))
    134  1.1  lukem 
    135  1.1  lukem 
    136  1.1  lukem #define ENTRY_BUFF_INCREMENT 500
    137  1.1  lukem 
    138  1.1  lukem static ConfigTable ldifcfg[] = {
    139  1.1  lukem 	{ "directory", "dir", 2, 2, 0, ARG_BERVAL|ARG_OFFSET,
    140  1.1  lukem 		(void *)offsetof(struct ldif_info, li_base_path),
    141  1.1  lukem 		"( OLcfgDbAt:0.1 NAME 'olcDbDirectory' "
    142  1.1  lukem 			"DESC 'Directory for database content' "
    143  1.1  lukem 			"EQUALITY caseIgnoreMatch "
    144  1.1  lukem 			"SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
    145  1.1  lukem 	{ NULL, NULL, 0, 0, 0, ARG_IGNORED,
    146  1.1  lukem 		NULL, NULL, NULL, NULL }
    147  1.1  lukem };
    148  1.1  lukem 
    149  1.1  lukem static ConfigOCs ldifocs[] = {
    150  1.1  lukem 	{ "( OLcfgDbOc:2.1 "
    151  1.1  lukem 		"NAME 'olcLdifConfig' "
    152  1.1  lukem 		"DESC 'LDIF backend configuration' "
    153  1.1  lukem 		"SUP olcDatabaseConfig "
    154  1.1  lukem 		"MUST ( olcDbDirectory ) )", Cft_Database, ldifcfg },
    155  1.1  lukem 	{ NULL, 0, NULL }
    156  1.1  lukem };
    157  1.1  lukem 
    158  1.1  lukem 
    159  1.1  lukem /* Set *res = LDIF filename path for the normalized DN */
    160  1.1  lukem static void
    161  1.1  lukem dn2path( BackendDB *be, struct berval *dn, struct berval *res )
    162  1.1  lukem {
    163  1.1  lukem 	struct ldif_info *li = (struct ldif_info *) be->be_private;
    164  1.1  lukem 	struct berval *suffixdn = &be->be_nsuffix[0];
    165  1.1  lukem 	const char *start, *end, *next, *p;
    166  1.1  lukem 	char ch, *ptr;
    167  1.1  lukem 	ber_len_t len;
    168  1.1  lukem 	static const char hex[] = "0123456789ABCDEF";
    169  1.1  lukem 
    170  1.1  lukem 	assert( dn != NULL );
    171  1.1  lukem 	assert( !BER_BVISNULL( dn ) );
    172  1.1  lukem 	assert( suffixdn != NULL );
    173  1.1  lukem 	assert( !BER_BVISNULL( suffixdn ) );
    174  1.1  lukem 	assert( dnIsSuffix( dn, suffixdn ) );
    175  1.1  lukem 
    176  1.1  lukem 	start = dn->bv_val;
    177  1.1  lukem 	end = start + dn->bv_len;
    178  1.1  lukem 
    179  1.1  lukem 	/* Room for dir, dirsep, dn, LDIF, "\hexpair"-escaping of unsafe chars */
    180  1.1  lukem 	len = li->li_base_path.bv_len + dn->bv_len + (1 + STRLENOF( LDIF ));
    181  1.1  lukem 	for ( p = start; p < end; ) {
    182  1.1  lukem 		ch = *p++;
    183  1.1  lukem 		if ( LDIF_NEED_ESCAPE( ch ) )
    184  1.1  lukem 			len += 2;
    185  1.1  lukem 	}
    186  1.1  lukem 	res->bv_val = ch_malloc( len + 1 );
    187  1.1  lukem 
    188  1.1  lukem 	ptr = lutil_strcopy( res->bv_val, li->li_base_path.bv_val );
    189  1.1  lukem 	for ( next = end - suffixdn->bv_len; end > start; end = next ) {
    190  1.1  lukem 		/* Set p = start of DN component, next = &',' or start of DN */
    191  1.1  lukem 		while ( (p = next) > start ) {
    192  1.1  lukem 			--next;
    193  1.1  lukem 			if ( DN_SEPARATOR( *next ) )
    194  1.1  lukem 				break;
    195  1.1  lukem 		}
    196  1.1  lukem 		/* Append <dirsep> <p..end-1: RDN or database-suffix> */
    197  1.1  lukem 		for ( *ptr++ = LDAP_DIRSEP[0]; p < end; *ptr++ = ch ) {
    198  1.1  lukem 			ch = *p++;
    199  1.1  lukem 			if ( LDIF_ESCAPE_CHAR != '\\' && ch == '\\' ) {
    200  1.1  lukem 				ch = LDIF_ESCAPE_CHAR;
    201  1.1  lukem 			} else if ( IX_FSL != IX_DNL && ch == IX_DNL ) {
    202  1.1  lukem 				ch = IX_FSL;
    203  1.1  lukem 			} else if ( IX_FSR != IX_DNR && ch == IX_DNR ) {
    204  1.1  lukem 				ch = IX_FSR;
    205  1.1  lukem 			} else if ( LDIF_NEED_ESCAPE( ch ) ) {
    206  1.1  lukem 				*ptr++ = LDIF_ESCAPE_CHAR;
    207  1.1  lukem 				*ptr++ = hex[(ch & 0xFFU) >> 4];
    208  1.1  lukem 				ch = hex[ch & 0x0FU];
    209  1.1  lukem 			}
    210  1.1  lukem 		}
    211  1.1  lukem 	}
    212  1.1  lukem 	ptr = lutil_strcopy( ptr, LDIF );
    213  1.1  lukem 	res->bv_len = ptr - res->bv_val;
    214  1.1  lukem 
    215  1.1  lukem 	assert( res->bv_len <= len );
    216  1.1  lukem }
    217  1.1  lukem 
    218  1.1  lukem static char * slurp_file(int fd) {
    219  1.1  lukem 	int read_chars_total = 0;
    220  1.1  lukem 	int read_chars = 0;
    221  1.1  lukem 	int entry_size;
    222  1.1  lukem 	char * entry;
    223  1.1  lukem 	char * entry_pos;
    224  1.1  lukem 	struct stat st;
    225  1.1  lukem 
    226  1.1  lukem 	fstat(fd, &st);
    227  1.1  lukem 	entry_size = st.st_size;
    228  1.1  lukem 	entry = ch_malloc( entry_size+1 );
    229  1.1  lukem 	entry_pos = entry;
    230  1.1  lukem 
    231  1.1  lukem 	while(1) {
    232  1.1  lukem 		read_chars = read(fd, (void *) entry_pos, entry_size - read_chars_total);
    233  1.1  lukem 		if(read_chars == -1) {
    234  1.1  lukem 			SLAP_FREE(entry);
    235  1.1  lukem 			return NULL;
    236  1.1  lukem 		}
    237  1.1  lukem 		if(read_chars == 0) {
    238  1.1  lukem 			entry[read_chars_total] = '\0';
    239  1.1  lukem 			break;
    240  1.1  lukem 		}
    241  1.1  lukem 		else {
    242  1.1  lukem 			read_chars_total += read_chars;
    243  1.1  lukem 			entry_pos += read_chars;
    244  1.1  lukem 		}
    245  1.1  lukem 	}
    246  1.1  lukem 	return entry;
    247  1.1  lukem }
    248  1.1  lukem 
    249  1.1  lukem /*
    250  1.1  lukem  * return nonnegative for success or -1 for error
    251  1.1  lukem  * do not return numbers less than -1
    252  1.1  lukem  */
    253  1.1  lukem static int spew_file(int fd, char * spew, int len) {
    254  1.1  lukem 	int writeres = 0;
    255  1.1  lukem 
    256  1.1  lukem 	while(len > 0) {
    257  1.1  lukem 		writeres = write(fd, spew, len);
    258  1.1  lukem 		if(writeres == -1) {
    259  1.1  lukem 			return -1;
    260  1.1  lukem 		}
    261  1.1  lukem 		else {
    262  1.1  lukem 			spew += writeres;
    263  1.1  lukem 			len -= writeres;
    264  1.1  lukem 		}
    265  1.1  lukem 	}
    266  1.1  lukem 	return writeres;
    267  1.1  lukem }
    268  1.1  lukem 
    269  1.1  lukem static int
    270  1.1  lukem spew_entry( Entry * e, struct berval * path, int dolock, int *save_errnop )
    271  1.1  lukem {
    272  1.1  lukem 	int rs, save_errno = 0;
    273  1.1  lukem 	int openres;
    274  1.1  lukem 	int res, spew_res;
    275  1.1  lukem 	int entry_length;
    276  1.1  lukem 	char * entry_as_string;
    277  1.1  lukem 	char *tmpfname = NULL;
    278  1.1  lukem 
    279  1.1  lukem 	tmpfname = ch_malloc( path->bv_len + STRLENOF( "XXXXXX" ) + 1 );
    280  1.1  lukem 	AC_MEMCPY( tmpfname, path->bv_val, path->bv_len );
    281  1.1  lukem 	AC_MEMCPY( &tmpfname[ path->bv_len ], "XXXXXX", STRLENOF( "XXXXXX" ) + 1 );
    282  1.1  lukem 
    283  1.1  lukem 	openres = mkstemp( tmpfname );
    284  1.1  lukem 	if ( openres == -1 ) {
    285  1.1  lukem 		save_errno = errno;
    286  1.1  lukem 		rs = LDAP_UNWILLING_TO_PERFORM;
    287  1.1  lukem 		Debug( LDAP_DEBUG_ANY, "could not create tmpfile \"%s\": %s\n",
    288  1.1  lukem 			tmpfname, STRERROR( save_errno ), 0 );
    289  1.1  lukem 
    290  1.1  lukem 	} else {
    291  1.1  lukem 		struct berval rdn;
    292  1.1  lukem 		int tmp;
    293  1.1  lukem 
    294  1.1  lukem 		/* Only save the RDN onto disk */
    295  1.1  lukem 		dnRdn( &e->e_name, &rdn );
    296  1.1  lukem 		if ( rdn.bv_len != e->e_name.bv_len ) {
    297  1.1  lukem 			e->e_name.bv_val[rdn.bv_len] = '\0';
    298  1.1  lukem 			tmp = e->e_name.bv_len;
    299  1.1  lukem 			e->e_name.bv_len = rdn.bv_len;
    300  1.1  lukem 			rdn.bv_len = tmp;
    301  1.1  lukem 		}
    302  1.1  lukem 
    303  1.1  lukem 		spew_res = -2;
    304  1.1  lukem 		if ( dolock ) {
    305  1.1  lukem 			ldap_pvt_thread_mutex_lock(&entry2str_mutex);
    306  1.1  lukem 		}
    307  1.1  lukem 
    308  1.1  lukem 		entry_as_string = entry2str(e, &entry_length);
    309  1.1  lukem 		if ( entry_as_string != NULL ) {
    310  1.1  lukem 			spew_res = spew_file( openres,
    311  1.1  lukem 				entry_as_string, entry_length );
    312  1.1  lukem 			if ( spew_res == -1 ) {
    313  1.1  lukem 				save_errno = errno;
    314  1.1  lukem 			}
    315  1.1  lukem 		}
    316  1.1  lukem 
    317  1.1  lukem 		if ( dolock ) {
    318  1.1  lukem 			ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
    319  1.1  lukem 		}
    320  1.1  lukem 
    321  1.1  lukem 		/* Restore full DN */
    322  1.1  lukem 		if ( rdn.bv_len != e->e_name.bv_len ) {
    323  1.1  lukem 			e->e_name.bv_val[e->e_name.bv_len] = ',';
    324  1.1  lukem 			e->e_name.bv_len = rdn.bv_len;
    325  1.1  lukem 		}
    326  1.1  lukem 
    327  1.1  lukem 		res = close( openres );
    328  1.1  lukem 		rs = LDAP_UNWILLING_TO_PERFORM;
    329  1.1  lukem 
    330  1.1  lukem 		if ( spew_res > -2 ) {
    331  1.1  lukem 			if ( res == -1 || spew_res == -1 ) {
    332  1.1  lukem 				if ( save_errno == 0 ) {
    333  1.1  lukem 					save_errno = errno;
    334  1.1  lukem 				}
    335  1.1  lukem 				Debug( LDAP_DEBUG_ANY, "write error to tmpfile \"%s\": %s\n",
    336  1.1  lukem 					tmpfname, STRERROR( save_errno ), 0 );
    337  1.1  lukem 
    338  1.1  lukem 			} else {
    339  1.1  lukem 				res = rename( tmpfname, path->bv_val );
    340  1.1  lukem 				if ( res == 0 ) {
    341  1.1  lukem 					rs = LDAP_SUCCESS;
    342  1.1  lukem 
    343  1.1  lukem 				} else {
    344  1.1  lukem 					save_errno = errno;
    345  1.1  lukem 					switch ( save_errno ) {
    346  1.1  lukem 					case ENOENT:
    347  1.1  lukem 						rs = LDAP_NO_SUCH_OBJECT;
    348  1.1  lukem 						break;
    349  1.1  lukem 
    350  1.1  lukem 					default:
    351  1.1  lukem 						break;
    352  1.1  lukem 					}
    353  1.1  lukem 				}
    354  1.1  lukem 			}
    355  1.1  lukem 		}
    356  1.1  lukem 
    357  1.1  lukem 		if ( rs != LDAP_SUCCESS ) {
    358  1.1  lukem 			unlink( tmpfname );
    359  1.1  lukem 		}
    360  1.1  lukem 	}
    361  1.1  lukem 
    362  1.1  lukem 	ch_free( tmpfname );
    363  1.1  lukem 
    364  1.1  lukem 	if ( rs != LDAP_SUCCESS && save_errnop != NULL ) {
    365  1.1  lukem 		*save_errnop = save_errno;
    366  1.1  lukem 	}
    367  1.1  lukem 
    368  1.1  lukem 	return rs;
    369  1.1  lukem }
    370  1.1  lukem 
    371  1.1  lukem static Entry * get_entry_for_fd(int fd,
    372  1.1  lukem 	struct berval *pdn,
    373  1.1  lukem 	struct berval *pndn)
    374  1.1  lukem {
    375  1.1  lukem 	char * entry = (char *) slurp_file(fd);
    376  1.1  lukem 	Entry * ldentry = NULL;
    377  1.1  lukem 
    378  1.1  lukem 	/* error reading file */
    379  1.1  lukem 	if(entry == NULL) {
    380  1.1  lukem 		goto return_value;
    381  1.1  lukem 	}
    382  1.1  lukem 
    383  1.1  lukem 	ldentry = str2entry(entry);
    384  1.1  lukem 	if ( ldentry ) {
    385  1.1  lukem 		struct berval rdn;
    386  1.1  lukem 		rdn = ldentry->e_name;
    387  1.1  lukem 		build_new_dn( &ldentry->e_name, pdn, &rdn, NULL );
    388  1.1  lukem 		ch_free( rdn.bv_val );
    389  1.1  lukem 		rdn = ldentry->e_nname;
    390  1.1  lukem 		build_new_dn( &ldentry->e_nname, pndn, &rdn, NULL );
    391  1.1  lukem 		ch_free( rdn.bv_val );
    392  1.1  lukem 	}
    393  1.1  lukem 
    394  1.1  lukem  return_value:
    395  1.1  lukem 	if(fd != -1) {
    396  1.1  lukem 		if(close(fd) != 0) {
    397  1.1  lukem 			/* log error */
    398  1.1  lukem 		}
    399  1.1  lukem 	}
    400  1.1  lukem 	if(entry != NULL)
    401  1.1  lukem 		SLAP_FREE(entry);
    402  1.1  lukem 	return ldentry;
    403  1.1  lukem }
    404  1.1  lukem 
    405  1.1  lukem static int
    406  1.1  lukem get_entry(
    407  1.1  lukem 	Operation *op,
    408  1.1  lukem 	Entry **entryp,
    409  1.1  lukem 	struct berval *pathp )
    410  1.1  lukem {
    411  1.1  lukem 	int rc;
    412  1.1  lukem 	struct berval path, pdn, pndn;
    413  1.1  lukem 	int fd;
    414  1.1  lukem 
    415  1.1  lukem 	dnParent(&op->o_req_dn, &pdn);
    416  1.1  lukem 	dnParent(&op->o_req_ndn, &pndn);
    417  1.1  lukem 	dn2path( op->o_bd, &op->o_req_ndn, &path );
    418  1.1  lukem 	fd = open(path.bv_val, O_RDONLY);
    419  1.1  lukem 	/* error opening file (mebbe should log error) */
    420  1.1  lukem 	if ( fd == -1 && ( errno != ENOENT || op->o_tag != LDAP_REQ_ADD ) ) {
    421  1.1  lukem 		Debug( LDAP_DEBUG_ANY, "failed to open file \"%s\": %s\n",
    422  1.1  lukem 			path.bv_val, STRERROR(errno), 0 );
    423  1.1  lukem 	}
    424  1.1  lukem 	*entryp = fd < 0 ? NULL : get_entry_for_fd( fd, &pdn, &pndn );
    425  1.1  lukem 	rc = *entryp ? LDAP_SUCCESS : LDAP_NO_SUCH_OBJECT;
    426  1.1  lukem 
    427  1.1  lukem 	if ( rc == LDAP_SUCCESS && pathp != NULL ) {
    428  1.1  lukem 		*pathp = path;
    429  1.1  lukem 	} else {
    430  1.1  lukem 		SLAP_FREE(path.bv_val);
    431  1.1  lukem 	}
    432  1.1  lukem 	return rc;
    433  1.1  lukem }
    434  1.1  lukem 
    435  1.1  lukem static void fullpath(struct berval *base, struct berval *name, struct berval *res) {
    436  1.1  lukem 	char *ptr;
    437  1.1  lukem 	res->bv_len = name->bv_len + base->bv_len + 1;
    438  1.1  lukem 	res->bv_val = ch_malloc( res->bv_len + 1 );
    439  1.1  lukem 	strcpy(res->bv_val, base->bv_val);
    440  1.1  lukem 	ptr = res->bv_val + base->bv_len;
    441  1.1  lukem 	*ptr++ = LDAP_DIRSEP[0];
    442  1.1  lukem 	strcpy(ptr, name->bv_val);
    443  1.1  lukem }
    444  1.1  lukem 
    445  1.1  lukem typedef struct bvlist {
    446  1.1  lukem 	struct bvlist *next;
    447  1.1  lukem 	struct berval bv;
    448  1.1  lukem 	struct berval num;
    449  1.1  lukem 	int inum;
    450  1.1  lukem 	int off;
    451  1.1  lukem } bvlist;
    452  1.1  lukem 
    453  1.1  lukem 
    454  1.1  lukem static int r_enum_tree(enumCookie *ck, struct berval *path, int base,
    455  1.1  lukem 	struct berval *pdn, struct berval *pndn)
    456  1.1  lukem {
    457  1.1  lukem 	Entry *e = NULL;
    458  1.1  lukem 	int fd = 0, rc = LDAP_SUCCESS;
    459  1.1  lukem 
    460  1.1  lukem 	if ( !base ) {
    461  1.1  lukem 		fd = open( path->bv_val, O_RDONLY );
    462  1.1  lukem 		if ( fd < 0 ) {
    463  1.1  lukem 			Debug( LDAP_DEBUG_TRACE,
    464  1.1  lukem 				"=> ldif_enum_tree: failed to open %s: %s\n",
    465  1.1  lukem 				path->bv_val, STRERROR(errno), 0 );
    466  1.1  lukem 			return LDAP_NO_SUCH_OBJECT;
    467  1.1  lukem 		}
    468  1.1  lukem 
    469  1.1  lukem 		e = get_entry_for_fd(fd, pdn, pndn);
    470  1.1  lukem 		if ( !e ) {
    471  1.1  lukem 			Debug( LDAP_DEBUG_ANY,
    472  1.1  lukem 				"=> ldif_enum_tree: failed to read entry for %s\n",
    473  1.1  lukem 				path->bv_val, 0, 0 );
    474  1.1  lukem 			return LDAP_BUSY;
    475  1.1  lukem 		}
    476  1.1  lukem 
    477  1.1  lukem 		if ( ck->op->ors_scope == LDAP_SCOPE_BASE ||
    478  1.1  lukem 			ck->op->ors_scope == LDAP_SCOPE_SUBTREE ) {
    479  1.1  lukem 			/* Send right away? */
    480  1.1  lukem 			if ( ck->rs ) {
    481  1.1  lukem 				/*
    482  1.1  lukem 				 * if it's a referral, add it to the list of referrals. only do
    483  1.1  lukem 				 * this for non-base searches, and don't check the filter
    484  1.1  lukem 				 * explicitly here since it's only a candidate anyway.
    485  1.1  lukem 				 */
    486  1.1  lukem 				if ( !get_manageDSAit( ck->op )
    487  1.1  lukem 						&& ck->op->ors_scope != LDAP_SCOPE_BASE
    488  1.1  lukem 						&& is_entry_referral( e ) )
    489  1.1  lukem 				{
    490  1.1  lukem 					BerVarray erefs = get_entry_referrals( ck->op, e );
    491  1.1  lukem 					ck->rs->sr_ref = referral_rewrite( erefs,
    492  1.1  lukem 							&e->e_name, NULL,
    493  1.1  lukem 							ck->op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
    494  1.1  lukem 								? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
    495  1.1  lukem 
    496  1.1  lukem 					ck->rs->sr_entry = e;
    497  1.1  lukem 					rc = send_search_reference( ck->op, ck->rs );
    498  1.1  lukem 					ber_bvarray_free( ck->rs->sr_ref );
    499  1.1  lukem 					ber_bvarray_free( erefs );
    500  1.1  lukem 					ck->rs->sr_ref = NULL;
    501  1.1  lukem 					ck->rs->sr_entry = NULL;
    502  1.1  lukem 
    503  1.1  lukem 				} else if ( test_filter( ck->op, e, ck->op->ors_filter ) == LDAP_COMPARE_TRUE )
    504  1.1  lukem 				{
    505  1.1  lukem 					ck->rs->sr_entry = e;
    506  1.1  lukem 					ck->rs->sr_attrs = ck->op->ors_attrs;
    507  1.1  lukem 					ck->rs->sr_flags = REP_ENTRY_MODIFIABLE;
    508  1.1  lukem 					rc = send_search_entry(ck->op, ck->rs);
    509  1.1  lukem 					ck->rs->sr_entry = NULL;
    510  1.1  lukem 				}
    511  1.1  lukem 				fd = 1;
    512  1.1  lukem 				if ( rc )
    513  1.1  lukem 					goto done;
    514  1.1  lukem 			} else {
    515  1.1  lukem 			/* Queueing up for tool mode */
    516  1.1  lukem 				if(ck->entries == NULL) {
    517  1.1  lukem 					ck->entries = (Entry **) ch_malloc(sizeof(Entry *) * ENTRY_BUFF_INCREMENT);
    518  1.1  lukem 					ck->elen = ENTRY_BUFF_INCREMENT;
    519  1.1  lukem 				}
    520  1.1  lukem 				if(ck->eind >= ck->elen) { /* grow entries if necessary */
    521  1.1  lukem 					ck->entries = (Entry **) ch_realloc(ck->entries, sizeof(Entry *) * (ck->elen) * 2);
    522  1.1  lukem 					ck->elen *= 2;
    523  1.1  lukem 				}
    524  1.1  lukem 
    525  1.1  lukem 				ck->entries[ck->eind++] = e;
    526  1.1  lukem 				fd = 0;
    527  1.1  lukem 			}
    528  1.1  lukem 		} else {
    529  1.1  lukem 			fd = 1;
    530  1.1  lukem 		}
    531  1.1  lukem 	}
    532  1.1  lukem 
    533  1.1  lukem 	if ( ck->op->ors_scope != LDAP_SCOPE_BASE ) {
    534  1.1  lukem 		DIR * dir_of_path;
    535  1.1  lukem 		bvlist *list = NULL, *ptr;
    536  1.1  lukem 
    537  1.1  lukem 		path->bv_len -= STRLENOF( LDIF );
    538  1.1  lukem 		path->bv_val[path->bv_len] = '\0';
    539  1.1  lukem 
    540  1.1  lukem 		dir_of_path = opendir(path->bv_val);
    541  1.1  lukem 		if(dir_of_path == NULL) { /* can't open directory */
    542  1.1  lukem 			if ( errno != ENOENT ) {
    543  1.1  lukem 				/* it shouldn't be treated as an error
    544  1.1  lukem 				 * only if the directory doesn't exist */
    545  1.1  lukem 				rc = LDAP_BUSY;
    546  1.1  lukem 				Debug( LDAP_DEBUG_ANY,
    547  1.1  lukem 					"=> ldif_enum_tree: failed to opendir %s (%d)\n",
    548  1.1  lukem 					path->bv_val, errno, 0 );
    549  1.1  lukem 			}
    550  1.1  lukem 			goto done;
    551  1.1  lukem 		}
    552  1.1  lukem 
    553  1.1  lukem 		while(1) {
    554  1.1  lukem 			struct berval fname, itmp;
    555  1.1  lukem 			struct dirent * dir;
    556  1.1  lukem 			bvlist *bvl, **prev;
    557  1.1  lukem 
    558  1.1  lukem 			dir = readdir(dir_of_path);
    559  1.1  lukem 			if(dir == NULL) break; /* end of the directory */
    560  1.1  lukem 			fname.bv_len = strlen( dir->d_name );
    561  1.1  lukem 			if ( fname.bv_len <= STRLENOF( LDIF ))
    562  1.1  lukem 				continue;
    563  1.1  lukem 			if ( strcmp( dir->d_name + (fname.bv_len - STRLENOF(LDIF)), LDIF))
    564  1.1  lukem 				continue;
    565  1.1  lukem 			fname.bv_val = dir->d_name;
    566  1.1  lukem 
    567  1.1  lukem 			bvl = ch_malloc( sizeof(bvlist) );
    568  1.1  lukem 			ber_dupbv( &bvl->bv, &fname );
    569  1.1  lukem 			BER_BVZERO( &bvl->num );
    570  1.1  lukem 			itmp.bv_val = ber_bvchr( &bvl->bv, IX_FSL );
    571  1.1  lukem 			if ( itmp.bv_val ) {
    572  1.1  lukem 				char *ptr;
    573  1.1  lukem 				itmp.bv_val++;
    574  1.1  lukem 				itmp.bv_len = bvl->bv.bv_len
    575  1.1  lukem 					- ( itmp.bv_val - bvl->bv.bv_val );
    576  1.1  lukem 				ptr = ber_bvchr( &itmp, IX_FSR );
    577  1.1  lukem 				if ( ptr ) {
    578  1.1  lukem 					itmp.bv_len = ptr - itmp.bv_val;
    579  1.1  lukem 					ber_dupbv( &bvl->num, &itmp );
    580  1.1  lukem 					bvl->inum = strtol( itmp.bv_val, NULL, 0 );
    581  1.1  lukem 					itmp.bv_val[0] = '\0';
    582  1.1  lukem 					bvl->off = itmp.bv_val - bvl->bv.bv_val;
    583  1.1  lukem 				}
    584  1.1  lukem 			}
    585  1.1  lukem 
    586  1.1  lukem 			for (prev = &list; (ptr = *prev) != NULL; prev = &ptr->next) {
    587  1.1  lukem 				int cmp = strcmp( bvl->bv.bv_val, ptr->bv.bv_val );
    588  1.1  lukem 				if ( !cmp && bvl->num.bv_val )
    589  1.1  lukem 					cmp = bvl->inum - ptr->inum;
    590  1.1  lukem 				if ( cmp < 0 )
    591  1.1  lukem 					break;
    592  1.1  lukem 			}
    593  1.1  lukem 			*prev = bvl;
    594  1.1  lukem 			bvl->next = ptr;
    595  1.1  lukem 
    596  1.1  lukem 		}
    597  1.1  lukem 		closedir(dir_of_path);
    598  1.1  lukem 
    599  1.1  lukem 		if (ck->op->ors_scope == LDAP_SCOPE_ONELEVEL)
    600  1.1  lukem 			ck->op->ors_scope = LDAP_SCOPE_BASE;
    601  1.1  lukem 		else if ( ck->op->ors_scope == LDAP_SCOPE_SUBORDINATE)
    602  1.1  lukem 			ck->op->ors_scope = LDAP_SCOPE_SUBTREE;
    603  1.1  lukem 
    604  1.1  lukem 		while ( ( ptr = list ) ) {
    605  1.1  lukem 			struct berval fpath;
    606  1.1  lukem 
    607  1.1  lukem 			list = ptr->next;
    608  1.1  lukem 
    609  1.1  lukem 			if ( rc == LDAP_SUCCESS ) {
    610  1.1  lukem 				if ( ptr->num.bv_val )
    611  1.1  lukem 					AC_MEMCPY( ptr->bv.bv_val + ptr->off, ptr->num.bv_val,
    612  1.1  lukem 						ptr->num.bv_len );
    613  1.1  lukem 				fullpath( path, &ptr->bv, &fpath );
    614  1.1  lukem 				rc = r_enum_tree(ck, &fpath, 0,
    615  1.1  lukem 					e != NULL ? &e->e_name : pdn,
    616  1.1  lukem 					e != NULL ? &e->e_nname : pndn );
    617  1.1  lukem 				free(fpath.bv_val);
    618  1.1  lukem 			}
    619  1.1  lukem 			if ( ptr->num.bv_val )
    620  1.1  lukem 				free( ptr->num.bv_val );
    621  1.1  lukem 			free(ptr->bv.bv_val);
    622  1.1  lukem 			free(ptr);
    623  1.1  lukem 		}
    624  1.1  lukem 	}
    625  1.1  lukem done:
    626  1.1  lukem 	if ( fd ) entry_free( e );
    627  1.1  lukem 	return rc;
    628  1.1  lukem }
    629  1.1  lukem 
    630  1.1  lukem static int
    631  1.1  lukem enum_tree(
    632  1.1  lukem 	enumCookie *ck
    633  1.1  lukem )
    634  1.1  lukem {
    635  1.1  lukem 	struct berval path;
    636  1.1  lukem 	struct berval pdn, pndn;
    637  1.1  lukem 	int rc;
    638  1.1  lukem 
    639  1.1  lukem 	dnParent( &ck->op->o_req_dn, &pdn );
    640  1.1  lukem 	dnParent( &ck->op->o_req_ndn, &pndn );
    641  1.1  lukem 	dn2path( ck->op->o_bd, &ck->op->o_req_ndn, &path );
    642  1.1  lukem 	rc = r_enum_tree(ck, &path, BER_BVISEMPTY( &ck->op->o_req_ndn ) ? 1 : 0, &pdn, &pndn);
    643  1.1  lukem 	ch_free( path.bv_val );
    644  1.1  lukem 	return rc;
    645  1.1  lukem }
    646  1.1  lukem 
    647  1.1  lukem 
    648  1.1  lukem /* Get the parent directory path, plus the LDIF suffix overwritten by a \0 */
    649  1.1  lukem static void
    650  1.1  lukem get_parent_path( struct berval *dnpath, struct berval *res )
    651  1.1  lukem {
    652  1.1  lukem 	int dnpathlen = dnpath->bv_len;
    653  1.1  lukem 	int i;
    654  1.1  lukem 
    655  1.1  lukem 	for(i = dnpathlen;i>0;i--) /* find the first path seperator */
    656  1.1  lukem 		if(dnpath->bv_val[i] == LDAP_DIRSEP[0])
    657  1.1  lukem 			break;
    658  1.1  lukem 	res->bv_len = i;
    659  1.1  lukem 	res->bv_val = ch_malloc( res->bv_len + 1 + STRLENOF(LDIF) );
    660  1.1  lukem 	strncpy(res->bv_val, dnpath->bv_val, i);
    661  1.1  lukem 	strcpy(res->bv_val+i, LDIF);
    662  1.1  lukem 	res->bv_val[i] = '\0';
    663  1.1  lukem }
    664  1.1  lukem 
    665  1.1  lukem static int apply_modify_to_entry(Entry * entry,
    666  1.1  lukem 				Modifications * modlist,
    667  1.1  lukem 				Operation * op,
    668  1.1  lukem 				SlapReply * rs)
    669  1.1  lukem {
    670  1.1  lukem 	char textbuf[SLAP_TEXT_BUFLEN];
    671  1.1  lukem 	int rc = modlist ? LDAP_UNWILLING_TO_PERFORM : LDAP_SUCCESS;
    672  1.1  lukem 	int is_oc = 0;
    673  1.1  lukem 	Modification *mods;
    674  1.1  lukem 
    675  1.1  lukem 	if (!acl_check_modlist(op, entry, modlist)) {
    676  1.1  lukem 		return LDAP_INSUFFICIENT_ACCESS;
    677  1.1  lukem 	}
    678  1.1  lukem 
    679  1.1  lukem 	for (; modlist != NULL; modlist = modlist->sml_next) {
    680  1.1  lukem 		mods = &modlist->sml_mod;
    681  1.1  lukem 
    682  1.1  lukem 		if ( mods->sm_desc == slap_schema.si_ad_objectClass ) {
    683  1.1  lukem 			is_oc = 1;
    684  1.1  lukem 		}
    685  1.1  lukem 		switch (mods->sm_op) {
    686  1.1  lukem 		case LDAP_MOD_ADD:
    687  1.1  lukem 			rc = modify_add_values(entry, mods,
    688  1.1  lukem 				   get_permissiveModify(op),
    689  1.1  lukem 				   &rs->sr_text, textbuf,
    690  1.1  lukem 				   sizeof( textbuf ) );
    691  1.1  lukem 			break;
    692  1.1  lukem 
    693  1.1  lukem 		case LDAP_MOD_DELETE:
    694  1.1  lukem 			rc = modify_delete_values(entry, mods,
    695  1.1  lukem 				get_permissiveModify(op),
    696  1.1  lukem 				&rs->sr_text, textbuf,
    697  1.1  lukem 				sizeof( textbuf ) );
    698  1.1  lukem 			break;
    699  1.1  lukem 
    700  1.1  lukem 		case LDAP_MOD_REPLACE:
    701  1.1  lukem 			rc = modify_replace_values(entry, mods,
    702  1.1  lukem 				 get_permissiveModify(op),
    703  1.1  lukem 				 &rs->sr_text, textbuf,
    704  1.1  lukem 				 sizeof( textbuf ) );
    705  1.1  lukem 			break;
    706  1.1  lukem 
    707  1.1  lukem 		case LDAP_MOD_INCREMENT:
    708  1.1  lukem 			rc = modify_increment_values( entry,
    709  1.1  lukem 				mods, get_permissiveModify(op),
    710  1.1  lukem 				&rs->sr_text, textbuf,
    711  1.1  lukem 				sizeof( textbuf ) );
    712  1.1  lukem 			break;
    713  1.1  lukem 
    714  1.1  lukem 		case SLAP_MOD_SOFTADD:
    715  1.1  lukem 			mods->sm_op = LDAP_MOD_ADD;
    716  1.1  lukem 			rc = modify_add_values(entry, mods,
    717  1.1  lukem 				   get_permissiveModify(op),
    718  1.1  lukem 				   &rs->sr_text, textbuf,
    719  1.1  lukem 				   sizeof( textbuf ) );
    720  1.1  lukem 			mods->sm_op = SLAP_MOD_SOFTADD;
    721  1.1  lukem 			if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
    722  1.1  lukem 				rc = LDAP_SUCCESS;
    723  1.1  lukem 			}
    724  1.1  lukem 			break;
    725  1.1  lukem 		}
    726  1.1  lukem 		if(rc != LDAP_SUCCESS) break;
    727  1.1  lukem 	}
    728  1.1  lukem 
    729  1.1  lukem 	if(rc == LDAP_SUCCESS) {
    730  1.1  lukem 		if ( is_oc ) {
    731  1.1  lukem 			entry->e_ocflags = 0;
    732  1.1  lukem 		}
    733  1.1  lukem 		/* check that the entry still obeys the schema */
    734  1.1  lukem 		rc = entry_schema_check( op, entry, NULL, 0, 0,
    735  1.1  lukem 			  &rs->sr_text, textbuf, sizeof( textbuf ) );
    736  1.1  lukem 	}
    737  1.1  lukem 
    738  1.1  lukem 	return rc;
    739  1.1  lukem }
    740  1.1  lukem 
    741  1.1  lukem int
    742  1.1  lukem ldif_back_referrals( Operation *op, SlapReply *rs )
    743  1.1  lukem {
    744  1.1  lukem 	struct ldif_info	*li = NULL;
    745  1.1  lukem 	Entry			*entry;
    746  1.1  lukem 	int			rc = LDAP_SUCCESS;
    747  1.1  lukem 
    748  1.1  lukem #if 0
    749  1.1  lukem 	if ( op->o_tag == LDAP_REQ_SEARCH ) {
    750  1.1  lukem 		/* let search take care of itself */
    751  1.1  lukem 		return rc;
    752  1.1  lukem 	}
    753  1.1  lukem #endif
    754  1.1  lukem 
    755  1.1  lukem 	if ( get_manageDSAit( op ) ) {
    756  1.1  lukem 		/* let op take care of DSA management */
    757  1.1  lukem 		return rc;
    758  1.1  lukem 	}
    759  1.1  lukem 
    760  1.1  lukem 	if ( BER_BVISEMPTY( &op->o_req_ndn ) ) {
    761  1.1  lukem 		/* the empty DN cannot be a referral */
    762  1.1  lukem 		return rc;
    763  1.1  lukem 	}
    764  1.1  lukem 
    765  1.1  lukem 	li = (struct ldif_info *)op->o_bd->be_private;
    766  1.1  lukem 	ldap_pvt_thread_rdwr_rlock( &li->li_rdwr );
    767  1.1  lukem 	get_entry( op, &entry, NULL );
    768  1.1  lukem 
    769  1.1  lukem 	/* no object is found for them */
    770  1.1  lukem 	if ( entry == NULL ) {
    771  1.1  lukem 		struct berval	odn = op->o_req_dn;
    772  1.1  lukem 		struct berval	ondn = op->o_req_ndn;
    773  1.1  lukem 		struct berval	pndn = ondn;
    774  1.1  lukem 		ber_len_t		min_dnlen = op->o_bd->be_nsuffix[0].bv_len;
    775  1.1  lukem 
    776  1.1  lukem 		if ( min_dnlen == 0 )
    777  1.1  lukem 			min_dnlen = 1;	   /* catch empty DN */
    778  1.1  lukem 
    779  1.1  lukem 		for ( ; entry == NULL; ) {
    780  1.1  lukem 			dnParent( &pndn, &pndn );
    781  1.1  lukem 			if ( pndn.bv_len < min_dnlen ) {
    782  1.1  lukem 				break;
    783  1.1  lukem 			}
    784  1.1  lukem 
    785  1.1  lukem 			op->o_req_dn = pndn;
    786  1.1  lukem 			op->o_req_ndn = pndn;
    787  1.1  lukem 
    788  1.1  lukem 			get_entry( op, &entry, NULL );
    789  1.1  lukem 		}
    790  1.1  lukem 
    791  1.1  lukem 		ldap_pvt_thread_rdwr_runlock( &li->li_rdwr );
    792  1.1  lukem 
    793  1.1  lukem 		op->o_req_dn = odn;
    794  1.1  lukem 		op->o_req_ndn = ondn;
    795  1.1  lukem 
    796  1.1  lukem 		rc = LDAP_SUCCESS;
    797  1.1  lukem 		rs->sr_matched = NULL;
    798  1.1  lukem 		if ( entry != NULL ) {
    799  1.1  lukem 			Debug( LDAP_DEBUG_TRACE,
    800  1.1  lukem 				"ldif_back_referrals: tag=%lu target=\"%s\" matched=\"%s\"\n",
    801  1.1  lukem 				(unsigned long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
    802  1.1  lukem 
    803  1.1  lukem 			if ( is_entry_referral( entry ) ) {
    804  1.1  lukem 				rc = LDAP_OTHER;
    805  1.1  lukem 				rs->sr_ref = get_entry_referrals( op, entry );
    806  1.1  lukem 				if ( rs->sr_ref ) {
    807  1.1  lukem 					rs->sr_matched = ber_strdup_x(
    808  1.1  lukem 					entry->e_name.bv_val, op->o_tmpmemctx );
    809  1.1  lukem 				}
    810  1.1  lukem 			}
    811  1.1  lukem 
    812  1.1  lukem 			entry_free(entry);
    813  1.1  lukem 
    814  1.1  lukem 		} else if ( default_referral != NULL ) {
    815  1.1  lukem 			rc = LDAP_OTHER;
    816  1.1  lukem 			rs->sr_ref = referral_rewrite( default_referral,
    817  1.1  lukem 				NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
    818  1.1  lukem 		}
    819  1.1  lukem 
    820  1.1  lukem 		if ( rs->sr_ref != NULL ) {
    821  1.1  lukem 			/* send referrals */
    822  1.1  lukem 			rc = rs->sr_err = LDAP_REFERRAL;
    823  1.1  lukem 			send_ldap_result( op, rs );
    824  1.1  lukem 			ber_bvarray_free( rs->sr_ref );
    825  1.1  lukem 			rs->sr_ref = NULL;
    826  1.1  lukem 
    827  1.1  lukem 		} else if ( rc != LDAP_SUCCESS ) {
    828  1.1  lukem 			rs->sr_text = rs->sr_matched ? "bad referral object" : NULL;
    829  1.1  lukem 		}
    830  1.1  lukem 
    831  1.1  lukem 		if ( rs->sr_matched ) {
    832  1.1  lukem 			op->o_tmpfree( (char *)rs->sr_matched, op->o_tmpmemctx );
    833  1.1  lukem 			rs->sr_matched = NULL;
    834  1.1  lukem 		}
    835  1.1  lukem 
    836  1.1  lukem 		return rc;
    837  1.1  lukem 	}
    838  1.1  lukem 
    839  1.1  lukem 	ldap_pvt_thread_rdwr_runlock( &li->li_rdwr );
    840  1.1  lukem 
    841  1.1  lukem 	if ( is_entry_referral( entry ) ) {
    842  1.1  lukem 		/* entry is a referral */
    843  1.1  lukem 		BerVarray refs = get_entry_referrals( op, entry );
    844  1.1  lukem 		rs->sr_ref = referral_rewrite(
    845  1.1  lukem 			refs, &entry->e_name, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
    846  1.1  lukem 
    847  1.1  lukem 		Debug( LDAP_DEBUG_TRACE,
    848  1.1  lukem 			"ldif_back_referrals: tag=%lu target=\"%s\" matched=\"%s\"\n",
    849  1.1  lukem 			(unsigned long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
    850  1.1  lukem 
    851  1.1  lukem 		rs->sr_matched = entry->e_name.bv_val;
    852  1.1  lukem 		if ( rs->sr_ref != NULL ) {
    853  1.1  lukem 			rc = rs->sr_err = LDAP_REFERRAL;
    854  1.1  lukem 			send_ldap_result( op, rs );
    855  1.1  lukem 			ber_bvarray_free( rs->sr_ref );
    856  1.1  lukem 			rs->sr_ref = NULL;
    857  1.1  lukem 
    858  1.1  lukem 		} else {
    859  1.1  lukem 			rc = LDAP_OTHER;
    860  1.1  lukem 			rs->sr_text = "bad referral object";
    861  1.1  lukem 		}
    862  1.1  lukem 
    863  1.1  lukem 		rs->sr_matched = NULL;
    864  1.1  lukem 		ber_bvarray_free( refs );
    865  1.1  lukem 	}
    866  1.1  lukem 
    867  1.1  lukem 	entry_free( entry );
    868  1.1  lukem 
    869  1.1  lukem 	return rc;
    870  1.1  lukem }
    871  1.1  lukem 
    872  1.1  lukem 
    873  1.1  lukem /* LDAP operations */
    874  1.1  lukem 
    875  1.1  lukem static int
    876  1.1  lukem ldif_back_bind( Operation *op, SlapReply *rs )
    877  1.1  lukem {
    878  1.1  lukem 	struct ldif_info *li;
    879  1.1  lukem 	Attribute *a;
    880  1.1  lukem 	AttributeDescription *password = slap_schema.si_ad_userPassword;
    881  1.1  lukem 	int return_val;
    882  1.1  lukem 	Entry *entry;
    883  1.1  lukem 
    884  1.1  lukem 	switch ( be_rootdn_bind( op, rs ) ) {
    885  1.1  lukem 	case SLAP_CB_CONTINUE:
    886  1.1  lukem 		break;
    887  1.1  lukem 
    888  1.1  lukem 	default:
    889  1.1  lukem 		/* in case of success, front end will send result;
    890  1.1  lukem 		 * otherwise, be_rootdn_bind() did */
    891  1.1  lukem 		return rs->sr_err;
    892  1.1  lukem 	}
    893  1.1  lukem 
    894  1.1  lukem 	li = (struct ldif_info *) op->o_bd->be_private;
    895  1.1  lukem 	ldap_pvt_thread_rdwr_rlock(&li->li_rdwr);
    896  1.1  lukem 	return_val = get_entry(op, &entry, NULL);
    897  1.1  lukem 
    898  1.1  lukem 	/* no object is found for them */
    899  1.1  lukem 	if(return_val != LDAP_SUCCESS) {
    900  1.1  lukem 		rs->sr_err = return_val = LDAP_INVALID_CREDENTIALS;
    901  1.1  lukem 		goto return_result;
    902  1.1  lukem 	}
    903  1.1  lukem 
    904  1.1  lukem 	/* they don't have userpassword */
    905  1.1  lukem 	if((a = attr_find(entry->e_attrs, password)) == NULL) {
    906  1.1  lukem 		rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
    907  1.1  lukem 		return_val = 1;
    908  1.1  lukem 		goto return_result;
    909  1.1  lukem 	}
    910  1.1  lukem 
    911  1.1  lukem 	/* authentication actually failed */
    912  1.1  lukem 	if(slap_passwd_check(op, entry, a, &op->oq_bind.rb_cred,
    913  1.1  lukem 			     &rs->sr_text) != 0) {
    914  1.1  lukem 		rs->sr_err = LDAP_INVALID_CREDENTIALS;
    915  1.1  lukem 		return_val = 1;
    916  1.1  lukem 		goto return_result;
    917  1.1  lukem 	}
    918  1.1  lukem 
    919  1.1  lukem 	/* let the front-end send success */
    920  1.1  lukem 	return_val = 0;
    921  1.1  lukem 	goto return_result;
    922  1.1  lukem 
    923  1.1  lukem  return_result:
    924  1.1  lukem 	ldap_pvt_thread_rdwr_runlock(&li->li_rdwr);
    925  1.1  lukem 	if(return_val != 0)
    926  1.1  lukem 		send_ldap_result( op, rs );
    927  1.1  lukem 	if(entry != NULL)
    928  1.1  lukem 		entry_free(entry);
    929  1.1  lukem 	return return_val;
    930  1.1  lukem }
    931  1.1  lukem 
    932  1.1  lukem static int ldif_back_search(Operation *op, SlapReply *rs)
    933  1.1  lukem {
    934  1.1  lukem 	struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
    935  1.1  lukem 	enumCookie ck = { NULL, NULL, NULL, 0, 0 };
    936  1.1  lukem 
    937  1.1  lukem 	ck.op = op;
    938  1.1  lukem 	ck.rs = rs;
    939  1.1  lukem 	ldap_pvt_thread_rdwr_rlock(&li->li_rdwr);
    940  1.1  lukem 	rs->sr_err = enum_tree( &ck );
    941  1.1  lukem 	ldap_pvt_thread_rdwr_runlock(&li->li_rdwr);
    942  1.1  lukem 	send_ldap_result(op, rs);
    943  1.1  lukem 
    944  1.1  lukem 	return rs->sr_err;
    945  1.1  lukem }
    946  1.1  lukem 
    947  1.1  lukem static int ldif_back_add(Operation *op, SlapReply *rs) {
    948  1.1  lukem 	struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
    949  1.1  lukem 	Entry * e = op->ora_e;
    950  1.1  lukem 	struct berval dn = e->e_nname;
    951  1.1  lukem 	struct berval leaf_path = BER_BVNULL;
    952  1.1  lukem 	struct stat stats;
    953  1.1  lukem 	int statres;
    954  1.1  lukem 	char textbuf[SLAP_TEXT_BUFLEN];
    955  1.1  lukem 
    956  1.1  lukem 	Debug( LDAP_DEBUG_TRACE, "ldif_back_add: \"%s\"\n", dn.bv_val, 0, 0);
    957  1.1  lukem 
    958  1.1  lukem 	rs->sr_err = entry_schema_check(op, e, NULL, 0, 1,
    959  1.1  lukem 		&rs->sr_text, textbuf, sizeof( textbuf ) );
    960  1.1  lukem 	if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
    961  1.1  lukem 
    962  1.1  lukem 	rs->sr_err = slap_add_opattrs( op,
    963  1.1  lukem 		&rs->sr_text, textbuf, sizeof( textbuf ), 1 );
    964  1.1  lukem 	if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
    965  1.1  lukem 
    966  1.1  lukem 	ldap_pvt_thread_rdwr_wlock(&li->li_rdwr);
    967  1.1  lukem 
    968  1.1  lukem 	dn2path( op->o_bd, &dn, &leaf_path );
    969  1.1  lukem 
    970  1.1  lukem 	if(leaf_path.bv_val != NULL) {
    971  1.1  lukem 		struct berval base = BER_BVNULL;
    972  1.1  lukem 		/* build path to container and ldif of container */
    973  1.1  lukem 		get_parent_path(&leaf_path, &base);
    974  1.1  lukem 
    975  1.1  lukem 		statres = stat(base.bv_val, &stats); /* check if container exists */
    976  1.1  lukem 		if(statres == -1 && errno == ENOENT) { /* container missing */
    977  1.1  lukem 			base.bv_val[base.bv_len] = LDIF_FILETYPE_SEP;
    978  1.1  lukem 			statres = stat(base.bv_val, &stats); /* check for leaf node */
    979  1.1  lukem 			base.bv_val[base.bv_len] = '\0';
    980  1.1  lukem 			if(statres == -1 && errno == ENOENT) {
    981  1.1  lukem 				rs->sr_err = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
    982  1.1  lukem 				rs->sr_text = "Parent does not exist";
    983  1.1  lukem 			}
    984  1.1  lukem 			else if(statres != -1) { /* create parent */
    985  1.1  lukem 				int mkdirres = mkdir(base.bv_val, 0750);
    986  1.1  lukem 				if(mkdirres == -1) {
    987  1.1  lukem 					rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
    988  1.1  lukem 					rs->sr_text = "Could not create parent folder";
    989  1.1  lukem 					Debug( LDAP_DEBUG_ANY, "could not create folder \"%s\": %s\n",
    990  1.1  lukem 						base.bv_val, STRERROR( errno ), 0 );
    991  1.1  lukem 				}
    992  1.1  lukem 			}
    993  1.1  lukem 			else
    994  1.1  lukem 				rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
    995  1.1  lukem 		}/* container was possibly created, move on to add the entry */
    996  1.1  lukem 		if(rs->sr_err == LDAP_SUCCESS) {
    997  1.1  lukem 			statres = stat(leaf_path.bv_val, &stats);
    998  1.1  lukem 			if(statres == -1 && errno == ENOENT) {
    999  1.1  lukem 				rs->sr_err = spew_entry(e, &leaf_path, 1, NULL);
   1000  1.1  lukem 			}
   1001  1.1  lukem 			else if ( statres == -1 ) {
   1002  1.1  lukem 				rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
   1003  1.1  lukem 				Debug( LDAP_DEBUG_ANY, "could not stat file \"%s\": %s\n",
   1004  1.1  lukem 					leaf_path.bv_val, STRERROR( errno ), 0 );
   1005  1.1  lukem 			}
   1006  1.1  lukem 			else /* it already exists */
   1007  1.1  lukem 				rs->sr_err = LDAP_ALREADY_EXISTS;
   1008  1.1  lukem 		}
   1009  1.1  lukem 		SLAP_FREE(base.bv_val);
   1010  1.1  lukem 		SLAP_FREE(leaf_path.bv_val);
   1011  1.1  lukem 	}
   1012  1.1  lukem 
   1013  1.1  lukem 	ldap_pvt_thread_rdwr_wunlock(&li->li_rdwr);
   1014  1.1  lukem 
   1015  1.1  lukem send_res:
   1016  1.1  lukem 	Debug( LDAP_DEBUG_TRACE,
   1017  1.1  lukem 			"ldif_back_add: err: %d text: %s\n", rs->sr_err, rs->sr_text ?
   1018  1.1  lukem 				rs->sr_text : "", 0);
   1019  1.1  lukem 	send_ldap_result(op, rs);
   1020  1.1  lukem 	slap_graduate_commit_csn( op );
   1021  1.1  lukem 	return rs->sr_err;
   1022  1.1  lukem }
   1023  1.1  lukem 
   1024  1.1  lukem static int ldif_back_modify(Operation *op, SlapReply *rs) {
   1025  1.1  lukem 	struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
   1026  1.1  lukem 	Modifications * modlst = op->orm_modlist;
   1027  1.1  lukem 	struct berval path;
   1028  1.1  lukem 	Entry *entry;
   1029  1.1  lukem 	int spew_res;
   1030  1.1  lukem 
   1031  1.1  lukem 	slap_mods_opattrs( op, &op->orm_modlist, 1 );
   1032  1.1  lukem 
   1033  1.1  lukem 	ldap_pvt_thread_rdwr_wlock(&li->li_rdwr);
   1034  1.1  lukem 
   1035  1.1  lukem 	rs->sr_err = get_entry( op, &entry, &path );
   1036  1.1  lukem 	if(entry != NULL) {
   1037  1.1  lukem 		rs->sr_err = apply_modify_to_entry(entry, modlst, op, rs);
   1038  1.1  lukem 		if(rs->sr_err == LDAP_SUCCESS) {
   1039  1.1  lukem 			int save_errno;
   1040  1.1  lukem 			spew_res = spew_entry(entry, &path, 1, &save_errno);
   1041  1.1  lukem 			if(spew_res == -1) {
   1042  1.1  lukem 				Debug( LDAP_DEBUG_ANY,
   1043  1.1  lukem 					"%s ldif_back_modify: could not output entry \"%s\": %s\n",
   1044  1.1  lukem 					op->o_log_prefix, entry->e_name.bv_val, STRERROR( save_errno ) );
   1045  1.1  lukem 				rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
   1046  1.1  lukem 			}
   1047  1.1  lukem 		}
   1048  1.1  lukem 
   1049  1.1  lukem 		entry_free( entry );
   1050  1.1  lukem 		SLAP_FREE( path.bv_val );
   1051  1.1  lukem 	}
   1052  1.1  lukem 
   1053  1.1  lukem 	rs->sr_text = NULL;
   1054  1.1  lukem 	ldap_pvt_thread_rdwr_wunlock(&li->li_rdwr);
   1055  1.1  lukem 	send_ldap_result(op, rs);
   1056  1.1  lukem 	slap_graduate_commit_csn( op );
   1057  1.1  lukem 	return rs->sr_err;
   1058  1.1  lukem }
   1059  1.1  lukem 
   1060  1.1  lukem static int ldif_back_delete(Operation *op, SlapReply *rs) {
   1061  1.1  lukem 	struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
   1062  1.1  lukem 	struct berval path;
   1063  1.1  lukem 	int res = 0;
   1064  1.1  lukem 
   1065  1.1  lukem 	if ( BER_BVISEMPTY( &op->o_csn )) {
   1066  1.1  lukem 		struct berval csn;
   1067  1.1  lukem 		char csnbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
   1068  1.1  lukem 
   1069  1.1  lukem 		csn.bv_val = csnbuf;
   1070  1.1  lukem 		csn.bv_len = sizeof( csnbuf );
   1071  1.1  lukem 		slap_get_csn( op, &csn, 1 );
   1072  1.1  lukem 	}
   1073  1.1  lukem 
   1074  1.1  lukem 	ldap_pvt_thread_rdwr_wlock(&li->li_rdwr);
   1075  1.1  lukem 
   1076  1.1  lukem 	dn2path( op->o_bd, &op->o_req_ndn, &path );
   1077  1.1  lukem 	path.bv_val[path.bv_len - STRLENOF(LDIF)] = '\0';
   1078  1.1  lukem 	res = rmdir(path.bv_val);
   1079  1.1  lukem 	path.bv_val[path.bv_len - STRLENOF(LDIF)] = LDIF_FILETYPE_SEP;
   1080  1.1  lukem 	rs->sr_err = LDAP_SUCCESS;
   1081  1.1  lukem 	if ( res ) {
   1082  1.1  lukem 		switch ( errno ) {
   1083  1.1  lukem 		case ENOTEMPTY:
   1084  1.1  lukem 			rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
   1085  1.1  lukem 			break;
   1086  1.1  lukem 
   1087  1.1  lukem 		case ENOENT:
   1088  1.1  lukem 			/* is leaf, go on */
   1089  1.1  lukem 			res = 0;
   1090  1.1  lukem 			break;
   1091  1.1  lukem 
   1092  1.1  lukem 		default:
   1093  1.1  lukem 			rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
   1094  1.1  lukem 			break;
   1095  1.1  lukem 		}
   1096  1.1  lukem 	}
   1097  1.1  lukem 
   1098  1.1  lukem 	if ( !res ) {
   1099  1.1  lukem 		res = unlink(path.bv_val);
   1100  1.1  lukem 		if ( res == -1 ) {
   1101  1.1  lukem 			switch ( errno ) {
   1102  1.1  lukem 			case ENOENT:
   1103  1.1  lukem 				rs->sr_err = LDAP_NO_SUCH_OBJECT;
   1104  1.1  lukem 				break;
   1105  1.1  lukem 
   1106  1.1  lukem 			default:
   1107  1.1  lukem 				rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
   1108  1.1  lukem 				break;
   1109  1.1  lukem 			}
   1110  1.1  lukem 		}
   1111  1.1  lukem 	}
   1112  1.1  lukem 
   1113  1.1  lukem 	SLAP_FREE(path.bv_val);
   1114  1.1  lukem 	ldap_pvt_thread_rdwr_wunlock(&li->li_rdwr);
   1115  1.1  lukem 	send_ldap_result(op, rs);
   1116  1.1  lukem 	slap_graduate_commit_csn( op );
   1117  1.1  lukem 	return rs->sr_err;
   1118  1.1  lukem }
   1119  1.1  lukem 
   1120  1.1  lukem 
   1121  1.1  lukem static int
   1122  1.1  lukem ldif_move_entry(
   1123  1.1  lukem 	Operation *op,
   1124  1.1  lukem 	Entry *entry,
   1125  1.1  lukem 	struct berval *oldpath )
   1126  1.1  lukem {
   1127  1.1  lukem 	int res;
   1128  1.1  lukem 	int exists_res;
   1129  1.1  lukem 	struct berval newpath;
   1130  1.1  lukem 
   1131  1.1  lukem 	dn2path( op->o_bd, &entry->e_nname, &newpath );
   1132  1.1  lukem 
   1133  1.1  lukem 	if((entry == NULL || oldpath->bv_val == NULL) || newpath.bv_val == NULL) {
   1134  1.1  lukem 		/* some object doesn't exist */
   1135  1.1  lukem 		res = LDAP_NO_SUCH_OBJECT;
   1136  1.1  lukem 	}
   1137  1.1  lukem 	else { /* do the modrdn */
   1138  1.1  lukem 		exists_res = open(newpath.bv_val, O_RDONLY);
   1139  1.1  lukem 		if(exists_res == -1 && errno == ENOENT) {
   1140  1.1  lukem 			ldap_pvt_thread_mutex_lock( &entry2str_mutex );
   1141  1.1  lukem 			res = spew_entry(entry, &newpath, 0, NULL);
   1142  1.1  lukem 			if(res != -1) {
   1143  1.1  lukem 				/* if this fails we should log something bad */
   1144  1.1  lukem 				res = unlink( oldpath->bv_val );
   1145  1.1  lukem 				oldpath->bv_val[oldpath->bv_len - STRLENOF(".ldif")] = '\0';
   1146  1.1  lukem 				newpath.bv_val[newpath.bv_len - STRLENOF(".ldif")] = '\0';
   1147  1.1  lukem 				res = rename( oldpath->bv_val, newpath.bv_val );
   1148  1.1  lukem 				res = LDAP_SUCCESS;
   1149  1.1  lukem 			}
   1150  1.1  lukem 			else {
   1151  1.1  lukem 				if(errno == ENOENT)
   1152  1.1  lukem 					res = LDAP_NO_SUCH_OBJECT;
   1153  1.1  lukem 				else
   1154  1.1  lukem 					res = LDAP_UNWILLING_TO_PERFORM;
   1155  1.1  lukem 				unlink(newpath.bv_val); /* in case file was created */
   1156  1.1  lukem 			}
   1157  1.1  lukem 			ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
   1158  1.1  lukem 		}
   1159  1.1  lukem 		else if(exists_res) {
   1160  1.1  lukem 			int close_res = close(exists_res);
   1161  1.1  lukem 			res = LDAP_ALREADY_EXISTS;
   1162  1.1  lukem 			if(close_res == -1) {
   1163  1.1  lukem 			/* log heinous error */
   1164  1.1  lukem 			}
   1165  1.1  lukem 		}
   1166  1.1  lukem 		else {
   1167  1.1  lukem 			res = LDAP_UNWILLING_TO_PERFORM;
   1168  1.1  lukem 		}
   1169  1.1  lukem 	}
   1170  1.1  lukem 
   1171  1.1  lukem 	if(newpath.bv_val != NULL)
   1172  1.1  lukem 		SLAP_FREE(newpath.bv_val);
   1173  1.1  lukem 	return res;
   1174  1.1  lukem }
   1175  1.1  lukem 
   1176  1.1  lukem static int
   1177  1.1  lukem ldif_back_modrdn(Operation *op, SlapReply *rs)
   1178  1.1  lukem {
   1179  1.1  lukem 	struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
   1180  1.1  lukem 	struct berval new_dn = BER_BVNULL, new_ndn = BER_BVNULL;
   1181  1.1  lukem 	struct berval p_dn, old_path;
   1182  1.1  lukem 	Entry *entry;
   1183  1.1  lukem 	int rc;
   1184  1.1  lukem 
   1185  1.1  lukem 	slap_mods_opattrs( op, &op->orr_modlist, 1 );
   1186  1.1  lukem 
   1187  1.1  lukem 	ldap_pvt_thread_rdwr_wlock( &li->li_rdwr );
   1188  1.1  lukem 
   1189  1.1  lukem 	rc = get_entry( op, &entry, &old_path );
   1190  1.1  lukem 	if ( rc == LDAP_SUCCESS ) {
   1191  1.1  lukem 		/* build new dn, and new ndn for the entry */
   1192  1.1  lukem 		if ( op->oq_modrdn.rs_newSup != NULL ) {
   1193  1.1  lukem 			struct berval	op_dn = op->o_req_dn,
   1194  1.1  lukem 					op_ndn = op->o_req_ndn;
   1195  1.1  lukem 			Entry		*np;
   1196  1.1  lukem 
   1197  1.1  lukem 			/* new superior */
   1198  1.1  lukem 			p_dn = *op->oq_modrdn.rs_newSup;
   1199  1.1  lukem 			op->o_req_dn = *op->oq_modrdn.rs_newSup;
   1200  1.1  lukem 			op->o_req_ndn = *op->oq_modrdn.rs_nnewSup;
   1201  1.1  lukem 			rc = get_entry( op, &np, NULL );
   1202  1.1  lukem 			op->o_req_dn = op_dn;
   1203  1.1  lukem 			op->o_req_ndn = op_ndn;
   1204  1.1  lukem 			if ( rc != LDAP_SUCCESS ) {
   1205  1.1  lukem 				goto no_such_object;
   1206  1.1  lukem 			}
   1207  1.1  lukem 			entry_free( np );
   1208  1.1  lukem 		} else {
   1209  1.1  lukem 			dnParent( &entry->e_name, &p_dn );
   1210  1.1  lukem 		}
   1211  1.1  lukem 		build_new_dn( &new_dn, &p_dn, &op->oq_modrdn.rs_newrdn, NULL );
   1212  1.1  lukem 		dnNormalize( 0, NULL, NULL, &new_dn, &new_ndn, NULL );
   1213  1.1  lukem 		ber_memfree_x( entry->e_name.bv_val, NULL );
   1214  1.1  lukem 		ber_memfree_x( entry->e_nname.bv_val, NULL );
   1215  1.1  lukem 		entry->e_name = new_dn;
   1216  1.1  lukem 		entry->e_nname = new_ndn;
   1217  1.1  lukem 
   1218  1.1  lukem 		/* perform the modifications */
   1219  1.1  lukem 		rc = apply_modify_to_entry( entry, op->orr_modlist, op, rs );
   1220  1.1  lukem 		if ( rc == LDAP_SUCCESS )
   1221  1.1  lukem 			rc = ldif_move_entry( op, entry, &old_path );
   1222  1.1  lukem 
   1223  1.1  lukem no_such_object:;
   1224  1.1  lukem 		entry_free( entry );
   1225  1.1  lukem 		SLAP_FREE( old_path.bv_val );
   1226  1.1  lukem 	}
   1227  1.1  lukem 
   1228  1.1  lukem 	rs->sr_text = "";
   1229  1.1  lukem 	ldap_pvt_thread_rdwr_wunlock( &li->li_rdwr );
   1230  1.1  lukem 	rs->sr_err = rc;
   1231  1.1  lukem 	send_ldap_result( op, rs );
   1232  1.1  lukem 	slap_graduate_commit_csn( op );
   1233  1.1  lukem 	return rs->sr_err;
   1234  1.1  lukem }
   1235  1.1  lukem 
   1236  1.1  lukem 
   1237  1.1  lukem /* Return LDAP_SUCCESS IFF we retrieve the specified entry. */
   1238  1.1  lukem static int
   1239  1.1  lukem ldif_back_entry_get(
   1240  1.1  lukem 	Operation *op,
   1241  1.1  lukem 	struct berval *ndn,
   1242  1.1  lukem 	ObjectClass *oc,
   1243  1.1  lukem 	AttributeDescription *at,
   1244  1.1  lukem 	int rw,
   1245  1.1  lukem 	Entry **e )
   1246  1.1  lukem {
   1247  1.1  lukem 	struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
   1248  1.1  lukem 	struct berval op_dn = op->o_req_dn, op_ndn = op->o_req_ndn;
   1249  1.1  lukem 	int rc;
   1250  1.1  lukem 
   1251  1.1  lukem 	assert( ndn != NULL );
   1252  1.1  lukem 	assert( !BER_BVISNULL( ndn ) );
   1253  1.1  lukem 
   1254  1.1  lukem 	ldap_pvt_thread_rdwr_rlock( &li->li_rdwr );
   1255  1.1  lukem 	op->o_req_dn = *ndn;
   1256  1.1  lukem 	op->o_req_ndn = *ndn;
   1257  1.1  lukem 	rc = get_entry( op, e, NULL );
   1258  1.1  lukem 	op->o_req_dn = op_dn;
   1259  1.1  lukem 	op->o_req_ndn = op_ndn;
   1260  1.1  lukem 	ldap_pvt_thread_rdwr_runlock( &li->li_rdwr );
   1261  1.1  lukem 
   1262  1.1  lukem 	if ( rc == LDAP_SUCCESS && oc && !is_entry_objectclass_or_sub( *e, oc ) ) {
   1263  1.1  lukem 		rc = LDAP_NO_SUCH_ATTRIBUTE;
   1264  1.1  lukem 		entry_free( *e );
   1265  1.1  lukem 		*e = NULL;
   1266  1.1  lukem 	}
   1267  1.1  lukem 
   1268  1.1  lukem 	return rc;
   1269  1.1  lukem }
   1270  1.1  lukem 
   1271  1.1  lukem 
   1272  1.1  lukem /* Slap tools */
   1273  1.1  lukem 
   1274  1.1  lukem static int ldif_tool_entry_open(BackendDB *be, int mode) {
   1275  1.1  lukem 	struct ldif_info *li = (struct ldif_info *) be->be_private;
   1276  1.1  lukem 	li->li_tool_current = 0;
   1277  1.1  lukem 	return 0;
   1278  1.1  lukem }
   1279  1.1  lukem 
   1280  1.1  lukem static int ldif_tool_entry_close(BackendDB * be) {
   1281  1.1  lukem 	struct ldif_info *li = (struct ldif_info *) be->be_private;
   1282  1.1  lukem 
   1283  1.1  lukem 	SLAP_FREE(li->li_tool_cookie.entries);
   1284  1.1  lukem 	return 0;
   1285  1.1  lukem }
   1286  1.1  lukem 
   1287  1.1  lukem static ID ldif_tool_entry_next(BackendDB *be)
   1288  1.1  lukem {
   1289  1.1  lukem 	struct ldif_info *li = (struct ldif_info *) be->be_private;
   1290  1.1  lukem 	if(li->li_tool_current >= li->li_tool_cookie.eind)
   1291  1.1  lukem 		return NOID;
   1292  1.1  lukem 	else
   1293  1.1  lukem 		return ++li->li_tool_current;
   1294  1.1  lukem }
   1295  1.1  lukem 
   1296  1.1  lukem static ID
   1297  1.1  lukem ldif_tool_entry_first(BackendDB *be)
   1298  1.1  lukem {
   1299  1.1  lukem 	struct ldif_info *li = (struct ldif_info *) be->be_private;
   1300  1.1  lukem 
   1301  1.1  lukem 	if(li->li_tool_cookie.entries == NULL) {
   1302  1.1  lukem 		Operation op = {0};
   1303  1.1  lukem 
   1304  1.1  lukem 		op.o_bd = be;
   1305  1.1  lukem 		op.o_req_dn = *be->be_suffix;
   1306  1.1  lukem 		op.o_req_ndn = *be->be_nsuffix;
   1307  1.1  lukem 		op.ors_scope = LDAP_SCOPE_SUBTREE;
   1308  1.1  lukem 		li->li_tool_cookie.op = &op;
   1309  1.1  lukem 		(void)enum_tree( &li->li_tool_cookie );
   1310  1.1  lukem 		li->li_tool_cookie.op = NULL;
   1311  1.1  lukem 	}
   1312  1.1  lukem 	return ldif_tool_entry_next( be );
   1313  1.1  lukem }
   1314  1.1  lukem 
   1315  1.1  lukem static Entry * ldif_tool_entry_get(BackendDB * be, ID id) {
   1316  1.1  lukem 	struct ldif_info *li = (struct ldif_info *) be->be_private;
   1317  1.1  lukem 	Entry * e;
   1318  1.1  lukem 
   1319  1.1  lukem 	if(id > li->li_tool_cookie.eind || id < 1)
   1320  1.1  lukem 		return NULL;
   1321  1.1  lukem 	else {
   1322  1.1  lukem 		e = li->li_tool_cookie.entries[id - 1];
   1323  1.1  lukem 		li->li_tool_cookie.entries[id - 1] = NULL;
   1324  1.1  lukem 		return e;
   1325  1.1  lukem 	}
   1326  1.1  lukem }
   1327  1.1  lukem 
   1328  1.1  lukem static ID ldif_tool_entry_put(BackendDB * be, Entry * e, struct berval *text) {
   1329  1.1  lukem 	struct berval leaf_path = BER_BVNULL;
   1330  1.1  lukem 	struct stat stats;
   1331  1.1  lukem 	int statres;
   1332  1.1  lukem 	int res = LDAP_SUCCESS;
   1333  1.1  lukem 
   1334  1.1  lukem 	dn2path( be, &e->e_nname, &leaf_path );
   1335  1.1  lukem 
   1336  1.1  lukem 	if(leaf_path.bv_val != NULL) {
   1337  1.1  lukem 		struct berval base = BER_BVNULL;
   1338  1.1  lukem 		/* build path to container, and path to ldif of container */
   1339  1.1  lukem 		get_parent_path(&leaf_path, &base);
   1340  1.1  lukem 
   1341  1.1  lukem 		statres = stat(base.bv_val, &stats); /* check if container exists */
   1342  1.1  lukem 		if(statres == -1 && errno == ENOENT) { /* container missing */
   1343  1.1  lukem 			base.bv_val[base.bv_len] = LDIF_FILETYPE_SEP;
   1344  1.1  lukem 			statres = stat(base.bv_val, &stats); /* check for leaf node */
   1345  1.1  lukem 			base.bv_val[base.bv_len] = '\0';
   1346  1.1  lukem 			if(statres == -1 && errno == ENOENT) {
   1347  1.1  lukem 				res = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
   1348  1.1  lukem 			}
   1349  1.1  lukem 			else if(statres != -1) { /* create parent */
   1350  1.1  lukem 				int mkdirres = mkdir(base.bv_val, 0750);
   1351  1.1  lukem 				if(mkdirres == -1) {
   1352  1.1  lukem 					res = LDAP_UNWILLING_TO_PERFORM;
   1353  1.1  lukem 				}
   1354  1.1  lukem 			}
   1355  1.1  lukem 			else
   1356  1.1  lukem 				res = LDAP_UNWILLING_TO_PERFORM;
   1357  1.1  lukem 		}/* container was possibly created, move on to add the entry */
   1358  1.1  lukem 		if(res == LDAP_SUCCESS) {
   1359  1.1  lukem 			statres = stat(leaf_path.bv_val, &stats);
   1360  1.1  lukem 			if(statres == -1 && errno == ENOENT) {
   1361  1.1  lukem 				res = spew_entry(e, &leaf_path, 0, NULL);
   1362  1.1  lukem 			}
   1363  1.1  lukem 			else /* it already exists */
   1364  1.1  lukem 				res = LDAP_ALREADY_EXISTS;
   1365  1.1  lukem 		}
   1366  1.1  lukem 		SLAP_FREE(base.bv_val);
   1367  1.1  lukem 		SLAP_FREE(leaf_path.bv_val);
   1368  1.1  lukem 	}
   1369  1.1  lukem 
   1370  1.1  lukem 	if(res == LDAP_SUCCESS) {
   1371  1.1  lukem 		return 1;
   1372  1.1  lukem 	}
   1373  1.1  lukem 	else
   1374  1.1  lukem 		return NOID;
   1375  1.1  lukem }
   1376  1.1  lukem 
   1377  1.1  lukem 
   1378  1.1  lukem /* Setup */
   1379  1.1  lukem 
   1380  1.1  lukem static int
   1381  1.1  lukem ldif_back_db_init( BackendDB *be, ConfigReply *cr )
   1382  1.1  lukem {
   1383  1.1  lukem 	struct ldif_info *li;
   1384  1.1  lukem 
   1385  1.1  lukem 	li = ch_calloc( 1, sizeof(struct ldif_info) );
   1386  1.1  lukem 	be->be_private = li;
   1387  1.1  lukem 	be->be_cf_ocs = ldifocs;
   1388  1.1  lukem 	ldap_pvt_thread_rdwr_init(&li->li_rdwr);
   1389  1.1  lukem 	SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_ONE_SUFFIX;
   1390  1.1  lukem 	return 0;
   1391  1.1  lukem }
   1392  1.1  lukem 
   1393  1.1  lukem static int
   1394  1.1  lukem ldif_back_db_destroy( Backend *be, ConfigReply *cr )
   1395  1.1  lukem {
   1396  1.1  lukem 	struct ldif_info *li = be->be_private;
   1397  1.1  lukem 
   1398  1.1  lukem 	ch_free(li->li_base_path.bv_val);
   1399  1.1  lukem 	ldap_pvt_thread_rdwr_destroy(&li->li_rdwr);
   1400  1.1  lukem 	free( be->be_private );
   1401  1.1  lukem 	return 0;
   1402  1.1  lukem }
   1403  1.1  lukem 
   1404  1.1  lukem static int
   1405  1.1  lukem ldif_back_db_open( Backend *be, ConfigReply *cr)
   1406  1.1  lukem {
   1407  1.1  lukem 	struct ldif_info *li = (struct ldif_info *) be->be_private;
   1408  1.1  lukem 	if( BER_BVISEMPTY(&li->li_base_path)) {/* missing base path */
   1409  1.1  lukem 		Debug( LDAP_DEBUG_ANY, "missing base path for back-ldif\n", 0, 0, 0);
   1410  1.1  lukem 		return 1;
   1411  1.1  lukem 	}
   1412  1.1  lukem 	return 0;
   1413  1.1  lukem }
   1414  1.1  lukem 
   1415  1.1  lukem int
   1416  1.1  lukem ldif_back_initialize(
   1417  1.1  lukem 			   BackendInfo	*bi
   1418  1.1  lukem 			   )
   1419  1.1  lukem {
   1420  1.1  lukem 	static char *controls[] = {
   1421  1.1  lukem 		LDAP_CONTROL_MANAGEDSAIT,
   1422  1.1  lukem 		NULL
   1423  1.1  lukem 	};
   1424  1.1  lukem 	int rc;
   1425  1.1  lukem 
   1426  1.1  lukem 	bi->bi_flags |=
   1427  1.1  lukem 		SLAP_BFLAG_INCREMENT |
   1428  1.1  lukem 		SLAP_BFLAG_REFERRALS;
   1429  1.1  lukem 
   1430  1.1  lukem 	bi->bi_controls = controls;
   1431  1.1  lukem 
   1432  1.1  lukem 	bi->bi_open = 0;
   1433  1.1  lukem 	bi->bi_close = 0;
   1434  1.1  lukem 	bi->bi_config = 0;
   1435  1.1  lukem 	bi->bi_destroy = 0;
   1436  1.1  lukem 
   1437  1.1  lukem 	bi->bi_db_init = ldif_back_db_init;
   1438  1.1  lukem 	bi->bi_db_config = config_generic_wrapper;
   1439  1.1  lukem 	bi->bi_db_open = ldif_back_db_open;
   1440  1.1  lukem 	bi->bi_db_close = 0;
   1441  1.1  lukem 	bi->bi_db_destroy = ldif_back_db_destroy;
   1442  1.1  lukem 
   1443  1.1  lukem 	bi->bi_op_bind = ldif_back_bind;
   1444  1.1  lukem 	bi->bi_op_unbind = 0;
   1445  1.1  lukem 	bi->bi_op_search = ldif_back_search;
   1446  1.1  lukem 	bi->bi_op_compare = 0;
   1447  1.1  lukem 	bi->bi_op_modify = ldif_back_modify;
   1448  1.1  lukem 	bi->bi_op_modrdn = ldif_back_modrdn;
   1449  1.1  lukem 	bi->bi_op_add = ldif_back_add;
   1450  1.1  lukem 	bi->bi_op_delete = ldif_back_delete;
   1451  1.1  lukem 	bi->bi_op_abandon = 0;
   1452  1.1  lukem 
   1453  1.1  lukem 	bi->bi_extended = 0;
   1454  1.1  lukem 
   1455  1.1  lukem 	bi->bi_chk_referrals = ldif_back_referrals;
   1456  1.1  lukem 
   1457  1.1  lukem 	bi->bi_connection_init = 0;
   1458  1.1  lukem 	bi->bi_connection_destroy = 0;
   1459  1.1  lukem 
   1460  1.1  lukem 	bi->bi_entry_get_rw = ldif_back_entry_get;
   1461  1.1  lukem 
   1462  1.1  lukem #if 0	/* NOTE: uncomment to completely disable access control */
   1463  1.1  lukem 	bi->bi_access_allowed = slap_access_always_allowed;
   1464  1.1  lukem #endif
   1465  1.1  lukem 
   1466  1.1  lukem 	bi->bi_tool_entry_open = ldif_tool_entry_open;
   1467  1.1  lukem 	bi->bi_tool_entry_close = ldif_tool_entry_close;
   1468  1.1  lukem 	bi->bi_tool_entry_first = ldif_tool_entry_first;
   1469  1.1  lukem 	bi->bi_tool_entry_next = ldif_tool_entry_next;
   1470  1.1  lukem 	bi->bi_tool_entry_get = ldif_tool_entry_get;
   1471  1.1  lukem 	bi->bi_tool_entry_put = ldif_tool_entry_put;
   1472  1.1  lukem 	bi->bi_tool_entry_reindex = 0;
   1473  1.1  lukem 	bi->bi_tool_sync = 0;
   1474  1.1  lukem 
   1475  1.1  lukem 	bi->bi_tool_dn2id_get = 0;
   1476  1.1  lukem 	bi->bi_tool_entry_modify = 0;
   1477  1.1  lukem 
   1478  1.1  lukem 	bi->bi_cf_ocs = ldifocs;
   1479  1.1  lukem 
   1480  1.1  lukem 	rc = config_register_schema( ldifcfg, ldifocs );
   1481  1.1  lukem 	if ( rc ) return rc;
   1482  1.1  lukem 	return 0;
   1483  1.1  lukem }
   1484