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