1/*	$NetBSD: dn2entry.c,v 1.3 2021/08/14 16:15:00 christos Exp $	*/
2
3/* dn2entry.c - routines to deal with the dn2id / id2entry glue */
4/* $OpenLDAP$ */
5/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6 *
7 * Copyright 2000-2021 The OpenLDAP Foundation.
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
12 * Public License.
13 *
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
17 */
18
19#include <sys/cdefs.h>
20__RCSID("$NetBSD: dn2entry.c,v 1.3 2021/08/14 16:15:00 christos Exp $");
21
22#include "portable.h"
23
24#include <stdio.h>
25#include <ac/string.h>
26
27#include "back-mdb.h"
28
29/*
30 * dn2entry - look up dn in the cache/indexes and return the corresponding
31 * entry. If the requested DN is not found and matched is TRUE, return info
32 * for the closest ancestor of the DN. Otherwise e is NULL.
33 */
34
35int
36mdb_dn2entry(
37	Operation *op,
38	MDB_txn *tid,
39	MDB_cursor *m2,
40	struct berval *dn,
41	Entry **e,
42	ID *nsubs,
43	int matched )
44{
45	struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
46	int rc, rc2;
47	ID id = NOID;
48	struct berval mbv, nmbv;
49	MDB_cursor *mc;
50
51	Debug(LDAP_DEBUG_TRACE, "mdb_dn2entry(\"%s\")\n",
52		dn->bv_val ? dn->bv_val : "" );
53
54	*e = NULL;
55
56	rc = mdb_dn2id( op, tid, m2, dn, &id, nsubs, &mbv, &nmbv );
57	if ( rc ) {
58		if ( matched ) {
59			rc2 = mdb_cursor_open( tid, mdb->mi_id2entry, &mc );
60			if ( rc2 == MDB_SUCCESS ) {
61				rc2 = mdb_id2entry( op, mc, id, e );
62				mdb_cursor_close( mc );
63			}
64		}
65
66	} else {
67		rc = mdb_cursor_open( tid, mdb->mi_id2entry, &mc );
68		if ( rc == MDB_SUCCESS ) {
69			rc = mdb_id2entry( op, mc, id, e );
70			mdb_cursor_close(mc);
71		}
72	}
73	if ( *e ) {
74		(*e)->e_name = mbv;
75		if ( rc == MDB_SUCCESS )
76			ber_dupbv_x( &(*e)->e_nname, dn, op->o_tmpmemctx );
77		else
78			ber_dupbv_x( &(*e)->e_nname, &nmbv, op->o_tmpmemctx );
79	} else {
80		op->o_tmpfree( mbv.bv_val, op->o_tmpmemctx );
81	}
82
83	return rc;
84}
85