OpenDNSSEC-enforcer 1.3.0
|
00001 /* 00002 * $Id: database_connection_mysql.c 5320 2011-07-12 10:42:26Z jakob $ 00003 * 00004 * Copyright (c) 2008-2009 Nominet UK. All rights reserved. 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions 00008 * are met: 00009 * 1. Redistributions of source code must retain the above copyright 00010 * notice, this list of conditions and the following disclaimer. 00011 * 2. Redistributions in binary form must reproduce the above copyright 00012 * notice, this list of conditions and the following disclaimer in the 00013 * documentation and/or other materials provided with the distribution. 00014 * 00015 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00016 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00017 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00018 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 00019 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00020 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 00021 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00022 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 00023 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 00024 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 00025 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00026 * 00027 */ 00028 00029 /*+ 00030 * database_connection.c - Database Connection Functions 00031 * 00032 * Description: 00033 * Contains the database management functions (such as connect and 00034 * disconnect) and holds session-specific database information. 00035 -*/ 00036 00037 #include <stdarg.h> 00038 #include <stdlib.h> 00039 00040 #include <mysql.h> 00041 00042 #include "ksm/database.h" 00043 #include "ksm/dbsdef.h" 00044 #include "ksm/message.h" 00045 #include "ksm/string_util2.h" 00046 00047 static MYSQL* m_dbhandle = NULL; /* Non-NULL if connected */ 00048 00049 00050 00051 /*+ 00052 * DbConnect - Connect to Database 00053 * 00054 * Description: 00055 * Creates a connection to the specified database using the parameters 00056 * supplied. If successful, the handle to the connection is stored 00057 * locally, for retrieval by DbHandle(). 00058 * 00059 * Should there be an error, a suitable message is output. 00060 * 00061 * Arguments: 00062 * DB_HANDLE* dbhandle 00063 * Address of a location into which the connection handle is put. This 00064 * is also stored locally for retrieval by DbHandle(). If this argument 00065 * is NULL, no handle is returned through the function call. 00066 * 00067 * Note that if a handle for an active connection is already stored 00068 * locally, this function will overwrite it, regardless of success or 00069 * failure. 00070 * 00071 * const char* database 00072 * name of database (NULL to pick up the default). 00073 * 00074 * ... 00075 * Optional arguments. 00076 * 00077 * For the MySql implementation, the following additional arguments are 00078 * required: 00079 * 00080 * const char* host 00081 * Host to which to connect. 00082 * 00083 * const char* password 00084 * Associated password 00085 * 00086 * const char* user 00087 * Username under which to connect. 00088 * 00089 * Returns: 00090 * int 00091 * 0 Success 00092 * Other Error on connection. The message will have been logged via 00093 * the MsgLog() function. 00094 -*/ 00095 00096 int DbConnect(DB_HANDLE* dbhandle, const char* database, ...) 00097 { 00098 MYSQL* connection = NULL; /* Local database handle */ 00099 MYSQL* ptrstatus = NULL; /* Status return when pointer is returned */ 00100 const char* host = NULL; /* Host on which database resides */ 00101 const char* password = NULL; /* Connection password */ 00102 const char* user = NULL; /* Connection username */ 00103 const char* char_port = NULL; /* Char version of connection port */ 00104 unsigned int port = 0; /* For mysql_real_connect */ 00105 va_list ap; /* Argument pointer */ 00106 int status = 0; /* Return status */ 00107 00108 /* Initialize if not already done so */ 00109 00110 DbInit(); 00111 00112 /* Get arguments */ 00113 00114 va_start(ap, database); 00115 host = va_arg(ap, const char*); 00116 password = va_arg(ap, const char*); 00117 user = va_arg(ap, const char*); 00118 char_port = va_arg(ap, const char*); 00119 va_end(ap); 00120 00121 /* Convert the port, we will leave it as 0 if there is nothing set */ 00122 if (char_port != NULL) { 00123 status = StrStrtoui(char_port, &port); 00124 00125 if (status != 0) { 00126 MsgLog(DBS_CONNFAIL, "Could not convert port number"); 00127 return status; 00128 } 00129 } 00130 00131 /* ... and connect */ 00132 00133 connection = mysql_init(NULL); 00134 if (connection) { 00135 00136 /* Connect to the database */ 00137 00138 ptrstatus = mysql_real_connect(connection, host, user, password, 00139 database, port, NULL, CLIENT_INTERACTIVE); 00140 if (ptrstatus) { 00141 00142 /* Enable autocommit */ 00143 00144 status = mysql_autocommit(connection, 1); 00145 if (status != 0) { 00146 status = MsgLog(DBS_AUTOCOMM, mysql_error(connection)); 00147 } 00148 } 00149 else { 00150 00151 /* Unable to connect */ 00152 00153 status = MsgLog(DBS_CONNFAIL, mysql_error(connection)); 00154 } 00155 } 00156 else { 00157 00158 /* Unable to initialize MySql structure */ 00159 00160 status = MsgLog(DBS_INITFAIL); 00161 } 00162 00163 /* Store the returned handle for retrieval by DbHandle() */ 00164 00165 m_dbhandle = connection; 00166 00167 /* ... and pass back to the caller via the argument list */ 00168 00169 if (dbhandle) { 00170 *dbhandle = (DB_HANDLE) connection; 00171 } 00172 00173 /* Check the version against what we have in database.h */ 00174 if (status == 0) { 00175 status = db_version_check(); 00176 } 00177 00178 return status; 00179 } 00180 00181 00182 /*+ 00183 * DbDisconnect - Disconnect from Database 00184 * 00185 * Description: 00186 * Disconnects from the current database. If there is no current database, 00187 * this is a no-op. 00188 * 00189 * Arguments: 00190 * DB_HANDLE dbhandle 00191 * Pointer to the connection handle. After this function is called, 00192 * the handle is invalid. 00193 * 00194 * If the handle passed to this function is the same as the one stored 00195 * locally (and returned by DbHandle()), then the local copy is zeroed. 00196 * 00197 * Returns: 00198 * int 00199 * Status return. One of: 00200 * 00201 * 0 Success 00202 * DBS_NOTCONN Not connected to a database 00203 * None. 00204 -*/ 00205 00206 int DbDisconnect(DB_HANDLE dbhandle) 00207 { 00208 int status = 0; /* Return status */ 00209 00210 if (dbhandle) { 00211 if (dbhandle == m_dbhandle) { 00212 m_dbhandle = NULL; 00213 } 00214 mysql_close((MYSQL*) dbhandle); 00215 mysql_library_end(); 00216 } 00217 else { 00218 status = MsgLog(DBS_NOTCONN); 00219 } 00220 00221 return status; 00222 } 00223 00224 00225 00226 /*+ 00227 * DbConnected - Check if Connected to a Database 00228 * 00229 * Description: 00230 * Interrogates the connection status. 00231 * 00232 * Arguments: 00233 * DB_HANDLE dbhandle 00234 * Handle to the connection. 00235 * 00236 * Returns: 00237 * int 00238 * true if connected to a database, false otherwise. 00239 -*/ 00240 00241 int DbConnected(DB_HANDLE dbhandle) 00242 { 00243 return dbhandle != NULL; 00244 } 00245 00246 00247 00248 /*+ 00249 * DbCheckConnected - Check If Connected 00250 * 00251 * Description: 00252 * Checks if connected to the database, and if not, outputs an error. 00253 * 00254 * Arguments: 00255 * DB_HANDLE dbhandle 00256 * Handle to the connection. 00257 * 00258 * Returns: 00259 * int 00260 * 1 if connected, 0 if not. 00261 -*/ 00262 00263 int DbCheckConnected(DB_HANDLE dbhandle) 00264 { 00265 int connected; 00266 00267 connected = DbConnected(dbhandle); 00268 if (! connected) { 00269 MsgLog(DBS_NOTCONERR); 00270 } 00271 00272 return connected; 00273 } 00274 00275 00276 /*+ 00277 * DbHandle - Return Database Handle 00278 * 00279 * Description: 00280 * Returns the handle to the database (the pointer to the MYSQL 00281 * structure). 00282 * 00283 * Arguments: 00284 * None. 00285 * 00286 * Returns: 00287 * DB_HANDLE 00288 * Database handle, which is NULL if none is stored. 00289 -*/ 00290 00291 DB_HANDLE DbHandle(void) 00292 { 00293 return (DB_HANDLE) m_dbhandle; 00294 }