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"
76 #include "citadel_dirs.h"
79 #include "ctdl_module.h"
83 int NetQDebugEnabled = 0;
84 struct CitContext networker_spool_CC;
86 /* comes from lookup3.c from libcitadel... */
87 extern uint32_t hashlittle( const void *key, size_t length, uint32_t initval);
89 typedef struct __roomlists {
93 * When we do network processing, it's accomplished in two passes; one to
94 * gather a list of rooms and one to actually do them. It's ok that rplist
95 * is global; we have a mutex that keeps it safe.
97 struct RoomProcList *rplist = NULL;
102 * Check the use table. This is a list of messages which have recently
103 * arrived on the system. It is maintained and queried to prevent the same
104 * message from being entered into the database multiple times if it happens
105 * to arrive multiple times by accident.
107 int network_usetable(struct CtdlMessage *msg)
110 struct CitContext *CCC = CC;
113 /* Bail out if we can't generate a message ID */
114 if ((msg == NULL) || (msg->cm_fields[emessageId] == NULL) || (IsEmptyStr(msg->cm_fields[emessageId])))
119 /* Generate the message ID */
120 msgid = NewStrBufPlain(msg->cm_fields[emessageId], -1);
121 if (haschar(ChrPtr(msgid), '@') == 0) {
122 StrBufAppendBufPlain(msgid, HKEY("@"), 0);
123 if (msg->cm_fields[eNodeName] != NULL) {
124 StrBufAppendBufPlain(msgid, msg->cm_fields[eNodeName], -1, 0);
132 if (CheckIfAlreadySeen("Networker Import",
136 CCC->cs_pid, 0) != 0)
149 * Send the *entire* contents of the current room to one specific network node,
150 * ignoring anything we know about which messages have already undergone
151 * network processing. This can be used to bring a new node into sync.
153 int network_sync_to(char *target_node, long len)
155 struct CitContext *CCC = CC;
156 OneRoomNetCfg OneRNCFG;
157 OneRoomNetCfg *pRNCFG;
158 const RoomNetCfgLine *pCfgLine;
162 /* Grab the configuration line we're looking for */
163 begin_critical_section(S_NETCONFIGS);
164 pRNCFG = CtdlGetNetCfgForRoom(CCC->room.QRnumber);
165 if ((pRNCFG == NULL) ||
166 (pRNCFG->NetConfigs[ignet_push_share] == NULL))
171 pCfgLine = pRNCFG->NetConfigs[ignet_push_share];
172 while (pCfgLine != NULL)
174 if (!strcmp(ChrPtr(pCfgLine->Value[0]), target_node))
176 pCfgLine = pCfgLine->next;
178 if (pCfgLine == NULL)
183 memset(&sc, 0, sizeof(SpoolControl));
184 memset(&OneRNCFG, 0, sizeof(OneRoomNetCfg));
185 sc.RNCfg = &OneRNCFG;
186 sc.RNCfg->NetConfigs[ignet_push_share] = DuplicateOneGenericCfgLine(pCfgLine);
187 sc.Users[ignet_push_share] = NewStrBufPlain(NULL,
188 StrLength(pCfgLine->Value[0]) +
189 StrLength(pCfgLine->Value[1]) + 10);
190 StrBufAppendBuf(sc.Users[ignet_push_share],
193 StrBufAppendBufPlain(sc.Users[ignet_push_share],
197 StrBufAppendBuf(sc.Users[ignet_push_share],
202 end_critical_section(S_NETCONFIGS);
204 sc.working_ignetcfg = CtdlLoadIgNetCfg();
205 sc.the_netmap = CtdlReadNetworkMap();
207 /* Send ALL messages */
208 num_spooled = CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL,
209 network_spool_msg, &sc);
211 /* Concise cleanup because we know there's only one node in the sc */
212 DeleteGenericCfgLine(NULL/*TODO*/, &sc.RNCfg->NetConfigs[ignet_push_share]);
214 DeleteHash(&sc.working_ignetcfg);
215 DeleteHash(&sc.the_netmap);
216 free_spoolcontrol_struct_members(&sc);
218 QN_syslog(LOG_NOTICE, "Synchronized %d messages to <%s>\n",
219 num_spooled, target_node);
225 * Implements the NSYN command
227 void cmd_nsyn(char *argbuf) {
230 char target_node[256];
232 if (CtdlAccessCheck(ac_aide)) return;
234 len = extract_token(target_node, argbuf, 0, '|', sizeof target_node);
235 num_spooled = network_sync_to(target_node, len);
236 if (num_spooled >= 0) {
237 cprintf("%d Spooled %d messages.\n", CIT_OK, num_spooled);
240 cprintf("%d No such room/node share exists.\n",
241 ERROR + ROOM_NOT_FOUND);
245 RoomProcList *CreateRoomProcListEntry(struct ctdlroom *qrbuf, OneRoomNetCfg *OneRNCFG)
248 struct RoomProcList *ptr;
250 ptr = (struct RoomProcList *) malloc(sizeof (struct RoomProcList));
251 if (ptr == NULL) return NULL;
253 ptr->namelen = strlen(qrbuf->QRname);
254 if (ptr->namelen > ROOMNAMELEN)
255 ptr->namelen = ROOMNAMELEN - 1;
257 memcpy (ptr->name, qrbuf->QRname, ptr->namelen);
258 ptr->name[ptr->namelen] = '\0';
259 ptr->QRNum = qrbuf->QRnumber;
261 for (i = 0; i < ptr->namelen; i++)
263 ptr->lcname[i] = tolower(ptr->name[i]);
266 ptr->lcname[ptr->namelen] = '\0';
267 ptr->key = hashlittle(ptr->lcname, ptr->namelen, 9872345);
268 ptr->lastsent = OneRNCFG->lastsent;
269 ptr->OneRNCfg = OneRNCFG;
274 * Batch up and send all outbound traffic from the current room
276 void network_queue_interesting_rooms(struct ctdlroom *qrbuf, void *data, OneRoomNetCfg *OneRNCfg)
278 struct RoomProcList *ptr;
279 roomlists *RP = (roomlists*) data;
281 if (!HaveSpoolConfig(OneRNCfg))
284 ptr = CreateRoomProcListEntry(qrbuf, OneRNCfg);
288 ptr->next = RP->rplist;
294 * Batch up and send all outbound traffic from the current room
296 int network_room_handler (struct ctdlroom *qrbuf)
298 struct RoomProcList *ptr;
299 OneRoomNetCfg* RNCfg;
301 if (qrbuf->QRdefaultview == VIEW_QUEUE)
304 RNCfg = CtdlGetNetCfgForRoom(qrbuf->QRnumber);
308 if (!HaveSpoolConfig(RNCfg))
311 ptr = CreateRoomProcListEntry(qrbuf, RNCfg);
315 ptr->OneRNCfg = NULL;
316 begin_critical_section(S_RPLIST);
319 end_critical_section(S_RPLIST);
323 void destroy_network_queue_room(RoomProcList *rplist)
325 struct RoomProcList *cur, *p;
336 void destroy_network_queue_room_locked (void)
338 begin_critical_section(S_RPLIST);
339 destroy_network_queue_room(rplist);
340 end_critical_section(S_RPLIST);
346 * Bounce a message back to the sender
348 void network_bounce(struct CtdlMessage *msg, char *reason)
350 struct CitContext *CCC = CC;
352 char bouncesource[SIZ];
354 struct recptypes *valid = NULL;
355 char force_room[ROOMNAMELEN];
356 static int serialnum = 0;
359 QNM_syslog(LOG_DEBUG, "entering network_bounce()\n");
361 if (msg == NULL) return;
363 snprintf(bouncesource, sizeof bouncesource, "%s@%s", BOUNCESOURCE, config.c_nodename);
366 * Give it a fresh message ID
368 len = snprintf(buf, sizeof(buf),
375 CM_SetField(msg, emessageId, buf, len);
378 * FIXME ... right now we're just sending a bounce; we really want to
379 * include the text of the bounced message.
381 CM_SetField(msg, eMesageText, reason, strlen(reason));
382 msg->cm_format_type = 0;
385 * Turn the message around
387 CM_FlushField(msg, eRecipient);
388 CM_FlushField(msg, eDestination);
390 len = snprintf(recipient, sizeof(recipient), "%s@%s",
391 msg->cm_fields[eAuthor],
392 msg->cm_fields[eNodeName]);
394 CM_SetField(msg, eAuthor, HKEY(BOUNCESOURCE));
395 CM_SetField(msg, eNodeName, config.c_nodename, strlen(config.c_nodename));
396 CM_SetField(msg, eMsgSubject, HKEY("Delivery Status Notification (Failure)"));
398 Netmap_AddMe(msg, HKEY("unknown_user"));
400 /* Now submit the message */
401 valid = validate_recipients(recipient, NULL, 0);
402 if (valid != NULL) if (valid->num_error != 0) {
403 free_recipients(valid);
406 if ( (valid == NULL) || (!strcasecmp(recipient, bouncesource)) ) {
407 strcpy(force_room, config.c_aideroom);
410 strcpy(force_room, "");
412 if ( (valid == NULL) && IsEmptyStr(force_room) ) {
413 strcpy(force_room, config.c_aideroom);
415 CtdlSubmitMsg(msg, valid, force_room, 0);
418 if (valid != NULL) free_recipients(valid);
419 CtdlFreeMessage(msg);
420 QNM_syslog(LOG_DEBUG, "leaving network_bounce()\n");
428 * Run through the rooms doing various types of network stuff.
430 void network_do_queue(void)
432 struct CitContext *CCC = CC;
433 static time_t last_run = 0L;
434 int full_processing = 1;
435 HashList *working_ignetcfg;
436 HashList *the_netmap = NULL;
437 int netmap_changed = 0;
439 SpoolControl *sc = NULL;
443 * Run the full set of processing tasks no more frequently
444 * than once every n seconds
446 if ( (time(NULL) - last_run) < config.c_net_freq ) {
448 syslog(LOG_DEBUG, "Network full processing in %ld seconds.\n",
449 config.c_net_freq - (time(NULL)- last_run)
453 become_session(&networker_spool_CC);
454 begin_critical_section(S_RPLIST);
457 end_critical_section(S_RPLIST);
458 ///TODO hm, check whether we have a config at all here?
459 /* Load the IGnet Configuration into memory */
460 working_ignetcfg = CtdlLoadIgNetCfg();
463 * Load the network map and filter list into memory.
465 if (!server_shutting_down)
466 the_netmap = CtdlReadNetworkMap();
468 /* filterlist isn't supported anymore
469 if (!server_shutting_down)
470 load_network_filter_list();
475 * Go ahead and run the queue
477 if (full_processing && !server_shutting_down) {
478 QNM_syslog(LOG_DEBUG, "network: loading outbound queue");
479 CtdlForEachNetCfgRoom(network_queue_interesting_rooms, &RL, maxRoomNetCfg);
482 if ((RL.rplist != NULL) && (!server_shutting_down)) {
483 RoomProcList *ptr, *cmp;
485 QNM_syslog(LOG_DEBUG, "network: running outbound queue");
486 while (ptr != NULL && !server_shutting_down) {
489 /* filter duplicates from the list... */
490 while (cmp != NULL) {
491 if ((cmp->namelen > 0) &&
492 (cmp->key == ptr->key) &&
493 (cmp->namelen == ptr->namelen) &&
494 (strcmp(cmp->lcname, ptr->lcname) == 0))
501 if (ptr->namelen > 0) {
502 InspectQueuedRoom(&sc,
515 network_spoolout_room(pSC);
523 free_spoolcontrol_struct(&pSC);
526 /* If there is anything in the inbound queue, process it */
527 if (!server_shutting_down) {
528 network_do_spoolin(working_ignetcfg,
533 /* Free the filter list in memory */
534 free_netfilter_list();
536 /* Save the network map back to disk */
537 if (netmap_changed) {
538 StrBuf *MapStr = CtdlSerializeNetworkMap(the_netmap);
539 char *pMapStr = SmashStrBuf(&MapStr);
540 CtdlPutSysConfig(IGNETMAP, pMapStr);
544 /* combine singe message files into one spool entry per remote node. */
545 network_consolidate_spoolout(working_ignetcfg, the_netmap);
549 DeleteHash(&the_netmap);
551 DeleteHash(&working_ignetcfg);
553 QNM_syslog(LOG_DEBUG, "network: queue run completed");
555 if (full_processing) {
556 last_run = time(NULL);
558 destroy_network_queue_room(RL.rplist);
559 SaveChangedConfigs();
570 void network_logout_hook(void)
572 CitContext *CCC = MyContext();
575 * If we were talking to a network node, we're not anymore...
577 if (!IsEmptyStr(CCC->net_node)) {
578 CtdlNetworkTalkingTo(CCC->net_node, strlen(CCC->net_node), NTT_REMOVE);
579 CCC->net_node[0] = '\0';
582 void network_cleanup_function(void)
584 struct CitContext *CCC = CC;
586 if (!IsEmptyStr(CCC->net_node)) {
587 CtdlNetworkTalkingTo(CCC->net_node, strlen(CCC->net_node), NTT_REMOVE);
588 CCC->net_node[0] = '\0';
597 void SetNetQDebugEnabled(const int n)
599 NetQDebugEnabled = n;
602 CTDL_MODULE_INIT(network)
606 CtdlFillSystemContext(&networker_spool_CC, "CitNetSpool");
607 CtdlRegisterDebugFlagHook(HKEY("networkqueue"), SetNetQDebugEnabled, &NetQDebugEnabled);
608 CtdlRegisterSessionHook(network_cleanup_function, EVT_STOP, PRIO_STOP + 30);
609 CtdlRegisterSessionHook(network_logout_hook, EVT_LOGOUT, PRIO_LOGOUT + 10);
610 CtdlRegisterProtoHook(cmd_nsyn, "NSYN", "Synchronize room to node");
611 CtdlRegisterRoomHook(network_room_handler);
612 CtdlRegisterCleanupHook(destroy_network_queue_room_locked);
613 CtdlRegisterSessionHook(network_do_queue, EVT_TIMER, PRIO_QUEUE + 10);