]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldif/ldif.c
ITS#3699, #3702 fix ldif_back_search to send entries immediately instead
[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 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         int elen;
39         int 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_mutex_t  li_mutex;
47 };
48
49 #define LDIF    ".ldif"
50
51 #define IX_DNL  '{'
52 #define IX_DNR  '}'
53 #ifndef IX_FSL
54 #define IX_FSL  IX_DNL
55 #define IX_FSR  IX_DNR
56 #endif
57
58 #define ENTRY_BUFF_INCREMENT 500
59
60 static ObjectClass *ldif_oc;
61
62 static ConfigDriver ldif_cf;
63
64 static ConfigTable ldifcfg[] = {
65         { "", "", 0, 0, 0, ARG_MAGIC,
66                 ldif_cf, NULL, NULL, NULL },
67         { "directory", "dir", 2, 2, 0, ARG_BERVAL|ARG_OFFSET,
68                 (void *)offsetof(struct ldif_info, li_base_path),
69                 "( OLcfgDbAt:0.1 NAME 'olcDbDirectory' "
70                         "DESC 'Directory for database content' "
71                         "EQUALITY caseIgnoreMatch "
72                         "SYNTAX OMsDirectoryString )", NULL, NULL },
73         { NULL, NULL, 0, 0, 0, ARG_IGNORED,
74                 NULL, NULL, NULL, NULL }
75 };
76
77 static ConfigOCs ldifocs[] = {
78         { "( OLcfgDbOc:2.1 "
79                 "NAME 'olcLdifConfig' "
80                 "DESC 'LDIF backend configuration' "
81                 "SUP olcDatabaseConfig "
82                 "MUST ( olcDbDirectory ) )", Cft_Database,
83                 &ldif_oc },
84         { NULL, 0, NULL }
85 };
86
87 static int
88 ldif_cf( ConfigArgs *c )
89 {
90         if ( c->op == SLAP_CONFIG_EMIT ) {
91                 value_add_one( &c->rvalue_vals, &ldif_oc->soc_cname );
92                 return 0;
93         }
94         return 1;
95 }
96
97 static void
98 dn2path(struct berval * dn, struct berval * rootdn, struct berval * base_path,
99         struct berval *res)
100 {
101         char *ptr, *sep, *end;
102
103         res->bv_len = dn->bv_len + base_path->bv_len + 1 + STRLENOF( LDIF );
104         res->bv_val = ch_malloc( res->bv_len + 1 );
105         ptr = lutil_strcopy( res->bv_val, base_path->bv_val );
106         *ptr++ = LDAP_DIRSEP[0];
107         ptr = lutil_strcopy( ptr, rootdn->bv_val );
108         end = dn->bv_val + dn->bv_len - rootdn->bv_len - 1;
109         while ( end > dn->bv_val ) {
110                 for (sep = end-1; sep >=dn->bv_val && !DN_SEPARATOR( *sep ); sep--);
111                 *ptr++ = LDAP_DIRSEP[0];
112                 ptr = lutil_strncopy( ptr, sep+1, end-sep-1 );
113                 end = sep;
114         }
115         strcpy(ptr, LDIF);
116 #if IX_FSL != IX_DNL
117         ptr = res->bv_val;
118         while( ptr=strchr(ptr, IX_DNL) ) {
119                 *ptr++ = IX_FSL;
120                 ptr = strchr(ptr, IX_DNR);
121                 if ( ptr )
122                         *ptr++ = IX_FSR;
123                 else
124                         break;
125         }
126 #endif
127 }
128
129 static char * slurp_file(int fd) {
130         int read_chars_total = 0;
131         int read_chars = 0;
132         int entry_size;
133         char * entry;
134         char * entry_pos;
135         struct stat st;
136
137         fstat(fd, &st);
138         entry_size = st.st_size;
139         entry = ch_malloc( entry_size+1 );
140         entry_pos = entry;
141         
142         while(1) {
143                 read_chars = read(fd, (void *) entry_pos, entry_size - read_chars_total);
144                 if(read_chars == -1) {
145                         SLAP_FREE(entry);
146                         return NULL;
147                 }
148                 if(read_chars == 0) {
149                         entry[read_chars_total] = '\0';
150                         break;
151                 }
152                 else {
153                         read_chars_total += read_chars;
154                         entry_pos += read_chars;
155                 }
156         }
157         return entry;
158 }
159
160 static int spew_file(int fd, char * spew) {
161         int written = 0;
162         int writeres = 0;
163         int len = strlen(spew);
164         char * spewptr = spew;
165         
166         while(written < len) {
167                 writeres = write(fd, spewptr, len - written);
168                 if(writeres == -1) {
169                         perror("could not spew write");
170                         return -1;
171                 }
172                 else {
173                         spewptr += writeres;
174                         written += writeres;
175                 }
176         }
177         return writeres;
178 }
179
180 static int spew_entry(Entry * e, struct berval * path) {
181         int rs;
182         int openres;
183         int spew_res;
184         int entry_length;
185         char * entry_as_string;
186
187         openres = open(path->bv_val, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
188         if(openres == -1) {
189                 if(errno == ENOENT)
190                         rs = LDAP_NO_SUCH_OBJECT;
191                 else
192                         rs = LDAP_UNWILLING_TO_PERFORM;
193         }
194         else {
195                 struct berval rdn;
196                 int tmp;
197
198                 /* Only save the RDN onto disk */
199                 dnRdn( &e->e_name, &rdn );
200                 if ( rdn.bv_len != e->e_name.bv_len ) {
201                         e->e_name.bv_val[rdn.bv_len] = '\0';
202                         tmp = e->e_name.bv_len;
203                         e->e_name.bv_len = rdn.bv_len;
204                         rdn.bv_len = tmp;
205                 }
206
207                 entry_as_string = entry2str(e, &entry_length);
208
209                 /* Restore full DN */
210                 if ( rdn.bv_len != e->e_name.bv_len ) {
211                         e->e_name.bv_val[e->e_name.bv_len] = ',';
212                         e->e_name.bv_len = rdn.bv_len;
213                 }
214
215                 if(entry_as_string == NULL) {
216                         rs = LDAP_UNWILLING_TO_PERFORM;
217                         close(openres);
218                 }
219                 else {
220                         spew_res = spew_file(openres, entry_as_string);
221                         close(openres);
222                         if(spew_res == -1)
223                                 rs = LDAP_UNWILLING_TO_PERFORM;
224                         else
225                                 rs = LDAP_SUCCESS;
226                 }
227         }
228         return rs;
229 }
230
231 static Entry * get_entry_for_fd(int fd,
232         struct berval *pdn,
233         struct berval *pndn)
234 {
235         char * entry = (char *) slurp_file(fd);
236         Entry * ldentry = NULL;
237         
238         /* error reading file */
239         if(entry == NULL) {
240                 goto return_value;
241         }
242
243         ldentry = str2entry(entry);
244         if ( ldentry ) {
245                 struct berval rdn;
246                 rdn = ldentry->e_name;
247                 build_new_dn( &ldentry->e_name, pdn, &rdn, NULL );
248                 ch_free( rdn.bv_val );
249                 rdn = ldentry->e_nname;
250                 build_new_dn( &ldentry->e_nname, pndn, &rdn, NULL );
251                 ch_free( rdn.bv_val );
252         }
253
254  return_value:
255         if(fd != -1) {
256                 if(close(fd) != 0) {
257                         /* log error */
258                 }
259         }
260         if(entry != NULL)
261                 SLAP_FREE(entry);
262         return ldentry;
263 }
264
265 static Entry * get_entry(Operation *op, struct berval *base_path) {
266         struct berval path, pdn, pndn;
267         int fd;
268
269         dnParent(&op->o_req_dn, &pdn);
270         dnParent(&op->o_req_ndn, &pndn);
271         dn2path(&op->o_req_ndn, op->o_bd->be_nsuffix, base_path, &path);
272         fd = open(path.bv_val, O_RDONLY);
273         /* error opening file (mebbe should log error) */
274         if(fd == -1) {
275                 perror("failed to open file");
276         }
277
278         if(path.bv_val != NULL)
279                 SLAP_FREE(path.bv_val);
280
281         if ( fd != -1 ) {
282                 return get_entry_for_fd(fd, &pdn, &pndn);
283         }
284
285         return NULL;
286 }
287
288 static void fullpath(struct berval *base, struct berval *name, struct berval *res) {
289         char *ptr;
290         res->bv_len = name->bv_len + base->bv_len + 1;
291         res->bv_val = ch_malloc( res->bv_len + 1 );
292         strcpy(res->bv_val, base->bv_val);
293         ptr = res->bv_val + base->bv_len;
294         *ptr++ = LDAP_DIRSEP[0];
295         strcpy(ptr, name->bv_val);
296 }
297
298 typedef struct bvlist {
299         struct bvlist *next;
300         struct berval bv;
301         struct berval num;
302         unsigned int inum;
303         int off;
304 } bvlist;
305
306
307 static int r_enum_tree(enumCookie *ck, struct berval *path,
308         struct berval *pdn, struct berval *pndn)
309 {
310         Entry *e;
311         int fd, rc = LDAP_SUCCESS;
312
313         fd = open( path->bv_val, O_RDONLY );
314         if ( fd < 0 ) {
315                 Debug( LDAP_DEBUG_TRACE,
316                         "=> ldif_enum_tree: failed to open %s\n",
317                         path->bv_val, 0, 0 );
318                 return LDAP_NO_SUCH_OBJECT;
319         }
320
321         e = get_entry_for_fd(fd, pdn, pndn);
322         if ( !e ) {
323                 Debug( LDAP_DEBUG_ANY,
324                         "=> ldif_enum_tree: failed to read entry for %s\n",
325                         path->bv_val, 0, 0 );
326                 return LDAP_BUSY;
327         }
328
329         if ( ck->op->ors_scope == LDAP_SCOPE_BASE ||
330                 ck->op->ors_scope == LDAP_SCOPE_SUBTREE ) {
331                 /* Send right away? */
332                 if ( ck->rs ) {
333                         /*
334                          * if it's a referral, add it to the list of referrals. only do
335                          * this for non-base searches, and don't check the filter
336                          * explicitly here since it's only a candidate anyway.
337                          */
338                         if ( !get_manageDSAit( ck->op )
339                                         && ck->op->ors_scope != LDAP_SCOPE_BASE
340                                         && is_entry_referral( e ) )
341                         {
342                                 BerVarray erefs = get_entry_referrals( ck->op, e );
343                                 ck->rs->sr_ref = referral_rewrite( erefs,
344                                                 &e->e_name, NULL,
345                                                 ck->op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
346                                                         ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
347
348                                 send_search_reference( ck->op, ck->rs );
349
350                                 ber_bvarray_free( ck->rs->sr_ref );
351                                 ber_bvarray_free( erefs );
352                                 ck->rs->sr_ref = NULL;
353
354                         } else if ( test_filter( ck->op, e, ck->op->ors_filter ) == LDAP_COMPARE_TRUE )
355                         {
356                                 ck->rs->sr_entry = e;
357                                 ck->rs->sr_attrs = ck->op->ors_attrs;
358                                 ck->rs->sr_flags = REP_ENTRY_MODIFIABLE;
359                                 send_search_entry(ck->op, ck->rs);
360                         }
361                         fd = 1;
362                 } else {
363                 /* Queueing up for tool mode */
364                         if(ck->entries == NULL) {
365                                 ck->entries = (Entry **) SLAP_MALLOC(sizeof(Entry *) * ENTRY_BUFF_INCREMENT);
366                                 ck->elen = ENTRY_BUFF_INCREMENT;
367                         }
368                         if(ck->eind >= ck->elen) { /* grow entries if necessary */      
369                                 ck->entries = (Entry **) SLAP_REALLOC(ck->entries, sizeof(Entry *) * (ck->elen) * 2);
370                                 ck->elen *= 2;
371                         }
372
373                         ck->entries[ck->eind++] = e;
374                         fd = 0;
375                 }
376         } else {
377                 fd = 1;
378         }
379
380         if ( ck->op->ors_scope != LDAP_SCOPE_BASE ) {
381                 DIR * dir_of_path;
382                 bvlist *list = NULL, *ptr;
383
384                 path->bv_len -= STRLENOF( LDIF );
385                 path->bv_val[path->bv_len] = '\0';
386
387                 dir_of_path = opendir(path->bv_val);
388                 if(dir_of_path == NULL) { /* can't open directory */
389                         if ( errno != ENOENT ) {
390                                 /* it shouldn't be treated as an error
391                                  * only if the directory doesn't exist */
392                                 rc = LDAP_BUSY;
393                                 Debug( LDAP_DEBUG_TRACE,
394                                         "=> ldif_enum_tree: failed to opendir %s (%d)\n",
395                                         path->bv_val, errno, 0 );
396                         }
397                         goto leave;
398                 }
399         
400                 while(1) {
401                         struct berval fname, itmp;
402                         struct dirent * dir;
403                         bvlist *bvl, *prev;
404
405                         dir = readdir(dir_of_path);
406                         if(dir == NULL) break; /* end of the directory */
407                         fname.bv_len = strlen( dir->d_name );
408                         if ( fname.bv_len <= STRLENOF( LDIF ))
409                                 continue;
410                         if ( strcmp( dir->d_name + (fname.bv_len - STRLENOF(LDIF)), LDIF))
411                                 continue;
412                         fname.bv_val = dir->d_name;
413
414                         bvl = ch_malloc( sizeof(bvlist) );
415                         ber_dupbv( &bvl->bv, &fname );
416                         BER_BVZERO( &bvl->num );
417                         itmp.bv_val = strchr( bvl->bv.bv_val, IX_FSL );
418                         if ( itmp.bv_val ) {
419                                 char *ptr;
420                                 itmp.bv_val++;
421                                 ptr = strchr( itmp.bv_val, IX_FSR );
422                                 if ( ptr ) {
423                                         itmp.bv_len = ptr - itmp.bv_val;
424                                         ber_dupbv( &bvl->num, &itmp );
425                                         bvl->inum = strtoul( itmp.bv_val, NULL, 0 );
426                                         itmp.bv_val[0] = '\0';
427                                         bvl->off = itmp.bv_val - bvl->bv.bv_val;
428                                 }
429                         }
430
431                         for (ptr = list, prev = (bvlist *)&list; ptr;
432                                 prev = ptr, ptr = ptr->next) {
433                                 int cmp = strcmp( bvl->bv.bv_val, ptr->bv.bv_val );
434                                 if ( !cmp && bvl->num.bv_val )
435                                         cmp = bvl->inum - ptr->inum;
436                                 if ( cmp < 0 )
437                                         break;
438                         }
439                         prev->next = bvl;
440                         bvl->next = ptr;
441                                 
442                 }
443                 closedir(dir_of_path);
444
445                 if (ck->op->ors_scope == LDAP_SCOPE_ONELEVEL)
446                         ck->op->ors_scope = LDAP_SCOPE_BASE;
447                 else if ( ck->op->ors_scope == LDAP_SCOPE_SUBORDINATE)
448                         ck->op->ors_scope = LDAP_SCOPE_SUBTREE;
449
450                 while ( ( ptr = list ) ) {
451                         struct berval fpath;
452
453                         list = ptr->next;
454
455                         if ( ptr->num.bv_val )
456                                 AC_MEMCPY( ptr->bv.bv_val + ptr->off, ptr->num.bv_val,
457                                         ptr->num.bv_len );
458                         fullpath( path, &ptr->bv, &fpath );
459                         r_enum_tree(ck, &fpath, &e->e_name, &e->e_nname );
460                         free(fpath.bv_val);
461                         if ( ptr->num.bv_val )
462                                 free( ptr->num.bv_val );
463                         free(ptr->bv.bv_val);
464                         free(ptr);
465                 }
466         }
467 leave:
468         if ( fd ) entry_free( e );
469         return rc;
470 }
471
472 static int
473 enum_tree(
474         enumCookie *ck
475 )
476 {
477         struct ldif_info *ni = (struct ldif_info *) ck->op->o_bd->be_private;
478         struct berval path;
479         struct berval pdn, pndn;
480
481         dnParent( &ck->op->o_req_dn, &pdn );
482         dnParent( &ck->op->o_req_ndn, &pndn );
483         dn2path( &ck->op->o_req_ndn, &ck->op->o_bd->be_nsuffix[0], &ni->li_base_path, &path);
484         return r_enum_tree(ck, &path, &pdn, &pndn);
485 }
486
487 /* Get the parent path plus the LDIF suffix */
488 static void get_parent_path(struct berval * dnpath, struct berval *res) {
489         int dnpathlen = dnpath->bv_len;
490         int i;
491         
492         for(i = dnpathlen;i>0;i--) /* find the first path seperator */
493                 if(dnpath->bv_val[i] == LDAP_DIRSEP[0])
494                         break;
495         res->bv_len = i;
496         res->bv_val = ch_malloc( res->bv_len + 1 + STRLENOF(LDIF) );
497         strncpy(res->bv_val, dnpath->bv_val, i);
498         strcpy(res->bv_val+i, LDIF);
499         res->bv_val[i] = '\0';
500 }
501
502 static int apply_modify_to_entry(Entry * entry,
503                                 Modifications * modlist,
504                                 Operation * op,
505                                 SlapReply * rs)
506 {
507         char textbuf[SLAP_TEXT_BUFLEN];
508         int rc = LDAP_UNWILLING_TO_PERFORM;
509         Modification *mods = NULL;
510
511         if (!acl_check_modlist(op, entry, modlist)) {
512                 return LDAP_INSUFFICIENT_ACCESS;
513         }
514
515         for (; modlist != NULL; modlist = modlist->sml_next) {
516                 mods = &modlist->sml_mod;
517
518                 switch (mods->sm_op) {
519                 case LDAP_MOD_ADD:
520                         rc = modify_add_values(entry, mods,
521                                    get_permissiveModify(op),
522                                    &rs->sr_text, textbuf,
523                                    sizeof( textbuf ) );
524                         break;
525                                 
526                 case LDAP_MOD_DELETE:
527                         rc = modify_delete_values(entry, mods,
528                                 get_permissiveModify(op),
529                                 &rs->sr_text, textbuf,
530                                 sizeof( textbuf ) );
531
532                         break;
533                                 
534                 case LDAP_MOD_REPLACE:
535                         rc = modify_replace_values(entry, mods,
536                                  get_permissiveModify(op),
537                                  &rs->sr_text, textbuf,
538                                  sizeof( textbuf ) );
539
540                         break;
541                 case LDAP_MOD_INCREMENT:
542                         break;
543                 case SLAP_MOD_SOFTADD:
544                         mods->sm_op = LDAP_MOD_ADD;
545                         rc = modify_add_values(entry, mods,
546                                    get_permissiveModify(op),
547                                    &rs->sr_text, textbuf,
548                                    sizeof( textbuf ) );
549                         mods->sm_op = SLAP_MOD_SOFTADD;
550                         if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
551                                 rc = LDAP_SUCCESS;
552                         }
553                         break;
554                 default:
555                         break;
556                 }
557                 if(rc != LDAP_SUCCESS) break;
558         }
559         
560         if(rc == LDAP_SUCCESS) {
561                 if ( mods->sm_desc == slap_schema.si_ad_objectClass ) {
562                         entry->e_ocflags = 0;
563                 }
564                 /* check that the entry still obeys the schema */
565                 rc = entry_schema_check(op->o_bd, entry, NULL,
566                                   &rs->sr_text, textbuf, sizeof( textbuf ) );
567         }
568         return rc;
569 }
570
571 int
572 ldif_back_referrals( Operation *op, SlapReply *rs )
573 {
574         struct ldif_info        *ni = NULL;
575         Entry                   *entry;
576         int                     rc = LDAP_SUCCESS;
577
578 #if 0
579         if ( op->o_tag == LDAP_REQ_SEARCH ) {
580                 /* let search take care of itself */
581                 return rc;
582         }
583 #endif
584
585         if ( get_manageDSAit( op ) ) {
586                 /* let op take care of DSA management */
587                 return rc;
588         }
589
590         ni = (struct ldif_info *)op->o_bd->be_private;
591         ldap_pvt_thread_mutex_lock( &ni->li_mutex );
592         entry = (Entry *)get_entry( op, &ni->li_base_path );
593
594         /* no object is found for them */
595         if ( entry == NULL ) {
596                 struct berval   odn = op->o_req_dn;
597                 struct berval   ondn = op->o_req_ndn;
598
599                 struct berval   pndn = op->o_req_ndn;
600
601                 for ( ; entry == NULL; ) {
602                         dnParent( &pndn, &pndn );
603                         
604                         if ( !dnIsSuffix( &pndn, &op->o_bd->be_nsuffix[0] ) ) {
605                                 break;
606                         }
607
608                         op->o_req_dn = pndn;
609                         op->o_req_ndn = pndn;
610
611                         entry = (Entry *)get_entry( op, &ni->li_base_path );
612                 }
613
614                 ldap_pvt_thread_mutex_unlock( &ni->li_mutex );
615
616                 op->o_req_dn = odn;
617                 op->o_req_ndn = ondn;
618
619                 rc = LDAP_SUCCESS;
620                 rs->sr_matched = NULL;
621                 if ( entry != NULL ) {
622                         Debug( LDAP_DEBUG_TRACE,
623                                 "ldif_back_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
624                                 (long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
625
626                         if ( is_entry_referral( entry ) ) {
627                                 rc = LDAP_OTHER;
628                                 rs->sr_ref = get_entry_referrals( op, entry );
629                                 if ( rs->sr_ref ) {
630                                         rs->sr_matched = ber_strdup_x(
631                                         entry->e_name.bv_val, op->o_tmpmemctx );
632                                 }
633                         }
634
635                         entry_free(entry);
636
637                 } else if ( default_referral != NULL ) {
638                         rc = LDAP_OTHER;
639                         rs->sr_ref = referral_rewrite( default_referral,
640                                 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
641                 }
642
643                 if ( rs->sr_ref != NULL ) {
644                         /* send referrals */
645                         rc = rs->sr_err = LDAP_REFERRAL;
646                         send_ldap_result( op, rs );
647                         ber_bvarray_free( rs->sr_ref );
648                         rs->sr_ref = NULL;
649
650                 } else if ( rc != LDAP_SUCCESS ) {
651                         rs->sr_err = rc;
652                         rs->sr_text = rs->sr_matched ? "bad referral object" : NULL;
653                         send_ldap_result( op, rs );
654                 }
655
656                 if ( rs->sr_matched ) {
657                         op->o_tmpfree( (char *)rs->sr_matched, op->o_tmpmemctx );
658                         rs->sr_matched = NULL;
659                 }
660
661                 return rc;
662         }
663
664         ldap_pvt_thread_mutex_unlock( &ni->li_mutex );
665
666         if ( is_entry_referral( entry ) ) {
667                 /* entry is a referral */
668                 BerVarray refs = get_entry_referrals( op, entry );
669                 rs->sr_ref = referral_rewrite(
670                         refs, &entry->e_name, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
671
672                 Debug( LDAP_DEBUG_TRACE,
673                         "ldif_back_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
674                         (long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
675
676                 rs->sr_matched = entry->e_name.bv_val;
677                 if ( rs->sr_ref != NULL ) {
678                         rc = rs->sr_err = LDAP_REFERRAL;
679                         send_ldap_result( op, rs );
680                         ber_bvarray_free( rs->sr_ref );
681                         rs->sr_ref = NULL;
682
683                 } else {
684                         send_ldap_error( op, rs, LDAP_OTHER, "bad referral object" );
685                         rc = rs->sr_err;
686                 }
687
688                 rs->sr_matched = NULL;
689                 ber_bvarray_free( refs );
690
691                 entry_free( entry );
692         }
693
694         return rc;
695 }
696
697 static int
698 ldif_back_bind( Operation *op, SlapReply *rs )
699 {
700         struct ldif_info *ni = NULL;
701         Attribute * a = NULL;
702         AttributeDescription *password = slap_schema.si_ad_userPassword;
703         int return_val = 0;
704         Entry * entry = NULL;
705
706         ni = (struct ldif_info *) op->o_bd->be_private;
707         ldap_pvt_thread_mutex_lock(&ni->li_mutex);
708         entry = (Entry *) get_entry(op, &ni->li_base_path);
709
710         /* no object is found for them */
711         if(entry == NULL) {
712                 if(be_isroot_pw(op)) {
713                         rs->sr_err = return_val = LDAP_SUCCESS;
714                 } else {
715                         rs->sr_err = return_val = LDAP_INVALID_CREDENTIALS;
716                 }
717                 goto return_result;
718         }
719
720         /* they don't have userpassword */
721         if((a = attr_find(entry->e_attrs, password)) == NULL) {
722                 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
723                 return_val = 1;
724                 goto return_result;
725         }
726
727         /* authentication actually failed */
728         if(slap_passwd_check(op, entry, a, &op->oq_bind.rb_cred,
729                              &rs->sr_text) != 0) {
730                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
731                 return_val = 1;
732                 goto return_result;
733         }
734
735         /* let the front-end send success */
736         return_val = 0;
737         goto return_result;
738
739  return_result:
740         ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
741         if(return_val != 0)
742                 send_ldap_result( op, rs );
743         if(entry != NULL)
744                 entry_free(entry);
745         return return_val;
746 }
747
748 static int ldif_back_search(Operation *op, SlapReply *rs)
749 {
750         struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
751         enumCookie ck = {0};
752
753         ck.op = op;
754         ck.rs = rs;
755         ldap_pvt_thread_mutex_lock(&ni->li_mutex);
756         rs->sr_err = enum_tree( &ck );
757         ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
758         send_ldap_result(op, rs);
759
760         return rs->sr_err;
761 }
762
763 static int ldif_back_add(Operation *op, SlapReply *rs) {
764         struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
765         Entry * e = op->ora_e;
766         struct berval dn = e->e_nname;
767         struct berval leaf_path = BER_BVNULL;
768         struct stat stats;
769         int statres;
770         char textbuf[SLAP_TEXT_BUFLEN];
771
772         rs->sr_err = entry_schema_check(op->o_bd, e,
773                                   NULL, &rs->sr_text, textbuf, sizeof( textbuf ) );
774         if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
775                                 
776         ldap_pvt_thread_mutex_lock(&ni->li_mutex);
777
778         dn2path(&dn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &leaf_path);
779
780         if(leaf_path.bv_val != NULL) {
781                 struct berval base = BER_BVNULL;
782                 /* build path to container and ldif of container */
783                 get_parent_path(&leaf_path, &base);
784
785                 statres = stat(base.bv_val, &stats); /* check if container exists */
786                 if(statres == -1 && errno == ENOENT) { /* container missing */
787                         base.bv_val[base.bv_len] = '.';
788                         statres = stat(base.bv_val, &stats); /* check for leaf node */
789                         base.bv_val[base.bv_len] = '\0';
790                         if(statres == -1 && errno == ENOENT) {
791                                 rs->sr_err = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
792                         }
793                         else if(statres != -1) { /* create parent */
794                                 int mkdirres = mkdir(base.bv_val, 0750);
795                                 if(mkdirres == -1) {
796                                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
797                                 }
798                         }
799                         else
800                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
801                 }/* container was possibly created, move on to add the entry */
802                 if(rs->sr_err == LDAP_SUCCESS) {
803                         statres = stat(leaf_path.bv_val, &stats);
804                         if(statres == -1 && errno == ENOENT) {
805                                 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
806                                 rs->sr_err = (int) spew_entry(e, &leaf_path);
807                                 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
808                         }
809                         else /* it already exists */
810                                 rs->sr_err = LDAP_ALREADY_EXISTS;
811                 }
812                 SLAP_FREE(base.bv_val);
813                 SLAP_FREE(leaf_path.bv_val);
814         }
815
816         ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
817
818 send_res:
819         send_ldap_result(op, rs);
820         return 0;
821 }
822
823 static int ldif_back_modify(Operation *op, SlapReply *rs) {
824         struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
825         Modifications * modlst = op->orm_modlist;
826         struct berval path = BER_BVNULL;
827         Entry * entry = NULL;
828         int spew_res;
829
830         ldap_pvt_thread_mutex_lock(&ni->li_mutex);
831         dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path,
832                 &path);
833         entry = (Entry *) get_entry(op, &ni->li_base_path);
834
835         if(entry != NULL) {
836                 rs->sr_err = apply_modify_to_entry(entry, modlst, op, rs);
837                 if(rs->sr_err == LDAP_SUCCESS) {
838                         ldap_pvt_thread_mutex_lock(&entry2str_mutex);
839                         spew_res = spew_entry(entry, &path);
840                         ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
841                         if(spew_res == -1) {
842                                 perror("could not output entry");
843                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
844                         }
845                 }
846         }
847         else {
848                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
849         }
850         
851         if(entry != NULL)
852                 entry_free(entry);
853         if(path.bv_val != NULL)
854                 SLAP_FREE(path.bv_val);
855         rs->sr_text = NULL;
856         ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
857         send_ldap_result(op, rs);
858         return 0;
859 }
860
861 static int ldif_back_delete(Operation *op, SlapReply *rs) {
862         struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
863         struct berval path = BER_BVNULL;
864         int res = 0;
865
866         ldap_pvt_thread_mutex_lock(&ni->li_mutex);
867         dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &path);
868
869         path.bv_val[path.bv_len - STRLENOF(LDIF)] = '\0';
870         res = rmdir(path.bv_val);
871         path.bv_val[path.bv_len - STRLENOF(LDIF)] = '.';
872         if ( res && errno != ENOENT ) {
873                 rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
874         } else {
875                 res = unlink(path.bv_val);
876         }
877
878         if(res == -1) {
879                 if(errno == ENOENT)
880                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
881                 else
882                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
883         }
884         else
885                 rs->sr_err = LDAP_SUCCESS;
886
887         SLAP_FREE(path.bv_val);
888         ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
889         send_ldap_result(op, rs);
890         return 0;
891 }
892
893
894 static int move_entry(Entry * entry, struct berval * ndn,
895                            struct berval * newndn, struct berval * rootdn,
896                            struct berval * base_path) {
897         int res;
898         int exists_res;
899         struct berval path;
900         struct berval newpath;
901
902         dn2path(ndn, rootdn, base_path, &path);
903         dn2path(newndn, rootdn, base_path, &newpath);
904
905         if((entry == NULL || path.bv_val == NULL) || newpath.bv_val == NULL) {
906                 /* some object doesn't exist */
907                 res = LDAP_NO_SUCH_OBJECT;
908         }
909         else { /* do the modrdn */
910                 exists_res = open(newpath.bv_val, O_RDONLY);
911                 if(exists_res == -1 && errno == ENOENT) {
912                         res = spew_entry(entry, &newpath);
913                         if(res != -1) {
914                                 /* if this fails we should log something bad */
915                                 res = unlink(path.bv_val);
916                                 res = LDAP_SUCCESS;
917                         }
918                         else {
919                                 if(errno == ENOENT)
920                                         res = LDAP_NO_SUCH_OBJECT;
921                                 else
922                                         res = LDAP_UNWILLING_TO_PERFORM;
923                                 unlink(newpath.bv_val); /* in case file was created */
924                         }
925                 }
926                 else if(exists_res) {
927                         int close_res = close(exists_res);
928                         res = LDAP_ALREADY_EXISTS;
929                         if(close_res == -1) {
930                         /* log heinous error */
931                         }
932                 }
933                 else {
934                         res = LDAP_UNWILLING_TO_PERFORM;
935                 }
936         }
937
938         if(newpath.bv_val != NULL)
939                 SLAP_FREE(newpath.bv_val);
940         if(path.bv_val != NULL)
941                 SLAP_FREE(path.bv_val);
942         return res;
943 }
944
945 static int ldif_back_modrdn(Operation *op, SlapReply *rs) {
946         struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
947         struct berval new_dn = BER_BVNULL, new_ndn = BER_BVNULL;
948         struct berval p_dn, bv = BER_BVNULL;
949         Entry * entry = NULL;
950         LDAPRDN new_rdn = NULL;
951         LDAPRDN old_rdn = NULL;
952         Modifications * mods = NULL;
953         int res;
954
955         ldap_pvt_thread_mutex_lock(&ni->li_mutex);
956         ldap_pvt_thread_mutex_lock(&entry2str_mutex);
957         entry = (Entry *) get_entry(op, &ni->li_base_path);
958
959         /* build the mods to the entry */
960         if(entry != NULL) {
961                 if(ldap_bv2rdn(&op->oq_modrdn.rs_newrdn, &new_rdn,
962                         (char **)&rs->sr_text, LDAP_DN_FORMAT_LDAP)) {
963                         rs->sr_err = LDAP_INVALID_DN_SYNTAX;
964                 }
965                 else if(op->oq_modrdn.rs_deleteoldrdn &&
966                         ldap_bv2rdn(&op->o_req_dn, &old_rdn, (char **)&rs->sr_text,
967                         LDAP_DN_FORMAT_LDAP)) {
968                         rs->sr_err = LDAP_OTHER;
969                 }
970                 else { /* got both rdns successfully, ready to build mods */
971                         if(slap_modrdn2mods(op, rs, entry, old_rdn, new_rdn, &mods)
972                                 != LDAP_SUCCESS) {
973                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
974                         }
975                         else { /* built mods successfully */
976
977                                 /* build new dn, and new ndn for the entry */
978                                 if(op->oq_modrdn.rs_newSup != NULL) /* new superior */
979                                         p_dn = *op->oq_modrdn.rs_newSup;
980                                 else
981                                         p_dn = slap_empty_bv;
982                                 dnParent(&entry->e_name, &p_dn);
983                                 build_new_dn(&new_dn, &p_dn, &op->oq_modrdn.rs_newrdn, NULL); 
984                                 dnNormalize( 0, NULL, NULL, &new_dn, &bv, op->o_tmpmemctx );
985                                 ber_dupbv( &new_ndn, &bv );
986                                 entry->e_name = new_dn;
987                                 entry->e_nname = new_ndn;
988
989                                 /* perform the modifications */
990                                 res = apply_modify_to_entry(entry, mods, op, rs);
991                                 if(res == LDAP_SUCCESS) {
992                                         rs->sr_err = move_entry(entry, &op->o_req_ndn,
993                                                                 &new_ndn,
994                                                                 &op->o_bd->be_nsuffix[0],
995                                                                 &ni->li_base_path);
996                                 }
997                                 else
998                                         rs->sr_err = res;
999                         }
1000                 }
1001         }
1002         else /* entry was null */
1003                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1004
1005         if(entry != NULL)
1006                 entry_free(entry);
1007         rs->sr_text = "";
1008         ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
1009         ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
1010         send_ldap_result(op, rs);
1011         return 0;
1012 }
1013
1014 static int ldif_back_compare(Operation *op, SlapReply *rs) {
1015         struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
1016         Entry * e = NULL;
1017         Attribute       *a;
1018
1019         ldap_pvt_thread_mutex_lock(&ni->li_mutex);
1020
1021         e = (Entry *) get_entry(op, &ni->li_base_path);
1022         if(e != NULL) {
1023                 for(a = attrs_find( e->e_attrs, op->oq_compare.rs_ava->aa_desc );
1024                         a != NULL;
1025                         a = attrs_find( a->a_next, op->oq_compare.rs_ava->aa_desc )) {
1026                         rs->sr_err = LDAP_COMPARE_FALSE;
1027                 
1028                         if (value_find_ex(op->oq_compare.rs_ava->aa_desc,
1029                                                 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1030                                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1031                                                 a->a_nvals, &op->oq_compare.rs_ava->aa_value,
1032                                                 op->o_tmpmemctx ) == 0) {
1033                                 rs->sr_err = LDAP_COMPARE_TRUE;
1034                                 break;
1035                         }
1036                 }
1037         }
1038         else {
1039                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1040         }
1041
1042         if(e != NULL)
1043                 entry_free(e);
1044         ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
1045         send_ldap_result(op, rs);
1046         return 0;
1047 }
1048
1049 static int ldif_tool_entry_open(BackendDB * be, int mode) {
1050         struct ldif_info *ni = (struct ldif_info *) be->be_private;
1051         ni->li_tool_current = 0;
1052         return 0;
1053 }                                       
1054
1055 static int ldif_tool_entry_close(BackendDB * be) {
1056         struct ldif_info *ni = (struct ldif_info *) be->be_private;
1057
1058         SLAP_FREE(ni->li_tool_cookie.entries);
1059         return 0;
1060 }
1061
1062 static ID
1063 ldif_tool_entry_first(BackendDB *be)
1064 {
1065         struct ldif_info *ni = (struct ldif_info *) be->be_private;
1066         ID id = 1; /* first entry in the array of entries shifted by one */
1067
1068         ni->li_tool_current = 1;
1069         if(ni->li_tool_cookie.entries == NULL) {
1070                 Operation op = {0};
1071
1072                 op.o_bd = be;
1073                 op.o_req_dn = *be->be_suffix;
1074                 op.o_req_ndn = *be->be_nsuffix;
1075                 op.ors_scope = LDAP_SCOPE_SUBTREE;
1076                 ni->li_tool_cookie.op = &op;
1077                 (void)enum_tree( &ni->li_tool_cookie );
1078                 ni->li_tool_cookie.op = NULL;
1079         }
1080         return id;
1081 }
1082
1083 static ID ldif_tool_entry_next(BackendDB *be)
1084 {
1085         struct ldif_info *ni = (struct ldif_info *) be->be_private;
1086         ni->li_tool_current += 1;
1087         if(ni->li_tool_current > ni->li_tool_cookie.eind)
1088                 return NOID;
1089         else
1090                 return ni->li_tool_current;
1091 }
1092
1093 static Entry * ldif_tool_entry_get(BackendDB * be, ID id) {
1094         struct ldif_info *ni = (struct ldif_info *) be->be_private;
1095         Entry * e;
1096
1097         if(id > ni->li_tool_cookie.eind || id < 1)
1098                 return NULL;
1099         else {
1100                 e = ni->li_tool_cookie.entries[id - 1];
1101                 ni->li_tool_cookie.entries[id - 1] = NULL;
1102                 return e;
1103         }
1104 }
1105
1106 static ID ldif_tool_entry_put(BackendDB * be, Entry * e, struct berval *text) {
1107         struct ldif_info *ni = (struct ldif_info *) be->be_private;
1108         struct berval dn = e->e_nname;
1109         struct berval leaf_path = BER_BVNULL;
1110         struct stat stats;
1111         int statres;
1112         int res = LDAP_SUCCESS;
1113
1114         dn2path(&dn, &be->be_nsuffix[0], &ni->li_base_path, &leaf_path);
1115
1116         if(leaf_path.bv_val != NULL) {
1117                 struct berval base = BER_BVNULL;
1118                 /* build path to container, and path to ldif of container */
1119                 get_parent_path(&leaf_path, &base);
1120
1121                 statres = stat(base.bv_val, &stats); /* check if container exists */
1122                 if(statres == -1 && errno == ENOENT) { /* container missing */
1123                         base.bv_val[base.bv_len] = '.';
1124                         statres = stat(base.bv_val, &stats); /* check for leaf node */
1125                         base.bv_val[base.bv_len] = '\0';
1126                         if(statres == -1 && errno == ENOENT) {
1127                                 res = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
1128                         }
1129                         else if(statres != -1) { /* create parent */
1130                                 int mkdirres = mkdir(base.bv_val, 0750);
1131                                 if(mkdirres == -1) {
1132                                         res = LDAP_UNWILLING_TO_PERFORM;
1133                                 }
1134                         }
1135                         else
1136                                 res = LDAP_UNWILLING_TO_PERFORM;
1137                 }/* container was possibly created, move on to add the entry */
1138                 if(res == LDAP_SUCCESS) {
1139                         statres = stat(leaf_path.bv_val, &stats);
1140                         if(statres == -1 && errno == ENOENT) {
1141                                 res = (int) spew_entry(e, &leaf_path);
1142                         }
1143                         else /* it already exists */
1144                                 res = LDAP_ALREADY_EXISTS;
1145                 }
1146                 SLAP_FREE(base.bv_val);
1147                 SLAP_FREE(leaf_path.bv_val);
1148         }
1149
1150         if(res == LDAP_SUCCESS) {
1151                 return 1;
1152         }
1153         else
1154                 return NOID;
1155 }
1156
1157 static int
1158 ldif_back_db_init( BackendDB *be )
1159 {
1160         struct ldif_info *ni;
1161
1162         ni = ch_calloc( 1, sizeof(struct ldif_info) );
1163         be->be_private = ni;
1164         be->be_cf_table = be->bd_info->bi_cf_table;
1165         ldap_pvt_thread_mutex_init(&ni->li_mutex);
1166         return 0;
1167 }
1168
1169 static int
1170 ldif_back_db_destroy(
1171                            Backend      *be
1172                            )
1173 {
1174         struct ldif_info *ni = be->be_private;
1175         ldap_pvt_thread_mutex_destroy(&ni->li_mutex);
1176         free( be->be_private );
1177         return 0;
1178 }
1179
1180 static int
1181 ldif_back_db_open(
1182                         Backend *be
1183                         )
1184 {
1185         struct ldif_info *ni = (struct ldif_info *) be->be_private;
1186         if( BER_BVISEMPTY(&ni->li_base_path)) {/* missing base path */
1187                 fprintf(stderr, "missing base path for back-ldif\n");
1188                 return 1;
1189         }
1190         return 0;
1191 }
1192
1193 int
1194 ldif_back_initialize(
1195                            BackendInfo  *bi
1196                            )
1197 {
1198         static char *controls[] = {
1199                 LDAP_CONTROL_MANAGEDSAIT,
1200                 NULL
1201         };
1202         int rc;
1203
1204         bi->bi_flags |=
1205                 SLAP_BFLAG_REFERRALS;
1206
1207         bi->bi_controls = controls;
1208
1209         bi->bi_cf_table = ldifcfg;
1210
1211         bi->bi_open = 0;
1212         bi->bi_close = 0;
1213         bi->bi_config = 0;
1214         bi->bi_destroy = 0;
1215
1216         bi->bi_db_init = ldif_back_db_init;
1217         bi->bi_db_config = config_generic_wrapper;
1218         bi->bi_db_open = ldif_back_db_open;
1219         bi->bi_db_close = 0;
1220         bi->bi_db_destroy = ldif_back_db_destroy;
1221
1222         bi->bi_op_bind = ldif_back_bind;
1223         bi->bi_op_unbind = 0;
1224         bi->bi_op_search = ldif_back_search;
1225         bi->bi_op_compare = ldif_back_compare;
1226         bi->bi_op_modify = ldif_back_modify;
1227         bi->bi_op_modrdn = ldif_back_modrdn;
1228         bi->bi_op_add = ldif_back_add;
1229         bi->bi_op_delete = ldif_back_delete;
1230         bi->bi_op_abandon = 0;
1231
1232         bi->bi_extended = 0;
1233
1234         bi->bi_chk_referrals = ldif_back_referrals;
1235
1236         bi->bi_connection_init = 0;
1237         bi->bi_connection_destroy = 0;
1238
1239         bi->bi_tool_entry_open = ldif_tool_entry_open;
1240         bi->bi_tool_entry_close = ldif_tool_entry_close;
1241         bi->bi_tool_entry_first = ldif_tool_entry_first;
1242         bi->bi_tool_entry_next = ldif_tool_entry_next;
1243         bi->bi_tool_entry_get = ldif_tool_entry_get;
1244         bi->bi_tool_entry_put = ldif_tool_entry_put;
1245         bi->bi_tool_entry_reindex = 0;
1246         bi->bi_tool_sync = 0;
1247         
1248         bi->bi_tool_dn2id_get = 0;
1249         bi->bi_tool_id2entry_get = 0;
1250         bi->bi_tool_entry_modify = 0;
1251
1252         rc = config_register_schema( ldifcfg, ldifocs );
1253         if ( rc ) return rc;
1254         ldifcfg[0].ad = slap_schema.si_ad_objectClass;
1255         return 0;
1256 }