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"
45 #define SLAP_EXOP_HIDE 0x0000
47 #define SLAP_EXOP_HIDE 0x8000
50 static struct extop_list {
51 struct extop_list *next;
54 SLAP_EXTOP_MAIN_FN *ext_main;
55 } *supp_ext_list = NULL;
57 static SLAP_EXTOP_MAIN_FN whoami_extop;
59 /* this list of built-in extops is for extops that are not part
60 * of backends or in external modules. essentially, this is
61 * just a way to get built-in extops onto the extop list without
62 * having a separate init routine for each built-in extop.
64 #ifdef LDAP_EXOP_X_CANCEL
65 const struct berval slap_EXOP_CANCEL = BER_BVC(LDAP_EXOP_X_CANCEL);
67 const struct berval slap_EXOP_WHOAMI = BER_BVC(LDAP_EXOP_X_WHO_AM_I);
68 const struct berval slap_EXOP_MODIFY_PASSWD = BER_BVC(LDAP_EXOP_MODIFY_PASSWD);
69 const struct berval slap_EXOP_START_TLS = BER_BVC(LDAP_EXOP_START_TLS);
72 const struct berval *oid;
74 SLAP_EXTOP_MAIN_FN *ext_main;
75 } builtin_extops[] = {
76 #ifdef LDAP_EXOP_X_CANCEL
77 { &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 );
91 int exop_root_dse_info( Entry *e )
93 AttributeDescription *ad_supportedExtension
94 = slap_schema.si_ad_supportedExtension;
95 struct berval vals[2];
96 struct extop_list *ext;
98 vals[1].bv_val = NULL;
101 for (ext = supp_ext_list; ext != NULL; ext = ext->next) {
102 if( ext->flags & SLAP_EXOP_HIDE ) continue;
106 if( attr_merge( e, ad_supportedExtension, vals, NULL ) ) {
120 struct berval reqdata = {0, NULL};
123 struct extop_list *ext = NULL;
125 #if defined(LDAP_SLAPI)
126 Slapi_PBlock *pb = op->o_pb;
127 SLAPI_FUNC funcAddr = NULL;
129 int msg_sent = FALSE;
130 #endif /* defined(LDAP_SLAPI) */
133 LDAP_LOG( OPERATION, ENTRY, "do_extended: conn %d\n", op->o_connid, 0, 0 );
135 Debug( LDAP_DEBUG_TRACE, "do_extended\n", 0, 0, 0 );
138 if( op->o_protocol < LDAP_VERSION3 ) {
140 LDAP_LOG( OPERATION, ERR,
141 "do_extended: protocol version (%d) too low.\n", op->o_protocol, 0, 0 );
143 Debug( LDAP_DEBUG_ANY,
144 "do_extended: protocol version (%d) too low\n",
145 op->o_protocol, 0 ,0 );
147 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "requires LDAPv3" );
152 if ( ber_scanf( op->o_ber, "{m" /*}*/, &op->ore_reqoid ) == LBER_ERROR ) {
154 LDAP_LOG( OPERATION, ERR, "do_extended: conn %d ber_scanf failed\n",
155 op->o_connid, 0, 0 );
157 Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
159 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
165 getPluginFunc( &op->ore_reqoid, &funcAddr ); /* NS-SLAPI extended operation */
166 if( !funcAddr && !(ext = find_extop(supp_ext_list, &op->ore_reqoid )))
168 if( !(ext = find_extop(supp_ext_list, &op->ore_reqoid )))
172 LDAP_LOG( OPERATION, ERR,
173 "do_extended: conn %d unsupported operation \"%s\"\n",
174 op->o_connid, op->ore_reqoid.bv_val, 0 );
176 Debug( LDAP_DEBUG_ANY, "do_extended: unsupported operation \"%s\"\n",
177 op->ore_reqoid.bv_val, 0 ,0 );
179 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
180 "unsupported extended operation" );
184 tag = ber_peek_tag( op->o_ber, &len );
186 if( ber_peek_tag( op->o_ber, &len ) == LDAP_TAG_EXOP_REQ_VALUE ) {
187 if( ber_scanf( op->o_ber, "m", &reqdata ) == LBER_ERROR ) {
189 LDAP_LOG( OPERATION, ERR,
190 "do_extended: conn %d ber_scanf failed\n",
191 op->o_connid, 0, 0 );
193 Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
195 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
201 if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
203 LDAP_LOG( OPERATION, ERR,
204 "do_extended: conn %d get_ctrls failed\n", op->o_connid, 0, 0 );
206 Debug( LDAP_DEBUG_ANY, "do_extended: get_ctrls failed\n", 0, 0 ,0 );
211 /* check for controls inappropriate for all extended operations */
212 if( get_manageDSAit( op ) == SLAP_CRITICAL_CONTROL ) {
213 send_ldap_error( op, rs,
214 LDAP_UNAVAILABLE_CRITICAL_EXTENSION,
215 "manageDSAit control inappropriate" );
220 LDAP_LOG( OPERATION, DETAIL1,
221 "do_extended: conn %d oid=%s\n.", op->o_connid, op->ore_reqoid.bv_val, 0 );
223 Debug( LDAP_DEBUG_ARGS, "do_extended: oid=%s\n", op->ore_reqoid.bv_val, 0 ,0 );
226 #if defined(LDAP_SLAPI)
227 if (ext != NULL) { /* OpenLDAP extended operation */
228 #endif /* defined(LDAP_SLAPI) */
230 if (reqdata.bv_val) op->ore_reqdata = &reqdata;
231 rs->sr_err = (ext->ext_main)( op, rs );
233 if( rs->sr_err != SLAPD_ABANDON ) {
234 if ( rs->sr_err == LDAP_REFERRAL && rs->sr_ref == NULL ) {
235 rs->sr_ref = referral_rewrite( default_referral,
236 NULL, NULL, LDAP_SCOPE_DEFAULT );
239 send_ldap_extended( op, rs );
241 ber_bvarray_free( rs->sr_ref );
244 if ( rs->sr_rspoid != NULL ) {
245 free( (char *)rs->sr_rspoid );
248 if ( rs->sr_rspdata != NULL ) {
249 ber_bvfree( rs->sr_rspdata );
252 #if defined( LDAP_SLAPI )
253 goto done; /* end of OpenLDAP extended operation */
255 } else { /* start of Netscape extended operation */
256 rs->sr_err = slapi_pblock_set( pb, SLAPI_EXT_OP_REQ_OID,
257 (void *)op->ore_reqoid.bv_val);
258 if ( rs->sr_err != LDAP_SUCCESS ) {
259 rs->sr_err = LDAP_OTHER;
263 rs->sr_err = slapi_pblock_set( pb, SLAPI_EXT_OP_REQ_VALUE,
265 if ( rs->sr_err != LDAP_SUCCESS ) {
266 rs->sr_err = LDAP_OTHER;
270 rs->sr_err = slapi_x_pblock_set_operation( pb, op );
271 if ( rs->sr_err != LDAP_SUCCESS ) {
272 rs->sr_err = LDAP_OTHER;
276 extop_rc = (*funcAddr)( pb );
277 if ( extop_rc == SLAPI_PLUGIN_EXTENDED_SENT_RESULT ) {
280 } else if ( extop_rc == SLAPI_PLUGIN_EXTENDED_NOT_HANDLED ) {
281 rs->sr_err = LDAP_PROTOCOL_ERROR;
282 rs->sr_text = UNSUPPORTED_EXTENDEDOP;
285 rs->sr_err = slapi_pblock_get( pb, SLAPI_EXT_OP_RET_OID,
287 if ( rs->sr_err != LDAP_SUCCESS ) {
291 rs->sr_err = slapi_pblock_get( pb, SLAPI_EXT_OP_RET_VALUE,
293 if ( rs->sr_err != LDAP_SUCCESS ) {
297 rs->sr_err = extop_rc;
298 send_ldap_extended( op, rs );
303 if ( rs->sr_err != LDAP_SUCCESS && msg_sent == FALSE ) {
304 send_ldap_result( op, rs );
307 if ( rs->sr_rspoid != NULL ) {
308 ch_free( (char *)rs->sr_rspoid );
311 if ( rs->sr_rspdata != NULL ) {
312 ber_bvfree( rs->sr_rspdata );
315 } /* end of Netscape extended operation */
316 #endif /* defined( LDAP_SLAPI ) */
324 struct berval *ext_oid,
325 slap_mask_t ext_flags,
326 SLAP_EXTOP_MAIN_FN *ext_main )
328 struct extop_list *ext;
330 if( ext_oid == NULL || ext_oid->bv_val == NULL ||
331 ext_oid->bv_val[0] == '\0' || ext_oid->bv_len == 0 ) return -1;
332 if(!ext_main) return -1;
334 ext = ch_calloc(1, sizeof(struct extop_list) + ext_oid->bv_len + 1);
338 ext->flags = ext_flags;
340 ext->oid.bv_val = (char *)(ext + 1);
341 AC_MEMCPY( ext->oid.bv_val, ext_oid->bv_val, ext_oid->bv_len );
342 ext->oid.bv_len = ext_oid->bv_len;
343 ext->oid.bv_val[ext->oid.bv_len] = '\0';
345 ext->ext_main = ext_main;
346 ext->next = supp_ext_list;
358 for (i = 0; builtin_extops[i].oid != NULL; i++) {
359 load_extop((struct berval *)builtin_extops[i].oid,
360 builtin_extops[i].flags,
361 builtin_extops[i].ext_main);
369 struct extop_list *ext;
371 /* we allocated the memory, so we have to free it, too. */
372 while ((ext = supp_ext_list) != NULL) {
373 supp_ext_list = ext->next;
379 static struct extop_list *
380 find_extop( struct extop_list *list, struct berval *oid )
382 struct extop_list *ext;
384 for (ext = list; ext; ext = ext->next) {
385 if (bvmatch(&ext->oid, oid))
399 if ( op->ore_reqdata != NULL ) {
400 /* no request data should be provided */
401 rs->sr_text = "no request data expected";
402 return LDAP_PROTOCOL_ERROR;
405 op->o_bd = op->o_conn->c_authz_backend;
406 if( backend_check_restrictions( op, rs,
407 (struct berval *)&slap_EXOP_WHOAMI ) != LDAP_SUCCESS ) {
411 bv = (struct berval *) ch_malloc( sizeof(struct berval) );
412 if( op->o_dn.bv_len ) {
413 bv->bv_len = op->o_dn.bv_len + sizeof("dn:")-1;
414 bv->bv_val = ch_malloc( bv->bv_len + 1 );
415 AC_MEMCPY( bv->bv_val, "dn:", sizeof("dn:")-1 );
416 AC_MEMCPY( &bv->bv_val[sizeof("dn:")-1], op->o_dn.bv_val,
418 bv->bv_val[bv->bv_len] = '\0';