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 "opensmtpd.h"
39 void *admd_message_new(struct osmtpd_ctx *);
40 void admd_message_free(struct osmtpd_ctx *, void *);
41 void admd_dataline(struct osmtpd_ctx *, const char *);
42 void admd_commit(struct osmtpd_ctx *);
43 void admd_err(struct admd_message *, const char *);
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");
77 if (strlcpy(authservid, argv[0], sizeof(authservid)) >=
79 osmtpd_errx(1, "authserv-id is too long");
81 if (gethostname(authservid, sizeof(authservid)) == -1)
82 osmtpd_err(1, "gethostname");
84 if (strchr(authservid, '\r') != NULL ||
85 strchr(authservid, '\n') != NULL)
86 osmtpd_errx(1, "ubsupported character in authserv-id");
88 osmtpd_local_message(admd_message_new, admd_message_free);
89 osmtpd_register_filter_dataline(admd_dataline);
90 osmtpd_register_filter_commit(admd_commit);
97 admd_message_new(struct osmtpd_ctx *ctx)
99 struct admd_message *msg;
101 if ((msg = malloc(sizeof(*msg))) == NULL)
102 osmtpd_err(1, "malloc");
107 msg->parsing_headers = 1;
116 admd_message_free(struct osmtpd_ctx *ctx, void *data)
118 struct admd_message *msg = data;
125 admd_dataline(struct osmtpd_ctx *ctx, const char *orig)
127 struct admd_message *msg = ctx->local_message;
128 const char *line = orig;
129 const char *msgauthid;
133 if (line[0] == '.' && line[1] =='\0')
134 osmtpd_filter_dataline(ctx, ".");
139 msg->parsing_headers = 0;
142 if (msg->parsing_headers) {
143 if (line[0] != ' ' && line[0] != '\t') {
145 msgauthid = admd_authservid(msg);
146 if (msgauthid == NULL && errno != EINVAL)
148 if (msgauthid != NULL &&
149 strcmp(msgauthid, authservid) == 0)
152 for (i = 0; i < msg->cachelen; i++)
153 osmtpd_filter_dataline(ctx,
154 "%s", msg->cache[i]);
160 if (strncasecmp(line, "Authentication-Results", 22) == 0) {
162 while (line[0] == ' ' || line[0] == '\t')
164 if (line++[0] == ':') {
166 admd_cache(msg, orig);
169 } else if (msg->inheader &&
170 (line[0] == ' ' || line[0] == '\t')) {
171 admd_cache(msg, orig);
176 osmtpd_filter_dataline(ctx, "%s", orig);
181 admd_commit(struct osmtpd_ctx *ctx)
183 struct admd_message *msg = ctx->local_message;
186 osmtpd_filter_disconnect(ctx, "Internal server error");
189 if (reject && msg->foundmatch) {
190 osmtpd_filter_disconnect(ctx, "Message contains "
191 "Authentication-Results header for authserv-id '%s'",
193 fprintf(stderr, "%016"PRIx64" Message contains "
194 "Authentication-Results header for authserv-id '%s': "
195 "rejected\n", ctx->reqid, authservid);
199 osmtpd_filter_proceed(ctx);
200 if (msg->foundmatch) {
201 fprintf(stderr, "%016"PRIx64" Message contains "
202 "Authentication-Results header for authserv-id '%s': "
203 "filtered\n", ctx->reqid, authservid);
205 fprintf(stderr, "%016"PRIx64" Message contains no "
206 "Authentication-Results header for authserv-id '%s'\n",
207 ctx->reqid, authservid);
211 admd_err(struct admd_message *message, const char *msg)
214 fprintf(stderr, "%s: %s\n", msg, strerror(errno));
218 admd_cache(struct admd_message *msg, const char *line)
222 if ((tcache = reallocarray(msg->cache, msg->cachelen + 1,
223 sizeof(*(msg->cache)))) == NULL) {
225 admd_err(msg, "malloc");
228 msg->cache[msg->cachelen] = strdup(line);
229 if (msg->cache[msg->cachelen] == NULL) {
231 admd_err(msg, "strdup");
234 msg->headerlen += strlen(line[0] == '.' ? line + 1 : line);
239 admd_authservid(struct admd_message *msg)
241 char *header0, *header, *line, *end;
245 headerlen = msg->headerlen + (msg->cachelen * 2) + 1;
246 header0 = header = malloc(headerlen);
247 if (header == NULL) {
248 admd_err(msg, "malloc");
252 for (i = 0; i < msg->cachelen; i++) {
253 line = msg->cache[i];
256 if (strlcat(header, line, headerlen) >= headerlen ||
257 strlcat(header, "\r\n", headerlen) >= headerlen) {
258 osmtpd_errx(1, "miscalculated header\n");
265 while (header[0] == ' ' || header[0] == '\t')
270 header = osmtpd_mheader_skip_cfws(header, 1);
272 if ((end = osmtpd_mheader_skip_value(header, 0)) == NULL) {
277 memmove(header0, header, end - header);
278 header0[end - header] = '\0';
284 admd_freecache(struct admd_message *msg)
286 while (msg->cachelen > 0) {
288 free(msg->cache[msg->cachelen]);
299 fprintf(stderr, "usage: filter-admdscrub [-rv] [-a authserv-id]\n");