2 * Utility functions for the Citadel SMTP implementation
4 * Copyright (c) 1998-2023 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include "../../sysdep.h"
24 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include <libcitadel.h>
35 #include "../../citadel_defs.h"
36 #include "../../server.h"
37 #include "../../citserver.h"
38 #include "../../support.h"
39 #include "../../config.h"
40 #include "../../control.h"
41 #include "../../user_ops.h"
42 #include "../../database.h"
43 #include "../../msgbase.h"
44 #include "../../internet_addressing.h"
45 #include "../../genstamp.h"
46 #include "../../domain.h"
47 #include "../../clientsocket.h"
48 #include "../../locate_host.h"
49 #include "../../citadel_dirs.h"
50 #include "../../ctdl_module.h"
51 #include "smtp_util.h"
55 * smtp_do_bounce() is caled by smtp_process_one_msg() to scan a set of delivery
56 * instructions for errors and produce/deliver a "bounce" message (delivery
57 * status notification).
59 * is_final should be set to:
60 * SDB_BOUNCE_FATALS Advise the sender of all 5XX (permanent failures)
61 * SDB_BOUNCE_ALL Advise the sender that all deliveries have failed and will not be retried
62 * SDB_WARN Warn the sender about all 4XX transient delays
64 void smtp_do_bounce(const char *instr, int is_final) {
76 struct CtdlMessage *bmsg = NULL;
77 int successful_bounce = 0;
82 syslog(LOG_DEBUG, "smtp_do_bounce() called");
84 boundary = NewStrBufPlain(HKEY("=_Citadel_Multipart_"));
86 StrBufAppendPrintf(boundary, "%s_%04x%04x", CtdlGetConfigStr("c_fqdn"), getpid(), ++seq);
88 /* Start building our bounce message */
90 bmsg = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
91 if (bmsg == NULL) return;
92 memset(bmsg, 0, sizeof(struct CtdlMessage));
93 BounceMB = NewStrBufPlain(NULL, 1024);
95 bmsg->cm_magic = CTDLMESSAGE_MAGIC;
96 bmsg->cm_anon_type = MES_NORMAL;
97 bmsg->cm_format_type = FMT_RFC822;
98 CM_SetField(bmsg, eAuthor, "Citadel");
99 CM_SetField(bmsg, eOriginalRoom, MAILROOM);
100 CM_SetField(bmsg, eMsgSubject, "Delivery Status Notification (Failure)");
101 StrBufAppendBufPlain(BounceMB, HKEY("Content-type: multipart/mixed; boundary=\""), 0);
102 StrBufAppendBuf(BounceMB, boundary, 0);
103 StrBufAppendBufPlain(BounceMB, HKEY("\"\r\n"), 0);
104 StrBufAppendBufPlain(BounceMB, HKEY("MIME-Version: 1.0\r\n"), 0);
105 StrBufAppendBufPlain(BounceMB, HKEY("X-Mailer: " CITADEL "\r\n"), 0);
106 StrBufAppendBufPlain(BounceMB, HKEY("\r\nThis is a multipart message in MIME format.\r\n\r\n"), 0);
107 StrBufAppendBufPlain(BounceMB, HKEY("--"), 0);
108 StrBufAppendBuf(BounceMB, boundary, 0);
109 StrBufAppendBufPlain(BounceMB, HKEY("\r\n"), 0);
110 StrBufAppendBufPlain(BounceMB, HKEY("Content-type: text/plain\r\n\r\n"), 0);
112 if (is_final == SDB_BOUNCE_ALL) {
113 StrBufAppendBufPlain(
115 HKEY( "A message you sent could not be delivered "
116 "to some or all of its recipients\ndue to "
117 "prolonged unavailability of its destination(s).\n"
118 "Giving up on the following addresses:\n\n"),
121 else if (is_final == SDB_BOUNCE_FATALS) {
122 StrBufAppendBufPlain(
124 HKEY( "A message you sent could not be delivered "
125 "to some or all of its recipients.\n"
126 "The following addresses were undeliverable:\n\n"),
129 else if (is_final == SDB_WARN) {
130 StrBufAppendBufPlain(
132 HKEY("A message you sent has not been delivered "
133 "to some or all of its recipients.\n"
134 "Citadel will continue attempting delivery for five days.\n"
135 "The following addresses were undeliverable:\n\n"),
138 else { // should never get here
139 StrBufAppendBufPlain(BounceMB, HKEY("This message should never occur.\n\n"), 0);
143 * Now go through the instructions checking for stuff.
145 lines = num_tokens(instr, '\n');
146 for (i=0; i<lines; ++i) {
149 extract_token(buf, instr, i, '\n', sizeof buf);
150 extract_token(key, buf, 0, '|', sizeof key);
151 addrlen = extract_token(addr, buf, 1, '|', sizeof addr);
152 status = extract_int(buf, 2);
153 dsnlen = extract_token(dsn, buf, 3, '|', sizeof dsn);
156 syslog(LOG_DEBUG, "key=<%s> addr=<%s> status=%d dsn=<%s>", key, addr, status, dsn);
158 if (!strcasecmp(key, "bounceto")) {
159 strcpy(bounceto, addr);
162 if (!strcasecmp(key, "msgid")) {
166 if (!strcasecmp(key, "remote")) {
167 if ((is_final == SDB_BOUNCE_FATALS) && (status == 5)) bounce_this = 1;
168 if ((is_final == SDB_BOUNCE_ALL) && (status != 2)) bounce_this = 1;
169 if ((is_final == SDB_WARN) && (status == 4)) bounce_this = 1;
174 StrBufAppendBufPlain(BounceMB, addr, addrlen, 0);
175 StrBufAppendBufPlain(BounceMB, HKEY(": "), 0);
176 StrBufAppendBufPlain(BounceMB, dsn, dsnlen, 0);
177 StrBufAppendBufPlain(BounceMB, HKEY("\r\n"), 0);
181 /* Attach the original message */
183 StrBufAppendBufPlain(BounceMB, HKEY("--"), 0);
184 StrBufAppendBuf(BounceMB, boundary, 0);
185 StrBufAppendBufPlain(BounceMB, HKEY("\r\n"), 0);
186 StrBufAppendBufPlain(BounceMB, HKEY("Content-type: message/rfc822\r\n"), 0);
187 StrBufAppendBufPlain(BounceMB, HKEY("Content-Transfer-Encoding: 7bit\r\n"), 0);
188 StrBufAppendBufPlain(BounceMB, HKEY("Content-Disposition: inline\r\n"), 0);
189 StrBufAppendBufPlain(BounceMB, HKEY("\r\n"), 0);
191 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
192 CtdlOutputMsg(omsgid,
198 StrBufAppendBuf(BounceMB, CC->redirect_buffer, 0);
199 FreeStrBuf(&CC->redirect_buffer);
202 /* Close the multipart MIME scope */
203 StrBufAppendBufPlain(BounceMB, HKEY("--"), 0);
204 StrBufAppendBuf(BounceMB, boundary, 0);
205 StrBufAppendBufPlain(BounceMB, HKEY("--\r\n"), 0);
206 CM_SetAsFieldSB(bmsg, eMessageText, &BounceMB);
208 /* Deliver the bounce if there's anything worth mentioning */
209 syslog(LOG_DEBUG, "num_bounces = %d", num_bounces);
210 if (num_bounces > 0) {
212 /* First try the user who sent the message */
213 if (IsEmptyStr(bounceto)) {
214 syslog(LOG_ERR, "No bounce address specified");
217 syslog(LOG_DEBUG, "bounce to user <%s>", bounceto);
220 /* If not, post it in the Aide> room */
221 if (successful_bounce == 0) {
222 CtdlSubmitMsg(bmsg, NULL, CtdlGetConfigStr("c_aideroom"));
226 FreeStrBuf(&boundary);
228 syslog(LOG_DEBUG, "Done processing bounces");
232 char *smtpcodes[][2] = {
233 { "211 - System status / system help reply" },
234 { "214", "Help message" },
235 { "220", "Domain service ready" },
236 { "221", "Domain service closing transmission channel" },
237 { "250", "Requested mail action completed and OK" },
238 { "251", "Not Local User, forward email to forward path" },
239 { "252", "Cannot Verify user, will attempt delivery later" },
240 { "253", "Pending messages for node started" },
241 { "354", "Start mail input; end with ." },
242 { "355", "Octet-offset is the transaction offset" },
243 { "421", "Domain service not available, closing transmission channel" },
244 { "432", "Domain service not available, closing transmission channel" },
245 { "450", "Requested mail action not taken: mailbox unavailable. request refused" },
246 { "451", "Requested action aborted: local error in processing Request is unable to be processed, try again" },
247 { "452", "Requested action not taken: insufficient system storage" },
248 { "453", "No mail" },
249 { "454", "TLS not available due to temporary reason. Encryption required for requested authentication mechanism" },
250 { "458", "Unable to queue messages for node" },
251 { "459", "Node not allowed: reason" },
252 { "500", "Syntax error, command unrecognized" },
253 { "501", "Syntax error in parameters or arguments" },
254 { "502", "Command not implemented" },
255 { "503", "Bad sequence of commands" },
256 { "504", "Command parameter not implemented" },
257 { "510", "Check the recipient address" },
258 { "512", "Domain can not be found. Unknown host." },
259 { "515", "Destination mailbox address invalid" },
260 { "517", "Problem with senders mail attribute, check properties" },
261 { "521", "Domain does not accept mail" },
262 { "522", "Recipient has exceeded mailbox limit" },
263 { "523", "Server limit exceeded. Message too large" },
264 { "530", "Access Denied. Authentication required" },
265 { "531", "Mail system Full" },
266 { "533", "Remote ../../server.has insufficient disk space to hold email" },
267 { "534", "Authentication mechanism is too weak. Message too big" },
268 { "535", "Multiple servers using same IP. Required Authentication" },
269 { "538", "Encryption required for requested authentication mechanism" },
270 { "540", "Email address has no DNS Server" },
271 { "541", "No response from host" },
272 { "542", "Bad Connection" },
273 { "543", "Routing server failure. No available route" },
274 { "546", "Email looping" },
275 { "547", "Delivery time-out" },
276 { "550", "Requested action not taken: mailbox unavailable or relaying denied" },
277 { "551", "User not local; please try forward path" },
278 { "552", "Requested mail action aborted: exceeded storage allocation" },
279 { "553", "Requested action not taken: mailbox name not allowed" },
280 { "554", "Transaction failed" }
284 char *smtpstatus(int code) {
287 for (i=0; i<(sizeof(smtpcodes)/sizeof(char *)/2); ++i) {
288 if (atoi(smtpcodes[i][0]) == code) {
289 return(smtpcodes[i][1]);
293 return("Unknown or other SMTP status");