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