]> git.sur5r.net Git - openldap/blob - servers/slapd/root_dse.c
Initial round of changes for 2.3 beta
[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         for ( i=LDAP_VERSION_MIN; i<=LDAP_VERSION_MAX; i++ ) {
180                 char buf[BUFSIZ];
181                 if (!( global_allows & SLAP_ALLOW_BIND_V2 ) &&
182                         ( i < LDAP_VERSION3 ) )
183                 {
184                         /* version 2 and lower are disallowed */
185                         continue;
186                 }
187                 snprintf(buf, sizeof buf, "%d", i);
188                 val.bv_val = buf;
189                 val.bv_len = strlen( val.bv_val );
190                 if( attr_merge_one( e, ad_supportedLDAPVersion, &val, NULL ) ) {
191                         return LDAP_OTHER;
192                 }
193         }
194
195         /* supportedSASLMechanism */
196         supportedSASLMechanisms = slap_sasl_mechs( conn );
197
198         if( supportedSASLMechanisms != NULL ) {
199                 for ( i=0; supportedSASLMechanisms[i] != NULL; i++ ) {
200                         val.bv_val = supportedSASLMechanisms[i];
201                         val.bv_len = strlen( val.bv_val );
202                         if( attr_merge_one( e, ad_supportedSASLMechanisms, &val, NULL ) ) {
203                                 return LDAP_OTHER;
204                         }
205                 }
206                 ldap_charray_free( supportedSASLMechanisms );
207         }
208
209         if ( default_referral != NULL ) {
210                 if( attr_merge( e, ad_ref, default_referral, NULL /* FIXME */ ) ) {
211                         return LDAP_OTHER;
212                 }
213         }
214
215         if( usr_attr != NULL) {
216                 Attribute *a;
217                 for( a = usr_attr->e_attrs; a != NULL; a = a->a_next ) {
218                         if( attr_merge( e, a->a_desc, a->a_vals,
219                                 (a->a_nvals == a->a_vals) ? NULL : a->a_nvals ) )
220                         {
221                                 return LDAP_OTHER;
222                         }
223                 }
224         }
225
226         *entry = e;
227         return LDAP_SUCCESS;
228 }
229
230 /*
231  * Read the entries specified in fname and merge the attributes
232  * to the user defined rootDSE. Note thaat if we find any errors
233  * what so ever, we will discard the entire entries, print an
234  * error message and return.
235  */
236 int read_root_dse_file( const char *fname )
237 {
238         FILE    *fp;
239         int rc = 0, lineno = 0, lmax = 0;
240         char    *buf = NULL;
241
242         if ( (fp = fopen( fname, "r" )) == NULL ) {
243                 Debug( LDAP_DEBUG_ANY,
244                         "could not open rootdse attr file \"%s\" - absolute path?\n",
245                         fname, 0, 0 );
246                 perror( fname );
247                 return EXIT_FAILURE;
248         }
249
250         usr_attr = (Entry *) SLAP_CALLOC( 1, sizeof(Entry) );
251         if( usr_attr == NULL ) {
252                 Debug( LDAP_DEBUG_ANY,
253                         "read_root_dse_file: SLAP_CALLOC failed", 0, 0, 0 );
254                 fclose( fp );
255                 return LDAP_OTHER;
256         }
257         usr_attr->e_attrs = NULL;
258
259         while( ldif_read_record( fp, &lineno, &buf, &lmax ) ) {
260                 Entry *e = str2entry( buf );
261                 Attribute *a;
262
263                 if( e == NULL ) {
264                         fprintf( stderr, "root_dse: could not parse entry (line=%d)\n",
265                                 lineno );
266                         rc = EXIT_FAILURE;
267                         break;
268                 }
269
270                 /* make sure the DN is the empty DN */
271                 if( e->e_nname.bv_len ) {
272                         fprintf( stderr,
273                                 "root_dse: invalid rootDSE - dn=\"%s\" (line=%d)\n",
274                                 e->e_dn, lineno );
275                         entry_free( e );
276                         rc = EXIT_FAILURE;
277                         break;
278                 }
279
280                 /*
281                  * we found a valid entry, so walk thru all the attributes in the
282                  * entry, and add each attribute type and description to the
283                  * usr_attr entry
284                  */
285
286                 for(a = e->e_attrs; a != NULL; a = a->a_next) {
287                         if( attr_merge( usr_attr, a->a_desc, a->a_vals,
288                                 (a->a_nvals == a->a_vals) ? NULL : a->a_nvals ) )
289                         {
290                                 rc = LDAP_OTHER;
291                                 break;
292                         }
293                 }
294
295                 entry_free( e );
296                 if (rc) break;
297         }
298
299         if (rc) {
300                 entry_free( usr_attr );
301                 usr_attr = NULL;
302         }
303
304         ch_free( buf );
305
306         fclose( fp );
307
308         Debug(LDAP_DEBUG_CONFIG, "rootDSE file %s read.\n", fname, 0, 0);
309         return rc;
310 }