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