]> git.sur5r.net Git - openldap/blob - servers/slapd/at.c
SLAPD_SCHEMA_NOT_COMPAT addl. filter changes (a work in progress).
[openldap] / servers / slapd / at.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /* at.c - routines for dealing with attribute types */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/ctype.h>
13 #include <ac/errno.h>
14 #include <ac/socket.h>
15 #include <ac/string.h>
16 #include <ac/time.h>
17
18 #include "ldap_pvt.h"
19 #include "slap.h"
20
21 #ifndef SLAPD_SCHEMA_NOT_COMPAT
22 char *
23 at_canonical_name( const char * a_type )
24 {
25         AttributeType   *atp;
26
27         atp=at_find(a_type);
28
29         if ( atp == NULL ) {
30                 return (char *) a_type;
31
32         } else if ( atp->sat_names
33                 && atp->sat_names[0] && (*(atp->sat_names[0]) != '\0') )
34         {
35                 return atp->sat_names[0];
36
37         } else if (atp->sat_oid && (*atp->sat_oid != '\0')) {
38                 return atp->sat_oid;
39         }
40
41         return (char *) a_type;
42 }
43
44 #define DEFAULT_SYNTAX  SYNTAX_CIS
45
46 /*
47  * attr_syntax - return the syntax of attribute type
48  */
49
50 int
51 attr_syntax( const char *type )
52 {
53         AttributeType   *sat;
54
55         sat = at_find(type);
56         if ( sat ) {
57                 return( sat->sat_syntax_compat );
58         }
59
60         return( DEFAULT_SYNTAX );
61 }
62
63 /*
64  * attr_syntax_config - process an attribute syntax config line
65  */
66
67 void
68 at_config(
69     const char  *fname,
70     int         lineno,
71     int         argc,
72     char        **argv
73 )
74 {
75         char                    *save;
76         LDAP_ATTRIBUTE_TYPE     *at;
77         int                     lasti;
78         int                     code;
79         const char              *err;
80
81         if ( argc < 2 ) {
82                 Debug( LDAP_DEBUG_ANY,
83 "%s: line %d: missing name in \"attribute <name>+ <syntax>\" (ignored)\n",
84                     fname, lineno, 0 );
85                 return;
86         }
87
88         at = (LDAP_ATTRIBUTE_TYPE *)
89                 ch_calloc( 1, sizeof(LDAP_ATTRIBUTE_TYPE) );
90
91 #define SYNTAX_DS_OID   "1.3.6.1.4.1.1466.115.121.1.15"
92 #define SYNTAX_DSCE_OID "2.5.13.5"
93 #define SYNTAX_IA5_OID  "1.3.6.1.4.1.1466.115.121.1.26"
94 #define SYNTAX_IA5CE_OID        "1.3.6.1.4.1.1466.109.114.1"
95 #define SYNTAX_DN_OID   SLAPD_OID_DN_SYNTAX
96 #define SYNTAX_TEL_OID  "1.3.6.1.4.1.1466.115.121.1.50"
97 #define SYNTAX_BIN_OID  "1.3.6.1.4.1.1466.115.121.1.40" /* octetString */
98
99         lasti = argc - 1;
100         if ( strcasecmp( argv[lasti], "caseignorestring" ) == 0 ||
101             strcasecmp( argv[lasti], "cis" ) == 0 ) {
102                 at->at_syntax_oid = SYNTAX_DS_OID;
103                 at->at_equality_oid = "2.5.13.2";
104                 at->at_ordering_oid = "2.5.13.3";
105                 at->at_substr_oid = "2.5.13.4";
106
107         } else if ( strcasecmp( argv[lasti], "telephone" ) == 0 ||
108             strcasecmp( argv[lasti], "tel" ) == 0 ) {
109                 at->at_syntax_oid = SYNTAX_TEL_OID;
110                 at->at_equality_oid = "2.5.13.20";
111                 at->at_substr_oid = "2.5.13.21";
112
113         } else if ( strcasecmp( argv[lasti], "dn" ) == 0 ) {
114                 at->at_syntax_oid = SYNTAX_DN_OID;
115                 at->at_equality_oid = "2.5.13.1";
116
117         } else if ( strcasecmp( argv[lasti], "caseexactstring" ) == 0 ||
118             strcasecmp( argv[lasti], "ces" ) == 0 ) {
119                 at->at_syntax_oid = SYNTAX_DS_OID;
120                 at->at_equality_oid = SYNTAX_DSCE_OID;
121                 at->at_ordering_oid = "2.5.13.6";
122                 at->at_substr_oid = "2.5.13.7";
123
124         } else if ( strcasecmp( argv[lasti], "binary" ) == 0 ||
125             strcasecmp( argv[lasti], "bin" ) == 0 ) {
126                 /* bin -> octetString, not binary! */
127                 at->at_syntax_oid = SYNTAX_BIN_OID;
128                 at->at_equality_oid = "2.5.13.17";
129
130         } else {
131                 Debug( LDAP_DEBUG_ANY,
132             "%s: line %d: unknown syntax \"%s\" in attribute line (ignored)\n",
133                     fname, lineno, argv[lasti] );
134                 Debug( LDAP_DEBUG_ANY,
135     "possible syntaxes are \"cis\", \"ces\", \"tel\", \"dn\", or \"bin\"\n",
136                     0, 0, 0 );
137                 free( (AttributeType *) at );
138                 return;
139         }
140
141         save = argv[lasti];
142         argv[lasti] = NULL;
143         at->at_names = charray_dup( argv );
144         argv[lasti] = save;
145
146         code = at_add( at, &err );
147         if ( code ) {
148                 fprintf( stderr, "%s: line %d: %s %s\n",
149                          fname, lineno, scherr2str(code), err);
150                 exit( EXIT_FAILURE );
151         }
152
153         ldap_memfree(at);
154 }
155
156 int
157 at_fake_if_needed(
158     const char  *name
159 )
160 {
161         char *argv[3];
162
163         if ( at_find( name ) ) {
164                 return 0;
165         } else {
166                 argv[0] = (char*) name;
167                 argv[1] = "cis";
168                 argv[2] = NULL;
169                 at_config( "implicit", 0, 2, argv );
170                 return 0;
171         }
172 }
173
174 #endif
175
176 int is_at_subtype(
177         AttributeType *sub,
178         AttributeType *sup )
179 {
180         for( ; sub != NULL; sub = sub->sat_sup ) {
181                 if( sub == sup ) return 1;
182         }
183
184         return 0;
185 }
186
187 struct aindexrec {
188         char            *air_name;
189         AttributeType   *air_at;
190 };
191
192 static Avlnode  *attr_index = NULL;
193 static AttributeType *attr_list = NULL;
194
195 static int
196 attr_index_cmp(
197     struct aindexrec    *air1,
198     struct aindexrec    *air2
199 )
200 {
201         return (strcasecmp( air1->air_name, air2->air_name ));
202 }
203
204 static int
205 attr_index_name_cmp(
206     char                *type,
207     struct aindexrec    *air
208 )
209 {
210         return (strcasecmp( type, air->air_name ));
211 }
212
213 AttributeType *
214 at_find(
215     const char          *name
216 )
217 {
218         struct aindexrec        *air;
219         char                    *tmpname;
220
221 #ifndef SLAPD_SCHEMA_NOT_COMPAT
222         /*
223          * The name may actually be an AttributeDescription, i.e. it may
224          * contain options.
225          */
226         /* Treat any attribute type with option as an unknown attribute type */
227         char *p = strchr( name, ';' );
228         if ( p ) {
229                 tmpname = ch_malloc( p-name+1 );
230                 strncpy( tmpname, name, p-name );
231                 tmpname[p-name] = '\0';
232         } else
233 #endif
234         {
235                 tmpname = (char *)name;
236         }
237
238         if ( (air = (struct aindexrec *) avl_find( attr_index, tmpname,
239             (AVL_CMP) attr_index_name_cmp )) != NULL ) {
240                 if ( tmpname != name )
241                         ldap_memfree( tmpname );
242                 return( air->air_at );
243         }
244
245         if ( tmpname != name )
246                 ldap_memfree( tmpname );
247         return( NULL );
248 }
249
250 int
251 at_append_to_list(
252     AttributeType       *sat,
253     AttributeType       ***listp
254 )
255 {
256         AttributeType   **list;
257         AttributeType   **list1;
258         int             size;
259
260         list = *listp;
261         if ( !list ) {
262                 size = 2;
263                 list = calloc(size, sizeof(AttributeType *));
264                 if ( !list ) {
265                         return -1;
266                 }
267         } else {
268                 size = 0;
269                 list1 = *listp;
270                 while ( *list1 ) {
271                         size++;
272                         list1++;
273                 }
274                 size += 2;
275                 list1 = realloc(list, size*sizeof(AttributeType *));
276                 if ( !list1 ) {
277                         return -1;
278                 }
279                 list = list1;
280         }
281         list[size-2] = sat;
282         list[size-1] = NULL;
283         *listp = list;
284         return 0;
285 }
286
287 int
288 at_delete_from_list(
289     int                 pos,
290     AttributeType       ***listp
291 )
292 {
293         AttributeType   **list;
294         AttributeType   **list1;
295         int             i;
296         int             j;
297
298         if ( pos < 0 ) {
299                 return -2;
300         }
301         list = *listp;
302         for ( i=0; list[i]; i++ )
303                 ;
304         if ( pos >= i ) {
305                 return -2;
306         }
307         for ( i=pos, j=pos+1; list[j]; i++, j++ ) {
308                 list[i] = list[j];
309         }
310         list[i] = NULL;
311         /* Tell the runtime this can be shrinked */
312         list1 = realloc(list, (i+1)*sizeof(AttributeType **));
313         if ( !list1 ) {
314                 return -1;
315         }
316         *listp = list1;
317         return 0;
318 }
319
320 int
321 at_find_in_list(
322     AttributeType       *sat,
323     AttributeType       **list
324 )
325 {
326         int     i;
327
328         if ( !list ) {
329                 return -1;
330         }
331         for ( i=0; list[i]; i++ ) {
332                 if ( sat == list[i] ) {
333                         return i;
334                 }
335         }
336         return -1;
337 }
338
339 static int
340 at_insert(
341     AttributeType       *sat,
342     const char          **err
343 )
344 {
345         AttributeType           **atp;
346         struct aindexrec        *air;
347         char                    **names;
348
349         atp = &attr_list;
350         while ( *atp != NULL ) {
351                 atp = &(*atp)->sat_next;
352         }
353         *atp = sat;
354
355         if ( sat->sat_oid ) {
356                 air = (struct aindexrec *)
357                         ch_calloc( 1, sizeof(struct aindexrec) );
358                 air->air_name = sat->sat_oid;
359                 air->air_at = sat;
360                 if ( avl_insert( &attr_index, (caddr_t) air,
361                                  (AVL_CMP) attr_index_cmp,
362                                  (AVL_DUP) avl_dup_error ) ) {
363                         *err = sat->sat_oid;
364                         ldap_memfree(air);
365                         return SLAP_SCHERR_DUP_ATTR;
366                 }
367                 /* FIX: temporal consistency check */
368                 at_find(air->air_name);
369         }
370
371         if ( (names = sat->sat_names) ) {
372                 while ( *names ) {
373                         air = (struct aindexrec *)
374                                 ch_calloc( 1, sizeof(struct aindexrec) );
375                         air->air_name = ch_strdup(*names);
376                         air->air_at = sat;
377                         if ( avl_insert( &attr_index, (caddr_t) air,
378                                          (AVL_CMP) attr_index_cmp,
379                                          (AVL_DUP) avl_dup_error ) ) {
380                                 *err = *names;
381                                 ldap_memfree(air->air_name);
382                                 ldap_memfree(air);
383                                 return SLAP_SCHERR_DUP_ATTR;
384                         }
385                         /* FIX: temporal consistency check */
386                         at_find(air->air_name);
387                         names++;
388                 }
389         }
390
391         return 0;
392 }
393
394 int
395 at_add(
396     LDAP_ATTRIBUTE_TYPE *at,
397     const char          **err
398 )
399 {
400         AttributeType   *sat;
401         MatchingRule    *mr;
402         Syntax          *syn;
403         int             code;
404         char                    *cname;
405
406         if ( at->at_names && at->at_names[0] ) {
407                 cname = at->at_names[0];
408         } else if ( at->at_oid ) {
409                 cname = at->at_oid;
410         } else {
411                 cname = "";
412                 return SLAP_SCHERR_ATTR_INCOMPLETE;
413         }
414         sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
415         memcpy( &sat->sat_atype, at, sizeof(LDAP_ATTRIBUTE_TYPE));
416
417 #ifdef SLAPD_SCHEMA_NOT_COMPAT
418         sat->sat_cname = cname;
419 #endif
420
421         if ( at->at_sup_oid ) {
422                 AttributeType *supsat = at_find(at->at_sup_oid);
423
424                 if ( (supsat == NULL ) ) {
425                         *err = at->at_sup_oid;
426                         return SLAP_SCHERR_ATTR_NOT_FOUND;
427                 }
428
429                 sat->sat_sup = supsat;
430
431                 if ( at_append_to_list(sat, &supsat->sat_subtypes) ) {
432                         *err = cname;
433                         return SLAP_SCHERR_OUTOFMEM;
434                 }
435         }
436
437         /*
438          * Inherit definitions from superiors.  We only check the
439          * direct superior since that one has already inherited from
440          * its own superiorss
441          */
442         if ( sat->sat_sup ) {
443                 sat->sat_syntax = sat->sat_sup->sat_syntax;
444 #ifndef SLAPD_SCHEMA_NOT_COMPAT
445                 sat->sat_syntax_compat = sat->sat_sup->sat_syntax_compat;
446 #endif
447                 sat->sat_equality = sat->sat_sup->sat_equality;
448                 sat->sat_ordering = sat->sat_sup->sat_ordering;
449                 sat->sat_substr = sat->sat_sup->sat_substr;
450         }
451
452         if ( at->at_syntax_oid ) {
453                 if ( (syn = syn_find(sat->sat_syntax_oid)) ) {
454                         sat->sat_syntax = syn;
455                 } else {
456                         *err = sat->sat_syntax_oid;
457                         return SLAP_SCHERR_SYN_NOT_FOUND;
458                 }
459
460 #ifndef SLAPD_SCHEMA_NOT_COMPAT
461                 if ( !strcmp(at->at_syntax_oid, SYNTAX_DS_OID) ) {
462                         if ( at->at_equality_oid && (
463                                 !strcmp(at->at_equality_oid, SYNTAX_DSCE_OID) ) )
464                         {
465                                 sat->sat_syntax_compat = SYNTAX_CES;
466                         } else {
467                                 sat->sat_syntax_compat = SYNTAX_CIS;
468                         }
469
470                 } else if ( !strcmp(at->at_syntax_oid, SYNTAX_IA5_OID) ) {
471                         if ( at->at_equality_oid && (
472                                 !strcmp(at->at_equality_oid, SYNTAX_IA5CE_OID) ) )
473                         {
474                                 sat->sat_syntax_compat = SYNTAX_CES;
475                         } else {
476                                 sat->sat_syntax_compat = SYNTAX_CIS;
477                         }
478
479                 } else if ( !strcmp(at->at_syntax_oid, SYNTAX_DN_OID ) ) {
480                         sat->sat_syntax_compat = SYNTAX_CIS | SYNTAX_DN;
481
482                 } else if ( !strcmp(at->at_syntax_oid, SYNTAX_TEL_OID ) ) {
483                         sat->sat_syntax_compat = SYNTAX_CIS | SYNTAX_TEL;
484
485                 } else if ( !strcmp(at->at_syntax_oid, SYNTAX_BIN_OID ) ) {
486                         sat->sat_syntax_compat = SYNTAX_BIN;
487
488                 } else {
489                         sat->sat_syntax_compat = DEFAULT_SYNTAX;
490                 }
491 #endif
492
493         } else if ( sat->sat_syntax == NULL ) {
494                 return SLAP_SCHERR_ATTR_INCOMPLETE;
495         }
496
497         if ( sat->sat_equality_oid ) {
498                 if ( (mr = mr_find(sat->sat_equality_oid)) ) {
499                         sat->sat_equality = mr;
500                 } else {
501                         *err = sat->sat_equality_oid;
502                         return SLAP_SCHERR_MR_NOT_FOUND;
503                 }
504
505         }
506
507         if ( sat->sat_ordering_oid ) {
508                 if ( (mr = mr_find(sat->sat_ordering_oid)) ) {
509                         sat->sat_ordering = mr;
510                 } else {
511                         *err = sat->sat_ordering_oid;
512                         return SLAP_SCHERR_MR_NOT_FOUND;
513                 }
514         }
515
516         if ( sat->sat_substr_oid ) {
517                 if ( (mr = mr_find(sat->sat_substr_oid)) ) {
518                         sat->sat_substr = mr;
519                 } else {
520                         *err = sat->sat_substr_oid;
521                         return SLAP_SCHERR_MR_NOT_FOUND;
522                 }
523         }
524
525         code = at_insert(sat,err);
526         return code;
527 }
528
529 #ifdef LDAP_DEBUG
530 static int
531 at_index_printnode( struct aindexrec *air )
532 {
533
534         printf("%s = %s\n",
535                 air->air_name,
536                 ldap_attributetype2str(&air->air_at->sat_atype) );
537         return( 0 );
538 }
539
540 static void
541 at_index_print( void )
542 {
543         printf("Printing attribute type index:\n");
544         (void) avl_apply( attr_index, (AVL_APPLY) at_index_printnode,
545                 0, -1, AVL_INORDER );
546 }
547 #endif
548
549 #if defined( SLAPD_SCHEMA_DN )
550 int
551 at_schema_info( Entry *e )
552 {
553         struct berval   val;
554         struct berval   *vals[2];
555         AttributeType   *at;
556
557         vals[0] = &val;
558         vals[1] = NULL;
559
560         for ( at = attr_list; at; at = at->sat_next ) {
561                 val.bv_val = ldap_attributetype2str( &at->sat_atype );
562                 if ( val.bv_val ) {
563                         val.bv_len = strlen( val.bv_val );
564                         Debug( LDAP_DEBUG_TRACE, "Merging at [%ld] %s\n",
565                                (long) val.bv_len, val.bv_val, 0 );
566                         attr_merge( e, "attributeTypes", vals );
567                         ldap_memfree( val.bv_val );
568                 } else {
569                         return -1;
570                 }
571         }
572         return 0;
573 }
574 #endif