More use of %m instead of strerror(errno)
[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-2017 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         StrBuf *Buf = NULL;
386         recptypes *valid;
387
388         /*
389          * Process client-side list participations for this room
390          */
391         if (sc->Users[participate] == NULL)
392                 return;
393
394         msg = CM_Duplicate(omsg);
395
396         /* Only send messages which originated on our own
397          * Citadel network, otherwise we'll end up sending the
398          * remote mailing list's messages back to it, which
399          * is rude...
400          */
401         ok_to_participate = 0;
402         if (!CM_IsEmpty(msg, eNodeName)) {
403                 if (!strcasecmp(msg->cm_fields[eNodeName], CtdlGetConfigStr("c_nodename")))
404                 {
405                         ok_to_participate = 1;
406                 }
407                 
408                 Buf = NewStrBufPlain(CM_KEY(msg, eNodeName));
409                 if (CtdlIsValidNode(NULL,
410                                     NULL,
411                                     Buf,
412                                     sc->working_ignetcfg,
413                                     sc->the_netmap) == 0)
414                 {
415                         ok_to_participate = 1;
416                 }
417         }
418         if (ok_to_participate)
419         {
420                 /* Replace the Internet email address of the
421                  * actual author with the email address of the
422                  * room itself, so the remote listserv doesn't
423                  * reject us.
424                  */
425                 CM_SetField(msg, erFc822Addr, SKEY(sc->Users[roommailalias]));
426
427                 valid = validate_recipients(ChrPtr(sc->Users[participate]) , NULL, 0);
428
429                 CM_SetField(msg, eRecipient, SKEY(sc->Users[roommailalias]));
430                 CtdlSubmitMsg(msg, valid, "", 0);
431                 free_recipients(valid);
432         }
433         FreeStrBuf(&Buf);
434         CM_Free(msg);
435 }
436
437 void network_process_ignetpush(SpoolControl *sc, struct CtdlMessage *omsg, long *delete_after_send)
438 {
439         StrBuf *Recipient;
440         StrBuf *RemoteRoom;
441         const char *Pos = NULL;
442         struct CtdlMessage *msg = NULL;
443         struct CitContext *CCC = CC;
444         struct ser_ret sermsg;
445         char buf[SIZ];
446         char filename[PATH_MAX];
447         FILE *fp;
448         StrBuf *Buf = NULL;
449         int i;
450         int bang = 0;
451         int send = 1;
452
453         if (sc->Users[ignet_push_share] == NULL)
454                 return;
455
456         /*
457          * Process IGnet push shares
458          */
459         msg = CM_Duplicate(omsg);
460
461         /* Prepend our node name to the Path field whenever
462          * sending a message to another IGnet node
463          */
464         Netmap_AddMe(msg, HKEY("username"));
465
466         /*
467          * Determine if this message is set to be deleted
468          * after sending out on the network
469          */
470         if (!CM_IsEmpty(msg, eSpecialField)) {
471                 if (!strcasecmp(msg->cm_fields[eSpecialField], "CANCEL")) {
472                         *delete_after_send = 1;
473                 }
474         }
475
476         /* Now send it to every node */
477         Recipient = NewStrBufPlain(NULL, StrLength(sc->Users[ignet_push_share]));
478         RemoteRoom = NewStrBufPlain(NULL, StrLength(sc->Users[ignet_push_share]));
479         while ((Pos != StrBufNOTNULL) &&
480                StrBufExtract_NextToken(Recipient, sc->Users[ignet_push_share], &Pos, ','))
481         {
482                 StrBufExtract_NextToken(RemoteRoom, sc->Users[ignet_push_share], &Pos, ',');
483                 send = 1;
484                 NewStrBufDupAppendFlush(&Buf, Recipient, NULL, 1);
485                         
486                 /* Check for valid node name */
487                 if (CtdlIsValidNode(NULL,
488                                     NULL,
489                                     Buf,
490                                     sc->working_ignetcfg,
491                                     sc->the_netmap) != 0)
492                 {
493                         syslog(LOG_ERR, "netmail: invalid node <%s>", ChrPtr(Recipient));
494                         send = 0;
495                 }
496                 
497                 /* Check for split horizon */
498                 syslog(LOG_DEBUG, "netmail: path is %s", msg->cm_fields[eMessagePath]);
499                 bang = num_tokens(msg->cm_fields[eMessagePath], '!');
500                 if (bang > 1) {
501                         for (i=0; i<(bang-1); ++i) {
502                                 extract_token(buf,
503                                               msg->cm_fields[eMessagePath],
504                                               i, '!',
505                                               sizeof buf);
506
507                                 syslog(LOG_DEBUG, "netmail: compare <%s> to <%s>", buf, ChrPtr(Recipient)) ;
508                                 if (!strcasecmp(buf, ChrPtr(Recipient))) {
509                                         send = 0;
510                                         break;
511                                 }
512                         }
513                         
514                         syslog(LOG_INFO, "netmail: %ssending to %s", (send)?"":"not ", ChrPtr(Recipient));
515                 }
516                 
517                 /* Send the message */
518                 if (send == 1)
519                 {
520                         /*
521                          * Force the message to appear in the correct
522                          * room on the far end by setting the C field
523                          * correctly
524                          */
525                         if (StrLength(RemoteRoom) > 0) {
526                                 CM_SetField(msg, eRemoteRoom, SKEY(RemoteRoom));
527                         }
528                         else {
529                                 CM_SetField(msg, eRemoteRoom, CCC->room.QRname, strlen(CCC->room.QRname));
530                         }
531                         
532                         /* serialize it for transmission */
533                         CtdlSerializeMessage(&sermsg, msg);
534                         if (sermsg.len > 0) {
535                                 
536                                 /* write it to a spool file */
537                                 snprintf(filename,
538                                         sizeof(filename),
539                                         "%s/%s@%lx%x-mail",
540                                         ctdl_netout_dir,
541                                         ChrPtr(Recipient),
542                                         time(NULL),
543                                         rand()
544                                 );
545                                         
546                                 syslog(LOG_DEBUG, "netmail: appending to %s", filename);
547                                 
548                                 fp = fopen(filename, "ab");
549                                 if (fp != NULL) {
550                                         fwrite(sermsg.ser, sermsg.len, 1, fp);
551                                         fclose(fp);
552                                 }
553                                 else {
554                                         syslog(LOG_ERR, "%s: %m", filename);
555                                 }
556
557                                 /* free the serialized version */
558                                 free(sermsg.ser);
559                         }
560                 }
561         }
562         FreeStrBuf(&Buf);
563         FreeStrBuf(&Recipient);
564         FreeStrBuf(&RemoteRoom);
565         CM_Free(msg);
566 }
567
568
569 /*
570  * Spools out one message from the list.
571  */
572 void network_spool_msg(long msgnum, void *userdata)
573 {
574         struct CtdlMessage *msg = NULL;
575         long delete_after_send = 0;     /* Set to 1 to delete after spooling */
576         SpoolControl *sc;
577
578         sc = (SpoolControl *)userdata;
579         msg = CtdlFetchMessage(msgnum, 1, 1);
580
581         if (msg == NULL)
582         {
583                 syslog(LOG_ERR, "netmail: failed to load Message <%ld> from disk", msgnum);
584                 return;
585         }
586         network_process_list(sc, msg, &delete_after_send);
587         network_process_digest(sc, msg, &delete_after_send);
588         network_process_participate(sc, msg, &delete_after_send);
589         network_process_ignetpush(sc, msg, &delete_after_send);
590         
591         CM_Free(msg);
592
593         /* update lastsent */
594         sc->lastsent = msgnum;
595
596         /* Delete this message if delete-after-send is set */
597         if (delete_after_send) {
598                 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");
599         }
600 }