]> git.sur5r.net Git - openldap/blob - servers/slapd/root_dse.c
fix previous commit (check for co_type==Cft_Overlay before calling
[openldap] / servers / slapd / root_dse.c
1 /* root_dse.c - Provides the Root DSA-Specific Entry */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1999-2005 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20
21 #include <ac/string.h>
22
23 #include "slap.h"
24 #include <ldif.h>
25 #include "lber_pvt.h"
26
27 #ifdef LDAP_SLAPI
28 #include "slapi/slapi.h"
29 #endif
30
31 static struct berval supportedFeatures[] = {
32         BER_BVC(LDAP_FEATURE_ALL_OP_ATTRS),                     /* All Op Attrs (+) */
33         BER_BVC(LDAP_FEATURE_OBJECTCLASS_ATTRS),        /* OCs in Attrs List (@class) */
34         BER_BVC(LDAP_FEATURE_ABSOLUTE_FILTERS),         /* (&) and (|) search filters */
35         BER_BVC(LDAP_FEATURE_LANGUAGE_TAG_OPTIONS), /* Language Tag Options */
36         BER_BVC(LDAP_FEATURE_LANGUAGE_RANGE_OPTIONS),/* Language Range Options */
37 #ifdef LDAP_FEATURE_SUBORDINATE_SCOPE
38         BER_BVC(LDAP_FEATURE_SUBORDINATE_SCOPE),        /* "children" search scope */
39 #endif
40 #ifdef LDAP_FEATURE_MODIFY_INCREMENT
41         BER_BVC(LDAP_FEATURE_MODIFY_INCREMENT),         /* Modify/increment */
42 #endif
43         {0,NULL}
44 };
45
46 static Entry    *usr_attr = NULL;
47
48 int
49 root_dse_info(
50         Connection *conn,
51         Entry **entry,
52         const char **text )
53 {
54         Entry           *e;
55         struct berval val, *bv;
56         struct berval nval;
57         int             i, j;
58         char ** supportedSASLMechanisms;
59         BackendDB *be;
60
61         AttributeDescription *ad_structuralObjectClass
62                 = slap_schema.si_ad_structuralObjectClass;
63         AttributeDescription *ad_objectClass
64                 = slap_schema.si_ad_objectClass;
65         AttributeDescription *ad_namingContexts
66                 = slap_schema.si_ad_namingContexts;
67         AttributeDescription *ad_supportedExtension
68                 = slap_schema.si_ad_supportedExtension;
69         AttributeDescription *ad_supportedLDAPVersion
70                 = slap_schema.si_ad_supportedLDAPVersion;
71         AttributeDescription *ad_supportedSASLMechanisms
72                 = slap_schema.si_ad_supportedSASLMechanisms;
73         AttributeDescription *ad_supportedFeatures
74                 = slap_schema.si_ad_supportedFeatures;
75         AttributeDescription *ad_monitorContext
76                 = slap_schema.si_ad_monitorContext;
77         AttributeDescription *ad_configContext
78                 = slap_schema.si_ad_configContext;
79         AttributeDescription *ad_ref
80                 = slap_schema.si_ad_ref;
81
82         e = (Entry *) SLAP_CALLOC( 1, sizeof(Entry) );
83
84         if( e == NULL ) {
85                 Debug( LDAP_DEBUG_ANY,
86                         "root_dse_info: SLAP_CALLOC failed", 0, 0, 0 );
87                 return LDAP_OTHER;
88         }
89
90         e->e_attrs = NULL;
91         e->e_name.bv_val = ch_strdup( LDAP_ROOT_DSE );
92         e->e_name.bv_len = sizeof( LDAP_ROOT_DSE )-1;
93         e->e_nname.bv_val = ch_strdup( LDAP_ROOT_DSE );
94         e->e_nname.bv_len = sizeof( LDAP_ROOT_DSE )-1;
95
96         /* the DN is an empty string so no pretty/normalization is needed */
97         assert( !e->e_name.bv_len );
98         assert( !e->e_nname.bv_len );
99
100         e->e_private = NULL;
101
102         BER_BVSTR( &val, "top" );
103         if( attr_merge_one( e, ad_objectClass, &val, NULL ) ) {
104                 return LDAP_OTHER;
105         }
106
107         BER_BVSTR( &val, "OpenLDAProotDSE" );
108         if( attr_merge_one( e, ad_objectClass, &val, NULL ) ) {
109                 return LDAP_OTHER;
110         }
111         if( attr_merge_one( e, ad_structuralObjectClass, &val, NULL ) ) {
112                 return LDAP_OTHER;
113         }
114
115         LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
116                 if ( be->be_suffix == NULL
117                                 || be->be_nsuffix == NULL ) {
118                         /* no suffix! */
119                         continue;
120                 }
121                 if ( SLAP_MONITOR( be )) {
122                         if( attr_merge_one( e, ad_monitorContext,
123                                         &be->be_suffix[0],
124                                         &be->be_nsuffix[0] ) )
125                         {
126                                 return LDAP_OTHER;
127                         }
128                         continue;
129                 }
130                 if ( SLAP_CONFIG( be )) {
131                         if( attr_merge_one( e, ad_configContext,
132                                         &be->be_suffix[0],
133                                         & be->be_nsuffix[0] ) )
134                         {
135                                 return LDAP_OTHER;
136                         }
137                         continue;
138                 }
139                 if ( SLAP_GLUE_SUBORDINATE( be ) && !SLAP_GLUE_ADVERTISE( be ) ) {
140                         continue;
141                 }
142                 for ( j = 0; be->be_suffix[j].bv_val != NULL; j++ ) {
143                         if( attr_merge_one( e, ad_namingContexts,
144                                         &be->be_suffix[j],
145                                         &be->be_nsuffix[0] ) )
146                         {
147                                 return LDAP_OTHER;
148                         }
149                 }
150         }
151
152         /* altServer unsupported */
153
154         /* supportedControl */
155         if ( controls_root_dse_info( e ) != 0 ) {
156                 return LDAP_OTHER;
157         }
158
159         /* supportedExtension */
160         if ( exop_root_dse_info( e ) != 0 ) {
161                 return LDAP_OTHER;
162         }
163
164 #ifdef LDAP_SLAPI
165         /* netscape supportedExtension */
166         for ( i = 0; (bv = slapi_int_get_supported_extop(i)) != NULL; i++ ) {
167                 if( attr_merge_one( e, ad_supportedExtension, bv, NULL ) ) {
168                         return LDAP_OTHER;
169                 }
170         }
171 #endif /* LDAP_SLAPI */
172
173         /* supportedFeatures */
174         if( attr_merge( e, ad_supportedFeatures, supportedFeatures, NULL ) ) {
175                 return LDAP_OTHER;
176         }
177
178         /* supportedLDAPVersion */
179                 /* don't publish version 2 as we don't really support it
180                  * (even when configured to accept version 2 Bind requests)
181                  * and the value would never be used by true LDAPv2 (or LDAPv3)
182                  * clients.
183                  */
184         for ( i=LDAP_VERSION3; i<=LDAP_VERSION_MAX; i++ ) {
185                 char buf[BUFSIZ];
186                 snprintf(buf, sizeof buf, "%d", i);
187                 val.bv_val = buf;
188                 val.bv_len = strlen( val.bv_val );
189                 if( attr_merge_one( e, ad_supportedLDAPVersion, &val, NULL ) ) {
190                         return LDAP_OTHER;
191                 }
192         }
193
194         /* supportedSASLMechanism */
195         supportedSASLMechanisms = slap_sasl_mechs( conn );
196
197         if( supportedSASLMechanisms != NULL ) {
198                 for ( i=0; supportedSASLMechanisms[i] != NULL; i++ ) {
199                         val.bv_val = supportedSASLMechanisms[i];
200                         val.bv_len = strlen( val.bv_val );
201                         if( attr_merge_one( e, ad_supportedSASLMechanisms, &val, NULL ) ) {
202                                 return LDAP_OTHER;
203                         }
204                 }
205                 ldap_charray_free( supportedSASLMechanisms );
206         }
207
208         if ( default_referral != NULL ) {
209                 if( attr_merge( e, ad_ref, default_referral, NULL /* FIXME */ ) ) {
210                         return LDAP_OTHER;
211                 }
212         }
213
214         if( usr_attr != NULL) {
215                 Attribute *a;
216                 for( a = usr_attr->e_attrs; a != NULL; a = a->a_next ) {
217                         if( attr_merge( e, a->a_desc, a->a_vals,
218                                 (a->a_nvals == a->a_vals) ? NULL : a->a_nvals ) )
219                         {
220                                 return LDAP_OTHER;
221                         }
222                 }
223         }
224
225         *entry = e;
226         return LDAP_SUCCESS;
227 }
228
229 /*
230  * Read the entries specified in fname and merge the attributes
231  * to the user defined rootDSE. Note thaat if we find any errors
232  * what so ever, we will discard the entire entries, print an
233  * error message and return.
234  */
235 int read_root_dse_file( const char *fname )
236 {
237         struct LDIFFP   *fp;
238         int rc = 0, lineno = 0, lmax = 0;
239         char    *buf = NULL;
240
241         if ( (fp = ldif_open( fname, "r" )) == NULL ) {
242                 Debug( LDAP_DEBUG_ANY,
243                         "could not open rootdse attr file \"%s\" - absolute path?\n",
244                         fname, 0, 0 );
245                 perror( fname );
246                 return EXIT_FAILURE;
247         }
248
249         usr_attr = (Entry *) SLAP_CALLOC( 1, sizeof(Entry) );
250         if( usr_attr == NULL ) {
251                 Debug( LDAP_DEBUG_ANY,
252                         "read_root_dse_file: SLAP_CALLOC failed", 0, 0, 0 );
253                 ldif_close( fp );
254                 return LDAP_OTHER;
255         }
256         usr_attr->e_attrs = NULL;
257
258         while( ldif_read_record( fp, &lineno, &buf, &lmax ) ) {
259                 Entry *e = str2entry( buf );
260                 Attribute *a;
261
262                 if( e == NULL ) {
263                         fprintf( stderr, "root_dse: could not parse entry (line=%d)\n",
264                                 lineno );
265                         rc = EXIT_FAILURE;
266                         break;
267                 }
268
269                 /* make sure the DN is the empty DN */
270                 if( e->e_nname.bv_len ) {
271                         fprintf( stderr,
272                                 "root_dse: invalid rootDSE - dn=\"%s\" (line=%d)\n",
273                                 e->e_dn, lineno );
274                         entry_free( e );
275                         rc = EXIT_FAILURE;
276                         break;
277                 }
278
279                 /*
280                  * we found a valid entry, so walk thru all the attributes in the
281                  * entry, and add each attribute type and description to the
282                  * usr_attr entry
283                  */
284
285                 for(a = e->e_attrs; a != NULL; a = a->a_next) {
286                         if( attr_merge( usr_attr, a->a_desc, a->a_vals,
287                                 (a->a_nvals == a->a_vals) ? NULL : a->a_nvals ) )
288                         {
289                                 rc = LDAP_OTHER;
290                                 break;
291                         }
292                 }
293
294                 entry_free( e );
295                 if (rc) break;
296         }
297
298         if (rc) {
299                 entry_free( usr_attr );
300                 usr_attr = NULL;
301         }
302
303         ch_free( buf );
304
305         ldif_close( fp );
306
307         Debug(LDAP_DEBUG_CONFIG, "rootDSE file %s read.\n", fname, 0, 0);
308         return rc;
309 }
310
311 int
312 slap_discover_feature(
313         const char      *uri,
314         int             version,
315         const char      *attr,
316         const char      *val )
317 {
318         LDAP            *ld;
319         LDAPMessage     *res = NULL, *entry;
320         int             rc, i;
321         struct berval   cred = BER_BVC( "" ),
322                         bv_val,
323                         **values = NULL;
324         char            *attrs[ 2 ] = { NULL, NULL };
325
326         ber_str2bv( val, 0, 0, &bv_val );
327         attrs[ 0 ] = (char *) attr;
328
329         rc = ldap_initialize( &ld, uri );
330         if ( rc != LDAP_SUCCESS ) {
331                 return rc;
332         }
333
334         rc = ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
335         if ( rc != LDAP_SUCCESS ) {
336                 goto done;
337         }
338
339         rc = ldap_sasl_bind_s( ld, "", LDAP_SASL_SIMPLE,
340                         &cred, NULL, NULL, NULL );
341         if ( rc != LDAP_SUCCESS ) {
342                 goto done;
343         }
344
345         rc = ldap_search_ext_s( ld, "", LDAP_SCOPE_BASE, "(objectClass=*)",
346                         attrs, 0, NULL, NULL, NULL, 0, &res );
347         if ( rc != LDAP_SUCCESS ) {
348                 goto done;
349         }
350
351         entry = ldap_first_entry( ld, res );
352         if ( entry == NULL ) {
353                 goto done;
354         }
355
356         values = ldap_get_values_len( ld, entry, attrs[ 0 ] );
357         if ( values == NULL ) {
358                 rc = LDAP_NO_SUCH_ATTRIBUTE;
359                 goto done;
360         }
361
362         for ( i = 0; values[ i ] != NULL; i++ ) {
363                 if ( bvmatch( &bv_val, values[ i ] ) ) {
364                         rc = LDAP_COMPARE_TRUE;
365                         goto done;
366                 }
367         }
368
369         rc = LDAP_COMPARE_FALSE;
370
371 done:;
372         if ( values != NULL ) {
373                 ldap_value_free_len( values );
374         }
375
376         if ( res != NULL ) {
377                 ldap_msgfree( res );
378         }
379
380         ldap_unbind_ext( ld, NULL, NULL );
381
382         return rc;
383 }
384