1 /*	$NetBSD: compare.c,v 1.1.1.3 2010/12/12 15:21:29 adam Exp $	*/
2 
3 /* OpenLDAP: pkg/ldap/libraries/libldap/compare.c,v 1.29.2.5 2010/04/13 20:22:56 kurt Exp */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5  *
6  * Copyright 1998-2010 The OpenLDAP Foundation.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
18  * All rights reserved.
19  */
20 
21 #include "portable.h"
22 
23 #include <stdio.h>
24 
25 #include <ac/socket.h>
26 #include <ac/string.h>
27 #include <ac/time.h>
28 
29 #include "ldap-int.h"
30 #include "ldap_log.h"
31 
32 /* The compare request looks like this:
33  *	CompareRequest ::= SEQUENCE {
34  *		entry	DistinguishedName,
35  *		ava	SEQUENCE {
36  *			type	AttributeType,
37  *			value	AttributeValue
38  *		}
39  *	}
40  */
41 
42 /*
43  * ldap_compare_ext - perform an ldap extended compare operation.  The dn
44  * of the entry to compare to and the attribute and value to compare (in
45  * attr and value) are supplied.  The msgid of the response is returned.
46  *
47  * Example:
48  *	struct berval bvalue = { "secret", sizeof("secret")-1 };
49  *	rc = ldap_compare( ld, "c=us@cn=bob",
50  *		"userPassword", &bvalue,
51  *		sctrl, cctrl, &msgid )
52  */
53 int
54 ldap_compare_ext(
55 	LDAP *ld,
56 	LDAP_CONST char *dn,
57 	LDAP_CONST char *attr,
58 	struct berval *bvalue,
59 	LDAPControl **sctrls,
60 	LDAPControl **cctrls,
61 	int	*msgidp )
62 {
63 	int rc;
64 	BerElement	*ber;
65 	ber_int_t	id;
66 
67 	Debug( LDAP_DEBUG_TRACE, "ldap_compare\n", 0, 0, 0 );
68 
69 	assert( ld != NULL );
70 	assert( LDAP_VALID( ld ) );
71 	assert( dn != NULL );
72 	assert( attr != NULL );
73 	assert( msgidp != NULL );
74 
75 	/* check client controls */
76 	rc = ldap_int_client_controls( ld, cctrls );
77 	if( rc != LDAP_SUCCESS ) return rc;
78 
79 	/* create a message to send */
80 	if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
81 		return( LDAP_NO_MEMORY );
82 	}
83 
84 	LDAP_NEXT_MSGID(ld, id);
85 	rc = ber_printf( ber, "{it{s{sON}N}", /* '}' */
86 		id,
87 		LDAP_REQ_COMPARE, dn, attr, bvalue );
88 	if ( rc == -1 )
89 	{
90 		ld->ld_errno = LDAP_ENCODING_ERROR;
91 		ber_free( ber, 1 );
92 		return( ld->ld_errno );
93 	}
94 
95 	/* Put Server Controls */
96 	if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
97 		ber_free( ber, 1 );
98 		return ld->ld_errno;
99 	}
100 
101 	if( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
102 		ld->ld_errno = LDAP_ENCODING_ERROR;
103 		ber_free( ber, 1 );
104 		return( ld->ld_errno );
105 	}
106 
107 
108 	/* send the message */
109 	*msgidp = ldap_send_initial_request( ld, LDAP_REQ_COMPARE, dn, ber, id );
110 	return ( *msgidp < 0 ? ld->ld_errno : LDAP_SUCCESS );
111 }
112 
113 /*
114  * ldap_compare_ext - perform an ldap extended compare operation.  The dn
115  * of the entry to compare to and the attribute and value to compare (in
116  * attr and value) are supplied.  The msgid of the response is returned.
117  *
118  * Example:
119  *	msgid = ldap_compare( ld, "c=us@cn=bob", "userPassword", "secret" )
120  */
121 int
122 ldap_compare(
123 	LDAP *ld,
124 	LDAP_CONST char *dn,
125 	LDAP_CONST char *attr,
126 	LDAP_CONST char *value )
127 {
128 	int msgid;
129 	struct berval bvalue;
130 
131 	assert( value != NULL );
132 
133 	bvalue.bv_val = (char *) value;
134 	bvalue.bv_len = (value == NULL) ? 0 : strlen( value );
135 
136 	return ldap_compare_ext( ld, dn, attr, &bvalue, NULL, NULL, &msgid ) == LDAP_SUCCESS
137 		? msgid : -1;
138 }
139 
140 int
141 ldap_compare_ext_s(
142 	LDAP *ld,
143 	LDAP_CONST char *dn,
144 	LDAP_CONST char *attr,
145 	struct berval *bvalue,
146 	LDAPControl **sctrl,
147 	LDAPControl **cctrl )
148 {
149 	int		rc;
150 	int		msgid;
151 	LDAPMessage	*res;
152 
153 	rc = ldap_compare_ext( ld, dn, attr, bvalue, sctrl, cctrl, &msgid );
154 
155 	if (  rc != LDAP_SUCCESS )
156 		return( rc );
157 
158 	if ( ldap_result( ld, msgid, LDAP_MSG_ALL, (struct timeval *) NULL, &res ) == -1 || !res )
159 		return( ld->ld_errno );
160 
161 	return( ldap_result2error( ld, res, 1 ) );
162 }
163 
164 int
165 ldap_compare_s(
166 	LDAP *ld,
167 	LDAP_CONST char *dn,
168 	LDAP_CONST char *attr,
169 	LDAP_CONST char *value )
170 {
171 	struct berval bvalue;
172 
173 	assert( value != NULL );
174 
175 	bvalue.bv_val = (char *) value;
176 	bvalue.bv_len = (value == NULL) ? 0 : strlen( value );
177 
178 	return ldap_compare_ext_s( ld, dn, attr, &bvalue, NULL, NULL );
179 }
180