| 1 |
/*********************************************************************** |
| 2 |
* |
| 3 |
* cdebconf - An implementation of the Debian Configuration Management |
| 4 |
* System |
| 5 |
* |
| 6 |
* File: src/modules/mysql/mysql.c |
| 7 |
* |
| 8 |
* Description: mysql DB module for cdebconf |
| 9 |
* |
| 10 |
* cdebconf is (c) 2000-2001 Randolph Chung and others under the following |
| 11 |
* license. |
| 12 |
* |
| 13 |
* Redistribution and use in source and binary forms, with or without |
| 14 |
* modification, are permitted provided that the following conditions |
| 15 |
* are met: |
| 16 |
* |
| 17 |
* 1. Redistributions of source code must retain the above copyright |
| 18 |
* notice, this list of conditions and the following disclaimer. |
| 19 |
* |
| 20 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 21 |
* notice, this list of conditions and the following disclaimer in the |
| 22 |
* documentation and/or other materials provided with the distribution. |
| 23 |
* |
| 24 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND |
| 25 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 26 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 27 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 28 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 29 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 30 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 31 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 32 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 33 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 34 |
* SUCH DAMAGE. |
| 35 |
* |
| 36 |
***********************************************************************/ |
| 37 |
#include "common.h" |
| 38 |
#include "configuration.h" |
| 39 |
#include "database.h" |
| 40 |
#include "mysql.h" |
| 41 |
#include "template.h" |
| 42 |
#include "question.h" |
| 43 |
#include "strutl.h" |
| 44 |
|
| 45 |
#include <dirent.h> |
| 46 |
#include <stdio.h> |
| 47 |
#include <stdarg.h> |
| 48 |
#include <string.h> |
| 49 |
#include <sys/types.h> |
| 50 |
|
| 51 |
#define CFGPREFIX "database::driver::mysql::" |
| 52 |
#define DBDATA(db) ((struct dbdata *)db) |
| 53 |
#define DBH(db) &(DBDATA(db)->dbh) |
| 54 |
|
| 55 |
#ifdef __GNUC__ |
| 56 |
static MYSQL_RES *mysql_runquery(struct database *db, const char *fmt, ...) |
| 57 |
__attribute__((format(printf, 2, 3))); |
| 58 |
#endif |
| 59 |
|
| 60 |
static MYSQL_RES *mysql_runquery(struct database *db, const char *fmt, ...) |
| 61 |
{ |
| 62 |
va_list ap; |
| 63 |
char buf[1024]; |
| 64 |
|
| 65 |
va_start(ap, fmt); |
| 66 |
vsnprintf(buf, sizeof(buf), fmt, ap); |
| 67 |
va_end(ap); |
| 68 |
|
| 69 |
INFO(INFO_DEBUG, "SQL: [%s]", buf); |
| 70 |
if (mysql_query(DBH(db), buf) != 0) |
| 71 |
return NULL; |
| 72 |
|
| 73 |
/* Fetch all results */ |
| 74 |
return mysql_store_result(DBH(db)); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
static int mysql_initialize(struct database *db, struct configuration *cfg) |
| 79 |
{ |
| 80 |
struct dbdata *data = NEW(struct dbdata); |
| 81 |
memset(data, 0, sizeof(struct dbdata)); |
| 82 |
db->data = data; |
| 83 |
|
| 84 |
mysql_init(DBH(db)); |
| 85 |
mysql_real_connect(DBH(db), |
| 86 |
cfg->get(cfg, CFGPREFIX "host", 0), |
| 87 |
cfg->get(cfg, CFGPREFIX "user", 0), |
| 88 |
cfg->get(cfg, CFGPREFIX "passwd", 0), |
| 89 |
cfg->get(cfg, CFGPREFIX "database", "DebianConfig"), |
| 90 |
cfg->geti(cfg, CFGPREFIX "port", 0), |
| 91 |
cfg->get(cfg, CFGPREFIX "socket", 0), |
| 92 |
0 |
| 93 |
); |
| 94 |
|
| 95 |
if (mysql_errno(DBH(db)) != 0) |
| 96 |
return DC_NOTOK; |
| 97 |
|
| 98 |
return DC_OK; |
| 99 |
} |
| 100 |
|
| 101 |
static int mysql_shutdownmod(struct database *db) |
| 102 |
{ |
| 103 |
mysql_close(DBH(db)); |
| 104 |
return DC_OK; |
| 105 |
} |
| 106 |
|
| 107 |
static int mysql_load(struct database *db) |
| 108 |
{ |
| 109 |
return DC_OK; |
| 110 |
} |
| 111 |
|
| 112 |
static int mysql_save(struct database *db) |
| 113 |
{ |
| 114 |
return DC_OK; |
| 115 |
} |
| 116 |
|
| 117 |
static int mysql_template_set(struct database *db, struct template *t) |
| 118 |
{ |
| 119 |
return DC_OK; |
| 120 |
} |
| 121 |
|
| 122 |
static struct template *mysql_template_get(struct database *db, |
| 123 |
const char *ltag) |
| 124 |
{ |
| 125 |
struct template *result = NULL; |
| 126 |
return result; |
| 127 |
} |
| 128 |
|
| 129 |
static int mysql_template_remove(struct database *db, const char *tag) |
| 130 |
{ |
| 131 |
return DC_OK; |
| 132 |
} |
| 133 |
|
| 134 |
static struct template *mysql_template_iterate(struct database *db, |
| 135 |
void **iter) |
| 136 |
{ |
| 137 |
return 0; |
| 138 |
} |
| 139 |
|
| 140 |
static int mysql_question_set(struct database *db, struct question *q) |
| 141 |
{ |
| 142 |
return DC_OK; |
| 143 |
} |
| 144 |
|
| 145 |
static struct question *mysql_question_get(struct database *db, |
| 146 |
const char *ltag) |
| 147 |
{ |
| 148 |
struct question *q = NULL; |
| 149 |
return q; |
| 150 |
} |
| 151 |
|
| 152 |
static int mysql_question_disown(struct database *db, const char *tag, |
| 153 |
const char *owner) |
| 154 |
{ |
| 155 |
return DC_OK; |
| 156 |
} |
| 157 |
|
| 158 |
static struct question *mysql_question_iterate(struct database *db, |
| 159 |
void **iter) |
| 160 |
{ |
| 161 |
return 0; |
| 162 |
} |
| 163 |
|
| 164 |
struct database_module debconf_database_module = |
| 165 |
{ |
| 166 |
initialize: mysql_initialize, |
| 167 |
shutdown: mysql_shutdownmod, |
| 168 |
load: mysql_load, |
| 169 |
save: mysql_save, |
| 170 |
|
| 171 |
template_set: mysql_template_set, |
| 172 |
template_get: mysql_template_get, |
| 173 |
template_remove: mysql_template_remove, |
| 174 |
template_iterate: mysql_template_iterate, |
| 175 |
|
| 176 |
question_get: mysql_question_get, |
| 177 |
question_set: mysql_question_set, |
| 178 |
question_disown: mysql_question_disown, |
| 179 |
question_iterate: mysql_question_iterate |
| 180 |
}; |