3 * Copyright 1999-2002 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.
13 * LDAPv3 Extended Operation Request
14 * ExtendedRequest ::= [APPLICATION 23] SEQUENCE {
15 * requestName [0] LDAPOID,
16 * requestValue [1] OCTET STRING OPTIONAL
19 * LDAPv3 Extended Operation Response
20 * ExtendedResponse ::= [APPLICATION 24] SEQUENCE {
21 * COMPONENTS OF LDAPResult,
22 * responseName [10] LDAPOID OPTIONAL,
23 * response [11] OCTET STRING OPTIONAL
29 #include "slapi_common.h"
32 #include <ac/socket.h>
33 #include <ac/string.h>
39 #define UNSUPPORTED_EXTENDEDOP "unsupported extended operation"
41 static struct extop_list {
42 struct extop_list *next;
44 SLAP_EXTOP_MAIN_FN *ext_main;
45 } *supp_ext_list = NULL;
47 static SLAP_EXTOP_MAIN_FN whoami_extop;
49 /* BerVal Constant initializer */
51 #define BVC(x) {sizeof(x)-1, x}
53 /* this list of built-in extops is for extops that are not part
54 * of backends or in external modules. essentially, this is
55 * just a way to get built-in extops onto the extop list without
56 * having a separate init routine for each built-in extop.
60 SLAP_EXTOP_MAIN_FN *ext_main;
61 } builtin_extops[] = {
63 { BVC(LDAP_EXOP_START_TLS), starttls_extop },
65 { BVC(LDAP_EXOP_MODIFY_PASSWD), passwd_extop },
66 { BVC(LDAP_EXOP_X_WHO_AM_I), whoami_extop },
71 static struct extop_list *find_extop(
72 struct extop_list *list, struct berval *oid );
75 get_supported_extop (int index)
77 struct extop_list *ext;
79 /* linear scan is slow, but this way doesn't force a
80 * big change on root_dse.c, where this routine is used.
82 for (ext = supp_ext_list; ext != NULL && --index >= 0; ext = ext->next) {
86 if (ext == NULL) return NULL;
97 int rc = LDAP_SUCCESS;
98 struct berval reqoid = {0, NULL};
99 struct berval reqdata = {0, NULL};
102 struct extop_list *ext;
106 struct berval *rspdata;
107 LDAPControl **rspctrls;
109 Slapi_PBlock *pb = op->o_pb;
110 SLAPI_FUNC funcAddr = NULL;
116 LDAP_LOG( OPERATION, ENTRY, "do_extended: conn %d\n", conn->c_connid, 0, 0 );
118 Debug( LDAP_DEBUG_TRACE, "do_extended\n", 0, 0, 0 );
121 if( op->o_protocol < LDAP_VERSION3 ) {
123 LDAP_LOG( OPERATION, ERR,
124 "do_extended: protocol version (%d) too low.\n", op->o_protocol, 0, 0 );
126 Debug( LDAP_DEBUG_ANY,
127 "do_extended: protocol version (%d) too low\n",
128 op->o_protocol, 0 ,0 );
130 send_ldap_disconnect( conn, op,
131 LDAP_PROTOCOL_ERROR, "requires LDAPv3" );
136 if ( ber_scanf( op->o_ber, "{m" /*}*/, &reqoid ) == LBER_ERROR ) {
138 LDAP_LOG( OPERATION, ERR, "do_extended: conn %d ber_scanf failed\n",
139 conn->c_connid, 0, 0 );
141 Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
143 send_ldap_disconnect( conn, op,
144 LDAP_PROTOCOL_ERROR, "decoding error" );
149 /* Netscape extended operation */
150 getPluginFunc( &reqoid, &funcAddr );
152 if( !(ext = find_extop(supp_ext_list, &reqoid)) && !(funcAddr) ) {
154 LDAP_LOG( OPERATION, ERR,
155 "do_extended: conn %d unsupported operation \"%s\"\n",
156 conn->c_connid, reqoid.bv_val, 0 );
158 Debug( LDAP_DEBUG_ANY, "do_extended: unsupported operation \"%s\"\n",
159 reqoid.bv_val, 0 ,0 );
161 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
162 NULL, "unsupported extended operation", NULL, NULL );
166 op->o_extendedop = reqoid.bv_val;
168 tag = ber_peek_tag( op->o_ber, &len );
170 if( ber_peek_tag( op->o_ber, &len ) == LDAP_TAG_EXOP_REQ_VALUE ) {
171 if( ber_scanf( op->o_ber, "m", &reqdata ) == LBER_ERROR ) {
173 LDAP_LOG( OPERATION, ERR,
174 "do_extended: conn %d ber_scanf failed\n",
175 conn->c_connid, 0, 0 );
177 Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
179 send_ldap_disconnect( conn, op,
180 LDAP_PROTOCOL_ERROR, "decoding error" );
186 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
188 LDAP_LOG( OPERATION, ERR,
189 "do_extended: conn %d get_ctrls failed\n", conn->c_connid, 0, 0 );
191 Debug( LDAP_DEBUG_ANY, "do_extended: get_ctrls failed\n", 0, 0 ,0 );
196 /* check for controls inappropriate for all extended operations */
197 if( get_manageDSAit( op ) == SLAP_CRITICAL_CONTROL ) {
198 send_ldap_result( conn, op,
199 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION,
200 NULL, "manageDSAit control inappropriate",
206 LDAP_LOG( OPERATION, DETAIL1,
207 "do_extended: conn %d oid=%d\n.", conn->c_connid, reqoid.bv_val, 0 );
209 Debug( LDAP_DEBUG_ARGS, "do_extended: oid=%s\n", reqoid.bv_val, 0 ,0 );
218 if (ext != NULL) { /* OpenLDAP extended operation */
219 rc = (ext->ext_main)( conn, op,
220 reqoid.bv_val, reqdata.bv_val ? &reqdata : NULL,
221 &rspoid, &rspdata, &rspctrls, &text, &refs );
223 if( rc != SLAPD_ABANDON ) {
224 if ( rc == LDAP_REFERRAL && refs == NULL ) {
225 refs = referral_rewrite( default_referral,
226 NULL, NULL, LDAP_SCOPE_DEFAULT );
229 send_ldap_extended( conn, op, rc, NULL, text, refs,
230 rspoid, rspdata, rspctrls );
232 ber_bvarray_free( refs );
235 if ( rspoid != NULL ) {
239 if ( rspdata != NULL ) {
240 ber_bvfree( rspdata );
242 #if !defined( LDAP_SLAPI )
244 #else /* defined( LDAP_SLAPI ) */
245 goto done; /* end of OpenLDAP extended operation */
247 } else { /* start of Netscape extended operation */
248 if ( ( rc = slapi_pblock_set( pb, SLAPI_EXT_OP_REQ_OID,(void *)reqoid.bv_val) ) == 0 &&
249 ( rc = slapi_pblock_set( pb, SLAPI_EXT_OP_REQ_VALUE,(void *)&reqdata) ) == 0 &&
250 ( rc = slapi_pblock_set( pb, SLAPI_CONNECTION,(void *)conn) ) == 0 &&
251 ( rc = slapi_pblock_set( pb, SLAPI_OPERATION, (void *)op) ) == 0) {
252 extop_rc = (*funcAddr)( pb );
253 if ( extop_rc == SLAPI_PLUGIN_EXTENDED_SENT_RESULT ) {
255 } else if ( extop_rc == SLAPI_PLUGIN_EXTENDED_NOT_HANDLED ) {
256 rc = LDAP_PROTOCOL_ERROR;
257 result_msg = UNSUPPORTED_EXTENDEDOP;
259 if ( ( rc = slapi_pblock_get(pb, SLAPI_EXT_OP_RET_OID,&rspoid) ) == 0 &&
260 ( rc = slapi_pblock_get(pb, SLAPI_EXT_OP_RET_VALUE,&rspdata) ) == 0 ) {
261 send_ldap_extended( conn, op, extop_rc, NULL, text, refs,
262 rspoid, rspdata, rspctrls );
265 rc = LDAP_OPERATIONS_ERROR;
269 rc = LDAP_OPERATIONS_ERROR;
272 if ( rc != LDAP_SUCCESS && msg_sent == FALSE ) {
273 send_ldap_result( conn, op, rc, NULL, result_msg, NULL, NULL );
275 if ( rspoid != NULL ) {
278 if ( rspdata != NULL ) {
279 ber_bvfree( rspdata );
282 } /* end of Netscape extended operation */
283 #endif /* defined( LDAP_SLAPI ) */
292 SLAP_EXTOP_MAIN_FN *ext_main )
294 struct extop_list *ext;
296 if( ext_oid == NULL || *ext_oid == '\0' ) return -1;
297 if(!ext_main) return -1;
299 ext = ch_calloc(1, sizeof(struct extop_list));
303 ber_str2bv( ext_oid, 0, 1, &ext->oid );
304 if (ext->oid.bv_val == NULL) {
309 ext->ext_main = ext_main;
310 ext->next = supp_ext_list;
322 for (i = 0; builtin_extops[i].oid.bv_val != NULL; i++) {
323 load_extop(builtin_extops[i].oid.bv_val, builtin_extops[i].ext_main);
331 struct extop_list *ext;
333 /* we allocated the memory, so we have to free it, too. */
334 while ((ext = supp_ext_list) != NULL) {
335 supp_ext_list = ext->next;
336 if (ext->oid.bv_val != NULL)
337 ch_free(ext->oid.bv_val);
343 static struct extop_list *
344 find_extop( struct extop_list *list, struct berval *oid )
346 struct extop_list *ext;
348 for (ext = list; ext; ext = ext->next) {
349 if (bvmatch(&ext->oid, oid))
361 struct berval * reqdata,
363 struct berval ** rspdata,
364 LDAPControl ***rspctrls,
370 if ( reqdata != NULL ) {
371 /* no request data should be provided */
372 *text = "no request data expected";
373 return LDAP_PROTOCOL_ERROR;
378 struct berval whoami = BER_BVC( LDAP_EXOP_X_WHO_AM_I );
380 rc = backend_check_restrictions( conn->c_authz_backend,
381 conn, op, &whoami, text );
383 if( rc != LDAP_SUCCESS ) return rc;
386 bv = (struct berval *) ch_malloc( sizeof(struct berval) );
387 if( op->o_dn.bv_len ) {
388 bv->bv_len = op->o_dn.bv_len + sizeof("dn:")-1;
389 bv->bv_val = ch_malloc( bv->bv_len + 1 );
390 AC_MEMCPY( bv->bv_val, "dn:", sizeof("dn:")-1 );
391 AC_MEMCPY( &bv->bv_val[sizeof("dn:")-1], op->o_dn.bv_val,
393 bv->bv_val[bv->bv_len] = '\0';