xref: /netbsd-src/external/bsd/openldap/dist/servers/slapd/back-wt/dn2entry.c (revision 549b59ed3ccf0d36d3097190a0db27b770f3a839)
1 /*	$NetBSD: dn2entry.c,v 1.2 2021/08/14 16:15:02 christos Exp $	*/
2 
3 /* OpenLDAP WiredTiger backend */
4 /* $OpenLDAP$ */
5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6  *
7  * Copyright 2002-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 /* ACKNOWLEDGEMENTS:
19  * This work was developed by HAMANO Tsukasa <hamano@osstech.co.jp>
20  * based on back-bdb for inclusion in OpenLDAP Software.
21  * WiredTiger is a product of MongoDB Inc.
22  */
23 
24 #include <sys/cdefs.h>
25 __RCSID("$NetBSD: dn2entry.c,v 1.2 2021/08/14 16:15:02 christos Exp $");
26 
27 #include "portable.h"
28 
29 #include <stdio.h>
30 #include <ac/string.h>
31 #include "back-wt.h"
32 #include "slap-config.h"
33 
34 /*
35  * dn2entry - look up dn in the db and return the corresponding entry.
36  * No longer return closest ancestor, see wt_dn2pentry().
37  */
wt_dn2entry(BackendDB * be,wt_ctx * wc,struct berval * ndn,Entry ** ep)38 int wt_dn2entry( BackendDB *be,
39 				 wt_ctx *wc,
40 				 struct berval *ndn,
41 				 Entry **ep ){
42 	uint64_t id;
43 	WT_CURSOR *cursor = NULL;
44 	WT_ITEM item;
45 	EntryHeader eh;
46 	int rc;
47 	int eoff;
48 	Entry *e = NULL;
49 	WT_SESSION *session = wc->session;
50 
51 	if( ndn->bv_len == 0 ){
52 		/* parent of root dn */
53 		return WT_NOTFOUND;
54 	}
55 
56 	rc = session->open_cursor(session,
57 							  WT_INDEX_DN"(id, entry)",
58 							  NULL, NULL, &cursor);
59 	if ( rc ) {
60 		Debug( LDAP_DEBUG_ANY,
61 			   LDAP_XSTRING(wt_dn2entry)
62 			   ": open_cursor failed: %s (%d)\n",
63 			   wiredtiger_strerror(rc), rc );
64 		goto done;
65 	}
66 
67 	cursor->set_key(cursor, ndn->bv_val);
68 	rc = cursor->search(cursor);
69 	switch( rc ){
70 	case 0:
71 		break;
72 	case WT_NOTFOUND:
73 		goto done;
74 	default:
75 		Debug( LDAP_DEBUG_ANY,
76 			   LDAP_XSTRING(wt_dn2entry)
77 			   ": search failed: %s (%d)\n",
78 			   wiredtiger_strerror(rc), rc );
79 		goto done;
80 	}
81 	cursor->get_value(cursor, &id, &item);
82 	rc = wt_entry_header( &item,  &eh );
83 
84 	eoff = eh.data - (char *)item.data;
85 	eh.bv.bv_len = eh.nvals * sizeof( struct berval ) + item.size;
86 	eh.bv.bv_val = ch_malloc( eh.bv.bv_len );
87 	memset(eh.bv.bv_val, 0xff, eh.bv.bv_len);
88 	eh.data = eh.bv.bv_val + eh.nvals * sizeof( struct berval );
89 	memcpy(eh.data, item.data, item.size);
90 	eh.data += eoff;
91 	rc = entry_decode( &eh, &e );
92 	if ( rc ) {
93 		Debug( LDAP_DEBUG_ANY,
94 			   LDAP_XSTRING(wt_dn2entry)
95 			   ": entry decode error: %d\n",
96 			   rc );
97 		goto done;
98 	}
99 
100 	e->e_id = id;
101 	*ep = e;
102 
103 done:
104 	if(cursor){
105 		cursor->close(cursor);
106 	}
107 	return rc;
108 }
109 
110 /* dn2pentry - return parent entry */
wt_dn2pentry(BackendDB * be,wt_ctx * wc,struct berval * ndn,Entry ** ep)111 int wt_dn2pentry( BackendDB *be,
112 				  wt_ctx *wc,
113 				  struct berval *ndn,
114 				  Entry **ep ){
115 	Entry *e = NULL;
116 	struct berval pdn;
117 	int rc;
118 
119 	if (be_issuffix( be, ndn )) {
120 		*ep = NULL;
121 		return WT_NOTFOUND;
122 	}
123 
124 	dnParent( ndn, &pdn );
125 	rc = wt_dn2entry(be, wc, &pdn, &e);
126 	*ep = e;
127 	return rc;
128 }
129 
130 /*
131  * Local variables:
132  * indent-tabs-mode: t
133  * tab-width: 4
134  * c-basic-offset: 4
135  * End:
136  */
137