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;
145 * Check the network map and determine whether the supplied node name is
146 * valid. If it is not a neighbor node, supply the name of a neighbor node
147 * which is the next hop.
149 int is_valid_node(char *nexthop, char *node) {
150 char *ignetcfg = NULL;
155 struct NetMap *nmptr;
162 * First try the neighbor nodes
164 ignetcfg = CtdlGetSysConfig(IGNETCFG);
165 if (ignetcfg == NULL) {
166 if (nexthop != NULL) {
173 if (nexthop != NULL) {
177 /* Use the string tokenizer to grab one line at a time */
178 for (i=0; i<num_tokens(ignetcfg, '\n'); ++i) {
179 extract_token(linebuf, ignetcfg, i, '\n');
180 extract(buf, linebuf, 0);
181 if (!strcasecmp(buf, node)) {
182 if (nexthop != NULL) {
191 return(retval); /* yup, it's a direct neighbor */
195 * If we get to this point we have to see if we know the next hop
197 if (the_netmap != NULL) {
198 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
199 if (!strcasecmp(nmptr->nodename, node)) {
200 if (nexthop != NULL) {
201 strcpy(nexthop, nmptr->nexthop);
209 * If we get to this point, the supplied node name is bogus.
211 lprintf(5, "Invalid node name <%s>\n", node);
219 void cmd_gnet(char *argbuf) {
224 if (CtdlAccessCheck(ac_room_aide)) return;
225 assoc_file_name(filename, &CC->quickroom, "netconfigs");
226 cprintf("%d Network settings for room #%ld <%s>\n",
228 CC->quickroom.QRnumber, CC->quickroom.QRname);
230 fp = fopen(filename, "r");
232 while (fgets(buf, sizeof buf, fp) != NULL) {
233 buf[strlen(buf)-1] = 0;
234 cprintf("%s\n", buf);
243 void cmd_snet(char *argbuf) {
244 char tempfilename[SIZ];
249 if (CtdlAccessCheck(ac_room_aide)) return;
250 safestrncpy(tempfilename, tmpnam(NULL), sizeof tempfilename);
251 assoc_file_name(filename, &CC->quickroom, "netconfigs");
253 fp = fopen(tempfilename, "w");
255 cprintf("%d Cannot open %s: %s\n",
256 ERROR+INTERNAL_ERROR,
261 cprintf("%d %s\n", SEND_LISTING, tempfilename);
262 while (client_gets(buf), strcmp(buf, "000")) {
263 fprintf(fp, "%s\n", buf);
267 /* Now copy the temp file to its permanent location
268 * (We use /bin/mv instead of link() because they may be on
269 * different filesystems)
272 snprintf(buf, sizeof buf, "/bin/mv %s %s", tempfilename, filename);
279 * Spools out one message from the list.
281 void network_spool_msg(long msgnum, void *userdata) {
282 struct SpoolControl *sc;
283 struct namelist *nptr;
287 char *newpath = NULL;
288 size_t instr_len = SIZ;
289 struct CtdlMessage *msg;
290 struct CtdlMessage *imsg;
291 struct ser_ret sermsg;
297 int delete_after_send = 0; /* Set to 1 to delete after spooling */
299 sc = (struct SpoolControl *)userdata;
302 * Process mailing list recipients
304 if (sc->listrecps != NULL) {
306 /* First, copy it to the spoolout room */
307 err = CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, msgnum, 0);
308 if (err != 0) return;
311 * Figure out how big a buffer we need to allocate
313 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
314 instr_len = instr_len + strlen(nptr->name);
320 lprintf(9, "Generating delivery instructions\n");
321 instr = mallok(instr_len);
323 lprintf(1, "Cannot allocate %d bytes for instr...\n",
328 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
329 "bounceto|postmaster@%s\n" ,
330 SPOOLMIME, msgnum, time(NULL), config.c_fqdn );
332 /* Generate delivery instructions for each recipient */
333 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
334 sprintf(&instr[strlen(instr)], "remote|%s|0||\n",
339 * Generate a message from the instructions
341 imsg = mallok(sizeof(struct CtdlMessage));
342 memset(imsg, 0, sizeof(struct CtdlMessage));
343 imsg->cm_magic = CTDLMESSAGE_MAGIC;
344 imsg->cm_anon_type = MES_NORMAL;
345 imsg->cm_format_type = FMT_RFC822;
346 imsg->cm_fields['A'] = strdoop("Citadel");
347 imsg->cm_fields['M'] = instr;
349 /* Save delivery instructions in spoolout room */
350 CtdlSaveMsg(imsg, "", SMTP_SPOOLOUT_ROOM, MES_LOCAL);
351 CtdlFreeMessage(imsg);
355 * Process IGnet push shares
357 if (sc->ignet_push_shares != NULL) {
359 msg = CtdlFetchMessage(msgnum);
362 /* Prepend our node name to the Path field whenever
363 * sending a message to another IGnet node
365 if (msg->cm_fields['P'] == NULL) {
366 msg->cm_fields['P'] = strdoop("username");
368 newpath = mallok(strlen(msg->cm_fields['P']) +
369 strlen(config.c_nodename) + 2);
370 sprintf(newpath, "%s!%s", config.c_nodename,
371 msg->cm_fields['P']);
372 phree(msg->cm_fields['P']);
373 msg->cm_fields['P'] = newpath;
376 * Force the message to appear in the correct room
377 * on the far end by setting the C field correctly
379 if (msg->cm_fields['C'] != NULL) {
380 phree(msg->cm_fields['C']);
382 msg->cm_fields['C'] = strdoop(CC->quickroom.QRname);
385 * Determine if this message is set to be deleted
386 * after sending out on the network
388 if (msg->cm_fields['S'] != NULL) {
389 if (!strcasecmp(msg->cm_fields['S'],
391 delete_after_send = 1;
396 * Now serialize it for transmission
398 serialize_message(&sermsg, msg);
399 CtdlFreeMessage(msg);
401 /* Now send it to every node */
402 for (nptr = sc->ignet_push_shares; nptr != NULL;
407 /* Check for valid node name */
408 if (is_valid_node(NULL, nptr->name) != 0) {
409 lprintf(3, "Invalid node <%s>\n",
414 /* Check for split horizon */
415 bang = num_tokens(msg->cm_fields['P'], '!');
416 if (bang > 1) for (i=0; i<(bang-1); ++i) {
417 extract_token(buf, msg->cm_fields['P'],
419 if (!strcasecmp(buf, nptr->name)) {
424 /* Send the message */
427 "./network/spoolout/%s",
429 fp = fopen(filename, "ab");
441 /* update lastsent */
442 sc->lastsent = msgnum;
444 /* Delete this message if delete-after-send is set */
445 if (delete_after_send) {
446 CtdlDeleteMessages(CC->quickroom.QRname, msgnum, "");
455 * Batch up and send all outbound traffic from the current room
457 void network_spoolout_room(char *room_to_spool) {
462 struct SpoolControl sc;
463 /* struct namelist *digestrecps = NULL; */
464 struct namelist *nptr;
466 lprintf(7, "Spooling <%s>\n", room_to_spool);
467 if (getroom(&CC->quickroom, room_to_spool) != 0) {
468 lprintf(1, "ERROR: cannot load <%s>\n", room_to_spool);
472 memset(&sc, 0, sizeof(struct SpoolControl));
473 assoc_file_name(filename, &CC->quickroom, "netconfigs");
475 fp = fopen(filename, "r");
477 lprintf(7, "Outbound batch processing skipped for <%s>\n",
478 CC->quickroom.QRname);
482 lprintf(5, "Outbound batch processing started for <%s>\n",
483 CC->quickroom.QRname);
485 while (fgets(buf, sizeof buf, fp) != NULL) {
486 buf[strlen(buf)-1] = 0;
488 extract(instr, buf, 0);
489 if (!strcasecmp(instr, "lastsent")) {
490 sc.lastsent = extract_long(buf, 1);
492 else if (!strcasecmp(instr, "listrecp")) {
493 nptr = (struct namelist *)
494 mallok(sizeof(struct namelist));
495 nptr->next = sc.listrecps;
496 extract(nptr->name, buf, 1);
499 else if (!strcasecmp(instr, "ignet_push_share")) {
500 nptr = (struct namelist *)
501 mallok(sizeof(struct namelist));
502 nptr->next = sc.ignet_push_shares;
503 extract(nptr->name, buf, 1);
504 sc.ignet_push_shares = nptr;
512 /* Do something useful */
513 CtdlForEachMessage(MSGS_GT, sc.lastsent, (-63), NULL, NULL,
514 network_spool_msg, &sc);
517 /* Now rewrite the config file */
518 fp = fopen(filename, "w");
520 lprintf(1, "ERROR: cannot open %s: %s\n",
521 filename, strerror(errno));
524 fprintf(fp, "lastsent|%ld\n", sc.lastsent);
526 /* Write out the listrecps while freeing from memory at the
527 * same time. Am I clever or what? :)
529 while (sc.listrecps != NULL) {
530 fprintf(fp, "listrecp|%s\n", sc.listrecps->name);
531 nptr = sc.listrecps->next;
535 while (sc.ignet_push_shares != NULL) {
536 fprintf(fp, "ignet_push_share|%s\n",
537 sc.ignet_push_shares->name);
538 nptr = sc.ignet_push_shares->next;
539 phree(sc.ignet_push_shares);
540 sc.ignet_push_shares = nptr;
546 lprintf(5, "Outbound batch processing finished for <%s>\n",
547 CC->quickroom.QRname);
552 * Batch up and send all outbound traffic from the current room
554 void network_queue_room(struct quickroom *qrbuf, void *data) {
555 struct RoomProcList *ptr;
557 ptr = (struct RoomProcList *) mallok(sizeof (struct RoomProcList));
558 if (ptr == NULL) return;
560 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
567 * Learn topology from path fields
569 void network_learn_topology(char *node, char *path) {
571 struct NetMap *nmptr;
575 if (num_tokens(path, '!') < 3) return;
576 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
577 if (!strcasecmp(nmptr->nodename, node)) {
578 extract_token(nmptr->nexthop, path, 0, '!');
579 nmptr->lastcontact = time(NULL);
584 /* If we got here then it's not in the map, so add it. */
585 nmptr = (struct NetMap *) mallok(sizeof (struct NetMap));
586 strcpy(nmptr->nodename, node);
587 nmptr->lastcontact = time(NULL);
588 extract_token(nmptr->nexthop, path, 0, '!');
589 nmptr->next = the_netmap;
596 * Process a buffer containing a single message from a single file
597 * from the inbound queue
599 void network_process_buffer(char *buffer, long size) {
600 struct CtdlMessage *msg;
605 struct usersupp tempUS;
607 char target_room[ROOMNAMELEN];
608 struct ser_ret sermsg;
609 char *oldpath = NULL;
613 /* Set default target room to trash */
614 strcpy(target_room, TWITROOM);
616 /* Load the message into memory */
617 msg = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
618 memset(msg, 0, sizeof(struct CtdlMessage));
619 msg->cm_magic = CTDLMESSAGE_MAGIC;
620 msg->cm_anon_type = buffer[1];
621 msg->cm_format_type = buffer[2];
623 for (pos = 3; pos < size; ++pos) {
625 msg->cm_fields[field] = strdoop(&buffer[pos+1]);
626 pos = pos + strlen(&buffer[(int)pos]);
629 /* Check for message routing */
630 if (msg->cm_fields['D'] != NULL) {
631 if (strcasecmp(msg->cm_fields['D'], config.c_nodename)) {
633 /* route the message */
634 if (is_valid_node(NULL, msg->cm_fields['D']) == 0) {
636 /* prepend our node to the path */
637 if (msg->cm_fields['P'] != NULL) {
638 oldpath = msg->cm_fields['P'];
639 msg->cm_fields['P'] = NULL;
642 oldpath = strdoop("unknown_user");
644 msg->cm_fields['P'] =
645 mallok(strlen(oldpath) + SIZ);
646 sprintf(msg->cm_fields['P'], "%s!%s",
647 config.c_nodename, oldpath);
650 /* serialize the message */
651 serialize_message(&sermsg, msg);
655 "./network/spoolout/%s",
656 msg->cm_fields['D']);
657 fp = fopen(filename, "ab");
664 CtdlFreeMessage(msg);
668 else { /* invalid destination node name */
670 /* FIXME bounce the msg */
676 /* FIXME check to see if we already have this message */
678 /* Learn network topology from the path */
679 if ((msg->cm_fields['N'] != NULL) && (msg->cm_fields['P'] != NULL)) {
680 network_learn_topology(msg->cm_fields['N'],
681 msg->cm_fields['P']);
684 /* Does it have a recipient? If so, validate it... */
685 if (msg->cm_fields['R'] != NULL) {
687 safestrncpy(recp, msg->cm_fields['R'], sizeof(recp));
689 e = alias(recp); /* alias and mail type */
690 if ((recp[0] == 0) || (e == MES_ERROR)) {
692 /* FIXME bounce the msg */
695 else if (e == MES_LOCAL) {
696 a = getuser(&tempUS, recp);
698 /* FIXME bounce the msg */
701 MailboxName(target_room, &tempUS, MAILROOM);
706 else if (msg->cm_fields['C'] != NULL) {
707 safestrncpy(target_room,
712 else if (msg->cm_fields['O'] != NULL) {
713 safestrncpy(target_room,
718 /* save the message into a room */
719 msg->cm_flags = CM_SKIP_HOOKS;
720 CtdlSaveMsg(msg, "", target_room, 0);
721 CtdlFreeMessage(msg);
726 * Process a single message from a single file from the inbound queue
728 void network_process_message(FILE *fp, long msgstart, long msgend) {
733 hold_pos = ftell(fp);
734 size = msgend - msgstart + 1;
735 buffer = mallok(size);
736 if (buffer != NULL) {
737 fseek(fp, msgstart, SEEK_SET);
738 fread(buffer, size, 1, fp);
739 network_process_buffer(buffer, size);
743 fseek(fp, hold_pos, SEEK_SET);
748 * Process a single file from the inbound queue
750 void network_process_file(char *filename) {
752 long msgstart = (-1L);
757 lprintf(7, "network: processing <%s>\n", filename);
759 fp = fopen(filename, "rb");
761 lprintf(5, "Error opening %s: %s\n",
762 filename, strerror(errno));
766 /* Look for messages in the data stream and break them out */
767 while (ch = getc(fp), ch >= 0) {
770 if (msgstart >= 0L) {
772 network_process_message(fp, msgstart, msgend);
781 if (msgstart >= 0L) {
782 network_process_message(fp, msgstart, msgend);
791 * Process anything in the inbound queue
793 void network_do_spoolin(void) {
798 dp = opendir("./network/spoolin");
799 if (dp == NULL) return;
801 while (d = readdir(dp), d != NULL) {
802 sprintf(filename, "./network/spoolin/%s", d->d_name);
803 network_process_file(filename);
814 * Run through the rooms doing various types of network stuff.
816 void network_do_queue(void) {
817 static int doing_queue = 0;
818 static time_t last_run = 0L;
819 struct RoomProcList *ptr;
822 * Run no more frequently than once every n seconds
824 if ( (time(NULL) - last_run) < NETWORK_QUEUE_FREQUENCY ) return;
827 * This is a simple concurrency check to make sure only one queue run
828 * is done at a time. We could do this with a mutex, but since we
829 * don't really require extremely fine granularity here, we'll do it
830 * with a static variable instead.
832 if (doing_queue) return;
834 last_run = time(NULL);
839 * Go ahead and run the queue
841 lprintf(7, "network: loading outbound queue\n");
842 ForEachRoom(network_queue_room, NULL);
844 lprintf(7, "network: running outbound queue\n");
845 while (rplist != NULL) {
846 network_spoolout_room(rplist->name);
848 rplist = rplist->next;
852 lprintf(7, "network: processing inbound queue\n");
853 network_do_spoolin();
857 lprintf(7, "network: queue run completed\n");
865 char *Dynamic_Module_Init(void)
867 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
868 CtdlRegisterProtoHook(cmd_snet, "SNET", "Get network config");
869 CtdlRegisterSessionHook(network_do_queue, EVT_TIMER);