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