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