mr.c 6 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-2002 The OpenLDAP Foundation, All Rights Reserved.
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 * 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 {
Howard Chu's avatar
Howard Chu committed
20
	struct berval	mir_name;
21
22
23
24
25
26
27
28
29
30
31
32
	MatchingRule	*mir_mr;
};

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

static int
mr_index_cmp(
    struct mindexrec	*mir1,
    struct mindexrec	*mir2
)
{
Howard Chu's avatar
Howard Chu committed
33
34
35
	int i = mir1->mir_name.bv_len - mir2->mir_name.bv_len;
	if (i) return i;
	return (strcmp( mir1->mir_name.bv_val, mir2->mir_name.bv_val ));
36
37
38
39
}

static int
mr_index_name_cmp(
Howard Chu's avatar
Howard Chu committed
40
    struct berval	*name,
41
42
43
    struct mindexrec	*mir
)
{
Howard Chu's avatar
Howard Chu committed
44
45
46
	int i = name->bv_len - mir->mir_name.bv_len;
	if (i) return i;
	return (strncmp( name->bv_val, mir->mir_name.bv_val, name->bv_len ));
47
48
49
50
}

MatchingRule *
mr_find( const char *mrname )
Howard Chu's avatar
Howard Chu committed
51
52
53
{
	struct berval bv;

Howard Chu's avatar
Howard Chu committed
54
	bv.bv_val = (char *)mrname;
Howard Chu's avatar
Howard Chu committed
55
56
57
58
59
60
	bv.bv_len = strlen( mrname );
	return mr_bvfind( &bv );
}

MatchingRule *
mr_bvfind( struct berval *mrname )
61
62
63
64
{
	struct mindexrec	*mir = NULL;

	if ( (mir = (struct mindexrec *) avl_find( mr_index, mrname,
Gary Williams's avatar
Gary Williams committed
65
	    (AVL_CMP) mr_index_name_cmp )) != NULL ) {
66
67
68
69
70
		return( mir->mir_mr );
	}
	return( NULL );
}

71
72
73
74
75
76
77
78
79
80
81
82
void
mr_destroy( void )
{
	MatchingRule *m, *n;

	avl_free(mr_index, ldap_memfree);
	for (m=mr_list; m; m=n) {
		n = m->smr_next;
		ldap_matchingrule_free((LDAPMatchingRule *)m);
	}
}

83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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) );
Howard Chu's avatar
Howard Chu committed
102
103
		mir->mir_name.bv_val = smr->smr_oid;
		mir->mir_name.bv_len = strlen( smr->smr_oid );
104
105
106
107
108
109
		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);
110
			return SLAP_SCHERR_MR_DUP;
111
112
		}
		/* FIX: temporal consistency check */
Howard Chu's avatar
Howard Chu committed
113
		mr_bvfind(&mir->mir_name);
114
115
116
117
118
	}
	if ( (names = smr->smr_names) ) {
		while ( *names ) {
			mir = (struct mindexrec *)
				ch_calloc( 1, sizeof(struct mindexrec) );
Howard Chu's avatar
Howard Chu committed
119
120
			mir->mir_name.bv_val = *names;
			mir->mir_name.bv_len = strlen( *names );
121
122
123
124
125
126
			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);
127
				return SLAP_SCHERR_MR_DUP;
128
129
			}
			/* FIX: temporal consistency check */
Howard Chu's avatar
Howard Chu committed
130
			mr_bvfind(&mir->mir_name);
131
132
133
134
135
136
137
138
			names++;
		}
	}
	return 0;
}

