~ubuntu-branches/ubuntu/wily/389-ds-base/wily

« back to all changes in this revision

Viewing changes to ldap/servers/plugins/syntaxes/bin.c

  • Committer: Package Import Robot
  • Author(s): Krzysztof Klimonda
  • Date: 2012-03-27 14:26:16 UTC
  • Revision ID: package-import@ubuntu.com-20120327142616-xt24t6nffm3f7ybz
Tags: upstream-1.2.11.7
ImportĀ upstreamĀ versionĀ 1.2.11.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** BEGIN COPYRIGHT BLOCK
 
2
 * This Program is free software; you can redistribute it and/or modify it under
 
3
 * the terms of the GNU General Public License as published by the Free Software
 
4
 * Foundation; version 2 of the License.
 
5
 * 
 
6
 * This Program is distributed in the hope that it will be useful, but WITHOUT
 
7
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
8
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 
9
 * 
 
10
 * You should have received a copy of the GNU General Public License along with
 
11
 * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
 
12
 * Place, Suite 330, Boston, MA 02111-1307 USA.
 
13
 * 
 
14
 * In addition, as a special exception, Red Hat, Inc. gives You the additional
 
15
 * right to link the code of this Program with code not covered under the GNU
 
16
 * General Public License ("Non-GPL Code") and to distribute linked combinations
 
17
 * including the two, subject to the limitations in this paragraph. Non-GPL Code
 
18
 * permitted under this exception must only link to the code of this Program
 
19
 * through those well defined interfaces identified in the file named EXCEPTION
 
20
 * found in the source code files (the "Approved Interfaces"). The files of
 
21
 * Non-GPL Code may instantiate templates or use macros or inline functions from
 
22
 * the Approved Interfaces without causing the resulting work to be covered by
 
23
 * the GNU General Public License. Only Red Hat, Inc. may make changes or
 
24
 * additions to the list of Approved Interfaces. You must obey the GNU General
 
25
 * Public License in all respects for all of the Program code and other code used
 
26
 * in conjunction with the Program except the Non-GPL Code covered by this
 
27
 * exception. If you modify this file, you may extend this exception to your
 
28
 * version of the file, but you are not obligated to do so. If you do not wish to
 
29
 * provide this exception without modification, you must delete this exception
 
30
 * statement from your version and license this file solely under the GPL without
 
31
 * exception. 
 
32
 * 
 
33
 * 
 
34
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 
35
 * Copyright (C) 2005 Red Hat, Inc.
 
36
 * All rights reserved.
 
37
 * END COPYRIGHT BLOCK **/
 
38
 
 
39
#ifdef HAVE_CONFIG_H
 
40
#  include <config.h>
 
41
#endif
 
42
 
 
43
/* bin.c - bin syntax routines */
 
44
 
 
45
/*
 
46
 * This file actually implements four syntax plugins: OctetString, JPEG,
 
47
 * Fax, and Binary.
 
48
 */
 
49
 
 
50
#include <stdio.h>
 
51
#include <string.h>
 
52
#include <sys/types.h>
 
53
#include "syntax.h"
 
54
 
 
55
#define CERTIFICATE_SYNTAX_OID "1.3.6.1.4.1.1466.115.121.1.8"
 
56
#define CERTIFICATELIST_SYNTAX_OID "1.3.6.1.4.1.1466.115.121.1.9"
 
57
#define CERTIFICATEPAIR_SYNTAX_OID "1.3.6.1.4.1.1466.115.121.1.10"
 
58
#define SUPPORTEDALGORITHM_SYNTAX_OID "1.3.6.1.4.1.1466.115.121.1.49"
 
59
 
 
60
static int bin_filter_ava( Slapi_PBlock *pb, struct berval *bvfilter,
 
61
                        Slapi_Value **bvals, int ftype, Slapi_Value **retVal );
 
