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