compare.c 2.34 KB
Newer Older
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1
2
3
4
5
6
7
8
9
10
11
12
13
/* compare.c - bdb backend compare routine */
/* $OpenLDAP$ */
/*
 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
 */

#include "portable.h"

#include <stdio.h>
#include <ac/string.h>

#include "back-bdb.h"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
14
#include "external.h"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
15
16
17

int
bdb_compare(
18
19
20
	BackendDB	*be,
	Connection	*conn,
	Operation	*op,
21
22
	struct berval	*dn,
	struct berval	*ndn,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
23
24
25
26
27
28
29
30
31
32
33
34
	AttributeAssertion *ava
)
{
	struct bdb_info *bdb = (struct bdb_info *) be->be_private;
	Entry		*matched;
	Entry		*e;
	Attribute	*a;
	int			rc; 
	const char	*text = NULL;
	int		manageDSAit = get_manageDSAit( op );

	/* get entry */
35
	rc = bdb_dn2entry( be, NULL, ndn, &e, &matched, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
36
37
38

	switch( rc ) {
	case DB_NOTFOUND:
Howard Chu's avatar
Howard Chu committed
39
	case 0:
Kurt Zeilenga's avatar
Kurt Zeilenga committed
40
41
42
43
44
45
46
47
48
		break;
	default:
		rc = LDAP_OTHER;
		text = "internal error";
		goto return_results;
	}

	if ( e == NULL ) {
		char *matched_dn = NULL;
49
		BVarray refs;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
50
51
52
53

		if ( matched != NULL ) {
			matched_dn = ch_strdup( matched->e_dn );
			refs = is_entry_referral( matched )
54
				? get_entry_referrals( be, conn, op, matched )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
55
56
				: NULL;
			bdb_entry_return( be, matched );
57
58
			matched = NULL;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
59
		} else {
60
			refs = referral_rewrite( default_referral,
61
				NULL, dn, LDAP_SCOPE_DEFAULT );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
62
63
64
65
66
		}

		send_ldap_result( conn, op, rc = LDAP_REFERRAL,
			matched_dn, NULL, refs, NULL );

67
		bvarray_free( refs );
68
		free( matched_dn );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
69
70
71
72
73
74

		goto done;
	}

	if (!manageDSAit && is_entry_referral( e ) ) {
		/* entry is a referral, don't allow add */
75
		BVarray refs = get_entry_referrals( be,
76
			conn, op, e );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
77
78

		Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
79
			0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
80
81

		send_ldap_result( conn, op, rc = LDAP_REFERRAL,
82
			e->e_dn, NULL, refs, NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
83

84
		bvarray_free( refs );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
85
86
87
88
		goto done;
	}

	if ( ! access_allowed( be, conn, op, e,
89
		ava->aa_desc, &ava->aa_value, ACL_COMPARE ) )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
90
91
92
93
94
95
96
97
98
99
100
101
102
	{
		rc = LDAP_INSUFFICIENT_ACCESS;
		goto return_results;
	}

	rc = LDAP_NO_SUCH_ATTRIBUTE;

	for(a = attrs_find( e->e_attrs, ava->aa_desc );
		a != NULL;
		a = attrs_find( a->a_next, ava->aa_desc ))
	{
		rc = LDAP_COMPARE_FALSE;

103
		if ( value_find( ava->aa_desc, a->a_vals, &ava->aa_value ) == 0 ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
			rc = LDAP_COMPARE_TRUE;
			break;
		}

	}

	if( rc != LDAP_NO_SUCH_ATTRIBUTE ) {
		rc = LDAP_SUCCESS;
	}


return_results:
	send_ldap_result( conn, op, LDAP_SUCCESS,
		NULL, text, NULL, NULL );

done:
	/* free entry */
121
122
123
	if( e != NULL ) {
		bdb_entry_return( be, e );
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
124
125

	return rc;
126
}