933b051f4445e6acc1b291befecc50fab67edd92
[citadel.git] / citadel / modules / network / serv_netmail.c
1 /*
2  * This module handles shared rooms, inter-Citadel mail, and outbound
3  * mailing list processing.
4  *
5  * Copyright (c) 2000-2018 by the citadel.org team
6  *
7  * This program is open source software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License, version 3.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * ** NOTE **   A word on the S_NETCONFIGS semaphore:
16  * This is a fairly high-level type of critical section.  It ensures that no
17  * two threads work on the netconfigs files at the same time.  Since we do
18  * so many things inside these, here are the rules:
19  *  1. begin_critical_section(S_NETCONFIGS) *before* begin_ any others.
20  *  2. Do *not* perform any I/O with the client during these sections.
21  *
22  */
23
24 /*
25  * Duration of time (in seconds) after which pending list subscribe/unsubscribe
26  * requests that have not been confirmed will be deleted.
27  */
28 #define EXP     259200  /* three days */
29
30 #include "sysdep.h"
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <stdio.h>
34 #include <fcntl.h>
35 #include <ctype.h>
36 #include <signal.h>
37 #include <pwd.h>
38 #include <errno.h>
39 #include <sys/stat.h>
40 #include <sys/types.h>
41 #include <dirent.h>
42 #if TIME_WITH_SYS_TIME
43 # include <sys/time.h>
44 # include <time.h>
45 #else
46 # if HAVE_SYS_TIME_H
47 #  include <sys/time.h>
48 # else
49 #  include <time.h>
50 # endif
51 #endif
52 #ifdef HAVE_SYSCALL_H
53 # include <syscall.h>
54 #else
55 # if HAVE_SYS_SYSCALL_H
56 #  include <sys/syscall.h>
57 # endif
58 #endif
59
60 #include <sys/wait.h>
61 #include <string.h>
62 #include <limits.h>
63 #include <libcitadel.h>
64 #include "citadel.h"
65 #include "server.h"
66 #include "citserver.h"
67 #include "support.h"
68 #include "config.h"
69 #include "user_ops.h"
70 #include "database.h"
71 #include "msgbase.h"
72 #include "internet_addressing.h"
73 #include "serv_network.h"
74 #include "clientsocket.h"
75 #include "citadel_dirs.h"
76 #include "threads.h"
77 #include "context.h"
78 #include "ctdl_module.h"
79 #include "netspool.h"
80 #include "netmail.h"
81
82 void network_deliver_list(struct CtdlMessage *msg, SpoolControl *sc, const char *RoomName);
83
84 void aggregate_recipients(StrBuf **recps, RoomNetCfg Which, OneRoomNetCfg *OneRNCfg, long nSegments)
85 {
86         int i;
87         size_t recps_len = 0;
88         RoomNetCfgLine *nptr;
89
90         *recps = NULL;
91         /*
92          * Figure out how big a buffer we need to allocate
93          */
94         for (nptr = OneRNCfg->NetConfigs[Which]; nptr != NULL; nptr = nptr->next) {
95                 recps_len = recps_len + StrLength(nptr->Value[0]) + 2;
96         }
97
98         /* Nothing todo... */
99         if (recps_len == 0)
100                 return;
101
102         *recps = NewStrBufPlain(NULL, recps_len);
103
104         if (*recps == NULL) {
105                 syslog(LOG_ERR, "netmail: cannot allocate %ld bytes for recps", (long)recps_len);
106                 abort();
107         }
108
109         /* Each recipient */
110         for (nptr = OneRNCfg->NetConfigs[Which]; nptr != NULL; nptr = nptr->next) {
111                 if (nptr != OneRNCfg->NetConfigs[Which]) {
112                         for (i = 0; i < nSegments; i++)
113                                 StrBufAppendBufPlain(*recps, HKEY(","), i);
114                 }
115                 StrBufAppendBuf(*recps, nptr->Value[0], 0);
116                 if (Which == ignet_push_share)
117                 {
118                         StrBufAppendBufPlain(*recps, HKEY(","), 0);
119                         StrBufAppendBuf(*recps, nptr->Value[1], 0);
120
121                 }
122         }
123 }
124
125 static void ListCalculateSubject(struct CtdlMessage *msg)
126 {
127         struct CitContext *CCC = CC;
128         StrBuf *Subject, *FlatSubject;
129         int rlen;
130         char *pCh;
131
132         if (CM_IsEmpty(msg, eMsgSubject)) {
133                 Subject = NewStrBufPlain(HKEY("(no subject)"));
134         }
135         else {
136                 Subject = NewStrBufPlain(CM_KEY(msg, eMsgSubject));
137         }
138         FlatSubject = NewStrBufPlain(NULL, StrLength(Subject));
139         StrBuf_RFC822_to_Utf8(FlatSubject, Subject, NULL, NULL);
140
141         rlen = strlen(CCC->room.QRname);
142         pCh  = strstr(ChrPtr(FlatSubject), CCC->room.QRname);
143         if ((pCh == NULL) ||
144             (*(pCh + rlen) != ']') ||
145             (pCh == ChrPtr(FlatSubject)) ||
146             (*(pCh - 1) != '[')
147                 )
148         {
149                 StrBuf *tmp;
150                 StrBufPlain(Subject, HKEY("["));
151                 StrBufAppendBufPlain(Subject,
152                                      CCC->room.QRname,
153                                      rlen, 0);
154                 StrBufAppendBufPlain(Subject, HKEY("] "), 0);
155                 StrBufAppendBuf(Subject, FlatSubject, 0);
156                 /* so we can free the right one swap them */
157                 tmp = Subject;
158                 Subject = FlatSubject;
159                 FlatSubject = tmp;
160                 StrBufRFC2047encode(&Subject, FlatSubject);
161         }
162
163         CM_SetAsFieldSB(msg, eMsgSubject, &Subject);
164
165         FreeStrBuf(&FlatSubject);
166 }
167
168 /*
169  * Deliver digest messages
170  */
171 void network_deliver_digest(SpoolControl *sc)
172 {
173         struct CitContext *CCC = CC;
174         long len;
175         char buf[SIZ];
176         char *pbuf;
177         struct CtdlMessage *msg = NULL;
178         long msglen;
179         recptypes *valid;
180         char bounce_to[256];
181
182         if (sc->Users[digestrecp] == NULL)
183                 return;
184
185         msg = malloc(sizeof(struct CtdlMessage));
186         memset(msg, 0, sizeof(struct CtdlMessage));
187         msg->cm_magic = CTDLMESSAGE_MAGIC;
188         msg->cm_format_type = FMT_RFC822;
189         msg->cm_anon_type = MES_NORMAL;
190
191         CM_SetFieldLONG(msg, eTimestamp, time(NULL));
192         CM_SetField(msg, eAuthor, CCC->room.QRname, strlen(CCC->room.QRname));
193         len = snprintf(buf, sizeof buf, "[%s]", CCC->room.QRname);
194         CM_SetField(msg, eMsgSubject, buf, len);
195
196         CM_SetField(msg, erFc822Addr, SKEY(sc->Users[roommailalias]));
197         CM_SetField(msg, eRecipient, SKEY(sc->Users[roommailalias]));
198
199         /* Set the 'List-ID' header */
200         CM_SetField(msg, eListID, SKEY(sc->ListID));
201
202         /*
203          * Go fetch the contents of the digest
204          */
205         fseek(sc->digestfp, 0L, SEEK_END);
206         msglen = ftell(sc->digestfp);
207
208         pbuf = malloc(msglen + 1);
209         fseek(sc->digestfp, 0L, SEEK_SET);
210         fread(pbuf, (size_t)msglen, 1, sc->digestfp);
211         pbuf[msglen] = '\0';
212         CM_SetAsField(msg, eMesageText, &pbuf, msglen);
213
214         /* Now generate the delivery instructions */
215
216         /* Where do we want bounces and other noise to be heard?
217          * Surely not the list members! */
218         snprintf(bounce_to, sizeof bounce_to, "room_aide@%s", CtdlGetConfigStr("c_fqdn"));
219
220         /* Now submit the message */
221         valid = validate_recipients(ChrPtr(sc->Users[digestrecp]), NULL, 0);
222         if (valid != NULL) {
223                 valid->bounce_to = strdup(bounce_to);
224                 valid->envelope_from = strdup(bounce_to);
225                 CtdlSubmitMsg(msg, valid, NULL, 0);
226         }
227         CM_Free(msg);
228         free_recipients(valid);
229 }
230
231
232 void network_process_digest(SpoolControl *sc, struct CtdlMessage *omsg, long *delete_after_send)
233 {
234
235         struct CtdlMessage *msg = NULL;
236
237         if (sc->Users[digestrecp] == NULL)
238                 return;
239
240         /* If there are digest recipients, we have to build a digest */
241         if (sc->digestfp == NULL) {
242                 
243                 sc->digestfp = create_digest_file(&sc->room, 1);
244
245                 if (sc->digestfp == NULL)
246                         return;
247
248                 sc->haveDigest = ftell(sc->digestfp) > 0;
249                 if (!sc->haveDigest) {
250                         fprintf(sc->digestfp, "Content-type: text/plain\n\n");
251                 }
252                 sc->haveDigest = 1;
253         }
254
255         msg = CM_Duplicate(omsg);
256         if (msg != NULL) {
257                 sc->haveDigest = 1;
258                 fprintf(sc->digestfp,
259                         " -----------------------------------"
260                         "------------------------------------"
261                         "-------\n");
262                 fprintf(sc->digestfp, "From: ");
263                 if (!CM_IsEmpty(msg, eAuthor)) {
264                         fprintf(sc->digestfp,
265                                 "%s ",
266                                 msg->cm_fields[eAuthor]);
267                 }
268                 if (!CM_IsEmpty(msg, erFc822Addr)) {
269                         fprintf(sc->digestfp,
270                                 "<%s> ",
271                                 msg->cm_fields[erFc822Addr]);
272                 }
273                 else if (!CM_IsEmpty(msg, eNodeName)) {
274                         fprintf(sc->digestfp,
275                                 "@%s ",
276                                 msg->cm_fields[eNodeName]);
277                 }
278                 fprintf(sc->digestfp, "\n");
279                 if (!CM_IsEmpty(msg, eMsgSubject)) {
280                         fprintf(sc->digestfp,
281                                 "Subject: %s\n",
282                                 msg->cm_fields[eMsgSubject]);
283                 }
284
285                 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
286
287                 safestrncpy(CC->preferred_formats,
288                             "text/plain",
289                             sizeof CC->preferred_formats);
290
291                 CtdlOutputPreLoadedMsg(msg,
292                                        MT_CITADEL,
293                                        HEADERS_NONE,
294                                        0, 0, 0);
295
296                 StrBufTrim(CC->redirect_buffer);
297                 fwrite(HKEY("\n"), 1, sc->digestfp);
298                 fwrite(SKEY(CC->redirect_buffer), 1, sc->digestfp);
299                 fwrite(HKEY("\n"), 1, sc->digestfp);
300
301                 FreeStrBuf(&CC->redirect_buffer);
302
303                 sc->num_msgs_spooled += 1;
304                 CM_Free(msg);
305         }
306 }
307
308
309 void network_process_list(SpoolControl *sc, struct CtdlMessage *omsg, long *delete_after_send)
310 {
311         struct CtdlMessage *msg = NULL;
312
313         /*
314          * Process mailing list recipients
315          */
316         if (sc->Users[listrecp] == NULL)
317                 return;
318
319         /* create our own copy of the message.
320          *  We're going to need to modify it
321          * in order to insert the [list name] in it, etc.
322          */
323
324         msg = CM_Duplicate(omsg);
325
326
327         CM_SetField(msg, eReplyTo, SKEY(sc->Users[roommailalias]));
328
329         /* if there is no other recipient, Set the recipient
330          * of the list message to the email address of the
331          * room itself.
332          */
333         if (CM_IsEmpty(msg, eRecipient))
334         {
335                 CM_SetField(msg, eRecipient, SKEY(sc->Users[roommailalias]));
336         }
337
338         /* Set the 'List-ID' header */
339         CM_SetField(msg, eListID, SKEY(sc->ListID));
340
341
342         /* Prepend "[List name]" to the subject */
343         ListCalculateSubject(msg);
344
345         /* Handle delivery */
346         network_deliver_list(msg, sc, CC->room.QRname);
347         CM_Free(msg);
348 }
349
350 /*
351  * Deliver list messages to everyone on the list ... efficiently
352  */
353 void network_deliver_list(struct CtdlMessage *msg, SpoolControl *sc, const char *RoomName)
354 {
355         recptypes *valid;
356         char bounce_to[256];
357
358         /* Don't do this if there were no recipients! */
359         if (sc->Users[listrecp] == NULL)
360                 return;
361
362         /* Now generate the delivery instructions */
363
364         /* Where do we want bounces and other noise to be heard?
365          *  Surely not the list members! */
366         snprintf(bounce_to, sizeof bounce_to, "room_aide@%s", CtdlGetConfigStr("c_fqdn"));
367
368         /* Now submit the message */
369         valid = validate_recipients(ChrPtr(sc->Users[listrecp]), NULL, 0);
370         if (valid != NULL) {
371                 valid->bounce_to = strdup(bounce_to);
372                 valid->envelope_from = strdup(bounce_to);
373                 valid->sending_room = strdup(RoomName);
374                 CtdlSubmitMsg(msg, valid, NULL, 0);
375                 free_recipients(valid);
376         }
377         /* Do not call CM_Free(msg) here; the caller will free it. */
378 }
379
380
381 void network_process_participate(SpoolControl *sc, struct CtdlMessage *omsg, long *delete_after_send)
382 {
383         struct CtdlMessage *msg = NULL;
384         int ok_to_participate = 0;
385         recptypes *valid;
386
387         /*
388          * Process client-side list participations for this room
389          */
390         if (sc->Users[participate] == NULL)
391                 return;
392
393         msg = CM_Duplicate(omsg);
394
395         /* Only send messages which originated on our own
396          * Citadel network, otherwise we'll end up sending the
397          * remote mailing list's messages back to it, which
398          * is rude...
399          */
400         ok_to_participate = 0;
401         if (!CM_IsEmpty(msg, eNodeName)) {
402                 if (!strcasecmp(msg->cm_fields[eNodeName], CtdlGetConfigStr("c_nodename")))
403                 {
404                         ok_to_participate = 1;
405                 }
406         }
407         if (ok_to_participate)
408         {
409                 /* Replace the Internet email address of the
410                  * actual author with the email address of the
411                  * room itself, so the remote listserv doesn't
412                  * reject us.
413                  */
414                 CM_SetField(msg, erFc822Addr, SKEY(sc->Users[roommailalias]));
415
416                 valid = validate_recipients(ChrPtr(sc->Users[participate]) , NULL, 0);
417
418                 CM_SetField(msg, eRecipient, SKEY(sc->Users[roommailalias]));
419                 CtdlSubmitMsg(msg, valid, "", 0);
420                 free_recipients(valid);
421         }
422         CM_Free(msg);
423 }
424
425
426 /*
427  * Spools out one message from the list.
428  */
429 void network_spool_msg(long msgnum, void *userdata)
430 {
431         struct CtdlMessage *msg = NULL;
432         long delete_after_send = 0;     /* Set to 1 to delete after spooling */
433         SpoolControl *sc;
434
435         sc = (SpoolControl *)userdata;
436         msg = CtdlFetchMessage(msgnum, 1, 1);
437
438         if (msg == NULL)
439         {
440                 syslog(LOG_ERR, "netmail: failed to load Message <%ld> from disk", msgnum);
441                 return;
442         }
443         network_process_list(sc, msg, &delete_after_send);
444         network_process_digest(sc, msg, &delete_after_send);
445         network_process_participate(sc, msg, &delete_after_send);
446         
447         CM_Free(msg);
448
449         /* update lastsent */
450         sc->lastsent = msgnum;
451
452         /* Delete this message if delete-after-send is set */
453         if (delete_after_send) {
454                 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");
455         }
456 }