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;
100 int GetNetworkedRoomNumbers(const char *DirName, HashList *DirList)
104 struct dirent *filedir_entry;
108 filedir = opendir (DirName);
109 if (filedir == NULL) {
113 d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 1);
118 while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
119 (filedir_entry != NULL))
121 RoomNR = atol(filedir_entry->d_name);
124 Put(DirList, LKEY(RoomNR), &Count, reference_free_handler);
136 * Check the use table. This is a list of messages which have recently
137 * arrived on the system. It is maintained and queried to prevent the same
138 * message from being entered into the database multiple times if it happens
139 * to arrive multiple times by accident.
141 int network_usetable(struct CtdlMessage *msg)
143 struct CitContext *CCC = CC;
145 struct cdbdata *cdbut;
148 /* Bail out if we can't generate a message ID */
152 if (msg->cm_fields['I'] == NULL) {
155 if (IsEmptyStr(msg->cm_fields['I'])) {
159 /* Generate the message ID */
160 strcpy(msgid, msg->cm_fields['I']);
161 if (haschar(msgid, '@') == 0) {
163 if (msg->cm_fields['N'] != NULL) {
164 strcat(msgid, msg->cm_fields['N']);
171 cdbut = cdb_fetch(CDB_USETABLE, msgid, strlen(msgid));
174 QN_syslog(LOG_DEBUG, "network_usetable() : we already have %s\n", msgid);
178 /* If we got to this point, it's unique: add it. */
179 strcpy(ut.ut_msgid, msgid);
180 ut.ut_timestamp = time(NULL);
181 cdb_store(CDB_USETABLE, msgid, strlen(msgid), &ut, sizeof(struct UseTable) );
188 * Send the *entire* contents of the current room to one specific network node,
189 * ignoring anything we know about which messages have already undergone
190 * network processing. This can be used to bring a new node into sync.
192 int network_sync_to(char *target_node, long len)
194 struct CitContext *CCC = CC;
195 const OneRoomNetCfg *OneRNCFG;
196 const RoomNetCfgLine *pCfgLine;
200 /* Grab the configuration line we're looking for */
201 begin_critical_section(S_NETCONFIGS);
202 OneRNCFG = CtdlGetNetCfgForRoom(CCC->room.QRnumber);
203 if ((OneRNCFG == NULL) ||
204 (OneRNCFG->NetConfigs[ignet_push_share] == NULL))
209 pCfgLine = OneRNCFG->NetConfigs[ignet_push_share];
210 while (pCfgLine != NULL)
212 if (strcmp(ChrPtr(pCfgLine->Value[0]), target_node))
214 pCfgLine = pCfgLine->next;
216 if (pCfgLine == NULL)
220 memset(&sc, 0, sizeof(SpoolControl));
222 sc.NetConfigs[ignet_push_share] = DuplicateOneGenericCfgLine(pCfgLine);
224 end_critical_section(S_NETCONFIGS);
226 sc.working_ignetcfg = CtdlLoadIgNetCfg();
227 sc.the_netmap = CtdlReadNetworkMap();
229 /* Send ALL messages */
230 num_spooled = CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL,
231 network_spool_msg, &sc);
233 /* Concise cleanup because we know there's only one node in the sc */
234 DeleteGenericCfgLine(NULL/*TODO*/, &sc.NetConfigs[ignet_push_share]);
236 DeleteHash(&sc.working_ignetcfg);
237 DeleteHash(&sc.the_netmap);
239 QN_syslog(LOG_NOTICE, "Synchronized %d messages to <%s>\n",
240 num_spooled, target_node);
246 * Implements the NSYN command
248 void cmd_nsyn(char *argbuf) {
251 char target_node[256];
253 if (CtdlAccessCheck(ac_aide)) return;
255 len = extract_token(target_node, argbuf, 0, '|', sizeof target_node);
256 num_spooled = network_sync_to(target_node, len);
257 if (num_spooled >= 0) {
258 cprintf("%d Spooled %d messages.\n", CIT_OK, num_spooled);
261 cprintf("%d No such room/node share exists.\n",
262 ERROR + ROOM_NOT_FOUND);
269 * Batch up and send all outbound traffic from the current room
271 void network_queue_interesting_rooms(struct ctdlroom *qrbuf, void *data) {
273 struct RoomProcList *ptr;
274 long QRNum = qrbuf->QRnumber;
276 roomlists *RP = (roomlists*) data;
278 if (!GetHash(RP->RoomsInterestedIn, LKEY(QRNum), &v))
281 ptr = (struct RoomProcList *) malloc(sizeof (struct RoomProcList));
282 if (ptr == NULL) return;
284 ptr->namelen = strlen(qrbuf->QRname);
285 if (ptr->namelen > ROOMNAMELEN)
286 ptr->namelen = ROOMNAMELEN - 1;
288 memcpy (ptr->name, qrbuf->QRname, ptr->namelen);
289 ptr->name[ptr->namelen] = '\0';
290 ptr->QRNum = qrbuf->QRnumber;
292 for (i = 0; i < ptr->namelen; i++)
294 ptr->lcname[i] = tolower(ptr->name[i]);
297 ptr->lcname[ptr->namelen] = '\0';
298 ptr->key = hashlittle(ptr->lcname, ptr->namelen, 9872345);
299 ptr->next = RP->rplist;
304 * Batch up and send all outbound traffic from the current room
306 void network_queue_room(struct ctdlroom *qrbuf, void *data) {
308 struct RoomProcList *ptr;
310 if (qrbuf->QRdefaultview == VIEW_QUEUE)
312 ptr = (struct RoomProcList *) malloc(sizeof (struct RoomProcList));
313 if (ptr == NULL) return;
315 ptr->namelen = strlen(qrbuf->QRname);
316 if (ptr->namelen > ROOMNAMELEN)
317 ptr->namelen = ROOMNAMELEN - 1;
319 memcpy (ptr->name, qrbuf->QRname, ptr->namelen);
320 ptr->name[ptr->namelen] = '\0';
321 ptr->QRNum = qrbuf->QRnumber;
323 for (i = 0; i < ptr->namelen; i++)
325 ptr->lcname[i] = tolower(ptr->name[i]);
327 ptr->lcname[ptr->namelen] = '\0';
328 ptr->key = hashlittle(ptr->lcname, ptr->namelen, 9872345);
330 begin_critical_section(S_RPLIST);
333 end_critical_section(S_RPLIST);
336 void destroy_network_queue_room(RoomProcList *rplist)
338 struct RoomProcList *cur, *p;
349 void destroy_network_queue_room_locked (void)
351 begin_critical_section(S_RPLIST);
352 destroy_network_queue_room(rplist);
353 end_critical_section(S_RPLIST);
359 * Bounce a message back to the sender
361 void network_bounce(struct CtdlMessage *msg, char *reason)
363 struct CitContext *CCC = CC;
364 char *oldpath = NULL;
366 char bouncesource[SIZ];
368 struct recptypes *valid = NULL;
369 char force_room[ROOMNAMELEN];
370 static int serialnum = 0;
373 QNM_syslog(LOG_DEBUG, "entering network_bounce()\n");
375 if (msg == NULL) return;
377 snprintf(bouncesource, sizeof bouncesource, "%s@%s", BOUNCESOURCE, config.c_nodename);
380 * Give it a fresh message ID
382 if (msg->cm_fields['I'] != NULL) {
383 free(msg->cm_fields['I']);
385 snprintf(buf, sizeof buf, "%ld.%04lx.%04x@%s",
386 (long)time(NULL), (long)getpid(), ++serialnum, config.c_fqdn);
387 msg->cm_fields['I'] = strdup(buf);
390 * FIXME ... right now we're just sending a bounce; we really want to
391 * include the text of the bounced message.
393 if (msg->cm_fields['M'] != NULL) {
394 free(msg->cm_fields['M']);
396 msg->cm_fields['M'] = strdup(reason);
397 msg->cm_format_type = 0;
400 * Turn the message around
402 if (msg->cm_fields['R'] == NULL) {
403 free(msg->cm_fields['R']);
406 if (msg->cm_fields['D'] == NULL) {
407 free(msg->cm_fields['D']);
410 snprintf(recipient, sizeof recipient, "%s@%s",
411 msg->cm_fields['A'], msg->cm_fields['N']);
413 if (msg->cm_fields['A'] == NULL) {
414 free(msg->cm_fields['A']);
417 if (msg->cm_fields['N'] == NULL) {
418 free(msg->cm_fields['N']);
421 if (msg->cm_fields['U'] == NULL) {
422 free(msg->cm_fields['U']);
425 msg->cm_fields['A'] = strdup(BOUNCESOURCE);
426 msg->cm_fields['N'] = strdup(config.c_nodename);
427 msg->cm_fields['U'] = strdup("Delivery Status Notification (Failure)");
429 /* prepend our node to the path */
430 if (msg->cm_fields['P'] != NULL) {
431 oldpath = msg->cm_fields['P'];
432 msg->cm_fields['P'] = NULL;
435 oldpath = strdup("unknown_user");
437 size = strlen(oldpath) + SIZ;
438 msg->cm_fields['P'] = malloc(size);
439 snprintf(msg->cm_fields['P'], size, "%s!%s", config.c_nodename, oldpath);
442 /* Now submit the message */
443 valid = validate_recipients(recipient, NULL, 0);
444 if (valid != NULL) if (valid->num_error != 0) {
445 free_recipients(valid);
448 if ( (valid == NULL) || (!strcasecmp(recipient, bouncesource)) ) {
449 strcpy(force_room, config.c_aideroom);
452 strcpy(force_room, "");
454 if ( (valid == NULL) && IsEmptyStr(force_room) ) {
455 strcpy(force_room, config.c_aideroom);
457 CtdlSubmitMsg(msg, valid, force_room, 0);
460 if (valid != NULL) free_recipients(valid);
461 CtdlFreeMessage(msg);
462 QNM_syslog(LOG_DEBUG, "leaving network_bounce()\n");
474 * Run through the rooms doing various types of network stuff.
476 void network_do_queue(void)
478 struct CitContext *CCC = CC;
479 static int doing_queue = 0;
480 static time_t last_run = 0L;
481 int full_processing = 1;
482 HashList *working_ignetcfg;
483 HashList *the_netmap = NULL;
484 int netmap_changed = 0;
488 * Run the full set of processing tasks no more frequently
489 * than once every n seconds
491 if ( (time(NULL) - last_run) < config.c_net_freq ) {
493 syslog(LOG_DEBUG, "Network full processing in %ld seconds.\n",
494 config.c_net_freq - (time(NULL)- last_run)
499 * This is a simple concurrency check to make sure only one queue run
500 * is done at a time. We could do this with a mutex, but since we
501 * don't really require extremely fine granularity here, we'll do it
502 * with a static variable instead.
509 become_session(&networker_spool_CC);
510 begin_critical_section(S_RPLIST);
513 end_critical_section(S_RPLIST);
515 RL.RoomsInterestedIn = NewHash(1, lFlathash);
516 if (full_processing &&
517 (GetNetworkedRoomNumbers(ctdl_netcfg_dir, RL.RoomsInterestedIn)==0))
520 DeleteHash(&RL.RoomsInterestedIn);
521 if (RL.rplist == NULL)
524 /* Load the IGnet Configuration into memory */
525 working_ignetcfg = CtdlLoadIgNetCfg();
528 * Load the network map and filter list into memory.
530 if (!server_shutting_down)
531 the_netmap = CtdlReadNetworkMap();
532 if (!server_shutting_down)
533 load_network_filter_list();
536 * Go ahead and run the queue
538 if (full_processing && !server_shutting_down) {
539 QNM_syslog(LOG_DEBUG, "network: loading outbound queue");
540 CtdlForEachRoom(network_queue_interesting_rooms, &RL);
543 if ((RL.rplist != NULL) && (!server_shutting_down)) {
544 RoomProcList *ptr, *cmp;
546 QNM_syslog(LOG_DEBUG, "network: running outbound queue");
547 while (ptr != NULL && !server_shutting_down) {
551 while (cmp != NULL) {
552 if ((cmp->namelen > 0) &&
553 (cmp->key == ptr->key) &&
554 (cmp->namelen == ptr->namelen) &&
555 (strcmp(cmp->lcname, ptr->lcname) == 0))
562 if (ptr->namelen > 0) {
563 network_spoolout_room(ptr,
571 /* If there is anything in the inbound queue, process it */
572 if (!server_shutting_down) {
573 network_do_spoolin(working_ignetcfg,
578 /* Free the filter list in memory */
579 free_netfilter_list();
581 /* Save the network map back to disk */
582 if (netmap_changed) {
583 StrBuf *MapStr = CtdlSerializeNetworkMap(the_netmap);
584 CtdlPutSysConfig(IGNETMAP, SmashStrBuf(&MapStr));
587 /* combine singe message files into one spool entry per remote node. */
588 network_consolidate_spoolout(working_ignetcfg, the_netmap);
592 DeleteHash(&the_netmap);
594 DeleteHash(&working_ignetcfg);
596 QNM_syslog(LOG_DEBUG, "network: queue run completed");
598 if (full_processing) {
599 last_run = time(NULL);
601 DeleteHash(&RL.RoomsInterestedIn);
602 destroy_network_queue_room(RL.rplist);
609 int network_room_handler (struct ctdlroom *room)
611 network_queue_room(room, NULL);
618 void network_logout_hook(void)
620 CitContext *CCC = MyContext();
623 * If we were talking to a network node, we're not anymore...
625 if (!IsEmptyStr(CCC->net_node)) {
626 CtdlNetworkTalkingTo(CCC->net_node, strlen(CCC->net_node), NTT_REMOVE);
627 CCC->net_node[0] = '\0';
630 void network_cleanup_function(void)
632 struct CitContext *CCC = CC;
634 if (!IsEmptyStr(CCC->net_node)) {
635 CtdlNetworkTalkingTo(CCC->net_node, strlen(CCC->net_node), NTT_REMOVE);
636 CCC->net_node[0] = '\0';
645 void SetNetQDebugEnabled(const int n)
647 NetQDebugEnabled = n;
650 CTDL_MODULE_INIT(network)
654 CtdlFillSystemContext(&networker_spool_CC, "CitNetSpool");
655 CtdlRegisterDebugFlagHook(HKEY("networkqueue"), SetNetQDebugEnabled, &NetQDebugEnabled);
656 CtdlRegisterSessionHook(network_cleanup_function, EVT_STOP, PRIO_STOP + 30);
657 CtdlRegisterSessionHook(network_logout_hook, EVT_LOGOUT, PRIO_LOGOUT + 10);
658 CtdlRegisterProtoHook(cmd_nsyn, "NSYN", "Synchronize room to node");
659 CtdlRegisterRoomHook(network_room_handler);
660 CtdlRegisterCleanupHook(destroy_network_queue_room_locked);
661 CtdlRegisterSessionHook(network_do_queue, EVT_TIMER, PRIO_QUEUE + 10);