]> git.sur5r.net Git - openldap/blob - libraries/libldap/compare.c
ITS#8262 add ldap_build_*_req functions
[openldap] / libraries / libldap / compare.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2015 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
82 /*
83  * ldap_compare_ext - perform an ldap extended compare operation.  The dn
84  * of the entry to compare to and the attribute and value to compare (in
85  * attr and value) are supplied.  The msgid of the response is returned.
86  *
87  * Example:
88  *      struct berval bvalue = { "secret", sizeof("secret")-1 };
89  *      rc = ldap_compare( ld, "c=us@cn=bob",
90  *              "userPassword", &bvalue,
91  *              sctrl, cctrl, &msgid )
92  */
93 int
94 ldap_compare_ext(
95         LDAP *ld,
96         LDAP_CONST char *dn,
97         LDAP_CONST char *attr,
98         struct berval *bvalue,
99         LDAPControl **sctrls,
100         LDAPControl **cctrls,
101         int     *msgidp )
102 {
103         int rc;
104         BerElement      *ber;
105         ber_int_t       id;
106
107         Debug( LDAP_DEBUG_TRACE, "ldap_compare\n", 0, 0, 0 );
108
109         assert( ld != NULL );
110         assert( LDAP_VALID( ld ) );
111         assert( dn != NULL );
112         assert( attr != NULL );
113         assert( msgidp != NULL );
114
115         /* check client controls */
116         rc = ldap_int_client_controls( ld, cctrls );
117         if( rc != LDAP_SUCCESS ) return rc;
118
119         ber = ldap_build_compare_req(
120                 ld, dn, attr, bvalue, sctrls, cctrls, &id );
121         if( !ber )
122                 return ld->ld_errno;
123
124         /* send the message */
125         *msgidp = ldap_send_initial_request( ld, LDAP_REQ_COMPARE, dn, ber, id );
126         return ( *msgidp < 0 ? ld->ld_errno : LDAP_SUCCESS );
127 }
128
129 /*
130  * ldap_compare_ext - perform an ldap extended compare operation.  The dn
131  * of the entry to compare to and the attribute and value to compare (in
132  * attr and value) are supplied.  The msgid of the response is returned.
133  *
134  * Example:
135  *      msgid = ldap_compare( ld, "c=us@cn=bob", "userPassword", "secret" )
136  */
137 int
138 ldap_compare(
139         LDAP *ld,
140         LDAP_CONST char *dn,
141         LDAP_CONST char *attr,
142         LDAP_CONST char *value )
143 {
144         int msgid;
145         struct berval bvalue;
146
147         assert( value != NULL );
148
149         bvalue.bv_val = (char *) value;
150         bvalue.bv_len = (value == NULL) ? 0 : strlen( value );
151
152         return ldap_compare_ext( ld, dn, attr, &bvalue, NULL, NULL, &msgid ) == LDAP_SUCCESS
153                 ? msgid : -1;
154 }
155
156 int
157 ldap_compare_ext_s(
158         LDAP *ld,
159         LDAP_CONST char *dn,
160         LDAP_CONST char *attr,
161         struct berval *bvalue,
162         LDAPControl **sctrl,
163         LDAPControl **cctrl )
164 {
165         int             rc;
166         int             msgid;
167         LDAPMessage     *res;
168
169         rc = ldap_compare_ext( ld, dn, attr, bvalue, sctrl, cctrl, &msgid );
170
171         if (  rc != LDAP_SUCCESS )
172                 return( rc );
173
174         if ( ldap_result( ld, msgid, LDAP_MSG_ALL, (struct timeval *) NULL, &res ) == -1 || !res )
175                 return( ld->ld_errno );
176
177         return( ldap_result2error( ld, res, 1 ) );
178 }
179
180 int
181 ldap_compare_s(
182         LDAP *ld,
183         LDAP_CONST char *dn,
184         LDAP_CONST char *attr,
185         LDAP_CONST char *value )
186 {
187         struct berval bvalue;
188
189         assert( value != NULL );
190
191         bvalue.bv_val = (char *) value;
192         bvalue.bv_len = (value == NULL) ? 0 : strlen( value );
193
194         return ldap_compare_ext_s( ld, dn, attr, &bvalue, NULL, NULL );
195 }