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