4 * This module handles shared rooms, inter-Citadel mail, and outbound
5 * mailing list processing.
7 * Copyright (C) 2000-2002 by Art Cancro and others.
8 * This code is released under the terms of the GNU General Public License.
14 * Don't allow polls during network processing
26 #include <sys/types.h>
28 #if TIME_WITH_SYS_TIME
29 # include <sys/time.h>
33 # include <sys/time.h>
44 #include "sysdep_decls.h"
45 #include "citserver.h"
48 #include "dynloader.h"
55 #include "internet_addressing.h"
56 #include "serv_network.h"
57 #include "clientsocket.h"
66 * When we do network processing, it's accomplished in two passes; one to
67 * gather a list of rooms and one to actually do them. It's ok that rplist
68 * is global; this process *only* runs as part of the housekeeping loop and
69 * therefore only one will run at a time.
71 struct RoomProcList *rplist = NULL;
74 * We build a map of network nodes during processing.
76 struct NetMap *the_netmap = NULL;
79 * Keep track of what messages to reject
81 struct FilterList *load_filter_list(void) {
82 char *serialized_list = NULL;
85 struct FilterList *newlist = NULL;
86 struct FilterList *nptr;
88 serialized_list = CtdlGetSysConfig(FILTERLIST);
89 if (serialized_list == NULL) return(NULL); /* if null, no entries */
91 /* Use the string tokenizer to grab one line at a time */
92 for (i=0; i<num_tokens(serialized_list, '\n'); ++i) {
93 extract_token(buf, serialized_list, i, '\n');
94 nptr = (struct FilterList *) mallok(sizeof(struct FilterList));
95 extract(nptr->fl_user, buf, 0);
96 striplt(nptr->fl_user);
97 extract(nptr->fl_room, buf, 1);
98 striplt(nptr->fl_room);
99 extract(nptr->fl_node, buf, 2);
100 striplt(nptr->fl_node);
102 /* Cowardly refuse to add an any/any/any entry that would
103 * end up filtering every single message.
105 if (strlen(nptr->fl_user) + strlen(nptr->fl_room)
106 + strlen(nptr->fl_node) == 0) {
110 nptr->next = newlist;
115 phree(serialized_list);
120 void free_filter_list(struct FilterList *fl) {
121 if (fl == NULL) return;
122 free_filter_list(fl->next);
129 * Check the use table. This is a list of messages which have recently
130 * arrived on the system. It is maintained and queried to prevent the same
131 * message from being entered into the database multiple times if it happens
132 * to arrive multiple times by accident.
134 int network_usetable(struct CtdlMessage *msg) {
137 struct cdbdata *cdbut;
140 /* Bail out if we can't generate a message ID */
144 if (msg->cm_fields['I'] == NULL) {
147 if (strlen(msg->cm_fields['I']) == 0) {
151 /* Generate the message ID */
152 strcpy(msgid, msg->cm_fields['I']);
153 if (haschar(msgid, '@') == 0) {
155 if (msg->cm_fields['N'] != NULL) {
156 strcat(msgid, msg->cm_fields['N']);
163 cdbut = cdb_fetch(CDB_USETABLE, msgid, strlen(msgid));
169 /* If we got to this point, it's unique: add it. */
170 strcpy(ut.ut_msgid, msgid);
171 ut.ut_timestamp = time(NULL);
172 cdb_store(CDB_USETABLE, msgid, strlen(msgid),
173 &ut, sizeof(struct UseTable) );
179 * Read the network map from its configuration file into memory.
181 void read_network_map(void) {
182 char *serialized_map = NULL;
185 struct NetMap *nmptr;
187 serialized_map = CtdlGetSysConfig(IGNETMAP);
188 if (serialized_map == NULL) return; /* if null, no entries */
190 /* Use the string tokenizer to grab one line at a time */
191 for (i=0; i<num_tokens(serialized_map, '\n'); ++i) {
192 extract_token(buf, serialized_map, i, '\n');
193 nmptr = (struct NetMap *) mallok(sizeof(struct NetMap));
194 extract(nmptr->nodename, buf, 0);
195 nmptr->lastcontact = extract_long(buf, 1);
196 extract(nmptr->nexthop, buf, 2);
197 nmptr->next = the_netmap;
201 phree(serialized_map);
206 * Write the network map from memory back to the configuration file.
208 void write_network_map(void) {
209 char *serialized_map = NULL;
210 struct NetMap *nmptr;
212 serialized_map = strdoop("");
214 if (the_netmap != NULL) {
215 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
216 serialized_map = reallok(serialized_map,
217 (strlen(serialized_map)+SIZ) );
218 if (strlen(nmptr->nodename) > 0) {
219 snprintf(&serialized_map[strlen(serialized_map)],
223 (long)nmptr->lastcontact,
229 CtdlPutSysConfig(IGNETMAP, serialized_map);
230 phree(serialized_map);
232 /* Now free the list */
233 while (the_netmap != NULL) {
234 nmptr = the_netmap->next;
243 * Check the network map and determine whether the supplied node name is
244 * valid. If it is not a neighbor node, supply the name of a neighbor node
245 * which is the next hop. If it *is* a neighbor node, we also fill in the
248 int is_valid_node(char *nexthop, char *secret, char *node) {
249 char *ignetcfg = NULL;
254 struct NetMap *nmptr;
261 * First try the neighbor nodes
263 ignetcfg = CtdlGetSysConfig(IGNETCFG);
264 if (ignetcfg == NULL) {
265 if (nexthop != NULL) {
272 if (nexthop != NULL) {
276 /* Use the string tokenizer to grab one line at a time */
277 for (i=0; i<num_tokens(ignetcfg, '\n'); ++i) {
278 extract_token(linebuf, ignetcfg, i, '\n');
279 extract(buf, linebuf, 0);
280 if (!strcasecmp(buf, node)) {
281 if (nexthop != NULL) {
284 if (secret != NULL) {
285 extract(secret, linebuf, 1);
293 return(retval); /* yup, it's a direct neighbor */
297 * If we get to this point we have to see if we know the next hop
299 if (the_netmap != NULL) {
300 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
301 if (!strcasecmp(nmptr->nodename, node)) {
302 if (nexthop != NULL) {
303 strcpy(nexthop, nmptr->nexthop);
311 * If we get to this point, the supplied node name is bogus.
313 lprintf(5, "Invalid node name <%s>\n", node);
321 void cmd_gnet(char *argbuf) {
326 if (CtdlAccessCheck(ac_room_aide)) return;
327 assoc_file_name(filename, sizeof filename, &CC->quickroom, "netconfigs");
328 cprintf("%d Network settings for room #%ld <%s>\n",
330 CC->quickroom.QRnumber, CC->quickroom.QRname);
332 fp = fopen(filename, "r");
334 while (fgets(buf, sizeof buf, fp) != NULL) {
335 buf[strlen(buf)-1] = 0;
336 cprintf("%s\n", buf);
345 void cmd_snet(char *argbuf) {
346 char tempfilename[SIZ];
351 if (CtdlAccessCheck(ac_room_aide)) return;
352 safestrncpy(tempfilename, tmpnam(NULL), sizeof tempfilename);
353 assoc_file_name(filename, sizeof filename, &CC->quickroom, "netconfigs");
355 fp = fopen(tempfilename, "w");
357 cprintf("%d Cannot open %s: %s\n",
358 ERROR+INTERNAL_ERROR,
363 cprintf("%d %s\n", SEND_LISTING, tempfilename);
364 while (client_gets(buf), strcmp(buf, "000")) {
365 fprintf(fp, "%s\n", buf);
369 /* Now copy the temp file to its permanent location
370 * (We use /bin/mv instead of link() because they may be on
371 * different filesystems)
374 snprintf(buf, sizeof buf, "/bin/mv %s %s", tempfilename, filename);
380 * Spools out one message from the list.
382 void network_spool_msg(long msgnum, void *userdata) {
383 struct SpoolControl *sc;
386 char *newpath = NULL;
388 size_t instr_len = SIZ;
389 struct CtdlMessage *msg = NULL;
390 struct CtdlMessage *imsg;
391 struct namelist *nptr;
392 struct ser_ret sermsg;
398 int delete_after_send = 0; /* Set to 1 to delete after spooling */
400 sc = (struct SpoolControl *)userdata;
403 * Process mailing list recipients
405 if (sc->listrecps != NULL) {
407 /* First, copy it to the spoolout room */
408 err = CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, msgnum, 0);
409 if (err != 0) return;
412 * Figure out how big a buffer we need to allocate
414 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
415 instr_len = instr_len + strlen(nptr->name);
421 lprintf(9, "Generating delivery instructions\n");
422 instr = mallok(instr_len);
424 lprintf(1, "Cannot allocate %ld bytes for instr...\n",
428 snprintf(instr, instr_len,
429 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
430 "bounceto|postmaster@%s\n" ,
431 SPOOLMIME, msgnum, (long)time(NULL), config.c_fqdn );
433 /* Generate delivery instructions for each recipient */
434 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
435 size_t tmp = strlen(instr);
436 snprintf(&instr[tmp], instr_len - tmp,
437 "remote|%s|0||\n", nptr->name);
441 * Generate a message from the instructions
443 imsg = mallok(sizeof(struct CtdlMessage));
444 memset(imsg, 0, sizeof(struct CtdlMessage));
445 imsg->cm_magic = CTDLMESSAGE_MAGIC;
446 imsg->cm_anon_type = MES_NORMAL;
447 imsg->cm_format_type = FMT_RFC822;
448 imsg->cm_fields['A'] = strdoop("Citadel");
449 imsg->cm_fields['M'] = instr;
451 /* Save delivery instructions in spoolout room */
452 CtdlSubmitMsg(imsg, NULL, SMTP_SPOOLOUT_ROOM);
453 CtdlFreeMessage(imsg);
457 * Process digest recipients
459 if ((sc->digestrecps != NULL) && (sc->digestfp != NULL)) {
460 fprintf(sc->digestfp, " -----------------------------------"
461 "------------------------------------"
463 CtdlRedirectOutput(sc->digestfp, -1);
464 CtdlOutputMsg(msgnum, MT_RFC822, HEADERS_ALL, 0, 0);
465 CtdlRedirectOutput(NULL, -1);
466 sc->num_msgs_spooled += 1;
470 * Process IGnet push shares
472 if (sc->ignet_push_shares != NULL) {
474 msg = CtdlFetchMessage(msgnum);
478 /* Prepend our node name to the Path field whenever
479 * sending a message to another IGnet node
481 if (msg->cm_fields['P'] == NULL) {
482 msg->cm_fields['P'] = strdoop("username");
484 newpath_len = strlen(msg->cm_fields['P']) +
485 strlen(config.c_nodename) + 2;
486 newpath = mallok(newpath_len);
487 snprintf(newpath, newpath_len, "%s!%s",
488 config.c_nodename, msg->cm_fields['P']);
489 phree(msg->cm_fields['P']);
490 msg->cm_fields['P'] = newpath;
493 * Force the message to appear in the correct room
494 * on the far end by setting the C field correctly
496 if (msg->cm_fields['C'] != NULL) {
497 phree(msg->cm_fields['C']);
499 msg->cm_fields['C'] = strdoop(CC->quickroom.QRname);
502 * Determine if this message is set to be deleted
503 * after sending out on the network
505 if (msg->cm_fields['S'] != NULL) {
506 if (!strcasecmp(msg->cm_fields['S'],
508 delete_after_send = 1;
513 * Now serialize it for transmission
515 serialize_message(&sermsg, msg);
517 /* Now send it to every node */
518 for (nptr = sc->ignet_push_shares; nptr != NULL;
523 /* Check for valid node name */
524 if (is_valid_node(NULL,NULL,nptr->name) != 0) {
525 lprintf(3, "Invalid node <%s>\n",
530 /* Check for split horizon */
531 lprintf(9, "Path is %s\n", msg->cm_fields['P']);
532 bang = num_tokens(msg->cm_fields['P'], '!');
533 if (bang > 1) for (i=0; i<(bang-1); ++i) {
534 extract_token(buf, msg->cm_fields['P'],
536 if (!strcasecmp(buf, nptr->name)) {
541 /* Send the message */
543 snprintf(filename, sizeof filename,
544 "./network/spoolout/%s",
546 fp = fopen(filename, "ab");
555 CtdlFreeMessage(msg);
559 /* update lastsent */
560 sc->lastsent = msgnum;
562 /* Delete this message if delete-after-send is set */
563 if (delete_after_send) {
564 CtdlDeleteMessages(CC->quickroom.QRname, msgnum, "");
571 * Deliver digest messages
573 void network_deliver_digest(struct SpoolControl *sc) {
576 struct CtdlMessage *msg;
580 size_t instr_len = SIZ;
581 struct CtdlMessage *imsg;
582 struct namelist *nptr;
584 if (sc->num_msgs_spooled < 1) {
585 fclose(sc->digestfp);
590 msg = mallok(sizeof(struct CtdlMessage));
591 memset(msg, 0, sizeof(struct CtdlMessage));
592 msg->cm_magic = CTDLMESSAGE_MAGIC;
593 msg->cm_format_type = FMT_RFC822;
594 msg->cm_anon_type = MES_NORMAL;
596 sprintf(buf, "%ld", time(NULL));
597 msg->cm_fields['T'] = strdoop(buf);
598 msg->cm_fields['A'] = strdoop(CC->quickroom.QRname);
599 msg->cm_fields['U'] = strdoop(CC->quickroom.QRname);
600 sprintf(buf, "room_%s@%s", CC->quickroom.QRname, config.c_fqdn);
601 for (i=0; i<strlen(buf); ++i) {
602 if (isspace(buf[i])) buf[i]='_';
603 buf[i] = tolower(buf[i]);
605 msg->cm_fields['F'] = strdoop(buf);
607 fseek(sc->digestfp, 0L, SEEK_END);
608 msglen = ftell(sc->digestfp);
610 msg->cm_fields['M'] = mallok(msglen + 1);
611 fseek(sc->digestfp, 0L, SEEK_SET);
612 fread(msg->cm_fields['M'], (size_t)msglen, 1, sc->digestfp);
613 msg->cm_fields['M'][msglen] = 0;
615 fclose(sc->digestfp);
618 msgnum = CtdlSubmitMsg(msg, NULL, SMTP_SPOOLOUT_ROOM);
619 CtdlFreeMessage(msg);
621 /* Now generate the delivery instructions */
624 * Figure out how big a buffer we need to allocate
626 for (nptr = sc->digestrecps; nptr != NULL; nptr = nptr->next) {
627 instr_len = instr_len + strlen(nptr->name);
633 lprintf(9, "Generating delivery instructions\n");
634 instr = mallok(instr_len);
636 lprintf(1, "Cannot allocate %ld bytes for instr...\n",
640 snprintf(instr, instr_len,
641 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
642 "bounceto|postmaster@%s\n" ,
643 SPOOLMIME, msgnum, (long)time(NULL), config.c_fqdn );
645 /* Generate delivery instructions for each recipient */
646 for (nptr = sc->digestrecps; nptr != NULL; nptr = nptr->next) {
647 size_t tmp = strlen(instr);
648 snprintf(&instr[tmp], instr_len - tmp,
649 "remote|%s|0||\n", nptr->name);
653 * Generate a message from the instructions
655 imsg = mallok(sizeof(struct CtdlMessage));
656 memset(imsg, 0, sizeof(struct CtdlMessage));
657 imsg->cm_magic = CTDLMESSAGE_MAGIC;
658 imsg->cm_anon_type = MES_NORMAL;
659 imsg->cm_format_type = FMT_RFC822;
660 imsg->cm_fields['A'] = strdoop("Citadel");
661 imsg->cm_fields['M'] = instr;
663 /* Save delivery instructions in spoolout room */
664 CtdlSubmitMsg(imsg, NULL, SMTP_SPOOLOUT_ROOM);
665 CtdlFreeMessage(imsg);
670 * Batch up and send all outbound traffic from the current room
672 void network_spoolout_room(char *room_to_spool) {
677 struct SpoolControl sc;
678 struct namelist *nptr;
682 lprintf(7, "Spooling <%s>\n", room_to_spool);
683 if (getroom(&CC->quickroom, room_to_spool) != 0) {
684 lprintf(1, "ERROR: cannot load <%s>\n", room_to_spool);
688 memset(&sc, 0, sizeof(struct SpoolControl));
689 assoc_file_name(filename, sizeof filename, &CC->quickroom, "netconfigs");
691 fp = fopen(filename, "r");
693 lprintf(7, "Outbound batch processing skipped for <%s>\n",
694 CC->quickroom.QRname);
698 lprintf(5, "Outbound batch processing started for <%s>\n",
699 CC->quickroom.QRname);
701 while (fgets(buf, sizeof buf, fp) != NULL) {
702 buf[strlen(buf)-1] = 0;
704 extract(instr, buf, 0);
705 if (!strcasecmp(instr, "lastsent")) {
706 sc.lastsent = extract_long(buf, 1);
708 else if (!strcasecmp(instr, "listrecp")) {
709 nptr = (struct namelist *)
710 mallok(sizeof(struct namelist));
711 nptr->next = sc.listrecps;
712 extract(nptr->name, buf, 1);
715 else if (!strcasecmp(instr, "digestrecp")) {
716 nptr = (struct namelist *)
717 mallok(sizeof(struct namelist));
718 nptr->next = sc.digestrecps;
719 extract(nptr->name, buf, 1);
720 sc.digestrecps = nptr;
722 else if (!strcasecmp(instr, "ignet_push_share")) {
723 nptr = (struct namelist *)
724 mallok(sizeof(struct namelist));
725 nptr->next = sc.ignet_push_shares;
726 extract(nptr->name, buf, 1);
727 sc.ignet_push_shares = nptr;
730 linesize = strlen(buf);
731 sc.misc = realloc(sc.misc,
732 (miscsize + linesize + 2) );
733 sprintf(&sc.misc[miscsize], "%s\n", buf);
734 miscsize = miscsize + linesize + 1;
741 /* If there are digest recipients, we have to build a digest */
742 if (sc.digestrecps != NULL) {
743 sc.digestfp = tmpfile();
744 fprintf(sc.digestfp, "Content-type: text/plain\n\n");
747 /* Do something useful */
748 CtdlForEachMessage(MSGS_GT, sc.lastsent, NULL, NULL,
749 network_spool_msg, &sc);
751 /* If we wrote a digest, deliver it and then close it */
752 if (sc.digestfp != NULL) {
753 fprintf(sc.digestfp, " -----------------------------------"
754 "------------------------------------"
756 "You are subscribed to the '%s' "
760 network_deliver_digest(&sc); /* deliver and close */
763 /* Now rewrite the config file */
764 fp = fopen(filename, "w");
766 lprintf(1, "ERROR: cannot open %s: %s\n",
767 filename, strerror(errno));
770 fprintf(fp, "lastsent|%ld\n", sc.lastsent);
772 /* Write out the listrecps while freeing from memory at the
773 * same time. Am I clever or what? :)
775 while (sc.listrecps != NULL) {
776 fprintf(fp, "listrecp|%s\n", sc.listrecps->name);
777 nptr = sc.listrecps->next;
781 /* Do the same for digestrecps */
782 while (sc.digestrecps != NULL) {
783 fprintf(fp, "digestrecp|%s\n", sc.digestrecps->name);
784 nptr = sc.digestrecps->next;
785 phree(sc.digestrecps);
786 sc.digestrecps = nptr;
788 while (sc.ignet_push_shares != NULL) {
789 fprintf(fp, "ignet_push_share|%s\n",
790 sc.ignet_push_shares->name);
791 nptr = sc.ignet_push_shares->next;
792 phree(sc.ignet_push_shares);
793 sc.ignet_push_shares = nptr;
795 fwrite(sc.misc, strlen(sc.misc), 1, fp);
801 lprintf(5, "Outbound batch processing finished for <%s>\n",
802 CC->quickroom.QRname);
807 * Batch up and send all outbound traffic from the current room
809 void network_queue_room(struct quickroom *qrbuf, void *data) {
810 struct RoomProcList *ptr;
812 ptr = (struct RoomProcList *) mallok(sizeof (struct RoomProcList));
813 if (ptr == NULL) return;
815 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
822 * Learn topology from path fields
824 void network_learn_topology(char *node, char *path) {
826 struct NetMap *nmptr;
830 if (num_tokens(path, '!') < 3) return;
831 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
832 if (!strcasecmp(nmptr->nodename, node)) {
833 extract_token(nmptr->nexthop, path, 0, '!');
834 nmptr->lastcontact = time(NULL);
839 /* If we got here then it's not in the map, so add it. */
840 nmptr = (struct NetMap *) mallok(sizeof (struct NetMap));
841 strcpy(nmptr->nodename, node);
842 nmptr->lastcontact = time(NULL);
843 extract_token(nmptr->nexthop, path, 0, '!');
844 nmptr->next = the_netmap;
852 * Bounce a message back to the sender
854 void network_bounce(struct CtdlMessage *msg, char *reason) {
855 char *oldpath = NULL;
857 char bouncesource[SIZ];
859 struct recptypes *valid = NULL;
860 char force_room[ROOMNAMELEN];
861 static int serialnum = 0;
864 lprintf(9, "entering network_bounce()\n");
866 if (msg == NULL) return;
868 snprintf(bouncesource, sizeof bouncesource, "%s@%s", BOUNCESOURCE, config.c_nodename);
871 * Give it a fresh message ID
873 if (msg->cm_fields['I'] != NULL) {
874 phree(msg->cm_fields['I']);
876 snprintf(buf, sizeof buf, "%ld.%04lx.%04x@%s",
877 (long)time(NULL), (long)getpid(), ++serialnum, config.c_fqdn);
878 msg->cm_fields['I'] = strdoop(buf);
881 * FIXME ... right now we're just sending a bounce; we really want to
882 * include the text of the bounced message.
884 if (msg->cm_fields['M'] != NULL) {
885 phree(msg->cm_fields['M']);
887 msg->cm_fields['M'] = strdoop(reason);
888 msg->cm_format_type = 0;
891 * Turn the message around
893 if (msg->cm_fields['R'] == NULL) {
894 phree(msg->cm_fields['R']);
897 if (msg->cm_fields['D'] == NULL) {
898 phree(msg->cm_fields['D']);
901 snprintf(recipient, sizeof recipient, "%s@%s",
902 msg->cm_fields['A'], msg->cm_fields['N']);
904 if (msg->cm_fields['A'] == NULL) {
905 phree(msg->cm_fields['A']);
908 if (msg->cm_fields['N'] == NULL) {
909 phree(msg->cm_fields['N']);
912 msg->cm_fields['A'] = strdoop(BOUNCESOURCE);
913 msg->cm_fields['N'] = strdoop(config.c_nodename);
916 /* prepend our node to the path */
917 if (msg->cm_fields['P'] != NULL) {
918 oldpath = msg->cm_fields['P'];
919 msg->cm_fields['P'] = NULL;
922 oldpath = strdoop("unknown_user");
924 size = strlen(oldpath) + SIZ;
925 msg->cm_fields['P'] = mallok(size);
926 snprintf(msg->cm_fields['P'], size, "%s!%s", config.c_nodename, oldpath);
929 /* Now submit the message */
930 valid = validate_recipients(recipient);
931 if (valid != NULL) if (valid->num_error > 0) {
935 if ( (valid == NULL) || (!strcasecmp(recipient, bouncesource)) ) {
936 strcpy(force_room, config.c_aideroom);
939 strcpy(force_room, "");
941 if ( (valid == NULL) && (strlen(force_room) == 0) ) {
942 strcpy(force_room, config.c_aideroom);
944 CtdlSubmitMsg(msg, valid, force_room);
947 if (valid != NULL) phree(valid);
948 CtdlFreeMessage(msg);
949 lprintf(9, "leaving network_bounce()\n");
956 * Process a buffer containing a single message from a single file
957 * from the inbound queue
959 void network_process_buffer(char *buffer, long size) {
960 struct CtdlMessage *msg;
963 struct recptypes *recp = NULL;
964 char target_room[ROOMNAMELEN];
965 struct ser_ret sermsg;
966 char *oldpath = NULL;
971 /* Set default target room to trash */
972 strcpy(target_room, TWITROOM);
974 /* Load the message into memory */
975 msg = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
976 memset(msg, 0, sizeof(struct CtdlMessage));
977 msg->cm_magic = CTDLMESSAGE_MAGIC;
978 msg->cm_anon_type = buffer[1];
979 msg->cm_format_type = buffer[2];
981 for (pos = 3; pos < size; ++pos) {
983 msg->cm_fields[field] = strdoop(&buffer[pos+1]);
984 pos = pos + strlen(&buffer[(int)pos]);
987 /* Check for message routing */
988 if (msg->cm_fields['D'] != NULL) {
989 if (strcasecmp(msg->cm_fields['D'], config.c_nodename)) {
991 /* route the message */
992 if (is_valid_node(NULL, NULL,
993 msg->cm_fields['D']) == 0) {
995 /* prepend our node to the path */
996 if (msg->cm_fields['P'] != NULL) {
997 oldpath = msg->cm_fields['P'];
998 msg->cm_fields['P'] = NULL;
1001 oldpath = strdoop("unknown_user");
1003 size = strlen(oldpath) + SIZ;
1004 msg->cm_fields['P'] = mallok(size);
1005 snprintf(msg->cm_fields['P'], size, "%s!%s",
1006 config.c_nodename, oldpath);
1009 /* serialize the message */
1010 serialize_message(&sermsg, msg);
1013 snprintf(filename, sizeof filename,
1014 "./network/spoolout/%s",
1015 msg->cm_fields['D']);
1016 fp = fopen(filename, "ab");
1023 CtdlFreeMessage(msg);
1027 else { /* invalid destination node name */
1030 "A message you sent could not be delivered due to an invalid destination node"
1031 " name. Please check the address and try sending the message again.\n");
1040 * Check to see if we already have a copy of this message
1042 if (network_usetable(msg) != 0) {
1043 snprintf(buf, sizeof buf,
1044 "Loopzapper rejected message <%s> "
1045 "from <%s> in <%s> @ <%s>\n",
1046 ((msg->cm_fields['I']!=NULL)?(msg->cm_fields['I']):""),
1047 ((msg->cm_fields['A']!=NULL)?(msg->cm_fields['A']):""),
1048 ((msg->cm_fields['O']!=NULL)?(msg->cm_fields['O']):""),
1049 ((msg->cm_fields['N']!=NULL)?(msg->cm_fields['N']):"")
1052 CtdlFreeMessage(msg);
1057 /* Learn network topology from the path */
1058 if ((msg->cm_fields['N'] != NULL) && (msg->cm_fields['P'] != NULL)) {
1059 network_learn_topology(msg->cm_fields['N'],
1060 msg->cm_fields['P']);
1063 /* Does it have a recipient? If so, validate it... */
1064 if (msg->cm_fields['R'] != NULL) {
1065 recp = validate_recipients(msg->cm_fields['R']);
1066 if (recp != NULL) if (recp->num_error > 0) {
1068 "A message you sent could not be delivered due to an invalid address.\n"
1069 "Please check the address and try sending the message again.\n");
1074 strcpy(target_room, ""); /* no target room if mail */
1077 else if (msg->cm_fields['C'] != NULL) {
1078 safestrncpy(target_room,
1079 msg->cm_fields['C'],
1080 sizeof target_room);
1083 else if (msg->cm_fields['O'] != NULL) {
1084 safestrncpy(target_room,
1085 msg->cm_fields['O'],
1086 sizeof target_room);
1089 /* Strip out fields that are only relevant during transit */
1090 if (msg->cm_fields['D'] != NULL) {
1091 phree(msg->cm_fields['D']);
1092 msg->cm_fields['D'] = NULL;
1094 if (msg->cm_fields['C'] != NULL) {
1095 phree(msg->cm_fields['C']);
1096 msg->cm_fields['C'] = NULL;
1099 /* save the message into a room */
1100 if (PerformNetprocHooks(msg, target_room) == 0) {
1101 msg->cm_flags = CM_SKIP_HOOKS;
1102 CtdlSubmitMsg(msg, recp, target_room);
1104 CtdlFreeMessage(msg);
1110 * Process a single message from a single file from the inbound queue
1112 void network_process_message(FILE *fp, long msgstart, long msgend) {
1117 hold_pos = ftell(fp);
1118 size = msgend - msgstart + 1;
1119 buffer = mallok(size);
1120 if (buffer != NULL) {
1121 fseek(fp, msgstart, SEEK_SET);
1122 fread(buffer, size, 1, fp);
1123 network_process_buffer(buffer, size);
1127 fseek(fp, hold_pos, SEEK_SET);
1132 * Process a single file from the inbound queue
1134 void network_process_file(char *filename) {
1136 long msgstart = (-1L);
1137 long msgend = (-1L);
1141 lprintf(7, "network: processing <%s>\n", filename);
1143 fp = fopen(filename, "rb");
1145 lprintf(5, "Error opening %s: %s\n",
1146 filename, strerror(errno));
1150 /* Look for messages in the data stream and break them out */
1151 while (ch = getc(fp), ch >= 0) {
1154 if (msgstart >= 0L) {
1155 msgend = msgcur - 1;
1156 network_process_message(fp, msgstart, msgend);
1164 msgend = msgcur - 1;
1165 if (msgstart >= 0L) {
1166 network_process_message(fp, msgstart, msgend);
1175 * Process anything in the inbound queue
1177 void network_do_spoolin(void) {
1182 dp = opendir("./network/spoolin");
1183 if (dp == NULL) return;
1185 while (d = readdir(dp), d != NULL) {
1186 snprintf(filename, sizeof filename, "./network/spoolin/%s", d->d_name);
1187 network_process_file(filename);
1199 * receive network spool from the remote system
1201 void receive_spool(int sock, char *remote_nodename) {
1203 long bytes_received;
1205 static char pbuf[IGNET_PACKET_SIZE];
1206 char tempfilename[PATH_MAX];
1210 strcpy(tempfilename, tmpnam(NULL));
1211 if (sock_puts(sock, "NDOP") < 0) return;
1212 if (sock_gets(sock, buf) < 0) return;
1213 lprintf(9, "<%s\n", buf);
1214 if (buf[0] != '2') {
1217 download_len = extract_long(&buf[4], 0);
1219 bytes_received = 0L;
1220 fp = fopen(tempfilename, "w");
1222 lprintf(9, "cannot open download file locally: %s\n",
1227 while (bytes_received < download_len) {
1228 snprintf(buf, sizeof buf, "READ %ld|%ld",
1230 ((download_len - bytes_received > IGNET_PACKET_SIZE)
1231 ? IGNET_PACKET_SIZE : (download_len - bytes_received)));
1232 if (sock_puts(sock, buf) < 0) {
1234 unlink(tempfilename);
1237 if (sock_gets(sock, buf) < 0) {
1239 unlink(tempfilename);
1242 if (buf[0] == '6') {
1243 plen = extract_long(&buf[4], 0);
1244 if (sock_read(sock, pbuf, plen) < 0) {
1246 unlink(tempfilename);
1249 fwrite((char *) pbuf, plen, 1, fp);
1250 bytes_received = bytes_received + plen;
1255 if (sock_puts(sock, "CLOS") < 0) {
1256 unlink(tempfilename);
1259 if (sock_gets(sock, buf) < 0) {
1260 unlink(tempfilename);
1263 lprintf(9, "%s\n", buf);
1264 snprintf(buf, sizeof buf, "mv %s ./network/spoolin/%s.%ld",
1265 tempfilename, remote_nodename, (long) getpid());
1272 * transmit network spool to the remote system
1274 void transmit_spool(int sock, char *remote_nodename)
1279 long bytes_to_write, thisblock;
1283 if (sock_puts(sock, "NUOP") < 0) return;
1284 if (sock_gets(sock, buf) < 0) return;
1285 lprintf(9, "<%s\n", buf);
1286 if (buf[0] != '2') {
1290 snprintf(sfname, sizeof sfname, "./network/spoolout/%s", remote_nodename);
1291 fd = open(sfname, O_RDONLY);
1293 if (errno == ENOENT) {
1294 lprintf(9, "Nothing to send.\n");
1296 lprintf(5, "cannot open upload file locally: %s\n",
1301 while (plen = (long) read(fd, pbuf, IGNET_PACKET_SIZE), plen > 0L) {
1302 bytes_to_write = plen;
1303 while (bytes_to_write > 0L) {
1304 snprintf(buf, sizeof buf, "WRIT %ld", bytes_to_write);
1305 if (sock_puts(sock, buf) < 0) {
1309 if (sock_gets(sock, buf) < 0) {
1313 thisblock = atol(&buf[4]);
1314 if (buf[0] == '7') {
1315 if (sock_write(sock, pbuf,
1316 (int) thisblock) < 0) {
1320 bytes_to_write = bytes_to_write - thisblock;
1329 if (sock_puts(sock, "UCLS 1") < 0) return;
1330 if (sock_gets(sock, buf) < 0) return;
1331 lprintf(9, "<%s\n", buf);
1332 if (buf[0] == '2') {
1340 * Poll one Citadel node (called by network_poll_other_citadel_nodes() below)
1342 void network_poll_node(char *node, char *secret, char *host, char *port) {
1346 if (network_talking_to(node, NTT_CHECK)) return;
1347 network_talking_to(node, NTT_ADD);
1348 lprintf(5, "Polling node <%s> at %s:%s\n", node, host, port);
1350 sock = sock_connect(host, port, "tcp");
1352 lprintf(7, "Could not connect: %s\n", strerror(errno));
1353 network_talking_to(node, NTT_REMOVE);
1357 lprintf(9, "Connected!\n");
1359 /* Read the server greeting */
1360 if (sock_gets(sock, buf) < 0) goto bail;
1361 lprintf(9, ">%s\n", buf);
1363 /* Identify ourselves */
1364 snprintf(buf, sizeof buf, "NETP %s|%s", config.c_nodename, secret);
1365 lprintf(9, "<%s\n", buf);
1366 if (sock_puts(sock, buf) <0) goto bail;
1367 if (sock_gets(sock, buf) < 0) goto bail;
1368 lprintf(9, ">%s\n", buf);
1369 if (buf[0] != '2') goto bail;
1371 /* At this point we are authenticated. */
1372 receive_spool(sock, node);
1373 transmit_spool(sock, node);
1375 sock_puts(sock, "QUIT");
1376 bail: sock_close(sock);
1377 network_talking_to(node, NTT_REMOVE);
1383 * Poll other Citadel nodes and transfer inbound/outbound network data.
1385 void network_poll_other_citadel_nodes(void) {
1386 char *ignetcfg = NULL;
1394 ignetcfg = CtdlGetSysConfig(IGNETCFG);
1395 if (ignetcfg == NULL) return; /* no nodes defined */
1397 /* Use the string tokenizer to grab one line at a time */
1398 for (i=0; i<num_tokens(ignetcfg, '\n'); ++i) {
1399 extract_token(linebuf, ignetcfg, i, '\n');
1400 extract(node, linebuf, 0);
1401 extract(secret, linebuf, 1);
1402 extract(host, linebuf, 2);
1403 extract(port, linebuf, 3);
1404 if ( (strlen(node) > 0) && (strlen(secret) > 0)
1405 && (strlen(host) > 0) && strlen(port) > 0) {
1406 network_poll_node(node, secret, host, port);
1420 * network_do_queue()
1422 * Run through the rooms doing various types of network stuff.
1424 void network_do_queue(void) {
1425 static int doing_queue = 0;
1426 static time_t last_run = 0L;
1427 struct RoomProcList *ptr;
1430 * Run no more frequently than once every n seconds
1432 if ( (time(NULL) - last_run) < config.c_net_freq ) return;
1435 * This is a simple concurrency check to make sure only one queue run
1436 * is done at a time. We could do this with a mutex, but since we
1437 * don't really require extremely fine granularity here, we'll do it
1438 * with a static variable instead.
1440 if (doing_queue) return;
1442 last_run = time(NULL);
1445 * Poll other Citadel nodes.
1447 network_poll_other_citadel_nodes();
1450 * Load the network map and filter list into memory.
1453 filterlist = load_filter_list();
1456 * Go ahead and run the queue
1458 lprintf(7, "network: loading outbound queue\n");
1459 ForEachRoom(network_queue_room, NULL);
1461 lprintf(7, "network: running outbound queue\n");
1462 while (rplist != NULL) {
1463 network_spoolout_room(rplist->name);
1465 rplist = rplist->next;
1469 lprintf(7, "network: processing inbound queue\n");
1470 network_do_spoolin();
1472 /* Save the network map back to disk */
1473 write_network_map();
1475 /* Free the filter list in memory */
1476 free_filter_list(filterlist);
1479 lprintf(7, "network: queue run completed\n");
1485 * cmd_netp() - authenticate to the server as another Citadel node polling
1486 * for network traffic
1488 void cmd_netp(char *cmdbuf)
1496 extract(node, cmdbuf, 0);
1497 extract(pass, cmdbuf, 1);
1499 if (is_valid_node(nexthop, secret, node) != 0) {
1500 cprintf("%d authentication failed\n", ERROR);
1504 if (strcasecmp(pass, secret)) {
1505 cprintf("%d authentication failed\n", ERROR);
1509 if (network_talking_to(node, NTT_CHECK)) {
1510 cprintf("%d Already talking to %s right now\n", ERROR, node);
1514 safestrncpy(CC->net_node, node, sizeof CC->net_node);
1515 network_talking_to(node, NTT_ADD);
1516 cprintf("%d authenticated as network node '%s'\n", CIT_OK,
1525 * Module entry point
1527 char *Dynamic_Module_Init(void)
1529 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
1530 CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");
1531 CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");
1532 CtdlRegisterSessionHook(network_do_queue, EVT_TIMER);