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