1 /* OpenLDAP WiredTiger backend */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2002-2021 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 developed by HAMANO Tsukasa <hamano@osstech.co.jp>
18  * based on back-bdb for inclusion in OpenLDAP Software.
19  * WiredTiger is a product of MongoDB Inc.
20  */
21 
22 #include "portable.h"
23 
24 #include <stdio.h>
25 #include <ac/string.h>
26 #include "back-wt.h"
27 #include "slap-config.h"
28 
29 /*
30  * dn2entry - look up dn in the db and return the corresponding entry.
31  * No longer return closest ancestor, see wt_dn2pentry().
32  */
wt_dn2entry(BackendDB * be,wt_ctx * wc,struct berval * ndn,Entry ** ep)33 int wt_dn2entry( BackendDB *be,
34 				 wt_ctx *wc,
35 				 struct berval *ndn,
36 				 Entry **ep ){
37 	uint64_t id;
38 	WT_ITEM item;
39 	EntryHeader eh;
40 	int rc;
41 	int eoff;
42 	Entry *e = NULL;
43 	WT_SESSION *session = wc->session;
44 	WT_CURSOR *cursor = wc->dn2entry;
45 
46 	if( ndn->bv_len == 0 ){
47 		/* empty dn */
48 		e = entry_alloc();
49 		ber_dupbv(&e->e_nname, ndn);
50 		*ep = e;
51 		return LDAP_SUCCESS;
52 	}
53 
54 	if(!cursor){
55 		rc = session->open_cursor(session,
56 								  WT_INDEX_DN"(id, entry)",
57 								  NULL, NULL, &cursor);
58 		if ( rc ) {
59 			Debug( LDAP_DEBUG_ANY,
60 				   "wt_dn2entry: open_cursor failed: %s (%d)\n",
61 				   wiredtiger_strerror(rc), rc );
62 			goto done;
63 		}
64 		wc->dn2entry = cursor;
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 			   "wt_dn2entry: search failed: %s (%d)\n",
77 			   wiredtiger_strerror(rc), rc );
78 		goto done;
79 	}
80 	cursor->get_value(cursor, &id, &item);
81 	rc = wt_entry_header( &item,  &eh );
82 
83 	eoff = eh.data - (char *)item.data;
84 	eh.bv.bv_len = eh.nvals * sizeof( struct berval ) + item.size;
85 	eh.bv.bv_val = ch_malloc( eh.bv.bv_len );
86 	memset(eh.bv.bv_val, 0xff, eh.bv.bv_len);
87 	eh.data = eh.bv.bv_val + eh.nvals * sizeof( struct berval );
88 	memcpy(eh.data, item.data, item.size);
89 	eh.data += eoff;
90 	rc = entry_decode( &eh, &e );
91 	if ( rc ) {
92 		Debug( LDAP_DEBUG_ANY,
93 			   "wt_dn2entry: entry decode error: %d\n", rc );
94 		goto done;
95 	}
96 
97 	e->e_id = id;
98 	*ep = e;
99 
100 done:
101 
102 #ifdef WT_CURSOR_CACHE
103 	if(cursor){
104 		cursor->reset(cursor);
105 	}
106 #else
107 	if(cursor){
108 		cursor->close(cursor);
109 		wc->dn2entry = NULL;
110 	}
111 #endif
112 	return rc;
113 }
114 
115 /* dn2pentry - return parent entry */
wt_dn2pentry(BackendDB * be,wt_ctx * wc,struct berval * ndn,Entry ** ep)116 int wt_dn2pentry( BackendDB *be,
117 				  wt_ctx *wc,
118 				  struct berval *ndn,
119 				  Entry **ep ){
120 	Entry *e = NULL;
121 	struct berval pdn;
122 	int rc;
123 
124 	if (be_issuffix( be, ndn )) {
125 		*ep = NULL;
126 		return WT_NOTFOUND;
127 	}
128 
129 	dnParent( ndn, &pdn );
130 	rc = wt_dn2entry(be, wc, &pdn, &e);
131 	*ep = e;
132 	return rc;
133 }
134 
135 /* dn2aentry - return ancestor entry */
wt_dn2aentry(BackendDB * be,wt_ctx * wc,struct berval * ndn,Entry ** ep)136 int wt_dn2aentry( BackendDB *be,
137 				  wt_ctx *wc,
138 				  struct berval *ndn,
139 				  Entry **ep ) {
140 	Entry *e = NULL;
141 	struct berval pdn;
142 	int rc;
143 
144 	if (be_issuffix( be, ndn )) {
145 		*ep = NULL;
146 		return 0;
147 	}
148 
149 	dnParent( ndn, &pdn );
150 	rc = wt_dn2entry(be, wc, &pdn, &e);
151 	switch( rc ) {
152 	case 0:
153 		*ep = e;
154 		break;
155 	case WT_NOTFOUND:
156 		rc = wt_dn2aentry(be, wc, &pdn, &e);
157 		if (rc != 0 && rc != WT_NOTFOUND) {
158 			return rc;
159 		}
160 		*ep = e;
161 		break;
162 	default:
163 		Debug( LDAP_DEBUG_ANY,
164 			   "wt_dn2aentry: failed %s (%d)\n",
165 			   wiredtiger_strerror(rc), rc );
166 	}
167 	return rc;
168 }
169 
170 /*
171  * Local variables:
172  * indent-tabs-mode: t
173  * tab-width: 4
174  * c-basic-offset: 4
175  * End:
176  */
177