]> git.sur5r.net Git - openldap/blob - contrib/slapd-modules/cloak/cloak.c
ITS#6758 Use rs_*() to manage SlapReply entries.
[openldap] / contrib / slapd-modules / cloak / cloak.c
1 /* cloak.c - Overlay to hide some attribute except if explicitely requested */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2008-2010 The OpenLDAP Foundation.
6  * Portions Copyright 2008 Emmanuel Dreyfus
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 originally developed by the Emmanuel Dreyfus for
19  * inclusion in OpenLDAP Software.
20  */
21
22 #include "portable.h"
23
24 #ifdef SLAPD_OVER_CLOAK
25
26 #include <stdio.h>
27
28 #include <ac/string.h>
29 #include <ac/socket.h>
30
31 #include "lutil.h"
32 #include "slap.h"
33 #include "config.h"
34
35 enum { CLOAK_ATTR = 1 };
36
37 typedef struct cloak_info_t {
38         ObjectClass             *ci_oc; 
39         AttributeDescription    *ci_ad;
40         struct cloak_info_t     *ci_next;
41 } cloak_info_t;
42
43 #define CLOAK_USAGE "\"cloak-attr <attr> [<class>]\": "
44
45 static int
46 cloak_cfgen( ConfigArgs *c )
47 {
48         slap_overinst   *on = (slap_overinst *)c->bi;
49         cloak_info_t    *ci = (cloak_info_t *)on->on_bi.bi_private;
50
51         int             rc = 0, i;
52
53         if ( c->op == SLAP_CONFIG_EMIT ) {
54                 switch( c->type ) {
55                 case CLOAK_ATTR:
56                         for ( i = 0; ci; i++, ci = ci->ci_next ) {
57                                 struct berval   bv;
58                                 int len;
59
60                                 assert( ci->ci_ad != NULL );
61
62                                 if ( ci->ci_oc != NULL )
63                                         len = snprintf( c->cr_msg, 
64                                         sizeof( c->cr_msg ),
65                                         SLAP_X_ORDERED_FMT "%s %s", i,
66                                         ci->ci_ad->ad_cname.bv_val,
67                                         ci->ci_oc->soc_cname.bv_val );
68                                 else
69                                         len = snprintf( c->cr_msg, 
70                                         sizeof( c->cr_msg ),
71                                         SLAP_X_ORDERED_FMT "%s", i,
72                                         ci->ci_ad->ad_cname.bv_val );
73
74                                 bv.bv_val = c->cr_msg;
75                                 bv.bv_len = len;
76                                 value_add_one( &c->rvalue_vals, &bv );
77                         }
78                         break;
79
80                 default:
81                         rc = 1;
82                         break;
83                 }
84
85                 return rc;
86
87         } else if ( c->op == LDAP_MOD_DELETE ) {
88                 cloak_info_t    *ci_next;
89
90                 switch( c->type ) {
91                 case CLOAK_ATTR:
92                         for ( ci_next = ci, i = 0; 
93                               ci_next, c->valx < 0 || i < c->valx; 
94                               ci = ci_next, i++ ){
95
96                                 ci_next = ci->ci_next;
97
98                                 ch_free ( ci->ci_ad );
99                                 if ( ci->ci_oc != NULL )
100                                         ch_free ( ci->ci_oc );
101
102                                 ch_free( ci );
103                         }
104                         ci = (cloak_info_t *)on->on_bi.bi_private;
105                         break;
106
107                 default:
108                         rc = 1;
109                         break;
110                 }
111
112                 return rc;
113         }
114
115         switch( c->type ) {
116         case CLOAK_ATTR: {
117                 ObjectClass             *oc = NULL;
118                 AttributeDescription    *ad = NULL;
119                 const char              *text;
120                 cloak_info_t           **cip = NULL;
121                 cloak_info_t            *ci_next = NULL;
122
123                 if ( c->argc == 3 ) {
124                         oc = oc_find( c->argv[ 2 ] );
125                         if ( oc == NULL ) {
126                                 snprintf( c->cr_msg, 
127                                           sizeof( c->cr_msg ), 
128                                           CLOAK_USAGE
129                                           "unable to find ObjectClass \"%s\"",
130                                           c->argv[ 2 ] );
131                                 Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
132                                        c->log, c->cr_msg, 0 );
133                                 return 1;
134                         }
135                 }
136
137                 rc = slap_str2ad( c->argv[ 1 ], &ad, &text );
138                 if ( rc != LDAP_SUCCESS ) {
139                         snprintf( c->cr_msg, sizeof( c->cr_msg ), CLOAK_USAGE
140                                 "unable to find AttributeDescription \"%s\"",
141                                 c->argv[ 1 ] );
142                         Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
143                                 c->log, c->cr_msg, 0 );
144                         return 1;
145                 }
146
147                 for ( i = 0, cip = (cloak_info_t **)&on->on_bi.bi_private;
148                       c->valx < 0 || i < c->valx, *cip;
149                       i++, cip = &(*cip)->ci_next ) {
150                         if ( c->valx >= 0 && *cip == NULL ) {
151                                 snprintf( c->cr_msg, sizeof( c->cr_msg ),
152                                         CLOAK_USAGE
153                                         "invalid index {%d}\n",
154                                         c->valx );
155                                 Debug( LDAP_DEBUG_ANY, "%s: %s.\n",
156                                         c->log, c->cr_msg, 0 );
157                                 return 1;
158                         }
159                         ci_next = *cip;
160                 }
161
162                 *cip = (cloak_info_t *)ch_calloc( 1, sizeof( cloak_info_t ) );
163                 (*cip)->ci_oc = oc;
164                 (*cip)->ci_ad = ad;
165                 (*cip)->ci_next = ci_next;
166
167                 rc = 0;
168                 break;
169         }
170
171         default:
172                 rc = 1;
173                 break;
174         }
175
176         return rc;
177 }
178
179 static int
180 cloak_search_cb( Operation *op, SlapReply *rs )
181 {
182         slap_callback   *sc;
183         cloak_info_t    *ci;
184         Entry           *e = NULL;
185         Entry           *me = NULL;
186
187         assert( op && op->o_callback && rs );
188
189         if ( rs->sr_type != REP_SEARCH || !rs->sr_entry ) {
190                 slap_freeself_cb( op, rs );
191                 return ( SLAP_CB_CONTINUE );
192         }
193
194         sc = op->o_callback;
195         e = rs->sr_entry;
196
197         /* 
198          * First perform a quick scan for an attribute to cloak
199          */
200         for ( ci = (cloak_info_t *)sc->sc_private; ci; ci = ci->ci_next ) {
201                 Attribute *a;
202
203                 if ( ci->ci_oc != NULL &&
204                      !is_entry_objectclass_or_sub( e, ci->ci_oc ) )
205                         continue;
206
207                 for ( a = e->e_attrs; a; a = a->a_next )
208                         if ( a->a_desc == ci->ci_ad )
209                                 break;
210
211                 if ( a != NULL )
212                         break;
213         }
214
215         /*
216          * Nothing found to cloak
217          */
218         if ( ci == NULL )
219                 return ( SLAP_CB_CONTINUE );
220
221         /*
222          * We are now committed to cloak an attribute.
223          */
224         rs_ensure_entry_modifiable( op, rs, (slap_overinst *) op->o_bd->bd_info );
225         me = rs->sr_entry;
226                 
227         for ( ci = (cloak_info_t *)sc->sc_private; ci; ci = ci->ci_next ) {
228                 Attribute *a;
229                 Attribute *pa;
230
231                 for ( pa = NULL, a = me->e_attrs;
232                       a; 
233                       pa = a, a = a->a_next ) {
234
235                         if ( a->a_desc != ci->ci_ad )
236                                 continue;
237
238                         Debug( LDAP_DEBUG_TRACE, "cloak_search_cb: cloak %s\n", 
239                                a->a_desc->ad_cname.bv_val,
240                                0, 0 );
241
242                         if ( pa != NULL ) 
243                                 pa->a_next = a->a_next;
244                         else
245                                 me->e_attrs = a->a_next;
246
247                         attr_clean( a );
248                 }
249
250         }
251
252         return ( SLAP_CB_CONTINUE );
253 }
254
255 static int
256 cloak_search( Operation *op, SlapReply *rs )
257 {
258         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
259         cloak_info_t    *ci = (cloak_info_t *)on->on_bi.bi_private; 
260         slap_callback   *sc;
261
262         if ( op->ors_attrsonly ||
263              op->ors_attrs ||
264              get_manageDSAit( op ) )
265                 return SLAP_CB_CONTINUE;
266
267         sc = op->o_tmpcalloc( 1, sizeof( *sc ), op->o_tmpmemctx );
268         sc->sc_response = cloak_search_cb;
269         sc->sc_cleanup = slap_freeself_cb;
270         sc->sc_next = op->o_callback;
271         sc->sc_private = ci;
272         op->o_callback = sc;
273
274         return SLAP_CB_CONTINUE;
275 }
276
277 static slap_overinst cloak_ovl;
278
279 static ConfigTable cloakcfg[] = {
280         { "cloak-attr", "attribute [class]",
281                 2, 3, 0, ARG_MAGIC|CLOAK_ATTR, cloak_cfgen,
282                 "( OLcfgCtAt:4.1 NAME 'olcCloakAttribute' "
283                         "DESC 'Cloaked attribute: attribute [class]' "
284                         "EQUALITY caseIgnoreMatch "
285                         "SYNTAX OMsDirectoryString "
286                         "X-ORDERED 'VALUES' )",
287                         NULL, NULL },
288         { NULL, NULL, 0, 0, 0, ARG_IGNORED }
289 };
290
291 static ConfigOCs cloakocs[] = {
292         { "( OLcfgCtOc:4.1 "
293           "NAME 'olcCloakConfig' "
294           "DESC 'Attribute cloak configuration' "
295           "SUP olcOverlayConfig "
296           "MAY ( olcCloakAttribute ) )", 
297           Cft_Overlay, cloakcfg },
298         { NULL, 0, NULL }
299 };
300
301 #if SLAPD_OVER_CLOAK == SLAPD_MOD_DYNAMIC
302 static
303 #endif
304 int
305 cloak_initialize( void ) {
306         int rc;
307         cloak_ovl.on_bi.bi_type = "cloak";
308         cloak_ovl.on_bi.bi_op_search = cloak_search;
309         cloak_ovl.on_bi.bi_cf_ocs = cloakocs;
310
311         rc = config_register_schema ( cloakcfg, cloakocs );
312         if ( rc ) 
313                 return rc;
314
315         return overlay_register( &cloak_ovl );
316 }
317
318 #if SLAPD_OVER_CLOAK == SLAPD_MOD_DYNAMIC
319 int init_module(int argc, char *argv[]) {
320         return cloak_initialize();
321 }
322 #endif
323
324 #endif /* defined(SLAPD_OVER_CLOAK) */
325