summaryrefslogtreecommitdiffstats
path: root/ldap/servers/slapd/match.c
blob: 91fa0a8f304c856068e199256a7b563ef7c66254 (plain)
1
2
3
4
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/** BEGIN COPYRIGHT BLOCK
 * This Program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; version 2 of the License.
 * 
 * This Program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA.
 * 
 * In addition, as a special exception, Red Hat, Inc. gives You the additional
 * right to link the code of this Program with code not covered under the GNU
 * General Public License ("Non-GPL Code") and to distribute linked combinations
 * including the two, subject to the limitations in this paragraph. Non-GPL Code
 * permitted under this exception must only link to the code of this Program
 * through those well defined interfaces identified in the file named EXCEPTION
 * found in the source code files (the "Approved Interfaces"). The files of
 * Non-GPL Code may instantiate templates or use macros or inline functions from
 * the Approved Interfaces without causing the resulting work to be covered by
 * the GNU General Public License. Only Red Hat, Inc. may make changes or
 * additions to the list of Approved Interfaces. You must obey the GNU General
 * Public License in all respects for all of the Program code and other code used
 * in conjunction with the Program except the Non-GPL Code covered by this
 * exception. If you modify this file, you may extend this exception to your
 * version of the file, but you are not obligated to do so. If you do not wish to
 * provide this exception without modification, you must delete this exception
 * statement from your version and license this file solely under the GPL without
 * exception. 
 * 
 * 
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

/*
 * match.c
 *
 * routines to "register" matching rules with the server
 *
 *
 * 
 *
 */

#include "slap.h"


struct matchingRuleList *g_get_global_mrl(void);
void g_set_global_mrl(struct matchingRuleList *newglobalmrl);
int slapi_matchingrule_register(Slapi_MatchingRuleEntry *mrule);
int slapi_matchingrule_unregister(char *oid);
Slapi_MatchingRuleEntry *slapi_matchingrule_new(void);
void slapi_matchingrule_free(Slapi_MatchingRuleEntry **mrEntry,
			     int freeMembers);
int slapi_matchingrule_get(Slapi_MatchingRuleEntry *mr, int arg, void *value);
int slapi_matchingrule_set(Slapi_MatchingRuleEntry *mr, int arg, void *value);


static int _mr_alloc_new(struct matchingRuleList **mrl);

static struct matchingRuleList *global_mrl=NULL;

struct matchingRuleList* 
g_get_global_mrl(void)
{
    return global_mrl;
}

void 
g_set_global_mrl(struct matchingRuleList *newglobalmrl)
{
    global_mrl = newglobalmrl;
}

int
slapi_matchingrule_set(Slapi_MatchingRuleEntry *mr, int arg, void *value)
{
    if(NULL == mr) {
	return(-1);
    }
    switch(arg) {
    case SLAPI_MATCHINGRULE_NAME:
	{
	    mr->mr_name = (char *)value;
	    break;
	}
    case SLAPI_MATCHINGRULE_OID:
	{
	    mr->mr_oid = (char *)value;
	    break;
	}
    case SLAPI_MATCHINGRULE_DESC:
	{
	    mr->mr_desc = (char *)value;
	    break;
	}
    case SLAPI_MATCHINGRULE_SYNTAX:
	{
	    mr->mr_syntax = (char *)value;
	    break;
	}
    case SLAPI_MATCHINGRULE_OBSOLETE:
	{
	    mr->mr_obsolete = *((int *)value);
	    break;
	}
    default:
	{
	    break;
	}
    }
    return(0);
}

int
slapi_matchingrule_get(Slapi_MatchingRuleEntry *mr, int arg, void *value)
{
    if((NULL == mr) || (NULL == value)) {
	return(-1);
    }
    switch(arg) {
    case SLAPI_MATCHINGRULE_NAME:
	{
	    (*(char **)value) = mr->mr_name;
	    break;
	}
    case SLAPI_MATCHINGRULE_OID:
	{
	    (*(char **)value) = mr->mr_oid;
	    break;
	}
    case SLAPI_MATCHINGRULE_DESC:
	{
	    (*(char **)value) = mr->mr_desc;
	    break;
	}
    case SLAPI_MATCHINGRULE_SYNTAX:
	{
	    (*(char **)value) = mr->mr_syntax;
	    break;
	}
    case SLAPI_MATCHINGRULE_OBSOLETE:
	{
	    (*(int *)value) = mr->mr_obsolete;
	    break;
	}
    default:
	{
	    return(-1);
	}
    }
    return(0);
}

Slapi_MatchingRuleEntry *
slapi_matchingrule_new(void)
{
    Slapi_MatchingRuleEntry *mrEntry=NULL;
    mrEntry = (Slapi_MatchingRuleEntry *)
	slapi_ch_calloc(1, sizeof(Slapi_MatchingRuleEntry));
    return(mrEntry);
}

void
slapi_matchingrule_free(Slapi_MatchingRuleEntry **mrEntry,
			int freeMembers)
{
    if((NULL == mrEntry) || (NULL == *mrEntry)) {
	return;
    }
    if(freeMembers) {
	slapi_ch_free((void **)&((*mrEntry)->mr_name));
	slapi_ch_free((void **)&((*mrEntry)->mr_oid));
	slapi_ch_free((void **)&((*mrEntry)->mr_desc));
	slapi_ch_free((void **)&((*mrEntry)->mr_syntax));
	slapi_ch_free((void **)&((*mrEntry)->mr_oidalias));
	slapi_ch_array_free((*mrEntry)->mr_compat_syntax);
    }
    slapi_ch_free((void **)mrEntry);
    return;
}

