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
287
288
289
290
291
292
293
294
295
296
|
/* $OpenBSD: table_api.c,v 1.2 2013/07/20 09:06:46 eric 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/uio.h>
#include <event.h>
#include <fcntl.h>
#include <imsg.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "smtpd-defines.h"
#include "smtpd-api.h"
#include "log.h"
static int (*handler_update)(void);
static int (*handler_check)(int, const char *);
static int (*handler_lookup)(int, const char *, char *, size_t);
static int (*handler_fetch)(int, char *, size_t);
static int quit;
static struct imsgbuf ibuf;
static struct imsg imsg;
static size_t rlen;
static char *rdata;
static struct ibuf *buf;
#if 0
static char *rootpath;
static char *user = SMTPD_USER;
#endif
static void
table_msg_get(void *dst, size_t len)
{
if (len > rlen) {
log_warnx("warn: table-proc: bad msg len");
fatalx("table-proc: exiting");
}
if (len == 0)
return;
if (dst)
memmove(dst, rdata, len);
rlen -= len;
rdata += len;
}
static void
table_msg_end(void)
{
if (rlen) {
log_warnx("warn: table-proc: bogus data");
fatalx("table-proc: exiting");
}
imsg_free(&imsg);
}
static void
table_msg_add(const void *data, size_t len)
{
if (buf == NULL)
buf = imsg_create(&ibuf, PROC_TABLE_OK, 0, 0, 1024);
if (buf == NULL) {
log_warnx("warn: table-api: imsg_create failed");
fatalx("table-api: exiting");
}
if (imsg_add(buf, data, len) == -1) {
log_warnx("warn: table-api: imsg_add failed");
fatalx("table-api: exiting");
}
}
static void
table_msg_close(void)
{
imsg_close(&ibuf, buf);
buf = NULL;
}
static void
table_msg_dispatch(void)
{
uint32_t version;
char res[4096];
int type, r;
switch (imsg.hdr.type) {
case PROC_TABLE_OPEN:
table_msg_get(&version, sizeof(version));
table_msg_end();
if (version != PROC_TABLE_API_VERSION) {
log_warnx("warn: table-api: bad API version");
fatalx("table-api: terminating");
}
imsg_compose(&ibuf, PROC_TABLE_OK, 0, 0, -1, NULL, 0);
break;
case PROC_TABLE_UPDATE:
table_msg_end();
if (handler_update)
r = handler_update();
else
r = 1;
imsg_compose(&ibuf, PROC_TABLE_OK, 0, 0, -1, &r, sizeof(r));
break;
case PROC_TABLE_CLOSE:
quit = 1;
break;
case PROC_TABLE_CHECK:
table_msg_get(&type, sizeof(type));
if (rlen == 0) {
log_warnx("warn: table-api: no key");
fatalx("table-api: exiting");
}
if (rdata[rlen - 1] != '\0') {
log_warnx("warn: table-api: key not NUL-terminated");
fatalx("table-api: exiting");
}
if (handler_check)
r = handler_check(type, rdata);
else
r = -1;
table_msg_get(NULL, rlen);
table_msg_end();
table_msg_add(&r, sizeof(r));
table_msg_close();
break;
case PROC_TABLE_LOOKUP:
table_msg_get(&type, sizeof(type));
if (rlen == 0) {
log_warnx("warn: table-api: no key");
fatalx("table-api: exiting");
}
if (rdata[rlen - 1] != '\0') {
log_warnx("warn: table-api: key not NUL-terminated");
fatalx("table-api: exiting");
}
if (handler_lookup)
r = handler_lookup(type, rdata, res, sizeof(res));
else
r = -1;
table_msg_get(NULL, rlen);
table_msg_end();
table_msg_add(&r, sizeof(r));
if (r == 1)
table_msg_add(res, strlen(res) + 1);
table_msg_close();
break;
case PROC_TABLE_FETCH:
table_msg_get(&type, sizeof(type));
table_msg_end();
if (handler_fetch)
r = handler_fetch(type, res, sizeof(res));
else
r = -1;
table_msg_add(&r, sizeof(r));
if (r == 1)
table_msg_add(res, strlen(res) + 1);
table_msg_close();
break;
default:
log_warnx("warn: table-api: bad message %i", imsg.hdr.type);
fatalx("table-api: exiting");
}
}
void
table_api_on_update(int(*cb)(void))
{
handler_update = cb;
}
void
table_api_on_check(int(*cb)(int, const char *))
{
handler_check = cb;
}
void
table_api_on_lookup(int(*cb)(int, const char *, char *, size_t))
{
handler_lookup = cb;
}
void
table_api_on_fetch(int(*cb)(int, char *, size_t))
{
handler_fetch = cb;
}
int
table_api_dispatch(void)
{
#if 0
struct passwd *pw;
#endif
ssize_t n;
#if 0
pw = getpwnam(user);
if (pw == NULL) {
log_warn("table-api: getpwnam");
fatalx("table-api: exiting");
}
if (rootpath) {
if (chroot(rootpath) == -1) {
log_warn("table-api: chroot");
fatalx("table-api: exiting");
}
if (chdir("/") == -1) {
log_warn("table-api: chdir");
fatalx("table-api: exiting");
}
}
if (setgroups(1, &pw->pw_gid) ||
setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid)) {
log_warn("table-api: cannot drop privileges");
fatalx("table-api: exiting");
}
#endif
imsg_init(&ibuf, 0);
while (1) {
n = imsg_get(&ibuf, &imsg);
if (n == -1) {
log_warn("warn: table-api: imsg_get");
break;
}
if (n) {
rdata = imsg.data;
rlen = imsg.hdr.len - IMSG_HEADER_SIZE;
table_msg_dispatch();
if (quit)
break;
imsg_flush(&ibuf);
continue;
}
n = imsg_read(&ibuf);
if (n == -1) {
log_warn("warn: table-api: imsg_read");
break;
}
if (n == 0) {
log_warnx("warn: table-api: pipe closed");
break;
}
}
return (1);
}
|