]> git.sur5r.net Git - openldap/blob - servers/slapd/root_dse.c
Memory context tweaks for Bind
[openldap] / servers / slapd / root_dse.c
1 /* $OpenLDAP$ */
2 /* root_dse.c - Provides the ROOT DSA-Specific Entry
3  *
4  * Copyright 1999-2003 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms are permitted only
8  * as authorized by the OpenLDAP Public License.  A copy of this
9  * license is available at http://www.OpenLDAP.org/license.html or
10  * in file LICENSE in the top-level directory of the distribution.
11  */
12
13 #include "portable.h"
14
15 #include <stdio.h>
16
17 #include <ac/string.h>
18
19 #include "slap.h"
20 #include <ldif.h>
21 #include "lber_pvt.h"
22
23 #ifdef LDAP_SLAPI
24 #include "slapi.h"
25 #endif
26
27 static struct berval supportedFeatures[] = {
28         BER_BVC(LDAP_FEATURE_ALL_OPERATIONAL_ATTRS), /* all Operational Attributes ("+") */
29         BER_BVC(LDAP_FEATURE_OBJECTCLASS_ATTRS), /* OCs in Attributes List */
30         BER_BVC(LDAP_FEATURE_ABSOLUTE_FILTERS), /* (&) and (|) search filters */
31         BER_BVC(LDAP_FEATURE_LANGUAGE_TAG_OPTIONS), /* Language Tag Options */
32         BER_BVC(LDAP_FEATURE_LANGUAGE_RANGE_OPTIONS), /* Language Range Options */
33         {0,NULL}
34 };
35
36 static Entry    *usr_attr = NULL;
37
38 int
39 root_dse_info(
40         Connection *conn,
41         Entry **entry,
42         const char **text )
43 {
44         Entry           *e;
45         struct berval vals[2], *bv;
46         struct berval nvals[2];
47         int             i, j;
48         char ** supportedSASLMechanisms;
49
50         AttributeDescription *ad_structuralObjectClass
51                 = slap_schema.si_ad_structuralObjectClass;
52         AttributeDescription *ad_objectClass
53                 = slap_schema.si_ad_objectClass;
54         AttributeDescription *ad_namingContexts
55                 = slap_schema.si_ad_namingContexts;
56         AttributeDescription *ad_supportedExtension
57                 = slap_schema.si_ad_supportedExtension;
58         AttributeDescription *ad_supportedLDAPVersion
59                 = slap_schema.si_ad_supportedLDAPVersion;
60         AttributeDescription *ad_supportedSASLMechanisms
61                 = slap_schema.si_ad_supportedSASLMechanisms;
62         AttributeDescription *ad_supportedFeatures
63                 = slap_schema.si_ad_supportedFeatures;
64         AttributeDescription *ad_monitorContext
65                 = slap_schema.si_ad_monitorContext;
66         AttributeDescription *ad_ref
67                 = slap_schema.si_ad_ref;
68
69         vals[1].bv_val = NULL;
70         nvals[1].bv_val = NULL;
71
72         e = (Entry *) SLAP_CALLOC( 1, sizeof(Entry) );
73
74         if( e == NULL ) {
75 #ifdef NEW_LOGGING
76                 LDAP_LOG( OPERATION, ERR,
77                         "root_dse_info: SLAP_CALLOC failed", 0, 0, 0 );
78 #else
79                 Debug( LDAP_DEBUG_ANY,
80                         "root_dse_info: SLAP_CALLOC failed", 0, 0, 0 );
81 #endif
82                 return LDAP_OTHER;
83         }
84
85         e->e_attrs = NULL;
86         e->e_name.bv_val = ch_strdup( LDAP_ROOT_DSE );
87         e->e_name.bv_len = sizeof( LDAP_ROOT_DSE )-1;
88         e->e_nname.bv_val = ch_strdup( LDAP_ROOT_DSE );
89         e->e_nname.bv_len = sizeof( LDAP_ROOT_DSE )-1;
90
91         /* the DN is an empty string so no pretty/normalization is needed */
92         assert( !e->e_name.bv_len );
93         assert( !e->e_nname.bv_len );
94
95         e->e_private = NULL;
96
97         vals[0].bv_val = "top";
98         vals[0].bv_len = sizeof("top")-1;
99         if( attr_merge( e, ad_objectClass, vals, NULL ) )
100         {
101                 return LDAP_OTHER;
102         }
103
104         vals[0].bv_val = "OpenLDAProotDSE";
105         vals[0].bv_len = sizeof("OpenLDAProotDSE")-1;
106         if( attr_merge( e, ad_objectClass, vals, NULL ) )
107                 return LDAP_OTHER;
108         if( attr_merge( e, ad_structuralObjectClass, vals, NULL ) )
109                 return LDAP_OTHER;
110
111         for ( i = 0; i < nbackends; i++ ) {
112                 if ( backends[i].be_flags & SLAP_BFLAG_MONITOR ) {
113                         vals[0] = backends[i].be_suffix[0];
114                         nvals[0] = backends[i].be_nsuffix[0];
115                         if( attr_merge( e, ad_monitorContext, vals, nvals ) )
116                         {
117                                 return LDAP_OTHER;
118                         }
119                         continue;
120                 }
121                 if ( SLAP_GLUE_SUBORDINATE( &backends[i] ) ) {
122                         continue;
123                 }
124                 for ( j = 0; backends[i].be_suffix[j].bv_val != NULL; j++ ) {
125                         vals[0] = backends[i].be_suffix[j];
126                         nvals[0] = backends[i].be_nsuffix[0];
127                         if( attr_merge( e, ad_namingContexts, vals, nvals ) )
128                         {
129                                 return LDAP_OTHER;
130                         }
131                 }
132         }
133
134         /* altServer unsupported */
135
136         /* supportedControl */
137         if ( controls_root_dse_info( e ) != 0 ) {
138                 return LDAP_OTHER;
139         }
140
141         /* supportedExtension */
142         for ( i=0; (bv = get_supported_extop(i)) != NULL; i++ ) {
143                 vals[0] = *bv;
144                 if( attr_merge( e, ad_supportedExtension, vals, NULL ) )
145                 {
146                         return LDAP_OTHER;
147                 }
148         }
149
150 #ifdef LDAP_SLAPI
151         /* netscape supportedExtension */
152         for ( i = 0; (bv = ns_get_supported_extop(i)) != NULL; i++ ) {
153                 vals[0] = *bv;
154                 if( attr_merge( e, ad_supportedExtension, vals, NULL ))
155                 {
156                         return LDAP_OTHER;
157                 }
158         }
159 #endif /* LDAP_SLAPI */
160
161         /* supportedFeatures */
162         if( attr_merge( e, ad_supportedFeatures,
163                 supportedFeatures, NULL ) )
164         {
165                 return LDAP_OTHER;
166         }
167
168         /* supportedLDAPVersion */
169         for ( i=LDAP_VERSION_MIN; i<=LDAP_VERSION_MAX; i++ ) {
170                 char buf[BUFSIZ];
171                 if (!( global_allows & SLAP_ALLOW_BIND_V2 ) &&
172                         ( i < LDAP_VERSION3 ) )
173                 {
174                         /* version 2 and lower are disallowed */
175                         continue;
176                 }
177                 snprintf(buf, sizeof buf, "%d", i);
178                 vals[0].bv_val = buf;
179                 vals[0].bv_len = strlen( vals[0].bv_val );
180                 if( attr_merge( e, ad_supportedLDAPVersion, vals, NULL ) )
181                 {
182                         return LDAP_OTHER;
183                 }
184         }
185
186         /* supportedSASLMechanism */
187         supportedSASLMechanisms = slap_sasl_mechs( conn );
188
189         if( supportedSASLMechanisms != NULL ) {
190                 for ( i=0; supportedSASLMechanisms[i] != NULL; i++ ) {
191                         vals[0].bv_val = supportedSASLMechanisms[i];
192                         vals[0].bv_len = strlen( vals[0].bv_val );
193                         if( attr_merge( e, ad_supportedSASLMechanisms, vals, NULL ) )
194                         {
195                                 return LDAP_OTHER;
196                         }
197                 }
198                 ldap_charray_free( supportedSASLMechanisms );
199         }
200
201         if ( default_referral != NULL ) {
202                 if( attr_merge( e, ad_ref, default_referral, NULL /* FIXME */ ) )
203                 {
204                         return LDAP_OTHER;
205                 }
206         }
207
208         if( usr_attr != NULL) {
209                 Attribute *a;
210                 for( a = usr_attr->e_attrs; a != NULL; a = a->a_next ) {
211                         if( attr_merge( e, a->a_desc, a->a_vals,
212                          (a->a_nvals == a->a_vals) ? NULL : a->a_nvals ) )
213                         {
214                                 return LDAP_OTHER;
215                         }
216                 }
217         }
218
219         *entry = e;
220         return LDAP_SUCCESS;
221 }
222
223 /*
224  * Read the entries specified in fname and merge the attributes
225  * to the user defined rootDSE. Note thaat if we find any errors
226  * what so ever, we will discard the entire entries, print an
227  * error message and return.
228  */
229 int read_root_dse_file( const char *fname )
230 {
231         FILE    *fp;
232         int rc = 0, lineno = 0, lmax = 0;
233         char    *buf = NULL;
234
235         if ( (fp = fopen( fname, "r" )) == NULL ) {
236                 Debug( LDAP_DEBUG_ANY,
237                         "could not open rootdse attr file \"%s\" - absolute path?\n",
238                         fname, 0, 0 );
239                 perror( fname );
240                 return EXIT_FAILURE;
241         }
242
243         usr_attr = (Entry *) SLAP_CALLOC( 1, sizeof(Entry) );
244         if( usr_attr == NULL ) {
245 #ifdef NEW_LOGGING
246                 LDAP_LOG( OPERATION, ERR,
247                         "read_root_dse_file: SLAP_CALLOC failed", 0, 0, 0 );
248 #else
249                 Debug( LDAP_DEBUG_ANY,
250                         "read_root_dse_file: SLAP_CALLOC failed", 0, 0, 0 );
251 #endif
252                 fclose( fp );
253                 return LDAP_OTHER;
254         }
255         usr_attr->e_attrs = NULL;
256
257         while( ldif_read_record( fp, &lineno, &buf, &lmax ) ) {
258                 Entry *e = str2entry( buf );
259                 Attribute *a;
260
261                 if( e == NULL ) {
262                         fprintf( stderr, "root_dse: could not parse entry (line=%d)\n",
263                                 lineno );
264                         rc = EXIT_FAILURE;
265                         break;
266                 }
267
268                 /* make sure the DN is the empty DN */
269                 if( e->e_nname.bv_len ) {
270                         fprintf( stderr,
271                                 "root_dse: invalid rootDSE - dn=\"%s\" (line=%d)\n",
272                                 e->e_dn, lineno );
273                         entry_free( e );
274                         rc = EXIT_FAILURE;
275                         break;
276                 }
277
278                 /*
279                  * we found a valid entry, so walk thru all the attributes in the
280                  * entry, and add each attribute type and description to the
281                  * usr_attr entry
282                  */
283
284                 for(a = e->e_attrs; a != NULL; a = a->a_next) {
285                         if( attr_merge( usr_attr, a->a_desc, a->a_vals,
286                         (a->a_nvals == a->a_vals) ? NULL : a->a_nvals ) )
287                         {
288                                 rc = LDAP_OTHER;
289                                 break;
290                         }
291                 }
292
293                 entry_free( e );
294                 if (rc) break;
295         }
296
297         if (rc) {
298                 entry_free( usr_attr );
299                 usr_attr = NULL;
300         }
301
302         ch_free( buf );
303
304         fclose( fp );
305
306         Debug(LDAP_DEBUG_CONFIG, "rootDSE file %s read.\n", fname, 0, 0);
307         return rc;
308 }