mr.c 5.55 KB
Newer Older
1
2
3
/* mr.c - routines to manage matching rule definitions */
/* $OpenLDAP$ */
/*
Kurt Zeilenga's avatar
Kurt Zeilenga committed
4
 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
 */

#include "portable.h"

#include <stdio.h>

#include <ac/ctype.h>
#include <ac/string.h>
#include <ac/socket.h>

#include "slap.h"
#include "ldap_pvt.h"


struct mindexrec {
	char		*mir_name;
	MatchingRule	*mir_mr;
};

static Avlnode	*mr_index = NULL;
static MatchingRule *mr_list = NULL;

static int
mr_index_cmp(
    struct mindexrec	*mir1,
    struct mindexrec	*mir2
)
{
	return (strcmp( mir1->mir_name, mir2->mir_name ));
}

static int
mr_index_name_cmp(
Gary Williams's avatar
Gary Williams committed
39
    char		*name,
40
41
42
43
44
45
46
47
48
49
50
51
    struct mindexrec	*mir
)
{
	return (strcmp( name, mir->mir_name ));
}

MatchingRule *
mr_find( const char *mrname )
{
	struct mindexrec	*mir = NULL;

	if ( (mir = (struct mindexrec *) avl_find( mr_index, mrname,
Gary Williams's avatar
Gary Williams committed
52
	    (AVL_CMP) mr_index_name_cmp )) != NULL ) {
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
		return( mir->mir_mr );
	}
	return( NULL );
}

static int
mr_insert(
    MatchingRule	*smr,
    const char		**err
)
{
	MatchingRule		**mrp;
	struct mindexrec	*mir;
	char			**names;

	mrp = &mr_list;
	while ( *mrp != NULL ) {
		mrp = &(*mrp)->smr_next;
	}
	*mrp = smr;

	if ( smr->smr_oid ) {
		mir = (struct mindexrec *)
			ch_calloc( 1, sizeof(struct mindexrec) );
		mir->mir_name = smr->smr_oid;
		mir->mir_mr = smr;
		if ( avl_insert( &mr_index, (caddr_t) mir,
				 (AVL_CMP) mr_index_cmp,
				 (AVL_DUP) avl_dup_error ) ) {
			*err = smr->smr_oid;
			ldap_memfree(mir);
			return SLAP_SCHERR_DUP_RULE;
		}
		/* FIX: temporal consistency check */
		mr_find(mir->mir_name);
	}
	if ( (names = smr->smr_names) ) {
		while ( *names ) {
			mir = (struct mindexrec *)
				ch_calloc( 1, sizeof(struct mindexrec) );
			mir->mir_name = ch_strdup(*names);
			mir->mir_mr = smr;
			if ( avl_insert( &mr_index, (caddr_t) mir,
					 (AVL_CMP) mr_index_cmp,
					 (AVL_DUP) avl_dup_error ) ) {
				*err = *names;
				ldap_memfree(mir);
				return SLAP_SCHERR_DUP_RULE;
			}
			/* FIX: temporal consistency check */
			mr_find(mir->mir_name);
			names++;
		}
	}
	return 0;
}

