]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/init.c
8005887e8c83fc3de1cadcf6eead10490a7afa98
[openldap] / servers / slapd / back-ldap / init.c
1 /* init.c - initialize ldap backend */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2003 The OpenLDAP Foundation.
6  * Portions Copyright 1999-2003 Howard Chu.
7  * Portions Copyright 2000-2003 Pierangelo Masarati.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted only as authorized by the OpenLDAP
12  * Public License.
13  *
14  * A copy of this license is available in the file LICENSE in the
15  * top-level directory of the distribution or, alternatively, at
16  * <http://www.OpenLDAP.org/license.html>.
17  */
18 /* ACKNOWLEDGEMENTS:
19  * This work was initially developed by the Howard Chu for inclusion
20  * in OpenLDAP Software and subsequently enhanced by Pierangelo
21  * Masarati.
22  */
23
24 #include "portable.h"
25
26 #include <stdio.h>
27
28 #include <ac/string.h>
29 #include <ac/socket.h>
30
31 #include "slap.h"
32 #include "back-ldap.h"
33
34 #ifdef SLAPD_LDAP_DYNAMIC
35
36 int init_module(int argc, char *argv[]) {
37     BackendInfo bi;
38
39     memset( &bi, '\0', sizeof(bi) );
40     bi.bi_type = "ldap";
41     bi.bi_init = ldap_back_initialize;
42
43     backend_add(&bi);
44     return 0;
45 }
46
47 #endif /* SLAPD_LDAP_DYNAMIC */
48
49 int
50 ldap_back_initialize(
51     BackendInfo *bi
52 )
53 {
54         bi->bi_controls = slap_known_controls;
55
56         bi->bi_open = 0;
57         bi->bi_config = 0;
58         bi->bi_close = 0;
59         bi->bi_destroy = 0;
60
61         bi->bi_db_init = ldap_back_db_init;
62         bi->bi_db_config = ldap_back_db_config;
63         bi->bi_db_open = 0;
64         bi->bi_db_close = 0;
65         bi->bi_db_destroy = ldap_back_db_destroy;
66
67         bi->bi_op_bind = ldap_back_bind;
68         bi->bi_op_unbind = 0;
69         bi->bi_op_search = ldap_back_search;
70         bi->bi_op_compare = ldap_back_compare;
71         bi->bi_op_modify = ldap_back_modify;
72         bi->bi_op_modrdn = ldap_back_modrdn;
73         bi->bi_op_add = ldap_back_add;
74         bi->bi_op_delete = ldap_back_delete;
75         bi->bi_op_abandon = 0;
76
77         bi->bi_extended = ldap_back_extended;
78
79         bi->bi_chk_referrals = 0;
80         bi->bi_entry_get_rw = ldap_back_entry_get;
81
82         bi->bi_connection_init = 0;
83         bi->bi_connection_destroy = ldap_back_conn_destroy;
84
85         ldap_chain_setup();
86
87         return 0;
88 }
89
90 int
91 ldap_back_db_init(
92     Backend     *be
93 )
94 {
95         struct ldapinfo *li;
96         struct ldapmapping *mapping;
97
98         li = (struct ldapinfo *) ch_calloc( 1, sizeof(struct ldapinfo) );
99         if ( li == NULL ) {
100                 return -1;
101         }
102
103         li->binddn.bv_val = NULL;
104         li->binddn.bv_len = 0;
105         li->bindpw.bv_val = NULL;
106         li->bindpw.bv_len = 0;
107
108 #ifdef ENABLE_REWRITE
109         li->rwmap.rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
110         if ( li->rwmap.rwm_rw == NULL ) {
111                 ch_free( li );
112                 return -1;
113         }
114 #endif /* ENABLE_REWRITE */
115
116         ldap_pvt_thread_mutex_init( &li->conn_mutex );
117
118         ldap_back_map_init( &li->rwmap.rwm_oc, &mapping );
119         ldap_back_map_init( &li->rwmap.rwm_at, &mapping );
120
121         li->be = be;
122         be->be_private = li;
123         be->be_flags |= SLAP_BFLAG_NOLASTMOD;
124
125         return 0;
126 }
127
128 void
129 ldap_back_conn_free( 
130         void *v_lc
131 )
132 {
133         struct ldapconn *lc = v_lc;
134         ldap_unbind( lc->ld );
135         if ( lc->bound_dn.bv_val ) {
136                 ch_free( lc->bound_dn.bv_val );
137         }
138         if ( lc->cred.bv_val ) {
139                 memset( lc->cred.bv_val, 0, lc->cred.bv_len );
140                 ch_free( lc->cred.bv_val );
141         }
142         if ( lc->local_dn.bv_val ) {
143                 ch_free( lc->local_dn.bv_val );
144         }
145         ldap_pvt_thread_mutex_destroy( &lc->lc_mutex );
146         ch_free( lc );
147 }
148
149 void
150 mapping_free( void *v_mapping )
151 {
152         struct ldapmapping *mapping = v_mapping;
153         ch_free( mapping->src.bv_val );
154         ch_free( mapping->dst.bv_val );
155         ch_free( mapping );
156 }
157
158 int
159 ldap_back_db_destroy(
160     Backend     *be
161 )
162 {
163         struct ldapinfo *li;
164
165         if (be->be_private) {
166                 li = (struct ldapinfo *)be->be_private;
167
168                 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
169
170                 if (li->url) {
171                         ch_free(li->url);
172                         li->url = NULL;
173                 }
174                 if (li->binddn.bv_val) {
175                         ch_free(li->binddn.bv_val);
176                         li->binddn.bv_val = NULL;
177                 }
178                 if (li->bindpw.bv_val) {
179                         ch_free(li->bindpw.bv_val);
180                         li->bindpw.bv_val = NULL;
181                 }
182                 if (li->conntree) {
183                         avl_free( li->conntree, ldap_back_conn_free );
184                 }
185 #ifdef ENABLE_REWRITE
186                 if (li->rwmap.rwm_rw) {
187                         rewrite_info_delete( &li->rwmap.rwm_rw );
188                 }
189 #else /* !ENABLE_REWRITE */
190                 if (li->rwmap.rwm_suffix_massage) {
191                         ber_bvarray_free( li->rwmap.rwm_suffix_massage );
192                 }
193 #endif /* !ENABLE_REWRITE */
194
195                 avl_free( li->rwmap.rwm_oc.remap, NULL );
196                 avl_free( li->rwmap.rwm_oc.map, mapping_free );
197                 avl_free( li->rwmap.rwm_at.remap, NULL );
198                 avl_free( li->rwmap.rwm_at.map, mapping_free );
199                 
200                 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
201                 ldap_pvt_thread_mutex_destroy( &li->conn_mutex );
202         }
203
204         ch_free( be->be_private );
205         return 0;
206 }