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