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