2 * Copyright (c) 2019 Martijn van Duren <martijn@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 #include "openbsd-compat.h"
26 #include "opensmtpd.h"
39 void admd_conf(const char *, const char *);
40 void *admd_message_new(struct osmtpd_ctx *);
41 void admd_message_free(struct osmtpd_ctx *, void *);
42 int admd_dataline(struct osmtpd_ctx *, const char *);
43 int admd_commit(struct osmtpd_ctx *);
44 void admd_cache(struct admd_message *, const char *);
45 const char *admd_authservid(struct admd_message *);
46 void admd_freecache(struct admd_message *);
53 main(int argc, char *argv[])
57 if (pledge("stdio", NULL) == -1)
58 osmtpd_err(1, "pledge");
60 while ((ch = getopt(argc, argv, "rv")) != -1) {
75 osmtpd_errx(1, "invalid authservid count");
79 osmtpd_local_message(admd_message_new, admd_message_free);
80 osmtpd_register_filter_dataline(admd_dataline);
81 osmtpd_register_filter_commit(admd_commit);
82 osmtpd_register_conf(admd_conf);
89 admd_conf(const char *key, const char *value)
92 if (authservid == NULL)
93 osmtpd_errx(1, "Didn't receive admd config option");
96 if (strcmp(key, "admd") == 0 && authservid == NULL) {
97 if ((authservid = strdup(value)) == NULL)
98 osmtpd_err(1, "malloc");
103 admd_message_new(struct osmtpd_ctx *ctx)
105 struct admd_message *msg;
107 if ((msg = malloc(sizeof(*msg))) == NULL)
108 osmtpd_err(1, "malloc");
112 msg->parsing_headers = 1;
121 admd_message_free(struct osmtpd_ctx *ctx, void *data)
123 struct admd_message *msg = data;
130 admd_dataline(struct osmtpd_ctx *ctx, const char *orig)
132 struct admd_message *msg = ctx->local_message;
133 const char *line = orig;
134 const char *msgauthid;
138 msg->parsing_headers = 0;
141 if (msg->parsing_headers) {
142 if (line[0] != ' ' && line[0] != '\t') {
144 msgauthid = admd_authservid(msg);
145 if (msgauthid == NULL && errno != EINVAL)
147 if (msgauthid != NULL &&
148 strcmp(msgauthid, authservid) == 0)
151 for (i = 0; i < msg->cachelen; i++)
152 osmtpd_filter_dataline(ctx,
153 "%s", msg->cache[i]);
159 if (strncasecmp(line, "Authentication-Results", 22) == 0) {
161 while (line[0] == ' ' || line[0] == '\t')
163 if (line++[0] == ':') {
165 admd_cache(msg, orig);
168 } else if (msg->inheader &&
169 (line[0] == ' ' || line[0] == '\t')) {
170 admd_cache(msg, orig);
175 osmtpd_filter_dataline(ctx, "%s", orig);
180 admd_commit(struct osmtpd_ctx *ctx)
182 struct admd_message *msg = ctx->local_message;
184 if (reject && msg->foundmatch) {
185 osmtpd_filter_disconnect(ctx, "Message contains "
186 "Authentication-Results header for authserv-id '%s'",
188 fprintf(stderr, "%016"PRIx64" Message contains "
189 "Authentication-Results header for authserv-id '%s': "
190 "rejected\n", ctx->reqid, authservid);
194 osmtpd_filter_proceed(ctx);
195 if (msg->foundmatch) {
196 fprintf(stderr, "%016"PRIx64" Message contains "
197 "Authentication-Results header for authserv-id '%s': "
198 "filtered\n", ctx->reqid, authservid);
200 fprintf(stderr, "%016"PRIx64" Message contains no "
201 "Authentication-Results header for authserv-id '%s'\n",
202 ctx->reqid, authservid);
208 admd_cache(struct admd_message *msg, const char *line)
212 if ((tcache = reallocarray(msg->cache, msg->cachelen + 1,
213 sizeof(*(msg->cache)))) == NULL) {
215 osmtpd_err(1, "malloc");
218 msg->cache[msg->cachelen] = strdup(line);
219 if (msg->cache[msg->cachelen] == NULL) {
221 osmtpd_err(1, "strdup");
224 msg->headerlen += strlen(line[0] == '.' ? line + 1 : line);
228 admd_authservid(struct admd_message *msg)
230 char *header0, *header, *line, *end;
234 headerlen = msg->headerlen + (msg->cachelen * 2) + 1;
235 header0 = header = malloc(headerlen);
236 if (header == NULL) {
237 osmtpd_err(1, "malloc");
241 for (i = 0; i < msg->cachelen; i++) {
242 line = msg->cache[i];
245 if (strlcat(header, line, headerlen) >= headerlen ||
246 strlcat(header, "\r\n", headerlen) >= headerlen) {
247 osmtpd_errx(1, "miscalculated header\n");
254 while (header[0] == ' ' || header[0] == '\t')
259 header = osmtpd_mheader_skip_cfws(header, 1);
261 if ((end = osmtpd_mheader_skip_value(header, 0)) == NULL) {
266 memmove(header0, header, end - header);
267 header0[end - header] = '\0';
273 admd_freecache(struct admd_message *msg)
275 while (msg->cachelen > 0) {
277 free(msg->cache[msg->cachelen]);
288 fprintf(stderr, "usage: filter-admdscrub [-rv] [-a authserv-id]\n");