]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/controls.c
Merged in preliminary support for Cyrus SASL library;
[openldap] / libraries / libldap / controls.c
index 7e3a5f6b21c228adf2e7104ba13ef34e5a095133..cab753e8810a00b3d20ccf2de46a485d66b16e5b 100644 (file)
@@ -1,14 +1,23 @@
+/* $OpenLDAP$ */
 /*
  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
  */
-/*
- * LDAP controls
+
+/* LDAPv3 Controls (RFC2251)
+ *
+ *     Controls ::= SEQUENCE OF Control  
+ *
+ *     Control ::= SEQUENCE { 
+ *             controlType             LDAPOID,
+ *             criticality             BOOLEAN DEFAULT FALSE,
+ *             controlValue    OCTET STRING OPTIONAL
+ *     }
  */
 
 #include "portable.h"
 
-#include <stdlib.h>
+#include <ac/stdlib.h>
 
 #include <ac/time.h>
 #include <ac/string.h>
  * ldap_int_put_controls
  */
 
-int ldap_int_put_controls(
+int
+ldap_int_put_controls(
        LDAP *ld,
-       LDAPControl **ctrls,
+       LDAPControl *const *ctrls,
        BerElement *ber )
 {
-       LDAPControl **c;
+       LDAPControl *const *c;
 
        assert( ld != NULL );
        assert( ber != NULL );
@@ -54,13 +64,13 @@ int ldap_int_put_controls(
        }
 
        /* Controls are encoded as a sequence of sequences */
-       if( ber_printf( ber, "t{", LDAP_TAG_CONTROLS ) == -1 ) {
+       if( ber_printf( ber, "t{"/*}*/, LDAP_TAG_CONTROLS ) == -1 ) {
                ld->ld_errno = LDAP_ENCODING_ERROR;
                return ld->ld_errno;
        }
 
        for( c = ctrls ; *c != NULL; c++ ) {
-               if ( ber_printf( ber, "{s",
+               if ( ber_printf( ber, "{s" /*}*/,
                        (*c)->ldctl_oid ) == -1 )
                {
                        ld->ld_errno = LDAP_ENCODING_ERROR;
@@ -69,7 +79,7 @@ int ldap_int_put_controls(
 
                if( (*c)->ldctl_iscritical /* only if true */
                        &&  ( ber_printf( ber, "b",
-                               (*c)->ldctl_iscritical ) == -1 ) )
+                               (ber_int_t) (*c)->ldctl_iscritical ) == -1 ) )
                {
                        ld->ld_errno = LDAP_ENCODING_ERROR;
                        return ld->ld_errno;
@@ -84,14 +94,14 @@ int ldap_int_put_controls(
                }
 
 
-               if( ber_printf( ber, "}" ) == -1 ) {
+               if( ber_printf( ber, /*{*/"}" ) == -1 ) {
                        ld->ld_errno = LDAP_ENCODING_ERROR;
                        return ld->ld_errno;
                }
        }
 
 
-       if( ber_printf( ber, "}" ) == -1 ) {
+       if( ber_printf( ber, /*{*/"}" ) == -1 ) {
                ld->ld_errno = LDAP_ENCODING_ERROR;
                return ld->ld_errno;
        }
@@ -99,17 +109,20 @@ int ldap_int_put_controls(
        return LDAP_SUCCESS;
 }
 
-int ldap_int_get_controls LDAP_P((
+int ldap_int_get_controls(
        BerElement *ber,
-       LDAPControl ***ctrls ))
+       LDAPControl ***ctrls )
 {
        int nctrls;
-       unsigned long tag, len;
+       ber_tag_t tag;
+       ber_len_t len;
        char *opaque;
 
        assert( ber != NULL );
-       assert( ctrls != NULL );
 
+       if( ctrls == NULL ) {
+               return LDAP_SUCCESS;
+       }
        *ctrls = NULL;
 
        len = ber_pvt_ber_remaining(ber);
@@ -119,6 +132,7 @@ int ldap_int_get_controls LDAP_P((
                return LDAP_SUCCESS;
        }
 
+
        if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
                if( tag == LBER_ERROR ) {
                        /* decoding error */
@@ -137,7 +151,7 @@ int ldap_int_get_controls LDAP_P((
                return LDAP_NO_MEMORY;
        }
 
-       ctrls[nctrls] = NULL;
+       *ctrls[nctrls] = NULL;
 
        for( tag = ber_first_element( ber, &len, &opaque );
                tag != LBER_ERROR;
@@ -171,14 +185,16 @@ int ldap_int_get_controls LDAP_P((
                tctrls[nctrls++] = tctrl;
                tctrls[nctrls] = NULL;
 
-               tag = ber_scanf( ber, "{a", &tctrl->ldctl_oid );
+               tag = ber_scanf( ber, "{a" /*}*/, &tctrl->ldctl_oid );
 
                if( tag != LBER_ERROR ) {
                        tag = ber_peek_tag( ber, &len );
                }
 
                if( tag == LBER_BOOLEAN ) {
-                       tag = ber_scanf( ber, "b", &tctrl->ldctl_iscritical );
+                       ber_int_t crit;
+                       tag = ber_scanf( ber, "b", &crit );
+                       tctrl->ldctl_iscritical = crit ? (char) 0 : (char) ~0;
                }
 
                if( tag != LBER_ERROR ) {
@@ -210,6 +226,8 @@ int ldap_int_get_controls LDAP_P((
 void
 ldap_control_free( LDAPControl *c )
 {
+       assert( c != NULL );
+
        if ( c != NULL ) {
                if( c->ldctl_oid != NULL) {
                        LDAP_FREE( c->ldctl_oid );
@@ -229,11 +247,13 @@ ldap_control_free( LDAPControl *c )
 void
 ldap_controls_free( LDAPControl **controls )
 {
+       assert( controls != NULL );
+
        if ( controls != NULL ) {
-               LDAPControl *c;
+               int i;
 
-               for(c = *controls; c != NULL; c++) {
-                       ldap_control_free( c );
+               for( i=0; controls[i] != NULL; i++) {
+                       ldap_control_free( controls[i] );
                }
 
                LDAP_FREE( controls );
@@ -243,7 +263,8 @@ ldap_controls_free( LDAPControl **controls )
 /*
  * Duplicate an array of LDAPControl
  */
-LDAPControl **ldap_controls_dup( const LDAPControl **controls )
+LDAPControl **
+ldap_controls_dup( LDAPControl *const *controls )
 {
        LDAPControl **new;
        int i;
@@ -285,7 +306,8 @@ LDAPControl **ldap_controls_dup( const LDAPControl **controls )
 /*
  * Duplicate a LDAPControl
  */
-LDAPControl *ldap_control_dup( const LDAPControl *c )
+LDAPControl *
+ldap_control_dup( const LDAPControl *c )
 {
        LDAPControl *new;
 
@@ -300,7 +322,7 @@ LDAPControl *ldap_control_dup( const LDAPControl *c )
        }
 
        if( c->ldctl_oid != NULL ) {
-               new->ldctl_oid = strdup( c->ldctl_oid );
+               new->ldctl_oid = LDAP_STRDUP( c->ldctl_oid );
 
                if(new->ldctl_oid == NULL) {
                        LDAP_FREE( new );