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