Remove global variables; replace by stack passing.
[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-2011 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 as published by
9  *  the Free Software Foundation; either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  * ** NOTE **   A word on the S_NETCONFIGS semaphore:
22  * This is a fairly high-level type of critical section.  It ensures that no
23  * two threads work on the netconfigs files at the same time.  Since we do
24  * so many things inside these, here are the rules:
25  *  1. begin_critical_section(S_NETCONFIGS) *before* begin_ any others.
26  *  2. Do *not* perform any I/O with the client during these sections.
27  *
28  */
29
30 /*
31  * Duration of time (in seconds) after which pending list subscribe/unsubscribe
32  * requests that have not been confirmed will be deleted.
33  */
34 #define EXP     259200  /* three days */
35
36 #include "sysdep.h"
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <stdio.h>
40 #include <fcntl.h>
41 #include <ctype.h>
42 #include <signal.h>
43 #include <pwd.h>
44 #include <errno.h>
45 #include <sys/stat.h>
46 #include <sys/types.h>
47 #include <dirent.h>
48 #if TIME_WITH_SYS_TIME
49 # include <sys/time.h>
50 # include <time.h>
51 #else
52 # if HAVE_SYS_TIME_H
53 #  include <sys/time.h>
54 # else
55 #  include <time.h>
56 # endif
57 #endif
58 #ifdef HAVE_SYSCALL_H
59 # include <syscall.h>
60 #else 
61 # if HAVE_SYS_SYSCALL_H
62 #  include <sys/syscall.h>
63 # endif
64 #endif
65
66 #include <sys/wait.h>
67 #include <string.h>
68 #include <limits.h>
69 #include <libcitadel.h>
70 #include "citadel.h"
71 #include "server.h"
72 #include "citserver.h"
73 #include "support.h"
74 #include "config.h"
75 #include "user_ops.h"
76 #include "database.h"
77 #include "msgbase.h"
78 #include "internet_addressing.h"
79 #include "serv_network.h"
80 #include "clientsocket.h"
81 #include "file_ops.h"
82 #include "citadel_dirs.h"
83 #include "threads.h"
84
85 #ifndef HAVE_SNPRINTF
86 #include "snprintf.h"
87 #endif
88
89 #include "context.h"
90 #include "netconfig.h"
91 #include "netspool.h"
92 #include "ctdl_module.h"
93
94
95 /*
96  * Deliver digest messages
97  */
98 void network_deliver_digest(SpoolControl *sc) {
99         char buf[SIZ];
100         int i;
101         struct CtdlMessage *msg = NULL;
102         long msglen;
103         char *recps = NULL;
104         size_t recps_len = SIZ;
105         struct recptypes *valid;
106         namelist *nptr;
107         char bounce_to[256];
108
109         if (sc->num_msgs_spooled < 1) {
110                 fclose(sc->digestfp);
111                 sc->digestfp = NULL;
112                 return;
113         }
114
115         msg = malloc(sizeof(struct CtdlMessage));
116         memset(msg, 0, sizeof(struct CtdlMessage));
117         msg->cm_magic = CTDLMESSAGE_MAGIC;
118         msg->cm_format_type = FMT_RFC822;
119         msg->cm_anon_type = MES_NORMAL;
120
121         sprintf(buf, "%ld", time(NULL));
122         msg->cm_fields['T'] = strdup(buf);
123         msg->cm_fields['A'] = strdup(CC->room.QRname);
124         snprintf(buf, sizeof buf, "[%s]", CC->room.QRname);
125         msg->cm_fields['U'] = strdup(buf);
126         sprintf(buf, "room_%s@%s", CC->room.QRname, config.c_fqdn);
127         for (i=0; buf[i]; ++i) {
128                 if (isspace(buf[i])) buf[i]='_';
129                 buf[i] = tolower(buf[i]);
130         }
131         msg->cm_fields['F'] = strdup(buf);
132         msg->cm_fields['R'] = strdup(buf);
133
134         /* Set the 'List-ID' header */
135         msg->cm_fields['L'] = malloc(1024);
136         snprintf(msg->cm_fields['L'], 1024,
137                 "%s <%ld.list-id.%s>",
138                 CC->room.QRname,
139                 CC->room.QRnumber,
140                 config.c_fqdn
141         );
142
143         /*
144          * Go fetch the contents of the digest
145          */
146         fseek(sc->digestfp, 0L, SEEK_END);
147         msglen = ftell(sc->digestfp);
148
149         msg->cm_fields['M'] = malloc(msglen + 1);
150         fseek(sc->digestfp, 0L, SEEK_SET);
151         fread(msg->cm_fields['M'], (size_t)msglen, 1, sc->digestfp);
152         msg->cm_fields['M'][msglen] = '\0';
153
154         fclose(sc->digestfp);
155         sc->digestfp = NULL;
156
157         /* Now generate the delivery instructions */
158
159         /* 
160          * Figure out how big a buffer we need to allocate
161          */
162         for (nptr = sc->digestrecps; nptr != NULL; nptr = nptr->next) {
163                 recps_len = recps_len + strlen(nptr->name) + 2;
164         }
165         
166         recps = malloc(recps_len);
167
168         if (recps == NULL) {
169                 syslog(LOG_EMERG, "Cannot allocate %ld bytes for recps...\n", (long)recps_len);
170                 abort();
171         }
172
173         strcpy(recps, "");
174
175         /* Each recipient */
176         for (nptr = sc->digestrecps; nptr != NULL; nptr = nptr->next) {
177                 if (nptr != sc->digestrecps) {
178                         strcat(recps, ",");
179                 }
180                 strcat(recps, nptr->name);
181         }
182
183         /* Where do we want bounces and other noise to be heard?  Surely not the list members! */
184         snprintf(bounce_to, sizeof bounce_to, "room_aide@%s", config.c_fqdn);
185
186         /* Now submit the message */
187         valid = validate_recipients(recps, NULL, 0);
188         free(recps);
189         if (valid != NULL) {
190                 valid->bounce_to = strdup(bounce_to);
191                 valid->envelope_from = strdup(bounce_to);
192                 CtdlSubmitMsg(msg, valid, NULL, 0);
193         }
194         CtdlFreeMessage(msg);
195         free_recipients(valid);
196 }
197
198
199 /*
200  * Deliver list messages to everyone on the list ... efficiently
201  */
202 void network_deliver_list(struct CtdlMessage *msg, SpoolControl *sc) {
203         char *recps = NULL;
204         size_t recps_len = SIZ;
205         struct recptypes *valid;
206         namelist *nptr;
207         char bounce_to[256];
208
209         /* Don't do this if there were no recipients! */
210         if (sc->listrecps == NULL) return;
211
212         /* Now generate the delivery instructions */
213
214         /* 
215          * Figure out how big a buffer we need to allocate
216          */
217         for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
218                 recps_len = recps_len + strlen(nptr->name) + 2;
219         }
220         
221         recps = malloc(recps_len);
222
223         if (recps == NULL) {
224                 syslog(LOG_EMERG, "Cannot allocate %ld bytes for recps...\n", (long)recps_len);
225                 abort();
226         }
227
228         strcpy(recps, "");
229
230         /* Each recipient */
231         for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
232                 if (nptr != sc->listrecps) {
233                         strcat(recps, ",");
234                 }
235                 strcat(recps, nptr->name);
236         }
237
238         /* Where do we want bounces and other noise to be heard?  Surely not the list members! */
239         snprintf(bounce_to, sizeof bounce_to, "room_aide@%s", config.c_fqdn);
240
241         /* Now submit the message */
242         valid = validate_recipients(recps, NULL, 0);
243         free(recps);
244         if (valid != NULL) {
245                 valid->bounce_to = strdup(bounce_to);
246                 valid->envelope_from = strdup(bounce_to);
247                 CtdlSubmitMsg(msg, valid, NULL, 0);
248                 free_recipients(valid);
249         }
250         /* Do not call CtdlFreeMessage(msg) here; the caller will free it. */
251 }
252
253
254
255
256 /*
257  * Spools out one message from the list.
258  */
259 void network_spool_msg(long msgnum, 
260                        void *userdata, 
261                        char *working_ignetcfg,
262                        NetMap *the_netmap)
263 {
264         SpoolControl *sc;
265         int i;
266         char *newpath = NULL;
267         struct CtdlMessage *msg = NULL;
268         namelist *nptr;
269         maplist *mptr;
270         struct ser_ret sermsg;
271         FILE *fp;
272         char filename[PATH_MAX];
273         char buf[SIZ];
274         int bang = 0;
275         int send = 1;
276         int delete_after_send = 0;      /* Set to 1 to delete after spooling */
277         int ok_to_participate = 0;
278         struct recptypes *valid;
279
280         sc = (SpoolControl *)userdata;
281
282         /*
283          * Process mailing list recipients
284          */
285         if (sc->listrecps != NULL) {
286                 /* Fetch the message.  We're going to need to modify it
287                  * in order to insert the [list name] in it, etc.
288                  */
289                 msg = CtdlFetchMessage(msgnum, 1);
290                 if (msg != NULL) {
291                         int rlen;
292                         char *pCh;
293                         StrBuf *Subject, *FlatSubject;
294
295                         if (msg->cm_fields['V'] == NULL){
296                                 /* local message, no enVelope */
297                                 StrBuf *Buf;
298                                 Buf = NewStrBuf();
299                                 StrBufAppendBufPlain(Buf, msg->cm_fields['O'], -1, 0);
300                                 StrBufAppendBufPlain(Buf, HKEY("@"), 0);
301                                 StrBufAppendBufPlain(Buf, config.c_fqdn, -1, 0);
302                                 
303                                 msg->cm_fields['K'] = SmashStrBuf(&Buf);
304                         }
305                         else {
306                                 msg->cm_fields['K'] = strdup (msg->cm_fields['V']);
307                         }
308                         /* Set the 'List-ID' header */
309                         if (msg->cm_fields['L'] != NULL) {
310                                 free(msg->cm_fields['L']);
311                         }
312                         msg->cm_fields['L'] = malloc(1024);
313                         snprintf(msg->cm_fields['L'], 1024,
314                                 "%s <%ld.list-id.%s>",
315                                 CC->room.QRname,
316                                 CC->room.QRnumber,
317                                 config.c_fqdn
318                         );
319
320                         /* Prepend "[List name]" to the subject */
321                         if (msg->cm_fields['U'] == NULL) {
322                                 Subject = NewStrBufPlain(HKEY("(no subject)"));
323                         }
324                         else {
325                                 Subject = NewStrBufPlain(msg->cm_fields['U'], -1);
326                         }
327                         FlatSubject = NewStrBufPlain(NULL, StrLength(Subject));
328                         StrBuf_RFC822_to_Utf8(FlatSubject, Subject, NULL, NULL);
329
330                         rlen = strlen(CC->room.QRname);
331                         pCh  = strstr(ChrPtr(FlatSubject), CC->room.QRname);
332                         if ((pCh == NULL) ||
333                             (*(pCh + rlen) != ']') ||
334                             (pCh == ChrPtr(FlatSubject)) ||
335                             (*(pCh - 1) != '[')
336                                 )
337                         {
338                                 StrBuf *tmp;
339                                 StrBufPlain(Subject, HKEY("["));
340                                 StrBufAppendBufPlain(Subject, CC->room.QRname, rlen, 0);
341                                 StrBufAppendBufPlain(Subject, HKEY("] "), 0);
342                                 StrBufAppendBuf(Subject, FlatSubject, 0);
343                                 tmp = Subject;  Subject = FlatSubject;  FlatSubject = tmp; /* so we can free the right one... */
344                                 StrBufRFC2047encode(&Subject, FlatSubject);
345                         }
346                         
347                         if (msg->cm_fields['U'] != NULL)
348                                 free (msg->cm_fields['U']);
349                         msg->cm_fields['U'] = SmashStrBuf(&Subject);
350
351                         FreeStrBuf(&FlatSubject);
352
353                         /* else we won't modify the buffer, since the roomname is already here. */
354
355                         /* Set the recipient of the list message to the
356                          * email address of the room itself.
357                          * FIXME ... I want to be able to pick any address
358                          */
359                         if (msg->cm_fields['R'] != NULL) {
360                                 free(msg->cm_fields['R']);
361                         }
362                         msg->cm_fields['R'] = malloc(256);
363                         snprintf(msg->cm_fields['R'], 256,
364                                 "room_%s@%s", CC->room.QRname,
365                                 config.c_fqdn);
366                         for (i=0; msg->cm_fields['R'][i]; ++i) {
367                                 if (isspace(msg->cm_fields['R'][i])) {
368                                         msg->cm_fields['R'][i] = '_';
369                                 }
370                         }
371
372                         /* Handle delivery */
373                         network_deliver_list(msg, sc);
374                         CtdlFreeMessage(msg);
375                 }
376         }
377
378         /*
379          * Process digest recipients
380          */
381         if ((sc->digestrecps != NULL) && (sc->digestfp != NULL)) {
382                 msg = CtdlFetchMessage(msgnum, 1);
383                 if (msg != NULL) {
384                         fprintf(sc->digestfp,   " -----------------------------------"
385                                                 "------------------------------------"
386                                                 "-------\n");
387                         fprintf(sc->digestfp, "From: ");
388                         if (msg->cm_fields['A'] != NULL) {
389                                 fprintf(sc->digestfp, "%s ", msg->cm_fields['A']);
390                         }
391                         if (msg->cm_fields['F'] != NULL) {
392                                 fprintf(sc->digestfp, "<%s> ", msg->cm_fields['F']);
393                         }
394                         else if (msg->cm_fields['N'] != NULL) {
395                                 fprintf(sc->digestfp, "@%s ", msg->cm_fields['N']);
396                         }
397                         fprintf(sc->digestfp, "\n");
398                         if (msg->cm_fields['U'] != NULL) {
399                                 fprintf(sc->digestfp, "Subject: %s\n", msg->cm_fields['U']);
400                         }
401
402                         CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
403                         
404                         safestrncpy(CC->preferred_formats, "text/plain", sizeof CC->preferred_formats);
405                         CtdlOutputPreLoadedMsg(msg, MT_CITADEL, HEADERS_NONE, 0, 0, 0);
406
407                         StrBufTrim(CC->redirect_buffer);
408                         fwrite(HKEY("\n"), 1, sc->digestfp);
409                         fwrite(SKEY(CC->redirect_buffer), 1, sc->digestfp);
410                         fwrite(HKEY("\n"), 1, sc->digestfp);
411
412                         FreeStrBuf(&CC->redirect_buffer);
413
414                         sc->num_msgs_spooled += 1;
415                         free(msg);
416                 }
417         }
418
419         /*
420          * Process client-side list participations for this room
421          */
422         if (sc->participates != NULL) {
423                 msg = CtdlFetchMessage(msgnum, 1);
424                 if (msg != NULL) {
425
426                         /* Only send messages which originated on our own Citadel
427                          * network, otherwise we'll end up sending the remote
428                          * mailing list's messages back to it, which is rude...
429                          */
430                         ok_to_participate = 0;
431                         if (msg->cm_fields['N'] != NULL) {
432                                 if (!strcasecmp(msg->cm_fields['N'], config.c_nodename)) {
433                                         ok_to_participate = 1;
434                                 }
435                                 if (is_valid_node(NULL, 
436                                                   NULL, 
437                                                   msg->cm_fields['N'], 
438                                                   working_ignetcfg, 
439                                                   the_netmap) == 0)
440                                 {
441                                         ok_to_participate = 1;
442                                 }
443                         }
444                         if (ok_to_participate) {
445                                 if (msg->cm_fields['F'] != NULL) {
446                                         free(msg->cm_fields['F']);
447                                 }
448                                 msg->cm_fields['F'] = malloc(SIZ);
449                                 /* Replace the Internet email address of the actual
450                                 * author with the email address of the room itself,
451                                 * so the remote listserv doesn't reject us.
452                                 * FIXME ... I want to be able to pick any address
453                                 */
454                                 snprintf(msg->cm_fields['F'], SIZ,
455                                         "room_%s@%s", CC->room.QRname,
456                                         config.c_fqdn);
457                                 for (i=0; msg->cm_fields['F'][i]; ++i) {
458                                         if (isspace(msg->cm_fields['F'][i])) {
459                                                 msg->cm_fields['F'][i] = '_';
460                                         }
461                                 }
462
463                                 /* 
464                                  * Figure out how big a buffer we need to allocate
465                                  */
466                                 for (nptr = sc->participates; nptr != NULL; nptr = nptr->next) {
467
468                                         if (msg->cm_fields['R'] == NULL) {
469                                                 free(msg->cm_fields['R']);
470                                         }
471                                         msg->cm_fields['R'] = strdup(nptr->name);
472         
473                                         valid = validate_recipients(nptr->name, NULL, 0);
474                                         CtdlSubmitMsg(msg, valid, "", 0);
475                                         free_recipients(valid);
476                                 }
477                         
478                         }
479                         CtdlFreeMessage(msg);
480                 }
481         }
482         
483         /*
484          * Process IGnet push shares
485          */
486         msg = CtdlFetchMessage(msgnum, 1);
487         if (msg != NULL) {
488                 size_t newpath_len;
489
490                 /* Prepend our node name to the Path field whenever
491                  * sending a message to another IGnet node
492                  */
493                 if (msg->cm_fields['P'] == NULL) {
494                         msg->cm_fields['P'] = strdup("username");
495                 }
496                 newpath_len = strlen(msg->cm_fields['P']) +
497                          strlen(config.c_nodename) + 2;
498                 newpath = malloc(newpath_len);
499                 snprintf(newpath, newpath_len, "%s!%s",
500                          config.c_nodename, msg->cm_fields['P']);
501                 free(msg->cm_fields['P']);
502                 msg->cm_fields['P'] = newpath;
503
504                 /*
505                  * Determine if this message is set to be deleted
506                  * after sending out on the network
507                  */
508                 if (msg->cm_fields['S'] != NULL) {
509                         if (!strcasecmp(msg->cm_fields['S'], "CANCEL")) {
510                                 delete_after_send = 1;
511                         }
512                 }
513
514                 /* Now send it to every node */
515                 if (sc->ignet_push_shares != NULL)
516                   for (mptr = sc->ignet_push_shares; mptr != NULL;
517                     mptr = mptr->next) {
518
519                         send = 1;
520
521                         /* Check for valid node name */
522                         if (is_valid_node(NULL, 
523                                           NULL, 
524                                           mptr->remote_nodename, 
525                                           working_ignetcfg, 
526                                           the_netmap) != 0)
527                         {
528                                 syslog(LOG_ERR, "Invalid node <%s>\n", mptr->remote_nodename);
529                                 send = 0;
530                         }
531
532                         /* Check for split horizon */
533                         syslog(LOG_DEBUG, "Path is %s\n", msg->cm_fields['P']);
534                         bang = num_tokens(msg->cm_fields['P'], '!');
535                         if (bang > 1) for (i=0; i<(bang-1); ++i) {
536                                 extract_token(buf, msg->cm_fields['P'], i, '!', sizeof buf);
537                                 syslog(LOG_DEBUG, "Compare <%s> to <%s>\n",
538                                         buf, mptr->remote_nodename) ;
539                                 if (!strcasecmp(buf, mptr->remote_nodename)) {
540                                         send = 0;
541                                         syslog(LOG_DEBUG, "Not sending to %s\n",
542                                                 mptr->remote_nodename);
543                                 }
544                                 else {
545                                         syslog(LOG_DEBUG, "Sending to %s\n", mptr->remote_nodename);
546                                 }
547                         }
548
549                         /* Send the message */
550                         if (send == 1) {
551
552                                 /*
553                                  * Force the message to appear in the correct room
554                                  * on the far end by setting the C field correctly
555                                  */
556                                 if (msg->cm_fields['C'] != NULL) {
557                                         free(msg->cm_fields['C']);
558                                 }
559                                 if (!IsEmptyStr(mptr->remote_roomname)) {
560                                         msg->cm_fields['C'] = strdup(mptr->remote_roomname);
561                                 }
562                                 else {
563                                         msg->cm_fields['C'] = strdup(CC->room.QRname);
564                                 }
565
566                                 /* serialize it for transmission */
567                                 serialize_message(&sermsg, msg);
568                                 if (sermsg.len > 0) {
569
570                                         /* write it to a spool file */
571                                         snprintf(filename, sizeof filename,"%s/%s@%lx%x",
572                                                 ctdl_netout_dir,
573                                                 mptr->remote_nodename,
574                                                 time(NULL),
575                                                 rand()
576                                         );
577                                         syslog(LOG_DEBUG, "Appending to %s\n", filename);
578                                         fp = fopen(filename, "ab");
579                                         if (fp != NULL) {
580                                                 fwrite(sermsg.ser,
581                                                         sermsg.len, 1, fp);
582                                                 fclose(fp);
583                                         }
584                                         else {
585                                                 syslog(LOG_ERR, "%s: %s\n", filename, strerror(errno));
586                                         }
587         
588                                         /* free the serialized version */
589                                         free(sermsg.ser);
590                                 }
591
592                         }
593                 }
594                 CtdlFreeMessage(msg);
595         }
596
597         /* update lastsent */
598         sc->lastsent = msgnum;
599
600         /* Delete this message if delete-after-send is set */
601         if (delete_after_send) {
602                 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");
603         }
604
605 }