]> git.sur5r.net Git - openldap/blob - libraries/libldap/compare.c
Merge remote-tracking branch 'origin/mdb.RE/0.9'
[openldap] / libraries / libldap / compare.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2017 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
16  * All rights reserved.
17  */
18
19 #include "portable.h"
20
21 #include <stdio.h>
22
23 #include <ac/socket.h>
24 #include <ac/string.h>
25 #include <ac/time.h>
26
27 #include "ldap-int.h"
28 #include "ldap_log.h"
29
30 /* The compare request looks like this:
31  *      CompareRequest ::= SEQUENCE {
32  *              entry   DistinguishedName,
33  *              ava     SEQUENCE {
34  *                      type    AttributeType,
35  *                      value   AttributeValue
36  *              }
37  *      }
38  */
39
40 BerElement *
41 ldap_build_compare_req(
42         LDAP *ld,
43         LDAP_CONST char *dn,
44         LDAP_CONST char *attr,
45         struct berval *bvalue,
46         LDAPControl **sctrls,
47         LDAPControl **cctrls,
48         int     *msgidp )
49 {
50         BerElement      *ber;
51         int rc;
52
53         /* create a message to send */
54         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
55                 return( NULL );
56         }
57
58         LDAP_NEXT_MSGID(ld, *msgidp);
59         rc = ber_printf( ber, "{it{s{sON}N}", /* '}' */
60                 *msgidp,
61                 LDAP_REQ_COMPARE, dn, attr, bvalue );
62         if ( rc == -1 )
63         {
64                 ld->ld_errno = LDAP_ENCODING_ERROR;
65                 ber_free( ber, 1 );
66                 return( NULL );
67         }
68
69         /* Put Server Controls */
70         if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
71                 ber_free( ber, 1 );
72                 return( NULL );
73         }
74
75         if( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
76                 ld->ld_errno = LDAP_ENCODING_ERROR;
77                 ber_free( ber, 1 );
78                 return( NULL );
79         }
80
81         return( ber );
82 }
83
84 /*
85  * ldap_compare_ext - perform an ldap extended compare operation.  The dn
86  * of the entry to compare to and the attribute and value to compare (in
87  * attr and value) are supplied.  The msgid of the response is returned.
88  *
89  * Example:
90  *      struct berval bvalue = { "secret", sizeof("secret")-1 };
91  *      rc = ldap_compare( ld, "c=us@cn=bob",
92  *              "userPassword", &bvalue,
93  *              sctrl, cctrl, &msgid )
94  */
95 int
96 ldap_compare_ext(
97         LDAP *ld,
98         LDAP_CONST char *dn,
99         LDAP_CONST char *attr,
100         struct berval *bvalue,
101         LDAPControl **sctrls,
102         LDAPControl **cctrls,
103         int     *msgidp )
104 {
105         int rc;
106         BerElement      *ber;
107         ber_int_t       id;
108
109         Debug( LDAP_DEBUG_TRACE, "ldap_compare\n", 0, 0, 0 );
110
111         assert( ld != NULL );
112         assert( LDAP_VALID( ld ) );
113         assert( dn != NULL );
114         assert( attr != NULL );
115         assert( msgidp != NULL );
116
117         /* check client controls */
118         rc = ldap_int_client_controls( ld, cctrls );
119         if( rc != LDAP_SUCCESS ) return rc;
120
121         ber = ldap_build_compare_req(
122                 ld, dn, attr, bvalue, sctrls, cctrls, &id );
123         if( !ber )
124                 return ld->ld_errno;
125
126         /* send the message */
127         *msgidp = ldap_send_initial_request( ld, LDAP_REQ_COMPARE, dn, ber, id );
128         return ( *msgidp < 0 ? ld->ld_errno : LDAP_SUCCESS );
129 }
130
131 /*
132  * ldap_compare_ext - perform an ldap extended compare operation.  The dn
133  * of the entry to compare to and the attribute and value to compare (in
134  * attr and value) are supplied.  The msgid of the response is returned.
135  *
136  * Example:
137  *      msgid = ldap_compare( ld, "c=us@cn=bob", "userPassword", "secret" )
138  */
139 int
140 ldap_compare(
141         LDAP *ld,
142         LDAP_CONST char *dn,
143         LDAP_CONST char *attr,
144         LDAP_CONST char *value )
145 {
146         int msgid;
147         struct berval bvalue;
148
149         assert( value != NULL );
150
151         bvalue.bv_val = (char *) value;
152         bvalue.bv_len = (value == NULL) ? 0 : strlen( value );
153
154         return ldap_compare_ext( ld, dn, attr, &bvalue, NULL, NULL, &msgid ) == LDAP_SUCCESS
155                 ? msgid : -1;
156 }
157
158 int
159 ldap_compare_ext_s(
160         LDAP *ld,
161         LDAP_CONST char *dn,
162         LDAP_CONST char *attr,
163         struct berval *bvalue,
164         LDAPControl **sctrl,
165         LDAPControl **cctrl )
166 {
167         int             rc;
168         int             msgid;
169         LDAPMessage     *res;
170
171         rc = ldap_compare_ext( ld, dn, attr, bvalue, sctrl, cctrl, &msgid );
172
173         if (  rc != LDAP_SUCCESS )
174                 return( rc );
175
176         if ( ldap_result( ld, msgid, LDAP_MSG_ALL, (struct timeval *) NULL, &res ) == -1 || !res )
177                 return( ld->ld_errno );
178
179         return( ldap_result2error( ld, res, 1 ) );
180 }
181
182 int
183 ldap_compare_s(
184         LDAP *ld,
185         LDAP_CONST char *dn,
186         LDAP_CONST char *attr,
187         LDAP_CONST char *value )
188 {
189         struct berval bvalue;
190
191         assert( value != NULL );
192
193         bvalue.bv_val = (char *) value;
194         bvalue.bv_len = (value == NULL) ? 0 : strlen( value );
195
196         return ldap_compare_ext_s( ld, dn, attr, &bvalue, NULL, NULL );
197 }