1 /* $OpenLDAP: pkg/ldap/libraries/libldap/compare.c,v 1.29.2.3 2008/02/11 23:26:41 kurt Exp $ */ 2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>. 3 * 4 * Copyright 1998-2008 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 19 #include "portable.h" 20 21 #include <stdio.h> 22 23 #include <ac/socket.h> 24 #include <ac/string.h> 25 #include <ac/time.h> 26 27 #include "ldap-int.h" 28 #include "ldap_log.h" 29 30 /* The compare request looks like this: 31 * CompareRequest ::= SEQUENCE { 32 * entry DistinguishedName, 33 * ava SEQUENCE { 34 * type AttributeType, 35 * value AttributeValue 36 * } 37 * } 38 */ 39 40 /* 41 * ldap_compare_ext - perform an ldap extended compare operation. The dn 42 * of the entry to compare to and the attribute and value to compare (in 43 * attr and value) are supplied. The msgid of the response is returned. 44 * 45 * Example: 46 * struct berval bvalue = { "secret", sizeof("secret")-1 }; 47 * rc = ldap_compare( ld, "c=us@cn=bob", 48 * "userPassword", &bvalue, 49 * sctrl, cctrl, &msgid ) 50 */ 51 int 52 ldap_compare_ext( 53 LDAP *ld, 54 LDAP_CONST char *dn, 55 LDAP_CONST char *attr, 56 struct berval *bvalue, 57 LDAPControl **sctrls, 58 LDAPControl **cctrls, 59 int *msgidp ) 60 { 61 int rc; 62 BerElement *ber; 63 ber_int_t id; 64 65 Debug( LDAP_DEBUG_TRACE, "ldap_compare\n", 0, 0, 0 ); 66 67 assert( ld != NULL ); 68 assert( LDAP_VALID( ld ) ); 69 assert( dn != NULL ); 70 assert( attr != NULL ); 71 assert( msgidp != NULL ); 72 73 /* check client controls */ 74 rc = ldap_int_client_controls( ld, cctrls ); 75 if( rc != LDAP_SUCCESS ) return rc; 76 77 /* create a message to send */ 78 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) { 79 return( LDAP_NO_MEMORY ); 80 } 81 82 LDAP_NEXT_MSGID(ld, id); 83 rc = ber_printf( ber, "{it{s{sON}N}", /* '}' */ 84 id, 85 LDAP_REQ_COMPARE, dn, attr, bvalue ); 86 if ( rc == -1 ) 87 { 88 ld->ld_errno = LDAP_ENCODING_ERROR; 89 ber_free( ber, 1 ); 90 return( ld->ld_errno ); 91 } 92 93 /* Put Server Controls */ 94 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) { 95 ber_free( ber, 1 ); 96 return ld->ld_errno; 97 } 98 99 if( ber_printf( ber, /*{*/ "N}" ) == -1 ) { 100 ld->ld_errno = LDAP_ENCODING_ERROR; 101 ber_free( ber, 1 ); 102 return( ld->ld_errno ); 103 } 104 105 106 /* send the message */ 107 *msgidp = ldap_send_initial_request( ld, LDAP_REQ_COMPARE, dn, ber, id ); 108 return ( *msgidp < 0 ? ld->ld_errno : LDAP_SUCCESS ); 109 } 110 111 /* 112 * ldap_compare_ext - perform an ldap extended compare operation. The dn 113 * of the entry to compare to and the attribute and value to compare (in 114 * attr and value) are supplied. The msgid of the response is returned. 115 * 116 * Example: 117 * msgid = ldap_compare( ld, "c=us@cn=bob", "userPassword", "secret" ) 118 */ 119 int 120 ldap_compare( 121 LDAP *ld, 122 LDAP_CONST char *dn, 123 LDAP_CONST char *attr, 124 LDAP_CONST char *value ) 125 { 126 int msgid; 127 struct berval bvalue; 128 129 assert( value != NULL ); 130 131 bvalue.bv_val = (char *) value; 132 bvalue.bv_len = (value == NULL) ? 0 : strlen( value ); 133 134 return ldap_compare_ext( ld, dn, attr, &bvalue, NULL, NULL, &msgid ) == LDAP_SUCCESS 135 ? msgid : -1; 136 } 137 138 int 139 ldap_compare_ext_s( 140 LDAP *ld, 141 LDAP_CONST char *dn, 142 LDAP_CONST char *attr, 143 struct berval *bvalue, 144 LDAPControl **sctrl, 145 LDAPControl **cctrl ) 146 { 147 int rc; 148 int msgid; 149 LDAPMessage *res; 150 151 rc = ldap_compare_ext( ld, dn, attr, bvalue, sctrl, cctrl, &msgid ); 152 153 if ( rc != LDAP_SUCCESS ) 154 return( rc ); 155 156 if ( ldap_result( ld, msgid, LDAP_MSG_ALL, (struct timeval *) NULL, &res ) == -1 || !res ) 157 return( ld->ld_errno ); 158 159 return( ldap_result2error( ld, res, 1 ) ); 160 } 161 162 int 163 ldap_compare_s( 164 LDAP *ld, 165 LDAP_CONST char *dn, 166 LDAP_CONST char *attr, 167 LDAP_CONST char *value ) 168 { 169 struct berval bvalue; 170 171 assert( value != NULL ); 172 173 bvalue.bv_val = (char *) value; 174 bvalue.bv_len = (value == NULL) ? 0 : strlen( value ); 175 176 return ldap_compare_ext_s( ld, dn, attr, &bvalue, NULL, NULL ); 177 } 178