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