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[] = {
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 },
67 { BVC(LDAP_EXOP_X_CANCEL), cancel_extop },
72 static struct extop_list *find_extop(
73 struct extop_list *list, struct berval *oid );
76 get_supported_extop (int index)
78 struct extop_list *ext;
80 /* linear scan is slow, but this way doesn't force a
81 * big change on root_dse.c, where this routine is used.
83 for (ext = supp_ext_list; ext != NULL && --index >= 0; ext = ext->next) {
87 if (ext == NULL) return NULL;
98 int rc = LDAP_SUCCESS;
99 struct berval reqoid = {0, NULL};
100 struct berval reqdata = {0, NULL};
103 struct extop_list *ext;
107 struct berval *rspdata;
108 LDAPControl **rspctrls;
110 #if defined(LDAP_SLAPI)
111 Slapi_PBlock *pb = op->o_pb;
112 SLAPI_FUNC funcAddr = NULL;
114 int msg_sent = FALSE;
115 char *result_msg = "";
116 #endif /* defined(LDAP_SLAPI) */
119 LDAP_LOG( OPERATION, ENTRY, "do_extended: conn %d\n", conn->c_connid, 0, 0 );
121 Debug( LDAP_DEBUG_TRACE, "do_extended\n", 0, 0, 0 );
124 if( op->o_protocol < LDAP_VERSION3 ) {
126 LDAP_LOG( OPERATION, ERR,
127 "do_extended: protocol version (%d) too low.\n", op->o_protocol, 0, 0 );
129 Debug( LDAP_DEBUG_ANY,
130 "do_extended: protocol version (%d) too low\n",
131 op->o_protocol, 0 ,0 );
133 send_ldap_disconnect( conn, op,
134 LDAP_PROTOCOL_ERROR, "requires LDAPv3" );
139 if ( ber_scanf( op->o_ber, "{m" /*}*/, &reqoid ) == LBER_ERROR ) {
141 LDAP_LOG( OPERATION, ERR, "do_extended: conn %d ber_scanf failed\n",
142 conn->c_connid, 0, 0 );
144 Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
146 send_ldap_disconnect( conn, op,
147 LDAP_PROTOCOL_ERROR, "decoding error" );
152 #if !defined(LDAP_SLAPI)
153 if( !(ext = find_extop(supp_ext_list, &reqoid)) ) {
154 #else /* defined(LDAP_SLAPI) */
155 /* Netscape extended operation */
156 getPluginFunc( &reqoid, &funcAddr );
158 if( !(ext = find_extop(supp_ext_list, &reqoid)) && !(funcAddr) ) {
159 #endif /* defined(LDAP_SLAPI) */
161 LDAP_LOG( OPERATION, ERR,
162 "do_extended: conn %d unsupported operation \"%s\"\n",
163 conn->c_connid, reqoid.bv_val, 0 );
165 Debug( LDAP_DEBUG_ANY, "do_extended: unsupported operation \"%s\"\n",
166 reqoid.bv_val, 0 ,0 );
168 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
169 NULL, "unsupported extended operation", NULL, NULL );
173 op->o_extendedop = reqoid.bv_val;
175 tag = ber_peek_tag( op->o_ber, &len );
177 if( ber_peek_tag( op->o_ber, &len ) == LDAP_TAG_EXOP_REQ_VALUE ) {
178 if( ber_scanf( op->o_ber, "m", &reqdata ) == LBER_ERROR ) {
180 LDAP_LOG( OPERATION, ERR,
181 "do_extended: conn %d ber_scanf failed\n",
182 conn->c_connid, 0, 0 );
184 Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
186 send_ldap_disconnect( conn, op,
187 LDAP_PROTOCOL_ERROR, "decoding error" );
193 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
195 LDAP_LOG( OPERATION, ERR,
196 "do_extended: conn %d get_ctrls failed\n", conn->c_connid, 0, 0 );
198 Debug( LDAP_DEBUG_ANY, "do_extended: get_ctrls failed\n", 0, 0 ,0 );
203 /* check for controls inappropriate for all extended operations */
204 if( get_manageDSAit( op ) == SLAP_CRITICAL_CONTROL ) {
205 send_ldap_result( conn, op,
206 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION,
207 NULL, "manageDSAit control inappropriate",
213 LDAP_LOG( OPERATION, DETAIL1,
214 "do_extended: conn %d oid=%d\n.", conn->c_connid, reqoid.bv_val, 0 );
216 Debug( LDAP_DEBUG_ARGS, "do_extended: oid=%s\n", reqoid.bv_val, 0 ,0 );
225 #if defined(LDAP_SLAPI)
226 if (ext != NULL) { /* OpenLDAP extended operation */
227 #endif /* defined(LDAP_SLAPI) */
229 rc = (ext->ext_main)( conn, op,
230 reqoid.bv_val, reqdata.bv_val ? &reqdata : NULL,
231 &rspoid, &rspdata, &rspctrls, &text, &refs );
233 if( rc != SLAPD_ABANDON ) {
234 if ( rc == LDAP_REFERRAL && refs == NULL ) {
235 refs = referral_rewrite( default_referral,
236 NULL, NULL, LDAP_SCOPE_DEFAULT );
239 send_ldap_extended( conn, op, rc, NULL, text, refs,
240 rspoid, rspdata, rspctrls );
242 ber_bvarray_free( refs );
245 if ( rspoid != NULL ) {
249 if ( rspdata != NULL ) {
250 ber_bvfree( rspdata );
253 #if defined( LDAP_SLAPI )
254 goto done; /* end of OpenLDAP extended operation */
256 } else { /* start of Netscape extended operation */
257 rc = slapi_pblock_set( pb, SLAPI_EXT_OP_REQ_OID,
258 (void *)reqoid.bv_val);
259 if ( rc != LDAP_SUCCESS ) {
264 rc = slapi_pblock_set( pb, SLAPI_EXT_OP_REQ_VALUE,
266 if ( rc != LDAP_SUCCESS ) {
271 rc = slapi_x_connection_set_pb( pb, conn );
272 if ( rc != LDAP_SUCCESS ) {
277 rc = slapi_x_operation_set_pb( pb, op );
278 if ( rc != LDAP_SUCCESS ) {
283 extop_rc = (*funcAddr)( pb );
284 if ( extop_rc == SLAPI_PLUGIN_EXTENDED_SENT_RESULT ) {
287 } else if ( extop_rc == SLAPI_PLUGIN_EXTENDED_NOT_HANDLED ) {
288 rc = LDAP_PROTOCOL_ERROR;
289 result_msg = UNSUPPORTED_EXTENDEDOP;
292 rc = slapi_pblock_get( pb, SLAPI_EXT_OP_RET_OID,
294 if ( rc != LDAP_SUCCESS ) {
298 rc = slapi_pblock_get( pb, SLAPI_EXT_OP_RET_VALUE,
300 if ( rc != LDAP_SUCCESS ) {
304 send_ldap_extended( conn, op, extop_rc, NULL, text,
305 refs, rspoid, rspdata, rspctrls );
310 if ( rc != LDAP_SUCCESS && msg_sent == FALSE ) {
311 send_ldap_result( conn, op, rc, NULL, result_msg,
315 if ( rspoid != NULL ) {
319 if ( rspdata != NULL ) {
320 ber_bvfree( rspdata );
323 } /* end of Netscape extended operation */
324 #endif /* defined( LDAP_SLAPI ) */
333 SLAP_EXTOP_MAIN_FN *ext_main )
335 struct extop_list *ext;
337 if( ext_oid == NULL || *ext_oid == '\0' ) return -1;
338 if(!ext_main) return -1;
340 ext = ch_calloc(1, sizeof(struct extop_list));
344 ber_str2bv( ext_oid, 0, 1, &ext->oid );
345 if (ext->oid.bv_val == NULL) {
350 ext->ext_main = ext_main;
351 ext->next = supp_ext_list;
363 for (i = 0; builtin_extops[i].oid.bv_val != NULL; i++) {
364 load_extop(builtin_extops[i].oid.bv_val, builtin_extops[i].ext_main);
372 struct extop_list *ext;
374 /* we allocated the memory, so we have to free it, too. */
375 while ((ext = supp_ext_list) != NULL) {
376 supp_ext_list = ext->next;
377 if (ext->oid.bv_val != NULL)
378 ch_free(ext->oid.bv_val);
384 static struct extop_list *
385 find_extop( struct extop_list *list, struct berval *oid )
387 struct extop_list *ext;
389 for (ext = list; ext; ext = ext->next) {
390 if (bvmatch(&ext->oid, oid))
402 struct berval * reqdata,
404 struct berval ** rspdata,
405 LDAPControl ***rspctrls,
411 if ( reqdata != NULL ) {
412 /* no request data should be provided */
413 *text = "no request data expected";
414 return LDAP_PROTOCOL_ERROR;
419 struct berval whoami = BER_BVC( LDAP_EXOP_X_WHO_AM_I );
421 rc = backend_check_restrictions( conn->c_authz_backend,
422 conn, op, &whoami, text );
424 if( rc != LDAP_SUCCESS ) return rc;
427 bv = (struct berval *) ch_malloc( sizeof(struct berval) );
428 if( op->o_dn.bv_len ) {
429 bv->bv_len = op->o_dn.bv_len + sizeof("dn:")-1;
430 bv->bv_val = ch_malloc( bv->bv_len + 1 );
431 AC_MEMCPY( bv->bv_val, "dn:", sizeof("dn:")-1 );
432 AC_MEMCPY( &bv->bv_val[sizeof("dn:")-1], op->o_dn.bv_val,
434 bv->bv_val[bv->bv_len] = '\0';