]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/init.c
Move backend_syncfreq code down into back-ldbm. Creates new configuration
[openldap] / servers / slapd / back-sql / init.c
1 /*
2  *       Copyright 1999, Dmitry Kovalev <mit@openldap.org>, All rights reserved.
3  *
4  *       Redistribution and use in source and binary forms are permitted only
5  *       as authorized by the OpenLDAP Public License.  A copy of this
6  *       license is available at http://www.OpenLDAP.org/license.html or
7  *       in file LICENSE in the top-level directory of the distribution.
8  */
9
10 #include "portable.h"
11
12 #ifdef SLAPD_SQL
13
14 #include <stdio.h>
15 #include <sys/types.h>
16 #include "slap.h"
17 #include "back-sql.h"
18 #include "sql-wrap.h"
19 #include "schema-map.h"
20 #include "util.h"
21
22 #ifdef SLAPD_SQL_DYNAMIC
23
24 int backsql_LTX_init_module(int argc, char *argv[]) {
25     BackendInfo bi;
26
27     memset( &bi, '\0', sizeof(bi) );
28     bi.bi_type = "sql";
29     bi.bi_init = backbacksql_initialize;
30
31     backend_add(&bi);
32     return 0;
33 }
34
35 #endif /* SLAPD_SHELL_DYNAMIC */
36
37 int sql_back_initialize(
38     BackendInfo *bi
39 )
40
41  Debug(LDAP_DEBUG_TRACE,"==>backsql_initialize()\n",0,0,0);
42         bi->bi_open = 0;
43         bi->bi_config = 0;
44         bi->bi_close = 0;
45         bi->bi_destroy = 0;
46
47         bi->bi_db_init = backsql_db_init;
48         bi->bi_db_config = backsql_db_config;
49         bi->bi_db_open = backsql_db_open;
50         bi->bi_db_close = backsql_db_close;
51         bi->bi_db_destroy = backsql_db_destroy;
52         bi->bi_db_sync = 0;
53
54 #ifdef BACKSQL_ALL_DONE
55         bi->bi_op_abandon = backsql_abandon;
56         bi->bi_op_compare = backsql_compare;
57 #else
58         bi->bi_op_abandon = 0;
59         bi->bi_op_compare = 0;
60 #endif
61         bi->bi_op_bind = backsql_bind;
62         bi->bi_op_unbind = backsql_unbind;
63         bi->bi_op_search = backsql_search;
64         bi->bi_op_modify = backsql_modify;
65         bi->bi_op_modrdn = backsql_modrdn;
66         bi->bi_op_add = backsql_add;
67         bi->bi_op_delete = backsql_delete;
68         
69         bi->bi_acl_group = 0;
70         bi->bi_acl_attribute = 0;
71         bi->bi_chk_referrals = 0;
72  
73         bi->bi_connection_init = 0;
74         bi->bi_connection_destroy = backsql_connection_destroy;
75         
76         Debug(LDAP_DEBUG_TRACE,"<==backsql_initialize()\n",0,0,0);
77         return 0;
78 }
79
80
81 int backsql_destroy ( BackendInfo *bi )
82 {
83  Debug(LDAP_DEBUG_TRACE,"==>backsql_destroy()\n",0,0,0);
84  Debug(LDAP_DEBUG_TRACE,"<==backsql_destroy()\n",0,0,0);
85  return 0;
86 }
87
88 int backsql_db_init(BackendDB *bd)
89 {
90  backsql_info *si;
91  
92  Debug(LDAP_DEBUG_TRACE,"==>backsql_db_init()\n",0,0,0);
93  si = (backsql_info *) ch_calloc( 1, sizeof(backsql_info) );
94  ldap_pvt_thread_mutex_init(&si->dbconn_mutex);
95  ldap_pvt_thread_mutex_init(&si->schema_mutex);
96  backsql_init_db_env(si);
97  
98  bd->be_private=si;
99  Debug(LDAP_DEBUG_TRACE,"<==backsql_db_init()\n",0,0,0);
100  return 0;
101 }
102
103 int backsql_db_destroy(BackendDB *bd)
104 {
105  backsql_info *si=(backsql_info*)bd->be_private;
106  
107  Debug(LDAP_DEBUG_TRACE,"==>backsql_db_destroy()\n",0,0,0);
108  ldap_pvt_thread_mutex_lock(&si->dbconn_mutex);
109  backsql_free_db_env(si);
110  ldap_pvt_thread_mutex_unlock(&si->dbconn_mutex);
111  ldap_pvt_thread_mutex_lock(&si->schema_mutex);
112  backsql_destroy_schema_map(si);
113  ldap_pvt_thread_mutex_unlock(&si->schema_mutex);
114  ldap_pvt_thread_mutex_destroy(&si->schema_mutex);
115  ldap_pvt_thread_mutex_destroy(&si->dbconn_mutex);
116  free(si->dbname);
117  free(si->dbuser);
118  if (si->dbpasswd)
119   free(si->dbpasswd);
120  if (si->dbhost)
121   free(si->dbhost);
122  if (si->upper_func)
123   free(si->upper_func);
124  free(si->subtree_cond);
125  free(si->oc_query);
126  free(si->at_query);
127  free(si->insentry_query);
128  free(si->delentry_query);
129  free(si);
130  Debug(LDAP_DEBUG_TRACE,"<==backsql_db_destroy()\n",0,0,0);
131  return 0;
132 }
133
134 int backsql_db_open (BackendDB *bd)
135 {
136  backsql_info *si=(backsql_info*)bd->be_private;
137  Connection tmp;
138  SQLHDBC dbh;
139  int idq_len;
140
141  Debug(LDAP_DEBUG_TRACE,"==>backsql_db_open(): testing RDBMS connection\n",0,0,0);
142  if (si->dbname==NULL)
143  {
144   Debug(LDAP_DEBUG_TRACE,"backsql_db_open(): datasource name not specified (use dbname directive in slapd.conf)\n",0,0,0);
145   return 1;
146  }
147  if (si->dbuser==NULL)
148  {
149   Debug(LDAP_DEBUG_TRACE,"backsql_db_open(): user name not specified (use dbuser directive in slapd.conf)\n",0,0,0);
150   return 1;
151  }
152  if (si->subtree_cond==NULL)
153  {
154   Debug(LDAP_DEBUG_TRACE,"backsql_db_open(): subtree search SQL condition not specified (use subtree_cond directive in slapd.conf)\n",0,0,0);
155   Debug(LDAP_DEBUG_TRACE,"backsql_db_open(): setting '%s' as default\n",backsql_def_subtree_cond,0,0);
156   si->subtree_cond=ch_strdup(backsql_def_subtree_cond);
157  }
158  if (si->oc_query==NULL)
159  {
160   Debug(LDAP_DEBUG_TRACE,"backsql_db_open(): objectclass mapping SQL statement not specified (use oc_query directive in slapd.conf)\n",0,0,0);
161   Debug(LDAP_DEBUG_TRACE,"backsql_db_open(): setting '%s' by default\n",backsql_def_oc_query,0,0);
162   si->oc_query=ch_strdup(backsql_def_oc_query);
163  }
164  if (si->at_query==NULL)
165  {
166   Debug(LDAP_DEBUG_TRACE,"backsql_db_open(): attribute mapping SQL statement not specified (use at_query directive in slapd.conf)\n",0,0,0);
167   Debug(LDAP_DEBUG_TRACE,"backsql_db_open(): setting '%s' by default\n",backsql_def_at_query,0,0);
168   si->at_query=ch_strdup(backsql_def_at_query);
169  }
170  if (si->insentry_query==NULL)
171  {
172   Debug(LDAP_DEBUG_TRACE,"backsql_db_open(): entry insertion SQL statement not specified (use insentry_query directive in slapd.conf)\n",0,0,0);
173   Debug(LDAP_DEBUG_TRACE,"backsql_db_open(): setting '%s' by default\n",backsql_def_insentry_query,0,0);
174   si->insentry_query=ch_strdup(backsql_def_insentry_query);
175  }
176  if (si->delentry_query==NULL)
177  {
178   Debug(LDAP_DEBUG_TRACE,"backsql_db_open(): entry deletion SQL statement not specified (use delentry_query directive in slapd.conf)\n",0,0,0);
179   Debug(LDAP_DEBUG_TRACE,"backsql_db_open(): setting '%s' by default\n",backsql_def_delentry_query,0,0);
180   si->delentry_query=ch_strdup(backsql_def_delentry_query);
181  }
182  si->id_query=NULL;
183  idq_len=0;
184  if (si->upper_func==NULL)
185  {
186   si->id_query=backsql_strcat(si->id_query,&idq_len,backsql_id_query,"dn=?",NULL);
187  }
188  else
189   {
190    si->id_query=backsql_strcat(si->id_query,&idq_len,backsql_id_query,si->upper_func,"(dn)=",si->upper_func,"(?)",NULL);
191   }
192  tmp.c_connid=-1;
193  dbh=backsql_get_db_conn(bd,&tmp);
194  if (!dbh)
195  {
196   Debug(LDAP_DEBUG_TRACE,"backsql_db_open(): connection failed, exiting\n",0,0,0);
197   return 1;
198  }
199  backsql_free_db_conn(bd,&tmp);
200  if (!si->schema_loaded)
201  {
202   Debug(LDAP_DEBUG_TRACE,"backsql_db_open(): test failed, schema map not loaded - exiting\n",0,0,0);
203   return 1;
204  }
205  Debug(LDAP_DEBUG_TRACE,"<==backsql_db_open(): test succeeded, schema map loaded\n",0,0,0);
206  return 0;
207 }
208
209 int backsql_db_close(BackendDB *bd)
210 {
211  Debug(LDAP_DEBUG_TRACE,"==>backsql_db_close()\n",0,0,0);
212  Debug(LDAP_DEBUG_TRACE,"<==backsql_db_close()\n",0,0,0);
213  return 0;
214 }
215
216 int backsql_connection_destroy(BackendDB *be,Connection *conn)
217 {
218  Debug(LDAP_DEBUG_TRACE,"==>backsql_connection_destroy()\n",0,0,0);
219  backsql_free_db_conn(be,conn);
220  Debug(LDAP_DEBUG_TRACE,"<==backsql_connection_destroy()\n",0,0,0);
221  return 0;
222 }
223
224 #endif /* SLAPD_SQL */