1/*	$NetBSD: compare.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: compare.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
32#include "back-wt.h"
33#include "slap-config.h"
34
35int
36wt_compare( Operation *op, SlapReply *rs )
37{
38    struct wt_info *wi = (struct wt_info *) op->o_bd->be_private;
39	Entry *e = NULL;
40	int manageDSAit = get_manageDSAit( op );
41	int rc;
42	wt_ctx *wc = NULL;
43
44	Debug( LDAP_DEBUG_ARGS, "==> " LDAP_XSTRING(wt_compare) ": %s\n",
45		   op->o_req_dn.bv_val );
46
47	wc = wt_ctx_get(op, wi);
48	if( !wc ){
49		Debug( LDAP_DEBUG_ANY,
50			   LDAP_XSTRING(wt_compare)
51			   ": wt_ctx_get failed\n" );
52		rs->sr_err = LDAP_OTHER;
53		rs->sr_text = "internal error";
54        send_ldap_result( op, rs );
55        return rs->sr_err;
56	}
57
58	rs->sr_err = wt_dn2entry(op->o_bd, wc, &op->o_req_ndn, &e);
59	switch( rs->sr_err ) {
60	case 0:
61	case WT_NOTFOUND:
62		break;
63	default:
64		rs->sr_err = LDAP_OTHER;
65		rs->sr_text = "internal error";
66		goto return_results;
67	}
68
69	if ( rs->sr_err == WT_NOTFOUND ) {
70		if ( e != NULL ) {
71			/* return referral only if "disclose" is granted on the object */
72			if ( ! access_allowed( op, e, slap_schema.si_ad_entry,
73								   NULL, ACL_DISCLOSE, NULL ) )
74			{
75				rs->sr_err = LDAP_NO_SUCH_OBJECT;
76			} else {
77				rs->sr_matched = ch_strdup( e->e_dn );
78				if ( is_entry_referral( e )) {
79					BerVarray ref = get_entry_referrals( op, e );
80					rs->sr_ref = referral_rewrite( ref,
81												   &e->e_name,
82												   &op->o_req_dn,
83												   LDAP_SCOPE_DEFAULT );
84					ber_bvarray_free( ref );
85				} else {
86					rs->sr_ref = NULL;
87				}
88				rs->sr_err = LDAP_REFERRAL;
89			}
90			wt_entry_return( e );
91			e = NULL;
92		} else {
93			rs->sr_ref = referral_rewrite( default_referral,
94										   NULL,
95										   &op->o_req_dn,
96										   LDAP_SCOPE_DEFAULT );
97			rs->sr_err = rs->sr_ref ? LDAP_REFERRAL : LDAP_NO_SUCH_OBJECT;
98		}
99
100		rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
101		send_ldap_result( op, rs );
102		goto done;
103	}
104
105	if (!manageDSAit && is_entry_referral( e ) ) {
106		/* return referral only if "disclose" is granted on the object */
107		if ( !access_allowed( op, e, slap_schema.si_ad_entry,
108							  NULL, ACL_DISCLOSE, NULL ) )
109		{
110			rs->sr_err = LDAP_NO_SUCH_OBJECT;
111		} else {
112			/* entry is a referral, don't allow compare */
113			rs->sr_ref = get_entry_referrals( op, e );
114			rs->sr_err = LDAP_REFERRAL;
115			rs->sr_matched = e->e_name.bv_val;
116		}
117
118		Debug( LDAP_DEBUG_TRACE, "entry is referral\n" );
119
120		send_ldap_result( op, rs );
121
122		ber_bvarray_free( rs->sr_ref );
123		rs->sr_ref = NULL;
124		rs->sr_matched = NULL;
125		goto done;
126	}
127
128	rs->sr_err = slap_compare_entry( op, e, op->orc_ava );
129
130return_results:
131	send_ldap_result( op, rs );
132
133	switch ( rs->sr_err ) {
134	case LDAP_COMPARE_FALSE:
135	case LDAP_COMPARE_TRUE:
136		rs->sr_err = LDAP_SUCCESS;
137		break;
138	}
139
140done:
141	if ( e != NULL ) {
142		wt_entry_return( e );
143	}
144    return rs->sr_err;
145}
146
147/*
148 * Local variables:
149 * indent-tabs-mode: t
150 * tab-width: 4
151 * c-basic-offset: 4
152 * End:
153 */
154