]> git.sur5r.net Git - openldap/blob - servers/slapd/backend.c
Modified make system to support CVS instead of RCS
[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 #endif
25
26 #ifdef LDAP_PASSWD
27 extern int      passwd_back_search();
28 extern int      passwd_back_config();
29 #endif
30
31 #ifdef LDAP_SHELL
32 extern int      shell_back_bind();
33 extern int      shell_back_unbind();
34 extern int      shell_back_search();
35 extern int      shell_back_compare();
36 extern int      shell_back_modify();
37 extern int      shell_back_modrdn();
38 extern int      shell_back_add();
39 extern int      shell_back_delete();
40 extern int      shell_back_abandon();
41 extern int      shell_back_config();
42 extern int      shell_back_init();
43 #endif
44
45 extern int      defsize;
46 extern int      deftime;
47
48 #define BACKEND_GRAB_SIZE       10
49
50 int             nbackends;
51 Backend         *backends;
52 static int      maxbackends;
53
54 Backend *
55 new_backend(
56     char        *type
57 )
58 {
59         Backend *be;
60         int     foundit;
61
62         if ( nbackends == maxbackends ) {
63                 maxbackends += BACKEND_GRAB_SIZE;
64                 backends = (Backend *) ch_realloc( (char *) backends,
65                     maxbackends * sizeof(Backend) );
66                 memset( &backends[nbackends], '\0', BACKEND_GRAB_SIZE *
67                     sizeof(Backend) );
68         }
69
70         be = &backends[nbackends++];
71         be->be_sizelimit = defsize;
72         be->be_timelimit = deftime;
73         foundit = 0;
74
75 #ifdef LDAP_LDBM
76         if ( strcasecmp( type, "ldbm" ) == 0 ) {
77                 be->be_bind = ldbm_back_bind;
78                 be->be_unbind = ldbm_back_unbind;
79                 be->be_search = ldbm_back_search;
80                 be->be_compare = ldbm_back_compare;
81                 be->be_modify = ldbm_back_modify;
82                 be->be_modrdn = ldbm_back_modrdn;
83                 be->be_add = ldbm_back_add;
84                 be->be_delete = ldbm_back_delete;
85                 be->be_abandon = ldbm_back_abandon;
86                 be->be_config = ldbm_back_config;
87                 be->be_init = ldbm_back_init;
88                 be->be_close = ldbm_back_close;
89                 be->be_type = "ldbm";
90                 foundit = 1;
91         }
92 #endif
93
94 #ifdef LDAP_PASSWD
95         if ( strcasecmp( type, "passwd" ) == 0 ) {
96                 be->be_bind = NULL;
97                 be->be_unbind = NULL;
98                 be->be_search = passwd_back_search;
99                 be->be_compare = NULL;
100                 be->be_modify = NULL;
101                 be->be_modrdn = NULL;
102                 be->be_add = NULL;
103                 be->be_delete = NULL;
104                 be->be_abandon = NULL;
105                 be->be_config = passwd_back_config;
106                 be->be_init = NULL;
107                 be->be_close = NULL;
108                 be->be_type = "passwd";
109                 foundit = 1;
110         }
111 #endif
112
113 #ifdef LDAP_SHELL
114         if ( strcasecmp( type, "shell" ) == 0 ) {
115                 be->be_bind = shell_back_bind;
116                 be->be_unbind = shell_back_unbind;
117                 be->be_search = shell_back_search;
118                 be->be_compare = shell_back_compare;
119                 be->be_modify = shell_back_modify;
120                 be->be_modrdn = shell_back_modrdn;
121                 be->be_add = shell_back_add;
122                 be->be_delete = shell_back_delete;
123                 be->be_abandon = shell_back_abandon;
124                 be->be_config = shell_back_config;
125                 be->be_init = shell_back_init;
126                 be->be_close = NULL;
127                 be->be_type = "shell";
128                 foundit = 1;
129         }
130 #endif
131
132         if ( be->be_init != NULL ) {
133                 (*be->be_init)( be );
134         }
135
136         if ( foundit == 0 ) {
137                 fprintf( stderr, "Unrecognized database type (%s)\n", type );
138                 exit( 1 );
139         }
140
141         return( be );
142 }
143
144 Backend *
145 select_backend( char * dn )
146 {
147         int     i, j, len, dnlen;
148
149         dnlen = strlen( dn );
150         for ( i = 0; i < nbackends; i++ ) {
151                 for ( j = 0; backends[i].be_suffix != NULL &&
152                     backends[i].be_suffix[j] != NULL; j++ ) {
153                         len = strlen( backends[i].be_suffix[j] );
154
155                         if ( len > dnlen ) {
156                                 continue;
157                         }
158
159                         if ( strcasecmp( backends[i].be_suffix[j],
160                             dn + (dnlen - len) ) == 0 ) {
161                                 return( &backends[i] );
162                         }
163                 }
164         }
165
166         return( NULL );
167 }
168
169 int
170 be_issuffix(
171     Backend     *be,
172     char        *suffix
173 )
174 {
175         int     i;
176
177         for ( i = 0; be->be_suffix != NULL && be->be_suffix[i] != NULL; i++ ) {
178                 if ( strcasecmp( be->be_suffix[i], suffix ) == 0 ) {
179                         return( 1 );
180                 }
181         }
182
183         return( 0 );
184 }
185
186 int
187 be_isroot( Backend *be, char *dn )
188 {
189         if ( dn == NULL ) {
190                 return( 0 );
191         }
192
193         return( be->be_rootdn ? strcasecmp( be->be_rootdn, dn ) == 0
194             : 0 );
195 }
196
197 int
198 be_isroot_pw( Backend *be, char *dn, struct berval *cred )
199 {
200         if ( ! be_isroot( be, dn ) || be->be_rootpw == NULL ) {
201                 return( 0 );
202         }
203
204         return( strcmp( be->be_rootpw, cred->bv_val ) == 0 );
205 }
206
207 void
208 be_close()
209 {
210         int     i;
211
212         for ( i = 0; i < nbackends; i++ ) {
213                 if ( backends[i].be_close != NULL ) {
214                         (*backends[i].be_close)( &backends[i] );
215                 }
216         }
217 }
218
219
220 void
221 be_unbind(
222         Connection   *conn,
223         Operation    *op
224 )
225 {
226         int     i;
227
228         for ( i = 0; i < nbackends; i++ ) {
229                 if ( backends[i].be_unbind != NULL ) {
230                         (*backends[i].be_unbind)( &backends[i], conn, op );
231                 }
232         }
233 }