3 * Copyright 1999-2002 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
31 #include <ac/socket.h>
32 #include <ac/string.h>
37 static struct extop_list {
38 struct extop_list *next;
40 SLAP_EXTOP_MAIN_FN *ext_main;
41 } *supp_ext_list = NULL;
43 static SLAP_EXTOP_MAIN_FN whoami_extop;
45 /* BerVal Constant initializer */
47 #define BVC(x) {sizeof(x)-1, x}
49 /* this list of built-in extops is for extops that are not part
50 * of backends or in external modules. essentially, this is
51 * just a way to get built-in extops onto the extop list without
52 * having a separate init routine for each built-in extop.
56 SLAP_EXTOP_MAIN_FN *ext_main;
57 } builtin_extops[] = {
59 { BVC(LDAP_EXOP_START_TLS), starttls_extop },
61 { BVC(LDAP_EXOP_MODIFY_PASSWD), passwd_extop },
62 { BVC(LDAP_EXOP_X_WHO_AM_I), whoami_extop },
67 static struct extop_list *find_extop(
68 struct extop_list *list, struct berval *oid );
71 get_supported_extop (int index)
73 struct extop_list *ext;
75 /* linear scan is slow, but this way doesn't force a
76 * big change on root_dse.c, where this routine is used.
78 for (ext = supp_ext_list; ext != NULL && --index >= 0; ext = ext->next) {
82 if (ext == NULL) return NULL;
93 int rc = LDAP_SUCCESS;
94 struct berval reqoid = {0, NULL};
95 struct berval reqdata = {0, NULL};
98 struct extop_list *ext;
102 struct berval *rspdata;
103 LDAPControl **rspctrls;
106 LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
107 "do_extended: conn %d\n", conn->c_connid ));
109 Debug( LDAP_DEBUG_TRACE, "do_extended\n", 0, 0, 0 );
112 if( op->o_protocol < LDAP_VERSION3 ) {
114 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
115 "do_extended: protocol version (%d) too low.\n",
118 Debug( LDAP_DEBUG_ANY,
119 "do_extended: protocol version (%d) too low\n",
120 op->o_protocol, 0 ,0 );
122 send_ldap_disconnect( conn, op,
123 LDAP_PROTOCOL_ERROR, "requires LDAPv3" );
128 if ( ber_scanf( op->o_ber, "{m" /*}*/, &reqoid ) == LBER_ERROR ) {
130 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
131 "do_extended: conn %d ber_scanf failed\n", conn->c_connid ));
133 Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
135 send_ldap_disconnect( conn, op,
136 LDAP_PROTOCOL_ERROR, "decoding error" );
141 if( !(ext = find_extop(supp_ext_list, &reqoid)) ) {
143 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
144 "do_extended: conn %d unsupported operation \"%s\"\n",
145 conn->c_connid, reqoid.bv_val ));
147 Debug( LDAP_DEBUG_ANY, "do_extended: unsupported operation \"%s\"\n",
148 reqoid.bv_val, 0 ,0 );
150 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
151 NULL, "unsupported extended operation", NULL, NULL );
155 tag = ber_peek_tag( op->o_ber, &len );
157 if( ber_peek_tag( op->o_ber, &len ) == LDAP_TAG_EXOP_REQ_VALUE ) {
158 if( ber_scanf( op->o_ber, "m", &reqdata ) == LBER_ERROR ) {
160 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
161 "do_extended: conn %d ber_scanf failed\n", conn->c_connid ));
163 Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
165 send_ldap_disconnect( conn, op,
166 LDAP_PROTOCOL_ERROR, "decoding error" );
172 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
174 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
175 "do_extended: conn %d get_ctrls failed\n", conn->c_connid ));
177 Debug( LDAP_DEBUG_ANY, "do_extended: get_ctrls failed\n", 0, 0 ,0 );
182 /* check for controls inappropriate for all extended operations */
183 if( get_manageDSAit( op ) == SLAP_CRITICAL_CONTROL ) {
184 send_ldap_result( conn, op,
185 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION,
186 NULL, "manageDSAit control inappropriate",
192 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
193 "do_extended: conn %d oid=%d\n.", conn->c_connid, reqoid.bv_val ));
195 Debug( LDAP_DEBUG_ARGS, "do_extended: oid=%s\n", reqoid.bv_val, 0 ,0 );
204 rc = (ext->ext_main)( conn, op,
205 reqoid.bv_val, reqdata.bv_val ? &reqdata : NULL,
206 &rspoid, &rspdata, &rspctrls, &text, &refs );
208 if( rc != SLAPD_ABANDON ) {
209 if ( rc == LDAP_REFERRAL && refs == NULL ) {
210 refs = referral_rewrite( default_referral,
211 NULL, NULL, LDAP_SCOPE_DEFAULT );
214 send_ldap_extended( conn, op, rc, NULL, text, refs,
215 rspoid, rspdata, rspctrls );
217 ber_bvarray_free( refs );
220 if ( rspoid != NULL ) {
224 if ( rspdata != NULL ) {
225 ber_bvfree( rspdata );
235 SLAP_EXTOP_MAIN_FN *ext_main )
237 struct extop_list *ext;
239 if( ext_oid == NULL || *ext_oid == '\0' ) return -1;
240 if(!ext_main) return -1;
242 ext = ch_calloc(1, sizeof(struct extop_list));
246 ber_str2bv( ext_oid, 0, 1, &ext->oid );
247 if (ext->oid.bv_val == NULL) {
252 ext->ext_main = ext_main;
253 ext->next = supp_ext_list;
265 for (i = 0; builtin_extops[i].oid.bv_val != NULL; i++) {
266 load_extop(builtin_extops[i].oid.bv_val, builtin_extops[i].ext_main);
274 struct extop_list *ext;
276 /* we allocated the memory, so we have to free it, too. */
277 while ((ext = supp_ext_list) != NULL) {
278 supp_ext_list = ext->next;
279 if (ext->oid.bv_val != NULL)
280 ch_free(ext->oid.bv_val);
286 static struct extop_list *
287 find_extop( struct extop_list *list, struct berval *oid )
289 struct extop_list *ext;
291 for (ext = list; ext; ext = ext->next) {
292 if (ber_bvcmp(&ext->oid, oid) == 0)
304 struct berval * reqdata,
306 struct berval ** rspdata,
307 LDAPControl ***rspctrls,
314 if ( reqdata != NULL ) {
315 /* no request data should be provided */
316 *text = "no request data expected";
317 return LDAP_PROTOCOL_ERROR;
322 struct berval whoami = BER_BVC( LDAP_EXOP_X_WHO_AM_I );
324 rc = backend_check_restrictions( conn->c_authz_backend,
325 conn, op, &whoami, text );
327 if( rc != LDAP_SUCCESS ) return rc;
330 bv = (struct berval *) ch_malloc( sizeof(struct berval) );
331 if( op->o_dn.bv_len ) {
332 bv->bv_len = op->o_dn.bv_len + sizeof("dn:")-1;
333 bv->bv_val = ch_malloc( bv->bv_len + 1 );
334 AC_MEMCPY( bv->bv_val, "dn:", sizeof("dn:")-1 );
335 AC_MEMCPY( &bv->bv_val[sizeof("dn:")-1], op->o_dn.bv_val,
337 bv->bv_val[bv->bv_len] = '\0';