]> git.sur5r.net Git - openldap/blob - libraries/libldap/compare.c
Fix ldap_send_initial_request() to open connection if not already
[openldap] / libraries / libldap / compare.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 /*  Portions
6  *  Copyright (c) 1990 Regents of the University of Michigan.
7  *  All rights reserved.
8  *
9  *  compare.c
10  */
11
12 #include "portable.h"
13
14 #include <stdio.h>
15
16 #include <ac/socket.h>
17 #include <ac/string.h>
18 #include <ac/time.h>
19
20 #include "ldap-int.h"
21
22 /*
23  * ldap_compare - perform an ldap (and X.500) compare operation.  The dn
24  * of the entry to compare to and the attribute and value to compare (in
25  * attr and value) are supplied.  The msgid of the response is returned.
26  *
27  * Example:
28  *      ldap_compare( ld, "c=us@cn=bob", "userPassword", "secret" )
29  */
30 int
31 ldap_compare( LDAP *ld, char *dn, char *attr, char *value )
32 {
33         BerElement      *ber;
34
35         /* The compare request looks like this:
36          *      CompareRequest ::= SEQUENCE {
37          *              entry   DistinguishedName,
38          *              ava     SEQUENCE {
39          *                      type    AttributeType,
40          *                      value   AttributeValue
41          *              }
42          *      }
43          * and must be wrapped in an LDAPMessage.
44          */
45
46         Debug( LDAP_DEBUG_TRACE, "ldap_compare\n", 0, 0, 0 );
47
48         /* create a message to send */
49         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
50                 return( -1 );
51         }
52
53         if ( ber_printf( ber, "{it{s{ss}}}", ++ld->ld_msgid, LDAP_REQ_COMPARE,
54             dn, attr, value ) == -1 ) {
55                 ld->ld_errno = LDAP_ENCODING_ERROR;
56                 ber_free( ber, 1 );
57                 return( -1 );
58         }
59
60 #ifndef LDAP_NOCACHE
61         if ( ld->ld_cache != NULL ) {
62                 if ( ldap_check_cache( ld, LDAP_REQ_COMPARE, ber ) == 0 ) {
63                         ber_free( ber, 1 );
64                         ld->ld_errno = LDAP_SUCCESS;
65                         return( ld->ld_msgid );
66                 }
67                 ldap_add_request_to_cache( ld, LDAP_REQ_COMPARE, ber );
68         }
69 #endif /* LDAP_NOCACHE */
70
71         /* send the message */
72         return ( ldap_send_initial_request( ld, LDAP_REQ_COMPARE, dn, ber ));
73 }
74
75 int
76 ldap_compare_s( LDAP *ld, char *dn, char *attr, char *value )
77 {
78         int             msgid;
79         LDAPMessage     *res;
80
81         if ( (msgid = ldap_compare( ld, dn, attr, value )) == -1 )
82                 return( ld->ld_errno );
83
84         if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
85                 return( ld->ld_errno );
86
87         return( ldap_result2error( ld, res, 1 ) );
88 }