]> git.sur5r.net Git - openldap/blob - servers/slapd/root_dse.c
Convert backendInfo/backendDB to STAILQs
[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 vals[2], *bv;
56         struct berval nvals[2];
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         vals[1].bv_val = NULL;
83         nvals[1].bv_val = NULL;
84
85         e = (Entry *) SLAP_CALLOC( 1, sizeof(Entry) );
86
87         if( e == NULL ) {
88                 Debug( LDAP_DEBUG_ANY,
89                         "root_dse_info: SLAP_CALLOC failed", 0, 0, 0 );
90                 return LDAP_OTHER;
91         }
92
93         e->e_attrs = NULL;
94         e->e_name.bv_val = ch_strdup( LDAP_ROOT_DSE );
95         e->e_name.bv_len = sizeof( LDAP_ROOT_DSE )-1;
96         e->e_nname.bv_val = ch_strdup( LDAP_ROOT_DSE );
97         e->e_nname.bv_len = sizeof( LDAP_ROOT_DSE )-1;
98
99         /* the DN is an empty string so no pretty/normalization is needed */
100         assert( !e->e_name.bv_len );
101         assert( !e->e_nname.bv_len );
102
103         e->e_private = NULL;
104
105         vals[0].bv_val = "top";
106         vals[0].bv_len = sizeof("top")-1;
107         if( attr_merge( e, ad_objectClass, vals, NULL ) ) {
108                 return LDAP_OTHER;
109         }
110
111         vals[0].bv_val = "OpenLDAProotDSE";
112         vals[0].bv_len = sizeof("OpenLDAProotDSE")-1;
113         if( attr_merge( e, ad_objectClass, vals, NULL ) ) {
114                 return LDAP_OTHER;
115         }
116         if( attr_merge( e, ad_structuralObjectClass, vals, NULL ) ) {
117                 return LDAP_OTHER;
118         }
119
120         LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
121                 if ( be->be_suffix == NULL
122                                 || be->be_nsuffix == NULL ) {
123                         /* no suffix! */
124                         continue;
125                 }
126                 if ( SLAP_MONITOR( be )) {
127                         vals[0] = be->be_suffix[0];
128                         nvals[0] = be->be_nsuffix[0];
129                         if( attr_merge( e, ad_monitorContext, vals, nvals ) ) {
130                                 return LDAP_OTHER;
131                         }
132                         continue;
133                 }
134                 if ( SLAP_CONFIG( be )) {
135                         vals[0] = be->be_suffix[0];
136                         nvals[0] = be->be_nsuffix[0];
137                         if( attr_merge( e, ad_configContext, vals, nvals ) ) {
138                                 return LDAP_OTHER;
139                         }
140                         continue;
141                 }
142                 if ( SLAP_GLUE_SUBORDINATE( be ) && !SLAP_GLUE_ADVERTISE( be ) ) {
143                         continue;
144                 }
145                 for ( j = 0; be->be_suffix[j].bv_val != NULL; j++ ) {
146                         vals[0] = be->be_suffix[j];
147                         nvals[0] = be->be_nsuffix[0];
148                         if( attr_merge( e, ad_namingContexts, vals, nvals ) ) {
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                 vals[0] = *bv;
170                 if( attr_merge( e, ad_supportedExtension, vals, NULL )) {
171                         return LDAP_OTHER;
172                 }
173         }
174 #endif /* LDAP_SLAPI */
175
176         /* supportedFeatures */
177         if( attr_merge( e, ad_supportedFeatures, supportedFeatures, NULL ) ) {
178                 return LDAP_OTHER;
179         }
180
181         /* supportedLDAPVersion */
182         for ( i=LDAP_VERSION_MIN; i<=LDAP_VERSION_MAX; i++ ) {
183                 char buf[BUFSIZ];
184                 if (!( global_allows & SLAP_ALLOW_BIND_V2 ) &&
185                         ( i < LDAP_VERSION3 ) )
186                 {
187                         /* version 2 and lower are disallowed */
188                         continue;
189                 }
190                 snprintf(buf, sizeof buf, "%d", i);
191                 vals[0].bv_val = buf;
192                 vals[0].bv_len = strlen( vals[0].bv_val );
193                 if( attr_merge( e, ad_supportedLDAPVersion, vals, 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                         vals[0].bv_val = supportedSASLMechanisms[i];
204                         vals[0].bv_len = strlen( vals[0].bv_val );
205                         if( attr_merge( e, ad_supportedSASLMechanisms, vals, 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         FILE    *fp;
242         int rc = 0, lineno = 0, lmax = 0;
243         char    *buf = NULL;
244
245         if ( (fp = fopen( 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                 fclose( 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         fclose( fp );
310
311         Debug(LDAP_DEBUG_CONFIG, "rootDSE file %s read.\n", fname, 0, 0);
312         return rc;
313 }