]> git.sur5r.net Git - openldap/blob - servers/slapd/root_dse.c
More struct berval fixes for modrdn
[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   val;
38         struct berval   *vals[2];
39         int             i, j;
40         char ** supportedSASLMechanisms;
41
42         AttributeDescription *ad_structuralObjectClass
43                 = slap_schema.si_ad_structuralObjectClass;
44         AttributeDescription *ad_objectClass
45                 = slap_schema.si_ad_objectClass;
46         AttributeDescription *ad_namingContexts
47                 = slap_schema.si_ad_namingContexts;
48         AttributeDescription *ad_supportedControl
49                 = slap_schema.si_ad_supportedControl;
50         AttributeDescription *ad_supportedExtension
51                 = slap_schema.si_ad_supportedExtension;
52         AttributeDescription *ad_supportedLDAPVersion
53                 = slap_schema.si_ad_supportedLDAPVersion;
54         AttributeDescription *ad_supportedSASLMechanisms
55                 = slap_schema.si_ad_supportedSASLMechanisms;
56         AttributeDescription *ad_supportedFeatures
57                 = slap_schema.si_ad_supportedFeatures;
58         AttributeDescription *ad_ref
59                 = slap_schema.si_ad_ref;
60
61         Attribute *a;
62
63         vals[0] = &val;
64         vals[1] = 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
79         e->e_private = NULL;
80
81         val.bv_val = "OpenLDAProotDSE";
82         val.bv_len = sizeof("OpenLDAProotDSE")-1;
83         attr_merge( e, ad_structuralObjectClass, vals );
84
85         val.bv_val = "top";
86         val.bv_len = sizeof("top")-1;
87         attr_merge( e, ad_objectClass, vals );
88
89         val.bv_val = "OpenLDAProotDSE";
90         val.bv_len = sizeof("OpenLDAProotDSE")-1;
91         attr_merge( e, ad_objectClass, vals );
92
93         for ( i = 0; i < nbackends; i++ ) {
94                 if ( backends[i].be_glueflags & SLAP_GLUE_SUBORDINATE )
95                         continue;
96                 for ( j = 0; backends[i].be_suffix[j] != NULL; j++ ) {
97                         val = *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; supportedControls[i] != NULL; i++ ) {
106                 val.bv_val = supportedControls[i];
107                 val.bv_len = strlen( val.bv_val );
108                 attr_merge( e, ad_supportedControl, vals );
109         }
110
111         /* supportedExtension */
112         for ( i=0; (val.bv_val = get_supported_extop(i)) != NULL; i++ ) {
113                 val.bv_len = strlen( val.bv_val );
114                 attr_merge( e, ad_supportedExtension, vals );
115         }
116
117         /* supportedFeatures */
118         for ( i=0; supportedFeatures[i] != NULL; i++ ) {
119                 val.bv_val = supportedFeatures[i];
120                 val.bv_len = strlen( val.bv_val );
121                 attr_merge( e, ad_supportedFeatures, vals );
122         }
123
124         /* supportedLDAPVersion */
125         for ( i=LDAP_VERSION_MIN; i<=LDAP_VERSION_MAX; i++ ) {
126                 if (!( global_allows & SLAP_ALLOW_BIND_V2 ) &&
127                         ( i < LDAP_VERSION3 ) )
128                 {
129                         /* version 2 and lower are disallowed */
130                         continue;
131                 }
132                 sprintf(buf,"%d",i);
133                 val.bv_val = buf;
134                 val.bv_len = strlen( val.bv_val );
135                 attr_merge( e, ad_supportedLDAPVersion, vals );
136         }
137
138         /* supportedSASLMechanism */
139         supportedSASLMechanisms = slap_sasl_mechs( conn );
140
141         if( supportedSASLMechanisms != NULL ) {
142                 for ( i=0; supportedSASLMechanisms[i] != NULL; i++ ) {
143                         val.bv_val = supportedSASLMechanisms[i];
144                         val.bv_len = strlen( val.bv_val );
145                         attr_merge( e, ad_supportedSASLMechanisms, vals );
146                 }
147                 charray_free( supportedSASLMechanisms );
148         }
149
150         if ( default_referral != NULL ) {
151                 attr_merge( e, ad_ref, default_referral );
152         }
153
154         if( usr_attr != NULL) {
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         Attribute *a;
177
178         if ( (fp = fopen( fname, "r" )) == NULL ) {
179                 Debug( LDAP_DEBUG_ANY,
180                         "could not open rootdse attr file \"%s\" - absolute path?\n",
181                         fname, 0, 0 );
182                 perror( fname );
183                 return EXIT_FAILURE;
184         }
185
186         usr_attr = (Entry *) ch_calloc( 1, sizeof(Entry) );
187         usr_attr->e_attrs = NULL;
188
189         while( ldif_read_record( fp, &lineno, &buf, &lmax ) ) {
190                 Entry *e = str2entry( buf );
191
192                 if( e == NULL ) {
193                         fprintf( stderr, "root_dse: could not parse entry (line=%d)\n",
194                                 lineno );
195                         entry_free( e );
196                         entry_free( usr_attr );
197                         usr_attr = NULL;
198                         return EXIT_FAILURE;
199                 }
200
201                 /* make sure the DN is the empty DN */
202                 if( e->e_nname.bv_len ) {
203                         fprintf( stderr,
204                                 "root_dse: invalid rootDSE - dn=\"%s\" (line=%d)\n",
205                                 e->e_dn, lineno );
206                         entry_free( e );
207                         entry_free( usr_attr );
208                         usr_attr = NULL;
209                         return EXIT_FAILURE;
210                 }
211
212                 /*
213                  * we found a valid entry, so walk thru all the attributes in the
214                  * entry, and add each attribute type and description to the
215                  * usr_attr entry
216                  */
217
218                 for(a = e->e_attrs; a != NULL; a = a->a_next) {
219                         attr_merge( usr_attr, a->a_desc, a->a_vals );
220                 }
221
222                 entry_free( e );
223         }
224
225         ch_free( buf );
226
227         Debug(LDAP_DEBUG_CONFIG,"rootDSE file %s read.\n", fname, 0, 0);
228         return rc;
229 }