62
static int bin_values2keys( Slapi_PBlock *pb, Slapi_Value **bvals,
 
63
        Slapi_Value ***ivals, int ftype );
 
64
static int bin_assertion2keys_ava( Slapi_PBlock *pb, Slapi_Value *bval,
 
65
        Slapi_Value ***ivals, int ftype );
 
66
static int bin_compare(struct berval *v1, struct berval *v2);
 
67
 
 
68
/*
 
69
 * Attribute syntaxes. We treat all of these the same since the
 
70
 * LDAP-specific encoding for all of them are simply strings of octets
 
71
 * with no real content restrictions (even though the content is supposed
 
72
 * to represent something specific).  For this reason, we do no
 
73
 * validation of the values for these syntaxes.
 
74
 */
 
75
static char *bin_names[] = { "Binary", "bin", BINARY_SYNTAX_OID, 0 };
 
76
 
 
77
static char *octetstring_names[] = { "OctetString", OCTETSTRING_SYNTAX_OID, 0 };
 
78
 
 
79
static char *jpeg_names[] = { "JPEG", JPEG_SYNTAX_OID, 0 };
 
80
 
 
81
static char *fax_names[] = { "FAX", FAX_SYNTAX_OID, 0 };
 
82
 
 
83
 
 
84
/* This syntax has "gone away" in RFC 4517, however we still use it for
 
85
 * a number of attributes in our default schema.  We should try to eliminate
 
86
 * it's use and remove support for it. */
 
87
static Slapi_PluginDesc bin_pdesc = {
 
88
        "bin-syntax", VENDOR, DS_PACKAGE_VERSION,
 
89
        "binary attribute syntax plugin"
 
90
};
 
91
 
 
92
static Slapi_PluginDesc octetstring_pdesc = {
 
93
        "octetstring-syntax", VENDOR, DS_PACKAGE_VERSION,
 
94
        "octet string attribute syntax plugin"
 
95
};
 
96
 
 
97
static Slapi_PluginDesc jpeg_pdesc = {
 
98
        "jpeg-syntax", VENDOR, DS_PACKAGE_VERSION,
 
99
        "JPEG attribute syntax plugin"
 
100
};
 
101
 
 
102
static Slapi_PluginDesc fax_pdesc = {
 
103
        "fax-syntax", VENDOR, DS_PACKAGE_VERSION,
 
104
        "Fax attribute syntax plugin"
 
105
};
 
106
 
 
107
static const char *octetStringMatch_names[] = {"octetStringMatch", "2.5.13.17", NULL};
 
108
static const char *octetStringOrderingMatch_names[] = {"octetStringOrderingMatch", "2.5.13.18", NULL};
 
109
 
 
110
static char *octetStringCompat_syntaxes[] = {BINARY_SYNTAX_OID, JPEG_SYNTAX_OID, FAX_SYNTAX_OID, CERTIFICATE_SYNTAX_OID, CERTIFICATELIST_SYNTAX_OID, CERTIFICATEPAIR_SYNTAX_OID, SUPPORTEDALGORITHM_SYNTAX_OID, NULL};
 
