]> git.sur5r.net Git - openldap/blob - libraries/libldap/compare.c
Set ciphers from slapd.conf.
[openldap] / libraries / libldap / compare.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 /*  Portions
6  *  Copyright (c) 1990 Regents of the University of Michigan.
7  *  All rights reserved.
8  *
9  *  compare.c
10  */
11
12 /* The compare request looks like this:
13  *      CompareRequest ::= SEQUENCE {
14  *              entry   DistinguishedName,
15  *              ava     SEQUENCE {
16  *                      type    AttributeType,
17  *                      value   AttributeValue
18  *              }
19  *      }
20  */
21
22 #include "portable.h"
23
24 #include <stdio.h>
25
26 #include <ac/socket.h>
27 #include <ac/string.h>
28 #include <ac/time.h>
29
30 #include "ldap-int.h"
31
32 /*
33  * ldap_compare_ext - perform an ldap extended compare operation.  The dn
34  * of the entry to compare to and the attribute and value to compare (in
35  * attr and value) are supplied.  The msgid of the response is returned.
36  *
37  * Example:
38  *      struct berval bvalue = { "secret", strlen("secret") };
39  *      rc = ldap_compare( ld, "c=us@cn=bob",
40  *              "userPassword", &bvalue,
41  *              sctrl, cctrl, &msgid )
42  */
43 int
44 ldap_compare_ext(
45         LDAP *ld,
46         LDAP_CONST char *dn,
47         LDAP_CONST char *attr,
48         struct berval *bvalue,
49         LDAPControl **sctrls,
50         LDAPControl **cctrls,
51         int     *msgidp )
52 {
53         BerElement      *ber;
54
55         Debug( LDAP_DEBUG_TRACE, "ldap_compare\n", 0, 0, 0 );
56
57         assert( ld != NULL );
58         assert( LDAP_VALID( ld ) );
59         assert( dn != NULL );
60         assert( attr != NULL );
61         assert( msgidp != NULL );
62
63         /* create a message to send */
64         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
65                 return( LDAP_NO_MEMORY );
66         }
67
68         if ( ber_printf( ber, "{it{s{sO}}", /* '}' */
69                 ++ld->ld_msgid,
70                 LDAP_REQ_COMPARE, dn, attr, &bvalue ) == -1 )
71         {
72                 ld->ld_errno = LDAP_ENCODING_ERROR;
73                 ber_free( ber, 1 );
74                 return( ld->ld_errno );
75         }
76
77         /* Put Server Controls */
78         if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
79                 ber_free( ber, 1 );
80                 return ld->ld_errno;
81         }
82
83         if( ber_printf( ber, /*{*/ "}" ) == -1 ) {
84                 ld->ld_errno = LDAP_ENCODING_ERROR;
85                 ber_free( ber, 1 );
86                 return( ld->ld_errno );
87         }
88
89 #ifndef LDAP_NOCACHE
90         if ( ld->ld_cache != NULL ) {
91                 if ( ldap_check_cache( ld, LDAP_REQ_COMPARE, ber ) == 0 ) {
92                         ber_free( ber, 1 );
93                         ld->ld_errno = LDAP_SUCCESS;
94                         *msgidp = ld->ld_msgid;
95                         return( ld->ld_errno );
96                 }
97                 ldap_add_request_to_cache( ld, LDAP_REQ_COMPARE, ber );
98         }
99 #endif /* LDAP_NOCACHE */
100
101         /* send the message */
102         *msgidp = ldap_send_initial_request( ld, LDAP_REQ_COMPARE, dn, ber );
103         return ( *msgidp < 0 ? ld->ld_errno : LDAP_SUCCESS );
104 }
105
106 /*
107  * ldap_compare_ext - perform an ldap extended compare operation.  The dn
108  * of the entry to compare to and the attribute and value to compare (in
109  * attr and value) are supplied.  The msgid of the response is returned.
110  *
111  * Example:
112  *      msgid = ldap_compare( ld, "c=us@cn=bob", "userPassword", "secret" )
113  */
114 int
115 ldap_compare(
116         LDAP *ld,
117         LDAP_CONST char *dn,
118         LDAP_CONST char *attr,
119         LDAP_CONST char *value )
120 {
121         int msgid;
122         struct berval bvalue;
123
124         bvalue.bv_val = (char *) value;
125         bvalue.bv_len = (value == NULL) ? 0 : strlen( value );
126
127         return ldap_compare_ext( ld, dn, attr, &bvalue, NULL, NULL, &msgid ) == LDAP_SUCCESS
128                 ? msgid : -1;
129 }
130
131 int
132 ldap_compare_ext_s(
133         LDAP *ld,
134         LDAP_CONST char *dn,
135         LDAP_CONST char *attr,
136         struct berval *bvalue,
137         LDAPControl **sctrl,
138         LDAPControl **cctrl )
139 {
140         int             rc;
141         int             msgid;
142         LDAPMessage     *res;
143
144         rc = ldap_compare_ext( ld, dn, attr, bvalue, sctrl, cctrl, &msgid );
145
146         if (  rc != LDAP_SUCCESS )
147                 return( rc );
148
149         if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
150                 return( ld->ld_errno );
151
152         return( ldap_result2error( ld, res, 1 ) );
153 }
154
155 int
156 ldap_compare_s(
157         LDAP *ld,
158         LDAP_CONST char *dn,
159         LDAP_CONST char *attr,
160         LDAP_CONST char *value )
161 {
162         struct berval bvalue;
163
164         bvalue.bv_val = (char *) value;
165         bvalue.bv_len = (value == NULL) ? 0 : strlen( value );
166
167         return ldap_compare_ext_s( ld, dn, attr, &bvalue, NULL, NULL );
168 }