3 * Copyright 1999 The OpenLDAP Foundation.
6 * Redistribution and use in source and binary forms are permitted only
7 * as authorized by the OpenLDAP Public License. A copy of this
8 * license is available at http://www.OpenLDAP.org/license.html or
9 * in file LICENSE in the top-level directory of the distribution.
15 #include <ac/string.h>
16 #include <ac/socket.h>
20 #include "../../libraries/liblber/lber-int.h"
22 char *supportedControls[] = {
23 LDAP_CONTROL_MANAGEDSAIT,
24 LDAP_CONTROL_X_CHANGE_PASSWD,
37 BerElement *ber = op->o_ber;
38 LDAPControl ***ctrls = &op->o_ctrls;
39 int rc = LDAP_SUCCESS;
42 len = ber_pvt_ber_remaining(ber);
50 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
51 if( tag == LBER_ERROR ) {
53 errmsg = "unexpected data in PDU";
59 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls\n", 0, 0, 0 );
61 if( op->o_protocol < LDAP_VERSION3 ) {
63 errmsg = "controls require LDAPv3";
67 /* set through each element */
68 *ctrls = ch_malloc( 1 * sizeof(LDAPControl *) );
71 if( *ctrls == NULL ) {
78 *ctrls[nctrls] = NULL;
80 for( tag = ber_first_element( ber, &len, &opaque );
82 tag = ber_next_element( ber, &len, opaque ) )
87 tctrl = ch_calloc( 1, sizeof(LDAPControl) );
88 tctrl->ldctl_oid = NULL;
89 tctrl->ldctl_value.bv_val = NULL;
91 /* allocate pointer space for current controls (nctrls)
92 * + this control + extra NULL
94 tctrls = (tctrl == NULL) ? NULL :
95 ch_realloc(*ctrls, (nctrls+2) * sizeof(LDAPControl *));
98 if( tctrls == NULL ) {
99 /* one of the above allocation failed */
101 if( tctrl != NULL ) {
105 ldap_controls_free(*ctrls);
109 errmsg = "no memory";
114 tctrls[nctrls++] = tctrl;
115 tctrls[nctrls] = NULL;
117 tag = ber_scanf( ber, "{a" /*}*/, &tctrl->ldctl_oid );
119 if( tag == LBER_ERROR ) {
120 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
123 ldap_controls_free( tctrls );
125 errmsg = "decoding controls error";
129 tag = ber_peek_tag( ber, &len );
131 if( tag == LBER_BOOLEAN ) {
133 tag = ber_scanf( ber, "b", &crit );
135 if( tag == LBER_ERROR ) {
136 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
139 ldap_controls_free( tctrls );
141 errmsg = "decoding controls error";
145 tctrl->ldctl_iscritical = (crit != 0);
146 tag = ber_peek_tag( ber, &len );
149 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: oid=\"%s\" (%scritical)\n",
151 tctrl->ldctl_iscritical ? "" : "non",
154 if( tag == LBER_OCTETSTRING ) {
155 tag = ber_scanf( ber, "o", &tctrl->ldctl_value );
157 if( tag == LBER_ERROR ) {
158 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get value failed.\n",
161 ldap_controls_free( tctrls );
163 errmsg = "decoding controls error";
168 if( tctrl->ldctl_iscritical &&
169 !charray_inlist( supportedControls, tctrl->ldctl_oid ) )
171 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
172 errmsg = "critical extension is unavailable ";
180 Debug( LDAP_DEBUG_TRACE, "<= get_ctrls: %d %d %s\n",
181 nctrls, rc, errmsg ? errmsg : "");
183 if( sendres && rc != LDAP_SUCCESS ) {
185 send_ldap_disconnect( conn, op, rc, errmsg );
187 send_ldap_result( conn, op, rc,
188 NULL, errmsg, NULL, NULL );
196 int get_manageDSAit( Operation *op )
199 if( op == NULL || op->o_ctrls == NULL ) {
203 for( i=0; op->o_ctrls[i] != NULL; i++ ) {
204 if( strcmp( LDAP_CONTROL_MANAGEDSAIT, op->o_ctrls[i]->ldctl_oid )