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