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