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