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