]> git.sur5r.net Git - openldap/blob - libraries/libldap/compare.c
slashpath from HEAD
[openldap] / libraries / libldap / compare.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2004 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 #ifdef NEW_LOGGING
69         LDAP_LOG ( OPERATION, ENTRY, "ldap_compare\n", 0, 0, 0 );
70 #else
71         Debug( LDAP_DEBUG_TRACE, "ldap_compare\n", 0, 0, 0 );
72 #endif
73
74         assert( ld != NULL );
75         assert( LDAP_VALID( ld ) );
76         assert( dn != NULL );
77         assert( attr != NULL );
78         assert( msgidp != NULL );
79
80         /* check client controls */
81         rc = ldap_int_client_controls( ld, cctrls );
82         if( rc != LDAP_SUCCESS ) return rc;
83
84         /* create a message to send */
85         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
86                 return( LDAP_NO_MEMORY );
87         }
88
89         LDAP_NEXT_MSGID(ld, id);
90         rc = ber_printf( ber, "{it{s{sON}N}", /* '}' */
91                 id,
92                 LDAP_REQ_COMPARE, dn, attr, bvalue );
93         if ( rc == -1 )
94         {
95                 ld->ld_errno = LDAP_ENCODING_ERROR;
96                 ber_free( ber, 1 );
97                 return( ld->ld_errno );
98         }
99
100         /* Put Server Controls */
101         if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
102                 ber_free( ber, 1 );
103                 return ld->ld_errno;
104         }
105
106         if( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
107                 ld->ld_errno = LDAP_ENCODING_ERROR;
108                 ber_free( ber, 1 );
109                 return( ld->ld_errno );
110         }
111
112
113         /* send the message */
114         *msgidp = ldap_send_initial_request( ld, LDAP_REQ_COMPARE, dn, ber, id );
115         return ( *msgidp < 0 ? ld->ld_errno : LDAP_SUCCESS );
116 }
117
118 /*
119  * ldap_compare_ext - perform an ldap extended compare operation.  The dn
120  * of the entry to compare to and the attribute and value to compare (in
121  * attr and value) are supplied.  The msgid of the response is returned.
122  *
123  * Example:
124  *      msgid = ldap_compare( ld, "c=us@cn=bob", "userPassword", "secret" )
125  */
126 int
127 ldap_compare(
128         LDAP *ld,
129         LDAP_CONST char *dn,
130         LDAP_CONST char *attr,
131         LDAP_CONST char *value )
132 {
133         int msgid;
134         struct berval bvalue;
135
136         assert( value != NULL );
137
138         bvalue.bv_val = (char *) value;
139         bvalue.bv_len = (value == NULL) ? 0 : strlen( value );
140
141         return ldap_compare_ext( ld, dn, attr, &bvalue, NULL, NULL, &msgid ) == LDAP_SUCCESS
142                 ? msgid : -1;
143 }
144
145 int
146 ldap_compare_ext_s(
147         LDAP *ld,
148         LDAP_CONST char *dn,
149         LDAP_CONST char *attr,
150         struct berval *bvalue,
151         LDAPControl **sctrl,
152         LDAPControl **cctrl )
153 {
154         int             rc;
155         int             msgid;
156         LDAPMessage     *res;
157
158         rc = ldap_compare_ext( ld, dn, attr, bvalue, sctrl, cctrl, &msgid );
159
160         if (  rc != LDAP_SUCCESS )
161                 return( rc );
162
163         if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
164                 return( ld->ld_errno );
165
166         return( ldap_result2error( ld, res, 1 ) );
167 }
168
169 int
170 ldap_compare_s(
171         LDAP *ld,
172         LDAP_CONST char *dn,
173         LDAP_CONST char *attr,
174         LDAP_CONST char *value )
175 {
176         struct berval bvalue;
177
178         assert( value != NULL );
179
180         bvalue.bv_val = (char *) value;
181         bvalue.bv_len = (value == NULL) ? 0 : strlen( value );
182
183         return ldap_compare_ext_s( ld, dn, attr, &bvalue, NULL, NULL );
184 }