static int
_mr_alloc_new(struct matchingRuleList **mrl)
{
    if(!mrl) {
        return(-1);
    }
    *mrl = NULL;
    *mrl = (struct matchingRuleList *)
        slapi_ch_calloc(1, sizeof(struct matchingRuleList));
 
 
    (*mrl)->mr_entry = (Slapi_MatchingRuleEntry *)
        slapi_ch_calloc(1, sizeof(Slapi_MatchingRuleEntry));
    return(0);
}

#if 0
static int
_mr_free(struct matchingRuleList **mrl /*, int freeEntry */)
{
    slapi_ch_free((void **)mrl);
    return(0);
}
#endif

int slapi_matchingrule_register(Slapi_MatchingRuleEntry *mrule)
{
    struct matchingRuleList *mrl=NULL;
    struct matchingRuleList *newmrl=NULL;
    
    if(NULL == mrule) {
	return(-1);
    }
    if(_mr_alloc_new(&newmrl) != 0) {
        return(-1);
    }
    if(NULL != mrule->mr_name) {
	newmrl->mr_entry->mr_name =
	    slapi_ch_strdup((char *) mrule->mr_name);
    }
    if(NULL != mrule->mr_oid) {
	newmrl->mr_entry->mr_oid =
	    slapi_ch_strdup((char *) mrule->mr_oid);
    }
    if(NULL != mrule->mr_oidalias) {
	newmrl->mr_entry->mr_oidalias =
	    slapi_ch_strdup((char *) mrule->mr_oidalias);
    }
    if(NULL != mrule->mr_desc) {
	newmrl->mr_entry->mr_desc = 
	    slapi_ch_strdup((char *) mrule->mr_desc);
    }
    if(NULL != mrule->mr_syntax) {
	newmrl->mr_entry->mr_syntax = 
	    slapi_ch_strdup((char *) mrule->mr_syntax);
    }
    newmrl->mr_entry->mr_obsolete = mrule->mr_obsolete;
    newmrl->mr_entry->mr_compat_syntax = charray_dup(mrule->mr_compat_syntax);

    for(mrl = g_get_global_mrl();
        ((NULL != mrl) && (NULL != mrl->mrl_next));
        mrl = mrl->mrl_next);

    if(NULL == mrl) {
        g_set_global_mrl(newmrl);
        mrl = newmrl;
    }
    mrl->mrl_next = newmrl;
    newmrl->mrl_next = NULL;
    return(LDAP_SUCCESS);
}

int slapi_matchingrule_unregister(char *oid)
{
    /* 
     * Currently, not implemented.
     * For now, the matching rules are read at startup and cannot be modified.
     * If and when, we do allow dynamic modifications, this routine will
     * have to do some work.
     */
    return(0);
}

/*
  See if a matching rule for this name or OID
  is registered and is an ORDERING matching rule that applies
  to the given syntax.
*/
int slapi_matchingrule_is_ordering(const char *oid_or_name, const char *syntax_oid)
{
    struct matchingRuleList *mrl=NULL;

    if (slapi_matchingrule_is_compat(oid_or_name, syntax_oid)) {
        for (mrl = g_get_global_mrl(); mrl != NULL; mrl = mrl->mrl_next) {
            if (mrl->mr_entry->mr_name && !strcasecmp(oid_or_name, mrl->mr_entry->mr_name)) {
                return (mrl->mr_entry->mr_name &&
                        PL_strcasestr(mrl->mr_entry->mr_name, "ordering"));
            }
            if (mrl->mr_entry->mr_oid && !strcmp(oid_or_name, mrl->mr_entry->mr_oid)) {
                return (mrl->mr_entry->mr_name &&
                        PL_strcasestr(mrl->mr_entry->mr_name, "ordering"));
            }
        }
    }

    return 0;
}

/*
  See if a matching rule for this name or OID
  is compatible with the given syntax.
*/
int slapi_matchingrule_is_compat(const char *mr_oid_or_name, const char *syntax_oid)
{
    struct matchingRuleList *mrl=NULL;
    int found = 0;

    for (mrl = g_get_global_mrl(); mrl != NULL; mrl = mrl->mrl_next) {
        if (mrl->mr_entry->mr_name && !strcasecmp(mr_oid_or_name, mrl->mr_entry->mr_name)) {
            found = 1;
            break;
        }
        if (mrl->mr_entry->mr_oid && !strcmp(mr_oid_or_name, mrl->mr_entry->mr_oid)) {
            found = 1;
            break;
        }
    }

    if (found && mrl) {
        char **mr_syntax;
        if (!strcmp(mrl->mr_entry->mr_syntax, syntax_oid)) {
            return 1;
        }
        for (mr_syntax = mrl->mr_entry->mr_compat_syntax;
             mr_syntax && *mr_syntax;
             mr_syntax++) {
            if (!strcmp(*mr_syntax, syntax_oid)) {
                return 1;
            }
        }
    }
            

    return 0;
}