1 /* ldif.c - the ldif backend */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2005-2008 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 * This work was originally developed by Eric Stokes for inclusion
18 * in OpenLDAP Software.
23 #include <ac/string.h>
24 #include <sys/types.h>
26 #include <ac/dirent.h>
29 #include <ac/unistd.h>
35 Entry **entries; /* collected by bi_tool_entry_first() */
36 ID elen; /* length of entries[] array */
37 ID ecount; /* number of entries */
38 ID ecurrent; /* bi_tool_entry_next() position */
39 # define ENTRY_BUFF_INCREMENT 500 /* initial entries[] length */
42 /* Per-database data */
44 struct berval li_base_path; /* database directory */
45 struct ldif_tool li_tool; /* for slap tools */
47 * Read-only LDAP requests readlock li_rdwr for filesystem input.
48 * Update requests first lock li_modop_mutex for filesystem I/O,
49 * and then writelock li_rdwr as well for filesystem output.
50 * This allows update requests to do callbacks that acquire
51 * read locks, e.g. access controls that inspect entries.
52 * (An alternative would be recursive read/write locks.)
54 ldap_pvt_thread_mutex_t li_modop_mutex; /* serialize update requests */
55 ldap_pvt_thread_rdwr_t li_rdwr; /* no other I/O when writing */
59 #define mkdir(a,b) mkdir(a)
60 #define move_file(from, to) (!MoveFileEx(from, to, MOVEFILE_REPLACE_EXISTING))
62 #define move_file(from, to) rename(from, to)
64 #define move_dir(from, to) rename(from, to)
68 #define LDIF_FILETYPE_SEP '.' /* LDIF[0] */
71 * Unsafe/translated characters in the filesystem.
73 * LDIF_UNSAFE_CHAR(c) returns true if the character c is not to be used
74 * in relative filenames, except it should accept '\\', '{' and '}' even
75 * if unsafe. The value should be a constant expression.
77 * If '\\' is unsafe, #define LDIF_ESCAPE_CHAR as a safe character.
78 * If '{' and '}' are unsafe, #define IX_FSL/IX_FSR as safe characters.
79 * (Not digits, '-' or '+'. IX_FSL == IX_FSR is allowed.)
81 * Characters are escaped as LDIF_ESCAPE_CHAR followed by two hex digits,
82 * except '\\' is replaced with LDIF_ESCAPE_CHAR and {} with IX_FS[LR].
83 * Also some LDIF special chars are hex-escaped.
85 * Thus an LDIF filename is a valid normalized RDN (or suffix DN)
86 * followed by ".ldif", except with '\\' replaced with LDIF_ESCAPE_CHAR.
92 * Unix/MacOSX version. ':' vs '/' can cause confusion on MacOSX so we
93 * escape both. We escape them on Unix so both OS variants get the same
96 #define LDIF_ESCAPE_CHAR '\\'
97 #define LDIF_UNSAFE_CHAR(c) ((c) == '/' || (c) == ':')
101 /* Windows version - Microsoft's list of unsafe characters, except '\\' */
102 #define LDIF_ESCAPE_CHAR '^' /* Not '\\' (unsafe on Windows) */
103 #define LDIF_UNSAFE_CHAR(c) \
104 ((c) == '/' || (c) == ':' || \
105 (c) == '<' || (c) == '>' || (c) == '"' || \
106 (c) == '|' || (c) == '?' || (c) == '*')
111 * Left and Right "{num}" prefix to ordered RDNs ("olcDatabase={1}bdb").
112 * IX_DN* are for LDAP RDNs, IX_FS* for their .ldif filenames.
117 #define IX_FSL IX_DNL
118 #define IX_FSR IX_DNR
122 * Test for unsafe chars, as well as chars handled specially by back-ldif:
123 * - If the escape char is not '\\', it must itself be escaped. Otherwise
124 * '\\' and the escape char would map to the same character.
125 * - Escape the '.' in ".ldif", so the directory for an RDN that actually
126 * ends with ".ldif" can not conflict with a file of the same name. And
127 * since some OSes/programs choke on multiple '.'s, escape all of them.
128 * - If '{' and '}' are translated to some other characters, those
129 * characters must in turn be escaped when they occur in an RDN.
131 #ifndef LDIF_NEED_ESCAPE
132 #define LDIF_NEED_ESCAPE(c) \
133 ((LDIF_UNSAFE_CHAR(c)) || \
134 LDIF_MAYBE_UNSAFE(c, LDIF_ESCAPE_CHAR) || \
135 LDIF_MAYBE_UNSAFE(c, LDIF_FILETYPE_SEP) || \
136 LDIF_MAYBE_UNSAFE(c, IX_FSL) || \
137 (IX_FSR != IX_FSL && LDIF_MAYBE_UNSAFE(c, IX_FSR)))
140 * Helper macro for LDIF_NEED_ESCAPE(): Treat character x as unsafe if
141 * back-ldif does not already treat is specially.
143 #define LDIF_MAYBE_UNSAFE(c, x) \
144 (!(LDIF_UNSAFE_CHAR(x) || (x) == '\\' || (x) == IX_DNL || (x) == IX_DNR) \
147 /* Collect other "safe char" tests here, until someone needs a fix. */
149 eq_unsafe = LDIF_UNSAFE_CHAR('='),
150 safe_filenames = STRLENOF("" LDAP_DIRSEP "") == 1 && !(
151 LDIF_UNSAFE_CHAR('-') || /* for "{-1}frontend" in bconfig.c */
152 LDIF_UNSAFE_CHAR(LDIF_ESCAPE_CHAR) ||
153 LDIF_UNSAFE_CHAR(IX_FSL) || LDIF_UNSAFE_CHAR(IX_FSR))
155 /* Sanity check: Try to force a compilation error if !safe_filenames */
157 int assert_safe_filenames : safe_filenames ? 2 : -2;
158 } assert_safe_filenames[safe_filenames ? 2 : -2];
161 static ConfigTable ldifcfg[] = {
162 { "directory", "dir", 2, 2, 0, ARG_BERVAL|ARG_OFFSET,
163 (void *)offsetof(struct ldif_info, li_base_path),
164 "( OLcfgDbAt:0.1 NAME 'olcDbDirectory' "
165 "DESC 'Directory for database content' "
166 "EQUALITY caseIgnoreMatch "
167 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
168 { NULL, NULL, 0, 0, 0, ARG_IGNORED,
169 NULL, NULL, NULL, NULL }
172 static ConfigOCs ldifocs[] = {
174 "NAME 'olcLdifConfig' "
175 "DESC 'LDIF backend configuration' "
176 "SUP olcDatabaseConfig "
177 "MUST ( olcDbDirectory ) )", Cft_Database, ldifcfg },
183 * Handle file/directory names.
186 /* Set *res = LDIF filename path for the normalized DN */
188 dn2path( BackendDB *be, struct berval *dn, struct berval *res )
190 struct ldif_info *li = (struct ldif_info *) be->be_private;
191 struct berval *suffixdn = &be->be_nsuffix[0];
192 const char *start, *end, *next, *p;
195 static const char hex[] = "0123456789ABCDEF";
197 assert( dn != NULL );
198 assert( !BER_BVISNULL( dn ) );
199 assert( suffixdn != NULL );
200 assert( !BER_BVISNULL( suffixdn ) );
201 assert( dnIsSuffix( dn, suffixdn ) );
204 end = start + dn->bv_len;
206 /* Room for dir, dirsep, dn, LDIF, "\hexpair"-escaping of unsafe chars */
207 len = li->li_base_path.bv_len + dn->bv_len + (1 + STRLENOF( LDIF ));
208 for ( p = start; p < end; ) {
210 if ( LDIF_NEED_ESCAPE( ch ) )
213 res->bv_val = ch_malloc( len + 1 );
215 ptr = lutil_strcopy( res->bv_val, li->li_base_path.bv_val );
216 for ( next = end - suffixdn->bv_len; end > start; end = next ) {
217 /* Set p = start of DN component, next = &',' or start of DN */
218 while ( (p = next) > start ) {
220 if ( DN_SEPARATOR( *next ) )
223 /* Append <dirsep> <p..end-1: RDN or database-suffix> */
224 for ( *ptr++ = LDAP_DIRSEP[0]; p < end; *ptr++ = ch ) {
226 if ( LDIF_ESCAPE_CHAR != '\\' && ch == '\\' ) {
227 ch = LDIF_ESCAPE_CHAR;
228 } else if ( IX_FSL != IX_DNL && ch == IX_DNL ) {
230 } else if ( IX_FSR != IX_DNR && ch == IX_DNR ) {
232 } else if ( LDIF_NEED_ESCAPE( ch ) ) {
233 *ptr++ = LDIF_ESCAPE_CHAR;
234 *ptr++ = hex[(ch & 0xFFU) >> 4];
235 ch = hex[ch & 0x0FU];
239 ptr = lutil_strcopy( ptr, LDIF );
240 res->bv_len = ptr - res->bv_val;
242 assert( res->bv_len <= len );
246 * *dest = dupbv(<dir + LDAP_DIRSEP>), plus room for <more>-sized filename.
247 * Return pointer past the dirname.
250 fullpath_alloc( struct berval *dest, const struct berval *dir, ber_len_t more )
252 char *s = SLAP_MALLOC( dir->bv_len + more + 2 );
257 Debug( LDAP_DEBUG_ANY, "back-ldif: out of memory\n", 0, 0, 0 );
259 s = lutil_strcopy( dest->bv_val, dir->bv_val );
260 *s++ = LDAP_DIRSEP[0];
262 dest->bv_len = s - dest->bv_val;
268 * Append filename to fullpath_alloc() dirname or replace previous filename.
269 * dir_end = fullpath_alloc() return value.
271 #define FILL_PATH(fpath, dir_end, filename) \
272 ((fpath)->bv_len = lutil_strcopy(dir_end, filename) - (fpath)->bv_val)
275 /* .ldif entry filename length <-> subtree dirname length. */
276 #define ldif2dir_len(bv) ((bv).bv_len -= STRLENOF(LDIF))
277 #define dir2ldif_len(bv) ((bv).bv_len += STRLENOF(LDIF))
278 /* .ldif entry filename <-> subtree dirname, both with dirname length. */
279 #define ldif2dir_name(bv) ((bv).bv_val[(bv).bv_len] = '\0')
280 #define dir2ldif_name(bv) ((bv).bv_val[(bv).bv_len] = LDIF_FILETYPE_SEP)
282 /* Get the parent directory path, plus the LDIF suffix overwritten by a \0. */
284 get_parent_path( struct berval *dnpath, struct berval *res )
286 ber_len_t i = dnpath->bv_len;
288 while ( i > 0 && dnpath->bv_val[ --i ] != LDAP_DIRSEP[0] ) ;
292 res->bv_val = SLAP_MALLOC( i + 1 + STRLENOF(LDIF) );
293 if ( res->bv_val == NULL )
295 AC_MEMCPY( res->bv_val, dnpath->bv_val, i );
298 strcpy( res->bv_val + i, LDIF );
299 res->bv_val[i] = '\0';
303 /* Make temporary filename pattern for mkstemp() based on dnpath. */
305 ldif_tempname( const struct berval *dnpath )
307 static const char suffix[] = ".XXXXXX";
308 ber_len_t len = dnpath->bv_len - STRLENOF( LDIF );
309 char *name = SLAP_MALLOC( len + sizeof( suffix ) );
311 if ( name != NULL ) {
312 AC_MEMCPY( name, dnpath->bv_val, len );
313 strcpy( name + len, suffix );
319 * Read a file, or stat() it if datap == NULL. Allocate and fill *datap.
320 * Return LDAP_SUCCESS, LDAP_NO_SUCH_OBJECT (no such file), or another error.
323 ldif_read_file( const char *path, char **datap )
326 int res = -1; /* 0:success, <0:error, >0:file too big/growing. */
328 char *data = NULL, *ptr;
330 if ( datap == NULL ) {
331 res = stat( path, &st );
334 fd = open( path, O_RDONLY );
336 if ( fstat( fd, &st ) == 0 ) {
337 if ( st.st_size > INT_MAX - 2 ) {
340 len = st.st_size + 1; /* +1 detects file size > st.st_size */
341 *datap = data = ptr = SLAP_MALLOC( len + 1 );
343 while ( len && (res = read( fd, ptr, len )) ) {
347 } else if ( errno != EINTR ) {
355 if ( close( fd ) < 0 )
361 Debug( LDAP_DEBUG_TRACE, "ldif_read_file: %s: \"%s\"\n",
362 datap ? "read entry file" : "entry file exists", path, 0 );
365 if ( res < 0 && errno == ENOENT ) {
366 Debug( LDAP_DEBUG_TRACE, "ldif_read_file: "
367 "no entry file \"%s\"\n", path, 0, 0 );
368 rc = LDAP_NO_SUCH_OBJECT;
370 const char *msg = res < 0 ? STRERROR( errno ) : "bad stat() size";
371 Debug( LDAP_DEBUG_ANY, "ldif_read_file: %s for \"%s\"\n",
382 * return nonnegative for success or -1 for error
383 * do not return numbers less than -1
386 spew_file( int fd, const char *spew, int len, int *save_errno )
391 writeres = write(fd, spew, len);
394 if (*save_errno != EINTR)
405 /* Write an entry LDIF file. Create parentdir first if non-NULL. */
410 const struct berval *path,
411 const char *parentdir,
414 int rc = LDAP_OTHER, res, save_errno = 0;
415 int fd, entry_length;
416 char *entry_as_string, *tmpfname;
419 return SLAPD_ABANDON;
421 if ( parentdir != NULL && mkdir( parentdir, 0750 ) < 0 ) {
423 Debug( LDAP_DEBUG_ANY, "ldif_write_entry: %s \"%s\": %s\n",
424 "cannot create parent directory",
425 parentdir, STRERROR( save_errno ) );
426 *text = "internal error (cannot create parent directory)";
430 tmpfname = ldif_tempname( path );
431 fd = tmpfname == NULL ? -1 : mkstemp( tmpfname );
434 Debug( LDAP_DEBUG_ANY, "ldif_write_entry: %s for \"%s\": %s\n",
435 "cannot create file", e->e_dn, STRERROR( save_errno ) );
436 *text = "internal error (cannot create file)";
439 ber_len_t dn_len = e->e_name.bv_len;
442 /* Only save the RDN onto disk */
443 dnRdn( &e->e_name, &rdn );
444 if ( rdn.bv_len != dn_len ) {
445 e->e_name.bv_val[rdn.bv_len] = '\0';
446 e->e_name.bv_len = rdn.bv_len;
450 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
451 entry_as_string = entry2str( e, &entry_length );
452 if ( entry_as_string != NULL )
453 res = spew_file( fd, entry_as_string, entry_length, &save_errno );
454 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
456 /* Restore full DN */
457 if ( rdn.bv_len != dn_len ) {
458 e->e_name.bv_val[rdn.bv_len] = ',';
459 e->e_name.bv_len = dn_len;
462 if ( close( fd ) < 0 && res >= 0 ) {
468 if ( move_file( tmpfname, path->bv_val ) == 0 ) {
469 Debug( LDAP_DEBUG_TRACE, "ldif_write_entry: "
470 "wrote entry \"%s\"\n", e->e_name.bv_val, 0, 0 );
474 Debug( LDAP_DEBUG_ANY, "ldif_write_entry: "
475 "could not put entry file for \"%s\" in place: %s\n",
476 e->e_name.bv_val, STRERROR( save_errno ), 0 );
477 *text = "internal error (could not put entry file in place)";
479 } else if ( res == -1 ) {
480 Debug( LDAP_DEBUG_ANY, "ldif_write_entry: %s \"%s\": %s\n",
481 "write error to", tmpfname, STRERROR( save_errno ) );
482 *text = "internal error (write error to entry file)";
485 if ( rc != LDAP_SUCCESS ) {
491 SLAP_FREE( tmpfname );
496 * Read the entry at path, or if entryp==NULL just see if it exists.
497 * pdn and pndn are the parent's DN and normalized DN, or both NULL.
498 * Return an LDAP result code.
511 char *entry_as_string;
514 /* TODO: Does slapd prevent Abandon of Bind as per rfc4511?
515 * If so we need not check for LDAP_REQ_BIND here.
517 if ( op->o_abandon && op->o_tag != LDAP_REQ_BIND )
518 return SLAPD_ABANDON;
520 rc = ldif_read_file( path, entryp ? &entry_as_string : NULL );
524 if ( entryp == NULL )
526 *entryp = entry = str2entry( entry_as_string );
527 SLAP_FREE( entry_as_string );
528 if ( entry == NULL ) {
531 *text = "internal error (cannot parse some entry file)";
534 if ( pdn == NULL || BER_BVISEMPTY( pdn ) )
536 /* Append parent DN to DN from LDIF file */
538 build_new_dn( &entry->e_name, pdn, &rdn, NULL );
539 SLAP_FREE( rdn.bv_val );
540 rdn = entry->e_nname;
541 build_new_dn( &entry->e_nname, pndn, &rdn, NULL );
542 SLAP_FREE( rdn.bv_val );
548 ? "internal error (cannot read some entry file)"
549 : "internal error (cannot stat some entry file)";
557 * Read the operation's entry, or if entryp==NULL just see if it exists.
558 * Return an LDAP result code. May set *text to a message on failure.
559 * If pathp is non-NULL, set it to the entry filename on success.
565 struct berval *pathp,
569 struct berval path, pdn, pndn;
571 dnParent( &op->o_req_dn, &pdn );
572 dnParent( &op->o_req_ndn, &pndn );
573 dn2path( op->o_bd, &op->o_req_ndn, &path );
574 rc = ldif_read_entry( op, path.bv_val, &pdn, &pndn, entryp, text );
576 if ( rc == LDAP_SUCCESS && pathp != NULL ) {
579 SLAP_FREE( path.bv_val );
586 * RDN-named directory entry, with special handling of "attr={num}val" RDNs.
587 * For sorting, filename "attr=val.ldif" is truncated to "attr="val\0ldif",
588 * and filename "attr={num}val.ldif" to "attr={\0um}val.ldif".
589 * Does not sort escaped chars correctly, would need to un-escape them.
591 typedef struct bvlist {
593 char *trunc; /* filename was truncated here */
594 int inum; /* num from "attr={num}" in filename, or INT_MIN */
595 char savech; /* original char at *trunc */
596 char fname; /* variable length array BVL_NAME(bvl) = &fname */
597 # define BVL_NAME(bvl) ((char *) (bvl) + offsetof(bvlist, fname))
598 # define BVL_SIZE(namelen) (sizeof(bvlist) + (namelen))
602 ldif_send_entry( Operation *op, SlapReply *rs, Entry *e, int scope )
604 int rc = LDAP_SUCCESS;
606 if ( scope == LDAP_SCOPE_BASE || scope == LDAP_SCOPE_SUBTREE ) {
608 /* Save the entry for tool mode */
609 struct ldif_tool *tl =
610 &((struct ldif_info *) op->o_bd->be_private)->li_tool;
612 if ( tl->ecount >= tl->elen ) {
613 /* Allocate/grow entries */
614 ID elen = tl->elen ? tl->elen * 2 : ENTRY_BUFF_INCREMENT;
615 Entry **entries = (Entry **) SLAP_REALLOC( tl->entries,
616 sizeof(Entry *) * elen );
617 if ( entries == NULL ) {
618 Debug( LDAP_DEBUG_ANY,
619 "ldif_send_entry: out of memory\n", 0, 0, 0 );
624 tl->entries = entries;
626 tl->entries[tl->ecount++] = e;
630 else if ( !get_manageDSAit( op ) && is_entry_referral( e ) ) {
631 /* Send a continuation reference.
632 * (ldif_back_referrals() handles baseobject referrals.)
633 * Don't check the filter since it's only a candidate.
635 BerVarray refs = get_entry_referrals( op, e );
636 rs->sr_ref = referral_rewrite( refs, &e->e_name, NULL, scope );
638 rc = send_search_reference( op, rs );
639 ber_bvarray_free( rs->sr_ref );
640 ber_bvarray_free( refs );
645 else if ( test_filter( op, e, op->ors_filter ) == LDAP_COMPARE_TRUE ) {
647 rs->sr_attrs = op->ors_attrs;
648 rs->sr_flags = REP_ENTRY_MODIFIABLE;
649 rc = send_search_entry( op, rs );
659 /* Read LDIF directory <path> into <listp>. Set *fname_maxlenp. */
664 const struct berval *path,
666 ber_len_t *fname_maxlenp )
668 int rc = LDAP_SUCCESS;
674 dir_of_path = opendir( path->bv_val );
675 if ( dir_of_path == NULL ) {
676 int save_errno = errno;
677 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
678 int is_rootDSE = (path->bv_len == li->li_base_path.bv_len);
680 /* Absent directory is OK (leaf entry), except the database dir */
681 if ( is_rootDSE || save_errno != ENOENT ) {
682 Debug( LDAP_DEBUG_ANY,
683 "=> ldif_search_entry: failed to opendir \"%s\": %s\n",
684 path->bv_val, STRERROR( save_errno ), 0 );
688 save_errno != ENOENT ? "internal error (bad directory)"
689 : !is_rootDSE ? "internal error (missing directory)"
690 : "internal error (database directory does not exist)";
698 while ( (dir = readdir( dir_of_path )) != NULL ) {
701 char *trunc, *idxp, *endp, *endp2;
703 fname_len = strlen( dir->d_name );
704 if ( fname_len < STRLENOF( "x=" LDIF )) /* min filename size */
706 if ( strcmp( dir->d_name + fname_len - STRLENOF(LDIF), LDIF ))
709 if ( *fname_maxlenp < fname_len )
710 *fname_maxlenp = fname_len;
712 bvl = SLAP_MALLOC( BVL_SIZE( fname_len ) );
718 strcpy( BVL_NAME( bvl ), dir->d_name );
720 /* Make it sortable by ("attr=val" or <preceding {num}, num>) */
721 trunc = BVL_NAME( bvl ) + fname_len - STRLENOF( LDIF );
722 if ( (idxp = strchr( BVL_NAME( bvl ) + 2, IX_FSL )) != NULL &&
723 (endp = strchr( ++idxp, IX_FSR )) != NULL && endp > idxp &&
724 (eq_unsafe || idxp[-2] == '=' || endp + 1 == trunc) )
726 /* attr={n}val or bconfig.c's "pseudo-indexed" attr=val{n} */
727 bvl->inum = strtol( idxp, &endp2, 10 );
728 if ( endp2 == endp ) {
736 bvl->savech = *trunc;
739 for ( prev = listp; (ptr = *prev) != NULL; prev = &ptr->next ) {
740 int cmp = strcmp( BVL_NAME( bvl ), BVL_NAME( ptr ));
741 if ( cmp < 0 || (cmp == 0 && bvl->inum < ptr->inum) )
748 if ( closedir( dir_of_path ) < 0 ) {
752 rs->sr_text = "internal error (bad directory)";
754 if ( rc != LDAP_SUCCESS ) {
755 Debug( LDAP_DEBUG_ANY, "ldif_search_entry: %s \"%s\": %s\n",
756 "error reading directory", path->bv_val,
757 STRERROR( save_errno ) );
765 * Send an entry, recursively search its children, and free or save it.
766 * Return an LDAP result code. Parameters:
767 * op, rs operation and reply. rs == NULL for slap tools.
768 * e entry to search, or NULL for rootDSE.
769 * scope scope for the part of the search from this entry.
770 * path LDIF filename -- bv_len and non-directory part are overwritten.
778 struct berval *path )
780 int rc = LDAP_SUCCESS;
781 struct berval dn = BER_BVC( "" ), ndn = BER_BVC( "" );
783 if ( scope != LDAP_SCOPE_BASE && e != NULL ) {
784 /* Copy DN/NDN since we send the entry with REP_ENTRY_MODIFIABLE,
785 * which bconfig.c seems to need. (TODO: see config_rename_one.)
787 if ( ber_dupbv( &dn, &e->e_name ) == NULL ||
788 ber_dupbv( &ndn, &e->e_nname ) == NULL )
790 Debug( LDAP_DEBUG_ANY,
791 "ldif_search_entry: out of memory\n", 0, 0, 0 );
797 /* Send the entry if appropriate, and free or save it */
799 rc = ldif_send_entry( op, rs, e, scope );
801 /* Search the children */
802 if ( scope != LDAP_SCOPE_BASE && rc == LDAP_SUCCESS ) {
804 struct berval fpath; /* becomes child pathname */
805 char *dir_end; /* will point past dirname in fpath */
807 ldif2dir_len( *path );
808 ldif2dir_name( *path );
809 rc = ldif_readdir( op, rs, path, &list, &fpath.bv_len );
811 if ( list != NULL ) {
812 const char **text = rs == NULL ? NULL : &rs->sr_text;
814 if ( scope == LDAP_SCOPE_ONELEVEL )
815 scope = LDAP_SCOPE_BASE;
816 else if ( scope == LDAP_SCOPE_SUBORDINATE )
817 scope = LDAP_SCOPE_SUBTREE;
819 /* Allocate fpath and fill in directory part */
820 dir_end = fullpath_alloc( &fpath, path, fpath.bv_len );
821 if ( dir_end == NULL )
827 if ( rc == LDAP_SUCCESS ) {
828 *ptr->trunc = ptr->savech;
829 FILL_PATH( &fpath, dir_end, BVL_NAME( ptr ));
831 rc = ldif_read_entry( op, fpath.bv_val, &dn, &ndn,
835 rc = ldif_search_entry( op, rs, e, scope, &fpath );
837 case LDAP_NO_SUCH_OBJECT:
838 /* Only the search baseDN may produce noSuchObject. */
841 rs->sr_text = "internal error "
842 "(did someone just remove an entry file?)";
843 Debug( LDAP_DEBUG_ANY, "ldif_search_entry: "
844 "file listed in parent directory does not exist: "
845 "\"%s\"\n", fpath.bv_val, 0, 0 );
852 } while ( list != NULL );
854 if ( !BER_BVISNULL( &fpath ) )
855 SLAP_FREE( fpath.bv_val );
860 if ( !BER_BVISEMPTY( &dn ) )
861 ber_memfree( dn.bv_val );
862 if ( !BER_BVISEMPTY( &ndn ) )
863 ber_memfree( ndn.bv_val );
868 search_tree( Operation *op, SlapReply *rs )
870 int rc = LDAP_SUCCESS;
873 struct berval pdn, pndn;
875 dn2path( op->o_bd, &op->o_req_ndn, &path );
876 if ( !BER_BVISEMPTY( &op->o_req_ndn ) ) {
877 /* Read baseObject */
878 dnParent( &op->o_req_dn, &pdn );
879 dnParent( &op->o_req_ndn, &pndn );
880 rc = ldif_read_entry( op, path.bv_val, &pdn, &pndn, &e,
881 rs == NULL ? NULL : &rs->sr_text );
883 if ( rc == LDAP_SUCCESS )
884 rc = ldif_search_entry( op, rs, e, op->ors_scope, &path );
886 ch_free( path.bv_val );
892 * Prepare to create or rename an entry:
893 * Check that the entry does not already exist.
894 * Check that the parent entry exists and can have subordinates,
895 * unless need_dir is NULL or adding the suffix entry.
897 * Return an LDAP result code. May set *text to a message on failure.
898 * If success, set *dnpath to LDIF entry path and *need_dir to
899 * (directory must be created ? dirname : NULL).
905 struct berval *dnpath,
909 BackendDB *be = op->o_bd;
910 struct ldif_info *li = (struct ldif_info *) be->be_private;
911 struct berval *ndn = &e->e_nname;
912 struct berval ppath = BER_BVNULL;
914 Entry *parent = NULL;
915 int rc = LDAP_SUCCESS;
918 return SLAPD_ABANDON;
920 dn2path( be, ndn, dnpath );
922 if ( stat( dnpath->bv_val, &st ) == 0 ) { /* entry .ldif file */
923 rc = LDAP_ALREADY_EXISTS;
925 } else if ( errno != ENOENT ) {
926 Debug( LDAP_DEBUG_ANY,
927 "ldif_prepare_create: cannot stat \"%s\": %s\n",
928 dnpath->bv_val, STRERROR( errno ), 0 );
930 *text = "internal error (cannot check entry file)";
932 } else if ( need_dir != NULL ) {
934 rc = get_parent_path( dnpath, &ppath );
935 /* If parent dir exists, so does parent .ldif:
936 * The directory gets created after and removed before the .ldif.
937 * Except with the database directory, which has no matching entry.
939 if ( rc == LDAP_SUCCESS && stat( ppath.bv_val, &st ) < 0 ) {
940 rc = errno == ENOENT && ppath.bv_len > li->li_base_path.bv_len
941 ? LDAP_NO_SUCH_OBJECT : LDAP_OTHER;
944 case LDAP_NO_SUCH_OBJECT:
945 /* No parent dir, check parent .ldif */
946 dir2ldif_name( ppath );
947 rc = ldif_read_entry( op, ppath.bv_val, NULL, NULL,
948 (op->o_tag != LDAP_REQ_ADD || get_manageDSAit( op )
953 /* Check that parent is not a referral, unless
954 * ldif_back_referrals() already checked.
956 if ( parent != NULL ) {
957 int is_ref = is_entry_referral( parent );
958 entry_free( parent );
960 rc = LDAP_AFFECTS_MULTIPLE_DSAS;
961 *text = op->o_tag == LDAP_REQ_MODDN
962 ? "newSuperior is a referral object"
963 : "parent is a referral object";
967 /* Must create parent directory. */
968 ldif2dir_name( ppath );
969 *need_dir = ppath.bv_val;
971 case LDAP_NO_SUCH_OBJECT:
972 *text = op->o_tag == LDAP_REQ_MODDN
973 ? "newSuperior object does not exist"
974 : "parent does not exist";
979 Debug( LDAP_DEBUG_ANY,
980 "ldif_prepare_create: cannot stat \"%s\" parent dir: %s\n",
981 ndn->bv_val, STRERROR( errno ), 0 );
982 *text = "internal error (cannot stat parent dir)";
985 if ( *need_dir == NULL && ppath.bv_val != NULL )
986 SLAP_FREE( ppath.bv_val );
989 if ( rc != LDAP_SUCCESS ) {
990 SLAP_FREE( dnpath->bv_val );
991 BER_BVZERO( dnpath );
997 apply_modify_to_entry(
999 Modifications *modlist,
1003 char textbuf[SLAP_TEXT_BUFLEN];
1004 int rc = modlist ? LDAP_UNWILLING_TO_PERFORM : LDAP_SUCCESS;
1008 if (!acl_check_modlist(op, entry, modlist)) {
1009 return LDAP_INSUFFICIENT_ACCESS;
1012 for (; modlist != NULL; modlist = modlist->sml_next) {
1013 mods = &modlist->sml_mod;
1015 if ( mods->sm_desc == slap_schema.si_ad_objectClass ) {
1018 switch (mods->sm_op) {
1020 rc = modify_add_values(entry, mods,
1021 get_permissiveModify(op),
1022 &rs->sr_text, textbuf,
1023 sizeof( textbuf ) );
1026 case LDAP_MOD_DELETE:
1027 rc = modify_delete_values(entry, mods,
1028 get_permissiveModify(op),
1029 &rs->sr_text, textbuf,
1030 sizeof( textbuf ) );
1033 case LDAP_MOD_REPLACE:
1034 rc = modify_replace_values(entry, mods,
1035 get_permissiveModify(op),
1036 &rs->sr_text, textbuf,
1037 sizeof( textbuf ) );
1040 case LDAP_MOD_INCREMENT:
1041 rc = modify_increment_values( entry,
1042 mods, get_permissiveModify(op),
1043 &rs->sr_text, textbuf,
1044 sizeof( textbuf ) );
1047 case SLAP_MOD_SOFTADD:
1048 mods->sm_op = LDAP_MOD_ADD;
1049 rc = modify_add_values(entry, mods,
1050 get_permissiveModify(op),
1051 &rs->sr_text, textbuf,
1052 sizeof( textbuf ) );
1053 mods->sm_op = SLAP_MOD_SOFTADD;
1054 if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
1059 if(rc != LDAP_SUCCESS) break;
1062 if ( rc == LDAP_SUCCESS ) {
1063 rs->sr_text = NULL; /* Needed at least with SLAP_MOD_SOFTADD */
1065 entry->e_ocflags = 0;
1067 /* check that the entry still obeys the schema */
1068 rc = entry_schema_check( op, entry, NULL, 0, 0,
1069 &rs->sr_text, textbuf, sizeof( textbuf ) );
1077 ldif_back_referrals( Operation *op, SlapReply *rs )
1079 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
1080 struct berval path, dn = op->o_req_dn, ndn = op->o_req_ndn;
1081 ber_len_t min_dnlen;
1082 Entry *entry = NULL, **entryp;
1086 min_dnlen = op->o_bd->be_nsuffix[0].bv_len;
1087 if ( min_dnlen == 0 ) {
1088 /* Catch root DSE (empty DN), it is not a referral */
1090 if ( BER_BVISEMPTY( &ndn ) )
1091 return LDAP_SUCCESS;
1094 entryp = get_manageDSAit( op ) ? NULL : &entry;
1095 dn2path( op->o_bd, &ndn, &path );
1096 ldap_pvt_thread_rdwr_rlock( &li->li_rdwr );
1099 dnParent( &dn, &dn );
1100 dnParent( &ndn, &ndn );
1101 rc = ldif_read_entry( op, path.bv_val, &dn, &ndn,
1102 entryp, &rs->sr_text );
1103 if ( rc != LDAP_NO_SUCH_OBJECT )
1107 if ( ndn.bv_len < min_dnlen )
1109 (void) get_parent_path( &path, NULL );
1110 dir2ldif_name( path );
1114 ldap_pvt_thread_rdwr_runlock( &li->li_rdwr );
1115 SLAP_FREE( path.bv_val );
1117 if ( entry != NULL ) {
1118 if ( is_entry_referral( entry ) ) {
1119 Debug( LDAP_DEBUG_TRACE,
1120 "ldif_back_referrals: tag=%lu target=\"%s\" matched=\"%s\"\n",
1121 (unsigned long) op->o_tag, op->o_req_dn.bv_val, entry->e_dn );
1123 ref = get_entry_referrals( op, entry );
1124 rs->sr_ref = referral_rewrite( ref, &entry->e_name, &op->o_req_dn,
1125 op->o_tag == LDAP_REQ_SEARCH ?
1126 op->ors_scope : LDAP_SCOPE_DEFAULT );
1127 ber_bvarray_free( ref );
1129 if ( rs->sr_ref != NULL ) {
1131 rc = rs->sr_err = LDAP_REFERRAL;
1132 rs->sr_matched = entry->e_dn;
1133 send_ldap_result( op, rs );
1134 ber_bvarray_free( rs->sr_ref );
1138 rs->sr_text = "bad referral object";
1140 rs->sr_matched = NULL;
1143 entry_free( entry );
1150 /* LDAP operations */
1153 ldif_back_bind( Operation *op, SlapReply *rs )
1155 struct ldif_info *li;
1157 AttributeDescription *password = slap_schema.si_ad_userPassword;
1159 Entry *entry = NULL;
1161 switch ( be_rootdn_bind( op, rs ) ) {
1162 case SLAP_CB_CONTINUE:
1166 /* in case of success, front end will send result;
1167 * otherwise, be_rootdn_bind() did */
1171 li = (struct ldif_info *) op->o_bd->be_private;
1172 ldap_pvt_thread_rdwr_rlock(&li->li_rdwr);
1173 return_val = get_entry(op, &entry, NULL, NULL);
1175 /* no object is found for them */
1176 if(return_val != LDAP_SUCCESS) {
1177 rs->sr_err = return_val = LDAP_INVALID_CREDENTIALS;
1181 /* they don't have userpassword */
1182 if((a = attr_find(entry->e_attrs, password)) == NULL) {
1183 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
1188 /* authentication actually failed */
1189 if(slap_passwd_check(op, entry, a, &op->oq_bind.rb_cred,
1190 &rs->sr_text) != 0) {
1191 rs->sr_err = LDAP_INVALID_CREDENTIALS;
1196 /* let the front-end send success */
1197 return_val = LDAP_SUCCESS;
1200 ldap_pvt_thread_rdwr_runlock(&li->li_rdwr);
1201 if(return_val != LDAP_SUCCESS)
1202 send_ldap_result( op, rs );
1209 ldif_back_search( Operation *op, SlapReply *rs )
1211 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
1213 ldap_pvt_thread_rdwr_rlock(&li->li_rdwr);
1214 rs->sr_err = search_tree( op, rs );
1215 ldap_pvt_thread_rdwr_runlock(&li->li_rdwr);
1216 send_ldap_result(op, rs);
1222 ldif_back_add( Operation *op, SlapReply *rs )
1224 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
1225 Entry * e = op->ora_e;
1228 char textbuf[SLAP_TEXT_BUFLEN];
1231 Debug( LDAP_DEBUG_TRACE, "ldif_back_add: \"%s\"\n", e->e_dn, 0, 0 );
1233 rc = entry_schema_check( op, e, NULL, 0, 1,
1234 &rs->sr_text, textbuf, sizeof( textbuf ) );
1235 if ( rc != LDAP_SUCCESS )
1238 rc = slap_add_opattrs( op, &rs->sr_text, textbuf, sizeof( textbuf ), 1 );
1239 if ( rc != LDAP_SUCCESS )
1242 ldap_pvt_thread_mutex_lock( &li->li_modop_mutex );
1244 rc = ldif_prepare_create( op, e, &path, &parentdir, &rs->sr_text );
1245 if ( rc == LDAP_SUCCESS ) {
1246 ldap_pvt_thread_rdwr_wlock( &li->li_rdwr );
1247 rc = ldif_write_entry( op, e, &path, parentdir, &rs->sr_text );
1248 ldap_pvt_thread_rdwr_wunlock( &li->li_rdwr );
1250 SLAP_FREE( path.bv_val );
1251 if ( parentdir != NULL )
1252 SLAP_FREE( parentdir );
1255 ldap_pvt_thread_mutex_unlock( &li->li_modop_mutex );
1259 Debug( LDAP_DEBUG_TRACE, "ldif_back_add: err: %d text: %s\n",
1260 rc, rs->sr_text ? rs->sr_text : "", 0 );
1261 send_ldap_result( op, rs );
1262 slap_graduate_commit_csn( op );
1267 ldif_back_modify( Operation *op, SlapReply *rs )
1269 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
1270 Modifications * modlst = op->orm_modlist;
1275 slap_mods_opattrs( op, &op->orm_modlist, 1 );
1277 ldap_pvt_thread_mutex_lock( &li->li_modop_mutex );
1279 rc = get_entry( op, &entry, &path, &rs->sr_text );
1280 if ( rc == LDAP_SUCCESS ) {
1281 rc = apply_modify_to_entry( entry, modlst, op, rs );
1282 if ( rc == LDAP_SUCCESS ) {
1283 ldap_pvt_thread_rdwr_wlock( &li->li_rdwr );
1284 rc = ldif_write_entry( op, entry, &path, NULL, &rs->sr_text );
1285 ldap_pvt_thread_rdwr_wunlock( &li->li_rdwr );
1288 entry_free( entry );
1289 SLAP_FREE( path.bv_val );
1292 ldap_pvt_thread_mutex_unlock( &li->li_modop_mutex );
1295 send_ldap_result( op, rs );
1296 slap_graduate_commit_csn( op );
1301 ldif_back_delete( Operation *op, SlapReply *rs )
1303 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
1305 int rc = LDAP_SUCCESS;
1307 if ( BER_BVISEMPTY( &op->o_csn )) {
1309 char csnbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
1311 csn.bv_val = csnbuf;
1312 csn.bv_len = sizeof( csnbuf );
1313 slap_get_csn( op, &csn, 1 );
1316 ldap_pvt_thread_mutex_lock( &li->li_modop_mutex );
1317 ldap_pvt_thread_rdwr_wlock( &li->li_rdwr );
1318 if ( op->o_abandon ) {
1323 dn2path( op->o_bd, &op->o_req_ndn, &path );
1324 ldif2dir_len( path );
1325 ldif2dir_name( path );
1326 if ( rmdir( path.bv_val ) < 0 ) {
1329 rc = LDAP_NOT_ALLOWED_ON_NONLEAF;
1332 /* is leaf, go on */
1336 rs->sr_text = "internal error (cannot delete subtree directory)";
1341 if ( rc == LDAP_SUCCESS ) {
1342 dir2ldif_name( path );
1343 if ( unlink( path.bv_val ) < 0 ) {
1344 rc = LDAP_NO_SUCH_OBJECT;
1345 if ( errno != ENOENT ) {
1347 rs->sr_text = "internal error (cannot delete entry file)";
1352 if ( rc == LDAP_OTHER ) {
1353 Debug( LDAP_DEBUG_ANY, "ldif_back_delete: %s \"%s\": %s\n",
1354 "cannot delete", path.bv_val, STRERROR( errno ) );
1357 SLAP_FREE( path.bv_val );
1359 ldap_pvt_thread_rdwr_wunlock( &li->li_rdwr );
1360 ldap_pvt_thread_mutex_unlock( &li->li_modop_mutex );
1362 send_ldap_result( op, rs );
1363 slap_graduate_commit_csn( op );
1373 struct berval *oldpath,
1376 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
1377 struct berval newpath;
1378 char *parentdir = NULL, *trash;
1385 rc = ldif_prepare_create( op, entry, &newpath,
1386 op->orr_newSup ? &parentdir : NULL, text );
1389 if ( rc == LDAP_SUCCESS ) {
1390 ldap_pvt_thread_rdwr_wlock( &li->li_rdwr );
1392 rc = ldif_write_entry( op, entry, &newpath, parentdir, text );
1393 if ( rc == LDAP_SUCCESS && !same_ndn ) {
1394 trash = oldpath->bv_val; /* will be .ldif file to delete */
1395 ldif2dir_len( newpath );
1396 ldif2dir_len( *oldpath );
1397 /* Move subdir before deleting old entry,
1398 * so .ldif always exists if subdir does.
1400 ldif2dir_name( newpath );
1401 ldif2dir_name( *oldpath );
1402 rename_res = move_dir( oldpath->bv_val, newpath.bv_val );
1403 if ( rename_res != 0 && errno != ENOENT ) {
1405 *text = "internal error (cannot move this subtree)";
1406 trash = newpath.bv_val;
1409 /* Delete old entry, or if error undo change */
1411 dir2ldif_name( newpath );
1412 dir2ldif_name( *oldpath );
1413 if ( unlink( trash ) == 0 )
1415 if ( rc == LDAP_SUCCESS ) {
1416 /* Prepare to undo change and return failure */
1418 *text = "internal error (cannot move this entry)";
1419 trash = newpath.bv_val;
1420 if ( rename_res != 0 )
1422 /* First move subdirectory back */
1423 ldif2dir_name( newpath );
1424 ldif2dir_name( *oldpath );
1425 if ( move_dir( newpath.bv_val, oldpath->bv_val ) == 0 )
1428 *text = "added new but couldn't delete old entry!";
1432 if ( rc != LDAP_SUCCESS ) {
1434 snprintf( s, sizeof s, "%s (%s)", *text, STRERROR( errno ));
1435 Debug( LDAP_DEBUG_ANY,
1436 "ldif_move_entry: %s: \"%s\" -> \"%s\"\n",
1437 s, op->o_req_dn.bv_val, entry->e_dn );
1441 ldap_pvt_thread_rdwr_wunlock( &li->li_rdwr );
1443 SLAP_FREE( newpath.bv_val );
1444 if ( parentdir != NULL )
1445 SLAP_FREE( parentdir );
1452 ldif_back_modrdn( Operation *op, SlapReply *rs )
1454 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
1455 struct berval new_dn = BER_BVNULL, new_ndn = BER_BVNULL;
1456 struct berval p_dn, old_path;
1460 slap_mods_opattrs( op, &op->orr_modlist, 1 );
1462 ldap_pvt_thread_mutex_lock( &li->li_modop_mutex );
1464 rc = get_entry( op, &entry, &old_path, &rs->sr_text );
1465 if ( rc == LDAP_SUCCESS ) {
1466 /* build new dn, and new ndn for the entry */
1467 if ( op->oq_modrdn.rs_newSup != NULL ) {
1468 p_dn = *op->oq_modrdn.rs_newSup;
1470 dnParent( &entry->e_name, &p_dn );
1472 build_new_dn( &new_dn, &p_dn, &op->oq_modrdn.rs_newrdn, NULL );
1473 dnNormalize( 0, NULL, NULL, &new_dn, &new_ndn, NULL );
1474 same_ndn = !ber_bvcmp( &entry->e_nname, &new_ndn );
1475 ber_memfree_x( entry->e_name.bv_val, NULL );
1476 ber_memfree_x( entry->e_nname.bv_val, NULL );
1477 entry->e_name = new_dn;
1478 entry->e_nname = new_ndn;
1480 /* perform the modifications */
1481 rc = apply_modify_to_entry( entry, op->orr_modlist, op, rs );
1482 if ( rc == LDAP_SUCCESS )
1483 rc = ldif_move_entry( op, entry, same_ndn, &old_path,
1486 entry_free( entry );
1487 SLAP_FREE( old_path.bv_val );
1490 ldap_pvt_thread_mutex_unlock( &li->li_modop_mutex );
1492 send_ldap_result( op, rs );
1493 slap_graduate_commit_csn( op );
1498 /* Return LDAP_SUCCESS IFF we retrieve the specified entry. */
1500 ldif_back_entry_get(
1504 AttributeDescription *at,
1508 struct ldif_info *li = (struct ldif_info *) op->o_bd->be_private;
1509 struct berval op_dn = op->o_req_dn, op_ndn = op->o_req_ndn;
1512 assert( ndn != NULL );
1513 assert( !BER_BVISNULL( ndn ) );
1515 ldap_pvt_thread_rdwr_rlock( &li->li_rdwr );
1516 op->o_req_dn = *ndn;
1517 op->o_req_ndn = *ndn;
1518 rc = get_entry( op, e, NULL, NULL );
1519 op->o_req_dn = op_dn;
1520 op->o_req_ndn = op_ndn;
1521 ldap_pvt_thread_rdwr_runlock( &li->li_rdwr );
1523 if ( rc == LDAP_SUCCESS && oc && !is_entry_objectclass_or_sub( *e, oc ) ) {
1524 rc = LDAP_NO_SUCH_ATTRIBUTE;
1536 ldif_tool_entry_open( BackendDB *be, int mode )
1538 struct ldif_tool *tl = &((struct ldif_info *) be->be_private)->li_tool;
1545 ldif_tool_entry_close( BackendDB *be )
1547 struct ldif_tool *tl = &((struct ldif_info *) be->be_private)->li_tool;
1548 Entry **entries = tl->entries;
1551 for ( i = tl->ecount; i--; )
1553 entry_free( entries[i] );
1554 SLAP_FREE( entries );
1556 tl->ecount = tl->elen = 0;
1561 ldif_tool_entry_next( BackendDB *be )
1563 struct ldif_tool *tl = &((struct ldif_info *) be->be_private)->li_tool;
1565 if ( tl->ecurrent >= tl->ecount )
1568 return ++tl->ecurrent;
1572 ldif_tool_entry_first( BackendDB *be )
1574 struct ldif_tool *tl = &((struct ldif_info *) be->be_private)->li_tool;
1576 if ( tl->entries == NULL ) {
1580 op.o_req_dn = *be->be_suffix;
1581 op.o_req_ndn = *be->be_nsuffix;
1582 op.ors_scope = LDAP_SCOPE_SUBTREE;
1583 if ( search_tree( &op, NULL ) != LDAP_SUCCESS ) {
1584 tl->ecurrent = tl->ecount; /* fail ldif_tool_entry_next() */
1585 return 0; /* fail ldif_tool_entry_get() */
1588 return ldif_tool_entry_next( be );
1592 ldif_tool_entry_get( BackendDB *be, ID id )
1594 struct ldif_tool *tl = &((struct ldif_info *) be->be_private)->li_tool;
1598 if ( id < tl->ecount ) {
1599 e = tl->entries[id];
1600 tl->entries[id] = NULL;
1606 ldif_tool_entry_put( BackendDB *be, Entry *e, struct berval *text )
1609 const char *errmsg = NULL;
1615 rc = ldif_prepare_create( &op, e, &path, &parentdir, &errmsg );
1616 if ( rc == LDAP_SUCCESS ) {
1617 rc = ldif_write_entry( &op, e, &path, parentdir, &errmsg );
1619 SLAP_FREE( path.bv_val );
1620 if ( parentdir != NULL )
1621 SLAP_FREE( parentdir );
1622 if ( rc == LDAP_SUCCESS )
1626 if ( errmsg == NULL && rc != LDAP_OTHER )
1627 errmsg = ldap_err2string( rc );
1628 if ( errmsg != NULL )
1629 snprintf( text->bv_val, text->bv_len, "%s", errmsg );
1637 ldif_back_db_init( BackendDB *be, ConfigReply *cr )
1639 struct ldif_info *li;
1641 li = ch_calloc( 1, sizeof(struct ldif_info) );
1642 be->be_private = li;
1643 be->be_cf_ocs = ldifocs;
1644 ldap_pvt_thread_mutex_init( &li->li_modop_mutex );
1645 ldap_pvt_thread_rdwr_init( &li->li_rdwr );
1646 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_ONE_SUFFIX;
1651 ldif_back_db_destroy( Backend *be, ConfigReply *cr )
1653 struct ldif_info *li = be->be_private;
1655 ch_free( li->li_base_path.bv_val );
1656 ldap_pvt_thread_rdwr_destroy( &li->li_rdwr );
1657 ldap_pvt_thread_mutex_destroy( &li->li_modop_mutex );
1658 free( be->be_private );
1663 ldif_back_db_open( Backend *be, ConfigReply *cr )
1665 struct ldif_info *li = (struct ldif_info *) be->be_private;
1666 if( BER_BVISEMPTY(&li->li_base_path)) {/* missing base path */
1667 Debug( LDAP_DEBUG_ANY, "missing base path for back-ldif\n", 0, 0, 0);
1674 ldif_back_initialize( BackendInfo *bi )
1676 static char *controls[] = {
1677 LDAP_CONTROL_MANAGEDSAIT,
1683 SLAP_BFLAG_INCREMENT |
1684 SLAP_BFLAG_REFERRALS;
1686 bi->bi_controls = controls;
1693 bi->bi_db_init = ldif_back_db_init;
1694 bi->bi_db_config = config_generic_wrapper;
1695 bi->bi_db_open = ldif_back_db_open;
1696 bi->bi_db_close = 0;
1697 bi->bi_db_destroy = ldif_back_db_destroy;
1699 bi->bi_op_bind = ldif_back_bind;
1700 bi->bi_op_unbind = 0;
1701 bi->bi_op_search = ldif_back_search;
1702 bi->bi_op_compare = 0;
1703 bi->bi_op_modify = ldif_back_modify;
1704 bi->bi_op_modrdn = ldif_back_modrdn;
1705 bi->bi_op_add = ldif_back_add;
1706 bi->bi_op_delete = ldif_back_delete;
1707 bi->bi_op_abandon = 0;
1709 bi->bi_extended = 0;
1711 bi->bi_chk_referrals = ldif_back_referrals;
1713 bi->bi_connection_init = 0;
1714 bi->bi_connection_destroy = 0;
1716 bi->bi_entry_get_rw = ldif_back_entry_get;
1718 #if 0 /* NOTE: uncomment to completely disable access control */
1719 bi->bi_access_allowed = slap_access_always_allowed;
1722 bi->bi_tool_entry_open = ldif_tool_entry_open;
1723 bi->bi_tool_entry_close = ldif_tool_entry_close;
1724 bi->bi_tool_entry_first = ldif_tool_entry_first;
1725 bi->bi_tool_entry_next = ldif_tool_entry_next;
1726 bi->bi_tool_entry_get = ldif_tool_entry_get;
1727 bi->bi_tool_entry_put = ldif_tool_entry_put;
1728 bi->bi_tool_entry_reindex = 0;
1729 bi->bi_tool_sync = 0;
1731 bi->bi_tool_dn2id_get = 0;
1732 bi->bi_tool_entry_modify = 0;
1734 bi->bi_cf_ocs = ldifocs;
1736 rc = config_register_schema( ldifcfg, ldifocs );
1737 if ( rc ) return rc;