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