2 * This module handles shared rooms, inter-Citadel mail, and outbound
3 * mailing list processing.
5 * Copyright (c) 2000-2011 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 as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * ** NOTE ** A word on the S_NETCONFIGS semaphore:
22 * This is a fairly high-level type of critical section. It ensures that no
23 * two threads work on the netconfigs files at the same time. Since we do
24 * so many things inside these, here are the rules:
25 * 1. begin_critical_section(S_NETCONFIGS) *before* begin_ any others.
26 * 2. Do *not* perform any I/O with the client during these sections.
31 * Duration of time (in seconds) after which pending list subscribe/unsubscribe
32 * requests that have not been confirmed will be deleted.
34 #define EXP 259200 /* three days */
46 #include <sys/types.h>
48 #if TIME_WITH_SYS_TIME
49 # include <sys/time.h>
53 # include <sys/time.h>
61 # if HAVE_SYS_SYSCALL_H
62 # include <sys/syscall.h>
69 #include <libcitadel.h>
72 #include "citserver.h"
78 #include "internet_addressing.h"
79 #include "serv_network.h"
80 #include "clientsocket.h"
82 #include "citadel_dirs.h"
90 #include "netconfig.h"
92 #include "ctdl_module.h"
97 * When we do network processing, it's accomplished in two passes; one to
98 * gather a list of rooms and one to actually do them. It's ok that rplist
99 * is global; we have a mutex that keeps it safe.
101 struct RoomProcList *rplist = NULL;
110 * Check the use table. This is a list of messages which have recently
111 * arrived on the system. It is maintained and queried to prevent the same
112 * message from being entered into the database multiple times if it happens
113 * to arrive multiple times by accident.
115 int network_usetable(struct CtdlMessage *msg) {
118 struct cdbdata *cdbut;
121 /* Bail out if we can't generate a message ID */
125 if (msg->cm_fields['I'] == NULL) {
128 if (IsEmptyStr(msg->cm_fields['I'])) {
132 /* Generate the message ID */
133 strcpy(msgid, msg->cm_fields['I']);
134 if (haschar(msgid, '@') == 0) {
136 if (msg->cm_fields['N'] != NULL) {
137 strcat(msgid, msg->cm_fields['N']);
144 cdbut = cdb_fetch(CDB_USETABLE, msgid, strlen(msgid));
147 syslog(LOG_DEBUG, "network_usetable() : we already have %s\n", msgid);
151 /* If we got to this point, it's unique: add it. */
152 strcpy(ut.ut_msgid, msgid);
153 ut.ut_timestamp = time(NULL);
154 cdb_store(CDB_USETABLE, msgid, strlen(msgid), &ut, sizeof(struct UseTable) );
160 * Send the *entire* contents of the current room to one specific network node,
161 * ignoring anything we know about which messages have already undergone
162 * network processing. This can be used to bring a new node into sync.
164 int network_sync_to(char *target_node) {
172 char filename[PATH_MAX];
175 /* Grab the configuration line we're looking for */
176 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
177 begin_critical_section(S_NETCONFIGS);
178 fp = fopen(filename, "r");
180 end_critical_section(S_NETCONFIGS);
183 while (fgets(buf, sizeof buf, fp) != NULL) {
184 buf[strlen(buf)-1] = 0;
185 extract_token(sc_type, buf, 0, '|', sizeof sc_type);
186 extract_token(sc_node, buf, 1, '|', sizeof sc_node);
187 extract_token(sc_room, buf, 2, '|', sizeof sc_room);
188 if ( (!strcasecmp(sc_type, "ignet_push_share"))
189 && (!strcasecmp(sc_node, target_node)) ) {
192 /* Concise syntax because we don't need a full linked-list */
193 memset(&sc, 0, sizeof(SpoolControl));
194 sc.ignet_push_shares = (maplist *)
195 malloc(sizeof(maplist));
196 sc.ignet_push_shares->next = NULL;
197 safestrncpy(sc.ignet_push_shares->remote_nodename,
199 sizeof sc.ignet_push_shares->remote_nodename);
200 safestrncpy(sc.ignet_push_shares->remote_roomname,
202 sizeof sc.ignet_push_shares->remote_roomname);
206 end_critical_section(S_NETCONFIGS);
208 if (!found_node) return(-1);
210 /* Send ALL messages */
211 num_spooled = CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL,
212 network_spool_msg, &sc);
214 /* Concise cleanup because we know there's only one node in the sc */
215 free(sc.ignet_push_shares);
217 syslog(LOG_NOTICE, "Synchronized %d messages to <%s>\n",
218 num_spooled, target_node);
224 * Implements the NSYN command
226 void cmd_nsyn(char *argbuf) {
228 char target_node[256];
230 if (CtdlAccessCheck(ac_aide)) return;
232 extract_token(target_node, argbuf, 0, '|', sizeof target_node);
233 num_spooled = network_sync_to(target_node);
234 if (num_spooled >= 0) {
235 cprintf("%d Spooled %d messages.\n", CIT_OK, num_spooled);
238 cprintf("%d No such room/node share exists.\n",
239 ERROR + ROOM_NOT_FOUND);
246 * Batch up and send all outbound traffic from the current room
248 void network_queue_room(struct ctdlroom *qrbuf, void *data) {
249 struct RoomProcList *ptr;
251 ptr = (struct RoomProcList *) malloc(sizeof (struct RoomProcList));
252 if (ptr == NULL) return;
254 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
255 begin_critical_section(S_RPLIST);
258 end_critical_section(S_RPLIST);
261 void destroy_network_queue_room(void)
263 struct RoomProcList *cur, *p;
267 begin_critical_section(S_RPLIST);
275 end_critical_section(S_RPLIST);
278 while (nmcur != NULL)
285 if (working_ignetcfg != NULL)
286 free (working_ignetcfg);
287 working_ignetcfg = NULL;
295 * Bounce a message back to the sender
297 void network_bounce(struct CtdlMessage *msg, char *reason) {
298 char *oldpath = NULL;
300 char bouncesource[SIZ];
302 struct recptypes *valid = NULL;
303 char force_room[ROOMNAMELEN];
304 static int serialnum = 0;
307 syslog(LOG_DEBUG, "entering network_bounce()\n");
309 if (msg == NULL) return;
311 snprintf(bouncesource, sizeof bouncesource, "%s@%s", BOUNCESOURCE, config.c_nodename);
314 * Give it a fresh message ID
316 if (msg->cm_fields['I'] != NULL) {
317 free(msg->cm_fields['I']);
319 snprintf(buf, sizeof buf, "%ld.%04lx.%04x@%s",
320 (long)time(NULL), (long)getpid(), ++serialnum, config.c_fqdn);
321 msg->cm_fields['I'] = strdup(buf);
324 * FIXME ... right now we're just sending a bounce; we really want to
325 * include the text of the bounced message.
327 if (msg->cm_fields['M'] != NULL) {
328 free(msg->cm_fields['M']);
330 msg->cm_fields['M'] = strdup(reason);
331 msg->cm_format_type = 0;
334 * Turn the message around
336 if (msg->cm_fields['R'] == NULL) {
337 free(msg->cm_fields['R']);
340 if (msg->cm_fields['D'] == NULL) {
341 free(msg->cm_fields['D']);
344 snprintf(recipient, sizeof recipient, "%s@%s",
345 msg->cm_fields['A'], msg->cm_fields['N']);
347 if (msg->cm_fields['A'] == NULL) {
348 free(msg->cm_fields['A']);
351 if (msg->cm_fields['N'] == NULL) {
352 free(msg->cm_fields['N']);
355 if (msg->cm_fields['U'] == NULL) {
356 free(msg->cm_fields['U']);
359 msg->cm_fields['A'] = strdup(BOUNCESOURCE);
360 msg->cm_fields['N'] = strdup(config.c_nodename);
361 msg->cm_fields['U'] = strdup("Delivery Status Notification (Failure)");
363 /* prepend our node to the path */
364 if (msg->cm_fields['P'] != NULL) {
365 oldpath = msg->cm_fields['P'];
366 msg->cm_fields['P'] = NULL;
369 oldpath = strdup("unknown_user");
371 size = strlen(oldpath) + SIZ;
372 msg->cm_fields['P'] = malloc(size);
373 snprintf(msg->cm_fields['P'], size, "%s!%s", config.c_nodename, oldpath);
376 /* Now submit the message */
377 valid = validate_recipients(recipient, NULL, 0);
378 if (valid != NULL) if (valid->num_error != 0) {
379 free_recipients(valid);
382 if ( (valid == NULL) || (!strcasecmp(recipient, bouncesource)) ) {
383 strcpy(force_room, config.c_aideroom);
386 strcpy(force_room, "");
388 if ( (valid == NULL) && IsEmptyStr(force_room) ) {
389 strcpy(force_room, config.c_aideroom);
391 CtdlSubmitMsg(msg, valid, force_room, 0);
394 if (valid != NULL) free_recipients(valid);
395 CtdlFreeMessage(msg);
396 syslog(LOG_DEBUG, "leaving network_bounce()\n");
403 * Run through the rooms doing various types of network stuff.
405 void network_do_queue(void) {
406 static int doing_queue = 0;
407 static time_t last_run = 0L;
408 struct RoomProcList *ptr;
409 int full_processing = 1;
412 * Run the full set of processing tasks no more frequently
413 * than once every n seconds
415 if ( (time(NULL) - last_run) < config.c_net_freq ) {
417 syslog(LOG_DEBUG, "Network full processing in %ld seconds.\n",
418 config.c_net_freq - (time(NULL)- last_run)
423 * This is a simple concurrency check to make sure only one queue run
424 * is done at a time. We could do this with a mutex, but since we
425 * don't really require extremely fine granularity here, we'll do it
426 * with a static variable instead.
433 /* Load the IGnet Configuration into memory */
434 load_working_ignetcfg();
438 * Load the network map and filter list into memory.
441 filterlist = load_filter_list();
444 * Go ahead and run the queue
446 if (full_processing && !server_shutting_down) {
447 syslog(LOG_DEBUG, "network: loading outbound queue\n");
448 CtdlForEachRoom(network_queue_room, NULL);
451 if (rplist != NULL) {
452 syslog(LOG_DEBUG, "network: running outbound queue\n");
453 while (rplist != NULL && !server_shutting_down) {
454 char spoolroomname[ROOMNAMELEN];
455 safestrncpy(spoolroomname, rplist->name, sizeof spoolroomname);
456 begin_critical_section(S_RPLIST);
458 /* pop this record off the list */
460 rplist = rplist->next;
463 /* invalidate any duplicate entries to prevent double processing */
464 for (ptr=rplist; ptr!=NULL; ptr=ptr->next) {
465 if (!strcasecmp(ptr->name, spoolroomname)) {
470 end_critical_section(S_RPLIST);
471 if (spoolroomname[0] != 0) {
472 network_spoolout_room(spoolroomname);
477 /* If there is anything in the inbound queue, process it */
478 if (!server_shutting_down) {
479 network_do_spoolin();
482 /* Save the network map back to disk */
485 /* Free the filter list in memory */
486 free_filter_list(filterlist);
489 network_consolidate_spoolout();
491 syslog(LOG_DEBUG, "network: queue run completed\n");
493 if (full_processing) {
494 last_run = time(NULL);
503 int network_room_handler (struct ctdlroom *room)
505 network_queue_room(room, NULL);
513 CTDL_MODULE_INIT(network)
517 CtdlRegisterProtoHook(cmd_nsyn, "NSYN", "Synchronize room to node");
518 CtdlRegisterRoomHook(network_room_handler);
519 CtdlRegisterCleanupHook(destroy_network_queue_room);
520 CtdlRegisterSessionHook(network_do_queue, EVT_TIMER);