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