compare.c revision 1.1.1.6
155430Srwatson/* $NetBSD: compare.c,v 1.1.1.6 2018/02/06 01:53:08 christos Exp $ */ 2115803Srwatson 355430Srwatson/* $OpenLDAP$ */ 455430Srwatson/* This work is part of OpenLDAP Software <http://www.openldap.org/>. 555430Srwatson * 655430Srwatson * Copyright 1998-2017 The OpenLDAP Foundation. 755430Srwatson * All rights reserved. 855430Srwatson * 955430Srwatson * Redistribution and use in source and binary forms, with or without 1055430Srwatson * modification, are permitted only as authorized by the OpenLDAP 1155430Srwatson * Public License. 1255430Srwatson * 1355430Srwatson * A copy of this license is available in the file LICENSE in the 1455430Srwatson * top-level directory of the distribution or, alternatively, at 1555430Srwatson * <http://www.OpenLDAP.org/license.html>. 1655430Srwatson */ 1755430Srwatson/* Portions Copyright (c) 1990 Regents of the University of Michigan. 1855430Srwatson * All rights reserved. 1955430Srwatson */ 2055430Srwatson 2155430Srwatson#include <sys/cdefs.h> 2255430Srwatson__RCSID("$NetBSD: compare.c,v 1.1.1.6 2018/02/06 01:53:08 christos Exp $"); 2355430Srwatson 2455430Srwatson#include "portable.h" 2555430Srwatson 2674277Srwatson#include <stdio.h> 2755430Srwatson 2855430Srwatson#include <ac/socket.h> 29206622Suqs#include <ac/string.h> 3055430Srwatson#include <ac/time.h> 3155430Srwatson 3279727Sschweikh#include "ldap-int.h" 33107788Sru#include "ldap_log.h" 3455430Srwatson 3584306Sru/* The compare request looks like this: 3684306Sru * CompareRequest ::= SEQUENCE { 3784306Sru * entry DistinguishedName, 3855430Srwatson * ava SEQUENCE { 3955430Srwatson * type AttributeType, 4074277Srwatson * value AttributeValue 4174277Srwatson * } 4274277Srwatson * } 4374277Srwatson */ 4474277Srwatson 4574277SrwatsonBerElement * 4674277Srwatsonldap_build_compare_req( 4774277Srwatson LDAP *ld, 4874277Srwatson LDAP_CONST char *dn, 4974277Srwatson LDAP_CONST char *attr, 50107788Sru struct berval *bvalue, 5174355Srwatson LDAPControl **sctrls, 5274355Srwatson LDAPControl **cctrls, 5374355Srwatson int *msgidp ) 5474277Srwatson{ 5578686Sdd BerElement *ber; 5674277Srwatson int rc; 5774277Srwatson 5874277Srwatson /* create a message to send */ 5974349Sru if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) { 6074277Srwatson return( NULL ); 6174277Srwatson } 6255430Srwatson 6355430Srwatson LDAP_NEXT_MSGID(ld, *msgidp); 6474277Srwatson rc = ber_printf( ber, "{it{s{sON}N}", /* '}' */ 65107788Sru *msgidp, 6674277Srwatson LDAP_REQ_COMPARE, dn, attr, bvalue ); 6774277Srwatson if ( rc == -1 ) 68107788Sru { 6974277Srwatson ld->ld_errno = LDAP_ENCODING_ERROR; 7074277Srwatson ber_free( ber, 1 ); 7174277Srwatson return( NULL ); 7270466Sru } 7355430Srwatson 74107788Sru /* Put Server Controls */ 75121385Shmp if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) { 76121385Shmp ber_free( ber, 1 ); 77115876Srwatson return( NULL ); 78115876Srwatson } 7955430Srwatson 8055430Srwatson if( ber_printf( ber, /*{*/ "N}" ) == -1 ) { 8155430Srwatson ld->ld_errno = LDAP_ENCODING_ERROR; 8255430Srwatson ber_free( ber, 1 ); 8374277Srwatson return( NULL ); 8455430Srwatson } 85115876Srwatson 8668753Sben return( ber ); 8755430Srwatson} 88147647Shmp 8955430Srwatson/* 9074277Srwatson * ldap_compare_ext - perform an ldap extended compare operation. The dn 9174277Srwatson * of the entry to compare to and the attribute and value to compare (in 9274277Srwatson * attr and value) are supplied. The msgid of the response is returned. 9374355Srwatson * 9474355Srwatson * Example: 9574355Srwatson * struct berval bvalue = { "secret", sizeof("secret")-1 }; 9674355Srwatson * rc = ldap_compare( ld, "c=us@cn=bob", 97 * "userPassword", &bvalue, 98 * sctrl, cctrl, &msgid ) 99 */ 100int 101ldap_compare_ext( 102 LDAP *ld, 103 LDAP_CONST char *dn, 104 LDAP_CONST char *attr, 105 struct berval *bvalue, 106 LDAPControl **sctrls, 107 LDAPControl **cctrls, 108 int *msgidp ) 109{ 110 int rc; 111 BerElement *ber; 112 ber_int_t id; 113 114 Debug( LDAP_DEBUG_TRACE, "ldap_compare\n", 0, 0, 0 ); 115 116 assert( ld != NULL ); 117 assert( LDAP_VALID( ld ) ); 118 assert( dn != NULL ); 119 assert( attr != NULL ); 120 assert( msgidp != NULL ); 121 122 /* check client controls */ 123 rc = ldap_int_client_controls( ld, cctrls ); 124 if( rc != LDAP_SUCCESS ) return rc; 125 126 ber = ldap_build_compare_req( 127 ld, dn, attr, bvalue, sctrls, cctrls, &id ); 128 if( !ber ) 129 return ld->ld_errno; 130 131 /* send the message */ 132 *msgidp = ldap_send_initial_request( ld, LDAP_REQ_COMPARE, dn, ber, id ); 133 return ( *msgidp < 0 ? ld->ld_errno : LDAP_SUCCESS ); 134} 135 136/* 137 * ldap_compare_ext - perform an ldap extended compare operation. The dn 138 * of the entry to compare to and the attribute and value to compare (in 139 * attr and value) are supplied. The msgid of the response is returned. 140 * 141 * Example: 142 * msgid = ldap_compare( ld, "c=us@cn=bob", "userPassword", "secret" ) 143 */ 144int 145ldap_compare( 146 LDAP *ld, 147 LDAP_CONST char *dn, 148 LDAP_CONST char *attr, 149 LDAP_CONST char *value ) 150{ 151 int msgid; 152 struct berval bvalue; 153 154 assert( value != NULL ); 155 156 bvalue.bv_val = (char *) value; 157 bvalue.bv_len = (value == NULL) ? 0 : strlen( value ); 158 159 return ldap_compare_ext( ld, dn, attr, &bvalue, NULL, NULL, &msgid ) == LDAP_SUCCESS 160 ? msgid : -1; 161} 162 163int 164ldap_compare_ext_s( 165 LDAP *ld, 166 LDAP_CONST char *dn, 167 LDAP_CONST char *attr, 168 struct berval *bvalue, 169 LDAPControl **sctrl, 170 LDAPControl **cctrl ) 171{ 172 int rc; 173 int msgid; 174 LDAPMessage *res; 175 176 rc = ldap_compare_ext( ld, dn, attr, bvalue, sctrl, cctrl, &msgid ); 177 178 if ( rc != LDAP_SUCCESS ) 179 return( rc ); 180 181 if ( ldap_result( ld, msgid, LDAP_MSG_ALL, (struct timeval *) NULL, &res ) == -1 || !res ) 182 return( ld->ld_errno ); 183 184 return( ldap_result2error( ld, res, 1 ) ); 185} 186 187int 188ldap_compare_s( 189 LDAP *ld, 190 LDAP_CONST char *dn, 191 LDAP_CONST char *attr, 192 LDAP_CONST char *value ) 193{ 194 struct berval bvalue; 195 196 assert( value != NULL ); 197 198 bvalue.bv_val = (char *) value; 199 bvalue.bv_len = (value == NULL) ? 0 : strlen( value ); 200 201 return ldap_compare_ext_s( ld, dn, attr, &bvalue, NULL, NULL ); 202} 203