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");
440 /* update lastsent */
441 sc->lastsent = msgnum;
443 /* Delete this message if delete-after-send is set */
444 if (delete_after_send) {
445 CtdlDeleteMessages(CC->quickroom.QRname, msgnum, "");
454 * Batch up and send all outbound traffic from the current room
456 void network_spoolout_room(char *room_to_spool) {
461 struct SpoolControl sc;
462 /* struct namelist *digestrecps = NULL; */
463 struct namelist *nptr;
465 lprintf(7, "Spooling <%s>\n", room_to_spool);
466 if (getroom(&CC->quickroom, room_to_spool) != 0) {
467 lprintf(1, "ERROR: cannot load <%s>\n", room_to_spool);
471 memset(&sc, 0, sizeof(struct SpoolControl));
472 assoc_file_name(filename, &CC->quickroom, "netconfigs");
474 fp = fopen(filename, "r");
476 lprintf(7, "Outbound batch processing skipped for <%s>\n",
477 CC->quickroom.QRname);
481 lprintf(5, "Outbound batch processing started for <%s>\n",
482 CC->quickroom.QRname);
484 while (fgets(buf, sizeof buf, fp) != NULL) {
485 buf[strlen(buf)-1] = 0;
487 extract(instr, buf, 0);
488 if (!strcasecmp(instr, "lastsent")) {
489 sc.lastsent = extract_long(buf, 1);
491 else if (!strcasecmp(instr, "listrecp")) {
492 nptr = (struct namelist *)
493 mallok(sizeof(struct namelist));
494 nptr->next = sc.listrecps;
495 extract(nptr->name, buf, 1);
498 else if (!strcasecmp(instr, "ignet_push_share")) {
499 nptr = (struct namelist *)
500 mallok(sizeof(struct namelist));
501 nptr->next = sc.ignet_push_shares;
502 extract(nptr->name, buf, 1);
503 sc.ignet_push_shares = nptr;
511 /* Do something useful */
512 CtdlForEachMessage(MSGS_GT, sc.lastsent, (-63), NULL, NULL,
513 network_spool_msg, &sc);
516 /* Now rewrite the config file */
517 fp = fopen(filename, "w");
519 lprintf(1, "ERROR: cannot open %s: %s\n",
520 filename, strerror(errno));
523 fprintf(fp, "lastsent|%ld\n", sc.lastsent);
525 /* Write out the listrecps while freeing from memory at the
526 * same time. Am I clever or what? :)
528 while (sc.listrecps != NULL) {
529 fprintf(fp, "listrecp|%s\n", sc.listrecps->name);
530 nptr = sc.listrecps->next;
534 while (sc.ignet_push_shares != NULL) {
535 fprintf(fp, "ignet_push_share|%s\n",
536 sc.ignet_push_shares->name);
537 nptr = sc.ignet_push_shares->next;
538 phree(sc.ignet_push_shares);
539 sc.ignet_push_shares = nptr;
545 lprintf(5, "Outbound batch processing finished for <%s>\n",
546 CC->quickroom.QRname);
551 * Batch up and send all outbound traffic from the current room
553 void network_queue_room(struct quickroom *qrbuf, void *data) {
554 struct RoomProcList *ptr;
556 ptr = (struct RoomProcList *) mallok(sizeof (struct RoomProcList));
557 if (ptr == NULL) return;
559 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
566 * Learn topology from path fields
568 void network_learn_topology(char *node, char *path) {
570 struct NetMap *nmptr;
574 if (num_tokens(path, '!') < 3) return;
575 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
576 if (!strcasecmp(nmptr->nodename, node)) {
577 extract_token(nmptr->nexthop, path, 0, '!');
578 nmptr->lastcontact = time(NULL);
583 /* If we got here then it's not in the map, so add it. */
584 nmptr = (struct NetMap *) mallok(sizeof (struct NetMap));
585 strcpy(nmptr->nodename, node);
586 nmptr->lastcontact = time(NULL);
587 extract_token(nmptr->nexthop, path, 0, '!');
588 nmptr->next = the_netmap;
595 * Process a buffer containing a single message from a single file
596 * from the inbound queue
598 void network_process_buffer(char *buffer, long size) {
599 struct CtdlMessage *msg;
604 struct usersupp tempUS;
606 char target_room[ROOMNAMELEN];
608 /* Set default target room to trash */
609 strcpy(target_room, TWITROOM);
611 /* Load the message into memory */
612 msg = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
613 memset(msg, 0, sizeof(struct CtdlMessage));
614 msg->cm_magic = CTDLMESSAGE_MAGIC;
615 msg->cm_anon_type = buffer[1];
616 msg->cm_format_type = buffer[2];
618 for (pos = 3; pos < size; ++pos) {
620 msg->cm_fields[field] = strdoop(&buffer[pos+1]);
621 pos = pos + strlen(&buffer[(int)pos]);
624 /* Check for message routing */
625 if (msg->cm_fields['D'] != NULL) {
626 if (strcasecmp(msg->cm_fields['D'], config.c_nodename)) {
628 /* FIXME route the message, stupid */
633 /* FIXME check to see if we already have this message */
635 /* Learn network topology from the path */
636 if ((msg->cm_fields['N'] != NULL) && (msg->cm_fields['P'] != NULL)) {
637 network_learn_topology(msg->cm_fields['N'],
638 msg->cm_fields['P']);
641 /* Does it have a recipient? If so, validate it... */
642 if (msg->cm_fields['R'] != NULL) {
644 safestrncpy(recp, msg->cm_fields['R'], sizeof(recp));
646 e = alias(recp); /* alias and mail type */
647 if ((recp[0] == 0) || (e == MES_ERROR)) {
649 /* FIXME bounce the msg */
652 else if (e == MES_LOCAL) {
653 a = getuser(&tempUS, recp);
655 /* FIXME bounce the msg */
658 MailboxName(target_room, &tempUS, MAILROOM);
663 else if (msg->cm_fields['C'] != NULL) {
664 safestrncpy(target_room,
669 else if (msg->cm_fields['O'] != NULL) {
670 safestrncpy(target_room,
675 /* save the message into a room */
676 msg->cm_flags = CM_SKIP_HOOKS;
677 CtdlSaveMsg(msg, "", target_room, 0);
678 CtdlFreeMessage(msg);
683 * Process a single message from a single file from the inbound queue
685 void network_process_message(FILE *fp, long msgstart, long msgend) {
690 hold_pos = ftell(fp);
691 size = msgend - msgstart + 1;
692 buffer = mallok(size);
693 if (buffer != NULL) {
694 fseek(fp, msgstart, SEEK_SET);
695 fread(buffer, size, 1, fp);
696 network_process_buffer(buffer, size);
700 fseek(fp, hold_pos, SEEK_SET);
705 * Process a single file from the inbound queue
707 void network_process_file(char *filename) {
709 long msgstart = (-1L);
714 lprintf(7, "network: processing <%s>\n", filename);
716 fp = fopen(filename, "rb");
718 lprintf(5, "Error opening %s: %s\n",
719 filename, strerror(errno));
723 /* Look for messages in the data stream and break them out */
724 while (ch = getc(fp), ch >= 0) {
727 if (msgstart >= 0L) {
729 network_process_message(fp, msgstart, msgend);
738 if (msgstart >= 0L) {
739 network_process_message(fp, msgstart, msgend);
748 * Process anything in the inbound queue
750 void network_do_spoolin(void) {
755 dp = opendir("./network/spoolin");
756 if (dp == NULL) return;
758 while (d = readdir(dp), d != NULL) {
759 sprintf(filename, "./network/spoolin/%s", d->d_name);
760 network_process_file(filename);
771 * Run through the rooms doing various types of network stuff.
773 void network_do_queue(void) {
774 static int doing_queue = 0;
775 static time_t last_run = 0L;
776 struct RoomProcList *ptr;
779 * Run no more frequently than once every n seconds
781 if ( (time(NULL) - last_run) < NETWORK_QUEUE_FREQUENCY ) return;
784 * This is a simple concurrency check to make sure only one queue run
785 * is done at a time. We could do this with a mutex, but since we
786 * don't really require extremely fine granularity here, we'll do it
787 * with a static variable instead.
789 if (doing_queue) return;
791 last_run = time(NULL);
796 * Go ahead and run the queue
798 lprintf(7, "network: loading outbound queue\n");
799 ForEachRoom(network_queue_room, NULL);
801 lprintf(7, "network: running outbound queue\n");
802 while (rplist != NULL) {
803 network_spoolout_room(rplist->name);
805 rplist = rplist->next;
809 lprintf(7, "network: processing inbound queue\n");
810 network_do_spoolin();
814 lprintf(7, "network: queue run completed\n");
822 char *Dynamic_Module_Init(void)
824 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
825 CtdlRegisterProtoHook(cmd_snet, "SNET", "Get network config");
826 CtdlRegisterSessionHook(network_do_queue, EVT_TIMER);