1N/A#pragma ident "%Z%%M% %I% %E% SMI"
1N/A
1N/A/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
1N/A *
1N/A * The contents of this file are subject to the Netscape Public License
1N/A * Version 1.0 (the "NPL"); you may not use this file except in
1N/A * compliance with the NPL. You may obtain a copy of the NPL at
1N/A * http://www.mozilla.org/NPL/
1N/A *
1N/A * Software distributed under the NPL is distributed on an "AS IS" basis,
1N/A * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
1N/A * for the specific language governing rights and limitations under the
1N/A * NPL.
1N/A *
1N/A * The Initial Developer of this code under the NPL is Netscape
1N/A * Communications Corporation. Portions created by Netscape are
1N/A * Copyright (C) 1998 Netscape Communications Corporation. All Rights
1N/A * Reserved.
1N/A */
1N/A#include "ldap-int.h"
1N/A
1N/A/*
1N/A * ldap_extended_operation - initiate an arbitrary ldapv3 extended operation.
1N/A * the oid and data of the extended operation are supplied. Returns an
1N/A * LDAP error code.
1N/A *
1N/A * Example:
1N/A * struct berval exdata;
1N/A * char *exoid;
1N/A * int err, msgid;
1N/A * ... fill in oid and data ...
1N/A * err = ldap_extended_operation( ld, exoid, &exdata, NULL, NULL, &msgid );
1N/A */
1N/A
1N/Aint
1N/ALDAP_CALL
1N/Aldap_extended_operation(
1N/A LDAP *ld,
1N/A const char *exoid,
1N/A const struct berval *exdata,
1N/A LDAPControl **serverctrls,
1N/A LDAPControl **clientctrls,
1N/A int *msgidp
1N/A)
1N/A{
1N/A BerElement *ber;
1N/A int rc, msgid;
1N/A
1N/A /*
1N/A * the ldapv3 extended operation request looks like this:
1N/A *
1N/A * ExtendedRequest ::= [APPLICATION 23] SEQUENCE {
1N/A * requestName LDAPOID,
1N/A * requestValue OCTET STRING
1N/A * }
1N/A *
1N/A * all wrapped up in an LDAPMessage sequence.
1N/A */
1N/A
1N/A LDAPDebug( LDAP_DEBUG_TRACE, "ldap_extended_operation\n", 0, 0, 0 );
1N/A
1N/A if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
1N/A return( LDAP_PARAM_ERROR );
1N/A }
1N/A
1N/A
1N/A /* only ldapv3 or higher can do extended operations */
1N/A if ( NSLDAPI_LDAP_VERSION( ld ) < LDAP_VERSION3 ) {
1N/A rc = LDAP_NOT_SUPPORTED;
1N/A LDAP_SET_LDERRNO( ld, rc, NULL, NULL );
1N/A return( rc );
1N/A }
1N/A
1N/A if ( msgidp == NULL || exoid == NULL || *exoid == '\0' ||
1N/A exdata == NULL || exdata->bv_val == NULL ) {
1N/A rc = LDAP_PARAM_ERROR;
1N/A LDAP_SET_LDERRNO( ld, rc, NULL, NULL );
1N/A return( rc );
1N/A }
1N/A
1N/A LDAP_MUTEX_LOCK( ld, LDAP_MSGID_LOCK );
1N/A msgid = ++ld->ld_msgid;
1N/A LDAP_MUTEX_UNLOCK( ld, LDAP_MSGID_LOCK );
1N/A
1N/A#if 0
1N/A if ( ld->ld_cache_on && ld->ld_cache_extendedop != NULL ) {
1N/A LDAP_MUTEX_LOCK( ld, LDAP_CACHE_LOCK );
1N/A if ( (rc = (ld->ld_cache_extendedop)( ld, msgid,
1N/A LDAP_REQ_EXTENDED, exoid, cred )) != 0 ) {
1N/A LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
1N/A return( rc );
1N/A }
1N/A LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
1N/A }
1N/A#endif
1N/A
1N/A /* create a message to send */
1N/A if (( rc = nsldapi_alloc_ber_with_options( ld, &ber ))
1N/A != LDAP_SUCCESS ) {
1N/A return( rc );
1N/A }
1N/A
1N/A /* fill it in */
1N/A if ( ber_printf( ber, "{it{tsto}", msgid, LDAP_REQ_EXTENDED,
1N/A LDAP_TAG_EXOP_REQ_OID, exoid, LDAP_TAG_EXOP_REQ_VALUE,
1N/A exdata->bv_val, (int)exdata->bv_len /* XXX lossy cast */ ) == -1 ) {
1N/A rc = LDAP_ENCODING_ERROR;
1N/A LDAP_SET_LDERRNO( ld, rc, NULL, NULL );
1N/A ber_free( ber, 1 );
1N/A return( rc );
1N/A }
1N/A
1N/A if (( rc = nsldapi_put_controls( ld, serverctrls, 1, ber ))
1N/A != LDAP_SUCCESS ) {
1N/A ber_free( ber, 1 );
1N/A return( rc );
1N/A }
1N/A
1N/A /* send the message */
1N/A rc = nsldapi_send_initial_request( ld, msgid, LDAP_REQ_EXTENDED, NULL,
1N/A ber );
1N/A *msgidp = rc;
1N/A return( rc < 0 ? LDAP_GET_LDERRNO( ld, NULL, NULL ) : LDAP_SUCCESS );
1N/A}
1N/A
1N/A
1N/A/*
1N/A * ldap_extended_operation_s - perform an arbitrary ldapv3 extended operation.
1N/A * the oid and data of the extended operation are supplied. LDAP_SUCCESS
1N/A * is returned upon success, the ldap error code otherwise.
1N/A *
1N/A * Example:
1N/A * struct berval exdata, exretval;
1N/A * char *exoid;
1N/A * int rc;
1N/A * ... fill in oid and data ...
1N/A * rc = ldap_extended_operation_s( ld, exoid, &exdata, &exretval );
1N/A */
1N/Aint
1N/ALDAP_CALL
1N/Aldap_extended_operation_s(
1N/A LDAP *ld,
1N/A const char *requestoid,
1N/A const struct berval *requestdata,
1N/A LDAPControl **serverctrls,
1N/A LDAPControl **clientctrls,
1N/A char **retoidp,
1N/A struct berval **retdatap
1N/A)
1N/A{
1N/A int err, msgid;
1N/A LDAPMessage *result;
1N/A
1N/A if (( err = ldap_extended_operation( ld, requestoid, requestdata,
1N/A serverctrls, clientctrls, &msgid )) != LDAP_SUCCESS ) {
1N/A return( err );
1N/A }
1N/A
1N/A if ( ldap_result( ld, msgid, 1, (struct timeval *) 0, &result )
1N/A == -1 ) {
1N/A return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
1N/A }
1N/A
1N/A if (( err = ldap_parse_extended_result( ld, result, retoidp, retdatap,
1N/A 0 )) != LDAP_SUCCESS ) {
1N/A ldap_msgfree( result );
1N/A return( err );
1N/A }
1N/A
1N/A return( ldap_result2error( ld, result, 1 ) );
1N/A}
1N/A
1N/A
1N/A/*
1N/A * Pull the oid returned by the server and the data out of an extended
1N/A * operation result. Return an LDAP error code.
1N/A */
1N/Aint
1N/ALDAP_CALL
1N/Aldap_parse_extended_result(
1N/A LDAP *ld,
1N/A LDAPMessage *res,
1N/A char **retoidp, /* may be NULL */
1N/A struct berval **retdatap, /* may be NULL */
1N/A int freeit
1N/A)
1N/A{
1N/A struct berelement ber;
1N/A ber_len_t len;
1N/A ber_int_t err;
1N/A char *m, *e, *roid;
1N/A struct berval *rdata;
1N/A
1N/A LDAPDebug( LDAP_DEBUG_TRACE, "ldap_parse_extended_result\n", 0, 0, 0 );
1N/A
1N/A if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
1N/A return( LDAP_PARAM_ERROR );
1N/A }
1N/A
1N/A if ( !NSLDAPI_VALID_LDAPMESSAGE_EXRESULT_POINTER( res )) {
1N/A return( LDAP_PARAM_ERROR );
1N/A }
1N/A
1N/A m = e = NULL;
1N/A ber = *(res->lm_ber);
1N/A if ( NSLDAPI_LDAP_VERSION( ld ) < LDAP_VERSION3 ) {
1N/A LDAP_SET_LDERRNO( ld, LDAP_NOT_SUPPORTED, NULL, NULL );
1N/A return( LDAP_NOT_SUPPORTED );
1N/A }
1N/A
1N/A if ( ber_scanf( &ber, "{iaa", &err, &m, &e ) == LBER_ERROR ) {
1N/A goto decoding_error;
1N/A }
1N/A roid = NULL;
1N/A if ( ber_peek_tag( &ber, &len ) == LDAP_TAG_EXOP_RES_OID ) {
1N/A if ( ber_scanf( &ber, "a", &roid ) == LBER_ERROR ) {
1N/A goto decoding_error;
1N/A }
1N/A }
1N/A if ( retoidp != NULL ) {
1N/A *retoidp = roid;
1N/A } else if ( roid != NULL ) {
1N/A NSLDAPI_FREE( roid );
1N/A }
1N/A
1N/A rdata = NULL;
1N/A if ( ber_peek_tag( &ber, &len ) == LDAP_TAG_EXOP_RES_VALUE ) {
1N/A if ( ber_scanf( &ber, "O", &rdata ) == LBER_ERROR ) {
1N/A goto decoding_error;
1N/A }
1N/A }
1N/A if ( retdatap != NULL ) {
1N/A *retdatap = rdata;
1N/A } else if ( rdata != NULL ) {
1N/A ber_bvfree( rdata );
1N/A }
1N/A
1N/A LDAP_SET_LDERRNO( ld, err, m, e );
1N/A
1N/A if ( freeit ) {
1N/A ldap_msgfree( res );
1N/A }
1N/A
1N/A return( LDAP_SUCCESS );
1N/A
1N/Adecoding_error:;
1N/A LDAP_SET_LDERRNO( ld, LDAP_DECODING_ERROR, NULL, NULL );
1N/A return( LDAP_DECODING_ERROR );
1N/A}