]> git.sur5r.net Git - openldap/blob - servers/slapd/extended.c
731f9553229413adb15558dd0050f38451dec9f1
[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 #ifdef NEW_LOGGING
143         LDAP_LOG( OPERATION, ENTRY, "do_extended: conn %d\n", op->o_connid, 0, 0 );
144 #else
145         Debug( LDAP_DEBUG_TRACE, "do_extended\n", 0, 0, 0 );
146 #endif
147
148         if( op->o_protocol < LDAP_VERSION3 ) {
149 #ifdef NEW_LOGGING
150                 LDAP_LOG( OPERATION, ERR, 
151                         "do_extended: protocol version (%d) too low.\n", op->o_protocol, 0, 0 );
152 #else
153                 Debug( LDAP_DEBUG_ANY,
154                         "do_extended: protocol version (%d) too low\n",
155                         op->o_protocol, 0 ,0 );
156 #endif
157                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "requires LDAPv3" );
158                 rs->sr_err = SLAPD_DISCONNECT;
159                 goto done;
160         }
161
162         if ( ber_scanf( op->o_ber, "{m" /*}*/, &op->ore_reqoid ) == LBER_ERROR ) {
163 #ifdef NEW_LOGGING
164                 LDAP_LOG( OPERATION, ERR, "do_extended: conn %d  ber_scanf failed\n", 
165                         op->o_connid, 0, 0 );
166 #else
167                 Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
168 #endif
169                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
170                 rs->sr_err = SLAPD_DISCONNECT;
171                 goto done;
172         }
173
174         tag = ber_peek_tag( op->o_ber, &len );
175         
176         if( ber_peek_tag( op->o_ber, &len ) == LDAP_TAG_EXOP_REQ_VALUE ) {
177                 if( ber_scanf( op->o_ber, "m", &reqdata ) == LBER_ERROR ) {
178 #ifdef NEW_LOGGING
179                         LDAP_LOG( OPERATION, ERR, 
180                                 "do_extended: conn %d  ber_scanf failed\n", 
181                                 op->o_connid, 0, 0 );
182 #else
183                         Debug( LDAP_DEBUG_ANY, "do_extended: ber_scanf failed\n", 0, 0 ,0 );
184 #endif
185                         send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
186                         rs->sr_err = SLAPD_DISCONNECT;
187                         goto done;
188                 }
189         }
190
191         if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
192 #ifdef NEW_LOGGING
193                 LDAP_LOG( OPERATION, ERR, 
194                         "do_extended: conn %d  get_ctrls failed\n", op->o_connid, 0, 0 );
195 #else
196                 Debug( LDAP_DEBUG_ANY, "do_extended: get_ctrls failed\n", 0, 0 ,0 );
197 #endif
198                 return rs->sr_err;
199         } 
200
201         /* check for controls inappropriate for all extended operations */
202         if( get_manageDSAit( op ) == SLAP_CRITICAL_CONTROL ) {
203                 send_ldap_error( op, rs,
204                         LDAP_UNAVAILABLE_CRITICAL_EXTENSION,
205                         "manageDSAit control inappropriate" );
206                 goto done;
207         }
208
209         /* FIXME: temporary? */
210         if ( reqdata.bv_val ) {
211                 op->ore_reqdata = &reqdata;
212         }
213
214         op->o_bd = frontendDB;
215         rs->sr_err = frontendDB->be_extended( op, rs );
216 done:
217         return rs->sr_err;
218 }
219
220 int
221 fe_extended( Operation *op, SlapReply *rs )
222 {
223         struct extop_list       *ext = NULL;
224         struct berval           reqdata = BER_BVNULL;
225
226 #if defined(LDAP_SLAPI) 
227         Slapi_PBlock            *pb = op->o_pb;
228         SLAPI_FUNC              funcAddr = NULL;
229         int                     extop_rc;
230         int                     msg_sent = FALSE;
231 #endif /* defined(LDAP_SLAPI) */
232
233         if (op->ore_reqdata) {
234                 reqdata = *op->ore_reqdata;
235         }
236
237 #ifdef LDAP_SLAPI
238         slapi_int_get_extop_plugin( &op->ore_reqoid, &funcAddr ); /* NS-SLAPI extended operation */
239         if( !funcAddr && !(ext = find_extop(supp_ext_list, &op->ore_reqoid )))
240 #else
241         if( !(ext = find_extop(supp_ext_list, &op->ore_reqoid )))
242 #endif
243         {
244 #ifdef NEW_LOGGING
245                 LDAP_LOG( OPERATION, ERR, 
246                         "do_extended: conn %d  unsupported operation \"%s\"\n",
247                         op->o_connid, op->ore_reqoid.bv_val, 0 );
248 #else
249                 Debug( LDAP_DEBUG_ANY, "do_extended: unsupported operation \"%s\"\n",
250                         op->ore_reqoid.bv_val, 0 ,0 );
251 #endif
252                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
253                         "unsupported extended operation" );
254                 goto done;
255         }
256
257 #ifdef NEW_LOGGING
258         LDAP_LOG( OPERATION, DETAIL1, 
259                 "do_extended: conn %d  oid=%s\n.", op->o_connid, op->ore_reqoid.bv_val, 0 );
260 #else
261         Debug( LDAP_DEBUG_ARGS, "do_extended: oid=%s\n", op->ore_reqoid.bv_val, 0 ,0 );
262 #endif
263
264 #if defined(LDAP_SLAPI)
265         if ( funcAddr != NULL ) {
266                 rs->sr_err = slapi_pblock_set( pb, SLAPI_EXT_OP_REQ_OID,
267                                 (void *)op->ore_reqoid.bv_val);
268                 if ( rs->sr_err != LDAP_SUCCESS ) {
269                         rs->sr_err = LDAP_OTHER;
270                         goto done;
271                 }
272
273                 rs->sr_err = slapi_pblock_set( pb, SLAPI_EXT_OP_REQ_VALUE,
274                                 (void *)&reqdata);
275                 if ( rs->sr_err != LDAP_SUCCESS ) {
276                         rs->sr_err = LDAP_OTHER;
277                         goto done;
278                 }
279
280                 rs->sr_err = slapi_int_pblock_set_operation( pb, op );
281                 if ( rs->sr_err != LDAP_SUCCESS ) {
282                         rs->sr_err = LDAP_OTHER;
283                         goto done;
284                 }
285
286                 extop_rc = (*funcAddr)( pb );
287                 if ( extop_rc == SLAPI_PLUGIN_EXTENDED_SENT_RESULT ) {
288                         msg_sent = TRUE;
289
290                 } else if ( extop_rc == SLAPI_PLUGIN_EXTENDED_NOT_HANDLED ) {
291                         rs->sr_err = LDAP_PROTOCOL_ERROR;
292                         rs->sr_text = UNSUPPORTED_EXOP;
293
294                 } else {
295                         rs->sr_err = slapi_pblock_get( pb, SLAPI_EXT_OP_RET_OID,
296                                         &rs->sr_rspoid);
297                         if ( rs->sr_err != LDAP_SUCCESS ) {
298                                 goto done2;
299                         }
300
301                         rs->sr_err = slapi_pblock_get( pb, SLAPI_EXT_OP_RET_VALUE,
302                                         &rs->sr_rspdata);
303                         if ( rs->sr_err != LDAP_SUCCESS ) {
304                                 goto done2;
305                         }
306
307                         rs->sr_err = extop_rc;
308                         send_ldap_extended( op, rs );
309                         msg_sent = TRUE;
310                 }
311
312 done2:;
313                 if ( rs->sr_err != LDAP_SUCCESS && msg_sent == FALSE ) {
314                         send_ldap_result( op, rs );
315                 }
316
317                 if ( rs->sr_rspoid != NULL ) {
318                         ch_free( (char *)rs->sr_rspoid );
319                 }
320
321                 if ( rs->sr_rspdata != NULL ) {
322                         ber_bvfree( rs->sr_rspdata );
323                 }
324         } else { /* start of OpenLDAP extended operation */
325 #endif /* defined( LDAP_SLAPI ) */
326                 rs->sr_err = (ext->ext_main)( op, rs );
327
328                 if( rs->sr_err != SLAPD_ABANDON ) {
329                         if ( rs->sr_err == LDAP_REFERRAL && rs->sr_ref == NULL ) {
330                                 rs->sr_ref = referral_rewrite( default_referral,
331                                         NULL, NULL, LDAP_SCOPE_DEFAULT );
332                                 if ( !rs->sr_ref ) rs->sr_ref = default_referral;
333                                 if ( !rs->sr_ref ) {
334                                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
335                                         rs->sr_text = "referral missing";
336                                 }
337                         }
338
339                         send_ldap_extended( op, rs );
340
341                         if ( rs->sr_ref != default_referral ) {
342                                 ber_bvarray_free( rs->sr_ref );
343                                 rs->sr_ref = NULL;
344                         }
345                 }
346
347                 if ( rs->sr_rspoid != NULL ) {
348                         free( (char *)rs->sr_rspoid );
349                 }
350
351                 if ( rs->sr_rspdata != NULL ) {
352                         ber_bvfree( rs->sr_rspdata );
353                 }
354 #ifdef LDAP_SLAPI
355         } /* end of OpenLDAP extended operation */
356 #endif /* LDAP_SLAPI */
357
358 done:;
359         return rs->sr_err;
360 }
361
362 int
363 load_extop(
364         struct berval *ext_oid,
365         slap_mask_t ext_flags,
366         SLAP_EXTOP_MAIN_FN *ext_main )
367 {
368         struct extop_list *ext;
369
370         if( ext_oid == NULL || ext_oid->bv_val == NULL ||
371                 ext_oid->bv_val[0] == '\0' || ext_oid->bv_len == 0 ) return -1; 
372         if(!ext_main) return -1; 
373
374         ext = ch_calloc(1, sizeof(struct extop_list) + ext_oid->bv_len + 1);
375         if (ext == NULL)
376                 return(-1);
377
378         ext->flags = ext_flags;
379
380         ext->oid.bv_val = (char *)(ext + 1);
381         AC_MEMCPY( ext->oid.bv_val, ext_oid->bv_val, ext_oid->bv_len );
382         ext->oid.bv_len = ext_oid->bv_len;
383         ext->oid.bv_val[ext->oid.bv_len] = '\0';
384
385         ext->ext_main = ext_main;
386         ext->next = supp_ext_list;
387
388         supp_ext_list = ext;
389
390         return(0);
391 }
392
393 int
394 extops_init (void)
395 {
396         int i;
397
398         for (i = 0; builtin_extops[i].oid != NULL; i++) {
399                 load_extop((struct berval *)builtin_extops[i].oid,
400                         builtin_extops[i].flags,
401                         builtin_extops[i].ext_main);
402         }
403         return(0);
404 }
405
406 int
407 extops_kill (void)
408 {
409         struct extop_list *ext;
410
411         /* we allocated the memory, so we have to free it, too. */
412         while ((ext = supp_ext_list) != NULL) {
413                 supp_ext_list = ext->next;
414                 ch_free(ext);
415         }
416         return(0);
417 }
418
419 static struct extop_list *
420 find_extop( struct extop_list *list, struct berval *oid )
421 {
422         struct extop_list *ext;
423
424         for (ext = list; ext; ext = ext->next) {
425                 if (bvmatch(&ext->oid, oid))
426                         return(ext);
427         }
428         return(NULL);
429 }
430
431
432 static int
433 whoami_extop (
434         Operation *op,
435         SlapReply *rs )
436 {
437         struct berval *bv;
438
439         if ( op->ore_reqdata != NULL ) {
440                 /* no request data should be provided */
441                 rs->sr_text = "no request data expected";
442                 return LDAP_PROTOCOL_ERROR;
443         }
444
445         op->o_bd = op->o_conn->c_authz_backend;
446         if( backend_check_restrictions( op, rs,
447                 (struct berval *)&slap_EXOP_WHOAMI ) != LDAP_SUCCESS ) {
448                 return rs->sr_err;
449         }
450
451         bv = (struct berval *) ch_malloc( sizeof(struct berval) );
452         if( op->o_dn.bv_len ) {
453                 bv->bv_len = op->o_dn.bv_len + sizeof("dn:")-1;
454                 bv->bv_val = ch_malloc( bv->bv_len + 1 );
455                 AC_MEMCPY( bv->bv_val, "dn:", sizeof("dn:")-1 );
456                 AC_MEMCPY( &bv->bv_val[sizeof("dn:")-1], op->o_dn.bv_val,
457                         op->o_dn.bv_len );
458                 bv->bv_val[bv->bv_len] = '\0';
459
460         } else {
461                 bv->bv_len = 0;
462                 bv->bv_val = NULL;
463         }
464
465         rs->sr_rspdata = bv;
466         return LDAP_SUCCESS;
467 }