This repository has been archived by the owner on Nov 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table-stdio.c
367 lines (290 loc) · 7.31 KB
/
table-stdio.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/* $OpenBSD$ */
/*
* Copyright (c) 2024 Omar Polo <[email protected]>
*
* 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 "openbsd-compat.h"
#include <sys/socket.h>
#include <sys/time.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <smtpd-api.h>
#include "log.h"
#define PROTOCOL_VERSION "0.1"
#ifndef __dead
#define __dead __attribute__((noreturn))
#endif
int services;
pid_t pid;
FILE *backend;
char *line;
size_t linesize;
ssize_t linelen;
char id[64];
static char *
nextid(void)
{
int r;
r = snprintf(id, sizeof(id), "%llx", (unsigned long long)arc4random());
if (r < 0 || (size_t)r >= sizeof(id))
fatal("snprintf");
return (id);
}
static const char *
service_name(int service)
{
switch (service) {
case K_ALIAS: return ("alias");
case K_DOMAIN: return ("domain");
case K_CREDENTIALS: return ("credentials");
case K_NETADDR: return ("netaddr");
case K_USERINFO: return ("userinfo");
case K_SOURCE: return ("source");
case K_MAILADDR: return ("mailaddr");
case K_ADDRNAME: return ("addrname");
case K_MAILADDRMAP: return ("mailaddrmap");
}
fatalx("unknown service %d", service);
}
static int
send_request(const char *type, int service, const char *param)
{
struct timeval tv;
gettimeofday(&tv, NULL);
fprintf(backend, "table|%s|%lld.%06ld|%s|%s",
PROTOCOL_VERSION, (long long)tv.tv_sec, (long)tv.tv_usec,
table_api_get_name(), type);
if (service != -1) {
fprintf(backend, "|%s|%s", service_name(service), nextid());
if (param)
fprintf(backend, "|%s", param);
fputc('\n', backend);
} else
fprintf(backend, "|%s\n", nextid());
if (fflush(backend) == EOF)
return (-1);
return (0);
}
static const char *
parse_reply(const char *type)
{
const char *l;
size_t len;
if ((linelen = getline(&line, &linesize, backend)) == -1)
fatal("getline");
if (line[linelen-1] == '\n')
line[--linelen] = '\0';
l = line;
len = strlen(type);
if (strncmp(l, type, len) != 0)
return (NULL);
l += len;
if (*l != '|')
return (NULL);
l++;
len = strlen(id);
if (strncmp(l, id, len) != 0)
return (NULL);
l += len;
if (*l != '|')
return (NULL);
return (++l);
}
static int
table_procexec_update(void)
{
const char *r;
if (send_request("update", -1, NULL) == -1)
fatal("send_request");
if ((r = parse_reply("update-result")) == NULL)
fatalx("malformed line: %s", line);
if (!strcmp(r, "ok"))
return (1);
if (strcmp(r, "error") != 0)
log_warnx("update-result: unexpected value: %s", r);
return (0);
}
static int
table_procexec_check(int service, struct dict *params, const char *key)
{
const char *r;
if (!(services & service))
return (-1);
if (send_request("check", service, key) == -1)
fatal("fflush");
if ((r = parse_reply("check-result")) == NULL)
fatalx("malformed line: %s", line);
if (!strcmp(r, "found"))
return (1);
if (strcmp(r, "error") != 0)
log_warnx("invalid response: %s", r);
return (-1);
}
static int
table_procexec_lookup(int service, struct dict *params, const char *key,
char *dst, size_t sz)
{
const char *r;
if (!(services & service))
return (-1);
if (send_request("lookup", service, key) == -1)
fatal("fflush");
if ((r = parse_reply("lookup-result")) == NULL)
fatalx("malformed line: %s", line);
if (!strncmp(r, "found|", 6)) {
r += 6;
if (strlcpy(dst, r, sz) >= sz) {
log_warnx("warn: result too large");
return (-1);
}
return (1);
}
if (!strcmp(r, "not-found"))
return (0);
if (strcmp(r, "error") != 0)
log_warnx("invalid response: %s", r);
return (-1);
}
static int
table_procexec_fetch(int service, struct dict *params, char *dst, size_t sz)
{
const char *r;
if (!(services & service))
return (-1);
if (send_request("fetch", service, NULL))
fatal("fflush");
if ((r = parse_reply("fetch-result")) == NULL)
fatalx("malformed line: %s", line);
if (!strcmp(r, "not-found"))
return (0);
if (!strcmp(r, "error"))
return (-1);
if (strncmp(r, "found|", 6) != 0)
fatalx("malformed line: %s", line);
r += 6;
if (strlcpy(dst, r, sz) >= sz)
return (-1);
return (1);
}
static int
spawn_backend(char *backend)
{
const char *argv[] = {"sh", "-c", NULL, NULL};
int p[2];
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
fatal("socketpair");
if ((pid = fork()) == -1)
fatal("fork");
if (pid == 0) { /* child */
close(p[0]);
dup2(p[1], 0);
dup2(p[1], 1);
/* stderr is inherited */
log_warnx("about to run %s", backend);
argv[2] = backend;
execvp("/bin/sh", (char *const *)argv);
log_warn("sh -c %s", backend);
_exit(1);
}
close(p[1]);
return (p[0]);
}
static void __dead
usage(void)
{
fprintf(stderr, "usage: %s table-backend\n", getprogname());
exit(1);
}
int
main(int argc, char **argv)
{
int ch, fd;
const char *service;
#if 0
static int attached;
while (!attached) {
fprintf(stderr, "my pid is %d\n", getpid());
sleep(1);
}
#endif
log_init(1);
log_verbose(1);
while ((ch = getopt(argc, argv, "")) != -1) {
switch (ch) {
default:
usage();
}
}
argc -= optind;
argv += optind;
if (argc != 1)
usage();
if ((fd = spawn_backend(argv[0])) == -1)
fatal("spawn_backend");
if ((backend = fdopen(fd, "r+")) == NULL) {
log_warn("fdopen");
kill(pid, SIGTERM);
exit(1);
}
fprintf(backend, "config|smtpd-version|7.4.0\n");
fprintf(backend, "config|protocol|"PROTOCOL_VERSION"\n");
fprintf(backend, "config|ready\n");
fflush(backend);
while ((linelen = getline(&line, &linesize, backend)) != -1) {
if (line[linelen-1] == '\n')
line[--linelen] = '\0';
if (strncmp(line, "register|", 9) != 0)
fatalx("invalid line: %s", line);
service = line + 9;
if (!strcmp(service, "ready"))
break;
else if (!strcmp(service, "alias"))
services |= K_ALIAS;
else if (!strcmp(service, "domain"))
services |= K_DOMAIN;
else if (!strcmp(service, "credentials"))
services |= K_CREDENTIALS;
else if (!strcmp(service, "netaddr"))
services |= K_NETADDR;
else if (!strcmp(service, "userinfo"))
services |= K_USERINFO;
else if (!strcmp(service, "source"))
services |= K_SOURCE;
else if (!strcmp(service, "mailaddr"))
services |= K_MAILADDR;
else if (!strcmp(service, "addrname"))
services |= K_ADDRNAME;
else if (!strcmp(service, "mailaddrmap"))
services |= K_MAILADDRMAP;
else
fatalx("unknown service %s", service);
}
if (ferror(backend))
fatal("getline");
if (services == 0)
fatalx("no services registered");
table_api_on_update(table_procexec_update);
table_api_on_check(table_procexec_check);
table_api_on_lookup(table_procexec_lookup);
table_api_on_fetch(table_procexec_fetch);
table_api_dispatch();
#if notyet
if (!died)
kill(pid, SIGTERM);
#endif
return (0);
}