compare.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. /*
  42. * Copyright (c) 1995 Regents of the University of Michigan.
  43. * All rights reserved.
  44. *
  45. * Redistribution and use in source and binary forms are permitted
  46. * provided that this notice is preserved and that due credit is given
  47. * to the University of Michigan at Ann Arbor. The name of the University
  48. * may not be used to endorse or promote products derived from this
  49. * software without specific prior written permission. This software
  50. * is provided ``as is'' without express or implied warranty.
  51. */
  52. #include <stdio.h>
  53. #include <sys/types.h>
  54. #ifndef _WIN32
  55. #include <sys/socket.h>
  56. #endif
  57. #include "slap.h"
  58. #include "pratom.h"
  59. void
  60. do_compare( Slapi_PBlock *pb )
  61. {
  62. BerElement *ber = pb->pb_op->o_ber;
  63. char *rawdn = NULL;
  64. const char *dn = NULL;
  65. struct ava ava = {0};
  66. Slapi_Backend *be = NULL;
  67. int err;
  68. Slapi_DN sdn;
  69. Slapi_Entry *referral = NULL;
  70. char errorbuf[BUFSIZ];
  71. LDAPDebug( LDAP_DEBUG_TRACE, "do_compare\n", 0, 0, 0 );
  72. /* count the compare request */
  73. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsCompareOps);
  74. /* have to init this here so we can "done" it below if we short circuit */
  75. slapi_sdn_init(&sdn);
  76. /*
  77. * Parse the compare request. It looks like this:
  78. *
  79. * CompareRequest := [APPLICATION 14] SEQUENCE {
  80. * entry DistinguishedName,
  81. * ava SEQUENCE {
  82. * type AttributeType,
  83. * value AttributeValue
  84. * }
  85. * }
  86. */
  87. if ( ber_scanf( ber, "{a{ao}}", &rawdn, &ava.ava_type,
  88. &ava.ava_value ) == LBER_ERROR ) {
  89. LDAPDebug( LDAP_DEBUG_ANY,
  90. "ber_scanf failed (op=Compare; params=DN,Type,Value)\n",
  91. 0, 0, 0 );
  92. send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL, NULL, 0,
  93. NULL );
  94. goto free_and_return;
  95. }
  96. /* Check if we should be performing strict validation. */
  97. if (config_get_dn_validate_strict()) {
  98. /* check that the dn is formatted correctly */
  99. err = slapi_dn_syntax_check(pb, rawdn, 1);
  100. if (err) { /* syntax check failed */
  101. op_shared_log_error_access(pb, "CMP",
  102. rawdn?rawdn:"", "strict: invalid dn");
  103. send_ldap_result(pb, LDAP_INVALID_DN_SYNTAX,
  104. NULL, "invalid dn", 0, NULL);
  105. slapi_ch_free((void **) &rawdn);
  106. return;
  107. }
  108. }
  109. slapi_sdn_init_dn_passin(&sdn, rawdn);
  110. dn = slapi_sdn_get_dn(&sdn);
  111. if (rawdn && (strlen(rawdn) > 0) && (NULL == dn)) {
  112. /* normalization failed */
  113. op_shared_log_error_access(pb, "CMP", rawdn, "invalid dn");
  114. send_ldap_result(pb, LDAP_INVALID_DN_SYNTAX, NULL,
  115. "invalid dn", 0, NULL);
  116. slapi_sdn_done(&sdn);
  117. return;
  118. }
  119. /*
  120. * in LDAPv3 there can be optional control extensions on
  121. * the end of an LDAPMessage. we need to read them in and
  122. * pass them to the backend.
  123. */
  124. if ( (err = get_ldapmessage_controls( pb, ber, NULL )) != 0 ) {
  125. send_ldap_result( pb, err, NULL, NULL, 0, NULL );
  126. goto free_and_return;
  127. }
  128. /* target spec is used to decide which plugins are applicable for the operation */
  129. operation_set_target_spec (pb->pb_op, &sdn);
  130. LDAPDebug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s)\n",
  131. rawdn, ava.ava_type, 0 );
  132. slapi_log_access( LDAP_DEBUG_STATS,
  133. "conn=%" NSPRIu64 " op=%d CMP dn=\"%s\" attr=\"%s\"\n",
  134. pb->pb_conn->c_connid, pb->pb_op->o_opid, dn, ava.ava_type );
  135. /*
  136. * We could be serving multiple database backends. Select the
  137. * appropriate one.
  138. */
  139. if ((err = slapi_mapping_tree_select(pb, &be, &referral, errorbuf)) != LDAP_SUCCESS) {
  140. send_ldap_result(pb, err, NULL, errorbuf, 0, NULL);
  141. be = NULL;
  142. goto free_and_return;
  143. }
  144. if (referral)
  145. {
  146. int managedsait;
  147. slapi_pblock_get(pb, SLAPI_MANAGEDSAIT, &managedsait);
  148. if (managedsait)
  149. {
  150. send_ldap_result(pb, LDAP_UNWILLING_TO_PERFORM, NULL,
  151. "cannot compare referral", 0, NULL);
  152. slapi_entry_free(referral);
  153. goto free_and_return;
  154. }
  155. send_referrals_from_entry(pb,referral);
  156. slapi_entry_free(referral);
  157. goto free_and_return;
  158. }
  159. if ( be->be_compare != NULL ) {
  160. int isroot;
  161. slapi_pblock_set( pb, SLAPI_BACKEND, be );
  162. isroot = pb->pb_op->o_isroot;
  163. slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, &isroot );
  164. /* EXCEPTION: compare target does not allocate memory. */
  165. /* target never be modified by plugins. */
  166. slapi_pblock_set( pb, SLAPI_COMPARE_TARGET_SDN, (void*)&sdn );
  167. slapi_pblock_set( pb, SLAPI_COMPARE_TYPE, ava.ava_type);
  168. slapi_pblock_set( pb, SLAPI_COMPARE_VALUE, &ava.ava_value );
  169. /*
  170. * call the pre-compare plugins. if they succeed, call
  171. * the backend compare function. then call the
  172. * post-compare plugins.
  173. */
  174. if ( plugin_call_plugins( pb,
  175. SLAPI_PLUGIN_PRE_COMPARE_FN ) == 0 ) {
  176. int rc;
  177. slapi_pblock_set( pb, SLAPI_PLUGIN, be->be_database );
  178. set_db_default_result_handlers(pb);
  179. rc = (*be->be_compare)( pb );
  180. slapi_pblock_set( pb, SLAPI_PLUGIN_OPRETURN, &rc );
  181. plugin_call_plugins( pb, SLAPI_PLUGIN_POST_COMPARE_FN );
  182. }
  183. } else {
  184. send_ldap_result( pb, LDAP_UNWILLING_TO_PERFORM, NULL,
  185. "Function not implemented", 0, NULL );
  186. }
  187. free_and_return:;
  188. if (be)
  189. slapi_be_Unlock(be);
  190. slapi_sdn_done(&sdn);
  191. ava_done( &ava );
  192. }