1
0

bin.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /* bin.c - bin syntax routines */
  13. /*
  14. * This file actually implements four syntax plugins: OctetString, JPEG,
  15. * Fax, and Binary.
  16. */
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <sys/types.h>
  20. #include "syntax.h"
  21. #define CERTIFICATE_SYNTAX_OID "1.3.6.1.4.1.1466.115.121.1.8"
  22. #define CERTIFICATELIST_SYNTAX_OID "1.3.6.1.4.1.1466.115.121.1.9"
  23. #define CERTIFICATEPAIR_SYNTAX_OID "1.3.6.1.4.1.1466.115.121.1.10"
  24. #define SUPPORTEDALGORITHM_SYNTAX_OID "1.3.6.1.4.1.1466.115.121.1.49"
  25. static int bin_filter_ava( Slapi_PBlock *pb, struct berval *bvfilter,
  26. Slapi_Value **bvals, int ftype, Slapi_Value **retVal );
  27. static int bin_values2keys( Slapi_PBlock *pb, Slapi_Value **bvals,
  28. Slapi_Value ***ivals, int ftype );
  29. static int bin_assertion2keys_ava( Slapi_PBlock *pb, Slapi_Value *bval,
  30. Slapi_Value ***ivals, int ftype );
  31. static int bin_compare(struct berval *v1, struct berval *v2);
  32. /*
  33. * Attribute syntaxes. We treat all of these the same since the
  34. * LDAP-specific encoding for all of them are simply strings of octets
  35. * with no real content restrictions (even though the content is supposed
  36. * to represent something specific). For this reason, we do no
  37. * validation of the values for these syntaxes.
  38. */
  39. static char *bin_names[] = { "Binary", "bin", BINARY_SYNTAX_OID, 0 };
  40. static char *octetstring_names[] = { "OctetString", OCTETSTRING_SYNTAX_OID, 0 };
  41. static char *jpeg_names[] = { "JPEG", JPEG_SYNTAX_OID, 0 };
  42. static char *fax_names[] = { "FAX", FAX_SYNTAX_OID, 0 };
  43. /* This syntax has "gone away" in RFC 4517, however we still use it for
  44. * a number of attributes in our default schema. We should try to eliminate
  45. * it's use and remove support for it. */
  46. static Slapi_PluginDesc bin_pdesc = {
  47. "bin-syntax", VENDOR, DS_PACKAGE_VERSION,
  48. "binary attribute syntax plugin"
  49. };
  50. static Slapi_PluginDesc octetstring_pdesc = {
  51. "octetstring-syntax", VENDOR, DS_PACKAGE_VERSION,
  52. "octet string attribute syntax plugin"
  53. };
  54. static Slapi_PluginDesc jpeg_pdesc = {
  55. "jpeg-syntax", VENDOR, DS_PACKAGE_VERSION,
  56. "JPEG attribute syntax plugin"
  57. };
  58. static Slapi_PluginDesc fax_pdesc = {
  59. "fax-syntax", VENDOR, DS_PACKAGE_VERSION,
  60. "Fax attribute syntax plugin"
  61. };
  62. static const char *octetStringMatch_names[] = {"octetStringMatch", "2.5.13.17", NULL};
  63. static const char *octetStringOrderingMatch_names[] = {"octetStringOrderingMatch", "2.5.13.18", NULL};
  64. 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};
  65. static struct mr_plugin_def mr_plugin_table[] = {
  66. {{"2.5.13.17", NULL, "octetStringMatch", "The octetStringMatch rule compares an assertion value of the Octet "
  67. "String syntax to an attribute value of a syntax (e.g., the Octet "
  68. "String or JPEG syntax) whose corresponding ASN.1 type is the OCTET "
  69. "STRING ASN.1 type. "
  70. "The rule evaluates to TRUE if and only if the attribute value and the "
  71. "assertion value are the same length and corresponding octets (by "
  72. "position) are the same.", OCTETSTRING_SYNTAX_OID, 0, octetStringCompat_syntaxes}, /* matching rule desc */
  73. {"octetStringMatch-mr", VENDOR, DS_PACKAGE_VERSION, "octetStringMatch matching rule plugin"}, /* plugin desc */
  74. octetStringMatch_names, /* matching rule name/oid/aliases */
  75. NULL, NULL, bin_filter_ava, NULL, bin_values2keys,
  76. bin_assertion2keys_ava, NULL, bin_compare},
  77. {{"2.5.13.18", NULL, "octetStringOrderingMatch", "The octetStringOrderingMatch rule compares an assertion value of the "
  78. "Octet String syntax to an attribute value of a syntax (e.g., the "
  79. "Octet String or JPEG syntax) whose corresponding ASN.1 type is the "
  80. "OCTET STRING ASN.1 type. "
  81. "The rule evaluates to TRUE if and only if the attribute value appears "
  82. "earlier in the collation order than the assertion value. The rule "
  83. "compares octet strings from the first octet to the last octet, and "
  84. "from the most significant bit to the least significant bit within the "
  85. "octet. The first occurrence of a different bit determines the "
  86. "ordering of the strings. A zero bit precedes a one bit. If the "
  87. "strings contain different numbers of octets but the longer string is "
  88. "identical to the shorter string up to the length of the shorter "
  89. "string, then the shorter string precedes the longer string.",
  90. OCTETSTRING_SYNTAX_OID, 0, octetStringCompat_syntaxes}, /* matching rule desc */
  91. {"octetStringOrderingMatch-mr", VENDOR, DS_PACKAGE_VERSION, "octetStringOrderingMatch matching rule plugin"}, /* plugin desc */
  92. octetStringOrderingMatch_names, /* matching rule name/oid/aliases */
  93. NULL, NULL, bin_filter_ava, NULL, bin_values2keys,
  94. bin_assertion2keys_ava, NULL, bin_compare}
  95. };
  96. /*
  97. certificateExactMatch
  98. certificateListExactMatch
  99. certificatePairExactMatch
  100. algorithmIdentifierMatch
  101. certificateMatch
  102. certificatePairMatch
  103. certificateListMatch
  104. */
  105. static size_t mr_plugin_table_size = sizeof(mr_plugin_table)/sizeof(mr_plugin_table[0]);
  106. static int
  107. matching_rule_plugin_init(Slapi_PBlock *pb)
  108. {
  109. return syntax_matching_rule_plugin_init(pb, mr_plugin_table, mr_plugin_table_size);
  110. }
  111. static int
  112. register_matching_rule_plugins()
  113. {
  114. return syntax_register_matching_rule_plugins(mr_plugin_table, mr_plugin_table_size, matching_rule_plugin_init);
  115. }
  116. /*
  117. * register_bin_like_plugin(): register all items for a bin-like plugin.
  118. */
  119. static int
  120. register_bin_like_plugin( Slapi_PBlock *pb, Slapi_PluginDesc *pdescp,
  121. char **names, char *oid )
  122. {
  123. int rc;
  124. rc = slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  125. (void *) SLAPI_PLUGIN_VERSION_01 );
  126. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  127. (void *)pdescp );
  128. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_AVA,
  129. (void *) bin_filter_ava );
  130. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALUES2KEYS,
  131. (void *) bin_values2keys );
  132. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_AVA,
  133. (void *) bin_assertion2keys_ava );
  134. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NAMES,
  135. (void *) names );
  136. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_OID,
  137. (void *) oid );
  138. return( rc );
  139. }
  140. int
  141. bin_init( Slapi_PBlock *pb )
  142. {
  143. int rc;
  144. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> bin_init\n", 0, 0, 0 );
  145. rc = register_bin_like_plugin( pb, &bin_pdesc, bin_names,
  146. BINARY_SYNTAX_OID );
  147. rc |= register_matching_rule_plugins();
  148. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= bin_init %d\n", rc, 0, 0 );
  149. return( rc );
  150. }
  151. int
  152. octetstring_init( Slapi_PBlock *pb )
  153. {
  154. int rc;
  155. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> octetstring_init\n", 0, 0, 0 );
  156. rc = register_bin_like_plugin( pb, &octetstring_pdesc, octetstring_names,
  157. OCTETSTRING_SYNTAX_OID );
  158. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= octetstring_init %d\n", rc, 0, 0 );
  159. return( rc );
  160. }
  161. int
  162. jpeg_init( Slapi_PBlock *pb )
  163. {
  164. int rc;
  165. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> jpeg_init\n", 0, 0, 0 );
  166. rc = register_bin_like_plugin( pb, &jpeg_pdesc, jpeg_names,
  167. JPEG_SYNTAX_OID );
  168. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= jpeg_init %d\n", rc, 0, 0 );
  169. return( rc );
  170. }
  171. int
  172. fax_init( Slapi_PBlock *pb )
  173. {
  174. int rc;
  175. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> fax_init\n", 0, 0, 0 );
  176. rc = register_bin_like_plugin( pb, &fax_pdesc, fax_names,
  177. FAX_SYNTAX_OID );
  178. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= fax_init %d\n", rc, 0, 0 );
  179. return( rc );
  180. }
  181. static int
  182. bin_filter_ava( Slapi_PBlock *pb, struct berval *bvfilter,
  183. Slapi_Value **bvals, int ftype, Slapi_Value **retVal )
  184. {
  185. int i;
  186. for ( i = 0; (bvals != NULL) && (bvals[i] != NULL); i++ ) {
  187. const struct berval *bv = slapi_value_get_berval(bvals[i]);
  188. int rc = slapi_berval_cmp(bv, bvfilter);
  189. switch ( ftype ) {
  190. case LDAP_FILTER_GE:
  191. if ( rc >= 0 ) {
  192. if(retVal) {
  193. *retVal = bvals[i];
  194. }
  195. return( 0 );
  196. }
  197. break;
  198. case LDAP_FILTER_LE:
  199. if ( rc <= 0 ) {
  200. if(retVal) {
  201. *retVal = bvals[i];
  202. }
  203. return( 0 );
  204. }
  205. break;
  206. case LDAP_FILTER_EQUALITY:
  207. if ( rc == 0 ) {
  208. if(retVal) {
  209. *retVal = bvals[i];
  210. }
  211. return( 0 );
  212. }
  213. break;
  214. }
  215. }
  216. if(retVal!=NULL)
  217. {
  218. *retVal= NULL;
  219. }
  220. return( -1 );
  221. }
  222. static int
  223. bin_values2keys( Slapi_PBlock *pb, Slapi_Value **bvals,
  224. Slapi_Value ***ivals, int ftype )
  225. {
  226. int i;
  227. if (NULL == ivals) {
  228. return 1;
  229. }
  230. *ivals = NULL;
  231. if (NULL == bvals) {
  232. return 1;
  233. }
  234. if ( ftype != LDAP_FILTER_EQUALITY ) {
  235. return( LDAP_PROTOCOL_ERROR );
  236. }
  237. for ( i = 0; bvals[i] != NULL; i++ ) {
  238. /* NULL */
  239. }
  240. (*ivals) = (Slapi_Value **) slapi_ch_malloc(( i + 1 ) *
  241. sizeof(Slapi_Value *) );
  242. for ( i = 0; bvals[i] != NULL; i++ )
  243. {
  244. (*ivals)[i] = slapi_value_dup(bvals[i]);
  245. }
  246. (*ivals)[i] = NULL;
  247. return( 0 );
  248. }
  249. static int
  250. bin_assertion2keys_ava( Slapi_PBlock *pb, Slapi_Value *bval,
  251. Slapi_Value ***ivals, int ftype )
  252. {
  253. Slapi_Value *tmpval=NULL;
  254. size_t len;
  255. if (( ftype != LDAP_FILTER_EQUALITY ) &&
  256. ( ftype != LDAP_FILTER_EQUALITY_FAST))
  257. {
  258. return( LDAP_PROTOCOL_ERROR );
  259. }
  260. if(ftype == LDAP_FILTER_EQUALITY_FAST) {
  261. /* With the fast option, we are trying to avoid creating and freeing
  262. * a bunch of structures - we just do one malloc here - see
  263. * ava_candidates in filterentry.c
  264. */
  265. len=slapi_value_get_length(bval);
  266. tmpval=(*ivals)[0];
  267. if (len > tmpval->bv.bv_len) {
  268. tmpval->bv.bv_val=(char *)slapi_ch_malloc(len);
  269. }
  270. tmpval->bv.bv_len=len;
  271. memcpy(tmpval->bv.bv_val,slapi_value_get_string(bval),len);
  272. } else {
  273. (*ivals) = (Slapi_Value **) slapi_ch_malloc( 2 * sizeof(Slapi_Value *) );
  274. (*ivals)[0] = slapi_value_dup( bval );
  275. (*ivals)[1] = NULL;
  276. }
  277. return( 0 );
  278. }
  279. #define BV_EMPTY(bv) ((!bv || !bv->bv_len || !bv->bv_val))
  280. static int
  281. bin_compare(
  282. struct berval *v1,
  283. struct berval *v2
  284. )
  285. {
  286. int rc = 0;
  287. if (BV_EMPTY(v1) && BV_EMPTY(v2)) {
  288. rc = 0; /* empty == empty */
  289. } else if (BV_EMPTY(v1) && !BV_EMPTY(v2)) {
  290. rc = 1; /* something in v2 always greater than empty v1 */
  291. } else if (!BV_EMPTY(v1) && BV_EMPTY(v2)) {
  292. rc = -1; /* something in v1 always greater than empty v2 */
  293. } else { /* both have actual data */
  294. rc = slapi_berval_cmp(v1, v2);
  295. }
  296. return rc;
  297. }