2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2006 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
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) The Internet Society (1997).
16 * ASN.1 fragments are from RFC 2251; see RFC for full legal notices.
20 * LDAPv3 Extended Operation Request
21 * ExtendedRequest ::= [APPLICATION 23] SEQUENCE {
22 * requestName [0] LDAPOID,
23 * requestValue [1] OCTET STRING OPTIONAL
26 * LDAPv3 Extended Operation Response
27 * ExtendedResponse ::= [APPLICATION 24] SEQUENCE {
28 * COMPONENTS OF LDAPResult,
29 * responseName [10] LDAPOID OPTIONAL,
30 * response [11] OCTET STRING OPTIONAL
38 #include <ac/stdlib.h>
40 #include <ac/socket.h>
41 #include <ac/string.h>
48 ldap_extended_operation(
50 LDAP_CONST char *reqoid,
51 struct berval *reqdata,
60 Debug( LDAP_DEBUG_TRACE, "ldap_extended_operation\n", 0, 0, 0 );
63 assert( LDAP_VALID( ld ) );
64 assert( reqoid != NULL && *reqoid != '\0' );
65 assert( msgidp != NULL );
67 /* must be version 3 (or greater) */
68 if ( ld->ld_version < LDAP_VERSION3 ) {
69 ld->ld_errno = LDAP_NOT_SUPPORTED;
70 return( ld->ld_errno );
73 /* create a message to send */
74 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
75 ld->ld_errno = LDAP_NO_MEMORY;
76 return( ld->ld_errno );
79 LDAP_NEXT_MSGID( ld, id );
80 if ( reqdata != NULL ) {
81 rc = ber_printf( ber, "{it{tstON}", /* '}' */
82 id, LDAP_REQ_EXTENDED,
83 LDAP_TAG_EXOP_REQ_OID, reqoid,
84 LDAP_TAG_EXOP_REQ_VALUE, reqdata );
87 rc = ber_printf( ber, "{it{tsN}", /* '}' */
88 id, LDAP_REQ_EXTENDED,
89 LDAP_TAG_EXOP_REQ_OID, reqoid );
93 ld->ld_errno = LDAP_ENCODING_ERROR;
95 return( ld->ld_errno );
98 /* Put Server Controls */
99 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
104 if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
105 ld->ld_errno = LDAP_ENCODING_ERROR;
107 return( ld->ld_errno );
110 /* send the message */
111 *msgidp = ldap_send_initial_request( ld, LDAP_REQ_EXTENDED, NULL, ber, id );
113 return( *msgidp < 0 ? ld->ld_errno : LDAP_SUCCESS );
117 ldap_extended_operation_s(
119 LDAP_CONST char *reqoid,
120 struct berval *reqdata,
121 LDAPControl **sctrls,
122 LDAPControl **cctrls,
124 struct berval **retdatap )
130 Debug( LDAP_DEBUG_TRACE, "ldap_extended_operation_s\n", 0, 0, 0 );
132 assert( ld != NULL );
133 assert( LDAP_VALID( ld ) );
134 assert( reqoid != NULL && *reqoid != '\0' );
136 rc = ldap_extended_operation( ld, reqoid, reqdata,
137 sctrls, cctrls, &msgid );
139 if ( rc != LDAP_SUCCESS ) {
143 if ( ldap_result( ld, msgid, LDAP_MSG_ALL, (struct timeval *) NULL, &res ) == -1 ) {
144 return( ld->ld_errno );
147 if ( retoidp != NULL ) *retoidp = NULL;
148 if ( retdatap != NULL ) *retdatap = NULL;
150 rc = ldap_parse_extended_result( ld, res, retoidp, retdatap, 0 );
152 if( rc != LDAP_SUCCESS ) {
157 return( ldap_result2error( ld, res, 1 ) );
160 /* Parse an extended result */
162 ldap_parse_extended_result (
166 struct berval **retdatap,
173 struct berval *resdata;
177 assert( ld != NULL );
178 assert( LDAP_VALID( ld ) );
179 assert( res != NULL );
181 Debug( LDAP_DEBUG_TRACE, "ldap_parse_extended_result\n", 0, 0, 0 );
183 if( ld->ld_version < LDAP_VERSION3 ) {
184 ld->ld_errno = LDAP_NOT_SUPPORTED;
188 if( res->lm_msgtype != LDAP_RES_EXTENDED ) {
189 ld->ld_errno = LDAP_PARAM_ERROR;
193 if( retoidp != NULL ) *retoidp = NULL;
194 if( retdatap != NULL ) *retdatap = NULL;
196 if ( ld->ld_error ) {
197 LDAP_FREE( ld->ld_error );
201 if ( ld->ld_matched ) {
202 LDAP_FREE( ld->ld_matched );
203 ld->ld_matched = NULL;
206 ber = ber_dup( res->lm_ber );
209 ld->ld_errno = LDAP_NO_MEMORY;
213 #ifdef LDAP_NULL_IS_NULL
214 rc = ber_scanf( ber, "{eAA" /*}*/, &errcode,
215 &ld->ld_matched, &ld->ld_error );
216 #else /* ! LDAP_NULL_IS_NULL */
217 rc = ber_scanf( ber, "{eaa" /*}*/, &errcode,
218 &ld->ld_matched, &ld->ld_error );
219 #endif /* ! LDAP_NULL_IS_NULL */
221 if( rc == LBER_ERROR ) {
222 ld->ld_errno = LDAP_DECODING_ERROR;
230 tag = ber_peek_tag( ber, &len );
232 if( tag == LDAP_TAG_REFERRAL ) {
233 /* skip over referral */
234 if( ber_scanf( ber, "x" ) == LBER_ERROR ) {
235 ld->ld_errno = LDAP_DECODING_ERROR;
240 tag = ber_peek_tag( ber, &len );
243 if( tag == LDAP_TAG_EXOP_RES_OID ) {
244 /* we have a resoid */
245 if( ber_scanf( ber, "a", &resoid ) == LBER_ERROR ) {
246 ld->ld_errno = LDAP_DECODING_ERROR;
251 #ifdef LDAP_NULL_IS_NULL
252 assert( resoid[ 0 ] != '\0' );
253 #endif /* LDAP_NULL_IS_NULL */
255 tag = ber_peek_tag( ber, &len );
258 if( tag == LDAP_TAG_EXOP_RES_VALUE ) {
259 /* we have a resdata */
260 if( ber_scanf( ber, "O", &resdata ) == LBER_ERROR ) {
261 ld->ld_errno = LDAP_DECODING_ERROR;
263 if( resoid != NULL ) LDAP_FREE( resoid );
270 if( retoidp != NULL ) {
276 if( retdatap != NULL ) {
279 ber_bvfree( resdata );
282 ld->ld_errno = errcode;
292 /* Parse an extended partial */
294 ldap_parse_intermediate (
298 struct berval **retdatap,
299 LDAPControl ***serverctrls,
306 struct berval *resdata;
309 assert( ld != NULL );
310 assert( LDAP_VALID( ld ) );
311 assert( res != NULL );
313 Debug( LDAP_DEBUG_TRACE, "ldap_parse_intermediate\n", 0, 0, 0 );
315 if( ld->ld_version < LDAP_VERSION3 ) {
316 ld->ld_errno = LDAP_NOT_SUPPORTED;
320 if( res->lm_msgtype != LDAP_RES_INTERMEDIATE ) {
321 ld->ld_errno = LDAP_PARAM_ERROR;
325 if( retoidp != NULL ) *retoidp = NULL;
326 if( retdatap != NULL ) *retdatap = NULL;
328 ber = ber_dup( res->lm_ber );
331 ld->ld_errno = LDAP_NO_MEMORY;
335 rc = ber_scanf( ber, "{" /*}*/ );
337 if( rc == LBER_ERROR ) {
338 ld->ld_errno = LDAP_DECODING_ERROR;
346 tag = ber_peek_tag( ber, &len );
349 * NOTE: accept intermediate and extended response tag values
350 * as older versions of slapd(8) incorrectly used extended
352 * Should be removed when 2.2 is moved to Historic.
354 if( tag == LDAP_TAG_IM_RES_OID || tag == LDAP_TAG_EXOP_RES_OID ) {
355 /* we have a resoid */
356 if( ber_scanf( ber, "a", &resoid ) == LBER_ERROR ) {
357 ld->ld_errno = LDAP_DECODING_ERROR;
362 #ifdef LDAP_NULL_IS_NULL
363 assert( resoid[ 0 ] != '\0' );
364 #endif /* LDAP_NULL_IS_NULL */
366 tag = ber_peek_tag( ber, &len );
369 if( tag == LDAP_TAG_IM_RES_VALUE || tag == LDAP_TAG_EXOP_RES_VALUE ) {
370 /* we have a resdata */
371 if( ber_scanf( ber, "O", &resdata ) == LBER_ERROR ) {
372 ld->ld_errno = LDAP_DECODING_ERROR;
374 if( resoid != NULL ) LDAP_FREE( resoid );
379 if ( serverctrls == NULL ) {
381 goto free_and_return;
384 if ( ber_scanf( ber, /*{*/ "}" ) == LBER_ERROR ) {
385 rc = LDAP_DECODING_ERROR;
386 goto free_and_return;
389 rc = ldap_pvt_get_controls( ber, serverctrls );
394 if( retoidp != NULL ) {
400 if( retdatap != NULL ) {
403 ber_bvfree( resdata );