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