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