]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/deref.c
8c42170299fe5adeeef3a7d7ddb9c21a8235b81b
[openldap] / servers / slapd / overlays / deref.c
1 /* deref.c - dereference overlay */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2008 The OpenLDAP Foundation.
6  * Portions Copyright 2008 Pierangelo Masarati.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by Pierangelo Masarati
19  * for inclusion in OpenLDAP Software.
20  */
21
22 #include "portable.h"
23
24 #ifdef SLAPD_OVER_DEREF
25
26 #include <stdio.h>
27
28 #include "ac/string.h"
29 #include "ac/socket.h"
30
31 #include "slap.h"
32 #include "config.h"
33
34 #include "lutil.h"
35
36 /*
37  * 1. Specification
38  *
39  * 1.1. Request
40  *
41  *  controlValue ::= SEQUENCE OF derefSpec DerefSpec
42  *
43  *  DerefSpec ::= SEQUENCE {
44  *      derefAttr       attributeDescription,    ; DN-valued
45  *      attributes      AttributeList }
46  *
47  *  AttributeList ::= SEQUENCE OF attr AttributeDescription
48  *
49  *  derefAttr MUST be unique within controlValue
50  *
51  *
52  * 1.2. Response
53  *
54  *  controlValue ::= SEQUENCE OF DerefRes
55  *
56  * From RFC 4511:
57  *      PartialAttribute ::= SEQUENCE {
58  *           type       AttributeDescription,
59  *           vals       SET OF value AttributeValue }
60  *
61  *      PartialAttributeList ::= SEQUENCE OF
62  *                           partialAttribute PartialAttribute
63  *
64  *  DerefRes ::= SEQUENCE {
65  *      derefAttr       AttributeDescription,
66  *      derefVal        LDAPDN,
67  *      attrVals        [0] PartialAttributeList OPTIONAL }
68  *
69  *  If vals is empty, partialAttribute is omitted.
70  *  If all vals in attrVals are empty, attrVals is omitted.
71  *      
72  * 2. Examples
73  *
74  * 2.1. Example
75  *
76  * 2.1.1. Request
77  *
78  * { { member, { GUID, SID } }, { memberOf, { GUID, SID } } }
79  *
80  * 2.1.2. Response
81  *
82  * { { memberOf, "cn=abartlet,cn=users,dc=abartlet,dc=net",
83  *     { { GUID, [ "0bc11d00-e431-40a0-8767-344a320142fa" ] },
84  *       { SID, [ "S-1-2-3-2345" ] } } },
85  *   { memberOf, "cn=ando,cn=users,dc=sys-net,dc=it",
86  *     { { GUID, [ "0bc11d00-e431-40a0-8767-344a320142fb" ] },
87  *       { SID, [ "S-1-2-3-2346" ] } } } }
88  *
89  * 2.2. Example
90  *
91  * 2.2.1. Request
92  *
93  * { { member, { cn, uid, drink } } }
94  *
95  * 2.2.2. Response
96  *
97  * { { member, "cn=ando,cn=users,dc=sys-net,dc=it",
98  *     { { cn, [ "ando", "Pierangelo Masarati" ] },
99  *       { uid, [ "ando" ] } } },
100  *   { member, "dc=sys-net,dc=it" } }
101  *
102  *
103  * 3. Security considerations
104  *
105  * The control result must not disclose information the client's
106  * identity could not have accessed directly by performing the related
107  * search operations.  The presence of a derefVal in the control
108  * response does not imply neither the existence of nor any access
109  * privilege to the corresponding entry.  It is merely a consequence
110  * of the read access the client's identity has on the corresponding
111  * attribute's value.
112  */
113
114 #define o_deref                 o_ctrlflag[deref_cid]
115 #define o_ctrlderef             o_controls[deref_cid]
116
117 typedef struct DerefSpec {
118         AttributeDescription    *ds_derefAttr;
119         AttributeDescription    **ds_attributes;
120         int                     ds_nattrs;
121         struct DerefSpec        *ds_next;
122 } DerefSpec;
123
124 typedef struct DerefVal {
125         struct berval   dv_derefSpecVal;
126         BerVarray       *dv_attrVals;
127 } DerefVal;
128
129 typedef struct DerefRes {
130         DerefSpec               dr_spec;
131         DerefVal                *dr_vals;
132         struct DerefRes         *dr_next;
133 } DerefRes;
134
135 typedef struct deref_cb_t {
136         slap_overinst *dc_on;
137         DerefSpec *dc_ds;
138 } deref_cb_t;
139
140 static int                      deref_cid;
141 static slap_overinst            deref;
142
143 static int
144 deref_parseCtrl (
145         Operation *op,
146         SlapReply *rs,
147         LDAPControl *ctrl )
148 {
149         ber_tag_t tag;
150         BerElementBuffer berbuf;
151         BerElement *ber = (BerElement *)&berbuf;
152         ber_len_t len;
153         char *last;
154         DerefSpec *dshead = NULL, **dsp = &dshead;
155         BerVarray attributes = NULL;
156
157         if ( op->o_deref != SLAP_CONTROL_NONE ) {
158                 rs->sr_text = "Dereference control specified multiple times";
159                 return LDAP_PROTOCOL_ERROR;
160         }
161
162         if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
163                 rs->sr_text = "Dereference control value is absent";
164                 return LDAP_PROTOCOL_ERROR;
165         }
166
167         if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
168                 rs->sr_text = "Dereference control value is empty";
169                 return LDAP_PROTOCOL_ERROR;
170         }
171
172         ber_init2( ber, &ctrl->ldctl_value, 0 );
173
174         for ( tag = ber_first_element( ber, &len, &last );
175                 tag != LBER_DEFAULT;
176                 tag = ber_next_element( ber, &len, last ) )
177         {
178                 struct berval derefAttr;
179                 DerefSpec *ds, *dstmp;
180                 const char *text;
181                 int rc;
182                 ber_len_t cnt = sizeof(struct berval);
183                 ber_len_t off = 0;
184
185                 if ( ber_scanf( ber, "{m{M}}", &derefAttr, &attributes, &cnt, off ) == LBER_ERROR )
186                 {
187                         rs->sr_text = "Dereference control: derefSpec decoding error";
188                         rs->sr_err = LDAP_PROTOCOL_ERROR;
189                         goto done;
190                 }
191
192                 ds = (DerefSpec *)op->o_tmpcalloc( 1,
193                         sizeof(DerefSpec) + sizeof(AttributeDescription *)*(cnt + 1),
194                         op->o_tmpmemctx );
195                 ds->ds_attributes = (AttributeDescription **)&ds[1];
196                 ds->ds_nattrs = cnt;
197
198                 rc = slap_bv2ad( &derefAttr, &ds->ds_derefAttr, &text );
199                 if ( rc != LDAP_SUCCESS ) {
200                         rs->sr_text = "Dereference control: derefAttr decoding error";
201                         rs->sr_err = LDAP_PROTOCOL_ERROR;
202                         goto done;
203                 }
204
205                 for ( dstmp = dshead; dstmp && dstmp != ds; dstmp = dstmp->ds_next ) {
206                         if ( dstmp->ds_derefAttr == ds->ds_derefAttr ) {
207                                 rs->sr_text = "Dereference control: derefAttr must be unique within control";
208                                 rs->sr_err = LDAP_PROTOCOL_ERROR;
209                                 goto done;
210                         }
211                 }
212
213                 if ( ds->ds_derefAttr->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
214                         if ( ctrl->ldctl_iscritical ) {
215                                 rs->sr_text = "Dereference control: derefAttr syntax not distinguishedName";
216                                 rs->sr_err = LDAP_PROTOCOL_ERROR;
217                                 goto done;
218                         }
219
220                         rs->sr_err = LDAP_SUCCESS;
221                         goto justcleanup;
222                 }
223
224                 for ( cnt = 0; !BER_BVISNULL( &attributes[ cnt ] ); cnt++ ) {
225                         rc = slap_bv2ad( &attributes[ cnt ], &ds->ds_attributes[ cnt ], &text );
226                         if ( rc != LDAP_SUCCESS ) {
227                                 rs->sr_text = "Dereference control: attribute decoding error";
228                                 rs->sr_err = LDAP_PROTOCOL_ERROR;
229                                 goto done;
230                         }
231                 }
232
233                 ber_memfree_x( attributes, op->o_tmpmemctx );
234                 attributes = NULL;
235
236                 *dsp = ds;
237                 dsp = &ds->ds_next;
238         }
239
240         op->o_ctrlderef = (void *)dshead;
241
242         op->o_deref = ctrl->ldctl_iscritical
243                 ? SLAP_CONTROL_CRITICAL
244                 : SLAP_CONTROL_NONCRITICAL;
245
246         rs->sr_err = LDAP_SUCCESS;
247
248 done:;
249         if ( rs->sr_err != LDAP_SUCCESS ) {
250 justcleanup:;
251                 for ( ; dshead; ) {
252                         DerefSpec *dsnext = dshead->ds_next;
253                         op->o_tmpfree( dshead, op->o_tmpmemctx );
254                         dshead = dsnext;
255                 }
256         }
257
258         if ( attributes != NULL ) {
259                 ber_memfree_x( attributes, op->o_tmpmemctx );
260         }
261
262         return rs->sr_err;
263 }
264
265 static int
266 deref_response( Operation *op, SlapReply *rs )
267 {
268         int rc = SLAP_CB_CONTINUE;
269
270         if ( rs->sr_type == REP_SEARCH ) {
271                 BerElementBuffer berbuf;
272                 BerElement *ber = (BerElement *) &berbuf;
273                 deref_cb_t *dc = (deref_cb_t *)op->o_callback->sc_private;
274                 DerefSpec *ds;
275                 DerefRes *dr, *drhead = NULL, **drp = &drhead;
276                 BackendInfo *bi = op->o_bd->bd_info;
277                 struct berval bv = BER_BVNULL;
278                 int nDerefRes = 0, nDerefVals = 0, nAttrs = 0, nVals = 0;
279                 struct berval ctrlval;
280                 LDAPControl *ctrl, **ctrlsp;
281                 AccessControlState acl_state = ACL_STATE_INIT;
282                 static char dummy = '\0';
283                 Entry *ebase;
284                 int i;
285
286                 op->o_bd->bd_info = (BackendInfo *)dc->dc_on->on_info;
287                 rc = overlay_entry_get_ov( op, &rs->sr_entry->e_nname, NULL, NULL, 0, &ebase, dc->dc_on );
288                 if ( rc != LDAP_SUCCESS || ebase == NULL ) {
289                         op->o_bd->bd_info = bi;
290                         return SLAP_CB_CONTINUE;
291                 }
292
293                 for ( ds = dc->dc_ds; ds; ds = ds->ds_next ) {
294                         Attribute *a = attr_find( ebase->e_attrs, ds->ds_derefAttr );
295
296                         if ( a != NULL ) {
297                                 DerefVal *dv;
298                                 BerVarray *bva;
299
300                                 if ( !access_allowed( op, rs->sr_entry, a->a_desc,
301                                                 NULL, ACL_READ, &acl_state ) )
302                                 {
303                                         continue;
304                                 }
305
306                                 dr = op->o_tmpcalloc( 1,
307                                         sizeof( DerefRes ) + ( sizeof( DerefVal ) + sizeof( BerVarray * ) * ds->ds_nattrs ) * ( a->a_numvals + 1 ),
308                                         op->o_tmpmemctx );
309                                 dr->dr_spec = *ds;
310                                 dv = dr->dr_vals = (DerefVal *)&dr[ 1 ];
311                                 bva = (BerVarray *)&dv[ a->a_numvals + 1 ];
312
313                                 bv.bv_len += ds->ds_derefAttr->ad_cname.bv_len;
314                                 nAttrs++;
315                                 nDerefRes++;
316
317                                 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ ) {
318                                         Entry *e = NULL;
319
320                                         dv[ i ].dv_attrVals = bva;
321                                         bva += ds->ds_nattrs;
322
323
324                                         if ( !access_allowed( op, rs->sr_entry, a->a_desc,
325                                                         &a->a_nvals[ i ], ACL_READ, &acl_state ) )
326                                         {
327                                                 dv[ i ].dv_derefSpecVal.bv_val = &dummy;
328                                                 continue;
329                                         }
330
331                                         dv[i].dv_derefSpecVal = a->a_vals[ i ];
332                                         bv.bv_len += dv[i].dv_derefSpecVal.bv_len;
333                                         nVals++;
334                                         nDerefVals++;
335
336                                         rc = overlay_entry_get_ov( op, &a->a_nvals[ i ], NULL, NULL, 0, &e, dc->dc_on );
337                                         if ( rc == LDAP_SUCCESS && e != NULL ) {
338                                                 int j;
339
340                                                 if ( access_allowed( op, e, slap_schema.si_ad_entry,
341                                                         NULL, ACL_READ, NULL ) )
342                                                 {
343                                                         for ( j = 0; j < ds->ds_nattrs; j++ ) {
344                                                                 Attribute *aa;
345
346                                                                 if ( !access_allowed( op, e, ds->ds_attributes[ j ], NULL,
347                                                                         ACL_READ, &acl_state ) )
348                                                                 {
349                                                                         continue;
350                                                                 }
351
352                                                                 aa = attr_find( e->e_attrs, ds->ds_attributes[ j ] );
353                                                                 if ( aa != NULL ) {
354                                                                         unsigned k, h, last = aa->a_numvals;
355
356                                                                         ber_bvarray_dup_x( &dv[ i ].dv_attrVals[ j ],
357                                                                                 aa->a_vals, op->o_tmpmemctx );
358
359                                                                         bv.bv_len += ds->ds_attributes[ j ]->ad_cname.bv_len;
360
361                                                                         for ( k = 0, h = 0; k < aa->a_numvals; k++ ) {
362                                                                                 if ( !access_allowed( op, e,
363                                                                                         aa->a_desc,
364                                                                                         &aa->a_nvals[ k ],
365                                                                                         ACL_READ, &acl_state ) )
366                                                                                 {
367                                                                                         op->o_tmpfree( dv[ i ].dv_attrVals[ j ][ h ].bv_val,
368                                                                                                 op->o_tmpmemctx );
369                                                                                         dv[ i ].dv_attrVals[ j ][ h ] = dv[ i ].dv_attrVals[ j ][ --last ];
370                                                                                         BER_BVZERO( &dv[ i ].dv_attrVals[ j ][ last ] );
371                                                                                         continue;
372                                                                                 }
373                                                                                 bv.bv_len += dv[ i ].dv_attrVals[ j ][ h ].bv_len;
374                                                                                 nVals++;
375                                                                                 h++;
376                                                                         }
377                                                                         nAttrs++;
378                                                                 }
379                                                         }
380                                                 }
381
382                                                 overlay_entry_release_ov( op, e, 0, dc->dc_on );
383                                         }
384                                 }
385
386                                 *drp = dr;
387                                 drp = &dr->dr_next;
388                         }
389                 }
390                 overlay_entry_release_ov( op, ebase, 0, dc->dc_on );
391                 op->o_bd->bd_info = bi;
392
393                 if ( drhead == NULL ) {
394                         return SLAP_CB_CONTINUE;
395                 }
396
397                 /* cook the control value */
398                 bv.bv_len += nVals * sizeof(struct berval)
399                         + nAttrs * sizeof(struct berval)
400                         + nDerefVals * sizeof(DerefVal)
401                         + nDerefRes * sizeof(DerefRes);
402                 bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
403
404                 ber_init2( ber, &bv, LBER_USE_DER );
405                 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
406
407                 rc = ber_printf( ber, "{" /*}*/ );
408                 for ( dr = drhead; dr != NULL; dr = dr->dr_next ) {
409                         for ( i = 0; !BER_BVISNULL( &dr->dr_vals[ i ].dv_derefSpecVal ); i++ ) {
410                                 int j, first = 1;
411
412                                 if ( dr->dr_vals[ i ].dv_derefSpecVal.bv_val == &dummy ) {
413                                         continue;
414                                 }
415
416                                 rc = ber_printf( ber, "{OO" /*}*/,
417                                         &dr->dr_spec.ds_derefAttr->ad_cname,
418                                         &dr->dr_vals[ i ].dv_derefSpecVal );
419                                 for ( j = 0; j < dr->dr_spec.ds_nattrs; j++ ) {
420                                         if ( dr->dr_vals[ i ].dv_attrVals[ j ] != NULL ) {
421                                                 if ( first ) {
422                                                         rc = ber_printf( ber, "t{" /*}*/,
423                                                                 (LBER_CONSTRUCTED|LBER_CLASS_CONTEXT) );
424                                                         first = 0;
425                                                 }
426                                                 rc = ber_printf( ber, "{O[W]}",
427                                                         &dr->dr_spec.ds_attributes[ j ]->ad_cname,
428                                                         dr->dr_vals[ i ].dv_attrVals[ j ] );
429                                                 op->o_tmpfree( dr->dr_vals[ i ].dv_attrVals[ j ],
430                                                         op->o_tmpmemctx );
431                                         }
432                                 }
433                                 if ( !first ) {
434                                         rc = ber_printf( ber, /*{{*/ "}N}" );
435                                 } else {
436                                         rc = ber_printf( ber, /*{*/ "}" );
437                                 }
438                         }
439                 }
440                 rc = ber_printf( ber, /*{*/ "}" );
441                 if ( ber_flatten2( ber, &ctrlval, 0 ) == -1 ) {
442                         if ( op->o_deref == SLAP_CONTROL_CRITICAL ) {
443                                 rc = LDAP_CONSTRAINT_VIOLATION;
444
445                         } else {
446                                 rc = SLAP_CB_CONTINUE;
447                         }
448                         goto cleanup;
449                 }
450
451                 ctrl = op->o_tmpcalloc( 1,
452                         sizeof( LDAPControl ) + ctrlval.bv_len + 1,
453                         op->o_tmpmemctx );
454                 ctrl->ldctl_value.bv_val = (char *)&ctrl[ 1 ];
455                 ctrl->ldctl_oid = LDAP_CONTROL_X_DEREF;
456                 ctrl->ldctl_iscritical = 0;
457                 ctrl->ldctl_value.bv_len = ctrlval.bv_len;
458                 lutil_strncopy( ctrl->ldctl_value.bv_val, ctrlval.bv_val, ctrlval.bv_len );
459                 ctrl->ldctl_value.bv_val[ ctrl->ldctl_value.bv_len ] = '\0';
460
461                 ber_free_buf( ber );
462
463                 i = 0;
464                 if ( rs->sr_ctrls ) {
465                         for ( ; rs->sr_ctrls[ i ] != NULL; i++ )
466                                 /* count'em */ ;
467                 }
468                 i += 2;
469                 ctrlsp = op->o_tmpcalloc( i, sizeof(LDAPControl *), op->o_tmpmemctx );
470                 i = 0;
471                 if ( rs->sr_ctrls != NULL ) {
472                         for ( ; rs->sr_ctrls[ i ] != NULL; i++ ) {
473                                 ctrlsp[ i ] = rs->sr_ctrls[ i ];
474                         }
475                 }
476                 ctrlsp[ i++ ] = ctrl;
477                 ctrlsp[ i++ ] = NULL;
478                 if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED ) {
479                         op->o_tmpfree( rs->sr_ctrls, op->o_tmpmemctx );
480                 }
481                 rs->sr_ctrls = ctrlsp;
482                 rs->sr_flags |= REP_CTRLS_MUSTBEFREED;
483
484                 rc = SLAP_CB_CONTINUE;
485
486 cleanup:;
487                 /* release all */
488                 for ( ; drhead != NULL; ) {
489                         DerefRes *drnext = drhead->dr_next;
490                         op->o_tmpfree( drhead, op->o_tmpmemctx );
491                         drhead = drnext;
492                 }
493         }
494
495         return rc;
496 }
497
498 static int
499 deref_cleanup( Operation *op, SlapReply *rs )
500 {
501         if ( rs->sr_type == REP_RESULT || rs->sr_err == SLAPD_ABANDON ) {
502                 op->o_tmpfree( op->o_callback, op->o_tmpmemctx );
503                 op->o_callback = NULL;
504
505                 op->o_tmpfree( op->o_ctrlderef, op->o_tmpmemctx );
506                 op->o_ctrlderef = NULL;
507         }
508
509         return SLAP_CB_CONTINUE;
510 }
511
512 static int
513 deref_op_search( Operation *op, SlapReply *rs )
514 {
515         if ( op->o_deref ) {
516                 slap_callback *sc;
517                 deref_cb_t *dc;
518
519                 sc = op->o_tmpcalloc( 1, sizeof( slap_callback ) + sizeof( deref_cb_t ), op->o_tmpmemctx );
520
521                 dc = (deref_cb_t *)&sc[ 1 ];
522                 dc->dc_on = (slap_overinst *)op->o_bd->bd_info;
523                 dc->dc_ds = (DerefSpec *)op->o_ctrlderef;
524
525                 sc->sc_response = deref_response;
526                 sc->sc_cleanup = deref_cleanup;
527                 sc->sc_private = (void *)dc;
528
529                 sc->sc_next = op->o_callback->sc_next;
530                 op->o_callback->sc_next = sc;
531         }
532
533         return SLAP_CB_CONTINUE;
534 }
535
536 int
537 deref_initialize(void)
538 {
539         int rc;
540
541         rc = register_supported_control( LDAP_CONTROL_X_DEREF,
542                 SLAP_CTRL_SEARCH, NULL,
543                 deref_parseCtrl, &deref_cid );
544         if ( rc != LDAP_SUCCESS ) {
545                 Debug( LDAP_DEBUG_ANY,
546                         "deref_init: Failed to register control (%d)\n",
547                 rc, 0, 0 );
548                 return -1;
549         }
550
551         deref.on_bi.bi_type = "deref";
552         deref.on_bi.bi_op_search = deref_op_search;
553
554         return overlay_register( &deref );
555 }
556
557 #if SLAPD_OVER_DEREF == SLAPD_MOD_DYNAMIC
558 int
559 init_module( int argc, char *argv[] )
560 {
561         return deref_initialize();
562 }
563 #endif /* SLAPD_OVER_DEREF == SLAPD_MOD_DYNAMIC */
564
565 #endif /* SLAPD_OVER_DEREF */