summaryrefslogtreecommitdiff
path: root/src/varcache.c
blob: e8ad349b0e12737c1466511d39c3b1f98df3615c (plain)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
 * PgBouncer - Lightweight connection pooler for PostgreSQL.
 * 
 * Copyright (c) 2007-2009  Marko Kreen, Skype Technologies OÜ
 * 
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * Operations with server config parameters.
 */

#include "bouncer.h"

struct var_lookup {
	const char *name;
	int offset;
	int len;
};

static const struct var_lookup lookup [] = {
 {"client_encoding",             offsetof(VarCache, client_encoding), VAR_ENCODING_LEN },
 {"datestyle",                   offsetof(VarCache, datestyle),       VAR_DATESTYLE_LEN },
 {"timezone",                    offsetof(VarCache, timezone),        VAR_TIMEZONE_LEN },
 {"standard_conforming_strings", offsetof(VarCache, std_strings),     VAR_STDSTR_LEN },
 {NULL},
};

static inline char *get_value(VarCache *cache, const struct var_lookup *lk)
{
	return (char *)(cache) + lk->offset;
}

bool varcache_set(VarCache *cache, const char *key, const char *value)
{
	int vlen;
	char *pos;
	const struct var_lookup *lk;

	/* convert NULL to empty string */
	if (value == NULL)
		value = "";

	for (lk = lookup; lk->name; lk++) {
		if (strcasecmp(lk->name, key) != 0)
			continue;

		vlen = strlen(value);
		if (vlen >= lk->len) {
			log_warning("varcache_set overflow: %s", key);
			return false;
		}

		pos = get_value(cache, lk);
		memcpy(pos, value, vlen + 1);
		return true;
	}
	return false;
}

static bool is_std_quote(VarCache *vars)
{
	const char *val = vars->std_strings;
	return strcasecmp(val, "on") == 0;
}

static bool quote_literal(char *buf, int buflen, const char *src, bool std_quote)
{
	char *dst = buf;
	char *end = buf + buflen - 2;

	if (buflen < 3)
		return false;

	*dst++ = '\'';
	while (*src && dst < end) {
		if (*src == '\'')
			*dst++ = '\'';
		else if (*src == '\\' && !std_quote)
			*dst++ = '\\';
		*dst++ = *src++;
	}
	if (*src || dst > end)
		return false;

	*dst++ = '\'';
	*dst = 0;

	return true;
}

static int apply_var(PktBuf *pkt, const char *key,
		     const char *cval, const char *sval,
		     bool std_quote)
{
	char buf[128];
	char qbuf[128];
	unsigned len;

	if (strcasecmp(cval, sval) == 0)
		return 0;

	/* if unset, ignore */
	if (!*cval)
		return 0;

	/* the string may have been taken from startup pkt */
	if (!quote_literal(qbuf, sizeof(qbuf), cval, std_quote))
		return 0;

	/* add SET statement to packet */
	len = snprintf(buf, sizeof(buf), "SET %s=%s;", key, qbuf);
	if (len < sizeof(buf)) {
		pktbuf_put_bytes(pkt, buf, len);
		return 1;
	} else {
		log_warning("got too long value, skipping");
		return 0;
	}
}

bool varcache_apply(PgSocket *server, PgSocket *client, bool *changes_p)
{
	PktBuf pkt;
	uint8_t buf[1024];
	int changes = 0;
	const char *cval, *sval;
	const struct var_lookup *lk;
	uint8_t *debug_sql;
	bool std_quote = is_std_quote(&server->vars);

	pktbuf_static(&pkt, buf, sizeof(buf));
	pktbuf_start_packet(&pkt, 'Q');

	/* grab quory position inside pkt */
	debug_sql = pkt.buf + pkt.write_pos;

	for (lk = lookup; lk->name; lk++) {
		sval = get_value(&server->vars, lk);
		cval = get_value(&client->vars, lk);
		changes += apply_var(&pkt, lk->name, cval, sval, std_quote);
	}
	*changes_p = changes > 0;
	if (!changes)
		return true;

	pktbuf_put_char(&pkt, 0);
	pktbuf_finish_packet(&pkt);

	slog_debug(server, "varcache_apply: %s", debug_sql);
	return pktbuf_send_immidiate(&pkt, server);
}

void varcache_fill_unset(VarCache *src, PgSocket *dst)
{
	char *srcval, *dstval;
	const struct var_lookup *lk;
	for (lk = lookup; lk->name; lk++) {
		srcval = get_value(src, lk);
		dstval = get_value(&dst->vars, lk);
		if (!*dstval)
			memcpy(dstval, srcval, lk->len);
	}
}

void varcache_clean(VarCache *cache)
{
	cache->client_encoding[0] = 0;
	cache->datestyle[0] = 0;
	cache->timezone[0] = 0;
	cache->std_strings[0] = 0;
}

void varcache_add_params(PktBuf *pkt, VarCache *vars)
{
	char *val;
	const struct var_lookup *lk;
	for (lk = lookup; lk->name; lk++) {
		val = get_value(vars, lk);
		if (*val)
			pktbuf_write_ParameterStatus(pkt, lk->name, val);
	}
}