1
0

compare.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 *dn = NULL;
  64. struct ava ava = {0};
  65. Slapi_Backend *be = NULL;
  66. int err;
  67. char ebuf[ BUFSIZ ];
  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. snmp_increment_counter(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}}", &dn, &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. /*
  97. * in LDAPv3 there can be optional control extensions on
  98. * the end of an LDAPMessage. we need to read them in and
  99. * pass them to the backend.
  100. */
  101. if ( (err = get_ldapmessage_controls( pb, ber, NULL )) != 0 ) {
  102. send_ldap_result( pb, err, NULL, NULL, 0, NULL );
  103. goto free_and_return;
  104. }
  105. slapi_sdn_init_dn_passin(&sdn,dn);
  106. dn = NULL; /* do not free - sdn owns it now */
  107. /* target spec is used to decide which plugins are applicable for the operation */
  108. operation_set_target_spec (pb->pb_op, &sdn);
  109. LDAPDebug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s)\n",
  110. dn, ava.ava_type, 0 );
  111. slapi_log_access( LDAP_DEBUG_STATS,
  112. "conn=%d op=%d CMP dn=\"%s\" attr=\"%s\"\n",
  113. pb->pb_conn->c_connid, pb->pb_op->o_opid,
  114. escape_string( dn, ebuf ), ava.ava_type );
  115. /*
  116. * We could be serving multiple database backends. Select the
  117. * appropriate one.
  118. */
  119. if ((err = slapi_mapping_tree_select(pb, &be, &referral, errorbuf)) != LDAP_SUCCESS) {
  120. send_ldap_result(pb, err, NULL, errorbuf, 0, NULL);
  121. be = NULL;
  122. goto free_and_return;
  123. }
  124. if (referral)
  125. {
  126. int managedsait;
  127. slapi_pblock_get(pb, SLAPI_MANAGEDSAIT, &managedsait);
  128. if (managedsait)
  129. {
  130. send_ldap_result(pb, LDAP_UNWILLING_TO_PERFORM, NULL,
  131. "cannot compare referral", 0, NULL);
  132. slapi_entry_free(referral);
  133. goto free_and_return;
  134. }
  135. send_referrals_from_entry(pb,referral);
  136. slapi_entry_free(referral);
  137. goto free_and_return;
  138. }
  139. if ( be->be_compare != NULL ) {
  140. int isroot;
  141. slapi_pblock_set( pb, SLAPI_BACKEND, be );
  142. isroot = pb->pb_op->o_isroot;
  143. slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, &isroot );
  144. slapi_pblock_set( pb, SLAPI_COMPARE_TARGET, (void*)slapi_sdn_get_ndn(&sdn) );
  145. slapi_pblock_set( pb, SLAPI_COMPARE_TYPE, ava.ava_type);
  146. slapi_pblock_set( pb, SLAPI_COMPARE_VALUE, &ava.ava_value );
  147. /*
  148. * call the pre-compare plugins. if they succeed, call
  149. * the backend compare function. then call the
  150. * post-compare plugins.
  151. */
  152. if ( plugin_call_plugins( pb,
  153. SLAPI_PLUGIN_PRE_COMPARE_FN ) == 0 ) {
  154. int rc;
  155. slapi_pblock_set( pb, SLAPI_PLUGIN, be->be_database );
  156. set_db_default_result_handlers(pb);
  157. rc = (*be->be_compare)( pb );
  158. slapi_pblock_set( pb, SLAPI_PLUGIN_OPRETURN, &rc );
  159. plugin_call_plugins( pb, SLAPI_PLUGIN_POST_COMPARE_FN );
  160. }
  161. } else {
  162. send_ldap_result( pb, LDAP_UNWILLING_TO_PERFORM, NULL,
  163. "Function not implemented", 0, NULL );
  164. }
  165. free_and_return:;
  166. if (be)
  167. slapi_be_Unlock(be);
  168. slapi_sdn_done(&sdn);
  169. slapi_ch_free_string(&dn);
  170. ava_done( &ava );
  171. }