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_conf(const char *, const char *);
40 void *admd_message_new(struct osmtpd_ctx *);
41 void admd_message_free(struct osmtpd_ctx *, void *);
42 void admd_dataline(struct osmtpd_ctx *, const char *);
43 void admd_commit(struct osmtpd_ctx *);
44 void admd_err(struct admd_message *, const char *);
45 void admd_cache(struct admd_message *, const char *);
46 const char *admd_authservid(struct admd_message *);
47 void admd_freecache(struct admd_message *);
54 main(int argc, char *argv[])
58 if (pledge("stdio", NULL) == -1)
59 osmtpd_err(1, "pledge");
61 while ((ch = getopt(argc, argv, "rv")) != -1) {
76 osmtpd_errx(1, "invalid authservid count");
80 osmtpd_local_message(admd_message_new, admd_message_free);
81 osmtpd_register_filter_dataline(admd_dataline);
82 osmtpd_register_filter_commit(admd_commit);
83 osmtpd_register_conf(admd_conf);
90 admd_conf(const char *key, const char *value)
93 if (authservid == NULL)
94 osmtpd_errx(1, "Didn't receieve admd config option");
97 if (strcmp(key, "admd") == 0 && authservid == NULL) {
98 if ((authservid = strdup(value)) == NULL)
99 osmtpd_err(1, "malloc");
104 admd_message_new(struct osmtpd_ctx *ctx)
106 struct admd_message *msg;
108 if ((msg = malloc(sizeof(*msg))) == NULL)
109 osmtpd_err(1, "malloc");
114 msg->parsing_headers = 1;
123 admd_message_free(struct osmtpd_ctx *ctx, void *data)
125 struct admd_message *msg = data;
132 admd_dataline(struct osmtpd_ctx *ctx, const char *orig)
134 struct admd_message *msg = ctx->local_message;
135 const char *line = orig;
136 const char *msgauthid;
140 if (line[0] == '.' && line[1] =='\0')
141 osmtpd_filter_dataline(ctx, ".");
146 msg->parsing_headers = 0;
149 if (msg->parsing_headers) {
150 if (line[0] != ' ' && line[0] != '\t') {
152 msgauthid = admd_authservid(msg);
153 if (msgauthid == NULL && errno != EINVAL)
155 if (msgauthid != NULL &&
156 strcmp(msgauthid, authservid) == 0)
159 for (i = 0; i < msg->cachelen; i++)
160 osmtpd_filter_dataline(ctx,
161 "%s", msg->cache[i]);
167 if (strncasecmp(line, "Authentication-Results", 22) == 0) {
169 while (line[0] == ' ' || line[0] == '\t')
171 if (line++[0] == ':') {
173 admd_cache(msg, orig);
176 } else if (msg->inheader &&
177 (line[0] == ' ' || line[0] == '\t')) {
178 admd_cache(msg, orig);
183 osmtpd_filter_dataline(ctx, "%s", orig);
188 admd_commit(struct osmtpd_ctx *ctx)
190 struct admd_message *msg = ctx->local_message;
193 osmtpd_filter_disconnect(ctx, "Internal server error");
196 if (reject && msg->foundmatch) {
197 osmtpd_filter_disconnect(ctx, "Message contains "
198 "Authentication-Results header for authserv-id '%s'",
200 fprintf(stderr, "%016"PRIx64" Message contains "
201 "Authentication-Results header for authserv-id '%s': "
202 "rejected\n", ctx->reqid, authservid);
206 osmtpd_filter_proceed(ctx);
207 if (msg->foundmatch) {
208 fprintf(stderr, "%016"PRIx64" Message contains "
209 "Authentication-Results header for authserv-id '%s': "
210 "filtered\n", ctx->reqid, authservid);
212 fprintf(stderr, "%016"PRIx64" Message contains no "
213 "Authentication-Results header for authserv-id '%s'\n",
214 ctx->reqid, authservid);
218 admd_err(struct admd_message *message, const char *msg)
221 fprintf(stderr, "%s: %s\n", msg, strerror(errno));
225 admd_cache(struct admd_message *msg, const char *line)
229 if ((tcache = reallocarray(msg->cache, msg->cachelen + 1,
230 sizeof(*(msg->cache)))) == NULL) {
232 admd_err(msg, "malloc");
235 msg->cache[msg->cachelen] = strdup(line);
236 if (msg->cache[msg->cachelen] == NULL) {
238 admd_err(msg, "strdup");
241 msg->headerlen += strlen(line[0] == '.' ? line + 1 : line);
246 admd_authservid(struct admd_message *msg)
248 char *header0, *header, *line, *end;
252 headerlen = msg->headerlen + (msg->cachelen * 2) + 1;
253 header0 = header = malloc(headerlen);
254 if (header == NULL) {
255 admd_err(msg, "malloc");
259 for (i = 0; i < msg->cachelen; i++) {
260 line = msg->cache[i];
263 if (strlcat(header, line, headerlen) >= headerlen ||
264 strlcat(header, "\r\n", headerlen) >= headerlen) {
265 osmtpd_errx(1, "miscalculated header\n");
272 while (header[0] == ' ' || header[0] == '\t')
277 header = osmtpd_mheader_skip_cfws(header, 1);
279 if ((end = osmtpd_mheader_skip_value(header, 0)) == NULL) {
284 memmove(header0, header, end - header);
285 header0[end - header] = '\0';
291 admd_freecache(struct admd_message *msg)
293 while (msg->cachelen > 0) {
295 free(msg->cache[msg->cachelen]);
306 fprintf(stderr, "usage: filter-admdscrub [-rv] [-a authserv-id]\n");