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['I'] == NULL) || (IsEmptyStr(msg->cm_fields['I'])))
119 /* Generate the message ID */
120 msgid = NewStrBufPlain(msg->cm_fields['I'], -1);
121 if (haschar(ChrPtr(msgid), '@') == 0) {
122 StrBufAppendBufPlain(msgid, HKEY("@"), 0);
123 if (msg->cm_fields['N'] != NULL) {
124 StrBufAppendBufPlain(msgid, msg->cm_fields['N'], -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;
351 char *oldpath = NULL;
353 char bouncesource[SIZ];
355 struct recptypes *valid = NULL;
356 char force_room[ROOMNAMELEN];
357 static int serialnum = 0;
360 QNM_syslog(LOG_DEBUG, "entering network_bounce()\n");
362 if (msg == NULL) return;
364 snprintf(bouncesource, sizeof bouncesource, "%s@%s", BOUNCESOURCE, config.c_nodename);
367 * Give it a fresh message ID
369 if (msg->cm_fields['I'] != NULL) {
370 free(msg->cm_fields['I']);
372 snprintf(buf, sizeof buf, "%ld.%04lx.%04x@%s",
373 (long)time(NULL), (long)getpid(), ++serialnum, config.c_fqdn);
374 msg->cm_fields['I'] = strdup(buf);
377 * FIXME ... right now we're just sending a bounce; we really want to
378 * include the text of the bounced message.
380 if (msg->cm_fields['M'] != NULL) {
381 free(msg->cm_fields['M']);
383 msg->cm_fields['M'] = strdup(reason);
384 msg->cm_format_type = 0;
387 * Turn the message around
389 if (msg->cm_fields['R'] == NULL) {
390 free(msg->cm_fields['R']);
393 if (msg->cm_fields['D'] == NULL) {
394 free(msg->cm_fields['D']);
397 snprintf(recipient, sizeof recipient, "%s@%s",
398 msg->cm_fields['A'], msg->cm_fields['N']);
400 if (msg->cm_fields['A'] == NULL) {
401 free(msg->cm_fields['A']);
404 if (msg->cm_fields['N'] == NULL) {
405 free(msg->cm_fields['N']);
408 if (msg->cm_fields['U'] == NULL) {
409 free(msg->cm_fields['U']);
412 msg->cm_fields['A'] = strdup(BOUNCESOURCE);
413 msg->cm_fields['N'] = strdup(config.c_nodename);
414 msg->cm_fields['U'] = strdup("Delivery Status Notification (Failure)");
416 /* prepend our node to the path */
417 if (msg->cm_fields['P'] != NULL) {
418 oldpath = msg->cm_fields['P'];
419 msg->cm_fields['P'] = NULL;
422 oldpath = strdup("unknown_user");
424 size = strlen(oldpath) + SIZ;
425 msg->cm_fields['P'] = malloc(size);
426 snprintf(msg->cm_fields['P'], size, "%s!%s", config.c_nodename, oldpath);
429 /* Now submit the message */
430 valid = validate_recipients(recipient, NULL, 0);
431 if (valid != NULL) if (valid->num_error != 0) {
432 free_recipients(valid);
435 if ( (valid == NULL) || (!strcasecmp(recipient, bouncesource)) ) {
436 strcpy(force_room, config.c_aideroom);
439 strcpy(force_room, "");
441 if ( (valid == NULL) && IsEmptyStr(force_room) ) {
442 strcpy(force_room, config.c_aideroom);
444 CtdlSubmitMsg(msg, valid, force_room, 0);
447 if (valid != NULL) free_recipients(valid);
448 CtdlFreeMessage(msg);
449 QNM_syslog(LOG_DEBUG, "leaving network_bounce()\n");
457 * Run through the rooms doing various types of network stuff.
459 void network_do_queue(void)
461 struct CitContext *CCC = CC;
462 static time_t last_run = 0L;
463 int full_processing = 1;
464 HashList *working_ignetcfg;
465 HashList *the_netmap = NULL;
466 int netmap_changed = 0;
468 SpoolControl *sc = NULL;
472 * Run the full set of processing tasks no more frequently
473 * than once every n seconds
475 if ( (time(NULL) - last_run) < config.c_net_freq ) {
477 syslog(LOG_DEBUG, "Network full processing in %ld seconds.\n",
478 config.c_net_freq - (time(NULL)- last_run)
482 become_session(&networker_spool_CC);
483 begin_critical_section(S_RPLIST);
486 end_critical_section(S_RPLIST);
487 ///TODO hm, check whether we have a config at all here?
488 /* Load the IGnet Configuration into memory */
489 working_ignetcfg = CtdlLoadIgNetCfg();
492 * Load the network map and filter list into memory.
494 if (!server_shutting_down)
495 the_netmap = CtdlReadNetworkMap();
497 /* filterlist isn't supported anymore
498 if (!server_shutting_down)
499 load_network_filter_list();
504 * Go ahead and run the queue
506 if (full_processing && !server_shutting_down) {
507 QNM_syslog(LOG_DEBUG, "network: loading outbound queue");
508 CtdlForEachNetCfgRoom(network_queue_interesting_rooms, &RL, maxRoomNetCfg);
511 if ((RL.rplist != NULL) && (!server_shutting_down)) {
512 RoomProcList *ptr, *cmp;
514 QNM_syslog(LOG_DEBUG, "network: running outbound queue");
515 while (ptr != NULL && !server_shutting_down) {
518 /* filter duplicates from the list... */
519 while (cmp != NULL) {
520 if ((cmp->namelen > 0) &&
521 (cmp->key == ptr->key) &&
522 (cmp->namelen == ptr->namelen) &&
523 (strcmp(cmp->lcname, ptr->lcname) == 0))
530 if (ptr->namelen > 0) {
531 InspectQueuedRoom(&sc,
544 network_spoolout_room(pSC);
552 free_spoolcontrol_struct(&pSC);
555 /* If there is anything in the inbound queue, process it */
556 if (!server_shutting_down) {
557 network_do_spoolin(working_ignetcfg,
562 /* Free the filter list in memory */
563 free_netfilter_list();
565 /* Save the network map back to disk */
566 if (netmap_changed) {
567 StrBuf *MapStr = CtdlSerializeNetworkMap(the_netmap);
568 char *pMapStr = SmashStrBuf(&MapStr);
569 CtdlPutSysConfig(IGNETMAP, pMapStr);
573 /* combine singe message files into one spool entry per remote node. */
574 network_consolidate_spoolout(working_ignetcfg, the_netmap);
578 DeleteHash(&the_netmap);
580 DeleteHash(&working_ignetcfg);
582 QNM_syslog(LOG_DEBUG, "network: queue run completed");
584 if (full_processing) {
585 last_run = time(NULL);
587 destroy_network_queue_room(RL.rplist);
588 SaveChangedConfigs();
599 void network_logout_hook(void)
601 CitContext *CCC = MyContext();
604 * If we were talking to a network node, we're not anymore...
606 if (!IsEmptyStr(CCC->net_node)) {
607 CtdlNetworkTalkingTo(CCC->net_node, strlen(CCC->net_node), NTT_REMOVE);
608 CCC->net_node[0] = '\0';
611 void network_cleanup_function(void)
613 struct CitContext *CCC = CC;
615 if (!IsEmptyStr(CCC->net_node)) {
616 CtdlNetworkTalkingTo(CCC->net_node, strlen(CCC->net_node), NTT_REMOVE);
617 CCC->net_node[0] = '\0';
626 void SetNetQDebugEnabled(const int n)
628 NetQDebugEnabled = n;
631 CTDL_MODULE_INIT(network)
635 CtdlFillSystemContext(&networker_spool_CC, "CitNetSpool");
636 CtdlRegisterDebugFlagHook(HKEY("networkqueue"), SetNetQDebugEnabled, &NetQDebugEnabled);
637 CtdlRegisterSessionHook(network_cleanup_function, EVT_STOP, PRIO_STOP + 30);
638 CtdlRegisterSessionHook(network_logout_hook, EVT_LOGOUT, PRIO_LOGOUT + 10);
639 CtdlRegisterProtoHook(cmd_nsyn, "NSYN", "Synchronize room to node");
640 CtdlRegisterRoomHook(network_room_handler);
641 CtdlRegisterCleanupHook(destroy_network_queue_room_locked);
642 CtdlRegisterSessionHook(network_do_queue, EVT_TIMER, PRIO_QUEUE + 10);