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>
42 #define UNSUPPORTED_EXTENDEDOP "unsupported extended operation"
44 static struct extop_list {
45 struct extop_list *next;
47 SLAP_EXTOP_MAIN_FN *ext_main;
48 } *supp_ext_list = NULL;
50 static SLAP_EXTOP_MAIN_FN whoami_extop;
52 /* this list of built-in extops is for extops that are not part
53 * of backends or in external modules. essentially, this is
54 * just a way to get built-in extops onto the extop list without
55 * having a separate init routine for each built-in extop.
57 #ifdef LDAP_EXOP_X_CANCEL
58 const struct berval slap_EXOP_CANCEL = BER_BVC(LDAP_EXOP_X_CANCEL);
60 const struct berval slap_EXOP_WHOAMI = BER_BVC(LDAP_EXOP_X_WHO_AM_I);
61 const struct berval slap_EXOP_MODIFY_PASSWD = BER_BVC(LDAP_EXOP_MODIFY_PASSWD);
62 const struct berval slap_EXOP_START_TLS = BER_BVC(LDAP_EXOP_START_TLS);
65 const struct berval *oid;
66 SLAP_EXTOP_MAIN_FN *ext_main;
67 } builtin_extops[] = {
68 #ifdef LDAP_EXOP_X_CANCEL
69 { &slap_EXOP_CANCEL, cancel_extop },
71 { &slap_EXOP_WHOAMI, whoami_extop },
72 { &slap_EXOP_MODIFY_PASSWD, passwd_extop },
74 { &slap_EXOP_START_TLS, starttls_extop },
80 static struct extop_list *find_extop(
81 struct extop_list *list, struct berval *oid );
84 get_supported_extop (int index)
86 struct extop_list *ext;
88 /* linear scan is slow, but this way doesn't force a
89 * big change on root_dse.c, where this routine is used.
91 for (ext = supp_ext_list; ext != NULL && --index >= 0; ext = ext->next) {
95 if (ext == NULL) return NULL;
106 int rc = LDAP_SUCCESS;
107 struct berval reqoid = {0, NULL};
108 struct berval reqdata = {0, NULL};
111 struct extop_list *ext = NULL;
115 struct berval *rspdata;
116 LDAPControl **rspctrls;
118 #if defined(LDAP_SLAPI)
119 Slapi_PBlock *pb = op->o_pb;
120 SLAPI_FUNC funcAddr = NULL;
122 int msg_sent = FALSE;
123 char *result_msg = "";
124 #endif /* defined(LDAP_SLAPI) */
127 LDAP_LOG( OPERATION, ENTRY, "do_extended: conn %d\n", conn->c_connid, 0, 0 );
129 Debug( LDAP_DEBUG_TRACE, "do_extended\n", 0, 0, 0 );
132 if( op->o_protocol < LDAP_VERSION3 ) {
134 LDAP_LOG( OPERATION, ERR,
135 "do_extended: protocol version (%d) too low.\n", op->o_protocol, 0, 0 );
137 Debug( LDAP_DEBUG_ANY,
138 "do_extended: protocol version (%d) too low\n",
139 op->o_protocol, 0 ,0 );
141 send_ldap_disconnect( conn, op,
142 LDAP_PROTOCOL_ERROR, "requires LDAPv3" );
147 if ( ber_scanf( op->o_ber, "{m" /*}*/, &reqoid ) == LBER_ERROR ) {
149 LDAP_LOG( OPERATION, ERR, "do_extended: conn %d ber_scanf failed\n",
150 conn->c_connid, 0, 0 );
152 Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
154 send_ldap_disconnect( conn, op,
155 LDAP_PROTOCOL_ERROR, "decoding error" );
161 getPluginFunc( &reqoid, &funcAddr ); /* NS-SLAPI extended operation */
162 if( !funcAddr && !(ext = find_extop(supp_ext_list, &reqoid )))
164 if( !(ext = find_extop(supp_ext_list, &reqoid )))
168 LDAP_LOG( OPERATION, ERR,
169 "do_extended: conn %d unsupported operation \"%s\"\n",
170 conn->c_connid, reqoid.bv_val, 0 );
172 Debug( LDAP_DEBUG_ANY, "do_extended: unsupported operation \"%s\"\n",
173 reqoid.bv_val, 0 ,0 );
175 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
176 NULL, "unsupported extended operation", NULL, NULL );
180 op->o_extendedop = reqoid.bv_val;
182 tag = ber_peek_tag( op->o_ber, &len );
184 if( ber_peek_tag( op->o_ber, &len ) == LDAP_TAG_EXOP_REQ_VALUE ) {
185 if( ber_scanf( op->o_ber, "m", &reqdata ) == LBER_ERROR ) {
187 LDAP_LOG( OPERATION, ERR,
188 "do_extended: conn %d ber_scanf failed\n",
189 conn->c_connid, 0, 0 );
191 Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
193 send_ldap_disconnect( conn, op,
194 LDAP_PROTOCOL_ERROR, "decoding error" );
200 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
202 LDAP_LOG( OPERATION, ERR,
203 "do_extended: conn %d get_ctrls failed\n", conn->c_connid, 0, 0 );
205 Debug( LDAP_DEBUG_ANY, "do_extended: get_ctrls failed\n", 0, 0 ,0 );
210 /* check for controls inappropriate for all extended operations */
211 if( get_manageDSAit( op ) == SLAP_CRITICAL_CONTROL ) {
212 send_ldap_result( conn, op,
213 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION,
214 NULL, "manageDSAit control inappropriate",
220 LDAP_LOG( OPERATION, DETAIL1,
221 "do_extended: conn %d oid=%d\n.", conn->c_connid, reqoid.bv_val, 0 );
223 Debug( LDAP_DEBUG_ARGS, "do_extended: oid=%s\n", reqoid.bv_val, 0 ,0 );
232 #if defined(LDAP_SLAPI)
233 if (ext != NULL) { /* OpenLDAP extended operation */
234 #endif /* defined(LDAP_SLAPI) */
236 rc = (ext->ext_main)( conn, op,
237 &reqoid, reqdata.bv_val ? &reqdata : NULL,
238 &rspoid, &rspdata, &rspctrls, &text, &refs );
240 if( rc != SLAPD_ABANDON ) {
241 if ( rc == LDAP_REFERRAL && refs == NULL ) {
242 refs = referral_rewrite( default_referral,
243 NULL, NULL, LDAP_SCOPE_DEFAULT );
246 send_ldap_extended( conn, op, rc, NULL, text, refs,
247 rspoid, rspdata, rspctrls );
249 ber_bvarray_free( refs );
252 if ( rspoid != NULL ) {
256 if ( rspdata != NULL ) {
257 ber_bvfree( rspdata );
260 #if defined( LDAP_SLAPI )
261 goto done; /* end of OpenLDAP extended operation */
263 } else { /* start of Netscape extended operation */
264 rc = slapi_pblock_set( pb, SLAPI_EXT_OP_REQ_OID,
265 (void *)reqoid.bv_val);
266 if ( rc != LDAP_SUCCESS ) {
271 rc = slapi_pblock_set( pb, SLAPI_EXT_OP_REQ_VALUE,
273 if ( rc != LDAP_SUCCESS ) {
278 rc = slapi_x_connection_set_pb( pb, conn );
279 if ( rc != LDAP_SUCCESS ) {
284 rc = slapi_x_operation_set_pb( pb, op );
285 if ( rc != LDAP_SUCCESS ) {
290 extop_rc = (*funcAddr)( pb );
291 if ( extop_rc == SLAPI_PLUGIN_EXTENDED_SENT_RESULT ) {
294 } else if ( extop_rc == SLAPI_PLUGIN_EXTENDED_NOT_HANDLED ) {
295 rc = LDAP_PROTOCOL_ERROR;
296 result_msg = UNSUPPORTED_EXTENDEDOP;
299 rc = slapi_pblock_get( pb, SLAPI_EXT_OP_RET_OID,
301 if ( rc != LDAP_SUCCESS ) {
305 rc = slapi_pblock_get( pb, SLAPI_EXT_OP_RET_VALUE,
307 if ( rc != LDAP_SUCCESS ) {
311 send_ldap_extended( conn, op, extop_rc, NULL, text,
312 refs, rspoid, rspdata, rspctrls );
317 if ( rc != LDAP_SUCCESS && msg_sent == FALSE ) {
318 send_ldap_result( conn, op, rc, NULL, result_msg,
322 if ( rspoid != NULL ) {
326 if ( rspdata != NULL ) {
327 ber_bvfree( rspdata );
330 } /* end of Netscape extended operation */
331 #endif /* defined( LDAP_SLAPI ) */
339 struct berval *ext_oid,
340 SLAP_EXTOP_MAIN_FN *ext_main )
342 struct extop_list *ext;
344 if( ext_oid == NULL || ext_oid->bv_val == NULL ||
345 ext_oid->bv_val[0] == '\0' || ext_oid->bv_len == 0 ) return -1;
346 if(!ext_main) return -1;
348 ext = ch_calloc(1, sizeof(struct extop_list) + ext_oid->bv_len + 1);
352 ext->oid.bv_val = (char *)(ext + 1);
353 AC_MEMCPY( ext->oid.bv_val, ext_oid->bv_val, ext_oid->bv_len );
354 ext->oid.bv_len = ext_oid->bv_len;
355 ext->oid.bv_val[ext->oid.bv_len] = '\0';
357 ext->ext_main = ext_main;
358 ext->next = supp_ext_list;
370 for (i = 0; builtin_extops[i].oid != NULL; i++) {
371 load_extop((struct berval *)builtin_extops[i].oid, builtin_extops[i].ext_main);
379 struct extop_list *ext;
381 /* we allocated the memory, so we have to free it, too. */
382 while ((ext = supp_ext_list) != NULL) {
383 supp_ext_list = ext->next;
389 static struct extop_list *
390 find_extop( struct extop_list *list, struct berval *oid )
392 struct extop_list *ext;
394 for (ext = list; ext; ext = ext->next) {
395 if (bvmatch(&ext->oid, oid))
406 struct berval * reqoid,
407 struct berval * reqdata,
409 struct berval ** rspdata,
410 LDAPControl ***rspctrls,
416 if ( reqdata != NULL ) {
417 /* no request data should be provided */
418 *text = "no request data expected";
419 return LDAP_PROTOCOL_ERROR;
423 int rc = backend_check_restrictions( conn->c_authz_backend,
424 conn, op, (struct berval *)&slap_EXOP_WHOAMI, text );
426 if( rc != LDAP_SUCCESS ) return rc;
429 bv = (struct berval *) ch_malloc( sizeof(struct berval) );
430 if( op->o_dn.bv_len ) {
431 bv->bv_len = op->o_dn.bv_len + sizeof("dn:")-1;
432 bv->bv_val = ch_malloc( bv->bv_len + 1 );
433 AC_MEMCPY( bv->bv_val, "dn:", sizeof("dn:")-1 );
434 AC_MEMCPY( &bv->bv_val[sizeof("dn:")-1], op->o_dn.bv_val,
436 bv->bv_val[bv->bv_len] = '\0';