]> git.sur5r.net Git - openldap/blob - servers/slapd/ad.c
Eliminate second session protocol version field.
[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 static int ad_keystring(
24         struct berval *bv )
25 {
26         ber_len_t i;
27
28         if( !AD_CHAR( bv->bv_val[0] ) ) {
29                 return 1;
30         }
31
32         for( i=1; i<bv->bv_len; i++ ) {
33                 if( !AD_CHAR( bv->bv_val[i] ) ) {
34                         return 1;
35                 }
36         }
37         return 0;
38 }
39
40 int slap_str2ad(
41         const char *str,
42         AttributeDescription **ad,
43         char **text )
44 {
45         struct berval bv;
46         bv.bv_val = (char *) str;
47         bv.bv_len = strlen( str );
48
49         return slap_bv2ad( &bv, ad, text );
50 }
51
52 int slap_bv2ad(
53         struct berval *bv,
54         AttributeDescription **ad,
55         char **text )
56 {
57         int rtn = LDAP_UNDEFINED_TYPE;
58         int i;
59         AttributeDescription desc;
60         char **tokens;
61
62         assert( *ad != NULL );
63         assert( *text != NULL );
64
65         if( bv == NULL || bv->bv_len == 0 ) {
66                 *text = "empty attribute description";
67                 return LDAP_UNDEFINED_TYPE;
68         }
69
70         /* make sure description is IA5 */
71         if( ad_keystring( bv ) ) {
72                 *text = "attribute description contains inappropriate characters";
73                 return LDAP_UNDEFINED_TYPE;
74         }
75
76         tokens = str2charray( bv->bv_val, ";");
77
78         if( tokens == NULL || *tokens == NULL ) {
79                 *text = "no attribute type";
80                 goto done;
81         }
82
83         desc.ad_type = at_find( *tokens );
84
85         if( desc.ad_type == NULL ) {
86                 *text = "attribute type undefined";
87                 goto done;
88         }
89
90         desc.ad_flags = SLAP_DESC_NONE;
91         desc.ad_lang = NULL;
92
93         for( i=1; tokens[i] != NULL; i++ ) {
94                 if( strcasecmp( tokens[i], "binary" ) == 0 ) {
95                         if( desc.ad_flags & SLAP_DESC_BINARY ) {
96                                 *text = "option \"binary\" specified multiple times";
97                                 goto done;
98                         }
99
100                         if(!( desc.ad_type->sat_syntax->ssyn_flags
101                                 & SLAP_SYNTAX_BINARY ))
102                         {
103                                 /* not stored in binary, disallow option */
104                                 *text = "option \"binary\" with type not supported";
105                                 goto done;
106                         }
107
108                         desc.ad_flags |= SLAP_DESC_BINARY;
109
110                 } else if ( strncasecmp( tokens[i], "lang-",
111                         sizeof("lang-")-1 ) == 0 && tokens[i][sizeof("lang-")-1] )
112                 {
113                         if( desc.ad_lang != NULL ) {
114                                 *text = "multiple language tag options specified";
115                                 goto done;
116                         }
117                         desc.ad_lang = tokens[i];
118
119                         /* normalize to all lower case, it's easy */
120                         ldap_pvt_str2lower( desc.ad_lang );
121
122                 } else {
123                         *text = "unrecognized option";
124                         goto done;
125                 }
126         }
127
128         desc.ad_cname = ch_malloc( sizeof( struct berval ) );
129
130         desc.ad_cname->bv_len = strlen( desc.ad_type->sat_cname );
131         if( desc.ad_flags & SLAP_DESC_BINARY ) {
132                 desc.ad_cname->bv_len += sizeof("binary");
133         }
134         if( desc.ad_lang != NULL ) {
135                 desc.ad_cname->bv_len += strlen( desc.ad_lang );
136         }
137
138         desc.ad_cname = ch_malloc( desc.ad_cname->bv_len + 1 );
139
140         strcpy( desc.ad_cname->bv_val, desc.ad_type->sat_cname );
141         strcat( desc.ad_cname->bv_val, ";binary" );
142         if( desc.ad_flags & SLAP_DESC_BINARY ) {
143                 strcat( desc.ad_cname->bv_val, ";binary" );
144         }
145
146         if( desc.ad_lang != NULL ) {
147                 strcat( desc.ad_cname->bv_val, ";" );
148                 strcat( desc.ad_cname->bv_val, desc.ad_lang );
149         }
150
151         *ad = ch_malloc( sizeof( AttributeDescription ) );
152         **ad = desc;
153
154         rtn = LDAP_SUCCESS;
155
156 done:
157         charray_free( tokens );
158         return rtn;
159 }
160
161 void
162 ad_free( AttributeDescription *ad, int freeit )
163 {
164         if( ad == NULL ) return;
165
166         ber_bvfree( ad->ad_cname );
167         free( ad->ad_lang );
168
169         if( freeit ) free( ad );
170 }
171
172 #endif
173