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 #if defined(LDAP_SLAPI)
110 Slapi_PBlock *pb = op->o_pb;
111 SLAPI_FUNC funcAddr = NULL;
113 int msg_sent = FALSE;
114 char *result_msg = "";
115 #endif /* defined(LDAP_SLAPI) */
118 LDAP_LOG( OPERATION, ENTRY, "do_extended: conn %d\n", conn->c_connid, 0, 0 );
120 Debug( LDAP_DEBUG_TRACE, "do_extended\n", 0, 0, 0 );
123 if( op->o_protocol < LDAP_VERSION3 ) {
125 LDAP_LOG( OPERATION, ERR,
126 "do_extended: protocol version (%d) too low.\n", op->o_protocol, 0, 0 );
128 Debug( LDAP_DEBUG_ANY,
129 "do_extended: protocol version (%d) too low\n",
130 op->o_protocol, 0 ,0 );
132 send_ldap_disconnect( conn, op,
133 LDAP_PROTOCOL_ERROR, "requires LDAPv3" );
138 if ( ber_scanf( op->o_ber, "{m" /*}*/, &reqoid ) == LBER_ERROR ) {
140 LDAP_LOG( OPERATION, ERR, "do_extended: conn %d ber_scanf failed\n",
141 conn->c_connid, 0, 0 );
143 Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
145 send_ldap_disconnect( conn, op,
146 LDAP_PROTOCOL_ERROR, "decoding error" );
151 #if !defined(LDAP_SLAPI)
152 if( !(ext = find_extop(supp_ext_list, &reqoid)) ) {
153 #else /* defined(LDAP_SLAPI) */
154 /* Netscape extended operation */
155 getPluginFunc( &reqoid, &funcAddr );
157 if( !(ext = find_extop(supp_ext_list, &reqoid)) && !(funcAddr) ) {
158 #endif /* defined(LDAP_SLAPI) */
160 LDAP_LOG( OPERATION, ERR,
161 "do_extended: conn %d unsupported operation \"%s\"\n",
162 conn->c_connid, reqoid.bv_val, 0 );
164 Debug( LDAP_DEBUG_ANY, "do_extended: unsupported operation \"%s\"\n",
165 reqoid.bv_val, 0 ,0 );
167 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
168 NULL, "unsupported extended operation", NULL, NULL );
172 op->o_extendedop = reqoid.bv_val;
174 tag = ber_peek_tag( op->o_ber, &len );
176 if( ber_peek_tag( op->o_ber, &len ) == LDAP_TAG_EXOP_REQ_VALUE ) {
177 if( ber_scanf( op->o_ber, "m", &reqdata ) == LBER_ERROR ) {
179 LDAP_LOG( OPERATION, ERR,
180 "do_extended: conn %d ber_scanf failed\n",
181 conn->c_connid, 0, 0 );
183 Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
185 send_ldap_disconnect( conn, op,
186 LDAP_PROTOCOL_ERROR, "decoding error" );
192 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
194 LDAP_LOG( OPERATION, ERR,
195 "do_extended: conn %d get_ctrls failed\n", conn->c_connid, 0, 0 );
197 Debug( LDAP_DEBUG_ANY, "do_extended: get_ctrls failed\n", 0, 0 ,0 );
202 /* check for controls inappropriate for all extended operations */
203 if( get_manageDSAit( op ) == SLAP_CRITICAL_CONTROL ) {
204 send_ldap_result( conn, op,
205 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION,
206 NULL, "manageDSAit control inappropriate",
212 LDAP_LOG( OPERATION, DETAIL1,
213 "do_extended: conn %d oid=%d\n.", conn->c_connid, reqoid.bv_val, 0 );
215 Debug( LDAP_DEBUG_ARGS, "do_extended: oid=%s\n", reqoid.bv_val, 0 ,0 );
224 #if defined(LDAP_SLAPI)
225 if (ext != NULL) { /* OpenLDAP extended operation */
226 #endif /* defined(LDAP_SLAPI) */
228 rc = (ext->ext_main)( conn, op,
229 reqoid.bv_val, reqdata.bv_val ? &reqdata : NULL,
230 &rspoid, &rspdata, &rspctrls, &text, &refs );
232 if( rc != SLAPD_ABANDON ) {
233 if ( rc == LDAP_REFERRAL && refs == NULL ) {
234 refs = referral_rewrite( default_referral,
235 NULL, NULL, LDAP_SCOPE_DEFAULT );
238 send_ldap_extended( conn, op, rc, NULL, text, refs,
239 rspoid, rspdata, rspctrls );
241 ber_bvarray_free( refs );
244 if ( rspoid != NULL ) {
248 if ( rspdata != NULL ) {
249 ber_bvfree( rspdata );
252 #if defined( LDAP_SLAPI )
253 goto done; /* end of OpenLDAP extended operation */
255 } else { /* start of Netscape extended operation */
256 rc = slapi_pblock_set( pb, SLAPI_EXT_OP_REQ_OID,
257 (void *)reqoid.bv_val);
258 if ( rc != LDAP_SUCCESS ) {
259 rc = LDAP_OPERATIONS_ERROR;
263 rc = slapi_pblock_set( pb, SLAPI_EXT_OP_REQ_VALUE,
265 if ( rc != LDAP_SUCCESS ) {
266 rc = LDAP_OPERATIONS_ERROR;
270 rc = slapi_pblock_set( pb, SLAPI_CONNECTION, (void *)conn );
271 if ( rc != LDAP_SUCCESS ) {
272 rc = LDAP_OPERATIONS_ERROR;
276 rc = slapi_pblock_set( pb, SLAPI_OPERATION, (void *)op);
277 if ( rc != LDAP_SUCCESS ) {
278 rc = LDAP_OPERATIONS_ERROR;
282 extop_rc = (*funcAddr)( pb );
283 if ( extop_rc == SLAPI_PLUGIN_EXTENDED_SENT_RESULT ) {
286 } else if ( extop_rc == SLAPI_PLUGIN_EXTENDED_NOT_HANDLED ) {
287 rc = LDAP_PROTOCOL_ERROR;
288 result_msg = UNSUPPORTED_EXTENDEDOP;
291 rc = slapi_pblock_get( pb, SLAPI_EXT_OP_RET_OID,
293 if ( rc != LDAP_SUCCESS ) {
297 rc = slapi_pblock_get( pb, SLAPI_EXT_OP_RET_VALUE,
299 if ( rc != LDAP_SUCCESS ) {
303 send_ldap_extended( conn, op, extop_rc, NULL, text,
304 refs, rspoid, rspdata, rspctrls );
309 if ( rc != LDAP_SUCCESS && msg_sent == FALSE ) {
310 send_ldap_result( conn, op, rc, NULL, result_msg,
314 if ( rspoid != NULL ) {
318 if ( rspdata != NULL ) {
319 ber_bvfree( rspdata );
322 } /* end of Netscape extended operation */
323 #endif /* defined( LDAP_SLAPI ) */
332 SLAP_EXTOP_MAIN_FN *ext_main )
334 struct extop_list *ext;
336 if( ext_oid == NULL || *ext_oid == '\0' ) return -1;
337 if(!ext_main) return -1;
339 ext = ch_calloc(1, sizeof(struct extop_list));
343 ber_str2bv( ext_oid, 0, 1, &ext->oid );
344 if (ext->oid.bv_val == NULL) {
349 ext->ext_main = ext_main;
350 ext->next = supp_ext_list;
362 for (i = 0; builtin_extops[i].oid.bv_val != NULL; i++) {
363 load_extop(builtin_extops[i].oid.bv_val, builtin_extops[i].ext_main);
371 struct extop_list *ext;
373 /* we allocated the memory, so we have to free it, too. */
374 while ((ext = supp_ext_list) != NULL) {
375 supp_ext_list = ext->next;
376 if (ext->oid.bv_val != NULL)
377 ch_free(ext->oid.bv_val);
383 static struct extop_list *
384 find_extop( struct extop_list *list, struct berval *oid )
386 struct extop_list *ext;
388 for (ext = list; ext; ext = ext->next) {
389 if (bvmatch(&ext->oid, oid))
401 struct berval * reqdata,
403 struct berval ** rspdata,
404 LDAPControl ***rspctrls,
410 if ( reqdata != NULL ) {
411 /* no request data should be provided */
412 *text = "no request data expected";
413 return LDAP_PROTOCOL_ERROR;
418 struct berval whoami = BER_BVC( LDAP_EXOP_X_WHO_AM_I );
420 rc = backend_check_restrictions( conn->c_authz_backend,
421 conn, op, &whoami, text );
423 if( rc != LDAP_SUCCESS ) return rc;
426 bv = (struct berval *) ch_malloc( sizeof(struct berval) );
427 if( op->o_dn.bv_len ) {
428 bv->bv_len = op->o_dn.bv_len + sizeof("dn:")-1;
429 bv->bv_val = ch_malloc( bv->bv_len + 1 );
430 AC_MEMCPY( bv->bv_val, "dn:", sizeof("dn:")-1 );
431 AC_MEMCPY( &bv->bv_val[sizeof("dn:")-1], op->o_dn.bv_val,
433 bv->bv_val[bv->bv_len] = '\0';