]> git.sur5r.net Git - openldap/blob - servers/slapd/cr.c
Change slap_sasl_authorized to take an Operation instead of a Connection,
[openldap] / servers / slapd / cr.c
1 /* cr.c - content rule routines */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/ctype.h>
13 #include <ac/string.h>
14 #include <ac/socket.h>
15
16 #include "slap.h"
17 #include "ldap_pvt.h"
18
19 #ifdef SLAP_EXTENDED_SCHEMA
20
21 struct cindexrec {
22         struct berval   cir_name;
23         ContentRule     *cir_cr;
24 };
25
26 static Avlnode  *cr_index = NULL;
27 static LDAP_SLIST_HEAD(CRList, slap_content_rule) cr_list
28         = LDAP_SLIST_HEAD_INITIALIZER(&cr_list);
29
30 static int
31 cr_index_cmp(
32     const void  *v_cir1,
33     const void  *v_cir2 )
34 {
35         const struct cindexrec  *cir1 = v_cir1;
36         const struct cindexrec  *cir2 = v_cir2;
37         int i = cir1->cir_name.bv_len - cir2->cir_name.bv_len;
38         if (i)
39                 return i;
40         return strcasecmp( cir1->cir_name.bv_val, cir2->cir_name.bv_val );
41 }
42
43 static int
44 cr_index_name_cmp(
45     const void  *v_name,
46     const void  *v_cir )
47 {
48         const struct berval    *name = v_name;
49         const struct cindexrec *cir  = v_cir;
50         int i = name->bv_len - cir->cir_name.bv_len;
51         if (i)
52                 return i;
53         return strncasecmp( name->bv_val, cir->cir_name.bv_val, name->bv_len );
54 }
55
56 ContentRule *
57 cr_find( const char *crname )
58 {
59         struct berval bv;
60
61         bv.bv_val = (char *)crname;
62         bv.bv_len = strlen( crname );
63
64         return( cr_bvfind( &bv ) );
65 }
66
67 ContentRule *
68 cr_bvfind( struct berval *crname )
69 {
70         struct cindexrec        *cir;
71
72         cir = avl_find( cr_index, crname, cr_index_name_cmp );
73
74         if ( cir != NULL ) {
75                 return( cir->cir_cr );
76         }
77
78         return( NULL );
79 }
80
81 static int
82 cr_destroy_one( ContentRule *c )
83 {
84         assert( c != NULL );
85
86         if (c->scr_auxiliaries) ldap_memfree(c->scr_auxiliaries);
87         if (c->scr_required) ldap_memfree(c->scr_required);
88         if (c->scr_allowed) ldap_memfree(c->scr_allowed);
89         if (c->scr_precluded) ldap_memfree(c->scr_precluded);
90         ldap_contentrule_free((LDAPContentRule *)c);
91
92         return 0;
93 }
94
95 void
96 cr_destroy( void )
97 {
98         ContentRule *c;
99
100         avl_free(cr_index, ldap_memfree);
101
102         while( !LDAP_SLIST_EMPTY(&cr_list) ) {
103                 c = LDAP_SLIST_FIRST(&cr_list);
104                 LDAP_SLIST_REMOVE_HEAD(&cr_list, scr_next);
105
106                 cr_destroy_one( c );
107         }
108 }
109
110 static int
111 cr_insert(
112     ContentRule         *scr,
113     const char          **err
114 )
115 {
116         struct cindexrec        *cir;
117         char                    **names;
118
119         LDAP_SLIST_NEXT( scr, scr_next ) = NULL;
120         LDAP_SLIST_INSERT_HEAD(&cr_list, scr, scr_next);
121
122         if ( scr->scr_oid ) {
123                 cir = (struct cindexrec *)
124                         ch_calloc( 1, sizeof(struct cindexrec) );
125                 cir->cir_name.bv_val = scr->scr_oid;
126                 cir->cir_name.bv_len = strlen( scr->scr_oid );
127                 cir->cir_cr = scr;
128
129                 assert( cir->cir_name.bv_val );
130                 assert( cir->cir_cr );
131
132                 if ( avl_insert( &cr_index, (caddr_t) cir,
133                                  cr_index_cmp, avl_dup_error ) )
134                 {
135                         *err = scr->scr_oid;
136                         ldap_memfree(cir);
137                         return SLAP_SCHERR_CR_DUP;
138                 }
139
140                 /* FIX: temporal consistency check */
141                 assert( cr_bvfind(&cir->cir_name) != NULL );
142         }
143
144         if ( (names = scr->scr_names) ) {
145                 while ( *names ) {
146                         cir = (struct cindexrec *)
147                                 ch_calloc( 1, sizeof(struct cindexrec) );
148                         cir->cir_name.bv_val = *names;
149                         cir->cir_name.bv_len = strlen( *names );
150                         cir->cir_cr = scr;
151
152                         assert( cir->cir_name.bv_val );
153                         assert( cir->cir_cr );
154
155                         if ( avl_insert( &cr_index, (caddr_t) cir,
156                                          cr_index_cmp, avl_dup_error ) )
157                         {
158                                 *err = *names;
159                                 ldap_memfree(cir);
160                                 return SLAP_SCHERR_CR_DUP;
161                         }
162
163                         /* FIX: temporal consistency check */
164                         assert( cr_bvfind(&cir->cir_name) != NULL );
165
166                         names++;
167                 }
168         }
169
170         return 0;
171 }
172
173 static int
174 cr_add_auxiliaries(
175     ContentRule         *scr,
176         int                     *op,
177     const char          **err )
178 {
179         int naux;
180
181         if( scr->scr_oc_oids_aux == NULL ) return 0;
182         
183         for( naux=0; scr->scr_oc_oids_aux[naux]; naux++ ) {
184                 /* count them */ ;
185         }
186
187         scr->scr_auxiliaries = ch_calloc( naux+1, sizeof(ObjectClass *));
188
189         for( naux=0; scr->scr_oc_oids_aux[naux]; naux++ ) {
190                 ObjectClass *soc = scr->scr_auxiliaries[naux]
191                         = oc_find(scr->scr_oc_oids_aux[naux]);
192                 if ( !soc ) {
193                         *err = scr->scr_oc_oids_aux[naux];
194                         return SLAP_SCHERR_CLASS_NOT_FOUND;
195                 }
196
197                 if( soc->soc_flags & SLAP_OC_OPERATIONAL ) (*op)++;
198
199                 if( soc->soc_kind != LDAP_SCHEMA_AUXILIARY ) {
200                         *err = scr->scr_oc_oids_aux[naux];
201                         return SLAP_SCHERR_CR_BAD_AUX;
202                 }
203         }
204
205         scr->scr_auxiliaries[naux] = NULL;
206
207         return 0;
208 }
209
210 static int
211 cr_create_required(
212     ContentRule         *scr,
213         int                     *op,
214     const char          **err )
215 {
216     char                **attrs = scr->scr_at_oids_must;
217         char            **attrs1;
218         AttributeType   *sat;
219
220         if ( attrs ) {
221                 attrs1 = attrs;
222                 while ( *attrs1 ) {
223                         sat = at_find(*attrs1);
224                         if ( !sat ) {
225                                 *err = *attrs1;
226                                 return SLAP_SCHERR_ATTR_NOT_FOUND;
227                         }
228
229                         if( is_at_operational( sat )) (*op)++;
230
231                         if ( at_find_in_list(sat, scr->scr_required) < 0) {
232                                 if ( at_append_to_list(sat, &scr->scr_required) ) {
233                                         *err = *attrs1;
234                                         return SLAP_SCHERR_OUTOFMEM;
235                                 }
236                         } else {
237                                 *err = *attrs1;
238                                 return SLAP_SCHERR_CR_BAD_AT;
239                         }
240                         attrs1++;
241                 }
242         }
243         return 0;
244 }
245
246 static int
247 cr_create_allowed(
248     ContentRule         *scr,
249         int                     *op,
250     const char          **err )
251 {
252     char                **attrs = scr->scr_at_oids_may;
253         char            **attrs1;
254         AttributeType   *sat;
255
256         if ( attrs ) {
257                 attrs1 = attrs;
258                 while ( *attrs1 ) {
259                         sat = at_find(*attrs1);
260                         if ( !sat ) {
261                                 *err = *attrs1;
262                                 return SLAP_SCHERR_ATTR_NOT_FOUND;
263                         }
264
265                         if( is_at_operational( sat )) (*op)++;
266
267                         if ( at_find_in_list(sat, scr->scr_required) < 0 &&
268                                 at_find_in_list(sat, scr->scr_allowed) < 0 )
269                         {
270                                 if ( at_append_to_list(sat, &scr->scr_allowed) ) {
271                                         *err = *attrs1;
272                                         return SLAP_SCHERR_OUTOFMEM;
273                                 }
274                         } else {
275                                 *err = *attrs1;
276                                 return SLAP_SCHERR_CR_BAD_AT;
277                         }
278                         attrs1++;
279                 }
280         }
281         return 0;
282 }
283
284 static int
285 cr_create_precluded(
286     ContentRule         *scr,
287         int                     *op,
288     const char          **err )
289 {
290     char                **attrs = scr->scr_at_oids_not;
291         char            **attrs1;
292         AttributeType   *sat;
293
294         if ( attrs ) {
295                 attrs1 = attrs;
296                 while ( *attrs1 ) {
297                         sat = at_find(*attrs1);
298                         if ( !sat ) {
299                                 *err = *attrs1;
300                                 return SLAP_SCHERR_ATTR_NOT_FOUND;
301                         }
302
303                         if( is_at_operational( sat )) (*op)++;
304
305                         /* FIXME: should also make sure attribute type is not
306                                 a required attribute of the structural class or
307                                 any auxiliary class */
308                         if ( at_find_in_list(sat, scr->scr_required) < 0 &&
309                                 at_find_in_list(sat, scr->scr_allowed) < 0 &&
310                                 at_find_in_list(sat, scr->scr_precluded) < 0 )
311                         {
312                                 if ( at_append_to_list(sat, &scr->scr_precluded) ) {
313                                         *err = *attrs1;
314                                         return SLAP_SCHERR_OUTOFMEM;
315                                 }
316                         } else {
317                                 *err = *attrs1;
318                                 return SLAP_SCHERR_CR_BAD_AT;
319                         }
320                         attrs1++;
321                 }
322         }
323         return 0;
324 }
325
326 int
327 cr_add(
328     LDAPContentRule     *cr,
329         int user,
330     const char          **err
331 )
332 {
333         ContentRule     *scr;
334         int             code;
335         int             op = 0;
336
337         if ( cr->cr_names != NULL ) {
338                 int i;
339
340                 for( i=0; cr->cr_names[i]; i++ ) {
341                         if( !slap_valid_descr( cr->cr_names[i] ) ) {
342                                 return SLAP_SCHERR_BAD_DESCR;
343                         }
344                 }
345         }
346
347         if ( !OID_LEADCHAR( cr->cr_oid[0] )) {
348                 /* Expand OID macros */
349                 char *oid = oidm_find( cr->cr_oid );
350                 if ( !oid ) {
351                         *err = cr->cr_oid;
352                         return SLAP_SCHERR_OIDM;
353                 }
354                 if ( oid != cr->cr_oid ) {
355                         ldap_memfree( cr->cr_oid );
356                         cr->cr_oid = oid;
357                 }
358         }
359
360         scr = (ContentRule *) ch_calloc( 1, sizeof(ContentRule) );
361         AC_MEMCPY( &scr->scr_crule, cr, sizeof(LDAPContentRule) );
362
363         scr->scr_sclass = oc_find(cr->cr_oid);
364         if ( !scr->scr_sclass ) {
365                 *err = cr->cr_oid;
366                 return SLAP_SCHERR_CLASS_NOT_FOUND;
367         }
368
369         /* check object class usage */
370         if( scr->scr_sclass->soc_kind != LDAP_SCHEMA_STRUCTURAL )
371         {
372                 *err = cr->cr_oid;
373                 return SLAP_SCHERR_CR_BAD_STRUCT;
374         }
375
376         if( scr->scr_sclass->soc_flags & SLAP_OC_OPERATIONAL ) op++;
377
378         code = cr_add_auxiliaries( scr, &op, err );
379         if ( code != 0 ) return code;
380
381         code = cr_create_required( scr, &op, err );
382         if ( code != 0 ) return code;
383
384         code = cr_create_allowed( scr, &op, err );
385         if ( code != 0 ) return code;
386
387         code = cr_create_precluded( scr, &op, err );
388         if ( code != 0 ) return code;
389
390         if( user && op ) return SLAP_SCHERR_CR_BAD_AUX;
391
392         code = cr_insert(scr,err);
393         return code;
394 }
395
396 #endif
397
398 int
399 cr_schema_info( Entry *e )
400 {
401 #ifdef SLAP_EXTENDED_SCHEMA
402         AttributeDescription *ad_ditContentRules
403                 = slap_schema.si_ad_ditContentRules;
404         ContentRule     *cr;
405
406         struct berval   val;
407         struct berval   nval;
408
409         LDAP_SLIST_FOREACH(cr, &cr_list, scr_next) {
410                 if ( ldap_contentrule2bv( &cr->scr_crule, &val ) == NULL ) {
411                         return -1;
412                 }
413
414 #if 0
415                 if( cr->scr_flags & SLAP_CR_HIDE ) continue;
416 #endif
417 #if 0
418                 Debug( LDAP_DEBUG_TRACE, "Merging cr [%ld] %s\n",
419                (long) val.bv_len, val.bv_val, 0 );
420 #endif
421
422                 nval.bv_val = cr->scr_oid;
423                 nval.bv_len = strlen(cr->scr_oid);
424
425                 if( attr_merge_one( e, ad_ditContentRules, &val, &nval ) )
426                 {
427                         return -1;
428                 }
429                 ldap_memfree( val.bv_val );
430         }
431 #endif
432         return 0;
433 }