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