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
25 * Duration of time (in seconds) after which pending list subscribe/unsubscribe
26 * requests that have not been confirmed will be deleted.
28 #define EXP 259200 /* three days */
39 #include <sys/types.h>
41 #if TIME_WITH_SYS_TIME
42 # include <sys/time.h>
46 # include <sys/time.h>
57 #include "sysdep_decls.h"
58 #include "citserver.h"
61 #include "dynloader.h"
68 #include "internet_addressing.h"
69 #include "serv_network.h"
70 #include "clientsocket.h"
79 * When we do network processing, it's accomplished in two passes; one to
80 * gather a list of rooms and one to actually do them. It's ok that rplist
81 * is global; this process *only* runs as part of the housekeeping loop and
82 * therefore only one will run at a time.
84 struct RoomProcList *rplist = NULL;
87 * We build a map of network nodes during processing.
89 struct NetMap *the_netmap = NULL;
92 * Keep track of what messages to reject
94 struct FilterList *load_filter_list(void) {
95 char *serialized_list = NULL;
98 struct FilterList *newlist = NULL;
99 struct FilterList *nptr;
101 serialized_list = CtdlGetSysConfig(FILTERLIST);
102 if (serialized_list == NULL) return(NULL); /* if null, no entries */
104 /* Use the string tokenizer to grab one line at a time */
105 for (i=0; i<num_tokens(serialized_list, '\n'); ++i) {
106 extract_token(buf, serialized_list, i, '\n');
107 nptr = (struct FilterList *) mallok(sizeof(struct FilterList));
108 extract(nptr->fl_user, buf, 0);
109 striplt(nptr->fl_user);
110 extract(nptr->fl_room, buf, 1);
111 striplt(nptr->fl_room);
112 extract(nptr->fl_node, buf, 2);
113 striplt(nptr->fl_node);
115 /* Cowardly refuse to add an any/any/any entry that would
116 * end up filtering every single message.
118 if (strlen(nptr->fl_user) + strlen(nptr->fl_room)
119 + strlen(nptr->fl_node) == 0) {
123 nptr->next = newlist;
128 phree(serialized_list);
133 void free_filter_list(struct FilterList *fl) {
134 if (fl == NULL) return;
135 free_filter_list(fl->next);
142 * Check the use table. This is a list of messages which have recently
143 * arrived on the system. It is maintained and queried to prevent the same
144 * message from being entered into the database multiple times if it happens
145 * to arrive multiple times by accident.
147 int network_usetable(struct CtdlMessage *msg) {
150 struct cdbdata *cdbut;
153 /* Bail out if we can't generate a message ID */
157 if (msg->cm_fields['I'] == NULL) {
160 if (strlen(msg->cm_fields['I']) == 0) {
164 /* Generate the message ID */
165 strcpy(msgid, msg->cm_fields['I']);
166 if (haschar(msgid, '@') == 0) {
168 if (msg->cm_fields['N'] != NULL) {
169 strcat(msgid, msg->cm_fields['N']);
176 cdbut = cdb_fetch(CDB_USETABLE, msgid, strlen(msgid));
182 /* If we got to this point, it's unique: add it. */
183 strcpy(ut.ut_msgid, msgid);
184 ut.ut_timestamp = time(NULL);
185 cdb_store(CDB_USETABLE, msgid, strlen(msgid),
186 &ut, sizeof(struct UseTable) );
192 * Read the network map from its configuration file into memory.
194 void read_network_map(void) {
195 char *serialized_map = NULL;
198 struct NetMap *nmptr;
200 serialized_map = CtdlGetSysConfig(IGNETMAP);
201 if (serialized_map == NULL) return; /* if null, no entries */
203 /* Use the string tokenizer to grab one line at a time */
204 for (i=0; i<num_tokens(serialized_map, '\n'); ++i) {
205 extract_token(buf, serialized_map, i, '\n');
206 nmptr = (struct NetMap *) mallok(sizeof(struct NetMap));
207 extract(nmptr->nodename, buf, 0);
208 nmptr->lastcontact = extract_long(buf, 1);
209 extract(nmptr->nexthop, buf, 2);
210 nmptr->next = the_netmap;
214 phree(serialized_map);
219 * Write the network map from memory back to the configuration file.
221 void write_network_map(void) {
222 char *serialized_map = NULL;
223 struct NetMap *nmptr;
225 serialized_map = strdoop("");
227 if (the_netmap != NULL) {
228 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
229 serialized_map = reallok(serialized_map,
230 (strlen(serialized_map)+SIZ) );
231 if (strlen(nmptr->nodename) > 0) {
232 snprintf(&serialized_map[strlen(serialized_map)],
236 (long)nmptr->lastcontact,
242 CtdlPutSysConfig(IGNETMAP, serialized_map);
243 phree(serialized_map);
245 /* Now free the list */
246 while (the_netmap != NULL) {
247 nmptr = the_netmap->next;
256 * Check the network map and determine whether the supplied node name is
257 * valid. If it is not a neighbor node, supply the name of a neighbor node
258 * which is the next hop. If it *is* a neighbor node, we also fill in the
261 int is_valid_node(char *nexthop, char *secret, char *node) {
262 char *ignetcfg = NULL;
267 struct NetMap *nmptr;
274 * First try the neighbor nodes
276 ignetcfg = CtdlGetSysConfig(IGNETCFG);
277 if (ignetcfg == NULL) {
278 if (nexthop != NULL) {
285 if (nexthop != NULL) {
289 /* Use the string tokenizer to grab one line at a time */
290 for (i=0; i<num_tokens(ignetcfg, '\n'); ++i) {
291 extract_token(linebuf, ignetcfg, i, '\n');
292 extract(buf, linebuf, 0);
293 if (!strcasecmp(buf, node)) {
294 if (nexthop != NULL) {
297 if (secret != NULL) {
298 extract(secret, linebuf, 1);
306 return(retval); /* yup, it's a direct neighbor */
310 * If we get to this point we have to see if we know the next hop
312 if (the_netmap != NULL) {
313 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
314 if (!strcasecmp(nmptr->nodename, node)) {
315 if (nexthop != NULL) {
316 strcpy(nexthop, nmptr->nexthop);
324 * If we get to this point, the supplied node name is bogus.
326 lprintf(5, "Invalid node name <%s>\n", node);
334 void cmd_gnet(char *argbuf) {
339 if (CtdlAccessCheck(ac_room_aide)) return;
340 assoc_file_name(filename, sizeof filename, &CC->quickroom, "netconfigs");
341 cprintf("%d Network settings for room #%ld <%s>\n",
343 CC->quickroom.QRnumber, CC->quickroom.QRname);
345 fp = fopen(filename, "r");
347 while (fgets(buf, sizeof buf, fp) != NULL) {
348 buf[strlen(buf)-1] = 0;
349 cprintf("%s\n", buf);
358 void cmd_snet(char *argbuf) {
359 char tempfilename[SIZ];
364 if (CtdlAccessCheck(ac_room_aide)) return;
365 safestrncpy(tempfilename, tmpnam(NULL), sizeof tempfilename);
366 assoc_file_name(filename, sizeof filename, &CC->quickroom, "netconfigs");
368 fp = fopen(tempfilename, "w");
370 cprintf("%d Cannot open %s: %s\n",
371 ERROR+INTERNAL_ERROR,
376 cprintf("%d %s\n", SEND_LISTING, tempfilename);
377 while (client_gets(buf), strcmp(buf, "000")) {
378 fprintf(fp, "%s\n", buf);
382 /* Now copy the temp file to its permanent location
383 * (We use /bin/mv instead of link() because they may be on
384 * different filesystems)
387 snprintf(buf, sizeof buf, "/bin/mv %s %s", tempfilename, filename);
388 begin_critical_section(S_NETCONFIGS);
390 end_critical_section(S_NETCONFIGS);
395 * Spools out one message from the list.
397 void network_spool_msg(long msgnum, void *userdata) {
398 struct SpoolControl *sc;
401 char *newpath = NULL;
403 size_t instr_len = SIZ;
404 struct CtdlMessage *msg = NULL;
405 struct CtdlMessage *imsg;
406 struct namelist *nptr;
407 struct ser_ret sermsg;
413 int delete_after_send = 0; /* Set to 1 to delete after spooling */
415 sc = (struct SpoolControl *)userdata;
418 * Process mailing list recipients
420 if (sc->listrecps != NULL) {
422 /* First, copy it to the spoolout room */
423 err = CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, msgnum, 0);
424 if (err != 0) return;
427 * Figure out how big a buffer we need to allocate
429 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
430 instr_len = instr_len + strlen(nptr->name);
436 lprintf(9, "Generating delivery instructions\n");
437 instr = mallok(instr_len);
439 lprintf(1, "Cannot allocate %ld bytes for instr...\n",
443 snprintf(instr, instr_len,
444 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
445 "bounceto|postmaster@%s\n" ,
446 SPOOLMIME, msgnum, (long)time(NULL), config.c_fqdn );
448 /* Generate delivery instructions for each recipient */
449 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
450 size_t tmp = strlen(instr);
451 snprintf(&instr[tmp], instr_len - tmp,
452 "remote|%s|0||\n", nptr->name);
456 * Generate a message from the instructions
458 imsg = mallok(sizeof(struct CtdlMessage));
459 memset(imsg, 0, sizeof(struct CtdlMessage));
460 imsg->cm_magic = CTDLMESSAGE_MAGIC;
461 imsg->cm_anon_type = MES_NORMAL;
462 imsg->cm_format_type = FMT_RFC822;
463 imsg->cm_fields['A'] = strdoop("Citadel");
464 imsg->cm_fields['M'] = instr;
466 /* Save delivery instructions in spoolout room */
467 CtdlSubmitMsg(imsg, NULL, SMTP_SPOOLOUT_ROOM);
468 CtdlFreeMessage(imsg);
472 * Process digest recipients
474 if ((sc->digestrecps != NULL) && (sc->digestfp != NULL)) {
475 fprintf(sc->digestfp, " -----------------------------------"
476 "------------------------------------"
478 CtdlRedirectOutput(sc->digestfp, -1);
479 CtdlOutputMsg(msgnum, MT_RFC822, HEADERS_ALL, 0, 0);
480 CtdlRedirectOutput(NULL, -1);
481 sc->num_msgs_spooled += 1;
485 * Process IGnet push shares
487 if (sc->ignet_push_shares != NULL) {
489 msg = CtdlFetchMessage(msgnum);
493 /* Prepend our node name to the Path field whenever
494 * sending a message to another IGnet node
496 if (msg->cm_fields['P'] == NULL) {
497 msg->cm_fields['P'] = strdoop("username");
499 newpath_len = strlen(msg->cm_fields['P']) +
500 strlen(config.c_nodename) + 2;
501 newpath = mallok(newpath_len);
502 snprintf(newpath, newpath_len, "%s!%s",
503 config.c_nodename, msg->cm_fields['P']);
504 phree(msg->cm_fields['P']);
505 msg->cm_fields['P'] = newpath;
508 * Force the message to appear in the correct room
509 * on the far end by setting the C field correctly
511 if (msg->cm_fields['C'] != NULL) {
512 phree(msg->cm_fields['C']);
514 msg->cm_fields['C'] = strdoop(CC->quickroom.QRname);
517 * Determine if this message is set to be deleted
518 * after sending out on the network
520 if (msg->cm_fields['S'] != NULL) {
521 if (!strcasecmp(msg->cm_fields['S'],
523 delete_after_send = 1;
528 * Now serialize it for transmission
530 serialize_message(&sermsg, msg);
532 /* Now send it to every node */
533 for (nptr = sc->ignet_push_shares; nptr != NULL;
538 /* Check for valid node name */
539 if (is_valid_node(NULL,NULL,nptr->name) != 0) {
540 lprintf(3, "Invalid node <%s>\n",
545 /* Check for split horizon */
546 lprintf(9, "Path is %s\n", msg->cm_fields['P']);
547 bang = num_tokens(msg->cm_fields['P'], '!');
548 if (bang > 1) for (i=0; i<(bang-1); ++i) {
549 extract_token(buf, msg->cm_fields['P'],
551 if (!strcasecmp(buf, nptr->name)) {
556 /* Send the message */
558 snprintf(filename, sizeof filename,
559 "./network/spoolout/%s",
561 fp = fopen(filename, "ab");
570 CtdlFreeMessage(msg);
574 /* update lastsent */
575 sc->lastsent = msgnum;
577 /* Delete this message if delete-after-send is set */
578 if (delete_after_send) {
579 CtdlDeleteMessages(CC->quickroom.QRname, msgnum, "");
586 * Deliver digest messages
588 void network_deliver_digest(struct SpoolControl *sc) {
591 struct CtdlMessage *msg;
595 size_t instr_len = SIZ;
596 struct CtdlMessage *imsg;
597 struct namelist *nptr;
599 if (sc->num_msgs_spooled < 1) {
600 fclose(sc->digestfp);
605 msg = mallok(sizeof(struct CtdlMessage));
606 memset(msg, 0, sizeof(struct CtdlMessage));
607 msg->cm_magic = CTDLMESSAGE_MAGIC;
608 msg->cm_format_type = FMT_RFC822;
609 msg->cm_anon_type = MES_NORMAL;
611 sprintf(buf, "%ld", time(NULL));
612 msg->cm_fields['T'] = strdoop(buf);
613 msg->cm_fields['A'] = strdoop(CC->quickroom.QRname);
614 msg->cm_fields['U'] = strdoop(CC->quickroom.QRname);
615 sprintf(buf, "room_%s@%s", CC->quickroom.QRname, config.c_fqdn);
616 for (i=0; i<strlen(buf); ++i) {
617 if (isspace(buf[i])) buf[i]='_';
618 buf[i] = tolower(buf[i]);
620 msg->cm_fields['F'] = strdoop(buf);
622 fseek(sc->digestfp, 0L, SEEK_END);
623 msglen = ftell(sc->digestfp);
625 msg->cm_fields['M'] = mallok(msglen + 1);
626 fseek(sc->digestfp, 0L, SEEK_SET);
627 fread(msg->cm_fields['M'], (size_t)msglen, 1, sc->digestfp);
628 msg->cm_fields['M'][msglen] = 0;
630 fclose(sc->digestfp);
633 msgnum = CtdlSubmitMsg(msg, NULL, SMTP_SPOOLOUT_ROOM);
634 CtdlFreeMessage(msg);
636 /* Now generate the delivery instructions */
639 * Figure out how big a buffer we need to allocate
641 for (nptr = sc->digestrecps; nptr != NULL; nptr = nptr->next) {
642 instr_len = instr_len + strlen(nptr->name);
648 lprintf(9, "Generating delivery instructions\n");
649 instr = mallok(instr_len);
651 lprintf(1, "Cannot allocate %ld bytes for instr...\n",
655 snprintf(instr, instr_len,
656 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
657 "bounceto|postmaster@%s\n" ,
658 SPOOLMIME, msgnum, (long)time(NULL), config.c_fqdn );
660 /* Generate delivery instructions for each recipient */
661 for (nptr = sc->digestrecps; nptr != NULL; nptr = nptr->next) {
662 size_t tmp = strlen(instr);
663 snprintf(&instr[tmp], instr_len - tmp,
664 "remote|%s|0||\n", nptr->name);
668 * Generate a message from the instructions
670 imsg = mallok(sizeof(struct CtdlMessage));
671 memset(imsg, 0, sizeof(struct CtdlMessage));
672 imsg->cm_magic = CTDLMESSAGE_MAGIC;
673 imsg->cm_anon_type = MES_NORMAL;
674 imsg->cm_format_type = FMT_RFC822;
675 imsg->cm_fields['A'] = strdoop("Citadel");
676 imsg->cm_fields['M'] = instr;
678 /* Save delivery instructions in spoolout room */
679 CtdlSubmitMsg(imsg, NULL, SMTP_SPOOLOUT_ROOM);
680 CtdlFreeMessage(imsg);
685 * Batch up and send all outbound traffic from the current room
687 void network_spoolout_room(char *room_to_spool) {
692 struct SpoolControl sc;
693 struct namelist *nptr;
696 int skipthisline = 0;
698 lprintf(7, "Spooling <%s>\n", room_to_spool);
699 if (getroom(&CC->quickroom, room_to_spool) != 0) {
700 lprintf(1, "ERROR: cannot load <%s>\n", room_to_spool);
704 memset(&sc, 0, sizeof(struct SpoolControl));
705 assoc_file_name(filename, sizeof filename, &CC->quickroom, "netconfigs");
707 begin_critical_section(S_NETCONFIGS);
708 end_critical_section(S_NETCONFIGS);
710 fp = fopen(filename, "r");
712 lprintf(7, "Outbound batch processing skipped for <%s>\n",
713 CC->quickroom.QRname);
714 end_critical_section(S_NETCONFIGS);
718 lprintf(5, "Outbound batch processing started for <%s>\n",
719 CC->quickroom.QRname);
721 while (fgets(buf, sizeof buf, fp) != NULL) {
722 buf[strlen(buf)-1] = 0;
724 extract(instr, buf, 0);
725 if (!strcasecmp(instr, "lastsent")) {
726 sc.lastsent = extract_long(buf, 1);
728 else if (!strcasecmp(instr, "listrecp")) {
729 nptr = (struct namelist *)
730 mallok(sizeof(struct namelist));
731 nptr->next = sc.listrecps;
732 extract(nptr->name, buf, 1);
735 else if (!strcasecmp(instr, "digestrecp")) {
736 nptr = (struct namelist *)
737 mallok(sizeof(struct namelist));
738 nptr->next = sc.digestrecps;
739 extract(nptr->name, buf, 1);
740 sc.digestrecps = nptr;
742 else if (!strcasecmp(instr, "ignet_push_share")) {
743 nptr = (struct namelist *)
744 mallok(sizeof(struct namelist));
745 nptr->next = sc.ignet_push_shares;
746 extract(nptr->name, buf, 1);
747 sc.ignet_push_shares = nptr;
750 /* Preserve 'other' lines ... *unless* they happen to
751 * be subscribe/unsubscribe pendings with expired
755 if (!strncasecmp(buf, "subpending|", 11)) {
756 if (time(NULL) - extract_long(buf, 4) > EXP) {
760 if (!strncasecmp(buf, "unsubpending|", 13)) {
761 if (time(NULL) - extract_long(buf, 3) > EXP) {
766 if (skipthisline == 0) {
767 linesize = strlen(buf);
768 sc.misc = realloc(sc.misc,
769 (miscsize + linesize + 2) );
770 sprintf(&sc.misc[miscsize], "%s\n", buf);
771 miscsize = miscsize + linesize + 1;
779 /* If there are digest recipients, we have to build a digest */
780 if (sc.digestrecps != NULL) {
781 sc.digestfp = tmpfile();
782 fprintf(sc.digestfp, "Content-type: text/plain\n\n");
785 /* Do something useful */
786 CtdlForEachMessage(MSGS_GT, sc.lastsent, NULL, NULL,
787 network_spool_msg, &sc);
789 /* If we wrote a digest, deliver it and then close it */
790 if (sc.digestfp != NULL) {
791 fprintf(sc.digestfp, " -----------------------------------"
792 "------------------------------------"
794 "You are subscribed to the '%s' "
798 network_deliver_digest(&sc); /* deliver and close */
801 /* Now rewrite the config file */
802 fp = fopen(filename, "w");
804 lprintf(1, "ERROR: cannot open %s: %s\n",
805 filename, strerror(errno));
808 fprintf(fp, "lastsent|%ld\n", sc.lastsent);
810 /* Write out the listrecps while freeing from memory at the
811 * same time. Am I clever or what? :)
813 while (sc.listrecps != NULL) {
814 fprintf(fp, "listrecp|%s\n", sc.listrecps->name);
815 nptr = sc.listrecps->next;
819 /* Do the same for digestrecps */
820 while (sc.digestrecps != NULL) {
821 fprintf(fp, "digestrecp|%s\n", sc.digestrecps->name);
822 nptr = sc.digestrecps->next;
823 phree(sc.digestrecps);
824 sc.digestrecps = nptr;
826 while (sc.ignet_push_shares != NULL) {
827 fprintf(fp, "ignet_push_share|%s\n",
828 sc.ignet_push_shares->name);
829 nptr = sc.ignet_push_shares->next;
830 phree(sc.ignet_push_shares);
831 sc.ignet_push_shares = nptr;
833 if (sc.misc != NULL) {
834 fwrite(sc.misc, strlen(sc.misc), 1, fp);
840 end_critical_section(S_NETCONFIGS);
842 lprintf(5, "Outbound batch processing finished for <%s>\n",
843 CC->quickroom.QRname);
848 * Batch up and send all outbound traffic from the current room
850 void network_queue_room(struct quickroom *qrbuf, void *data) {
851 struct RoomProcList *ptr;
853 ptr = (struct RoomProcList *) mallok(sizeof (struct RoomProcList));
854 if (ptr == NULL) return;
856 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
863 * Learn topology from path fields
865 void network_learn_topology(char *node, char *path) {
867 struct NetMap *nmptr;
871 if (num_tokens(path, '!') < 3) return;
872 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
873 if (!strcasecmp(nmptr->nodename, node)) {
874 extract_token(nmptr->nexthop, path, 0, '!');
875 nmptr->lastcontact = time(NULL);
880 /* If we got here then it's not in the map, so add it. */
881 nmptr = (struct NetMap *) mallok(sizeof (struct NetMap));
882 strcpy(nmptr->nodename, node);
883 nmptr->lastcontact = time(NULL);
884 extract_token(nmptr->nexthop, path, 0, '!');
885 nmptr->next = the_netmap;
893 * Bounce a message back to the sender
895 void network_bounce(struct CtdlMessage *msg, char *reason) {
896 char *oldpath = NULL;
898 char bouncesource[SIZ];
900 struct recptypes *valid = NULL;
901 char force_room[ROOMNAMELEN];
902 static int serialnum = 0;
905 lprintf(9, "entering network_bounce()\n");
907 if (msg == NULL) return;
909 snprintf(bouncesource, sizeof bouncesource, "%s@%s", BOUNCESOURCE, config.c_nodename);
912 * Give it a fresh message ID
914 if (msg->cm_fields['I'] != NULL) {
915 phree(msg->cm_fields['I']);
917 snprintf(buf, sizeof buf, "%ld.%04lx.%04x@%s",
918 (long)time(NULL), (long)getpid(), ++serialnum, config.c_fqdn);
919 msg->cm_fields['I'] = strdoop(buf);
922 * FIXME ... right now we're just sending a bounce; we really want to
923 * include the text of the bounced message.
925 if (msg->cm_fields['M'] != NULL) {
926 phree(msg->cm_fields['M']);
928 msg->cm_fields['M'] = strdoop(reason);
929 msg->cm_format_type = 0;
932 * Turn the message around
934 if (msg->cm_fields['R'] == NULL) {
935 phree(msg->cm_fields['R']);
938 if (msg->cm_fields['D'] == NULL) {
939 phree(msg->cm_fields['D']);
942 snprintf(recipient, sizeof recipient, "%s@%s",
943 msg->cm_fields['A'], msg->cm_fields['N']);
945 if (msg->cm_fields['A'] == NULL) {
946 phree(msg->cm_fields['A']);
949 if (msg->cm_fields['N'] == NULL) {
950 phree(msg->cm_fields['N']);
953 msg->cm_fields['A'] = strdoop(BOUNCESOURCE);
954 msg->cm_fields['N'] = strdoop(config.c_nodename);
957 /* prepend our node to the path */
958 if (msg->cm_fields['P'] != NULL) {
959 oldpath = msg->cm_fields['P'];
960 msg->cm_fields['P'] = NULL;
963 oldpath = strdoop("unknown_user");
965 size = strlen(oldpath) + SIZ;
966 msg->cm_fields['P'] = mallok(size);
967 snprintf(msg->cm_fields['P'], size, "%s!%s", config.c_nodename, oldpath);
970 /* Now submit the message */
971 valid = validate_recipients(recipient);
972 if (valid != NULL) if (valid->num_error > 0) {
976 if ( (valid == NULL) || (!strcasecmp(recipient, bouncesource)) ) {
977 strcpy(force_room, config.c_aideroom);
980 strcpy(force_room, "");
982 if ( (valid == NULL) && (strlen(force_room) == 0) ) {
983 strcpy(force_room, config.c_aideroom);
985 CtdlSubmitMsg(msg, valid, force_room);
988 if (valid != NULL) phree(valid);
989 CtdlFreeMessage(msg);
990 lprintf(9, "leaving network_bounce()\n");
997 * Process a buffer containing a single message from a single file
998 * from the inbound queue
1000 void network_process_buffer(char *buffer, long size) {
1001 struct CtdlMessage *msg;
1004 struct recptypes *recp = NULL;
1005 char target_room[ROOMNAMELEN];
1006 struct ser_ret sermsg;
1007 char *oldpath = NULL;
1012 /* Set default target room to trash */
1013 strcpy(target_room, TWITROOM);
1015 /* Load the message into memory */
1016 msg = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
1017 memset(msg, 0, sizeof(struct CtdlMessage));
1018 msg->cm_magic = CTDLMESSAGE_MAGIC;
1019 msg->cm_anon_type = buffer[1];
1020 msg->cm_format_type = buffer[2];
1022 for (pos = 3; pos < size; ++pos) {
1023 field = buffer[pos];
1024 msg->cm_fields[field] = strdoop(&buffer[pos+1]);
1025 pos = pos + strlen(&buffer[(int)pos]);
1028 /* Check for message routing */
1029 if (msg->cm_fields['D'] != NULL) {
1030 if (strcasecmp(msg->cm_fields['D'], config.c_nodename)) {
1032 /* route the message */
1033 if (is_valid_node(NULL, NULL,
1034 msg->cm_fields['D']) == 0) {
1036 /* prepend our node to the path */
1037 if (msg->cm_fields['P'] != NULL) {
1038 oldpath = msg->cm_fields['P'];
1039 msg->cm_fields['P'] = NULL;
1042 oldpath = strdoop("unknown_user");
1044 size = strlen(oldpath) + SIZ;
1045 msg->cm_fields['P'] = mallok(size);
1046 snprintf(msg->cm_fields['P'], size, "%s!%s",
1047 config.c_nodename, oldpath);
1050 /* serialize the message */
1051 serialize_message(&sermsg, msg);
1054 snprintf(filename, sizeof filename,
1055 "./network/spoolout/%s",
1056 msg->cm_fields['D']);
1057 fp = fopen(filename, "ab");
1064 CtdlFreeMessage(msg);
1068 else { /* invalid destination node name */
1071 "A message you sent could not be delivered due to an invalid destination node"
1072 " name. Please check the address and try sending the message again.\n");
1081 * Check to see if we already have a copy of this message
1083 if (network_usetable(msg) != 0) {
1084 snprintf(buf, sizeof buf,
1085 "Loopzapper rejected message <%s> "
1086 "from <%s> in <%s> @ <%s>\n",
1087 ((msg->cm_fields['I']!=NULL)?(msg->cm_fields['I']):""),
1088 ((msg->cm_fields['A']!=NULL)?(msg->cm_fields['A']):""),
1089 ((msg->cm_fields['O']!=NULL)?(msg->cm_fields['O']):""),
1090 ((msg->cm_fields['N']!=NULL)?(msg->cm_fields['N']):"")
1093 CtdlFreeMessage(msg);
1098 /* Learn network topology from the path */
1099 if ((msg->cm_fields['N'] != NULL) && (msg->cm_fields['P'] != NULL)) {
1100 network_learn_topology(msg->cm_fields['N'],
1101 msg->cm_fields['P']);
1104 /* Does it have a recipient? If so, validate it... */
1105 if (msg->cm_fields['R'] != NULL) {
1106 recp = validate_recipients(msg->cm_fields['R']);
1107 if (recp != NULL) if (recp->num_error > 0) {
1109 "A message you sent could not be delivered due to an invalid address.\n"
1110 "Please check the address and try sending the message again.\n");
1115 strcpy(target_room, ""); /* no target room if mail */
1118 else if (msg->cm_fields['C'] != NULL) {
1119 safestrncpy(target_room,
1120 msg->cm_fields['C'],
1121 sizeof target_room);
1124 else if (msg->cm_fields['O'] != NULL) {
1125 safestrncpy(target_room,
1126 msg->cm_fields['O'],
1127 sizeof target_room);
1130 /* Strip out fields that are only relevant during transit */
1131 if (msg->cm_fields['D'] != NULL) {
1132 phree(msg->cm_fields['D']);
1133 msg->cm_fields['D'] = NULL;
1135 if (msg->cm_fields['C'] != NULL) {
1136 phree(msg->cm_fields['C']);
1137 msg->cm_fields['C'] = NULL;
1140 /* save the message into a room */
1141 if (PerformNetprocHooks(msg, target_room) == 0) {
1142 msg->cm_flags = CM_SKIP_HOOKS;
1143 CtdlSubmitMsg(msg, recp, target_room);
1145 CtdlFreeMessage(msg);
1151 * Process a single message from a single file from the inbound queue
1153 void network_process_message(FILE *fp, long msgstart, long msgend) {
1158 hold_pos = ftell(fp);
1159 size = msgend - msgstart + 1;
1160 buffer = mallok(size);
1161 if (buffer != NULL) {
1162 fseek(fp, msgstart, SEEK_SET);
1163 fread(buffer, size, 1, fp);
1164 network_process_buffer(buffer, size);
1168 fseek(fp, hold_pos, SEEK_SET);
1173 * Process a single file from the inbound queue
1175 void network_process_file(char *filename) {
1177 long msgstart = (-1L);
1178 long msgend = (-1L);
1182 lprintf(7, "network: processing <%s>\n", filename);
1184 fp = fopen(filename, "rb");
1186 lprintf(5, "Error opening %s: %s\n",
1187 filename, strerror(errno));
1191 /* Look for messages in the data stream and break them out */
1192 while (ch = getc(fp), ch >= 0) {
1195 if (msgstart >= 0L) {
1196 msgend = msgcur - 1;
1197 network_process_message(fp, msgstart, msgend);
1205 msgend = msgcur - 1;
1206 if (msgstart >= 0L) {
1207 network_process_message(fp, msgstart, msgend);
1216 * Process anything in the inbound queue
1218 void network_do_spoolin(void) {
1223 dp = opendir("./network/spoolin");
1224 if (dp == NULL) return;
1226 while (d = readdir(dp), d != NULL) {
1227 snprintf(filename, sizeof filename, "./network/spoolin/%s", d->d_name);
1228 network_process_file(filename);
1240 * receive network spool from the remote system
1242 void receive_spool(int sock, char *remote_nodename) {
1244 long bytes_received;
1246 static char pbuf[IGNET_PACKET_SIZE];
1247 char tempfilename[PATH_MAX];
1251 strcpy(tempfilename, tmpnam(NULL));
1252 if (sock_puts(sock, "NDOP") < 0) return;
1253 if (sock_gets(sock, buf) < 0) return;
1254 lprintf(9, "<%s\n", buf);
1255 if (buf[0] != '2') {
1258 download_len = extract_long(&buf[4], 0);
1260 bytes_received = 0L;
1261 fp = fopen(tempfilename, "w");
1263 lprintf(9, "cannot open download file locally: %s\n",
1268 while (bytes_received < download_len) {
1269 snprintf(buf, sizeof buf, "READ %ld|%ld",
1271 ((download_len - bytes_received > IGNET_PACKET_SIZE)
1272 ? IGNET_PACKET_SIZE : (download_len - bytes_received)));
1273 if (sock_puts(sock, buf) < 0) {
1275 unlink(tempfilename);
1278 if (sock_gets(sock, buf) < 0) {
1280 unlink(tempfilename);
1283 if (buf[0] == '6') {
1284 plen = extract_long(&buf[4], 0);
1285 if (sock_read(sock, pbuf, plen) < 0) {
1287 unlink(tempfilename);
1290 fwrite((char *) pbuf, plen, 1, fp);
1291 bytes_received = bytes_received + plen;
1296 if (sock_puts(sock, "CLOS") < 0) {
1297 unlink(tempfilename);
1300 if (sock_gets(sock, buf) < 0) {
1301 unlink(tempfilename);
1304 lprintf(9, "%s\n", buf);
1305 snprintf(buf, sizeof buf, "mv %s ./network/spoolin/%s.%ld",
1306 tempfilename, remote_nodename, (long) getpid());
1313 * transmit network spool to the remote system
1315 void transmit_spool(int sock, char *remote_nodename)
1320 long bytes_to_write, thisblock;
1324 if (sock_puts(sock, "NUOP") < 0) return;
1325 if (sock_gets(sock, buf) < 0) return;
1326 lprintf(9, "<%s\n", buf);
1327 if (buf[0] != '2') {
1331 snprintf(sfname, sizeof sfname, "./network/spoolout/%s", remote_nodename);
1332 fd = open(sfname, O_RDONLY);
1334 if (errno == ENOENT) {
1335 lprintf(9, "Nothing to send.\n");
1337 lprintf(5, "cannot open upload file locally: %s\n",
1342 while (plen = (long) read(fd, pbuf, IGNET_PACKET_SIZE), plen > 0L) {
1343 bytes_to_write = plen;
1344 while (bytes_to_write > 0L) {
1345 snprintf(buf, sizeof buf, "WRIT %ld", bytes_to_write);
1346 if (sock_puts(sock, buf) < 0) {
1350 if (sock_gets(sock, buf) < 0) {
1354 thisblock = atol(&buf[4]);
1355 if (buf[0] == '7') {
1356 if (sock_write(sock, pbuf,
1357 (int) thisblock) < 0) {
1361 bytes_to_write = bytes_to_write - thisblock;
1370 if (sock_puts(sock, "UCLS 1") < 0) return;
1371 if (sock_gets(sock, buf) < 0) return;
1372 lprintf(9, "<%s\n", buf);
1373 if (buf[0] == '2') {
1381 * Poll one Citadel node (called by network_poll_other_citadel_nodes() below)
1383 void network_poll_node(char *node, char *secret, char *host, char *port) {
1387 if (network_talking_to(node, NTT_CHECK)) return;
1388 network_talking_to(node, NTT_ADD);
1389 lprintf(5, "Polling node <%s> at %s:%s\n", node, host, port);
1391 sock = sock_connect(host, port, "tcp");
1393 lprintf(7, "Could not connect: %s\n", strerror(errno));
1394 network_talking_to(node, NTT_REMOVE);
1398 lprintf(9, "Connected!\n");
1400 /* Read the server greeting */
1401 if (sock_gets(sock, buf) < 0) goto bail;
1402 lprintf(9, ">%s\n", buf);
1404 /* Identify ourselves */
1405 snprintf(buf, sizeof buf, "NETP %s|%s", config.c_nodename, secret);
1406 lprintf(9, "<%s\n", buf);
1407 if (sock_puts(sock, buf) <0) goto bail;
1408 if (sock_gets(sock, buf) < 0) goto bail;
1409 lprintf(9, ">%s\n", buf);
1410 if (buf[0] != '2') goto bail;
1412 /* At this point we are authenticated. */
1413 receive_spool(sock, node);
1414 transmit_spool(sock, node);
1416 sock_puts(sock, "QUIT");
1417 bail: sock_close(sock);
1418 network_talking_to(node, NTT_REMOVE);
1424 * Poll other Citadel nodes and transfer inbound/outbound network data.
1426 void network_poll_other_citadel_nodes(void) {
1427 char *ignetcfg = NULL;
1435 ignetcfg = CtdlGetSysConfig(IGNETCFG);
1436 if (ignetcfg == NULL) return; /* no nodes defined */
1438 /* Use the string tokenizer to grab one line at a time */
1439 for (i=0; i<num_tokens(ignetcfg, '\n'); ++i) {
1440 extract_token(linebuf, ignetcfg, i, '\n');
1441 extract(node, linebuf, 0);
1442 extract(secret, linebuf, 1);
1443 extract(host, linebuf, 2);
1444 extract(port, linebuf, 3);
1445 if ( (strlen(node) > 0) && (strlen(secret) > 0)
1446 && (strlen(host) > 0) && strlen(port) > 0) {
1447 network_poll_node(node, secret, host, port);
1461 * network_do_queue()
1463 * Run through the rooms doing various types of network stuff.
1465 void network_do_queue(void) {
1466 static int doing_queue = 0;
1467 static time_t last_run = 0L;
1468 struct RoomProcList *ptr;
1471 * Run no more frequently than once every n seconds
1473 if ( (time(NULL) - last_run) < config.c_net_freq ) return;
1476 * This is a simple concurrency check to make sure only one queue run
1477 * is done at a time. We could do this with a mutex, but since we
1478 * don't really require extremely fine granularity here, we'll do it
1479 * with a static variable instead.
1481 if (doing_queue) return;
1483 last_run = time(NULL);
1486 * Poll other Citadel nodes.
1488 network_poll_other_citadel_nodes();
1491 * Load the network map and filter list into memory.
1494 filterlist = load_filter_list();
1497 * Go ahead and run the queue
1499 lprintf(7, "network: loading outbound queue\n");
1500 ForEachRoom(network_queue_room, NULL);
1502 lprintf(7, "network: running outbound queue\n");
1503 while (rplist != NULL) {
1504 network_spoolout_room(rplist->name);
1506 rplist = rplist->next;
1510 lprintf(7, "network: processing inbound queue\n");
1511 network_do_spoolin();
1513 /* Save the network map back to disk */
1514 write_network_map();
1516 /* Free the filter list in memory */
1517 free_filter_list(filterlist);
1520 lprintf(7, "network: queue run completed\n");
1526 * cmd_netp() - authenticate to the server as another Citadel node polling
1527 * for network traffic
1529 void cmd_netp(char *cmdbuf)
1537 extract(node, cmdbuf, 0);
1538 extract(pass, cmdbuf, 1);
1540 if (is_valid_node(nexthop, secret, node) != 0) {
1541 cprintf("%d authentication failed\n", ERROR);
1545 if (strcasecmp(pass, secret)) {
1546 cprintf("%d authentication failed\n", ERROR);
1550 if (network_talking_to(node, NTT_CHECK)) {
1551 cprintf("%d Already talking to %s right now\n", ERROR, node);
1555 safestrncpy(CC->net_node, node, sizeof CC->net_node);
1556 network_talking_to(node, NTT_ADD);
1557 cprintf("%d authenticated as network node '%s'\n", CIT_OK,
1566 * Module entry point
1568 char *Dynamic_Module_Init(void)
1570 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
1571 CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");
1572 CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");
1573 CtdlRegisterSessionHook(network_do_queue, EVT_TIMER);