]> git.sur5r.net Git - openldap/blob - servers/slapd/root_dse.c
e8a3971c402e3bd9612edc380f584b7b9cd1eab4
[openldap] / servers / slapd / root_dse.c
1 /* $OpenLDAP$ */
2 /* root_dse.c - Provides the ROOT DSA-Specific Entry
3  *
4  * Copyright 1999-2003 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms are permitted only
8  * as authorized by the OpenLDAP Public License.  A copy of this
9  * license is available at http://www.OpenLDAP.org/license.html or
10  * in file LICENSE in the top-level directory of the distribution.
11  */
12
13 #include "portable.h"
14
15 #include <stdio.h>
16
17 #include <ac/string.h>
18
19 #include "slap.h"
20 #include <ldif.h>
21 #include "lber_pvt.h"
22
23 #ifdef LDAP_SLAPI
24 #include "slapi.h"
25 #endif
26
27 static struct berval supportedFeatures[] = {
28         BER_BVC(LDAP_FEATURE_ALL_OPERATIONAL_ATTRS), /* all Operational Attributes ("+") */
29         BER_BVC(LDAP_FEATURE_OBJECTCLASS_ATTRS), /* OCs in Attributes List */
30         BER_BVC(LDAP_FEATURE_ABSOLUTE_FILTERS), /* (&) and (|) search filters */
31         BER_BVC(LDAP_FEATURE_LANGUAGE_TAG_OPTIONS), /* Language Tag Options */
32         BER_BVC(LDAP_FEATURE_LANGUAGE_RANGE_OPTIONS), /* Language Range Options */
33         {0,NULL}
34 };
35
36 static Entry    *usr_attr = NULL;
37
38 int
39 root_dse_info(
40         Connection *conn,
41         Entry **entry,
42         const char **text )
43 {
44         Entry           *e;
45         struct berval vals[2], *bv;
46 #ifdef SLAP_NVALUES
47         struct berval nvals[2];
48 #endif
49         int             i, j;
50         char ** supportedSASLMechanisms;
51
52         AttributeDescription *ad_structuralObjectClass
53                 = slap_schema.si_ad_structuralObjectClass;
54         AttributeDescription *ad_objectClass
55                 = slap_schema.si_ad_objectClass;
56         AttributeDescription *ad_namingContexts
57                 = slap_schema.si_ad_namingContexts;
58         AttributeDescription *ad_supportedExtension
59                 = slap_schema.si_ad_supportedExtension;
60         AttributeDescription *ad_supportedLDAPVersion
61                 = slap_schema.si_ad_supportedLDAPVersion;
62         AttributeDescription *ad_supportedSASLMechanisms
63                 = slap_schema.si_ad_supportedSASLMechanisms;
64         AttributeDescription *ad_supportedFeatures
65                 = slap_schema.si_ad_supportedFeatures;
66         AttributeDescription *ad_monitorContext
67                 = slap_schema.si_ad_monitorContext;
68         AttributeDescription *ad_ref
69                 = slap_schema.si_ad_ref;
70
71         vals[1].bv_val = NULL;
72 #ifdef SLAP_NVALUES
73         nvals[1].bv_val = NULL;
74 #endif
75
76         e = (Entry *) SLAP_CALLOC( 1, sizeof(Entry) );
77
78         if( e == NULL ) {
79 #ifdef NEW_LOGGING
80                 LDAP_LOG( OPERATION, ERR,
81                         "root_dse_info: SLAP_CALLOC failed", 0, 0, 0 );
82 #else
83                 Debug( LDAP_DEBUG_ANY,
84                         "root_dse_info: SLAP_CALLOC failed", 0, 0, 0 );
85 #endif
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         vals[0].bv_val = "top";
102         vals[0].bv_len = sizeof("top")-1;
103 #ifdef SLAP_NVALUES
104         if( attr_merge( e, ad_objectClass, vals, NULL ) )
105 #else
106         if( attr_merge( e, ad_objectClass, vals ) )
107 #endif
108         {
109                 return LDAP_OTHER;
110         }
111
112         vals[0].bv_val = "OpenLDAProotDSE";
113         vals[0].bv_len = sizeof("OpenLDAProotDSE")-1;
114 #ifdef SLAP_NVALUES
115         if( attr_merge( e, ad_objectClass, vals, NULL ) )
116 #else
117         if( attr_merge( e, ad_objectClass, vals ) )
118 #endif
119                 return LDAP_OTHER;
120 #ifdef SLAP_NVALUES
121         if( attr_merge( e, ad_structuralObjectClass, vals, NULL ) )
122 #else
123         if( attr_merge( e, ad_structuralObjectClass, vals ) )
124 #endif
125                 return LDAP_OTHER;
126
127         for ( i = 0; i < nbackends; i++ ) {
128                 if ( backends[i].be_flags & SLAP_BFLAG_MONITOR ) {
129                         vals[0] = backends[i].be_suffix[0];
130 #ifdef SLAP_NVALUES
131                         nvals[0] = backends[i].be_nsuffix[0];
132                         if( attr_merge( e, ad_monitorContext, vals, nvals ) )
133 #else
134                         if( attr_merge( e, ad_monitorContext, vals ) )
135 #endif
136                         {
137                                 return LDAP_OTHER;
138                         }
139                         continue;
140                 }
141                 if ( SLAP_GLUE_SUBORDINATE( &backends[i] ) ) {
142                         continue;
143                 }
144                 for ( j = 0; backends[i].be_suffix[j].bv_val != NULL; j++ ) {
145                         vals[0] = backends[i].be_suffix[j];
146 #ifdef SLAP_NVALUES
147                         nvals[0] = backends[i].be_nsuffix[0];
148                         if( attr_merge( e, ad_namingContexts, vals, nvals ) )
149 #else
150                         if( attr_merge( e, ad_namingContexts, vals ) )
151 #endif
152                         {
153                                 return LDAP_OTHER;
154                         }
155                 }
156         }
157
158         /* altServer unsupported */
159
160         /* supportedControl */
161         if ( controls_root_dse_info( e ) != 0 ) {
162                 return LDAP_OTHER;
163         }
164
165         /* supportedExtension */
166         for ( i=0; (bv = get_supported_extop(i)) != NULL; i++ ) {
167                 vals[0] = *bv;
168 #ifdef SLAP_NVALUES
169                 if( attr_merge( e, ad_supportedExtension, vals, NULL ) )
170 #else
171                 if( attr_merge( e, ad_supportedExtension, vals ) )
172 #endif
173                 {
174                         return LDAP_OTHER;
175                 }
176         }
177
178 #ifdef LDAP_SLAPI
179         /* netscape supportedExtension */
180         for ( i = 0; (bv = ns_get_supported_extop(i)) != NULL; i++ ) {
181                 vals[0] = *bv;
182 #ifdef SLAP_NVALUES
183                 if( attr_merge( e, ad_supportedExtension, vals, NULL ))
184 #else
185                 if( attr_merge( e, ad_supportedExtension, vals ))
186 #endif
187                 {
188                         return LDAP_OTHER;
189                 }
190         }
191 #endif /* LDAP_SLAPI */
192
193         /* supportedFeatures */
194 #ifdef SLAP_NVALUES
195         if( attr_merge( e, ad_supportedFeatures,
196                 supportedFeatures, NULL ) )
197 #else
198         if( attr_merge( e, ad_supportedFeatures, supportedFeatures ) )
199 #endif
200         {
201                 return LDAP_OTHER;
202         }
203
204         /* supportedLDAPVersion */
205         for ( i=LDAP_VERSION_MIN; i<=LDAP_VERSION_MAX; i++ ) {
206                 char buf[BUFSIZ];
207                 if (!( global_allows & SLAP_ALLOW_BIND_V2 ) &&
208                         ( i < LDAP_VERSION3 ) )
209                 {
210                         /* version 2 and lower are disallowed */
211                         continue;
212                 }
213                 snprintf(buf, sizeof buf, "%d", i);
214                 vals[0].bv_val = buf;
215                 vals[0].bv_len = strlen( vals[0].bv_val );
216 #ifdef SLAP_NVALUES
217                 if( attr_merge( e, ad_supportedLDAPVersion, vals, NULL ) )
218 #else
219                 if( attr_merge( e, ad_supportedLDAPVersion, vals ) )
220 #endif
221                 {
222                         return LDAP_OTHER;
223                 }
224         }
225
226         /* supportedSASLMechanism */
227         supportedSASLMechanisms = slap_sasl_mechs( conn );
228
229         if( supportedSASLMechanisms != NULL ) {
230                 for ( i=0; supportedSASLMechanisms[i] != NULL; i++ ) {
231                         vals[0].bv_val = supportedSASLMechanisms[i];
232                         vals[0].bv_len = strlen( vals[0].bv_val );
233 #ifdef SLAP_NVALUES
234                         if( attr_merge( e, ad_supportedSASLMechanisms, vals, NULL ) )
235 #else
236                         if( attr_merge( e, ad_supportedSASLMechanisms, vals ) )
237 #endif
238                         {
239                                 return LDAP_OTHER;
240                         }
241                 }
242                 ldap_charray_free( supportedSASLMechanisms );
243         }
244
245         if ( default_referral != NULL ) {
246 #ifdef SLAP_NVALUES
247                 if( attr_merge( e, ad_ref, default_referral, NULL /* FIXME */ ) )
248 #else
249                 if( attr_merge( e, ad_ref, default_referral ) )
250 #endif
251                 {
252                         return LDAP_OTHER;
253                 }
254         }
255
256         if( usr_attr != NULL) {
257                 Attribute *a;
258                 for( a = usr_attr->e_attrs; a != NULL; a = a->a_next ) {
259 #ifdef SLAP_NVALUES
260                         if( attr_merge( e, a->a_desc, a->a_vals,
261                          (a->a_nvals == a->a_vals) ? NULL : a->a_nvals ) )
262 #else
263                         if( attr_merge( e, a->a_desc, a->a_vals ) )
264 #endif
265                         {
266                                 return LDAP_OTHER;
267                         }
268                 }
269         }
270
271         *entry = e;
272         return LDAP_SUCCESS;
273 }
274
275 /*
276  * Read the entries specified in fname and merge the attributes
277  * to the user defined rootDSE. Note thaat if we find any errors
278  * what so ever, we will discard the entire entries, print an
279  * error message and return.
280  */
281 int read_root_dse_file( const char *fname )
282 {
283         FILE    *fp;
284         int rc = 0, lineno = 0, lmax = 0;
285         char    *buf = NULL;
286
287         if ( (fp = fopen( fname, "r" )) == NULL ) {
288                 Debug( LDAP_DEBUG_ANY,
289                         "could not open rootdse attr file \"%s\" - absolute path?\n",
290                         fname, 0, 0 );
291                 perror( fname );
292                 return EXIT_FAILURE;
293         }
294
295         usr_attr = (Entry *) SLAP_CALLOC( 1, sizeof(Entry) );
296         if( usr_attr == NULL ) {
297 #ifdef NEW_LOGGING
298                 LDAP_LOG( OPERATION, ERR,
299                         "read_root_dse_file: SLAP_CALLOC failed", 0, 0, 0 );
300 #else
301                 Debug( LDAP_DEBUG_ANY,
302                         "read_root_dse_file: SLAP_CALLOC failed", 0, 0, 0 );
303 #endif
304                 fclose( fp );
305                 return LDAP_OTHER;
306         }
307         usr_attr->e_attrs = NULL;
308
309         while( ldif_read_record( fp, &lineno, &buf, &lmax ) ) {
310                 Entry *e = str2entry( buf );
311                 Attribute *a;
312
313                 if( e == NULL ) {
314                         fprintf( stderr, "root_dse: could not parse entry (line=%d)\n",
315                                 lineno );
316                         rc = EXIT_FAILURE;
317                         break;
318                 }
319
320                 /* make sure the DN is the empty DN */
321                 if( e->e_nname.bv_len ) {
322                         fprintf( stderr,
323                                 "root_dse: invalid rootDSE - dn=\"%s\" (line=%d)\n",
324                                 e->e_dn, lineno );
325                         entry_free( e );
326                         rc = EXIT_FAILURE;
327                         break;
328                 }
329
330                 /*
331                  * we found a valid entry, so walk thru all the attributes in the
332                  * entry, and add each attribute type and description to the
333                  * usr_attr entry
334                  */
335
336                 for(a = e->e_attrs; a != NULL; a = a->a_next) {
337 #ifdef SLAP_NVALUES
338                         if( attr_merge( usr_attr, a->a_desc, a->a_vals,
339                         (a->a_nvals == a->a_vals) ? NULL : a->a_nvals ) )
340 #else
341                         if( attr_merge( usr_attr, a->a_desc, a->a_vals ) )
342 #endif
343                         {
344                                 rc = LDAP_OTHER;
345                                 break;
346                         }
347                 }
348
349                 entry_free( e );
350                 if (rc) break;
351         }
352
353         if (rc) {
354                 entry_free( usr_attr );
355                 usr_attr = NULL;
356         }
357
358         ch_free( buf );
359
360         fclose( fp );
361
362         Debug(LDAP_DEBUG_CONFIG, "rootDSE file %s read.\n", fname, 0, 0);
363         return rc;
364 }