]> git.sur5r.net Git - openldap/blob - servers/slapd/root_dse.c
Suck in changes from HEAD
[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("1.3.6.1.4.1.4203.1.5.1"), /* all Operational Attributes ("+") */
24         BER_BVC("1.3.6.1.4.1.4203.1.5.2"), /* OCs in Attributes List */
25         BER_BVC("1.3.6.1.4.1.4203.1.5.3"), /* (&) and (|) search filters */
26         BER_BVC("1.3.6.1.4.1.4203.1.5.4"), /* Language Tag Options */
27         BER_BVC("1.3.6.1.4.1.4203.1.5.5"), /* 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         char buf[BUFSIZ];
40         Entry           *e;
41         struct berval   vals[2], *bv;
42         int             i, j;
43         char ** supportedSASLMechanisms;
44
45         AttributeDescription *ad_structuralObjectClass
46                 = slap_schema.si_ad_structuralObjectClass;
47         AttributeDescription *ad_objectClass
48                 = slap_schema.si_ad_objectClass;
49         AttributeDescription *ad_namingContexts
50                 = slap_schema.si_ad_namingContexts;
51         AttributeDescription *ad_supportedControl
52                 = slap_schema.si_ad_supportedControl;
53         AttributeDescription *ad_supportedExtension
54                 = slap_schema.si_ad_supportedExtension;
55         AttributeDescription *ad_supportedLDAPVersion
56                 = slap_schema.si_ad_supportedLDAPVersion;
57         AttributeDescription *ad_supportedSASLMechanisms
58                 = slap_schema.si_ad_supportedSASLMechanisms;
59         AttributeDescription *ad_supportedFeatures
60                 = slap_schema.si_ad_supportedFeatures;
61         AttributeDescription *ad_monitorContext
62                 = slap_schema.si_ad_monitorContext;
63         AttributeDescription *ad_ref
64                 = slap_schema.si_ad_ref;
65
66         vals[1].bv_val = NULL;
67
68         e = (Entry *) ch_calloc( 1, sizeof(Entry) );
69
70         e->e_attrs = NULL;
71         e->e_name.bv_val = ch_strdup( LDAP_ROOT_DSE );
72         e->e_name.bv_len = sizeof( LDAP_ROOT_DSE )-1;
73         e->e_nname.bv_val = ch_strdup( LDAP_ROOT_DSE );
74         e->e_nname.bv_len = sizeof( LDAP_ROOT_DSE )-1;
75
76         /* the DN is an empty string so no pretty/normalization is needed */
77         assert( !e->e_name.bv_len );
78         assert( !e->e_nname.bv_len );
79
80         e->e_private = NULL;
81
82         vals[0].bv_val = "top";
83         vals[0].bv_len = sizeof("top")-1;
84         attr_merge( e, ad_objectClass, vals );
85
86         vals[0].bv_val = "OpenLDAProotDSE";
87         vals[0].bv_len = sizeof("OpenLDAProotDSE")-1;
88         attr_merge( e, ad_objectClass, vals );
89         attr_merge( e, ad_structuralObjectClass, vals );
90
91         for ( i = 0; i < nbackends; i++ ) {
92                 if ( backends[i].be_flags & SLAP_BFLAG_MONITOR ) {
93                         vals[0] = backends[i].be_suffix[0];
94                         attr_merge( e, ad_monitorContext, vals );
95                         continue;
96                 }
97                 if ( backends[i].be_flags & SLAP_BFLAG_GLUE_SUBORDINATE ) {
98                         continue;
99                 }
100                 for ( j = 0; backends[i].be_suffix[j].bv_val != NULL; j++ ) {
101                         vals[0] = backends[i].be_suffix[j];
102                         attr_merge( e, ad_namingContexts, vals );
103                 }
104         }
105
106         /* altServer unsupported */
107
108         /* supportedControl */
109         for ( i=0; (vals[0].bv_val = get_supported_ctrl(i)) != NULL; i++ ) {
110                 vals[0].bv_len = strlen( vals[0].bv_val );
111                 attr_merge( e, ad_supportedControl, vals );
112         }
113
114         /* supportedExtension */
115         for ( i=0; (bv = get_supported_extop(i)) != NULL; i++ ) {
116                 vals[0] = *bv;
117                 attr_merge( e, ad_supportedExtension, vals );
118         }
119
120         /* supportedFeatures */
121         attr_merge( e, ad_supportedFeatures, supportedFeatures );
122
123         /* supportedLDAPVersion */
124         for ( i=LDAP_VERSION_MIN; i<=LDAP_VERSION_MAX; i++ ) {
125                 if (!( global_allows & SLAP_ALLOW_BIND_V2 ) &&
126                         ( i < LDAP_VERSION3 ) )
127                 {
128                         /* version 2 and lower are disallowed */
129                         continue;
130                 }
131                 sprintf(buf,"%d",i);
132                 vals[0].bv_val = buf;
133                 vals[0].bv_len = strlen( vals[0].bv_val );
134                 attr_merge( e, ad_supportedLDAPVersion, vals );
135         }
136
137         /* supportedSASLMechanism */
138         supportedSASLMechanisms = slap_sasl_mechs( conn );
139
140         if( supportedSASLMechanisms != NULL ) {
141                 for ( i=0; supportedSASLMechanisms[i] != NULL; i++ ) {
142                         vals[0].bv_val = supportedSASLMechanisms[i];
143                         vals[0].bv_len = strlen( vals[0].bv_val );
144                         attr_merge( e, ad_supportedSASLMechanisms, vals );
145                 }
146                 charray_free( supportedSASLMechanisms );
147         }
148
149         if ( default_referral != NULL ) {
150                 attr_merge( e, ad_ref, default_referral );
151         }
152
153         if( usr_attr != NULL) {
154                 Attribute *a;
155                 for( a = usr_attr->e_attrs; a != NULL; a = a->a_next ) {
156                         attr_merge( e, a->a_desc, a->a_vals );
157                 }
158         }
159
160         *entry = e;
161         return LDAP_SUCCESS;
162 }
163
164 /*
165  * Read the entries specified in fname and merge the attributes
166  * to the user defined rootDSE. Note thaat if we find any errors
167  * what so ever, we will discard the entire entries, print an
168  * error message and return.
169  */
170 int read_root_dse_file( const char *fname )
171 {
172         FILE    *fp;
173         int rc = 0, lineno = 0, lmax = 0;
174         char    *buf = NULL;
175
176         if ( (fp = fopen( fname, "r" )) == NULL ) {
177                 Debug( LDAP_DEBUG_ANY,
178                         "could not open rootdse attr file \"%s\" - absolute path?\n",
179                         fname, 0, 0 );
180                 perror( fname );
181                 return EXIT_FAILURE;
182         }
183
184         usr_attr = (Entry *) ch_calloc( 1, sizeof(Entry) );
185         usr_attr->e_attrs = NULL;
186
187         while( ldif_read_record( fp, &lineno, &buf, &lmax ) ) {
188                 Entry *e = str2entry( buf );
189                 Attribute *a;
190
191                 if( e == NULL ) {
192                         fprintf( stderr, "root_dse: could not parse entry (line=%d)\n",
193                                 lineno );
194                         entry_free( e );
195                         entry_free( usr_attr );
196                         usr_attr = NULL;
197                         return EXIT_FAILURE;
198                 }
199
200                 /* make sure the DN is the empty DN */
201                 if( e->e_nname.bv_len ) {
202                         fprintf( stderr,
203                                 "root_dse: invalid rootDSE - dn=\"%s\" (line=%d)\n",
204                                 e->e_dn, lineno );
205                         entry_free( e );
206                         entry_free( usr_attr );
207                         usr_attr = NULL;
208                         return EXIT_FAILURE;
209                 }
210
211                 /*
212                  * we found a valid entry, so walk thru all the attributes in the
213                  * entry, and add each attribute type and description to the
214                  * usr_attr entry
215                  */
216
217                 for(a = e->e_attrs; a != NULL; a = a->a_next) {
218                         attr_merge( usr_attr, a->a_desc, a->a_vals );
219                 }
220
221                 entry_free( e );
222         }
223
224         ch_free( buf );
225
226         Debug(LDAP_DEBUG_CONFIG, "rootDSE file %s read.\n", fname, 0, 0);
227         return rc;
228 }