]> git.sur5r.net Git - openldap/blob - servers/slapd/backend.c
Don't include portable.h. Headers can and should assume portable.h
[openldap] / servers / slapd / backend.c
1 /* backend.c - routines for dealing with back-end databases */
2
3
4 #include "portable.h"
5
6 #include <stdio.h>
7
8 #include <ac/string.h>
9 #include <ac/socket.h>
10
11 #include <sys/stat.h>
12
13 #include "slap.h"
14
15 #ifdef SLAPD_LDBM
16 extern int      ldbm_back_bind();
17 extern int      ldbm_back_unbind();
18 extern int      ldbm_back_search();
19 extern int      ldbm_back_compare();
20 extern int      ldbm_back_modify();
21 extern int      ldbm_back_modrdn();
22 extern int      ldbm_back_add();
23 extern int      ldbm_back_delete();
24 extern int      ldbm_back_abandon();
25 extern int      ldbm_back_config();
26 extern int      ldbm_back_init();
27 extern int      ldbm_back_close();
28 extern int      ldbm_back_group();
29 #endif
30
31 #ifdef SLAPD_PASSWD
32 extern int      passwd_back_search();
33 extern int      passwd_back_config();
34 #endif
35
36 #ifdef SLAPD_SHELL
37 extern int      shell_back_bind();
38 extern int      shell_back_unbind();
39 extern int      shell_back_search();
40 extern int      shell_back_compare();
41 extern int      shell_back_modify();
42 extern int      shell_back_modrdn();
43 extern int      shell_back_add();
44 extern int      shell_back_delete();
45 extern int      shell_back_abandon();
46 extern int      shell_back_config();
47 extern int      shell_back_init();
48 #endif
49
50 extern int      defsize;
51 extern int      deftime;
52
53 #define BACKEND_GRAB_SIZE       10
54
55 int             nbackends;
56 Backend         *backends;
57 static int      maxbackends;
58
59 Backend *
60 new_backend(
61     char        *type
62 )
63 {
64         Backend *be;
65         int     foundit;
66
67         if ( nbackends == maxbackends ) {
68                 maxbackends += BACKEND_GRAB_SIZE;
69                 backends = (Backend *) ch_realloc( (char *) backends,
70                     maxbackends * sizeof(Backend) );
71                 memset( &backends[nbackends], '\0', BACKEND_GRAB_SIZE *
72                     sizeof(Backend) );
73         }
74
75         be = &backends[nbackends++];
76         be->be_sizelimit = defsize;
77         be->be_timelimit = deftime;
78         foundit = 0;
79
80 #ifdef SLAPD_LDBM
81         if ( strcasecmp( type, "ldbm" ) == 0 ) {
82                 be->be_bind = ldbm_back_bind;
83                 be->be_unbind = ldbm_back_unbind;
84                 be->be_search = ldbm_back_search;
85                 be->be_compare = ldbm_back_compare;
86                 be->be_modify = ldbm_back_modify;
87                 be->be_modrdn = ldbm_back_modrdn;
88                 be->be_add = ldbm_back_add;
89                 be->be_delete = ldbm_back_delete;
90                 be->be_abandon = ldbm_back_abandon;
91                 be->be_config = ldbm_back_config;
92                 be->be_init = ldbm_back_init;
93                 be->be_close = ldbm_back_close;
94 #ifdef SLAPD_ACLGROUPS
95                 be->be_group = ldbm_back_group;
96 #endif
97                 be->be_type = "ldbm";
98                 foundit = 1;
99         }
100 #endif
101
102 #ifdef SLAPD_PASSWD
103         if ( strcasecmp( type, "passwd" ) == 0 ) {
104                 be->be_bind = NULL;
105                 be->be_unbind = NULL;
106                 be->be_search = passwd_back_search;
107                 be->be_compare = NULL;
108                 be->be_modify = NULL;
109                 be->be_modrdn = NULL;
110                 be->be_add = NULL;
111                 be->be_delete = NULL;
112                 be->be_abandon = NULL;
113                 be->be_config = passwd_back_config;
114                 be->be_init = NULL;
115                 be->be_close = NULL;
116 #ifdef SLAPD_ACLGROUPS
117                 be->be_group = NULL;
118 #endif
119                 be->be_type = "passwd";
120                 foundit = 1;
121         }
122 #endif
123
124 #ifdef SLAPD_SHELL
125         if ( strcasecmp( type, "shell" ) == 0 ) {
126                 be->be_bind = shell_back_bind;
127                 be->be_unbind = shell_back_unbind;
128                 be->be_search = shell_back_search;
129                 be->be_compare = shell_back_compare;
130                 be->be_modify = shell_back_modify;
131                 be->be_modrdn = shell_back_modrdn;
132                 be->be_add = shell_back_add;
133                 be->be_delete = shell_back_delete;
134                 be->be_abandon = shell_back_abandon;
135                 be->be_config = shell_back_config;
136                 be->be_init = shell_back_init;
137                 be->be_close = NULL;
138 #ifdef SLAPD_ACLGROUPS
139                 be->be_group = NULL;
140 #endif
141                 be->be_type = "shell";
142                 foundit = 1;
143         }
144 #endif
145
146         if ( be->be_init != NULL ) {
147                 (*be->be_init)( be );
148         }
149
150         if ( foundit == 0 ) {
151                 fprintf( stderr, "Unrecognized database type (%s)\n", type );
152                 exit( 1 );
153         }
154
155         return( be );
156 }
157
158 Backend *
159 select_backend( char * dn )
160 {
161         int     i, j, len, dnlen;
162
163         dnlen = strlen( dn );
164         for ( i = 0; i < nbackends; i++ ) {
165                 for ( j = 0; backends[i].be_suffix != NULL &&
166                     backends[i].be_suffix[j] != NULL; j++ )
167                 {
168 #ifdef LDAP_ALLOW_NULL_SEARCH_BASE
169                         /* Add greg@greg.rim.or.jp
170                          * It's quick hack for cheap client
171                          * Some browser offer a NULL base at ldap_search
172                          */
173                         if(dnlen == 0) {
174                                 Debug( LDAP_DEBUG_TRACE,
175                                         "select_backend: use default backend\n", 0, 0, 0 );
176                                 return (&backends[i]);
177                         }
178 #endif /* LDAP_ALLOW_NULL_SEARCH_BASE */
179
180                         len = strlen( backends[i].be_suffix[j] );
181
182                         if ( len > dnlen ) {
183                                 continue;
184                         }
185
186                         if ( strcasecmp( backends[i].be_suffix[j],
187                             dn + (dnlen - len) ) == 0 ) {
188                                 return( &backends[i] );
189                         }
190                 }
191         }
192
193         /* if no proper suffix could be found then check for aliases */
194         for ( i = 0; i < nbackends; i++ ) {
195                 for ( j = 0; 
196                       backends[i].be_suffixAlias != NULL && 
197                       backends[i].be_suffixAlias[j] != NULL; 
198                       j += 2 )
199                 {
200                         len = strlen( backends[i].be_suffixAlias[j] );
201
202                         if ( len > dnlen ) {
203                                 continue;
204                         }
205
206                         if ( strcasecmp( backends[i].be_suffixAlias[j],
207                             dn + (dnlen - len) ) == 0 ) {
208                                 return( &backends[i] );
209                         }
210                 }
211         }
212
213
214         return( NULL );
215 }
216
217 int
218 be_issuffix(
219     Backend     *be,
220     char        *suffix
221 )
222 {
223         int     i;
224
225         for ( i = 0; be->be_suffix != NULL && be->be_suffix[i] != NULL; i++ ) {
226                 if ( strcasecmp( be->be_suffix[i], suffix ) == 0 ) {
227                         return( 1 );
228                 }
229         }
230
231         return( 0 );
232 }
233
234 int
235 be_isroot( Backend *be, char *dn )
236 {
237         if ( dn == NULL ) {
238                 return( 0 );
239         }
240
241         return( be->be_rootdn ? strcasecmp( be->be_rootdn, dn ) == 0
242             : 0 );
243 }
244
245 int
246 be_isroot_pw( Backend *be, char *dn, struct berval *cred )
247 {
248         if ( ! be_isroot( be, dn ) || be->be_rootpw == NULL ) {
249                 return( 0 );
250         }
251
252         return( strcmp( be->be_rootpw, cred->bv_val ) == 0 );
253 }
254
255 void
256 be_close()
257 {
258         int     i;
259
260         for ( i = 0; i < nbackends; i++ ) {
261                 if ( backends[i].be_close != NULL ) {
262                         (*backends[i].be_close)( &backends[i] );
263                 }
264         }
265 }
266
267
268 void
269 be_unbind(
270         Connection   *conn,
271         Operation    *op
272 )
273 {
274         int     i;
275
276         for ( i = 0; i < nbackends; i++ ) {
277                 if ( backends[i].be_unbind != NULL ) {
278                         (*backends[i].be_unbind)( &backends[i], conn, op );
279                 }
280         }
281 }
282
283 #ifdef SLAPD_ACLGROUPS
284 int 
285 be_group(Backend *be, char *bdn, char *edn, char *objectclassValue, char *groupattrName)
286 {
287         if (be->be_group)
288                 return(be->be_group(be, bdn, edn, objectclassValue, groupattrName));
289         else
290                 return(1);
291 }
292 #endif