]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldif/ldif.c
unifdef -DSLAP_ACL_HONOR_DISCLOSE
[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-2006 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, int len) {
148         int writeres = 0;
149         
150         while(len > 0) {
151                 writeres = write(fd, spew, len);
152                 if(writeres == -1) {
153                         Debug( LDAP_DEBUG_ANY, "could not spew write: %s\n",
154                                 strerror( errno ), 0, 0 );
155                         return -1;
156                 }
157                 else {
158                         spew += writeres;
159                         len -= writeres;
160                 }
161         }
162         return writeres;
163 }
164
165 static int spew_entry(Entry * e, struct berval * path) {
166         int rs;
167         int openres;
168         int spew_res;
169         int entry_length;
170         char * entry_as_string;
171
172         openres = open(path->bv_val, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR | S_IWUSR);
173         if(openres == -1) {
174                 if(errno == ENOENT)
175                         rs = LDAP_NO_SUCH_OBJECT;
176                 else
177                         rs = LDAP_UNWILLING_TO_PERFORM;
178                 Debug( LDAP_DEBUG_ANY, "could not open \"%s\": %s\n",
179                         path->bv_val, strerror( errno ), 0 );
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, entry_length);
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                 Debug(LDAP_DEBUG_ANY, "failed to open file \"%s\": %s\n",
263                         path.bv_val, strerror(errno), 0 );
264         }
265
266         if(path.bv_val != NULL)
267                 SLAP_FREE(path.bv_val);
268
269         if ( fd != -1 ) {
270                 return get_entry_for_fd(fd, &pdn, &pndn);
271         }
272
273         return NULL;
274 }
275
276 static void fullpath(struct berval *base, struct berval *name, struct berval *res) {
277         char *ptr;
278         res->bv_len = name->bv_len + base->bv_len + 1;
279         res->bv_val = ch_malloc( res->bv_len + 1 );
280         strcpy(res->bv_val, base->bv_val);
281         ptr = res->bv_val + base->bv_len;
282         *ptr++ = LDAP_DIRSEP[0];
283         strcpy(ptr, name->bv_val);
284 }
285
286 typedef struct bvlist {
287         struct bvlist *next;
288         struct berval bv;
289         struct berval num;
290         unsigned int inum;
291         int off;
292 } bvlist;
293
294
295 static int r_enum_tree(enumCookie *ck, struct berval *path,
296         struct berval *pdn, struct berval *pndn)
297 {
298         Entry *e;
299         int fd, rc = LDAP_SUCCESS;
300
301         fd = open( path->bv_val, O_RDONLY );
302         if ( fd < 0 ) {
303                 Debug( LDAP_DEBUG_ANY,
304                         "=> ldif_enum_tree: failed to open %s: %s\n",
305                         path->bv_val, strerror(errno), 0 );
306                 return LDAP_NO_SUCH_OBJECT;
307         }
308
309         e = get_entry_for_fd(fd, pdn, pndn);
310         if ( !e ) {
311                 Debug( LDAP_DEBUG_ANY,
312                         "=> ldif_enum_tree: failed to read entry for %s\n",
313                         path->bv_val, 0, 0 );
314                 return LDAP_BUSY;
315         }
316
317         if ( ck->op->ors_scope == LDAP_SCOPE_BASE ||
318                 ck->op->ors_scope == LDAP_SCOPE_SUBTREE ) {
319                 /* Send right away? */
320                 if ( ck->rs ) {
321                         /*
322                          * if it's a referral, add it to the list of referrals. only do
323                          * this for non-base searches, and don't check the filter
324                          * explicitly here since it's only a candidate anyway.
325                          */
326                         if ( !get_manageDSAit( ck->op )
327                                         && ck->op->ors_scope != LDAP_SCOPE_BASE
328                                         && is_entry_referral( e ) )
329                         {
330                                 BerVarray erefs = get_entry_referrals( ck->op, e );
331                                 ck->rs->sr_ref = referral_rewrite( erefs,
332                                                 &e->e_name, NULL,
333                                                 ck->op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
334                                                         ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
335
336                                 ck->rs->sr_entry = e;
337                                 rc = send_search_reference( ck->op, ck->rs );
338                                 ber_bvarray_free( ck->rs->sr_ref );
339                                 ber_bvarray_free( erefs );
340                                 ck->rs->sr_ref = NULL;
341                                 ck->rs->sr_entry = NULL;
342
343                         } else if ( test_filter( ck->op, e, ck->op->ors_filter ) == LDAP_COMPARE_TRUE )
344                         {
345                                 ck->rs->sr_entry = e;
346                                 ck->rs->sr_attrs = ck->op->ors_attrs;
347                                 ck->rs->sr_flags = REP_ENTRY_MODIFIABLE;
348                                 rc = send_search_entry(ck->op, ck->rs);
349                                 ck->rs->sr_entry = NULL;
350                         }
351                         fd = 1;
352                         if ( rc )
353                                 goto done;
354                 } else {
355                 /* Queueing up for tool mode */
356                         if(ck->entries == NULL) {
357                                 ck->entries = (Entry **) ch_malloc(sizeof(Entry *) * ENTRY_BUFF_INCREMENT);
358                                 ck->elen = ENTRY_BUFF_INCREMENT;
359                         }
360                         if(ck->eind >= ck->elen) { /* grow entries if necessary */      
361                                 ck->entries = (Entry **) ch_realloc(ck->entries, sizeof(Entry *) * (ck->elen) * 2);
362                                 ck->elen *= 2;
363                         }
364
365                         ck->entries[ck->eind++] = e;
366                         fd = 0;
367                 }
368         } else {
369                 fd = 1;
370         }
371
372         if ( ck->op->ors_scope != LDAP_SCOPE_BASE ) {
373                 DIR * dir_of_path;
374                 bvlist *list = NULL, *ptr;
375
376                 path->bv_len -= STRLENOF( LDIF );
377                 path->bv_val[path->bv_len] = '\0';
378
379                 dir_of_path = opendir(path->bv_val);
380                 if(dir_of_path == NULL) { /* can't open directory */
381                         if ( errno != ENOENT ) {
382                                 /* it shouldn't be treated as an error
383                                  * only if the directory doesn't exist */
384                                 rc = LDAP_BUSY;
385                                 Debug( LDAP_DEBUG_ANY,
386                                         "=> ldif_enum_tree: failed to opendir %s (%d)\n",
387                                         path->bv_val, errno, 0 );
388                         }
389                         goto done;
390                 }
391         
392                 while(1) {
393                         struct berval fname, itmp;
394                         struct dirent * dir;
395                         bvlist *bvl, **prev;
396
397                         dir = readdir(dir_of_path);
398                         if(dir == NULL) break; /* end of the directory */
399                         fname.bv_len = strlen( dir->d_name );
400                         if ( fname.bv_len <= STRLENOF( LDIF ))
401                                 continue;
402                         if ( strcmp( dir->d_name + (fname.bv_len - STRLENOF(LDIF)), LDIF))
403                                 continue;
404                         fname.bv_val = dir->d_name;
405
406                         bvl = ch_malloc( sizeof(bvlist) );
407                         ber_dupbv( &bvl->bv, &fname );
408                         BER_BVZERO( &bvl->num );
409                         itmp.bv_val = strchr( bvl->bv.bv_val, IX_FSL );
410                         if ( itmp.bv_val ) {
411                                 char *ptr;
412                                 itmp.bv_val++;
413                                 ptr = strchr( itmp.bv_val, IX_FSR );
414                                 if ( ptr ) {
415                                         itmp.bv_len = ptr - itmp.bv_val;
416                                         ber_dupbv( &bvl->num, &itmp );
417                                         bvl->inum = strtoul( itmp.bv_val, NULL, 0 );
418                                         itmp.bv_val[0] = '\0';
419                                         bvl->off = itmp.bv_val - bvl->bv.bv_val;
420                                 }
421                         }
422
423                         for (prev = &list; (ptr = *prev) != NULL; prev = &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 = 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 done:
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         int rc;
474
475         dnParent( &ck->op->o_req_dn, &pdn );
476         dnParent( &ck->op->o_req_ndn, &pndn );
477         dn2path( &ck->op->o_req_ndn, &ck->op->o_bd->be_nsuffix[0], &ni->li_base_path, &path);
478         rc = r_enum_tree(ck, &path, &pdn, &pndn);
479         ch_free( path.bv_val );
480         return rc;
481 }
482
483 /* Get the parent path plus the LDIF suffix */
484 static void get_parent_path(struct berval * dnpath, struct berval *res) {
485         int dnpathlen = dnpath->bv_len;
486         int i;
487         
488         for(i = dnpathlen;i>0;i--) /* find the first path seperator */
489                 if(dnpath->bv_val[i] == LDAP_DIRSEP[0])
490                         break;
491         res->bv_len = i;
492         res->bv_val = ch_malloc( res->bv_len + 1 + STRLENOF(LDIF) );
493         strncpy(res->bv_val, dnpath->bv_val, i);
494         strcpy(res->bv_val+i, LDIF);
495         res->bv_val[i] = '\0';
496 }
497
498 static int apply_modify_to_entry(Entry * entry,
499                                 Modifications * modlist,
500                                 Operation * op,
501                                 SlapReply * rs)
502 {
503         char textbuf[SLAP_TEXT_BUFLEN];
504         int rc = LDAP_UNWILLING_TO_PERFORM;
505         Modification *mods = NULL;
506
507         if (!acl_check_modlist(op, entry, modlist)) {
508                 return LDAP_INSUFFICIENT_ACCESS;
509         }
510
511         for (; modlist != NULL; modlist = modlist->sml_next) {
512                 mods = &modlist->sml_mod;
513
514                 switch (mods->sm_op) {
515                 case LDAP_MOD_ADD:
516                         rc = modify_add_values(entry, mods,
517                                    get_permissiveModify(op),
518                                    &rs->sr_text, textbuf,
519                                    sizeof( textbuf ) );
520                         break;
521                                 
522                 case LDAP_MOD_DELETE:
523                         rc = modify_delete_values(entry, mods,
524                                 get_permissiveModify(op),
525                                 &rs->sr_text, textbuf,
526                                 sizeof( textbuf ) );
527                         break;
528                                 
529                 case LDAP_MOD_REPLACE:
530                         rc = modify_replace_values(entry, mods,
531                                  get_permissiveModify(op),
532                                  &rs->sr_text, textbuf,
533                                  sizeof( textbuf ) );
534                         break;
535
536                 case LDAP_MOD_INCREMENT:
537                         rc = modify_increment_values( entry,
538                                 mods, get_permissiveModify(op),
539                                 &rs->sr_text, textbuf,
540                                 sizeof( textbuf ) );
541                         break;
542
543                         break;
544
545                 case SLAP_MOD_SOFTADD:
546                         mods->sm_op = LDAP_MOD_ADD;
547                         rc = modify_add_values(entry, mods,
548                                    get_permissiveModify(op),
549                                    &rs->sr_text, textbuf,
550                                    sizeof( textbuf ) );
551                         mods->sm_op = SLAP_MOD_SOFTADD;
552                         if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
553                                 rc = LDAP_SUCCESS;
554                         }
555                         break;
556                 default:
557                         break;
558                 }
559                 if(rc != LDAP_SUCCESS) break;
560         }
561         
562         if(rc == LDAP_SUCCESS) {
563                 if ( mods->sm_desc == slap_schema.si_ad_objectClass ) {
564                         entry->e_ocflags = 0;
565                 }
566                 /* check that the entry still obeys the schema */
567                 rc = entry_schema_check(op, entry, NULL, 0,
568                           &rs->sr_text, textbuf, sizeof( textbuf ) );
569         }
570         return rc;
571 }
572
573 int
574 ldif_back_referrals( Operation *op, SlapReply *rs )
575 {
576         struct ldif_info        *ni = NULL;
577         Entry                   *entry;
578         int                     rc = LDAP_SUCCESS;
579
580 #if 0
581         if ( op->o_tag == LDAP_REQ_SEARCH ) {
582                 /* let search take care of itself */
583                 return rc;
584         }
585 #endif
586
587         if ( get_manageDSAit( op ) ) {
588                 /* let op take care of DSA management */
589                 return rc;
590         }
591
592         ni = (struct ldif_info *)op->o_bd->be_private;
593         ldap_pvt_thread_mutex_lock( &ni->li_mutex );
594         entry = (Entry *)get_entry( op, &ni->li_base_path );
595
596         /* no object is found for them */
597         if ( entry == NULL ) {
598                 struct berval   odn = op->o_req_dn;
599                 struct berval   ondn = op->o_req_ndn;
600
601                 struct berval   pndn = op->o_req_ndn;
602
603                 for ( ; entry == NULL; ) {
604                         dnParent( &pndn, &pndn );
605                         
606                         if ( !dnIsSuffix( &pndn, &op->o_bd->be_nsuffix[0] ) ) {
607                                 break;
608                         }
609
610                         op->o_req_dn = pndn;
611                         op->o_req_ndn = pndn;
612
613                         entry = (Entry *)get_entry( op, &ni->li_base_path );
614                 }
615
616                 ldap_pvt_thread_mutex_unlock( &ni->li_mutex );
617
618                 op->o_req_dn = odn;
619                 op->o_req_ndn = ondn;
620
621                 rc = LDAP_SUCCESS;
622                 rs->sr_matched = NULL;
623                 if ( entry != NULL ) {
624                         Debug( LDAP_DEBUG_TRACE,
625                                 "ldif_back_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
626                                 (long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
627
628                         if ( is_entry_referral( entry ) ) {
629                                 rc = LDAP_OTHER;
630                                 rs->sr_ref = get_entry_referrals( op, entry );
631                                 if ( rs->sr_ref ) {
632                                         rs->sr_matched = ber_strdup_x(
633                                         entry->e_name.bv_val, op->o_tmpmemctx );
634                                 }
635                         }
636
637                         entry_free(entry);
638
639                 } else if ( default_referral != NULL ) {
640                         rc = LDAP_OTHER;
641                         rs->sr_ref = referral_rewrite( default_referral,
642                                 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
643                 }
644
645                 if ( rs->sr_ref != NULL ) {
646                         /* send referrals */
647                         rc = rs->sr_err = LDAP_REFERRAL;
648                         send_ldap_result( op, rs );
649                         ber_bvarray_free( rs->sr_ref );
650                         rs->sr_ref = NULL;
651
652                 } else if ( rc != LDAP_SUCCESS ) {
653                         rs->sr_err = rc;
654                         rs->sr_text = rs->sr_matched ? "bad referral object" : NULL;
655                         send_ldap_result( op, rs );
656                 }
657
658                 if ( rs->sr_matched ) {
659                         op->o_tmpfree( (char *)rs->sr_matched, op->o_tmpmemctx );
660                         rs->sr_matched = NULL;
661                 }
662
663                 return rc;
664         }
665
666         ldap_pvt_thread_mutex_unlock( &ni->li_mutex );
667
668         if ( is_entry_referral( entry ) ) {
669                 /* entry is a referral */
670                 BerVarray refs = get_entry_referrals( op, entry );
671                 rs->sr_ref = referral_rewrite(
672                         refs, &entry->e_name, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
673
674                 Debug( LDAP_DEBUG_TRACE,
675                         "ldif_back_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
676                         (long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
677
678                 rs->sr_matched = entry->e_name.bv_val;
679                 if ( rs->sr_ref != NULL ) {
680                         rc = rs->sr_err = LDAP_REFERRAL;
681                         send_ldap_result( op, rs );
682                         ber_bvarray_free( rs->sr_ref );
683                         rs->sr_ref = NULL;
684
685                 } else {
686                         send_ldap_error( op, rs, LDAP_OTHER, "bad referral object" );
687                         rc = rs->sr_err;
688                 }
689
690                 rs->sr_matched = NULL;
691                 ber_bvarray_free( refs );
692         }
693
694         entry_free( entry );
695
696         return rc;
697 }
698
699 static int
700 ldif_back_bind( Operation *op, SlapReply *rs )
701 {
702         struct ldif_info *ni = NULL;
703         Attribute * a = NULL;
704         AttributeDescription *password = slap_schema.si_ad_userPassword;
705         int return_val = 0;
706         Entry * entry = NULL;
707
708         ni = (struct ldif_info *) op->o_bd->be_private;
709         ldap_pvt_thread_mutex_lock(&ni->li_mutex);
710         entry = (Entry *) get_entry(op, &ni->li_base_path);
711
712         /* no object is found for them */
713         if(entry == NULL) {
714                 if(be_isroot_pw(op)) {
715                         rs->sr_err = return_val = LDAP_SUCCESS;
716                 } else {
717                         rs->sr_err = return_val = LDAP_INVALID_CREDENTIALS;
718                 }
719                 goto return_result;
720         }
721
722         /* they don't have userpassword */
723         if((a = attr_find(entry->e_attrs, password)) == NULL) {
724                 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
725                 return_val = 1;
726                 goto return_result;
727         }
728
729         /* authentication actually failed */
730         if(slap_passwd_check(op, entry, a, &op->oq_bind.rb_cred,
731                              &rs->sr_text) != 0) {
732                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
733                 return_val = 1;
734                 goto return_result;
735         }
736
737         /* let the front-end send success */
738         return_val = 0;
739         goto return_result;
740
741  return_result:
742         ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
743         if(return_val != 0)
744                 send_ldap_result( op, rs );
745         if(entry != NULL)
746                 entry_free(entry);
747         return return_val;
748 }
749
750 static int ldif_back_search(Operation *op, SlapReply *rs)
751 {
752         struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
753         enumCookie ck = { NULL, NULL, NULL, 0, 0 };
754
755         ck.op = op;
756         ck.rs = rs;
757         ldap_pvt_thread_mutex_lock(&ni->li_mutex);
758         rs->sr_err = enum_tree( &ck );
759         ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
760         send_ldap_result(op, rs);
761
762         return rs->sr_err;
763 }
764
765 static int ldif_back_add(Operation *op, SlapReply *rs) {
766         struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
767         Entry * e = op->ora_e;
768         struct berval dn = e->e_nname;
769         struct berval leaf_path = BER_BVNULL;
770         struct stat stats;
771         int statres;
772         char textbuf[SLAP_TEXT_BUFLEN];
773
774         Debug( LDAP_DEBUG_TRACE, "ldif_back_add: \"%s\"\n", dn.bv_val, 0, 0);
775         slap_add_opattrs( op, &rs->sr_text, textbuf, sizeof( textbuf ), 1 );
776
777         rs->sr_err = entry_schema_check(op, e, NULL, 0,
778                 &rs->sr_text, textbuf, sizeof( textbuf ) );
779         if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
780                                 
781         ldap_pvt_thread_mutex_lock(&ni->li_mutex);
782
783         dn2path(&dn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &leaf_path);
784
785         if(leaf_path.bv_val != NULL) {
786                 struct berval base = BER_BVNULL;
787                 /* build path to container and ldif of container */
788                 get_parent_path(&leaf_path, &base);
789
790                 statres = stat(base.bv_val, &stats); /* check if container exists */
791                 if(statres == -1 && errno == ENOENT) { /* container missing */
792                         base.bv_val[base.bv_len] = '.';
793                         statres = stat(base.bv_val, &stats); /* check for leaf node */
794                         base.bv_val[base.bv_len] = '\0';
795                         if(statres == -1 && errno == ENOENT) {
796                                 rs->sr_err = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
797                                 rs->sr_text = "Parent does not exist";
798                         }
799                         else if(statres != -1) { /* create parent */
800                                 int mkdirres = mkdir(base.bv_val, 0750);
801                                 if(mkdirres == -1) {
802                                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
803                                         rs->sr_text = "Could not create parent folder";
804                                         Debug( LDAP_DEBUG_ANY, "could not create folder \"%s\": %s\n",
805                                                 base.bv_val, strerror( errno ), 0 );
806                                 }
807                         }
808                         else
809                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
810                 }/* container was possibly created, move on to add the entry */
811                 if(rs->sr_err == LDAP_SUCCESS) {
812                         statres = stat(leaf_path.bv_val, &stats);
813                         if(statres == -1 && errno == ENOENT) {
814                                 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
815                                 rs->sr_err = (int) spew_entry(e, &leaf_path);
816                                 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
817                         }
818                         else if ( statres == -1 ) {
819                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
820                                 Debug( LDAP_DEBUG_ANY, "could not stat file \"%s\": %s\n",
821                                         leaf_path.bv_val, strerror( errno ), 0 );
822                         }
823                         else /* it already exists */
824                                 rs->sr_err = LDAP_ALREADY_EXISTS;
825                 }
826                 SLAP_FREE(base.bv_val);
827                 SLAP_FREE(leaf_path.bv_val);
828         }
829
830         ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
831
832 send_res:
833         Debug( LDAP_DEBUG_TRACE, 
834                         "ldif_back_add: err: %d text: %s\n", rs->sr_err, rs->sr_text, 0);
835         send_ldap_result(op, rs);
836         slap_graduate_commit_csn( op );
837         return 0;
838 }
839
840 static int ldif_back_modify(Operation *op, SlapReply *rs) {
841         struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
842         Modifications * modlst = op->orm_modlist;
843         struct berval path = BER_BVNULL;
844         Entry * entry = NULL;
845         int spew_res;
846
847         slap_mods_opattrs( op, &op->orm_modlist, 1 );
848
849         ldap_pvt_thread_mutex_lock(&ni->li_mutex);
850         dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path,
851                 &path);
852         entry = (Entry *) get_entry(op, &ni->li_base_path);
853
854         if(entry != NULL) {
855                 rs->sr_err = apply_modify_to_entry(entry, modlst, op, rs);
856                 if(rs->sr_err == LDAP_SUCCESS) {
857                         int save_errno;
858                         ldap_pvt_thread_mutex_lock(&entry2str_mutex);
859                         spew_res = spew_entry(entry, &path);
860                         save_errno = errno;
861                         ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
862                         if(spew_res == -1) {
863                                 Debug( LDAP_DEBUG_ANY,
864                                         "%s ldif_back_modify: could not output entry \"%s\": %s\n",
865                                         op->o_log_prefix, entry->e_name.bv_val, strerror( save_errno ) );
866                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
867                         }
868                 }
869         }
870         else {
871                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
872         }
873         
874         if(entry != NULL)
875                 entry_free(entry);
876         if(path.bv_val != NULL)
877                 SLAP_FREE(path.bv_val);
878         rs->sr_text = NULL;
879         ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
880         send_ldap_result(op, rs);
881         slap_graduate_commit_csn( op );
882         return 0;
883 }
884
885 static int ldif_back_delete(Operation *op, SlapReply *rs) {
886         struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
887         struct berval path = BER_BVNULL;
888         int res = 0;
889
890         if ( BER_BVISEMPTY( &op->o_csn )) {
891                 struct berval csn;
892                 char csnbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
893
894                 csn.bv_val = csnbuf;
895                 csn.bv_len = sizeof( csnbuf );
896                 slap_get_csn( op, &csn, 1 );
897         }
898
899         ldap_pvt_thread_mutex_lock(&ni->li_mutex);
900         dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &path);
901
902         path.bv_val[path.bv_len - STRLENOF(LDIF)] = '\0';
903         res = rmdir(path.bv_val);
904         path.bv_val[path.bv_len - STRLENOF(LDIF)] = '.';
905         if ( res && errno != ENOENT ) {
906                 rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
907         } else {
908                 res = unlink(path.bv_val);
909         }
910
911         if(res == -1) {
912                 if(errno == ENOENT)
913                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
914                 else
915                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
916         }
917         else
918                 rs->sr_err = LDAP_SUCCESS;
919
920         SLAP_FREE(path.bv_val);
921         ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
922         send_ldap_result(op, rs);
923         slap_graduate_commit_csn( op );
924         return 0;
925 }
926
927
928 static int move_entry(Entry * entry, struct berval * ndn,
929                            struct berval * newndn, struct berval * rootdn,
930                            struct berval * base_path) {
931         int res;
932         int exists_res;
933         struct berval path;
934         struct berval newpath;
935
936         dn2path(ndn, rootdn, base_path, &path);
937         dn2path(newndn, rootdn, base_path, &newpath);
938
939         if((entry == NULL || path.bv_val == NULL) || newpath.bv_val == NULL) {
940                 /* some object doesn't exist */
941                 res = LDAP_NO_SUCH_OBJECT;
942         }
943         else { /* do the modrdn */
944                 exists_res = open(newpath.bv_val, O_RDONLY);
945                 if(exists_res == -1 && errno == ENOENT) {
946                         res = spew_entry(entry, &newpath);
947                         if(res != -1) {
948                                 /* if this fails we should log something bad */
949                                 res = unlink(path.bv_val);
950                                 res = LDAP_SUCCESS;
951                         }
952                         else {
953                                 if(errno == ENOENT)
954                                         res = LDAP_NO_SUCH_OBJECT;
955                                 else
956                                         res = LDAP_UNWILLING_TO_PERFORM;
957                                 unlink(newpath.bv_val); /* in case file was created */
958                         }
959                 }
960                 else if(exists_res) {
961                         int close_res = close(exists_res);
962                         res = LDAP_ALREADY_EXISTS;
963                         if(close_res == -1) {
964                         /* log heinous error */
965                         }
966                 }
967                 else {
968                         res = LDAP_UNWILLING_TO_PERFORM;
969                 }
970         }
971
972         if(newpath.bv_val != NULL)
973                 SLAP_FREE(newpath.bv_val);
974         if(path.bv_val != NULL)
975                 SLAP_FREE(path.bv_val);
976         return res;
977 }
978
979 static int
980 ldif_back_modrdn(Operation *op, SlapReply *rs)
981 {
982         struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
983         struct berval new_dn = BER_BVNULL, new_ndn = BER_BVNULL;
984         struct berval p_dn;
985         Entry * entry = NULL;
986         int res;
987
988         slap_mods_opattrs( op, &op->orr_modlist, 1 );
989
990         ldap_pvt_thread_mutex_lock( &ni->li_mutex );
991         ldap_pvt_thread_mutex_lock( &entry2str_mutex );
992         entry = (Entry *) get_entry( op, &ni->li_base_path );
993
994         /* build the mods to the entry */
995         if ( entry != NULL ) {
996                 /* build new dn, and new ndn for the entry */
997                 if ( op->oq_modrdn.rs_newSup != NULL ) {
998                         /* new superior */
999                         p_dn = *op->oq_modrdn.rs_newSup;
1000                 } else {
1001                         p_dn = slap_empty_bv;
1002                 }
1003                 dnParent( &entry->e_name, &p_dn );
1004                 build_new_dn( &new_dn, &p_dn, &op->oq_modrdn.rs_newrdn, NULL ); 
1005                 dnNormalize( 0, NULL, NULL, &new_dn, &new_ndn, NULL );
1006                 ber_memfree_x( entry->e_name.bv_val, NULL );
1007                 ber_memfree_x( entry->e_nname.bv_val, NULL );
1008                 entry->e_name = new_dn;
1009                 entry->e_nname = new_ndn;
1010
1011                 /* perform the modifications */
1012                 res = apply_modify_to_entry( entry, op->orr_modlist, op, rs );
1013                 if ( res == LDAP_SUCCESS ) {
1014                         rs->sr_err = move_entry( entry, &op->o_req_ndn,
1015                                                 &new_ndn,
1016                                                 &op->o_bd->be_nsuffix[0],
1017                                                 &ni->li_base_path );
1018                 } else {
1019                         rs->sr_err = res;
1020                 }
1021         } else {
1022                 /* entry was null */
1023                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1024         }
1025
1026         if ( entry != NULL ) {
1027                 entry_free( entry );
1028         }
1029         rs->sr_text = "";
1030         ldap_pvt_thread_mutex_unlock( &ni->li_mutex );
1031         ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
1032         send_ldap_result( op, rs );
1033         slap_graduate_commit_csn( op );
1034         return 0;
1035 }
1036
1037 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
1038  */
1039 int ldif_back_entry_get(
1040         Operation *op,
1041         struct berval *ndn,
1042         ObjectClass *oc,
1043         AttributeDescription *at,
1044         int rw,
1045         Entry **ent )
1046 {
1047         struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
1048
1049         ldap_pvt_thread_mutex_lock( &ni->li_mutex );
1050
1051         *ent = (Entry *) get_entry( op, &ni->li_base_path );
1052
1053         ldap_pvt_thread_mutex_unlock( &ni->li_mutex );
1054
1055         return ( *ent == NULL ? 1 : 0 );
1056 }
1057
1058 static int ldif_tool_entry_open(BackendDB * be, int mode) {
1059         struct ldif_info *ni = (struct ldif_info *) be->be_private;
1060         ni->li_tool_current = 0;
1061         return 0;
1062 }                                       
1063
1064 static int ldif_tool_entry_close(BackendDB * be) {
1065         struct ldif_info *ni = (struct ldif_info *) be->be_private;
1066
1067         SLAP_FREE(ni->li_tool_cookie.entries);
1068         return 0;
1069 }
1070
1071 static ID
1072 ldif_tool_entry_first(BackendDB *be)
1073 {
1074         struct ldif_info *ni = (struct ldif_info *) be->be_private;
1075         ID id = 1; /* first entry in the array of entries shifted by one */
1076
1077         ni->li_tool_current = 1;
1078         if(ni->li_tool_cookie.entries == NULL) {
1079                 Operation op = {0};
1080
1081                 op.o_bd = be;
1082                 op.o_req_dn = *be->be_suffix;
1083                 op.o_req_ndn = *be->be_nsuffix;
1084                 op.ors_scope = LDAP_SCOPE_SUBTREE;
1085                 ni->li_tool_cookie.op = &op;
1086                 (void)enum_tree( &ni->li_tool_cookie );
1087                 ni->li_tool_cookie.op = NULL;
1088         }
1089         return id;
1090 }
1091
1092 static ID ldif_tool_entry_next(BackendDB *be)
1093 {
1094         struct ldif_info *ni = (struct ldif_info *) be->be_private;
1095         ni->li_tool_current += 1;
1096         if(ni->li_tool_current > ni->li_tool_cookie.eind)
1097                 return NOID;
1098         else
1099                 return ni->li_tool_current;
1100 }
1101
1102 static Entry * ldif_tool_entry_get(BackendDB * be, ID id) {
1103         struct ldif_info *ni = (struct ldif_info *) be->be_private;
1104         Entry * e;
1105
1106         if(id > ni->li_tool_cookie.eind || id < 1)
1107                 return NULL;
1108         else {
1109                 e = ni->li_tool_cookie.entries[id - 1];
1110                 ni->li_tool_cookie.entries[id - 1] = NULL;
1111                 return e;
1112         }
1113 }
1114
1115 static ID ldif_tool_entry_put(BackendDB * be, Entry * e, struct berval *text) {
1116         struct ldif_info *ni = (struct ldif_info *) be->be_private;
1117         struct berval dn = e->e_nname;
1118         struct berval leaf_path = BER_BVNULL;
1119         struct stat stats;
1120         int statres;
1121         int res = LDAP_SUCCESS;
1122
1123         dn2path(&dn, &be->be_nsuffix[0], &ni->li_base_path, &leaf_path);
1124
1125         if(leaf_path.bv_val != NULL) {
1126                 struct berval base = BER_BVNULL;
1127                 /* build path to container, and path to ldif of container */
1128                 get_parent_path(&leaf_path, &base);
1129
1130                 statres = stat(base.bv_val, &stats); /* check if container exists */
1131                 if(statres == -1 && errno == ENOENT) { /* container missing */
1132                         base.bv_val[base.bv_len] = '.';
1133                         statres = stat(base.bv_val, &stats); /* check for leaf node */
1134                         base.bv_val[base.bv_len] = '\0';
1135                         if(statres == -1 && errno == ENOENT) {
1136                                 res = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
1137                         }
1138                         else if(statres != -1) { /* create parent */
1139                                 int mkdirres = mkdir(base.bv_val, 0750);
1140                                 if(mkdirres == -1) {
1141                                         res = LDAP_UNWILLING_TO_PERFORM;
1142                                 }
1143                         }
1144                         else
1145                                 res = LDAP_UNWILLING_TO_PERFORM;
1146                 }/* container was possibly created, move on to add the entry */
1147                 if(res == LDAP_SUCCESS) {
1148                         statres = stat(leaf_path.bv_val, &stats);
1149                         if(statres == -1 && errno == ENOENT) {
1150                                 res = (int) spew_entry(e, &leaf_path);
1151                         }
1152                         else /* it already exists */
1153                                 res = LDAP_ALREADY_EXISTS;
1154                 }
1155                 SLAP_FREE(base.bv_val);
1156                 SLAP_FREE(leaf_path.bv_val);
1157         }
1158
1159         if(res == LDAP_SUCCESS) {
1160                 return 1;
1161         }
1162         else
1163                 return NOID;
1164 }
1165
1166 static int
1167 ldif_back_db_init( BackendDB *be )
1168 {
1169         struct ldif_info *ni;
1170
1171         ni = ch_calloc( 1, sizeof(struct ldif_info) );
1172         be->be_private = ni;
1173         be->be_cf_ocs = ldifocs;
1174         ldap_pvt_thread_mutex_init(&ni->li_mutex);
1175         return 0;
1176 }
1177
1178 static int
1179 ldif_back_db_destroy(
1180                            Backend      *be
1181                            )
1182 {
1183         struct ldif_info *ni = be->be_private;
1184
1185         ch_free(ni->li_base_path.bv_val);
1186         ldap_pvt_thread_mutex_destroy(&ni->li_mutex);
1187         free( be->be_private );
1188         return 0;
1189 }
1190
1191 static int
1192 ldif_back_db_open(
1193                         Backend *be
1194                         )
1195 {
1196         struct ldif_info *ni = (struct ldif_info *) be->be_private;
1197         if( BER_BVISEMPTY(&ni->li_base_path)) {/* missing base path */
1198                 Debug( LDAP_DEBUG_ANY, "missing base path for back-ldif\n", 0, 0, 0);
1199                 return 1;
1200         }
1201         return 0;
1202 }
1203
1204 int
1205 ldif_back_initialize(
1206                            BackendInfo  *bi
1207                            )
1208 {
1209         static char *controls[] = {
1210                 LDAP_CONTROL_MANAGEDSAIT,
1211                 NULL
1212         };
1213         int rc;
1214
1215         bi->bi_flags |=
1216                 SLAP_BFLAG_INCREMENT |
1217                 SLAP_BFLAG_REFERRALS;
1218
1219         bi->bi_controls = controls;
1220
1221         bi->bi_open = 0;
1222         bi->bi_close = 0;
1223         bi->bi_config = 0;
1224         bi->bi_destroy = 0;
1225
1226         bi->bi_db_init = ldif_back_db_init;
1227         bi->bi_db_config = config_generic_wrapper;
1228         bi->bi_db_open = ldif_back_db_open;
1229         bi->bi_db_close = 0;
1230         bi->bi_db_destroy = ldif_back_db_destroy;
1231
1232         bi->bi_op_bind = ldif_back_bind;
1233         bi->bi_op_unbind = 0;
1234         bi->bi_op_search = ldif_back_search;
1235         bi->bi_op_compare = 0;
1236         bi->bi_op_modify = ldif_back_modify;
1237         bi->bi_op_modrdn = ldif_back_modrdn;
1238         bi->bi_op_add = ldif_back_add;
1239         bi->bi_op_delete = ldif_back_delete;
1240         bi->bi_op_abandon = 0;
1241
1242         bi->bi_extended = 0;
1243
1244         bi->bi_chk_referrals = ldif_back_referrals;
1245
1246         bi->bi_connection_init = 0;
1247         bi->bi_connection_destroy = 0;
1248
1249         bi->bi_entry_get_rw = ldif_back_entry_get;
1250
1251 #if 0   /* NOTE: uncomment to completely disable access control */
1252         bi->bi_access_allowed = slap_access_always_allowed;
1253 #endif
1254
1255         bi->bi_tool_entry_open = ldif_tool_entry_open;
1256         bi->bi_tool_entry_close = ldif_tool_entry_close;
1257         bi->bi_tool_entry_first = ldif_tool_entry_first;
1258         bi->bi_tool_entry_next = ldif_tool_entry_next;
1259         bi->bi_tool_entry_get = ldif_tool_entry_get;
1260         bi->bi_tool_entry_put = ldif_tool_entry_put;
1261         bi->bi_tool_entry_reindex = 0;
1262         bi->bi_tool_sync = 0;
1263         
1264         bi->bi_tool_dn2id_get = 0;
1265         bi->bi_tool_id2entry_get = 0;
1266         bi->bi_tool_entry_modify = 0;
1267
1268         bi->bi_cf_ocs = ldifocs;
1269
1270         rc = config_register_schema( ldifcfg, ldifocs );
1271         if ( rc ) return rc;
1272         return 0;
1273 }