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