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