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