]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-ldif/ldif.c
apply naming scheme to global/database schema
[openldap] / servers / slapd / back-ldif / ldif.c
index 80146064fc2cd7d65bf8451189989ba019e4d054..9467d96288d00ec412e4e33619115c0ad3c93922 100644 (file)
@@ -60,7 +60,7 @@ static ConfigTable ldifcfg[] = {
                ldif_cf, NULL, NULL, NULL },
        { "directory", "dir", 2, 2, 0, ARG_BERVAL|ARG_OFFSET,
                (void *)offsetof(struct ldif_info, li_base_path),
-               "( OLcfgAt:1.1 NAME 'dbDirectory' "
+               "( OLcfgDbAt:0.1 NAME 'olcDbDirectory' "
                        "DESC 'Directory for database content' "
                        "EQUALITY caseIgnoreMatch "
                        "SYNTAX OMsDirectoryString )", NULL, NULL },
@@ -69,11 +69,11 @@ static ConfigTable ldifcfg[] = {
 };
 
 static ConfigOCs ldifocs[] = {
-       { "( OLcfgOc:2.1 "
-               "NAME 'ldifConfig' "
+       { "( OLcfgDbOc:2.1 "
+               "NAME 'olcLdifConfig' "
                "DESC 'LDIF backend configuration' "
                "SUP olcDatabaseConfig "
-               "MUST ( dbDirectory ) )", Cft_Database,
+               "MUST ( olcDbDirectory ) )", Cft_Database,
                &ldif_oc },
        { NULL, 0, NULL }
 };
@@ -153,7 +153,7 @@ static char * slurp_file(int fd) {
 
 static int spew_file(int fd, char * spew) {
        int written = 0;
-       int writeres;
+       int writeres = 0;
        int len = strlen(spew);
        char * spewptr = spew;
        
@@ -224,7 +224,8 @@ static int spew_entry(Entry * e, struct berval * path) {
 
 static Entry * get_entry_for_fd(int fd,
        struct berval *pdn,
-       struct berval *pndn) {
+       struct berval *pndn)
+{
        char * entry = (char *) slurp_file(fd);
        Entry * ldentry = NULL;
        
@@ -270,7 +271,12 @@ static Entry * get_entry(Operation *op, struct berval *base_path) {
 
        if(path.bv_val != NULL)
                SLAP_FREE(path.bv_val);
-       return get_entry_for_fd(fd, &pdn, &pndn);
+
+       if ( fd != -1 ) {
+               return get_entry_for_fd(fd, &pdn, &pndn);
+       }
+
+       return NULL;
 }
 
 static void fullpath(struct berval *base, struct berval *name, struct berval *res) {
@@ -298,29 +304,31 @@ typedef struct enumCookie {
        int scope;
 } enumCookie;
 
-static void r_enum_tree(enumCookie *ck, struct berval *path,
-       struct berval *pdn, struct berval *pndn) {
+static int r_enum_tree(enumCookie *ck, struct berval *path,
+       struct berval *pdn, struct berval *pndn)
+{
        Entry *e;
-       int fd;
-
-       if(ck->entries == NULL) {
-               ck->entries = (Entry **) SLAP_MALLOC(sizeof(Entry *) * ENTRY_BUFF_INCREMENT);
-               ck->elen = ENTRY_BUFF_INCREMENT;
-       }
+       int fd, rc = LDAP_SUCCESS;
 
        fd = open( path->bv_val, O_RDONLY );
        if ( fd < 0 ) {
                Debug( LDAP_DEBUG_TRACE,
                        "=> ldif_enum_tree: failed to open %s\n",
                        path->bv_val, 0, 0 );
-               return;
+               return LDAP_NO_SUCH_OBJECT;
        }
+
+       if(ck->entries == NULL) {
+               ck->entries = (Entry **) SLAP_MALLOC(sizeof(Entry *) * ENTRY_BUFF_INCREMENT);
+               ck->elen = ENTRY_BUFF_INCREMENT;
+       }
+
        e = get_entry_for_fd(fd, pdn, pndn);
        if ( !e ) {
                Debug( LDAP_DEBUG_ANY,
                        "=> ldif_enum_tree: failed to read entry for %s\n",
                        path->bv_val, 0, 0 );
-               return;
+               return LDAP_BUSY;
        }
 
        if ( ck->scope == LDAP_SCOPE_BASE || ck->scope == LDAP_SCOPE_SUBTREE ) {
@@ -344,10 +352,15 @@ static void r_enum_tree(enumCookie *ck, struct berval *path,
                path->bv_val[path->bv_len] = '\0';
 
                dir_of_path = opendir(path->bv_val);
-               if(dir_of_path == NULL) {/* can't open directory */
-                       Debug( LDAP_DEBUG_TRACE,
-                               "=> ldif_enum_tree: failed to opendir %s\n",
-                               path->bv_val, 0, 0 );
+               if(dir_of_path == NULL) { /* can't open directory */
+                       if ( errno != ENOENT ) {
+                               /* it shouldn't be treated as an error
+                                * only if the directory doesn't exist */
+                               rc = LDAP_BUSY;
+                               Debug( LDAP_DEBUG_TRACE,
+                                       "=> ldif_enum_tree: failed to opendir %s (%d)\n",
+                                       path->bv_val, errno, 0 );
+                       }
                        goto leave;
                }
        
@@ -401,7 +414,7 @@ static void r_enum_tree(enumCookie *ck, struct berval *path,
                else if ( ck->scope == LDAP_SCOPE_SUBORDINATE)
                        ck->scope = LDAP_SCOPE_SUBTREE;
 
-               while ( ptr=list ) {
+               while ( ( ptr = list ) ) {
                        struct berval fpath;
 
                        list = ptr->next;
@@ -420,29 +433,35 @@ static void r_enum_tree(enumCookie *ck, struct berval *path,
        }
 leave:
        if ( fd ) entry_free( e );
-       return;
+       return rc;
 }
 
-static Entry ** enum_tree(
+static int
+enum_tree(
        BackendDB *be,
        struct berval *dn,
        struct berval *ndn,
        int * length,
+       Entry ***e,
        int scope )
 {
        struct ldif_info *ni = (struct ldif_info *) be->be_private;
        struct berval path;
-       int index = 0;
+       int rc;
        enumCookie ck = {0};
        struct berval pdn, pndn;
 
+       assert( e != NULL );
+       *e = NULL;
+
        ck.scope = scope;
        dnParent( dn, &pdn );
        dnParent( ndn, &pndn );
        dn2path(ndn, &be->be_nsuffix[0], &ni->li_base_path, &path);
-       r_enum_tree(&ck, &path, &pdn, &pndn);
+       rc = r_enum_tree(&ck, &path, &pdn, &pndn);
        *length = ck.eind;
-       return ck.entries;
+       *e = ck.entries;
+       return rc;
 }
 
 /* Get the parent path plus the LDIF suffix */
@@ -466,19 +485,13 @@ static int apply_modify_to_entry(Entry * entry,
                                SlapReply * rs)
 {
        char textbuf[SLAP_TEXT_BUFLEN];
-       size_t textlen = sizeof textbuf;
-       int rc;
-       int tempdebug;
+       int rc = LDAP_UNWILLING_TO_PERFORM;
        Modification *mods = NULL;
-       Attribute *save_attrs;
 
        if (!acl_check_modlist(op, entry, modlist)) {
                return LDAP_INSUFFICIENT_ACCESS;
        }
 
-       /*  save_attrs = entry->e_attrs; Why?
-                       entry->e_attrs = attrs_dup(entry->e_attrs); */
-
        for (; modlist != NULL; modlist = modlist->sml_next) {
                mods = &modlist->sml_mod;
 
@@ -487,14 +500,14 @@ static int apply_modify_to_entry(Entry * entry,
                        rc = modify_add_values(entry, mods,
                                   get_permissiveModify(op),
                                   &rs->sr_text, textbuf,
-                                  textlen);
+                                  sizeof( textbuf ) );
                        break;
                                
                case LDAP_MOD_DELETE:
                        rc = modify_delete_values(entry, mods,
                                get_permissiveModify(op),
                                &rs->sr_text, textbuf,
-                               textlen);
+                               sizeof( textbuf ) );
 
                        break;
                                
@@ -502,7 +515,7 @@ static int apply_modify_to_entry(Entry * entry,
                        rc = modify_replace_values(entry, mods,
                                 get_permissiveModify(op),
                                 &rs->sr_text, textbuf,
-                                textlen);
+                                sizeof( textbuf ) );
 
                        break;
                case LDAP_MOD_INCREMENT:
@@ -512,7 +525,7 @@ static int apply_modify_to_entry(Entry * entry,
                        rc = modify_add_values(entry, mods,
                                   get_permissiveModify(op),
                                   &rs->sr_text, textbuf,
-                                  textlen);
+                                  sizeof( textbuf ) );
                        mods->sm_op = SLAP_MOD_SOFTADD;
                        if (rc == LDAP_TYPE_OR_VALUE_EXISTS) {
                                rc = LDAP_SUCCESS;
@@ -529,10 +542,135 @@ static int apply_modify_to_entry(Entry * entry,
                        entry->e_ocflags = 0;
                }
                /* check that the entry still obeys the schema */
-               rc = entry_schema_check(op->o_bd, entry,
-                                 save_attrs, &rs->sr_text,
-                                 textbuf, textlen);
+               rc = entry_schema_check(op->o_bd, entry, NULL,
+                                 &rs->sr_text, textbuf, sizeof( textbuf ) );
+       }
+       return rc;
+}
+
+int
+ldif_back_referrals( Operation *op, SlapReply *rs )
+{
+       struct ldif_info        *ni = NULL;
+       Entry                   *entry;
+       int                     rc = LDAP_SUCCESS;
+
+#if 0
+       if ( op->o_tag == LDAP_REQ_SEARCH ) {
+               /* let search take care of itself */
+               return rc;
+       }
+#endif
+
+       if ( get_manageDSAit( op ) ) {
+               /* let op take care of DSA management */
+               return rc;
+       }
+
+       ni = (struct ldif_info *)op->o_bd->be_private;
+       ldap_pvt_thread_mutex_lock( &ni->li_mutex );
+       entry = (Entry *)get_entry( op, &ni->li_base_path );
+
+       /* no object is found for them */
+       if ( entry == NULL ) {
+               struct berval   odn = op->o_req_dn;
+               struct berval   ondn = op->o_req_ndn;
+
+               struct berval   pndn = op->o_req_ndn;
+
+               for ( ; entry == NULL; ) {
+                       dnParent( &pndn, &pndn );
+                       
+                       if ( !dnIsSuffix( &pndn, &op->o_bd->be_nsuffix[0] ) ) {
+                               break;
+                       }
+
+                       op->o_req_dn = pndn;
+                       op->o_req_ndn = pndn;
+
+                       entry = (Entry *)get_entry( op, &ni->li_base_path );
+               }
+
+               ldap_pvt_thread_mutex_unlock( &ni->li_mutex );
+
+               op->o_req_dn = odn;
+               op->o_req_ndn = ondn;
+
+               rc = LDAP_SUCCESS;
+               rs->sr_matched = NULL;
+               if ( entry != NULL ) {
+                       Debug( LDAP_DEBUG_TRACE,
+                               "ldif_back_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
+                               (long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
+
+                       if ( is_entry_referral( entry ) ) {
+                               rc = LDAP_OTHER;
+                               rs->sr_ref = get_entry_referrals( op, entry );
+                               if ( rs->sr_ref ) {
+                                       rs->sr_matched = ber_strdup_x(
+                                       entry->e_name.bv_val, op->o_tmpmemctx );
+                               }
+                       }
+
+                       entry_free(entry);
+
+               } else if ( default_referral != NULL ) {
+                       rc = LDAP_OTHER;
+                       rs->sr_ref = referral_rewrite( default_referral,
+                               NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
+               }
+
+               if ( rs->sr_ref != NULL ) {
+                       /* send referrals */
+                       rc = rs->sr_err = LDAP_REFERRAL;
+                       send_ldap_result( op, rs );
+                       ber_bvarray_free( rs->sr_ref );
+                       rs->sr_ref = NULL;
+
+               } else if ( rc != LDAP_SUCCESS ) {
+                       rs->sr_err = rc;
+                       rs->sr_text = rs->sr_matched ? "bad referral object" : NULL;
+                       send_ldap_result( op, rs );
+               }
+
+               if ( rs->sr_matched ) {
+                       op->o_tmpfree( (char *)rs->sr_matched, op->o_tmpmemctx );
+                       rs->sr_matched = NULL;
+               }
+
+               return rc;
        }
+
+       ldap_pvt_thread_mutex_unlock( &ni->li_mutex );
+
+       if ( is_entry_referral( entry ) ) {
+               /* entry is a referral */
+               BerVarray refs = get_entry_referrals( op, entry );
+               rs->sr_ref = referral_rewrite(
+                       refs, &entry->e_name, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
+
+               Debug( LDAP_DEBUG_TRACE,
+                       "ldif_back_referrals: op=%ld target=\"%s\" matched=\"%s\"\n",
+                       (long) op->o_tag, op->o_req_dn.bv_val, entry->e_name.bv_val );
+
+               rs->sr_matched = entry->e_name.bv_val;
+               if ( rs->sr_ref != NULL ) {
+                       rc = rs->sr_err = LDAP_REFERRAL;
+                       send_ldap_result( op, rs );
+                       ber_bvarray_free( rs->sr_ref );
+                       rs->sr_ref = NULL;
+
+               } else {
+                       send_ldap_error( op, rs, LDAP_OTHER, "bad referral object" );
+                       rc = rs->sr_err;
+               }
+
+               rs->sr_matched = NULL;
+               ber_bvarray_free( refs );
+
+               entry_free( entry );
+       }
+
        return rc;
 }
 
@@ -552,19 +690,11 @@ ldif_back_bind( Operation *op, SlapReply *rs )
        /* no object is found for them */
        if(entry == NULL) {
                if(be_isroot_pw(op)) {
-                       return_val = LDAP_SUCCESS;
-                       goto return_result;
-               }
-               else if(be_root_dn(op->o_bd)) {
-                       return_val = LDAP_INVALID_CREDENTIALS;
-                       rs->sr_err = LDAP_INVALID_CREDENTIALS;
-                       goto return_result;
-               }
-               else {
-                       rs->sr_err = LDAP_NO_SUCH_OBJECT;
-                       return_val = 1;
-                       goto return_result;
+                       rs->sr_err = return_val = LDAP_SUCCESS;
+               } else {
+                       rs->sr_err = return_val = LDAP_INVALID_CREDENTIALS;
                }
+               goto return_result;
        }
 
        /* they don't have userpassword */
@@ -603,11 +733,34 @@ static int ldif_back_search(Operation *op, SlapReply *rs)
        Entry ** entries = NULL;
 
        ldap_pvt_thread_mutex_lock(&ni->li_mutex);
-       entries = (Entry **) enum_tree(op->o_bd, &op->o_req_dn, &op->o_req_ndn, &numentries, op->ors_scope);
-
-       if(entries != NULL) {
-               for(i=0;i<numentries;i++) {
-                       if(test_filter(op, entries[i], op->ors_filter) == LDAP_COMPARE_TRUE) {
+       rs->sr_err = enum_tree(op->o_bd, &op->o_req_dn, &op->o_req_ndn, &numentries, &entries, op->ors_scope);
+       if ( rs->sr_err == LDAP_SUCCESS ) {
+               for ( i = 0; i < numentries; i++ ) {
+
+
+                       /*
+                        * if it's a referral, add it to the list of referrals. only do
+                        * this for non-base searches, and don't check the filter
+                        * explicitly here since it's only a candidate anyway.
+                        */
+                       if ( !get_manageDSAit( op )
+                                       && op->oq_search.rs_scope != LDAP_SCOPE_BASE
+                                       && is_entry_referral( entries[i] ) )
+                       {
+                               BerVarray erefs = get_entry_referrals( op, entries[i] );
+                               rs->sr_ref = referral_rewrite( erefs,
+                                               &entries[i]->e_name, NULL,
+                                               op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
+                                                       ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
+
+                               send_search_reference( op, rs );
+
+                               ber_bvarray_free( rs->sr_ref );
+                               ber_bvarray_free( erefs );
+                               rs->sr_ref = NULL;
+
+                       } else if ( test_filter( op, entries[i], op->ors_filter ) == LDAP_COMPARE_TRUE )
+                       {
                                rs->sr_entry = entries[i];
                                rs->sr_attrs = op->ors_attrs;
                                rs->sr_flags = REP_ENTRY_MODIFIABLE;
@@ -615,18 +768,12 @@ static int ldif_back_search(Operation *op, SlapReply *rs)
                        }
                        entry_free(entries[i]);
                }
-               SLAP_FREE(entries);
-               rs->sr_err = LDAP_SUCCESS;
-               ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
-               send_ldap_result(op, rs);
-       }
-       else {
-               rs->sr_err = LDAP_BUSY;
-               ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
-               send_ldap_result(op, rs);
        }
+       SLAP_FREE(entries);
+       ldap_pvt_thread_mutex_unlock(&ni->li_mutex);
+       send_ldap_result(op, rs);
 
-       return 0;
+       return rs->sr_err;
 }
 
 static int ldif_back_add(Operation *op, SlapReply *rs) {
@@ -637,10 +784,9 @@ static int ldif_back_add(Operation *op, SlapReply *rs) {
        struct stat stats;
        int statres;
        char textbuf[SLAP_TEXT_BUFLEN];
-       size_t textlen = sizeof textbuf;
 
        rs->sr_err = entry_schema_check(op->o_bd, e,
-                                 NULL, &rs->sr_text, textbuf, textlen);
+                                 NULL, &rs->sr_text, textbuf, sizeof( textbuf ) );
        if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
                                
        ldap_pvt_thread_mutex_lock(&ni->li_mutex);
@@ -814,9 +960,8 @@ static int move_entry(Entry * entry, struct berval * ndn,
 
 static int ldif_back_modrdn(Operation *op, SlapReply *rs) {
        struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private;
-       struct berval new_dn = {0, NULL}, new_ndn = {0, NULL};
-       struct berval * new_parent_dn = NULL;
-       struct berval p_dn, bv = {0, NULL};
+       struct berval new_dn = BER_BVNULL, new_ndn = BER_BVNULL;
+       struct berval p_dn, bv = BER_BVNULL;
        Entry * entry = NULL;
        LDAPRDN new_rdn = NULL;
        LDAPRDN old_rdn = NULL;
@@ -933,25 +1078,30 @@ static int ldif_tool_entry_close(BackendDB * be) {
        return 0;
 }
 
-static ID ldif_tool_entry_first(BackendDB *be) {
+static ID
+ldif_tool_entry_first(BackendDB *be)
+{
        struct ldif_info *ni = (struct ldif_info *) be->be_private;
        ID id = 1; /* first entry in the array of entries shifted by one */
 
        ni->tool_current = 1;
        if(ni->tool_entries == NULL || ni->tool_put_entry_flag) {
-               ni->tool_entries = (Entry **) enum_tree(be, be->be_suffix,
-                       be->be_nsuffix, &ni->tool_numentries, LDAP_SCOPE_SUBTREE);
+               (void)enum_tree(be, be->be_suffix, be->be_nsuffix,
+                       &ni->tool_numentries, &ni->tool_entries,
+                       LDAP_SCOPE_SUBTREE);
                ni->tool_put_entry_flag = 0;
        }
        return id;
 }
 
-static ID ldif_tool_entry_next(BackendDB *be) {
+static ID ldif_tool_entry_next(BackendDB *be)
+{
        struct ldif_info *ni = (struct ldif_info *) be->be_private;
        ni->tool_current += 1;
        if(ni->tool_put_entry_flag) {
-               ni->tool_entries = (Entry **) enum_tree(be, be->be_suffix,
-                       be->be_nsuffix, &ni->tool_numentries, LDAP_SCOPE_SUBTREE);
+                (void)enum_tree(be, be->be_suffix, be->be_nsuffix,
+                       &ni->tool_numentries, &ni->tool_entries,
+                       LDAP_SCOPE_SUBTREE);
                ni->tool_put_entry_flag = 0;
        }
        if(ni->tool_current > ni->tool_numentries)
@@ -975,13 +1125,10 @@ static Entry * ldif_tool_entry_get(BackendDB * be, ID id) {
 
 static ID ldif_tool_entry_put(BackendDB * be, Entry * e, struct berval *text) {
        struct ldif_info *ni = (struct ldif_info *) be->be_private;
-       Attribute *save_attrs;
        struct berval dn = e->e_nname;
        struct berval leaf_path = BER_BVNULL;
        struct stat stats;
        int statres;
-       char textbuf[SLAP_TEXT_BUFLEN];
-       size_t textlen = sizeof textbuf;
        int res = LDAP_SUCCESS;
 
        dn2path(&dn, &be->be_nsuffix[0], &ni->li_base_path, &leaf_path);
@@ -1069,8 +1216,17 @@ ldif_back_initialize(
                           BackendInfo  *bi
                           )
 {
+       static char *controls[] = {
+               LDAP_CONTROL_MANAGEDSAIT,
+               NULL
+       };
        int rc;
 
+       bi->bi_flags |=
+               SLAP_BFLAG_REFERRALS;
+
+       bi->bi_controls = controls;
+
        bi->bi_cf_table = ldifcfg;
 
        bi->bi_open = 0;
@@ -1096,7 +1252,7 @@ ldif_back_initialize(
 
        bi->bi_extended = 0;
 
-       bi->bi_chk_referrals = 0;
+       bi->bi_chk_referrals = ldif_back_referrals;
 
        bi->bi_connection_init = 0;
        bi->bi_connection_destroy = 0;