Back to index

openldap  2.4.31
assertion.c
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  *
00004  * Copyright 1998-2012 The OpenLDAP Foundation.
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted only as authorized by the OpenLDAP
00009  * Public License.
00010  *
00011  * A copy of this license is available in the file LICENSE in the
00012  * top-level directory of the distribution or, alternatively, at
00013  * <http://www.OpenLDAP.org/license.html>.
00014  */
00015 
00016 #include "portable.h"
00017 
00018 #include <stdio.h>
00019 #include <ac/stdlib.h>
00020 #include <ac/string.h>
00021 #include <ac/time.h>
00022 
00023 #include "ldap-int.h"
00024 
00025 int
00026 ldap_create_assertion_control_value(
00027        LDAP          *ld,
00028        char          *assertion,
00029        struct berval *value )
00030 {
00031        BerElement           *ber = NULL;
00032        int                  err;
00033 
00034        if ( assertion == NULL || assertion[ 0 ] == '\0' ) {
00035               ld->ld_errno = LDAP_PARAM_ERROR;
00036               return ld->ld_errno;
00037        }
00038 
00039        if ( value == NULL ) {
00040               ld->ld_errno = LDAP_PARAM_ERROR;
00041               return ld->ld_errno;
00042        }
00043 
00044        BER_BVZERO( value );
00045 
00046        ber = ldap_alloc_ber_with_options( ld );
00047        if ( ber == NULL ) {
00048               ld->ld_errno = LDAP_NO_MEMORY;
00049               return ld->ld_errno;
00050        }
00051 
00052        err = ldap_pvt_put_filter( ber, assertion );
00053        if ( err < 0 ) {
00054               ld->ld_errno = LDAP_ENCODING_ERROR;
00055               goto done;
00056        }
00057 
00058        err = ber_flatten2( ber, value, 1 );
00059        if ( err < 0 ) {
00060               ld->ld_errno = LDAP_NO_MEMORY;
00061               goto done;
00062        }
00063 
00064 done:;
00065        if ( ber != NULL ) {
00066               ber_free( ber, 1 );
00067        }
00068 
00069        return ld->ld_errno;
00070 }
00071 
00072 int
00073 ldap_create_assertion_control(
00074        LDAP          *ld,
00075        char          *assertion,
00076        int           iscritical,
00077        LDAPControl   **ctrlp )
00078 {
00079        struct berval value;
00080 
00081        if ( ctrlp == NULL ) {
00082               ld->ld_errno = LDAP_PARAM_ERROR;
00083               return ld->ld_errno;
00084        }
00085 
00086        ld->ld_errno = ldap_create_assertion_control_value( ld,
00087               assertion, &value );
00088        if ( ld->ld_errno == LDAP_SUCCESS ) {
00089               ld->ld_errno = ldap_control_create( LDAP_CONTROL_ASSERT,
00090                      iscritical, &value, 0, ctrlp );
00091               if ( ld->ld_errno != LDAP_SUCCESS ) {
00092                      LDAP_FREE( value.bv_val );
00093               }
00094        }
00095 
00096        return ld->ld_errno;
00097 }
00098