]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-mdb/dn2id.c
Plug memleak
[openldap] / servers / slapd / back-mdb / dn2id.c
index d696830d90d4d13f5c01d80429dd559a6e995813..82ec07b7f1c8b5df96b9529f755655b8d2f9a164 100644 (file)
@@ -310,17 +310,8 @@ mdb_dn2id(
                data.mv_data = d;
                rc = mdb_cursor_get( cursor, &key, &data, MDB_GET_BOTH );
                op->o_tmpfree( d, op->o_tmpmemctx );
-               if ( rc == MDB_NOTFOUND ) {
-                       if ( matched && matched->bv_len ) {
-                               ptr = op->o_tmpalloc( matched->bv_len+1, op->o_tmpmemctx );
-                               strcpy( ptr, matched->bv_val );
-                               matched->bv_val = ptr;
-                       }
-               }
-               if ( rc ) {
-                       mdb_cursor_close( cursor );
+               if ( rc )
                        break;
-               }
                ptr = (char *) data.mv_data + data.mv_size - sizeof(ID);
                memcpy( &nid, ptr, sizeof(ID));
 
@@ -350,6 +341,12 @@ mdb_dn2id(
                }
        }
        *id = nid; 
+       mdb_cursor_close( cursor );
+       if ( matched && matched->bv_len ) {
+               ptr = op->o_tmpalloc( matched->bv_len+1, op->o_tmpmemctx );
+               strcpy( ptr, matched->bv_val );
+               matched->bv_val = ptr;
+       }
 
 done:
        if( rc != 0 ) {
@@ -538,6 +535,7 @@ int
 mdb_id2name(
        Operation *op,
        MDB_txn *txn,
+       MDB_cursor **cursp,
        ID id,
        struct berval *name,
        struct berval *nname )
@@ -553,8 +551,11 @@ mdb_id2name(
 
        key.mv_size = sizeof(ID);
 
-       rc = mdb_cursor_open( txn, dbi, &cursor );
-       if ( rc ) return rc;
+       if ( !*cursp ) {
+               rc = mdb_cursor_open( txn, dbi, cursp );
+               if ( rc ) return rc;
+       }
+       cursor = *cursp;
 
        len = 0;
        nlen = 0;
@@ -593,10 +594,129 @@ mdb_id2name(
                memcpy( nname->bv_val, ndn, nname->bv_len );
                nname->bv_val[nname->bv_len] = '\0';
        }
+       return rc;
+}
+
+/* Find each id in ids that is a child of base and move it to res.
+ */
+int
+mdb_idscope(
+       Operation *op,
+       MDB_txn *txn,
+       ID base,
+       ID *ids,
+       ID *res )
+{
+       struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
+       MDB_dbi dbi = mdb->mi_dn2id;
+       MDB_val         key, data;
+       MDB_cursor      *cursor;
+       ID ida, id, cid, ci0, idc = 0;
+       char    *ptr;
+       int             rc;
+
+       key.mv_size = sizeof(ID);
+
+       MDB_IDL_ZERO( res );
+
+       rc = mdb_cursor_open( txn, dbi, &cursor );
+       if ( rc ) return rc;
+
+       ida = mdb_idl_first( ids, &cid );
+
+       /* Don't bother moving out of ids if it's a range */
+       if (!MDB_IDL_IS_RANGE(ids)) {
+               idc = ids[0];
+               ci0 = cid;
+       }
+
+       while (ida != NOID) {
+               id = ida;
+               while (id) {
+                       key.mv_data = &id;
+                       rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
+                       if ( rc ) {
+                               /* not found, move on to next */
+                               if (idc) {
+                                       if (ci0 != cid)
+                                               ids[ci0] = ids[cid];
+                                       ci0++;
+                               }
+                               break;
+                       }
+                       ptr = data.mv_data;
+                       ptr += data.mv_size - sizeof(ID);
+                       memcpy( &id, ptr, sizeof(ID) );
+                       if ( id == base ) {
+                               res[0]++;
+                               res[res[0]] = ida;
+                               if (idc)
+                                       idc--;
+                               break;
+                       } else {
+                               if (idc) {
+                                       if (ci0 != cid)
+                                               ids[ci0] = ids[cid];
+                                       ci0++;
+                               }
+                       }
+                       if ( op->ors_scope == LDAP_SCOPE_ONELEVEL )
+                               break;
+               }
+               ida = mdb_idl_next( ids, &cid );
+       }
+       if (!MDB_IDL_IS_RANGE( ids ))
+               ids[0] = idc;
+
        mdb_cursor_close( cursor );
        return rc;
 }
 
+/* See if base is a child of any of the scopes
+ */
+int
+mdb_idscopes(
+       Operation *op,
+       MDB_txn *txn,
+       MDB_cursor **cursp,
+       ID base,
+       ID *scopes )
+{
+       struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
+       MDB_dbi dbi = mdb->mi_dn2id;
+       MDB_val         key, data;
+       MDB_cursor      *cursor;
+       ID id;
+       char    *ptr;
+       int             rc;
+       unsigned int x;
+
+       key.mv_size = sizeof(ID);
+
+       if ( !*cursp ) {
+               rc = mdb_cursor_open( txn, dbi, cursp );
+               if ( rc ) return rc;
+       }
+       cursor = *cursp;
+
+       id = base;
+       while (id) {
+               key.mv_data = &id;
+               rc = mdb_cursor_get( cursor, &key, &data, MDB_SET );
+               if ( rc )
+                       break;
+               ptr = data.mv_data;
+               ptr += data.mv_size - sizeof(ID);
+               memcpy( &id, ptr, sizeof(ID) );
+               x = mdb_idl_search( scopes, id );
+               if ( scopes[x] == id )
+                       return MDB_SUCCESS;
+               if ( op->ors_scope == LDAP_SCOPE_ONELEVEL )
+                       break;
+       }
+       return MDB_NOTFOUND;
+}
+
 #if 0
 /* mdb_dn2idl:
  * We can't just use mdb_idl_fetch_key because