int
mr_add(
139
    LDAPMatchingRule		*mr,
140
    slap_mrule_defs_rec	*def,
141
	MatchingRule	*amr,
142
143
144
145
146
147
148
149
    const char		**err
)
{
	MatchingRule	*smr;
	Syntax		*syn;
	int		code;

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

152
	smr->smr_oidlen = strlen( mr->mr_oid );
153
154
155
156
157
158
	smr->smr_usage = def->mrd_usage;
	smr->smr_convert = def->mrd_convert;
	smr->smr_normalize = def->mrd_normalize;
	smr->smr_match = def->mrd_match;
	smr->smr_indexer = def->mrd_indexer;
	smr->smr_filter = def->mrd_filter;
159
	smr->smr_associated = amr;
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178

	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(
179
	slap_mrule_defs_rec *def )
180
{
181
	LDAPMatchingRule *mr;
182
	MatchingRule *amr = NULL;
183
184
185
	int		code;
	const char	*err;

186
	if( def->mrd_usage == SLAP_MR_NONE ) {
187
#ifdef NEW_LOGGING
Julius Enarusai's avatar
   
Julius Enarusai committed
188
		LDAP_LOG( OPERATION, ERR, 
189
			"register_matching_rule: %s not usable\n", def->mrd_desc, 0, 0 );
190
#else
191
		Debug( LDAP_DEBUG_ANY, "register_matching_rule: not usable %s\n",
192
		    def->mrd_desc, 0, 0 );
193
194
#endif

195
196
197
		return -1;
	}

198
199
	if( def->mrd_associated != NULL ) {
		amr = mr_find( def->mrd_associated );
200

201
202
203
204
#if 0
		/* ignore for now */

		if( amr == NULL ) {
205
#ifdef NEW_LOGGING
Julius Enarusai's avatar
   
Julius Enarusai committed
206
207
			LDAP_LOG( OPERATION, ERR,
			   "register_matching_rule: could not locate associated "
208
			   "matching rule %s for %s\n",  def->mrd_associated, def->mrd_desc, 0 );
209
#else
210
211
			Debug( LDAP_DEBUG_ANY, "register_matching_rule: could not locate "
				"associated matching rule %s for %s\n",
212
				def->mrd_associated, def->mrd_desc, 0 );
213
214
#endif

215
216
217
			return -1;
		}
#endif
218
219
220

	}

221
	mr = ldap_str2matchingrule( def->mrd_desc, &code, &err, LDAP_SCHEMA_ALLOW_ALL);
222
	if ( !mr ) {
223
#ifdef NEW_LOGGING
Julius Enarusai's avatar
   
Julius Enarusai committed
224
225
		LDAP_LOG( OPERATION, ERR, 
			"register_matching_rule: %s before %s in %s.\n",
226
			ldap_scherr2str(code), err, def->mrd_desc );
227
#else
228
		Debug( LDAP_DEBUG_ANY, "Error in register_matching_rule: %s before %s in %s\n",
229
		    ldap_scherr2str(code), err, def->mrd_desc );
230
231
#endif

232
233
234
		return( -1 );
	}

235
	code = mr_add( mr, def, amr, &err );
236

237
238
	ldap_memfree( mr );

239
	if ( code ) {
240
#ifdef NEW_LOGGING
Julius Enarusai's avatar
   
Julius Enarusai committed
241
242
		LDAP_LOG( OPERATION, ERR, 
			"register_matching_rule: %s for %s in %s.\n",
243
			scherr2str(code), err, def->mrd_desc );
244
#else
245
		Debug( LDAP_DEBUG_ANY, "Error in register_matching_rule: %s for %s in %s\n",
246
		    scherr2str(code), err, def->mrd_desc );
247
248
#endif

249
250
		return( -1 );
	}
251

252
253
254
255
256
257
258
259
	return( 0 );
}


#if defined( SLAPD_SCHEMA_DN )

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

263
264
	AttributeDescription *ad_matchingRules = slap_schema.si_ad_matchingRules;

265
	vals[1].bv_val = NULL;
266
267

	for ( mr = mr_list; mr; mr = mr->smr_next ) {
268
269
270
271
272
		if ( mr->smr_usage & SLAP_MR_HIDE ) {
			/* skip hidden rules */
			continue;
		}

273
274
275
276
277
		if ( ! mr->smr_match ) {
			/* skip rules without matching functions */
			continue;
		}

278
		if ( ldap_matchingrule2bv( &mr->smr_mrule, vals ) == NULL ) {
279
280
			return -1;
		}
281
#if 0
282
		Debug( LDAP_DEBUG_TRACE, "Merging mr [%ld] %s\n",
283
	       (long) vals[0].bv_len, vals[0].bv_val, 0 );
284
#endif
285
		attr_merge( e, ad_matchingRules, vals );
286
		ldap_memfree( vals[0].bv_val );
287
288
289
290
	}
	return 0;
}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
291
292
293
294
295
int mru_schema_info( Entry *e )
{
	return 0;
}

296
#endif