fixed renameAccount function (merging mode)
[NeonServV5.git] / src / mysqlConn.c
1 /* mysqlConn.c - NeonServ v5.6
2  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17
18 #include "mysqlConn.h"
19 #include "ConfigParser.h"
20 #define DATABASE_VERSION "20"
21
22 static void show_mysql_error();
23
24 struct mysql_conn_struct {
25     unsigned int tid;
26     MYSQL *mysql_conn;
27     struct used_result *used_results;
28     struct escaped_string *escaped_strings;
29     struct mysql_conn_struct *next;
30 };
31
32 struct used_result {
33     MYSQL_RES *result;
34     struct used_result *next;
35 };
36
37 struct escaped_string {
38     char *string;
39     struct escaped_string *next;
40 };
41
42 struct mysql_conn_struct *get_mysql_conn_struct();
43
44 struct mysql_conn_struct *mysql_conns = NULL;
45 static int mysql_serverport;
46 static char *mysql_host, *mysql_user, *mysql_pass, *mysql_base;
47
48 #ifdef HAVE_THREADS
49 static pthread_mutex_t synchronized;
50 #endif
51
52 static void check_mysql() {
53     MYSQL *mysql_conn = get_mysql_conn();
54     int errid;
55     if((errid = mysql_ping(mysql_conn))) {
56         if(mysql_errno(mysql_conn) == CR_SERVER_GONE_ERROR) {
57             if(!mysql_real_connect(mysql_conn, mysql_host, mysql_user, mysql_pass, mysql_base, mysql_serverport, NULL, 0)) {
58                 show_mysql_error();
59             }
60         } else {
61             //mysql error
62             show_mysql_error();
63         }
64     }
65 }
66
67 MYSQL_RES *mysql_use() {
68     struct mysql_conn_struct *mysql_conn = get_mysql_conn_struct();
69     MYSQL_RES *res = mysql_store_result(mysql_conn->mysql_conn);
70     struct used_result *result = malloc(sizeof(*result));
71     if (!result) {
72         mysql_free_result(res);
73         return NULL;
74     }
75     result->result = res;
76     result->next = mysql_conn->used_results;
77     mysql_conn->used_results = result;
78     return res;
79 }
80
81 void mysql_free() {
82     struct mysql_conn_struct *mysql_conn = get_mysql_conn_struct();
83     if(!mysql_conn) return;
84     struct used_result *result, *next_result;
85     for(result = mysql_conn->used_results; result; result = next_result) {
86         next_result = result->next;
87         mysql_free_result(result->result);
88         free(result);
89     }
90     mysql_conn->used_results = NULL;
91     struct escaped_string *escaped, *next_escaped;
92     for(escaped = mysql_conn->escaped_strings; escaped; escaped = next_escaped) {
93         next_escaped = escaped->next;
94         free(escaped->string);
95         free(escaped);
96     }
97     mysql_conn->escaped_strings = NULL;
98 }
99
100 int reload_mysql() {
101     char *new_mysql_host = get_string_field("MySQL.host");
102     char *new_mysql_user = get_string_field("MySQL.user");
103     char *new_mysql_pass = get_string_field("MySQL.pass");
104     char *new_mysql_base = get_string_field("MySQL.base");
105     if(!(new_mysql_host && new_mysql_user && new_mysql_pass && new_mysql_base))
106         return 0;
107     
108     //replace login data
109     if(mysql_host)
110         free(mysql_host);
111     mysql_host = strdup(new_mysql_host);
112     
113     if(mysql_user)
114         free(mysql_user);
115     mysql_user = strdup(new_mysql_user);
116     
117     if(mysql_pass)
118         free(mysql_pass);
119     mysql_pass = strdup(new_mysql_pass);
120     
121     if(mysql_base)
122         free(mysql_base);
123     mysql_base = strdup(new_mysql_base);
124     
125     mysql_serverport = get_int_field("MySQL.port");
126     if(!mysql_serverport)
127         mysql_serverport = 3306;
128     return 1;
129 }
130
131 void init_mysql() {
132     THREAD_MUTEX_INIT(synchronized);
133     
134     MYSQL *mysql_conn = get_mysql_conn();
135     
136     //check database version...
137     int version = 0;
138     if(!mysql_query(mysql_conn, "SELECT `database_version` FROM `version`")) {
139         MYSQL_RES *res = mysql_use();
140         MYSQL_ROW row;
141         if((row = mysql_fetch_row(res))) {
142             version = atoi(row[0]);
143         }
144     }
145     if(!version) {
146         //CREATE DATABASE
147         FILE *f = fopen("database.sql", "r");
148         mysql_set_server_option(mysql_conn, MYSQL_OPTION_MULTI_STATEMENTS_ON);
149         if (f) {
150             char line[512];
151             char query_buffer[8192];
152             int query_buffer_pos = 0;
153             while (fgets(line, sizeof(line), f)) {
154                 query_buffer_pos += sprintf(query_buffer + query_buffer_pos, " %s", line);
155                 if(line[(strlen(line) - 2)] == ';') {
156                     if(mysql_query(mysql_conn, query_buffer))
157                         show_mysql_error();
158                     query_buffer_pos = 0;
159                 }
160             }
161             fclose(f);
162         }
163         f = fopen("database.defaults.sql", "r");
164         if (f) {
165             char line[4096];
166             char query_buffer[131072];
167             int query_buffer_pos = 0;
168             while (fgets(line, sizeof(line), f)) {
169                 query_buffer_pos += sprintf(query_buffer + query_buffer_pos, " %s", line);
170                 if(line[(strlen(line) - 2)] == ';') {
171                     if(mysql_query(mysql_conn, query_buffer))
172                         show_mysql_error();
173                     query_buffer_pos = 0;
174                 }
175             }
176             fclose(f);
177         }
178         do { 
179             MYSQL_RES *res = mysql_store_result(mysql_conn); 
180             mysql_free_result(res); 
181         } while(!mysql_next_result(mysql_conn));
182         mysql_set_server_option(mysql_conn, MYSQL_OPTION_MULTI_STATEMENTS_OFF);
183         mysql_query(mysql_conn, "INSERT INTO `version` (`database_version`) VALUES ('" DATABASE_VERSION "')");
184     }
185     else if(version < atoi(DATABASE_VERSION)) {
186         //UPDATE DATABASE
187         FILE *f = fopen("database.upgrade.sql", "r");
188         mysql_set_server_option(mysql_conn, MYSQL_OPTION_MULTI_STATEMENTS_ON);
189         if (f) {
190             char line[512];
191             char query_buffer[8192];
192             int query_buffer_pos = 0, use_querys = 0;
193             sprintf(query_buffer, "-- version: %d", version);
194             while (fgets(line, sizeof(line), f)) {
195                 if(use_querys) {
196                     query_buffer_pos += sprintf(query_buffer + query_buffer_pos, " %s", line);
197                     if(line[strlen(line) - 1] == ';') {
198                         mysql_query(mysql_conn, query_buffer);
199                         query_buffer_pos = 0;
200                     }
201                 } else if(!stricmplen(query_buffer, line, strlen(query_buffer))) {
202                     use_querys = 1;
203                 }
204             }
205             if(query_buffer_pos) {
206                 if(mysql_query(mysql_conn, query_buffer))
207                     show_mysql_error();
208             }
209             fclose(f);
210         } else
211             perror("database.sql missing!");
212         do { 
213             MYSQL_RES *res = mysql_store_result(mysql_conn); 
214             mysql_free_result(res); 
215         } while(!mysql_next_result(mysql_conn));
216         mysql_set_server_option(mysql_conn, MYSQL_OPTION_MULTI_STATEMENTS_OFF);
217         mysql_query(mysql_conn, "UPDATE `version` SET `database_version` = '" DATABASE_VERSION "'");
218     }
219 }
220
221 void free_mysql() {
222     struct mysql_conn_struct *mysql_conn, *next;
223     for(mysql_conn = mysql_conns; mysql_conn; mysql_conn = next) {
224         next = mysql_conn->next;
225         mysql_close(mysql_conn->mysql_conn);
226         free(mysql_conn);
227     }
228     mysql_conns = NULL;
229 }
230
231 static void show_mysql_error() {
232     MYSQL *mysql_conn = get_mysql_conn();
233     //show mysql_error()
234     putlog(LOGLEVEL_ERROR, "MySQL Error: %s\n", mysql_error(mysql_conn));
235 }
236
237 void printf_mysql_query(const char *text, ...) {
238     MYSQL *mysql_conn = get_mysql_conn();
239     va_list arg_list;
240     char queryBuf[MYSQLMAXLEN];
241     int pos;
242     queryBuf[0] = '\0';
243     va_start(arg_list, text);
244     pos = vsnprintf(queryBuf, MYSQLMAXLEN - 2, text, arg_list);
245     va_end(arg_list);
246     if (pos < 0 || pos > (MYSQLMAXLEN - 2)) pos = MYSQLMAXLEN - 2;
247     queryBuf[pos] = '\0';
248     putlog(LOGLEVEL_MYSQL, "MySQL: %s\n", queryBuf);
249     if(mysql_query(mysql_conn, queryBuf)) {
250         check_mysql();
251         if(mysql_query(mysql_conn, queryBuf)) {
252             show_mysql_error();
253         }
254     }
255 }
256
257 void printf_long_mysql_query(int len, const char *text, ...) {
258     MYSQL *mysql_conn = get_mysql_conn();
259     va_list arg_list;
260     char queryBuf[len];
261     int pos;
262     queryBuf[0] = '\0';
263     va_start(arg_list, text);
264     pos = vsnprintf(queryBuf, len - 2, text, arg_list);
265     va_end(arg_list);
266     if (pos < 0 || pos > (len - 2)) pos = len - 2;
267     queryBuf[pos] = '\0';
268     putlog(LOGLEVEL_MYSQL, "MySQL: %s\n", queryBuf);
269     if(mysql_query(mysql_conn, queryBuf)) {
270         check_mysql();
271         if(mysql_query(mysql_conn, queryBuf)) {
272             show_mysql_error();
273         }
274     }
275 }
276
277 char* escape_string(const char *str) {
278     struct mysql_conn_struct *mysql_conn = get_mysql_conn_struct();
279     struct escaped_string *escapedstr = malloc(sizeof(*escapedstr));
280     if (!escapedstr) {
281         return NULL;
282     }
283     char escaped[strlen(str)*2+1];
284     mysql_real_escape_string(mysql_conn->mysql_conn, escaped, str, strlen(str));
285     escapedstr->string = strdup(escaped);
286     escapedstr->next = mysql_conn->escaped_strings;
287     mysql_conn->escaped_strings = escapedstr;
288     return escapedstr->string;
289 }
290
291 struct mysql_conn_struct *get_mysql_conn_struct() {
292     SYNCHRONIZE(synchronized);
293     struct mysql_conn_struct *mysql_conn;
294     unsigned int tid;
295     #ifdef HAVE_THREADS
296     tid = (unsigned int) pthread_self_tid();
297     #else
298     tid = 1;
299     #endif
300     for(mysql_conn = mysql_conns; mysql_conn; mysql_conn = mysql_conn->next) {
301         if(mysql_conn->tid == tid) {
302             DESYNCHRONIZE(synchronized);
303             return mysql_conn;
304         }
305     }
306     mysql_conn = malloc(sizeof(*mysql_conn));
307     mysql_conn->mysql_conn = mysql_init(NULL);
308     mysql_conn->tid = tid;
309     mysql_conn->used_results = NULL;
310     mysql_conn->escaped_strings = NULL;
311     mysql_conn->next = mysql_conns;
312     mysql_conns = mysql_conn;
313     if (!mysql_real_connect(mysql_conn->mysql_conn, mysql_host, mysql_user, mysql_pass, mysql_base, mysql_serverport, NULL, 0)) {
314         //error
315         show_mysql_error();
316     }
317     DESYNCHRONIZE(synchronized);
318     return mysql_conn;
319 }
320
321 MYSQL *get_mysql_conn() {
322     struct mysql_conn_struct *mysql_conn = get_mysql_conn_struct();
323     return mysql_conn->mysql_conn;
324 }