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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
|
/* $OpenBSD: table.c,v 1.21 2015/11/30 14:13:03 gilles Exp $ */
/*
* Copyright (c) 2013 Eric Faurot <eric@openbsd.org>
* Copyright (c) 2008 Gilles Chehade <gilles@poolp.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 <sys/stat.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <event.h>
#include <imsg.h>
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <limits.h>
#include <string.h>
#include <unistd.h>
#include "smtpd.h"
#include "log.h"
struct table_backend *table_backend_lookup(const char *);
extern struct table_backend table_backend_static;
extern struct table_backend table_backend_db;
extern struct table_backend table_backend_getpwnam;
extern struct table_backend table_backend_proc;
static const char * table_service_name(enum table_service);
static const char * table_backend_name(struct table_backend *);
static const char * table_dump_lookup(enum table_service, union lookup *);
static int parse_sockaddr(struct sockaddr *, int, const char *);
static unsigned int last_table_id = 0;
struct table_backend *
table_backend_lookup(const char *backend)
{
if (!strcmp(backend, "static") || !strcmp(backend, "file"))
return &table_backend_static;
if (!strcmp(backend, "db"))
return &table_backend_db;
if (!strcmp(backend, "getpwnam"))
return &table_backend_getpwnam;
if (!strcmp(backend, "proc"))
return &table_backend_proc;
return NULL;
}
static const char *
table_backend_name(struct table_backend *backend)
{
if (backend == &table_backend_static)
return "static";
if (backend == &table_backend_db)
return "db";
if (backend == &table_backend_getpwnam)
return "getpwnam";
if (backend == &table_backend_proc)
return "proc";
return "???";
}
static const char *
table_service_name(enum table_service s)
{
switch (s) {
case K_NONE: return "NONE";
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";
default: return "???";
}
}
struct table *
table_find(const char *name, const char *tag)
{
char buf[LINE_MAX];
if (tag == NULL)
return dict_get(env->sc_tables_dict, name);
if ((size_t)snprintf(buf, sizeof(buf), "%s#%s", name, tag) >= sizeof(buf)) {
log_warnx("warn: table name too long: %s#%s", name, tag);
return (NULL);
}
return dict_get(env->sc_tables_dict, buf);
}
int
table_lookup(struct table *table, struct dict *params, const char *key, enum table_service kind,
union lookup *lk)
{
int r;
char lkey[1024];
if (table->t_backend->lookup == NULL)
return (-1);
if (! lowercase(lkey, key, sizeof lkey)) {
log_warnx("warn: lookup key too long: %s", key);
return -1;
}
r = table->t_backend->lookup(table->t_handle, params, lkey, kind, lk);
if (r == 1)
log_trace(TRACE_LOOKUP, "lookup: %s \"%s\" as %s in table %s:%s -> %s%s%s",
lk ? "lookup" : "check",
lkey,
table_service_name(kind),
table_backend_name(table->t_backend),
table->t_name,
lk ? "\"" : "",
(lk) ? table_dump_lookup(kind, lk): "found",
lk ? "\"" : "");
else
log_trace(TRACE_LOOKUP, "lookup: %s \"%s\" as %s in table %s:%s -> %d",
lk ? "lookup" : "check",
lkey,
table_service_name(kind),
table_backend_name(table->t_backend),
table->t_name,
r);
return (r);
}
int
table_fetch(struct table *table, struct dict *params, enum table_service kind, union lookup *lk)
{
int r;
if (table->t_backend->fetch == NULL)
return (-1);
r = table->t_backend->fetch(table->t_handle, params, kind, lk);
if (r == 1)
log_trace(TRACE_LOOKUP, "lookup: fetch %s from table %s:%s -> %s%s%s",
table_service_name(kind),
table_backend_name(table->t_backend),
table->t_name,
lk ? "\"" : "",
(lk) ? table_dump_lookup(kind, lk): "found",
lk ? "\"" : "");
else
log_trace(TRACE_LOOKUP, "lookup: fetch %s from table %s:%s -> %d",
table_service_name(kind),
table_backend_name(table->t_backend),
table->t_name,
r);
return (r);
}
struct table *
table_create(const char *backend, const char *name, const char *tag,
const char *config)
{
struct table *t;
struct table_backend *tb;
char *ps;
char *p;
char buf[LINE_MAX];
char path[LINE_MAX];
char pathsplit[LINE_MAX];
size_t n;
struct stat sb;
if (name && tag) {
if ((size_t)snprintf(buf, sizeof(buf), "%s#%s", name, tag) >=
sizeof(buf))
fatalx("table_create: name too long \"%s#%s\"",
name, tag);
name = buf;
}
if (name && table_find(name, NULL))
fatalx("table_create: table \"%s\" already defined", name);
if ((tb = table_backend_lookup(backend)) == NULL) {
(void)strlcpy(pathsplit, PATH_LIBEXEC, sizeof pathsplit);
for (ps = pathsplit; (p = strsep(&ps, ":")) != NULL;) {
if ((size_t)snprintf(path, sizeof(path), "%s/table-%s",
p, backend) >= sizeof(path)) {
fatalx("table_create: path too long \""
"%s/table-%s\"", p, backend);
}
if (stat(path, &sb) == 0) {
tb = table_backend_lookup("proc");
(void)strlcpy(path, backend, sizeof(path));
if (config) {
(void)strlcat(path, ":", sizeof(path));
if (strlcat(path, config, sizeof(path))
>= sizeof(path))
fatalx("table_create: config file path too long");
}
config = path;
break;
}
}
}
if (tb == NULL)
fatalx("table_create: backend \"%s\" does not exist", backend);
t = xcalloc(1, sizeof(*t), "table_create");
t->t_backend = tb;
/* XXX */
/*
* until people forget about it, "file" really means "static"
*/
if (!strcmp(backend, "file"))
backend = "static";
if (config) {
if (strlcpy(t->t_config, config, sizeof t->t_config)
>= sizeof t->t_config)
fatalx("table_create: table config \"%s\" too large",
t->t_config);
}
if (strcmp(backend, "static") != 0)
t->t_type = T_DYNAMIC;
if (name == NULL)
(void)snprintf(t->t_name, sizeof(t->t_name), "<dynamic:%u>",
last_table_id++);
else {
n = strlcpy(t->t_name, name, sizeof(t->t_name));
if (n >= sizeof(t->t_name))
fatalx("table_create: table name too long");
}
dict_init(&t->t_dict);
dict_set(env->sc_tables_dict, t->t_name, t);
return (t);
}
void
table_destroy(struct table *t)
{
void *p = NULL;
while (dict_poproot(&t->t_dict, (void **)&p))
free(p);
dict_xpop(env->sc_tables_dict, t->t_name);
free(t);
}
int
table_config(struct table *t)
{
if (t->t_backend->config == NULL)
return (1);
return (t->t_backend->config(t));
}
void
table_add(struct table *t, const char *key, const char *val)
{
char lkey[1024], *old;
if (t->t_type & T_DYNAMIC)
fatalx("table_add: cannot add to table");
if (! lowercase(lkey, key, sizeof lkey)) {
log_warnx("warn: lookup key too long: %s", key);
return;
}
old = dict_set(&t->t_dict, lkey, val ? xstrdup(val, "table_add") : NULL);
if (old) {
log_warnx("warn: duplicate key \"%s\" in static table \"%s\"",
lkey, t->t_name);
free(old);
}
}
int
table_check_type(struct table *t, uint32_t mask)
{
return t->t_type & mask;
}
int
table_check_service(struct table *t, uint32_t mask)
{
return t->t_backend->services & mask;
}
int
table_check_use(struct table *t, uint32_t tmask, uint32_t smask)
{
return table_check_type(t, tmask) && table_check_service(t, smask);
}
int
table_open(struct table *t)
{
t->t_handle = NULL;
if (t->t_backend->open == NULL)
return (1);
t->t_handle = t->t_backend->open(t);
if (t->t_handle == NULL)
return (0);
return (1);
}
void
table_close(struct table *t)
{
if (t->t_backend->close)
t->t_backend->close(t->t_handle);
}
int
table_update(struct table *t)
{
if (t->t_backend->update == NULL)
return (1);
return (t->t_backend->update(t));
}
/*
* quick reminder:
* in *_match() s1 comes from session, s2 comes from table
*/
int
table_domain_match(const char *s1, const char *s2)
{
return hostname_match(s1, s2);
}
int
table_mailaddr_match(const char *s1, const char *s2)
{
struct mailaddr m1;
struct mailaddr m2;
if (! text_to_mailaddr(&m1, s1))
return 0;
if (! text_to_mailaddr(&m2, s2))
return 0;
return mailaddr_match(&m1, &m2);
}
static int table_match_mask(struct sockaddr_storage *, struct netaddr *);
static int table_inet4_match(struct sockaddr_in *, struct netaddr *);
static int table_inet6_match(struct sockaddr_in6 *, struct netaddr *);
int
table_netaddr_match(const char *s1, const char *s2)
{
struct netaddr n1;
struct netaddr n2;
if (strcasecmp(s1, s2) == 0)
return 1;
if (! text_to_netaddr(&n1, s1))
return 0;
if (! text_to_netaddr(&n2, s2))
return 0;
if (n1.ss.ss_family != n2.ss.ss_family)
return 0;
if (n1.ss.ss_len != n2.ss.ss_len)
return 0;
return table_match_mask(&n1.ss, &n2);
}
static int
table_match_mask(struct sockaddr_storage *ss, struct netaddr *ssmask)
{
if (ss->ss_family == AF_INET)
return table_inet4_match((struct sockaddr_in *)ss, ssmask);
if (ss->ss_family == AF_INET6)
return table_inet6_match((struct sockaddr_in6 *)ss, ssmask);
return (0);
}
static int
table_inet4_match(struct sockaddr_in *ss, struct netaddr *ssmask)
{
in_addr_t mask;
int i;
/* a.b.c.d/8 -> htonl(0xff000000) */
mask = 0;
for (i = 0; i < ssmask->bits; ++i)
mask = (mask >> 1) | 0x80000000;
mask = htonl(mask);
/* (addr & mask) == (net & mask) */
if ((ss->sin_addr.s_addr & mask) ==
(((struct sockaddr_in *)ssmask)->sin_addr.s_addr & mask))
return 1;
return 0;
}
static int
table_inet6_match(struct sockaddr_in6 *ss, struct netaddr *ssmask)
{
struct in6_addr *in;
struct in6_addr *inmask;
struct in6_addr mask;
int i;
memset(&mask, 0, sizeof(mask));
for (i = 0; i < ssmask->bits / 8; i++)
mask.s6_addr[i] = 0xff;
i = ssmask->bits % 8;
if (i)
mask.s6_addr[ssmask->bits / 8] = 0xff00 >> i;
in = &ss->sin6_addr;
inmask = &((struct sockaddr_in6 *)&ssmask->ss)->sin6_addr;
for (i = 0; i < 16; i++) {
if ((in->s6_addr[i] & mask.s6_addr[i]) !=
(inmask->s6_addr[i] & mask.s6_addr[i]))
return (0);
}
return (1);
}
void
table_dump_all(void)
{
struct table *t;
void *iter, *i2;
const char *key, *sep;
char *value;
char buf[1024];
iter = NULL;
while (dict_iter(env->sc_tables_dict, &iter, NULL, (void **)&t)) {
i2 = NULL;
sep = "";
buf[0] = '\0';
if (t->t_type & T_DYNAMIC) {
(void)strlcat(buf, "DYNAMIC", sizeof(buf));
sep = ",";
}
if (t->t_type & T_LIST) {
(void)strlcat(buf, sep, sizeof(buf));
(void)strlcat(buf, "LIST", sizeof(buf));
sep = ",";
}
if (t->t_type & T_HASH) {
(void)strlcat(buf, sep, sizeof(buf));
(void)strlcat(buf, "HASH", sizeof(buf));
sep = ",";
}
log_debug("TABLE \"%s\" type=%s config=\"%s\"",
t->t_name, buf, t->t_config);
while(dict_iter(&t->t_dict, &i2, &key, (void**)&value)) {
if (value)
log_debug(" \"%s\" -> \"%s\"", key, value);
else
log_debug(" \"%s\"", key);
}
}
}
void
table_open_all(void)
{
struct table *t;
void *iter;
iter = NULL;
while (dict_iter(env->sc_tables_dict, &iter, NULL, (void **)&t))
if (! table_open(t))
fatalx("failed to open table %s", t->t_name);
}
void
table_close_all(void)
{
struct table *t;
void *iter;
iter = NULL;
while (dict_iter(env->sc_tables_dict, &iter, NULL, (void **)&t))
table_close(t);
}
int
table_parse_lookup(enum table_service service, const char *key,
const char *line, union lookup *lk)
{
char buffer[LINE_MAX], *p;
size_t len;
len = strlen(line);
switch (service) {
case K_ALIAS:
lk->expand = calloc(1, sizeof(*lk->expand));
if (lk->expand == NULL)
return (-1);
if (!expand_line(lk->expand, line, 1)) {
expand_free(lk->expand);
return (-1);
}
return (1);
case K_DOMAIN:
if (strlcpy(lk->domain.name, line, sizeof(lk->domain.name))
>= sizeof(lk->domain.name))
return (-1);
return (1);
case K_CREDENTIALS:
/* credentials are stored as user:password */
if (len < 3)
return (-1);
/* too big to fit in a smtp session line */
if (len >= LINE_MAX)
return (-1);
p = strchr(line, ':');
if (p == NULL) {
if (strlcpy(lk->creds.username, key, sizeof (lk->creds.username))
>= sizeof (lk->creds.username))
return (-1);
if (strlcpy(lk->creds.password, line, sizeof(lk->creds.password))
>= sizeof(lk->creds.password))
return (-1);
return (1);
}
if (p == line || p == line + len - 1)
return (-1);
memmove(lk->creds.username, line, p - line);
lk->creds.username[p - line] = '\0';
if (strlcpy(lk->creds.password, p+1, sizeof(lk->creds.password))
>= sizeof(lk->creds.password))
return (-1);
return (1);
case K_NETADDR:
if (!text_to_netaddr(&lk->netaddr, line))
return (-1);
return (1);
case K_USERINFO:
if (!bsnprintf(buffer, sizeof(buffer), "%s:%s", key, line))
return (-1);
if (!text_to_userinfo(&lk->userinfo, buffer))
return (-1);
return (1);
case K_SOURCE:
if (parse_sockaddr((struct sockaddr *)&lk->source.addr,
PF_UNSPEC, line) == -1)
return (-1);
return (1);
case K_MAILADDR:
if (!text_to_mailaddr(&lk->mailaddr, line))
return (-1);
return (1);
case K_MAILADDRMAP:
lk->maddrmap = calloc(1, sizeof(*lk->maddrmap));
if (lk->maddrmap == NULL)
return (-1);
maddrmap_init(lk->maddrmap);
if (! mailaddr_line(lk->maddrmap, line)) {
maddrmap_free(lk->maddrmap);
return (-1);
}
return (1);
case K_ADDRNAME:
if (parse_sockaddr((struct sockaddr *)&lk->addrname.addr,
PF_UNSPEC, key) == -1)
return (-1);
if (strlcpy(lk->addrname.name, line, sizeof(lk->addrname.name))
>= sizeof(lk->addrname.name))
return (-1);
return (1);
default:
return (-1);
}
}
static const char *
table_dump_lookup(enum table_service s, union lookup *lk)
{
static char buf[LINE_MAX];
int ret;
switch (s) {
case K_NONE:
break;
case K_ALIAS:
expand_to_text(lk->expand, buf, sizeof(buf));
break;
case K_DOMAIN:
ret = snprintf(buf, sizeof(buf), "%s", lk->domain.name);
if (ret == -1 || (size_t)ret >= sizeof (buf))
goto err;
break;
case K_CREDENTIALS:
ret = snprintf(buf, sizeof(buf), "%s:%s",
lk->creds.username, lk->creds.password);
if (ret == -1 || (size_t)ret >= sizeof (buf))
goto err;
break;
case K_NETADDR:
ret = snprintf(buf, sizeof(buf), "%s/%d",
sockaddr_to_text((struct sockaddr *)&lk->netaddr.ss),
lk->netaddr.bits);
if (ret == -1 || (size_t)ret >= sizeof (buf))
goto err;
break;
case K_USERINFO:
ret = snprintf(buf, sizeof(buf), "%s:%d:%d:%s",
lk->userinfo.username,
lk->userinfo.uid,
lk->userinfo.gid,
lk->userinfo.directory);
if (ret == -1 || (size_t)ret >= sizeof (buf))
goto err;
break;
case K_SOURCE:
ret = snprintf(buf, sizeof(buf), "%s",
ss_to_text(&lk->source.addr));
if (ret == -1 || (size_t)ret >= sizeof (buf))
goto err;
break;
case K_MAILADDR:
ret = snprintf(buf, sizeof(buf), "%s@%s",
lk->mailaddr.user,
lk->mailaddr.domain);
if (ret == -1 || (size_t)ret >= sizeof (buf))
goto err;
break;
case K_ADDRNAME:
ret = snprintf(buf, sizeof(buf), "%s",
lk->addrname.name);
if (ret == -1 || (size_t)ret >= sizeof (buf))
goto err;
break;
default:
break;
}
return (buf);
err:
return (NULL);
}
static int
parse_sockaddr(struct sockaddr *sa, int family, const char *str)
{
struct in_addr ina;
struct in6_addr in6a;
struct sockaddr_in *sin;
struct sockaddr_in6 *sin6;
char *cp, *str2;
const char *errstr;
switch (family) {
case PF_UNSPEC:
if (parse_sockaddr(sa, PF_INET, str) == 0)
return (0);
return parse_sockaddr(sa, PF_INET6, str);
case PF_INET:
if (inet_pton(PF_INET, str, &ina) != 1)
return (-1);
sin = (struct sockaddr_in *)sa;
memset(sin, 0, sizeof *sin);
sin->sin_len = sizeof(struct sockaddr_in);
sin->sin_family = PF_INET;
sin->sin_addr.s_addr = ina.s_addr;
return (0);
case PF_INET6:
if (strncasecmp("ipv6:", str, 5) == 0)
str += 5;
cp = strchr(str, SCOPE_DELIMITER);
if (cp) {
str2 = strdup(str);
if (str2 == NULL)
return (-1);
str2[cp - str] = '\0';
if (inet_pton(PF_INET6, str2, &in6a) != 1) {
free(str2);
return (-1);
}
cp++;
free(str2);
} else if (inet_pton(PF_INET6, str, &in6a) != 1)
return (-1);
sin6 = (struct sockaddr_in6 *)sa;
memset(sin6, 0, sizeof *sin6);
sin6->sin6_len = sizeof(struct sockaddr_in6);
sin6->sin6_family = PF_INET6;
sin6->sin6_addr = in6a;
if (cp == NULL)
return (0);
if (IN6_IS_ADDR_LINKLOCAL(&in6a) ||
IN6_IS_ADDR_MC_LINKLOCAL(&in6a) ||
IN6_IS_ADDR_MC_INTFACELOCAL(&in6a))
if ((sin6->sin6_scope_id = if_nametoindex(cp)))
return (0);
sin6->sin6_scope_id = strtonum(cp, 0, UINT32_MAX, &errstr);
if (errstr)
return (-1);
return (0);
default:
break;
}
return (-1);
}
|