5ddd1976e293f2550e2691d721bbd38a3cbb510a
[citadel.git] / citadel / modules / network / serv_netspool.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 <assert.h>
64 #include <libcitadel.h>
65 #include "citadel.h"
66 #include "server.h"
67 #include "citserver.h"
68 #include "support.h"
69 #include "config.h"
70 #include "user_ops.h"
71 #include "database.h"
72 #include "msgbase.h"
73 #include "internet_addressing.h"
74 #include "serv_network.h"
75 #include "clientsocket.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
84 void ParseLastSent(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, OneRoomNetCfg *OneRNCFG)
85 {
86         RoomNetCfgLine *nptr;
87         nptr = (RoomNetCfgLine *) malloc(sizeof(RoomNetCfgLine));
88         memset(nptr, 0, sizeof(RoomNetCfgLine));
89         OneRNCFG->lastsent = extract_long(LinePos, 0);
90         OneRNCFG->NetConfigs[ThisOne->C] = nptr;
91 }
92
93 void ParseRoomAlias(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, OneRoomNetCfg *rncfg)
94 {
95         if (rncfg->Sender != NULL) {
96                 return;
97         }
98
99         ParseGeneric(ThisOne, Line, LinePos, rncfg);
100         rncfg->Sender = NewStrBufDup(rncfg->NetConfigs[roommailalias]->Value[0]);
101 }
102
103 void ParseSubPendingLine(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, OneRoomNetCfg *OneRNCFG)
104 {
105         if (time(NULL) - extract_long(LinePos, 3) > EXP) 
106                 return; /* expired subscription... */
107
108         ParseGeneric(ThisOne, Line, LinePos, OneRNCFG);
109 }
110 void ParseUnSubPendingLine(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, OneRoomNetCfg *OneRNCFG)
111 {
112         if (time(NULL) - extract_long(LinePos, 2) > EXP)
113                 return; /* expired subscription... */
114
115         ParseGeneric(ThisOne, Line, LinePos, OneRNCFG);
116 }
117
118
119 void SerializeLastSent(const CfgLineType *ThisOne, StrBuf *OutputBuffer, OneRoomNetCfg *RNCfg, RoomNetCfgLine *data)
120 {
121         StrBufAppendBufPlain(OutputBuffer, CKEY(ThisOne->Str), 0);
122         StrBufAppendPrintf(OutputBuffer, "|%ld\n", RNCfg->lastsent);
123 }
124
125 void DeleteLastSent(const CfgLineType *ThisOne, RoomNetCfgLine **data)
126 {
127         free(*data);
128         *data = NULL;
129 }
130
131 static const RoomNetCfg SpoolCfgs [4] = {
132         listrecp,
133         digestrecp,
134         participate,
135 };
136
137 static const long SpoolCfgsCopyN [4] = {
138         1, 1, 1, 2
139 };
140
141 int HaveSpoolConfig(OneRoomNetCfg* RNCfg)
142 {
143         int i;
144         int interested = 0;
145         for (i=0; i < 4; i++) if (RNCfg->NetConfigs[SpoolCfgs[i]] == NULL) interested = 1;
146         return interested;
147 }
148
149 void Netmap_AddMe(struct CtdlMessage *msg, const char *defl, long defllen)
150 {
151         long node_len;
152         char buf[SIZ];
153
154         /* prepend our node to the path */
155         if (CM_IsEmpty(msg, eMessagePath)) {
156                 CM_SetField(msg, eMessagePath, defl, defllen);
157         }
158         node_len = strlen(CtdlGetConfigStr("c_nodename"));
159         if (node_len >= SIZ) 
160                 node_len = SIZ - 1;
161         memcpy(buf, CtdlGetConfigStr("c_nodename"), node_len);
162         buf[node_len] = '!';
163         buf[node_len + 1] = '\0';
164         CM_PrependToField(msg, eMessagePath, buf, node_len + 1);
165 }
166
167
168 void InspectQueuedRoom(SpoolControl **pSC,
169                        RoomProcList *room_to_spool,     
170                        HashList *working_ignetcfg,
171                        HashList *the_netmap
172 ) {
173         SpoolControl *sc;
174         int i = 0;
175
176         syslog(LOG_INFO, "netspool: InspectQueuedRoom(%s)", room_to_spool->name);
177
178         sc = (SpoolControl*)malloc(sizeof(SpoolControl));
179         memset(sc, 0, sizeof(SpoolControl));
180         sc->working_ignetcfg = working_ignetcfg;
181         sc->the_netmap = the_netmap;
182
183         /*
184          * If the room doesn't exist, don't try to perform its networking tasks.
185          * Normally this should never happen, but once in a while maybe a room gets
186          * queued for networking and then deleted before it can happen.
187          */
188         if (CtdlGetRoom(&sc->room, room_to_spool->name) != 0) {
189                 syslog(LOG_INFO, "netspool: ERROR; cannot load <%s>", room_to_spool->name);
190                 free(sc);
191                 return;
192         }
193
194         assert(sc->RNCfg == NULL);      // checking to make sure we cleared it from last time
195
196         sc->RNCfg = CtdlGetNetCfgForRoom(sc->room.QRnumber);
197
198         syslog(LOG_DEBUG, "netspool: room <%s> highest=%ld lastsent=%ld", room_to_spool->name, sc->room.QRhighest, sc->RNCfg->lastsent);
199         if ( (!HaveSpoolConfig(sc->RNCfg)) || (sc->room.QRhighest <= sc->RNCfg->lastsent) ) 
200         {
201                 // There is nothing to send from this room.
202                 syslog(LOG_DEBUG, "netspool: nothing to do for <%s>", room_to_spool->name);
203                 FreeRoomNetworkStruct(&sc->RNCfg);
204                 sc->RNCfg = NULL;
205                 free(sc);
206                 return;
207         }
208
209         sc->lastsent = sc->RNCfg->lastsent;
210         room_to_spool->lastsent = sc->lastsent;
211
212         /* Now lets remember whats needed for the actual work... */
213
214         for (i=0; i < 4; i++)
215         {
216                 aggregate_recipients(&sc->Users[SpoolCfgs[i]], SpoolCfgs[i], sc->RNCfg, SpoolCfgsCopyN[i]);
217         }
218         
219         if (StrLength(sc->RNCfg->Sender) > 0) {
220                 sc->Users[roommailalias] = NewStrBufDup(sc->RNCfg->Sender);
221         }
222
223         sc->next = *pSC;
224         *pSC = sc;
225
226         FreeRoomNetworkStruct(&sc->RNCfg);      // done with this for now, we'll grab it again next time
227         sc->RNCfg = NULL;
228 }
229
230
231 void CalcListID(SpoolControl *sc)
232 {
233         StrBuf *RoomName;
234         struct CitContext *CCC = CC;
235 #define MAX_LISTIDLENGTH 150
236
237         // Load the room banner as the list description
238         struct CtdlMessage *msg = CtdlFetchMessage(sc->room.msgnum_info, 1, 1);
239         if (msg != NULL) {
240                 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
241                 CtdlOutputPreLoadedMsg(msg, MT_CITADEL, HEADERS_NONE, 0, 0, 0);
242                 CM_Free(msg);
243                 sc->RoomInfo = CC->redirect_buffer;
244                 CC->redirect_buffer = NULL;
245         }
246         else {
247                 sc->RoomInfo = NewStrBufPlain(NULL, SIZ);
248         }
249
250         // Calculate the List ID
251         sc->ListID = NewStrBufPlain(NULL, 1024);
252         if (StrLength(sc->RoomInfo) > 0)
253         {
254                 const char *Pos = NULL;
255                 StrBufSipLine(sc->ListID, sc->RoomInfo, &Pos);
256
257                 if (StrLength(sc->ListID) > MAX_LISTIDLENGTH)
258                 {
259                         StrBufCutAt(sc->ListID, MAX_LISTIDLENGTH, NULL);
260                         StrBufAppendBufPlain(sc->ListID, HKEY("..."), 0);
261                 }
262                 StrBufAsciify(sc->ListID, ' ');
263         }
264         else
265         {
266                 StrBufAppendBufPlain(sc->ListID, CCC->room.QRname, -1, 0);
267         }
268
269         StrBufAppendBufPlain(sc->ListID, HKEY("<"), 0);
270         RoomName = NewStrBufPlain (sc->room.QRname, -1);
271         StrBufAsciify(RoomName, '_');
272         StrBufReplaceChars(RoomName, ' ', '_');
273
274         if (StrLength(sc->Users[roommailalias]) > 0)
275         {
276                 long Pos;
277                 const char *AtPos;
278
279                 Pos = StrLength(sc->ListID);
280                 StrBufAppendBuf(sc->ListID, sc->Users[roommailalias], 0);
281                 AtPos = strchr(ChrPtr(sc->ListID) + Pos, '@');
282
283                 if (AtPos != NULL)
284                 {
285                         StrBufPeek(sc->ListID, AtPos, 0, '.');
286                 }
287         }
288         else
289         {
290                 StrBufAppendBufPlain(sc->ListID, HKEY("room_"), 0);
291                 StrBufAppendBuf(sc->ListID, RoomName, 0);
292                 StrBufAppendBufPlain(sc->ListID, HKEY("."), 0);
293                 StrBufAppendBufPlain(sc->ListID, CtdlGetConfigStr("c_fqdn"), -1, 0);
294                 /*
295                  * this used to be:
296                  * roomname <Room-Number.list-id.fqdn>
297                  * according to rfc2919.txt it only has to be a unique identifier
298                  * under the domain of the system; 
299                  * in general MUAs use it to calculate the reply address nowadays.
300                  */
301         }
302         StrBufAppendBufPlain(sc->ListID, HKEY(">"), 0);
303
304         if (StrLength(sc->Users[roommailalias]) == 0)
305         {
306                 sc->Users[roommailalias] = NewStrBuf();
307                 
308                 StrBufAppendBufPlain(sc->Users[roommailalias], HKEY("room_"), 0);
309                 StrBufAppendBuf(sc->Users[roommailalias], RoomName, 0);
310                 StrBufAppendBufPlain(sc->Users[roommailalias], HKEY("@"), 0);
311                 StrBufAppendBufPlain(sc->Users[roommailalias], CtdlGetConfigStr("c_fqdn"), -1, 0);
312
313                 StrBufLowerCase(sc->Users[roommailalias]);
314         }
315
316         FreeStrBuf(&RoomName);
317 }
318
319
320 /*
321  * Batch up and send all outbound traffic from the current room (this is definitely used for mailing lists)
322  */
323 void network_spoolout_room(SpoolControl *sc)
324 {
325         struct CitContext *CCC = CC;
326         char buf[SIZ];
327         int i;
328         long lastsent;
329
330         /*
331          * If the room doesn't exist, don't try to perform its networking tasks.
332          * Normally this should never happen, but once in a while maybe a room gets
333          * queued for networking and then deleted before it can happen.
334          */
335         memcpy (&CCC->room, &sc->room, sizeof(ctdlroom));
336
337         syslog(LOG_INFO, "netspool: network_spoolout_room(room=%s, lastsent=%ld)", CCC->room.QRname, sc->lastsent);
338
339         CalcListID(sc);
340
341         /* remember where we started... */
342         lastsent = sc->lastsent;
343
344         /* Fetch the messages we ought to send & prepare them. */
345         CtdlForEachMessage(MSGS_GT, sc->lastsent, NULL, NULL, NULL, network_spool_msg, sc);
346
347         if (StrLength(sc->Users[roommailalias]) > 0)
348         {
349                 long len;
350                 len = StrLength(sc->Users[roommailalias]);
351                 if (len + 1 > sizeof(buf))
352                         len = sizeof(buf) - 1;
353                 memcpy(buf, ChrPtr(sc->Users[roommailalias]), len);
354                 buf[len] = '\0';
355         }
356         else
357         {
358                 snprintf(buf, sizeof buf, "room_%s@%s", CCC->room.QRname, CtdlGetConfigStr("c_fqdn"));
359         }
360
361         for (i=0; buf[i]; ++i) {
362                 buf[i] = tolower(buf[i]);
363                 if (isspace(buf[i])) buf[i] = '_';
364         }
365
366         /* If we wrote a digest, deliver it and then close it */
367         if ( (sc->Users[digestrecp] != NULL) && (sc->digestfp != NULL) )
368         {
369                 fprintf(sc->digestfp,
370                         " ------------------------------------------------------------------------------\n"
371                         "You are subscribed to the '%s' list.\n"
372                         "To post to the list: %s\n",
373                         CCC->room.QRname, buf
374                 );
375                 network_deliver_digest(sc);     /* deliver */
376                 fclose(sc->digestfp);
377                 sc->digestfp = NULL;
378                 remove_digest_file(&sc->room);
379         }
380
381         /* Now rewrite the netconfig */
382         syslog(LOG_DEBUG, "netspool: lastsent was %ld , now it is %ld", lastsent, sc->lastsent);
383         if (sc->lastsent != lastsent)
384         {
385                 OneRoomNetCfg *r;
386
387                 begin_critical_section(S_NETCONFIGS);
388                 r = CtdlGetNetCfgForRoom(sc->room.QRnumber);
389                 r->lastsent = sc->lastsent;
390                 SaveRoomNetConfigFile(r, sc->room.QRnumber);
391                 FreeRoomNetworkStruct(&r);
392                 end_critical_section(S_NETCONFIGS);
393         }
394 }
395
396
397 void free_spoolcontrol_struct(SpoolControl **sc)
398 {
399         free_spoolcontrol_struct_members(*sc);
400         free(*sc);
401         *sc = NULL;
402 }
403
404
405 void free_spoolcontrol_struct_members(SpoolControl *sc)
406 {
407         int i;
408         FreeStrBuf(&sc->RoomInfo);
409         FreeStrBuf(&sc->ListID);
410         for (i = 0; i < maxRoomNetCfg; i++)
411                 FreeStrBuf(&sc->Users[i]);
412 }
413
414
415 /*
416  * It's ok if these directories already exist.  Just fail silently.
417  */
418 void create_spool_dirs(void) {
419         if ((mkdir(ctdl_spool_dir, 0700) != 0) && (errno != EEXIST))
420                 syslog(LOG_EMERG, "netspool: unable to create directory [%s]: %s", ctdl_spool_dir, strerror(errno));
421         if (chown(ctdl_spool_dir, CTDLUID, (-1)) != 0)
422                 syslog(LOG_EMERG, "netspool: unable to set the access rights for [%s]: %s", ctdl_spool_dir, strerror(errno));
423         if ((mkdir(ctdl_nettmp_dir, 0700) != 0) && (errno != EEXIST))
424                 syslog(LOG_EMERG, "netspool: unable to create directory [%s]: %s", ctdl_nettmp_dir, strerror(errno));
425         if (chown(ctdl_nettmp_dir, CTDLUID, (-1)) != 0)
426                 syslog(LOG_EMERG, "netspool: unable to set the access rights for [%s]: %s", ctdl_nettmp_dir, strerror(errno));
427 }
428
429
430 /*
431  * Module entry point
432  */
433 CTDL_MODULE_INIT(network_spool)
434 {
435         if (!threading)
436         {
437                 CtdlREGISTERRoomCfgType(subpending,       ParseSubPendingLine,   0, 5, SerializeGeneric,  DeleteGenericCfgLine);
438                 CtdlREGISTERRoomCfgType(unsubpending,     ParseUnSubPendingLine, 0, 4, SerializeGeneric,  DeleteGenericCfgLine);
439                 CtdlREGISTERRoomCfgType(lastsent,         ParseLastSent,         1, 1, SerializeLastSent, DeleteLastSent);
440                 CtdlREGISTERRoomCfgType(listrecp,         ParseGeneric,          0, 1, SerializeGeneric,  DeleteGenericCfgLine);
441                 CtdlREGISTERRoomCfgType(digestrecp,       ParseGeneric,          0, 1, SerializeGeneric,  DeleteGenericCfgLine);
442                 CtdlREGISTERRoomCfgType(participate,      ParseGeneric,          0, 1, SerializeGeneric,  DeleteGenericCfgLine);
443                 CtdlREGISTERRoomCfgType(roommailalias,    ParseRoomAlias,        0, 1, SerializeGeneric,  DeleteGenericCfgLine);
444                 create_spool_dirs();
445         }
446         return "network_spool";
447 }