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