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.
10 * ** NOTE ** A word on the S_NETCONFIGS semaphore:
11 * This is a fairly high-level type of critical section. It ensures that no
12 * two threads work on the netconfigs files at the same time. Since we do
13 * so many things inside these, here are the rules:
14 * 1. begin_critical_section(S_NETCONFIGS) *before* begin_ any others.
15 * 2. Do *not* perform any I/O with the client during these sections.
21 * Don't allow polls during network processing
33 #include <sys/types.h>
35 #if TIME_WITH_SYS_TIME
36 # include <sys/time.h>
40 # include <sys/time.h>
51 #include "sysdep_decls.h"
52 #include "citserver.h"
55 #include "dynloader.h"
62 #include "internet_addressing.h"
63 #include "serv_network.h"
64 #include "clientsocket.h"
73 * When we do network processing, it's accomplished in two passes; one to
74 * gather a list of rooms and one to actually do them. It's ok that rplist
75 * is global; this process *only* runs as part of the housekeeping loop and
76 * therefore only one will run at a time.
78 struct RoomProcList *rplist = NULL;
81 * We build a map of network nodes during processing.
83 struct NetMap *the_netmap = NULL;
86 * Keep track of what messages to reject
88 struct FilterList *load_filter_list(void) {
89 char *serialized_list = NULL;
92 struct FilterList *newlist = NULL;
93 struct FilterList *nptr;
95 serialized_list = CtdlGetSysConfig(FILTERLIST);
96 if (serialized_list == NULL) return(NULL); /* if null, no entries */
98 /* Use the string tokenizer to grab one line at a time */
99 for (i=0; i<num_tokens(serialized_list, '\n'); ++i) {
100 extract_token(buf, serialized_list, i, '\n');
101 nptr = (struct FilterList *) mallok(sizeof(struct FilterList));
102 extract(nptr->fl_user, buf, 0);
103 striplt(nptr->fl_user);
104 extract(nptr->fl_room, buf, 1);
105 striplt(nptr->fl_room);
106 extract(nptr->fl_node, buf, 2);
107 striplt(nptr->fl_node);
109 /* Cowardly refuse to add an any/any/any entry that would
110 * end up filtering every single message.
112 if (strlen(nptr->fl_user) + strlen(nptr->fl_room)
113 + strlen(nptr->fl_node) == 0) {
117 nptr->next = newlist;
122 phree(serialized_list);
127 void free_filter_list(struct FilterList *fl) {
128 if (fl == NULL) return;
129 free_filter_list(fl->next);
136 * Check the use table. This is a list of messages which have recently
137 * arrived on the system. It is maintained and queried to prevent the same
138 * message from being entered into the database multiple times if it happens
139 * to arrive multiple times by accident.
141 int network_usetable(struct CtdlMessage *msg) {
144 struct cdbdata *cdbut;
147 /* Bail out if we can't generate a message ID */
151 if (msg->cm_fields['I'] == NULL) {
154 if (strlen(msg->cm_fields['I']) == 0) {
158 /* Generate the message ID */
159 strcpy(msgid, msg->cm_fields['I']);
160 if (haschar(msgid, '@') == 0) {
162 if (msg->cm_fields['N'] != NULL) {
163 strcat(msgid, msg->cm_fields['N']);
170 cdbut = cdb_fetch(CDB_USETABLE, msgid, strlen(msgid));
176 /* If we got to this point, it's unique: add it. */
177 strcpy(ut.ut_msgid, msgid);
178 ut.ut_timestamp = time(NULL);
179 cdb_store(CDB_USETABLE, msgid, strlen(msgid),
180 &ut, sizeof(struct UseTable) );
186 * Read the network map from its configuration file into memory.
188 void read_network_map(void) {
189 char *serialized_map = NULL;
192 struct NetMap *nmptr;
194 serialized_map = CtdlGetSysConfig(IGNETMAP);
195 if (serialized_map == NULL) return; /* if null, no entries */
197 /* Use the string tokenizer to grab one line at a time */
198 for (i=0; i<num_tokens(serialized_map, '\n'); ++i) {
199 extract_token(buf, serialized_map, i, '\n');
200 nmptr = (struct NetMap *) mallok(sizeof(struct NetMap));
201 extract(nmptr->nodename, buf, 0);
202 nmptr->lastcontact = extract_long(buf, 1);
203 extract(nmptr->nexthop, buf, 2);
204 nmptr->next = the_netmap;
208 phree(serialized_map);
213 * Write the network map from memory back to the configuration file.
215 void write_network_map(void) {
216 char *serialized_map = NULL;
217 struct NetMap *nmptr;
219 serialized_map = strdoop("");
221 if (the_netmap != NULL) {
222 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
223 serialized_map = reallok(serialized_map,
224 (strlen(serialized_map)+SIZ) );
225 if (strlen(nmptr->nodename) > 0) {
226 snprintf(&serialized_map[strlen(serialized_map)],
230 (long)nmptr->lastcontact,
236 CtdlPutSysConfig(IGNETMAP, serialized_map);
237 phree(serialized_map);
239 /* Now free the list */
240 while (the_netmap != NULL) {
241 nmptr = the_netmap->next;
250 * Check the network map and determine whether the supplied node name is
251 * valid. If it is not a neighbor node, supply the name of a neighbor node
252 * which is the next hop. If it *is* a neighbor node, we also fill in the
255 int is_valid_node(char *nexthop, char *secret, char *node) {
256 char *ignetcfg = NULL;
261 struct NetMap *nmptr;
268 * First try the neighbor nodes
270 ignetcfg = CtdlGetSysConfig(IGNETCFG);
271 if (ignetcfg == NULL) {
272 if (nexthop != NULL) {
279 if (nexthop != NULL) {
283 /* Use the string tokenizer to grab one line at a time */
284 for (i=0; i<num_tokens(ignetcfg, '\n'); ++i) {
285 extract_token(linebuf, ignetcfg, i, '\n');
286 extract(buf, linebuf, 0);
287 if (!strcasecmp(buf, node)) {
288 if (nexthop != NULL) {
291 if (secret != NULL) {
292 extract(secret, linebuf, 1);
300 return(retval); /* yup, it's a direct neighbor */
304 * If we get to this point we have to see if we know the next hop
306 if (the_netmap != NULL) {
307 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
308 if (!strcasecmp(nmptr->nodename, node)) {
309 if (nexthop != NULL) {
310 strcpy(nexthop, nmptr->nexthop);
318 * If we get to this point, the supplied node name is bogus.
320 lprintf(5, "Invalid node name <%s>\n", node);
328 void cmd_gnet(char *argbuf) {
333 if (CtdlAccessCheck(ac_room_aide)) return;
334 assoc_file_name(filename, sizeof filename, &CC->quickroom, "netconfigs");
335 cprintf("%d Network settings for room #%ld <%s>\n",
337 CC->quickroom.QRnumber, CC->quickroom.QRname);
339 fp = fopen(filename, "r");
341 while (fgets(buf, sizeof buf, fp) != NULL) {
342 buf[strlen(buf)-1] = 0;
343 cprintf("%s\n", buf);
352 void cmd_snet(char *argbuf) {
353 char tempfilename[SIZ];
358 if (CtdlAccessCheck(ac_room_aide)) return;
359 safestrncpy(tempfilename, tmpnam(NULL), sizeof tempfilename);
360 assoc_file_name(filename, sizeof filename, &CC->quickroom, "netconfigs");
362 fp = fopen(tempfilename, "w");
364 cprintf("%d Cannot open %s: %s\n",
365 ERROR+INTERNAL_ERROR,
370 cprintf("%d %s\n", SEND_LISTING, tempfilename);
371 while (client_gets(buf), strcmp(buf, "000")) {
372 fprintf(fp, "%s\n", buf);
376 /* Now copy the temp file to its permanent location
377 * (We use /bin/mv instead of link() because they may be on
378 * different filesystems)
381 snprintf(buf, sizeof buf, "/bin/mv %s %s", tempfilename, filename);
382 begin_critical_section(S_NETCONFIGS);
384 end_critical_section(S_NETCONFIGS);
389 * Spools out one message from the list.
391 void network_spool_msg(long msgnum, void *userdata) {
392 struct SpoolControl *sc;
395 char *newpath = NULL;
397 size_t instr_len = SIZ;
398 struct CtdlMessage *msg = NULL;
399 struct CtdlMessage *imsg;
400 struct namelist *nptr;
401 struct ser_ret sermsg;
407 int delete_after_send = 0; /* Set to 1 to delete after spooling */
409 sc = (struct SpoolControl *)userdata;
412 * Process mailing list recipients
414 if (sc->listrecps != NULL) {
416 /* First, copy it to the spoolout room */
417 err = CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, msgnum, 0);
418 if (err != 0) return;
421 * Figure out how big a buffer we need to allocate
423 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
424 instr_len = instr_len + strlen(nptr->name);
430 lprintf(9, "Generating delivery instructions\n");
431 instr = mallok(instr_len);
433 lprintf(1, "Cannot allocate %ld bytes for instr...\n",
437 snprintf(instr, instr_len,
438 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
439 "bounceto|postmaster@%s\n" ,
440 SPOOLMIME, msgnum, (long)time(NULL), config.c_fqdn );
442 /* Generate delivery instructions for each recipient */
443 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
444 size_t tmp = strlen(instr);
445 snprintf(&instr[tmp], instr_len - tmp,
446 "remote|%s|0||\n", nptr->name);
450 * Generate a message from the instructions
452 imsg = mallok(sizeof(struct CtdlMessage));
453 memset(imsg, 0, sizeof(struct CtdlMessage));
454 imsg->cm_magic = CTDLMESSAGE_MAGIC;
455 imsg->cm_anon_type = MES_NORMAL;
456 imsg->cm_format_type = FMT_RFC822;
457 imsg->cm_fields['A'] = strdoop("Citadel");
458 imsg->cm_fields['M'] = instr;
460 /* Save delivery instructions in spoolout room */
461 CtdlSubmitMsg(imsg, NULL, SMTP_SPOOLOUT_ROOM);
462 CtdlFreeMessage(imsg);
466 * Process digest recipients
468 if ((sc->digestrecps != NULL) && (sc->digestfp != NULL)) {
469 fprintf(sc->digestfp, " -----------------------------------"
470 "------------------------------------"
472 CtdlRedirectOutput(sc->digestfp, -1);
473 CtdlOutputMsg(msgnum, MT_RFC822, HEADERS_ALL, 0, 0);
474 CtdlRedirectOutput(NULL, -1);
475 sc->num_msgs_spooled += 1;
479 * Process IGnet push shares
481 if (sc->ignet_push_shares != NULL) {
483 msg = CtdlFetchMessage(msgnum);
487 /* Prepend our node name to the Path field whenever
488 * sending a message to another IGnet node
490 if (msg->cm_fields['P'] == NULL) {
491 msg->cm_fields['P'] = strdoop("username");
493 newpath_len = strlen(msg->cm_fields['P']) +
494 strlen(config.c_nodename) + 2;
495 newpath = mallok(newpath_len);
496 snprintf(newpath, newpath_len, "%s!%s",
497 config.c_nodename, msg->cm_fields['P']);
498 phree(msg->cm_fields['P']);
499 msg->cm_fields['P'] = newpath;
502 * Force the message to appear in the correct room
503 * on the far end by setting the C field correctly
505 if (msg->cm_fields['C'] != NULL) {
506 phree(msg->cm_fields['C']);
508 msg->cm_fields['C'] = strdoop(CC->quickroom.QRname);
511 * Determine if this message is set to be deleted
512 * after sending out on the network
514 if (msg->cm_fields['S'] != NULL) {
515 if (!strcasecmp(msg->cm_fields['S'],
517 delete_after_send = 1;
522 * Now serialize it for transmission
524 serialize_message(&sermsg, msg);
526 /* Now send it to every node */
527 for (nptr = sc->ignet_push_shares; nptr != NULL;
532 /* Check for valid node name */
533 if (is_valid_node(NULL,NULL,nptr->name) != 0) {
534 lprintf(3, "Invalid node <%s>\n",
539 /* Check for split horizon */
540 lprintf(9, "Path is %s\n", msg->cm_fields['P']);
541 bang = num_tokens(msg->cm_fields['P'], '!');
542 if (bang > 1) for (i=0; i<(bang-1); ++i) {
543 extract_token(buf, msg->cm_fields['P'],
545 if (!strcasecmp(buf, nptr->name)) {
550 /* Send the message */
552 snprintf(filename, sizeof filename,
553 "./network/spoolout/%s",
555 fp = fopen(filename, "ab");
564 CtdlFreeMessage(msg);
568 /* update lastsent */
569 sc->lastsent = msgnum;
571 /* Delete this message if delete-after-send is set */
572 if (delete_after_send) {
573 CtdlDeleteMessages(CC->quickroom.QRname, msgnum, "");
580 * Deliver digest messages
582 void network_deliver_digest(struct SpoolControl *sc) {
585 struct CtdlMessage *msg;
589 size_t instr_len = SIZ;
590 struct CtdlMessage *imsg;
591 struct namelist *nptr;
593 if (sc->num_msgs_spooled < 1) {
594 fclose(sc->digestfp);
599 msg = mallok(sizeof(struct CtdlMessage));
600 memset(msg, 0, sizeof(struct CtdlMessage));
601 msg->cm_magic = CTDLMESSAGE_MAGIC;
602 msg->cm_format_type = FMT_RFC822;
603 msg->cm_anon_type = MES_NORMAL;
605 sprintf(buf, "%ld", time(NULL));
606 msg->cm_fields['T'] = strdoop(buf);
607 msg->cm_fields['A'] = strdoop(CC->quickroom.QRname);
608 msg->cm_fields['U'] = strdoop(CC->quickroom.QRname);
609 sprintf(buf, "room_%s@%s", CC->quickroom.QRname, config.c_fqdn);
610 for (i=0; i<strlen(buf); ++i) {
611 if (isspace(buf[i])) buf[i]='_';
612 buf[i] = tolower(buf[i]);
614 msg->cm_fields['F'] = strdoop(buf);
616 fseek(sc->digestfp, 0L, SEEK_END);
617 msglen = ftell(sc->digestfp);
619 msg->cm_fields['M'] = mallok(msglen + 1);
620 fseek(sc->digestfp, 0L, SEEK_SET);
621 fread(msg->cm_fields['M'], (size_t)msglen, 1, sc->digestfp);
622 msg->cm_fields['M'][msglen] = 0;
624 fclose(sc->digestfp);
627 msgnum = CtdlSubmitMsg(msg, NULL, SMTP_SPOOLOUT_ROOM);
628 CtdlFreeMessage(msg);
630 /* Now generate the delivery instructions */
633 * Figure out how big a buffer we need to allocate
635 for (nptr = sc->digestrecps; nptr != NULL; nptr = nptr->next) {
636 instr_len = instr_len + strlen(nptr->name);
642 lprintf(9, "Generating delivery instructions\n");
643 instr = mallok(instr_len);
645 lprintf(1, "Cannot allocate %ld bytes for instr...\n",
649 snprintf(instr, instr_len,
650 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
651 "bounceto|postmaster@%s\n" ,
652 SPOOLMIME, msgnum, (long)time(NULL), config.c_fqdn );
654 /* Generate delivery instructions for each recipient */
655 for (nptr = sc->digestrecps; nptr != NULL; nptr = nptr->next) {
656 size_t tmp = strlen(instr);
657 snprintf(&instr[tmp], instr_len - tmp,
658 "remote|%s|0||\n", nptr->name);
662 * Generate a message from the instructions
664 imsg = mallok(sizeof(struct CtdlMessage));
665 memset(imsg, 0, sizeof(struct CtdlMessage));
666 imsg->cm_magic = CTDLMESSAGE_MAGIC;
667 imsg->cm_anon_type = MES_NORMAL;
668 imsg->cm_format_type = FMT_RFC822;
669 imsg->cm_fields['A'] = strdoop("Citadel");
670 imsg->cm_fields['M'] = instr;
672 /* Save delivery instructions in spoolout room */
673 CtdlSubmitMsg(imsg, NULL, SMTP_SPOOLOUT_ROOM);
674 CtdlFreeMessage(imsg);
679 * Batch up and send all outbound traffic from the current room
681 void network_spoolout_room(char *room_to_spool) {
686 struct SpoolControl sc;
687 struct namelist *nptr;
691 lprintf(7, "Spooling <%s>\n", room_to_spool);
692 if (getroom(&CC->quickroom, room_to_spool) != 0) {
693 lprintf(1, "ERROR: cannot load <%s>\n", room_to_spool);
697 memset(&sc, 0, sizeof(struct SpoolControl));
698 assoc_file_name(filename, sizeof filename, &CC->quickroom, "netconfigs");
700 begin_critical_section(S_NETCONFIGS);
701 end_critical_section(S_NETCONFIGS);
703 fp = fopen(filename, "r");
705 lprintf(7, "Outbound batch processing skipped for <%s>\n",
706 CC->quickroom.QRname);
707 end_critical_section(S_NETCONFIGS);
711 lprintf(5, "Outbound batch processing started for <%s>\n",
712 CC->quickroom.QRname);
714 while (fgets(buf, sizeof buf, fp) != NULL) {
715 buf[strlen(buf)-1] = 0;
717 extract(instr, buf, 0);
718 if (!strcasecmp(instr, "lastsent")) {
719 sc.lastsent = extract_long(buf, 1);
721 else if (!strcasecmp(instr, "listrecp")) {
722 nptr = (struct namelist *)
723 mallok(sizeof(struct namelist));
724 nptr->next = sc.listrecps;
725 extract(nptr->name, buf, 1);
728 else if (!strcasecmp(instr, "digestrecp")) {
729 nptr = (struct namelist *)
730 mallok(sizeof(struct namelist));
731 nptr->next = sc.digestrecps;
732 extract(nptr->name, buf, 1);
733 sc.digestrecps = nptr;
735 else if (!strcasecmp(instr, "ignet_push_share")) {
736 nptr = (struct namelist *)
737 mallok(sizeof(struct namelist));
738 nptr->next = sc.ignet_push_shares;
739 extract(nptr->name, buf, 1);
740 sc.ignet_push_shares = nptr;
743 linesize = strlen(buf);
744 sc.misc = realloc(sc.misc,
745 (miscsize + linesize + 2) );
746 sprintf(&sc.misc[miscsize], "%s\n", buf);
747 miscsize = miscsize + linesize + 1;
754 /* If there are digest recipients, we have to build a digest */
755 if (sc.digestrecps != NULL) {
756 sc.digestfp = tmpfile();
757 fprintf(sc.digestfp, "Content-type: text/plain\n\n");
760 /* Do something useful */
761 CtdlForEachMessage(MSGS_GT, sc.lastsent, NULL, NULL,
762 network_spool_msg, &sc);
764 /* If we wrote a digest, deliver it and then close it */
765 if (sc.digestfp != NULL) {
766 fprintf(sc.digestfp, " -----------------------------------"
767 "------------------------------------"
769 "You are subscribed to the '%s' "
773 network_deliver_digest(&sc); /* deliver and close */
776 /* Now rewrite the config file */
777 fp = fopen(filename, "w");
779 lprintf(1, "ERROR: cannot open %s: %s\n",
780 filename, strerror(errno));
783 fprintf(fp, "lastsent|%ld\n", sc.lastsent);
785 /* Write out the listrecps while freeing from memory at the
786 * same time. Am I clever or what? :)
788 while (sc.listrecps != NULL) {
789 fprintf(fp, "listrecp|%s\n", sc.listrecps->name);
790 nptr = sc.listrecps->next;
794 /* Do the same for digestrecps */
795 while (sc.digestrecps != NULL) {
796 fprintf(fp, "digestrecp|%s\n", sc.digestrecps->name);
797 nptr = sc.digestrecps->next;
798 phree(sc.digestrecps);
799 sc.digestrecps = nptr;
801 while (sc.ignet_push_shares != NULL) {
802 fprintf(fp, "ignet_push_share|%s\n",
803 sc.ignet_push_shares->name);
804 nptr = sc.ignet_push_shares->next;
805 phree(sc.ignet_push_shares);
806 sc.ignet_push_shares = nptr;
808 if (sc.misc != NULL) {
809 fwrite(sc.misc, strlen(sc.misc), 1, fp);
815 end_critical_section(S_NETCONFIGS);
817 lprintf(5, "Outbound batch processing finished for <%s>\n",
818 CC->quickroom.QRname);
823 * Batch up and send all outbound traffic from the current room
825 void network_queue_room(struct quickroom *qrbuf, void *data) {
826 struct RoomProcList *ptr;
828 ptr = (struct RoomProcList *) mallok(sizeof (struct RoomProcList));
829 if (ptr == NULL) return;
831 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
838 * Learn topology from path fields
840 void network_learn_topology(char *node, char *path) {
842 struct NetMap *nmptr;
846 if (num_tokens(path, '!') < 3) return;
847 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
848 if (!strcasecmp(nmptr->nodename, node)) {
849 extract_token(nmptr->nexthop, path, 0, '!');
850 nmptr->lastcontact = time(NULL);
855 /* If we got here then it's not in the map, so add it. */
856 nmptr = (struct NetMap *) mallok(sizeof (struct NetMap));
857 strcpy(nmptr->nodename, node);
858 nmptr->lastcontact = time(NULL);
859 extract_token(nmptr->nexthop, path, 0, '!');
860 nmptr->next = the_netmap;
868 * Bounce a message back to the sender
870 void network_bounce(struct CtdlMessage *msg, char *reason) {
871 char *oldpath = NULL;
873 char bouncesource[SIZ];
875 struct recptypes *valid = NULL;
876 char force_room[ROOMNAMELEN];
877 static int serialnum = 0;
880 lprintf(9, "entering network_bounce()\n");
882 if (msg == NULL) return;
884 snprintf(bouncesource, sizeof bouncesource, "%s@%s", BOUNCESOURCE, config.c_nodename);
887 * Give it a fresh message ID
889 if (msg->cm_fields['I'] != NULL) {
890 phree(msg->cm_fields['I']);
892 snprintf(buf, sizeof buf, "%ld.%04lx.%04x@%s",
893 (long)time(NULL), (long)getpid(), ++serialnum, config.c_fqdn);
894 msg->cm_fields['I'] = strdoop(buf);
897 * FIXME ... right now we're just sending a bounce; we really want to
898 * include the text of the bounced message.
900 if (msg->cm_fields['M'] != NULL) {
901 phree(msg->cm_fields['M']);
903 msg->cm_fields['M'] = strdoop(reason);
904 msg->cm_format_type = 0;
907 * Turn the message around
909 if (msg->cm_fields['R'] == NULL) {
910 phree(msg->cm_fields['R']);
913 if (msg->cm_fields['D'] == NULL) {
914 phree(msg->cm_fields['D']);
917 snprintf(recipient, sizeof recipient, "%s@%s",
918 msg->cm_fields['A'], msg->cm_fields['N']);
920 if (msg->cm_fields['A'] == NULL) {
921 phree(msg->cm_fields['A']);
924 if (msg->cm_fields['N'] == NULL) {
925 phree(msg->cm_fields['N']);
928 msg->cm_fields['A'] = strdoop(BOUNCESOURCE);
929 msg->cm_fields['N'] = strdoop(config.c_nodename);
932 /* prepend our node to the path */
933 if (msg->cm_fields['P'] != NULL) {
934 oldpath = msg->cm_fields['P'];
935 msg->cm_fields['P'] = NULL;
938 oldpath = strdoop("unknown_user");
940 size = strlen(oldpath) + SIZ;
941 msg->cm_fields['P'] = mallok(size);
942 snprintf(msg->cm_fields['P'], size, "%s!%s", config.c_nodename, oldpath);
945 /* Now submit the message */
946 valid = validate_recipients(recipient);
947 if (valid != NULL) if (valid->num_error > 0) {
951 if ( (valid == NULL) || (!strcasecmp(recipient, bouncesource)) ) {
952 strcpy(force_room, config.c_aideroom);
955 strcpy(force_room, "");
957 if ( (valid == NULL) && (strlen(force_room) == 0) ) {
958 strcpy(force_room, config.c_aideroom);
960 CtdlSubmitMsg(msg, valid, force_room);
963 if (valid != NULL) phree(valid);
964 CtdlFreeMessage(msg);
965 lprintf(9, "leaving network_bounce()\n");
972 * Process a buffer containing a single message from a single file
973 * from the inbound queue
975 void network_process_buffer(char *buffer, long size) {
976 struct CtdlMessage *msg;
979 struct recptypes *recp = NULL;
980 char target_room[ROOMNAMELEN];
981 struct ser_ret sermsg;
982 char *oldpath = NULL;
987 /* Set default target room to trash */
988 strcpy(target_room, TWITROOM);
990 /* Load the message into memory */
991 msg = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
992 memset(msg, 0, sizeof(struct CtdlMessage));
993 msg->cm_magic = CTDLMESSAGE_MAGIC;
994 msg->cm_anon_type = buffer[1];
995 msg->cm_format_type = buffer[2];
997 for (pos = 3; pos < size; ++pos) {
999 msg->cm_fields[field] = strdoop(&buffer[pos+1]);
1000 pos = pos + strlen(&buffer[(int)pos]);
1003 /* Check for message routing */
1004 if (msg->cm_fields['D'] != NULL) {
1005 if (strcasecmp(msg->cm_fields['D'], config.c_nodename)) {
1007 /* route the message */
1008 if (is_valid_node(NULL, NULL,
1009 msg->cm_fields['D']) == 0) {
1011 /* prepend our node to the path */
1012 if (msg->cm_fields['P'] != NULL) {
1013 oldpath = msg->cm_fields['P'];
1014 msg->cm_fields['P'] = NULL;
1017 oldpath = strdoop("unknown_user");
1019 size = strlen(oldpath) + SIZ;
1020 msg->cm_fields['P'] = mallok(size);
1021 snprintf(msg->cm_fields['P'], size, "%s!%s",
1022 config.c_nodename, oldpath);
1025 /* serialize the message */
1026 serialize_message(&sermsg, msg);
1029 snprintf(filename, sizeof filename,
1030 "./network/spoolout/%s",
1031 msg->cm_fields['D']);
1032 fp = fopen(filename, "ab");
1039 CtdlFreeMessage(msg);
1043 else { /* invalid destination node name */
1046 "A message you sent could not be delivered due to an invalid destination node"
1047 " name. Please check the address and try sending the message again.\n");
1056 * Check to see if we already have a copy of this message
1058 if (network_usetable(msg) != 0) {
1059 snprintf(buf, sizeof buf,
1060 "Loopzapper rejected message <%s> "
1061 "from <%s> in <%s> @ <%s>\n",
1062 ((msg->cm_fields['I']!=NULL)?(msg->cm_fields['I']):""),
1063 ((msg->cm_fields['A']!=NULL)?(msg->cm_fields['A']):""),
1064 ((msg->cm_fields['O']!=NULL)?(msg->cm_fields['O']):""),
1065 ((msg->cm_fields['N']!=NULL)?(msg->cm_fields['N']):"")
1068 CtdlFreeMessage(msg);
1073 /* Learn network topology from the path */
1074 if ((msg->cm_fields['N'] != NULL) && (msg->cm_fields['P'] != NULL)) {
1075 network_learn_topology(msg->cm_fields['N'],
1076 msg->cm_fields['P']);
1079 /* Does it have a recipient? If so, validate it... */
1080 if (msg->cm_fields['R'] != NULL) {
1081 recp = validate_recipients(msg->cm_fields['R']);
1082 if (recp != NULL) if (recp->num_error > 0) {
1084 "A message you sent could not be delivered due to an invalid address.\n"
1085 "Please check the address and try sending the message again.\n");
1090 strcpy(target_room, ""); /* no target room if mail */
1093 else if (msg->cm_fields['C'] != NULL) {
1094 safestrncpy(target_room,
1095 msg->cm_fields['C'],
1096 sizeof target_room);
1099 else if (msg->cm_fields['O'] != NULL) {
1100 safestrncpy(target_room,
1101 msg->cm_fields['O'],
1102 sizeof target_room);
1105 /* Strip out fields that are only relevant during transit */
1106 if (msg->cm_fields['D'] != NULL) {
1107 phree(msg->cm_fields['D']);
1108 msg->cm_fields['D'] = NULL;
1110 if (msg->cm_fields['C'] != NULL) {
1111 phree(msg->cm_fields['C']);
1112 msg->cm_fields['C'] = NULL;
1115 /* save the message into a room */
1116 if (PerformNetprocHooks(msg, target_room) == 0) {
1117 msg->cm_flags = CM_SKIP_HOOKS;
1118 CtdlSubmitMsg(msg, recp, target_room);
1120 CtdlFreeMessage(msg);
1126 * Process a single message from a single file from the inbound queue
1128 void network_process_message(FILE *fp, long msgstart, long msgend) {
1133 hold_pos = ftell(fp);
1134 size = msgend - msgstart + 1;
1135 buffer = mallok(size);
1136 if (buffer != NULL) {
1137 fseek(fp, msgstart, SEEK_SET);
1138 fread(buffer, size, 1, fp);
1139 network_process_buffer(buffer, size);
1143 fseek(fp, hold_pos, SEEK_SET);
1148 * Process a single file from the inbound queue
1150 void network_process_file(char *filename) {
1152 long msgstart = (-1L);
1153 long msgend = (-1L);
1157 lprintf(7, "network: processing <%s>\n", filename);
1159 fp = fopen(filename, "rb");
1161 lprintf(5, "Error opening %s: %s\n",
1162 filename, strerror(errno));
1166 /* Look for messages in the data stream and break them out */
1167 while (ch = getc(fp), ch >= 0) {
1170 if (msgstart >= 0L) {
1171 msgend = msgcur - 1;
1172 network_process_message(fp, msgstart, msgend);
1180 msgend = msgcur - 1;
1181 if (msgstart >= 0L) {
1182 network_process_message(fp, msgstart, msgend);
1191 * Process anything in the inbound queue
1193 void network_do_spoolin(void) {
1198 dp = opendir("./network/spoolin");
1199 if (dp == NULL) return;
1201 while (d = readdir(dp), d != NULL) {
1202 snprintf(filename, sizeof filename, "./network/spoolin/%s", d->d_name);
1203 network_process_file(filename);
1215 * receive network spool from the remote system
1217 void receive_spool(int sock, char *remote_nodename) {
1219 long bytes_received;
1221 static char pbuf[IGNET_PACKET_SIZE];
1222 char tempfilename[PATH_MAX];
1226 strcpy(tempfilename, tmpnam(NULL));
1227 if (sock_puts(sock, "NDOP") < 0) return;
1228 if (sock_gets(sock, buf) < 0) return;
1229 lprintf(9, "<%s\n", buf);
1230 if (buf[0] != '2') {
1233 download_len = extract_long(&buf[4], 0);
1235 bytes_received = 0L;
1236 fp = fopen(tempfilename, "w");
1238 lprintf(9, "cannot open download file locally: %s\n",
1243 while (bytes_received < download_len) {
1244 snprintf(buf, sizeof buf, "READ %ld|%ld",
1246 ((download_len - bytes_received > IGNET_PACKET_SIZE)
1247 ? IGNET_PACKET_SIZE : (download_len - bytes_received)));
1248 if (sock_puts(sock, buf) < 0) {
1250 unlink(tempfilename);
1253 if (sock_gets(sock, buf) < 0) {
1255 unlink(tempfilename);
1258 if (buf[0] == '6') {
1259 plen = extract_long(&buf[4], 0);
1260 if (sock_read(sock, pbuf, plen) < 0) {
1262 unlink(tempfilename);
1265 fwrite((char *) pbuf, plen, 1, fp);
1266 bytes_received = bytes_received + plen;
1271 if (sock_puts(sock, "CLOS") < 0) {
1272 unlink(tempfilename);
1275 if (sock_gets(sock, buf) < 0) {
1276 unlink(tempfilename);
1279 lprintf(9, "%s\n", buf);
1280 snprintf(buf, sizeof buf, "mv %s ./network/spoolin/%s.%ld",
1281 tempfilename, remote_nodename, (long) getpid());
1288 * transmit network spool to the remote system
1290 void transmit_spool(int sock, char *remote_nodename)
1295 long bytes_to_write, thisblock;
1299 if (sock_puts(sock, "NUOP") < 0) return;
1300 if (sock_gets(sock, buf) < 0) return;
1301 lprintf(9, "<%s\n", buf);
1302 if (buf[0] != '2') {
1306 snprintf(sfname, sizeof sfname, "./network/spoolout/%s", remote_nodename);
1307 fd = open(sfname, O_RDONLY);
1309 if (errno == ENOENT) {
1310 lprintf(9, "Nothing to send.\n");
1312 lprintf(5, "cannot open upload file locally: %s\n",
1317 while (plen = (long) read(fd, pbuf, IGNET_PACKET_SIZE), plen > 0L) {
1318 bytes_to_write = plen;
1319 while (bytes_to_write > 0L) {
1320 snprintf(buf, sizeof buf, "WRIT %ld", bytes_to_write);
1321 if (sock_puts(sock, buf) < 0) {
1325 if (sock_gets(sock, buf) < 0) {
1329 thisblock = atol(&buf[4]);
1330 if (buf[0] == '7') {
1331 if (sock_write(sock, pbuf,
1332 (int) thisblock) < 0) {
1336 bytes_to_write = bytes_to_write - thisblock;
1345 if (sock_puts(sock, "UCLS 1") < 0) return;
1346 if (sock_gets(sock, buf) < 0) return;
1347 lprintf(9, "<%s\n", buf);
1348 if (buf[0] == '2') {
1356 * Poll one Citadel node (called by network_poll_other_citadel_nodes() below)
1358 void network_poll_node(char *node, char *secret, char *host, char *port) {
1362 if (network_talking_to(node, NTT_CHECK)) return;
1363 network_talking_to(node, NTT_ADD);
1364 lprintf(5, "Polling node <%s> at %s:%s\n", node, host, port);
1366 sock = sock_connect(host, port, "tcp");
1368 lprintf(7, "Could not connect: %s\n", strerror(errno));
1369 network_talking_to(node, NTT_REMOVE);
1373 lprintf(9, "Connected!\n");
1375 /* Read the server greeting */
1376 if (sock_gets(sock, buf) < 0) goto bail;
1377 lprintf(9, ">%s\n", buf);
1379 /* Identify ourselves */
1380 snprintf(buf, sizeof buf, "NETP %s|%s", config.c_nodename, secret);
1381 lprintf(9, "<%s\n", buf);
1382 if (sock_puts(sock, buf) <0) goto bail;
1383 if (sock_gets(sock, buf) < 0) goto bail;
1384 lprintf(9, ">%s\n", buf);
1385 if (buf[0] != '2') goto bail;
1387 /* At this point we are authenticated. */
1388 receive_spool(sock, node);
1389 transmit_spool(sock, node);
1391 sock_puts(sock, "QUIT");
1392 bail: sock_close(sock);
1393 network_talking_to(node, NTT_REMOVE);
1399 * Poll other Citadel nodes and transfer inbound/outbound network data.
1401 void network_poll_other_citadel_nodes(void) {
1402 char *ignetcfg = NULL;
1410 ignetcfg = CtdlGetSysConfig(IGNETCFG);
1411 if (ignetcfg == NULL) return; /* no nodes defined */
1413 /* Use the string tokenizer to grab one line at a time */
1414 for (i=0; i<num_tokens(ignetcfg, '\n'); ++i) {
1415 extract_token(linebuf, ignetcfg, i, '\n');
1416 extract(node, linebuf, 0);
1417 extract(secret, linebuf, 1);
1418 extract(host, linebuf, 2);
1419 extract(port, linebuf, 3);
1420 if ( (strlen(node) > 0) && (strlen(secret) > 0)
1421 && (strlen(host) > 0) && strlen(port) > 0) {
1422 network_poll_node(node, secret, host, port);
1436 * network_do_queue()
1438 * Run through the rooms doing various types of network stuff.
1440 void network_do_queue(void) {
1441 static int doing_queue = 0;
1442 static time_t last_run = 0L;
1443 struct RoomProcList *ptr;
1446 * Run no more frequently than once every n seconds
1448 if ( (time(NULL) - last_run) < config.c_net_freq ) return;
1451 * This is a simple concurrency check to make sure only one queue run
1452 * is done at a time. We could do this with a mutex, but since we
1453 * don't really require extremely fine granularity here, we'll do it
1454 * with a static variable instead.
1456 if (doing_queue) return;
1458 last_run = time(NULL);
1461 * Poll other Citadel nodes.
1463 network_poll_other_citadel_nodes();
1466 * Load the network map and filter list into memory.
1469 filterlist = load_filter_list();
1472 * Go ahead and run the queue
1474 lprintf(7, "network: loading outbound queue\n");
1475 ForEachRoom(network_queue_room, NULL);
1477 lprintf(7, "network: running outbound queue\n");
1478 while (rplist != NULL) {
1479 network_spoolout_room(rplist->name);
1481 rplist = rplist->next;
1485 lprintf(7, "network: processing inbound queue\n");
1486 network_do_spoolin();
1488 /* Save the network map back to disk */
1489 write_network_map();
1491 /* Free the filter list in memory */
1492 free_filter_list(filterlist);
1495 lprintf(7, "network: queue run completed\n");
1501 * cmd_netp() - authenticate to the server as another Citadel node polling
1502 * for network traffic
1504 void cmd_netp(char *cmdbuf)
1512 extract(node, cmdbuf, 0);
1513 extract(pass, cmdbuf, 1);
1515 if (is_valid_node(nexthop, secret, node) != 0) {
1516 cprintf("%d authentication failed\n", ERROR);
1520 if (strcasecmp(pass, secret)) {
1521 cprintf("%d authentication failed\n", ERROR);
1525 if (network_talking_to(node, NTT_CHECK)) {
1526 cprintf("%d Already talking to %s right now\n", ERROR, node);
1530 safestrncpy(CC->net_node, node, sizeof CC->net_node);
1531 network_talking_to(node, NTT_ADD);
1532 cprintf("%d authenticated as network node '%s'\n", CIT_OK,
1541 * Module entry point
1543 char *Dynamic_Module_Init(void)
1545 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
1546 CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");
1547 CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");
1548 CtdlRegisterSessionHook(network_do_queue, EVT_TIMER);