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