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