2 * This module handles shared rooms, inter-Citadel mail, and outbound
3 * mailing list processing.
5 * Copyright (c) 2000-2018 by the citadel.org team
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.
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.
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.
25 * Duration of time (in seconds) after which pending list subscribe/unsubscribe
26 * requests that have not been confirmed will be deleted.
28 #define EXP 259200 /* three days */
40 #include <sys/types.h>
42 #if TIME_WITH_SYS_TIME
43 # include <sys/time.h>
47 # include <sys/time.h>
55 # if HAVE_SYS_SYSCALL_H
56 # include <sys/syscall.h>
64 #include <libcitadel.h>
67 #include "citserver.h"
73 #include "internet_addressing.h"
74 #include "serv_network.h"
75 #include "clientsocket.h"
76 #include "citadel_dirs.h"
79 #include "ctdl_module.h"
84 void ParseLastSent(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, OneRoomNetCfg *OneRNCFG)
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;
93 void ParseRoomAlias(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, OneRoomNetCfg *rncfg)
95 if (rncfg->Sender != NULL) {
99 ParseGeneric(ThisOne, Line, LinePos, rncfg);
100 rncfg->Sender = NewStrBufDup(rncfg->NetConfigs[roommailalias]->Value[0]);
103 void ParseSubPendingLine(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, OneRoomNetCfg *OneRNCFG)
105 if (time(NULL) - extract_long(LinePos, 3) > EXP)
106 return; /* expired subscription... */
108 ParseGeneric(ThisOne, Line, LinePos, OneRNCFG);
110 void ParseUnSubPendingLine(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, OneRoomNetCfg *OneRNCFG)
112 if (time(NULL) - extract_long(LinePos, 2) > EXP)
113 return; /* expired subscription... */
115 ParseGeneric(ThisOne, Line, LinePos, OneRNCFG);
119 void SerializeLastSent(const CfgLineType *ThisOne, StrBuf *OutputBuffer, OneRoomNetCfg *RNCfg, RoomNetCfgLine *data)
121 StrBufAppendBufPlain(OutputBuffer, CKEY(ThisOne->Str), 0);
122 StrBufAppendPrintf(OutputBuffer, "|%ld\n", RNCfg->lastsent);
125 void DeleteLastSent(const CfgLineType *ThisOne, RoomNetCfgLine **data)
131 static const RoomNetCfg SpoolCfgs [4] = {
137 static const long SpoolCfgsCopyN [4] = {
141 int HaveSpoolConfig(OneRoomNetCfg* RNCfg)
145 for (i=0; i < 4; i++) if (RNCfg->NetConfigs[SpoolCfgs[i]] == NULL) interested = 1;
150 void InspectQueuedRoom(SpoolControl **pSC,
151 RoomProcList *room_to_spool,
152 HashList *working_ignetcfg,
158 syslog(LOG_INFO, "netspool: InspectQueuedRoom(%s)", room_to_spool->name);
160 sc = (SpoolControl*)malloc(sizeof(SpoolControl));
161 memset(sc, 0, sizeof(SpoolControl));
162 sc->working_ignetcfg = working_ignetcfg;
163 sc->the_netmap = the_netmap;
166 * If the room doesn't exist, don't try to perform its networking tasks.
167 * Normally this should never happen, but once in a while maybe a room gets
168 * queued for networking and then deleted before it can happen.
170 if (CtdlGetRoom(&sc->room, room_to_spool->name) != 0) {
171 syslog(LOG_INFO, "netspool: ERROR; cannot load <%s>", room_to_spool->name);
176 assert(sc->RNCfg == NULL); // checking to make sure we cleared it from last time
178 sc->RNCfg = CtdlGetNetCfgForRoom(sc->room.QRnumber);
180 syslog(LOG_DEBUG, "netspool: room <%s> highest=%ld lastsent=%ld", room_to_spool->name, sc->room.QRhighest, sc->RNCfg->lastsent);
181 if ( (!HaveSpoolConfig(sc->RNCfg)) || (sc->room.QRhighest <= sc->RNCfg->lastsent) )
183 // There is nothing to send from this room.
184 syslog(LOG_DEBUG, "netspool: nothing to do for <%s>", room_to_spool->name);
185 FreeRoomNetworkStruct(&sc->RNCfg);
191 sc->lastsent = sc->RNCfg->lastsent;
192 room_to_spool->lastsent = sc->lastsent;
194 /* Now lets remember whats needed for the actual work... */
196 for (i=0; i < 4; i++)
198 aggregate_recipients(&sc->Users[SpoolCfgs[i]], SpoolCfgs[i], sc->RNCfg, SpoolCfgsCopyN[i]);
201 if (StrLength(sc->RNCfg->Sender) > 0) {
202 sc->Users[roommailalias] = NewStrBufDup(sc->RNCfg->Sender);
208 FreeRoomNetworkStruct(&sc->RNCfg); // done with this for now, we'll grab it again next time
213 void CalcListID(SpoolControl *sc)
216 struct CitContext *CCC = CC;
217 #define MAX_LISTIDLENGTH 150
219 // Load the room banner as the list description
220 struct CtdlMessage *msg = CtdlFetchMessage(sc->room.msgnum_info, 1, 1);
222 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
223 CtdlOutputPreLoadedMsg(msg, MT_CITADEL, HEADERS_NONE, 0, 0, 0);
225 sc->RoomInfo = CC->redirect_buffer;
226 CC->redirect_buffer = NULL;
229 sc->RoomInfo = NewStrBufPlain(NULL, SIZ);
232 // Calculate the List ID
233 sc->ListID = NewStrBufPlain(NULL, 1024);
234 if (StrLength(sc->RoomInfo) > 0)
236 const char *Pos = NULL;
237 StrBufSipLine(sc->ListID, sc->RoomInfo, &Pos);
239 if (StrLength(sc->ListID) > MAX_LISTIDLENGTH)
241 StrBufCutAt(sc->ListID, MAX_LISTIDLENGTH, NULL);
242 StrBufAppendBufPlain(sc->ListID, HKEY("..."), 0);
244 StrBufAsciify(sc->ListID, ' ');
248 StrBufAppendBufPlain(sc->ListID, CCC->room.QRname, -1, 0);
251 StrBufAppendBufPlain(sc->ListID, HKEY("<"), 0);
252 RoomName = NewStrBufPlain (sc->room.QRname, -1);
253 StrBufAsciify(RoomName, '_');
254 StrBufReplaceChars(RoomName, ' ', '_');
256 if (StrLength(sc->Users[roommailalias]) > 0)
261 Pos = StrLength(sc->ListID);
262 StrBufAppendBuf(sc->ListID, sc->Users[roommailalias], 0);
263 AtPos = strchr(ChrPtr(sc->ListID) + Pos, '@');
267 StrBufPeek(sc->ListID, AtPos, 0, '.');
272 StrBufAppendBufPlain(sc->ListID, HKEY("room_"), 0);
273 StrBufAppendBuf(sc->ListID, RoomName, 0);
274 StrBufAppendBufPlain(sc->ListID, HKEY("."), 0);
275 StrBufAppendBufPlain(sc->ListID, CtdlGetConfigStr("c_fqdn"), -1, 0);
278 * roomname <Room-Number.list-id.fqdn>
279 * according to rfc2919.txt it only has to be a unique identifier
280 * under the domain of the system;
281 * in general MUAs use it to calculate the reply address nowadays.
284 StrBufAppendBufPlain(sc->ListID, HKEY(">"), 0);
286 if (StrLength(sc->Users[roommailalias]) == 0)
288 sc->Users[roommailalias] = NewStrBuf();
290 StrBufAppendBufPlain(sc->Users[roommailalias], HKEY("room_"), 0);
291 StrBufAppendBuf(sc->Users[roommailalias], RoomName, 0);
292 StrBufAppendBufPlain(sc->Users[roommailalias], HKEY("@"), 0);
293 StrBufAppendBufPlain(sc->Users[roommailalias], CtdlGetConfigStr("c_fqdn"), -1, 0);
295 StrBufLowerCase(sc->Users[roommailalias]);
298 FreeStrBuf(&RoomName);
303 * Batch up and send all outbound traffic from the current room (this is definitely used for mailing lists)
305 void network_spoolout_room(SpoolControl *sc)
307 struct CitContext *CCC = CC;
313 * If the room doesn't exist, don't try to perform its networking tasks.
314 * Normally this should never happen, but once in a while maybe a room gets
315 * queued for networking and then deleted before it can happen.
317 memcpy (&CCC->room, &sc->room, sizeof(ctdlroom));
319 syslog(LOG_INFO, "netspool: network_spoolout_room(room=%s, lastsent=%ld)", CCC->room.QRname, sc->lastsent);
323 /* remember where we started... */
324 lastsent = sc->lastsent;
326 /* Fetch the messages we ought to send & prepare them. */
327 CtdlForEachMessage(MSGS_GT, sc->lastsent, NULL, NULL, NULL, network_spool_msg, sc);
329 if (StrLength(sc->Users[roommailalias]) > 0)
332 len = StrLength(sc->Users[roommailalias]);
333 if (len + 1 > sizeof(buf))
334 len = sizeof(buf) - 1;
335 memcpy(buf, ChrPtr(sc->Users[roommailalias]), len);
340 snprintf(buf, sizeof buf, "room_%s@%s", CCC->room.QRname, CtdlGetConfigStr("c_fqdn"));
343 for (i=0; buf[i]; ++i) {
344 buf[i] = tolower(buf[i]);
345 if (isspace(buf[i])) buf[i] = '_';
348 /* If we wrote a digest, deliver it and then close it */
349 if ( (sc->Users[digestrecp] != NULL) && (sc->digestfp != NULL) )
351 fprintf(sc->digestfp,
352 " ------------------------------------------------------------------------------\n"
353 "You are subscribed to the '%s' list.\n"
354 "To post to the list: %s\n",
355 CCC->room.QRname, buf
357 network_deliver_digest(sc); /* deliver */
358 fclose(sc->digestfp);
360 remove_digest_file(&sc->room);
363 /* Now rewrite the netconfig */
364 syslog(LOG_DEBUG, "netspool: lastsent was %ld , now it is %ld", lastsent, sc->lastsent);
365 if (sc->lastsent != lastsent)
369 begin_critical_section(S_NETCONFIGS);
370 r = CtdlGetNetCfgForRoom(sc->room.QRnumber);
371 r->lastsent = sc->lastsent;
372 SaveRoomNetConfigFile(r, sc->room.QRnumber);
373 FreeRoomNetworkStruct(&r);
374 end_critical_section(S_NETCONFIGS);
379 void free_spoolcontrol_struct(SpoolControl **sc)
381 free_spoolcontrol_struct_members(*sc);
387 void free_spoolcontrol_struct_members(SpoolControl *sc)
390 FreeStrBuf(&sc->RoomInfo);
391 FreeStrBuf(&sc->ListID);
392 for (i = 0; i < maxRoomNetCfg; i++)
393 FreeStrBuf(&sc->Users[i]);
398 * It's ok if these directories already exist. Just fail silently.
400 void create_spool_dirs(void) {
401 if ((mkdir(ctdl_spool_dir, 0700) != 0) && (errno != EEXIST))
402 syslog(LOG_EMERG, "netspool: unable to create directory [%s]: %s", ctdl_spool_dir, strerror(errno));
403 if (chown(ctdl_spool_dir, CTDLUID, (-1)) != 0)
404 syslog(LOG_EMERG, "netspool: unable to set the access rights for [%s]: %s", ctdl_spool_dir, strerror(errno));
405 if ((mkdir(ctdl_nettmp_dir, 0700) != 0) && (errno != EEXIST))
406 syslog(LOG_EMERG, "netspool: unable to create directory [%s]: %s", ctdl_nettmp_dir, strerror(errno));
407 if (chown(ctdl_nettmp_dir, CTDLUID, (-1)) != 0)
408 syslog(LOG_EMERG, "netspool: unable to set the access rights for [%s]: %s", ctdl_nettmp_dir, strerror(errno));
415 CTDL_MODULE_INIT(network_spool)
419 CtdlREGISTERRoomCfgType(subpending, ParseSubPendingLine, 0, 5, SerializeGeneric, DeleteGenericCfgLine);
420 CtdlREGISTERRoomCfgType(unsubpending, ParseUnSubPendingLine, 0, 4, SerializeGeneric, DeleteGenericCfgLine);
421 CtdlREGISTERRoomCfgType(lastsent, ParseLastSent, 1, 1, SerializeLastSent, DeleteLastSent);
422 CtdlREGISTERRoomCfgType(listrecp, ParseGeneric, 0, 1, SerializeGeneric, DeleteGenericCfgLine);
423 CtdlREGISTERRoomCfgType(digestrecp, ParseGeneric, 0, 1, SerializeGeneric, DeleteGenericCfgLine);
424 CtdlREGISTERRoomCfgType(participate, ParseGeneric, 0, 1, SerializeGeneric, DeleteGenericCfgLine);
425 CtdlREGISTERRoomCfgType(roommailalias, ParseRoomAlias, 0, 1, SerializeGeneric, DeleteGenericCfgLine);
428 return "network_spool";