From 2b4f6ebbb6ce456f0572032c9cf36693c988fa1b Mon Sep 17 00:00:00 2001 From: gilles Date: Sat, 17 Oct 2015 22:24:36 +0000 Subject: [PATCH] mailaddr_match() allows comparing two struct mailaddr taking into account catchall and +-tags ok millert@ and jung@ for util.c --- usr.sbin/smtpd/smtpd.h | 3 ++- usr.sbin/smtpd/util.c | 31 +++++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/usr.sbin/smtpd/smtpd.h b/usr.sbin/smtpd/smtpd.h index 9455c2bbe59..0c6228f0f41 100644 --- a/usr.sbin/smtpd/smtpd.h +++ b/usr.sbin/smtpd/smtpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: smtpd.h,v 1.477 2015/10/14 22:16:38 gilles Exp $ */ +/* $OpenBSD: smtpd.h,v 1.478 2015/10/17 22:24:36 gilles Exp $ */ /* * Copyright (c) 2008 Gilles Chehade @@ -1398,6 +1398,7 @@ int bsnprintf(char *, size_t, const char *, ...) int mkdirs(char *, mode_t); int safe_fclose(FILE *); int hostname_match(const char *, const char *); +int mailaddr_match(const struct mailaddr *, const struct mailaddr *); int valid_localpart(const char *); int valid_domainpart(const char *); int secure_file(int, char *, char *, uid_t, int); diff --git a/usr.sbin/smtpd/util.c b/usr.sbin/smtpd/util.c index 70961173512..41b2af5a736 100644 --- a/usr.sbin/smtpd/util.c +++ b/usr.sbin/smtpd/util.c @@ -1,4 +1,4 @@ -/* $OpenBSD: util.c,v 1.121 2015/10/17 12:59:52 gilles Exp $ */ +/* $OpenBSD: util.c,v 1.122 2015/10/17 22:24:36 gilles Exp $ */ /* * Copyright (c) 2000,2001 Markus Friedl. All rights reserved. @@ -226,7 +226,6 @@ mkdirs(char *path, mode_t mode) return 1; } - int ckdir(const char *path, mode_t mode, uid_t owner, gid_t group, int create) { @@ -439,6 +438,34 @@ hostname_match(const char *hostname, const char *pattern) return (*hostname == '\0' && *pattern == '\0'); } +int +mailaddr_match(const struct mailaddr *maddr1, const struct mailaddr *maddr2) +{ + struct mailaddr m1 = *maddr1; + struct mailaddr m2 = *maddr2; + char *p; + + /* catchall */ + if (m2.user[0] == '\0' && m2.domain[0] == '\0') + return 1; + + if (! hostname_match(m1.domain, m2.domain)) + return 0; + + if (m2.user[0]) { + /* if address from table has a tag, we must respect it */ + if (strchr(m2.user, '+') == NULL) { + /* otherwise, strip tag from session address if any */ + p = strchr(m1.user, '+'); + if (p) + *p = '\0'; + } + if (strcasecmp(m1.user, m2.user)) + return 0; + } + return 1; +} + int valid_localpart(const char *s) { -- 2.20.1