]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/deref.c
fef5ab1250f7e4d64d3c1ebcf2039ad9c662b388
[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 #define o_deref                 o_ctrlflag[deref_cid]
104 #define o_ctrlderef             o_controls[deref_cid]
105
106 typedef struct DerefSpec {
107         AttributeDescription    *ds_derefAttr;
108         AttributeDescription    **ds_attributes;
109         int                     ds_nattrs;
110         struct DerefSpec        *ds_next;
111 } DerefSpec;
112
113 typedef struct DerefVal {
114         struct berval   dv_derefSpecVal;
115         BerVarray       *dv_attrVals;
116 } DerefVal;
117
118 typedef struct DerefRes {
119         DerefSpec               dr_spec;
120         DerefVal                *dr_vals;
121         struct DerefRes         *dr_next;
122 } DerefRes;
123
124 typedef struct deref_cb_t {
125         slap_overinst *dc_on;
126         DerefSpec *dc_ds;
127 } deref_cb_t;
128
129 static int                      deref_cid;
130 static slap_overinst            deref;
131
132 static int
133 deref_parseCtrl (
134         Operation *op,
135         SlapReply *rs,
136         LDAPControl *ctrl )
137 {
138         ber_tag_t tag;
139         BerElementBuffer berbuf;
140         BerElement *ber = (BerElement *)&berbuf;
141         ber_len_t len;
142         char *last;
143         DerefSpec *dshead = NULL, **dsp = &dshead;
144         BerVarray attributes = NULL;
145
146         if ( op->o_deref != SLAP_CONTROL_NONE ) {
147                 rs->sr_text = "Dereference control specified multiple times";
148                 return LDAP_PROTOCOL_ERROR;
149         }
150
151         if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
152                 rs->sr_text = "Dereference control value is absent";
153                 return LDAP_PROTOCOL_ERROR;
154         }
155
156         if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
157                 rs->sr_text = "Dereference control value is empty";
158                 return LDAP_PROTOCOL_ERROR;
159         }
160
161         ber_init2( ber, &ctrl->ldctl_value, 0 );
162
163         for ( tag = ber_first_element( ber, &len, &last );
164                 tag != LBER_DEFAULT;
165                 tag = ber_next_element( ber, &len, last ) )
166         {
167                 struct berval derefAttr;
168                 DerefSpec *ds, *dstmp;
169                 const char *text;
170                 int rc;
171                 ber_len_t cnt = sizeof(struct berval);
172                 ber_len_t off = 0;
173
174                 if ( ber_scanf( ber, "{m{M}}", &derefAttr, &attributes, &cnt, off ) == LBER_ERROR )
175                 {
176                         rs->sr_text = "Dereference control: derefSpec decoding error";
177                         rs->sr_err = LDAP_PROTOCOL_ERROR;
178                         goto done;
179                 }
180
181                 ds = (DerefSpec *)op->o_tmpcalloc( 1,
182                         sizeof(DerefSpec) + sizeof(AttributeDescription *)*(cnt + 1),
183                         op->o_tmpmemctx );
184                 ds->ds_attributes = (AttributeDescription **)&ds[1];
185                 ds->ds_nattrs = cnt;
186
187                 rc = slap_bv2ad( &derefAttr, &ds->ds_derefAttr, &text );
188                 if ( rc != LDAP_SUCCESS ) {
189                         rs->sr_text = "Dereference control: derefAttr decoding error";
190                         rs->sr_err = LDAP_PROTOCOL_ERROR;
191                         goto done;
192                 }
193
194                 for ( dstmp = dshead; dstmp && dstmp != ds; dstmp = dstmp->ds_next ) {
195                         if ( dstmp->ds_derefAttr == ds->ds_derefAttr ) {
196                                 rs->sr_text = "Dereference control: derefAttr must be unique within control";
197                                 rs->sr_err = LDAP_PROTOCOL_ERROR;
198                                 goto done;
199                         }
200                 }
201
202                 if ( ds->ds_derefAttr->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
203                         if ( ctrl->ldctl_iscritical ) {
204                                 rs->sr_text = "Dereference control: derefAttr syntax not distinguishedName";
205                                 rs->sr_err = LDAP_PROTOCOL_ERROR;
206                                 goto done;
207                         }
208
209                         rs->sr_err = LDAP_SUCCESS;
210                         goto justcleanup;
211                 }
212
213                 for ( cnt = 0; !BER_BVISNULL( &attributes[ cnt ] ); cnt++ ) {
214                         rc = slap_bv2ad( &attributes[ cnt ], &ds->ds_attributes[ cnt ], &text );
215                         if ( rc != LDAP_SUCCESS ) {
216                                 rs->sr_text = "Dereference control: attribute decoding error";
217                                 rs->sr_err = LDAP_PROTOCOL_ERROR;
218                                 goto done;
219                         }
220                 }
221
222                 ber_memfree_x( attributes, op->o_tmpmemctx );
223                 attributes = NULL;
224
225                 *dsp = ds;
226                 dsp = &ds->ds_next;
227         }
228
229         op->o_ctrlderef = (void *)dshead;
230
231         op->o_deref = ctrl->ldctl_iscritical
232                 ? SLAP_CONTROL_CRITICAL
233                 : SLAP_CONTROL_NONCRITICAL;
234
235         rs->sr_err = LDAP_SUCCESS;
236
237 done:;
238         if ( rs->sr_err != LDAP_SUCCESS ) {
239 justcleanup:;
240                 for ( ; dshead; ) {
241                         DerefSpec *dsnext = dshead->ds_next;
242                         op->o_tmpfree( dshead, op->o_tmpmemctx );
243                         dshead = dsnext;
244                 }
245         }
246
247         if ( attributes != NULL ) {
248                 ber_memfree_x( attributes, op->o_tmpmemctx );
249         }
250
251         return rs->sr_err;
252 }
253
254 static int
255 deref_response( Operation *op, SlapReply *rs )
256 {
257         int rc = SLAP_CB_CONTINUE;
258
259         if ( rs->sr_type == REP_SEARCH ) {
260                 BerElementBuffer berbuf;
261                 BerElement *ber = (BerElement *) &berbuf;
262                 deref_cb_t *dc = (deref_cb_t *)op->o_callback->sc_private;
263                 DerefSpec *ds;
264                 DerefRes *dr, *drhead = NULL, **drp = &drhead;
265                 BackendInfo *bi = op->o_bd->bd_info;
266                 struct berval bv = BER_BVNULL;
267                 int nDerefRes = 0, nDerefVals = 0, nAttrs = 0, nVals = 0;
268                 struct berval ctrlval;
269                 LDAPControl *ctrl, **ctrlsp;
270                 int i;
271
272                 op->o_bd->bd_info = (BackendInfo *)dc->dc_on->on_info;
273                 for ( ds = dc->dc_ds; ds; ds = ds->ds_next ) {
274                         Attribute *a = attr_find( rs->sr_entry->e_attrs, ds->ds_derefAttr );
275
276                         if ( a != NULL ) {
277                                 DerefVal *dv;
278                                 BerVarray *bva;
279
280                                 dr = op->o_tmpcalloc( 1,
281                                         sizeof( DerefRes ) + ( sizeof( DerefVal ) + sizeof( BerVarray * ) * ds->ds_nattrs ) * ( a->a_numvals + 1 ),
282                                         op->o_tmpmemctx );
283                                 dr->dr_spec = *ds;
284                                 dv = dr->dr_vals = (DerefVal *)&dr[ 1 ];
285                                 bva = (BerVarray *)&dv[ a->a_numvals + 1 ];
286
287                                 bv.bv_len += ds->ds_derefAttr->ad_cname.bv_len;
288                                 nAttrs++;
289                                 nDerefRes++;
290
291                                 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ ) {
292                                         Entry *e = NULL;
293
294                                         dv[ i ].dv_attrVals = bva;
295                                         bva += ds->ds_nattrs;
296
297                                         dv[i].dv_derefSpecVal = a->a_vals[ i ];
298                                         bv.bv_len += dv[i].dv_derefSpecVal.bv_len;
299                                         nVals++;
300                                         nDerefVals++;
301
302                                         rc = overlay_entry_get_ov( op, &a->a_nvals[ i ], NULL, NULL, 0, &e, dc->dc_on );
303                                         if ( rc == LDAP_SUCCESS && e != NULL ) {
304                                                 int j;
305
306                                                 for ( j = 0; j < ds->ds_nattrs; j++ ) {
307                                                         Attribute *aa = attr_find( e->e_attrs, ds->ds_attributes[ j ] );
308                                                         if ( aa != NULL ) {
309                                                                 int k;
310
311                                                                 dv[i].dv_attrVals[ j ] = aa->a_vals;
312
313                                                                 bv.bv_len += ds->ds_attributes[ j ]->ad_cname.bv_len;
314                                                                 for ( k = 0; !BER_BVISNULL( &aa->a_vals[ k ] ); k++ ) {
315                                                                         bv.bv_len += aa->a_vals[ k ].bv_len;
316                                                                         nVals++;
317                                                                 }
318                                                                 nAttrs++;
319                                                         }
320                                                 }
321
322                                                 overlay_entry_release_ov( op, e, 0, dc->dc_on );
323                                         }
324                                 }
325
326                                 *drp = dr;
327                                 drp = &dr->dr_next;
328                         }
329                 }
330                 op->o_bd->bd_info = bi;
331
332                 if ( drhead == NULL ) {
333                         return SLAP_CB_CONTINUE;
334                 }
335
336                 /* cook the control value */
337                 bv.bv_len += nVals * sizeof(struct berval)
338                         + nAttrs * sizeof(struct berval)
339                         + nDerefVals * sizeof(DerefVal)
340                         + nDerefRes * sizeof(DerefRes);
341                 bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
342
343                 ber_init2( ber, &bv, LBER_USE_DER );
344                 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
345
346                 rc = ber_printf( ber, "{" /*}*/ );
347                 for ( dr = drhead; dr != NULL; dr = dr->dr_next ) {
348                         for ( i = 0; !BER_BVISNULL( &dr->dr_vals[ i ].dv_derefSpecVal ); i++ ) {
349                                 int j, first = 1;
350
351                                 rc = ber_printf( ber, "{OO" /*}*/,
352                                         &dr->dr_spec.ds_derefAttr->ad_cname,
353                                         &dr->dr_vals[ i ].dv_derefSpecVal );
354                                 for ( j = 0; j < dr->dr_spec.ds_nattrs; j++ ) {
355                                         if ( dr->dr_vals[ i ].dv_attrVals[ j ] != NULL ) {
356                                                 if ( first ) {
357                                                         rc = ber_printf( ber, "t{" /*}*/,
358                                                                 (LBER_CONSTRUCTED|LBER_CLASS_CONTEXT) );
359                                                         first = 0;
360                                                 }
361                                                 rc = ber_printf( ber, "{O[W]}",
362                                                         &dr->dr_spec.ds_attributes[ j ]->ad_cname,
363                                                         dr->dr_vals[ i ].dv_attrVals[ j ] );
364                                         }
365                                 }
366                                 if ( !first ) {
367                                         rc = ber_printf( ber, /*{{*/ "}N}" );
368                                 } else {
369                                         rc = ber_printf( ber, /*{*/ "}" );
370                                 }
371                         }
372                 }
373                 rc = ber_printf( ber, /*{*/ "}" );
374                 if ( ber_flatten2( ber, &ctrlval, 0 ) == -1 ) {
375                         if ( op->o_deref == SLAP_CONTROL_CRITICAL ) {
376                                 rc = LDAP_CONSTRAINT_VIOLATION;
377
378                         } else {
379                                 rc = SLAP_CB_CONTINUE;
380                         }
381                         goto cleanup;
382                 }
383
384                 ctrl = op->o_tmpcalloc( 1,
385                         sizeof( LDAPControl ) + ctrlval.bv_len + 1,
386                         op->o_tmpmemctx );
387                 ctrl->ldctl_value.bv_val = (char *)&ctrl[ 1 ];
388                 ctrl->ldctl_oid = LDAP_CONTROL_X_DEREF;
389                 ctrl->ldctl_iscritical = 0;
390                 ctrl->ldctl_value.bv_len = ctrlval.bv_len;
391                 lutil_strncopy( ctrl->ldctl_value.bv_val, ctrlval.bv_val, ctrlval.bv_len );
392                 ctrl->ldctl_value.bv_val[ ctrl->ldctl_value.bv_len ] = '\0';
393
394                 ber_free_buf( ber );
395
396                 i = 0;
397                 if ( rs->sr_ctrls ) {
398                         for ( ; rs->sr_ctrls[ i ] != NULL; i++ )
399                                 /* count'em */ ;
400                 }
401                 i += 2;
402                 ctrlsp = op->o_tmpcalloc( i, sizeof(LDAPControl *), op->o_tmpmemctx );
403                 i = 0;
404                 if ( rs->sr_ctrls != NULL ) {
405                         for ( ; rs->sr_ctrls[ i ] != NULL; i++ ) {
406                                 ctrlsp[ i ] = rs->sr_ctrls[ i ];
407                         }
408                 }
409                 ctrlsp[ i++ ] = ctrl;
410                 ctrlsp[ i++ ] = NULL;
411                 if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED ) {
412                         op->o_tmpfree( rs->sr_ctrls, op->o_tmpmemctx );
413                 }
414                 rs->sr_ctrls = ctrlsp;
415                 rs->sr_flags |= REP_CTRLS_MUSTBEFREED;
416
417                 rc = SLAP_CB_CONTINUE;
418
419 cleanup:;
420                 /* release all */
421                 for ( ; drhead != NULL; ) {
422                         DerefRes *drnext = drhead->dr_next;
423                         op->o_tmpfree( drhead, op->o_tmpmemctx );
424                         drhead = drnext;
425                 }
426         }
427
428         return rc;
429 }
430
431 static int
432 deref_cleanup( Operation *op, SlapReply *rs )
433 {
434         if ( rs->sr_type == REP_RESULT || rs->sr_err == SLAPD_ABANDON ) {
435                 op->o_tmpfree( op->o_callback, op->o_tmpmemctx );
436                 op->o_callback = NULL;
437
438                 op->o_tmpfree( op->o_ctrlderef, op->o_tmpmemctx );
439                 op->o_ctrlderef = NULL;
440         }
441
442         return SLAP_CB_CONTINUE;
443 }
444
445 static int
446 deref_op_search( Operation *op, SlapReply *rs )
447 {
448         if ( op->o_deref ) {
449                 slap_callback *sc;
450                 deref_cb_t *dc;
451
452                 sc = op->o_tmpcalloc( 1, sizeof( slap_callback ) + sizeof( deref_cb_t ), op->o_tmpmemctx );
453
454                 dc = (deref_cb_t *)&sc[ 1 ];
455                 dc->dc_on = (slap_overinst *)op->o_bd->bd_info;
456                 dc->dc_ds = (DerefSpec *)op->o_ctrlderef;
457
458                 sc->sc_response = deref_response;
459                 sc->sc_cleanup = deref_cleanup;
460                 sc->sc_private = (void *)dc;
461
462                 sc->sc_next = op->o_callback->sc_next;
463                 op->o_callback->sc_next = sc;
464         }
465
466         return SLAP_CB_CONTINUE;
467 }
468
469 int
470 deref_initialize(void)
471 {
472         int rc;
473
474         rc = register_supported_control( LDAP_CONTROL_X_DEREF,
475                 SLAP_CTRL_SEARCH, NULL,
476                 deref_parseCtrl, &deref_cid );
477         if ( rc != LDAP_SUCCESS ) {
478                 Debug( LDAP_DEBUG_ANY,
479                         "deref_init: Failed to register control (%d)\n",
480                 rc, 0, 0 );
481                 return -1;
482         }
483
484         deref.on_bi.bi_type = "deref";
485         deref.on_bi.bi_op_search = deref_op_search;
486
487         return overlay_register( &deref );
488 }
489
490 #if SLAPD_OVER_DEREF == SLAPD_MOD_DYNAMIC
491 int
492 init_module( int argc, char *argv[] )
493 {
494         return deref_initialize();
495 }
496 #endif /* SLAPD_OVER_DEREF == SLAPD_MOD_DYNAMIC */
497
498 #endif /* SLAPD_OVER_DEREF */