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