]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldif/ldif.c
apply naming scheme to global/database schema
[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                 "( OLcfgDbAt:0.1 NAME 'olcDbDirectory' "
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         { "( OLcfgDbOc:2.1 "
73                 "NAME 'olcLdifConfig' "
74                 "DESC 'LDIF backend configuration' "
75                 "SUP olcDatabaseConfig "
76                 "MUST ( olcDbDirectory ) )", 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                         dnParent( &pndn, &pndn );
583                         
584                         if ( !dnIsSuffix( &pndn, &op->o_bd->be_nsuffix[0] ) ) {
585                                 break;
586                         }
587
588                         op->o_req_dn = pndn;
589                         op->o_req_ndn = pndn;
590
591                         entry = (Entry *)get_entry( op, &ni->li_base_path );
592                 }
593
594                 ldap_pvt_thread_mutex_unlock( &ni->li_mutex );
595
596                 op->o_req_dn = odn;
597                 op->o_req_ndn = ondn;
598
599                 rc = LDAP_SUCCESS;
600                 rs->sr_matched = NULL;
601                 if ( entry != NULL ) {
602                         Debug( LDAP_DEBUG_TRACE,
603                                 "ldif_back_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
604                                 (long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
605
606                         if ( is_entry_referral( entry ) ) {
607                                 rc = LDAP_OTHER;
608                                 rs->sr_ref = get_entry_referrals( op, entry );
609                                 if ( rs->sr_ref ) {
610                                         rs->sr_matched = ber_strdup_x(
611                                         entry->e_name.bv_val, op->o_tmpmemctx );
612                                 }
613                         }
614
615                         entry_free(entry);
616
617                 } else if ( default_referral != NULL ) {
618                         rc = LDAP_OTHER;
619                         rs->sr_ref = referral_rewrite( default_referral,
620                                 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
621                 }
622
623                 if ( rs->sr_ref != NULL ) {
624                         /* send referrals */
625                         rc = rs->sr_err = LDAP_REFERRAL;
626                         send_ldap_result( op, rs );
627                         ber_bvarray_free( rs->sr_ref );
628                         rs->sr_ref = NULL;
629
630                 } else if ( rc != LDAP_SUCCESS ) {
631                         rs->sr_err = rc;
632                         rs->sr_text = rs->sr_matched ? "bad referral object" : NULL;
633                         send_ldap_result( op, rs );
634                 }
635
636                 if ( rs->sr_matched ) {
637                         op->o_tmpfree( (char *)rs->sr_matched, op->o_tmpmemctx );
638                         rs->sr_matched = NULL;
639                 }
640
641                 return rc;
642         }
643
644         ldap_pvt_thread_mutex_unlock( &ni->li_mutex );
645
646         if ( is_entry_referral( entry ) ) {
647                 /* entry is a referral */
648                 BerVarray refs = get_entry_referrals( op, entry );
649                 rs->sr_ref = referral_rewrite(
650                         refs, &entry->e_name, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
651
652                 Debug( LDAP_DEBUG_TRACE,
653                         "ldif_back_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
654                         (long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
655
656                 rs->sr_matched = entry->e_name.bv_val;
657                 if ( rs->sr_ref != NULL ) {
658                         rc = rs->sr_err = LDAP_REFERRAL;
659                         send_ldap_result( op, rs );
660                         ber_bvarray_free( rs->sr_ref );
661                         rs->sr_ref = NULL;
662
663                 } else {
664                         send_ldap_error( op, rs, LDAP_OTHER, "bad referral object" );
665                         rc = rs->sr_err;
666                 }
667
668                 rs->sr_matched = NULL;
669                 ber_bvarray_free( refs );
670
671                 entry_free( entry );
672         }
673
674         return rc;
675 }
676
677 static int
678 ldif_back_bind( Operation *op, SlapReply *rs )
679 {
680         struct ldif_info *ni = NULL;
681         Attribute * a = NULL;
682         AttributeDescription *password = slap_schema.si_ad_userPassword;
683         int return_val = 0;
684         Entry * entry = NULL;
685
686         ni = (struct ldif_info *) op->o_bd->be_private;
687         ldap_pvt_thread_mutex_lock(&ni->li_mutex);
688         entry = (Entry *) get_entry(op, &ni->li_base_path);
689
690         /* no object is found for them */
691         if(entry == NULL) {
692                 if(be_isroot_pw(op)) {
693                         rs->sr_err = return_val = LDAP_SUCCESS;
694                 } else {
695                         rs->sr_err = return_val = LDAP_INVALID_CREDENTIALS;
696                 }
697                 goto return_result;
698         }
699
700         /* they don't have userpassword */
701         if((a = attr_find(entry->e_attrs, password)) == NULL) {
702                 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
703                 return_val = 1;
704                 goto return_result;
705         }
706
707         /* authentication actually failed */
708         if(slap_passwd_check(op, entry, a, &op->oq_bind.rb_cred,
709                              &rs->sr_text) != 0) {
710                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
711                 return_val = 1;
712                 goto return_result;
713         }
714
715         /* let the front-end send success */
716         return_val = 0;
717         goto return_result;
718
719  return_result:
720         ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
721         if(return_val != 0)
722                 send_ldap_result( op, rs );
723         if(entry != NULL)
724                 entry_free(entry);
725         return return_val;
726 }
727
728 static int ldif_back_search(Operation *op, SlapReply *rs)
729 {
730         struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
731         int numentries = 0;
732         int i = 0;
733         Entry ** entries = NULL;
734
735         ldap_pvt_thread_mutex_lock(&ni->li_mutex);
736         rs->sr_err = enum_tree(op->o_bd, &op->o_req_dn, &op->o_req_ndn, &numentries, &entries, op->ors_scope);
737         if ( rs->sr_err == LDAP_SUCCESS ) {
738                 for ( i = 0; i < numentries; i++ ) {
739
740
741                         /*
742                          * if it's a referral, add it to the list of referrals. only do
743                          * this for non-base searches, and don't check the filter
744                          * explicitly here since it's only a candidate anyway.
745                          */
746                         if ( !get_manageDSAit( op )
747                                         && op->oq_search.rs_scope != LDAP_SCOPE_BASE
748                                         && is_entry_referral( entries[i] ) )
749                         {
750                                 BerVarray erefs = get_entry_referrals( op, entries[i] );
751                                 rs->sr_ref = referral_rewrite( erefs,
752                                                 &entries[i]->e_name, NULL,
753                                                 op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
754                                                         ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
755
756                                 send_search_reference( op, rs );
757
758                                 ber_bvarray_free( rs->sr_ref );
759                                 ber_bvarray_free( erefs );
760                                 rs->sr_ref = NULL;
761
762                         } else if ( test_filter( op, entries[i], op->ors_filter ) == LDAP_COMPARE_TRUE )
763                         {
764                                 rs->sr_entry = entries[i];
765                                 rs->sr_attrs = op->ors_attrs;
766                                 rs->sr_flags = REP_ENTRY_MODIFIABLE;
767                                 send_search_entry(op, rs);
768                         }
769                         entry_free(entries[i]);
770                 }
771         }
772         SLAP_FREE(entries);
773         ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
774         send_ldap_result(op, rs);
775
776         return rs->sr_err;
777 }
778
779 static int ldif_back_add(Operation *op, SlapReply *rs) {
780         struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
781         Entry * e = op->ora_e;
782         struct berval dn = e->e_nname;
783         struct berval leaf_path = BER_BVNULL;
784         struct stat stats;
785         int statres;
786         char textbuf[SLAP_TEXT_BUFLEN];
787
788         rs->sr_err = entry_schema_check(op->o_bd, e,
789                                   NULL, &rs->sr_text, textbuf, sizeof( textbuf ) );
790         if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
791                                 
792         ldap_pvt_thread_mutex_lock(&ni->li_mutex);
793
794         dn2path(&dn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &leaf_path);
795
796         if(leaf_path.bv_val != NULL) {
797                 struct berval base = BER_BVNULL;
798                 /* build path to container and ldif of container */
799                 get_parent_path(&leaf_path, &base);
800
801                 statres = stat(base.bv_val, &stats); /* check if container exists */
802                 if(statres == -1 && errno == ENOENT) { /* container missing */
803                         base.bv_val[base.bv_len] = '.';
804                         statres = stat(base.bv_val, &stats); /* check for leaf node */
805                         base.bv_val[base.bv_len] = '\0';
806                         if(statres == -1 && errno == ENOENT) {
807                                 rs->sr_err = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
808                         }
809                         else if(statres != -1) { /* create parent */
810                                 int mkdirres = mkdir(base.bv_val, 0750);
811                                 if(mkdirres == -1) {
812                                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
813                                 }
814                         }
815                         else
816                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
817                 }/* container was possibly created, move on to add the entry */
818                 if(rs->sr_err == LDAP_SUCCESS) {
819                         statres = stat(leaf_path.bv_val, &stats);
820                         if(statres == -1 && errno == ENOENT) {
821                                 ldap_pvt_thread_mutex_lock(&entry2str_mutex);
822                                 rs->sr_err = (int) spew_entry(e, &leaf_path);
823                                 ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
824                         }
825                         else /* it already exists */
826                                 rs->sr_err = LDAP_ALREADY_EXISTS;
827                 }
828                 SLAP_FREE(base.bv_val);
829                 SLAP_FREE(leaf_path.bv_val);
830         }
831
832         ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
833
834 send_res:
835         send_ldap_result(op, rs);
836         return 0;
837 }
838
839 static int ldif_back_modify(Operation *op, SlapReply *rs) {
840         struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
841         Modifications * modlst = op->orm_modlist;
842         struct berval path = BER_BVNULL;
843         Entry * entry = NULL;
844         int spew_res;
845
846         ldap_pvt_thread_mutex_lock(&ni->li_mutex);
847         dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path,
848                 &path);
849         entry = (Entry *) get_entry(op, &ni->li_base_path);
850
851         if(entry != NULL) {
852                 rs->sr_err = apply_modify_to_entry(entry, modlst, op, rs);
853                 if(rs->sr_err == LDAP_SUCCESS) {
854                         ldap_pvt_thread_mutex_lock(&entry2str_mutex);
855                         spew_res = spew_entry(entry, &path);
856                         ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
857                         if(spew_res == -1) {
858                                 perror("could not output entry");
859                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
860                         }
861                 }
862         }
863         else {
864                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
865         }
866         
867         if(entry != NULL)
868                 entry_free(entry);
869         if(path.bv_val != NULL)
870                 SLAP_FREE(path.bv_val);
871         rs->sr_text = NULL;
872         ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
873         send_ldap_result(op, rs);
874         return 0;
875 }
876
877 static int ldif_back_delete(Operation *op, SlapReply *rs) {
878         struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
879         struct berval path = BER_BVNULL;
880         int res = 0;
881
882         ldap_pvt_thread_mutex_lock(&ni->li_mutex);
883         dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &path);
884
885         path.bv_val[path.bv_len - STRLENOF(LDIF)] = '\0';
886         res = rmdir(path.bv_val);
887         path.bv_val[path.bv_len - STRLENOF(LDIF)] = '.';
888         if ( res && errno != ENOENT ) {
889                 rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
890         } else {
891                 res = unlink(path.bv_val);
892         }
893
894         if(res == -1) {
895                 if(errno == ENOENT)
896                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
897                 else
898                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
899         }
900         else
901                 rs->sr_err = LDAP_SUCCESS;
902
903         SLAP_FREE(path.bv_val);
904         ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
905         send_ldap_result(op, rs);
906         return 0;
907 }
908
909
910 static int move_entry(Entry * entry, struct berval * ndn,
911                            struct berval * newndn, struct berval * rootdn,
912                            struct berval * base_path) {
913         int res;
914         int exists_res;
915         struct berval path;
916         struct berval newpath;
917
918         dn2path(ndn, rootdn, base_path, &path);
919         dn2path(newndn, rootdn, base_path, &newpath);
920
921         if((entry == NULL || path.bv_val == NULL) || newpath.bv_val == NULL) {
922                 /* some object doesn't exist */
923                 res = LDAP_NO_SUCH_OBJECT;
924         }
925         else { /* do the modrdn */
926                 exists_res = open(newpath.bv_val, O_RDONLY);
927                 if(exists_res == -1 && errno == ENOENT) {
928                         res = spew_entry(entry, &newpath);
929                         if(res != -1) {
930                                 /* if this fails we should log something bad */
931                                 res = unlink(path.bv_val);
932                                 res = LDAP_SUCCESS;
933                         }
934                         else {
935                                 if(errno == ENOENT)
936                                         res = LDAP_NO_SUCH_OBJECT;
937                                 else
938                                         res = LDAP_UNWILLING_TO_PERFORM;
939                                 unlink(newpath.bv_val); /* in case file was created */
940                         }
941                 }
942                 else if(exists_res) {
943                         int close_res = close(exists_res);
944                         res = LDAP_ALREADY_EXISTS;
945                         if(close_res == -1) {
946                         /* log heinous error */
947                         }
948                 }
949                 else {
950                         res = LDAP_UNWILLING_TO_PERFORM;
951                 }
952         }
953
954         if(newpath.bv_val != NULL)
955                 SLAP_FREE(newpath.bv_val);
956         if(path.bv_val != NULL)
957                 SLAP_FREE(path.bv_val);
958         return res;
959 }
960
961 static int ldif_back_modrdn(Operation *op, SlapReply *rs) {
962         struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
963         struct berval new_dn = BER_BVNULL, new_ndn = BER_BVNULL;
964         struct berval p_dn, bv = BER_BVNULL;
965         Entry * entry = NULL;
966         LDAPRDN new_rdn = NULL;
967         LDAPRDN old_rdn = NULL;
968         Modifications * mods = NULL;
969         int res;
970
971         ldap_pvt_thread_mutex_lock(&ni->li_mutex);
972         ldap_pvt_thread_mutex_lock(&entry2str_mutex);
973         entry = (Entry *) get_entry(op, &ni->li_base_path);
974
975         /* build the mods to the entry */
976         if(entry != NULL) {
977                 if(ldap_bv2rdn(&op->oq_modrdn.rs_newrdn, &new_rdn,
978                         (char **)&rs->sr_text, LDAP_DN_FORMAT_LDAP)) {
979                         rs->sr_err = LDAP_INVALID_DN_SYNTAX;
980                 }
981                 else if(op->oq_modrdn.rs_deleteoldrdn &&
982                         ldap_bv2rdn(&op->o_req_dn, &old_rdn, (char **)&rs->sr_text,
983                         LDAP_DN_FORMAT_LDAP)) {
984                         rs->sr_err = LDAP_OTHER;
985                 }
986                 else { /* got both rdns successfully, ready to build mods */
987                         if(slap_modrdn2mods(op, rs, entry, old_rdn, new_rdn, &mods)
988                                 != LDAP_SUCCESS) {
989                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
990                         }
991                         else { /* built mods successfully */
992
993                                 /* build new dn, and new ndn for the entry */
994                                 if(op->oq_modrdn.rs_newSup != NULL) /* new superior */
995                                         p_dn = *op->oq_modrdn.rs_newSup;
996                                 else
997                                         p_dn = slap_empty_bv;
998                                 dnParent(&entry->e_name, &p_dn);
999                                 build_new_dn(&new_dn, &p_dn, &op->oq_modrdn.rs_newrdn, NULL); 
1000                                 dnNormalize( 0, NULL, NULL, &new_dn, &bv, op->o_tmpmemctx );
1001                                 ber_dupbv( &new_ndn, &bv );
1002                                 entry->e_name = new_dn;
1003                                 entry->e_nname = new_ndn;
1004
1005                                 /* perform the modifications */
1006                                 res = apply_modify_to_entry(entry, mods, op, rs);
1007                                 if(res == LDAP_SUCCESS) {
1008                                         rs->sr_err = move_entry(entry, &op->o_req_ndn,
1009                                                                 &new_ndn,
1010                                                                 &op->o_bd->be_nsuffix[0],
1011                                                                 &ni->li_base_path);
1012                                 }
1013                                 else
1014                                         rs->sr_err = res;
1015                         }
1016                 }
1017         }
1018         else /* entry was null */
1019                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1020
1021         if(entry != NULL)
1022                 entry_free(entry);
1023         rs->sr_text = "";
1024         ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
1025         ldap_pvt_thread_mutex_unlock(&entry2str_mutex);
1026         send_ldap_result(op, rs);
1027         return 0;
1028 }
1029
1030 static int ldif_back_compare(Operation *op, SlapReply *rs) {
1031         struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
1032         Entry * e = NULL;
1033         Attribute       *a;
1034
1035         ldap_pvt_thread_mutex_lock(&ni->li_mutex);
1036
1037         e = (Entry *) get_entry(op, &ni->li_base_path);
1038         if(e != NULL) {
1039                 for(a = attrs_find( e->e_attrs, op->oq_compare.rs_ava->aa_desc );
1040                         a != NULL;
1041                         a = attrs_find( a->a_next, op->oq_compare.rs_ava->aa_desc )) {
1042                         rs->sr_err = LDAP_COMPARE_FALSE;
1043                 
1044                         if (value_find_ex(op->oq_compare.rs_ava->aa_desc,
1045                                                 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1046                                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1047                                                 a->a_nvals, &op->oq_compare.rs_ava->aa_value,
1048                                                 op->o_tmpmemctx ) == 0) {
1049                                 rs->sr_err = LDAP_COMPARE_TRUE;
1050                                 break;
1051                         }
1052                 }
1053         }
1054         else {
1055                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1056         }
1057
1058         if(e != NULL)
1059                 entry_free(e);
1060         ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
1061         send_ldap_result(op, rs);
1062         return 0;
1063 }
1064
1065 static int ldif_tool_entry_open(BackendDB * be, int mode) {
1066         struct ldif_info *ni = (struct ldif_info *) be->be_private;
1067         ni->tool_entries = NULL;
1068         ni->tool_numentries = 0;
1069         ni->tool_current = 0;
1070         ni->tool_put_entry_flag = 0;
1071         return 0;
1072 }                                       
1073
1074 static int ldif_tool_entry_close(BackendDB * be) {
1075         struct ldif_info *ni = (struct ldif_info *) be->be_private;
1076
1077         SLAP_FREE(ni->tool_entries);
1078         return 0;
1079 }
1080
1081 static ID
1082 ldif_tool_entry_first(BackendDB *be)
1083 {
1084         struct ldif_info *ni = (struct ldif_info *) be->be_private;
1085         ID id = 1; /* first entry in the array of entries shifted by one */
1086
1087         ni->tool_current = 1;
1088         if(ni->tool_entries == NULL || ni->tool_put_entry_flag) {
1089                 (void)enum_tree(be, be->be_suffix, be->be_nsuffix,
1090                         &ni->tool_numentries, &ni->tool_entries,
1091                         LDAP_SCOPE_SUBTREE);
1092                 ni->tool_put_entry_flag = 0;
1093         }
1094         return id;
1095 }
1096
1097 static ID ldif_tool_entry_next(BackendDB *be)
1098 {
1099         struct ldif_info *ni = (struct ldif_info *) be->be_private;
1100         ni->tool_current += 1;
1101         if(ni->tool_put_entry_flag) {
1102                  (void)enum_tree(be, be->be_suffix, be->be_nsuffix,
1103                         &ni->tool_numentries, &ni->tool_entries,
1104                         LDAP_SCOPE_SUBTREE);
1105                 ni->tool_put_entry_flag = 0;
1106         }
1107         if(ni->tool_current > ni->tool_numentries)
1108                 return NOID;
1109         else
1110                 return ni->tool_current;
1111 }
1112
1113 static Entry * ldif_tool_entry_get(BackendDB * be, ID id) {
1114         struct ldif_info *ni = (struct ldif_info *) be->be_private;
1115         Entry * e;
1116
1117         if(id > ni->tool_numentries || id < 1)
1118                 return NULL;
1119         else {
1120                 e = ni->tool_entries[id - 1];
1121                 ni->tool_entries[id - 1] = NULL;
1122                 return e;
1123         }
1124 }
1125
1126 static ID ldif_tool_entry_put(BackendDB * be, Entry * e, struct berval *text) {
1127         struct ldif_info *ni = (struct ldif_info *) be->be_private;
1128         struct berval dn = e->e_nname;
1129         struct berval leaf_path = BER_BVNULL;
1130         struct stat stats;
1131         int statres;
1132         int res = LDAP_SUCCESS;
1133
1134         dn2path(&dn, &be->be_nsuffix[0], &ni->li_base_path, &leaf_path);
1135
1136         if(leaf_path.bv_val != NULL) {
1137                 struct berval base = BER_BVNULL;
1138                 /* build path to container, and path to ldif of container */
1139                 get_parent_path(&leaf_path, &base);
1140
1141                 statres = stat(base.bv_val, &stats); /* check if container exists */
1142                 if(statres == -1 && errno == ENOENT) { /* container missing */
1143                         base.bv_val[base.bv_len] = '.';
1144                         statres = stat(base.bv_val, &stats); /* check for leaf node */
1145                         base.bv_val[base.bv_len] = '\0';
1146                         if(statres == -1 && errno == ENOENT) {
1147                                 res = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */
1148                         }
1149                         else if(statres != -1) { /* create parent */
1150                                 int mkdirres = mkdir(base.bv_val, 0750);
1151                                 if(mkdirres == -1) {
1152                                         res = LDAP_UNWILLING_TO_PERFORM;
1153                                 }
1154                         }
1155                         else
1156                                 res = LDAP_UNWILLING_TO_PERFORM;
1157                 }/* container was possibly created, move on to add the entry */
1158                 if(res == LDAP_SUCCESS) {
1159                         statres = stat(leaf_path.bv_val, &stats);
1160                         if(statres == -1 && errno == ENOENT) {
1161                                 res = (int) spew_entry(e, &leaf_path);
1162                         }
1163                         else /* it already exists */
1164                                 res = LDAP_ALREADY_EXISTS;
1165                 }
1166                 SLAP_FREE(base.bv_val);
1167                 SLAP_FREE(leaf_path.bv_val);
1168         }
1169
1170         if(res == LDAP_SUCCESS) {
1171                 ni->tool_put_entry_flag = 1;
1172                 return 1;
1173         }
1174         else
1175                 return NOID;
1176 }
1177
1178 static int
1179 ldif_back_db_init( BackendDB *be )
1180 {
1181         struct ldif_info *ni;
1182
1183         ni = ch_calloc( 1, sizeof(struct ldif_info) );
1184         be->be_private = ni;
1185         be->be_cf_table = be->bd_info->bi_cf_table;
1186         ldap_pvt_thread_mutex_init(&ni->li_mutex);
1187         return 0;
1188 }
1189
1190 static int
1191 ldif_back_db_destroy(
1192                            Backend      *be
1193                            )
1194 {
1195         struct ldif_info *ni = be->be_private;
1196         ldap_pvt_thread_mutex_destroy(&ni->li_mutex);
1197         free( be->be_private );
1198         return 0;
1199 }
1200
1201 static int
1202 ldif_back_db_open(
1203                         Backend *be
1204                         )
1205 {
1206         struct ldif_info *ni = (struct ldif_info *) be->be_private;
1207         if( BER_BVISEMPTY(&ni->li_base_path)) {/* missing base path */
1208                 fprintf(stderr, "missing base path for back-ldif\n");
1209                 return 1;
1210         }
1211         return 0;
1212 }
1213
1214 int
1215 ldif_back_initialize(
1216                            BackendInfo  *bi
1217                            )
1218 {
1219         static char *controls[] = {
1220                 LDAP_CONTROL_MANAGEDSAIT,
1221                 NULL
1222         };
1223         int rc;
1224
1225         bi->bi_flags |=
1226                 SLAP_BFLAG_REFERRALS;
1227
1228         bi->bi_controls = controls;
1229
1230         bi->bi_cf_table = ldifcfg;
1231
1232         bi->bi_open = 0;
1233         bi->bi_close = 0;
1234         bi->bi_config = 0;
1235         bi->bi_destroy = 0;
1236
1237         bi->bi_db_init = ldif_back_db_init;
1238         bi->bi_db_config = config_generic_wrapper;
1239         bi->bi_db_open = ldif_back_db_open;
1240         bi->bi_db_close = 0;
1241         bi->bi_db_destroy = ldif_back_db_destroy;
1242
1243         bi->bi_op_bind = ldif_back_bind;
1244         bi->bi_op_unbind = 0;
1245         bi->bi_op_search = ldif_back_search;
1246         bi->bi_op_compare = ldif_back_compare;
1247         bi->bi_op_modify = ldif_back_modify;
1248         bi->bi_op_modrdn = ldif_back_modrdn;
1249         bi->bi_op_add = ldif_back_add;
1250         bi->bi_op_delete = ldif_back_delete;
1251         bi->bi_op_abandon = 0;
1252
1253         bi->bi_extended = 0;
1254
1255         bi->bi_chk_referrals = ldif_back_referrals;
1256
1257         bi->bi_connection_init = 0;
1258         bi->bi_connection_destroy = 0;
1259
1260         bi->bi_tool_entry_open = ldif_tool_entry_open;
1261         bi->bi_tool_entry_close = ldif_tool_entry_close;
1262         bi->bi_tool_entry_first = ldif_tool_entry_first;
1263         bi->bi_tool_entry_next = ldif_tool_entry_next;
1264         bi->bi_tool_entry_get = ldif_tool_entry_get;
1265         bi->bi_tool_entry_put = ldif_tool_entry_put;
1266         bi->bi_tool_entry_reindex = 0;
1267         bi->bi_tool_sync = 0;
1268         
1269         bi->bi_tool_dn2id_get = 0;
1270         bi->bi_tool_id2entry_get = 0;
1271         bi->bi_tool_entry_modify = 0;
1272
1273         rc = config_register_schema( ldifcfg, ldifocs );
1274         if ( rc ) return rc;
1275         ldifcfg[0].ad = slap_schema.si_ad_objectClass;
1276         return 0;
1277 }