]> git.sur5r.net Git - openldap/blob - servers/slapd/root_dse.c
Ready for 2.2beta
[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 Op Attrs (+) */
29         BER_BVC(LDAP_FEATURE_OBJECTCLASS_ATTRS), /* OCs in Attrs List (+person) */
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 #ifdef LDAP_DEVEL
34         BER_BVC(LDAP_FEATURE_MODIFY_INCREMENT), /* Modify/increment */
35 #endif
36         {0,NULL}
37 };
38
39 static Entry    *usr_attr = NULL;
40
41 int
42 root_dse_info(
43         Connection *conn,
44         Entry **entry,
45         const char **text )
46 {
47         Entry           *e;
48         struct berval vals[2], *bv;
49         struct berval nvals[2];
50         int             i, j;
51         char ** supportedSASLMechanisms;
52
53         AttributeDescription *ad_structuralObjectClass
54                 = slap_schema.si_ad_structuralObjectClass;
55         AttributeDescription *ad_objectClass
56                 = slap_schema.si_ad_objectClass;
57         AttributeDescription *ad_namingContexts
58                 = slap_schema.si_ad_namingContexts;
59         AttributeDescription *ad_supportedExtension
60                 = slap_schema.si_ad_supportedExtension;
61         AttributeDescription *ad_supportedLDAPVersion
62                 = slap_schema.si_ad_supportedLDAPVersion;
63         AttributeDescription *ad_supportedSASLMechanisms
64                 = slap_schema.si_ad_supportedSASLMechanisms;
65         AttributeDescription *ad_supportedFeatures
66                 = slap_schema.si_ad_supportedFeatures;
67         AttributeDescription *ad_monitorContext
68                 = slap_schema.si_ad_monitorContext;
69         AttributeDescription *ad_ref
70                 = slap_schema.si_ad_ref;
71
72         vals[1].bv_val = NULL;
73         nvals[1].bv_val = NULL;
74
75         e = (Entry *) SLAP_CALLOC( 1, sizeof(Entry) );
76
77         if( e == NULL ) {
78 #ifdef NEW_LOGGING
79                 LDAP_LOG( OPERATION, ERR,
80                         "root_dse_info: SLAP_CALLOC failed", 0, 0, 0 );
81 #else
82                 Debug( LDAP_DEBUG_ANY,
83                         "root_dse_info: SLAP_CALLOC failed", 0, 0, 0 );
84 #endif
85                 return LDAP_OTHER;
86         }
87
88         e->e_attrs = NULL;
89         e->e_name.bv_val = ch_strdup( LDAP_ROOT_DSE );
90         e->e_name.bv_len = sizeof( LDAP_ROOT_DSE )-1;
91         e->e_nname.bv_val = ch_strdup( LDAP_ROOT_DSE );
92         e->e_nname.bv_len = sizeof( LDAP_ROOT_DSE )-1;
93
94         /* the DN is an empty string so no pretty/normalization is needed */
95         assert( !e->e_name.bv_len );
96         assert( !e->e_nname.bv_len );
97
98         e->e_private = NULL;
99
100         vals[0].bv_val = "top";
101         vals[0].bv_len = sizeof("top")-1;
102         if( attr_merge( e, ad_objectClass, vals, NULL ) ) {
103                 return LDAP_OTHER;
104         }
105
106         vals[0].bv_val = "OpenLDAProotDSE";
107         vals[0].bv_len = sizeof("OpenLDAProotDSE")-1;
108         if( attr_merge( e, ad_objectClass, vals, NULL ) ) {
109                 return LDAP_OTHER;
110         }
111         if( attr_merge( e, ad_structuralObjectClass, vals, NULL ) ) {
112                 return LDAP_OTHER;
113         }
114
115         for ( i = 0; i < nbackends; i++ ) {
116                 if ( backends[i].be_flags & SLAP_BFLAG_MONITOR ) {
117                         vals[0] = backends[i].be_suffix[0];
118                         nvals[0] = backends[i].be_nsuffix[0];
119                         if( attr_merge( e, ad_monitorContext, vals, nvals ) ) {
120                                 return LDAP_OTHER;
121                         }
122                         continue;
123                 }
124                 if ( SLAP_GLUE_SUBORDINATE( &backends[i] ) ) {
125                         continue;
126                 }
127                 for ( j = 0; backends[i].be_suffix[j].bv_val != NULL; j++ ) {
128                         vals[0] = backends[i].be_suffix[j];
129                         nvals[0] = backends[i].be_nsuffix[0];
130                         if( attr_merge( e, ad_namingContexts, vals, nvals ) ) {
131                                 return LDAP_OTHER;
132                         }
133                 }
134         }
135
136         /* altServer unsupported */
137
138         /* supportedControl */
139         if ( controls_root_dse_info( e ) != 0 ) {
140                 return LDAP_OTHER;
141         }
142
143         /* supportedExtension */
144         if ( exop_root_dse_info( e ) != 0 ) {
145                 return LDAP_OTHER;
146         }
147
148 #ifdef LDAP_SLAPI
149         /* netscape supportedExtension */
150         for ( i = 0; (bv = ns_get_supported_extop(i)) != NULL; i++ ) {
151                 vals[0] = *bv;
152                 if( attr_merge( e, ad_supportedExtension, vals, NULL )) {
153                         return LDAP_OTHER;
154                 }
155         }
156 #endif /* LDAP_SLAPI */
157
158         /* supportedFeatures */
159         if( attr_merge( e, ad_supportedFeatures, supportedFeatures, NULL ) ) {
160                 return LDAP_OTHER;
161         }
162
163         /* supportedLDAPVersion */
164         for ( i=LDAP_VERSION_MIN; i<=LDAP_VERSION_MAX; i++ ) {
165                 char buf[BUFSIZ];
166                 if (!( global_allows & SLAP_ALLOW_BIND_V2 ) &&
167                         ( i < LDAP_VERSION3 ) )
168                 {
169                         /* version 2 and lower are disallowed */
170                         continue;
171                 }
172                 snprintf(buf, sizeof buf, "%d", i);
173                 vals[0].bv_val = buf;
174                 vals[0].bv_len = strlen( vals[0].bv_val );
175                 if( attr_merge( e, ad_supportedLDAPVersion, vals, NULL ) ) {
176                         return LDAP_OTHER;
177                 }
178         }
179
180         /* supportedSASLMechanism */
181         supportedSASLMechanisms = slap_sasl_mechs( conn );
182
183         if( supportedSASLMechanisms != NULL ) {
184                 for ( i=0; supportedSASLMechanisms[i] != NULL; i++ ) {
185                         vals[0].bv_val = supportedSASLMechanisms[i];
186                         vals[0].bv_len = strlen( vals[0].bv_val );
187                         if( attr_merge( e, ad_supportedSASLMechanisms, vals, NULL ) ) {
188                                 return LDAP_OTHER;
189                         }
190                 }
191                 ldap_charray_free( supportedSASLMechanisms );
192         }
193
194         if ( default_referral != NULL ) {
195                 if( attr_merge( e, ad_ref, default_referral, NULL /* FIXME */ ) ) {
196                         return LDAP_OTHER;
197                 }
198         }
199
200         if( usr_attr != NULL) {
201                 Attribute *a;
202                 for( a = usr_attr->e_attrs; a != NULL; a = a->a_next ) {
203                         if( attr_merge( e, a->a_desc, a->a_vals,
204                                 (a->a_nvals == a->a_vals) ? NULL : a->a_nvals ) )
205                         {
206                                 return LDAP_OTHER;
207                         }
208                 }
209         }
210
211         *entry = e;
212         return LDAP_SUCCESS;
213 }
214
215 /*
216  * Read the entries specified in fname and merge the attributes
217  * to the user defined rootDSE. Note thaat if we find any errors
218  * what so ever, we will discard the entire entries, print an
219  * error message and return.
220  */
221 int read_root_dse_file( const char *fname )
222 {
223         FILE    *fp;
224         int rc = 0, lineno = 0, lmax = 0;
225         char    *buf = NULL;
226
227         if ( (fp = fopen( fname, "r" )) == NULL ) {
228                 Debug( LDAP_DEBUG_ANY,
229                         "could not open rootdse attr file \"%s\" - absolute path?\n",
230                         fname, 0, 0 );
231                 perror( fname );
232                 return EXIT_FAILURE;
233         }
234
235         usr_attr = (Entry *) SLAP_CALLOC( 1, sizeof(Entry) );
236         if( usr_attr == NULL ) {
237 #ifdef NEW_LOGGING
238                 LDAP_LOG( OPERATION, ERR,
239                         "read_root_dse_file: SLAP_CALLOC failed", 0, 0, 0 );
240 #else
241                 Debug( LDAP_DEBUG_ANY,
242                         "read_root_dse_file: SLAP_CALLOC failed", 0, 0, 0 );
243 #endif
244                 fclose( fp );
245                 return LDAP_OTHER;
246         }
247         usr_attr->e_attrs = NULL;
248
249         while( ldif_read_record( fp, &lineno, &buf, &lmax ) ) {
250                 Entry *e = str2entry( buf );
251                 Attribute *a;
252
253                 if( e == NULL ) {
254                         fprintf( stderr, "root_dse: could not parse entry (line=%d)\n",
255                                 lineno );
256                         rc = EXIT_FAILURE;
257                         break;
258                 }
259
260                 /* make sure the DN is the empty DN */
261                 if( e->e_nname.bv_len ) {
262                         fprintf( stderr,
263                                 "root_dse: invalid rootDSE - dn=\"%s\" (line=%d)\n",
264                                 e->e_dn, lineno );
265                         entry_free( e );
266                         rc = EXIT_FAILURE;
267                         break;
268                 }
269
270                 /*
271                  * we found a valid entry, so walk thru all the attributes in the
272                  * entry, and add each attribute type and description to the
273                  * usr_attr entry
274                  */
275
276                 for(a = e->e_attrs; a != NULL; a = a->a_next) {
277                         if( attr_merge( usr_attr, a->a_desc, a->a_vals,
278                                 (a->a_nvals == a->a_vals) ? NULL : a->a_nvals ) )
279                         {
280                                 rc = LDAP_OTHER;
281                                 break;
282                         }
283                 }
284
285                 entry_free( e );
286                 if (rc) break;
287         }
288
289         if (rc) {
290                 entry_free( usr_attr );
291                 usr_attr = NULL;
292         }
293
294         ch_free( buf );
295
296         fclose( fp );
297
298         Debug(LDAP_DEBUG_CONFIG, "rootDSE file %s read.\n", fname, 0, 0);
299         return rc;
300 }