3 * Copyright 1999-2003 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
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[] = {
62 #ifdef LDAP_EXOP_X_CANCEL
63 { BVC(LDAP_EXOP_X_CANCEL), cancel_extop },
65 { BVC(LDAP_EXOP_X_WHO_AM_I), whoami_extop },
66 { BVC(LDAP_EXOP_MODIFY_PASSWD), passwd_extop },
68 { BVC(LDAP_EXOP_START_TLS), starttls_extop },
74 static struct extop_list *find_extop(
75 struct extop_list *list, struct berval *oid );
78 get_supported_extop (int index)
80 struct extop_list *ext;
82 /* linear scan is slow, but this way doesn't force a
83 * big change on root_dse.c, where this routine is used.
85 for (ext = supp_ext_list; ext != NULL && --index >= 0; ext = ext->next) {
89 if (ext == NULL) return NULL;
100 int rc = LDAP_SUCCESS;
101 struct berval reqoid = {0, NULL};
102 struct berval reqdata = {0, NULL};
105 struct extop_list *ext;
109 struct berval *rspdata;
110 LDAPControl **rspctrls;
112 #if defined(LDAP_SLAPI)
113 Slapi_PBlock *pb = op->o_pb;
114 SLAPI_FUNC funcAddr = NULL;
116 int msg_sent = FALSE;
117 char *result_msg = "";
118 #endif /* defined(LDAP_SLAPI) */
121 LDAP_LOG( OPERATION, ENTRY, "do_extended: conn %d\n", conn->c_connid, 0, 0 );
123 Debug( LDAP_DEBUG_TRACE, "do_extended\n", 0, 0, 0 );
126 if( op->o_protocol < LDAP_VERSION3 ) {
128 LDAP_LOG( OPERATION, ERR,
129 "do_extended: protocol version (%d) too low.\n", op->o_protocol, 0, 0 );
131 Debug( LDAP_DEBUG_ANY,
132 "do_extended: protocol version (%d) too low\n",
133 op->o_protocol, 0 ,0 );
135 send_ldap_disconnect( conn, op,
136 LDAP_PROTOCOL_ERROR, "requires LDAPv3" );
141 if ( ber_scanf( op->o_ber, "{m" /*}*/, &reqoid ) == LBER_ERROR ) {
143 LDAP_LOG( OPERATION, ERR, "do_extended: conn %d ber_scanf failed\n",
144 conn->c_connid, 0, 0 );
146 Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
148 send_ldap_disconnect( conn, op,
149 LDAP_PROTOCOL_ERROR, "decoding error" );
154 if( !(ext = find_extop(supp_ext_list, &reqoid))
160 /* Netscape extended operation */
161 getPluginFunc( &reqoid, &funcAddr );
165 LDAP_LOG( OPERATION, ERR,
166 "do_extended: conn %d unsupported operation \"%s\"\n",
167 conn->c_connid, reqoid.bv_val, 0 );
169 Debug( LDAP_DEBUG_ANY, "do_extended: unsupported operation \"%s\"\n",
170 reqoid.bv_val, 0 ,0 );
172 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
173 NULL, "unsupported extended operation", NULL, NULL );
177 op->o_extendedop = reqoid.bv_val;
179 tag = ber_peek_tag( op->o_ber, &len );
181 if( ber_peek_tag( op->o_ber, &len ) == LDAP_TAG_EXOP_REQ_VALUE ) {
182 if( ber_scanf( op->o_ber, "m", &reqdata ) == LBER_ERROR ) {
184 LDAP_LOG( OPERATION, ERR,
185 "do_extended: conn %d ber_scanf failed\n",
186 conn->c_connid, 0, 0 );
188 Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
190 send_ldap_disconnect( conn, op,
191 LDAP_PROTOCOL_ERROR, "decoding error" );
197 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
199 LDAP_LOG( OPERATION, ERR,
200 "do_extended: conn %d get_ctrls failed\n", conn->c_connid, 0, 0 );
202 Debug( LDAP_DEBUG_ANY, "do_extended: get_ctrls failed\n", 0, 0 ,0 );
207 /* check for controls inappropriate for all extended operations */
208 if( get_manageDSAit( op ) == SLAP_CRITICAL_CONTROL ) {
209 send_ldap_result( conn, op,
210 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION,
211 NULL, "manageDSAit control inappropriate",
217 LDAP_LOG( OPERATION, DETAIL1,
218 "do_extended: conn %d oid=%d\n.", conn->c_connid, reqoid.bv_val, 0 );
220 Debug( LDAP_DEBUG_ARGS, "do_extended: oid=%s\n", reqoid.bv_val, 0 ,0 );
229 #if defined(LDAP_SLAPI)
230 if (ext != NULL) { /* OpenLDAP extended operation */
231 #endif /* defined(LDAP_SLAPI) */
233 rc = (ext->ext_main)( conn, op,
234 reqoid.bv_val, reqdata.bv_val ? &reqdata : NULL,
235 &rspoid, &rspdata, &rspctrls, &text, &refs );
237 if( rc != SLAPD_ABANDON ) {
238 if ( rc == LDAP_REFERRAL && refs == NULL ) {
239 refs = referral_rewrite( default_referral,
240 NULL, NULL, LDAP_SCOPE_DEFAULT );
243 send_ldap_extended( conn, op, rc, NULL, text, refs,
244 rspoid, rspdata, rspctrls );
246 ber_bvarray_free( refs );
249 if ( rspoid != NULL ) {
253 if ( rspdata != NULL ) {
254 ber_bvfree( rspdata );
257 #if defined( LDAP_SLAPI )
258 goto done; /* end of OpenLDAP extended operation */
260 } else { /* start of Netscape extended operation */
261 rc = slapi_pblock_set( pb, SLAPI_EXT_OP_REQ_OID,
262 (void *)reqoid.bv_val);
263 if ( rc != LDAP_SUCCESS ) {
268 rc = slapi_pblock_set( pb, SLAPI_EXT_OP_REQ_VALUE,
270 if ( rc != LDAP_SUCCESS ) {
275 rc = slapi_x_connection_set_pb( pb, conn );
276 if ( rc != LDAP_SUCCESS ) {
281 rc = slapi_x_operation_set_pb( pb, op );
282 if ( rc != LDAP_SUCCESS ) {
287 extop_rc = (*funcAddr)( pb );
288 if ( extop_rc == SLAPI_PLUGIN_EXTENDED_SENT_RESULT ) {
291 } else if ( extop_rc == SLAPI_PLUGIN_EXTENDED_NOT_HANDLED ) {
292 rc = LDAP_PROTOCOL_ERROR;
293 result_msg = UNSUPPORTED_EXTENDEDOP;
296 rc = slapi_pblock_get( pb, SLAPI_EXT_OP_RET_OID,
298 if ( rc != LDAP_SUCCESS ) {
302 rc = slapi_pblock_get( pb, SLAPI_EXT_OP_RET_VALUE,
304 if ( rc != LDAP_SUCCESS ) {
308 send_ldap_extended( conn, op, extop_rc, NULL, text,
309 refs, rspoid, rspdata, rspctrls );
314 if ( rc != LDAP_SUCCESS && msg_sent == FALSE ) {
315 send_ldap_result( conn, op, rc, NULL, result_msg,
319 if ( rspoid != NULL ) {
323 if ( rspdata != NULL ) {
324 ber_bvfree( rspdata );
327 } /* end of Netscape extended operation */
328 #endif /* defined( LDAP_SLAPI ) */
337 SLAP_EXTOP_MAIN_FN *ext_main )
339 struct extop_list *ext;
341 if( ext_oid == NULL || *ext_oid == '\0' ) return -1;
342 if(!ext_main) return -1;
344 ext = ch_calloc(1, sizeof(struct extop_list));
348 ber_str2bv( ext_oid, 0, 1, &ext->oid );
349 if (ext->oid.bv_val == NULL) {
354 ext->ext_main = ext_main;
355 ext->next = supp_ext_list;
367 for (i = 0; builtin_extops[i].oid.bv_val != NULL; i++) {
368 load_extop(builtin_extops[i].oid.bv_val, builtin_extops[i].ext_main);
376 struct extop_list *ext;
378 /* we allocated the memory, so we have to free it, too. */
379 while ((ext = supp_ext_list) != NULL) {
380 supp_ext_list = ext->next;
381 if (ext->oid.bv_val != NULL)
382 ch_free(ext->oid.bv_val);
388 static struct extop_list *
389 find_extop( struct extop_list *list, struct berval *oid )
391 struct extop_list *ext;
393 for (ext = list; ext; ext = ext->next) {
394 if (bvmatch(&ext->oid, oid))
406 struct berval * reqdata,
408 struct berval ** rspdata,
409 LDAPControl ***rspctrls,
415 if ( reqdata != NULL ) {
416 /* no request data should be provided */
417 *text = "no request data expected";
418 return LDAP_PROTOCOL_ERROR;
423 struct berval whoami = BER_BVC( LDAP_EXOP_X_WHO_AM_I );
425 rc = backend_check_restrictions( conn->c_authz_backend,
426 conn, op, &whoami, text );
428 if( rc != LDAP_SUCCESS ) return rc;
431 bv = (struct berval *) ch_malloc( sizeof(struct berval) );
432 if( op->o_dn.bv_len ) {
433 bv->bv_len = op->o_dn.bv_len + sizeof("dn:")-1;
434 bv->bv_val = ch_malloc( bv->bv_len + 1 );
435 AC_MEMCPY( bv->bv_val, "dn:", sizeof("dn:")-1 );
436 AC_MEMCPY( &bv->bv_val[sizeof("dn:")-1], op->o_dn.bv_val,
438 bv->bv_val[bv->bv_len] = '\0';