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>
36 static struct extop_list {
37 struct extop_list *next;
39 SLAP_EXTOP_MAIN_FN *ext_main;
40 } *supp_ext_list = NULL;
42 static SLAP_EXTOP_MAIN_FN whoami_extop;
44 /* this list of built-in extops is for extops that are not part
45 * of backends or in external modules. essentially, this is
46 * just a way to get built-in extops onto the extop list without
47 * having a separate init routine for each built-in extop.
51 SLAP_EXTOP_MAIN_FN *ext_main;
52 } builtin_extops[] = {
54 { LDAP_EXOP_START_TLS, starttls_extop },
56 { LDAP_EXOP_MODIFY_PASSWD, passwd_extop },
57 { LDAP_EXOP_X_WHO_AM_I, whoami_extop },
62 static struct extop_list *find_extop(
63 struct extop_list *list, char *oid );
66 get_supported_extop (int index)
68 struct extop_list *ext;
70 /* linear scan is slow, but this way doesn't force a
71 * big change on root_dse.c, where this routine is used.
73 for (ext = supp_ext_list; ext != NULL && --index >= 0; ext = ext->next) {
77 if (ext == NULL) return NULL;
88 int rc = LDAP_SUCCESS;
90 struct berval *reqdata;
93 struct extop_list *ext;
97 struct berval *rspdata;
98 LDAPControl **rspctrls;
101 LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
102 "do_extended: conn %d\n", conn->c_connid ));
104 Debug( LDAP_DEBUG_TRACE, "do_extended\n", 0, 0, 0 );
109 if( op->o_protocol < LDAP_VERSION3 ) {
111 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
112 "do_extended: protocol version (%d) too low.\n",
115 Debug( LDAP_DEBUG_ANY,
116 "do_extended: protocol version (%d) too low\n",
117 op->o_protocol, 0 ,0 );
119 send_ldap_disconnect( conn, op,
120 LDAP_PROTOCOL_ERROR, "requires LDAPv3" );
125 if ( ber_scanf( op->o_ber, "{a" /*}*/, &reqoid ) == LBER_ERROR ) {
127 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
128 "do_extended: conn %d ber_scanf failed\n", conn->c_connid ));
130 Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
132 send_ldap_disconnect( conn, op,
133 LDAP_PROTOCOL_ERROR, "decoding error" );
138 if( !(ext = find_extop(supp_ext_list, reqoid)) ) {
140 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
141 "do_extended: conn %d unsupported operation \"%s\"\n",
142 conn->c_connid, reqoid ));
144 Debug( LDAP_DEBUG_ANY, "do_extended: unsupported operation \"%s\"\n",
147 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
148 NULL, "unsupported extended operation", NULL, NULL );
152 tag = ber_peek_tag( op->o_ber, &len );
154 if( ber_peek_tag( op->o_ber, &len ) == LDAP_TAG_EXOP_REQ_VALUE ) {
155 if( ber_scanf( op->o_ber, "O", &reqdata ) == LBER_ERROR ) {
157 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
158 "do_extended: conn %d ber_scanf failed\n", conn->c_connid ));
160 Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
162 send_ldap_disconnect( conn, op,
163 LDAP_PROTOCOL_ERROR, "decoding error" );
169 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
171 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
172 "do_extended: conn %d get_ctrls failed\n", conn->c_connid ));
174 Debug( LDAP_DEBUG_ANY, "do_extended: get_ctrls failed\n", 0, 0 ,0 );
179 /* check for controls inappropriate for all extended operations */
180 if( get_manageDSAit( op ) == SLAP_CRITICAL_CONTROL ) {
181 send_ldap_result( conn, op,
182 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION,
183 NULL, "manageDSAit control inappropriate",
189 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
190 "do_extended: conn %d oid=%d\n.", conn->c_connid, reqoid ));
192 Debug( LDAP_DEBUG_ARGS, "do_extended: oid=%s\n", reqoid, 0 ,0 );
201 rc = (ext->ext_main)( conn, op,
203 &rspoid, &rspdata, &rspctrls, &text, &refs );
205 if( rc != SLAPD_ABANDON ) {
206 if ( rc == LDAP_REFERRAL && refs == NULL ) {
207 refs = referral_rewrite( default_referral,
208 NULL, NULL, LDAP_SCOPE_DEFAULT );
211 send_ldap_extended( conn, op, rc, NULL, text, refs,
212 rspoid, rspdata, rspctrls );
214 ber_bvarray_free( refs );
217 if ( rspoid != NULL ) {
221 if ( rspdata != NULL ) {
222 ber_bvfree( rspdata );
226 if ( reqdata != NULL ) {
227 ber_bvfree( reqdata );
229 if ( reqoid != NULL ) {
239 SLAP_EXTOP_MAIN_FN *ext_main )
241 struct extop_list *ext;
243 if( ext_oid == NULL || *ext_oid == '\0' ) return -1;
244 if(!ext_main) return -1;
246 ext = ch_calloc(1, sizeof(struct extop_list));
250 ext->oid = ch_strdup( ext_oid );
251 if (ext->oid == NULL) {
256 ext->ext_main = ext_main;
257 ext->next = supp_ext_list;
269 for (i = 0; builtin_extops[i].oid != NULL; i++) {
270 load_extop(builtin_extops[i].oid, builtin_extops[i].ext_main);
278 struct extop_list *ext;
280 /* we allocated the memory, so we have to free it, too. */
281 while ((ext = supp_ext_list) != NULL) {
282 supp_ext_list = ext->next;
283 if (ext->oid != NULL)
290 static struct extop_list *
291 find_extop( struct extop_list *list, char *oid )
293 struct extop_list *ext;
295 for (ext = list; ext; ext = ext->next) {
296 if (strcmp(ext->oid, oid) == 0)
308 struct berval * reqdata,
310 struct berval ** rspdata,
311 LDAPControl ***rspctrls,
317 if ( reqdata != NULL ) {
318 /* no request data should be provided */
319 *text = "no request data expected";
320 return LDAP_PROTOCOL_ERROR;
323 bv = (struct berval *) ch_malloc( sizeof(struct berval) );
324 if( op->o_dn.bv_len ) {
325 bv->bv_len = op->o_dn.bv_len + sizeof("dn:")-1;
326 bv->bv_val = ch_malloc( bv->bv_len + 1 );
327 AC_MEMCPY( bv->bv_val, "dn:", sizeof("dn:")-1 );
328 AC_MEMCPY( &bv->bv_val[sizeof("dn:")-1], op->o_dn.bv_val,
330 bv->bv_val[bv->bv_len] = '\0';