4 * This module handles shared rooms, inter-Citadel mail, and outbound
5 * mailing list processing.
7 * Copyright (C) 2000-2005 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.
20 * Duration of time (in seconds) after which pending list subscribe/unsubscribe
21 * requests that have not been confirmed will be deleted.
23 #define EXP 259200 /* three days */
35 #include <sys/types.h>
37 #if TIME_WITH_SYS_TIME
38 # include <sys/time.h>
42 # include <sys/time.h>
51 #include <libcitadel.h>
54 #include "citserver.h"
62 #include "internet_addressing.h"
63 #include "serv_network.h"
64 #include "clientsocket.h"
66 #include "citadel_dirs.h"
73 #include "ctdl_module.h"
77 /* Nonzero while we are doing network processing */
78 static int doing_queue = 0;
81 * When we do network processing, it's accomplished in two passes; one to
82 * gather a list of rooms and one to actually do them. It's ok that rplist
83 * is global; we have a mutex that keeps it safe.
85 struct RoomProcList *rplist = NULL;
88 * We build a map of network nodes during processing.
90 NetMap *the_netmap = NULL;
91 int netmap_changed = 0;
92 char *working_ignetcfg = NULL;
95 * Load or refresh the Citadel network (IGnet) configuration for this node.
97 void load_working_ignetcfg(void) {
101 cfg = CtdlGetSysConfig(IGNETCFG);
106 oldcfg = working_ignetcfg;
107 working_ignetcfg = cfg;
108 if (oldcfg != NULL) {
118 * Keep track of what messages to reject
120 FilterList *load_filter_list(void) {
121 char *serialized_list = NULL;
124 FilterList *newlist = NULL;
127 serialized_list = CtdlGetSysConfig(FILTERLIST);
128 if (serialized_list == NULL) return(NULL); /* if null, no entries */
130 /* Use the string tokenizer to grab one line at a time */
131 for (i=0; i<num_tokens(serialized_list, '\n'); ++i) {
132 extract_token(buf, serialized_list, i, '\n', sizeof buf);
133 nptr = (FilterList *) malloc(sizeof(FilterList));
134 extract_token(nptr->fl_user, buf, 0, '|', sizeof nptr->fl_user);
135 striplt(nptr->fl_user);
136 extract_token(nptr->fl_room, buf, 1, '|', sizeof nptr->fl_room);
137 striplt(nptr->fl_room);
138 extract_token(nptr->fl_node, buf, 2, '|', sizeof nptr->fl_node);
139 striplt(nptr->fl_node);
141 /* Cowardly refuse to add an any/any/any entry that would
142 * end up filtering every single message.
144 if (IsEmptyStr(nptr->fl_user) &&
145 IsEmptyStr(nptr->fl_room) &&
146 IsEmptyStr(nptr->fl_node)) {
150 nptr->next = newlist;
155 free(serialized_list);
160 void free_filter_list(FilterList *fl) {
161 if (fl == NULL) return;
162 free_filter_list(fl->next);
169 * Check the use table. This is a list of messages which have recently
170 * arrived on the system. It is maintained and queried to prevent the same
171 * message from being entered into the database multiple times if it happens
172 * to arrive multiple times by accident.
174 int network_usetable(struct CtdlMessage *msg) {
177 struct cdbdata *cdbut;
180 /* Bail out if we can't generate a message ID */
184 if (msg->cm_fields['I'] == NULL) {
187 if (IsEmptyStr(msg->cm_fields['I'])) {
191 /* Generate the message ID */
192 strcpy(msgid, msg->cm_fields['I']);
193 if (haschar(msgid, '@') == 0) {
195 if (msg->cm_fields['N'] != NULL) {
196 strcat(msgid, msg->cm_fields['N']);
203 cdbut = cdb_fetch(CDB_USETABLE, msgid, strlen(msgid));
209 /* If we got to this point, it's unique: add it. */
210 strcpy(ut.ut_msgid, msgid);
211 ut.ut_timestamp = time(NULL);
212 cdb_store(CDB_USETABLE, msgid, strlen(msgid),
213 &ut, sizeof(struct UseTable) );
219 * Read the network map from its configuration file into memory.
221 void read_network_map(void) {
222 char *serialized_map = NULL;
227 serialized_map = CtdlGetSysConfig(IGNETMAP);
228 if (serialized_map == NULL) return; /* if null, no entries */
230 /* Use the string tokenizer to grab one line at a time */
231 for (i=0; i<num_tokens(serialized_map, '\n'); ++i) {
232 extract_token(buf, serialized_map, i, '\n', sizeof buf);
233 nmptr = (NetMap *) malloc(sizeof(NetMap));
234 extract_token(nmptr->nodename, buf, 0, '|', sizeof nmptr->nodename);
235 nmptr->lastcontact = extract_long(buf, 1);
236 extract_token(nmptr->nexthop, buf, 2, '|', sizeof nmptr->nexthop);
237 nmptr->next = the_netmap;
241 free(serialized_map);
247 * Write the network map from memory back to the configuration file.
249 void write_network_map(void) {
250 char *serialized_map = NULL;
254 if (netmap_changed) {
255 serialized_map = strdup("");
257 if (the_netmap != NULL) {
258 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
259 serialized_map = realloc(serialized_map,
260 (strlen(serialized_map)+SIZ) );
261 if (!IsEmptyStr(nmptr->nodename)) {
262 snprintf(&serialized_map[strlen(serialized_map)],
266 (long)nmptr->lastcontact,
272 CtdlPutSysConfig(IGNETMAP, serialized_map);
273 free(serialized_map);
276 /* Now free the list */
277 while (the_netmap != NULL) {
278 nmptr = the_netmap->next;
288 * Check the network map and determine whether the supplied node name is
289 * valid. If it is not a neighbor node, supply the name of a neighbor node
290 * which is the next hop. If it *is* a neighbor node, we also fill in the
293 int is_valid_node(char *nexthop, char *secret, char *node) {
305 * First try the neighbor nodes
307 if (working_ignetcfg == NULL) {
308 lprintf(CTDL_ERR, "working_ignetcfg is NULL!\n");
309 if (nexthop != NULL) {
316 if (nexthop != NULL) {
320 /* Use the string tokenizer to grab one line at a time */
321 for (i=0; i<num_tokens(working_ignetcfg, '\n'); ++i) {
322 extract_token(linebuf, working_ignetcfg, i, '\n', sizeof linebuf);
323 extract_token(buf, linebuf, 0, '|', sizeof buf);
324 if (!strcasecmp(buf, node)) {
325 if (nexthop != NULL) {
328 if (secret != NULL) {
329 extract_token(secret, linebuf, 1, '|', 256);
336 return(retval); /* yup, it's a direct neighbor */
340 * If we get to this point we have to see if we know the next hop
342 if (the_netmap != NULL) {
343 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
344 if (!strcasecmp(nmptr->nodename, node)) {
345 if (nexthop != NULL) {
346 strcpy(nexthop, nmptr->nexthop);
354 * If we get to this point, the supplied node name is bogus.
356 lprintf(CTDL_ERR, "Invalid node name <%s>\n", node);
364 void cmd_gnet(char *argbuf) {
369 if ( (CC->room.QRflags & QR_MAILBOX) && (CC->user.usernum == atol(CC->room.QRname)) ) {
370 /* users can edit the netconfigs for their own mailbox rooms */
372 else if (CtdlAccessCheck(ac_room_aide)) return;
374 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
375 cprintf("%d Network settings for room #%ld <%s>\n",
377 CC->room.QRnumber, CC->room.QRname);
379 fp = fopen(filename, "r");
381 while (fgets(buf, sizeof buf, fp) != NULL) {
382 buf[strlen(buf)-1] = 0;
383 cprintf("%s\n", buf);
392 void cmd_snet(char *argbuf) {
393 char tempfilename[SIZ];
400 if ( (CC->room.QRflags & QR_MAILBOX) && (CC->user.usernum == atol(CC->room.QRname)) ) {
401 /* users can edit the netconfigs for their own mailbox rooms */
403 else if (CtdlAccessCheck(ac_room_aide)) return;
405 CtdlMakeTempFileName(tempfilename, sizeof tempfilename);
406 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
408 fp = fopen(tempfilename, "w");
410 cprintf("%d Cannot open %s: %s\n",
411 ERROR + INTERNAL_ERROR,
416 cprintf("%d %s\n", SEND_LISTING, tempfilename);
417 while (client_getln(buf, sizeof buf), strcmp(buf, "000")) {
418 fprintf(fp, "%s\n", buf);
422 /* Now copy the temp file to its permanent location.
423 * (We copy instead of link because they may be on different filesystems)
425 begin_critical_section(S_NETCONFIGS);
426 fp = fopen(tempfilename, "r");
428 newfp = fopen(filename, "w");
430 while (fgets(buf, sizeof buf, fp) != NULL) {
431 fprintf(newfp, "%s", buf);
437 end_critical_section(S_NETCONFIGS);
438 unlink(tempfilename);
443 * Deliver digest messages
445 void network_deliver_digest(SpoolControl *sc) {
448 struct CtdlMessage *msg = NULL;
451 size_t recps_len = SIZ;
452 struct recptypes *valid;
455 if (sc->num_msgs_spooled < 1) {
456 fclose(sc->digestfp);
461 msg = malloc(sizeof(struct CtdlMessage));
462 memset(msg, 0, sizeof(struct CtdlMessage));
463 msg->cm_magic = CTDLMESSAGE_MAGIC;
464 msg->cm_format_type = FMT_RFC822;
465 msg->cm_anon_type = MES_NORMAL;
467 sprintf(buf, "%ld", time(NULL));
468 msg->cm_fields['T'] = strdup(buf);
469 msg->cm_fields['A'] = strdup(CC->room.QRname);
470 snprintf(buf, sizeof buf, "[%s]", CC->room.QRname);
471 msg->cm_fields['U'] = strdup(buf);
472 sprintf(buf, "room_%s@%s", CC->room.QRname, config.c_fqdn);
473 for (i=0; buf[i]; ++i) {
474 if (isspace(buf[i])) buf[i]='_';
475 buf[i] = tolower(buf[i]);
477 msg->cm_fields['F'] = strdup(buf);
478 msg->cm_fields['R'] = strdup(buf);
481 * Go fetch the contents of the digest
483 fseek(sc->digestfp, 0L, SEEK_END);
484 msglen = ftell(sc->digestfp);
486 msg->cm_fields['M'] = malloc(msglen + 1);
487 fseek(sc->digestfp, 0L, SEEK_SET);
488 fread(msg->cm_fields['M'], (size_t)msglen, 1, sc->digestfp);
489 msg->cm_fields['M'][msglen] = 0;
491 fclose(sc->digestfp);
494 /* Now generate the delivery instructions */
497 * Figure out how big a buffer we need to allocate
499 for (nptr = sc->digestrecps; nptr != NULL; nptr = nptr->next) {
500 recps_len = recps_len + strlen(nptr->name) + 2;
503 recps = malloc(recps_len);
506 lprintf(CTDL_EMERG, "Cannot allocate %ld bytes for recps...\n", (long)recps_len);
513 for (nptr = sc->digestrecps; nptr != NULL; nptr = nptr->next) {
514 if (nptr != sc->digestrecps) {
517 strcat(recps, nptr->name);
520 /* Now submit the message */
521 valid = validate_recipients(recps, NULL, 0);
523 CtdlSubmitMsg(msg, valid, NULL);
524 CtdlFreeMessage(msg);
525 free_recipients(valid);
530 * Deliver list messages to everyone on the list ... efficiently
532 void network_deliver_list(struct CtdlMessage *msg, SpoolControl *sc) {
534 size_t recps_len = SIZ;
535 struct recptypes *valid;
538 /* Don't do this if there were no recipients! */
539 if (sc->listrecps == NULL) return;
541 /* Now generate the delivery instructions */
544 * Figure out how big a buffer we need to allocate
546 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
547 recps_len = recps_len + strlen(nptr->name) + 2;
550 recps = malloc(recps_len);
553 lprintf(CTDL_EMERG, "Cannot allocate %ld bytes for recps...\n", (long)recps_len);
560 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
561 if (nptr != sc->listrecps) {
564 strcat(recps, nptr->name);
567 /* Now submit the message */
568 valid = validate_recipients(recps, NULL, 0);
570 CtdlSubmitMsg(msg, valid, NULL);
571 free_recipients(valid);
572 /* Do not call CtdlFreeMessage(msg) here; the caller will free it. */
579 * Spools out one message from the list.
581 void network_spool_msg(long msgnum, void *userdata) {
584 char *newpath = NULL;
585 size_t instr_len = SIZ;
586 struct CtdlMessage *msg = NULL;
589 struct ser_ret sermsg;
595 int delete_after_send = 0; /* Set to 1 to delete after spooling */
596 int ok_to_participate = 0;
597 struct recptypes *valid;
599 sc = (SpoolControl *)userdata;
602 * Process mailing list recipients
605 if (sc->listrecps != NULL) {
606 /* Fetch the message. We're going to need to modify it
607 * in order to insert the [list name] in it, etc.
609 msg = CtdlFetchMessage(msgnum, 1);
612 /* Prepend "[List name]" to the subject */
613 if (msg->cm_fields['U'] == NULL) {
614 msg->cm_fields['U'] = strdup("(no subject)");
616 snprintf(buf, sizeof buf, "[%s] %s", CC->room.QRname, msg->cm_fields['U']);
617 free(msg->cm_fields['U']);
618 msg->cm_fields['U'] = strdup(buf);
620 /* Set the recipient of the list message to the
621 * email address of the room itself.
622 * FIXME ... I want to be able to pick any address
624 if (msg->cm_fields['R'] != NULL) {
625 free(msg->cm_fields['R']);
627 msg->cm_fields['R'] = malloc(256);
628 snprintf(msg->cm_fields['R'], 256,
629 "room_%s@%s", CC->room.QRname,
631 for (i=0; msg->cm_fields['R'][i]; ++i) {
632 if (isspace(msg->cm_fields['R'][i])) {
633 msg->cm_fields['R'][i] = '_';
637 /* Handle delivery */
638 network_deliver_list(msg, sc);
639 CtdlFreeMessage(msg);
644 * Process digest recipients
646 if ((sc->digestrecps != NULL) && (sc->digestfp != NULL)) {
647 msg = CtdlFetchMessage(msgnum, 1);
649 fprintf(sc->digestfp, " -----------------------------------"
650 "------------------------------------"
652 fprintf(sc->digestfp, "From: ");
653 if (msg->cm_fields['A'] != NULL) {
654 fprintf(sc->digestfp, "%s ", msg->cm_fields['A']);
656 if (msg->cm_fields['F'] != NULL) {
657 fprintf(sc->digestfp, "<%s> ", msg->cm_fields['F']);
659 else if (msg->cm_fields['N'] != NULL) {
660 fprintf(sc->digestfp, "@%s ", msg->cm_fields['N']);
662 fprintf(sc->digestfp, "\n");
663 if (msg->cm_fields['U'] != NULL) {
664 fprintf(sc->digestfp, "Subject: %s\n", msg->cm_fields['U']);
667 CC->redirect_buffer = malloc(SIZ);
668 CC->redirect_len = 0;
669 CC->redirect_alloc = SIZ;
671 safestrncpy(CC->preferred_formats, "text/plain", sizeof CC->preferred_formats);
672 CtdlOutputPreLoadedMsg(msg, MT_CITADEL, HEADERS_NONE, 0, 0);
674 striplt(CC->redirect_buffer);
675 fprintf(sc->digestfp, "\n%s\n", CC->redirect_buffer);
677 free(CC->redirect_buffer);
678 CC->redirect_buffer = NULL;
679 CC->redirect_len = 0;
680 CC->redirect_alloc = 0;
682 sc->num_msgs_spooled += 1;
688 * Process client-side list participations for this room
691 if (sc->participates != NULL) {
692 msg = CtdlFetchMessage(msgnum, 1);
695 /* Only send messages which originated on our own Citadel
696 * network, otherwise we'll end up sending the remote
697 * mailing list's messages back to it, which is rude...
699 ok_to_participate = 0;
700 if (msg->cm_fields['N'] != NULL) {
701 if (!strcasecmp(msg->cm_fields['N'], config.c_nodename)) {
702 ok_to_participate = 1;
704 if (is_valid_node(NULL, NULL, msg->cm_fields['N']) == 0) {
705 ok_to_participate = 1;
708 if (ok_to_participate) {
709 if (msg->cm_fields['F'] != NULL) {
710 free(msg->cm_fields['F']);
712 msg->cm_fields['F'] = malloc(SIZ);
713 /* Replace the Internet email address of the actual
714 * author with the email address of the room itself,
715 * so the remote listserv doesn't reject us.
716 * FIXME ... I want to be able to pick any address
718 snprintf(msg->cm_fields['F'], SIZ,
719 "room_%s@%s", CC->room.QRname,
721 for (i=0; msg->cm_fields['F'][i]; ++i) {
722 if (isspace(msg->cm_fields['F'][i])) {
723 msg->cm_fields['F'][i] = '_';
728 * Figure out how big a buffer we need to allocate
730 for (nptr = sc->participates; nptr != NULL; nptr = nptr->next) {
732 if (msg->cm_fields['R'] == NULL) {
733 free(msg->cm_fields['R']);
735 msg->cm_fields['R'] = strdup(nptr->name);
737 valid = validate_recipients(nptr->name, NULL, 0);
738 CtdlSubmitMsg(msg, valid, "");
739 free_recipients(valid);
743 CtdlFreeMessage(msg);
748 * Process IGnet push shares
750 msg = CtdlFetchMessage(msgnum, 1);
754 /* Prepend our node name to the Path field whenever
755 * sending a message to another IGnet node
757 if (msg->cm_fields['P'] == NULL) {
758 msg->cm_fields['P'] = strdup("username");
760 newpath_len = strlen(msg->cm_fields['P']) +
761 strlen(config.c_nodename) + 2;
762 newpath = malloc(newpath_len);
763 snprintf(newpath, newpath_len, "%s!%s",
764 config.c_nodename, msg->cm_fields['P']);
765 free(msg->cm_fields['P']);
766 msg->cm_fields['P'] = newpath;
769 * Determine if this message is set to be deleted
770 * after sending out on the network
772 if (msg->cm_fields['S'] != NULL) {
773 if (!strcasecmp(msg->cm_fields['S'], "CANCEL")) {
774 delete_after_send = 1;
778 /* Now send it to every node */
779 if (sc->ignet_push_shares != NULL)
780 for (mptr = sc->ignet_push_shares; mptr != NULL;
785 /* Check for valid node name */
786 if (is_valid_node(NULL, NULL, mptr->remote_nodename) != 0) {
787 lprintf(CTDL_ERR, "Invalid node <%s>\n",
788 mptr->remote_nodename);
792 /* Check for split horizon */
793 lprintf(CTDL_DEBUG, "Path is %s\n", msg->cm_fields['P']);
794 bang = num_tokens(msg->cm_fields['P'], '!');
795 if (bang > 1) for (i=0; i<(bang-1); ++i) {
796 extract_token(buf, msg->cm_fields['P'],
798 if (!strcasecmp(buf, mptr->remote_nodename)) {
803 /* Send the message */
807 * Force the message to appear in the correct room
808 * on the far end by setting the C field correctly
810 if (msg->cm_fields['C'] != NULL) {
811 free(msg->cm_fields['C']);
813 if (!IsEmptyStr(mptr->remote_roomname)) {
814 msg->cm_fields['C'] = strdup(mptr->remote_roomname);
817 msg->cm_fields['C'] = strdup(CC->room.QRname);
820 /* serialize it for transmission */
821 serialize_message(&sermsg, msg);
822 if (sermsg.len > 0) {
824 /* write it to the spool file */
825 snprintf(filename, sizeof filename,"%s/%s",
827 mptr->remote_nodename);
828 lprintf(CTDL_DEBUG, "Appending to %s\n", filename);
829 fp = fopen(filename, "ab");
836 lprintf(CTDL_ERR, "%s: %s\n", filename, strerror(errno));
839 /* free the serialized version */
845 CtdlFreeMessage(msg);
848 /* update lastsent */
849 sc->lastsent = msgnum;
851 /* Delete this message if delete-after-send is set */
852 if (delete_after_send) {
853 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");
859 int read_spoolcontrol_file(SpoolControl **scc, char *filename)
865 char roomname[ROOMNAMELEN];
869 int skipthisline = 0;
870 namelist *nptr = NULL;
871 maplist *mptr = NULL;
874 fp = fopen(filename, "r");
878 sc = malloc(sizeof(SpoolControl));
879 memset(sc, 0, sizeof(SpoolControl));
882 while (fgets(buf, sizeof buf, fp) != NULL) {
883 buf[strlen(buf)-1] = 0;
885 extract_token(instr, buf, 0, '|', sizeof instr);
886 if (!strcasecmp(instr, "lastsent")) {
887 sc->lastsent = extract_long(buf, 1);
889 else if (!strcasecmp(instr, "listrecp")) {
891 malloc(sizeof(namelist));
892 nptr->next = sc->listrecps;
893 extract_token(nptr->name, buf, 1, '|', sizeof nptr->name);
894 sc->listrecps = nptr;
896 else if (!strcasecmp(instr, "participate")) {
898 malloc(sizeof(namelist));
899 nptr->next = sc->participates;
900 extract_token(nptr->name, buf, 1, '|', sizeof nptr->name);
901 sc->participates = nptr;
903 else if (!strcasecmp(instr, "digestrecp")) {
905 malloc(sizeof(namelist));
906 nptr->next = sc->digestrecps;
907 extract_token(nptr->name, buf, 1, '|', sizeof nptr->name);
908 sc->digestrecps = nptr;
910 else if (!strcasecmp(instr, "ignet_push_share")) {
911 /* by checking each node's validity, we automatically
912 * purge nodes which do not exist from room network
913 * configurations at this time.
915 extract_token(nodename, buf, 1, '|', sizeof nodename);
916 extract_token(roomname, buf, 2, '|', sizeof roomname);
917 strcpy(nexthop, "xxx");
918 if (is_valid_node(nexthop, NULL, nodename) == 0) {
919 if (IsEmptyStr(nexthop)) {
921 malloc(sizeof(maplist));
922 mptr->next = sc->ignet_push_shares;
923 strcpy(mptr->remote_nodename, nodename);
924 strcpy(mptr->remote_roomname, roomname);
925 sc->ignet_push_shares = mptr;
930 /* Preserve 'other' lines ... *unless* they happen to
931 * be subscribe/unsubscribe pendings with expired
935 if (!strncasecmp(buf, "subpending|", 11)) {
936 if (time(NULL) - extract_long(buf, 4) > EXP) {
940 if (!strncasecmp(buf, "unsubpending|", 13)) {
941 if (time(NULL) - extract_long(buf, 3) > EXP) {
946 if (skipthisline == 0) {
947 linesize = strlen(buf);
948 sc->misc = realloc(sc->misc,
949 (miscsize + linesize + 2) );
950 sprintf(&sc->misc[miscsize], "%s\n", buf);
951 miscsize = miscsize + linesize + 1;
961 void free_spoolcontrol_struct(SpoolControl **scc)
964 namelist *nptr = NULL;
965 maplist *mptr = NULL;
968 while (sc->listrecps != NULL) {
969 nptr = sc->listrecps->next;
971 sc->listrecps = nptr;
973 /* Do the same for digestrecps */
974 while (sc->digestrecps != NULL) {
975 nptr = sc->digestrecps->next;
976 free(sc->digestrecps);
977 sc->digestrecps = nptr;
979 /* Do the same for participates */
980 while (sc->participates != NULL) {
981 nptr = sc->participates->next;
982 free(sc->participates);
983 sc->participates = nptr;
985 while (sc->ignet_push_shares != NULL) {
986 mptr = sc->ignet_push_shares->next;
987 free(sc->ignet_push_shares);
988 sc->ignet_push_shares = mptr;
995 int writenfree_spoolcontrol_file(SpoolControl **scc, char *filename)
999 namelist *nptr = NULL;
1000 maplist *mptr = NULL;
1003 fp = fopen(filename, "w");
1005 lprintf(CTDL_CRIT, "ERROR: cannot open %s: %s\n",
1006 filename, strerror(errno));
1007 free_spoolcontrol_struct(scc);
1010 fprintf(fp, "lastsent|%ld\n", sc->lastsent);
1012 /* Write out the listrecps while freeing from memory at the
1013 * same time. Am I clever or what? :)
1015 while (sc->listrecps != NULL) {
1016 fprintf(fp, "listrecp|%s\n", sc->listrecps->name);
1017 nptr = sc->listrecps->next;
1018 free(sc->listrecps);
1019 sc->listrecps = nptr;
1021 /* Do the same for digestrecps */
1022 while (sc->digestrecps != NULL) {
1023 fprintf(fp, "digestrecp|%s\n", sc->digestrecps->name);
1024 nptr = sc->digestrecps->next;
1025 free(sc->digestrecps);
1026 sc->digestrecps = nptr;
1028 /* Do the same for participates */
1029 while (sc->participates != NULL) {
1030 fprintf(fp, "participate|%s\n", sc->participates->name);
1031 nptr = sc->participates->next;
1032 free(sc->participates);
1033 sc->participates = nptr;
1035 while (sc->ignet_push_shares != NULL) {
1036 /* by checking each node's validity, we automatically
1037 * purge nodes which do not exist from room network
1038 * configurations at this time.
1040 if (is_valid_node(NULL, NULL, sc->ignet_push_shares->remote_nodename) == 0) {
1042 fprintf(fp, "ignet_push_share|%s",
1043 sc->ignet_push_shares->remote_nodename);
1044 if (!IsEmptyStr(sc->ignet_push_shares->remote_roomname)) {
1045 fprintf(fp, "|%s", sc->ignet_push_shares->remote_roomname);
1048 mptr = sc->ignet_push_shares->next;
1049 free(sc->ignet_push_shares);
1050 sc->ignet_push_shares = mptr;
1052 if (sc->misc != NULL) {
1053 fwrite(sc->misc, strlen(sc->misc), 1, fp);
1063 int is_recipient(SpoolControl *sc, const char *Name)
1067 nptr = sc->listrecps;
1068 while (nptr != NULL) {
1069 if (strcmp(Name, nptr->name)==0)
1073 /* Do the same for digestrecps */
1074 nptr = sc->digestrecps;
1075 while (nptr != NULL) {
1076 if (strcmp(Name, nptr->name)==0)
1080 /* Do the same for participates */
1081 nptr = sc->participates;
1082 while (nptr != NULL) {
1083 if (strcmp(Name, nptr->name)==0)
1092 * Batch up and send all outbound traffic from the current room
1094 void network_spoolout_room(char *room_to_spool) {
1101 * If the room doesn't exist, don't try to perform its networking tasks.
1102 * Normally this should never happen, but once in a while maybe a room gets
1103 * queued for networking and then deleted before it can happen.
1105 if (getroom(&CC->room, room_to_spool) != 0) {
1106 lprintf(CTDL_CRIT, "ERROR: cannot load <%s>\n", room_to_spool);
1110 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
1112 lprintf(CTDL_INFO, "Networking started for <%s>\n", CC->room.QRname);
1113 begin_critical_section(S_NETCONFIGS);
1115 /* Only do net processing for rooms that have netconfigs */
1117 if (!read_spoolcontrol_file(&sc, filename))
1119 end_critical_section(S_NETCONFIGS);
1123 /* If there are digest recipients, we have to build a digest */
1124 if (sc->digestrecps != NULL) {
1125 sc->digestfp = tmpfile();
1126 fprintf(sc->digestfp, "Content-type: text/plain\n\n");
1129 /* Do something useful */
1130 CtdlForEachMessage(MSGS_GT, sc->lastsent, NULL, NULL, NULL,
1131 network_spool_msg, sc);
1133 /* If we wrote a digest, deliver it and then close it */
1134 snprintf(buf, sizeof buf, "room_%s@%s",
1135 CC->room.QRname, config.c_fqdn);
1136 for (i=0; buf[i]; ++i) {
1137 buf[i] = tolower(buf[i]);
1138 if (isspace(buf[i])) buf[i] = '_';
1140 if (sc->digestfp != NULL) {
1141 fprintf(sc->digestfp, " -----------------------------------"
1142 "------------------------------------"
1144 "You are subscribed to the '%s' "
1146 "To post to the list: %s\n",
1147 CC->room.QRname, buf
1149 network_deliver_digest(sc); /* deliver and close */
1152 /* Now rewrite the config file */
1153 writenfree_spoolcontrol_file (&sc, filename);
1154 end_critical_section(S_NETCONFIGS);
1160 * Send the *entire* contents of the current room to one specific network node,
1161 * ignoring anything we know about which messages have already undergone
1162 * network processing. This can be used to bring a new node into sync.
1164 int network_sync_to(char *target_node) {
1166 int num_spooled = 0;
1175 /* Grab the configuration line we're looking for */
1176 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
1177 begin_critical_section(S_NETCONFIGS);
1178 fp = fopen(filename, "r");
1180 end_critical_section(S_NETCONFIGS);
1183 while (fgets(buf, sizeof buf, fp) != NULL) {
1184 buf[strlen(buf)-1] = 0;
1185 extract_token(sc_type, buf, 0, '|', sizeof sc_type);
1186 extract_token(sc_node, buf, 1, '|', sizeof sc_node);
1187 extract_token(sc_room, buf, 2, '|', sizeof sc_room);
1188 if ( (!strcasecmp(sc_type, "ignet_push_share"))
1189 && (!strcasecmp(sc_node, target_node)) ) {
1192 /* Concise syntax because we don't need a full linked-list */
1193 memset(&sc, 0, sizeof(SpoolControl));
1194 sc.ignet_push_shares = (maplist *)
1195 malloc(sizeof(maplist));
1196 sc.ignet_push_shares->next = NULL;
1197 safestrncpy(sc.ignet_push_shares->remote_nodename,
1199 sizeof sc.ignet_push_shares->remote_nodename);
1200 safestrncpy(sc.ignet_push_shares->remote_roomname,
1202 sizeof sc.ignet_push_shares->remote_roomname);
1206 end_critical_section(S_NETCONFIGS);
1208 if (!found_node) return(-1);
1210 /* Send ALL messages */
1211 num_spooled = CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL,
1212 network_spool_msg, &sc);
1214 /* Concise cleanup because we know there's only one node in the sc */
1215 free(sc.ignet_push_shares);
1217 lprintf(CTDL_NOTICE, "Synchronized %d messages to <%s>\n",
1218 num_spooled, target_node);
1219 return(num_spooled);
1224 * Implements the NSYN command
1226 void cmd_nsyn(char *argbuf) {
1228 char target_node[256];
1230 if (CtdlAccessCheck(ac_aide)) return;
1232 extract_token(target_node, argbuf, 0, '|', sizeof target_node);
1233 num_spooled = network_sync_to(target_node);
1234 if (num_spooled >= 0) {
1235 cprintf("%d Spooled %d messages.\n", CIT_OK, num_spooled);
1238 cprintf("%d No such room/node share exists.\n",
1239 ERROR + ROOM_NOT_FOUND);
1246 * Batch up and send all outbound traffic from the current room
1248 void network_queue_room(struct ctdlroom *qrbuf, void *data) {
1249 struct RoomProcList *ptr;
1251 ptr = (struct RoomProcList *) malloc(sizeof (struct RoomProcList));
1252 if (ptr == NULL) return;
1254 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
1255 begin_critical_section(S_RPLIST);
1258 end_critical_section(S_RPLIST);
1261 void destroy_network_queue_room(void)
1263 struct RoomProcList *cur, *p;
1264 NetMap *nmcur, *nmp;
1267 begin_critical_section(S_RPLIST);
1275 end_critical_section(S_RPLIST);
1278 while (nmcur != NULL)
1285 if (working_ignetcfg != NULL)
1286 free (working_ignetcfg);
1287 working_ignetcfg = NULL;
1292 * Learn topology from path fields
1294 void network_learn_topology(char *node, char *path) {
1298 strcpy(nexthop, "");
1300 if (num_tokens(path, '!') < 3) return;
1301 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
1302 if (!strcasecmp(nmptr->nodename, node)) {
1303 extract_token(nmptr->nexthop, path, 0, '!', sizeof nmptr->nexthop);
1304 nmptr->lastcontact = time(NULL);
1310 /* If we got here then it's not in the map, so add it. */
1311 nmptr = (NetMap *) malloc(sizeof (NetMap));
1312 strcpy(nmptr->nodename, node);
1313 nmptr->lastcontact = time(NULL);
1314 extract_token(nmptr->nexthop, path, 0, '!', sizeof nmptr->nexthop);
1315 nmptr->next = the_netmap;
1324 * Bounce a message back to the sender
1326 void network_bounce(struct CtdlMessage *msg, char *reason) {
1327 char *oldpath = NULL;
1329 char bouncesource[SIZ];
1330 char recipient[SIZ];
1331 struct recptypes *valid = NULL;
1332 char force_room[ROOMNAMELEN];
1333 static int serialnum = 0;
1336 lprintf(CTDL_DEBUG, "entering network_bounce()\n");
1338 if (msg == NULL) return;
1340 snprintf(bouncesource, sizeof bouncesource, "%s@%s", BOUNCESOURCE, config.c_nodename);
1343 * Give it a fresh message ID
1345 if (msg->cm_fields['I'] != NULL) {
1346 free(msg->cm_fields['I']);
1348 snprintf(buf, sizeof buf, "%ld.%04lx.%04x@%s",
1349 (long)time(NULL), (long)getpid(), ++serialnum, config.c_fqdn);
1350 msg->cm_fields['I'] = strdup(buf);
1353 * FIXME ... right now we're just sending a bounce; we really want to
1354 * include the text of the bounced message.
1356 if (msg->cm_fields['M'] != NULL) {
1357 free(msg->cm_fields['M']);
1359 msg->cm_fields['M'] = strdup(reason);
1360 msg->cm_format_type = 0;
1363 * Turn the message around
1365 if (msg->cm_fields['R'] == NULL) {
1366 free(msg->cm_fields['R']);
1369 if (msg->cm_fields['D'] == NULL) {
1370 free(msg->cm_fields['D']);
1373 snprintf(recipient, sizeof recipient, "%s@%s",
1374 msg->cm_fields['A'], msg->cm_fields['N']);
1376 if (msg->cm_fields['A'] == NULL) {
1377 free(msg->cm_fields['A']);
1380 if (msg->cm_fields['N'] == NULL) {
1381 free(msg->cm_fields['N']);
1384 if (msg->cm_fields['U'] == NULL) {
1385 free(msg->cm_fields['U']);
1388 msg->cm_fields['A'] = strdup(BOUNCESOURCE);
1389 msg->cm_fields['N'] = strdup(config.c_nodename);
1390 msg->cm_fields['U'] = strdup("Delivery Status Notification (Failure)");
1392 /* prepend our node to the path */
1393 if (msg->cm_fields['P'] != NULL) {
1394 oldpath = msg->cm_fields['P'];
1395 msg->cm_fields['P'] = NULL;
1398 oldpath = strdup("unknown_user");
1400 size = strlen(oldpath) + SIZ;
1401 msg->cm_fields['P'] = malloc(size);
1402 snprintf(msg->cm_fields['P'], size, "%s!%s", config.c_nodename, oldpath);
1405 /* Now submit the message */
1406 valid = validate_recipients(recipient, NULL, 0);
1407 if (valid != NULL) if (valid->num_error != 0) {
1408 free_recipients(valid);
1411 if ( (valid == NULL) || (!strcasecmp(recipient, bouncesource)) ) {
1412 strcpy(force_room, config.c_aideroom);
1415 strcpy(force_room, "");
1417 if ( (valid == NULL) && IsEmptyStr(force_room) ) {
1418 strcpy(force_room, config.c_aideroom);
1420 CtdlSubmitMsg(msg, valid, force_room);
1423 if (valid != NULL) free_recipients(valid);
1424 CtdlFreeMessage(msg);
1425 lprintf(CTDL_DEBUG, "leaving network_bounce()\n");
1432 * Process a buffer containing a single message from a single file
1433 * from the inbound queue
1435 void network_process_buffer(char *buffer, long size) {
1436 struct CtdlMessage *msg = NULL;
1439 struct recptypes *recp = NULL;
1440 char target_room[ROOMNAMELEN];
1441 struct ser_ret sermsg;
1442 char *oldpath = NULL;
1446 unsigned char firstbyte;
1447 unsigned char lastbyte;
1449 /* Validate just a little bit. First byte should be FF and * last byte should be 00. */
1450 firstbyte = buffer[0];
1451 lastbyte = buffer[size-1];
1452 if ( (firstbyte != 255) || (lastbyte != 0) ) {
1453 lprintf(CTDL_ERR, "Corrupt message ignored. Length=%ld, firstbyte = %d, lastbyte = %d\n",
1454 size, firstbyte, lastbyte);
1458 /* Set default target room to trash */
1459 strcpy(target_room, TWITROOM);
1461 /* Load the message into memory */
1462 msg = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
1463 memset(msg, 0, sizeof(struct CtdlMessage));
1464 msg->cm_magic = CTDLMESSAGE_MAGIC;
1465 msg->cm_anon_type = buffer[1];
1466 msg->cm_format_type = buffer[2];
1468 for (pos = 3; pos < size; ++pos) {
1469 field = buffer[pos];
1470 msg->cm_fields[field] = strdup(&buffer[pos+1]);
1471 pos = pos + strlen(&buffer[(int)pos]);
1474 /* Check for message routing */
1475 if (msg->cm_fields['D'] != NULL) {
1476 if (strcasecmp(msg->cm_fields['D'], config.c_nodename)) {
1478 /* route the message */
1479 strcpy(nexthop, "");
1480 if (is_valid_node(nexthop, NULL,
1481 msg->cm_fields['D']) == 0) {
1483 /* prepend our node to the path */
1484 if (msg->cm_fields['P'] != NULL) {
1485 oldpath = msg->cm_fields['P'];
1486 msg->cm_fields['P'] = NULL;
1489 oldpath = strdup("unknown_user");
1491 size = strlen(oldpath) + SIZ;
1492 msg->cm_fields['P'] = malloc(size);
1493 snprintf(msg->cm_fields['P'], size, "%s!%s",
1494 config.c_nodename, oldpath);
1497 /* serialize the message */
1498 serialize_message(&sermsg, msg);
1501 if (IsEmptyStr(nexthop)) {
1502 strcpy(nexthop, msg->cm_fields['D']);
1509 lprintf(CTDL_DEBUG, "Appending to %s\n", filename);
1510 fp = fopen(filename, "ab");
1517 lprintf(CTDL_ERR, "%s: %s\n", filename, strerror(errno));
1520 CtdlFreeMessage(msg);
1524 else { /* invalid destination node name */
1527 "A message you sent could not be delivered due to an invalid destination node"
1528 " name. Please check the address and try sending the message again.\n");
1537 * Check to see if we already have a copy of this message, and
1538 * abort its processing if so. (We used to post a warning to Aide>
1539 * every time this happened, but the network is now so densely
1540 * connected that it's inevitable.)
1542 if (network_usetable(msg) != 0) {
1543 CtdlFreeMessage(msg);
1547 /* Learn network topology from the path */
1548 if ((msg->cm_fields['N'] != NULL) && (msg->cm_fields['P'] != NULL)) {
1549 network_learn_topology(msg->cm_fields['N'],
1550 msg->cm_fields['P']);
1553 /* Is the sending node giving us a very persuasive suggestion about
1554 * which room this message should be saved in? If so, go with that.
1556 if (msg->cm_fields['C'] != NULL) {
1557 safestrncpy(target_room,
1558 msg->cm_fields['C'],
1559 sizeof target_room);
1562 /* Otherwise, does it have a recipient? If so, validate it... */
1563 else if (msg->cm_fields['R'] != NULL) {
1564 recp = validate_recipients(msg->cm_fields['R'], NULL, 0);
1565 if (recp != NULL) if (recp->num_error != 0) {
1567 "A message you sent could not be delivered due to an invalid address.\n"
1568 "Please check the address and try sending the message again.\n");
1570 free_recipients(recp);
1573 strcpy(target_room, ""); /* no target room if mail */
1576 /* Our last shot at finding a home for this message is to see if
1577 * it has the O field (Originating room) set.
1579 else if (msg->cm_fields['O'] != NULL) {
1580 safestrncpy(target_room,
1581 msg->cm_fields['O'],
1582 sizeof target_room);
1585 /* Strip out fields that are only relevant during transit */
1586 if (msg->cm_fields['D'] != NULL) {
1587 free(msg->cm_fields['D']);
1588 msg->cm_fields['D'] = NULL;
1590 if (msg->cm_fields['C'] != NULL) {
1591 free(msg->cm_fields['C']);
1592 msg->cm_fields['C'] = NULL;
1595 /* save the message into a room */
1596 if (PerformNetprocHooks(msg, target_room) == 0) {
1597 msg->cm_flags = CM_SKIP_HOOKS;
1598 CtdlSubmitMsg(msg, recp, target_room);
1600 CtdlFreeMessage(msg);
1601 free_recipients(recp);
1606 * Process a single message from a single file from the inbound queue
1608 void network_process_message(FILE *fp, long msgstart, long msgend) {
1613 hold_pos = ftell(fp);
1614 size = msgend - msgstart + 1;
1615 buffer = malloc(size);
1616 if (buffer != NULL) {
1617 fseek(fp, msgstart, SEEK_SET);
1618 fread(buffer, size, 1, fp);
1619 network_process_buffer(buffer, size);
1623 fseek(fp, hold_pos, SEEK_SET);
1628 * Process a single file from the inbound queue
1630 void network_process_file(char *filename) {
1632 long msgstart = (-1L);
1633 long msgend = (-1L);
1638 fp = fopen(filename, "rb");
1640 lprintf(CTDL_CRIT, "Error opening %s: %s\n", filename, strerror(errno));
1644 fseek(fp, 0L, SEEK_END);
1645 lprintf(CTDL_INFO, "network: processing %ld bytes from %s\n", ftell(fp), filename);
1648 /* Look for messages in the data stream and break them out */
1649 while (ch = getc(fp), ch >= 0) {
1652 if (msgstart >= 0L) {
1653 msgend = msgcur - 1;
1654 network_process_message(fp, msgstart, msgend);
1662 msgend = msgcur - 1;
1663 if (msgstart >= 0L) {
1664 network_process_message(fp, msgstart, msgend);
1673 * Process anything in the inbound queue
1675 void network_do_spoolin(void) {
1678 struct stat statbuf;
1680 static time_t last_spoolin_mtime = 0L;
1683 * Check the spoolin directory's modification time. If it hasn't
1684 * been touched, we don't need to scan it.
1686 if (stat(ctdl_netin_dir, &statbuf)) return;
1687 if (statbuf.st_mtime == last_spoolin_mtime) {
1688 lprintf(CTDL_DEBUG, "network: nothing in inbound queue\n");
1691 last_spoolin_mtime = statbuf.st_mtime;
1692 lprintf(CTDL_DEBUG, "network: processing inbound queue\n");
1695 * Ok, there's something interesting in there, so scan it.
1697 dp = opendir(ctdl_netin_dir);
1698 if (dp == NULL) return;
1700 while (d = readdir(dp), d != NULL) {
1701 if ((strcmp(d->d_name, ".")) && (strcmp(d->d_name, ".."))) {
1707 network_process_file(filename);
1715 * Delete any files in the outbound queue that were intended
1716 * to be sent to nodes which no longer exist.
1718 void network_purge_spoolout(void) {
1725 dp = opendir(ctdl_netout_dir);
1726 if (dp == NULL) return;
1728 while (d = readdir(dp), d != NULL) {
1729 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
1737 strcpy(nexthop, "");
1738 i = is_valid_node(nexthop, NULL, d->d_name);
1740 if ( (i != 0) || !IsEmptyStr(nexthop) ) {
1751 * receive network spool from the remote system
1753 void receive_spool(int sock, char *remote_nodename) {
1754 long download_len = 0L;
1755 long bytes_received = 0L;
1756 long bytes_copied = 0L;
1758 static char pbuf[IGNET_PACKET_SIZE];
1759 char tempfilename[PATH_MAX];
1760 char filename[PATH_MAX];
1764 CtdlMakeTempFileName(tempfilename, sizeof tempfilename);
1765 if (sock_puts(sock, "NDOP") < 0) return;
1766 if (sock_getln(sock, buf, sizeof buf) < 0) return;
1767 lprintf(CTDL_DEBUG, "<%s\n", buf);
1768 if (buf[0] != '2') {
1771 download_len = extract_long(&buf[4], 0);
1773 bytes_received = 0L;
1774 fp = fopen(tempfilename, "w");
1776 lprintf(CTDL_CRIT, "cannot open download file locally: %s\n",
1781 while (bytes_received < download_len) {
1782 snprintf(buf, sizeof buf, "READ %ld|%ld",
1784 ((download_len - bytes_received > IGNET_PACKET_SIZE)
1785 ? IGNET_PACKET_SIZE : (download_len - bytes_received)));
1786 if (sock_puts(sock, buf) < 0) {
1788 unlink(tempfilename);
1791 if (sock_getln(sock, buf, sizeof buf) < 0) {
1793 unlink(tempfilename);
1796 if (buf[0] == '6') {
1797 plen = extract_long(&buf[4], 0);
1798 if (sock_read(sock, pbuf, plen, 1) < 0) {
1800 unlink(tempfilename);
1803 fwrite((char *) pbuf, plen, 1, fp);
1804 bytes_received = bytes_received + plen;
1809 if (sock_puts(sock, "CLOS") < 0) {
1810 unlink(tempfilename);
1813 if (sock_getln(sock, buf, sizeof buf) < 0) {
1814 unlink(tempfilename);
1817 if (download_len > 0) {
1818 lprintf(CTDL_NOTICE, "Received %ld octets from <%s>\n", download_len, remote_nodename);
1820 lprintf(CTDL_DEBUG, "%s\n", buf);
1822 /* Now copy the temp file to its permanent location.
1823 * (We copy instead of link because they may be on different filesystems)
1825 begin_critical_section(S_NETSPOOL);
1833 fp = fopen(tempfilename, "r");
1835 newfp = fopen(filename, "w");
1836 if (newfp != NULL) {
1838 while (bytes_copied < download_len) {
1839 plen = download_len - bytes_copied;
1840 if (plen > sizeof buf) {
1843 fread(buf, plen, 1, fp);
1844 fwrite(buf, plen, 1, newfp);
1845 bytes_copied += plen;
1851 end_critical_section(S_NETSPOOL);
1852 unlink(tempfilename);
1858 * transmit network spool to the remote system
1860 void transmit_spool(int sock, char *remote_nodename)
1865 long bytes_to_write, thisblock, bytes_written;
1869 if (sock_puts(sock, "NUOP") < 0) return;
1870 if (sock_getln(sock, buf, sizeof buf) < 0) return;
1871 lprintf(CTDL_DEBUG, "<%s\n", buf);
1872 if (buf[0] != '2') {
1876 snprintf(sfname, sizeof sfname,
1880 fd = open(sfname, O_RDONLY);
1882 if (errno != ENOENT) {
1883 lprintf(CTDL_CRIT, "cannot open upload file locally: %s\n",
1889 while (plen = (long) read(fd, pbuf, IGNET_PACKET_SIZE), plen > 0L) {
1890 bytes_to_write = plen;
1891 while (bytes_to_write > 0L) {
1892 snprintf(buf, sizeof buf, "WRIT %ld", bytes_to_write);
1893 if (sock_puts(sock, buf) < 0) {
1897 if (sock_getln(sock, buf, sizeof buf) < 0) {
1901 thisblock = atol(&buf[4]);
1902 if (buf[0] == '7') {
1903 if (sock_write(sock, pbuf,
1904 (int) thisblock) < 0) {
1908 bytes_to_write -= thisblock;
1909 bytes_written += thisblock;
1918 if (sock_puts(sock, "UCLS 1") < 0) return;
1919 if (sock_getln(sock, buf, sizeof buf) < 0) return;
1920 lprintf(CTDL_NOTICE, "Sent %ld octets to <%s>\n",
1921 bytes_written, remote_nodename);
1922 lprintf(CTDL_DEBUG, "<%s\n", buf);
1923 if (buf[0] == '2') {
1924 lprintf(CTDL_DEBUG, "Removing <%s>\n", sfname);
1932 * Poll one Citadel node (called by network_poll_other_citadel_nodes() below)
1934 void network_poll_node(char *node, char *secret, char *host, char *port) {
1938 if (network_talking_to(node, NTT_CHECK)) return;
1939 network_talking_to(node, NTT_ADD);
1940 lprintf(CTDL_NOTICE, "Connecting to <%s> at %s:%s\n", node, host, port);
1942 sock = sock_connect(host, port, "tcp");
1944 lprintf(CTDL_ERR, "Could not connect: %s\n", strerror(errno));
1945 network_talking_to(node, NTT_REMOVE);
1949 lprintf(CTDL_DEBUG, "Connected!\n");
1951 /* Read the server greeting */
1952 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
1953 lprintf(CTDL_DEBUG, ">%s\n", buf);
1955 /* Identify ourselves */
1956 snprintf(buf, sizeof buf, "NETP %s|%s", config.c_nodename, secret);
1957 lprintf(CTDL_DEBUG, "<%s\n", buf);
1958 if (sock_puts(sock, buf) <0) goto bail;
1959 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
1960 lprintf(CTDL_DEBUG, ">%s\n", buf);
1961 if (buf[0] != '2') goto bail;
1963 /* At this point we are authenticated. */
1964 receive_spool(sock, node);
1965 transmit_spool(sock, node);
1967 sock_puts(sock, "QUIT");
1968 bail: sock_close(sock);
1969 network_talking_to(node, NTT_REMOVE);
1975 * Poll other Citadel nodes and transfer inbound/outbound network data.
1976 * Set "full" to nonzero to force a poll of every node, or to zero to poll
1977 * only nodes to which we have data to send.
1979 void network_poll_other_citadel_nodes(int full_poll) {
1987 char spoolfile[256];
1989 if (working_ignetcfg == NULL) {
1990 lprintf(CTDL_DEBUG, "No nodes defined - not polling\n");
1994 /* Use the string tokenizer to grab one line at a time */
1995 for (i=0; i<num_tokens(working_ignetcfg, '\n'); ++i) {
1996 extract_token(linebuf, working_ignetcfg, i, '\n', sizeof linebuf);
1997 extract_token(node, linebuf, 0, '|', sizeof node);
1998 extract_token(secret, linebuf, 1, '|', sizeof secret);
1999 extract_token(host, linebuf, 2, '|', sizeof host);
2000 extract_token(port, linebuf, 3, '|', sizeof port);
2001 if ( !IsEmptyStr(node) && !IsEmptyStr(secret)
2002 && !IsEmptyStr(host) && !IsEmptyStr(port)) {
2010 if (access(spoolfile, R_OK) == 0) {
2015 network_poll_node(node, secret, host, port);
2026 * It's ok if these directories already exist. Just fail silently.
2028 void create_spool_dirs(void) {
2029 mkdir(ctdl_spool_dir, 0700);
2030 chown(ctdl_spool_dir, CTDLUID, (-1));
2031 mkdir(ctdl_netin_dir, 0700);
2032 chown(ctdl_netin_dir, CTDLUID, (-1));
2033 mkdir(ctdl_netout_dir, 0700);
2034 chown(ctdl_netout_dir, CTDLUID, (-1));
2042 * network_do_queue()
2044 * Run through the rooms doing various types of network stuff.
2046 void *network_do_queue(void *args) {
2047 static time_t last_run = 0L;
2048 struct RoomProcList *ptr;
2049 int full_processing = 1;
2052 * Run the full set of processing tasks no more frequently
2053 * than once every n seconds
2055 if ( (time(NULL) - last_run) < config.c_net_freq ) {
2056 full_processing = 0;
2057 CtdlLogPrintf(CTDL_DEBUG, "Network full processing in %ld seconds.\n", config.c_net_freq - (time(NULL)- last_run));
2061 * This is a simple concurrency check to make sure only one queue run
2062 * is done at a time. We could do this with a mutex, but since we
2063 * don't really require extremely fine granularity here, we'll do it
2064 * with a static variable instead.
2066 if (doing_queue) return NULL;
2069 /* Load the IGnet Configuration into memory */
2070 load_working_ignetcfg();
2073 * Poll other Citadel nodes. Maybe. If "full_processing" is set
2074 * then we poll everyone. Otherwise we only poll nodes we have stuff
2077 network_poll_other_citadel_nodes(full_processing);
2080 * Load the network map and filter list into memory.
2083 filterlist = load_filter_list();
2086 * Go ahead and run the queue
2088 if (full_processing) {
2089 lprintf(CTDL_DEBUG, "network: loading outbound queue\n");
2090 ForEachRoom(network_queue_room, NULL);
2093 if (rplist != NULL) {
2094 lprintf(CTDL_DEBUG, "network: running outbound queue\n");
2095 while (rplist != NULL) {
2096 char spoolroomname[ROOMNAMELEN];
2097 safestrncpy(spoolroomname, rplist->name, sizeof spoolroomname);
2098 begin_critical_section(S_RPLIST);
2100 /* pop this record off the list */
2102 rplist = rplist->next;
2105 /* invalidate any duplicate entries to prevent double processing */
2106 for (ptr=rplist; ptr!=NULL; ptr=ptr->next) {
2107 if (!strcasecmp(ptr->name, spoolroomname)) {
2112 end_critical_section(S_RPLIST);
2113 if (spoolroomname[0] != 0) {
2114 network_spoolout_room(spoolroomname);
2119 /* If there is anything in the inbound queue, process it */
2120 network_do_spoolin();
2122 /* Save the network map back to disk */
2123 write_network_map();
2125 /* Free the filter list in memory */
2126 free_filter_list(filterlist);
2129 network_purge_spoolout();
2131 lprintf(CTDL_DEBUG, "network: queue run completed\n");
2133 if (full_processing) {
2134 last_run = time(NULL);
2138 CtdlThreadSchedule("IGnet Network", CTDLTHREAD_BIGSTACK, network_do_queue, NULL, time(NULL) + 60);
2144 * cmd_netp() - authenticate to the server as another Citadel node polling
2145 * for network traffic
2147 void cmd_netp(char *cmdbuf)
2157 extract_token(node, cmdbuf, 0, '|', sizeof node);
2158 extract_token(pass, cmdbuf, 1, '|', sizeof pass);
2161 lprintf(CTDL_WARNING, "Network node <%s> refused - spooling", node);
2162 cprintf("%d spooling - try again in a few minutes\n",
2163 ERROR + RESOURCE_BUSY);
2167 /* load the IGnet Configuration to check node validity */
2168 load_working_ignetcfg();
2169 v = is_valid_node(nexthop, secret, node);
2172 lprintf(CTDL_WARNING, "Unknown node <%s>\n", node);
2173 cprintf("%d authentication failed\n",
2174 ERROR + PASSWORD_REQUIRED);
2178 if (strcasecmp(pass, secret)) {
2179 lprintf(CTDL_WARNING, "Bad password for network node <%s>", node);
2180 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
2184 if (network_talking_to(node, NTT_CHECK)) {
2185 lprintf(CTDL_WARNING, "Duplicate session for network node <%s>", node);
2186 cprintf("%d Already talking to %s right now\n", ERROR + RESOURCE_BUSY, node);
2190 safestrncpy(CC->net_node, node, sizeof CC->net_node);
2191 network_talking_to(node, NTT_ADD);
2192 lprintf(CTDL_NOTICE, "Network node <%s> logged in\n", CC->net_node);
2193 cprintf("%d authenticated as network node '%s'\n", CIT_OK,
2197 int network_room_handler (struct ctdlroom *room)
2199 network_queue_room(room, NULL);
2204 * Module entry point
2206 CTDL_MODULE_INIT(network)
2210 create_spool_dirs();
2211 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
2212 CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");
2213 CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");
2214 CtdlRegisterProtoHook(cmd_nsyn, "NSYN", "Synchronize room to node");
2215 // CtdlRegisterSessionHook(network_do_queue, EVT_TIMER);
2216 CtdlRegisterRoomHook(network_room_handler);
2217 CtdlRegisterCleanupHook(destroy_network_queue_room);
2220 CtdlThreadSchedule("IGnet Network", CTDLTHREAD_BIGSTACK, network_do_queue, NULL, 0);
2221 /* return our Subversion id for the Log */