include/mscvpdb.h: Use flexible array members for the rest of structures.
[wine.git] / libs / ldap / libldap / compare.c
blob86285c685ac34f669492c5fc115c16181e28a3f7
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2022 The OpenLDAP Foundation.
5 * All rights reserved.
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.
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>.
15 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
16 * All rights reserved.
19 #include "portable.h"
21 #include <stdio.h>
23 #include <ac/socket.h>
24 #include <ac/string.h>
25 #include <ac/time.h>
27 #include "ldap-int.h"
28 #include "ldap_log.h"
30 /* The compare request looks like this:
31 * CompareRequest ::= SEQUENCE {
32 * entry DistinguishedName,
33 * ava SEQUENCE {
34 * type AttributeType,
35 * value AttributeValue
36 * }
37 * }
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 )
50 BerElement *ber;
51 int rc;
53 /* create a message to send */
54 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
55 return( NULL );
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 )
64 ld->ld_errno = LDAP_ENCODING_ERROR;
65 ber_free( ber, 1 );
66 return( NULL );
69 /* Put Server Controls */
70 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
71 ber_free( ber, 1 );
72 return( NULL );
75 if( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
76 ld->ld_errno = LDAP_ENCODING_ERROR;
77 ber_free( ber, 1 );
78 return( NULL );
81 return( ber );
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.
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 )
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 )
105 int rc;
106 BerElement *ber;
107 ber_int_t id;
109 Debug0( LDAP_DEBUG_TRACE, "ldap_compare\n" );
111 assert( ld != NULL );
112 assert( LDAP_VALID( ld ) );
113 assert( dn != NULL );
114 assert( attr != NULL );
115 assert( msgidp != NULL );
117 /* check client controls */
118 rc = ldap_int_client_controls( ld, cctrls );
119 if( rc != LDAP_SUCCESS ) return rc;
121 ber = ldap_build_compare_req(
122 ld, dn, attr, bvalue, sctrls, cctrls, &id );
123 if( !ber )
124 return ld->ld_errno;
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 );
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.
136 * Example:
137 * msgid = ldap_compare( ld, "c=us@cn=bob", "userPassword", "secret" )
140 ldap_compare(
141 LDAP *ld,
142 LDAP_CONST char *dn,
143 LDAP_CONST char *attr,
144 LDAP_CONST char *value )
146 int msgid;
147 struct berval bvalue;
149 assert( value != NULL );
151 bvalue.bv_val = (char *) value;
152 bvalue.bv_len = (value == NULL) ? 0 : strlen( value );
154 return ldap_compare_ext( ld, dn, attr, &bvalue, NULL, NULL, &msgid ) == LDAP_SUCCESS
155 ? msgid : -1;
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 )
167 int rc;
168 int msgid;
169 LDAPMessage *res;
171 rc = ldap_compare_ext( ld, dn, attr, bvalue, sctrl, cctrl, &msgid );
173 if ( rc != LDAP_SUCCESS )
174 return( rc );
176 if ( ldap_result( ld, msgid, LDAP_MSG_ALL, (struct timeval *) NULL, &res ) == -1 || !res )
177 return( ld->ld_errno );
179 return( ldap_result2error( ld, res, 1 ) );
183 ldap_compare_s(
184 LDAP *ld,
185 LDAP_CONST char *dn,
186 LDAP_CONST char *attr,
187 LDAP_CONST char *value )
189 struct berval bvalue;
191 assert( value != NULL );
193 bvalue.bv_val = (char *) value;
194 bvalue.bv_len = (value == NULL) ? 0 : strlen( value );
196 return ldap_compare_ext_s( ld, dn, attr, &bvalue, NULL, NULL );