111
 
 
112
static struct mr_plugin_def mr_plugin_table[] = {
 
113
{{"2.5.13.17", NULL, "octetStringMatch", "The octetStringMatch rule compares an assertion value of the Octet "
 
114
"String syntax to an attribute value of a syntax (e.g., the Octet "
 
115
"String or JPEG syntax) whose corresponding ASN.1 type is the OCTET "
 
116
"STRING ASN.1 type.  "
 
117
"The rule evaluates to TRUE if and only if the attribute value and the "
 
118
"assertion value are the same length and corresponding octets (by "
 
119
"position) are the same.", OCTETSTRING_SYNTAX_OID, 0, octetStringCompat_syntaxes}, /* matching rule desc */
 
120
 {"octetStringMatch-mr", VENDOR, DS_PACKAGE_VERSION, "octetStringMatch matching rule plugin"}, /* plugin desc */
 
121
   octetStringMatch_names, /* matching rule name/oid/aliases */
 
122
   NULL, NULL, bin_filter_ava, NULL, bin_values2keys,
 
123
   bin_assertion2keys_ava, NULL, bin_compare},
 
124
{{"2.5.13.18", NULL, "octetStringOrderingMatch", "The octetStringOrderingMatch rule compares an assertion value of the "
 
125
"Octet String syntax to an attribute value of a syntax (e.g., the "
 
126
"Octet String or JPEG syntax) whose corresponding ASN.1 type is the "
 
127
"OCTET STRING ASN.1 type.  "
 
128
"The rule evaluates to TRUE if and only if the attribute value appears "
 
129
"earlier in the collation order than the assertion value.  The rule "
 
130
"compares octet strings from the first octet to the last octet, and "
 
131
"from the most significant bit to the least significant bit within the "
 
132
"octet.  The first occurrence of a different bit determines the "
 
133
"ordering of the strings.  A zero bit precedes a one bit.  If the "
 
134
"strings contain different numbers of octets but the longer string is "
 
135
"identical to the shorter string up to the length of the shorter "
 
136
"string, then the shorter string precedes the longer string.",
 
137
OCTETSTRING_SYNTAX_OID, 0, octetStringCompat_syntaxes}, /* matching rule desc */
 
138
 {"octetStringOrderingMatch-mr", VENDOR, DS_PACKAGE_VERSION, "octetStringOrderingMatch matching rule plugin"}, /* plugin desc */
 
139
 octetStringOrderingMatch_names, /* matching rule name/oid/aliases */
 
140
 NULL, NULL, bin_filter_ava, NULL, bin_values2keys,
 
141
 bin_assertion2keys_ava, NULL, bin_compare}
 
142
};
 
143
/*
 
144
certificateExactMatch
 
145
certificateListExactMatch
 
146
certificatePairExactMatch
 
147
algorithmIdentifierMatch
 
148
certificateMatch
 
149
certificatePairMatch
 
150
certificateListMatch
 
151
*/
 
152
 
 
153
static size_t mr_plugin_table_size = sizeof(mr_plugin_table)/sizeof(mr_plugin_table[0]);
 
154
 
 
155
static int
 
156
matching_rule_plugin_init(Slapi_PBlock *pb)
 
157
{
 
158
        return syntax_matching_rule_plugin_init(pb, mr_plugin_table, mr_plugin_table_size);
 
159
}
 
160
 
 
161
static int
 
162
register_matching_rule_plugins()
 
163
{
 
164
        return syntax_register_matching_rule_plugins(mr_plugin_table, mr_plugin_table_size, matching_rule_plugin_init);
 
165
}
 
166
 
 
167
/*
 
168
 * register_bin_like_plugin():  register all items for a bin-like plugin.
 
169
 */
 
170
static int
 
171
register_bin_like_plugin( Slapi_PBlock *pb, Slapi_PluginDesc *pdescp,
 
172
                char **names, char *oid )
 
173
{
 
174
        int     rc;
 
175
 
 
176
        rc = slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
 
177
            (void *) SLAPI_PLUGIN_VERSION_01 );
 
178
        rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
 
179
            (void *)pdescp );
 
180
        rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_AVA,
 
181
            (void *) bin_filter_ava );
 
182
        rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALUES2KEYS,
 
183
            (void *) bin_values2keys );
 
184
        rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_AVA,
 
185
            (void *) bin_assertion2keys_ava );
 
186
        rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NAMES,
 
187
            (void *) names );
 
188
        rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_OID,
 
189
            (void *) oid );
 
190
 
 
191
        return( rc );
 
192
}
 
193
 
 
194
 
 
195
int
 
196
bin_init( Slapi_PBlock *pb )
 
