bin.c 7.5 KB

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