diff options
author | Eric Faurot <eric@cvs.openbsd.org> | 2017-11-21 12:20:35 +0000 |
---|---|---|
committer | Eric Faurot <eric@cvs.openbsd.org> | 2017-11-21 12:20:35 +0000 |
commit | 307e93e68501841c60a473251c7ffa926280a3cd (patch) | |
tree | 9c21280ad20cfb58c14cc7f0a6d6f5b4dc974382 /usr.sbin/smtpd | |
parent | 81b364c7b8e40c226b5dc4d2df5a1075910817ea (diff) |
no need to check the sending process in imsg handlers when there is no
ambiguity: just use a single switch.
ok gilles@ sunil@
Diffstat (limited to 'usr.sbin/smtpd')
-rw-r--r-- | usr.sbin/smtpd/ca.c | 125 | ||||
-rw-r--r-- | usr.sbin/smtpd/lka.c | 475 | ||||
-rw-r--r-- | usr.sbin/smtpd/mda.c | 595 | ||||
-rw-r--r-- | usr.sbin/smtpd/mta.c | 469 | ||||
-rw-r--r-- | usr.sbin/smtpd/queue.c | 825 | ||||
-rw-r--r-- | usr.sbin/smtpd/smtp.c | 83 | ||||
-rw-r--r-- | usr.sbin/smtpd/smtpd.c | 164 |
7 files changed, 1321 insertions, 1415 deletions
diff --git a/usr.sbin/smtpd/ca.c b/usr.sbin/smtpd/ca.c index aefac18824a..e4806726be5 100644 --- a/usr.sbin/smtpd/ca.c +++ b/usr.sbin/smtpd/ca.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ca.c,v 1.27 2017/05/17 14:00:06 deraadt Exp $ */ +/* $OpenBSD: ca.c,v 1.28 2017/11/21 12:20:34 eric Exp $ */ /* * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org> @@ -226,79 +226,70 @@ ca_imsg(struct mproc *p, struct imsg *imsg) if (imsg == NULL) ca_shutdown(); - if (p->proc == PROC_PARENT) { - switch (imsg->hdr.type) { - case IMSG_CONF_START: - return; - case IMSG_CONF_END: - ca_init(); - - /* Start fulfilling requests */ - mproc_enable(p_pony); - return; - } - } + switch (imsg->hdr.type) { + case IMSG_CONF_START: + return; + case IMSG_CONF_END: + ca_init(); + + /* Start fulfilling requests */ + mproc_enable(p_pony); + return; + + case IMSG_CTL_VERBOSE: + m_msg(&m, imsg); + m_get_int(&m, &v); + m_end(&m); + log_trace_verbose(v); + return; + + case IMSG_CTL_PROFILE: + m_msg(&m, imsg); + m_get_int(&m, &v); + m_end(&m); + profiling = v; + return; + + case IMSG_CA_PRIVENC: + case IMSG_CA_PRIVDEC: + m_msg(&m, imsg); + m_get_id(&m, &id); + m_get_string(&m, &pkiname); + m_get_data(&m, &from, &flen); + m_get_size(&m, &tlen); + m_get_size(&m, &padding); + m_end(&m); + + pki = dict_get(env->sc_pki_dict, pkiname); + if (pki == NULL || pki->pki_pkey == NULL || + (rsa = EVP_PKEY_get1_RSA(pki->pki_pkey)) == NULL) + fatalx("ca_imsg: invalid pki"); + + if ((to = calloc(1, tlen)) == NULL) + fatalx("ca_imsg: calloc"); - if (p->proc == PROC_CONTROL) { - switch (imsg->hdr.type) { - case IMSG_CTL_VERBOSE: - m_msg(&m, imsg); - m_get_int(&m, &v); - m_end(&m); - log_trace_verbose(v); - return; - case IMSG_CTL_PROFILE: - m_msg(&m, imsg); - m_get_int(&m, &v); - m_end(&m); - profiling = v; - return; - } - } - - if (p->proc == PROC_PONY) { switch (imsg->hdr.type) { case IMSG_CA_PRIVENC: + ret = RSA_private_encrypt(flen, from, to, rsa, + padding); + break; case IMSG_CA_PRIVDEC: - m_msg(&m, imsg); - m_get_id(&m, &id); - m_get_string(&m, &pkiname); - m_get_data(&m, &from, &flen); - m_get_size(&m, &tlen); - m_get_size(&m, &padding); - m_end(&m); - - pki = dict_get(env->sc_pki_dict, pkiname); - if (pki == NULL || pki->pki_pkey == NULL || - (rsa = EVP_PKEY_get1_RSA(pki->pki_pkey)) == NULL) - fatalx("ca_imsg: invalid pki"); - - if ((to = calloc(1, tlen)) == NULL) - fatalx("ca_imsg: calloc"); - - switch (imsg->hdr.type) { - case IMSG_CA_PRIVENC: - ret = RSA_private_encrypt(flen, from, to, rsa, - padding); - break; - case IMSG_CA_PRIVDEC: - ret = RSA_private_decrypt(flen, from, to, rsa, - padding); - break; - } + ret = RSA_private_decrypt(flen, from, to, rsa, + padding); + break; + } - m_create(p, imsg->hdr.type, 0, 0, -1); - m_add_id(p, id); - m_add_int(p, ret); - if (ret > 0) - m_add_data(p, to, (size_t)ret); - m_close(p); + m_create(p, imsg->hdr.type, 0, 0, -1); + m_add_id(p, id); + m_add_int(p, ret); + if (ret > 0) + m_add_data(p, to, (size_t)ret); + m_close(p); - free(to); - RSA_free(rsa); + free(to); + RSA_free(rsa); - return; - } + return; } errx(1, "ca_imsg: unexpected %s imsg", imsg_to_str(imsg->hdr.type)); diff --git a/usr.sbin/smtpd/lka.c b/usr.sbin/smtpd/lka.c index 22d186d6ac8..e09be22788a 100644 --- a/usr.sbin/smtpd/lka.c +++ b/usr.sbin/smtpd/lka.c @@ -1,4 +1,4 @@ -/* $OpenBSD: lka.c,v 1.199 2017/05/17 14:00:06 deraadt Exp $ */ +/* $OpenBSD: lka.c,v 1.200 2017/11/21 12:20:34 eric Exp $ */ /* * Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org> @@ -86,294 +86,273 @@ lka_imsg(struct mproc *p, struct imsg *imsg) if (imsg == NULL) lka_shutdown(); - if (imsg->hdr.type == IMSG_MTA_DNS_HOST || - imsg->hdr.type == IMSG_MTA_DNS_PTR || - imsg->hdr.type == IMSG_SMTP_DNS_PTR || - imsg->hdr.type == IMSG_MTA_DNS_MX || - imsg->hdr.type == IMSG_MTA_DNS_MX_PREFERENCE) { + switch (imsg->hdr.type) { + + case IMSG_MTA_DNS_HOST: + case IMSG_MTA_DNS_PTR: + case IMSG_SMTP_DNS_PTR: + case IMSG_MTA_DNS_MX: + case IMSG_MTA_DNS_MX_PREFERENCE: dns_imsg(p, imsg); return; - } - if (p->proc == PROC_PONY) { - switch (imsg->hdr.type) { - case IMSG_SMTP_CHECK_SENDER: - m_msg(&m, imsg); - m_get_id(&m, &reqid); - m_get_string(&m, &tablename); - m_get_string(&m, &username); - m_get_mailaddr(&m, &maddr); - m_end(&m); - - ret = lka_mailaddrmap(tablename, username, &maddr); - - m_create(p, IMSG_SMTP_CHECK_SENDER, 0, 0, -1); - m_add_id(p, reqid); - m_add_int(p, ret); - m_close(p); - return; + case IMSG_SMTP_CHECK_SENDER: + m_msg(&m, imsg); + m_get_id(&m, &reqid); + m_get_string(&m, &tablename); + m_get_string(&m, &username); + m_get_mailaddr(&m, &maddr); + m_end(&m); - case IMSG_SMTP_EXPAND_RCPT: - m_msg(&m, imsg); - m_get_id(&m, &reqid); - m_get_envelope(&m, &evp); - m_end(&m); - lka_session(reqid, &evp); - return; + ret = lka_mailaddrmap(tablename, username, &maddr); - case IMSG_SMTP_LOOKUP_HELO: - m_msg(&m, imsg); - m_get_id(&m, &reqid); - m_get_string(&m, &tablename); - m_get_sockaddr(&m, (struct sockaddr *)&ss); - m_end(&m); - - ret = lka_addrname(tablename, (struct sockaddr*)&ss, - &addrname); - - m_create(p, IMSG_SMTP_LOOKUP_HELO, 0, 0, -1); - m_add_id(p, reqid); - m_add_int(p, ret); - if (ret == LKA_OK) - m_add_string(p, addrname.name); - m_close(p); - return; + m_create(p, IMSG_SMTP_CHECK_SENDER, 0, 0, -1); + m_add_id(p, reqid); + m_add_int(p, ret); + m_close(p); + return; - case IMSG_SMTP_TLS_INIT: - case IMSG_MTA_TLS_INIT: - req_ca_cert = imsg->data; - resp_ca_cert.reqid = req_ca_cert->reqid; - - xlowercase(buf, req_ca_cert->name, sizeof(buf)); - log_debug("debug: lka: looking up pki \"%s\"", buf); - pki = dict_get(env->sc_pki_dict, buf); - if (pki == NULL) - if (req_ca_cert->fallback) - pki = dict_get(env->sc_pki_dict, "*"); - if (pki == NULL) { - resp_ca_cert.status = CA_FAIL; - m_compose(p, imsg->hdr.type, 0, 0, -1, &resp_ca_cert, - sizeof(resp_ca_cert)); - return; - } - resp_ca_cert.status = CA_OK; - resp_ca_cert.cert_len = pki->pki_cert_len; - (void)strlcpy(resp_ca_cert.name, pki->pki_name, sizeof resp_ca_cert.name); - iov[0].iov_base = &resp_ca_cert; - iov[0].iov_len = sizeof(resp_ca_cert); - iov[1].iov_base = pki->pki_cert; - iov[1].iov_len = pki->pki_cert_len; - m_composev(p, imsg->hdr.type, 0, 0, -1, iov, nitems(iov)); - return; + case IMSG_SMTP_EXPAND_RCPT: + m_msg(&m, imsg); + m_get_id(&m, &reqid); + m_get_envelope(&m, &evp); + m_end(&m); + lka_session(reqid, &evp); + return; - case IMSG_SMTP_TLS_VERIFY_CERT: - case IMSG_MTA_TLS_VERIFY_CERT: - req_ca_vrfy = xmemdup(imsg->data, sizeof *req_ca_vrfy, "lka:ca_vrfy"); - req_ca_vrfy->cert = xmemdup((char *)imsg->data + - sizeof *req_ca_vrfy, req_ca_vrfy->cert_len, "lka:ca_vrfy"); - req_ca_vrfy->chain_cert = xcalloc(req_ca_vrfy->n_chain, - sizeof (unsigned char *), "lka:ca_vrfy"); - req_ca_vrfy->chain_cert_len = xcalloc(req_ca_vrfy->n_chain, - sizeof (off_t), "lka:ca_vrfy"); - return; + case IMSG_SMTP_LOOKUP_HELO: + m_msg(&m, imsg); + m_get_id(&m, &reqid); + m_get_string(&m, &tablename); + m_get_sockaddr(&m, (struct sockaddr *)&ss); + m_end(&m); + + ret = lka_addrname(tablename, (struct sockaddr*)&ss, + &addrname); + + m_create(p, IMSG_SMTP_LOOKUP_HELO, 0, 0, -1); + m_add_id(p, reqid); + m_add_int(p, ret); + if (ret == LKA_OK) + m_add_string(p, addrname.name); + m_close(p); + return; - case IMSG_SMTP_TLS_VERIFY_CHAIN: - case IMSG_MTA_TLS_VERIFY_CHAIN: - if (req_ca_vrfy == NULL) - fatalx("lka:ca_vrfy: chain without a certificate"); - req_ca_vrfy_chain = imsg->data; - req_ca_vrfy->chain_cert[req_ca_vrfy->chain_offset] = xmemdup((char *)imsg->data + - sizeof *req_ca_vrfy_chain, req_ca_vrfy_chain->cert_len, "lka:ca_vrfy"); - req_ca_vrfy->chain_cert_len[req_ca_vrfy->chain_offset] = req_ca_vrfy_chain->cert_len; - req_ca_vrfy->chain_offset++; + case IMSG_SMTP_TLS_INIT: + case IMSG_MTA_TLS_INIT: + req_ca_cert = imsg->data; + resp_ca_cert.reqid = req_ca_cert->reqid; + + xlowercase(buf, req_ca_cert->name, sizeof(buf)); + log_debug("debug: lka: looking up pki \"%s\"", buf); + pki = dict_get(env->sc_pki_dict, buf); + if (pki == NULL) + if (req_ca_cert->fallback) + pki = dict_get(env->sc_pki_dict, "*"); + if (pki == NULL) { + resp_ca_cert.status = CA_FAIL; + m_compose(p, imsg->hdr.type, 0, 0, -1, &resp_ca_cert, + sizeof(resp_ca_cert)); return; + } + resp_ca_cert.status = CA_OK; + resp_ca_cert.cert_len = pki->pki_cert_len; + (void)strlcpy(resp_ca_cert.name, pki->pki_name, sizeof resp_ca_cert.name); + iov[0].iov_base = &resp_ca_cert; + iov[0].iov_len = sizeof(resp_ca_cert); + iov[1].iov_base = pki->pki_cert; + iov[1].iov_len = pki->pki_cert_len; + m_composev(p, imsg->hdr.type, 0, 0, -1, iov, nitems(iov)); + return; - case IMSG_SMTP_TLS_VERIFY: - case IMSG_MTA_TLS_VERIFY: - if (req_ca_vrfy == NULL) - fatalx("lka:ca_vrfy: verify without a certificate"); - lka_certificate_verify(imsg->hdr.type, req_ca_vrfy); - req_ca_vrfy = NULL; - return; + case IMSG_SMTP_TLS_VERIFY_CERT: + case IMSG_MTA_TLS_VERIFY_CERT: + req_ca_vrfy = xmemdup(imsg->data, sizeof *req_ca_vrfy, "lka:ca_vrfy"); + req_ca_vrfy->cert = xmemdup((char *)imsg->data + + sizeof *req_ca_vrfy, req_ca_vrfy->cert_len, "lka:ca_vrfy"); + req_ca_vrfy->chain_cert = xcalloc(req_ca_vrfy->n_chain, + sizeof (unsigned char *), "lka:ca_vrfy"); + req_ca_vrfy->chain_cert_len = xcalloc(req_ca_vrfy->n_chain, + sizeof (off_t), "lka:ca_vrfy"); + return; - case IMSG_SMTP_AUTHENTICATE: - m_msg(&m, imsg); - m_get_id(&m, &reqid); - m_get_string(&m, &tablename); - m_get_string(&m, &username); - m_get_string(&m, &password); - m_end(&m); - - if (!tablename[0]) { - m_create(p_parent, IMSG_LKA_AUTHENTICATE, - 0, 0, -1); - m_add_id(p_parent, reqid); - m_add_string(p_parent, username); - m_add_string(p_parent, password); - m_close(p_parent); - return; - } + case IMSG_SMTP_TLS_VERIFY_CHAIN: + case IMSG_MTA_TLS_VERIFY_CHAIN: + if (req_ca_vrfy == NULL) + fatalx("lka:ca_vrfy: chain without a certificate"); + req_ca_vrfy_chain = imsg->data; + req_ca_vrfy->chain_cert[req_ca_vrfy->chain_offset] = xmemdup((char *)imsg->data + + sizeof *req_ca_vrfy_chain, req_ca_vrfy_chain->cert_len, "lka:ca_vrfy"); + req_ca_vrfy->chain_cert_len[req_ca_vrfy->chain_offset] = req_ca_vrfy_chain->cert_len; + req_ca_vrfy->chain_offset++; + return; - ret = lka_authenticate(tablename, username, password); + case IMSG_SMTP_TLS_VERIFY: + case IMSG_MTA_TLS_VERIFY: + if (req_ca_vrfy == NULL) + fatalx("lka:ca_vrfy: verify without a certificate"); + lka_certificate_verify(imsg->hdr.type, req_ca_vrfy); + req_ca_vrfy = NULL; + return; - m_create(p, IMSG_SMTP_AUTHENTICATE, 0, 0, -1); - m_add_id(p, reqid); - m_add_int(p, ret); - m_close(p); + case IMSG_SMTP_AUTHENTICATE: + m_msg(&m, imsg); + m_get_id(&m, &reqid); + m_get_string(&m, &tablename); + m_get_string(&m, &username); + m_get_string(&m, &password); + m_end(&m); + + if (!tablename[0]) { + m_create(p_parent, IMSG_LKA_AUTHENTICATE, + 0, 0, -1); + m_add_id(p_parent, reqid); + m_add_string(p_parent, username); + m_add_string(p_parent, password); + m_close(p_parent); return; } - } - if (p->proc == PROC_PONY) { - switch (imsg->hdr.type) { - case IMSG_MDA_LOOKUP_USERINFO: - m_msg(&m, imsg); - m_get_id(&m, &reqid); - m_get_string(&m, &tablename); - m_get_string(&m, &username); - m_end(&m); - - ret = lka_userinfo(tablename, username, &userinfo); - - m_create(p, IMSG_MDA_LOOKUP_USERINFO, 0, 0, -1); - m_add_id(p, reqid); - m_add_int(p, ret); - if (ret == LKA_OK) - m_add_data(p, &userinfo, sizeof(userinfo)); - m_close(p); - return; - } - } + ret = lka_authenticate(tablename, username, password); + + m_create(p, IMSG_SMTP_AUTHENTICATE, 0, 0, -1); + m_add_id(p, reqid); + m_add_int(p, ret); + m_close(p); + return; - if (p->proc == PROC_PONY) { - switch (imsg->hdr.type) { + case IMSG_MDA_LOOKUP_USERINFO: + m_msg(&m, imsg); + m_get_id(&m, &reqid); + m_get_string(&m, &tablename); + m_get_string(&m, &username); + m_end(&m); + + ret = lka_userinfo(tablename, username, &userinfo); + + m_create(p, IMSG_MDA_LOOKUP_USERINFO, 0, 0, -1); + m_add_id(p, reqid); + m_add_int(p, ret); + if (ret == LKA_OK) + m_add_data(p, &userinfo, sizeof(userinfo)); + m_close(p); + return; - case IMSG_MTA_LOOKUP_CREDENTIALS: - m_msg(&m, imsg); - m_get_id(&m, &reqid); - m_get_string(&m, &tablename); - m_get_string(&m, &label); - m_end(&m); + case IMSG_MTA_LOOKUP_CREDENTIALS: + m_msg(&m, imsg); + m_get_id(&m, &reqid); + m_get_string(&m, &tablename); + m_get_string(&m, &label); + m_end(&m); - lka_credentials(tablename, label, buf, sizeof(buf)); + lka_credentials(tablename, label, buf, sizeof(buf)); - m_create(p, IMSG_MTA_LOOKUP_CREDENTIALS, 0, 0, -1); - m_add_id(p, reqid); - m_add_string(p, buf); - m_close(p); - return; + m_create(p, IMSG_MTA_LOOKUP_CREDENTIALS, 0, 0, -1); + m_add_id(p, reqid); + m_add_string(p, buf); + m_close(p); + return; - case IMSG_MTA_LOOKUP_SOURCE: - m_msg(&m, imsg); - m_get_id(&m, &reqid); - m_get_string(&m, &tablename); - m_end(&m); + case IMSG_MTA_LOOKUP_SOURCE: + m_msg(&m, imsg); + m_get_id(&m, &reqid); + m_get_string(&m, &tablename); + m_end(&m); - table = table_find(tablename, NULL); + table = table_find(tablename, NULL); - m_create(p, IMSG_MTA_LOOKUP_SOURCE, 0, 0, -1); - m_add_id(p, reqid); + m_create(p, IMSG_MTA_LOOKUP_SOURCE, 0, 0, -1); + m_add_id(p, reqid); - if (table == NULL) { - log_warn("warn: source address table %s missing", - tablename); + if (table == NULL) { + log_warn("warn: source address table %s missing", + tablename); + m_add_int(p, LKA_TEMPFAIL); + } + else { + ret = table_fetch(table, NULL, K_SOURCE, &lk); + if (ret == -1) m_add_int(p, LKA_TEMPFAIL); - } + else if (ret == 0) + m_add_int(p, LKA_PERMFAIL); else { - ret = table_fetch(table, NULL, K_SOURCE, &lk); - if (ret == -1) - m_add_int(p, LKA_TEMPFAIL); - else if (ret == 0) - m_add_int(p, LKA_PERMFAIL); - else { - m_add_int(p, LKA_OK); - m_add_sockaddr(p, - (struct sockaddr *)&lk.source.addr); - } + m_add_int(p, LKA_OK); + m_add_sockaddr(p, + (struct sockaddr *)&lk.source.addr); } - m_close(p); - return; - - case IMSG_MTA_LOOKUP_HELO: - m_msg(&m, imsg); - m_get_id(&m, &reqid); - m_get_string(&m, &tablename); - m_get_sockaddr(&m, (struct sockaddr *)&ss); - m_end(&m); - - ret = lka_addrname(tablename, (struct sockaddr*)&ss, - &addrname); - - m_create(p, IMSG_MTA_LOOKUP_HELO, 0, 0, -1); - m_add_id(p, reqid); - m_add_int(p, ret); - if (ret == LKA_OK) - m_add_string(p, addrname.name); - m_close(p); - return; - } - } + m_close(p); + return; - if (p->proc == PROC_PARENT) { - switch (imsg->hdr.type) { - case IMSG_CONF_START: - return; + case IMSG_MTA_LOOKUP_HELO: + m_msg(&m, imsg); + m_get_id(&m, &reqid); + m_get_string(&m, &tablename); + m_get_sockaddr(&m, (struct sockaddr *)&ss); + m_end(&m); + + ret = lka_addrname(tablename, (struct sockaddr*)&ss, + &addrname); + + m_create(p, IMSG_MTA_LOOKUP_HELO, 0, 0, -1); + m_add_id(p, reqid); + m_add_int(p, ret); + if (ret == LKA_OK) + m_add_string(p, addrname.name); + m_close(p); + return; - case IMSG_CONF_END: - if (tracing & TRACE_TABLES) - table_dump_all(); + case IMSG_CONF_START: + return; - /* fork & exec tables that need it */ - table_open_all(); + case IMSG_CONF_END: + if (tracing & TRACE_TABLES) + table_dump_all(); - /* revoke proc & exec */ - if (pledge("stdio rpath inet dns getpw recvfd", - NULL) == -1) - err(1, "pledge"); + /* fork & exec tables that need it */ + table_open_all(); - /* Start fulfilling requests */ - mproc_enable(p_pony); - return; + /* revoke proc & exec */ + if (pledge("stdio rpath inet dns getpw recvfd", + NULL) == -1) + err(1, "pledge"); - case IMSG_LKA_OPEN_FORWARD: - lka_session_forward_reply(imsg->data, imsg->fd); - return; + /* Start fulfilling requests */ + mproc_enable(p_pony); + return; - case IMSG_LKA_AUTHENTICATE: - imsg->hdr.type = IMSG_SMTP_AUTHENTICATE; - m_forward(p_pony, imsg); - return; - } - } + case IMSG_LKA_OPEN_FORWARD: + lka_session_forward_reply(imsg->data, imsg->fd); + return; - if (p->proc == PROC_CONTROL) { - switch (imsg->hdr.type) { + case IMSG_LKA_AUTHENTICATE: + imsg->hdr.type = IMSG_SMTP_AUTHENTICATE; + m_forward(p_pony, imsg); + return; - case IMSG_CTL_VERBOSE: - m_msg(&m, imsg); - m_get_int(&m, &v); - m_end(&m); - log_trace_verbose(v); - return; + case IMSG_CTL_VERBOSE: + m_msg(&m, imsg); + m_get_int(&m, &v); + m_end(&m); + log_trace_verbose(v); + return; - case IMSG_CTL_PROFILE: - m_msg(&m, imsg); - m_get_int(&m, &v); - m_end(&m); - profiling = v; - return; + case IMSG_CTL_PROFILE: + m_msg(&m, imsg); + m_get_int(&m, &v); + m_end(&m); + profiling = v; + return; - case IMSG_CTL_UPDATE_TABLE: - table = table_find(imsg->data, NULL); - if (table == NULL) { - log_warnx("warn: Lookup table not found: " - "\"%s\"", (char *)imsg->data); - return; - } - table_update(table); + case IMSG_CTL_UPDATE_TABLE: + table = table_find(imsg->data, NULL); + if (table == NULL) { + log_warnx("warn: Lookup table not found: " + "\"%s\"", (char *)imsg->data); return; } + table_update(table); + return; } errx(1, "lka_imsg: unexpected %s imsg", imsg_to_str(imsg->hdr.type)); diff --git a/usr.sbin/smtpd/mda.c b/usr.sbin/smtpd/mda.c index ead92a851c4..ea536eb02ef 100644 --- a/usr.sbin/smtpd/mda.c +++ b/usr.sbin/smtpd/mda.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mda.c,v 1.127 2017/07/31 16:45:03 gilles Exp $ */ +/* $OpenBSD: mda.c,v 1.128 2017/11/21 12:20:34 eric Exp $ */ /* * Copyright (c) 2008 Gilles Chehade <gilles@poolp.org> @@ -127,354 +127,343 @@ mda_imsg(struct mproc *p, struct imsg *imsg) int n; enum lka_resp_status status; - if (p->proc == PROC_LKA) { - switch (imsg->hdr.type) { - case IMSG_MDA_LOOKUP_USERINFO: - m_msg(&m, imsg); - m_get_id(&m, &reqid); - m_get_int(&m, (int *)&status); - if (status == LKA_OK) - m_get_data(&m, &data, &sz); - m_end(&m); - - u = tree_xget(&users, reqid); - - if (status == LKA_TEMPFAIL) - mda_fail(u, 0, - "Temporary failure in user lookup", - ESC_OTHER_ADDRESS_STATUS); - else if (status == LKA_PERMFAIL) - mda_fail(u, 1, - "Permanent failure in user lookup", - ESC_DESTINATION_MAILBOX_HAS_MOVED); - else { - if (sz != sizeof(u->userinfo)) - fatalx("mda: userinfo size mismatch"); - memmove(&u->userinfo, data, sz); - u->flags &= ~USER_WAITINFO; - u->flags |= USER_RUNNABLE; - TAILQ_INSERT_TAIL(&runnable, u, entry_runnable); - mda_drain(); + switch (imsg->hdr.type) { + case IMSG_MDA_LOOKUP_USERINFO: + m_msg(&m, imsg); + m_get_id(&m, &reqid); + m_get_int(&m, (int *)&status); + if (status == LKA_OK) + m_get_data(&m, &data, &sz); + m_end(&m); + + u = tree_xget(&users, reqid); + + if (status == LKA_TEMPFAIL) + mda_fail(u, 0, + "Temporary failure in user lookup", + ESC_OTHER_ADDRESS_STATUS); + else if (status == LKA_PERMFAIL) + mda_fail(u, 1, + "Permanent failure in user lookup", + ESC_DESTINATION_MAILBOX_HAS_MOVED); + else { + if (sz != sizeof(u->userinfo)) + fatalx("mda: userinfo size mismatch"); + memmove(&u->userinfo, data, sz); + u->flags &= ~USER_WAITINFO; + u->flags |= USER_RUNNABLE; + TAILQ_INSERT_TAIL(&runnable, u, entry_runnable); + mda_drain(); + } + return; + + case IMSG_QUEUE_DELIVER: + m_msg(&m, imsg); + m_get_envelope(&m, &evp); + m_end(&m); + + u = mda_user(&evp); + + if (u->evpcount >= env->sc_mda_task_hiwat) { + if (!(u->flags & USER_ONHOLD)) { + log_debug("debug: mda: hiwat reached for " + "user \"%s\": holding envelopes", + mda_user_to_text(u)); + u->flags |= USER_ONHOLD; } + } + + if (u->flags & USER_ONHOLD) { + u->flags |= USER_HOLDQ; + m_create(p_queue, IMSG_MDA_DELIVERY_HOLD, + 0, 0, -1); + m_add_evpid(p_queue, evp.id); + m_add_id(p_queue, u->id); + m_close(p_queue); return; } - } - if (p->proc == PROC_QUEUE) { - switch (imsg->hdr.type) { + e = mda_envelope(&evp); + TAILQ_INSERT_TAIL(&u->envelopes, e, entry); + u->evpcount += 1; + stat_increment("mda.pending", 1); - case IMSG_QUEUE_DELIVER: - m_msg(&m, imsg); - m_get_envelope(&m, &evp); - m_end(&m); + if (!(u->flags & USER_RUNNABLE) && + !(u->flags & USER_WAITINFO)) { + u->flags |= USER_RUNNABLE; + TAILQ_INSERT_TAIL(&runnable, u, entry_runnable); + } - u = mda_user(&evp); + mda_drain(); + return; - if (u->evpcount >= env->sc_mda_task_hiwat) { - if (!(u->flags & USER_ONHOLD)) { - log_debug("debug: mda: hiwat reached for " - "user \"%s\": holding envelopes", - mda_user_to_text(u)); - u->flags |= USER_ONHOLD; - } - } + case IMSG_MDA_OPEN_MESSAGE: + m_msg(&m, imsg); + m_get_id(&m, &reqid); + m_end(&m); - if (u->flags & USER_ONHOLD) { - u->flags |= USER_HOLDQ; - m_create(p_queue, IMSG_MDA_DELIVERY_HOLD, - 0, 0, -1); - m_add_evpid(p_queue, evp.id); - m_add_id(p_queue, u->id); - m_close(p_queue); - return; - } + s = tree_xget(&sessions, reqid); + e = s->evp; - e = mda_envelope(&evp); - TAILQ_INSERT_TAIL(&u->envelopes, e, entry); - u->evpcount += 1; - stat_increment("mda.pending", 1); + if (imsg->fd == -1) { + log_debug("debug: mda: cannot get message fd"); + mda_queue_tempfail(e->id, + "Cannot get message fd", + ESC_OTHER_MAIL_SYSTEM_STATUS); + mda_log(e, "TempFail", "Cannot get message fd"); + mda_done(s); + return; + } - if (!(u->flags & USER_RUNNABLE) && - !(u->flags & USER_WAITINFO)) { - u->flags |= USER_RUNNABLE; - TAILQ_INSERT_TAIL(&runnable, u, entry_runnable); - } + log_debug("debug: mda: got message fd %d " + "for session %016"PRIx64 " evpid %016"PRIx64, + imsg->fd, s->id, e->id); - mda_drain(); + if ((s->datafp = fdopen(imsg->fd, "r")) == NULL) { + log_warn("warn: mda: fdopen"); + close(imsg->fd); + mda_queue_tempfail(e->id, "fdopen failed", + ESC_OTHER_MAIL_SYSTEM_STATUS); + mda_log(e, "TempFail", "fdopen failed"); + mda_done(s); return; + } - case IMSG_MDA_OPEN_MESSAGE: - m_msg(&m, imsg); - m_get_id(&m, &reqid); - m_end(&m); + /* check delivery loop */ + if (mda_check_loop(s->datafp, e)) { + log_debug("debug: mda: loop detected"); + mda_queue_loop(e->id); + mda_log(e, "PermFail", "Loop detected"); + mda_done(s); + return; + } - s = tree_xget(&sessions, reqid); - e = s->evp; + n = 0; + /* + * prepend "From " separator ... for + * A_MDA and A_FILENAME backends only + */ + if (e->method == A_MDA || e->method == A_FILENAME) { + time(&now); + if (e->sender[0]) + n = io_printf(s->io, "From %s %s", + e->sender, ctime(&now)); + else + n = io_printf(s->io, + "From MAILER-DAEMON@%s %s", + env->sc_hostname, ctime(&now)); + } + if (n != -1) { + /* start queueing delivery headers */ + if (e->sender[0]) + /* + * XXX: remove existing Return-Path, + * if any + */ + n = io_printf(s->io, + "Return-Path: %s\n" + "Delivered-To: %s\n", + e->sender, + e->rcpt ? e->rcpt : e->dest); + else + n = io_printf(s->io, + "Delivered-To: %s\n", + e->rcpt ? e->rcpt : e->dest); + } + if (n == -1) { + log_warn("warn: mda: " + "fail to write delivery info"); + mda_queue_tempfail(e->id, "Out of memory", + ESC_OTHER_MAIL_SYSTEM_STATUS); + mda_log(e, "TempFail", "Out of memory"); + mda_done(s); + return; + } - if (imsg->fd == -1) { - log_debug("debug: mda: cannot get message fd"); + /* request parent to fork a helper process */ + userinfo = &s->user->userinfo; + memset(&deliver, 0, sizeof deliver); + switch (e->method) { + case A_MDA: + deliver.mode = A_MDA; + deliver.userinfo = *userinfo; + (void)strlcpy(deliver.user, userinfo->username, + sizeof(deliver.user)); + if (strlcpy(deliver.to, e->buffer, + sizeof(deliver.to)) + >= sizeof(deliver.to)) { mda_queue_tempfail(e->id, - "Cannot get message fd", + "mda command too long", ESC_OTHER_MAIL_SYSTEM_STATUS); - mda_log(e, "TempFail", "Cannot get message fd"); + mda_log(e, "TempFail", + "mda command too long"); mda_done(s); return; } + break; - log_debug("debug: mda: got message fd %d " - "for session %016"PRIx64 " evpid %016"PRIx64, - imsg->fd, s->id, e->id); + case A_MBOX: + /* + * MBOX is a special case as we MUST + * deliver as root, just override the uid. + */ + deliver.mode = A_MBOX; + deliver.userinfo = *userinfo; + deliver.userinfo.uid = 0; + (void)strlcpy(deliver.user, "root", + sizeof(deliver.user)); + (void)strlcpy(deliver.from, e->sender, + sizeof(deliver.from)); + (void)strlcpy(deliver.to, userinfo->username, + sizeof(deliver.to)); + break; - if ((s->datafp = fdopen(imsg->fd, "r")) == NULL) { - log_warn("warn: mda: fdopen"); - close(imsg->fd); - mda_queue_tempfail(e->id, "fdopen failed", + case A_MAILDIR: + deliver.mode = A_MAILDIR; + deliver.userinfo = *userinfo; + (void)strlcpy(deliver.user, userinfo->username, + sizeof(deliver.user)); + (void)strlcpy(deliver.dest, e->dest, + sizeof(deliver.dest)); + if (strlcpy(deliver.to, e->buffer, + sizeof(deliver.to)) + >= sizeof(deliver.to)) { + log_warn("warn: mda: " + "deliver buffer too large"); + mda_queue_tempfail(e->id, + "Maildir path too long", ESC_OTHER_MAIL_SYSTEM_STATUS); - mda_log(e, "TempFail", "fdopen failed"); + mda_log(e, "TempFail", + "Maildir path too long"); mda_done(s); return; } + break; - /* check delivery loop */ - if (mda_check_loop(s->datafp, e)) { - log_debug("debug: mda: loop detected"); - mda_queue_loop(e->id); - mda_log(e, "PermFail", "Loop detected"); + case A_FILENAME: + deliver.mode = A_FILENAME; + deliver.userinfo = *userinfo; + (void)strlcpy(deliver.user, userinfo->username, + sizeof deliver.user); + if (strlcpy(deliver.to, e->buffer, + sizeof(deliver.to)) + >= sizeof(deliver.to)) { + log_warn("warn: mda: " + "deliver buffer too large"); + mda_queue_tempfail(e->id, + "filename path too long", + ESC_OTHER_MAIL_SYSTEM_STATUS); + mda_log(e, "TempFail", + "filename path too long"); mda_done(s); return; } + break; - n = 0; - /* - * prepend "From " separator ... for - * A_MDA and A_FILENAME backends only - */ - if (e->method == A_MDA || e->method == A_FILENAME) { - time(&now); - if (e->sender[0]) - n = io_printf(s->io, "From %s %s", - e->sender, ctime(&now)); - else - n = io_printf(s->io, - "From MAILER-DAEMON@%s %s", - env->sc_hostname, ctime(&now)); - } - if (n != -1) { - /* start queueing delivery headers */ - if (e->sender[0]) - /* - * XXX: remove existing Return-Path, - * if any - */ - n = io_printf(s->io, - "Return-Path: %s\n" - "Delivered-To: %s\n", - e->sender, - e->rcpt ? e->rcpt : e->dest); - else - n = io_printf(s->io, - "Delivered-To: %s\n", - e->rcpt ? e->rcpt : e->dest); - } - if (n == -1) { + case A_LMTP: + deliver.mode = A_LMTP; + deliver.userinfo = *userinfo; + (void)strlcpy(deliver.user, e->user, + sizeof(deliver.user)); + (void)strlcpy(deliver.from, e->sender, + sizeof(deliver.from)); + (void)strlcpy(deliver.dest, e->dest, + sizeof(deliver.dest)); + if (strlcpy(deliver.to, e->buffer, + sizeof(deliver.to)) + >= sizeof(deliver.to)) { log_warn("warn: mda: " - "fail to write delivery info"); - mda_queue_tempfail(e->id, "Out of memory", + "deliver buffer too large"); + mda_queue_tempfail(e->id, + "socket path too long", ESC_OTHER_MAIL_SYSTEM_STATUS); - mda_log(e, "TempFail", "Out of memory"); + mda_log(e, "TempFail", + "socket path too long"); mda_done(s); return; } + break; - /* request parent to fork a helper process */ - userinfo = &s->user->userinfo; - memset(&deliver, 0, sizeof deliver); - switch (e->method) { - case A_MDA: - deliver.mode = A_MDA; - deliver.userinfo = *userinfo; - (void)strlcpy(deliver.user, userinfo->username, - sizeof(deliver.user)); - if (strlcpy(deliver.to, e->buffer, - sizeof(deliver.to)) - >= sizeof(deliver.to)) { - mda_queue_tempfail(e->id, - "mda command too long", - ESC_OTHER_MAIL_SYSTEM_STATUS); - mda_log(e, "TempFail", - "mda command too long"); - mda_done(s); - return; - } - break; - - case A_MBOX: - /* - * MBOX is a special case as we MUST - * deliver as root, just override the uid. - */ - deliver.mode = A_MBOX; - deliver.userinfo = *userinfo; - deliver.userinfo.uid = 0; - (void)strlcpy(deliver.user, "root", - sizeof(deliver.user)); - (void)strlcpy(deliver.from, e->sender, - sizeof(deliver.from)); - (void)strlcpy(deliver.to, userinfo->username, - sizeof(deliver.to)); - break; - - case A_MAILDIR: - deliver.mode = A_MAILDIR; - deliver.userinfo = *userinfo; - (void)strlcpy(deliver.user, userinfo->username, - sizeof(deliver.user)); - (void)strlcpy(deliver.dest, e->dest, - sizeof(deliver.dest)); - if (strlcpy(deliver.to, e->buffer, - sizeof(deliver.to)) - >= sizeof(deliver.to)) { - log_warn("warn: mda: " - "deliver buffer too large"); - mda_queue_tempfail(e->id, - "Maildir path too long", - ESC_OTHER_MAIL_SYSTEM_STATUS); - mda_log(e, "TempFail", - "Maildir path too long"); - mda_done(s); - return; - } - break; - - case A_FILENAME: - deliver.mode = A_FILENAME; - deliver.userinfo = *userinfo; - (void)strlcpy(deliver.user, userinfo->username, - sizeof deliver.user); - if (strlcpy(deliver.to, e->buffer, - sizeof(deliver.to)) - >= sizeof(deliver.to)) { - log_warn("warn: mda: " - "deliver buffer too large"); - mda_queue_tempfail(e->id, - "filename path too long", - ESC_OTHER_MAIL_SYSTEM_STATUS); - mda_log(e, "TempFail", - "filename path too long"); - mda_done(s); - return; - } - break; - - case A_LMTP: - deliver.mode = A_LMTP; - deliver.userinfo = *userinfo; - (void)strlcpy(deliver.user, e->user, - sizeof(deliver.user)); - (void)strlcpy(deliver.from, e->sender, - sizeof(deliver.from)); - (void)strlcpy(deliver.dest, e->dest, - sizeof(deliver.dest)); - if (strlcpy(deliver.to, e->buffer, - sizeof(deliver.to)) - >= sizeof(deliver.to)) { - log_warn("warn: mda: " - "deliver buffer too large"); - mda_queue_tempfail(e->id, - "socket path too long", - ESC_OTHER_MAIL_SYSTEM_STATUS); - mda_log(e, "TempFail", - "socket path too long"); - mda_done(s); - return; - } - break; + default: + errx(1, "mda: unknown delivery method: %d", + e->method); + } - default: - errx(1, "mda: unknown delivery method: %d", - e->method); - } + log_debug("debug: mda: querying mda fd " + "for session %016"PRIx64 " evpid %016"PRIx64, + s->id, s->evp->id); - log_debug("debug: mda: querying mda fd " - "for session %016"PRIx64 " evpid %016"PRIx64, - s->id, s->evp->id); + m_create(p_parent, IMSG_MDA_FORK, 0, 0, -1); + m_add_id(p_parent, reqid); + m_add_data(p_parent, &deliver, sizeof(deliver)); + m_close(p_parent); + return; - m_create(p_parent, IMSG_MDA_FORK, 0, 0, -1); - m_add_id(p_parent, reqid); - m_add_data(p_parent, &deliver, sizeof(deliver)); - m_close(p_parent); + case IMSG_MDA_FORK: + m_msg(&m, imsg); + m_get_id(&m, &reqid); + m_end(&m); + + s = tree_xget(&sessions, reqid); + e = s->evp; + if (imsg->fd == -1) { + log_warn("warn: mda: fail to retrieve mda fd"); + mda_queue_tempfail(e->id, "Cannot get mda fd", + ESC_OTHER_MAIL_SYSTEM_STATUS); + mda_log(e, "TempFail", "Cannot get mda fd"); + mda_done(s); return; } - } - if (p->proc == PROC_PARENT) { - switch (imsg->hdr.type) { - case IMSG_MDA_FORK: - m_msg(&m, imsg); - m_get_id(&m, &reqid); - m_end(&m); - - s = tree_xget(&sessions, reqid); - e = s->evp; - if (imsg->fd == -1) { - log_warn("warn: mda: fail to retrieve mda fd"); - mda_queue_tempfail(e->id, "Cannot get mda fd", - ESC_OTHER_MAIL_SYSTEM_STATUS); - mda_log(e, "TempFail", "Cannot get mda fd"); - mda_done(s); - return; - } - - log_debug("debug: mda: got mda fd %d " - "for session %016"PRIx64 " evpid %016"PRIx64, - imsg->fd, s->id, s->evp->id); - - io_set_nonblocking(imsg->fd); - io_set_fd(s->io, imsg->fd); - io_set_write(s->io); - return; + log_debug("debug: mda: got mda fd %d " + "for session %016"PRIx64 " evpid %016"PRIx64, + imsg->fd, s->id, s->evp->id); - case IMSG_MDA_DONE: - m_msg(&m, imsg); - m_get_id(&m, &reqid); - m_get_string(&m, &parent_error); - m_end(&m); + io_set_nonblocking(imsg->fd); + io_set_fd(s->io, imsg->fd); + io_set_write(s->io); + return; - s = tree_xget(&sessions, reqid); - e = s->evp; - /* - * Grab last line of mda stdout/stderr if available. - */ - out[0] = '\0'; - if (imsg->fd != -1) - mda_getlastline(imsg->fd, out, sizeof(out)); - /* - * Choose between parent's description of error and - * child's output, the latter having preference over - * the former. - */ - error = NULL; - if (strcmp(parent_error, "exited okay") == 0) { - if (s->datafp || (s->io && io_queued(s->io))) - error = "mda exited prematurely"; - } else - error = out[0] ? out : parent_error; - - /* update queue entry */ - if (error) { - mda_queue_tempfail(e->id, error, - ESC_OTHER_MAIL_SYSTEM_STATUS); - (void)snprintf(buf, sizeof buf, - "Error (%s)", error); - mda_log(e, "TempFail", buf); - } - else { - mda_queue_ok(e->id); - mda_log(e, "Ok", "Delivered"); - } - mda_done(s); - return; + case IMSG_MDA_DONE: + m_msg(&m, imsg); + m_get_id(&m, &reqid); + m_get_string(&m, &parent_error); + m_end(&m); + + s = tree_xget(&sessions, reqid); + e = s->evp; + /* + * Grab last line of mda stdout/stderr if available. + */ + out[0] = '\0'; + if (imsg->fd != -1) + mda_getlastline(imsg->fd, out, sizeof(out)); + /* + * Choose between parent's description of error and + * child's output, the latter having preference over + * the former. + */ + error = NULL; + if (strcmp(parent_error, "exited okay") == 0) { + if (s->datafp || (s->io && io_queued(s->io))) + error = "mda exited prematurely"; + } else + error = out[0] ? out : parent_error; + + /* update queue entry */ + if (error) { + mda_queue_tempfail(e->id, error, + ESC_OTHER_MAIL_SYSTEM_STATUS); + (void)snprintf(buf, sizeof buf, + "Error (%s)", error); + mda_log(e, "TempFail", buf); } + else { + mda_queue_ok(e->id); + mda_log(e, "Ok", "Delivered"); + } + mda_done(s); + return; } errx(1, "mda_imsg: unexpected %s imsg", imsg_to_str(imsg->hdr.type)); diff --git a/usr.sbin/smtpd/mta.c b/usr.sbin/smtpd/mta.c index d15f86c347b..2006eac813a 100644 --- a/usr.sbin/smtpd/mta.c +++ b/usr.sbin/smtpd/mta.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mta.c,v 1.205 2017/09/15 11:50:39 eric Exp $ */ +/* $OpenBSD: mta.c,v 1.206 2017/11/21 12:20:34 eric Exp $ */ /* * Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org> @@ -198,270 +198,257 @@ mta_imsg(struct mproc *p, struct imsg *imsg) void *iter; uint64_t u64; - if (p->proc == PROC_QUEUE) { - switch (imsg->hdr.type) { - - case IMSG_QUEUE_TRANSFER: - m_msg(&m, imsg); - m_get_envelope(&m, &evp); - m_end(&m); - mta_handle_envelope(&evp); - return; - - case IMSG_MTA_OPEN_MESSAGE: - mta_session_imsg(p, imsg); - return; - } - } + switch (imsg->hdr.type) { + case IMSG_QUEUE_TRANSFER: + m_msg(&m, imsg); + m_get_envelope(&m, &evp); + m_end(&m); + mta_handle_envelope(&evp); + return; - if (p->proc == PROC_LKA) { - switch (imsg->hdr.type) { + case IMSG_MTA_OPEN_MESSAGE: + mta_session_imsg(p, imsg); + return; - case IMSG_MTA_LOOKUP_CREDENTIALS: - m_msg(&m, imsg); - m_get_id(&m, &reqid); - m_get_string(&m, &secret); - m_end(&m); - relay = tree_xpop(&wait_secret, reqid); - mta_on_secret(relay, secret[0] ? secret : NULL); - return; + case IMSG_MTA_LOOKUP_CREDENTIALS: + m_msg(&m, imsg); + m_get_id(&m, &reqid); + m_get_string(&m, &secret); + m_end(&m); + relay = tree_xpop(&wait_secret, reqid); + mta_on_secret(relay, secret[0] ? secret : NULL); + return; - case IMSG_MTA_LOOKUP_SOURCE: - m_msg(&m, imsg); - m_get_id(&m, &reqid); - m_get_int(&m, &status); - if (status == LKA_OK) - m_get_sockaddr(&m, (struct sockaddr*)&ss); - m_end(&m); - - relay = tree_xpop(&wait_source, reqid); - mta_on_source(relay, (status == LKA_OK) ? - mta_source((struct sockaddr *)&ss) : NULL); - return; + case IMSG_MTA_LOOKUP_SOURCE: + m_msg(&m, imsg); + m_get_id(&m, &reqid); + m_get_int(&m, &status); + if (status == LKA_OK) + m_get_sockaddr(&m, (struct sockaddr*)&ss); + m_end(&m); - case IMSG_MTA_LOOKUP_HELO: - mta_session_imsg(p, imsg); - return; + relay = tree_xpop(&wait_source, reqid); + mta_on_source(relay, (status == LKA_OK) ? + mta_source((struct sockaddr *)&ss) : NULL); + return; - case IMSG_MTA_DNS_HOST: - m_msg(&m, imsg); - m_get_id(&m, &reqid); - m_get_sockaddr(&m, (struct sockaddr*)&ss); - m_get_int(&m, &preference); - m_end(&m); - domain = tree_xget(&wait_mx, reqid); - mx = xcalloc(1, sizeof *mx, "mta: mx"); - mx->host = mta_host((struct sockaddr*)&ss); - mx->preference = preference; - TAILQ_FOREACH(imx, &domain->mxs, entry) { - if (imx->preference > mx->preference) { - TAILQ_INSERT_BEFORE(imx, mx, entry); - return; - } - } - TAILQ_INSERT_TAIL(&domain->mxs, mx, entry); - return; + case IMSG_MTA_LOOKUP_HELO: + mta_session_imsg(p, imsg); + return; - case IMSG_MTA_DNS_HOST_END: - m_msg(&m, imsg); - m_get_id(&m, &reqid); - m_get_int(&m, &dnserror); - m_end(&m); - domain = tree_xpop(&wait_mx, reqid); - domain->mxstatus = dnserror; - if (domain->mxstatus == DNS_OK) { - log_debug("debug: MXs for domain %s:", - domain->name); - TAILQ_FOREACH(mx, &domain->mxs, entry) - log_debug(" %s preference %d", - sa_to_text(mx->host->sa), - mx->preference); - } - else { - log_debug("debug: Failed MX query for %s:", - domain->name); + case IMSG_MTA_DNS_HOST: + m_msg(&m, imsg); + m_get_id(&m, &reqid); + m_get_sockaddr(&m, (struct sockaddr*)&ss); + m_get_int(&m, &preference); + m_end(&m); + domain = tree_xget(&wait_mx, reqid); + mx = xcalloc(1, sizeof *mx, "mta: mx"); + mx->host = mta_host((struct sockaddr*)&ss); + mx->preference = preference; + TAILQ_FOREACH(imx, &domain->mxs, entry) { + if (imx->preference > mx->preference) { + TAILQ_INSERT_BEFORE(imx, mx, entry); + return; } - domain->lastmxquery = time(NULL); - waitq_run(&domain->mxs, domain); - return; + } + TAILQ_INSERT_TAIL(&domain->mxs, mx, entry); + return; - case IMSG_MTA_DNS_MX_PREFERENCE: - m_msg(&m, imsg); - m_get_id(&m, &reqid); - m_get_int(&m, &dnserror); - if (dnserror == 0) - m_get_int(&m, &preference); - m_end(&m); - - relay = tree_xpop(&wait_preference, reqid); - if (dnserror) { - log_warnx("warn: Couldn't find backup " - "preference for %s: error %d", - mta_relay_to_text(relay), dnserror); - preference = INT_MAX; - } - mta_on_preference(relay, preference); - return; + case IMSG_MTA_DNS_HOST_END: + m_msg(&m, imsg); + m_get_id(&m, &reqid); + m_get_int(&m, &dnserror); + m_end(&m); + domain = tree_xpop(&wait_mx, reqid); + domain->mxstatus = dnserror; + if (domain->mxstatus == DNS_OK) { + log_debug("debug: MXs for domain %s:", + domain->name); + TAILQ_FOREACH(mx, &domain->mxs, entry) + log_debug(" %s preference %d", + sa_to_text(mx->host->sa), + mx->preference); + } + else { + log_debug("debug: Failed MX query for %s:", + domain->name); + } + domain->lastmxquery = time(NULL); + waitq_run(&domain->mxs, domain); + return; - case IMSG_MTA_DNS_PTR: - mta_session_imsg(p, imsg); - return; + case IMSG_MTA_DNS_MX_PREFERENCE: + m_msg(&m, imsg); + m_get_id(&m, &reqid); + m_get_int(&m, &dnserror); + if (dnserror == 0) + m_get_int(&m, &preference); + m_end(&m); + + relay = tree_xpop(&wait_preference, reqid); + if (dnserror) { + log_warnx("warn: Couldn't find backup " + "preference for %s: error %d", + mta_relay_to_text(relay), dnserror); + preference = INT_MAX; + } + mta_on_preference(relay, preference); + return; - case IMSG_MTA_TLS_INIT: - mta_session_imsg(p, imsg); - return; + case IMSG_MTA_DNS_PTR: + mta_session_imsg(p, imsg); + return; - case IMSG_MTA_TLS_VERIFY: - mta_session_imsg(p, imsg); - return; - } - } + case IMSG_MTA_TLS_INIT: + mta_session_imsg(p, imsg); + return; - if (p->proc == PROC_CONTROL) { - switch (imsg->hdr.type) { + case IMSG_MTA_TLS_VERIFY: + mta_session_imsg(p, imsg); + return; - case IMSG_CTL_RESUME_ROUTE: - u64 = *((uint64_t *)imsg->data); - if (u64) - log_debug("resuming route: %llu", - (unsigned long long)u64); - else - log_debug("resuming all routes"); - SPLAY_FOREACH(route, mta_route_tree, &routes) { - if (u64 && route->id != u64) - continue; - - if (route->flags & ROUTE_DISABLED) { - log_info("smtp-out: Enabling route %s per admin request", - mta_route_to_text(route)); - if (!runq_cancel(runq_route, NULL, route)) { - log_warnx("warn: route not on runq"); - fatalx("exiting"); - } - route->flags &= ~ROUTE_DISABLED; - route->flags |= ROUTE_NEW; - route->nerror = 0; - route->penalty = 0; - mta_route_unref(route); /* from mta_route_disable */ + case IMSG_CTL_RESUME_ROUTE: + u64 = *((uint64_t *)imsg->data); + if (u64) + log_debug("resuming route: %llu", + (unsigned long long)u64); + else + log_debug("resuming all routes"); + SPLAY_FOREACH(route, mta_route_tree, &routes) { + if (u64 && route->id != u64) + continue; + + if (route->flags & ROUTE_DISABLED) { + log_info("smtp-out: Enabling route %s per admin request", + mta_route_to_text(route)); + if (!runq_cancel(runq_route, NULL, route)) { + log_warnx("warn: route not on runq"); + fatalx("exiting"); } - - if (u64) - break; + route->flags &= ~ROUTE_DISABLED; + route->flags |= ROUTE_NEW; + route->nerror = 0; + route->penalty = 0; + mta_route_unref(route); /* from mta_route_disable */ } - return; - case IMSG_CTL_MTA_SHOW_HOSTS: - t = time(NULL); - SPLAY_FOREACH(host, mta_host_tree, &hosts) { - (void)snprintf(buf, sizeof(buf), - "%s %s refcount=%d nconn=%zu lastconn=%s", - sockaddr_to_text(host->sa), - host->ptrname, - host->refcount, - host->nconn, - host->lastconn ? duration_to_text(t - host->lastconn) : "-"); - m_compose(p, IMSG_CTL_MTA_SHOW_HOSTS, - imsg->hdr.peerid, 0, -1, - buf, strlen(buf) + 1); - } - m_compose(p, IMSG_CTL_MTA_SHOW_HOSTS, imsg->hdr.peerid, - 0, -1, NULL, 0); - return; + if (u64) + break; + } + return; - case IMSG_CTL_MTA_SHOW_RELAYS: - t = time(NULL); - SPLAY_FOREACH(relay, mta_relay_tree, &relays) - mta_relay_show(relay, p, imsg->hdr.peerid, t); - m_compose(p, IMSG_CTL_MTA_SHOW_RELAYS, imsg->hdr.peerid, - 0, -1, NULL, 0); - return; + case IMSG_CTL_MTA_SHOW_HOSTS: + t = time(NULL); + SPLAY_FOREACH(host, mta_host_tree, &hosts) { + (void)snprintf(buf, sizeof(buf), + "%s %s refcount=%d nconn=%zu lastconn=%s", + sockaddr_to_text(host->sa), + host->ptrname, + host->refcount, + host->nconn, + host->lastconn ? duration_to_text(t - host->lastconn) : "-"); + m_compose(p, IMSG_CTL_MTA_SHOW_HOSTS, + imsg->hdr.peerid, 0, -1, + buf, strlen(buf) + 1); + } + m_compose(p, IMSG_CTL_MTA_SHOW_HOSTS, imsg->hdr.peerid, + 0, -1, NULL, 0); + return; - case IMSG_CTL_MTA_SHOW_ROUTES: - SPLAY_FOREACH(route, mta_route_tree, &routes) { - v = runq_pending(runq_route, NULL, route, &t); - (void)snprintf(buf, sizeof(buf), - "%llu. %s %c%c%c%c nconn=%zu nerror=%d penalty=%d timeout=%s", - (unsigned long long)route->id, - mta_route_to_text(route), - route->flags & ROUTE_NEW ? 'N' : '-', - route->flags & ROUTE_DISABLED ? 'D' : '-', - route->flags & ROUTE_RUNQ ? 'Q' : '-', - route->flags & ROUTE_KEEPALIVE ? 'K' : '-', - route->nconn, - route->nerror, - route->penalty, - v ? duration_to_text(t - time(NULL)) : "-"); - m_compose(p, IMSG_CTL_MTA_SHOW_ROUTES, - imsg->hdr.peerid, 0, -1, - buf, strlen(buf) + 1); - } - m_compose(p, IMSG_CTL_MTA_SHOW_ROUTES, imsg->hdr.peerid, - 0, -1, NULL, 0); - return; + case IMSG_CTL_MTA_SHOW_RELAYS: + t = time(NULL); + SPLAY_FOREACH(relay, mta_relay_tree, &relays) + mta_relay_show(relay, p, imsg->hdr.peerid, t); + m_compose(p, IMSG_CTL_MTA_SHOW_RELAYS, imsg->hdr.peerid, + 0, -1, NULL, 0); + return; - case IMSG_CTL_MTA_SHOW_HOSTSTATS: - iter = NULL; - while (dict_iter(&hoststat, &iter, &hostname, - (void **)&hs)) { - (void)snprintf(buf, sizeof(buf), - "%s|%llu|%s", - hostname, (unsigned long long) hs->tm, - hs->error); - m_compose(p, IMSG_CTL_MTA_SHOW_HOSTSTATS, - imsg->hdr.peerid, 0, -1, - buf, strlen(buf) + 1); - } + case IMSG_CTL_MTA_SHOW_ROUTES: + SPLAY_FOREACH(route, mta_route_tree, &routes) { + v = runq_pending(runq_route, NULL, route, &t); + (void)snprintf(buf, sizeof(buf), + "%llu. %s %c%c%c%c nconn=%zu nerror=%d penalty=%d timeout=%s", + (unsigned long long)route->id, + mta_route_to_text(route), + route->flags & ROUTE_NEW ? 'N' : '-', + route->flags & ROUTE_DISABLED ? 'D' : '-', + route->flags & ROUTE_RUNQ ? 'Q' : '-', + route->flags & ROUTE_KEEPALIVE ? 'K' : '-', + route->nconn, + route->nerror, + route->penalty, + v ? duration_to_text(t - time(NULL)) : "-"); + m_compose(p, IMSG_CTL_MTA_SHOW_ROUTES, + imsg->hdr.peerid, 0, -1, + buf, strlen(buf) + 1); + } + m_compose(p, IMSG_CTL_MTA_SHOW_ROUTES, imsg->hdr.peerid, + 0, -1, NULL, 0); + return; + + case IMSG_CTL_MTA_SHOW_HOSTSTATS: + iter = NULL; + while (dict_iter(&hoststat, &iter, &hostname, + (void **)&hs)) { + (void)snprintf(buf, sizeof(buf), + "%s|%llu|%s", + hostname, (unsigned long long) hs->tm, + hs->error); m_compose(p, IMSG_CTL_MTA_SHOW_HOSTSTATS, - imsg->hdr.peerid, - 0, -1, NULL, 0); - return; + imsg->hdr.peerid, 0, -1, + buf, strlen(buf) + 1); + } + m_compose(p, IMSG_CTL_MTA_SHOW_HOSTSTATS, + imsg->hdr.peerid, + 0, -1, NULL, 0); + return; - case IMSG_CTL_MTA_BLOCK: - m_msg(&m, imsg); - m_get_sockaddr(&m, (struct sockaddr*)&ss); - m_get_string(&m, &dom); - m_end(&m); - source = mta_source((struct sockaddr*)&ss); - if (*dom != '\0') { - if (!(strlcpy(buf, dom, sizeof(buf)) - >= sizeof(buf))) - mta_block(source, buf); - } - else - mta_block(source, NULL); - mta_source_unref(source); - m_compose(p, IMSG_CTL_OK, imsg->hdr.peerid, 0, -1, NULL, 0); - return; + case IMSG_CTL_MTA_BLOCK: + m_msg(&m, imsg); + m_get_sockaddr(&m, (struct sockaddr*)&ss); + m_get_string(&m, &dom); + m_end(&m); + source = mta_source((struct sockaddr*)&ss); + if (*dom != '\0') { + if (!(strlcpy(buf, dom, sizeof(buf)) + >= sizeof(buf))) + mta_block(source, buf); + } + else + mta_block(source, NULL); + mta_source_unref(source); + m_compose(p, IMSG_CTL_OK, imsg->hdr.peerid, 0, -1, NULL, 0); + return; - case IMSG_CTL_MTA_UNBLOCK: - m_msg(&m, imsg); - m_get_sockaddr(&m, (struct sockaddr*)&ss); - m_get_string(&m, &dom); - m_end(&m); - source = mta_source((struct sockaddr*)&ss); - if (*dom != '\0') { - if (!(strlcpy(buf, dom, sizeof(buf)) - >= sizeof(buf))) - mta_unblock(source, buf); - } - else - mta_unblock(source, NULL); - mta_source_unref(source); - m_compose(p, IMSG_CTL_OK, imsg->hdr.peerid, 0, -1, NULL, 0); - return; + case IMSG_CTL_MTA_UNBLOCK: + m_msg(&m, imsg); + m_get_sockaddr(&m, (struct sockaddr*)&ss); + m_get_string(&m, &dom); + m_end(&m); + source = mta_source((struct sockaddr*)&ss); + if (*dom != '\0') { + if (!(strlcpy(buf, dom, sizeof(buf)) + >= sizeof(buf))) + mta_unblock(source, buf); + } + else + mta_unblock(source, NULL); + mta_source_unref(source); + m_compose(p, IMSG_CTL_OK, imsg->hdr.peerid, 0, -1, NULL, 0); + return; - case IMSG_CTL_MTA_SHOW_BLOCK: - SPLAY_FOREACH(block, mta_block_tree, &blocks) { - (void)snprintf(buf, sizeof(buf), "%s -> %s", - mta_source_to_text(block->source), - block->domain ? block->domain : "*"); - m_compose(p, IMSG_CTL_MTA_SHOW_BLOCK, - imsg->hdr.peerid, 0, -1, buf, strlen(buf) + 1); - } - m_compose(p, IMSG_CTL_MTA_SHOW_BLOCK, imsg->hdr.peerid, - 0, -1, NULL, 0); - return; + case IMSG_CTL_MTA_SHOW_BLOCK: + SPLAY_FOREACH(block, mta_block_tree, &blocks) { + (void)snprintf(buf, sizeof(buf), "%s -> %s", + mta_source_to_text(block->source), + block->domain ? block->domain : "*"); + m_compose(p, IMSG_CTL_MTA_SHOW_BLOCK, + imsg->hdr.peerid, 0, -1, buf, strlen(buf) + 1); } + m_compose(p, IMSG_CTL_MTA_SHOW_BLOCK, imsg->hdr.peerid, + 0, -1, NULL, 0); + return; } errx(1, "mta_imsg: unexpected %s imsg", imsg_to_str(imsg->hdr.type)); diff --git a/usr.sbin/smtpd/queue.c b/usr.sbin/smtpd/queue.c index b826523fb9a..7e9bd0ae218 100644 --- a/usr.sbin/smtpd/queue.c +++ b/usr.sbin/smtpd/queue.c @@ -1,4 +1,4 @@ -/* $OpenBSD: queue.c,v 1.183 2017/01/09 09:53:23 reyk Exp $ */ +/* $OpenBSD: queue.c,v 1.184 2017/11/21 12:20:34 eric Exp $ */ /* * Copyright (c) 2008 Gilles Chehade <gilles@poolp.org> @@ -69,489 +69,470 @@ queue_imsg(struct mproc *p, struct imsg *imsg) queue_shutdown(); memset(&bounce, 0, sizeof(struct delivery_bounce)); - if (p->proc == PROC_PONY) { - - switch (imsg->hdr.type) { - case IMSG_SMTP_MESSAGE_CREATE: - m_msg(&m, imsg); - m_get_id(&m, &reqid); - m_end(&m); - - ret = queue_message_create(&msgid); - - m_create(p, IMSG_SMTP_MESSAGE_CREATE, 0, 0, -1); - m_add_id(p, reqid); - if (ret == 0) - m_add_int(p, 0); - else { - m_add_int(p, 1); - m_add_msgid(p, msgid); - } - m_close(p); - return; - - case IMSG_SMTP_MESSAGE_ROLLBACK: - m_msg(&m, imsg); - m_get_msgid(&m, &msgid); - m_end(&m); - queue_message_delete(msgid); + switch (imsg->hdr.type) { + case IMSG_SMTP_MESSAGE_CREATE: + m_msg(&m, imsg); + m_get_id(&m, &reqid); + m_end(&m); + + ret = queue_message_create(&msgid); + + m_create(p, IMSG_SMTP_MESSAGE_CREATE, 0, 0, -1); + m_add_id(p, reqid); + if (ret == 0) + m_add_int(p, 0); + else { + m_add_int(p, 1); + m_add_msgid(p, msgid); + } + m_close(p); + return; - m_create(p_scheduler, IMSG_QUEUE_MESSAGE_ROLLBACK, - 0, 0, -1); - m_add_msgid(p_scheduler, msgid); - m_close(p_scheduler); - return; + case IMSG_SMTP_MESSAGE_ROLLBACK: + m_msg(&m, imsg); + m_get_msgid(&m, &msgid); + m_end(&m); - case IMSG_SMTP_MESSAGE_COMMIT: - m_msg(&m, imsg); - m_get_id(&m, &reqid); - m_get_msgid(&m, &msgid); - m_end(&m); + queue_message_delete(msgid); - ret = queue_message_commit(msgid); + m_create(p_scheduler, IMSG_QUEUE_MESSAGE_ROLLBACK, + 0, 0, -1); + m_add_msgid(p_scheduler, msgid); + m_close(p_scheduler); + return; - m_create(p, IMSG_SMTP_MESSAGE_COMMIT, 0, 0, -1); - m_add_id(p, reqid); - m_add_int(p, (ret == 0) ? 0 : 1); - m_close(p); + case IMSG_SMTP_MESSAGE_COMMIT: + m_msg(&m, imsg); + m_get_id(&m, &reqid); + m_get_msgid(&m, &msgid); + m_end(&m); - if (ret) { - m_create(p_scheduler, IMSG_QUEUE_MESSAGE_COMMIT, - 0, 0, -1); - m_add_msgid(p_scheduler, msgid); - m_close(p_scheduler); - } - return; + ret = queue_message_commit(msgid); - case IMSG_SMTP_MESSAGE_OPEN: - m_msg(&m, imsg); - m_get_id(&m, &reqid); - m_get_msgid(&m, &msgid); - m_end(&m); + m_create(p, IMSG_SMTP_MESSAGE_COMMIT, 0, 0, -1); + m_add_id(p, reqid); + m_add_int(p, (ret == 0) ? 0 : 1); + m_close(p); - fd = queue_message_fd_rw(msgid); + if (ret) { + m_create(p_scheduler, IMSG_QUEUE_MESSAGE_COMMIT, + 0, 0, -1); + m_add_msgid(p_scheduler, msgid); + m_close(p_scheduler); + } + return; - m_create(p, IMSG_SMTP_MESSAGE_OPEN, 0, 0, fd); - m_add_id(p, reqid); - m_add_int(p, (fd == -1) ? 0 : 1); - m_close(p); - return; + case IMSG_SMTP_MESSAGE_OPEN: + m_msg(&m, imsg); + m_get_id(&m, &reqid); + m_get_msgid(&m, &msgid); + m_end(&m); - case IMSG_QUEUE_SMTP_SESSION: - bounce_fd(imsg->fd); - return; - } - } + fd = queue_message_fd_rw(msgid); - if (p->proc == PROC_LKA) { - switch (imsg->hdr.type) { - case IMSG_LKA_ENVELOPE_SUBMIT: - m_msg(&m, imsg); - m_get_id(&m, &reqid); - m_get_envelope(&m, &evp); - m_end(&m); - - if (evp.id == 0) - log_warnx("warn: imsg_queue_submit_envelope: evpid=0"); - if (evpid_to_msgid(evp.id) == 0) - log_warnx("warn: imsg_queue_submit_envelope: msgid=0, " - "evpid=%016"PRIx64, evp.id); - ret = queue_envelope_create(&evp); - m_create(p_pony, IMSG_QUEUE_ENVELOPE_SUBMIT, 0, 0, -1); - m_add_id(p_pony, reqid); - if (ret == 0) - m_add_int(p_pony, 0); - else { - m_add_int(p_pony, 1); - m_add_evpid(p_pony, evp.id); - } - m_close(p_pony); - if (ret) { - m_create(p_scheduler, - IMSG_QUEUE_ENVELOPE_SUBMIT, 0, 0, -1); - m_add_envelope(p_scheduler, &evp); - m_close(p_scheduler); + m_create(p, IMSG_SMTP_MESSAGE_OPEN, 0, 0, fd); + m_add_id(p, reqid); + m_add_int(p, (fd == -1) ? 0 : 1); + m_close(p); + return; - } - return; + case IMSG_QUEUE_SMTP_SESSION: + bounce_fd(imsg->fd); + return; - case IMSG_LKA_ENVELOPE_COMMIT: - m_msg(&m, imsg); - m_get_id(&m, &reqid); - m_end(&m); - m_create(p_pony, IMSG_QUEUE_ENVELOPE_COMMIT, 0, 0, -1); - m_add_id(p_pony, reqid); + case IMSG_LKA_ENVELOPE_SUBMIT: + m_msg(&m, imsg); + m_get_id(&m, &reqid); + m_get_envelope(&m, &evp); + m_end(&m); + + if (evp.id == 0) + log_warnx("warn: imsg_queue_submit_envelope: evpid=0"); + if (evpid_to_msgid(evp.id) == 0) + log_warnx("warn: imsg_queue_submit_envelope: msgid=0, " + "evpid=%016"PRIx64, evp.id); + ret = queue_envelope_create(&evp); + m_create(p_pony, IMSG_QUEUE_ENVELOPE_SUBMIT, 0, 0, -1); + m_add_id(p_pony, reqid); + if (ret == 0) + m_add_int(p_pony, 0); + else { m_add_int(p_pony, 1); - m_close(p_pony); - return; + m_add_evpid(p_pony, evp.id); } - } + m_close(p_pony); + if (ret) { + m_create(p_scheduler, + IMSG_QUEUE_ENVELOPE_SUBMIT, 0, 0, -1); + m_add_envelope(p_scheduler, &evp); + m_close(p_scheduler); + } + return; - if (p->proc == PROC_SCHEDULER) { - switch (imsg->hdr.type) { - case IMSG_SCHED_ENVELOPE_REMOVE: - m_msg(&m, imsg); - m_get_evpid(&m, &evpid); - m_end(&m); + case IMSG_LKA_ENVELOPE_COMMIT: + m_msg(&m, imsg); + m_get_id(&m, &reqid); + m_end(&m); + m_create(p_pony, IMSG_QUEUE_ENVELOPE_COMMIT, 0, 0, -1); + m_add_id(p_pony, reqid); + m_add_int(p_pony, 1); + m_close(p_pony); + return; - m_create(p_scheduler, IMSG_QUEUE_ENVELOPE_ACK, 0, 0, -1); - m_add_evpid(p_scheduler, evpid); - m_close(p_scheduler); + case IMSG_SCHED_ENVELOPE_REMOVE: + m_msg(&m, imsg); + m_get_evpid(&m, &evpid); + m_end(&m); - /* already removed by scheduler */ - if (queue_envelope_load(evpid, &evp) == 0) - return; + m_create(p_scheduler, IMSG_QUEUE_ENVELOPE_ACK, 0, 0, -1); + m_add_evpid(p_scheduler, evpid); + m_close(p_scheduler); - queue_log(&evp, "Remove", "Removed by administrator"); - queue_envelope_delete(evpid); + /* already removed by scheduler */ + if (queue_envelope_load(evpid, &evp) == 0) return; - case IMSG_SCHED_ENVELOPE_EXPIRE: - m_msg(&m, imsg); - m_get_evpid(&m, &evpid); - m_end(&m); + queue_log(&evp, "Remove", "Removed by administrator"); + queue_envelope_delete(evpid); + return; - m_create(p_scheduler, IMSG_QUEUE_ENVELOPE_ACK, 0, 0, -1); - m_add_evpid(p_scheduler, evpid); - m_close(p_scheduler); + case IMSG_SCHED_ENVELOPE_EXPIRE: + m_msg(&m, imsg); + m_get_evpid(&m, &evpid); + m_end(&m); - /* already removed by scheduler*/ - if (queue_envelope_load(evpid, &evp) == 0) - return; - - bounce.type = B_ERROR; - envelope_set_errormsg(&evp, "Envelope expired"); - envelope_set_esc_class(&evp, ESC_STATUS_TEMPFAIL); - envelope_set_esc_code(&evp, ESC_DELIVERY_TIME_EXPIRED); - queue_bounce(&evp, &bounce); - queue_log(&evp, "Expire", "Envelope expired"); - queue_envelope_delete(evpid); - return; + m_create(p_scheduler, IMSG_QUEUE_ENVELOPE_ACK, 0, 0, -1); + m_add_evpid(p_scheduler, evpid); + m_close(p_scheduler); - case IMSG_SCHED_ENVELOPE_BOUNCE: - CHECK_IMSG_DATA_SIZE(imsg, sizeof *req_bounce); - req_bounce = imsg->data; - evpid = req_bounce->evpid; - - if (queue_envelope_load(evpid, &evp) == 0) { - log_warnx("queue: bounce: failed to load envelope"); - m_create(p_scheduler, IMSG_QUEUE_ENVELOPE_REMOVE, 0, 0, -1); - m_add_evpid(p_scheduler, evpid); - m_add_u32(p_scheduler, 0); /* not in-flight */ - m_close(p_scheduler); - return; - } - queue_bounce(&evp, &req_bounce->bounce); - evp.lastbounce = req_bounce->timestamp; - if (!queue_envelope_update(&evp)) - log_warnx("warn: could not update envelope %016"PRIx64, evpid); + /* already removed by scheduler*/ + if (queue_envelope_load(evpid, &evp) == 0) return; - case IMSG_SCHED_ENVELOPE_DELIVER: - m_msg(&m, imsg); - m_get_evpid(&m, &evpid); - m_end(&m); - if (queue_envelope_load(evpid, &evp) == 0) { - log_warnx("queue: deliver: failed to load envelope"); - m_create(p_scheduler, IMSG_QUEUE_ENVELOPE_REMOVE, 0, 0, -1); - m_add_evpid(p_scheduler, evpid); - m_add_u32(p_scheduler, 1); /* in-flight */ - m_close(p_scheduler); - return; - } - evp.lasttry = time(NULL); - m_create(p_pony, IMSG_QUEUE_DELIVER, 0, 0, -1); - m_add_envelope(p_pony, &evp); - m_close(p_pony); - return; + bounce.type = B_ERROR; + envelope_set_errormsg(&evp, "Envelope expired"); + envelope_set_esc_class(&evp, ESC_STATUS_TEMPFAIL); + envelope_set_esc_code(&evp, ESC_DELIVERY_TIME_EXPIRED); + queue_bounce(&evp, &bounce); + queue_log(&evp, "Expire", "Envelope expired"); + queue_envelope_delete(evpid); + return; + + case IMSG_SCHED_ENVELOPE_BOUNCE: + CHECK_IMSG_DATA_SIZE(imsg, sizeof *req_bounce); + req_bounce = imsg->data; + evpid = req_bounce->evpid; - case IMSG_SCHED_ENVELOPE_INJECT: - m_msg(&m, imsg); - m_get_evpid(&m, &evpid); - m_end(&m); - bounce_add(evpid); + if (queue_envelope_load(evpid, &evp) == 0) { + log_warnx("queue: bounce: failed to load envelope"); + m_create(p_scheduler, IMSG_QUEUE_ENVELOPE_REMOVE, 0, 0, -1); + m_add_evpid(p_scheduler, evpid); + m_add_u32(p_scheduler, 0); /* not in-flight */ + m_close(p_scheduler); return; + } + queue_bounce(&evp, &req_bounce->bounce); + evp.lastbounce = req_bounce->timestamp; + if (!queue_envelope_update(&evp)) + log_warnx("warn: could not update envelope %016"PRIx64, evpid); + return; - case IMSG_SCHED_ENVELOPE_TRANSFER: - m_msg(&m, imsg); - m_get_evpid(&m, &evpid); - m_end(&m); - if (queue_envelope_load(evpid, &evp) == 0) { - log_warnx("queue: failed to load envelope"); - m_create(p_scheduler, IMSG_QUEUE_ENVELOPE_REMOVE, 0, 0, -1); - m_add_evpid(p_scheduler, evpid); - m_add_u32(p_scheduler, 1); /* in-flight */ - m_close(p_scheduler); - return; - } - evp.lasttry = time(NULL); - m_create(p_pony, IMSG_QUEUE_TRANSFER, 0, 0, -1); - m_add_envelope(p_pony, &evp); - m_close(p_pony); + case IMSG_SCHED_ENVELOPE_DELIVER: + m_msg(&m, imsg); + m_get_evpid(&m, &evpid); + m_end(&m); + if (queue_envelope_load(evpid, &evp) == 0) { + log_warnx("queue: deliver: failed to load envelope"); + m_create(p_scheduler, IMSG_QUEUE_ENVELOPE_REMOVE, 0, 0, -1); + m_add_evpid(p_scheduler, evpid); + m_add_u32(p_scheduler, 1); /* in-flight */ + m_close(p_scheduler); return; + } + evp.lasttry = time(NULL); + m_create(p_pony, IMSG_QUEUE_DELIVER, 0, 0, -1); + m_add_envelope(p_pony, &evp); + m_close(p_pony); + return; - case IMSG_CTL_LIST_ENVELOPES: - if (imsg->hdr.len == sizeof imsg->hdr) { - m_forward(p_control, imsg); - return; - } + case IMSG_SCHED_ENVELOPE_INJECT: + m_msg(&m, imsg); + m_get_evpid(&m, &evpid); + m_end(&m); + bounce_add(evpid); + return; - m_msg(&m, imsg); - m_get_evpid(&m, &evpid); - m_get_int(&m, &flags); - m_get_time(&m, &nexttry); - m_end(&m); + case IMSG_SCHED_ENVELOPE_TRANSFER: + m_msg(&m, imsg); + m_get_evpid(&m, &evpid); + m_end(&m); + if (queue_envelope_load(evpid, &evp) == 0) { + log_warnx("queue: failed to load envelope"); + m_create(p_scheduler, IMSG_QUEUE_ENVELOPE_REMOVE, 0, 0, -1); + m_add_evpid(p_scheduler, evpid); + m_add_u32(p_scheduler, 1); /* in-flight */ + m_close(p_scheduler); + return; + } + evp.lasttry = time(NULL); + m_create(p_pony, IMSG_QUEUE_TRANSFER, 0, 0, -1); + m_add_envelope(p_pony, &evp); + m_close(p_pony); + return; - if (queue_envelope_load(evpid, &evp) == 0) - return; /* Envelope is gone, drop it */ + case IMSG_CTL_LIST_ENVELOPES: + if (imsg->hdr.len == sizeof imsg->hdr) { + m_forward(p_control, imsg); + return; + } + m_msg(&m, imsg); + m_get_evpid(&m, &evpid); + m_get_int(&m, &flags); + m_get_time(&m, &nexttry); + m_end(&m); + + if (queue_envelope_load(evpid, &evp) == 0) + return; /* Envelope is gone, drop it */ + + /* + * XXX consistency: The envelope might already be on + * its way back to the scheduler. We need to detect + * this properly and report that state. + */ + if (flags & EF_INFLIGHT) { /* - * XXX consistency: The envelope might already be on - * its way back to the scheduler. We need to detect - * this properly and report that state. + * Not exactly correct but pretty close: The + * value is not recorded on the envelope unless + * a tempfail occurs. */ - if (flags & EF_INFLIGHT) { - /* - * Not exactly correct but pretty close: The - * value is not recorded on the envelope unless - * a tempfail occurs. - */ - evp.lasttry = nexttry; - } - - m_create(p_control, IMSG_CTL_LIST_ENVELOPES, - imsg->hdr.peerid, 0, -1); - m_add_int(p_control, flags); - m_add_time(p_control, nexttry); - m_add_envelope(p_control, &evp); - m_close(p_control); - return; + evp.lasttry = nexttry; } - } - if (p->proc == PROC_PONY) { - switch (imsg->hdr.type) { - case IMSG_MDA_OPEN_MESSAGE: - case IMSG_MTA_OPEN_MESSAGE: - m_msg(&m, imsg); - m_get_id(&m, &reqid); - m_get_msgid(&m, &msgid); - m_end(&m); - fd = queue_message_fd_r(msgid); - m_create(p, imsg->hdr.type, 0, 0, fd); - m_add_id(p, reqid); - m_close(p); - return; + m_create(p_control, IMSG_CTL_LIST_ENVELOPES, + imsg->hdr.peerid, 0, -1); + m_add_int(p_control, flags); + m_add_time(p_control, nexttry); + m_add_envelope(p_control, &evp); + m_close(p_control); + return; - case IMSG_MDA_DELIVERY_OK: - case IMSG_MTA_DELIVERY_OK: - m_msg(&m, imsg); - m_get_evpid(&m, &evpid); - if (imsg->hdr.type == IMSG_MTA_DELIVERY_OK) - m_get_int(&m, &mta_ext); - m_end(&m); - if (queue_envelope_load(evpid, &evp) == 0) { - log_warn("queue: dsn: failed to load envelope"); - return; - } - if (evp.dsn_notify & DSN_SUCCESS) { - bounce.type = B_DSN; - bounce.dsn_ret = evp.dsn_ret; - envelope_set_esc_class(&evp, ESC_STATUS_OK); - if (imsg->hdr.type == IMSG_MDA_DELIVERY_OK) - queue_bounce(&evp, &bounce); - else if (imsg->hdr.type == IMSG_MTA_DELIVERY_OK && - (mta_ext & MTA_EXT_DSN) == 0) { - bounce.mta_without_dsn = 1; - queue_bounce(&evp, &bounce); - } + case IMSG_MDA_OPEN_MESSAGE: + case IMSG_MTA_OPEN_MESSAGE: + m_msg(&m, imsg); + m_get_id(&m, &reqid); + m_get_msgid(&m, &msgid); + m_end(&m); + fd = queue_message_fd_r(msgid); + m_create(p, imsg->hdr.type, 0, 0, fd); + m_add_id(p, reqid); + m_close(p); + return; + + case IMSG_MDA_DELIVERY_OK: + case IMSG_MTA_DELIVERY_OK: + m_msg(&m, imsg); + m_get_evpid(&m, &evpid); + if (imsg->hdr.type == IMSG_MTA_DELIVERY_OK) + m_get_int(&m, &mta_ext); + m_end(&m); + if (queue_envelope_load(evpid, &evp) == 0) { + log_warn("queue: dsn: failed to load envelope"); + return; + } + if (evp.dsn_notify & DSN_SUCCESS) { + bounce.type = B_DSN; + bounce.dsn_ret = evp.dsn_ret; + envelope_set_esc_class(&evp, ESC_STATUS_OK); + if (imsg->hdr.type == IMSG_MDA_DELIVERY_OK) + queue_bounce(&evp, &bounce); + else if (imsg->hdr.type == IMSG_MTA_DELIVERY_OK && + (mta_ext & MTA_EXT_DSN) == 0) { + bounce.mta_without_dsn = 1; + queue_bounce(&evp, &bounce); } - queue_envelope_delete(evpid); - m_create(p_scheduler, IMSG_QUEUE_DELIVERY_OK, 0, 0, -1); + } + queue_envelope_delete(evpid); + m_create(p_scheduler, IMSG_QUEUE_DELIVERY_OK, 0, 0, -1); + m_add_evpid(p_scheduler, evpid); + m_close(p_scheduler); + return; + + case IMSG_MDA_DELIVERY_TEMPFAIL: + case IMSG_MTA_DELIVERY_TEMPFAIL: + m_msg(&m, imsg); + m_get_evpid(&m, &evpid); + m_get_string(&m, &reason); + m_get_int(&m, &code); + m_end(&m); + if (queue_envelope_load(evpid, &evp) == 0) { + log_warnx("queue: tempfail: failed to load envelope"); + m_create(p_scheduler, IMSG_QUEUE_ENVELOPE_REMOVE, 0, 0, -1); m_add_evpid(p_scheduler, evpid); + m_add_u32(p_scheduler, 1); /* in-flight */ m_close(p_scheduler); return; + } + envelope_set_errormsg(&evp, "%s", reason); + envelope_set_esc_class(&evp, ESC_STATUS_TEMPFAIL); + envelope_set_esc_code(&evp, code); + evp.retry++; + if (!queue_envelope_update(&evp)) + log_warnx("warn: could not update envelope %016"PRIx64, evpid); + m_create(p_scheduler, IMSG_QUEUE_DELIVERY_TEMPFAIL, 0, 0, -1); + m_add_envelope(p_scheduler, &evp); + m_close(p_scheduler); + return; - case IMSG_MDA_DELIVERY_TEMPFAIL: - case IMSG_MTA_DELIVERY_TEMPFAIL: - m_msg(&m, imsg); - m_get_evpid(&m, &evpid); - m_get_string(&m, &reason); - m_get_int(&m, &code); - m_end(&m); - if (queue_envelope_load(evpid, &evp) == 0) { - log_warnx("queue: tempfail: failed to load envelope"); - m_create(p_scheduler, IMSG_QUEUE_ENVELOPE_REMOVE, 0, 0, -1); - m_add_evpid(p_scheduler, evpid); - m_add_u32(p_scheduler, 1); /* in-flight */ - m_close(p_scheduler); - return; - } - envelope_set_errormsg(&evp, "%s", reason); - envelope_set_esc_class(&evp, ESC_STATUS_TEMPFAIL); - envelope_set_esc_code(&evp, code); - evp.retry++; - if (!queue_envelope_update(&evp)) - log_warnx("warn: could not update envelope %016"PRIx64, evpid); - m_create(p_scheduler, IMSG_QUEUE_DELIVERY_TEMPFAIL, 0, 0, -1); - m_add_envelope(p_scheduler, &evp); + case IMSG_MDA_DELIVERY_PERMFAIL: + case IMSG_MTA_DELIVERY_PERMFAIL: + m_msg(&m, imsg); + m_get_evpid(&m, &evpid); + m_get_string(&m, &reason); + m_get_int(&m, &code); + m_end(&m); + if (queue_envelope_load(evpid, &evp) == 0) { + log_warnx("queue: permfail: failed to load envelope"); + m_create(p_scheduler, IMSG_QUEUE_ENVELOPE_REMOVE, 0, 0, -1); + m_add_evpid(p_scheduler, evpid); + m_add_u32(p_scheduler, 1); /* in-flight */ m_close(p_scheduler); return; + } + bounce.type = B_ERROR; + envelope_set_errormsg(&evp, "%s", reason); + envelope_set_esc_class(&evp, ESC_STATUS_PERMFAIL); + envelope_set_esc_code(&evp, code); + queue_bounce(&evp, &bounce); + queue_envelope_delete(evpid); + m_create(p_scheduler, IMSG_QUEUE_DELIVERY_PERMFAIL, 0, 0, -1); + m_add_evpid(p_scheduler, evpid); + m_close(p_scheduler); + return; - case IMSG_MDA_DELIVERY_PERMFAIL: - case IMSG_MTA_DELIVERY_PERMFAIL: - m_msg(&m, imsg); - m_get_evpid(&m, &evpid); - m_get_string(&m, &reason); - m_get_int(&m, &code); - m_end(&m); - if (queue_envelope_load(evpid, &evp) == 0) { - log_warnx("queue: permfail: failed to load envelope"); - m_create(p_scheduler, IMSG_QUEUE_ENVELOPE_REMOVE, 0, 0, -1); - m_add_evpid(p_scheduler, evpid); - m_add_u32(p_scheduler, 1); /* in-flight */ - m_close(p_scheduler); - return; - } - bounce.type = B_ERROR; - envelope_set_errormsg(&evp, "%s", reason); - envelope_set_esc_class(&evp, ESC_STATUS_PERMFAIL); - envelope_set_esc_code(&evp, code); - queue_bounce(&evp, &bounce); - queue_envelope_delete(evpid); - m_create(p_scheduler, IMSG_QUEUE_DELIVERY_PERMFAIL, 0, 0, -1); + case IMSG_MDA_DELIVERY_LOOP: + case IMSG_MTA_DELIVERY_LOOP: + m_msg(&m, imsg); + m_get_evpid(&m, &evpid); + m_end(&m); + if (queue_envelope_load(evpid, &evp) == 0) { + log_warnx("queue: loop: failed to load envelope"); + m_create(p_scheduler, IMSG_QUEUE_ENVELOPE_REMOVE, 0, 0, -1); m_add_evpid(p_scheduler, evpid); + m_add_u32(p_scheduler, 1); /* in-flight */ m_close(p_scheduler); return; + } + envelope_set_errormsg(&evp, "%s", "Loop detected"); + envelope_set_esc_class(&evp, ESC_STATUS_TEMPFAIL); + envelope_set_esc_code(&evp, ESC_ROUTING_LOOP_DETECTED); + bounce.type = B_ERROR; + queue_bounce(&evp, &bounce); + queue_envelope_delete(evp.id); + m_create(p_scheduler, IMSG_QUEUE_DELIVERY_LOOP, 0, 0, -1); + m_add_evpid(p_scheduler, evp.id); + m_close(p_scheduler); + return; - case IMSG_MDA_DELIVERY_LOOP: - case IMSG_MTA_DELIVERY_LOOP: - m_msg(&m, imsg); - m_get_evpid(&m, &evpid); - m_end(&m); - if (queue_envelope_load(evpid, &evp) == 0) { - log_warnx("queue: loop: failed to load envelope"); - m_create(p_scheduler, IMSG_QUEUE_ENVELOPE_REMOVE, 0, 0, -1); - m_add_evpid(p_scheduler, evpid); - m_add_u32(p_scheduler, 1); /* in-flight */ - m_close(p_scheduler); - return; - } - envelope_set_errormsg(&evp, "%s", "Loop detected"); - envelope_set_esc_class(&evp, ESC_STATUS_TEMPFAIL); - envelope_set_esc_code(&evp, ESC_ROUTING_LOOP_DETECTED); - bounce.type = B_ERROR; - queue_bounce(&evp, &bounce); - queue_envelope_delete(evp.id); - m_create(p_scheduler, IMSG_QUEUE_DELIVERY_LOOP, 0, 0, -1); - m_add_evpid(p_scheduler, evp.id); - m_close(p_scheduler); - return; + case IMSG_MTA_DELIVERY_HOLD: + case IMSG_MDA_DELIVERY_HOLD: + imsg->hdr.type = IMSG_QUEUE_HOLDQ_HOLD; + m_forward(p_scheduler, imsg); + return; - case IMSG_MTA_DELIVERY_HOLD: - case IMSG_MDA_DELIVERY_HOLD: - imsg->hdr.type = IMSG_QUEUE_HOLDQ_HOLD; - m_forward(p_scheduler, imsg); - return; + case IMSG_MTA_SCHEDULE: + imsg->hdr.type = IMSG_QUEUE_ENVELOPE_SCHEDULE; + m_forward(p_scheduler, imsg); + return; - case IMSG_MTA_SCHEDULE: - imsg->hdr.type = IMSG_QUEUE_ENVELOPE_SCHEDULE; - m_forward(p_scheduler, imsg); - return; + case IMSG_MTA_HOLDQ_RELEASE: + case IMSG_MDA_HOLDQ_RELEASE: + m_msg(&m, imsg); + m_get_id(&m, &holdq); + m_get_int(&m, &v); + m_end(&m); + m_create(p_scheduler, IMSG_QUEUE_HOLDQ_RELEASE, 0, 0, -1); + if (imsg->hdr.type == IMSG_MTA_HOLDQ_RELEASE) + m_add_int(p_scheduler, D_MTA); + else + m_add_int(p_scheduler, D_MDA); + m_add_id(p_scheduler, holdq); + m_add_int(p_scheduler, v); + m_close(p_scheduler); + return; - case IMSG_MTA_HOLDQ_RELEASE: - case IMSG_MDA_HOLDQ_RELEASE: - m_msg(&m, imsg); - m_get_id(&m, &holdq); - m_get_int(&m, &v); - m_end(&m); - m_create(p_scheduler, IMSG_QUEUE_HOLDQ_RELEASE, 0, 0, -1); - if (imsg->hdr.type == IMSG_MTA_HOLDQ_RELEASE) - m_add_int(p_scheduler, D_MTA); - else - m_add_int(p_scheduler, D_MDA); - m_add_id(p_scheduler, holdq); - m_add_int(p_scheduler, v); - m_close(p_scheduler); - return; - } - } + case IMSG_CTL_PAUSE_MDA: + case IMSG_CTL_PAUSE_MTA: + case IMSG_CTL_RESUME_MDA: + case IMSG_CTL_RESUME_MTA: + m_forward(p_scheduler, imsg); + return; - if (p->proc == PROC_CONTROL) { - switch (imsg->hdr.type) { - case IMSG_CTL_PAUSE_MDA: - case IMSG_CTL_PAUSE_MTA: - case IMSG_CTL_RESUME_MDA: - case IMSG_CTL_RESUME_MTA: - m_forward(p_scheduler, imsg); - return; + case IMSG_CTL_VERBOSE: + m_msg(&m, imsg); + m_get_int(&m, &v); + m_end(&m); + log_trace_verbose(v); + return; - case IMSG_CTL_VERBOSE: - m_msg(&m, imsg); - m_get_int(&m, &v); - m_end(&m); - log_trace_verbose(v); - return; + case IMSG_CTL_PROFILE: + m_msg(&m, imsg); + m_get_int(&m, &v); + m_end(&m); + profiling = v; + return; - case IMSG_CTL_PROFILE: - m_msg(&m, imsg); - m_get_int(&m, &v); - m_end(&m); - profiling = v; + case IMSG_CTL_DISCOVER_EVPID: + m_msg(&m, imsg); + m_get_evpid(&m, &evpid); + m_end(&m); + if (queue_envelope_load(evpid, &evp) == 0) { + log_warnx("queue: discover: failed to load " + "envelope %016" PRIx64, evpid); + n_evp = 0; + m_compose(p_control, imsg->hdr.type, + imsg->hdr.peerid, 0, -1, + &n_evp, sizeof n_evp); return; + } - case IMSG_CTL_DISCOVER_EVPID: - m_msg(&m, imsg); - m_get_evpid(&m, &evpid); - m_end(&m); - if (queue_envelope_load(evpid, &evp) == 0) { - log_warnx("queue: discover: failed to load " - "envelope %016" PRIx64, evpid); - n_evp = 0; - m_compose(p_control, imsg->hdr.type, - imsg->hdr.peerid, 0, -1, - &n_evp, sizeof n_evp); - return; - } - - m_create(p_scheduler, IMSG_QUEUE_DISCOVER_EVPID, - 0, 0, -1); - m_add_envelope(p_scheduler, &evp); - m_close(p_scheduler); + m_create(p_scheduler, IMSG_QUEUE_DISCOVER_EVPID, + 0, 0, -1); + m_add_envelope(p_scheduler, &evp); + m_close(p_scheduler); - m_create(p_scheduler, IMSG_QUEUE_DISCOVER_MSGID, - 0, 0, -1); - m_add_msgid(p_scheduler, evpid_to_msgid(evpid)); - m_close(p_scheduler); - n_evp = 1; - m_compose(p_control, imsg->hdr.type, imsg->hdr.peerid, - 0, -1, &n_evp, sizeof n_evp); - return; + m_create(p_scheduler, IMSG_QUEUE_DISCOVER_MSGID, + 0, 0, -1); + m_add_msgid(p_scheduler, evpid_to_msgid(evpid)); + m_close(p_scheduler); + n_evp = 1; + m_compose(p_control, imsg->hdr.type, imsg->hdr.peerid, + 0, -1, &n_evp, sizeof n_evp); + return; - case IMSG_CTL_DISCOVER_MSGID: - m_msg(&m, imsg); - m_get_msgid(&m, &msgid); - m_end(&m); - /* handle concurrent walk requests */ - wi = xcalloc(1, sizeof *wi, "queu_imsg"); - wi->msgid = msgid; - wi->peerid = imsg->hdr.peerid; - evtimer_set(&wi->ev, queue_msgid_walk, wi); - tv.tv_sec = 0; - tv.tv_usec = 10; - evtimer_add(&wi->ev, &tv); - return; + case IMSG_CTL_DISCOVER_MSGID: + m_msg(&m, imsg); + m_get_msgid(&m, &msgid); + m_end(&m); + /* handle concurrent walk requests */ + wi = xcalloc(1, sizeof *wi, "queu_imsg"); + wi->msgid = msgid; + wi->peerid = imsg->hdr.peerid; + evtimer_set(&wi->ev, queue_msgid_walk, wi); + tv.tv_sec = 0; + tv.tv_usec = 10; + evtimer_add(&wi->ev, &tv); + return; - case IMSG_CTL_UNCORRUPT_MSGID: - m_msg(&m, imsg); - m_get_msgid(&m, &msgid); - m_end(&m); - ret = queue_message_uncorrupt(msgid); - m_compose(p_control, imsg->hdr.type, imsg->hdr.peerid, - 0, -1, &ret, sizeof ret); - return; - } + case IMSG_CTL_UNCORRUPT_MSGID: + m_msg(&m, imsg); + m_get_msgid(&m, &msgid); + m_end(&m); + ret = queue_message_uncorrupt(msgid); + m_compose(p_control, imsg->hdr.type, imsg->hdr.peerid, + 0, -1, &ret, sizeof ret); + return; } errx(1, "queue_imsg: unexpected %s imsg", imsg_to_str(imsg->hdr.type)); diff --git a/usr.sbin/smtpd/smtp.c b/usr.sbin/smtpd/smtp.c index 523161897cf..1ba02b0092c 100644 --- a/usr.sbin/smtpd/smtp.c +++ b/usr.sbin/smtpd/smtp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: smtp.c,v 1.156 2017/05/22 13:43:15 gilles Exp $ */ +/* $OpenBSD: smtp.c,v 1.157 2017/11/21 12:20:34 eric Exp $ */ /* * Copyright (c) 2008 Gilles Chehade <gilles@poolp.org> @@ -58,57 +58,46 @@ static size_t maxsessions; void smtp_imsg(struct mproc *p, struct imsg *imsg) { - if (p->proc == PROC_LKA) { - switch (imsg->hdr.type) { - case IMSG_SMTP_DNS_PTR: - case IMSG_SMTP_CHECK_SENDER: - case IMSG_SMTP_EXPAND_RCPT: - case IMSG_SMTP_LOOKUP_HELO: - case IMSG_SMTP_AUTHENTICATE: - case IMSG_SMTP_TLS_INIT: - case IMSG_SMTP_TLS_VERIFY: - smtp_session_imsg(p, imsg); - return; - } - } + switch (imsg->hdr.type) { + case IMSG_SMTP_DNS_PTR: + case IMSG_SMTP_CHECK_SENDER: + case IMSG_SMTP_EXPAND_RCPT: + case IMSG_SMTP_LOOKUP_HELO: + case IMSG_SMTP_AUTHENTICATE: + case IMSG_SMTP_TLS_INIT: + case IMSG_SMTP_TLS_VERIFY: + smtp_session_imsg(p, imsg); + return; - if (p->proc == PROC_QUEUE) { - switch (imsg->hdr.type) { - case IMSG_SMTP_MESSAGE_COMMIT: - case IMSG_SMTP_MESSAGE_CREATE: - case IMSG_SMTP_MESSAGE_OPEN: - case IMSG_QUEUE_ENVELOPE_SUBMIT: - case IMSG_QUEUE_ENVELOPE_COMMIT: - smtp_session_imsg(p, imsg); - return; + case IMSG_SMTP_MESSAGE_COMMIT: + case IMSG_SMTP_MESSAGE_CREATE: + case IMSG_SMTP_MESSAGE_OPEN: + case IMSG_QUEUE_ENVELOPE_SUBMIT: + case IMSG_QUEUE_ENVELOPE_COMMIT: + smtp_session_imsg(p, imsg); + return; - case IMSG_QUEUE_SMTP_SESSION: - m_compose(p, IMSG_QUEUE_SMTP_SESSION, 0, 0, - smtp_enqueue(), imsg->data, - imsg->hdr.len - sizeof imsg->hdr); - return; - } - } + case IMSG_QUEUE_SMTP_SESSION: + m_compose(p, IMSG_QUEUE_SMTP_SESSION, 0, 0, smtp_enqueue(), + imsg->data, imsg->hdr.len - sizeof imsg->hdr); + return; - if (p->proc == PROC_CONTROL) { - switch (imsg->hdr.type) { - case IMSG_CTL_SMTP_SESSION: - m_compose(p, IMSG_CTL_SMTP_SESSION, imsg->hdr.peerid, 0, - smtp_enqueue(), NULL, 0); - return; + case IMSG_CTL_SMTP_SESSION: + m_compose(p, IMSG_CTL_SMTP_SESSION, imsg->hdr.peerid, 0, + smtp_enqueue(), NULL, 0); + return; - case IMSG_CTL_PAUSE_SMTP: - log_debug("debug: smtp: pausing listening sockets"); - smtp_pause(); - env->sc_flags |= SMTPD_SMTP_PAUSED; - return; + case IMSG_CTL_PAUSE_SMTP: + log_debug("debug: smtp: pausing listening sockets"); + smtp_pause(); + env->sc_flags |= SMTPD_SMTP_PAUSED; + return; - case IMSG_CTL_RESUME_SMTP: - log_debug("debug: smtp: resuming listening sockets"); - env->sc_flags &= ~SMTPD_SMTP_PAUSED; - smtp_resume(); - return; - } + case IMSG_CTL_RESUME_SMTP: + log_debug("debug: smtp: resuming listening sockets"); + env->sc_flags &= ~SMTPD_SMTP_PAUSED; + smtp_resume(); + return; } errx(1, "smtp_imsg: unexpected %s imsg", imsg_to_str(imsg->hdr.type)); diff --git a/usr.sbin/smtpd/smtpd.c b/usr.sbin/smtpd/smtpd.c index 5433711f36a..03c2f803e0d 100644 --- a/usr.sbin/smtpd/smtpd.c +++ b/usr.sbin/smtpd/smtpd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: smtpd.c,v 1.290 2017/09/08 16:51:22 eric Exp $ */ +/* $OpenBSD: smtpd.c,v 1.291 2017/11/21 12:20:34 eric Exp $ */ /* * Copyright (c) 2008 Gilles Chehade <gilles@poolp.org> @@ -162,100 +162,90 @@ parent_imsg(struct mproc *p, struct imsg *imsg) if (imsg == NULL) fatalx("process %s socket closed", p->name); - if (p->proc == PROC_LKA) { - switch (imsg->hdr.type) { - case IMSG_LKA_OPEN_FORWARD: - CHECK_IMSG_DATA_SIZE(imsg, sizeof *fwreq); - fwreq = imsg->data; - fd = parent_forward_open(fwreq->user, fwreq->directory, - fwreq->uid, fwreq->gid); - fwreq->status = 0; - if (fd == -1 && errno != ENOENT) { - if (errno == EAGAIN) - fwreq->status = -1; - } - else - fwreq->status = 1; - m_compose(p, IMSG_LKA_OPEN_FORWARD, 0, 0, fd, - fwreq, sizeof *fwreq); - return; - - case IMSG_LKA_AUTHENTICATE: - /* - * If we reached here, it means we want root to lookup - * system user. - */ - m_msg(&m, imsg); - m_get_id(&m, &reqid); - m_get_string(&m, &username); - m_get_string(&m, &password); - m_end(&m); - - ret = parent_auth_user(username, password); - - m_create(p, IMSG_LKA_AUTHENTICATE, 0, 0, -1); - m_add_id(p, reqid); - m_add_int(p, ret); - m_close(p); - return; + switch (imsg->hdr.type) { + case IMSG_LKA_OPEN_FORWARD: + CHECK_IMSG_DATA_SIZE(imsg, sizeof *fwreq); + fwreq = imsg->data; + fd = parent_forward_open(fwreq->user, fwreq->directory, + fwreq->uid, fwreq->gid); + fwreq->status = 0; + if (fd == -1 && errno != ENOENT) { + if (errno == EAGAIN) + fwreq->status = -1; } - } + else + fwreq->status = 1; + m_compose(p, IMSG_LKA_OPEN_FORWARD, 0, 0, fd, + fwreq, sizeof *fwreq); + return; - if (p->proc == PROC_PONY) { - switch (imsg->hdr.type) { - case IMSG_MDA_FORK: - m_msg(&m, imsg); - m_get_id(&m, &reqid); - m_get_data(&m, &data, &sz); - m_end(&m); - if (sz != sizeof(deliver)) - fatalx("expected deliver"); - memmove(&deliver, data, sz); - forkmda(p, reqid, &deliver); - return; + case IMSG_LKA_AUTHENTICATE: + /* + * If we reached here, it means we want root to lookup + * system user. + */ + m_msg(&m, imsg); + m_get_id(&m, &reqid); + m_get_string(&m, &username); + m_get_string(&m, &password); + m_end(&m); + + ret = parent_auth_user(username, password); + + m_create(p, IMSG_LKA_AUTHENTICATE, 0, 0, -1); + m_add_id(p, reqid); + m_add_int(p, ret); + m_close(p); + return; - case IMSG_MDA_KILL: - m_msg(&m, imsg); - m_get_id(&m, &reqid); - m_get_string(&m, &cause); - m_end(&m); - - i = NULL; - while ((n = tree_iter(&children, &i, NULL, (void**)&c))) - if (c->type == CHILD_MDA && - c->mda_id == reqid && - c->cause == NULL) - break; - if (!n) { - log_debug("debug: smtpd: " - "kill request: proc not found"); - return; - } + case IMSG_MDA_FORK: + m_msg(&m, imsg); + m_get_id(&m, &reqid); + m_get_data(&m, &data, &sz); + m_end(&m); + if (sz != sizeof(deliver)) + fatalx("expected deliver"); + memmove(&deliver, data, sz); + forkmda(p, reqid, &deliver); + return; - c->cause = xstrdup(cause, "parent_imsg"); - log_debug("debug: smtpd: kill requested for %u: %s", - c->pid, c->cause); - kill(c->pid, SIGTERM); + case IMSG_MDA_KILL: + m_msg(&m, imsg); + m_get_id(&m, &reqid); + m_get_string(&m, &cause); + m_end(&m); + + i = NULL; + while ((n = tree_iter(&children, &i, NULL, (void**)&c))) + if (c->type == CHILD_MDA && + c->mda_id == reqid && + c->cause == NULL) + break; + if (!n) { + log_debug("debug: smtpd: " + "kill request: proc not found"); return; } - } - if (p->proc == PROC_CONTROL) { - switch (imsg->hdr.type) { - case IMSG_CTL_VERBOSE: - m_msg(&m, imsg); - m_get_int(&m, &v); - m_end(&m); - log_trace_verbose(v); - return; + c->cause = xstrdup(cause, "parent_imsg"); + log_debug("debug: smtpd: kill requested for %u: %s", + c->pid, c->cause); + kill(c->pid, SIGTERM); + return; - case IMSG_CTL_PROFILE: - m_msg(&m, imsg); - m_get_int(&m, &v); - m_end(&m); - profiling = v; - return; - } + case IMSG_CTL_VERBOSE: + m_msg(&m, imsg); + m_get_int(&m, &v); + m_end(&m); + log_trace_verbose(v); + return; + + case IMSG_CTL_PROFILE: + m_msg(&m, imsg); + m_get_int(&m, &v); + m_end(&m); + profiling = v; + return; } errx(1, "parent_imsg: unexpected %s imsg from %s", |