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