bitwise.c 6.1 KB

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