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