xref: /openbsd/usr.sbin/ldapd/matching.c (revision 6c195454)
1*6c195454Smartinh /*	$OpenBSD: matching.c,v 1.2 2010/11/04 15:35:00 martinh Exp $ */
2d6378167Smartinh 
3d6378167Smartinh /*
4d6378167Smartinh  * Copyright (c) 2010 Martin Hedenfalk <martinh@openbsd.org>
5d6378167Smartinh  *
6d6378167Smartinh  * Permission to use, copy, modify, and distribute this software for any
7d6378167Smartinh  * purpose with or without fee is hereby granted, provided that the above
8d6378167Smartinh  * copyright notice and this permission notice appear in all copies.
9d6378167Smartinh  *
10d6378167Smartinh  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11d6378167Smartinh  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12d6378167Smartinh  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13d6378167Smartinh  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14d6378167Smartinh  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15d6378167Smartinh  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16d6378167Smartinh  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17d6378167Smartinh  */
18d6378167Smartinh 
19d6378167Smartinh #include <sys/types.h>
20d6378167Smartinh #include <sys/queue.h>
21d6378167Smartinh #include <sys/tree.h>
22d6378167Smartinh 
23d6378167Smartinh #include <stdio.h>
24d6378167Smartinh #include <stdlib.h>
25d6378167Smartinh #include <string.h>
26d6378167Smartinh 
27d6378167Smartinh #include "schema.h"
28d6378167Smartinh 
29d6378167Smartinh #ifndef nitems
30d6378167Smartinh # define nitems(_a)	 (sizeof((_a)) / sizeof((_a)[0]))
31d6378167Smartinh #endif
32d6378167Smartinh 
33d6378167Smartinh static const char *ia5string_syntaxes[] = {
34d6378167Smartinh 	"1.3.6.1.4.1.1466.115.121.1.26",
35d6378167Smartinh 	NULL
36d6378167Smartinh };
37d6378167Smartinh 
38d6378167Smartinh static const char *dir_string_syntaxes[] = {
39d6378167Smartinh 	"1.3.6.1.4.1.1466.115.121.1.15",
40d6378167Smartinh 	"1.3.6.1.4.1.1466.115.121.1.44",
41d6378167Smartinh 	"1.3.6.1.4.1.1466.115.121.1.11",
42d6378167Smartinh 	"1.3.6.1.4.1.1466.115.121.1.50",
43d6378167Smartinh 	"1.3.6.1.4.1.1466.115.121.1.26",
44d6378167Smartinh 	NULL
45d6378167Smartinh };
46d6378167Smartinh 
47d6378167Smartinh static const char *num_string_syntaxes[] = {
48d6378167Smartinh 	"1.3.6.1.4.1.1466.115.121.1.36",
49d6378167Smartinh 	NULL
50d6378167Smartinh };
51d6378167Smartinh 
52d6378167Smartinh static const char *telephone_syntaxes[] = {
53d6378167Smartinh 	"1.3.6.1.4.1.1466.115.121.1.50",
54d6378167Smartinh 	NULL
55d6378167Smartinh };
56d6378167Smartinh 
57d6378167Smartinh static const char *dir_string_sequence_syntaxes[] = {
58d6378167Smartinh 	"1.3.6.1.4.1.1466.115.121.1.41",
59d6378167Smartinh 	NULL
60d6378167Smartinh };
61d6378167Smartinh 
62d6378167Smartinh static const char *int_first_component_syntaxes[] = {
63d6378167Smartinh 	"1.3.6.1.4.1.1466.115.121.1.17",
64d6378167Smartinh 	NULL
65d6378167Smartinh };
66d6378167Smartinh 
67d6378167Smartinh static const char *oid_first_component_syntaxes[] = {
68d6378167Smartinh 	"1.3.6.1.4.1.1466.115.121.1.3",
69d6378167Smartinh 	"1.3.6.1.4.1.1466.115.121.1.16",
70d6378167Smartinh 	"1.3.6.1.4.1.1466.115.121.1.54",
71d6378167Smartinh 	"1.3.6.1.4.1.1466.115.121.1.30",
72d6378167Smartinh 	"1.3.6.1.4.1.1466.115.121.1.31",
73d6378167Smartinh 	"1.3.6.1.4.1.1466.115.121.1.35",
74d6378167Smartinh 	"1.3.6.1.4.1.1466.115.121.1.37",
75d6378167Smartinh 	NULL
76d6378167Smartinh };
77d6378167Smartinh 
78*6c195454Smartinh struct match_rule match_rules[] = {
79d6378167Smartinh 
80d6378167Smartinh 	{ "1.3.6.1.1.16.2", "uuidMatch", MATCH_EQUALITY, NULL, "1.3.6.1.1.16.1", NULL },
81d6378167Smartinh 	{ "1.3.6.1.1.16.3", "uuidOrderingMatch", MATCH_ORDERING, NULL, "1.3.6.1.1.16.1", NULL },
82d6378167Smartinh 	{ "1.3.6.1.4.1.1466.109.114.1", "caseExactIA5Match", MATCH_EQUALITY, NULL, "1.3.6.1.4.1.1466.115.121.1.26", ia5string_syntaxes },
83d6378167Smartinh 	{ "1.3.6.1.4.1.1466.109.114.2", "caseIgnoreIA5Match", MATCH_EQUALITY, NULL, "1.3.6.1.4.1.1466.115.121.1.26", ia5string_syntaxes },
84d6378167Smartinh 	{ "1.3.6.1.4.1.1466.109.114.3", "caseIgnoreIA5SubstringsMatch", MATCH_SUBSTR, NULL, "1.3.6.1.4.1.1466.115.121.1.58", ia5string_syntaxes },
85d6378167Smartinh 	{ "2.5.13.0", "objectIdentifierMatch", MATCH_EQUALITY, NULL, "1.3.6.1.4.1.1466.115.121.1.38", NULL },
86d6378167Smartinh 	{ "2.5.13.1", "distinguishedNameMatch", MATCH_EQUALITY, NULL, "1.3.6.1.4.1.1466.115.121.1.12", NULL },
87d6378167Smartinh 	{ "2.5.13.10", "numericStringSubstringsMatch", MATCH_SUBSTR, NULL, "1.3.6.1.4.1.1466.115.121.1.58", num_string_syntaxes },
88d6378167Smartinh 	{ "2.5.13.11", "caseIgnoreListMatch", MATCH_EQUALITY, NULL, "1.3.6.1.4.1.1466.115.121.1.41", NULL },
89d6378167Smartinh 	{ "2.5.13.12", "caseIgnoreListSubstringsMatch", MATCH_SUBSTR, NULL, "1.3.6.1.4.1.1466.115.121.1.58", dir_string_sequence_syntaxes },
90d6378167Smartinh 	{ "2.5.13.13", "booleanMatch", MATCH_EQUALITY, NULL, "1.3.6.1.4.1.1466.115.121.1.7", NULL },
91d6378167Smartinh 	{ "2.5.13.14", "integerMatch", MATCH_EQUALITY, NULL, "1.3.6.1.4.1.1466.115.121.1.27", NULL },
92d6378167Smartinh 	{ "2.5.13.15", "integerOrderingMatch", MATCH_ORDERING, NULL, "1.3.6.1.4.1.1466.115.121.1.27", NULL },
93d6378167Smartinh 	{ "2.5.13.16", "bitStringMatch", MATCH_EQUALITY,  NULL, "1.3.6.1.4.1.1466.115.121.1.6", NULL },
94d6378167Smartinh 	{ "2.5.13.17", "octetStringMatch", MATCH_EQUALITY, NULL, "1.3.6.1.4.1.1466.115.121.1.40", NULL },
95d6378167Smartinh 	{ "2.5.13.18", "octetStringOrderingMatch", MATCH_ORDERING, NULL, "1.3.6.1.4.1.1466.115.121.1.40", NULL },
96d6378167Smartinh 	{ "2.5.13.2", "caseIgnoreMatch", MATCH_EQUALITY, NULL, "1.3.6.1.4.1.1466.115.121.1.15", dir_string_syntaxes },
97d6378167Smartinh 	{ "2.5.13.20", "telephoneNumberMatch", MATCH_EQUALITY, NULL, "1.3.6.1.4.1.1466.115.121.1.50", NULL },
98d6378167Smartinh 	{ "2.5.13.21", "telephoneNumberSubstringsMatch", MATCH_SUBSTR, NULL, "1.3.6.1.4.1.1466.115.121.1.58", telephone_syntaxes },
99d6378167Smartinh 	{ "2.5.13.23", "uniqueMemberMatch", MATCH_EQUALITY, NULL, "1.3.6.1.4.1.1466.115.121.1.34", NULL },
100d6378167Smartinh 	{ "2.5.13.27", "generalizedTimeMatch", MATCH_EQUALITY, NULL, "1.3.6.1.4.1.1466.115.121.1.24", NULL },
101d6378167Smartinh 	{ "2.5.13.28", "generalizedTimeOrderingMatch", MATCH_ORDERING, NULL, "1.3.6.1.4.1.1466.115.121.1.24", NULL },
102d6378167Smartinh 	{ "2.5.13.29", "integerFirstComponentMatch", MATCH_EQUALITY, NULL, "1.3.6.1.4.1.1466.115.121.1.27", int_first_component_syntaxes },
103d6378167Smartinh 	{ "2.5.13.3", "caseIgnoreOrderingMatch", MATCH_ORDERING, NULL, "1.3.6.1.4.1.1466.115.121.1.15", dir_string_syntaxes },
104d6378167Smartinh 	{ "2.5.13.30", "objectIdentifierFirstComponentMatch", MATCH_EQUALITY, NULL, "1.3.6.1.4.1.1466.115.121.1.38", oid_first_component_syntaxes },
105d6378167Smartinh 	{ "2.5.13.31", "directoryStringFirstComponentMatch", MATCH_EQUALITY, NULL, "1.3.6.1.4.1.1466.115.121.1.15", NULL },
106d6378167Smartinh 	{ "2.5.13.4", "caseIgnoreSubstringsMatch", MATCH_SUBSTR, NULL, "1.3.6.1.4.1.1466.115.121.1.58", dir_string_syntaxes },
107d6378167Smartinh 	{ "2.5.13.5", "caseExactMatch", MATCH_EQUALITY, NULL, "1.3.6.1.4.1.1466.115.121.1.15", NULL },
108d6378167Smartinh 	{ "2.5.13.6", "caseExactOrderingMatch", MATCH_ORDERING, NULL, "1.3.6.1.4.1.1466.115.121.1.15", NULL },
109d6378167Smartinh 	{ "2.5.13.7", "caseExactSubstringsMatch", MATCH_SUBSTR, NULL, "1.3.6.1.4.1.1466.115.121.1.58", dir_string_syntaxes },
110d6378167Smartinh 	{ "2.5.13.8", "numericStringMatch", MATCH_EQUALITY, NULL, "1.3.6.1.4.1.1466.115.121.1.36", NULL },
111d6378167Smartinh 	{ "2.5.13.9", "numericStringOrderingMatch", MATCH_ORDERING, NULL, "1.3.6.1.4.1.1466.115.121.1.36", NULL },
112d6378167Smartinh 
113d6378167Smartinh #if 0
114d6378167Smartinh 	{ "2.5.13.32", "wordMatch", "1.3.6.1.4.1.1466.115.121.1.15", MATCH_EQUALITY, NULL },
115d6378167Smartinh 	{ "2.5.13.33", "keywordMatch", "1.3.6.1.4.1.1466.115.121.1.15", MATCH_EQUALITY, NULL },
116d6378167Smartinh #endif
117d6378167Smartinh };
118d6378167Smartinh 
119*6c195454Smartinh int num_match_rules = nitems(match_rules);
120*6c195454Smartinh 
121d6378167Smartinh static struct match_rule_alias {
122d6378167Smartinh 	char	*name;
123d6378167Smartinh 	char	*oid;
124d6378167Smartinh } aliases[] = {
125d6378167Smartinh 	{ "caseExactIA5SubstringsMatch", "caseExactSubstringsMatch" },
126d6378167Smartinh };
127d6378167Smartinh 
128d6378167Smartinh const struct match_rule *
match_rule_lookup(const char * oid_or_name)129d6378167Smartinh match_rule_lookup(const char *oid_or_name)
130d6378167Smartinh {
131d6378167Smartinh 	unsigned int		 i;
132d6378167Smartinh 
133d6378167Smartinh 	for (i = 0; i < nitems(match_rules); i++) {
134d6378167Smartinh 		if (strcasecmp(oid_or_name, match_rules[i].name) == 0 ||
135d6378167Smartinh 		    strcmp(oid_or_name, match_rules[i].oid) == 0)
136d6378167Smartinh 			return &match_rules[i];
137d6378167Smartinh 	}
138d6378167Smartinh 
139d6378167Smartinh 	for (i = 0; i < nitems(aliases); i++) {
140d6378167Smartinh 		if (strcasecmp(oid_or_name, aliases[i].name) == 0)
141d6378167Smartinh 			return match_rule_lookup(aliases[i].oid);
142d6378167Smartinh 	}
143d6378167Smartinh 
144d6378167Smartinh 	return NULL;
145d6378167Smartinh }
146d6378167Smartinh 
147