OpenDNSSEC-enforcer  1.4.10
database_connection_mysql.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2009 Nominet UK. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
19  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
21  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
23  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  */
26 
27 /*+
28  * database_connection.c - Database Connection Functions
29  *
30  * Description:
31  * Contains the database management functions (such as connect and
32  * disconnect) and holds session-specific database information.
33 -*/
34 
35 #include <stdarg.h>
36 #include <stdlib.h>
37 
38 #include <mysql.h>
39 
40 #include "ksm/database.h"
41 #include "ksm/dbsdef.h"
42 #include "ksm/message.h"
43 #include "ksm/string_util2.h"
44 
45 static MYSQL* m_dbhandle = NULL; /* Non-NULL if connected */
46 
47 
48 /*+
49  * DbConnect - Connect to Database
50  *
51  * Description:
52  * Creates a connection to the specified database using the parameters
53  * supplied. If successful, the handle to the connection is stored
54  * locally, for retrieval by DbHandle().
55  *
56  * Should there be an error, a suitable message is output.
57  *
58  * Arguments:
59  * DB_HANDLE* dbhandle
60  * Address of a location into which the connection handle is put. This
61  * is also stored locally for retrieval by DbHandle(). If this argument
62  * is NULL, no handle is returned through the function call.
63  *
64  * Note that if a handle for an active connection is already stored
65  * locally, this function will overwrite it, regardless of success or
66  * failure.
67  *
68  * const char* database
69  * name of database (NULL to pick up the default).
70  *
71  * ...
72  * Optional arguments.
73  *
74  * For the MySql implementation, the following additional arguments are
75  * required:
76  *
77  * const char* host
78  * Host to which to connect.
79  *
80  * const char* password
81  * Associated password
82  *
83  * const char* user
84  * Username under which to connect.
85  *
86  * Returns:
87  * int
88  * 0 Success
89  * Other Error on connection. The message will have been logged via
90  * the MsgLog() function.
91 -*/
92 
93 int DbConnect(DB_HANDLE* dbhandle, const char* database, ...)
94 {
95  MYSQL* connection = NULL; /* Local database handle */
96  MYSQL* ptrstatus = NULL; /* Status return when pointer is returned */
97  const char* host = NULL; /* Host on which database resides */
98  const char* password = NULL; /* Connection password */
99  const char* user = NULL; /* Connection username */
100  const char* char_port = NULL; /* Char version of connection port */
101  unsigned int port = 0; /* For mysql_real_connect */
102  va_list ap; /* Argument pointer */
103  int status = 0; /* Return status */
104 
105  /* Initialize if not already done so */
106 
107  DbInit();
108 
109  /* Get arguments */
110 
111  va_start(ap, database);
112  host = va_arg(ap, const char*);
113  password = va_arg(ap, const char*);
114  user = va_arg(ap, const char*);
115  char_port = va_arg(ap, const char*);
116  va_end(ap);
117 
118  /* Convert the port, we will leave it as 0 if there is nothing set */
119  if (char_port != NULL) {
120  status = StrStrtoui(char_port, &port);
121 
122  if (status != 0) {
123  MsgLog(DBS_CONNFAIL, "Could not convert port number");
124  return status;
125  }
126  }
127 
128  /* ... and connect */
129 
130  connection = mysql_init(NULL);
131  if (connection) {
132 
133  /* Connect to the database */
134 
135  ptrstatus = mysql_real_connect(connection, host, user, password,
136  database, port, NULL, CLIENT_INTERACTIVE);
137  if (ptrstatus) {
138 
139  /* Enable autocommit */
140 
141  status = mysql_autocommit(connection, 1);
142  if (status != 0) {
143  status = MsgLog(DBS_AUTOCOMM, mysql_error(connection));
144  }
145  }
146  else {
147 
148  /* Unable to connect */
149 
150  status = MsgLog(DBS_CONNFAIL, mysql_error(connection));
151  }
152  }
153  else {
154 
155  /* Unable to initialize MySql structure */
156 
157  status = MsgLog(DBS_INITFAIL);
158  }
159 
160  /* Store the returned handle for retrieval by DbHandle() */
161 
162  m_dbhandle = connection;
163 
164  /* ... and pass back to the caller via the argument list */
165 
166  if (dbhandle) {
167  *dbhandle = (DB_HANDLE) connection;
168  }
169 
170  /* Check the version against what we have in database.h */
171  if (status == 0) {
172  status = db_version_check();
173  }
174 
175  return status;
176 }
177 
178 
179 /*+
180  * DbDisconnect - Disconnect from Database
181  *
182  * Description:
183  * Disconnects from the current database. If there is no current database,
184  * this is a no-op.
185  *
186  * Arguments:
187  * DB_HANDLE dbhandle
188  * Pointer to the connection handle. After this function is called,
189  * the handle is invalid.
190  *
191  * If the handle passed to this function is the same as the one stored
192  * locally (and returned by DbHandle()), then the local copy is zeroed.
193  *
194  * Returns:
195  * int
196  * Status return. One of:
197  *
198  * 0 Success
199  * DBS_NOTCONN Not connected to a database
200  * None.
201 -*/
202 
203 int DbDisconnect(DB_HANDLE dbhandle)
204 {
205  int status = 0; /* Return status */
206 
207  if (dbhandle) {
208  if (dbhandle == m_dbhandle) {
209  m_dbhandle = NULL;
210  }
211  mysql_close((MYSQL*) dbhandle);
212  mysql_library_end();
213  }
214  else {
215  status = MsgLog(DBS_NOTCONN);
216  }
217 
218  return status;
219 }
220 
221 
222 
223 /*+
224  * DbConnected - Check if Connected to a Database
225  *
226  * Description:
227  * Interrogates the connection status.
228  *
229  * Arguments:
230  * DB_HANDLE dbhandle
231  * Handle to the connection.
232  *
233  * Returns:
234  * int
235  * true if connected to a database, false otherwise.
236 -*/
237 
238 int DbConnected(DB_HANDLE dbhandle)
239 {
240  return dbhandle != NULL;
241 }
242 
243 
244 
245 /*+
246  * DbCheckConnected - Check If Connected
247  *
248  * Description:
249  * Checks if connected to the database, and if not, outputs an error.
250  *
251  * Arguments:
252  * DB_HANDLE dbhandle
253  * Handle to the connection.
254  *
255  * Returns:
256  * int
257  * 1 if connected, 0 if not.
258 -*/
259 
261 {
262  int connected;
263 
264  connected = DbConnected(dbhandle);
265  if (! connected) {
267  }
268 
269  return connected;
270 }
271 
272 
273 /*+
274  * DbHandle - Return Database Handle
275  *
276  * Description:
277  * Returns the handle to the database (the pointer to the MYSQL
278  * structure).
279  *
280  * Arguments:
281  * None.
282  *
283  * Returns:
284  * DB_HANDLE
285  * Database handle, which is NULL if none is stored.
286 -*/
287 
289 {
290  return (DB_HANDLE) m_dbhandle;
291 }
sqlite3 * DB_HANDLE
Definition: database.h:77
#define DBS_NOTCONN
Definition: dbsdef.h:53
#define DBS_INITFAIL
Definition: dbsdef.h:47
int db_version_check(void)
#define DBS_CONNFAIL
Definition: dbsdef.h:46
int DbConnect(DB_HANDLE *dbhandle, const char *database,...)
int MsgLog(int status,...)
Definition: message.c:335
int DbDisconnect(DB_HANDLE dbhandle)
void DbInit(void)
#define DBS_AUTOCOMM
Definition: dbsdef.h:44
int StrStrtoui(const char *string, unsigned int *value)
Definition: string_util2.c:549
int DbConnected(DB_HANDLE dbhandle)
int DbCheckConnected(DB_HANDLE dbhandle)
DB_HANDLE DbHandle(void)
#define DBS_NOTCONERR
Definition: dbsdef.h:52