]> git.sur5r.net Git - openldap/blob - servers/slapd/extended.c
Add SLAP_MR_ORDERED_INDEX - support for inequality indexing. Currently
[openldap] / servers / slapd / extended.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2004 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
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>.
14  */
15
16 /*
17  * LDAPv3 Extended Operation Request
18  *      ExtendedRequest ::= [APPLICATION 23] SEQUENCE {
19  *              requestName      [0] LDAPOID,
20  *              requestValue     [1] OCTET STRING OPTIONAL
21  *      }
22  *
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
28  *      }
29  *
30  */
31
32 #include "portable.h"
33
34 #include <stdio.h>
35
36 #include <ac/socket.h>
37 #include <ac/string.h>
38
39 #include "slap.h"
40 #include "lber_pvt.h"
41
42 #ifdef LDAP_SLAPI
43 #include "slapi/slapi.h"
44 #endif
45
46 #define UNSUPPORTED_EXOP "unsupported extended operation"
47
48 #ifdef LDAP_DEVEL
49 #define SLAP_EXOP_HIDE 0x0000
50 #else
51 #define SLAP_EXOP_HIDE 0x8000
52 #endif
53
54 static struct extop_list {
55         struct extop_list *next;
56         struct berval oid;
57         slap_mask_t flags;
58         SLAP_EXTOP_MAIN_FN *ext_main;
59 } *supp_ext_list = NULL;
60
61 static SLAP_EXTOP_MAIN_FN whoami_extop;
62
63 /* This list of built-in extops is for extops that are not part
64  * of backends or in external modules.  Essentially, this is
65  * just a way to get built-in extops onto the extop list without
66  * having a separate init routine for each built-in extop.
67  */
68 const struct berval slap_EXOP_CANCEL = BER_BVC(LDAP_EXOP_X_CANCEL);
69 const struct berval slap_EXOP_WHOAMI = BER_BVC(LDAP_EXOP_X_WHO_AM_I);
70 const struct berval slap_EXOP_MODIFY_PASSWD = BER_BVC(LDAP_EXOP_MODIFY_PASSWD);
71 const struct berval slap_EXOP_START_TLS = BER_BVC(LDAP_EXOP_START_TLS);
72
73 static struct {
74         const struct berval *oid;
75         slap_mask_t flags;
76         SLAP_EXTOP_MAIN_FN *ext_main;
77 } builtin_extops[] = {
78         { &slap_EXOP_CANCEL, SLAP_EXOP_HIDE, cancel_extop },
79         { &slap_EXOP_WHOAMI, 0, whoami_extop },
80         { &slap_EXOP_MODIFY_PASSWD, 0, passwd_extop },
81 #ifdef HAVE_TLS
82         { &slap_EXOP_START_TLS, 0, starttls_extop },
83 #endif
84         { NULL, 0, NULL }
85 };
86
87
88 static struct extop_list *find_extop(
89         struct extop_list *list, struct berval *oid );
90
91 struct berval *
92 get_supported_extop (int index)
93 {
94         struct extop_list *ext;
95
96         /* linear scan is slow, but this way doesn't force a
97          * big change on root_dse.c, where this routine is used.
98          */
99         for (ext = supp_ext_list; ext != NULL && --index >= 0; ext = ext->next) {
100                 ; /* empty */
101         }
102
103         if (ext == NULL) return NULL;
104
105         return &ext->oid;
106 }
107
108
109 int exop_root_dse_info( Entry *e )
110 {
111         AttributeDescription *ad_supportedExtension
112                 = slap_schema.si_ad_supportedExtension;
113         struct berval vals[2];
114         struct extop_list *ext;
115
116         vals[1].bv_val = NULL;
117         vals[1].bv_len = 0;
118
119         for (ext = supp_ext_list; ext != NULL; ext = ext->next) {
120                 if( ext->flags & SLAP_EXOP_HIDE ) continue;
121
122                 vals[0] = ext->oid;
123
124                 if( attr_merge( e, ad_supportedExtension, vals, NULL ) ) {
125                         return LDAP_OTHER;
126                 }
127         }
128
129         return LDAP_SUCCESS;
130 }
131
132 int
133 do_extended(
134     Operation   *op,
135     SlapReply   *rs
136 )
137 {
138         struct berval reqdata = {0, NULL};
139         ber_tag_t tag;
140         ber_len_t len;
141
142         Debug( LDAP_DEBUG_TRACE, "do_extended\n", 0, 0, 0 );
143
144         if( op->o_protocol < LDAP_VERSION3 ) {
145                 Debug( LDAP_DEBUG_ANY,
146                         "do_extended: protocol version (%d) too low\n",
147                         op->o_protocol, 0 ,0 );
148                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "requires LDAPv3" );
149                 rs->sr_err = SLAPD_DISCONNECT;
150                 goto done;
151         }
152
153         if ( ber_scanf( op->o_ber, "{m" /*}*/, &op->ore_reqoid ) == LBER_ERROR ) {
154                 Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
155                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
156                 rs->sr_err = SLAPD_DISCONNECT;
157                 goto done;
158         }
159
160         tag = ber_peek_tag( op->o_ber, &len );
161         
162         if( ber_peek_tag( op->o_ber, &len ) == LDAP_TAG_EXOP_REQ_VALUE ) {
163                 if( ber_scanf( op->o_ber, "m", &reqdata ) == LBER_ERROR ) {
164                         Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
165                         send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
166                         rs->sr_err = SLAPD_DISCONNECT;
167                         goto done;
168                 }
169         }
170
171         if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
172                 Debug( LDAP_DEBUG_ANY, "do_extended: get_ctrls failed\n", 0, 0 ,0 );
173                 return rs->sr_err;
174         } 
175
176         /* check for controls inappropriate for all extended operations */
177         if( get_manageDSAit( op ) == SLAP_CRITICAL_CONTROL ) {
178                 send_ldap_error( op, rs,
179                         LDAP_UNAVAILABLE_CRITICAL_EXTENSION,
180                         "manageDSAit control inappropriate" );
181                 goto done;
182         }
183
184         /* FIXME: temporary? */
185         if ( reqdata.bv_val ) {
186                 op->ore_reqdata = &reqdata;
187         }
188
189         op->o_bd = frontendDB;
190         rs->sr_err = frontendDB->be_extended( op, rs );
191 done:
192         return rs->sr_err;
193 }
194
195 int
196 fe_extended( Operation *op, SlapReply *rs )
197 {
198         struct extop_list       *ext = NULL;
199         struct berval           reqdata = BER_BVNULL;
200
201 #if defined(LDAP_SLAPI) 
202         Slapi_PBlock            *pb = op->o_pb;
203         SLAPI_FUNC              funcAddr = NULL;
204         int                     extop_rc;
205         int                     msg_sent = FALSE;
206 #endif /* defined(LDAP_SLAPI) */
207
208         if (op->ore_reqdata) {
209                 reqdata = *op->ore_reqdata;
210         }
211
212 #ifdef LDAP_SLAPI
213     /* NS-SLAPI extended operation */
214         slapi_int_get_extop_plugin( &op->ore_reqoid, &funcAddr );
215
216         if( !funcAddr && !(ext = find_extop(supp_ext_list, &op->ore_reqoid )))
217 #else
218         if( !(ext = find_extop(supp_ext_list, &op->ore_reqoid )))
219 #endif
220         {
221                 Debug( LDAP_DEBUG_ANY, "do_extended: unsupported operation \"%s\"\n",
222                         op->ore_reqoid.bv_val, 0 ,0 );
223                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
224                         "unsupported extended operation" );
225                 goto done;
226         }
227
228         Debug( LDAP_DEBUG_ARGS, "do_extended: oid=%s\n",
229                 op->ore_reqoid.bv_val, 0 ,0 );
230
231 #if defined(LDAP_SLAPI)
232         if ( funcAddr != NULL ) {
233                 rs->sr_err = slapi_pblock_set( pb, SLAPI_EXT_OP_REQ_OID,
234                                 (void *)op->ore_reqoid.bv_val);
235                 if ( rs->sr_err != LDAP_SUCCESS ) {
236                         rs->sr_err = LDAP_OTHER;
237                         goto done;
238                 }
239
240                 rs->sr_err = slapi_pblock_set( pb, SLAPI_EXT_OP_REQ_VALUE,
241                                 (void *)&reqdata);
242                 if ( rs->sr_err != LDAP_SUCCESS ) {
243                         rs->sr_err = LDAP_OTHER;
244                         goto done;
245                 }
246
247                 rs->sr_err = slapi_int_pblock_set_operation( pb, op );
248                 if ( rs->sr_err != LDAP_SUCCESS ) {
249                         rs->sr_err = LDAP_OTHER;
250                         goto done;
251                 }
252
253                 extop_rc = (*funcAddr)( pb );
254                 if ( extop_rc == SLAPI_PLUGIN_EXTENDED_SENT_RESULT ) {
255                         msg_sent = TRUE;
256
257                 } else if ( extop_rc == SLAPI_PLUGIN_EXTENDED_NOT_HANDLED ) {
258                         rs->sr_err = LDAP_PROTOCOL_ERROR;
259                         rs->sr_text = UNSUPPORTED_EXOP;
260
261                 } else {
262                         rs->sr_err = slapi_pblock_get( pb, SLAPI_EXT_OP_RET_OID,
263                                         &rs->sr_rspoid);
264                         if ( rs->sr_err != LDAP_SUCCESS ) {
265                                 goto done2;
266                         }
267
268                         rs->sr_err = slapi_pblock_get( pb, SLAPI_EXT_OP_RET_VALUE,
269                                         &rs->sr_rspdata);
270                         if ( rs->sr_err != LDAP_SUCCESS ) {
271                                 goto done2;
272                         }
273
274                         rs->sr_err = extop_rc;
275                         send_ldap_extended( op, rs );
276                         msg_sent = TRUE;
277                 }
278
279 done2:;
280                 if ( rs->sr_err != LDAP_SUCCESS && msg_sent == FALSE ) {
281                         send_ldap_result( op, rs );
282                 }
283
284                 if ( rs->sr_rspoid != NULL ) {
285                         ch_free( (char *)rs->sr_rspoid );
286                 }
287
288                 if ( rs->sr_rspdata != NULL ) {
289                         ber_bvfree( rs->sr_rspdata );
290                 }
291         } else
292 #endif /* defined( LDAP_SLAPI ) */
293         { /* start of OpenLDAP extended operation */
294                 rs->sr_err = (ext->ext_main)( op, rs );
295
296                 if( rs->sr_err != SLAPD_ABANDON ) {
297                         if ( rs->sr_err == LDAP_REFERRAL && rs->sr_ref == NULL ) {
298                                 rs->sr_ref = referral_rewrite( default_referral,
299                                         NULL, NULL, LDAP_SCOPE_DEFAULT );
300                                 if ( !rs->sr_ref ) rs->sr_ref = default_referral;
301                                 if ( !rs->sr_ref ) {
302                                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
303                                         rs->sr_text = "referral missing";
304                                 }
305                         }
306
307                         send_ldap_extended( op, rs );
308
309                         if ( rs->sr_ref != default_referral ) {
310                                 ber_bvarray_free( rs->sr_ref );
311                                 rs->sr_ref = NULL;
312                         }
313                 }
314
315                 if ( rs->sr_rspoid != NULL ) {
316                         free( (char *)rs->sr_rspoid );
317                 }
318
319                 if ( rs->sr_rspdata != NULL ) {
320                         ber_bvfree( rs->sr_rspdata );
321                 }
322         } /* end of OpenLDAP extended operation */
323
324 done:;
325         return rs->sr_err;
326 }
327
328 int
329 load_extop(
330         struct berval *ext_oid,
331         slap_mask_t ext_flags,
332         SLAP_EXTOP_MAIN_FN *ext_main )
333 {
334         struct extop_list *ext;
335
336         if( ext_oid == NULL || ext_oid->bv_val == NULL ||
337                 ext_oid->bv_val[0] == '\0' || ext_oid->bv_len == 0 ) return -1; 
338         if(!ext_main) return -1; 
339
340         ext = ch_calloc(1, sizeof(struct extop_list) + ext_oid->bv_len + 1);
341         if (ext == NULL)
342                 return(-1);
343
344         ext->flags = ext_flags;
345
346         ext->oid.bv_val = (char *)(ext + 1);
347         AC_MEMCPY( ext->oid.bv_val, ext_oid->bv_val, ext_oid->bv_len );
348         ext->oid.bv_len = ext_oid->bv_len;
349         ext->oid.bv_val[ext->oid.bv_len] = '\0';
350
351         ext->ext_main = ext_main;
352         ext->next = supp_ext_list;
353
354         supp_ext_list = ext;
355
356         return(0);
357 }
358
359 int
360 extops_init (void)
361 {
362         int i;
363
364         for (i = 0; builtin_extops[i].oid != NULL; i++) {
365                 load_extop((struct berval *)builtin_extops[i].oid,
366                         builtin_extops[i].flags,
367                         builtin_extops[i].ext_main);
368         }
369         return(0);
370 }
371
372 int
373 extops_kill (void)
374 {
375         struct extop_list *ext;
376
377         /* we allocated the memory, so we have to free it, too. */
378         while ((ext = supp_ext_list) != NULL) {
379                 supp_ext_list = ext->next;
380                 ch_free(ext);
381         }
382         return(0);
383 }
384
385 static struct extop_list *
386 find_extop( struct extop_list *list, struct berval *oid )
387 {
388         struct extop_list *ext;
389
390         for (ext = list; ext; ext = ext->next) {
391                 if (bvmatch(&ext->oid, oid))
392                         return(ext);
393         }
394         return(NULL);
395 }
396
397
398 static int
399 whoami_extop (
400         Operation *op,
401         SlapReply *rs )
402 {
403         struct berval *bv;
404
405         if ( op->ore_reqdata != NULL ) {
406                 /* no request data should be provided */
407                 rs->sr_text = "no request data expected";
408                 return LDAP_PROTOCOL_ERROR;
409         }
410
411         op->o_bd = op->o_conn->c_authz_backend;
412         if( backend_check_restrictions( op, rs,
413                 (struct berval *)&slap_EXOP_WHOAMI ) != LDAP_SUCCESS ) {
414                 return rs->sr_err;
415         }
416
417         bv = (struct berval *) ch_malloc( sizeof(struct berval) );
418         if( op->o_dn.bv_len ) {
419                 bv->bv_len = op->o_dn.bv_len + sizeof("dn:")-1;
420                 bv->bv_val = ch_malloc( bv->bv_len + 1 );
421                 AC_MEMCPY( bv->bv_val, "dn:", sizeof("dn:")-1 );
422                 AC_MEMCPY( &bv->bv_val[sizeof("dn:")-1], op->o_dn.bv_val,
423                         op->o_dn.bv_len );
424                 bv->bv_val[bv->bv_len] = '\0';
425
426         } else {
427                 bv->bv_len = 0;
428                 bv->bv_val = NULL;
429         }
430
431         rs->sr_rspdata = bv;
432         return LDAP_SUCCESS;
433 }