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