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.
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 */
34 #include <sys/types.h>
36 #if TIME_WITH_SYS_TIME
37 # include <sys/time.h>
41 # include <sys/time.h>
52 #include "sysdep_decls.h"
53 #include "citserver.h"
56 #include "serv_extensions.h"
63 #include "internet_addressing.h"
64 #include "serv_network.h"
65 #include "clientsocket.h"
72 /* Nonzero while we are doing outbound network processing */
73 static int doing_queue = 0;
76 * When we do network processing, it's accomplished in two passes; one to
77 * gather a list of rooms and one to actually do them. It's ok that rplist
78 * is global; this process *only* runs as part of the housekeeping loop and
79 * therefore only one will run at a time.
81 struct RoomProcList *rplist = NULL;
84 * We build a map of network nodes during processing.
86 struct NetMap *the_netmap = NULL;
87 char *working_ignetcfg = NULL;
90 * Load or refresh the Citadel network (IGnet) configuration for this node.
92 void load_working_ignetcfg(void) {
96 cfg = CtdlGetSysConfig(IGNETCFG);
101 oldcfg = working_ignetcfg;
102 working_ignetcfg = cfg;
103 if (oldcfg != NULL) {
113 * Keep track of what messages to reject
115 struct FilterList *load_filter_list(void) {
116 char *serialized_list = NULL;
119 struct FilterList *newlist = NULL;
120 struct FilterList *nptr;
122 serialized_list = CtdlGetSysConfig(FILTERLIST);
123 if (serialized_list == NULL) return(NULL); /* if null, no entries */
125 /* Use the string tokenizer to grab one line at a time */
126 for (i=0; i<num_tokens(serialized_list, '\n'); ++i) {
127 extract_token(buf, serialized_list, i, '\n', sizeof buf);
128 nptr = (struct FilterList *) malloc(sizeof(struct FilterList));
129 extract_token(nptr->fl_user, buf, 0, '|', sizeof nptr->fl_user);
130 striplt(nptr->fl_user);
131 extract_token(nptr->fl_room, buf, 1, '|', sizeof nptr->fl_room);
132 striplt(nptr->fl_room);
133 extract_token(nptr->fl_node, buf, 2, '|', sizeof nptr->fl_node);
134 striplt(nptr->fl_node);
136 /* Cowardly refuse to add an any/any/any entry that would
137 * end up filtering every single message.
139 if (strlen(nptr->fl_user) + strlen(nptr->fl_room)
140 + strlen(nptr->fl_node) == 0) {
144 nptr->next = newlist;
149 free(serialized_list);
154 void free_filter_list(struct FilterList *fl) {
155 if (fl == NULL) return;
156 free_filter_list(fl->next);
163 * Check the use table. This is a list of messages which have recently
164 * arrived on the system. It is maintained and queried to prevent the same
165 * message from being entered into the database multiple times if it happens
166 * to arrive multiple times by accident.
168 int network_usetable(struct CtdlMessage *msg) {
171 struct cdbdata *cdbut;
174 /* Bail out if we can't generate a message ID */
178 if (msg->cm_fields['I'] == NULL) {
181 if (strlen(msg->cm_fields['I']) == 0) {
185 /* Generate the message ID */
186 strcpy(msgid, msg->cm_fields['I']);
187 if (haschar(msgid, '@') == 0) {
189 if (msg->cm_fields['N'] != NULL) {
190 strcat(msgid, msg->cm_fields['N']);
197 cdbut = cdb_fetch(CDB_USETABLE, msgid, strlen(msgid));
203 /* If we got to this point, it's unique: add it. */
204 strcpy(ut.ut_msgid, msgid);
205 ut.ut_timestamp = time(NULL);
206 cdb_store(CDB_USETABLE, msgid, strlen(msgid),
207 &ut, sizeof(struct UseTable) );
213 * Read the network map from its configuration file into memory.
215 void read_network_map(void) {
216 char *serialized_map = NULL;
219 struct NetMap *nmptr;
221 serialized_map = CtdlGetSysConfig(IGNETMAP);
222 if (serialized_map == NULL) return; /* if null, no entries */
224 /* Use the string tokenizer to grab one line at a time */
225 for (i=0; i<num_tokens(serialized_map, '\n'); ++i) {
226 extract_token(buf, serialized_map, i, '\n', sizeof buf);
227 nmptr = (struct NetMap *) malloc(sizeof(struct NetMap));
228 extract_token(nmptr->nodename, buf, 0, '|', sizeof nmptr->nodename);
229 nmptr->lastcontact = extract_long(buf, 1);
230 extract_token(nmptr->nexthop, buf, 2, '|', sizeof nmptr->nexthop);
231 nmptr->next = the_netmap;
235 free(serialized_map);
240 * Write the network map from memory back to the configuration file.
242 void write_network_map(void) {
243 char *serialized_map = NULL;
244 struct NetMap *nmptr;
246 serialized_map = strdup("");
248 if (the_netmap != NULL) {
249 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
250 serialized_map = realloc(serialized_map,
251 (strlen(serialized_map)+SIZ) );
252 if (strlen(nmptr->nodename) > 0) {
253 snprintf(&serialized_map[strlen(serialized_map)],
257 (long)nmptr->lastcontact,
263 CtdlPutSysConfig(IGNETMAP, serialized_map);
264 free(serialized_map);
266 /* Now free the list */
267 while (the_netmap != NULL) {
268 nmptr = the_netmap->next;
277 * Check the network map and determine whether the supplied node name is
278 * valid. If it is not a neighbor node, supply the name of a neighbor node
279 * which is the next hop. If it *is* a neighbor node, we also fill in the
282 int is_valid_node(char *nexthop, char *secret, char *node) {
287 struct NetMap *nmptr;
294 * First try the neighbor nodes
296 if (working_ignetcfg == NULL) {
297 lprintf(CTDL_ERR, "working_ignetcfg is NULL!\n");
298 if (nexthop != NULL) {
305 if (nexthop != NULL) {
309 /* Use the string tokenizer to grab one line at a time */
310 for (i=0; i<num_tokens(working_ignetcfg, '\n'); ++i) {
311 extract_token(linebuf, working_ignetcfg, i, '\n', sizeof linebuf);
312 extract_token(buf, linebuf, 0, '|', sizeof buf);
313 if (!strcasecmp(buf, node)) {
314 if (nexthop != NULL) {
317 if (secret != NULL) {
318 extract_token(secret, linebuf, 1, '|', 256);
325 return(retval); /* yup, it's a direct neighbor */
329 * If we get to this point we have to see if we know the next hop
331 if (the_netmap != NULL) {
332 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
333 if (!strcasecmp(nmptr->nodename, node)) {
334 if (nexthop != NULL) {
335 strcpy(nexthop, nmptr->nexthop);
343 * If we get to this point, the supplied node name is bogus.
345 lprintf(CTDL_ERR, "Invalid node name <%s>\n", node);
353 void cmd_gnet(char *argbuf) {
358 if (CtdlAccessCheck(ac_room_aide)) return;
359 assoc_file_name(filename, sizeof filename, &CC->room, "netconfigs");
360 cprintf("%d Network settings for room #%ld <%s>\n",
362 CC->room.QRnumber, CC->room.QRname);
364 fp = fopen(filename, "r");
366 while (fgets(buf, sizeof buf, fp) != NULL) {
367 buf[strlen(buf)-1] = 0;
368 cprintf("%s\n", buf);
377 void cmd_snet(char *argbuf) {
378 char tempfilename[SIZ];
385 if (CtdlAccessCheck(ac_room_aide)) return;
386 safestrncpy(tempfilename, tmpnam(NULL), sizeof tempfilename);
387 assoc_file_name(filename, sizeof filename, &CC->room, "netconfigs");
389 fp = fopen(tempfilename, "w");
391 cprintf("%d Cannot open %s: %s\n",
392 ERROR + INTERNAL_ERROR,
397 cprintf("%d %s\n", SEND_LISTING, tempfilename);
398 while (client_getln(buf, sizeof buf), strcmp(buf, "000")) {
399 fprintf(fp, "%s\n", buf);
403 /* Now copy the temp file to its permanent location
404 * (We use /bin/mv instead of link() because they may be on
405 * different filesystems)
408 snprintf(buf, sizeof buf, "/bin/mv %s %s", tempfilename, filename);
409 begin_critical_section(S_NETCONFIGS);
411 end_critical_section(S_NETCONFIGS);
416 * Spools out one message from the list.
418 void network_spool_msg(long msgnum, void *userdata) {
419 struct SpoolControl *sc;
422 char *newpath = NULL;
424 size_t instr_len = SIZ;
425 struct CtdlMessage *msg = NULL;
426 struct CtdlMessage *imsg;
427 struct namelist *nptr;
428 struct maplist *mptr;
429 struct ser_ret sermsg;
435 int delete_after_send = 0; /* Set to 1 to delete after spooling */
436 long list_msgnum = 0L;
437 int ok_to_participate = 0;
438 char *end_of_headers = NULL;
440 sc = (struct SpoolControl *)userdata;
443 * Process mailing list recipients
446 if (sc->listrecps != NULL) {
448 /* First, copy it to the spoolout room */
449 err = CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, msgnum, 0);
450 if (err != 0) return;
453 * Figure out how big a buffer we need to allocate
455 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
456 instr_len = instr_len + strlen(nptr->name);
462 lprintf(CTDL_DEBUG, "Generating delivery instructions\n");
463 instr = malloc(instr_len);
466 "Cannot allocate %ld bytes for instr...\n",
470 snprintf(instr, instr_len,
471 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
472 "bounceto|postmaster@%s\n" ,
473 SPOOLMIME, msgnum, (long)time(NULL), config.c_fqdn );
475 /* Generate delivery instructions for each recipient */
476 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
477 size_t tmp = strlen(instr);
478 snprintf(&instr[tmp], instr_len - tmp,
479 "remote|%s|0||\n", nptr->name);
483 * Generate a message from the instructions
485 imsg = malloc(sizeof(struct CtdlMessage));
486 memset(imsg, 0, sizeof(struct CtdlMessage));
487 imsg->cm_magic = CTDLMESSAGE_MAGIC;
488 imsg->cm_anon_type = MES_NORMAL;
489 imsg->cm_format_type = FMT_RFC822;
490 imsg->cm_fields['A'] = strdup("Citadel");
491 imsg->cm_fields['M'] = instr;
493 /* Save delivery instructions in spoolout room */
494 CtdlSubmitMsg(imsg, NULL, SMTP_SPOOLOUT_ROOM);
495 CtdlFreeMessage(imsg);
499 * Process digest recipients
501 if ((sc->digestrecps != NULL) && (sc->digestfp != NULL)) {
502 msg = CtdlFetchMessage(msgnum, 1);
504 fprintf(sc->digestfp, " -----------------------------------"
505 "------------------------------------"
507 fprintf(sc->digestfp, "From: ");
508 if (msg->cm_fields['A'] != NULL) {
509 fprintf(sc->digestfp, "%s ", msg->cm_fields['A']);
511 if (msg->cm_fields['F'] != NULL) {
512 fprintf(sc->digestfp, "<%s> ", msg->cm_fields['F']);
514 else if (msg->cm_fields['N'] != NULL) {
515 fprintf(sc->digestfp, "@%s ", msg->cm_fields['N']);
517 fprintf(sc->digestfp, "\n");
518 if (msg->cm_fields['U'] != NULL) {
519 fprintf(sc->digestfp, "Subject: %s\n", msg->cm_fields['U']);
522 CC->redirect_buffer = malloc(SIZ);
523 CC->redirect_len = 0;
524 CC->redirect_alloc = SIZ;
526 safestrncpy(CC->preferred_formats, "text/plain", sizeof CC->preferred_formats);
527 CtdlOutputPreLoadedMsg(msg, 0L, MT_MIME, HEADERS_NONE, 0, 0);
529 end_of_headers = bmstrstr(CC->redirect_buffer, "\n\r\n", strncmp);
530 if (end_of_headers == NULL) {
531 end_of_headers = bmstrstr(CC->redirect_buffer, "\n\n", strncmp);
533 if (end_of_headers == NULL) {
534 end_of_headers = CC->redirect_buffer;
536 striplt(end_of_headers);
537 fprintf(sc->digestfp, "\n%s\n", end_of_headers);
539 free(CC->redirect_buffer);
540 CC->redirect_buffer = NULL;
541 CC->redirect_len = 0;
542 CC->redirect_alloc = 0;
544 sc->num_msgs_spooled += 1;
550 * Process client-side list participations for this room
553 if (sc->participates != NULL) {
554 msg = CtdlFetchMessage(msgnum, 1);
557 /* Only send messages which originated on our own Citadel
558 * network, otherwise we'll end up sending the remote
559 * mailing list's messages back to it, which is rude...
561 ok_to_participate = 0;
562 if (msg->cm_fields['N'] != NULL) {
563 if (!strcasecmp(msg->cm_fields['N'], config.c_nodename)) {
564 ok_to_participate = 1;
566 if (is_valid_node(NULL, NULL, msg->cm_fields['N']) == 0) {
567 ok_to_participate = 1;
570 if (ok_to_participate) {
571 if (msg->cm_fields['F'] != NULL) {
572 free(msg->cm_fields['F']);
574 msg->cm_fields['F'] = malloc(SIZ);
575 /* Replace the Internet email address of the actual
576 * author with the email address of the room itself,
577 * so the remote listserv doesn't reject us.
578 * FIXME ... I want to be able to pick any address
580 snprintf(msg->cm_fields['F'], SIZ,
581 "room_%s@%s", CC->room.QRname,
583 for (i=0; i<strlen(msg->cm_fields['F']); ++i) {
584 if (isspace(msg->cm_fields['F'][i])) {
585 msg->cm_fields['F'][i] = '_';
589 /* Now save it and generate delivery instructions */
590 list_msgnum = CtdlSubmitMsg(msg, NULL, SMTP_SPOOLOUT_ROOM);
593 * Figure out how big a buffer we need to allocate
595 for (nptr = sc->participates; nptr != NULL; nptr = nptr->next) {
596 instr_len = instr_len + strlen(nptr->name);
602 instr = malloc(instr_len);
605 "Cannot allocate %ld bytes for instr...\n",
609 snprintf(instr, instr_len,
610 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
611 "bounceto|postmaster@%s\n" ,
612 SPOOLMIME, list_msgnum, (long)time(NULL), config.c_fqdn );
614 /* Generate delivery instructions for each recipient */
615 for (nptr = sc->participates; nptr != NULL; nptr = nptr->next) {
616 size_t tmp = strlen(instr);
617 snprintf(&instr[tmp], instr_len - tmp,
618 "remote|%s|0||\n", nptr->name);
622 * Generate a message from the instructions
624 imsg = malloc(sizeof(struct CtdlMessage));
625 memset(imsg, 0, sizeof(struct CtdlMessage));
626 imsg->cm_magic = CTDLMESSAGE_MAGIC;
627 imsg->cm_anon_type = MES_NORMAL;
628 imsg->cm_format_type = FMT_RFC822;
629 imsg->cm_fields['A'] = strdup("Citadel");
630 imsg->cm_fields['M'] = instr;
632 /* Save delivery instructions in spoolout room */
633 CtdlSubmitMsg(imsg, NULL, SMTP_SPOOLOUT_ROOM);
634 CtdlFreeMessage(imsg);
636 CtdlFreeMessage(msg);
641 * Process IGnet push shares
643 if (sc->ignet_push_shares != NULL) {
645 msg = CtdlFetchMessage(msgnum, 1);
649 /* Prepend our node name to the Path field whenever
650 * sending a message to another IGnet node
652 if (msg->cm_fields['P'] == NULL) {
653 msg->cm_fields['P'] = strdup("username");
655 newpath_len = strlen(msg->cm_fields['P']) +
656 strlen(config.c_nodename) + 2;
657 newpath = malloc(newpath_len);
658 snprintf(newpath, newpath_len, "%s!%s",
659 config.c_nodename, msg->cm_fields['P']);
660 free(msg->cm_fields['P']);
661 msg->cm_fields['P'] = newpath;
664 * Determine if this message is set to be deleted
665 * after sending out on the network
667 if (msg->cm_fields['S'] != NULL) {
668 if (!strcasecmp(msg->cm_fields['S'],
670 delete_after_send = 1;
674 /* Now send it to every node */
675 for (mptr = sc->ignet_push_shares; mptr != NULL;
680 /* Check for valid node name */
681 if (is_valid_node(NULL, NULL, mptr->remote_nodename) != 0) {
682 lprintf(CTDL_ERR, "Invalid node <%s>\n",
683 mptr->remote_nodename);
687 /* Check for split horizon */
688 lprintf(CTDL_DEBUG, "Path is %s\n", msg->cm_fields['P']);
689 bang = num_tokens(msg->cm_fields['P'], '!');
690 if (bang > 1) for (i=0; i<(bang-1); ++i) {
691 extract_token(buf, msg->cm_fields['P'],
693 if (!strcasecmp(buf, mptr->remote_nodename)) {
698 /* Send the message */
702 * Force the message to appear in the correct room
703 * on the far end by setting the C field correctly
705 if (msg->cm_fields['C'] != NULL) {
706 free(msg->cm_fields['C']);
708 if (strlen(mptr->remote_roomname) > 0) {
709 msg->cm_fields['C'] = strdup(mptr->remote_roomname);
712 msg->cm_fields['C'] = strdup(CC->room.QRname);
715 /* serialize it for transmission */
716 serialize_message(&sermsg, msg);
718 /* write it to the spool file */
719 snprintf(filename, sizeof filename,
720 "./network/spoolout/%s",
721 mptr->remote_nodename);
722 fp = fopen(filename, "ab");
729 /* free the serialized version */
733 CtdlFreeMessage(msg);
737 /* update lastsent */
738 sc->lastsent = msgnum;
740 /* Delete this message if delete-after-send is set */
741 if (delete_after_send) {
742 CtdlDeleteMessages(CC->room.QRname, msgnum, "");
749 * Deliver digest messages
751 void network_deliver_digest(struct SpoolControl *sc) {
754 struct CtdlMessage *msg;
758 size_t instr_len = SIZ;
759 struct CtdlMessage *imsg;
760 struct namelist *nptr;
762 if (sc->num_msgs_spooled < 1) {
763 fclose(sc->digestfp);
768 msg = malloc(sizeof(struct CtdlMessage));
769 memset(msg, 0, sizeof(struct CtdlMessage));
770 msg->cm_magic = CTDLMESSAGE_MAGIC;
771 msg->cm_format_type = FMT_RFC822;
772 msg->cm_anon_type = MES_NORMAL;
774 sprintf(buf, "%ld", time(NULL));
775 msg->cm_fields['T'] = strdup(buf);
776 msg->cm_fields['A'] = strdup(CC->room.QRname);
777 msg->cm_fields['U'] = strdup(CC->room.QRname);
778 sprintf(buf, "room_%s@%s", CC->room.QRname, config.c_fqdn);
779 for (i=0; i<strlen(buf); ++i) {
780 if (isspace(buf[i])) buf[i]='_';
781 buf[i] = tolower(buf[i]);
783 msg->cm_fields['F'] = strdup(buf);
785 fseek(sc->digestfp, 0L, SEEK_END);
786 msglen = ftell(sc->digestfp);
788 msg->cm_fields['M'] = malloc(msglen + 1);
789 fseek(sc->digestfp, 0L, SEEK_SET);
790 fread(msg->cm_fields['M'], (size_t)msglen, 1, sc->digestfp);
791 msg->cm_fields['M'][msglen] = 0;
793 fclose(sc->digestfp);
796 msgnum = CtdlSubmitMsg(msg, NULL, SMTP_SPOOLOUT_ROOM);
797 CtdlFreeMessage(msg);
799 /* Now generate the delivery instructions */
802 * Figure out how big a buffer we need to allocate
804 for (nptr = sc->digestrecps; nptr != NULL; nptr = nptr->next) {
805 instr_len = instr_len + strlen(nptr->name);
811 lprintf(CTDL_DEBUG, "Generating delivery instructions\n");
812 instr = malloc(instr_len);
814 lprintf(CTDL_EMERG, "Cannot allocate %ld bytes for instr...\n",
818 snprintf(instr, instr_len,
819 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
820 "bounceto|postmaster@%s\n" ,
821 SPOOLMIME, msgnum, (long)time(NULL), config.c_fqdn );
823 /* Generate delivery instructions for each recipient */
824 for (nptr = sc->digestrecps; nptr != NULL; nptr = nptr->next) {
825 size_t tmp = strlen(instr);
826 snprintf(&instr[tmp], instr_len - tmp,
827 "remote|%s|0||\n", nptr->name);
831 * Generate a message from the instructions
833 imsg = malloc(sizeof(struct CtdlMessage));
834 memset(imsg, 0, sizeof(struct CtdlMessage));
835 imsg->cm_magic = CTDLMESSAGE_MAGIC;
836 imsg->cm_anon_type = MES_NORMAL;
837 imsg->cm_format_type = FMT_RFC822;
838 imsg->cm_fields['A'] = strdup("Citadel");
839 imsg->cm_fields['M'] = instr;
841 /* Save delivery instructions in spoolout room */
842 CtdlSubmitMsg(imsg, NULL, SMTP_SPOOLOUT_ROOM);
843 CtdlFreeMessage(imsg);
848 * Batch up and send all outbound traffic from the current room
850 void network_spoolout_room(char *room_to_spool) {
855 char roomname[ROOMNAMELEN];
858 struct SpoolControl sc;
859 struct namelist *nptr = NULL;
860 struct maplist *mptr = NULL;
863 int skipthisline = 0;
866 if (getroom(&CC->room, room_to_spool) != 0) {
867 lprintf(CTDL_CRIT, "ERROR: cannot load <%s>\n", room_to_spool);
871 memset(&sc, 0, sizeof(struct SpoolControl));
872 assoc_file_name(filename, sizeof filename, &CC->room, "netconfigs");
874 begin_critical_section(S_NETCONFIGS);
876 fp = fopen(filename, "r");
878 end_critical_section(S_NETCONFIGS);
882 lprintf(CTDL_INFO, "Networking started for <%s>\n", CC->room.QRname);
884 while (fgets(buf, sizeof buf, fp) != NULL) {
885 buf[strlen(buf)-1] = 0;
887 extract_token(instr, buf, 0, '|', sizeof instr);
888 if (!strcasecmp(instr, "lastsent")) {
889 sc.lastsent = extract_long(buf, 1);
891 else if (!strcasecmp(instr, "listrecp")) {
892 nptr = (struct namelist *)
893 malloc(sizeof(struct namelist));
894 nptr->next = sc.listrecps;
895 extract_token(nptr->name, buf, 1, '|', sizeof nptr->name);
898 else if (!strcasecmp(instr, "participate")) {
899 nptr = (struct namelist *)
900 malloc(sizeof(struct namelist));
901 nptr->next = sc.participates;
902 extract_token(nptr->name, buf, 1, '|', sizeof nptr->name);
903 sc.participates = nptr;
905 else if (!strcasecmp(instr, "digestrecp")) {
906 nptr = (struct namelist *)
907 malloc(sizeof(struct namelist));
908 nptr->next = sc.digestrecps;
909 extract_token(nptr->name, buf, 1, '|', sizeof nptr->name);
910 sc.digestrecps = nptr;
912 else if (!strcasecmp(instr, "ignet_push_share")) {
913 /* by checking each node's validity, we automatically
914 * purge nodes which do not exist from room network
915 * configurations at this time.
917 extract_token(nodename, buf, 1, '|', sizeof nodename);
918 extract_token(roomname, buf, 2, '|', sizeof roomname);
919 strcpy(nexthop, "xxx");
920 if (is_valid_node(nexthop, NULL, nodename) == 0) {
921 if (strlen(nexthop) == 0) {
922 mptr = (struct maplist *)
923 malloc(sizeof(struct maplist));
924 mptr->next = sc.ignet_push_shares;
925 strcpy(mptr->remote_nodename, nodename);
926 strcpy(mptr->remote_roomname, roomname);
927 sc.ignet_push_shares = mptr;
932 /* Preserve 'other' lines ... *unless* they happen to
933 * be subscribe/unsubscribe pendings with expired
937 if (!strncasecmp(buf, "subpending|", 11)) {
938 if (time(NULL) - extract_long(buf, 4) > EXP) {
942 if (!strncasecmp(buf, "unsubpending|", 13)) {
943 if (time(NULL) - extract_long(buf, 3) > EXP) {
948 if (skipthisline == 0) {
949 linesize = strlen(buf);
950 sc.misc = realloc(sc.misc,
951 (miscsize + linesize + 2) );
952 sprintf(&sc.misc[miscsize], "%s\n", buf);
953 miscsize = miscsize + linesize + 1;
961 /* If there are digest recipients, we have to build a digest */
962 if (sc.digestrecps != NULL) {
963 sc.digestfp = tmpfile();
964 fprintf(sc.digestfp, "Content-type: text/plain\n\n");
967 /* Do something useful */
968 CtdlForEachMessage(MSGS_GT, sc.lastsent, NULL, NULL,
969 network_spool_msg, &sc);
971 /* If we wrote a digest, deliver it and then close it */
972 snprintf(buf, sizeof buf, "room_%s@%s",
973 CC->room.QRname, config.c_fqdn);
974 for (i=0; i<strlen(buf); ++i) {
975 buf[i] = tolower(buf[i]);
976 if (isspace(buf[i])) buf[i] = '_';
978 if (sc.digestfp != NULL) {
979 fprintf(sc.digestfp, " -----------------------------------"
980 "------------------------------------"
982 "You are subscribed to the '%s' "
984 "To post to the list: %s\n",
987 network_deliver_digest(&sc); /* deliver and close */
990 /* Now rewrite the config file */
991 fp = fopen(filename, "w");
993 lprintf(CTDL_CRIT, "ERROR: cannot open %s: %s\n",
994 filename, strerror(errno));
997 fprintf(fp, "lastsent|%ld\n", sc.lastsent);
999 /* Write out the listrecps while freeing from memory at the
1000 * same time. Am I clever or what? :)
1002 while (sc.listrecps != NULL) {
1003 fprintf(fp, "listrecp|%s\n", sc.listrecps->name);
1004 nptr = sc.listrecps->next;
1006 sc.listrecps = nptr;
1008 /* Do the same for digestrecps */
1009 while (sc.digestrecps != NULL) {
1010 fprintf(fp, "digestrecp|%s\n", sc.digestrecps->name);
1011 nptr = sc.digestrecps->next;
1012 free(sc.digestrecps);
1013 sc.digestrecps = nptr;
1015 /* Do the same for participates */
1016 while (sc.participates != NULL) {
1017 fprintf(fp, "participate|%s\n", sc.participates->name);
1018 nptr = sc.participates->next;
1019 free(sc.participates);
1020 sc.participates = nptr;
1022 while (sc.ignet_push_shares != NULL) {
1023 /* by checking each node's validity, we automatically
1024 * purge nodes which do not exist from room network
1025 * configurations at this time.
1027 if (is_valid_node(NULL, NULL, sc.ignet_push_shares->remote_nodename) == 0) {
1029 fprintf(fp, "ignet_push_share|%s",
1030 sc.ignet_push_shares->remote_nodename);
1031 if (strlen(sc.ignet_push_shares->remote_roomname) > 0) {
1032 fprintf(fp, "|%s", sc.ignet_push_shares->remote_roomname);
1035 mptr = sc.ignet_push_shares->next;
1036 free(sc.ignet_push_shares);
1037 sc.ignet_push_shares = mptr;
1039 if (sc.misc != NULL) {
1040 fwrite(sc.misc, strlen(sc.misc), 1, fp);
1046 end_critical_section(S_NETCONFIGS);
1052 * Send the *entire* contents of the current room to one specific network node,
1053 * ignoring anything we know about which messages have already undergone
1054 * network processing. This can be used to bring a new node into sync.
1056 int network_sync_to(char *target_node) {
1057 struct SpoolControl sc;
1058 int num_spooled = 0;
1067 /* Grab the configuration line we're looking for */
1068 assoc_file_name(filename, sizeof filename, &CC->room, "netconfigs");
1069 begin_critical_section(S_NETCONFIGS);
1070 fp = fopen(filename, "r");
1072 end_critical_section(S_NETCONFIGS);
1075 while (fgets(buf, sizeof buf, fp) != NULL) {
1076 buf[strlen(buf)-1] = 0;
1077 extract_token(sc_type, buf, 0, '|', sizeof sc_type);
1078 extract_token(sc_node, buf, 1, '|', sizeof sc_node);
1079 extract_token(sc_room, buf, 2, '|', sizeof sc_room);
1080 if ( (!strcasecmp(sc_type, "ignet_push_share"))
1081 && (!strcasecmp(sc_node, target_node)) ) {
1084 /* Concise syntax because we don't need a full linked-list */
1085 memset(&sc, 0, sizeof(struct SpoolControl));
1086 sc.ignet_push_shares = (struct maplist *)
1087 malloc(sizeof(struct maplist));
1088 sc.ignet_push_shares->next = NULL;
1089 safestrncpy(sc.ignet_push_shares->remote_nodename,
1091 sizeof sc.ignet_push_shares->remote_nodename);
1092 safestrncpy(sc.ignet_push_shares->remote_roomname,
1094 sizeof sc.ignet_push_shares->remote_roomname);
1098 end_critical_section(S_NETCONFIGS);
1100 if (!found_node) return(-1);
1102 /* Send ALL messages */
1103 num_spooled = CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL,
1104 network_spool_msg, &sc);
1106 /* Concise cleanup because we know there's only one node in the sc */
1107 free(sc.ignet_push_shares);
1109 lprintf(CTDL_NOTICE, "Synchronized %d messages to <%s>\n",
1110 num_spooled, target_node);
1111 return(num_spooled);
1116 * Implements the NSYN command
1118 void cmd_nsyn(char *argbuf) {
1120 char target_node[256];
1122 if (CtdlAccessCheck(ac_aide)) return;
1124 extract_token(target_node, argbuf, 0, '|', sizeof target_node);
1125 num_spooled = network_sync_to(target_node);
1126 if (num_spooled >= 0) {
1127 cprintf("%d Spooled %d messages.\n", CIT_OK, num_spooled);
1130 cprintf("%d No such room/node share exists.\n",
1131 ERROR + ROOM_NOT_FOUND);
1138 * Batch up and send all outbound traffic from the current room
1140 void network_queue_room(struct ctdlroom *qrbuf, void *data) {
1141 struct RoomProcList *ptr;
1143 ptr = (struct RoomProcList *) malloc(sizeof (struct RoomProcList));
1144 if (ptr == NULL) return;
1146 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
1153 * Learn topology from path fields
1155 void network_learn_topology(char *node, char *path) {
1157 struct NetMap *nmptr;
1159 strcpy(nexthop, "");
1161 if (num_tokens(path, '!') < 3) return;
1162 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
1163 if (!strcasecmp(nmptr->nodename, node)) {
1164 extract_token(nmptr->nexthop, path, 0, '!', sizeof nmptr->nexthop);
1165 nmptr->lastcontact = time(NULL);
1170 /* If we got here then it's not in the map, so add it. */
1171 nmptr = (struct NetMap *) malloc(sizeof (struct NetMap));
1172 strcpy(nmptr->nodename, node);
1173 nmptr->lastcontact = time(NULL);
1174 extract_token(nmptr->nexthop, path, 0, '!', sizeof nmptr->nexthop);
1175 nmptr->next = the_netmap;
1183 * Bounce a message back to the sender
1185 void network_bounce(struct CtdlMessage *msg, char *reason) {
1186 char *oldpath = NULL;
1188 char bouncesource[SIZ];
1189 char recipient[SIZ];
1190 struct recptypes *valid = NULL;
1191 char force_room[ROOMNAMELEN];
1192 static int serialnum = 0;
1195 lprintf(CTDL_DEBUG, "entering network_bounce()\n");
1197 if (msg == NULL) return;
1199 snprintf(bouncesource, sizeof bouncesource, "%s@%s", BOUNCESOURCE, config.c_nodename);
1202 * Give it a fresh message ID
1204 if (msg->cm_fields['I'] != NULL) {
1205 free(msg->cm_fields['I']);
1207 snprintf(buf, sizeof buf, "%ld.%04lx.%04x@%s",
1208 (long)time(NULL), (long)getpid(), ++serialnum, config.c_fqdn);
1209 msg->cm_fields['I'] = strdup(buf);
1212 * FIXME ... right now we're just sending a bounce; we really want to
1213 * include the text of the bounced message.
1215 if (msg->cm_fields['M'] != NULL) {
1216 free(msg->cm_fields['M']);
1218 msg->cm_fields['M'] = strdup(reason);
1219 msg->cm_format_type = 0;
1222 * Turn the message around
1224 if (msg->cm_fields['R'] == NULL) {
1225 free(msg->cm_fields['R']);
1228 if (msg->cm_fields['D'] == NULL) {
1229 free(msg->cm_fields['D']);
1232 snprintf(recipient, sizeof recipient, "%s@%s",
1233 msg->cm_fields['A'], msg->cm_fields['N']);
1235 if (msg->cm_fields['A'] == NULL) {
1236 free(msg->cm_fields['A']);
1239 if (msg->cm_fields['N'] == NULL) {
1240 free(msg->cm_fields['N']);
1243 msg->cm_fields['A'] = strdup(BOUNCESOURCE);
1244 msg->cm_fields['N'] = strdup(config.c_nodename);
1247 /* prepend our node to the path */
1248 if (msg->cm_fields['P'] != NULL) {
1249 oldpath = msg->cm_fields['P'];
1250 msg->cm_fields['P'] = NULL;
1253 oldpath = strdup("unknown_user");
1255 size = strlen(oldpath) + SIZ;
1256 msg->cm_fields['P'] = malloc(size);
1257 snprintf(msg->cm_fields['P'], size, "%s!%s", config.c_nodename, oldpath);
1260 /* Now submit the message */
1261 valid = validate_recipients(recipient);
1262 if (valid != NULL) if (valid->num_error > 0) {
1266 if ( (valid == NULL) || (!strcasecmp(recipient, bouncesource)) ) {
1267 strcpy(force_room, config.c_aideroom);
1270 strcpy(force_room, "");
1272 if ( (valid == NULL) && (strlen(force_room) == 0) ) {
1273 strcpy(force_room, config.c_aideroom);
1275 CtdlSubmitMsg(msg, valid, force_room);
1278 if (valid != NULL) free(valid);
1279 CtdlFreeMessage(msg);
1280 lprintf(CTDL_DEBUG, "leaving network_bounce()\n");
1287 * Process a buffer containing a single message from a single file
1288 * from the inbound queue
1290 void network_process_buffer(char *buffer, long size) {
1291 struct CtdlMessage *msg;
1294 struct recptypes *recp = NULL;
1295 char target_room[ROOMNAMELEN];
1296 struct ser_ret sermsg;
1297 char *oldpath = NULL;
1301 unsigned char firstbyte;
1302 unsigned char lastbyte;
1304 /* Validate just a little bit. First byte should be FF and
1305 * last byte should be 00.
1307 memcpy(&firstbyte, &buffer[0], 1);
1308 memcpy(&lastbyte, &buffer[size-1], 1);
1309 if ( (firstbyte != 255) || (lastbyte != 0) ) {
1310 lprintf(CTDL_ERR, "Corrupt message! Ignoring.\n");
1314 /* Set default target room to trash */
1315 strcpy(target_room, TWITROOM);
1317 /* Load the message into memory */
1318 msg = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
1319 memset(msg, 0, sizeof(struct CtdlMessage));
1320 msg->cm_magic = CTDLMESSAGE_MAGIC;
1321 msg->cm_anon_type = buffer[1];
1322 msg->cm_format_type = buffer[2];
1324 for (pos = 3; pos < size; ++pos) {
1325 field = buffer[pos];
1326 msg->cm_fields[field] = strdup(&buffer[pos+1]);
1327 pos = pos + strlen(&buffer[(int)pos]);
1330 /* Check for message routing */
1331 if (msg->cm_fields['D'] != NULL) {
1332 if (strcasecmp(msg->cm_fields['D'], config.c_nodename)) {
1334 /* route the message */
1335 strcpy(nexthop, "");
1336 if (is_valid_node(nexthop, NULL,
1337 msg->cm_fields['D']) == 0) {
1339 /* prepend our node to the path */
1340 if (msg->cm_fields['P'] != NULL) {
1341 oldpath = msg->cm_fields['P'];
1342 msg->cm_fields['P'] = NULL;
1345 oldpath = strdup("unknown_user");
1347 size = strlen(oldpath) + SIZ;
1348 msg->cm_fields['P'] = malloc(size);
1349 snprintf(msg->cm_fields['P'], size, "%s!%s",
1350 config.c_nodename, oldpath);
1353 /* serialize the message */
1354 serialize_message(&sermsg, msg);
1357 if (strlen(nexthop) == 0) {
1358 strcpy(nexthop, msg->cm_fields['D']);
1360 snprintf(filename, sizeof filename,
1361 "./network/spoolout/%s", nexthop);
1362 fp = fopen(filename, "ab");
1369 CtdlFreeMessage(msg);
1373 else { /* invalid destination node name */
1376 "A message you sent could not be delivered due to an invalid destination node"
1377 " name. Please check the address and try sending the message again.\n");
1386 * Check to see if we already have a copy of this message, and
1387 * abort its processing if so. (We used to post a warning to Aide>
1388 * every time this happened, but the network is now so densely
1389 * connected that it's inevitable.)
1391 if (network_usetable(msg) != 0) {
1395 /* Learn network topology from the path */
1396 if ((msg->cm_fields['N'] != NULL) && (msg->cm_fields['P'] != NULL)) {
1397 network_learn_topology(msg->cm_fields['N'],
1398 msg->cm_fields['P']);
1401 /* Is the sending node giving us a very persuasive suggestion about
1402 * which room this message should be saved in? If so, go with that.
1404 if (msg->cm_fields['C'] != NULL) {
1405 safestrncpy(target_room,
1406 msg->cm_fields['C'],
1407 sizeof target_room);
1410 /* Otherwise, does it have a recipient? If so, validate it... */
1411 else if (msg->cm_fields['R'] != NULL) {
1412 recp = validate_recipients(msg->cm_fields['R']);
1413 if (recp != NULL) if (recp->num_error > 0) {
1415 "A message you sent could not be delivered due to an invalid address.\n"
1416 "Please check the address and try sending the message again.\n");
1421 strcpy(target_room, ""); /* no target room if mail */
1424 /* Our last shot at finding a home for this message is to see if
1425 * it has the O field (Originating room) set.
1427 else if (msg->cm_fields['O'] != NULL) {
1428 safestrncpy(target_room,
1429 msg->cm_fields['O'],
1430 sizeof target_room);
1433 /* Strip out fields that are only relevant during transit */
1434 if (msg->cm_fields['D'] != NULL) {
1435 free(msg->cm_fields['D']);
1436 msg->cm_fields['D'] = NULL;
1438 if (msg->cm_fields['C'] != NULL) {
1439 free(msg->cm_fields['C']);
1440 msg->cm_fields['C'] = NULL;
1443 /* save the message into a room */
1444 if (PerformNetprocHooks(msg, target_room) == 0) {
1445 msg->cm_flags = CM_SKIP_HOOKS;
1446 CtdlSubmitMsg(msg, recp, target_room);
1448 CtdlFreeMessage(msg);
1454 * Process a single message from a single file from the inbound queue
1456 void network_process_message(FILE *fp, long msgstart, long msgend) {
1461 hold_pos = ftell(fp);
1462 size = msgend - msgstart + 1;
1463 buffer = malloc(size);
1464 if (buffer != NULL) {
1465 fseek(fp, msgstart, SEEK_SET);
1466 fread(buffer, size, 1, fp);
1467 network_process_buffer(buffer, size);
1471 fseek(fp, hold_pos, SEEK_SET);
1476 * Process a single file from the inbound queue
1478 void network_process_file(char *filename) {
1480 long msgstart = (-1L);
1481 long msgend = (-1L);
1486 fp = fopen(filename, "rb");
1488 lprintf(CTDL_CRIT, "Error opening %s: %s\n",
1489 filename, strerror(errno));
1493 lprintf(CTDL_INFO, "network: processing <%s>\n", filename);
1495 /* Look for messages in the data stream and break them out */
1496 while (ch = getc(fp), ch >= 0) {
1499 if (msgstart >= 0L) {
1500 msgend = msgcur - 1;
1501 network_process_message(fp, msgstart, msgend);
1509 msgend = msgcur - 1;
1510 if (msgstart >= 0L) {
1511 network_process_message(fp, msgstart, msgend);
1520 * Process anything in the inbound queue
1522 void network_do_spoolin(void) {
1527 dp = opendir("./network/spoolin");
1528 if (dp == NULL) return;
1530 while (d = readdir(dp), d != NULL) {
1531 snprintf(filename, sizeof filename,
1532 "./network/spoolin/%s", d->d_name);
1533 network_process_file(filename);
1542 * Delete any files in the outbound queue that were intended
1543 * to be sent to nodes which no longer exist.
1545 void network_purge_spoolout(void) {
1552 dp = opendir("./network/spoolout");
1553 if (dp == NULL) return;
1555 while (d = readdir(dp), d != NULL) {
1556 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
1558 snprintf(filename, sizeof filename,
1559 "./network/spoolout/%s", d->d_name);
1561 strcpy(nexthop, "");
1562 i = is_valid_node(nexthop, NULL, d->d_name);
1564 if ( (i != 0) || (strlen(nexthop) > 0) ) {
1576 * receive network spool from the remote system
1578 void receive_spool(int sock, char *remote_nodename) {
1580 long bytes_received;
1582 static char pbuf[IGNET_PACKET_SIZE];
1583 char tempfilename[PATH_MAX];
1587 strcpy(tempfilename, tmpnam(NULL));
1588 if (sock_puts(sock, "NDOP") < 0) return;
1589 if (sock_gets(sock, buf) < 0) return;
1590 lprintf(CTDL_DEBUG, "<%s\n", buf);
1591 if (buf[0] != '2') {
1594 download_len = extract_long(&buf[4], 0);
1596 bytes_received = 0L;
1597 fp = fopen(tempfilename, "w");
1599 lprintf(CTDL_CRIT, "cannot open download file locally: %s\n",
1604 while (bytes_received < download_len) {
1605 snprintf(buf, sizeof buf, "READ %ld|%ld",
1607 ((download_len - bytes_received > IGNET_PACKET_SIZE)
1608 ? IGNET_PACKET_SIZE : (download_len - bytes_received)));
1609 if (sock_puts(sock, buf) < 0) {
1611 unlink(tempfilename);
1614 if (sock_gets(sock, buf) < 0) {
1616 unlink(tempfilename);
1619 if (buf[0] == '6') {
1620 plen = extract_long(&buf[4], 0);
1621 if (sock_read(sock, pbuf, plen) < 0) {
1623 unlink(tempfilename);
1626 fwrite((char *) pbuf, plen, 1, fp);
1627 bytes_received = bytes_received + plen;
1632 if (sock_puts(sock, "CLOS") < 0) {
1633 unlink(tempfilename);
1636 if (sock_gets(sock, buf) < 0) {
1637 unlink(tempfilename);
1640 if (download_len > 0)
1641 lprintf(CTDL_NOTICE, "Received %ld octets from <%s>",
1642 download_len, remote_nodename);
1643 lprintf(CTDL_DEBUG, "%s", buf);
1644 snprintf(buf, sizeof buf, "mv %s ./network/spoolin/%s.%ld",
1645 tempfilename, remote_nodename, (long) getpid());
1652 * transmit network spool to the remote system
1654 void transmit_spool(int sock, char *remote_nodename)
1659 long bytes_to_write, thisblock, bytes_written;
1663 if (sock_puts(sock, "NUOP") < 0) return;
1664 if (sock_gets(sock, buf) < 0) return;
1665 lprintf(CTDL_DEBUG, "<%s\n", buf);
1666 if (buf[0] != '2') {
1670 snprintf(sfname, sizeof sfname, "./network/spoolout/%s", remote_nodename);
1671 fd = open(sfname, O_RDONLY);
1673 if (errno != ENOENT) {
1674 lprintf(CTDL_CRIT, "cannot open upload file locally: %s\n",
1680 while (plen = (long) read(fd, pbuf, IGNET_PACKET_SIZE), plen > 0L) {
1681 bytes_to_write = plen;
1682 while (bytes_to_write > 0L) {
1683 snprintf(buf, sizeof buf, "WRIT %ld", bytes_to_write);
1684 if (sock_puts(sock, buf) < 0) {
1688 if (sock_gets(sock, buf) < 0) {
1692 thisblock = atol(&buf[4]);
1693 if (buf[0] == '7') {
1694 if (sock_write(sock, pbuf,
1695 (int) thisblock) < 0) {
1699 bytes_to_write -= thisblock;
1700 bytes_written += thisblock;
1709 if (sock_puts(sock, "UCLS 1") < 0) return;
1710 if (sock_gets(sock, buf) < 0) return;
1711 lprintf(CTDL_NOTICE, "Sent %ld octets to <%s>",
1712 bytes_written, remote_nodename);
1713 lprintf(CTDL_DEBUG, "<%s\n", buf);
1714 if (buf[0] == '2') {
1722 * Poll one Citadel node (called by network_poll_other_citadel_nodes() below)
1724 void network_poll_node(char *node, char *secret, char *host, char *port) {
1728 if (network_talking_to(node, NTT_CHECK)) return;
1729 network_talking_to(node, NTT_ADD);
1730 lprintf(CTDL_NOTICE, "Connecting to <%s> at %s:%s\n", node, host, port);
1732 sock = sock_connect(host, port, "tcp");
1734 lprintf(CTDL_ERR, "Could not connect: %s\n", strerror(errno));
1735 network_talking_to(node, NTT_REMOVE);
1739 lprintf(CTDL_DEBUG, "Connected!\n");
1741 /* Read the server greeting */
1742 if (sock_gets(sock, buf) < 0) goto bail;
1743 lprintf(CTDL_DEBUG, ">%s\n", buf);
1745 /* Identify ourselves */
1746 snprintf(buf, sizeof buf, "NETP %s|%s", config.c_nodename, secret);
1747 lprintf(CTDL_DEBUG, "<%s\n", buf);
1748 if (sock_puts(sock, buf) <0) goto bail;
1749 if (sock_gets(sock, buf) < 0) goto bail;
1750 lprintf(CTDL_DEBUG, ">%s\n", buf);
1751 if (buf[0] != '2') goto bail;
1753 /* At this point we are authenticated. */
1754 receive_spool(sock, node);
1755 transmit_spool(sock, node);
1757 sock_puts(sock, "QUIT");
1758 bail: sock_close(sock);
1759 network_talking_to(node, NTT_REMOVE);
1765 * Poll other Citadel nodes and transfer inbound/outbound network data.
1766 * Set "full" to nonzero to force a poll of every node, or to zero to poll
1767 * only nodes to which we have data to send.
1769 void network_poll_other_citadel_nodes(int full_poll) {
1777 char spoolfile[256];
1779 if (working_ignetcfg == NULL) {
1780 lprintf(CTDL_DEBUG, "No nodes defined - not polling\n");
1784 /* Use the string tokenizer to grab one line at a time */
1785 for (i=0; i<num_tokens(working_ignetcfg, '\n'); ++i) {
1786 extract_token(linebuf, working_ignetcfg, i, '\n', sizeof linebuf);
1787 extract_token(node, linebuf, 0, '|', sizeof node);
1788 extract_token(secret, linebuf, 1, '|', sizeof secret);
1789 extract_token(host, linebuf, 2, '|', sizeof host);
1790 extract_token(port, linebuf, 3, '|', sizeof port);
1791 if ( (strlen(node) > 0) && (strlen(secret) > 0)
1792 && (strlen(host) > 0) && strlen(port) > 0) {
1795 snprintf(spoolfile, sizeof spoolfile,
1796 "./network/spoolout/%s", node);
1797 if (access(spoolfile, R_OK) == 0) {
1802 network_poll_node(node, secret, host, port);
1816 * network_do_queue()
1818 * Run through the rooms doing various types of network stuff.
1820 void network_do_queue(void) {
1821 static time_t last_run = 0L;
1822 struct RoomProcList *ptr;
1823 int full_processing = 1;
1826 * Run the full set of processing tasks no more frequently
1827 * than once every n seconds
1829 if ( (time(NULL) - last_run) < config.c_net_freq ) {
1830 full_processing = 0;
1834 * This is a simple concurrency check to make sure only one queue run
1835 * is done at a time. We could do this with a mutex, but since we
1836 * don't really require extremely fine granularity here, we'll do it
1837 * with a static variable instead.
1839 if (doing_queue) return;
1842 /* Load the IGnet Configuration into memory */
1843 load_working_ignetcfg();
1846 * Poll other Citadel nodes. Maybe. If "full_processing" is set
1847 * then we poll everyone. Otherwise we only poll nodes we have stuff
1850 network_poll_other_citadel_nodes(full_processing);
1853 * Load the network map and filter list into memory.
1856 filterlist = load_filter_list();
1859 * Go ahead and run the queue
1861 if (full_processing) {
1862 lprintf(CTDL_DEBUG, "network: loading outbound queue\n");
1863 ForEachRoom(network_queue_room, NULL);
1865 lprintf(CTDL_DEBUG, "network: running outbound queue\n");
1866 while (rplist != NULL) {
1867 network_spoolout_room(rplist->name);
1869 rplist = rplist->next;
1874 lprintf(CTDL_DEBUG, "network: processing inbound queue\n");
1875 network_do_spoolin();
1877 /* Save the network map back to disk */
1878 write_network_map();
1880 /* Free the filter list in memory */
1881 free_filter_list(filterlist);
1884 network_purge_spoolout();
1886 lprintf(CTDL_DEBUG, "network: queue run completed\n");
1888 if (full_processing) {
1889 last_run = time(NULL);
1897 * cmd_netp() - authenticate to the server as another Citadel node polling
1898 * for network traffic
1900 void cmd_netp(char *cmdbuf)
1910 extract_token(node, cmdbuf, 0, '|', sizeof node);
1911 extract_token(pass, cmdbuf, 1, '|', sizeof pass);
1914 lprintf(CTDL_WARNING, "Network node <%s> refused - spooling", node);
1915 cprintf("%d spooling - try again in a few minutes\n",
1916 ERROR + RESOURCE_BUSY);
1920 /* load the IGnet Configuration to check node validity */
1921 load_working_ignetcfg();
1922 v = is_valid_node(nexthop, secret, node);
1925 lprintf(CTDL_WARNING, "Unknown node <%s>\n", node);
1926 cprintf("%d authentication failed\n",
1927 ERROR + PASSWORD_REQUIRED);
1931 if (strcasecmp(pass, secret)) {
1932 lprintf(CTDL_WARNING, "Bad password for network node <%s>", node);
1933 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
1937 if (network_talking_to(node, NTT_CHECK)) {
1938 lprintf(CTDL_WARNING, "Duplicate session for network node <%s>", node);
1939 cprintf("%d Already talking to %s right now\n", ERROR + RESOURCE_BUSY, node);
1943 safestrncpy(CC->net_node, node, sizeof CC->net_node);
1944 network_talking_to(node, NTT_ADD);
1945 lprintf(CTDL_NOTICE, "Network node <%s> logged in\n", CC->net_node);
1946 cprintf("%d authenticated as network node '%s'\n", CIT_OK,
1953 * Module entry point
1955 char *serv_network_init(void)
1957 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
1958 CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");
1959 CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");
1960 CtdlRegisterProtoHook(cmd_nsyn, "NSYN", "Synchronize room to node");
1961 CtdlRegisterSessionHook(network_do_queue, EVT_TIMER);