2 * This module handles shared rooms, inter-Citadel mail, and outbound
3 * mailing list processing.
5 * Copyright (c) 2000-2017 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.
24 * Duration of time (in seconds) after which pending list subscribe/unsubscribe
25 * requests that have not been confirmed will be deleted.
27 #define EXP 259200 /* three days */
39 #include <sys/types.h>
41 #if TIME_WITH_SYS_TIME
42 # include <sys/time.h>
46 # include <sys/time.h>
54 # if HAVE_SYS_SYSCALL_H
55 # include <sys/syscall.h>
62 #include <libcitadel.h>
65 #include "citserver.h"
71 #include "internet_addressing.h"
72 #include "serv_network.h"
73 #include "clientsocket.h"
74 #include "citadel_dirs.h"
77 #include "ctdl_module.h"
81 struct CitContext networker_spool_CC;
83 /* comes from lookup3.c from libcitadel... */
84 extern uint32_t hashlittle( const void *key, size_t length, uint32_t initval);
86 typedef struct __roomlists {
92 * When we do network processing, it's accomplished in two passes; one to
93 * gather a list of rooms and one to actually do them. It's ok that rplist
94 * is global; we have a mutex that keeps it safe.
96 struct RoomProcList *rplist = NULL;
100 * Send the *entire* contents of the current room to one specific network node,
101 * ignoring anything we know about which messages have already undergone
102 * network processing. This can be used to bring a new node into sync.
104 int network_sync_to(char *target_node, long len)
106 struct CitContext *CCC = CC;
107 OneRoomNetCfg OneRNCFG;
108 OneRoomNetCfg *pRNCFG;
109 const RoomNetCfgLine *pCfgLine;
113 /* Load the netconfig for this room */
114 pRNCFG = CtdlGetNetCfgForRoom(CCC->room.QRnumber);
115 if (pRNCFG == NULL) { // no netconfig at all?
118 if (pRNCFG->NetConfigs[ignet_push_share] == NULL) // no ignet push shares?
120 FreeRoomNetworkStruct(&pRNCFG);
124 /* Search for an ignet_oush_share configuration bearing the target node's name */
125 for (pCfgLine = pRNCFG->NetConfigs[ignet_push_share]; pCfgLine != NULL; pCfgLine = pCfgLine->next)
127 if (!strcmp(ChrPtr(pCfgLine->Value[0]), target_node))
131 /* If we aren't sharing with that node, bail out */
132 if (pCfgLine == NULL)
134 FreeRoomNetworkStruct(&pRNCFG);
138 /* If we got here, we're good to go ... make up a dummy spoolconfig and roll with it */
140 begin_critical_section(S_NETCONFIGS);
141 memset(&sc, 0, sizeof(SpoolControl));
142 memset(&OneRNCFG, 0, sizeof(OneRoomNetCfg));
143 sc.RNCfg = &OneRNCFG;
144 sc.RNCfg->NetConfigs[ignet_push_share] = DuplicateOneGenericCfgLine(pCfgLine);
145 sc.Users[ignet_push_share] = NewStrBufPlain(NULL, (StrLength(pCfgLine->Value[0]) + StrLength(pCfgLine->Value[1]) + 10) );
146 StrBufAppendBuf(sc.Users[ignet_push_share], pCfgLine->Value[0], 0);
147 StrBufAppendBufPlain(sc.Users[ignet_push_share], HKEY(","), 0);
148 StrBufAppendBuf(sc.Users[ignet_push_share], pCfgLine->Value[1], 0);
150 end_critical_section(S_NETCONFIGS);
152 sc.working_ignetcfg = CtdlLoadIgNetCfg();
153 sc.the_netmap = CtdlReadNetworkMap();
155 /* Send ALL messages */
156 num_spooled = CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, network_spool_msg, &sc);
158 /* Concise cleanup because we know there's only one node in the sc */
159 DeleteGenericCfgLine(NULL, &sc.RNCfg->NetConfigs[ignet_push_share]);
161 DeleteHash(&sc.working_ignetcfg);
162 DeleteHash(&sc.the_netmap);
163 free_spoolcontrol_struct_members(&sc);
165 syslog(LOG_NOTICE, "network: synchronized %d messages to <%s>", num_spooled, target_node);
171 * Implements the NSYN command
173 void cmd_nsyn(char *argbuf) {
176 char target_node[256];
178 if (CtdlAccessCheck(ac_aide)) return;
180 len = extract_token(target_node, argbuf, 0, '|', sizeof target_node);
181 num_spooled = network_sync_to(target_node, len);
182 if (num_spooled >= 0) {
183 cprintf("%d Spooled %d messages.\n", CIT_OK, num_spooled);
186 cprintf("%d No such room/node share exists.\n", ERROR + ROOM_NOT_FOUND);
190 RoomProcList *CreateRoomProcListEntry(struct ctdlroom *qrbuf, OneRoomNetCfg *OneRNCFG)
193 struct RoomProcList *ptr;
195 ptr = (struct RoomProcList *) malloc(sizeof (struct RoomProcList));
200 ptr->namelen = strlen(qrbuf->QRname);
201 if (ptr->namelen > ROOMNAMELEN) {
202 ptr->namelen = ROOMNAMELEN - 1;
205 memcpy (ptr->name, qrbuf->QRname, ptr->namelen);
206 ptr->name[ptr->namelen] = '\0';
207 ptr->QRNum = qrbuf->QRnumber;
209 for (i = 0; i < ptr->namelen; i++)
211 ptr->lcname[i] = tolower(ptr->name[i]);
214 ptr->lcname[ptr->namelen] = '\0';
215 ptr->key = hashlittle(ptr->lcname, ptr->namelen, 9872345);
220 * Batch up and send all outbound traffic from the current room
222 void network_queue_interesting_rooms(struct ctdlroom *qrbuf, void *data, OneRoomNetCfg *OneRNCfg)
224 struct RoomProcList *ptr;
225 roomlists *RP = (roomlists*) data;
227 if (!HaveSpoolConfig(OneRNCfg)) {
231 ptr = CreateRoomProcListEntry(qrbuf, OneRNCfg);
235 ptr->next = RP->rplist;
241 * Batch up and send all outbound traffic from the current room
243 int network_room_handler(struct ctdlroom *qrbuf)
245 struct RoomProcList *ptr;
246 OneRoomNetCfg *RNCfg;
248 if (qrbuf->QRdefaultview == VIEW_QUEUE) {
252 RNCfg = CtdlGetNetCfgForRoom(qrbuf->QRnumber);
257 if (!HaveSpoolConfig(RNCfg)) {
258 FreeRoomNetworkStruct(&RNCfg);
262 ptr = CreateRoomProcListEntry(qrbuf, RNCfg);
264 FreeRoomNetworkStruct(&RNCfg);
268 begin_critical_section(S_RPLIST);
271 end_critical_section(S_RPLIST);
272 FreeRoomNetworkStruct(&RNCfg);
276 void destroy_network_queue_room(RoomProcList *rplist)
278 struct RoomProcList *cur, *p;
289 void destroy_network_queue_room_locked (void)
291 begin_critical_section(S_RPLIST);
292 destroy_network_queue_room(rplist);
293 end_critical_section(S_RPLIST);
300 * Run through the rooms doing various types of network stuff.
302 void network_do_queue(void)
304 static time_t last_run = 0L;
305 int full_processing = 1;
306 HashList *working_ignetcfg;
307 HashList *the_netmap = NULL;
308 int netmap_changed = 0;
310 SpoolControl *sc = NULL;
314 * Run the full set of processing tasks no more frequently
315 * than once every n seconds
317 if ( (time(NULL) - last_run) < CtdlGetConfigLong("c_net_freq") )
320 syslog(LOG_DEBUG, "network: full processing in %ld seconds.", CtdlGetConfigLong("c_net_freq") - (time(NULL)- last_run));
323 become_session(&networker_spool_CC);
324 begin_critical_section(S_RPLIST);
327 end_critical_section(S_RPLIST);
329 // TODO hm, check whether we have a config at all here?
330 /* Load the IGnet Configuration into memory */
331 working_ignetcfg = CtdlLoadIgNetCfg();
334 * Load the network map and filter list into memory.
336 if (!server_shutting_down) {
337 the_netmap = CtdlReadNetworkMap();
341 * Go ahead and run the queue
343 if (full_processing && !server_shutting_down) {
344 syslog(LOG_DEBUG, "network: loading outbound queue");
345 CtdlForEachNetCfgRoom(network_queue_interesting_rooms, &RL);
348 if ((RL.rplist != NULL) && (!server_shutting_down)) {
349 RoomProcList *ptr, *cmp;
351 syslog(LOG_DEBUG, "network: running outbound queue");
352 while (ptr != NULL && !server_shutting_down) {
355 /* filter duplicates from the list... */
356 while (cmp != NULL) {
357 if ((cmp->namelen > 0) &&
358 (cmp->key == ptr->key) &&
359 (cmp->namelen == ptr->namelen) &&
360 (strcmp(cmp->lcname, ptr->lcname) == 0))
367 if (ptr->namelen > 0) {
368 InspectQueuedRoom(&sc, ptr, working_ignetcfg, the_netmap);
378 network_spoolout_room(pSC);
386 free_spoolcontrol_struct(&pSC);
389 /* If there is anything in the inbound queue, process it */
390 if (!server_shutting_down) {
391 network_do_spoolin(working_ignetcfg, the_netmap, &netmap_changed);
394 /* Save the network map back to disk */
395 if (netmap_changed) {
396 StrBuf *MapStr = CtdlSerializeNetworkMap(the_netmap);
397 char *pMapStr = SmashStrBuf(&MapStr);
398 CtdlPutSysConfig(IGNETMAP, pMapStr);
402 /* combine single message files into one spool entry per remote node. */
403 network_consolidate_spoolout(working_ignetcfg, the_netmap);
407 DeleteHash(&the_netmap);
409 DeleteHash(&working_ignetcfg);
411 syslog(LOG_DEBUG, "network: queue run completed");
413 if (full_processing) {
414 last_run = time(NULL);
416 destroy_network_queue_room(RL.rplist);
420 void network_logout_hook(void)
422 CitContext *CCC = MyContext();
425 * If we were talking to a network node, we're not anymore...
427 if (!IsEmptyStr(CCC->net_node)) {
428 CtdlNetworkTalkingTo(CCC->net_node, strlen(CCC->net_node), NTT_REMOVE);
429 CCC->net_node[0] = '\0';
434 void network_cleanup_function(void)
436 struct CitContext *CCC = CC;
438 if (!IsEmptyStr(CCC->net_node)) {
439 CtdlNetworkTalkingTo(CCC->net_node, strlen(CCC->net_node), NTT_REMOVE);
440 CCC->net_node[0] = '\0';
445 int ignet_aftersave(struct CtdlMessage *msg, recptypes *recps)
447 /* For IGnet mail, we have to save a new copy into the spooler for
448 * each recipient, with the R and D fields set to the recipient and
449 * destination-node. This has two ugly side effects: all other
450 * recipients end up being unlisted in this recipient's copy of the
451 * message, and it has to deliver multiple messages to the same
452 * node. We'll revisit this again in a year or so when everyone has
453 * a network spool receiver that can handle the new style messages.
455 if ((recps != NULL) && (recps->num_ignet > 0))
460 FILE *network_fp = NULL;
461 char submit_filename[128];
462 static int seqnum = 1;
464 char *hold_R, *hold_D, *RBuf, *DBuf;
465 long hrlen, hdlen, rblen, dblen, count, rlen;
466 CitContext *CCC = MyContext();
468 CM_GetAsField(msg, eRecipient, &hold_R, &hrlen);;
469 CM_GetAsField(msg, eDestination, &hold_D, &hdlen);;
471 count = num_tokens(recps->recp_ignet, '|');
472 rlen = strlen(recps->recp_ignet);
473 recipient = malloc(rlen + 1);
474 RBuf = malloc(rlen + 1);
475 DBuf = malloc(rlen + 1);
476 for (i=0; i<count; ++i) {
477 extract_token(recipient, recps->recp_ignet, i, '|', rlen + 1);
479 rblen = extract_token(RBuf, recipient, 0, '@', rlen + 1);
480 dblen = extract_token(DBuf, recipient, 1, '@', rlen + 1);
482 CM_SetAsField(msg, eRecipient, &RBuf, rblen);;
483 CM_SetAsField(msg, eDestination, &DBuf, dblen);;
484 CtdlSerializeMessage(&smr, msg);
486 snprintf(submit_filename, sizeof submit_filename,
487 "%s/netmail.%04lx.%04x.%04x",
493 network_fp = fopen(submit_filename, "wb+");
494 if (network_fp != NULL) {
495 rv = fwrite(smr.ser, smr.len, 1, network_fp);
497 syslog(LOG_EMERG, "network: CtdlSubmitMsg() Couldn't write network spool file: %s", strerror(errno));
503 CM_GetAsField(msg, eRecipient, &RBuf, &rblen);;
504 CM_GetAsField(msg, eDestination, &DBuf, &dblen);;
509 CM_SetAsField(msg, eRecipient, &hold_R, hrlen);
510 CM_SetAsField(msg, eDestination, &hold_D, hdlen);
522 CTDL_MODULE_INIT(network)
526 CtdlRegisterMessageHook(ignet_aftersave, EVT_AFTERSAVE);
527 CtdlFillSystemContext(&networker_spool_CC, "CitNetSpool");
528 CtdlRegisterSessionHook(network_cleanup_function, EVT_STOP, PRIO_STOP + 30);
529 CtdlRegisterSessionHook(network_logout_hook, EVT_LOGOUT, PRIO_LOGOUT + 10);
530 CtdlRegisterProtoHook(cmd_nsyn, "NSYN", "Synchronize room to node");
531 CtdlRegisterRoomHook(network_room_handler);
532 CtdlRegisterCleanupHook(destroy_network_queue_room_locked);
533 CtdlRegisterSessionHook(network_do_queue, EVT_TIMER, PRIO_QUEUE + 10);