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