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