2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2004 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
17 * LDAPv3 Extended Operation Request
18 * ExtendedRequest ::= [APPLICATION 23] SEQUENCE {
19 * requestName [0] LDAPOID,
20 * requestValue [1] OCTET STRING OPTIONAL
23 * LDAPv3 Extended Operation Response
24 * ExtendedResponse ::= [APPLICATION 24] SEQUENCE {
25 * COMPONENTS OF LDAPResult,
26 * responseName [10] LDAPOID OPTIONAL,
27 * response [11] OCTET STRING OPTIONAL
36 #include <ac/socket.h>
37 #include <ac/string.h>
43 #include "slapi/slapi.h"
46 #define UNSUPPORTED_EXOP "unsupported extended operation"
49 #define SLAP_EXOP_HIDE 0x0000
51 #define SLAP_EXOP_HIDE 0x8000
54 static struct extop_list {
55 struct extop_list *next;
58 SLAP_EXTOP_MAIN_FN *ext_main;
59 } *supp_ext_list = NULL;
61 static SLAP_EXTOP_MAIN_FN whoami_extop;
63 /* This list of built-in extops is for extops that are not part
64 * of backends or in external modules. Essentially, this is
65 * just a way to get built-in extops onto the extop list without
66 * having a separate init routine for each built-in extop.
68 const struct berval slap_EXOP_CANCEL = BER_BVC(LDAP_EXOP_X_CANCEL);
69 const struct berval slap_EXOP_WHOAMI = BER_BVC(LDAP_EXOP_X_WHO_AM_I);
70 const struct berval slap_EXOP_MODIFY_PASSWD = BER_BVC(LDAP_EXOP_MODIFY_PASSWD);
71 const struct berval slap_EXOP_START_TLS = BER_BVC(LDAP_EXOP_START_TLS);
74 const struct berval *oid;
76 SLAP_EXTOP_MAIN_FN *ext_main;
77 } builtin_extops[] = {
78 { &slap_EXOP_CANCEL, SLAP_EXOP_HIDE, cancel_extop },
79 { &slap_EXOP_WHOAMI, 0, whoami_extop },
80 { &slap_EXOP_MODIFY_PASSWD, 0, passwd_extop },
82 { &slap_EXOP_START_TLS, 0, starttls_extop },
88 static struct extop_list *find_extop(
89 struct extop_list *list, struct berval *oid );
92 get_supported_extop (int index)
94 struct extop_list *ext;
96 /* linear scan is slow, but this way doesn't force a
97 * big change on root_dse.c, where this routine is used.
99 for (ext = supp_ext_list; ext != NULL && --index >= 0; ext = ext->next) {
103 if (ext == NULL) return NULL;
109 int exop_root_dse_info( Entry *e )
111 AttributeDescription *ad_supportedExtension
112 = slap_schema.si_ad_supportedExtension;
113 struct berval vals[2];
114 struct extop_list *ext;
116 vals[1].bv_val = NULL;
119 for (ext = supp_ext_list; ext != NULL; ext = ext->next) {
120 if( ext->flags & SLAP_EXOP_HIDE ) continue;
124 if( attr_merge( e, ad_supportedExtension, vals, NULL ) ) {
138 struct berval reqdata = {0, NULL};
141 struct extop_list *ext = NULL;
143 #if defined(LDAP_SLAPI)
144 Slapi_PBlock *pb = op->o_pb;
145 SLAPI_FUNC funcAddr = NULL;
147 int msg_sent = FALSE;
148 #endif /* defined(LDAP_SLAPI) */
151 LDAP_LOG( OPERATION, ENTRY, "do_extended: conn %d\n", op->o_connid, 0, 0 );
153 Debug( LDAP_DEBUG_TRACE, "do_extended\n", 0, 0, 0 );
156 if( op->o_protocol < LDAP_VERSION3 ) {
158 LDAP_LOG( OPERATION, ERR,
159 "do_extended: protocol version (%d) too low.\n", op->o_protocol, 0, 0 );
161 Debug( LDAP_DEBUG_ANY,
162 "do_extended: protocol version (%d) too low\n",
163 op->o_protocol, 0 ,0 );
165 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "requires LDAPv3" );
170 if ( ber_scanf( op->o_ber, "{m" /*}*/, &op->ore_reqoid ) == LBER_ERROR ) {
172 LDAP_LOG( OPERATION, ERR, "do_extended: conn %d ber_scanf failed\n",
173 op->o_connid, 0, 0 );
175 Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
177 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
183 slapi_int_get_extop_plugin( &op->ore_reqoid, &funcAddr ); /* NS-SLAPI extended operation */
184 if( !funcAddr && !(ext = find_extop(supp_ext_list, &op->ore_reqoid )))
186 if( !(ext = find_extop(supp_ext_list, &op->ore_reqoid )))
190 LDAP_LOG( OPERATION, ERR,
191 "do_extended: conn %d unsupported operation \"%s\"\n",
192 op->o_connid, op->ore_reqoid.bv_val, 0 );
194 Debug( LDAP_DEBUG_ANY, "do_extended: unsupported operation \"%s\"\n",
195 op->ore_reqoid.bv_val, 0 ,0 );
197 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
198 "unsupported extended operation" );
202 tag = ber_peek_tag( op->o_ber, &len );
204 if( ber_peek_tag( op->o_ber, &len ) == LDAP_TAG_EXOP_REQ_VALUE ) {
205 if( ber_scanf( op->o_ber, "m", &reqdata ) == LBER_ERROR ) {
207 LDAP_LOG( OPERATION, ERR,
208 "do_extended: conn %d ber_scanf failed\n",
209 op->o_connid, 0, 0 );
211 Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
213 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
219 if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
221 LDAP_LOG( OPERATION, ERR,
222 "do_extended: conn %d get_ctrls failed\n", op->o_connid, 0, 0 );
224 Debug( LDAP_DEBUG_ANY, "do_extended: get_ctrls failed\n", 0, 0 ,0 );
229 /* check for controls inappropriate for all extended operations */
230 if( get_manageDSAit( op ) == SLAP_CRITICAL_CONTROL ) {
231 send_ldap_error( op, rs,
232 LDAP_UNAVAILABLE_CRITICAL_EXTENSION,
233 "manageDSAit control inappropriate" );
238 LDAP_LOG( OPERATION, DETAIL1,
239 "do_extended: conn %d oid=%s\n.", op->o_connid, op->ore_reqoid.bv_val, 0 );
241 Debug( LDAP_DEBUG_ARGS, "do_extended: oid=%s\n", op->ore_reqoid.bv_val, 0 ,0 );
244 #if defined(LDAP_SLAPI)
245 if ( funcAddr != NULL ) {
246 rs->sr_err = slapi_pblock_set( pb, SLAPI_EXT_OP_REQ_OID,
247 (void *)op->ore_reqoid.bv_val);
248 if ( rs->sr_err != LDAP_SUCCESS ) {
249 rs->sr_err = LDAP_OTHER;
253 rs->sr_err = slapi_pblock_set( pb, SLAPI_EXT_OP_REQ_VALUE,
255 if ( rs->sr_err != LDAP_SUCCESS ) {
256 rs->sr_err = LDAP_OTHER;
260 rs->sr_err = slapi_int_pblock_set_operation( pb, op );
261 if ( rs->sr_err != LDAP_SUCCESS ) {
262 rs->sr_err = LDAP_OTHER;
266 extop_rc = (*funcAddr)( pb );
267 if ( extop_rc == SLAPI_PLUGIN_EXTENDED_SENT_RESULT ) {
270 } else if ( extop_rc == SLAPI_PLUGIN_EXTENDED_NOT_HANDLED ) {
271 rs->sr_err = LDAP_PROTOCOL_ERROR;
272 rs->sr_text = UNSUPPORTED_EXOP;
275 rs->sr_err = slapi_pblock_get( pb, SLAPI_EXT_OP_RET_OID,
277 if ( rs->sr_err != LDAP_SUCCESS ) {
281 rs->sr_err = slapi_pblock_get( pb, SLAPI_EXT_OP_RET_VALUE,
283 if ( rs->sr_err != LDAP_SUCCESS ) {
287 rs->sr_err = extop_rc;
288 send_ldap_extended( op, rs );
293 if ( rs->sr_err != LDAP_SUCCESS && msg_sent == FALSE ) {
294 send_ldap_result( op, rs );
297 if ( rs->sr_rspoid != NULL ) {
298 ch_free( (char *)rs->sr_rspoid );
301 if ( rs->sr_rspdata != NULL ) {
302 ber_bvfree( rs->sr_rspdata );
304 } else { /* start of OpenLDAP extended operation */
305 #endif /* defined( LDAP_SLAPI ) */
306 if (reqdata.bv_val) op->ore_reqdata = &reqdata;
307 rs->sr_err = (ext->ext_main)( op, rs );
309 if( rs->sr_err != SLAPD_ABANDON ) {
310 if ( rs->sr_err == LDAP_REFERRAL && rs->sr_ref == NULL ) {
311 rs->sr_ref = referral_rewrite( default_referral,
312 NULL, NULL, LDAP_SCOPE_DEFAULT );
313 if ( !rs->sr_ref ) rs->sr_ref = default_referral;
315 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
316 rs->sr_text = "referral missing";
320 send_ldap_extended( op, rs );
322 if ( rs->sr_ref != default_referral ) {
323 ber_bvarray_free( rs->sr_ref );
328 if ( rs->sr_rspoid != NULL ) {
329 free( (char *)rs->sr_rspoid );
332 if ( rs->sr_rspdata != NULL ) {
333 ber_bvfree( rs->sr_rspdata );
336 } /* end of OpenLDAP extended operation */
337 #endif /* LDAP_SLAPI */
345 struct berval *ext_oid,
346 slap_mask_t ext_flags,
347 SLAP_EXTOP_MAIN_FN *ext_main )
349 struct extop_list *ext;
351 if( ext_oid == NULL || ext_oid->bv_val == NULL ||
352 ext_oid->bv_val[0] == '\0' || ext_oid->bv_len == 0 ) return -1;
353 if(!ext_main) return -1;
355 ext = ch_calloc(1, sizeof(struct extop_list) + ext_oid->bv_len + 1);
359 ext->flags = ext_flags;
361 ext->oid.bv_val = (char *)(ext + 1);
362 AC_MEMCPY( ext->oid.bv_val, ext_oid->bv_val, ext_oid->bv_len );
363 ext->oid.bv_len = ext_oid->bv_len;
364 ext->oid.bv_val[ext->oid.bv_len] = '\0';
366 ext->ext_main = ext_main;
367 ext->next = supp_ext_list;
379 for (i = 0; builtin_extops[i].oid != NULL; i++) {
380 load_extop((struct berval *)builtin_extops[i].oid,
381 builtin_extops[i].flags,
382 builtin_extops[i].ext_main);
390 struct extop_list *ext;
392 /* we allocated the memory, so we have to free it, too. */
393 while ((ext = supp_ext_list) != NULL) {
394 supp_ext_list = ext->next;
400 static struct extop_list *
401 find_extop( struct extop_list *list, struct berval *oid )
403 struct extop_list *ext;
405 for (ext = list; ext; ext = ext->next) {
406 if (bvmatch(&ext->oid, oid))
420 if ( op->ore_reqdata != NULL ) {
421 /* no request data should be provided */
422 rs->sr_text = "no request data expected";
423 return LDAP_PROTOCOL_ERROR;
426 op->o_bd = op->o_conn->c_authz_backend;
427 if( backend_check_restrictions( op, rs,
428 (struct berval *)&slap_EXOP_WHOAMI ) != LDAP_SUCCESS ) {
432 bv = (struct berval *) ch_malloc( sizeof(struct berval) );
433 if( op->o_dn.bv_len ) {
434 bv->bv_len = op->o_dn.bv_len + sizeof("dn:")-1;
435 bv->bv_val = ch_malloc( bv->bv_len + 1 );
436 AC_MEMCPY( bv->bv_val, "dn:", sizeof("dn:")-1 );
437 AC_MEMCPY( &bv->bv_val[sizeof("dn:")-1], op->o_dn.bv_val,
439 bv->bv_val[bv->bv_len] = '\0';