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