]> git.sur5r.net Git - openldap/blob - libraries/libldap/compare.c
489905b9703270097c505c523351b369563b5069
[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_ext - perform an ldap extended 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  *      struct berval bvalue = { "secret", strlen("secret") };
29  *      rc = ldap_compare( ld, "c=us@cn=bob",
30  *              "userPassword", &bvalue,
31  *              sctrl, cctrl, &msgid )
32  */
33 int
34 ldap_compare_ext(
35         LDAP *ld,
36         LDAP_CONST char *dn,
37         LDAP_CONST char *attr,
38         struct berval *bvalue,
39         LDAPControl **sctrls,
40         LDAPControl **cctrls,
41         int     *msgidp )
42 {
43         BerElement      *ber;
44
45         /* The compare request looks like this:
46          *      CompareRequest ::= SEQUENCE {
47          *              entry   DistinguishedName,
48          *              ava     SEQUENCE {
49          *                      type    AttributeType,
50          *                      value   AttributeValue
51          *              }
52          *      }
53          * and must be wrapped in an LDAPMessage.
54          */
55
56         Debug( LDAP_DEBUG_TRACE, "ldap_compare\n", 0, 0, 0 );
57
58         /* create a message to send */
59         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
60                 return( LDAP_NO_MEMORY );
61         }
62
63         if ( ber_printf( ber, "{it{s{sO}}", /* leave open '}' */
64                 ++ld->ld_msgid,
65                 LDAP_REQ_COMPARE, dn, attr, &bvalue ) == -1 )
66         {
67                 ld->ld_errno = LDAP_ENCODING_ERROR;
68                 ber_free( ber, 1 );
69                 return( ld->ld_errno );
70         }
71
72         /* Put Server Controls */
73         if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
74                 ber_free( ber, 1 );
75                 return ld->ld_errno;
76         }
77
78         if( ber_printf( ber, "}" ) == -1 ) {
79                 ld->ld_errno = LDAP_ENCODING_ERROR;
80                 ber_free( ber, 1 );
81                 return( ld->ld_errno );
82         }
83
84 #ifndef LDAP_NOCACHE
85         if ( ld->ld_cache != NULL ) {
86                 if ( ldap_check_cache( ld, LDAP_REQ_COMPARE, ber ) == 0 ) {
87                         ber_free( ber, 1 );
88                         ld->ld_errno = LDAP_SUCCESS;
89                         *msgidp = ld->ld_msgid;
90                         return( ld->ld_errno );
91                 }
92                 ldap_add_request_to_cache( ld, LDAP_REQ_COMPARE, ber );
93         }
94 #endif /* LDAP_NOCACHE */
95
96         /* send the message */
97         *msgidp = ldap_send_initial_request( ld, LDAP_REQ_COMPARE, dn, ber );
98         return ( *msgidp < 0 ? ld->ld_errno : LDAP_SUCCESS );
99 }
100
101 /*
102  * ldap_compare_ext - perform an ldap extended compare operation.  The dn
103  * of the entry to compare to and the attribute and value to compare (in
104  * attr and value) are supplied.  The msgid of the response is returned.
105  *
106  * Example:
107  *      msgid = ldap_compare( ld, "c=us@cn=bob", "userPassword", "secret" )
108  */
109 int
110 ldap_compare(
111         LDAP *ld,
112         LDAP_CONST char *dn,
113         LDAP_CONST char *attr,
114         LDAP_CONST char *value )
115 {
116         int msgid;
117         struct berval bvalue;
118
119         bvalue.bv_val = (char *) value;
120         bvalue.bv_len = (value == NULL) ? 0 : strlen( value );
121
122         return ldap_compare_ext( ld, dn, attr, &bvalue, NULL, NULL, &msgid ) == LDAP_SUCCESS
123                 ? msgid : -1;
124 }
125
126 int
127 ldap_compare_ext_s(
128         LDAP *ld,
129         LDAP_CONST char *dn,
130         LDAP_CONST char *attr,
131         struct berval *bvalue,
132         LDAPControl **sctrl,
133         LDAPControl **cctrl )
134 {
135         int             rc;
136         int             msgid;
137         LDAPMessage     *res;
138
139         rc = ldap_compare_ext( ld, dn, attr, bvalue, sctrl, cctrl, &msgid );
140
141         if (  rc != LDAP_SUCCESS )
142                 return( rc );
143
144         if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
145                 return( ld->ld_errno );
146
147         return( ldap_result2error( ld, res, 1 ) );
148 }
149
150 int
151 ldap_compare_s(
152         LDAP *ld,
153         LDAP_CONST char *dn,
154         LDAP_CONST char *attr,
155         LDAP_CONST char *value )
156 {
157         struct berval bvalue;
158
159         bvalue.bv_val = (char *) value;
160         bvalue.bv_len = (value == NULL) ? 0 : strlen( value );
161
162         return ldap_compare_ext_s( ld, dn, attr, &bvalue, NULL, NULL );
163 }