]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/init.c
d14af1190a50cb5d50495824954829575cbb7563
[openldap] / servers / slapd / back-sql / init.c
1 /*
2  *       Copyright 1999, Dmitry Kovalev (zmit@mail.ru), 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 #include <stdio.h>
13 #include <sys/types.h>
14 #include "slap.h"
15 #include "back-sql.h"
16 #include "sql-wrap.h"
17 #include "schema-map.h"
18 #include "util.h"
19
20 #ifdef SLAPD_SQL_DYNAMIC
21
22 int backsql_LTX_init_module(int argc, char *argv[]) {
23     BackendInfo bi;
24
25     memset( &bi, 0, sizeof(bi) );
26     bi.bi_type = "sql";
27     bi.bi_init = backbacksql_initialize;
28
29     backend_add(&bi);
30     return 0;
31 }
32
33 #endif /* SLAPD_SHELL_DYNAMIC */
34
35 int sql_back_initialize(
36     BackendInfo *bi
37 )
38
39  Debug(LDAP_DEBUG_TRACE,"==>backsql_initialize()\n",0,0,0);
40         bi->bi_open = 0;
41         bi->bi_config = 0;
42         bi->bi_close = 0;
43         bi->bi_destroy = 0;
44
45         bi->bi_db_init = backsql_db_init;
46         bi->bi_db_config = backsql_db_config;
47         bi->bi_db_open = backsql_db_open;
48         bi->bi_db_close = backsql_db_close;
49         bi->bi_db_destroy = backsql_db_destroy;
50
51 #ifdef BACKSQL_ALL_DONE
52         bi->bi_op_abandon = backsql_abandon;
53         bi->bi_op_compare = backsql_compare;
54 #else
55         bi->bi_op_abandon = 0;
56         bi->bi_op_compare = 0;
57 #endif
58         bi->bi_op_bind = backsql_bind;
59         bi->bi_op_unbind = backsql_unbind;
60         bi->bi_op_search = backsql_search;
61         bi->bi_op_modify = backsql_modify;
62         bi->bi_op_modrdn = backsql_modrdn;
63         bi->bi_op_add = backsql_add;
64         bi->bi_op_delete = backsql_delete;
65         
66         bi->bi_acl_group = 0;
67  
68         bi->bi_connection_init = 0;
69         bi->bi_connection_destroy = 0;
70         
71         Debug(LDAP_DEBUG_TRACE,"<==backsql_initialize()\n",0,0,0);
72         return 0;
73 }
74
75
76 int backsql_destroy ( BackendInfo *bi )
77 {
78  Debug(LDAP_DEBUG_TRACE,"==>backsql_destroy()\n",0,0,0);
79  Debug(LDAP_DEBUG_TRACE,"<==backsql_destroy()\n",0,0,0);
80  return 0;
81 }
82
83 int backsql_db_init(BackendDB *bd)
84 {
85  backsql_info *si;
86  
87  Debug(LDAP_DEBUG_TRACE,"==>backsql_db_init()\n",0,0,0);
88  si = (backsql_info *) ch_calloc( 1, sizeof(backsql_info) );
89  ldap_pvt_thread_mutex_init(&si->dbconn_mutex);
90  ldap_pvt_thread_mutex_init(&si->schema_mutex);
91  backsql_init_db_env(si);
92  
93  bd->be_private=si;
94  Debug(LDAP_DEBUG_TRACE,"<==backsql_db_init()\n",0,0,0);
95  return 0;
96 }
97
98 int backsql_db_destroy(BackendDB *bd)
99 {
100  backsql_info *si=(backsql_info*)bd->be_private;
101  
102  Debug(LDAP_DEBUG_TRACE,"==>backsql_db_destroy()\n",0,0,0);
103  ldap_pvt_thread_mutex_lock(&si->dbconn_mutex);
104  backsql_free_db_env(si);
105  ldap_pvt_thread_mutex_unlock(&si->dbconn_mutex);
106  ldap_pvt_thread_mutex_lock(&si->schema_mutex);
107  backsql_destroy_schema_map(si);
108  ldap_pvt_thread_mutex_unlock(&si->schema_mutex);
109  ldap_pvt_thread_mutex_destroy(&si->schema_mutex);
110  ldap_pvt_thread_mutex_destroy(&si->dbconn_mutex);
111  free(si->dbname);
112  free(si->dbuser);
113  if (si->dbpasswd)
114   free(si->dbpasswd);
115  if (si->dbhost)
116   free(si->dbhost);
117  free(si->subtree_cond);
118  free(si->oc_query);
119  free(si->at_query);
120  free(si->insentry_query);
121  free(si->delentry_query);
122  free(si);
123  Debug(LDAP_DEBUG_TRACE,"<==backsql_db_destroy()\n",0,0,0);
124  return 0;
125 }
126
127 int backsql_db_open (BackendDB *bd)
128 {
129  backsql_info *si=(backsql_info*)bd->be_private;
130  Connection tmp;
131  SQLHDBC dbh;
132
133  Debug(LDAP_DEBUG_TRACE,"==>backsql_db_open(): testing RDBMS connection\n",0,0,0);
134  if (si->dbname==NULL)
135  {
136   Debug(LDAP_DEBUG_TRACE,"backsql_db_open(): datasource name not specified (use dbname directive in slapd.conf)\n",0,0,0);
137   return 1;
138  }
139  if (si->dbuser==NULL)
140  {
141   Debug(LDAP_DEBUG_TRACE,"backsql_db_open(): user name not specified (use dbuser directive in slapd.conf)\n",0,0,0);
142   return 1;
143  }
144  if (si->subtree_cond==NULL)
145  {
146   Debug(LDAP_DEBUG_TRACE,"backsql_db_open(): subtree search SQL condition not specified (use subtree_cond directive in slapd.conf)\n",0,0,0);
147   Debug(LDAP_DEBUG_TRACE,"backsql_db_open(): setting '%s' as default\n",backsql_def_subtree_cond,0,0);
148   si->subtree_cond=strdup(backsql_def_subtree_cond);
149  }
150  if (si->oc_query==NULL)
151  {
152   Debug(LDAP_DEBUG_TRACE,"backsql_db_open(): objectclass mapping SQL statement not specified (use oc_query directive in slapd.conf)\n",0,0,0);
153   Debug(LDAP_DEBUG_TRACE,"backsql_db_open(): setting '%s' by default\n",backsql_def_oc_query,0,0);
154   si->oc_query=strdup(backsql_def_oc_query);
155  }
156  if (si->at_query==NULL)
157  {
158   Debug(LDAP_DEBUG_TRACE,"backsql_db_open(): attribute mapping SQL statement not specified (use at_query directive in slapd.conf)\n",0,0,0);
159   Debug(LDAP_DEBUG_TRACE,"backsql_db_open(): setting '%s' by default\n",backsql_def_at_query,0,0);
160   si->at_query=strdup(backsql_def_at_query);
161  }
162  if (si->insentry_query==NULL)
163  {
164   Debug(LDAP_DEBUG_TRACE,"backsql_db_open(): entry insertion SQL statement not specified (use insentry_query directive in slapd.conf)\n",0,0,0);
165   Debug(LDAP_DEBUG_TRACE,"backsql_db_open(): setting '%s' by default\n",backsql_def_insentry_query,0,0);
166   si->insentry_query=strdup(backsql_def_insentry_query);
167  }
168  if (si->delentry_query==NULL)
169  {
170   Debug(LDAP_DEBUG_TRACE,"backsql_db_open(): entry deletion SQL statement not specified (use delentry_query directive in slapd.conf)\n",0,0,0);
171   Debug(LDAP_DEBUG_TRACE,"backsql_db_open(): setting '%s' by default\n",backsql_def_delentry_query,0,0);
172   si->delentry_query=strdup(backsql_def_delentry_query);
173  }
174  tmp.c_connid=-1;
175  dbh=backsql_get_db_conn(bd,&tmp);
176  if (!dbh)
177  {
178   Debug(LDAP_DEBUG_TRACE,"backsql_db_open(): connection failed, exiting\n",0,0,0);
179   return 1;
180  }
181  backsql_free_db_conn(bd,&tmp);
182  Debug(LDAP_DEBUG_TRACE,"<==backsql_db_open(): test succeeded, schema map loaded\n",0,0,0);
183  return 0;
184 }
185
186 int backsql_db_close(BackendDB *bd)
187 {
188  Debug(LDAP_DEBUG_TRACE,"==>backsql_db_close()\n",0,0,0);
189  Debug(LDAP_DEBUG_TRACE,"<==backsql_db_close()\n",0,0,0);
190  return 0;
191 }