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)
109 struct CitContext *CCC = CC;
111 struct cdbdata *cdbut;
114 /* Bail out if we can't generate a message ID */
118 if (msg->cm_fields['I'] == NULL) {
121 if (IsEmptyStr(msg->cm_fields['I'])) {
125 /* Generate the message ID */
126 strcpy(msgid, msg->cm_fields['I']);
127 if (haschar(msgid, '@') == 0) {
129 if (msg->cm_fields['N'] != NULL) {
130 strcat(msgid, msg->cm_fields['N']);
137 cdbut = cdb_fetch(CDB_USETABLE, msgid, strlen(msgid));
140 QN_syslog(LOG_DEBUG, "network_usetable() : we already have %s\n", msgid);
144 /* If we got to this point, it's unique: add it. */
145 strcpy(ut.ut_msgid, msgid);
146 ut.ut_timestamp = time(NULL);
147 cdb_store(CDB_USETABLE, msgid, strlen(msgid), &ut, sizeof(struct UseTable) );
154 * Send the *entire* contents of the current room to one specific network node,
155 * ignoring anything we know about which messages have already undergone
156 * network processing. This can be used to bring a new node into sync.
158 int network_sync_to(char *target_node, long len)
160 struct CitContext *CCC = CC;
161 OneRoomNetCfg OneRNCFG;
162 OneRoomNetCfg *pRNCFG;
163 const RoomNetCfgLine *pCfgLine;
167 /* Grab the configuration line we're looking for */
168 begin_critical_section(S_NETCONFIGS);
169 pRNCFG = CtdlGetNetCfgForRoom(CCC->room.QRnumber);
170 if ((pRNCFG == NULL) ||
171 (pRNCFG->NetConfigs[ignet_push_share] == NULL))
176 pCfgLine = pRNCFG->NetConfigs[ignet_push_share];
177 while (pCfgLine != NULL)
179 if (strcmp(ChrPtr(pCfgLine->Value[0]), target_node))
181 pCfgLine = pCfgLine->next;
183 if (pCfgLine == NULL)
187 memset(&sc, 0, sizeof(SpoolControl));
188 memset(&OneRNCFG, 0, sizeof(OneRoomNetCfg));
189 sc.RNCfg = &OneRNCFG;
190 sc.RNCfg->NetConfigs[ignet_push_share] = DuplicateOneGenericCfgLine(pCfgLine);
192 end_critical_section(S_NETCONFIGS);
194 sc.working_ignetcfg = CtdlLoadIgNetCfg();
195 sc.the_netmap = CtdlReadNetworkMap();
197 /* Send ALL messages */
198 num_spooled = CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL,
199 network_spool_msg, &sc);
201 /* Concise cleanup because we know there's only one node in the sc */
202 DeleteGenericCfgLine(NULL/*TODO*/, &sc.RNCfg->NetConfigs[ignet_push_share]);
204 DeleteHash(&sc.working_ignetcfg);
205 DeleteHash(&sc.the_netmap);
207 QN_syslog(LOG_NOTICE, "Synchronized %d messages to <%s>\n",
208 num_spooled, target_node);
214 * Implements the NSYN command
216 void cmd_nsyn(char *argbuf) {
219 char target_node[256];
221 if (CtdlAccessCheck(ac_aide)) return;
223 len = extract_token(target_node, argbuf, 0, '|', sizeof target_node);
224 num_spooled = network_sync_to(target_node, len);
225 if (num_spooled >= 0) {
226 cprintf("%d Spooled %d messages.\n", CIT_OK, num_spooled);
229 cprintf("%d No such room/node share exists.\n",
230 ERROR + ROOM_NOT_FOUND);
234 RoomProcList *CreateRoomProcListEntry(struct ctdlroom *qrbuf, OneRoomNetCfg *OneRNCFG)
237 struct RoomProcList *ptr;
239 ptr = (struct RoomProcList *) malloc(sizeof (struct RoomProcList));
240 if (ptr == NULL) return NULL;
242 ptr->namelen = strlen(qrbuf->QRname);
243 if (ptr->namelen > ROOMNAMELEN)
244 ptr->namelen = ROOMNAMELEN - 1;
246 memcpy (ptr->name, qrbuf->QRname, ptr->namelen);
247 ptr->name[ptr->namelen] = '\0';
248 ptr->QRNum = qrbuf->QRnumber;
250 for (i = 0; i < ptr->namelen; i++)
252 ptr->lcname[i] = tolower(ptr->name[i]);
255 ptr->lcname[ptr->namelen] = '\0';
256 ptr->key = hashlittle(ptr->lcname, ptr->namelen, 9872345);
257 ptr->lastsent = OneRNCFG->lastsent;
258 ptr->OneRNCfg = OneRNCFG;
263 * Batch up and send all outbound traffic from the current room
265 void network_queue_interesting_rooms(struct ctdlroom *qrbuf, void *data, OneRoomNetCfg *OneRNCfg)
267 struct RoomProcList *ptr;
268 roomlists *RP = (roomlists*) data;
270 if (!HaveSpoolConfig(OneRNCfg))
273 ptr = CreateRoomProcListEntry(qrbuf, OneRNCfg);
277 ptr->next = RP->rplist;
283 * Batch up and send all outbound traffic from the current room
285 int network_room_handler (struct ctdlroom *qrbuf)
287 struct RoomProcList *ptr;
288 OneRoomNetCfg* RNCfg;
290 if (qrbuf->QRdefaultview == VIEW_QUEUE)
293 RNCfg = CtdlGetNetCfgForRoom(qrbuf->QRnumber);
297 if (!HaveSpoolConfig(RNCfg))
300 ptr = CreateRoomProcListEntry(qrbuf, RNCfg);
304 ptr->OneRNCfg = NULL;
305 begin_critical_section(S_RPLIST);
308 end_critical_section(S_RPLIST);
312 void destroy_network_queue_room(RoomProcList *rplist)
314 struct RoomProcList *cur, *p;
325 void destroy_network_queue_room_locked (void)
327 begin_critical_section(S_RPLIST);
328 destroy_network_queue_room(rplist);
329 end_critical_section(S_RPLIST);
335 * Bounce a message back to the sender
337 void network_bounce(struct CtdlMessage *msg, char *reason)
339 struct CitContext *CCC = CC;
340 char *oldpath = NULL;
342 char bouncesource[SIZ];
344 struct recptypes *valid = NULL;
345 char force_room[ROOMNAMELEN];
346 static int serialnum = 0;
349 QNM_syslog(LOG_DEBUG, "entering network_bounce()\n");
351 if (msg == NULL) return;
353 snprintf(bouncesource, sizeof bouncesource, "%s@%s", BOUNCESOURCE, config.c_nodename);
356 * Give it a fresh message ID
358 if (msg->cm_fields['I'] != NULL) {
359 free(msg->cm_fields['I']);
361 snprintf(buf, sizeof buf, "%ld.%04lx.%04x@%s",
362 (long)time(NULL), (long)getpid(), ++serialnum, config.c_fqdn);
363 msg->cm_fields['I'] = strdup(buf);
366 * FIXME ... right now we're just sending a bounce; we really want to
367 * include the text of the bounced message.
369 if (msg->cm_fields['M'] != NULL) {
370 free(msg->cm_fields['M']);
372 msg->cm_fields['M'] = strdup(reason);
373 msg->cm_format_type = 0;
376 * Turn the message around
378 if (msg->cm_fields['R'] == NULL) {
379 free(msg->cm_fields['R']);
382 if (msg->cm_fields['D'] == NULL) {
383 free(msg->cm_fields['D']);
386 snprintf(recipient, sizeof recipient, "%s@%s",
387 msg->cm_fields['A'], msg->cm_fields['N']);
389 if (msg->cm_fields['A'] == NULL) {
390 free(msg->cm_fields['A']);
393 if (msg->cm_fields['N'] == NULL) {
394 free(msg->cm_fields['N']);
397 if (msg->cm_fields['U'] == NULL) {
398 free(msg->cm_fields['U']);
401 msg->cm_fields['A'] = strdup(BOUNCESOURCE);
402 msg->cm_fields['N'] = strdup(config.c_nodename);
403 msg->cm_fields['U'] = strdup("Delivery Status Notification (Failure)");
405 /* prepend our node to the path */
406 if (msg->cm_fields['P'] != NULL) {
407 oldpath = msg->cm_fields['P'];
408 msg->cm_fields['P'] = NULL;
411 oldpath = strdup("unknown_user");
413 size = strlen(oldpath) + SIZ;
414 msg->cm_fields['P'] = malloc(size);
415 snprintf(msg->cm_fields['P'], size, "%s!%s", config.c_nodename, oldpath);
418 /* Now submit the message */
419 valid = validate_recipients(recipient, NULL, 0);
420 if (valid != NULL) if (valid->num_error != 0) {
421 free_recipients(valid);
424 if ( (valid == NULL) || (!strcasecmp(recipient, bouncesource)) ) {
425 strcpy(force_room, config.c_aideroom);
428 strcpy(force_room, "");
430 if ( (valid == NULL) && IsEmptyStr(force_room) ) {
431 strcpy(force_room, config.c_aideroom);
433 CtdlSubmitMsg(msg, valid, force_room, 0);
436 if (valid != NULL) free_recipients(valid);
437 CtdlFreeMessage(msg);
438 QNM_syslog(LOG_DEBUG, "leaving network_bounce()\n");
442 void free_network_spoolout_room(SpoolControl *sc)
447 for (i = subpending; i < maxRoomNetCfg; i++)
448 FreeStrBuf(&sc->Users[i]);
459 * Run through the rooms doing various types of network stuff.
461 void network_do_queue(void)
463 struct CitContext *CCC = CC;
464 static time_t last_run = 0L;
465 int full_processing = 1;
466 HashList *working_ignetcfg;
467 HashList *the_netmap = NULL;
468 int netmap_changed = 0;
470 SpoolControl *sc = NULL;
474 * Run the full set of processing tasks no more frequently
475 * than once every n seconds
477 if ( (time(NULL) - last_run) < config.c_net_freq ) {
479 syslog(LOG_DEBUG, "Network full processing in %ld seconds.\n",
480 config.c_net_freq - (time(NULL)- last_run)
484 become_session(&networker_spool_CC);
485 begin_critical_section(S_RPLIST);
488 end_critical_section(S_RPLIST);
489 ///TODO hm, check whether we have a config at all here?
490 /* Load the IGnet Configuration into memory */
491 working_ignetcfg = CtdlLoadIgNetCfg();
494 * Load the network map and filter list into memory.
496 if (!server_shutting_down)
497 the_netmap = CtdlReadNetworkMap();
499 /* filterlist isn't supported anymore
500 if (!server_shutting_down)
501 load_network_filter_list();
506 * Go ahead and run the queue
508 if (full_processing && !server_shutting_down) {
509 QNM_syslog(LOG_DEBUG, "network: loading outbound queue");
510 CtdlForEachNetCfgRoom(network_queue_interesting_rooms, &RL, maxRoomNetCfg);
513 if ((RL.rplist != NULL) && (!server_shutting_down)) {
514 RoomProcList *ptr, *cmp;
516 QNM_syslog(LOG_DEBUG, "network: running outbound queue");
517 while (ptr != NULL && !server_shutting_down) {
520 /* filter duplicates from the list... */
521 while (cmp != NULL) {
522 if ((cmp->namelen > 0) &&
523 (cmp->key == ptr->key) &&
524 (cmp->namelen == ptr->namelen) &&
525 (strcmp(cmp->lcname, ptr->lcname) == 0))
532 if (ptr->namelen > 0) {
533 InspectQueuedRoom(&sc,
546 network_spoolout_room(pSC);
554 free_network_spoolout_room(pSC);
557 /* If there is anything in the inbound queue, process it */
558 if (!server_shutting_down) {
559 network_do_spoolin(working_ignetcfg,
564 /* Free the filter list in memory */
565 free_netfilter_list();
567 /* Save the network map back to disk */
568 if (netmap_changed) {
569 StrBuf *MapStr = CtdlSerializeNetworkMap(the_netmap);
570 char *pMapStr = SmashStrBuf(&MapStr);
571 CtdlPutSysConfig(IGNETMAP, pMapStr);
575 /* combine singe message files into one spool entry per remote node. */
576 network_consolidate_spoolout(working_ignetcfg, the_netmap);
580 DeleteHash(&the_netmap);
582 DeleteHash(&working_ignetcfg);
584 QNM_syslog(LOG_DEBUG, "network: queue run completed");
586 if (full_processing) {
587 last_run = time(NULL);
589 destroy_network_queue_room(RL.rplist);
590 SaveChangedConfigs();
601 void network_logout_hook(void)
603 CitContext *CCC = MyContext();
606 * If we were talking to a network node, we're not anymore...
608 if (!IsEmptyStr(CCC->net_node)) {
609 CtdlNetworkTalkingTo(CCC->net_node, strlen(CCC->net_node), NTT_REMOVE);
610 CCC->net_node[0] = '\0';
613 void network_cleanup_function(void)
615 struct CitContext *CCC = CC;
617 if (!IsEmptyStr(CCC->net_node)) {
618 CtdlNetworkTalkingTo(CCC->net_node, strlen(CCC->net_node), NTT_REMOVE);
619 CCC->net_node[0] = '\0';
628 void SetNetQDebugEnabled(const int n)
630 NetQDebugEnabled = n;
633 CTDL_MODULE_INIT(network)
637 CtdlFillSystemContext(&networker_spool_CC, "CitNetSpool");
638 CtdlRegisterDebugFlagHook(HKEY("networkqueue"), SetNetQDebugEnabled, &NetQDebugEnabled);
639 CtdlRegisterSessionHook(network_cleanup_function, EVT_STOP, PRIO_STOP + 30);
640 CtdlRegisterSessionHook(network_logout_hook, EVT_LOGOUT, PRIO_LOGOUT + 10);
641 CtdlRegisterProtoHook(cmd_nsyn, "NSYN", "Synchronize room to node");
642 CtdlRegisterRoomHook(network_room_handler);
643 CtdlRegisterCleanupHook(destroy_network_queue_room_locked);
644 CtdlRegisterSessionHook(network_do_queue, EVT_TIMER, PRIO_QUEUE + 10);