2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2003 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,
61 LDAP_LOG ( OPERATION, ENTRY, "ldap_extended_operation\n", 0,0,0 );
63 Debug( LDAP_DEBUG_TRACE, "ldap_extended_operation\n", 0, 0, 0 );
67 assert( LDAP_VALID( ld ) );
68 assert( reqoid != NULL || *reqoid == '\0' );
69 assert( msgidp != NULL );
71 /* must be version 3 (or greater) */
72 if ( ld->ld_version < LDAP_VERSION3 ) {
73 ld->ld_errno = LDAP_NOT_SUPPORTED;
74 return( ld->ld_errno );
77 /* create a message to send */
78 if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
79 ld->ld_errno = LDAP_NO_MEMORY;
80 return( ld->ld_errno );
83 LDAP_NEXT_MSGID( ld, id );
84 if ( reqdata != NULL ) {
85 rc = ber_printf( ber, "{it{tstON}", /* '}' */
86 id, LDAP_REQ_EXTENDED,
87 LDAP_TAG_EXOP_REQ_OID, reqoid,
88 LDAP_TAG_EXOP_REQ_VALUE, reqdata );
91 rc = ber_printf( ber, "{it{tsN}", /* '}' */
92 id, LDAP_REQ_EXTENDED,
93 LDAP_TAG_EXOP_REQ_OID, reqoid );
97 ld->ld_errno = LDAP_ENCODING_ERROR;
99 return( ld->ld_errno );
102 /* Put Server Controls */
103 if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
108 if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
109 ld->ld_errno = LDAP_ENCODING_ERROR;
111 return( ld->ld_errno );
114 /* send the message */
115 *msgidp = ldap_send_initial_request( ld, LDAP_REQ_EXTENDED, NULL, ber, id );
117 return( *msgidp < 0 ? ld->ld_errno : LDAP_SUCCESS );
121 ldap_extended_operation_s(
123 LDAP_CONST char *reqoid,
124 struct berval *reqdata,
125 LDAPControl **sctrls,
126 LDAPControl **cctrls,
128 struct berval **retdatap )
135 LDAP_LOG ( OPERATION, ENTRY, "ldap_extended_operation_s\n", 0,0,0 );
137 Debug( LDAP_DEBUG_TRACE, "ldap_extended_operation_s\n", 0, 0, 0 );
140 assert( ld != NULL );
141 assert( LDAP_VALID( ld ) );
142 assert( reqoid != NULL || *reqoid == '\0' );
143 assert( retoidp != NULL || retdatap != NULL );
145 rc = ldap_extended_operation( ld, reqoid, reqdata,
146 sctrls, cctrls, &msgid );
148 if ( rc != LDAP_SUCCESS ) {
152 if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 ) {
153 return( ld->ld_errno );
156 if ( retoidp != NULL ) *retoidp = NULL;
157 if ( retdatap != NULL ) *retdatap = NULL;
159 rc = ldap_parse_extended_result( ld, res, retoidp, retdatap, 0 );
161 if( rc != LDAP_SUCCESS ) {
166 return( ldap_result2error( ld, res, 1 ) );
169 /* Parse an extended result */
171 ldap_parse_extended_result (
175 struct berval **retdatap,
182 struct berval *resdata;
186 assert( ld != NULL );
187 assert( LDAP_VALID( ld ) );
188 assert( res != NULL );
191 LDAP_LOG ( OPERATION, ENTRY, "ldap_parse_extended_result\n", 0,0,0 );
193 Debug( LDAP_DEBUG_TRACE, "ldap_parse_extended_result\n", 0, 0, 0 );
196 if( ld->ld_version < LDAP_VERSION3 ) {
197 ld->ld_errno = LDAP_NOT_SUPPORTED;
201 if( res->lm_msgtype != LDAP_RES_EXTENDED ) {
202 ld->ld_errno = LDAP_PARAM_ERROR;
206 if( retoidp != NULL ) *retoidp = NULL;
207 if( retdatap != NULL ) *retdatap = NULL;
209 if ( ld->ld_error ) {
210 LDAP_FREE( ld->ld_error );
214 if ( ld->ld_matched ) {
215 LDAP_FREE( ld->ld_matched );
216 ld->ld_matched = NULL;
219 ber = ber_dup( res->lm_ber );
222 ld->ld_errno = LDAP_NO_MEMORY;
226 rc = ber_scanf( ber, "{iaa" /*}*/, &errcode,
227 &ld->ld_matched, &ld->ld_error );
229 if( rc == LBER_ERROR ) {
230 ld->ld_errno = LDAP_DECODING_ERROR;
238 tag = ber_peek_tag( ber, &len );
240 if( tag == LDAP_TAG_REFERRAL ) {
241 /* skip over referral */
242 if( ber_scanf( ber, "x" ) == LBER_ERROR ) {
243 ld->ld_errno = LDAP_DECODING_ERROR;
248 tag = ber_peek_tag( ber, &len );
251 if( tag == LDAP_TAG_EXOP_RES_OID ) {
252 /* we have a resoid */
253 if( ber_scanf( ber, "a", &resoid ) == LBER_ERROR ) {
254 ld->ld_errno = LDAP_DECODING_ERROR;
259 tag = ber_peek_tag( ber, &len );
262 if( tag == LDAP_TAG_EXOP_RES_VALUE ) {
263 /* we have a resdata */
264 if( ber_scanf( ber, "O", &resdata ) == LBER_ERROR ) {
265 ld->ld_errno = LDAP_DECODING_ERROR;
267 if( resoid != NULL ) LDAP_FREE( resoid );
274 if( retoidp != NULL ) {
280 if( retdatap != NULL ) {
283 ber_bvfree( resdata );
286 ld->ld_errno = errcode;
296 /* Parse an extended partial */
298 ldap_parse_intermediate (
302 struct berval **retdatap,
303 LDAPControl ***serverctrls,
310 struct berval *resdata;
313 assert( ld != NULL );
314 assert( LDAP_VALID( ld ) );
315 assert( res != NULL );
318 LDAP_LOG ( OPERATION, ENTRY, "ldap_parse_intermediate\n", 0,0,0 );
320 Debug( LDAP_DEBUG_TRACE, "ldap_parse_intermediate\n", 0, 0, 0 );
323 if( ld->ld_version < LDAP_VERSION3 ) {
324 ld->ld_errno = LDAP_NOT_SUPPORTED;
328 if( res->lm_msgtype != LDAP_RES_INTERMEDIATE ) {
329 ld->ld_errno = LDAP_PARAM_ERROR;
333 if( retoidp != NULL ) *retoidp = NULL;
334 if( retdatap != NULL ) *retdatap = NULL;
336 ber = ber_dup( res->lm_ber );
339 ld->ld_errno = LDAP_NO_MEMORY;
343 rc = ber_scanf( ber, "{" /*}*/ );
345 if( rc == LBER_ERROR ) {
346 ld->ld_errno = LDAP_DECODING_ERROR;
354 tag = ber_peek_tag( ber, &len );
356 if( tag == LDAP_TAG_EXOP_RES_OID ) {
357 /* we have a resoid */
358 if( ber_scanf( ber, "a", &resoid ) == LBER_ERROR ) {
359 ld->ld_errno = LDAP_DECODING_ERROR;
364 tag = ber_peek_tag( ber, &len );
367 if( tag == LDAP_TAG_EXOP_RES_VALUE ) {
368 /* we have a resdata */
369 if( ber_scanf( ber, "O", &resdata ) == LBER_ERROR ) {
370 ld->ld_errno = LDAP_DECODING_ERROR;
372 if( resoid != NULL ) LDAP_FREE( resoid );
377 if ( serverctrls == NULL ) {
379 goto free_and_return;
382 if ( ber_scanf( ber, /*{*/ "}" ) == LBER_ERROR ) {
383 rc = LDAP_DECODING_ERROR;
384 goto free_and_return;
387 rc = ldap_pvt_get_controls( ber, serverctrls );
392 if( retoidp != NULL ) {
398 if( retdatap != NULL ) {
401 ber_bvfree( resdata );