1 /* cvm/cvm-mysql.c - MySQL CVM
2  * Copyright (C) 2010  Bruce Guenter <bruce@untroubled.org>
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 2 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, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18 #include <mysql/errmsg.h>
19 #include <mysql/mysql.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <bglibs/str.h>
23 #include "module.h"
24 #include "sql.h"
25 
26 const char program[] = "cvm-mysql";
27 
28 const char sql_query_var[] = "CVM_MYSQL_QUERY";
29 const char sql_pwcmp_var[] = "CVM_MYSQL_PWCMP";
30 const char sql_postq_var[] = "CVM_MYSQL_POSTQ";
31 
32 static MYSQL mysql;
33 
34 static const char* host;
35 static const char* user;
36 static const char* pass;
37 static const char* db;
38 static unsigned port;
39 static const char* unix_socket;
40 
do_connect(void)41 static int do_connect(void)
42 {
43   if (!mysql_real_connect(&mysql, host, user, pass, db,
44 			  port, unix_socket, 0)) return CVME_IO;
45   return 0;
46 }
47 
sql_auth_init(void)48 int sql_auth_init(void)
49 {
50   const char* tmp;
51 
52   host = getenv("CVM_MYSQL_HOST");
53   user = getenv("CVM_MYSQL_USER");
54   pass = getenv("CVM_MYSQL_PASS");
55   db = getenv("CVM_MYSQL_DB");
56   tmp = getenv("CVM_MYSQL_PORT");
57   port = tmp ? atoi(tmp) : 0;
58   unix_socket = getenv("CVM_MYSQL_SOCKET");
59 
60   mysql_init(&mysql);
61   if ((tmp = getenv("CVM_MYSQL_DEFAULT_FILE")) != 0)
62     if (mysql_options(&mysql, MYSQL_READ_DEFAULT_FILE, tmp))
63       return CVME_CONFIG;
64   if ((tmp = getenv("CVM_MYSQL_DEFAULT_GROUP")) != 0)
65     if (mysql_options(&mysql, MYSQL_READ_DEFAULT_GROUP, tmp))
66       return CVME_CONFIG;
67   return do_connect();
68 }
69 
sql_post_query(const str * query)70 int sql_post_query(const str* query)
71 {
72   int i;
73   if (mysql_real_query(&mysql, query->s, query->len) == 0) return 0;
74   if (mysql_errno(&mysql) != CR_SERVER_LOST) return CVME_IO | CVME_FATAL;
75   mysql_close(&mysql);
76   if ((i = do_connect()) != 0) return i;
77   if (mysql_real_query(&mysql, query->s, query->len) == 0) return 0;
78   return CVME_IO | CVME_FATAL;
79 }
80 
81 static MYSQL_ROW row;
82 
sql_auth_query(const str * query)83 int sql_auth_query(const str* query)
84 {
85   int i;
86   static MYSQL_RES* result = 0;
87   if ((i = sql_post_query(query)) != 0) return -i;
88   if (result != 0) mysql_free_result(result);
89   result = mysql_store_result(&mysql);
90   row = mysql_fetch_row(result);
91   return mysql_num_rows(result);
92 }
93 
sql_get_field(int field)94 const char* sql_get_field(int field)
95 {
96   return row[field];
97 }
98 
sql_auth_stop(void)99 void sql_auth_stop(void)
100 {
101   mysql_close(&mysql);
102 }
103