Route all access to cm_fields[] through api functions
[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-2012 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 "file_ops.h"
76 #include "citadel_dirs.h"
77 #include "threads.h"
78 #include "context.h"
79 #include "ctdl_module.h"
80 #include "netspool.h"
81 #include "netmail.h"
82
83 void network_deliver_list(struct CtdlMessage *msg, SpoolControl *sc, const char *RoomName);
84
85 void aggregate_recipients(StrBuf **recps, RoomNetCfg Which, OneRoomNetCfg *OneRNCfg, long nSegments)
86 {
87         int i;
88         size_t recps_len = 0;
89         RoomNetCfgLine *nptr;
90         struct CitContext *CCC = CC;
91
92         *recps = NULL;
93         /*
94          * Figure out how big a buffer we need to allocate
95          */
96         for (nptr = OneRNCfg->NetConfigs[Which]; nptr != NULL; nptr = nptr->next) {
97                 recps_len = recps_len + StrLength(nptr->Value[0]) + 2;
98         }
99
100         /* Nothing todo... */
101         if (recps_len == 0)
102                 return;
103
104         *recps = NewStrBufPlain(NULL, recps_len);
105
106         if (*recps == NULL) {
107                 QN_syslog(LOG_EMERG,
108                           "Cannot allocate %ld bytes for recps...\n",
109                           (long)recps_len);
110                 abort();
111         }
112
113         /* Each recipient */
114         for (nptr = OneRNCfg->NetConfigs[Which]; nptr != NULL; nptr = nptr->next) {
115                 if (nptr != OneRNCfg->NetConfigs[Which]) {
116                         for (i = 0; i < nSegments; i++)
117                                 StrBufAppendBufPlain(*recps, HKEY(","), i);
118                 }
119                 StrBufAppendBuf(*recps, nptr->Value[0], 0);
120                 if (Which == ignet_push_share)
121                 {
122                         StrBufAppendBufPlain(*recps, HKEY(","), 0);
123                         StrBufAppendBuf(*recps, nptr->Value[1], 0);
124
125                 }
126         }
127 }
128
129 static void ListCalculateSubject(struct CtdlMessage *msg)
130 {
131         struct CitContext *CCC = CC;
132         StrBuf *Subject, *FlatSubject;
133         int rlen;
134         char *pCh;
135
136         if (msg->cm_fields[eMsgSubject] == NULL) {
137                 Subject = NewStrBufPlain(HKEY("(no subject)"));
138         }
139         else {
140                 Subject = NewStrBufPlain(
141                         msg->cm_fields[eMsgSubject], -1);
142         }
143         FlatSubject = NewStrBufPlain(NULL, StrLength(Subject));
144         StrBuf_RFC822_to_Utf8(FlatSubject, Subject, NULL, NULL);
145
146         rlen = strlen(CCC->room.QRname);
147         pCh  = strstr(ChrPtr(FlatSubject), CCC->room.QRname);
148         if ((pCh == NULL) ||
149             (*(pCh + rlen) != ']') ||
150             (pCh == ChrPtr(FlatSubject)) ||
151             (*(pCh - 1) != '[')
152                 )
153         {
154                 StrBuf *tmp;
155                 StrBufPlain(Subject, HKEY("["));
156                 StrBufAppendBufPlain(Subject,
157                                      CCC->room.QRname,
158                                      rlen, 0);
159                 StrBufAppendBufPlain(Subject, HKEY("] "), 0);
160                 StrBufAppendBuf(Subject, FlatSubject, 0);
161                 /* so we can free the right one swap them */
162                 tmp = Subject;
163                 Subject = FlatSubject;
164                 FlatSubject = tmp;
165                 StrBufRFC2047encode(&Subject, FlatSubject);
166         }
167
168         CM_SetAsFieldSB(msg, eMsgSubject, &Subject);
169
170         FreeStrBuf(&FlatSubject);
171 }
172
173 /*
174  * Deliver digest messages
175  */
176 void network_deliver_digest(SpoolControl *sc)
177 {
178         struct CitContext *CCC = CC;
179         long len;
180         char buf[SIZ];
181         char *pbuf;
182         struct CtdlMessage *msg = NULL;
183         long msglen;
184         struct recptypes *valid;
185         char bounce_to[256];
186
187         if (sc->Users[listrecp] == NULL)
188                 return;
189
190         if (sc->num_msgs_spooled < 1) {
191                 fclose(sc->digestfp);
192                 sc->digestfp = NULL;
193                 return;
194         }
195
196         msg = malloc(sizeof(struct CtdlMessage));
197         memset(msg, 0, sizeof(struct CtdlMessage));
198         msg->cm_magic = CTDLMESSAGE_MAGIC;
199         msg->cm_format_type = FMT_RFC822;
200         msg->cm_anon_type = MES_NORMAL;
201
202         CM_SetFieldLONG(msg, eTimestamp, time(NULL));
203         CM_SetField(msg, eAuthor, CCC->room.QRname, strlen(CCC->room.QRname));
204         len = snprintf(buf, sizeof buf, "[%s]", CCC->room.QRname);
205         CM_SetField(msg, eMsgSubject, buf, len);
206
207         CM_SetField(msg, erFc822Addr, SKEY(sc->Users[roommailalias]));
208         CM_SetField(msg, eRecipient, SKEY(sc->Users[roommailalias]));
209
210         /* Set the 'List-ID' header */
211         CM_SetField(msg, eListID, SKEY(sc->ListID));
212
213         /*
214          * Go fetch the contents of the digest
215          */
216         fseek(sc->digestfp, 0L, SEEK_END);
217         msglen = ftell(sc->digestfp);
218
219         pbuf = malloc(msglen + 1);
220         fseek(sc->digestfp, 0L, SEEK_SET);
221         fread(pbuf, (size_t)msglen, 1, sc->digestfp);
222         pbuf[msglen] = '\0';
223         CM_SetAsField(msg, eMesageText, &pbuf, msglen);
224         fclose(sc->digestfp);
225         sc->digestfp = NULL;
226
227         /* Now generate the delivery instructions */
228         if (sc->Users[listrecp] == NULL)
229                 return;
230
231         /* Where do we want bounces and other noise to be heard?
232          *Surely not the list members! */
233         snprintf(bounce_to, sizeof bounce_to, "room_aide@%s", config.c_fqdn);
234
235         /* Now submit the message */
236         valid = validate_recipients(ChrPtr(sc->Users[listrecp]), NULL, 0);
237         if (valid != NULL) {
238                 valid->bounce_to = strdup(bounce_to);
239                 valid->envelope_from = strdup(bounce_to);
240                 CtdlSubmitMsg(msg, valid, NULL, 0);
241         }
242         CtdlFreeMessage(msg);
243         free_recipients(valid);
244 }
245
246
247 void network_process_digest(SpoolControl *sc, struct CtdlMessage *omsg, long *delete_after_send)
248 {
249
250         struct CtdlMessage *msg = NULL;
251
252         /*
253          * Process digest recipients
254          */
255         if ((sc->Users[digestrecp] == NULL)||
256             (sc->digestfp == NULL))
257                 return;
258
259         msg = CtdlDuplicateMessage(omsg);
260         if (msg != NULL) {
261                 fprintf(sc->digestfp,
262                         " -----------------------------------"
263                         "------------------------------------"
264                         "-------\n");
265                 fprintf(sc->digestfp, "From: ");
266                 if (msg->cm_fields[eAuthor] != NULL) {
267                         fprintf(sc->digestfp,
268                                 "%s ",
269                                 msg->cm_fields[eAuthor]);
270                 }
271                 if (msg->cm_fields[erFc822Addr] != NULL) {
272                         fprintf(sc->digestfp,
273                                 "<%s> ",
274                                 msg->cm_fields[erFc822Addr]);
275                 }
276                 else if (msg->cm_fields[eNodeName] != NULL) {
277                         fprintf(sc->digestfp,
278                                 "@%s ",
279                                 msg->cm_fields[eNodeName]);
280                 }
281                 fprintf(sc->digestfp, "\n");
282                 if (msg->cm_fields[eMsgSubject] != NULL) {
283                         fprintf(sc->digestfp,
284                                 "Subject: %s\n",
285                                 msg->cm_fields[eMsgSubject]);
286                 }
287
288                 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
289
290                 safestrncpy(CC->preferred_formats,
291                             "text/plain",
292                             sizeof CC->preferred_formats);
293
294                 CtdlOutputPreLoadedMsg(msg,
295                                        MT_CITADEL,
296                                        HEADERS_NONE,
297                                        0, 0, 0);
298
299                 StrBufTrim(CC->redirect_buffer);
300                 fwrite(HKEY("\n"), 1, sc->digestfp);
301                 fwrite(SKEY(CC->redirect_buffer), 1, sc->digestfp);
302                 fwrite(HKEY("\n"), 1, sc->digestfp);
303
304                 FreeStrBuf(&CC->redirect_buffer);
305
306                 sc->num_msgs_spooled += 1;
307                 CtdlFreeMessage(msg);
308         }
309 }
310
311
312 void network_process_list(SpoolControl *sc, struct CtdlMessage *omsg, long *delete_after_send)
313 {
314         struct CtdlMessage *msg = NULL;
315
316         /*
317          * Process mailing list recipients
318          */
319         if (sc->Users[listrecp] == NULL)
320                 return;
321
322         /* create our own copy of the message.
323          *  We're going to need to modify it
324          * in order to insert the [list name] in it, etc.
325          */
326
327         msg = CtdlDuplicateMessage(omsg);
328
329
330         CM_SetField(msg, eListID, SKEY(sc->Users[roommailalias]));
331
332         /* if there is no other recipient, Set the recipient
333          * of the list message to the email address of the
334          * room itself.
335          */
336         if ((msg->cm_fields[eRecipient] == NULL) ||
337             IsEmptyStr(msg->cm_fields[eRecipient]))
338         {
339                 CM_SetField(msg, eRecipient, SKEY(sc->Users[roommailalias]));
340         }
341
342         /* Set the 'List-ID' header */
343         CM_SetField(msg, eListID, SKEY(sc->ListID));
344
345
346         /* Prepend "[List name]" to the subject */
347         ListCalculateSubject(msg);
348
349         /* Handle delivery */
350         network_deliver_list(msg, sc, CC->room.QRname);
351         CtdlFreeMessage(msg);
352 }
353
354 /*
355  * Deliver list messages to everyone on the list ... efficiently
356  */
357 void network_deliver_list(struct CtdlMessage *msg, SpoolControl *sc, const char *RoomName)
358 {
359         struct recptypes *valid;
360         char bounce_to[256];
361
362         /* Don't do this if there were no recipients! */
363         if (sc->Users[listrecp] == NULL)
364                 return;
365
366         /* Now generate the delivery instructions */
367
368         /* Where do we want bounces and other noise to be heard?
369          *  Surely not the list members! */
370         snprintf(bounce_to, sizeof bounce_to, "room_aide@%s", config.c_fqdn);
371
372         /* Now submit the message */
373         valid = validate_recipients(ChrPtr(sc->Users[listrecp]), NULL, 0);
374         if (valid != NULL) {
375                 valid->bounce_to = strdup(bounce_to);
376                 valid->envelope_from = strdup(bounce_to);
377                 valid->sending_room = strdup(RoomName);
378                 CtdlSubmitMsg(msg, valid, NULL, 0);
379                 free_recipients(valid);
380         }
381         /* Do not call CtdlFreeMessage(msg) here; the caller will free it. */
382 }
383
384
385 void network_process_participate(SpoolControl *sc, struct CtdlMessage *omsg, long *delete_after_send)
386 {
387         struct CtdlMessage *msg = NULL;
388         int ok_to_participate = 0;
389         StrBuf *Buf = NULL;
390         struct recptypes *valid;
391
392         /*
393          * Process client-side list participations for this room
394          */
395         if (sc->Users[participate] == NULL)
396                 return;
397
398         msg = CtdlDuplicateMessage(omsg);
399
400         /* Only send messages which originated on our own
401          * Citadel network, otherwise we'll end up sending the
402          * remote mailing list's messages back to it, which
403          * is rude...
404          */
405         ok_to_participate = 0;
406         if (msg->cm_fields[eNodeName] != NULL) {
407                 if (!strcasecmp(msg->cm_fields[eNodeName],
408                                 config.c_nodename)) {
409                         ok_to_participate = 1;
410                 }
411                 
412                 Buf = NewStrBufPlain(msg->cm_fields[eNodeName], -1);
413                 if (CtdlIsValidNode(NULL,
414                                     NULL,
415                                     Buf,
416                                     sc->working_ignetcfg,
417                                     sc->the_netmap) == 0)
418                 {
419                         ok_to_participate = 1;
420                 }
421         }
422         if (ok_to_participate)
423         {
424                 /* Replace the Internet email address of the
425                  * actual author with the email address of the
426                  * room itself, so the remote listserv doesn't
427                  * reject us.
428                  */
429                 CM_SetField(msg, erFc822Addr, SKEY(sc->Users[roommailalias]));
430
431                 valid = validate_recipients(ChrPtr(sc->Users[participate]) , NULL, 0);
432
433                 CM_SetField(msg, eRecipient, SKEY(sc->Users[roommailalias]));
434                 CtdlSubmitMsg(msg, valid, "", 0);
435                 free_recipients(valid);
436         }
437         FreeStrBuf(&Buf);
438         CtdlFreeMessage(msg);
439 }
440
441 void network_process_ignetpush(SpoolControl *sc, struct CtdlMessage *omsg, long *delete_after_send)
442 {
443         StrBuf *Recipient;
444         StrBuf *RemoteRoom;
445         const char *Pos = NULL;
446         struct CtdlMessage *msg = NULL;
447         struct CitContext *CCC = CC;
448         struct ser_ret sermsg;
449         char buf[SIZ];
450         char filename[PATH_MAX];
451         FILE *fp;
452         StrBuf *Buf = NULL;
453         int i;
454         int bang = 0;
455         int send = 1;
456
457         if (sc->Users[ignet_push_share] == NULL)
458                 return;
459
460         /*
461          * Process IGnet push shares
462          */
463         msg = CtdlDuplicateMessage(omsg);
464
465         /* Prepend our node name to the Path field whenever
466          * sending a message to another IGnet node
467          */
468         Netmap_AddMe(msg, HKEY("username"));
469
470         /*
471          * Determine if this message is set to be deleted
472          * after sending out on the network
473          */
474         if (msg->cm_fields[eSpecialField] != NULL) {
475                 if (!strcasecmp(msg->cm_fields[eSpecialField], "CANCEL")) {
476                         *delete_after_send = 1;
477                 }
478         }
479
480         /* Now send it to every node */
481         Recipient = NewStrBufPlain(NULL, StrLength(sc->Users[ignet_push_share]));
482         RemoteRoom = NewStrBufPlain(NULL, StrLength(sc->Users[ignet_push_share]));
483         while ((Pos != StrBufNOTNULL) &&
484                StrBufExtract_NextToken(Recipient, sc->Users[ignet_push_share], &Pos, ','))
485         {
486                 StrBufExtract_NextToken(RemoteRoom, sc->Users[ignet_push_share], &Pos, ',');
487                 send = 1;
488                 NewStrBufDupAppendFlush(&Buf, Recipient, NULL, 1);
489                         
490                 /* Check for valid node name */
491                 if (CtdlIsValidNode(NULL,
492                                     NULL,
493                                     Buf,
494                                     sc->working_ignetcfg,
495                                     sc->the_netmap) != 0)
496                 {
497                         QN_syslog(LOG_ERR,
498                                   "Invalid node <%s>\n",
499                                   ChrPtr(Recipient));
500                         
501                         send = 0;
502                 }
503                 
504                 /* Check for split horizon */
505                 QN_syslog(LOG_DEBUG, "Path is %s\n", msg->cm_fields[eMessagePath]);
506                 bang = num_tokens(msg->cm_fields[eMessagePath], '!');
507                 if (bang > 1) {
508                         for (i=0; i<(bang-1); ++i) {
509                                 extract_token(buf,
510                                               msg->cm_fields[eMessagePath],
511                                               i, '!',
512                                               sizeof buf);
513
514                                 QN_syslog(LOG_DEBUG, "Compare <%s> to <%s>\n",
515                                           buf, ChrPtr(Recipient)) ;
516                                 if (!strcasecmp(buf, ChrPtr(Recipient))) {
517                                         send = 0;
518                                         break;
519                                 }
520                         }
521                         
522                         QN_syslog(LOG_INFO,
523                                   " %sSending to %s\n",
524                                   (send)?"":"Not ",
525                                   ChrPtr(Recipient));
526                 }
527                 
528                 /* Send the message */
529                 if (send == 1)
530                 {
531                         /*
532                          * Force the message to appear in the correct
533                          * room on the far end by setting the C field
534                          * correctly
535                          */
536                         if (StrLength(RemoteRoom) > 0) {
537                                 CM_SetField(msg, eRemoteRoom, SKEY(RemoteRoom));
538                         }
539                         else {
540                                 CM_SetField(msg, eRemoteRoom, CCC->room.QRname, strlen(CCC->room.QRname));
541                         }
542                         
543                         /* serialize it for transmission */
544                         serialize_message(&sermsg, msg);
545                         if (sermsg.len > 0) {
546                                 
547                                 /* write it to a spool file */
548                                 snprintf(filename,
549                                          sizeof(filename),
550                                          "%s/%s@%lx%x",
551                                          ctdl_netout_dir,
552                                          ChrPtr(Recipient),
553                                          time(NULL),
554                                          rand()
555                                         );
556                                         
557                                 QN_syslog(LOG_DEBUG,
558                                           "Appending to %s\n",
559                                           filename);
560                                 
561                                 fp = fopen(filename, "ab");
562                                 if (fp != NULL) {
563                                         fwrite(sermsg.ser,
564                                                sermsg.len, 1, fp);
565                                         fclose(fp);
566                                 }
567                                 else {
568                                         QN_syslog(LOG_ERR,
569                                                   "%s: %s\n",
570                                                   filename,
571                                                   strerror(errno));
572                                 }
573
574                                 /* free the serialized version */
575                                 free(sermsg.ser);
576                         }
577                 }
578         }
579         FreeStrBuf(&Buf);
580         FreeStrBuf(&Recipient);
581         FreeStrBuf(&RemoteRoom);
582         CtdlFreeMessage(msg);
583 }
584
585
586 /*
587  * Spools out one message from the list.
588  */
589 void network_spool_msg(long msgnum,
590                        void *userdata)
591 {
592         struct CitContext *CCC = CC;
593         struct CtdlMessage *msg = NULL;
594         long delete_after_send = 0;     /* Set to 1 to delete after spooling */
595         SpoolControl *sc;
596
597         sc = (SpoolControl *)userdata;
598
599         msg = CtdlFetchMessage(msgnum, 1);
600
601         if (msg == NULL)
602         {
603                 QN_syslog(LOG_ERR,
604                           "failed to load Message <%ld> from disk\n",
605                           msgnum);
606                 return;
607         }
608         network_process_list(sc, msg, &delete_after_send);
609         network_process_digest(sc, msg, &delete_after_send);
610         network_process_participate(sc, msg, &delete_after_send);
611         network_process_ignetpush(sc, msg, &delete_after_send);
612         
613         CtdlFreeMessage(msg);
614
615         /* update lastsent */
616         sc->lastsent = msgnum;
617
618         /* Delete this message if delete-after-send is set */
619         if (delete_after_send) {
620                 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");
621         }
622 }