2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2014 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>.
19 #include <ac/stdlib.h>
21 #include <ac/socket.h>
22 #include <ac/string.h>
29 * LDAPv3 Extended Operation Request
30 * ExtendedRequest ::= [APPLICATION 23] SEQUENCE {
31 * requestName [0] LDAPOID,
32 * requestValue [1] OCTET STRING OPTIONAL
35 * LDAPv3 Extended Operation Response
36 * ExtendedResponse ::= [APPLICATION 24] SEQUENCE {
37 * COMPONENTS OF LDAPResult,
38 * responseName [10] LDAPOID OPTIONAL,
39 * response [11] OCTET STRING OPTIONAL
46 ldap_extended_operation(
48 LDAP_CONST char *reqoid,
49 struct berval *reqdata,
58 Debug( LDAP_DEBUG_TRACE, "ldap_extended_operation\n", 0, 0, 0 );
61 assert( LDAP_VALID( ld ) );
62 assert( reqoid != NULL && *reqoid != '\0' );
63 assert( msgidp != NULL );
65 /* must be version 3 (or greater) */
66 if ( ld->ld_version < LDAP_VERSION3 ) {
67 ld->ld_errno = LDAP_NOT_SUPPORTED;
68 return( ld->ld_errno );
71 /* create a message to send */
72 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
73 ld->ld_errno = LDAP_NO_MEMORY;
74 return( ld->ld_errno );
77 LDAP_NEXT_MSGID( ld, id );
78 if ( reqdata != NULL ) {
79 rc = ber_printf( ber, "{it{tstON}", /* '}' */
80 id, LDAP_REQ_EXTENDED,
81 LDAP_TAG_EXOP_REQ_OID, reqoid,
82 LDAP_TAG_EXOP_REQ_VALUE, reqdata );
85 rc = ber_printf( ber, "{it{tsN}", /* '}' */
86 id, LDAP_REQ_EXTENDED,
87 LDAP_TAG_EXOP_REQ_OID, reqoid );
91 ld->ld_errno = LDAP_ENCODING_ERROR;
93 return( ld->ld_errno );
96 /* Put Server Controls */
97 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
102 if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
103 ld->ld_errno = LDAP_ENCODING_ERROR;
105 return( ld->ld_errno );
108 /* send the message */
109 *msgidp = ldap_send_initial_request( ld, LDAP_REQ_EXTENDED, NULL, ber, id );
111 return( *msgidp < 0 ? ld->ld_errno : LDAP_SUCCESS );
115 ldap_extended_operation_s(
117 LDAP_CONST char *reqoid,
118 struct berval *reqdata,
119 LDAPControl **sctrls,
120 LDAPControl **cctrls,
122 struct berval **retdatap )
128 Debug( LDAP_DEBUG_TRACE, "ldap_extended_operation_s\n", 0, 0, 0 );
130 assert( ld != NULL );
131 assert( LDAP_VALID( ld ) );
132 assert( reqoid != NULL && *reqoid != '\0' );
134 rc = ldap_extended_operation( ld, reqoid, reqdata,
135 sctrls, cctrls, &msgid );
137 if ( rc != LDAP_SUCCESS ) {
141 if ( ldap_result( ld, msgid, LDAP_MSG_ALL, (struct timeval *) NULL, &res ) == -1 || !res ) {
142 return( ld->ld_errno );
145 if ( retoidp != NULL ) *retoidp = NULL;
146 if ( retdatap != NULL ) *retdatap = NULL;
148 rc = ldap_parse_extended_result( ld, res, retoidp, retdatap, 0 );
150 if( rc != LDAP_SUCCESS ) {
155 return( ldap_result2error( ld, res, 1 ) );
158 /* Parse an extended result */
160 ldap_parse_extended_result (
164 struct berval **retdatap,
171 struct berval *resdata;
175 assert( ld != NULL );
176 assert( LDAP_VALID( ld ) );
177 assert( res != NULL );
179 Debug( LDAP_DEBUG_TRACE, "ldap_parse_extended_result\n", 0, 0, 0 );
181 if( ld->ld_version < LDAP_VERSION3 ) {
182 ld->ld_errno = LDAP_NOT_SUPPORTED;
186 if( res->lm_msgtype != LDAP_RES_EXTENDED ) {
187 ld->ld_errno = LDAP_PARAM_ERROR;
191 if( retoidp != NULL ) *retoidp = NULL;
192 if( retdatap != NULL ) *retdatap = NULL;
194 if ( ld->ld_error ) {
195 LDAP_FREE( ld->ld_error );
199 if ( ld->ld_matched ) {
200 LDAP_FREE( ld->ld_matched );
201 ld->ld_matched = NULL;
204 ber = ber_dup( res->lm_ber );
207 ld->ld_errno = LDAP_NO_MEMORY;
211 rc = ber_scanf( ber, "{eAA" /*}*/, &errcode,
212 &ld->ld_matched, &ld->ld_error );
214 if( rc == LBER_ERROR ) {
215 ld->ld_errno = LDAP_DECODING_ERROR;
223 tag = ber_peek_tag( ber, &len );
225 if( tag == LDAP_TAG_REFERRAL ) {
226 /* skip over referral */
227 if( ber_scanf( ber, "x" ) == LBER_ERROR ) {
228 ld->ld_errno = LDAP_DECODING_ERROR;
233 tag = ber_peek_tag( ber, &len );
236 if( tag == LDAP_TAG_EXOP_RES_OID ) {
237 /* we have a resoid */
238 if( ber_scanf( ber, "a", &resoid ) == LBER_ERROR ) {
239 ld->ld_errno = LDAP_DECODING_ERROR;
244 assert( resoid[ 0 ] != '\0' );
246 tag = ber_peek_tag( ber, &len );
249 if( tag == LDAP_TAG_EXOP_RES_VALUE ) {
250 /* we have a resdata */
251 if( ber_scanf( ber, "O", &resdata ) == LBER_ERROR ) {
252 ld->ld_errno = LDAP_DECODING_ERROR;
254 if( resoid != NULL ) LDAP_FREE( resoid );
261 if( retoidp != NULL ) {
267 if( retdatap != NULL ) {
270 ber_bvfree( resdata );
273 ld->ld_errno = errcode;
283 /* Parse an extended partial */
285 ldap_parse_intermediate (
289 struct berval **retdatap,
290 LDAPControl ***serverctrls,
296 struct berval *resdata;
299 assert( ld != NULL );
300 assert( LDAP_VALID( ld ) );
301 assert( res != NULL );
303 Debug( LDAP_DEBUG_TRACE, "ldap_parse_intermediate\n", 0, 0, 0 );
305 if( ld->ld_version < LDAP_VERSION3 ) {
306 ld->ld_errno = LDAP_NOT_SUPPORTED;
310 if( res->lm_msgtype != LDAP_RES_INTERMEDIATE ) {
311 ld->ld_errno = LDAP_PARAM_ERROR;
315 if( retoidp != NULL ) *retoidp = NULL;
316 if( retdatap != NULL ) *retdatap = NULL;
317 if( serverctrls != NULL ) *serverctrls = NULL;
319 ber = ber_dup( res->lm_ber );
322 ld->ld_errno = LDAP_NO_MEMORY;
326 tag = ber_scanf( ber, "{" /*}*/ );
328 if( tag == LBER_ERROR ) {
329 ld->ld_errno = LDAP_DECODING_ERROR;
337 tag = ber_peek_tag( ber, &len );
340 * NOTE: accept intermediate and extended response tag values
341 * as older versions of slapd(8) incorrectly used extended
343 * Should be removed when 2.2 is moved to Historic.
345 if( tag == LDAP_TAG_IM_RES_OID || tag == LDAP_TAG_EXOP_RES_OID ) {
346 /* we have a resoid */
347 if( ber_scanf( ber, "a", &resoid ) == LBER_ERROR ) {
348 ld->ld_errno = LDAP_DECODING_ERROR;
353 assert( resoid[ 0 ] != '\0' );
355 tag = ber_peek_tag( ber, &len );
358 if( tag == LDAP_TAG_IM_RES_VALUE || tag == LDAP_TAG_EXOP_RES_VALUE ) {
359 /* we have a resdata */
360 if( ber_scanf( ber, "O", &resdata ) == LBER_ERROR ) {
361 ld->ld_errno = LDAP_DECODING_ERROR;
363 if( resoid != NULL ) LDAP_FREE( resoid );
368 if ( serverctrls == NULL ) {
369 ld->ld_errno = LDAP_SUCCESS;
370 goto free_and_return;
373 if ( ber_scanf( ber, /*{*/ "}" ) == LBER_ERROR ) {
374 ld->ld_errno = LDAP_DECODING_ERROR;
375 goto free_and_return;
378 ld->ld_errno = ldap_pvt_get_controls( ber, serverctrls );
383 if( retoidp != NULL ) {
389 if( retdatap != NULL ) {
392 ber_bvfree( resdata );