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