2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2007 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>
42 static struct extop_list {
43 struct extop_list *next;
46 SLAP_EXTOP_MAIN_FN *ext_main;
47 } *supp_ext_list = NULL;
49 static SLAP_EXTOP_MAIN_FN whoami_extop;
51 /* This list of built-in extops is for extops that are not part
52 * of backends or in external modules. Essentially, this is
53 * just a way to get built-in extops onto the extop list without
54 * having a separate init routine for each built-in extop.
57 const struct berval *oid;
59 SLAP_EXTOP_MAIN_FN *ext_main;
60 } builtin_extops[] = {
62 { &slap_EXOP_TXN_START, 0, txn_start_extop },
63 { &slap_EXOP_TXN_END, 0, txn_end_extop },
65 { &slap_EXOP_CANCEL, 0, cancel_extop },
66 { &slap_EXOP_WHOAMI, 0, whoami_extop },
67 { &slap_EXOP_MODIFY_PASSWD, SLAP_EXOP_WRITES, passwd_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;
93 int exop_root_dse_info( Entry *e )
95 AttributeDescription *ad_supportedExtension
96 = slap_schema.si_ad_supportedExtension;
97 struct berval vals[2];
98 struct extop_list *ext;
100 vals[1].bv_val = NULL;
103 for (ext = supp_ext_list; ext != NULL; ext = ext->next) {
104 if( ext->flags & SLAP_EXOP_HIDE ) continue;
108 if( attr_merge( e, ad_supportedExtension, vals, NULL ) ) {
122 struct berval reqdata = {0, NULL};
126 Debug( LDAP_DEBUG_TRACE, "do_extended\n", 0, 0, 0 );
128 if( op->o_protocol < LDAP_VERSION3 ) {
129 Debug( LDAP_DEBUG_ANY,
130 "do_extended: protocol version (%d) too low\n",
131 op->o_protocol, 0 ,0 );
132 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "requires LDAPv3" );
133 rs->sr_err = SLAPD_DISCONNECT;
137 if ( ber_scanf( op->o_ber, "{m" /*}*/, &op->ore_reqoid ) == LBER_ERROR ) {
138 Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
139 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
140 rs->sr_err = SLAPD_DISCONNECT;
144 tag = ber_peek_tag( op->o_ber, &len );
146 if( ber_peek_tag( op->o_ber, &len ) == LDAP_TAG_EXOP_REQ_VALUE ) {
147 if( ber_scanf( op->o_ber, "m", &reqdata ) == LBER_ERROR ) {
148 Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
149 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
150 rs->sr_err = SLAPD_DISCONNECT;
155 if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
156 Debug( LDAP_DEBUG_ANY, "do_extended: get_ctrls failed\n", 0, 0 ,0 );
160 /* check for controls inappropriate for all extended operations */
161 if( get_manageDSAit( op ) == SLAP_CONTROL_CRITICAL ) {
162 Statslog( LDAP_DEBUG_STATS, "%s EXT oid=%s\n",
163 op->o_log_prefix, op->ore_reqoid.bv_val, 0, 0, 0 );
164 send_ldap_error( op, rs,
165 LDAP_UNAVAILABLE_CRITICAL_EXTENSION,
166 "manageDSAit control inappropriate" );
170 /* FIXME: temporary? */
171 if ( reqdata.bv_val ) {
172 op->ore_reqdata = &reqdata;
175 op->o_bd = frontendDB;
176 rs->sr_err = frontendDB->be_extended( op, rs );
178 /* clean up in case some overlay set them? */
179 if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
180 if ( !BER_BVISNULL( &op->o_req_dn )
181 && op->o_req_ndn.bv_val != op->o_req_dn.bv_val )
183 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
185 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
186 BER_BVZERO( &op->o_req_dn );
187 BER_BVZERO( &op->o_req_ndn );
195 fe_extended( Operation *op, SlapReply *rs )
197 struct extop_list *ext = NULL;
198 struct berval reqdata = BER_BVNULL;
200 if (op->ore_reqdata) {
201 reqdata = *op->ore_reqdata;
204 ext = find_extop(supp_ext_list, &op->ore_reqoid );
206 Statslog( LDAP_DEBUG_STATS, "%s EXT oid=%s\n",
207 op->o_log_prefix, op->ore_reqoid.bv_val, 0, 0, 0 );
208 Debug( LDAP_DEBUG_ANY, "do_extended: unsupported operation \"%s\"\n",
209 op->ore_reqoid.bv_val, 0 ,0 );
210 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
211 "unsupported extended operation" );
215 op->ore_flags = ext->flags;
217 Debug( LDAP_DEBUG_ARGS, "do_extended: oid=%s\n",
218 op->ore_reqoid.bv_val, 0 ,0 );
220 { /* start of OpenLDAP extended operation */
221 BackendDB *bd = op->o_bd;
223 rs->sr_err = (ext->ext_main)( op, rs );
225 if( rs->sr_err != SLAPD_ABANDON ) {
226 if ( rs->sr_err == LDAP_REFERRAL && rs->sr_ref == NULL ) {
227 rs->sr_ref = referral_rewrite( default_referral,
228 NULL, NULL, LDAP_SCOPE_DEFAULT );
229 if ( !rs->sr_ref ) rs->sr_ref = default_referral;
231 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
232 rs->sr_text = "referral missing";
236 if ( op->o_bd == NULL )
238 send_ldap_extended( op, rs );
240 if ( rs->sr_ref != default_referral ) {
241 ber_bvarray_free( rs->sr_ref );
246 if ( rs->sr_rspoid != NULL ) {
247 free( (char *)rs->sr_rspoid );
250 if ( rs->sr_rspdata != NULL ) {
251 ber_bvfree( rs->sr_rspdata );
253 } /* end of OpenLDAP extended operation */
261 const struct berval *ext_oid,
262 slap_mask_t ext_flags,
263 SLAP_EXTOP_MAIN_FN *ext_main,
266 struct berval oidm = BER_BVNULL;
267 struct extop_list *ext;
274 if ( ext_oid == NULL || BER_BVISNULL( ext_oid ) ||
275 BER_BVISEMPTY( ext_oid ) )
280 if ( numericoidValidate( NULL, (struct berval *)ext_oid ) !=
283 oidm.bv_val = oidm_find( ext_oid->bv_val );
284 if ( ext_oid == NULL ) {
287 oidm.bv_len = strlen( oidm.bv_val );
291 for ( ext = supp_ext_list; ext; ext = ext->next ) {
292 if ( bvmatch( ext_oid, &ext->oid ) ) {
300 if ( flags == 0 || ext == NULL ) {
301 ext = ch_calloc( 1, sizeof(struct extop_list) + ext_oid->bv_len + 1 );
306 ext->oid.bv_val = (char *)(ext + 1);
307 AC_MEMCPY( ext->oid.bv_val, ext_oid->bv_val, ext_oid->bv_len );
308 ext->oid.bv_len = ext_oid->bv_len;
309 ext->oid.bv_val[ext->oid.bv_len] = '\0';
314 ext->flags = ext_flags;
315 ext->ext_main = ext_main;
318 ext->next = supp_ext_list;
330 for ( i = 0; builtin_extops[i].oid != NULL; i++ ) {
331 load_extop( (struct berval *)builtin_extops[i].oid,
332 builtin_extops[i].flags,
333 builtin_extops[i].ext_main );
341 struct extop_list *ext;
343 /* we allocated the memory, so we have to free it, too. */
344 while ((ext = supp_ext_list) != NULL) {
345 supp_ext_list = ext->next;
351 static struct extop_list *
352 find_extop( struct extop_list *list, struct berval *oid )
354 struct extop_list *ext;
356 for (ext = list; ext; ext = ext->next) {
357 if (bvmatch(&ext->oid, oid))
364 const struct berval slap_EXOP_WHOAMI = BER_BVC(LDAP_EXOP_WHO_AM_I);
373 if ( op->ore_reqdata != NULL ) {
374 /* no request data should be provided */
375 rs->sr_text = "no request data expected";
376 return LDAP_PROTOCOL_ERROR;
379 Statslog( LDAP_DEBUG_STATS, "%s WHOAMI\n",
380 op->o_log_prefix, 0, 0, 0, 0 );
382 op->o_bd = op->o_conn->c_authz_backend;
383 if( backend_check_restrictions( op, rs,
384 (struct berval *)&slap_EXOP_WHOAMI ) != LDAP_SUCCESS )
389 bv = (struct berval *) ch_malloc( sizeof(struct berval) );
390 if( op->o_dn.bv_len ) {
391 bv->bv_len = op->o_dn.bv_len + STRLENOF( "dn:" );
392 bv->bv_val = ch_malloc( bv->bv_len + 1 );
393 AC_MEMCPY( bv->bv_val, "dn:", STRLENOF( "dn:" ) );
394 AC_MEMCPY( &bv->bv_val[STRLENOF( "dn:" )], op->o_dn.bv_val,
396 bv->bv_val[bv->bv_len] = '\0';