197
{
 
198
        int     rc;
 
199
 
 
200
        LDAPDebug( LDAP_DEBUG_PLUGIN, "=> bin_init\n", 0, 0, 0 );
 
201
        rc = register_bin_like_plugin( pb, &bin_pdesc, bin_names,
 
202
                        BINARY_SYNTAX_OID );
 
203
        rc |= register_matching_rule_plugins();
 
204
        LDAPDebug( LDAP_DEBUG_PLUGIN, "<= bin_init %d\n", rc, 0, 0 );
 
205
        return( rc );
 
206
}
 
207
 
 
208
 
 
209
int
 
210
octetstring_init( Slapi_PBlock *pb )
 
211
{
 
212
        int     rc;
 
213
 
 
214
        LDAPDebug( LDAP_DEBUG_PLUGIN, "=> octetstring_init\n", 0, 0, 0 );
 
215
        rc = register_bin_like_plugin( pb, &octetstring_pdesc, octetstring_names,
 
216
                        OCTETSTRING_SYNTAX_OID );
 
217
        LDAPDebug( LDAP_DEBUG_PLUGIN, "<= octetstring_init %d\n", rc, 0, 0 );
 
218
        return( rc );
 
219
}
 
220
 
 
221
 
 
222
int
 
223
jpeg_init( Slapi_PBlock *pb )
 
224
{
 
225
        int     rc;
 
226
 
 
227
        LDAPDebug( LDAP_DEBUG_PLUGIN, "=> jpeg_init\n", 0, 0, 0 );
 
228
        rc = register_bin_like_plugin( pb, &jpeg_pdesc, jpeg_names,
 
229
                        JPEG_SYNTAX_OID );
 
230
        LDAPDebug( LDAP_DEBUG_PLUGIN, "<= jpeg_init %d\n", rc, 0, 0 );
 
231
        return( rc );
 
232
}
 
233
 
 
234
 
 
235
int
 
236
fax_init( Slapi_PBlock *pb )
 
237
{
 
238
        int     rc;
 
239
 
 
240
        LDAPDebug( LDAP_DEBUG_PLUGIN, "=> fax_init\n", 0, 0, 0 );
 
241
        rc = register_bin_like_plugin( pb, &fax_pdesc, fax_names,
 
242
                        FAX_SYNTAX_OID );
 
243
        LDAPDebug( LDAP_DEBUG_PLUGIN, "<= fax_init %d\n", rc, 0, 0 );
 
244
        return( rc );
 
245
}
 
246
 
 
247
 
 
248
static int
 
249
bin_filter_ava( Slapi_PBlock *pb, struct berval *bvfilter,
 
250
    Slapi_Value **bvals, int ftype, Slapi_Value **retVal )
 
251
{
 
252
    int i;
 
253
 
 
254
    for ( i = 0; (bvals != NULL) && (bvals[i] != NULL); i++ ) {
 
255
        const struct berval *bv = slapi_value_get_berval(bvals[i]);
 
256
        int rc = slapi_berval_cmp(bv, bvfilter);
 
257
 
 
258
        switch ( ftype ) {
 
259
        case LDAP_FILTER_GE:
 
260
            if ( rc >= 0 ) {
 
261
                if(retVal) {
 
262
                    *retVal = bvals[i];
 
263
                }
 
264
                return( 0 );
 
265
            }
 
266
            break;
 
267
        case LDAP_FILTER_LE:
 
268
            if ( rc <= 0 ) {
 
269
                if(retVal) {
 
270
                    *retVal = bvals[i];
 
271
                }
 
272
                return( 0 );
 
273
            }
 
274
            break;
 
275
        case LDAP_FILTER_EQUALITY:
 
276
            if ( rc == 0 ) {
 
277
                if(retVal) {
 
278
                    *retVal = bvals[i];
 
279
                }
 
280
                return( 0 );
 
281
            }
 
282
            break;
 
283
        }
 
284
    }
 
285
    if(retVal!=NULL)
 
286
    {
 
287
        *retVal= NULL;
 
288
    }
 
289
    return( -1 );
 
290
}
 
