]> git.sur5r.net Git - openldap/blob - servers/slapd/ad.c
Yet another round of SLAPD_SCHEMA_NOT_COMPAT changes...
[openldap] / servers / slapd / ad.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /* ad.c - routines for dealing with attribute descriptions */
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 #ifdef SLAPD_SCHEMA_NOT_COMPAT
22
23 int slap_bv2ad(
24         struct berval *bv,
25         AttributeDescription **ad,
26         char **text )
27 {
28         int rtn = LDAP_UNDEFINED_TYPE;
29         int i;
30         AttributeDescription desc;
31         char **tokens;
32
33         assert( *ad != NULL );
34         assert( *text != NULL );
35
36         if( bv == NULL || bv->bv_len == 0 ) {
37                 *text = "empty attribute description";
38                 return LDAP_UNDEFINED_TYPE;
39         }
40
41         /* make sure description is IA5 */
42         if( IA5StringValidate( NULL, bv ) != 0 ) {
43                 *text = "attribute description contains non-IA5 characters";
44                 return LDAP_UNDEFINED_TYPE;
45         }
46
47         tokens = str2charray( bv->bv_val, ";");
48
49         if( tokens == NULL || *tokens == NULL ) {
50                 *text = "no attribute type";
51                 goto done;
52         }
53
54         desc.ad_type = at_find( *tokens );
55
56         if( desc.ad_type == NULL ) {
57                 *text = "attribute type undefined";
58                 goto done;
59         }
60
61         desc.ad_flags = SLAP_DESC_NONE;
62         desc.ad_lang = NULL;
63
64         for( i=1; tokens[i] != NULL; i++ ) {
65                 if( strcasecmp( tokens[i], "binary" ) == 0 ) {
66                         if( desc.ad_flags & SLAP_DESC_BINARY ) {
67                                 *text = "option \"binary\" specified multiple times";
68                                 goto done;
69                         }
70
71                         if(!( desc.ad_type->sat_syntax->ssyn_flags
72                                 & SLAP_SYNTAX_BINARY ))
73                         {
74                                 /* not stored in binary, disallow option */
75                                 *text = "option \"binary\" with type not supported";
76                                 goto done;
77                         }
78
79                         desc.ad_flags |= SLAP_DESC_BINARY;
80
81                 } else if ( strncasecmp( tokens[i], "lang-",
82                         sizeof("lang-")-1 ) == 0 && tokens[i][sizeof("lang-")-1] )
83                 {
84                         if( desc.ad_lang != NULL ) {
85                                 *text = "multiple language tag options specified";
86                                 goto done;
87                         }
88                         desc.ad_lang = tokens[i];
89
90                         /* normalize to all lower case, it's easy */
91                         ldap_pvt_str2lower( desc.ad_lang );
92
93                 } else {
94                         *text = "unrecognized option";
95                         goto done;
96                 }
97         }
98
99         desc.ad_cname = ch_malloc( sizeof( struct berval ) );
100
101         desc.ad_cname->bv_len = strlen( desc.ad_type->sat_cname );
102         if( desc.ad_flags & SLAP_DESC_BINARY ) {
103                 desc.ad_cname->bv_len += sizeof("binary");
104         }
105         if( desc.ad_lang != NULL ) {
106                 desc.ad_cname->bv_len += strlen( desc.ad_lang );
107         }
108
109         desc.ad_cname = ch_malloc( desc.ad_cname->bv_len + 1 );
110
111         strcpy( desc.ad_cname->bv_val, desc.ad_type->sat_cname );
112         strcat( desc.ad_cname->bv_val, ";binary" );
113         if( desc.ad_flags & SLAP_DESC_BINARY ) {
114                 strcat( desc.ad_cname->bv_val, ";binary" );
115         }
116
117         if( desc.ad_lang != NULL ) {
118                 strcat( desc.ad_cname->bv_val, ";" );
119                 strcat( desc.ad_cname->bv_val, desc.ad_lang );
120         }
121
122         *ad = ch_malloc( sizeof( AttributeDescription ) );
123         **ad = desc;
124
125         rtn = LDAP_SUCCESS;
126
127 done:
128         charray_free( tokens );
129         return rtn;
130 }
131
132 void
133 ad_free( AttributeDescription *ad, int freeit )
134 {
135         if( ad == NULL ) return;
136
137         ber_bvfree( ad->ad_cname );
138         free( ad->ad_lang );
139
140         if( freeit ) free( ad );
141 }
142
143 #endif
144