Removed an unused parameter from CtdlSubmitMsg(). Why was it even there?
[citadel.git] / citadel / modules / smtp / smtp_util.c
1 /*
2  * Utility functions for the Citadel SMTP implementation
3  *
4  * Copyright (c) 1998-2020 by the citadel.org team
5  *
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.
8  *
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.
13  */
14
15 #include "sysdep.h"
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <stdio.h>
19 #include <termios.h>
20 #include <fcntl.h>
21 #include <signal.h>
22 #include <pwd.h>
23 #include <errno.h>
24 #include <sys/types.h>
25 #include <syslog.h>
26
27 #if TIME_WITH_SYS_TIME
28 # include <sys/time.h>
29 # include <time.h>
30 #else
31 # if HAVE_SYS_TIME_H
32 #  include <sys/time.h>
33 # else
34 #  include <time.h>
35 # endif
36 #endif
37
38 #include <sys/wait.h>
39 #include <ctype.h>
40 #include <string.h>
41 #include <limits.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
45 #include <libcitadel.h>
46 #include "citadel.h"
47 #include "server.h"
48 #include "citserver.h"
49 #include "support.h"
50 #include "config.h"
51 #include "control.h"
52 #include "user_ops.h"
53 #include "database.h"
54 #include "msgbase.h"
55 #include "internet_addressing.h"
56 #include "genstamp.h"
57 #include "domain.h"
58 #include "clientsocket.h"
59 #include "locate_host.h"
60 #include "citadel_dirs.h"
61 #include "ctdl_module.h"
62 #include "smtp_util.h"
63
64 const char *smtp_get_Recipients(void)
65 {
66         citsmtp *sSMTP = SMTP;
67
68         if (sSMTP == NULL)
69                 return NULL;
70         else return ChrPtr(sSMTP->from);
71 }
72
73
74 /*
75  * smtp_do_bounce() is caled by smtp_process_one_msg() to scan a set of delivery
76  * instructions for errors and produce/deliver a "bounce" message (delivery
77  * status notification).
78  *
79  * is_final should be set to:
80  *      SDB_BOUNCE_FATALS       Advise the sender of all 5XX (permanent failures)
81  *      SDB_BOUNCE_ALL          Advise the sender that all deliveries have failed and will not be retried
82  *      SDB_WARN                Warn the sender about all 4XX transient delays
83  */
84 void smtp_do_bounce(const char *instr, int is_final)
85 {
86         int i;
87         int lines;
88         int status;
89         char buf[1024];
90         char key[1024];
91         char addr[1024];
92         char dsn[1024];
93         char bounceto[1024];
94         StrBuf *boundary;
95         int num_bounces = 0;
96         int bounce_this = 0;
97         struct CtdlMessage *bmsg = NULL;
98         recptypes *valid;
99         int successful_bounce = 0;
100         static int seq = 0;
101         StrBuf *BounceMB;
102         long omsgid = (-1);
103
104         syslog(LOG_DEBUG, "smtp_do_bounce() called");
105         strcpy(bounceto, "");
106         boundary = NewStrBufPlain(HKEY("=_Citadel_Multipart_"));
107
108         StrBufAppendPrintf(boundary, "%s_%04x%04x", CtdlGetConfigStr("c_fqdn"), getpid(), ++seq);
109
110
111         /* Start building our bounce message */
112
113         bmsg = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
114         if (bmsg == NULL) return;
115         memset(bmsg, 0, sizeof(struct CtdlMessage));
116         BounceMB = NewStrBufPlain(NULL, 1024);
117
118         bmsg->cm_magic = CTDLMESSAGE_MAGIC;
119         bmsg->cm_anon_type = MES_NORMAL;
120         bmsg->cm_format_type = FMT_RFC822;
121         CM_SetField(bmsg, eAuthor, HKEY("Citadel"));
122         CM_SetField(bmsg, eOriginalRoom, HKEY(MAILROOM));
123         CM_SetField(bmsg, eMsgSubject, HKEY("Delivery Status Notification (Failure)"));
124         StrBufAppendBufPlain(BounceMB, HKEY("Content-type: multipart/mixed; boundary=\""), 0);
125         StrBufAppendBuf(BounceMB, boundary, 0);
126         StrBufAppendBufPlain(BounceMB, HKEY("\"\r\n"), 0);
127         StrBufAppendBufPlain(BounceMB, HKEY("MIME-Version: 1.0\r\n"), 0);
128         StrBufAppendBufPlain(BounceMB, HKEY("X-Mailer: " CITADEL "\r\n"), 0);
129         StrBufAppendBufPlain(BounceMB, HKEY("\r\nThis is a multipart message in MIME format.\r\n\r\n"), 0);
130         StrBufAppendBufPlain(BounceMB, HKEY("--"), 0);
131         StrBufAppendBuf(BounceMB, boundary, 0);
132         StrBufAppendBufPlain(BounceMB, HKEY("\r\n"), 0);
133         StrBufAppendBufPlain(BounceMB, HKEY("Content-type: text/plain\r\n\r\n"), 0);
134
135         if (is_final == SDB_BOUNCE_ALL)
136         {
137                 StrBufAppendBufPlain(
138                         BounceMB,
139                         HKEY(   "A message you sent could not be delivered "
140                                 "to some or all of its recipients\ndue to "
141                                 "prolonged unavailability of its destination(s).\n"
142                                 "Giving up on the following addresses:\n\n"),
143                         0);
144         }
145         else if (is_final == SDB_BOUNCE_FATALS)
146         {
147                 StrBufAppendBufPlain(
148                         BounceMB,
149                         HKEY(   "A message you sent could not be delivered "
150                                 "to some or all of its recipients.\n"
151                                 "The following addresses were undeliverable:\n\n"),
152                         0);
153         }
154         else if (is_final == SDB_WARN)
155         {
156                 StrBufAppendBufPlain(
157                         BounceMB,
158                         HKEY("A message you sent has not been delivered "
159                                 "to some or all of its recipients.\n"
160                                 "Citadel will continue attempting delivery for five days.\n"
161                                 "The following addresses were undeliverable:\n\n"),
162                         0);
163         }
164         else    // should never get here
165         {
166                 StrBufAppendBufPlain(BounceMB, HKEY("This message should never occur.\n\n"), 0);
167         }
168
169         /*
170          * Now go through the instructions checking for stuff.
171          */
172         lines = num_tokens(instr, '\n');
173         for (i=0; i<lines; ++i) {
174                 long addrlen;
175                 long dsnlen;
176                 extract_token(buf, instr, i, '\n', sizeof buf);
177                 extract_token(key, buf, 0, '|', sizeof key);
178                 addrlen = extract_token(addr, buf, 1, '|', sizeof addr);
179                 status = extract_int(buf, 2);
180                 dsnlen = extract_token(dsn, buf, 3, '|', sizeof dsn);
181                 bounce_this = 0;
182
183                 syslog(LOG_DEBUG, "key=<%s> addr=<%s> status=%d dsn=<%s>", key, addr, status, dsn);
184
185                 if (!strcasecmp(key, "bounceto")) {
186                         strcpy(bounceto, addr);
187                 }
188
189                 if (!strcasecmp(key, "msgid")) {
190                         omsgid = atol(addr);
191                 }
192
193                 if (!strcasecmp(key, "remote")) {
194                         if ((is_final == SDB_BOUNCE_FATALS) && (status == 5)) bounce_this = 1;
195                         if ((is_final == SDB_BOUNCE_ALL) && (status != 2)) bounce_this = 1;
196                         if ((is_final == SDB_WARN) && (status == 4)) bounce_this = 1;
197                 }
198
199                 if (bounce_this) {
200                         ++num_bounces;
201                         StrBufAppendBufPlain(BounceMB, addr, addrlen, 0);
202                         StrBufAppendBufPlain(BounceMB, HKEY(": "), 0);
203                         StrBufAppendBufPlain(BounceMB, dsn, dsnlen, 0);
204                         StrBufAppendBufPlain(BounceMB, HKEY("\r\n"), 0);
205                 }
206         }
207
208         /* Attach the original message */
209         if (omsgid >= 0) {
210                 StrBufAppendBufPlain(BounceMB, HKEY("--"), 0);
211                 StrBufAppendBuf(BounceMB, boundary, 0);
212                 StrBufAppendBufPlain(BounceMB, HKEY("\r\n"), 0);
213                 StrBufAppendBufPlain(BounceMB, HKEY("Content-type: message/rfc822\r\n"), 0);
214                 StrBufAppendBufPlain(BounceMB, HKEY("Content-Transfer-Encoding: 7bit\r\n"), 0);
215                 StrBufAppendBufPlain(BounceMB, HKEY("Content-Disposition: inline\r\n"), 0);
216                 StrBufAppendBufPlain(BounceMB, HKEY("\r\n"), 0);
217
218                 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
219                 CtdlOutputMsg(omsgid,
220                         MT_RFC822,
221                         HEADERS_ALL,
222                         0, 1, NULL, 0,
223                         NULL, NULL, NULL
224                 );
225                 StrBufAppendBuf(BounceMB, CC->redirect_buffer, 0);
226                 FreeStrBuf(&CC->redirect_buffer);
227         }
228
229         /* Close the multipart MIME scope */
230         StrBufAppendBufPlain(BounceMB, HKEY("--"), 0);
231         StrBufAppendBuf(BounceMB, boundary, 0);
232         StrBufAppendBufPlain(BounceMB, HKEY("--\r\n"), 0);
233         CM_SetAsFieldSB(bmsg, eMesageText, &BounceMB);
234
235         /* Deliver the bounce if there's anything worth mentioning */
236         syslog(LOG_DEBUG, "num_bounces = %d", num_bounces);
237         if (num_bounces > 0) {
238
239                 /* First try the user who sent the message */
240                 if (IsEmptyStr(bounceto)) {
241                         syslog(LOG_ERR, "No bounce address specified");
242                 }
243                 else {
244                         syslog(LOG_DEBUG, "bounce to user <%s>", bounceto);
245                 }
246                 /* Can we deliver the bounce to the original sender? */
247                 valid = validate_recipients(bounceto, smtp_get_Recipients (), 0);
248                 if (valid != NULL) {
249                         if (valid->num_error == 0) {
250                                 CtdlSubmitMsg(bmsg, valid, "");
251                                 successful_bounce = 1;
252                         }
253                 }
254
255                 /* If not, post it in the Aide> room */
256                 if (successful_bounce == 0) {
257                         CtdlSubmitMsg(bmsg, NULL, CtdlGetConfigStr("c_aideroom"));
258                 }
259
260                 /* Free up the memory we used */
261                 if (valid != NULL) {
262                         free_recipients(valid);
263                 }
264         }
265         FreeStrBuf(&boundary);
266         CM_Free(bmsg);
267         syslog(LOG_DEBUG, "Done processing bounces");
268 }
269
270
271
272
273
274 char *smtpcodes[][2] = {
275         { "211 - System status / system help reply" },
276         { "214", "Help message" },
277         { "220", "Domain service ready" },
278         { "221", "Domain service closing transmission channel" },
279         { "250", "Requested mail action completed and OK" },
280         { "251", "Not Local User, forward email to forward path" },
281         { "252", "Cannot Verify user, will attempt delivery later" },
282         { "253", "Pending messages for node started" },
283         { "354", "Start mail input; end with ." },
284         { "355", "Octet-offset is the transaction offset" },
285         { "421", "Domain service not available, closing transmission channel" },
286         { "432", "Domain service not available, closing transmission channel" },
287         { "450", "Requested mail action not taken: mailbox unavailable. request refused" },
288         { "451", "Requested action aborted: local error in processing Request is unable to be processed, try again" },
289         { "452", "Requested action not taken: insufficient system storage" },
290         { "453", "No mail" },
291         { "454", "TLS not available due to temporary reason. Encryption required for requested authentication mechanism" },
292         { "458", "Unable to queue messages for node" },
293         { "459", "Node not allowed: reason" },
294         { "500", "Syntax error, command unrecognized" },
295         { "501", "Syntax error in parameters or arguments" },
296         { "502", "Command not implemented" },
297         { "503", "Bad sequence of commands" },
298         { "504", "Command parameter not implemented" },
299         { "510", "Check the recipient address" },
300         { "512", "Domain can not be found. Unknown host." },
301         { "515", "Destination mailbox address invalid" },
302         { "517", "Problem with senders mail attribute, check properties" },
303         { "521", "Domain does not accept mail" },
304         { "522", "Recipient has exceeded mailbox limit" },
305         { "523", "Server limit exceeded. Message too large" },
306         { "530", "Access Denied. Authentication required" },
307         { "531", "Mail system Full" },
308         { "533", "Remote server has insufficient disk space to hold email" },
309         { "534", "Authentication mechanism is too weak. Message too big" },
310         { "535", "Multiple servers using same IP. Required Authentication" },
311         { "538", "Encryption required for requested authentication mechanism" },
312         { "540", "Email address has no DNS Server" },
313         { "541", "No response from host" },
314         { "542", "Bad Connection" },
315         { "543", "Routing server failure. No available route" },
316         { "546", "Email looping" },
317         { "547", "Delivery time-out" },
318         { "550", "Requested action not taken: mailbox unavailable or relaying denied" },
319         { "551", "User not local; please try forward path" },
320         { "552", "Requested mail action aborted: exceeded storage allocation" },
321         { "553", "Requested action not taken: mailbox name not allowed" },
322         { "554", "Transaction failed" }
323 };
324
325
326
327 char *smtpstatus(int code) {
328         int i;
329
330         for (i=0; i<(sizeof(smtpcodes)/sizeof(char *)/2); ++i) {
331                 if (atoi(smtpcodes[i][0]) == code) {
332                         return(smtpcodes[i][1]);
333                 }
334         }
335         
336         return("Unknown or other SMTP status");
337 }
338