291
 
 
292
static int
 
293
bin_values2keys( Slapi_PBlock *pb, Slapi_Value **bvals,
 
294
                                        Slapi_Value ***ivals, int ftype )
 
295
{
 
296
        int     i;
 
297
 
 
298
        if (NULL == ivals) {
 
299
                return 1;
 
300
        }
 
301
        *ivals = NULL;
 
302
        if (NULL == bvals) {
 
303
                return 1;
 
304
        }
 
305
 
 
306
        if ( ftype != LDAP_FILTER_EQUALITY ) {
 
307
                return( LDAP_PROTOCOL_ERROR );
 
308
        }
 
309
 
 
310
        for ( i = 0; bvals[i] != NULL; i++ ) {
 
311
                /* NULL */
 
312
        }
 
313
        (*ivals) = (Slapi_Value **) slapi_ch_malloc(( i + 1 ) *
 
314
            sizeof(Slapi_Value *) );
 
315
 
 
316
        for ( i = 0; bvals[i] != NULL; i++ )
 
317
        {
 
318
                (*ivals)[i] = slapi_value_dup(bvals[i]);
 
319
        }
 
320
        (*ivals)[i] = NULL;
 
321
 
 
322
        return( 0 );
 
323
}
 
324
 
 
325
static int
 
326
bin_assertion2keys_ava( Slapi_PBlock *pb, Slapi_Value *bval,
 
327
    Slapi_Value ***ivals, int ftype )
 
328
{
 
329
    Slapi_Value *tmpval=NULL;
 
330
    size_t len;
 
331
 
 
332
    if (( ftype != LDAP_FILTER_EQUALITY ) &&
 
333
        ( ftype != LDAP_FILTER_EQUALITY_FAST))
 
334
    {
 
335
                return( LDAP_PROTOCOL_ERROR );
 
336
        }
 
337
    if(ftype == LDAP_FILTER_EQUALITY_FAST) {
 
338
                /* With the fast option, we are trying to avoid creating and freeing
 
339
         * a bunch of structures - we just do one malloc here - see
 
340
         * ava_candidates in filterentry.c
 
341
         */
 
342
        len=slapi_value_get_length(bval);
 
343
        tmpval=(*ivals)[0];
 
344
        if (len > tmpval->bv.bv_len) {
 
345
            tmpval->bv.bv_val=(char *)slapi_ch_malloc(len);
 
346
        }
 
347
        tmpval->bv.bv_len=len;
 
348
        memcpy(tmpval->bv.bv_val,slapi_value_get_string(bval),len);
 
349
    } else {
 
350
            (*ivals) = (Slapi_Value **) slapi_ch_malloc( 2 * sizeof(Slapi_Value *) );
 
351
            (*ivals)[0] = slapi_value_dup( bval );
 
352
            (*ivals)[1] = NULL;
 
353
    }
 
354
        return( 0 );
 
355
}
 
356
 
 
357
#define BV_EMPTY(bv) ((!bv || !bv->bv_len || !bv->bv_val))
 
358
 
 
359
static int
 
360
bin_compare(    
 
361
        struct berval   *v1,
 
362
    struct berval       *v2
 
363
)
 
364
{
 
365
    int rc = 0;
 
366
 
 
367
    if (BV_EMPTY(v1) && BV_EMPTY(v2)) {
 
368
        rc = 0; /* empty == empty */
 
369
    } else if (BV_EMPTY(v1) && !BV_EMPTY(v2)) {
 
370
        rc = 1; /* something in v2 always greater than empty v1 */
 
371
    } else if (!BV_EMPTY(v1) && BV_EMPTY(v2)) {
 
372
        rc = -1; /* something in v1 always greater than empty v2 */
 
373
    } else { /* both have actual data */
 
374
        rc = slapi_berval_cmp(v1, v2);
 
375
    }
 
376
 
 
377
    return rc;
 
378
}