]> git.sur5r.net Git - openldap/blob - servers/slapd/at.c
Use #ifdef, not #if
[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   "1.3.6.1.4.1.1466.115.121.1.12"
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_syntax(
177         AttributeType *at,
178         const char *oid )
179 {
180         for( ; at != NULL; at = at->sat_sup ) {
181                 if( at->sat_syntax_oid ) {
182                         return ( strcmp( at->sat_syntax_oid, oid ) == 0 );
183                 }
184         }
185
186         return 0;
187 }
188
189 int is_at_subtype(
190         AttributeType *sub,
191         AttributeType *sup )
192 {
193         for( ; sub != NULL; sub = sub->sat_sup ) {
194                 if( sub == sup ) return 1;
195         }
196
197         return 0;
198 }
199
200 struct aindexrec {
201         char            *air_name;
202         AttributeType   *air_at;
203 };
204
205 static Avlnode  *attr_index = NULL;
206 static AttributeType *attr_list = NULL;
207
208 static int
209 attr_index_cmp(
210     struct aindexrec    *air1,
211     struct aindexrec    *air2
212 )
213 {
214         return (strcasecmp( air1->air_name, air2->air_name ));
215 }
216
217 static int
218 attr_index_name_cmp(
219     const char          *type,
220     struct aindexrec    *air
221 )
222 {
223         return (strcasecmp( type, air->air_name ));
224 }
225
226 AttributeType *
227 at_find(
228     const char          *name
229 )
230 {
231         struct aindexrec        *air;
232         char                    *tmpname;
233
234 #ifndef SLAPD_SCHEMA_NOT_COMPAT
235         /*
236          * The name may actually be an AttributeDescription, i.e. it may
237          * contain options.
238          */
239         /* Treat any attribute type with option as an unknown attribute type */
240         char *p = strchr( name, ';' );
241         if ( p ) {
242                 tmpname = ch_malloc( p-name+1 );
243                 strncpy( tmpname, name, p-name );
244                 tmpname[p-name] = '\0';
245         } else
246 #endif
247         {
248                 tmpname = (char *)name;
249         }
250
251         if ( (air = (struct aindexrec *) avl_find( attr_index, tmpname,
252             (AVL_CMP) attr_index_name_cmp )) != NULL ) {
253                 if ( tmpname != name )
254                         ldap_memfree( tmpname );
255                 return( air->air_at );
256         }
257
258         if ( tmpname != name )
259                 ldap_memfree( tmpname );
260         return( NULL );
261 }
262
263 int
264 at_append_to_list(
265     AttributeType       *sat,
266     AttributeType       ***listp
267 )
268 {
269         AttributeType   **list;
270         AttributeType   **list1;
271         int             size;
272
273         list = *listp;
274         if ( !list ) {
275                 size = 2;
276                 list = calloc(size, sizeof(AttributeType *));
277                 if ( !list ) {
278                         return -1;
279                 }
280         } else {
281                 size = 0;
282                 list1 = *listp;
283                 while ( *list1 ) {
284                         size++;
285                         list1++;
286                 }
287                 size += 2;
288                 list1 = realloc(list, size*sizeof(AttributeType *));
289                 if ( !list1 ) {
290                         return -1;
291                 }
292                 list = list1;
293         }
294         list[size-2] = sat;
295         list[size-1] = NULL;
296         *listp = list;
297         return 0;
298 }
299
300 int
301 at_delete_from_list(
302     int                 pos,
303     AttributeType       ***listp
304 )
305 {
306         AttributeType   **list;
307         AttributeType   **list1;
308         int             i;
309         int             j;
310
311         if ( pos < 0 ) {
312                 return -2;
313         }
314         list = *listp;
315         for ( i=0; list[i]; i++ )
316                 ;
317         if ( pos >= i ) {
318                 return -2;
319         }
320         for ( i=pos, j=pos+1; list[j]; i++, j++ ) {
321                 list[i] = list[j];
322         }
323         list[i] = NULL;
324         /* Tell the runtime this can be shrinked */
325         list1 = realloc(list, (i+1)*sizeof(AttributeType **));
326         if ( !list1 ) {
327                 return -1;
328         }
329         *listp = list1;
330         return 0;
331 }
332
333 int
334 at_find_in_list(
335     AttributeType       *sat,
336     AttributeType       **list
337 )
338 {
339         int     i;
340
341         if ( !list ) {
342                 return -1;
343         }
344         for ( i=0; list[i]; i++ ) {
345                 if ( sat == list[i] ) {
346                         return i;
347                 }
348         }
349         return -1;
350 }
351
352 static int
353 at_insert(
354     AttributeType       *sat,
355     const char          **err
356 )
357 {
358         AttributeType           **atp;
359         struct aindexrec        *air;
360         char                    **names;
361
362         atp = &attr_list;
363         while ( *atp != NULL ) {
364                 atp = &(*atp)->sat_next;
365         }
366         *atp = sat;
367
368         if ( sat->sat_oid ) {
369                 air = (struct aindexrec *)
370                         ch_calloc( 1, sizeof(struct aindexrec) );
371                 air->air_name = sat->sat_oid;
372                 air->air_at = sat;
373                 if ( avl_insert( &attr_index, (caddr_t) air,
374                                  (AVL_CMP) attr_index_cmp,
375                                  (AVL_DUP) avl_dup_error ) ) {
376                         *err = sat->sat_oid;
377                         ldap_memfree(air);
378                         return SLAP_SCHERR_DUP_ATTR;
379                 }
380                 /* FIX: temporal consistency check */
381                 at_find(air->air_name);
382         }
383
384         if ( (names = sat->sat_names) ) {
385                 while ( *names ) {
386                         air = (struct aindexrec *)
387                                 ch_calloc( 1, sizeof(struct aindexrec) );
388                         air->air_name = ch_strdup(*names);
389                         air->air_at = sat;
390                         if ( avl_insert( &attr_index, (caddr_t) air,
391                                          (AVL_CMP) attr_index_cmp,
392                                          (AVL_DUP) avl_dup_error ) ) {
393                                 *err = *names;
394                                 ldap_memfree(air->air_name);
395                                 ldap_memfree(air);
396                                 return SLAP_SCHERR_DUP_ATTR;
397                         }
398                         /* FIX: temporal consistency check */
399                         at_find(air->air_name);
400                         names++;
401                 }
402         }
403
404         return 0;
405 }
406
407 int
408 at_add(
409     LDAP_ATTRIBUTE_TYPE *at,
410     const char          **err
411 )
412 {
413         AttributeType   *sat;
414         MatchingRule    *mr;
415         Syntax          *syn;
416         int             code;
417         char                    *cname;
418
419         if ( at->at_names && at->at_names[0] ) {
420                 cname = at->at_names[0];
421         } else if ( at->at_oid ) {
422                 cname = at->at_oid;
423         } else {
424                 cname = "";
425                 return SLAP_SCHERR_ATTR_INCOMPLETE;
426         }
427         sat = (AttributeType *) ch_calloc( 1, sizeof(AttributeType) );
428         memcpy( &sat->sat_atype, at, sizeof(LDAP_ATTRIBUTE_TYPE));
429
430 #ifdef SLAPD_SCHEMA_NOT_COMPAT
431         sat->sat_cname = cname;
432 #endif
433
434         if ( at->at_sup_oid ) {
435                 AttributeType *supsat = at_find(at->at_sup_oid);
436
437                 if ( (supsat == NULL ) ) {
438                         *err = at->at_sup_oid;
439                         return SLAP_SCHERR_ATTR_NOT_FOUND;
440                 }
441
442                 sat->sat_sup = supsat;
443
444                 if ( at_append_to_list(sat, &supsat->sat_subtypes) ) {
445                         *err = cname;
446                         return SLAP_SCHERR_OUTOFMEM;
447                 }
448         }
449
450         /*
451          * Inherit definitions from superiors.  We only check the
452          * direct superior since that one has already inherited from
453          * its own superiorss
454          */
455         if ( sat->sat_sup ) {
456                 sat->sat_syntax = sat->sat_sup->sat_syntax;
457 #ifndef SLAPD_SCHEMA_NOT_COMPAT
458                 sat->sat_syntax_compat = sat->sat_sup->sat_syntax_compat;
459 #endif
460                 sat->sat_equality = sat->sat_sup->sat_equality;
461                 sat->sat_ordering = sat->sat_sup->sat_ordering;
462                 sat->sat_substr = sat->sat_sup->sat_substr;
463         }
464
465         if ( at->at_syntax_oid ) {
466                 if ( (syn = syn_find(sat->sat_syntax_oid)) ) {
467                         sat->sat_syntax = syn;
468                 } else {
469                         *err = sat->sat_syntax_oid;
470                         return SLAP_SCHERR_SYN_NOT_FOUND;
471                 }
472
473 #ifndef SLAPD_SCHEMA_NOT_COMPAT
474                 if ( !strcmp(at->at_syntax_oid, SYNTAX_DS_OID) ) {
475                         if ( at->at_equality_oid && (
476                                 !strcmp(at->at_equality_oid, SYNTAX_DSCE_OID) ) )
477                         {
478                                 sat->sat_syntax_compat = SYNTAX_CES;
479                         } else {
480                                 sat->sat_syntax_compat = SYNTAX_CIS;
481                         }
482
483                 } else if ( !strcmp(at->at_syntax_oid, SYNTAX_IA5_OID) ) {
484                         if ( at->at_equality_oid && (
485                                 !strcmp(at->at_equality_oid, SYNTAX_IA5CE_OID) ) )
486                         {
487                                 sat->sat_syntax_compat = SYNTAX_CES;
488                         } else {
489                                 sat->sat_syntax_compat = SYNTAX_CIS;
490                         }
491
492                 } else if ( !strcmp(at->at_syntax_oid, SYNTAX_DN_OID ) ) {
493                         sat->sat_syntax_compat = SYNTAX_CIS | SYNTAX_DN;
494
495                 } else if ( !strcmp(at->at_syntax_oid, SYNTAX_TEL_OID ) ) {
496                         sat->sat_syntax_compat = SYNTAX_CIS | SYNTAX_TEL;
497
498                 } else if ( !strcmp(at->at_syntax_oid, SYNTAX_BIN_OID ) ) {
499                         sat->sat_syntax_compat = SYNTAX_BIN;
500
501                 } else {
502                         sat->sat_syntax_compat = DEFAULT_SYNTAX;
503                 }
504 #endif
505
506         } else if ( sat->sat_syntax == NULL ) {
507                 return SLAP_SCHERR_ATTR_INCOMPLETE;
508         }
509
510         if ( sat->sat_equality_oid ) {
511                 if ( (mr = mr_find(sat->sat_equality_oid)) ) {
512                         sat->sat_equality = mr;
513                 } else {
514                         *err = sat->sat_equality_oid;
515                         return SLAP_SCHERR_MR_NOT_FOUND;
516                 }
517
518         }
519
520         if ( sat->sat_ordering_oid ) {
521                 if ( (mr = mr_find(sat->sat_ordering_oid)) ) {
522                         sat->sat_ordering = mr;
523                 } else {
524                         *err = sat->sat_ordering_oid;
525                         return SLAP_SCHERR_MR_NOT_FOUND;
526                 }
527         }
528
529         if ( sat->sat_substr_oid ) {
530                 if ( (mr = mr_find(sat->sat_substr_oid)) ) {
531                         sat->sat_substr = mr;
532                 } else {
533                         *err = sat->sat_substr_oid;
534                         return SLAP_SCHERR_MR_NOT_FOUND;
535                 }
536         }
537
538         code = at_insert(sat,err);
539         return code;
540 }
541
542 #ifdef LDAP_DEBUG
543 static int
544 at_index_printnode( struct aindexrec *air )
545 {
546
547         printf("%s = %s\n",
548                 air->air_name,
549                 ldap_attributetype2str(&air->air_at->sat_atype) );
550         return( 0 );
551 }
552
553 static void
554 at_index_print( void )
555 {
556         printf("Printing attribute type index:\n");
557         (void) avl_apply( attr_index, (AVL_APPLY) at_index_printnode,
558                 0, -1, AVL_INORDER );
559 }
560 #endif
561
562 #if defined( SLAPD_SCHEMA_DN )
563 int
564 at_schema_info( Entry *e )
565 {
566         struct berval   val;
567         struct berval   *vals[2];
568         AttributeType   *at;
569
570 #ifdef SLAPD_SCHEMA_NOT_COMPAT
571         AttributeDescription *ad_attributeTypes = slap_schema.si_ad_attributeTypes;
572 #else
573         char *ad_attributeTypes = "attributeTypes";
574 #endif
575
576         vals[0] = &val;
577         vals[1] = NULL;
578
579         for ( at = attr_list; at; at = at->sat_next ) {
580                 val.bv_val = ldap_attributetype2str( &at->sat_atype );
581                 if ( val.bv_val == NULL ) {
582                         return -1;
583                 }
584                 val.bv_len = strlen( val.bv_val );
585 #if 0
586                 Debug( LDAP_DEBUG_TRACE, "Merging at [%ld] %s\n",
587                        (long) val.bv_len, val.bv_val, 0 );
588 #endif
589                 attr_merge( e, ad_attributeTypes, vals );
590                 ldap_memfree( val.bv_val );
591         }
592         return 0;
593 }
594 #endif