compare.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* $OpenLDAP$ */
  2. /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  3. *
  4. * Copyright 1998-2024 The OpenLDAP Foundation.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted only as authorized by the OpenLDAP
  9. * Public License.
  10. *
  11. * A copy of this license is available in the file LICENSE in the
  12. * top-level directory of the distribution or, alternatively, at
  13. * <http://www.OpenLDAP.org/license.html>.
  14. */
  15. /* Portions Copyright (c) 1990 Regents of the University of Michigan.
  16. * All rights reserved.
  17. */
  18. #include "portable.h"
  19. #include <stdio.h>
  20. #include <ac/socket.h>
  21. #include <ac/string.h>
  22. #include <ac/time.h>
  23. #include "ldap-int.h"
  24. #include "ldap_log.h"
  25. /* The compare request looks like this:
  26. * CompareRequest ::= SEQUENCE {
  27. * entry DistinguishedName,
  28. * ava SEQUENCE {
  29. * type AttributeType,
  30. * value AttributeValue
  31. * }
  32. * }
  33. */
  34. BerElement *
  35. ldap_build_compare_req(
  36. LDAP *ld,
  37. LDAP_CONST char *dn,
  38. LDAP_CONST char *attr,
  39. struct berval *bvalue,
  40. LDAPControl **sctrls,
  41. LDAPControl **cctrls,
  42. int *msgidp )
  43. {
  44. BerElement *ber;
  45. int rc;
  46. /* create a message to send */
  47. if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
  48. return( NULL );
  49. }
  50. LDAP_NEXT_MSGID(ld, *msgidp);
  51. rc = ber_printf( ber, "{it{s{sON}N}", /* '}' */
  52. *msgidp,
  53. LDAP_REQ_COMPARE, dn, attr, bvalue );
  54. if ( rc == -1 )
  55. {
  56. ld->ld_errno = LDAP_ENCODING_ERROR;
  57. ber_free( ber, 1 );
  58. return( NULL );
  59. }
  60. /* Put Server Controls */
  61. if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
  62. ber_free( ber, 1 );
  63. return( NULL );
  64. }
  65. if( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
  66. ld->ld_errno = LDAP_ENCODING_ERROR;
  67. ber_free( ber, 1 );
  68. return( NULL );
  69. }
  70. return( ber );
  71. }
  72. /*
  73. * ldap_compare_ext - perform an ldap extended compare operation. The dn
  74. * of the entry to compare to and the attribute and value to compare (in
  75. * attr and value) are supplied. The msgid of the response is returned.
  76. *
  77. * Example:
  78. * struct berval bvalue = { "secret", sizeof("secret")-1 };
  79. * rc = ldap_compare( ld, "c=us@cn=bob",
  80. * "userPassword", &bvalue,
  81. * sctrl, cctrl, &msgid )
  82. */
  83. int
  84. ldap_compare_ext(
  85. LDAP *ld,
  86. LDAP_CONST char *dn,
  87. LDAP_CONST char *attr,
  88. struct berval *bvalue,
  89. LDAPControl **sctrls,
  90. LDAPControl **cctrls,
  91. int *msgidp )
  92. {
  93. int rc;
  94. BerElement *ber;
  95. ber_int_t id;
  96. Debug0( LDAP_DEBUG_TRACE, "ldap_compare\n" );
  97. assert( ld != NULL );
  98. assert( LDAP_VALID( ld ) );
  99. assert( dn != NULL );
  100. assert( attr != NULL );
  101. assert( msgidp != NULL );
  102. /* check client controls */
  103. rc = ldap_int_client_controls( ld, cctrls );
  104. if( rc != LDAP_SUCCESS ) return rc;
  105. ber = ldap_build_compare_req(
  106. ld, dn, attr, bvalue, sctrls, cctrls, &id );
  107. if( !ber )
  108. return ld->ld_errno;
  109. /* send the message */
  110. *msgidp = ldap_send_initial_request( ld, LDAP_REQ_COMPARE, dn, ber, id );
  111. return ( *msgidp < 0 ? ld->ld_errno : LDAP_SUCCESS );
  112. }
  113. /*
  114. * ldap_compare_ext - perform an ldap extended compare operation. The dn
  115. * of the entry to compare to and the attribute and value to compare (in
  116. * attr and value) are supplied. The msgid of the response is returned.
  117. *
  118. * Example:
  119. * msgid = ldap_compare( ld, "c=us@cn=bob", "userPassword", "secret" )
  120. */
  121. int
  122. ldap_compare(
  123. LDAP *ld,
  124. LDAP_CONST char *dn,
  125. LDAP_CONST char *attr,
  126. LDAP_CONST char *value )
  127. {
  128. int msgid;
  129. struct berval bvalue;
  130. assert( value != NULL );
  131. bvalue.bv_val = (char *) value;
  132. bvalue.bv_len = (value == NULL) ? 0 : strlen( value );
  133. return ldap_compare_ext( ld, dn, attr, &bvalue, NULL, NULL, &msgid ) == LDAP_SUCCESS
  134. ? msgid : -1;
  135. }
  136. int
  137. ldap_compare_ext_s(
  138. LDAP *ld,
  139. LDAP_CONST char *dn,
  140. LDAP_CONST char *attr,
  141. struct berval *bvalue,
  142. LDAPControl **sctrl,
  143. LDAPControl **cctrl )
  144. {
  145. int rc;
  146. int msgid;
  147. LDAPMessage *res;
  148. rc = ldap_compare_ext( ld, dn, attr, bvalue, sctrl, cctrl, &msgid );
  149. if ( rc != LDAP_SUCCESS )
  150. return( rc );
  151. if ( ldap_result( ld, msgid, LDAP_MSG_ALL, (struct timeval *) NULL, &res ) == -1 || !res )
  152. return( ld->ld_errno );
  153. return( ldap_result2error( ld, res, 1 ) );
  154. }
  155. int
  156. ldap_compare_s(
  157. LDAP *ld,
  158. LDAP_CONST char *dn,
  159. LDAP_CONST char *attr,
  160. LDAP_CONST char *value )
  161. {
  162. struct berval bvalue;
  163. assert( value != NULL );
  164. bvalue.bv_val = (char *) value;
  165. bvalue.bv_len = (value == NULL) ? 0 : strlen( value );
  166. return ldap_compare_ext_s( ld, dn, attr, &bvalue, NULL, NULL );
  167. }