3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
8 * LDAPv3 Extended Operation Request
9 * ExtendedRequest ::= [APPLICATION 23] SEQUENCE {
10 * requestName [0] LDAPOID,
11 * requestValue [1] OCTET STRING OPTIONAL
14 * LDAPv3 Extended Operation Response
15 * ExtendedResponse ::= [APPLICATION 24] SEQUENCE {
16 * COMPONENTS OF LDAPResult,
17 * responseName [10] LDAPOID OPTIONAL,
18 * response [11] OCTET STRING OPTIONAL
26 #include <ac/stdlib.h>
28 #include <ac/socket.h>
29 #include <ac/string.h>
35 ldap_extended_operation(
37 LDAP_CONST char *reqoid,
38 struct berval *reqdata,
46 Debug( LDAP_DEBUG_TRACE, "ldap_extended_operation\n", 0, 0, 0 );
49 assert( LDAP_VALID( ld ) );
50 assert( reqoid != NULL || *reqoid == '\0' );
51 assert( msgidp != NULL );
53 /* must be version 3 (or greater) */
54 if ( ld->ld_version < LDAP_VERSION3 ) {
55 ld->ld_errno = LDAP_NOT_SUPPORTED;
56 return( ld->ld_errno );
59 if( reqoid == NULL || *reqoid == '\0' || msgidp == NULL ) {
60 ld->ld_errno = LDAP_PARAM_ERROR;
61 return( ld->ld_errno );
64 /* create a message to send */
65 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
66 ld->ld_errno = LDAP_NO_MEMORY;
67 return( ld->ld_errno );
70 if ( reqdata != NULL ) {
71 rc = ber_printf( ber, "{it{tstON}", /* '}' */
72 ++ld->ld_msgid, LDAP_REQ_EXTENDED,
73 LDAP_TAG_EXOP_REQ_OID, reqoid,
74 LDAP_TAG_EXOP_REQ_VALUE, reqdata );
77 rc = ber_printf( ber, "{it{tsN}", /* '}' */
78 ++ld->ld_msgid, LDAP_REQ_EXTENDED,
79 LDAP_TAG_EXOP_REQ_OID, reqoid );
83 ld->ld_errno = LDAP_ENCODING_ERROR;
85 return( ld->ld_errno );
88 /* Put Server Controls */
89 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
94 if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
95 ld->ld_errno = LDAP_ENCODING_ERROR;
97 return( ld->ld_errno );
100 /* send the message */
101 *msgidp = ldap_send_initial_request( ld, LDAP_REQ_EXTENDED, NULL, ber );
103 return( *msgidp < 0 ? ld->ld_errno : LDAP_SUCCESS );
107 ldap_extended_operation_s(
109 LDAP_CONST char *reqoid,
110 struct berval *reqdata,
111 LDAPControl **sctrls,
112 LDAPControl **cctrls,
114 struct berval **retdatap )
120 Debug( LDAP_DEBUG_TRACE, "ldap_extended_operation_s\n", 0, 0, 0 );
122 assert( ld != NULL );
123 assert( LDAP_VALID( ld ) );
124 assert( reqoid != NULL || *reqoid == '\0' );
125 assert( retoidp != NULL || retdatap != NULL );
127 if( retoidp == NULL || retdatap == NULL ) {
128 ld->ld_errno = LDAP_PARAM_ERROR;
129 return( ld->ld_errno );
132 rc = ldap_extended_operation( ld, reqoid, reqdata,
133 sctrls, cctrls, &msgid );
135 if ( rc != LDAP_SUCCESS ) {
139 if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 ) {
140 return( ld->ld_errno );
146 rc = ldap_parse_extended_result( ld, res, retoidp, retdatap, 0 );
148 if( rc != LDAP_SUCCESS ) {
153 return( ldap_result2error( ld, res, 1 ) );
156 /* Parse an extended result */
158 ldap_parse_extended_result (
162 struct berval **retdatap,
169 struct berval *resdata;
173 assert( ld != NULL );
174 assert( LDAP_VALID( ld ) );
175 assert( res != NULL );
177 Debug( LDAP_DEBUG_TRACE, "ldap_parse_extended_result\n", 0, 0, 0 );
179 if( ld->ld_version < LDAP_VERSION3 ) {
180 ld->ld_errno = LDAP_NOT_SUPPORTED;
184 if( res->lm_msgtype != LDAP_RES_EXTENDED ) {
185 ld->ld_errno = LDAP_PARAM_ERROR;
189 if( retoidp != NULL ) *retoidp = NULL;
190 if( retdatap != NULL ) *retdatap = NULL;
192 if ( ld->ld_error ) {
193 LDAP_FREE( ld->ld_error );
197 if ( ld->ld_matched ) {
198 LDAP_FREE( ld->ld_matched );
199 ld->ld_matched = NULL;
202 ber = ber_dup( res->lm_ber );
205 ld->ld_errno = LDAP_NO_MEMORY;
209 rc = ber_scanf( ber, "{iaa" /*}*/, &errcode,
210 &ld->ld_matched, &ld->ld_error );
212 if( rc == LBER_ERROR ) {
213 ld->ld_errno = LDAP_DECODING_ERROR;
221 tag = ber_peek_tag( ber, &len );
223 if( tag == LDAP_TAG_REFERRAL ) {
224 /* skip over referral */
225 if( ber_scanf( ber, "x" ) == LBER_ERROR ) {
226 ld->ld_errno = LDAP_DECODING_ERROR;
231 tag = ber_peek_tag( ber, &len );
234 if( tag == LDAP_TAG_EXOP_RES_OID ) {
235 /* we have a resoid */
236 if( ber_scanf( ber, "a", &resoid ) == LBER_ERROR ) {
237 ld->ld_errno = LDAP_DECODING_ERROR;
242 tag = ber_peek_tag( ber, &len );
245 if( tag == LDAP_TAG_EXOP_RES_VALUE ) {
246 /* we have a resdata */
247 if( ber_scanf( ber, "O", &resdata ) == LBER_ERROR ) {
248 ld->ld_errno = LDAP_DECODING_ERROR;
250 if( resoid != NULL ) LDAP_FREE( resoid );
257 if( retoidp != NULL ) {
263 if( retdatap != NULL ) {
266 ber_bvfree( resdata );
269 ld->ld_errno = errcode;
279 /* Parse an extended partial */
281 ldap_parse_extended_partial (
285 struct berval **retdatap,
286 LDAPControl ***serverctrls,
293 struct berval *resdata;
296 assert( ld != NULL );
297 assert( LDAP_VALID( ld ) );
298 assert( res != NULL );
300 Debug( LDAP_DEBUG_TRACE, "ldap_parse_extended_result\n", 0, 0, 0 );
302 if( ld->ld_version < LDAP_VERSION3 ) {
303 ld->ld_errno = LDAP_NOT_SUPPORTED;
307 if( res->lm_msgtype != LDAP_RES_EXTENDED_PARTIAL ) {
308 ld->ld_errno = LDAP_PARAM_ERROR;
312 if( retoidp != NULL ) *retoidp = NULL;
313 if( retdatap != NULL ) *retdatap = NULL;
315 ber = ber_dup( res->lm_ber );
318 ld->ld_errno = LDAP_NO_MEMORY;
322 rc = ber_scanf( ber, "{" /*}*/ );
324 if( rc == LBER_ERROR ) {
325 ld->ld_errno = LDAP_DECODING_ERROR;
333 tag = ber_peek_tag( ber, &len );
335 if( tag == LDAP_TAG_EXOP_RES_OID ) {
336 /* we have a resoid */
337 if( ber_scanf( ber, "a", &resoid ) == LBER_ERROR ) {
338 ld->ld_errno = LDAP_DECODING_ERROR;
343 tag = ber_peek_tag( ber, &len );
346 if( tag == LDAP_TAG_EXOP_RES_VALUE ) {
347 /* we have a resdata */
348 if( ber_scanf( ber, "O", &resdata ) == LBER_ERROR ) {
349 ld->ld_errno = LDAP_DECODING_ERROR;
351 if( resoid != NULL ) LDAP_FREE( resoid );
356 if ( serverctrls == NULL ) {
358 goto free_and_return;
361 if ( ber_scanf( ber, /*{*/ "}" ) == LBER_ERROR ) {
362 rc = LDAP_DECODING_ERROR;
363 goto free_and_return;
366 rc = ldap_int_get_controls( ber, serverctrls );
371 if( retoidp != NULL ) {
377 if( retdatap != NULL ) {
380 ber_bvfree( resdata );