bitwise.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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) 2007 Red Hat, Inc.
  35. * All rights reserved.
  36. * END COPYRIGHT BLOCK **/
  37. #ifdef HAVE_CONFIG_H
  38. # include <config.h>
  39. #endif
  40. /* orfilter.c - implementation of ordering rule filter */
  41. #include <ldap.h> /* LDAP_UTF8INC */
  42. #include <slap.h> /* for debug macros */
  43. #include <slapi-plugin.h> /* slapi_berval_cmp, SLAPI_BERVAL_EQ */
  44. #ifdef HPUX11
  45. #include <dl.h>
  46. #endif /* HPUX11 */
  47. /* the match function needs the attribute type and value from the search
  48. filter - this is unfortunately not passed into the match fn, so we
  49. have to keep track of this
  50. */
  51. struct bitwise_match_cb {
  52. char *type; /* the attribute type from the filter ava */
  53. struct berval *val; /* the value from the filter ava */
  54. };
  55. /*
  56. The type and val pointers are assumed to have sufficient lifetime -
  57. we don't have to copy them - they are usually just pointers into
  58. the SLAPI_PLUGIN_MR_TYPE and SLAPI_PLUGIN_MR_VALUE fields of the
  59. operation pblock, whose lifetime should encompass the creation
  60. and destruction of the bitwise_match_cb object.
  61. */
  62. static struct bitwise_match_cb *
  63. new_bitwise_match_cb(char *type, struct berval *val)
  64. {
  65. struct bitwise_match_cb *bmc = (struct bitwise_match_cb *)slapi_ch_calloc(1, sizeof(struct bitwise_match_cb));
  66. bmc->type = slapi_ch_strdup(type);
  67. bmc->val = val;
  68. return bmc;
  69. }
  70. static void
  71. delete_bitwise_match_cb(struct bitwise_match_cb *bmc)
  72. {
  73. slapi_ch_free_string(&bmc->type);
  74. slapi_ch_free((void **)&bmc);
  75. }
  76. static void
  77. bitwise_filter_destroy(Slapi_PBlock* pb)
  78. {
  79. void *obj = NULL;
  80. slapi_pblock_get(pb, SLAPI_PLUGIN_OBJECT, &obj);
  81. if (obj) {
  82. struct bitwise_match_cb *bmc = (struct bitwise_match_cb *)obj;
  83. delete_bitwise_match_cb(bmc);
  84. obj = NULL;
  85. slapi_pblock_set(pb, SLAPI_PLUGIN_OBJECT, obj);
  86. }
  87. }
  88. #define BITWISE_OP_AND 0
  89. #define BITWISE_OP_OR 1
  90. static int
  91. internal_bitwise_filter_match(void* obj, Slapi_Entry* entry, Slapi_Attr* attr, int op)
  92. /* returns: 0 filter matched
  93. * -1 filter did not match
  94. * >0 an LDAP error code
  95. */
  96. {
  97. struct bitwise_match_cb *bmc = obj;
  98. auto int rc = -1; /* no match */
  99. char **ary = NULL;
  100. int ii;
  101. ary = slapi_entry_attr_get_charray(entry, bmc->type);
  102. /* look through all values until we find a match */
  103. for (ii = 0; (rc == -1) && ary && ary[ii]; ++ii) {
  104. unsigned long long a, b;
  105. char *val_from_entry = ary[ii];
  106. errno = 0;
  107. a = strtoull(val_from_entry, NULL, 10);
  108. if (errno != ERANGE) {
  109. errno = 0;
  110. b = strtoull(bmc->val->bv_val, NULL, 10);
  111. if (errno == ERANGE) {
  112. rc = LDAP_CONSTRAINT_VIOLATION;
  113. } else {
  114. int result = 0;
  115. /* The Microsoft Windows AD bitwise operators do not work exactly
  116. as the plain old C bitwise operators work. For the AND case
  117. the matching rule is true only if all bits from the given value
  118. match the value from the entry. For the OR case, the matching
  119. rule is true if any bits from the given value match the value
  120. from the entry.
  121. For the AND case, this means that even though (a & b) is True,
  122. if (a & b) != b, the matching rule will return False.
  123. For the OR case, this means that even though (a | b) is True,
  124. this may be because there are bits in a. But we only care
  125. about bits in a that are also in b. So we do (a & b) - this
  126. will return what we want, which is to return True if any of
  127. the bits in b are also in a.
  128. */
  129. if (op == BITWISE_OP_AND) {
  130. result = ((a & b) == b); /* all the bits in the given value are found in the value from the entry */
  131. } else if (op == BITWISE_OP_OR) {
  132. result = (a & b); /* any of the bits in b are also in a */
  133. }
  134. if (result) {
  135. rc = 0;
  136. }
  137. }
  138. }
  139. }
  140. slapi_ch_array_free(ary);
  141. return rc;
  142. }
  143. static int
  144. bitwise_filter_match_and (void* obj, Slapi_Entry* entry, Slapi_Attr* attr)
  145. /* returns: 0 filter matched
  146. * -1 filter did not match
  147. * >0 an LDAP error code
  148. */
  149. {
  150. return internal_bitwise_filter_match(obj, entry, attr, BITWISE_OP_AND);
  151. }
  152. static int
  153. bitwise_filter_match_or (void* obj, Slapi_Entry* entry, Slapi_Attr* attr)
  154. /* returns: 0 filter matched
  155. * -1 filter did not match
  156. * >0 an LDAP error code
  157. */
  158. {
  159. return internal_bitwise_filter_match(obj, entry, attr, BITWISE_OP_OR);
  160. }
  161. static int
  162. bitwise_filter_create (Slapi_PBlock* pb)
  163. {
  164. auto int rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION; /* failed to initialize */
  165. auto char* mrOID = NULL;
  166. auto char* mrTYPE = NULL;
  167. auto struct berval* mrVALUE = NULL;
  168. if (!slapi_pblock_get (pb, SLAPI_PLUGIN_MR_OID, &mrOID) && mrOID != NULL &&
  169. !slapi_pblock_get (pb, SLAPI_PLUGIN_MR_TYPE, &mrTYPE) && mrTYPE != NULL &&
  170. !slapi_pblock_get (pb, SLAPI_PLUGIN_MR_VALUE, &mrVALUE) && mrVALUE != NULL) {
  171. struct bitwise_match_cb *bmc = NULL;
  172. if (strcmp(mrOID, "1.2.840.113556.1.4.803") == 0) {
  173. slapi_pblock_set (pb, SLAPI_PLUGIN_MR_FILTER_MATCH_FN, (void*)bitwise_filter_match_and);
  174. } else if (strcmp(mrOID, "1.2.840.113556.1.4.804") == 0) {
  175. slapi_pblock_set (pb, SLAPI_PLUGIN_MR_FILTER_MATCH_FN, (void*)bitwise_filter_match_or);
  176. } else { /* this oid not handled by this plugin */
  177. LDAPDebug (LDAP_DEBUG_FILTER, "=> bitwise_filter_create OID (%s) not handled\n", mrOID, 0, 0);
  178. return rc;
  179. }
  180. bmc = new_bitwise_match_cb(mrTYPE, mrVALUE);
  181. slapi_pblock_set (pb, SLAPI_PLUGIN_OBJECT, bmc);
  182. slapi_pblock_set (pb, SLAPI_PLUGIN_DESTROY_FN, (void*)bitwise_filter_destroy);
  183. rc = LDAP_SUCCESS;
  184. } else {
  185. LDAPDebug (LDAP_DEBUG_FILTER, "=> bitwise_filter_create missing parameter(s)\n", 0, 0, 0);
  186. }
  187. LDAPDebug (LDAP_DEBUG_FILTER, "<= bitwise_filter_create %i\n", rc, 0, 0);
  188. return LDAP_SUCCESS;
  189. }
  190. static Slapi_PluginDesc pdesc = { "bitwise", VENDOR, DS_PACKAGE_VERSION,
  191. "bitwise match plugin" };
  192. int /* LDAP error code */
  193. bitwise_init (Slapi_PBlock* pb)
  194. {
  195. int rc;
  196. rc = slapi_pblock_set (pb, SLAPI_PLUGIN_MR_FILTER_CREATE_FN, (void*)bitwise_filter_create);
  197. if ( rc == 0 ) {
  198. rc = slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION, (void *)&pdesc );
  199. }
  200. LDAPDebug (LDAP_DEBUG_FILTER, "bitwise_init %i\n", rc, 0, 0);
  201. return rc;
  202. }