int
mr_add(
112
    LDAPMatchingRule		*mr,
113
	unsigned usage,
114
115
116
117
118
	slap_mr_convert_func *convert,
	slap_mr_normalize_func *normalize,
    slap_mr_match_func	*match,
	slap_mr_indexer_func *indexer,
    slap_mr_filter_func	*filter,
119
	MatchingRule	*amr,
120
121
122
123
124
125
126
127
    const char		**err
)
{
	MatchingRule	*smr;
	Syntax		*syn;
	int		code;

	smr = (MatchingRule *) ch_calloc( 1, sizeof(MatchingRule) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
128
	AC_MEMCPY( &smr->smr_mrule, mr, sizeof(LDAPMatchingRule));
129

130
	smr->smr_usage = usage;
131
132
133
134
135
	smr->smr_convert = convert;
	smr->smr_normalize = normalize;
	smr->smr_match = match;
	smr->smr_indexer = indexer;
	smr->smr_filter = filter;
136
	smr->smr_associated = amr;
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156

	if ( smr->smr_syntax_oid ) {
		if ( (syn = syn_find(smr->smr_syntax_oid)) ) {
			smr->smr_syntax = syn;
		} else {
			*err = smr->smr_syntax_oid;
			return SLAP_SCHERR_SYN_NOT_FOUND;
		}
	} else {
		*err = "";
		return SLAP_SCHERR_MR_INCOMPLETE;
	}
	code = mr_insert(smr,err);
	return code;
}


int
register_matching_rule(
	char * desc,
157
	unsigned usage,
158
159
160
161
	slap_mr_convert_func *convert,
	slap_mr_normalize_func *normalize,
	slap_mr_match_func *match,
	slap_mr_indexer_func *indexer,
162
163
	slap_mr_filter_func *filter,
	const char* associated )
164
{
165
	LDAPMatchingRule *mr;
166
	MatchingRule *amr = NULL;
167
168
169
	int		code;
	const char	*err;

170
	if( usage == SLAP_MR_NONE ) {
171
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
172
173
		LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
			   "register_matching_rule: %s not usable\n", desc ));
174
#else
175
176
		Debug( LDAP_DEBUG_ANY, "register_matching_rule: not usable %s\n",
		    desc, 0, 0 );
177
178
#endif

179
180
181
		return -1;
	}

182
183
184
	if( associated != NULL ) {
		amr = mr_find( associated );

185
186
187
188
#if 0
		/* ignore for now */

		if( amr == NULL ) {
189
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
190
191
192
			LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
				   "register_matching_rule: could not locate associated matching rule %s for %s\n",
				   associated, desc ));
193
#else
194
195
196
			Debug( LDAP_DEBUG_ANY, "register_matching_rule: could not locate "
				"associated matching rule %s for %s\n",
				associated, desc, 0 );
197
198
#endif

199
200
201
			return -1;
		}
#endif
202
203
204

	}

205
	mr = ldap_str2matchingrule( desc, &code, &err, LDAP_SCHEMA_ALLOW_ALL);
206
	if ( !mr ) {
207
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
208
209
210
		LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
			   "register_matching_rule: %s before %s in %s.\n",
			   ldap_scherr2str(code), err, desc ));
211
#else
212
213
		Debug( LDAP_DEBUG_ANY, "Error in register_matching_rule: %s before %s in %s\n",
		    ldap_scherr2str(code), err, desc );
214
215
#endif

216
217
218
		return( -1 );
	}

219
	code = mr_add( mr, usage,
220
		convert, normalize, match, indexer, filter, amr,
221
222
		&err );

223
224
	ldap_memfree( mr );

225
	if ( code ) {
226
#ifdef NEW_LOGGING
Gary Williams's avatar
Gary Williams committed
227
228
229
		LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
			   "register_matching_rule: %s for %s in %s.\n",
			   scherr2str(code), err, desc ));
230
#else
231
		Debug( LDAP_DEBUG_ANY, "Error in register_matching_rule: %s for %s in %s\n",
232
		    scherr2str(code), err, desc );
233
234
#endif

235
236
		return( -1 );
	}
237

238
239
240
241
242
243
244
245
246
247
248
249
	return( 0 );
}


#if defined( SLAPD_SCHEMA_DN )

int mr_schema_info( Entry *e )
{
	struct berval	val;
	struct berval	*vals[2];
	MatchingRule	*mr;

250
251
	AttributeDescription *ad_matchingRules = slap_schema.si_ad_matchingRules;

252
253
254
255
	vals[0] = &val;
	vals[1] = NULL;

	for ( mr = mr_list; mr; mr = mr->smr_next ) {
256
257
258
259
260
		if ( ! mr->smr_match ) {
			/* skip rules without matching functions */
			continue;
		}

261
		val.bv_val = ldap_matchingrule2str( &mr->smr_mrule );
262
263

		if ( val.bv_val == NULL ) {
264
265
			return -1;
		}
266
267

		val.bv_len = strlen( val.bv_val );
268
#if 0
269
270
		Debug( LDAP_DEBUG_TRACE, "Merging mr [%ld] %s\n",
	       (long) val.bv_len, val.bv_val, 0 );
271
#endif
272
273
		attr_merge( e, ad_matchingRules, vals );
		ldap_memfree( val.bv_val );
274
275
276
277
278
	}
	return 0;
}

#endif