2 * This module handles shared rooms, inter-Citadel mail, and outbound
3 * mailing list processing.
5 * Copyright (c) 2000-2012 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>
63 #include <libcitadel.h>
66 #include "citserver.h"
72 #include "internet_addressing.h"
73 #include "serv_network.h"
74 #include "clientsocket.h"
75 #include "citadel_dirs.h"
78 #include "ctdl_module.h"
82 int NetQDebugEnabled = 0;
83 struct CitContext networker_spool_CC;
85 /* comes from lookup3.c from libcitadel... */
86 extern uint32_t hashlittle( const void *key, size_t length, uint32_t initval);
88 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;
101 * Check the use table. This is a list of messages which have recently
102 * arrived on the system. It is maintained and queried to prevent the same
103 * message from being entered into the database multiple times if it happens
104 * to arrive multiple times by accident.
106 int network_usetable(struct CtdlMessage *msg)
109 struct CitContext *CCC = CC;
112 /* Bail out if we can't generate a message ID */
113 if ((msg == NULL) || CM_IsEmpty(msg, emessageId))
118 /* Generate the message ID */
119 msgid = NewStrBufPlain(msg->cm_fields[emessageId], -1);
120 if (haschar(ChrPtr(msgid), '@') == 0) {
121 StrBufAppendBufPlain(msgid, HKEY("@"), 0);
122 if (!CM_IsEmpty(msg, eNodeName)) {
123 StrBufAppendBufPlain(msgid, msg->cm_fields[eNodeName], -1, 0);
131 if (CheckIfAlreadySeen("Networker Import",
135 CCC->cs_pid, 0) != 0)
148 * Send the *entire* contents of the current room to one specific network node,
149 * ignoring anything we know about which messages have already undergone
150 * network processing. This can be used to bring a new node into sync.
152 int network_sync_to(char *target_node, long len)
154 struct CitContext *CCC = CC;
155 OneRoomNetCfg OneRNCFG;
156 OneRoomNetCfg *pRNCFG;
157 const RoomNetCfgLine *pCfgLine;
161 /* Grab the configuration line we're looking for */
162 begin_critical_section(S_NETCONFIGS);
163 pRNCFG = CtdlGetNetCfgForRoom(CCC->room.QRnumber);
164 if ((pRNCFG == NULL) ||
165 (pRNCFG->NetConfigs[ignet_push_share] == NULL))
170 pCfgLine = pRNCFG->NetConfigs[ignet_push_share];
171 while (pCfgLine != NULL)
173 if (!strcmp(ChrPtr(pCfgLine->Value[0]), target_node))
175 pCfgLine = pCfgLine->next;
177 if (pCfgLine == NULL)
182 memset(&sc, 0, sizeof(SpoolControl));
183 memset(&OneRNCFG, 0, sizeof(OneRoomNetCfg));
184 sc.RNCfg = &OneRNCFG;
185 sc.RNCfg->NetConfigs[ignet_push_share] = DuplicateOneGenericCfgLine(pCfgLine);
186 sc.Users[ignet_push_share] = NewStrBufPlain(NULL,
187 StrLength(pCfgLine->Value[0]) +
188 StrLength(pCfgLine->Value[1]) + 10);
189 StrBufAppendBuf(sc.Users[ignet_push_share],
192 StrBufAppendBufPlain(sc.Users[ignet_push_share],
196 StrBufAppendBuf(sc.Users[ignet_push_share],
201 end_critical_section(S_NETCONFIGS);
203 sc.working_ignetcfg = CtdlLoadIgNetCfg();
204 sc.the_netmap = CtdlReadNetworkMap();
206 /* Send ALL messages */
207 num_spooled = CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL,
208 network_spool_msg, &sc);
210 /* Concise cleanup because we know there's only one node in the sc */
211 DeleteGenericCfgLine(NULL/*TODO*/, &sc.RNCfg->NetConfigs[ignet_push_share]);
213 DeleteHash(&sc.working_ignetcfg);
214 DeleteHash(&sc.the_netmap);
215 free_spoolcontrol_struct_members(&sc);
217 QN_syslog(LOG_NOTICE, "Synchronized %d messages to <%s>\n",
218 num_spooled, target_node);
224 * Implements the NSYN command
226 void cmd_nsyn(char *argbuf) {
229 char target_node[256];
231 if (CtdlAccessCheck(ac_aide)) return;
233 len = extract_token(target_node, argbuf, 0, '|', sizeof target_node);
234 num_spooled = network_sync_to(target_node, len);
235 if (num_spooled >= 0) {
236 cprintf("%d Spooled %d messages.\n", CIT_OK, num_spooled);
239 cprintf("%d No such room/node share exists.\n",
240 ERROR + ROOM_NOT_FOUND);
244 RoomProcList *CreateRoomProcListEntry(struct ctdlroom *qrbuf, OneRoomNetCfg *OneRNCFG)
247 struct RoomProcList *ptr;
249 ptr = (struct RoomProcList *) malloc(sizeof (struct RoomProcList));
250 if (ptr == NULL) return NULL;
252 ptr->namelen = strlen(qrbuf->QRname);
253 if (ptr->namelen > ROOMNAMELEN)
254 ptr->namelen = ROOMNAMELEN - 1;
256 memcpy (ptr->name, qrbuf->QRname, ptr->namelen);
257 ptr->name[ptr->namelen] = '\0';
258 ptr->QRNum = qrbuf->QRnumber;
260 for (i = 0; i < ptr->namelen; i++)
262 ptr->lcname[i] = tolower(ptr->name[i]);
265 ptr->lcname[ptr->namelen] = '\0';
266 ptr->key = hashlittle(ptr->lcname, ptr->namelen, 9872345);
267 ptr->lastsent = OneRNCFG->lastsent;
268 ptr->OneRNCfg = OneRNCFG;
273 * Batch up and send all outbound traffic from the current room
275 void network_queue_interesting_rooms(struct ctdlroom *qrbuf, void *data, OneRoomNetCfg *OneRNCfg)
277 struct RoomProcList *ptr;
278 roomlists *RP = (roomlists*) data;
280 if (!HaveSpoolConfig(OneRNCfg))
283 ptr = CreateRoomProcListEntry(qrbuf, OneRNCfg);
287 ptr->next = RP->rplist;
293 * Batch up and send all outbound traffic from the current room
295 int network_room_handler (struct ctdlroom *qrbuf)
297 struct RoomProcList *ptr;
298 OneRoomNetCfg* RNCfg;
300 if (qrbuf->QRdefaultview == VIEW_QUEUE)
303 RNCfg = CtdlGetNetCfgForRoom(qrbuf->QRnumber);
307 if (!HaveSpoolConfig(RNCfg))
310 ptr = CreateRoomProcListEntry(qrbuf, RNCfg);
314 ptr->OneRNCfg = NULL;
315 begin_critical_section(S_RPLIST);
318 end_critical_section(S_RPLIST);
322 void destroy_network_queue_room(RoomProcList *rplist)
324 struct RoomProcList *cur, *p;
335 void destroy_network_queue_room_locked (void)
337 begin_critical_section(S_RPLIST);
338 destroy_network_queue_room(rplist);
339 end_critical_section(S_RPLIST);
345 * Bounce a message back to the sender
347 void network_bounce(struct CtdlMessage *msg, char *reason)
349 struct CitContext *CCC = CC;
351 char bouncesource[SIZ];
353 struct recptypes *valid = NULL;
354 char force_room[ROOMNAMELEN];
355 static int serialnum = 0;
358 QNM_syslog(LOG_DEBUG, "entering network_bounce()\n");
360 if (msg == NULL) return;
362 snprintf(bouncesource, sizeof bouncesource, "%s@%s", BOUNCESOURCE, config.c_nodename);
365 * Give it a fresh message ID
367 len = snprintf(buf, sizeof(buf),
374 CM_SetField(msg, emessageId, buf, len);
377 * FIXME ... right now we're just sending a bounce; we really want to
378 * include the text of the bounced message.
380 CM_SetField(msg, eMesageText, reason, strlen(reason));
381 msg->cm_format_type = 0;
384 * Turn the message around
386 CM_FlushField(msg, eRecipient);
387 CM_FlushField(msg, eDestination);
389 len = snprintf(recipient, sizeof(recipient), "%s@%s",
390 msg->cm_fields[eAuthor],
391 msg->cm_fields[eNodeName]);
393 CM_SetField(msg, eAuthor, HKEY(BOUNCESOURCE));
394 CM_SetField(msg, eNodeName, config.c_nodename, strlen(config.c_nodename));
395 CM_SetField(msg, eMsgSubject, HKEY("Delivery Status Notification (Failure)"));
397 Netmap_AddMe(msg, HKEY("unknown_user"));
399 /* Now submit the message */
400 valid = validate_recipients(recipient, NULL, 0);
401 if (valid != NULL) if (valid->num_error != 0) {
402 free_recipients(valid);
405 if ( (valid == NULL) || (!strcasecmp(recipient, bouncesource)) ) {
406 strcpy(force_room, config.c_aideroom);
409 strcpy(force_room, "");
411 if ( (valid == NULL) && IsEmptyStr(force_room) ) {
412 strcpy(force_room, config.c_aideroom);
414 CtdlSubmitMsg(msg, valid, force_room, 0);
417 if (valid != NULL) free_recipients(valid);
419 QNM_syslog(LOG_DEBUG, "leaving network_bounce()\n");
427 * Run through the rooms doing various types of network stuff.
429 void network_do_queue(void)
431 struct CitContext *CCC = CC;
432 static time_t last_run = 0L;
433 int full_processing = 1;
434 HashList *working_ignetcfg;
435 HashList *the_netmap = NULL;
436 int netmap_changed = 0;
438 SpoolControl *sc = NULL;
442 * Run the full set of processing tasks no more frequently
443 * than once every n seconds
445 if ( (time(NULL) - last_run) < config.c_net_freq ) {
447 syslog(LOG_DEBUG, "Network full processing in %ld seconds.\n",
448 config.c_net_freq - (time(NULL)- last_run)
452 become_session(&networker_spool_CC);
453 begin_critical_section(S_RPLIST);
456 end_critical_section(S_RPLIST);
457 ///TODO hm, check whether we have a config at all here?
458 /* Load the IGnet Configuration into memory */
459 working_ignetcfg = CtdlLoadIgNetCfg();
462 * Load the network map and filter list into memory.
464 if (!server_shutting_down)
465 the_netmap = CtdlReadNetworkMap();
467 /* filterlist isn't supported anymore
468 if (!server_shutting_down)
469 load_network_filter_list();
474 * Go ahead and run the queue
476 if (full_processing && !server_shutting_down) {
477 QNM_syslog(LOG_DEBUG, "network: loading outbound queue");
478 CtdlForEachNetCfgRoom(network_queue_interesting_rooms, &RL, maxRoomNetCfg);
481 if ((RL.rplist != NULL) && (!server_shutting_down)) {
482 RoomProcList *ptr, *cmp;
484 QNM_syslog(LOG_DEBUG, "network: running outbound queue");
485 while (ptr != NULL && !server_shutting_down) {
488 /* filter duplicates from the list... */
489 while (cmp != NULL) {
490 if ((cmp->namelen > 0) &&
491 (cmp->key == ptr->key) &&
492 (cmp->namelen == ptr->namelen) &&
493 (strcmp(cmp->lcname, ptr->lcname) == 0))
500 if (ptr->namelen > 0) {
501 InspectQueuedRoom(&sc,
514 network_spoolout_room(pSC);
522 free_spoolcontrol_struct(&pSC);
525 /* If there is anything in the inbound queue, process it */
526 if (!server_shutting_down) {
527 network_do_spoolin(working_ignetcfg,
532 /* Free the filter list in memory */
533 free_netfilter_list();
535 /* Save the network map back to disk */
536 if (netmap_changed) {
537 StrBuf *MapStr = CtdlSerializeNetworkMap(the_netmap);
538 char *pMapStr = SmashStrBuf(&MapStr);
539 CtdlPutSysConfig(IGNETMAP, pMapStr);
543 /* combine singe message files into one spool entry per remote node. */
544 network_consolidate_spoolout(working_ignetcfg, the_netmap);
548 DeleteHash(&the_netmap);
550 DeleteHash(&working_ignetcfg);
552 QNM_syslog(LOG_DEBUG, "network: queue run completed");
554 if (full_processing) {
555 last_run = time(NULL);
557 destroy_network_queue_room(RL.rplist);
558 SaveChangedConfigs();
569 void network_logout_hook(void)
571 CitContext *CCC = MyContext();
574 * If we were talking to a network node, we're not anymore...
576 if (!IsEmptyStr(CCC->net_node)) {
577 CtdlNetworkTalkingTo(CCC->net_node, strlen(CCC->net_node), NTT_REMOVE);
578 CCC->net_node[0] = '\0';
581 void network_cleanup_function(void)
583 struct CitContext *CCC = CC;
585 if (!IsEmptyStr(CCC->net_node)) {
586 CtdlNetworkTalkingTo(CCC->net_node, strlen(CCC->net_node), NTT_REMOVE);
587 CCC->net_node[0] = '\0';
596 void SetNetQDebugEnabled(const int n)
598 NetQDebugEnabled = n;
601 CTDL_MODULE_INIT(network)
605 CtdlFillSystemContext(&networker_spool_CC, "CitNetSpool");
606 CtdlRegisterDebugFlagHook(HKEY("networkqueue"), SetNetQDebugEnabled, &NetQDebugEnabled);
607 CtdlRegisterSessionHook(network_cleanup_function, EVT_STOP, PRIO_STOP + 30);
608 CtdlRegisterSessionHook(network_logout_hook, EVT_LOGOUT, PRIO_LOGOUT + 10);
609 CtdlRegisterProtoHook(cmd_nsyn, "NSYN", "Synchronize room to node");
610 CtdlRegisterRoomHook(network_room_handler);
611 CtdlRegisterCleanupHook(destroy_network_queue_room_locked);
612 CtdlRegisterSessionHook(network_do_queue, EVT_TIMER, PRIO_QUEUE + 10);