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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
/* $OpenBSD: table_proc.c,v 1.6 2015/12/05 13:14:21 claudio Exp $ */
/*
* Copyright (c) 2013 Eric Faurot <eric@openbsd.org>
*
* Permission to use, copy, modify, and 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.
*/
#include <sys/types.h>
#include <sys/queue.h>
#include <sys/tree.h>
#include <sys/socket.h>
#include <ctype.h>
#include <errno.h>
#include <event.h>
#include <fcntl.h>
#include <imsg.h>
#include <paths.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>
#include "smtpd.h"
#include "log.h"
struct table_proc_priv {
pid_t pid;
struct imsgbuf ibuf;
};
static struct imsg imsg;
static size_t rlen;
static char *rdata;
extern char **environ;
static void
table_proc_call(struct table_proc_priv *p)
{
ssize_t n;
if (imsg_flush(&p->ibuf) == -1) {
log_warn("warn: table-proc: imsg_flush");
fatalx("table-proc: exiting");
}
while (1) {
if ((n = imsg_get(&p->ibuf, &imsg)) == -1) {
log_warn("warn: table-proc: imsg_get");
break;
}
if (n) {
rlen = imsg.hdr.len - IMSG_HEADER_SIZE;
rdata = imsg.data;
if (imsg.hdr.type != PROC_TABLE_OK) {
log_warnx("warn: table-proc: bad response");
break;
}
return;
}
if ((n = imsg_read(&p->ibuf)) == -1 && errno != EAGAIN) {
log_warn("warn: table-proc: imsg_read");
break;
}
if (n == 0) {
log_warnx("warn: table-proc: pipe closed");
break;
}
}
fatalx("table-proc: exiting");
}
static void
table_proc_read(void *dst, size_t len)
{
if (len > rlen) {
log_warnx("warn: table-proc: bad msg len");
fatalx("table-proc: exiting");
}
if (dst)
memmove(dst, rdata, len);
rlen -= len;
rdata += len;
}
static void
table_proc_end(void)
{
if (rlen) {
log_warnx("warn: table-proc: bogus data");
fatalx("table-proc: exiting");
}
imsg_free(&imsg);
}
/*
* API
*/
static void *
table_proc_open(struct table *table)
{
struct table_proc_priv *priv;
struct table_open_params op;
int fd;
priv = xcalloc(1, sizeof(*priv), "table_proc_open");
fd = fork_proc_backend("table", table->t_config, table->t_name);
if (fd == -1)
fatalx("table-proc: exiting");
imsg_init(&priv->ibuf, fd);
memset(&op, 0, sizeof op);
op.version = PROC_TABLE_API_VERSION;
(void)strlcpy(op.name, table->t_name, sizeof op.name);
imsg_compose(&priv->ibuf, PROC_TABLE_OPEN, 0, 0, -1, &op, sizeof op);
table_proc_call(priv);
table_proc_end();
return (priv);
}
static int
table_proc_update(struct table *table)
{
struct table_proc_priv *priv = table->t_handle;
int r;
imsg_compose(&priv->ibuf, PROC_TABLE_UPDATE, 0, 0, -1, NULL, 0);
table_proc_call(priv);
table_proc_read(&r, sizeof(r));
table_proc_end();
return (r);
}
static void
table_proc_close(void *arg)
{
struct table_proc_priv *priv = arg;
imsg_compose(&priv->ibuf, PROC_TABLE_CLOSE, 0, 0, -1, NULL, 0);
imsg_flush(&priv->ibuf);
}
static int
imsg_add_params(struct ibuf *buf, struct dict *params)
{
size_t count;
const char *key;
char *value;
void *iter;
count = 0;
if (params)
count = dict_count(params);
if (imsg_add(buf, &count, sizeof(count)) == -1)
return (-1);
if (count == 0)
return (0);
iter = NULL;
while (dict_iter(params, &iter, &key, (void **)&value)) {
if (imsg_add(buf, key, strlen(key) + 1) == -1)
return (-1);
if (imsg_add(buf, value, strlen(value) + 1) == -1)
return (-1);
}
return (0);
}
static int
table_proc_lookup(void *arg, struct dict *params, const char *k, enum table_service s,
union lookup *lk)
{
struct table_proc_priv *priv = arg;
struct ibuf *buf;
int r;
buf = imsg_create(&priv->ibuf,
lk ? PROC_TABLE_LOOKUP : PROC_TABLE_CHECK, 0, 0,
sizeof(s) + strlen(k) + 1);
if (buf == NULL)
return (-1);
if (imsg_add(buf, &s, sizeof(s)) == -1)
return (-1);
if (imsg_add_params(buf, params) == -1)
return (-1);
if (imsg_add(buf, k, strlen(k) + 1) == -1)
return (-1);
imsg_close(&priv->ibuf, buf);
table_proc_call(priv);
table_proc_read(&r, sizeof(r));
if (r == 1 && lk) {
if (rlen == 0) {
log_warnx("warn: table-proc: empty response");
fatalx("table-proc: exiting");
}
if (rdata[rlen - 1] != '\0') {
log_warnx("warn: table-proc: not NUL-terminated");
fatalx("table-proc: exiting");
}
r = table_parse_lookup(s, k, rdata, lk);
table_proc_read(NULL, rlen);
}
table_proc_end();
return (r);
}
static int
table_proc_fetch(void *arg, struct dict *params, enum table_service s, union lookup *lk)
{
struct table_proc_priv *priv = arg;
struct ibuf *buf;
int r;
buf = imsg_create(&priv->ibuf, PROC_TABLE_FETCH, 0, 0, sizeof(s));
if (buf == NULL)
return (-1);
if (imsg_add(buf, &s, sizeof(s)) == -1)
return (-1);
if (imsg_add_params(buf, params) == -1)
return (-1);
imsg_close(&priv->ibuf, buf);
table_proc_call(priv);
table_proc_read(&r, sizeof(r));
if (r == 1) {
if (rlen == 0) {
log_warnx("warn: table-proc: empty response");
fatalx("table-proc: exiting");
}
if (rdata[rlen - 1] != '\0') {
log_warnx("warn: table-proc: not NUL-terminated");
fatalx("table-proc: exiting");
}
r = table_parse_lookup(s, NULL, rdata, lk);
table_proc_read(NULL, rlen);
}
table_proc_end();
return (r);
}
struct table_backend table_backend_proc = {
K_ANY,
NULL,
table_proc_open,
table_proc_update,
table_proc_close,
table_proc_lookup,
table_proc_fetch,
};
|