OpenDNSSEC-enforcer  1.3.9
database_connection_mysql.c
Go to the documentation of this file.
1 /*
2  * $Id: database_connection_mysql.c 4150 2010-11-01 11:55:30Z rb $
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 /*+
52  * DbConnect - Connect to Database
53  *
54  * Description:
55  * Creates a connection to the specified database using the parameters
56  * supplied. If successful, the handle to the connection is stored
57  * locally, for retrieval by DbHandle().
58  *
59  * Should there be an error, a suitable message is output.
60  *
61  * Arguments:
62  * DB_HANDLE* dbhandle
63  * Address of a location into which the connection handle is put. This
64  * is also stored locally for retrieval by DbHandle(). If this argument
65  * is NULL, no handle is returned through the function call.
66  *
67  * Note that if a handle for an active connection is already stored
68  * locally, this function will overwrite it, regardless of success or
69  * failure.
70  *
71  * const char* database
72  * name of database (NULL to pick up the default).
73  *
74  * ...
75  * Optional arguments.
76  *
77  * For the MySql implementation, the following additional arguments are
78  * required:
79  *
80  * const char* host
81  * Host to which to connect.
82  *
83  * const char* password
84  * Associated password
85  *
86  * const char* user
87  * Username under which to connect.
88  *
89  * Returns:
90  * int
91  * 0 Success
92  * Other Error on connection. The message will have been logged via
93  * the MsgLog() function.
94 -*/
95 
96 int DbConnect(DB_HANDLE* dbhandle, const char* database, ...)
97 {
98  MYSQL* connection = NULL; /* Local database handle */
99  MYSQL* ptrstatus = NULL; /* Status return when pointer is returned */
100  const char* host = NULL; /* Host on which database resides */
101  const char* password = NULL; /* Connection password */
102  const char* user = NULL; /* Connection username */
103  const char* char_port = NULL; /* Char version of connection port */
104  unsigned int port = 0; /* For mysql_real_connect */
105  va_list ap; /* Argument pointer */
106  int status = 0; /* Return status */
107 
108  /* Initialize if not already done so */
109 
110  DbInit();
111 
112  /* Get arguments */
113 
114  va_start(ap, database);
115  host = va_arg(ap, const char*);
116  password = va_arg(ap, const char*);
117  user = va_arg(ap, const char*);
118  char_port = va_arg(ap, const char*);
119  va_end(ap);
120 
121  /* Convert the port, we will leave it as 0 if there is nothing set */
122  if (char_port != NULL) {
123  status = StrStrtoui(char_port, &port);
124 
125  if (status != 0) {
126  MsgLog(DBS_CONNFAIL, "Could not convert port number");
127  return status;
128  }
129  }
130 
131  /* ... and connect */
132 
133  connection = mysql_init(NULL);
134  if (connection) {
135 
136  /* Connect to the database */
137 
138  ptrstatus = mysql_real_connect(connection, host, user, password,
139  database, port, NULL, CLIENT_INTERACTIVE);
140  if (ptrstatus) {
141 
142  /* Enable autocommit */
143 
144  status = mysql_autocommit(connection, 1);
145  if (status != 0) {
146  status = MsgLog(DBS_AUTOCOMM, mysql_error(connection));
147  }
148  }
149  else {
150 
151  /* Unable to connect */
152 
153  status = MsgLog(DBS_CONNFAIL, mysql_error(connection));
154  }
155  }
156  else {
157 
158  /* Unable to initialize MySql structure */
159 
160  status = MsgLog(DBS_INITFAIL);
161  }
162 
163  /* Store the returned handle for retrieval by DbHandle() */
164 
165  m_dbhandle = connection;
166 
167  /* ... and pass back to the caller via the argument list */
168 
169  if (dbhandle) {
170  *dbhandle = (DB_HANDLE) connection;
171  }
172 
173  /* Check the version against what we have in database.h */
174  if (status == 0) {
175  status = db_version_check();
176  }
177 
178  return status;
179 }
180 
181 
182 /*+
183  * DbDisconnect - Disconnect from Database
184  *
185  * Description:
186  * Disconnects from the current database. If there is no current database,
187  * this is a no-op.
188  *
189  * Arguments:
190  * DB_HANDLE dbhandle
191  * Pointer to the connection handle. After this function is called,
192  * the handle is invalid.
193  *
194  * If the handle passed to this function is the same as the one stored
195  * locally (and returned by DbHandle()), then the local copy is zeroed.
196  *
197  * Returns:
198  * int
199  * Status return. One of:
200  *
201  * 0 Success
202  * DBS_NOTCONN Not connected to a database
203  * None.
204 -*/
205 
206 int DbDisconnect(DB_HANDLE dbhandle)
207 {
208  int status = 0; /* Return status */
209 
210  if (dbhandle) {
211  if (dbhandle == m_dbhandle) {
212  m_dbhandle = NULL;
213  }
214  mysql_close((MYSQL*) dbhandle);
215  mysql_library_end();
216  }
217  else {
218  status = MsgLog(DBS_NOTCONN);
219  }
220 
221  return status;
222 }
223 
224 
225 
226 /*+
227  * DbConnected - Check if Connected to a Database
228  *
229  * Description:
230  * Interrogates the connection status.
231  *
232  * Arguments:
233  * DB_HANDLE dbhandle
234  * Handle to the connection.
235  *
236  * Returns:
237  * int
238  * true if connected to a database, false otherwise.
239 -*/
240 
241 int DbConnected(DB_HANDLE dbhandle)
242 {
243  return dbhandle != NULL;
244 }
245 
246 
247 
248 /*+
249  * DbCheckConnected - Check If Connected
250  *
251  * Description:
252  * Checks if connected to the database, and if not, outputs an error.
253  *
254  * Arguments:
255  * DB_HANDLE dbhandle
256  * Handle to the connection.
257  *
258  * Returns:
259  * int
260  * 1 if connected, 0 if not.
261 -*/
262 
264 {
265  int connected;
266 
267  connected = DbConnected(dbhandle);
268  if (! connected) {
270  }
271 
272  return connected;
273 }
274 
275 
276 /*+
277  * DbHandle - Return Database Handle
278  *
279  * Description:
280  * Returns the handle to the database (the pointer to the MYSQL
281  * structure).
282  *
283  * Arguments:
284  * None.
285  *
286  * Returns:
287  * DB_HANDLE
288  * Database handle, which is NULL if none is stored.
289 -*/
290 
292 {
293  return (DB_HANDLE) m_dbhandle;
294 }