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