]> git.sur5r.net Git - openldap/blob - servers/slapd/root_dse.c
more re-engineering; now rdn_attrs() and so are written in terms of ldap_str2rdn...
[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_dn = ch_strdup( LDAP_ROOT_DSE );
70         e->e_ndn = ch_strdup( LDAP_ROOT_DSE );
71         (void) dn_normalize( e->e_ndn );
72         e->e_private = NULL;
73
74         val.bv_val = "OpenLDAProotDSE";
75         val.bv_len = sizeof("OpenLDAProotDSE")-1;
76         attr_merge( e, ad_structuralObjectClass, vals );
77
78         val.bv_val = "top";
79         val.bv_len = sizeof("top")-1;
80         attr_merge( e, ad_objectClass, vals );
81
82         val.bv_val = "OpenLDAProotDSE";
83         val.bv_len = sizeof("OpenLDAProotDSE")-1;
84         attr_merge( e, ad_objectClass, vals );
85
86         for ( i = 0; i < nbackends; i++ ) {
87                 if ( backends[i].be_glueflags & SLAP_GLUE_SUBORDINATE )
88                         continue;
89                 for ( j = 0; backends[i].be_suffix[j] != NULL; j++ ) {
90                         val.bv_val = backends[i].be_suffix[j];
91                         val.bv_len = strlen( val.bv_val );
92                         attr_merge( e, ad_namingContexts, vals );
93                 }
94         }
95
96         /* altServer unsupported */
97
98         /* supportedControl */
99         for ( i=0; supportedControls[i] != NULL; i++ ) {
100                 val.bv_val = supportedControls[i];
101                 val.bv_len = strlen( val.bv_val );
102                 attr_merge( e, ad_supportedControl, vals );
103         }
104
105         /* supportedExtension */
106         for ( i=0; (val.bv_val = get_supported_extop(i)) != NULL; i++ ) {
107                 val.bv_len = strlen( val.bv_val );
108                 attr_merge( e, ad_supportedExtension, vals );
109         }
110
111         /* supportedFeatures */
112         for ( i=0; supportedFeatures[i] != NULL; i++ ) {
113                 val.bv_val = supportedFeatures[i];
114                 val.bv_len = strlen( val.bv_val );
115                 attr_merge( e, ad_supportedFeatures, vals );
116         }
117
118         /* supportedLDAPVersion */
119         for ( i=LDAP_VERSION_MIN; i<=LDAP_VERSION_MAX; i++ ) {
120                 if (!( global_allows & SLAP_ALLOW_BIND_V2 ) &&
121                         ( i < LDAP_VERSION3 ) )
122                 {
123                         /* version 2 and lower are disallowed */
124                         continue;
125                 }
126                 sprintf(buf,"%d",i);
127                 val.bv_val = buf;
128                 val.bv_len = strlen( val.bv_val );
129                 attr_merge( e, ad_supportedLDAPVersion, vals );
130         }
131
132         /* supportedSASLMechanism */
133         supportedSASLMechanisms = slap_sasl_mechs( conn );
134
135         if( supportedSASLMechanisms != NULL ) {
136                 for ( i=0; supportedSASLMechanisms[i] != NULL; i++ ) {
137                         val.bv_val = supportedSASLMechanisms[i];
138                         val.bv_len = strlen( val.bv_val );
139                         attr_merge( e, ad_supportedSASLMechanisms, vals );
140                 }
141                 charray_free( supportedSASLMechanisms );
142         }
143
144         if ( default_referral != NULL ) {
145                 attr_merge( e, ad_ref, default_referral );
146         }
147
148         if( usr_attr != NULL) {
149                 for(a = usr_attr->e_attrs; a != NULL; a = a->a_next) {
150                         attr_merge( e, a->a_desc, a->a_vals );
151                 }
152         }
153
154         *entry = e;
155         return LDAP_SUCCESS;
156 }
157
158 /*
159  * Read the entries specified in fname and merge the attributes
160  * to the user defined rootDSE. Note thaat if we find any errors
161  * what so ever, we will discard the entire entries, print an
162  * error message and return.
163  */
164 int read_root_dse_file( const char *fname )
165 {
166         FILE    *fp;
167         int rc = 0, lineno = 0, lmax = 0;
168         char    *buf = NULL;
169
170         Attribute *a;
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
186                 if( e == NULL ) {
187                         fprintf( stderr, "root_dse: could not parse entry (line=%d)\n",
188                                 lineno );
189                         entry_free( e );
190                         entry_free( usr_attr );
191                         usr_attr = NULL;
192                         return EXIT_FAILURE;
193                 }
194
195                 if( dn_normalize( e->e_ndn ) == NULL ) {
196                         fprintf( stderr, "root_dse: invalid dn=\"%s\" (line=%d)\n",
197                                 e->e_dn, lineno );
198                         entry_free( e );
199                         entry_free( usr_attr );
200                         usr_attr = NULL;
201                         return EXIT_FAILURE;
202                 }
203
204                 /* make sure the DN is a valid rootdse(rootdse is a null string) */
205                 if( strcmp(e->e_ndn, "") != 0 ) {
206                         fprintf( stderr,
207                                 "root_dse: invalid rootDSE - dn=\"%s\" (line=%d)\n",
208                                 e->e_dn, lineno );
209                         entry_free( e );
210                         entry_free( usr_attr );
211                         usr_attr = NULL;
212                         return EXIT_FAILURE;
213                 }
214
215                 /*
216                  * we found a valid entry, so walk thru all the attributes in the
217                  * entry, and add each attribute type and description to the
218                  * usr_attr entry
219                  */
220
221                 for(a = e->e_attrs; a != NULL; a = a->a_next) {
222                         attr_merge( usr_attr, a->a_desc, a->a_vals );
223                 }
224
225                 entry_free( e );
226         }
227
228         ch_free( buf );
229
230         Debug(LDAP_DEBUG_CONFIG,"rootDSE file %s read.\n", fname, 0, 0);
231         return rc;
232 }