2024-02-02 15:33:42 -08:00
|
|
|
/* $OpenBSD: lka_session.c,v 1.100 2024/02/02 23:33:42 gilles Exp $ */
|
2011-05-16 14:05:51 -07:00
|
|
|
|
|
|
|
/*
|
2013-01-26 01:37:22 -08:00
|
|
|
* Copyright (c) 2011 Gilles Chehade <gilles@poolp.org>
|
2012-09-27 11:57:25 -07:00
|
|
|
* Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
|
2011-05-16 14:05:51 -07:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "smtpd.h"
|
|
|
|
#include "log.h"
|
|
|
|
|
2012-10-03 11:09:18 -07:00
|
|
|
#define EXPAND_DEPTH 10
|
|
|
|
|
2013-01-26 01:37:22 -08:00
|
|
|
#define F_WAITING 0x01
|
2012-09-21 06:23:07 -07:00
|
|
|
|
|
|
|
struct lka_session {
|
2013-01-26 01:37:22 -08:00
|
|
|
uint64_t id; /* given by smtp */
|
2012-09-27 11:57:25 -07:00
|
|
|
|
|
|
|
TAILQ_HEAD(, envelope) deliverylist;
|
|
|
|
struct expand expand;
|
|
|
|
|
|
|
|
int flags;
|
2013-01-26 01:37:22 -08:00
|
|
|
int error;
|
2013-05-24 10:03:14 -07:00
|
|
|
const char *errormsg;
|
2012-09-27 11:57:25 -07:00
|
|
|
struct envelope envelope;
|
|
|
|
struct xnodes nodes;
|
|
|
|
/* waiting for fwdrq */
|
|
|
|
struct rule *rule;
|
|
|
|
struct expandnode *node;
|
2012-09-21 06:23:07 -07:00
|
|
|
};
|
|
|
|
|
2013-01-26 01:37:22 -08:00
|
|
|
static void lka_expand(struct lka_session *, struct rule *,
|
|
|
|
struct expandnode *);
|
|
|
|
static void lka_submit(struct lka_session *, struct rule *,
|
|
|
|
struct expandnode *);
|
2012-09-27 11:57:25 -07:00
|
|
|
static void lka_resume(struct lka_session *);
|
2013-07-19 01:12:19 -07:00
|
|
|
|
2013-05-24 10:03:14 -07:00
|
|
|
static int init;
|
|
|
|
static struct tree sessions;
|
2012-09-18 06:42:39 -07:00
|
|
|
|
2011-05-16 14:05:51 -07:00
|
|
|
void
|
2013-01-26 01:37:22 -08:00
|
|
|
lka_session(uint64_t id, struct envelope *envelope)
|
2011-05-16 14:05:51 -07:00
|
|
|
{
|
2012-09-27 11:57:25 -07:00
|
|
|
struct lka_session *lks;
|
|
|
|
struct expandnode xn;
|
2011-05-16 14:05:51 -07:00
|
|
|
|
2013-05-24 10:03:14 -07:00
|
|
|
if (init == 0) {
|
|
|
|
init = 1;
|
|
|
|
tree_init(&sessions);
|
|
|
|
}
|
|
|
|
|
2018-05-31 14:06:12 -07:00
|
|
|
lks = xcalloc(1, sizeof(*lks));
|
2013-01-26 01:37:22 -08:00
|
|
|
lks->id = id;
|
2012-09-21 09:40:20 -07:00
|
|
|
RB_INIT(&lks->expand.tree);
|
2012-09-18 06:42:39 -07:00
|
|
|
TAILQ_INIT(&lks->deliverylist);
|
|
|
|
tree_xset(&sessions, lks->id, lks);
|
|
|
|
|
2013-01-26 01:37:22 -08:00
|
|
|
lks->envelope = *envelope;
|
2011-05-16 14:05:51 -07:00
|
|
|
|
2012-09-27 11:57:25 -07:00
|
|
|
TAILQ_INIT(&lks->nodes);
|
2013-12-26 09:25:32 -08:00
|
|
|
memset(&xn, 0, sizeof xn);
|
2012-09-27 11:57:25 -07:00
|
|
|
xn.type = EXPAND_ADDRESS;
|
2013-01-26 01:37:22 -08:00
|
|
|
xn.u.mailaddr = lks->envelope.rcpt;
|
2018-05-24 04:38:24 -07:00
|
|
|
lks->expand.parent = NULL;
|
2012-09-27 11:57:25 -07:00
|
|
|
lks->expand.rule = NULL;
|
|
|
|
lks->expand.queue = &lks->nodes;
|
|
|
|
expand_insert(&lks->expand, &xn);
|
|
|
|
lka_resume(lks);
|
2011-05-16 14:05:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
lka_session_forward_reply(struct forward_req *fwreq, int fd)
|
|
|
|
{
|
2013-01-26 01:37:22 -08:00
|
|
|
struct lka_session *lks;
|
2018-05-24 04:38:24 -07:00
|
|
|
struct dispatcher *dsp;
|
2013-01-26 01:37:22 -08:00
|
|
|
struct rule *rule;
|
|
|
|
struct expandnode *xn;
|
2013-05-24 10:03:14 -07:00
|
|
|
int ret;
|
2011-05-16 14:05:51 -07:00
|
|
|
|
2012-09-18 06:42:39 -07:00
|
|
|
lks = tree_xget(&sessions, fwreq->id);
|
2012-09-27 11:57:25 -07:00
|
|
|
xn = lks->node;
|
|
|
|
rule = lks->rule;
|
2011-05-21 11:04:51 -07:00
|
|
|
|
2012-09-27 11:57:25 -07:00
|
|
|
lks->flags &= ~F_WAITING;
|
2011-05-16 14:05:51 -07:00
|
|
|
|
2013-01-26 01:37:22 -08:00
|
|
|
switch (fwreq->status) {
|
|
|
|
case 0:
|
|
|
|
/* permanent failure while lookup ~/.forward */
|
2013-02-14 04:30:49 -08:00
|
|
|
log_trace(TRACE_EXPAND, "expand: ~/.forward failed for user %s",
|
2013-01-26 01:37:22 -08:00
|
|
|
fwreq->user);
|
|
|
|
lks->error = LKA_PERMFAIL;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if (fd == -1) {
|
2018-05-24 04:38:24 -07:00
|
|
|
dsp = dict_get(env->sc_dispatchers, lks->rule->dispatcher);
|
|
|
|
if (dsp->u.local.forward_only) {
|
2013-11-06 02:01:29 -08:00
|
|
|
log_trace(TRACE_EXPAND, "expand: no .forward "
|
|
|
|
"for user %s on forward-only rule", fwreq->user);
|
|
|
|
lks->error = LKA_TEMPFAIL;
|
|
|
|
}
|
2018-05-24 04:38:24 -07:00
|
|
|
else if (dsp->u.local.expand_only) {
|
2013-11-06 02:01:29 -08:00
|
|
|
log_trace(TRACE_EXPAND, "expand: no .forward "
|
|
|
|
"for user %s and no default action on rule", fwreq->user);
|
|
|
|
lks->error = LKA_PERMFAIL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
log_trace(TRACE_EXPAND, "expand: no .forward for "
|
|
|
|
"user %s, just deliver", fwreq->user);
|
|
|
|
lka_submit(lks, rule, xn);
|
|
|
|
}
|
2013-01-26 01:37:22 -08:00
|
|
|
}
|
|
|
|
else {
|
2018-05-24 04:38:24 -07:00
|
|
|
dsp = dict_get(env->sc_dispatchers, rule->dispatcher);
|
|
|
|
|
2013-01-26 01:37:22 -08:00
|
|
|
/* expand for the current user and rule */
|
|
|
|
lks->expand.rule = rule;
|
|
|
|
lks->expand.parent = xn;
|
2018-05-24 04:38:24 -07:00
|
|
|
|
2013-01-26 01:37:22 -08:00
|
|
|
/* forwards_get() will close the descriptor no matter what */
|
2013-05-24 10:03:14 -07:00
|
|
|
ret = forwards_get(fd, &lks->expand);
|
|
|
|
if (ret == -1) {
|
2013-02-14 04:30:49 -08:00
|
|
|
log_trace(TRACE_EXPAND, "expand: temporary "
|
2013-01-28 03:09:53 -08:00
|
|
|
"forward error for user %s", fwreq->user);
|
2013-01-26 01:37:22 -08:00
|
|
|
lks->error = LKA_TEMPFAIL;
|
|
|
|
}
|
2013-05-24 10:03:14 -07:00
|
|
|
else if (ret == 0) {
|
2018-05-24 04:38:24 -07:00
|
|
|
if (dsp->u.local.forward_only) {
|
2013-11-06 02:01:29 -08:00
|
|
|
log_trace(TRACE_EXPAND, "expand: empty .forward "
|
|
|
|
"for user %s on forward-only rule", fwreq->user);
|
|
|
|
lks->error = LKA_TEMPFAIL;
|
|
|
|
}
|
2018-05-24 04:38:24 -07:00
|
|
|
else if (dsp->u.local.expand_only) {
|
2013-11-06 02:01:29 -08:00
|
|
|
log_trace(TRACE_EXPAND, "expand: empty .forward "
|
|
|
|
"for user %s and no default action on rule", fwreq->user);
|
|
|
|
lks->error = LKA_PERMFAIL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
log_trace(TRACE_EXPAND, "expand: empty .forward "
|
|
|
|
"for user %s, just deliver", fwreq->user);
|
|
|
|
lka_submit(lks, rule, xn);
|
|
|
|
}
|
2013-05-24 10:03:14 -07:00
|
|
|
}
|
2011-05-16 14:05:51 -07:00
|
|
|
}
|
2013-01-26 01:37:22 -08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* temporary failure while looking up ~/.forward */
|
|
|
|
lks->error = LKA_TEMPFAIL;
|
2011-05-16 14:05:51 -07:00
|
|
|
}
|
2013-01-26 01:37:22 -08:00
|
|
|
|
2018-07-08 06:10:49 -07:00
|
|
|
if (lks->error == LKA_TEMPFAIL && lks->errormsg == NULL)
|
|
|
|
lks->errormsg = "424 4.2.4 Mailing list expansion problem";
|
|
|
|
if (lks->error == LKA_PERMFAIL && lks->errormsg == NULL)
|
|
|
|
lks->errormsg = "524 5.2.4 Mailing list expansion problem";
|
|
|
|
|
2012-09-27 11:57:25 -07:00
|
|
|
lka_resume(lks);
|
2011-05-16 14:05:51 -07:00
|
|
|
}
|
|
|
|
|
2012-09-27 11:57:25 -07:00
|
|
|
static void
|
|
|
|
lka_resume(struct lka_session *lks)
|
2011-05-16 14:05:51 -07:00
|
|
|
{
|
2012-09-27 11:57:25 -07:00
|
|
|
struct envelope *ep;
|
|
|
|
struct expandnode *xn;
|
2011-05-16 14:05:51 -07:00
|
|
|
|
2013-01-26 01:37:22 -08:00
|
|
|
if (lks->error)
|
2012-09-27 11:57:25 -07:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
/* pop next node and expand it */
|
2013-01-26 01:37:22 -08:00
|
|
|
while ((xn = TAILQ_FIRST(&lks->nodes))) {
|
2012-09-27 11:57:25 -07:00
|
|
|
TAILQ_REMOVE(&lks->nodes, xn, tq_entry);
|
|
|
|
lka_expand(lks, xn->rule, xn);
|
|
|
|
if (lks->flags & F_WAITING)
|
|
|
|
return;
|
2013-01-26 01:37:22 -08:00
|
|
|
if (lks->error)
|
2012-09-27 11:57:25 -07:00
|
|
|
goto error;
|
2011-05-16 14:05:51 -07:00
|
|
|
}
|
|
|
|
|
2012-09-27 11:57:25 -07:00
|
|
|
/* delivery list is empty, reject */
|
2011-05-16 14:05:51 -07:00
|
|
|
if (TAILQ_FIRST(&lks->deliverylist) == NULL) {
|
2013-02-14 04:30:49 -08:00
|
|
|
log_trace(TRACE_EXPAND, "expand: lka_done: expanded to empty "
|
2013-01-28 03:09:53 -08:00
|
|
|
"delivery list");
|
2013-01-26 01:37:22 -08:00
|
|
|
lks->error = LKA_PERMFAIL;
|
2018-07-08 06:10:49 -07:00
|
|
|
lks->errormsg = "524 5.2.4 Mailing list expansion problem";
|
2011-05-16 14:05:51 -07:00
|
|
|
}
|
2012-09-27 11:57:25 -07:00
|
|
|
error:
|
2013-01-26 01:37:22 -08:00
|
|
|
if (lks->error) {
|
2020-12-31 00:27:15 -08:00
|
|
|
m_create(p_dispatcher, IMSG_SMTP_EXPAND_RCPT, 0, 0, -1);
|
|
|
|
m_add_id(p_dispatcher, lks->id);
|
|
|
|
m_add_int(p_dispatcher, lks->error);
|
2013-05-24 10:03:14 -07:00
|
|
|
|
|
|
|
if (lks->errormsg)
|
2020-12-31 00:27:15 -08:00
|
|
|
m_add_string(p_dispatcher, lks->errormsg);
|
2013-05-24 10:03:14 -07:00
|
|
|
else {
|
|
|
|
if (lks->error == LKA_PERMFAIL)
|
2020-12-31 00:27:15 -08:00
|
|
|
m_add_string(p_dispatcher, "550 Invalid recipient");
|
2013-05-24 10:03:14 -07:00
|
|
|
else if (lks->error == LKA_TEMPFAIL)
|
2020-12-31 00:27:15 -08:00
|
|
|
m_add_string(p_dispatcher, "451 Temporary failure");
|
2013-05-24 10:03:14 -07:00
|
|
|
}
|
|
|
|
|
2020-12-31 00:27:15 -08:00
|
|
|
m_close(p_dispatcher);
|
2012-09-27 11:57:25 -07:00
|
|
|
while ((ep = TAILQ_FIRST(&lks->deliverylist)) != NULL) {
|
|
|
|
TAILQ_REMOVE(&lks->deliverylist, ep, entry);
|
|
|
|
free(ep);
|
|
|
|
}
|
2011-05-16 14:05:51 -07:00
|
|
|
}
|
2012-09-27 11:57:25 -07:00
|
|
|
else {
|
2013-01-26 01:37:22 -08:00
|
|
|
/* Process the delivery list and submit envelopes to queue */
|
2012-09-27 11:57:25 -07:00
|
|
|
while ((ep = TAILQ_FIRST(&lks->deliverylist)) != NULL) {
|
|
|
|
TAILQ_REMOVE(&lks->deliverylist, ep, entry);
|
2014-04-04 09:10:41 -07:00
|
|
|
m_create(p_queue, IMSG_LKA_ENVELOPE_SUBMIT, 0, 0, -1);
|
2013-01-26 01:37:22 -08:00
|
|
|
m_add_id(p_queue, lks->id);
|
|
|
|
m_add_envelope(p_queue, ep);
|
|
|
|
m_close(p_queue);
|
2012-09-27 11:57:25 -07:00
|
|
|
free(ep);
|
|
|
|
}
|
2013-01-26 01:37:22 -08:00
|
|
|
|
2014-04-04 09:10:41 -07:00
|
|
|
m_create(p_queue, IMSG_LKA_ENVELOPE_COMMIT, 0, 0, -1);
|
2013-01-26 01:37:22 -08:00
|
|
|
m_add_id(p_queue, lks->id);
|
|
|
|
m_close(p_queue);
|
2011-05-16 14:05:51 -07:00
|
|
|
}
|
|
|
|
|
2013-01-31 10:34:43 -08:00
|
|
|
expand_clear(&lks->expand);
|
2012-09-18 06:42:39 -07:00
|
|
|
tree_xpop(&sessions, lks->id);
|
2011-05-16 14:05:51 -07:00
|
|
|
free(lks);
|
|
|
|
}
|
|
|
|
|
2012-09-18 06:42:39 -07:00
|
|
|
static void
|
2012-09-27 11:57:25 -07:00
|
|
|
lka_expand(struct lka_session *lks, struct rule *rule, struct expandnode *xn)
|
2011-05-16 14:05:51 -07:00
|
|
|
{
|
2012-09-27 11:57:25 -07:00
|
|
|
struct forward_req fwreq;
|
|
|
|
struct envelope ep;
|
|
|
|
struct expandnode node;
|
2013-05-24 10:03:14 -07:00
|
|
|
struct mailaddr maddr;
|
2018-05-24 04:38:24 -07:00
|
|
|
struct dispatcher *dsp;
|
|
|
|
struct table *userbase;
|
2012-10-13 01:01:47 -07:00
|
|
|
int r;
|
2013-05-24 10:03:14 -07:00
|
|
|
union lookup lk;
|
2015-11-30 02:56:25 -08:00
|
|
|
char *tag;
|
2019-09-20 10:46:05 -07:00
|
|
|
const char *srs_decoded;
|
|
|
|
|
2012-10-03 11:09:18 -07:00
|
|
|
if (xn->depth >= EXPAND_DEPTH) {
|
2013-02-14 04:30:49 -08:00
|
|
|
log_trace(TRACE_EXPAND, "expand: lka_expand: node too deep.");
|
2013-01-26 01:37:22 -08:00
|
|
|
lks->error = LKA_PERMFAIL;
|
2018-07-08 06:10:49 -07:00
|
|
|
lks->errormsg = "524 5.2.4 Mailing list expansion problem";
|
2012-09-27 11:57:25 -07:00
|
|
|
return;
|
|
|
|
}
|
2011-05-16 14:05:51 -07:00
|
|
|
|
|
|
|
switch (xn->type) {
|
2012-09-24 01:56:12 -07:00
|
|
|
case EXPAND_INVALID:
|
|
|
|
case EXPAND_INCLUDE:
|
2012-09-27 11:57:25 -07:00
|
|
|
fatalx("lka_expand: unexpected type");
|
2012-09-24 01:56:12 -07:00
|
|
|
break;
|
2011-05-16 14:05:51 -07:00
|
|
|
|
2012-09-24 01:56:12 -07:00
|
|
|
case EXPAND_ADDRESS:
|
2013-01-28 03:09:53 -08:00
|
|
|
|
2013-02-14 04:30:49 -08:00
|
|
|
log_trace(TRACE_EXPAND, "expand: lka_expand: address: %s@%s "
|
2013-01-28 03:09:53 -08:00
|
|
|
"[depth=%d]",
|
2012-10-03 11:09:18 -07:00
|
|
|
xn->u.mailaddr.user, xn->u.mailaddr.domain, xn->depth);
|
2011-05-16 14:05:51 -07:00
|
|
|
|
2019-09-20 10:46:05 -07:00
|
|
|
|
2012-09-27 11:57:25 -07:00
|
|
|
ep = lks->envelope;
|
|
|
|
ep.dest = xn->u.mailaddr;
|
|
|
|
if (xn->parent) /* nodes with parent are forward addresses */
|
2013-01-26 01:37:22 -08:00
|
|
|
ep.flags |= EF_INTERNAL;
|
2018-05-24 04:38:24 -07:00
|
|
|
|
2019-09-20 10:46:05 -07:00
|
|
|
/* handle SRS */
|
2021-09-22 10:19:58 -07:00
|
|
|
if (env->sc_srs_key != NULL &&
|
|
|
|
ep.sender.user[0] == '\0' &&
|
2021-09-22 10:11:35 -07:00
|
|
|
(strncasecmp(ep.dest.user, "SRS0=", 5) == 0 ||
|
|
|
|
strncasecmp(ep.dest.user, "SRS1=", 5) == 0)) {
|
|
|
|
srs_decoded = srs_decode(mailaddr_to_text(&ep.dest));
|
2019-09-20 10:46:05 -07:00
|
|
|
if (srs_decoded &&
|
2021-09-22 10:11:35 -07:00
|
|
|
text_to_mailaddr(&ep.dest, srs_decoded)) {
|
|
|
|
/* flag envelope internal and override dest */
|
2019-09-20 10:46:05 -07:00
|
|
|
ep.flags |= EF_INTERNAL;
|
2021-09-22 10:11:35 -07:00
|
|
|
xn->u.mailaddr = ep.dest;
|
2019-09-20 10:46:05 -07:00
|
|
|
lks->envelope = ep;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
log_warn("SRS failed to decode: %s",
|
2021-09-22 10:11:35 -07:00
|
|
|
mailaddr_to_text(&ep.dest));
|
2019-09-20 10:46:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Pass the node through the ruleset */
|
2012-09-27 11:57:25 -07:00
|
|
|
rule = ruleset_match(&ep);
|
2018-05-24 04:38:24 -07:00
|
|
|
if (rule == NULL || rule->reject) {
|
2013-01-26 01:37:22 -08:00
|
|
|
lks->error = (errno == EAGAIN) ?
|
|
|
|
LKA_TEMPFAIL : LKA_PERMFAIL;
|
|
|
|
break;
|
2012-09-27 11:57:25 -07:00
|
|
|
}
|
2013-01-26 01:37:22 -08:00
|
|
|
|
2018-05-24 04:38:24 -07:00
|
|
|
dsp = dict_xget(env->sc_dispatchers, rule->dispatcher);
|
|
|
|
if (dsp->type == DISPATCHER_REMOTE) {
|
2012-09-27 11:57:25 -07:00
|
|
|
lka_submit(lks, rule, xn);
|
|
|
|
}
|
2018-05-24 04:38:24 -07:00
|
|
|
else if (dsp->u.local.table_virtual) {
|
2012-09-27 11:57:25 -07:00
|
|
|
/* expand */
|
|
|
|
lks->expand.rule = rule;
|
|
|
|
lks->expand.parent = xn;
|
2013-05-24 10:03:14 -07:00
|
|
|
|
|
|
|
/* temporary replace the mailaddr with a copy where
|
|
|
|
* we eventually strip the '+'-part before lookup.
|
|
|
|
*/
|
|
|
|
maddr = xn->u.mailaddr;
|
2015-11-30 03:14:01 -08:00
|
|
|
xlowercase(maddr.user, xn->u.mailaddr.user,
|
2013-05-24 10:03:14 -07:00
|
|
|
sizeof maddr.user);
|
|
|
|
r = aliases_virtual_get(&lks->expand, &maddr);
|
2012-10-13 01:01:47 -07:00
|
|
|
if (r == -1) {
|
2013-01-26 01:37:22 -08:00
|
|
|
lks->error = LKA_TEMPFAIL;
|
2013-02-14 04:30:49 -08:00
|
|
|
log_trace(TRACE_EXPAND, "expand: lka_expand: "
|
2013-01-26 01:37:22 -08:00
|
|
|
"error in virtual alias lookup");
|
2012-10-13 01:01:47 -07:00
|
|
|
}
|
|
|
|
else if (r == 0) {
|
2013-01-26 01:37:22 -08:00
|
|
|
lks->error = LKA_PERMFAIL;
|
2013-02-14 04:30:49 -08:00
|
|
|
log_trace(TRACE_EXPAND, "expand: lka_expand: "
|
2013-01-26 01:37:22 -08:00
|
|
|
"no aliases for virtual");
|
2012-09-27 11:57:25 -07:00
|
|
|
}
|
2018-07-08 06:10:49 -07:00
|
|
|
if (lks->error == LKA_TEMPFAIL && lks->errormsg == NULL)
|
|
|
|
lks->errormsg = "424 4.2.4 Mailing list expansion problem";
|
|
|
|
if (lks->error == LKA_PERMFAIL && lks->errormsg == NULL)
|
|
|
|
lks->errormsg = "524 5.2.4 Mailing list expansion problem";
|
2012-09-27 11:57:25 -07:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
lks->expand.rule = rule;
|
|
|
|
lks->expand.parent = xn;
|
2018-05-24 04:38:24 -07:00
|
|
|
xn->rule = rule;
|
|
|
|
|
2013-12-26 09:25:32 -08:00
|
|
|
memset(&node, 0, sizeof node);
|
2012-09-27 11:57:25 -07:00
|
|
|
node.type = EXPAND_USERNAME;
|
2015-11-30 03:14:01 -08:00
|
|
|
xlowercase(node.u.user, xn->u.mailaddr.user,
|
|
|
|
sizeof node.u.user);
|
2012-09-27 11:57:25 -07:00
|
|
|
expand_insert(&lks->expand, &node);
|
|
|
|
}
|
|
|
|
break;
|
2011-05-16 14:05:51 -07:00
|
|
|
|
2012-09-24 01:56:12 -07:00
|
|
|
case EXPAND_USERNAME:
|
2013-02-14 04:30:49 -08:00
|
|
|
log_trace(TRACE_EXPAND, "expand: lka_expand: username: %s "
|
2018-05-24 04:38:24 -07:00
|
|
|
"[depth=%d, sameuser=%d]",
|
|
|
|
xn->u.user, xn->depth, xn->sameuser);
|
2011-05-16 14:05:51 -07:00
|
|
|
|
2012-09-27 11:57:25 -07:00
|
|
|
/* expand aliases with the given rule */
|
2018-05-24 04:38:24 -07:00
|
|
|
dsp = dict_xget(env->sc_dispatchers, rule->dispatcher);
|
|
|
|
|
2012-09-27 11:57:25 -07:00
|
|
|
lks->expand.rule = rule;
|
|
|
|
lks->expand.parent = xn;
|
2018-05-24 04:38:24 -07:00
|
|
|
|
|
|
|
if (!xn->sameuser &&
|
|
|
|
(dsp->u.local.table_alias || dsp->u.local.table_virtual)) {
|
|
|
|
if (dsp->u.local.table_alias)
|
|
|
|
r = aliases_get(&lks->expand, xn->u.user);
|
|
|
|
if (dsp->u.local.table_virtual)
|
|
|
|
r = aliases_virtual_get(&lks->expand, &xn->u.mailaddr);
|
2012-10-13 01:01:47 -07:00
|
|
|
if (r == -1) {
|
2013-02-14 04:30:49 -08:00
|
|
|
log_trace(TRACE_EXPAND, "expand: lka_expand: "
|
2013-01-26 01:37:22 -08:00
|
|
|
"error in alias lookup");
|
|
|
|
lks->error = LKA_TEMPFAIL;
|
2018-07-08 06:10:49 -07:00
|
|
|
if (lks->errormsg == NULL)
|
|
|
|
lks->errormsg = "424 4.2.4 Mailing list expansion problem";
|
2012-10-13 01:01:47 -07:00
|
|
|
}
|
|
|
|
if (r)
|
|
|
|
break;
|
|
|
|
}
|
2012-09-27 11:57:25 -07:00
|
|
|
|
2015-11-30 02:56:25 -08:00
|
|
|
/* gilles+hackers@ -> gilles@ */
|
2018-12-27 07:41:50 -08:00
|
|
|
if ((tag = strchr(xn->u.user, *env->sc_subaddressing_delim)) != NULL) {
|
2015-11-30 02:56:25 -08:00
|
|
|
*tag++ = '\0';
|
2018-12-27 07:41:50 -08:00
|
|
|
(void)strlcpy(xn->subaddress, tag, sizeof xn->subaddress);
|
|
|
|
}
|
2015-11-30 02:56:25 -08:00
|
|
|
|
2018-12-28 03:40:29 -08:00
|
|
|
userbase = table_find(env, dsp->u.local.table_userbase);
|
2018-12-26 12:13:43 -08:00
|
|
|
r = table_lookup(userbase, K_USERINFO, xn->u.user, &lk);
|
2013-01-26 01:37:22 -08:00
|
|
|
if (r == -1) {
|
2013-02-14 04:30:49 -08:00
|
|
|
log_trace(TRACE_EXPAND, "expand: lka_expand: "
|
2013-01-26 01:37:22 -08:00
|
|
|
"backend error while searching user");
|
|
|
|
lks->error = LKA_TEMPFAIL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (r == 0) {
|
2013-02-14 04:30:49 -08:00
|
|
|
log_trace(TRACE_EXPAND, "expand: lka_expand: "
|
2013-01-26 01:37:22 -08:00
|
|
|
"user-part does not match system user");
|
|
|
|
lks->error = LKA_PERMFAIL;
|
2012-10-13 14:33:33 -07:00
|
|
|
break;
|
|
|
|
}
|
2018-05-24 04:38:24 -07:00
|
|
|
xn->realuser = 1;
|
2024-02-02 14:02:12 -08:00
|
|
|
xn->realuser_uid = lk.userinfo.uid;
|
2018-05-24 04:38:24 -07:00
|
|
|
|
|
|
|
if (xn->sameuser && xn->parent->forwarded) {
|
|
|
|
log_trace(TRACE_EXPAND, "expand: lka_expand: same "
|
|
|
|
"user, submitting");
|
|
|
|
lka_submit(lks, rule, xn);
|
|
|
|
break;
|
|
|
|
}
|
2012-10-13 14:33:33 -07:00
|
|
|
|
2024-02-02 15:33:42 -08:00
|
|
|
|
|
|
|
/* when alternate delivery user is provided,
|
|
|
|
* skip other users forward files.
|
|
|
|
*/
|
|
|
|
if (dsp->u.local.user) {
|
|
|
|
if (strcmp(dsp->u.local.user, xn->u.user) != 0) {
|
|
|
|
log_trace(TRACE_EXPAND, "expand: lka_expand: "
|
|
|
|
"alternate delivery user mismatch recipient "
|
|
|
|
"user, skip .forward, submitting");
|
|
|
|
lka_submit(lks, rule, xn);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-27 11:57:25 -07:00
|
|
|
/* no aliases found, query forward file */
|
|
|
|
lks->rule = rule;
|
|
|
|
lks->node = xn;
|
2018-05-24 04:38:24 -07:00
|
|
|
xn->forwarded = 1;
|
2013-10-28 02:14:58 -07:00
|
|
|
|
2013-12-26 09:25:32 -08:00
|
|
|
memset(&fwreq, 0, sizeof(fwreq));
|
2012-09-27 11:57:25 -07:00
|
|
|
fwreq.id = lks->id;
|
2013-05-24 10:03:14 -07:00
|
|
|
(void)strlcpy(fwreq.user, lk.userinfo.username, sizeof(fwreq.user));
|
|
|
|
(void)strlcpy(fwreq.directory, lk.userinfo.directory, sizeof(fwreq.directory));
|
|
|
|
fwreq.uid = lk.userinfo.uid;
|
|
|
|
fwreq.gid = lk.userinfo.gid;
|
2013-10-28 02:14:58 -07:00
|
|
|
|
2014-04-04 09:10:41 -07:00
|
|
|
m_compose(p_parent, IMSG_LKA_OPEN_FORWARD, 0, 0, -1,
|
2013-01-26 01:37:22 -08:00
|
|
|
&fwreq, sizeof(fwreq));
|
2012-09-27 11:57:25 -07:00
|
|
|
lks->flags |= F_WAITING;
|
|
|
|
break;
|
2011-05-16 14:05:51 -07:00
|
|
|
|
2012-09-24 01:56:12 -07:00
|
|
|
case EXPAND_FILENAME:
|
2024-02-02 14:02:12 -08:00
|
|
|
if (xn->parent->realuser && xn->parent->realuser_uid == 0) {
|
|
|
|
log_trace(TRACE_EXPAND, "expand: filename not allowed in root's forward");
|
|
|
|
lks->error = LKA_TEMPFAIL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-05-24 04:38:24 -07:00
|
|
|
dsp = dict_xget(env->sc_dispatchers, rule->dispatcher);
|
|
|
|
if (dsp->u.local.forward_only) {
|
2013-11-06 02:01:29 -08:00
|
|
|
log_trace(TRACE_EXPAND, "expand: filename matched on forward-only rule");
|
|
|
|
lks->error = LKA_TEMPFAIL;
|
|
|
|
break;
|
|
|
|
}
|
2013-02-14 04:30:49 -08:00
|
|
|
log_trace(TRACE_EXPAND, "expand: lka_expand: filename: %s "
|
2013-01-28 03:09:53 -08:00
|
|
|
"[depth=%d]", xn->u.buffer, xn->depth);
|
2012-09-27 11:57:25 -07:00
|
|
|
lka_submit(lks, rule, xn);
|
2012-09-24 01:56:12 -07:00
|
|
|
break;
|
2011-05-16 14:05:51 -07:00
|
|
|
|
2013-05-24 10:03:14 -07:00
|
|
|
case EXPAND_ERROR:
|
2018-05-24 04:38:24 -07:00
|
|
|
dsp = dict_xget(env->sc_dispatchers, rule->dispatcher);
|
|
|
|
if (dsp->u.local.forward_only) {
|
2013-11-06 02:01:29 -08:00
|
|
|
log_trace(TRACE_EXPAND, "expand: error matched on forward-only rule");
|
|
|
|
lks->error = LKA_TEMPFAIL;
|
|
|
|
break;
|
|
|
|
}
|
2013-05-24 10:03:14 -07:00
|
|
|
log_trace(TRACE_EXPAND, "expand: lka_expand: error: %s "
|
|
|
|
"[depth=%d]", xn->u.buffer, xn->depth);
|
|
|
|
if (xn->u.buffer[0] == '4')
|
|
|
|
lks->error = LKA_TEMPFAIL;
|
|
|
|
else if (xn->u.buffer[0] == '5')
|
|
|
|
lks->error = LKA_PERMFAIL;
|
|
|
|
lks->errormsg = xn->u.buffer;
|
|
|
|
break;
|
|
|
|
|
2012-09-24 01:56:12 -07:00
|
|
|
case EXPAND_FILTER:
|
2024-02-02 14:02:12 -08:00
|
|
|
if (xn->parent->realuser && xn->parent->realuser_uid == 0) {
|
|
|
|
log_trace(TRACE_EXPAND, "expand: filter not allowed in root's forward");
|
|
|
|
lks->error = LKA_TEMPFAIL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-05-24 04:38:24 -07:00
|
|
|
dsp = dict_xget(env->sc_dispatchers, rule->dispatcher);
|
|
|
|
if (dsp->u.local.forward_only) {
|
2013-11-06 02:01:29 -08:00
|
|
|
log_trace(TRACE_EXPAND, "expand: filter matched on forward-only rule");
|
|
|
|
lks->error = LKA_TEMPFAIL;
|
|
|
|
break;
|
|
|
|
}
|
2013-02-14 04:30:49 -08:00
|
|
|
log_trace(TRACE_EXPAND, "expand: lka_expand: filter: %s "
|
2013-01-28 03:09:53 -08:00
|
|
|
"[depth=%d]", xn->u.buffer, xn->depth);
|
2012-09-27 11:57:25 -07:00
|
|
|
lka_submit(lks, rule, xn);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-10 11:02:37 -07:00
|
|
|
static struct expandnode *
|
|
|
|
lka_find_ancestor(struct expandnode *xn, enum expand_type type)
|
|
|
|
{
|
2013-01-26 01:37:22 -08:00
|
|
|
while (xn && (xn->type != type))
|
2012-10-10 11:02:37 -07:00
|
|
|
xn = xn->parent;
|
|
|
|
if (xn == NULL) {
|
2013-10-26 05:27:58 -07:00
|
|
|
log_warnx("warn: lka_find_ancestor: no ancestors of type %d",
|
2013-01-26 01:37:22 -08:00
|
|
|
type);
|
2012-10-10 11:02:37 -07:00
|
|
|
fatalx(NULL);
|
|
|
|
}
|
|
|
|
return (xn);
|
|
|
|
}
|
|
|
|
|
2012-09-27 11:57:25 -07:00
|
|
|
static void
|
|
|
|
lka_submit(struct lka_session *lks, struct rule *rule, struct expandnode *xn)
|
|
|
|
{
|
|
|
|
struct envelope *ep;
|
2018-05-24 04:38:24 -07:00
|
|
|
struct dispatcher *dsp;
|
|
|
|
const char *user;
|
|
|
|
const char *format;
|
2012-09-27 11:57:25 -07:00
|
|
|
|
2018-05-31 14:06:12 -07:00
|
|
|
ep = xmemdup(&lks->envelope, sizeof *ep);
|
2018-05-24 04:38:24 -07:00
|
|
|
(void)strlcpy(ep->dispatcher, rule->dispatcher, sizeof ep->dispatcher);
|
2012-09-27 11:57:25 -07:00
|
|
|
|
2018-05-24 04:38:24 -07:00
|
|
|
dsp = dict_xget(env->sc_dispatchers, ep->dispatcher);
|
|
|
|
|
|
|
|
switch (dsp->type) {
|
|
|
|
case DISPATCHER_REMOTE:
|
2012-09-27 11:57:25 -07:00
|
|
|
if (xn->type != EXPAND_ADDRESS)
|
|
|
|
fatalx("lka_deliver: expect address");
|
2012-10-10 11:02:37 -07:00
|
|
|
ep->type = D_MTA;
|
2012-09-27 11:57:25 -07:00
|
|
|
ep->dest = xn->u.mailaddr;
|
|
|
|
break;
|
2018-05-24 04:38:24 -07:00
|
|
|
|
|
|
|
case DISPATCHER_BOUNCE:
|
|
|
|
case DISPATCHER_LOCAL:
|
|
|
|
if (xn->type != EXPAND_USERNAME &&
|
|
|
|
xn->type != EXPAND_FILENAME &&
|
|
|
|
xn->type != EXPAND_FILTER)
|
|
|
|
fatalx("lka_deliver: wrong type: %d", xn->type);
|
|
|
|
|
2012-09-27 11:57:25 -07:00
|
|
|
ep->type = D_MDA;
|
2012-10-10 11:02:37 -07:00
|
|
|
ep->dest = lka_find_ancestor(xn, EXPAND_ADDRESS)->u.mailaddr;
|
2018-12-27 07:41:50 -08:00
|
|
|
if (xn->type == EXPAND_USERNAME) {
|
2018-05-24 04:38:24 -07:00
|
|
|
(void)strlcpy(ep->mda_user, xn->u.user, sizeof(ep->mda_user));
|
2018-12-27 07:41:50 -08:00
|
|
|
(void)strlcpy(ep->mda_subaddress, xn->subaddress, sizeof(ep->mda_subaddress));
|
|
|
|
}
|
2012-10-10 11:02:37 -07:00
|
|
|
else {
|
2018-05-24 04:38:24 -07:00
|
|
|
user = !xn->parent->realuser ?
|
|
|
|
SMTPD_USER :
|
|
|
|
xn->parent->u.user;
|
|
|
|
(void)strlcpy(ep->mda_user, user, sizeof (ep->mda_user));
|
|
|
|
|
|
|
|
/* this battle needs to be fought ... */
|
2018-11-01 11:01:51 -07:00
|
|
|
if (xn->type == EXPAND_FILTER &&
|
|
|
|
strcmp(ep->mda_user, SMTPD_USER) == 0)
|
|
|
|
log_warnx("commands executed from aliases "
|
2018-05-24 04:38:24 -07:00
|
|
|
"run with %s privileges", SMTPD_USER);
|
|
|
|
|
2023-11-03 06:40:07 -07:00
|
|
|
format = "%s";
|
2018-05-24 04:38:24 -07:00
|
|
|
if (xn->type == EXPAND_FILENAME)
|
2018-07-25 03:19:28 -07:00
|
|
|
format = "/usr/libexec/mail.mboxfile -f %%{mbox.from} %s";
|
2018-05-24 04:38:24 -07:00
|
|
|
(void)snprintf(ep->mda_exec, sizeof(ep->mda_exec),
|
|
|
|
format, xn->u.buffer);
|
2012-10-16 04:10:38 -07:00
|
|
|
}
|
2012-09-24 01:56:12 -07:00
|
|
|
break;
|
2011-05-16 14:05:51 -07:00
|
|
|
}
|
|
|
|
|
2012-09-27 11:57:25 -07:00
|
|
|
TAILQ_INSERT_TAIL(&lks->deliverylist, ep, entry);
|
2011-05-16 14:05:51 -07:00
|
|
|
}
|