4 * This module will eventually replace netproc and some of its utilities. In
5 * the meantime, it serves as a mailing list manager.
7 * Copyright (C) 2000-2001 by Art Cancro and others.
8 * This code is released under the terms of the GNU General Public License.
20 #include <sys/types.h>
22 #if TIME_WITH_SYS_TIME
23 # include <sys/time.h>
27 # include <sys/time.h>
38 #include "sysdep_decls.h"
39 #include "citserver.h"
42 #include "dynloader.h"
49 #include "internet_addressing.h"
50 #include "serv_network.h"
54 * When we do network processing, it's accomplished in two passes; one to
55 * gather a list of rooms and one to actually do them. It's ok that rplist
56 * is global; this process *only* runs as part of the housekeeping loop and
57 * therefore only one will run at a time.
60 struct RoomProcList *next;
61 char name[ROOMNAMELEN];
64 struct RoomProcList *rplist = NULL;
68 * We build a map of the Citadel network during network runs.
77 struct NetMap *the_netmap = NULL;
82 * Read the network map from its configuration file into memory.
84 void read_network_map(void) {
85 char *serialized_map = NULL;
90 serialized_map = CtdlGetSysConfig(IGNETMAP);
91 if (serialized_map == NULL) return; /* if null, no entries */
93 /* Use the string tokenizer to grab one line at a time */
94 for (i=0; i<num_tokens(serialized_map, '\n'); ++i) {
95 extract_token(buf, serialized_map, i, '\n');
96 nmptr = (struct NetMap *) mallok(sizeof(struct NetMap));
97 extract(nmptr->nodename, buf, 0);
98 nmptr->lastcontact = extract_long(buf, 1);
99 extract(nmptr->nexthop, buf, 2);
100 nmptr->next = the_netmap;
104 phree(serialized_map);
109 * Write the network map from memory back to the configuration file.
111 void write_network_map(void) {
112 char *serialized_map = NULL;
113 struct NetMap *nmptr;
115 serialized_map = strdoop("");
117 if (the_netmap != NULL) {
118 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
119 serialized_map = reallok(serialized_map,
120 (strlen(serialized_map)+SIZ) );
121 if (strlen(nmptr->nodename) > 0) {
122 sprintf(&serialized_map[strlen(serialized_map)],
131 CtdlPutSysConfig(IGNETMAP, serialized_map);
132 phree(serialized_map);
134 /* Now free the list */
135 while (the_netmap != NULL) {
136 nmptr = the_netmap->next;
146 void cmd_gnet(char *argbuf) {
151 if (CtdlAccessCheck(ac_room_aide)) return;
152 assoc_file_name(filename, &CC->quickroom, "netconfigs");
153 cprintf("%d Network settings for room #%ld <%s>\n",
155 CC->quickroom.QRnumber, CC->quickroom.QRname);
157 fp = fopen(filename, "r");
159 while (fgets(buf, sizeof buf, fp) != NULL) {
160 buf[strlen(buf)-1] = 0;
161 cprintf("%s\n", buf);
170 void cmd_snet(char *argbuf) {
171 char tempfilename[SIZ];
176 if (CtdlAccessCheck(ac_room_aide)) return;
177 safestrncpy(tempfilename, tmpnam(NULL), sizeof tempfilename);
178 assoc_file_name(filename, &CC->quickroom, "netconfigs");
180 fp = fopen(tempfilename, "w");
182 cprintf("%d Cannot open %s: %s\n",
183 ERROR+INTERNAL_ERROR,
188 cprintf("%d %s\n", SEND_LISTING, tempfilename);
189 while (client_gets(buf), strcmp(buf, "000")) {
190 fprintf(fp, "%s\n", buf);
194 /* Now copy the temp file to its permanent location
195 * (We use /bin/mv instead of link() because they may be on
196 * different filesystems)
199 snprintf(buf, sizeof buf, "/bin/mv %s %s", tempfilename, filename);
206 * Spools out one message from the list.
208 void network_spool_msg(long msgnum, void *userdata) {
209 struct SpoolControl *sc;
210 struct namelist *nptr;
214 char *newpath = NULL;
215 size_t instr_len = SIZ;
216 struct CtdlMessage *msg;
217 struct CtdlMessage *imsg;
218 struct ser_ret sermsg;
224 int delete_after_send = 0; /* Set to 1 to delete after spooling */
226 sc = (struct SpoolControl *)userdata;
229 * Process mailing list recipients
231 if (sc->listrecps != NULL) {
233 /* First, copy it to the spoolout room */
234 err = CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, msgnum, 0);
235 if (err != 0) return;
238 * Figure out how big a buffer we need to allocate
240 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
241 instr_len = instr_len + strlen(nptr->name);
247 lprintf(9, "Generating delivery instructions\n");
248 instr = mallok(instr_len);
250 lprintf(1, "Cannot allocate %d bytes for instr...\n",
255 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
256 "bounceto|postmaster@%s\n" ,
257 SPOOLMIME, msgnum, time(NULL), config.c_fqdn );
259 /* Generate delivery instructions for each recipient */
260 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
261 sprintf(&instr[strlen(instr)], "remote|%s|0||\n",
266 * Generate a message from the instructions
268 imsg = mallok(sizeof(struct CtdlMessage));
269 memset(imsg, 0, sizeof(struct CtdlMessage));
270 imsg->cm_magic = CTDLMESSAGE_MAGIC;
271 imsg->cm_anon_type = MES_NORMAL;
272 imsg->cm_format_type = FMT_RFC822;
273 imsg->cm_fields['A'] = strdoop("Citadel");
274 imsg->cm_fields['M'] = instr;
276 /* Save delivery instructions in spoolout room */
277 CtdlSaveMsg(imsg, "", SMTP_SPOOLOUT_ROOM, MES_LOCAL);
278 CtdlFreeMessage(imsg);
282 * Process IGnet push shares
284 if (sc->ignet_push_shares != NULL) {
286 msg = CtdlFetchMessage(msgnum);
289 /* Prepend our node name to the Path field whenever
290 * sending a message to another IGnet node
292 if (msg->cm_fields['P'] == NULL) {
293 msg->cm_fields['P'] = strdoop("username");
295 newpath = mallok(strlen(msg->cm_fields['P']) +
296 strlen(config.c_nodename) + 2);
297 sprintf(newpath, "%s!%s", config.c_nodename,
298 msg->cm_fields['P']);
299 phree(msg->cm_fields['P']);
300 msg->cm_fields['P'] = newpath;
303 * Force the message to appear in the correct room
304 * on the far end by setting the C field correctly
306 if (msg->cm_fields['C'] != NULL) {
307 phree(msg->cm_fields['C']);
309 msg->cm_fields['C'] = strdoop(CC->quickroom.QRname);
312 * Determine if this message is set to be deleted
313 * after sending out on the network
315 if (msg->cm_fields['S'] != NULL) {
316 if (!strcasecmp(msg->cm_fields['S'],
318 delete_after_send = 1;
323 * Now serialize it for transmission
325 serialize_message(&sermsg, msg);
326 CtdlFreeMessage(msg);
328 /* Now send it to every node */
329 for (nptr = sc->ignet_push_shares; nptr != NULL;
334 /* FIXME check for valid node name */
336 /* Check for split horizon */
337 bang = num_tokens(msg->cm_fields['P'], '!');
338 if (bang > 1) for (i=0; i<(bang-1); ++i) {
339 extract_token(buf, msg->cm_fields['P'],
341 if (!strcasecmp(buf, nptr->name)) {
346 /* Send the message */
349 "./network/spoolout/%s",
351 fp = fopen(filename, "ab");
362 /* update lastsent */
363 sc->lastsent = msgnum;
365 /* Delete this message if delete-after-send is set */
366 if (delete_after_send) {
367 CtdlDeleteMessages(CC->quickroom.QRname, msgnum, "");
376 * Batch up and send all outbound traffic from the current room
378 void network_spoolout_room(char *room_to_spool) {
383 struct SpoolControl sc;
384 /* struct namelist *digestrecps = NULL; */
385 struct namelist *nptr;
387 lprintf(7, "Spooling <%s>\n", room_to_spool);
388 if (getroom(&CC->quickroom, room_to_spool) != 0) {
389 lprintf(1, "ERROR: cannot load <%s>\n", room_to_spool);
393 memset(&sc, 0, sizeof(struct SpoolControl));
394 assoc_file_name(filename, &CC->quickroom, "netconfigs");
396 fp = fopen(filename, "r");
398 lprintf(7, "Outbound batch processing skipped for <%s>\n",
399 CC->quickroom.QRname);
403 lprintf(5, "Outbound batch processing started for <%s>\n",
404 CC->quickroom.QRname);
406 while (fgets(buf, sizeof buf, fp) != NULL) {
407 buf[strlen(buf)-1] = 0;
409 extract(instr, buf, 0);
410 if (!strcasecmp(instr, "lastsent")) {
411 sc.lastsent = extract_long(buf, 1);
413 else if (!strcasecmp(instr, "listrecp")) {
414 nptr = (struct namelist *)
415 mallok(sizeof(struct namelist));
416 nptr->next = sc.listrecps;
417 extract(nptr->name, buf, 1);
420 else if (!strcasecmp(instr, "ignet_push_share")) {
421 nptr = (struct namelist *)
422 mallok(sizeof(struct namelist));
423 nptr->next = sc.ignet_push_shares;
424 extract(nptr->name, buf, 1);
425 sc.ignet_push_shares = nptr;
433 /* Do something useful */
434 CtdlForEachMessage(MSGS_GT, sc.lastsent, (-63), NULL, NULL,
435 network_spool_msg, &sc);
438 /* Now rewrite the config file */
439 fp = fopen(filename, "w");
441 lprintf(1, "ERROR: cannot open %s: %s\n",
442 filename, strerror(errno));
445 fprintf(fp, "lastsent|%ld\n", sc.lastsent);
447 /* Write out the listrecps while freeing from memory at the
448 * same time. Am I clever or what? :)
450 while (sc.listrecps != NULL) {
451 fprintf(fp, "listrecp|%s\n", sc.listrecps->name);
452 nptr = sc.listrecps->next;
456 while (sc.ignet_push_shares != NULL) {
457 fprintf(fp, "ignet_push_share|%s\n",
458 sc.ignet_push_shares->name);
459 nptr = sc.ignet_push_shares->next;
460 phree(sc.ignet_push_shares);
461 sc.ignet_push_shares = nptr;
467 lprintf(5, "Outbound batch processing finished for <%s>\n",
468 CC->quickroom.QRname);
473 * Batch up and send all outbound traffic from the current room
475 void network_queue_room(struct quickroom *qrbuf, void *data) {
476 struct RoomProcList *ptr;
478 ptr = (struct RoomProcList *) mallok(sizeof (struct RoomProcList));
479 if (ptr == NULL) return;
481 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
488 * Learn topology from path fields
490 void network_learn_topology(char *node, char *path) {
492 struct NetMap *nmptr;
496 if (num_tokens(path, '!') < 3) return;
497 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
498 if (!strcasecmp(nmptr->nodename, node)) {
499 extract_token(nmptr->nexthop, path, 0, '!');
500 nmptr->lastcontact = time(NULL);
505 /* If we got here then it's not in the map, so add it. */
506 nmptr = (struct NetMap *) mallok(sizeof (struct NetMap));
507 strcpy(nmptr->nodename, node);
508 nmptr->lastcontact = time(NULL);
509 extract_token(nmptr->nexthop, path, 0, '!');
510 nmptr->next = the_netmap;
517 * Process a buffer containing a single message from a single file
518 * from the inbound queue
520 void network_process_buffer(char *buffer, long size) {
521 struct CtdlMessage *msg;
525 struct usersupp tempUS;
528 msg = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
529 memset(msg, 0, sizeof(struct CtdlMessage));
530 msg->cm_magic = CTDLMESSAGE_MAGIC;
531 msg->cm_anon_type = buffer[1];
532 msg->cm_format_type = buffer[2];
534 for (pos = 3; pos < size; ++pos) {
536 msg->cm_fields[field] = strdoop(&buffer[pos+1]);
537 pos = pos + strlen(&buffer[(int)pos]);
540 /* Check for message routing */
541 if (msg->cm_fields['D'] != NULL) {
542 if (strcasecmp(msg->cm_fields['D'], config.c_nodename)) {
544 /* FIXME route the message, stupid */
549 /* Learn network topology from the path */
550 if ((msg->cm_fields['N'] != NULL) && (msg->cm_fields['P'] != NULL)) {
551 network_learn_topology(msg->cm_fields['N'],
552 msg->cm_fields['P']);
555 /* Does it have a recipient? If so, validate it... */
556 if (msg->cm_fields['D'] != NULL) {
558 safestrncpy(recp, msg->cm_fields['D'], sizeof(recp));
560 e = alias(recp); /* alias and mail type */
561 if ((recp[0] == 0) || (e == MES_ERROR)) {
563 /* FIXME bounce the msg */
566 else if (e == MES_LOCAL) {
567 a = getuser(&tempUS, recp);
570 /* FIXME bounce the msg */
576 /* FIXME ... do something with it! */
578 CtdlFreeMessage(msg);
583 * Process a single message from a single file from the inbound queue
585 void network_process_message(FILE *fp, long msgstart, long msgend) {
590 hold_pos = ftell(fp);
591 size = msgend - msgstart + 1;
592 buffer = mallok(size);
593 if (buffer != NULL) {
594 fseek(fp, msgstart, SEEK_SET);
595 fread(buffer, size, 1, fp);
596 network_process_buffer(buffer, size);
600 fseek(fp, hold_pos, SEEK_SET);
605 * Process a single file from the inbound queue
607 void network_process_file(char *filename) {
609 long msgstart = (-1L);
614 lprintf(7, "network: processing <%s>\n", filename);
616 fp = fopen(filename, "rb");
618 lprintf(5, "Error opening %s: %s\n",
619 filename, strerror(errno));
623 /* Look for messages in the data stream and break them out */
624 while (ch = getc(fp), ch >= 0) {
627 if (msgstart >= 0L) {
629 network_process_message(fp, msgstart, msgend);
638 if (msgstart >= 0L) {
639 network_process_message(fp, msgstart, msgend);
643 /* unlink(filename); FIXME put back in */
648 * Process anything in the inbound queue
650 void network_do_spoolin(void) {
655 dp = opendir("./network/spoolin");
656 if (dp == NULL) return;
658 while (d = readdir(dp), d != NULL) {
659 sprintf(filename, "./network/spoolin/%s", d->d_name);
660 network_process_file(filename);
671 * Run through the rooms doing various types of network stuff.
673 void network_do_queue(void) {
674 static int doing_queue = 0;
675 static time_t last_run = 0L;
676 struct RoomProcList *ptr;
678 #define NETWORK_QUEUE_FREQUENCY 3600 /* one hour ... FIXME put in config */
680 * Run no more frequently than once every n seconds
682 if ( (time(NULL) - last_run) < NETWORK_QUEUE_FREQUENCY ) return;
685 * This is a simple concurrency check to make sure only one queue run
686 * is done at a time. We could do this with a mutex, but since we
687 * don't really require extremely fine granularity here, we'll do it
688 * with a static variable instead.
690 if (doing_queue) return;
692 last_run = time(NULL);
697 * Go ahead and run the queue
699 lprintf(7, "network: loading outbound queue\n");
700 ForEachRoom(network_queue_room, NULL);
702 lprintf(7, "network: running outbound queue\n");
703 while (rplist != NULL) {
704 network_spoolout_room(rplist->name);
706 rplist = rplist->next;
710 lprintf(7, "network: processing inbound queue\n");
711 network_do_spoolin();
715 lprintf(7, "network: queue run completed\n");
723 char *Dynamic_Module_Init(void)
725 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
726 CtdlRegisterProtoHook(cmd_snet, "SNET", "Get network config");
727 CtdlRegisterSessionHook(network_do_queue, EVT_TIMER);