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 {
91 HashList *RoomsInterestedIn;
94 * When we do network processing, it's accomplished in two passes; one to
95 * gather a list of rooms and one to actually do them. It's ok that rplist
96 * is global; we have a mutex that keeps it safe.
98 struct RoomProcList *rplist = NULL;
103 * Check the use table. This is a list of messages which have recently
104 * arrived on the system. It is maintained and queried to prevent the same
105 * message from being entered into the database multiple times if it happens
106 * to arrive multiple times by accident.
108 int network_usetable(struct CtdlMessage *msg)
110 struct CitContext *CCC = CC;
112 struct cdbdata *cdbut;
115 /* Bail out if we can't generate a message ID */
119 if (msg->cm_fields['I'] == NULL) {
122 if (IsEmptyStr(msg->cm_fields['I'])) {
126 /* Generate the message ID */
127 strcpy(msgid, msg->cm_fields['I']);
128 if (haschar(msgid, '@') == 0) {
130 if (msg->cm_fields['N'] != NULL) {
131 strcat(msgid, msg->cm_fields['N']);
138 cdbut = cdb_fetch(CDB_USETABLE, msgid, strlen(msgid));
141 QN_syslog(LOG_DEBUG, "network_usetable() : we already have %s\n", msgid);
145 /* If we got to this point, it's unique: add it. */
146 strcpy(ut.ut_msgid, msgid);
147 ut.ut_timestamp = time(NULL);
148 cdb_store(CDB_USETABLE, msgid, strlen(msgid), &ut, sizeof(struct UseTable) );
155 * Send the *entire* contents of the current room to one specific network node,
156 * ignoring anything we know about which messages have already undergone
157 * network processing. This can be used to bring a new node into sync.
159 int network_sync_to(char *target_node, long len)
161 struct CitContext *CCC = CC;
162 const OneRoomNetCfg *OneRNCFG;
163 const RoomNetCfgLine *pCfgLine;
167 /* Grab the configuration line we're looking for */
168 begin_critical_section(S_NETCONFIGS);
169 OneRNCFG = CtdlGetNetCfgForRoom(CCC->room.QRnumber);
170 if ((OneRNCFG == NULL) ||
171 (OneRNCFG->NetConfigs[ignet_push_share] == NULL))
176 pCfgLine = OneRNCFG->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));
189 sc.NetConfigs[ignet_push_share] = DuplicateOneGenericCfgLine(pCfgLine);
191 end_critical_section(S_NETCONFIGS);
193 sc.working_ignetcfg = CtdlLoadIgNetCfg();
194 sc.the_netmap = CtdlReadNetworkMap();
196 /* Send ALL messages */
197 num_spooled = CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL,
198 network_spool_msg, &sc);
200 /* Concise cleanup because we know there's only one node in the sc */
201 DeleteGenericCfgLine(NULL/*TODO*/, &sc.NetConfigs[ignet_push_share]);
203 DeleteHash(&sc.working_ignetcfg);
204 DeleteHash(&sc.the_netmap);
206 QN_syslog(LOG_NOTICE, "Synchronized %d messages to <%s>\n",
207 num_spooled, target_node);
213 * Implements the NSYN command
215 void cmd_nsyn(char *argbuf) {
218 char target_node[256];
220 if (CtdlAccessCheck(ac_aide)) return;
222 len = extract_token(target_node, argbuf, 0, '|', sizeof target_node);
223 num_spooled = network_sync_to(target_node, len);
224 if (num_spooled >= 0) {
225 cprintf("%d Spooled %d messages.\n", CIT_OK, num_spooled);
228 cprintf("%d No such room/node share exists.\n",
229 ERROR + ROOM_NOT_FOUND);
233 RoomProcList *CreateRoomProcListEntry(struct ctdlroom *qrbuf, const OneRoomNetCfg *OneRNCFG)
236 struct RoomProcList *ptr;
238 ptr = (struct RoomProcList *) malloc(sizeof (struct RoomProcList));
239 if (ptr == NULL) return NULL;
241 ptr->namelen = strlen(qrbuf->QRname);
242 if (ptr->namelen > ROOMNAMELEN)
243 ptr->namelen = ROOMNAMELEN - 1;
245 memcpy (ptr->name, qrbuf->QRname, ptr->namelen);
246 ptr->name[ptr->namelen] = '\0';
247 ptr->QRNum = qrbuf->QRnumber;
249 for (i = 0; i < ptr->namelen; i++)
251 ptr->lcname[i] = tolower(ptr->name[i]);
254 ptr->lcname[ptr->namelen] = '\0';
255 ptr->key = hashlittle(ptr->lcname, ptr->namelen, 9872345);
256 ptr->OneRNCFG = OneRNCFG;
261 * Batch up and send all outbound traffic from the current room
263 void network_queue_interesting_rooms(struct ctdlroom *qrbuf, void *data, const OneRoomNetCfg *OneRNCfg)
265 struct RoomProcList *ptr;
266 roomlists *RP = (roomlists*) data;
268 ptr = CreateRoomProcListEntry(qrbuf, OneRNCfg);
270 ptr->next = RP->rplist;
275 * Batch up and send all outbound traffic from the current room
277 int network_room_handler (struct ctdlroom *qrbuf)
279 struct RoomProcList *ptr;
280 const OneRoomNetCfg* RNCfg;
282 if (qrbuf->QRdefaultview == VIEW_QUEUE)
285 RNCfg = CtdlGetNetCfgForRoom(qrbuf->QRnumber);
289 ptr = CreateRoomProcListEntry(qrbuf, RNCfg);
293 begin_critical_section(S_RPLIST);
296 end_critical_section(S_RPLIST);
300 void destroy_network_queue_room(RoomProcList *rplist)
302 struct RoomProcList *cur, *p;
313 void destroy_network_queue_room_locked (void)
315 begin_critical_section(S_RPLIST);
316 destroy_network_queue_room(rplist);
317 end_critical_section(S_RPLIST);
323 * Bounce a message back to the sender
325 void network_bounce(struct CtdlMessage *msg, char *reason)
327 struct CitContext *CCC = CC;
328 char *oldpath = NULL;
330 char bouncesource[SIZ];
332 struct recptypes *valid = NULL;
333 char force_room[ROOMNAMELEN];
334 static int serialnum = 0;
337 QNM_syslog(LOG_DEBUG, "entering network_bounce()\n");
339 if (msg == NULL) return;
341 snprintf(bouncesource, sizeof bouncesource, "%s@%s", BOUNCESOURCE, config.c_nodename);
344 * Give it a fresh message ID
346 if (msg->cm_fields['I'] != NULL) {
347 free(msg->cm_fields['I']);
349 snprintf(buf, sizeof buf, "%ld.%04lx.%04x@%s",
350 (long)time(NULL), (long)getpid(), ++serialnum, config.c_fqdn);
351 msg->cm_fields['I'] = strdup(buf);
354 * FIXME ... right now we're just sending a bounce; we really want to
355 * include the text of the bounced message.
357 if (msg->cm_fields['M'] != NULL) {
358 free(msg->cm_fields['M']);
360 msg->cm_fields['M'] = strdup(reason);
361 msg->cm_format_type = 0;
364 * Turn the message around
366 if (msg->cm_fields['R'] == NULL) {
367 free(msg->cm_fields['R']);
370 if (msg->cm_fields['D'] == NULL) {
371 free(msg->cm_fields['D']);
374 snprintf(recipient, sizeof recipient, "%s@%s",
375 msg->cm_fields['A'], msg->cm_fields['N']);
377 if (msg->cm_fields['A'] == NULL) {
378 free(msg->cm_fields['A']);
381 if (msg->cm_fields['N'] == NULL) {
382 free(msg->cm_fields['N']);
385 if (msg->cm_fields['U'] == NULL) {
386 free(msg->cm_fields['U']);
389 msg->cm_fields['A'] = strdup(BOUNCESOURCE);
390 msg->cm_fields['N'] = strdup(config.c_nodename);
391 msg->cm_fields['U'] = strdup("Delivery Status Notification (Failure)");
393 /* prepend our node to the path */
394 if (msg->cm_fields['P'] != NULL) {
395 oldpath = msg->cm_fields['P'];
396 msg->cm_fields['P'] = NULL;
399 oldpath = strdup("unknown_user");
401 size = strlen(oldpath) + SIZ;
402 msg->cm_fields['P'] = malloc(size);
403 snprintf(msg->cm_fields['P'], size, "%s!%s", config.c_nodename, oldpath);
406 /* Now submit the message */
407 valid = validate_recipients(recipient, NULL, 0);
408 if (valid != NULL) if (valid->num_error != 0) {
409 free_recipients(valid);
412 if ( (valid == NULL) || (!strcasecmp(recipient, bouncesource)) ) {
413 strcpy(force_room, config.c_aideroom);
416 strcpy(force_room, "");
418 if ( (valid == NULL) && IsEmptyStr(force_room) ) {
419 strcpy(force_room, config.c_aideroom);
421 CtdlSubmitMsg(msg, valid, force_room, 0);
424 if (valid != NULL) free_recipients(valid);
425 CtdlFreeMessage(msg);
426 QNM_syslog(LOG_DEBUG, "leaving network_bounce()\n");
438 * Run through the rooms doing various types of network stuff.
440 void network_do_queue(void)
442 struct CitContext *CCC = CC;
443 static int doing_queue = 0;
444 static time_t last_run = 0L;
445 int full_processing = 1;
446 HashList *working_ignetcfg;
447 HashList *the_netmap = NULL;
448 int netmap_changed = 0;
452 * Run the full set of processing tasks no more frequently
453 * than once every n seconds
455 if ( (time(NULL) - last_run) < config.c_net_freq ) {
457 syslog(LOG_DEBUG, "Network full processing in %ld seconds.\n",
458 config.c_net_freq - (time(NULL)- last_run)
463 * This is a simple concurrency check to make sure only one queue run
464 * is done at a time. We could do this with a mutex, but since we
465 * don't really require extremely fine granularity here, we'll do it
466 * with a static variable instead.
473 become_session(&networker_spool_CC);
474 begin_critical_section(S_RPLIST);
477 end_critical_section(S_RPLIST);
478 ///TODO hm, check whether we have a config at all here?
479 /* Load the IGnet Configuration into memory */
480 working_ignetcfg = CtdlLoadIgNetCfg();
483 * Load the network map and filter list into memory.
485 if (!server_shutting_down)
486 the_netmap = CtdlReadNetworkMap();
487 if (!server_shutting_down)
488 load_network_filter_list();
491 * Go ahead and run the queue
493 if (full_processing && !server_shutting_down) {
494 QNM_syslog(LOG_DEBUG, "network: loading outbound queue");
495 CtdlForEachNetCfgRoom(network_queue_interesting_rooms, &RL, maxRoomNetCfg);
498 if ((RL.rplist != NULL) && (!server_shutting_down)) {
499 RoomProcList *ptr, *cmp;
501 QNM_syslog(LOG_DEBUG, "network: running outbound queue");
502 while (ptr != NULL && !server_shutting_down) {
506 while (cmp != NULL) {
507 if ((cmp->namelen > 0) &&
508 (cmp->key == ptr->key) &&
509 (cmp->namelen == ptr->namelen) &&
510 (strcmp(cmp->lcname, ptr->lcname) == 0))
517 if (ptr->namelen > 0) {
518 network_spoolout_room(ptr,
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 CtdlPutSysConfig(IGNETMAP, SmashStrBuf(&MapStr));
542 /* combine singe message files into one spool entry per remote node. */
543 network_consolidate_spoolout(working_ignetcfg, the_netmap);
547 DeleteHash(&the_netmap);
549 DeleteHash(&working_ignetcfg);
551 QNM_syslog(LOG_DEBUG, "network: queue run completed");
553 if (full_processing) {
554 last_run = time(NULL);
556 DeleteHash(&RL.RoomsInterestedIn);
557 destroy_network_queue_room(RL.rplist);
568 void network_logout_hook(void)
570 CitContext *CCC = MyContext();
573 * If we were talking to a network node, we're not anymore...
575 if (!IsEmptyStr(CCC->net_node)) {
576 CtdlNetworkTalkingTo(CCC->net_node, strlen(CCC->net_node), NTT_REMOVE);
577 CCC->net_node[0] = '\0';
580 void network_cleanup_function(void)
582 struct CitContext *CCC = CC;
584 if (!IsEmptyStr(CCC->net_node)) {
585 CtdlNetworkTalkingTo(CCC->net_node, strlen(CCC->net_node), NTT_REMOVE);
586 CCC->net_node[0] = '\0';
595 void SetNetQDebugEnabled(const int n)
597 NetQDebugEnabled = n;
600 CTDL_MODULE_INIT(network)
604 CtdlFillSystemContext(&networker_spool_CC, "CitNetSpool");
605 CtdlRegisterDebugFlagHook(HKEY("networkqueue"), SetNetQDebugEnabled, &NetQDebugEnabled);
606 CtdlRegisterSessionHook(network_cleanup_function, EVT_STOP, PRIO_STOP + 30);
607 CtdlRegisterSessionHook(network_logout_hook, EVT_LOGOUT, PRIO_LOGOUT + 10);
608 CtdlRegisterProtoHook(cmd_nsyn, "NSYN", "Synchronize room to node");
609 CtdlRegisterRoomHook(network_room_handler);
610 CtdlRegisterCleanupHook(destroy_network_queue_room_locked);
611 CtdlRegisterSessionHook(network_do_queue, EVT_TIMER, PRIO_QUEUE + 10);