forked from jgarzik/pushpool
-
Notifications
You must be signed in to change notification settings - Fork 4
/
db-postgresql.c
137 lines (123 loc) · 3.51 KB
/
db-postgresql.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* Copyright 2011 Shane Wegner
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include "autotools-config.h"
#ifdef HAVE_POSTGRESQL
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <libpq-fe.h>
#include "server.h"
#define DEFAULT_STMT_PWDB \
"SELECT password FROM pool_worker WHERE username = $1"
#define DEFAULT_STMT_SHARELOG \
"insert into shares (rem_host, username, our_result, \
upstream_result, reason, solution) values($1, $2, $3, $4, $5, decode($6, 'hex'))"
static bool pg_conncheck(void)
{
if (PQstatus(srv.db_cxn) != CONNECTION_OK) {
applog(LOG_WARNING,
"Connection to PostgreSQL lost: reconnecting.");
PQreset(srv.db_cxn);
if (PQstatus(srv.db_cxn) != CONNECTION_OK) {
applog(LOG_ERR, "Reconnect attempt failed.");
return false;
}
}
return true;
}
static char *pg_pwdb_lookup(const char *user)
{
char *pw = NULL;
PGresult *res;
const char *paramvalues[] = { user };
if (!pg_conncheck())
return NULL;
res =
PQexecParams(srv.db_cxn, srv.db_stmt_pwdb, 1, NULL,
paramvalues, NULL, NULL, 0);
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
applog(LOG_ERR, "pg_pwdb_lookup query failed: %s",
PQerrorMessage(srv.db_cxn));
goto out;
}
if (PQnfields(res) != 1 || PQntuples(res) < 1)
goto out;
pw = strdup(PQgetvalue(res, 0, 0));
out:
PQclear(res);
return pw;
}
static bool pg_sharelog(const char *rem_host, const char *username,
const char *our_result,
const char *upstream_result, const char *reason,
const char *solution)
{
PGresult *res;
/* PG does a fine job with timestamps so we won't bother. */
const char *paramvalues[] = { rem_host, username, our_result,
upstream_result, reason, solution
};
if (!pg_conncheck())
return false;
res =
PQexecParams(srv.db_cxn, srv.db_stmt_sharelog, 6, NULL,
paramvalues, NULL, NULL, 0);
if (PQresultStatus(res) != PGRES_COMMAND_OK)
applog(LOG_ERR, "pg_sharelog failed: %s",
PQerrorMessage(srv.db_cxn));
PQclear(res);
return true;
}
static void pg_close(void)
{
PQfinish(srv.db_cxn);
}
static bool pg_open(void)
{
char *portstr = NULL;
if (srv.db_port > 0)
if (asprintf(&portstr, "%d", srv.db_port) < 0)
return false;
srv.db_cxn = PQsetdbLogin(srv.db_host, portstr, NULL, NULL,
srv.db_name, srv.db_username,
srv.db_password);
free(portstr);
if (PQstatus(srv.db_cxn) != CONNECTION_OK) {
applog(LOG_ERR, "failed to connect to postgresql: %s",
PQerrorMessage(srv.db_cxn));
pg_close();
return false;
}
if (srv.db_stmt_pwdb == NULL || !*srv.db_stmt_pwdb)
srv.db_stmt_pwdb = strdup(DEFAULT_STMT_PWDB);
if (srv.db_stmt_sharelog == NULL || !*srv.db_stmt_sharelog)
srv.db_stmt_sharelog = strdup(DEFAULT_STMT_SHARELOG);
return true;
}
struct server_db_ops postgresql_db_ops = {
.pwdb_lookup = pg_pwdb_lookup,
.sharelog = pg_sharelog,
.open = pg_open,
.close = pg_close,
};
#endif