Blob


1 #include <sys/socket.h>
3 #include <err.h>
4 #include <errno.h>
5 #include <netdb.h>
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <unistd.h>
11 #include "smtp_proc.h"
13 static char **blacklists = NULL;
14 static size_t nblacklists = 0;
16 void usage(void);
17 enum filter_decision dnsbl_connect(char *, int, time_t, char *, char *,
18 uint64_t, uint64_t, struct smtp_filter_connect *);
20 int
21 main(int argc, char *argv[])
22 {
23 int ch;
24 size_t i;
25 char *line = NULL;
26 size_t linesize = 0;
27 ssize_t linelen;
28 char *msgid, *token, *ip;
29 const char *errstr;
30 int lookup;
32 if (pledge("stdio dns", NULL) == -1)
33 err(1, "pledge");
35 while ((ch = getopt(argc, argv, "b:f:r:")) != -1) {
36 switch (ch) {
37 case 'b':
38 blacklists = reallocarray(blacklists, nblacklists + 1,
39 sizeof(*blacklists));
40 if (blacklists == NULL)
41 err(1, NULL);
42 if ((blacklists[nblacklists] = strdup(optarg)) == NULL)
43 err(1, NULL);
44 nblacklists++;
45 break;
46 default:
47 usage();
48 }
49 }
51 if (nblacklists == 0)
52 errx(1, "No blacklist specified");
54 smtp_register_filter_connect(dnsbl_connect);
55 smtp_run();
57 return 0;
58 }
60 enum filter_decision
61 dnsbl_connect(char *type, int version, time_t tm, char *direction, char *phase,
62 uint64_t reqid, uint64_t token, struct smtp_filter_connect *params)
63 {
64 char query[255];
65 char reply[1500];
66 struct hostent *hent;
67 u_char *addr;
68 int i, try;
70 addr = (u_char *)&(params->addr);
71 for (i = 0; i < nblacklists; i++) {
72 if (params->af == AF_INET) {
73 if (snprintf(query, sizeof(query), "%u.%u.%u.%u.%s",
74 addr[3], addr[2], addr[1], addr[0],
75 blacklists[i]) >= sizeof(query))
76 errx(1, "Can't create query, domain too long");
77 } else if (params->af == AF_INET6) {
78 if (snprintf(query, sizeof(query), "%hhx.%hhx.%hhx.%hhx"
79 ".%hhx.%hhx.%hhx.%hhx.%hhx.%hhx.%hhx.%hhx.%hhx.%hhx"
80 ".%hhx.%hhx.%hhx.%hhx.%hhx.%hhx.%hhx.%hhx.%hhx.%hhx"
81 ".%hhx.%hhx.%hhx.%hhx.%hhx.%hhx.%hhx.%hhx.%s",
82 (u_char) (addr[15] & 0xf), (u_char) (addr[15] >> 4),
83 (u_char) (addr[14] & 0xf), (u_char) (addr[14] >> 4),
84 (u_char) (addr[13] & 0xf), (u_char) (addr[13] >> 4),
85 (u_char) (addr[12] & 0xf), (u_char) (addr[12] >> 4),
86 (u_char) (addr[11] & 0xf), (u_char) (addr[11] >> 4),
87 (u_char) (addr[10] & 0xf), (u_char) (addr[10] >> 4),
88 (u_char) (addr[9] & 0xf), (u_char) (addr[9] >> 4),
89 (u_char) (addr[8] & 0xf), (u_char) (addr[8] >> 4),
90 (u_char) (addr[7] & 0xf), (u_char) (addr[8] >> 4),
91 (u_char) (addr[6] & 0xf), (u_char) (addr[7] >> 4),
92 (u_char) (addr[5] & 0xf), (u_char) (addr[5] >> 4),
93 (u_char) (addr[4] & 0xf), (u_char) (addr[4] >> 4),
94 (u_char) (addr[3] & 0xf), (u_char) (addr[3] >> 4),
95 (u_char) (addr[2] & 0xf), (u_char) (addr[2] >> 4),
96 (u_char) (addr[1] & 0xf), (u_char) (addr[1] >> 4),
97 (u_char) (addr[0] & 0xf), (u_char) (addr[0] >> 4),
98 blacklists[i]) >= sizeof(query))
99 errx(1, "Can't create query, domain too long");
100 } else
101 errx(1, "Invalid address family received");
103 if ((hent = gethostbyname(query)) == NULL) {
104 if (h_errno == HOST_NOT_FOUND)
105 break;
106 if (h_errno != TRY_AGAIN) {
107 smtp_filter_disconnect(reqid, token,
108 "Blacklist check failed");
109 return FILTER_DISCONNECT;
111 } else {
112 smtp_filter_disconnect(reqid, token,
113 "Listed at %s", blacklists[i]);
114 return FILTER_DISCONNECT;
117 return FILTER_PROCEED;
120 __dead void
121 usage(void)
123 fprintf(stderr, "usage: %s [-b blacklist]\n",
124 getprogname());
125 exit(1);