]> git.sur5r.net Git - openldap/blob - servers/slapd/root_dse.c
Added overlay objects
[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
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_ref
77                 = slap_schema.si_ad_ref;
78
79         vals[1].bv_val = NULL;
80         nvals[1].bv_val = NULL;
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         vals[0].bv_val = "top";
103         vals[0].bv_len = sizeof("top")-1;
104         if( attr_merge( e, ad_objectClass, vals, NULL ) ) {
105                 return LDAP_OTHER;
106         }
107
108         vals[0].bv_val = "OpenLDAProotDSE";
109         vals[0].bv_len = sizeof("OpenLDAProotDSE")-1;
110         if( attr_merge( e, ad_objectClass, vals, NULL ) ) {
111                 return LDAP_OTHER;
112         }
113         if( attr_merge( e, ad_structuralObjectClass, vals, NULL ) ) {
114                 return LDAP_OTHER;
115         }
116
117         for ( i = 0; i < nbackends; i++ ) {
118                 if ( backends[i].be_suffix == NULL
119                                 || backends[i].be_nsuffix == NULL ) {
120                         /* no suffix! */
121                         continue;
122                 }
123                 if ( SLAP_MONITOR( &backends[i] )) {
124                         vals[0] = backends[i].be_suffix[0];
125                         nvals[0] = backends[i].be_nsuffix[0];
126                         if( attr_merge( e, ad_monitorContext, vals, nvals ) ) {
127                                 return LDAP_OTHER;
128                         }
129                         continue;
130                 }
131                 if ( SLAP_GLUE_SUBORDINATE( &backends[i] ) && !SLAP_GLUE_ADVERTISE( &backends[i] ) ) {
132                         continue;
133                 }
134                 for ( j = 0; backends[i].be_suffix[j].bv_val != NULL; j++ ) {
135                         vals[0] = backends[i].be_suffix[j];
136                         nvals[0] = backends[i].be_nsuffix[0];
137                         if( attr_merge( e, ad_namingContexts, vals, nvals ) ) {
138                                 return LDAP_OTHER;
139                         }
140                 }
141         }
142
143         /* altServer unsupported */
144
145         /* supportedControl */
146         if ( controls_root_dse_info( e ) != 0 ) {
147                 return LDAP_OTHER;
148         }
149
150         /* supportedExtension */
151         if ( exop_root_dse_info( e ) != 0 ) {
152                 return LDAP_OTHER;
153         }
154
155 #ifdef LDAP_SLAPI
156         /* netscape supportedExtension */
157         for ( i = 0; (bv = slapi_int_get_supported_extop(i)) != NULL; i++ ) {
158                 vals[0] = *bv;
159                 if( attr_merge( e, ad_supportedExtension, vals, NULL )) {
160                         return LDAP_OTHER;
161                 }
162         }
163 #endif /* LDAP_SLAPI */
164
165         /* supportedFeatures */
166         if( attr_merge( e, ad_supportedFeatures, supportedFeatures, NULL ) ) {
167                 return LDAP_OTHER;
168         }
169
170         /* supportedLDAPVersion */
171         for ( i=LDAP_VERSION_MIN; i<=LDAP_VERSION_MAX; i++ ) {
172                 char buf[BUFSIZ];
173                 if (!( global_allows & SLAP_ALLOW_BIND_V2 ) &&
174                         ( i < LDAP_VERSION3 ) )
175                 {
176                         /* version 2 and lower are disallowed */
177                         continue;
178                 }
179                 snprintf(buf, sizeof buf, "%d", i);
180                 vals[0].bv_val = buf;
181                 vals[0].bv_len = strlen( vals[0].bv_val );
182                 if( attr_merge( e, ad_supportedLDAPVersion, vals, NULL ) ) {
183                         return LDAP_OTHER;
184                 }
185         }
186
187         /* supportedSASLMechanism */
188         supportedSASLMechanisms = slap_sasl_mechs( conn );
189
190         if( supportedSASLMechanisms != NULL ) {
191                 for ( i=0; supportedSASLMechanisms[i] != NULL; i++ ) {
192                         vals[0].bv_val = supportedSASLMechanisms[i];
193                         vals[0].bv_len = strlen( vals[0].bv_val );
194                         if( attr_merge( e, ad_supportedSASLMechanisms, vals, NULL ) ) {
195                                 return LDAP_OTHER;
196                         }
197                 }
198                 ldap_charray_free( supportedSASLMechanisms );
199         }
200
201         if ( default_referral != NULL ) {
202                 if( attr_merge( e, ad_ref, default_referral, NULL /* FIXME */ ) ) {
203                         return LDAP_OTHER;
204                 }
205         }
206
207         if( usr_attr != NULL) {
208                 Attribute *a;
209                 for( a = usr_attr->e_attrs; a != NULL; a = a->a_next ) {
210                         if( attr_merge( e, a->a_desc, a->a_vals,
211                                 (a->a_nvals == a->a_vals) ? NULL : a->a_nvals ) )
212                         {
213                                 return LDAP_OTHER;
214                         }
215                 }
216         }
217
218         *entry = e;
219         return LDAP_SUCCESS;
220 }
221
222 /*
223  * Read the entries specified in fname and merge the attributes
224  * to the user defined rootDSE. Note thaat if we find any errors
225  * what so ever, we will discard the entire entries, print an
226  * error message and return.
227  */
228 int read_root_dse_file( const char *fname )
229 {
230         FILE    *fp;
231         int rc = 0, lineno = 0, lmax = 0;
232         char    *buf = NULL;
233
234         if ( (fp = fopen( fname, "r" )) == NULL ) {
235                 Debug( LDAP_DEBUG_ANY,
236                         "could not open rootdse attr file \"%s\" - absolute path?\n",
237                         fname, 0, 0 );
238                 perror( fname );
239                 return EXIT_FAILURE;
240         }
241
242         usr_attr = (Entry *) SLAP_CALLOC( 1, sizeof(Entry) );
243         if( usr_attr == NULL ) {
244                 Debug( LDAP_DEBUG_ANY,
245                         "read_root_dse_file: SLAP_CALLOC failed", 0, 0, 0 );
246                 fclose( fp );
247                 return LDAP_OTHER;
248         }
249         usr_attr->e_attrs = NULL;
250
251         while( ldif_read_record( fp, &lineno, &buf, &lmax ) ) {
252                 Entry *e = str2entry( buf );
253                 Attribute *a;
254
255                 if( e == NULL ) {
256                         fprintf( stderr, "root_dse: could not parse entry (line=%d)\n",
257                                 lineno );
258                         rc = EXIT_FAILURE;
259                         break;
260                 }
261
262                 /* make sure the DN is the empty DN */
263                 if( e->e_nname.bv_len ) {
264                         fprintf( stderr,
265                                 "root_dse: invalid rootDSE - dn=\"%s\" (line=%d)\n",
266                                 e->e_dn, lineno );
267                         entry_free( e );
268                         rc = EXIT_FAILURE;
269                         break;
270                 }
271
272                 /*
273                  * we found a valid entry, so walk thru all the attributes in the
274                  * entry, and add each attribute type and description to the
275                  * usr_attr entry
276                  */
277
278                 for(a = e->e_attrs; a != NULL; a = a->a_next) {
279                         if( attr_merge( usr_attr, a->a_desc, a->a_vals,
280                                 (a->a_nvals == a->a_vals) ? NULL : a->a_nvals ) )
281                         {
282                                 rc = LDAP_OTHER;
283                                 break;
284                         }
285                 }
286
287                 entry_free( e );
288                 if (rc) break;
289         }
290
291         if (rc) {
292                 entry_free( usr_attr );
293                 usr_attr = NULL;
294         }
295
296         ch_free( buf );
297
298         fclose( fp );
299
300         Debug(LDAP_DEBUG_CONFIG, "rootDSE file %s read.\n", fname, 0, 0);
301         return rc;
302 }