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