bin.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /* bin.c - bin syntax routines */
  42. /*
  43. * This file actually implements four syntax plugins: OctetString, JPEG,
  44. * Fax, and Binary.
  45. */
  46. #include <stdio.h>
  47. #include <string.h>
  48. #include <sys/types.h>
  49. #include "syntax.h"
  50. static int bin_filter_ava( Slapi_PBlock *pb, struct berval *bvfilter,
  51. Slapi_Value **bvals, int ftype, Slapi_Value **retVal );
  52. static int bin_values2keys( Slapi_PBlock *pb, Slapi_Value **bvals,
  53. Slapi_Value ***ivals, int ftype );
  54. static int bin_assertion2keys_ava( Slapi_PBlock *pb, Slapi_Value *bval,
  55. Slapi_Value ***ivals, int ftype );
  56. /*
  57. * Attribute syntaxes. We treat all of these the same since the
  58. * LDAP-specific encoding for all of them are simply strings of octets
  59. * with no real content restrictions (even though the content is supposed
  60. * to represent something specific). For this reason, we do no
  61. * validation of the values for these syntaxes.
  62. */
  63. static char *bin_names[] = { "Binary", "bin", BINARY_SYNTAX_OID, 0 };
  64. static char *octetstring_names[] = { "OctetString", OCTETSTRING_SYNTAX_OID, 0 };
  65. static char *jpeg_names[] = { "JPEG", JPEG_SYNTAX_OID, 0 };
  66. static char *fax_names[] = { "FAX", FAX_SYNTAX_OID, 0 };
  67. /* This syntax has "gone away" in RFC 4517, however we still use it for
  68. * a number of attributes in our default schema. We should try to eliminate
  69. * it's use and remove support for it. */
  70. static Slapi_PluginDesc bin_pdesc = {
  71. "bin-syntax", VENDOR, PACKAGE_VERSION,
  72. "binary attribute syntax plugin"
  73. };
  74. static Slapi_PluginDesc octetstring_pdesc = {
  75. "octetstring-syntax", VENDOR, PACKAGE_VERSION,
  76. "octet string attribute syntax plugin"
  77. };
  78. static Slapi_PluginDesc jpeg_pdesc = {
  79. "jpeg-syntax", VENDOR, PACKAGE_VERSION,
  80. "JPEG attribute syntax plugin"
  81. };
  82. static Slapi_PluginDesc fax_pdesc = {
  83. "fax-syntax", VENDOR, PACKAGE_VERSION,
  84. "Fax attribute syntax plugin"
  85. };
  86. /*
  87. * register_bin_like_plugin(): register all items for a bin-like plugin.
  88. */
  89. static int
  90. register_bin_like_plugin( Slapi_PBlock *pb, Slapi_PluginDesc *pdescp,
  91. char **names, char *oid )
  92. {
  93. int rc;
  94. rc = slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  95. (void *) SLAPI_PLUGIN_VERSION_01 );
  96. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  97. (void *)pdescp );
  98. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_AVA,
  99. (void *) bin_filter_ava );
  100. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALUES2KEYS,
  101. (void *) bin_values2keys );
  102. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_AVA,
  103. (void *) bin_assertion2keys_ava );
  104. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NAMES,
  105. (void *) names );
  106. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_OID,
  107. (void *) oid );
  108. return( rc );
  109. }
  110. int
  111. bin_init( Slapi_PBlock *pb )
  112. {
  113. int rc;
  114. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> bin_init\n", 0, 0, 0 );
  115. rc = register_bin_like_plugin( pb, &bin_pdesc, bin_names,
  116. BINARY_SYNTAX_OID );
  117. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= bin_init %d\n", rc, 0, 0 );
  118. return( rc );
  119. }
  120. int
  121. octetstring_init( Slapi_PBlock *pb )
  122. {
  123. int rc;
  124. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> octetstring_init\n", 0, 0, 0 );
  125. rc = register_bin_like_plugin( pb, &octetstring_pdesc, octetstring_names,
  126. OCTETSTRING_SYNTAX_OID );
  127. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= octetstring_init %d\n", rc, 0, 0 );
  128. return( rc );
  129. }
  130. int
  131. jpeg_init( Slapi_PBlock *pb )
  132. {
  133. int rc;
  134. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> jpeg_init\n", 0, 0, 0 );
  135. rc = register_bin_like_plugin( pb, &jpeg_pdesc, jpeg_names,
  136. JPEG_SYNTAX_OID );
  137. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= jpeg_init %d\n", rc, 0, 0 );
  138. return( rc );
  139. }
  140. int
  141. fax_init( Slapi_PBlock *pb )
  142. {
  143. int rc;
  144. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> fax_init\n", 0, 0, 0 );
  145. rc = register_bin_like_plugin( pb, &fax_pdesc, fax_names,
  146. FAX_SYNTAX_OID );
  147. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= fax_init %d\n", rc, 0, 0 );
  148. return( rc );
  149. }
  150. static int
  151. bin_filter_ava( Slapi_PBlock *pb, struct berval *bvfilter,
  152. Slapi_Value **bvals, int ftype, Slapi_Value **retVal )
  153. {
  154. int i;
  155. for ( i = 0; bvals[i] != NULL; i++ ) {
  156. const struct berval *bv = slapi_value_get_berval(bvals[i]);
  157. if ( ( bv->bv_len == bvfilter->bv_len ) &&
  158. ( 0 == memcmp( bv->bv_val, bvfilter->bv_val, bvfilter->bv_len ) ) )
  159. {
  160. if(retVal!=NULL)
  161. {
  162. *retVal= bvals[i];
  163. }
  164. return( 0 );
  165. }
  166. }
  167. if(retVal!=NULL)
  168. {
  169. *retVal= NULL;
  170. }
  171. return( -1 );
  172. }
  173. static int
  174. bin_values2keys( Slapi_PBlock *pb, Slapi_Value **bvals,
  175. Slapi_Value ***ivals, int ftype )
  176. {
  177. int i;
  178. if (NULL == ivals) {
  179. return 1;
  180. }
  181. *ivals = NULL;
  182. if (NULL == bvals) {
  183. return 1;
  184. }
  185. if ( ftype != LDAP_FILTER_EQUALITY ) {
  186. return( LDAP_PROTOCOL_ERROR );
  187. }
  188. for ( i = 0; bvals[i] != NULL; i++ ) {
  189. /* NULL */
  190. }
  191. (*ivals) = (Slapi_Value **) slapi_ch_malloc(( i + 1 ) *
  192. sizeof(Slapi_Value *) );
  193. for ( i = 0; bvals[i] != NULL; i++ )
  194. {
  195. (*ivals)[i] = slapi_value_dup(bvals[i]);
  196. }
  197. (*ivals)[i] = NULL;
  198. return( 0 );
  199. }
  200. static int
  201. bin_assertion2keys_ava( Slapi_PBlock *pb, Slapi_Value *bval,
  202. Slapi_Value ***ivals, int ftype )
  203. {
  204. Slapi_Value *tmpval=NULL;
  205. size_t len;
  206. if (( ftype != LDAP_FILTER_EQUALITY ) &&
  207. ( ftype != LDAP_FILTER_EQUALITY_FAST))
  208. {
  209. return( LDAP_PROTOCOL_ERROR );
  210. }
  211. if(ftype == LDAP_FILTER_EQUALITY_FAST) {
  212. /* With the fast option, we are trying to avoid creating and freeing
  213. * a bunch of structures - we just do one malloc here - see
  214. * ava_candidates in filterentry.c
  215. */
  216. len=slapi_value_get_length(bval);
  217. tmpval=(*ivals)[0];
  218. if (len > tmpval->bv.bv_len) {
  219. tmpval->bv.bv_val=(char *)slapi_ch_malloc(len);
  220. }
  221. tmpval->bv.bv_len=len;
  222. memcpy(tmpval->bv.bv_val,slapi_value_get_string(bval),len);
  223. } else {
  224. (*ivals) = (Slapi_Value **) slapi_ch_malloc( 2 * sizeof(Slapi_Value *) );
  225. (*ivals)[0] = slapi_value_dup( bval );
  226. (*ivals)[1] = NULL;
  227. }
  228. return( 0 );
  229. }