]> git.sur5r.net Git - openldap/blob - servers/slapd/backend.c
67dafc69ec907479d5e58fbf66ad3b05d5bc27bb
[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         return( NULL );
190 }
191
192 int
193 be_issuffix(
194     Backend     *be,
195     char        *suffix
196 )
197 {
198         int     i;
199
200         for ( i = 0; be->be_suffix != NULL && be->be_suffix[i] != NULL; i++ ) {
201                 if ( strcasecmp( be->be_suffix[i], suffix ) == 0 ) {
202                         return( 1 );
203                 }
204         }
205
206         return( 0 );
207 }
208
209 int
210 be_isroot( Backend *be, char *dn )
211 {
212         if ( dn == NULL ) {
213                 return( 0 );
214         }
215
216         return( be->be_rootdn ? strcasecmp( be->be_rootdn, dn ) == 0
217             : 0 );
218 }
219
220 int
221 be_isroot_pw( Backend *be, char *dn, struct berval *cred )
222 {
223         if ( ! be_isroot( be, dn ) || be->be_rootpw == NULL ) {
224                 return( 0 );
225         }
226
227         return( strcmp( be->be_rootpw, cred->bv_val ) == 0 );
228 }
229
230 void
231 be_close()
232 {
233         int     i;
234
235         for ( i = 0; i < nbackends; i++ ) {
236                 if ( backends[i].be_close != NULL ) {
237                         (*backends[i].be_close)( &backends[i] );
238                 }
239         }
240 }
241
242
243 void
244 be_unbind(
245         Connection   *conn,
246         Operation    *op
247 )
248 {
249         int     i;
250
251         for ( i = 0; i < nbackends; i++ ) {
252                 if ( backends[i].be_unbind != NULL ) {
253                         (*backends[i].be_unbind)( &backends[i], conn, op );
254                 }
255         }
256 }
257
258 #ifdef ACLGROUP
259 int 
260 be_group(Backend *be, char *bdn, char *edn)
261 {
262         if (be->be_group)
263                 return(be->be_group(be, bdn, edn));
264         else
265                 return(1);
266 }
267 #endif