]> git.sur5r.net Git - openldap/blob - servers/slapd/ad.c
Exit loop after matching command is found in openldap_ldap_init_w_conf
[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 AttributeDescription *ad_dup(
23         AttributeDescription *desc )
24 {
25         AttributeDescription *ad;
26
27         if( desc == NULL ) {
28                 return NULL;
29         }
30
31         ad = (AttributeDescription *) ch_malloc( sizeof(AttributeDescription) );
32
33         *ad = *desc;
34
35         ad->ad_cname = ber_bvdup( ad->ad_cname );
36         ad->ad_lang = ch_strdup( ad->ad_lang );
37
38         return ad;
39 }
40
41 void
42 ad_free( AttributeDescription *ad, int freeit )
43 {
44         if( ad == NULL ) return;
45
46         ber_bvfree( ad->ad_cname );
47         free( ad->ad_lang );
48
49         if( freeit ) free( ad );
50 }
51
52 static int ad_keystring(
53         struct berval *bv )
54 {
55         ber_len_t i;
56
57         if( !AD_CHAR( bv->bv_val[0] ) ) {
58                 return 1;
59         }
60
61         for( i=1; i<bv->bv_len; i++ ) {
62                 if( !AD_CHAR( bv->bv_val[i] ) ) {
63                         return 1;
64                 }
65         }
66         return 0;
67 }
68
69 int slap_str2ad(
70         const char *str,
71         AttributeDescription **ad,
72         char **text )
73 {
74         struct berval bv;
75         bv.bv_val = (char *) str;
76         bv.bv_len = strlen( str );
77
78         return slap_bv2ad( &bv, ad, text );
79 }
80
81 int slap_bv2ad(
82         struct berval *bv,
83         AttributeDescription **ad,
84         char **text )
85 {
86         int rtn = LDAP_UNDEFINED_TYPE;
87         int i;
88         AttributeDescription desc;
89         char **tokens;
90
91         assert( *ad != NULL );
92         assert( *text != NULL );
93
94         if( bv == NULL || bv->bv_len == 0 ) {
95                 *text = "empty attribute description";
96                 return LDAP_UNDEFINED_TYPE;
97         }
98
99         /* make sure description is IA5 */
100         if( ad_keystring( bv ) ) {
101                 *text = "attribute description contains inappropriate characters";
102                 return LDAP_UNDEFINED_TYPE;
103         }
104
105         tokens = str2charray( bv->bv_val, ";");
106
107         if( tokens == NULL || *tokens == NULL ) {
108                 *text = "no attribute type";
109                 goto done;
110         }
111
112         desc.ad_type = at_find( *tokens );
113
114         if( desc.ad_type == NULL ) {
115                 *text = "attribute type undefined";
116                 goto done;
117         }
118
119         desc.ad_flags = SLAP_DESC_NONE;
120         desc.ad_lang = NULL;
121
122         for( i=1; tokens[i] != NULL; i++ ) {
123                 if( strcasecmp( tokens[i], "binary" ) == 0 ) {
124                         if( desc.ad_flags & SLAP_DESC_BINARY ) {
125                                 *text = "option \"binary\" specified multiple times";
126                                 goto done;
127                         }
128
129                         if(!( desc.ad_type->sat_syntax->ssyn_flags
130                                 & SLAP_SYNTAX_BINARY ))
131                         {
132                                 /* not stored in binary, disallow option */
133                                 *text = "option \"binary\" with type not supported";
134                                 goto done;
135                         }
136
137                         desc.ad_flags |= SLAP_DESC_BINARY;
138
139                 } else if ( strncasecmp( tokens[i], "lang-",
140                         sizeof("lang-")-1 ) == 0 && tokens[i][sizeof("lang-")-1] )
141                 {
142                         if( desc.ad_lang != NULL ) {
143                                 *text = "multiple language tag options specified";
144                                 goto done;
145                         }
146                         desc.ad_lang = tokens[i];
147
148                         /* normalize to all lower case, it's easy */
149                         ldap_pvt_str2lower( desc.ad_lang );
150
151                 } else {
152                         *text = "unrecognized option";
153                         goto done;
154                 }
155         }
156
157         desc.ad_cname = ch_malloc( sizeof( struct berval ) );
158
159         desc.ad_cname->bv_len = strlen( desc.ad_type->sat_cname );
160         if( desc.ad_flags & SLAP_DESC_BINARY ) {
161                 desc.ad_cname->bv_len += sizeof("binary");
162         }
163         if( desc.ad_lang != NULL ) {
164                 desc.ad_cname->bv_len += strlen( desc.ad_lang );
165         }
166
167         desc.ad_cname = ch_malloc( desc.ad_cname->bv_len + 1 );
168
169         strcpy( desc.ad_cname->bv_val, desc.ad_type->sat_cname );
170         strcat( desc.ad_cname->bv_val, ";binary" );
171         if( desc.ad_flags & SLAP_DESC_BINARY ) {
172                 strcat( desc.ad_cname->bv_val, ";binary" );
173         }
174
175         if( desc.ad_lang != NULL ) {
176                 strcat( desc.ad_cname->bv_val, ";" );
177                 strcat( desc.ad_cname->bv_val, desc.ad_lang );
178         }
179
180         if( *ad == NULL ) {
181                 *ad = ch_malloc( sizeof( AttributeDescription ) );
182         }
183
184         **ad = desc;
185
186         rtn = LDAP_SUCCESS;
187
188 done:
189         charray_free( tokens );
190         return rtn;
191 }
192
193 int is_ad_subtype(
194         AttributeDescription *sub,
195         AttributeDescription *super
196 )
197 {
198         if( !is_at_subtype( sub->ad_type, super->ad_type ) ) {
199                 return 0;
200         }
201
202         if( super->ad_flags && ( super->ad_flags == sub->ad_flags )) {
203                 return 0;
204         }
205
206         if( super->ad_lang != NULL && ( sub->ad_lang == NULL
207                 || strcasecmp( super->ad_lang, sub->ad_lang )))
208         {
209                 return 0;
210         }
211
212         return 1;
213 }
214
215
216 int ad_inlist(
217         AttributeDescription *desc,
218         char **attrs )
219 {
220         int i;
221         for( i=0; attrs[i] != NULL; i++ ) {
222                 AttributeDescription *ad = NULL;
223                 char *text;
224                 int rc;
225                 
226                 rc = slap_str2ad( attrs[i], &ad, &text );
227
228                 if( rc != LDAP_SUCCESS ) continue;
229
230                 rc = is_ad_subtype( desc, ad );
231
232                 ad_free( ad, 1 );
233
234                 if( rc ) return 1;
235         }
236
237         return 0;
238 }
239
240 #endif
241