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, ENTRY, "do_extended: conn %d\n", conn->c_connid, 0, 0 );
108 Debug( LDAP_DEBUG_TRACE, "do_extended\n", 0, 0, 0 );
111 if( op->o_protocol < LDAP_VERSION3 ) {
113 LDAP_LOG( OPERATION, ERR,
114 "do_extended: protocol version (%d) too low.\n", op->o_protocol, 0, 0 );
116 Debug( LDAP_DEBUG_ANY,
117 "do_extended: protocol version (%d) too low\n",
118 op->o_protocol, 0 ,0 );
120 send_ldap_disconnect( conn, op,
121 LDAP_PROTOCOL_ERROR, "requires LDAPv3" );
126 if ( ber_scanf( op->o_ber, "{m" /*}*/, &reqoid ) == LBER_ERROR ) {
128 LDAP_LOG( OPERATION, ERR, "do_extended: conn %d ber_scanf failed\n",
129 conn->c_connid, 0, 0 );
131 Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
133 send_ldap_disconnect( conn, op,
134 LDAP_PROTOCOL_ERROR, "decoding error" );
139 if( !(ext = find_extop(supp_ext_list, &reqoid)) ) {
141 LDAP_LOG( OPERATION, ERR,
142 "do_extended: conn %d unsupported operation \"%s\"\n",
143 conn->c_connid, reqoid.bv_val, 0 );
145 Debug( LDAP_DEBUG_ANY, "do_extended: unsupported operation \"%s\"\n",
146 reqoid.bv_val, 0 ,0 );
148 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
149 NULL, "unsupported extended operation", NULL, NULL );
153 tag = ber_peek_tag( op->o_ber, &len );
155 if( ber_peek_tag( op->o_ber, &len ) == LDAP_TAG_EXOP_REQ_VALUE ) {
156 if( ber_scanf( op->o_ber, "m", &reqdata ) == LBER_ERROR ) {
158 LDAP_LOG( OPERATION, ERR,
159 "do_extended: conn %d ber_scanf failed\n",
160 conn->c_connid, 0, 0 );
162 Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
164 send_ldap_disconnect( conn, op,
165 LDAP_PROTOCOL_ERROR, "decoding error" );
171 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
173 LDAP_LOG( OPERATION, ERR,
174 "do_extended: conn %d get_ctrls failed\n", conn->c_connid, 0, 0 );
176 Debug( LDAP_DEBUG_ANY, "do_extended: get_ctrls failed\n", 0, 0 ,0 );
181 /* check for controls inappropriate for all extended operations */
182 if( get_manageDSAit( op ) == SLAP_CRITICAL_CONTROL ) {
183 send_ldap_result( conn, op,
184 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION,
185 NULL, "manageDSAit control inappropriate",
191 LDAP_LOG( OPERATION, DETAIL1,
192 "do_extended: conn %d oid=%d\n.", conn->c_connid, reqoid.bv_val, 0 );
194 Debug( LDAP_DEBUG_ARGS, "do_extended: oid=%s\n", reqoid.bv_val, 0 ,0 );
203 rc = (ext->ext_main)( conn, op,
204 reqoid.bv_val, reqdata.bv_val ? &reqdata : NULL,
205 &rspoid, &rspdata, &rspctrls, &text, &refs );
207 if( rc != SLAPD_ABANDON ) {
208 if ( rc == LDAP_REFERRAL && refs == NULL ) {
209 refs = referral_rewrite( default_referral,
210 NULL, NULL, LDAP_SCOPE_DEFAULT );
213 send_ldap_extended( conn, op, rc, NULL, text, refs,
214 rspoid, rspdata, rspctrls );
216 ber_bvarray_free( refs );
219 if ( rspoid != NULL ) {
223 if ( rspdata != NULL ) {
224 ber_bvfree( rspdata );
234 SLAP_EXTOP_MAIN_FN *ext_main )
236 struct extop_list *ext;
238 if( ext_oid == NULL || *ext_oid == '\0' ) return -1;
239 if(!ext_main) return -1;
241 ext = ch_calloc(1, sizeof(struct extop_list));
245 ber_str2bv( ext_oid, 0, 1, &ext->oid );
246 if (ext->oid.bv_val == NULL) {
251 ext->ext_main = ext_main;
252 ext->next = supp_ext_list;
264 for (i = 0; builtin_extops[i].oid.bv_val != NULL; i++) {
265 load_extop(builtin_extops[i].oid.bv_val, builtin_extops[i].ext_main);
273 struct extop_list *ext;
275 /* we allocated the memory, so we have to free it, too. */
276 while ((ext = supp_ext_list) != NULL) {
277 supp_ext_list = ext->next;
278 if (ext->oid.bv_val != NULL)
279 ch_free(ext->oid.bv_val);
285 static struct extop_list *
286 find_extop( struct extop_list *list, struct berval *oid )
288 struct extop_list *ext;
290 for (ext = list; ext; ext = ext->next) {
291 if (bvmatch(&ext->oid, oid))
303 struct berval * reqdata,
305 struct berval ** rspdata,
306 LDAPControl ***rspctrls,
312 if ( reqdata != NULL ) {
313 /* no request data should be provided */
314 *text = "no request data expected";
315 return LDAP_PROTOCOL_ERROR;
320 struct berval whoami = BER_BVC( LDAP_EXOP_X_WHO_AM_I );
322 rc = backend_check_restrictions( conn->c_authz_backend,
323 conn, op, &whoami, text );
325 if( rc != LDAP_SUCCESS ) return rc;
328 bv = (struct berval *) ch_malloc( sizeof(struct berval) );
329 if( op->o_dn.bv_len ) {
330 bv->bv_len = op->o_dn.bv_len + sizeof("dn:")-1;
331 bv->bv_val = ch_malloc( bv->bv_len + 1 );
332 AC_MEMCPY( bv->bv_val, "dn:", sizeof("dn:")-1 );
333 AC_MEMCPY( &bv->bv_val[sizeof("dn:")-1], op->o_dn.bv_val,
335 bv->bv_val[bv->bv_len] = '\0';