]> git.sur5r.net Git - openldap/blob - libraries/libldap/compare.c
moved rdn parsing in a dedicated routine, ldap_str2rdn(), that can be used directly...
[openldap] / libraries / libldap / compare.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*  Portions
7  *  Copyright (c) 1990 Regents of the University of Michigan.
8  *  All rights reserved.
9  *
10  *  compare.c
11  */
12
13 /* The compare request looks like this:
14  *      CompareRequest ::= SEQUENCE {
15  *              entry   DistinguishedName,
16  *              ava     SEQUENCE {
17  *                      type    AttributeType,
18  *                      value   AttributeValue
19  *              }
20  *      }
21  */
22
23 #include "portable.h"
24
25 #include <stdio.h>
26
27 #include <ac/socket.h>
28 #include <ac/string.h>
29 #include <ac/time.h>
30
31 #include "ldap-int.h"
32
33 /*
34  * ldap_compare_ext - perform an ldap extended compare operation.  The dn
35  * of the entry to compare to and the attribute and value to compare (in
36  * attr and value) are supplied.  The msgid of the response is returned.
37  *
38  * Example:
39  *      struct berval bvalue = { "secret", strlen("secret") };
40  *      rc = ldap_compare( ld, "c=us@cn=bob",
41  *              "userPassword", &bvalue,
42  *              sctrl, cctrl, &msgid )
43  */
44 int
45 ldap_compare_ext(
46         LDAP *ld,
47         LDAP_CONST char *dn,
48         LDAP_CONST char *attr,
49         struct berval *bvalue,
50         LDAPControl **sctrls,
51         LDAPControl **cctrls,
52         int     *msgidp )
53 {
54         int rc;
55         BerElement      *ber;
56
57         Debug( LDAP_DEBUG_TRACE, "ldap_compare\n", 0, 0, 0 );
58
59         assert( ld != NULL );
60         assert( LDAP_VALID( ld ) );
61         assert( dn != NULL );
62         assert( attr != NULL );
63         assert( msgidp != NULL );
64
65         /* check client controls */
66         rc = ldap_int_client_controls( ld, cctrls );
67         if( rc != LDAP_SUCCESS ) return rc;
68
69         /* create a message to send */
70         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
71                 return( LDAP_NO_MEMORY );
72         }
73
74         if ( ber_printf( ber, "{it{s{sON}N}", /* '}' */
75                 ++ld->ld_msgid,
76                 LDAP_REQ_COMPARE, dn, attr, bvalue ) == -1 )
77         {
78                 ld->ld_errno = LDAP_ENCODING_ERROR;
79                 ber_free( ber, 1 );
80                 return( ld->ld_errno );
81         }
82
83         /* Put Server Controls */
84         if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
85                 ber_free( ber, 1 );
86                 return ld->ld_errno;
87         }
88
89         if( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
90                 ld->ld_errno = LDAP_ENCODING_ERROR;
91                 ber_free( ber, 1 );
92                 return( ld->ld_errno );
93         }
94
95 #ifndef LDAP_NOCACHE
96         if ( ld->ld_cache != NULL ) {
97                 if ( ldap_check_cache( ld, LDAP_REQ_COMPARE, ber ) == 0 ) {
98                         ber_free( ber, 1 );
99                         ld->ld_errno = LDAP_SUCCESS;
100                         *msgidp = ld->ld_msgid;
101                         return( ld->ld_errno );
102                 }
103                 ldap_add_request_to_cache( ld, LDAP_REQ_COMPARE, ber );
104         }
105 #endif /* LDAP_NOCACHE */
106
107         /* send the message */
108         *msgidp = ldap_send_initial_request( ld, LDAP_REQ_COMPARE, dn, ber );
109         return ( *msgidp < 0 ? ld->ld_errno : LDAP_SUCCESS );
110 }
111
112 /*
113  * ldap_compare_ext - perform an ldap extended compare operation.  The dn
114  * of the entry to compare to and the attribute and value to compare (in
115  * attr and value) are supplied.  The msgid of the response is returned.
116  *
117  * Example:
118  *      msgid = ldap_compare( ld, "c=us@cn=bob", "userPassword", "secret" )
119  */
120 int
121 ldap_compare(
122         LDAP *ld,
123         LDAP_CONST char *dn,
124         LDAP_CONST char *attr,
125         LDAP_CONST char *value )
126 {
127         int msgid;
128         struct berval bvalue;
129
130         bvalue.bv_val = (char *) value;
131         bvalue.bv_len = (value == NULL) ? 0 : strlen( value );
132
133         return ldap_compare_ext( ld, dn, attr, &bvalue, NULL, NULL, &msgid ) == LDAP_SUCCESS
134                 ? msgid : -1;
135 }
136
137 int
138 ldap_compare_ext_s(
139         LDAP *ld,
140         LDAP_CONST char *dn,
141         LDAP_CONST char *attr,
142         struct berval *bvalue,
143         LDAPControl **sctrl,
144         LDAPControl **cctrl )
145 {
146         int             rc;
147         int             msgid;
148         LDAPMessage     *res;
149
150         rc = ldap_compare_ext( ld, dn, attr, bvalue, sctrl, cctrl, &msgid );
151
152         if (  rc != LDAP_SUCCESS )
153                 return( rc );
154
155         if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
156                 return( ld->ld_errno );
157
158         return( ldap_result2error( ld, res, 1 ) );
159 }
160
161 int
162 ldap_compare_s(
163         LDAP *ld,
164         LDAP_CONST char *dn,
165         LDAP_CONST char *attr,
166         LDAP_CONST char *value )
167 {
168         struct berval bvalue;
169
170         bvalue.bv_val = (char *) value;
171         bvalue.bv_len = (value == NULL) ? 0 : strlen( value );
172
173         return ldap_compare_ext_s( ld, dn, attr, &bvalue, NULL, NULL );
174 }