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