compare.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /*
  13. * Copyright (c) 1995 Regents of the University of Michigan.
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms are permitted
  17. * provided that this notice is preserved and that due credit is given
  18. * to the University of Michigan at Ann Arbor. The name of the University
  19. * may not be used to endorse or promote products derived from this
  20. * software without specific prior written permission. This software
  21. * is provided ``as is'' without express or implied warranty.
  22. */
  23. #include <stdio.h>
  24. #include <sys/types.h>
  25. #include <sys/socket.h>
  26. #include "slap.h"
  27. #include "pratom.h"
  28. void
  29. do_compare( Slapi_PBlock *pb )
  30. {
  31. BerElement *ber = pb->pb_op->o_ber;
  32. char *rawdn = NULL;
  33. const char *dn = NULL;
  34. struct ava ava = {0};
  35. Slapi_Backend *be = NULL;
  36. int err;
  37. Slapi_DN sdn;
  38. Slapi_Entry *referral = NULL;
  39. char errorbuf[BUFSIZ];
  40. LDAPDebug( LDAP_DEBUG_TRACE, "do_compare\n", 0, 0, 0 );
  41. /* count the compare request */
  42. slapi_counter_increment(g_get_global_snmp_vars()->ops_tbl.dsCompareOps);
  43. /* have to init this here so we can "done" it below if we short circuit */
  44. slapi_sdn_init(&sdn);
  45. /*
  46. * Parse the compare request. It looks like this:
  47. *
  48. * CompareRequest := [APPLICATION 14] SEQUENCE {
  49. * entry DistinguishedName,
  50. * ava SEQUENCE {
  51. * type AttributeType,
  52. * value AttributeValue
  53. * }
  54. * }
  55. */
  56. if ( ber_scanf( ber, "{a{ao}}", &rawdn, &ava.ava_type,
  57. &ava.ava_value ) == LBER_ERROR ) {
  58. LDAPDebug( LDAP_DEBUG_ANY,
  59. "ber_scanf failed (op=Compare; params=DN,Type,Value)\n",
  60. 0, 0, 0 );
  61. send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL, NULL, 0,
  62. NULL );
  63. goto free_and_return;
  64. }
  65. /* Check if we should be performing strict validation. */
  66. if (config_get_dn_validate_strict()) {
  67. /* check that the dn is formatted correctly */
  68. err = slapi_dn_syntax_check(pb, rawdn, 1);
  69. if (err) { /* syntax check failed */
  70. op_shared_log_error_access(pb, "CMP",
  71. rawdn?rawdn:"", "strict: invalid dn");
  72. send_ldap_result(pb, LDAP_INVALID_DN_SYNTAX,
  73. NULL, "invalid dn", 0, NULL);
  74. slapi_ch_free((void **) &rawdn);
  75. return;
  76. }
  77. }
  78. slapi_sdn_init_dn_passin(&sdn, rawdn);
  79. dn = slapi_sdn_get_dn(&sdn);
  80. if (rawdn && (strlen(rawdn) > 0) && (NULL == dn)) {
  81. /* normalization failed */
  82. op_shared_log_error_access(pb, "CMP", rawdn, "invalid dn");
  83. send_ldap_result(pb, LDAP_INVALID_DN_SYNTAX, NULL,
  84. "invalid dn", 0, NULL);
  85. slapi_sdn_done(&sdn);
  86. return;
  87. }
  88. /*
  89. * in LDAPv3 there can be optional control extensions on
  90. * the end of an LDAPMessage. we need to read them in and
  91. * pass them to the backend.
  92. */
  93. if ( (err = get_ldapmessage_controls( pb, ber, NULL )) != 0 ) {
  94. send_ldap_result( pb, err, NULL, NULL, 0, NULL );
  95. goto free_and_return;
  96. }
  97. /* target spec is used to decide which plugins are applicable for the operation */
  98. operation_set_target_spec (pb->pb_op, &sdn);
  99. LDAPDebug( LDAP_DEBUG_ARGS, "do_compare: dn (%s) attr (%s)\n",
  100. rawdn, ava.ava_type, 0 );
  101. slapi_log_access( LDAP_DEBUG_STATS,
  102. "conn=%" NSPRIu64 " op=%d CMP dn=\"%s\" attr=\"%s\"\n",
  103. pb->pb_conn->c_connid, pb->pb_op->o_opid, dn, ava.ava_type );
  104. /*
  105. * We could be serving multiple database backends. Select the
  106. * appropriate one.
  107. */
  108. if ((err = slapi_mapping_tree_select(pb, &be, &referral, errorbuf)) != LDAP_SUCCESS) {
  109. send_ldap_result(pb, err, NULL, errorbuf, 0, NULL);
  110. be = NULL;
  111. goto free_and_return;
  112. }
  113. if (referral)
  114. {
  115. int managedsait;
  116. slapi_pblock_get(pb, SLAPI_MANAGEDSAIT, &managedsait);
  117. if (managedsait)
  118. {
  119. send_ldap_result(pb, LDAP_UNWILLING_TO_PERFORM, NULL,
  120. "cannot compare referral", 0, NULL);
  121. slapi_entry_free(referral);
  122. goto free_and_return;
  123. }
  124. send_referrals_from_entry(pb,referral);
  125. slapi_entry_free(referral);
  126. goto free_and_return;
  127. }
  128. if ( be->be_compare != NULL ) {
  129. int isroot;
  130. slapi_pblock_set( pb, SLAPI_BACKEND, be );
  131. isroot = pb->pb_op->o_isroot;
  132. slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, &isroot );
  133. /* EXCEPTION: compare target does not allocate memory. */
  134. /* target never be modified by plugins. */
  135. slapi_pblock_set( pb, SLAPI_COMPARE_TARGET_SDN, (void*)&sdn );
  136. slapi_pblock_set( pb, SLAPI_COMPARE_TYPE, ava.ava_type);
  137. slapi_pblock_set( pb, SLAPI_COMPARE_VALUE, &ava.ava_value );
  138. /*
  139. * call the pre-compare plugins. if they succeed, call
  140. * the backend compare function. then call the
  141. * post-compare plugins.
  142. */
  143. if ( plugin_call_plugins( pb,
  144. SLAPI_PLUGIN_PRE_COMPARE_FN ) == 0 ) {
  145. int rc;
  146. slapi_pblock_set( pb, SLAPI_PLUGIN, be->be_database );
  147. set_db_default_result_handlers(pb);
  148. rc = (*be->be_compare)( pb );
  149. slapi_pblock_set( pb, SLAPI_PLUGIN_OPRETURN, &rc );
  150. plugin_call_plugins( pb, SLAPI_PLUGIN_POST_COMPARE_FN );
  151. }
  152. } else {
  153. send_ldap_result( pb, LDAP_UNWILLING_TO_PERFORM, NULL,
  154. "Function not implemented", 0, NULL );
  155. }
  156. free_and_return:;
  157. if (be)
  158. slapi_be_Unlock(be);
  159. slapi_sdn_done(&sdn);
  160. ava_done( &ava );
  161. }