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 */
35 #include <sys/types.h>
37 #if TIME_WITH_SYS_TIME
38 # include <sys/time.h>
42 # include <sys/time.h>
53 #include "sysdep_decls.h"
54 #include "citserver.h"
57 #include "serv_extensions.h"
64 #include "internet_addressing.h"
65 #include "serv_network.h"
66 #include "clientsocket.h"
73 /* Nonzero while we are doing outbound network processing */
74 static int doing_queue = 0;
77 * When we do network processing, it's accomplished in two passes; one to
78 * gather a list of rooms and one to actually do them. It's ok that rplist
79 * is global; this process *only* runs as part of the housekeeping loop and
80 * therefore only one will run at a time.
82 struct RoomProcList *rplist = NULL;
85 * We build a map of network nodes during processing.
87 struct NetMap *the_netmap = NULL;
88 int netmap_changed = 0;
89 char *working_ignetcfg = NULL;
92 * Load or refresh the Citadel network (IGnet) configuration for this node.
94 void load_working_ignetcfg(void) {
98 cfg = CtdlGetSysConfig(IGNETCFG);
103 oldcfg = working_ignetcfg;
104 working_ignetcfg = cfg;
105 if (oldcfg != NULL) {
115 * Keep track of what messages to reject
117 struct FilterList *load_filter_list(void) {
118 char *serialized_list = NULL;
121 struct FilterList *newlist = NULL;
122 struct FilterList *nptr;
124 serialized_list = CtdlGetSysConfig(FILTERLIST);
125 if (serialized_list == NULL) return(NULL); /* if null, no entries */
127 /* Use the string tokenizer to grab one line at a time */
128 for (i=0; i<num_tokens(serialized_list, '\n'); ++i) {
129 extract_token(buf, serialized_list, i, '\n', sizeof buf);
130 nptr = (struct FilterList *) malloc(sizeof(struct FilterList));
131 extract_token(nptr->fl_user, buf, 0, '|', sizeof nptr->fl_user);
132 striplt(nptr->fl_user);
133 extract_token(nptr->fl_room, buf, 1, '|', sizeof nptr->fl_room);
134 striplt(nptr->fl_room);
135 extract_token(nptr->fl_node, buf, 2, '|', sizeof nptr->fl_node);
136 striplt(nptr->fl_node);
138 /* Cowardly refuse to add an any/any/any entry that would
139 * end up filtering every single message.
141 if (strlen(nptr->fl_user) + strlen(nptr->fl_room)
142 + strlen(nptr->fl_node) == 0) {
146 nptr->next = newlist;
151 free(serialized_list);
156 void free_filter_list(struct FilterList *fl) {
157 if (fl == NULL) return;
158 free_filter_list(fl->next);
165 * Check the use table. This is a list of messages which have recently
166 * arrived on the system. It is maintained and queried to prevent the same
167 * message from being entered into the database multiple times if it happens
168 * to arrive multiple times by accident.
170 int network_usetable(struct CtdlMessage *msg) {
173 struct cdbdata *cdbut;
176 /* Bail out if we can't generate a message ID */
180 if (msg->cm_fields['I'] == NULL) {
183 if (strlen(msg->cm_fields['I']) == 0) {
187 /* Generate the message ID */
188 strcpy(msgid, msg->cm_fields['I']);
189 if (haschar(msgid, '@') == 0) {
191 if (msg->cm_fields['N'] != NULL) {
192 strcat(msgid, msg->cm_fields['N']);
199 cdbut = cdb_fetch(CDB_USETABLE, msgid, strlen(msgid));
205 /* If we got to this point, it's unique: add it. */
206 strcpy(ut.ut_msgid, msgid);
207 ut.ut_timestamp = time(NULL);
208 cdb_store(CDB_USETABLE, msgid, strlen(msgid),
209 &ut, sizeof(struct UseTable) );
215 * Read the network map from its configuration file into memory.
217 void read_network_map(void) {
218 char *serialized_map = NULL;
221 struct NetMap *nmptr;
223 serialized_map = CtdlGetSysConfig(IGNETMAP);
224 if (serialized_map == NULL) return; /* if null, no entries */
226 /* Use the string tokenizer to grab one line at a time */
227 for (i=0; i<num_tokens(serialized_map, '\n'); ++i) {
228 extract_token(buf, serialized_map, i, '\n', sizeof buf);
229 nmptr = (struct NetMap *) malloc(sizeof(struct NetMap));
230 extract_token(nmptr->nodename, buf, 0, '|', sizeof nmptr->nodename);
231 nmptr->lastcontact = extract_long(buf, 1);
232 extract_token(nmptr->nexthop, buf, 2, '|', sizeof nmptr->nexthop);
233 nmptr->next = the_netmap;
237 free(serialized_map);
243 * Write the network map from memory back to the configuration file.
245 void write_network_map(void) {
246 char *serialized_map = NULL;
247 struct NetMap *nmptr;
250 if (netmap_changed) {
251 serialized_map = strdup("");
253 if (the_netmap != NULL) {
254 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
255 serialized_map = realloc(serialized_map,
256 (strlen(serialized_map)+SIZ) );
257 if (strlen(nmptr->nodename) > 0) {
258 snprintf(&serialized_map[strlen(serialized_map)],
262 (long)nmptr->lastcontact,
268 CtdlPutSysConfig(IGNETMAP, serialized_map);
269 free(serialized_map);
272 /* Now free the list */
273 while (the_netmap != NULL) {
274 nmptr = the_netmap->next;
284 * Check the network map and determine whether the supplied node name is
285 * valid. If it is not a neighbor node, supply the name of a neighbor node
286 * which is the next hop. If it *is* a neighbor node, we also fill in the
289 int is_valid_node(char *nexthop, char *secret, char *node) {
294 struct NetMap *nmptr;
301 * First try the neighbor nodes
303 if (working_ignetcfg == NULL) {
304 lprintf(CTDL_ERR, "working_ignetcfg is NULL!\n");
305 if (nexthop != NULL) {
312 if (nexthop != NULL) {
316 /* Use the string tokenizer to grab one line at a time */
317 for (i=0; i<num_tokens(working_ignetcfg, '\n'); ++i) {
318 extract_token(linebuf, working_ignetcfg, i, '\n', sizeof linebuf);
319 extract_token(buf, linebuf, 0, '|', sizeof buf);
320 if (!strcasecmp(buf, node)) {
321 if (nexthop != NULL) {
324 if (secret != NULL) {
325 extract_token(secret, linebuf, 1, '|', 256);
332 return(retval); /* yup, it's a direct neighbor */
336 * If we get to this point we have to see if we know the next hop
338 if (the_netmap != NULL) {
339 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
340 if (!strcasecmp(nmptr->nodename, node)) {
341 if (nexthop != NULL) {
342 strcpy(nexthop, nmptr->nexthop);
350 * If we get to this point, the supplied node name is bogus.
352 lprintf(CTDL_ERR, "Invalid node name <%s>\n", node);
360 void cmd_gnet(char *argbuf) {
365 if (CtdlAccessCheck(ac_room_aide)) return;
366 assoc_file_name(filename, sizeof filename, &CC->room, "netconfigs");
367 cprintf("%d Network settings for room #%ld <%s>\n",
369 CC->room.QRnumber, CC->room.QRname);
371 fp = fopen(filename, "r");
373 while (fgets(buf, sizeof buf, fp) != NULL) {
374 buf[strlen(buf)-1] = 0;
375 cprintf("%s\n", buf);
384 void cmd_snet(char *argbuf) {
385 char tempfilename[SIZ];
392 if (CtdlAccessCheck(ac_room_aide)) return;
393 safestrncpy(tempfilename, tmpnam(NULL), sizeof tempfilename);
394 assoc_file_name(filename, sizeof filename, &CC->room, "netconfigs");
396 fp = fopen(tempfilename, "w");
398 cprintf("%d Cannot open %s: %s\n",
399 ERROR + INTERNAL_ERROR,
404 cprintf("%d %s\n", SEND_LISTING, tempfilename);
405 while (client_getln(buf, sizeof buf), strcmp(buf, "000")) {
406 fprintf(fp, "%s\n", buf);
410 /* Now copy the temp file to its permanent location
411 * (We use /bin/mv instead of link() because they may be on
412 * different filesystems)
415 snprintf(buf, sizeof buf, "/bin/mv %s %s", tempfilename, filename);
416 begin_critical_section(S_NETCONFIGS);
418 end_critical_section(S_NETCONFIGS);
423 * Spools out one message from the list.
425 void network_spool_msg(long msgnum, void *userdata) {
426 struct SpoolControl *sc;
428 char *newpath = NULL;
429 size_t instr_len = SIZ;
430 struct CtdlMessage *msg = NULL;
431 struct namelist *nptr;
432 struct maplist *mptr;
433 struct ser_ret sermsg;
439 int delete_after_send = 0; /* Set to 1 to delete after spooling */
440 int ok_to_participate = 0;
441 struct recptypes *valid;
443 sc = (struct SpoolControl *)userdata;
446 * Process mailing list recipients
449 if (sc->listrecps != NULL) {
450 /* Fetch the message. We're going to need to modify it
451 * for each recipient in order to satisfy foreign mailers, etc.
453 msg = CtdlFetchMessage(msgnum, 1);
456 /* Prepend "[List name]" to the subject */
457 if (msg->cm_fields['U'] == NULL) {
458 msg->cm_fields['U'] = strdup("(no subject)");
460 snprintf(buf, sizeof buf, "[%s] %s", CC->room.QRname, msg->cm_fields['U']);
461 free(msg->cm_fields['U']);
462 msg->cm_fields['U'] = strdup(buf);
464 /* For each recipient... */
465 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
467 if (msg->cm_fields['R'] == NULL) {
468 free(msg->cm_fields['R']);
470 msg->cm_fields['R'] = strdup(nptr->name);
472 valid = validate_recipients(nptr->name);
473 CtdlSubmitMsg(msg, valid, NULL, NULL, "");
477 CtdlFreeMessage(msg);
482 * Process digest recipients
484 if ((sc->digestrecps != NULL) && (sc->digestfp != NULL)) {
485 msg = CtdlFetchMessage(msgnum, 1);
487 fprintf(sc->digestfp, " -----------------------------------"
488 "------------------------------------"
490 fprintf(sc->digestfp, "From: ");
491 if (msg->cm_fields['A'] != NULL) {
492 fprintf(sc->digestfp, "%s ", msg->cm_fields['A']);
494 if (msg->cm_fields['F'] != NULL) {
495 fprintf(sc->digestfp, "<%s> ", msg->cm_fields['F']);
497 else if (msg->cm_fields['N'] != NULL) {
498 fprintf(sc->digestfp, "@%s ", msg->cm_fields['N']);
500 fprintf(sc->digestfp, "\n");
501 if (msg->cm_fields['U'] != NULL) {
502 fprintf(sc->digestfp, "Subject: %s\n", msg->cm_fields['U']);
505 CC->redirect_buffer = malloc(SIZ);
506 CC->redirect_len = 0;
507 CC->redirect_alloc = SIZ;
509 safestrncpy(CC->preferred_formats, "text/plain", sizeof CC->preferred_formats);
510 CtdlOutputPreLoadedMsg(msg, 0L, MT_CITADEL, HEADERS_NONE, 0, 0);
512 striplt(CC->redirect_buffer);
513 fprintf(sc->digestfp, "\n%s\n", CC->redirect_buffer);
515 free(CC->redirect_buffer);
516 CC->redirect_buffer = NULL;
517 CC->redirect_len = 0;
518 CC->redirect_alloc = 0;
520 sc->num_msgs_spooled += 1;
526 * Process client-side list participations for this room
529 if (sc->participates != NULL) {
530 msg = CtdlFetchMessage(msgnum, 1);
533 /* Only send messages which originated on our own Citadel
534 * network, otherwise we'll end up sending the remote
535 * mailing list's messages back to it, which is rude...
537 ok_to_participate = 0;
538 if (msg->cm_fields['N'] != NULL) {
539 if (!strcasecmp(msg->cm_fields['N'], config.c_nodename)) {
540 ok_to_participate = 1;
542 if (is_valid_node(NULL, NULL, msg->cm_fields['N']) == 0) {
543 ok_to_participate = 1;
546 if (ok_to_participate) {
547 if (msg->cm_fields['F'] != NULL) {
548 free(msg->cm_fields['F']);
550 msg->cm_fields['F'] = malloc(SIZ);
551 /* Replace the Internet email address of the actual
552 * author with the email address of the room itself,
553 * so the remote listserv doesn't reject us.
554 * FIXME ... I want to be able to pick any address
556 snprintf(msg->cm_fields['F'], SIZ,
557 "room_%s@%s", CC->room.QRname,
559 for (i=0; i<strlen(msg->cm_fields['F']); ++i) {
560 if (isspace(msg->cm_fields['F'][i])) {
561 msg->cm_fields['F'][i] = '_';
566 * Figure out how big a buffer we need to allocate
568 for (nptr = sc->participates; nptr != NULL; nptr = nptr->next) {
570 if (msg->cm_fields['R'] == NULL) {
571 free(msg->cm_fields['R']);
573 msg->cm_fields['R'] = strdup(nptr->name);
575 valid = validate_recipients(nptr->name);
576 CtdlSubmitMsg(msg, valid, NULL, NULL, "");
581 CtdlFreeMessage(msg);
586 * Process IGnet push shares
588 if (sc->ignet_push_shares != NULL) {
590 msg = CtdlFetchMessage(msgnum, 1);
594 /* Prepend our node name to the Path field whenever
595 * sending a message to another IGnet node
597 if (msg->cm_fields['P'] == NULL) {
598 msg->cm_fields['P'] = strdup("username");
600 newpath_len = strlen(msg->cm_fields['P']) +
601 strlen(config.c_nodename) + 2;
602 newpath = malloc(newpath_len);
603 snprintf(newpath, newpath_len, "%s!%s",
604 config.c_nodename, msg->cm_fields['P']);
605 free(msg->cm_fields['P']);
606 msg->cm_fields['P'] = newpath;
609 * Determine if this message is set to be deleted
610 * after sending out on the network
612 if (msg->cm_fields['S'] != NULL) {
613 if (!strcasecmp(msg->cm_fields['S'],
615 delete_after_send = 1;
619 /* Now send it to every node */
620 for (mptr = sc->ignet_push_shares; mptr != NULL;
625 /* Check for valid node name */
626 if (is_valid_node(NULL, NULL, mptr->remote_nodename) != 0) {
627 lprintf(CTDL_ERR, "Invalid node <%s>\n",
628 mptr->remote_nodename);
632 /* Check for split horizon */
633 lprintf(CTDL_DEBUG, "Path is %s\n", msg->cm_fields['P']);
634 bang = num_tokens(msg->cm_fields['P'], '!');
635 if (bang > 1) for (i=0; i<(bang-1); ++i) {
636 extract_token(buf, msg->cm_fields['P'],
638 if (!strcasecmp(buf, mptr->remote_nodename)) {
643 /* Send the message */
647 * Force the message to appear in the correct room
648 * on the far end by setting the C field correctly
650 if (msg->cm_fields['C'] != NULL) {
651 free(msg->cm_fields['C']);
653 if (strlen(mptr->remote_roomname) > 0) {
654 msg->cm_fields['C'] = strdup(mptr->remote_roomname);
657 msg->cm_fields['C'] = strdup(CC->room.QRname);
660 /* serialize it for transmission */
661 serialize_message(&sermsg, msg);
663 /* write it to the spool file */
664 snprintf(filename, sizeof filename,
665 #ifndef HAVE_SPOOL_DIR
669 #endif /* HAVE_SPOOL_DIR */
670 "/network/spoolout/%s",
671 mptr->remote_nodename);
672 lprintf(CTDL_DEBUG, "Appending to %s\n", filename);
673 fp = fopen(filename, "ab");
680 lprintf(CTDL_ERR, "%s: %s\n", filename, strerror(errno));
683 /* free the serialized version */
687 CtdlFreeMessage(msg);
691 /* update lastsent */
692 sc->lastsent = msgnum;
694 /* Delete this message if delete-after-send is set */
695 if (delete_after_send) {
696 CtdlDeleteMessages(CC->room.QRname, msgnum, "", 0);
703 * Deliver digest messages
705 void network_deliver_digest(struct SpoolControl *sc) {
708 struct CtdlMessage *msg;
712 size_t instr_len = SIZ;
713 struct CtdlMessage *imsg;
714 struct namelist *nptr;
716 if (sc->num_msgs_spooled < 1) {
717 fclose(sc->digestfp);
722 msg = malloc(sizeof(struct CtdlMessage));
723 memset(msg, 0, sizeof(struct CtdlMessage));
724 msg->cm_magic = CTDLMESSAGE_MAGIC;
725 msg->cm_format_type = FMT_RFC822;
726 msg->cm_anon_type = MES_NORMAL;
728 sprintf(buf, "%ld", time(NULL));
729 msg->cm_fields['T'] = strdup(buf);
730 msg->cm_fields['A'] = strdup(CC->room.QRname);
731 snprintf(buf, sizeof buf, "[%s]", CC->room.QRname);
732 msg->cm_fields['U'] = strdup(buf);
733 sprintf(buf, "room_%s@%s", CC->room.QRname, config.c_fqdn);
734 for (i=0; i<strlen(buf); ++i) {
735 if (isspace(buf[i])) buf[i]='_';
736 buf[i] = tolower(buf[i]);
738 msg->cm_fields['F'] = strdup(buf);
740 fseek(sc->digestfp, 0L, SEEK_END);
741 msglen = ftell(sc->digestfp);
743 msg->cm_fields['M'] = malloc(msglen + 1);
744 fseek(sc->digestfp, 0L, SEEK_SET);
745 fread(msg->cm_fields['M'], (size_t)msglen, 1, sc->digestfp);
746 msg->cm_fields['M'][msglen] = 0;
748 fclose(sc->digestfp);
751 msgnum = CtdlSubmitMsg(msg, NULL, NULL, NULL, SMTP_SPOOLOUT_ROOM);
752 CtdlFreeMessage(msg);
754 /* Now generate the delivery instructions */
757 * Figure out how big a buffer we need to allocate
759 for (nptr = sc->digestrecps; nptr != NULL; nptr = nptr->next) {
760 instr_len = instr_len + strlen(nptr->name);
766 lprintf(CTDL_DEBUG, "Generating delivery instructions\n");
767 instr = malloc(instr_len);
769 lprintf(CTDL_EMERG, "Cannot allocate %ld bytes for instr...\n",
773 snprintf(instr, instr_len,
774 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
775 "bounceto|postmaster@%s\n" ,
776 SPOOLMIME, msgnum, (long)time(NULL), config.c_fqdn );
778 /* Generate delivery instructions for each recipient */
779 for (nptr = sc->digestrecps; nptr != NULL; nptr = nptr->next) {
780 size_t tmp = strlen(instr);
781 snprintf(&instr[tmp], instr_len - tmp,
782 "remote|%s|0||\n", nptr->name);
786 * Generate a message from the instructions
788 imsg = malloc(sizeof(struct CtdlMessage));
789 memset(imsg, 0, sizeof(struct CtdlMessage));
790 imsg->cm_magic = CTDLMESSAGE_MAGIC;
791 imsg->cm_anon_type = MES_NORMAL;
792 imsg->cm_format_type = FMT_RFC822;
793 imsg->cm_fields['A'] = strdup("Citadel");
794 imsg->cm_fields['M'] = instr;
796 /* Save delivery instructions in spoolout room */
797 CtdlSubmitMsg(imsg, NULL, NULL, NULL, SMTP_SPOOLOUT_ROOM);
798 CtdlFreeMessage(imsg);
803 * Batch up and send all outbound traffic from the current room
805 void network_spoolout_room(char *room_to_spool) {
810 char roomname[ROOMNAMELEN];
813 struct SpoolControl sc;
814 struct namelist *nptr = NULL;
815 struct maplist *mptr = NULL;
818 int skipthisline = 0;
821 if (getroom(&CC->room, room_to_spool) != 0) {
822 lprintf(CTDL_CRIT, "ERROR: cannot load <%s>\n", room_to_spool);
826 memset(&sc, 0, sizeof(struct SpoolControl));
827 assoc_file_name(filename, sizeof filename, &CC->room, "netconfigs");
829 begin_critical_section(S_NETCONFIGS);
831 fp = fopen(filename, "r");
833 end_critical_section(S_NETCONFIGS);
837 lprintf(CTDL_INFO, "Networking started for <%s>\n", CC->room.QRname);
839 while (fgets(buf, sizeof buf, fp) != NULL) {
840 buf[strlen(buf)-1] = 0;
842 extract_token(instr, buf, 0, '|', sizeof instr);
843 if (!strcasecmp(instr, "lastsent")) {
844 sc.lastsent = extract_long(buf, 1);
846 else if (!strcasecmp(instr, "listrecp")) {
847 nptr = (struct namelist *)
848 malloc(sizeof(struct namelist));
849 nptr->next = sc.listrecps;
850 extract_token(nptr->name, buf, 1, '|', sizeof nptr->name);
853 else if (!strcasecmp(instr, "participate")) {
854 nptr = (struct namelist *)
855 malloc(sizeof(struct namelist));
856 nptr->next = sc.participates;
857 extract_token(nptr->name, buf, 1, '|', sizeof nptr->name);
858 sc.participates = nptr;
860 else if (!strcasecmp(instr, "digestrecp")) {
861 nptr = (struct namelist *)
862 malloc(sizeof(struct namelist));
863 nptr->next = sc.digestrecps;
864 extract_token(nptr->name, buf, 1, '|', sizeof nptr->name);
865 sc.digestrecps = nptr;
867 else if (!strcasecmp(instr, "ignet_push_share")) {
868 /* by checking each node's validity, we automatically
869 * purge nodes which do not exist from room network
870 * configurations at this time.
872 extract_token(nodename, buf, 1, '|', sizeof nodename);
873 extract_token(roomname, buf, 2, '|', sizeof roomname);
874 strcpy(nexthop, "xxx");
875 if (is_valid_node(nexthop, NULL, nodename) == 0) {
876 if (strlen(nexthop) == 0) {
877 mptr = (struct maplist *)
878 malloc(sizeof(struct maplist));
879 mptr->next = sc.ignet_push_shares;
880 strcpy(mptr->remote_nodename, nodename);
881 strcpy(mptr->remote_roomname, roomname);
882 sc.ignet_push_shares = mptr;
887 /* Preserve 'other' lines ... *unless* they happen to
888 * be subscribe/unsubscribe pendings with expired
892 if (!strncasecmp(buf, "subpending|", 11)) {
893 if (time(NULL) - extract_long(buf, 4) > EXP) {
897 if (!strncasecmp(buf, "unsubpending|", 13)) {
898 if (time(NULL) - extract_long(buf, 3) > EXP) {
903 if (skipthisline == 0) {
904 linesize = strlen(buf);
905 sc.misc = realloc(sc.misc,
906 (miscsize + linesize + 2) );
907 sprintf(&sc.misc[miscsize], "%s\n", buf);
908 miscsize = miscsize + linesize + 1;
916 /* If there are digest recipients, we have to build a digest */
917 if (sc.digestrecps != NULL) {
918 sc.digestfp = tmpfile();
919 fprintf(sc.digestfp, "Content-type: text/plain\n\n");
922 /* Do something useful */
923 CtdlForEachMessage(MSGS_GT, sc.lastsent, NULL, NULL,
924 network_spool_msg, &sc);
926 /* If we wrote a digest, deliver it and then close it */
927 snprintf(buf, sizeof buf, "room_%s@%s",
928 CC->room.QRname, config.c_fqdn);
929 for (i=0; i<strlen(buf); ++i) {
930 buf[i] = tolower(buf[i]);
931 if (isspace(buf[i])) buf[i] = '_';
933 if (sc.digestfp != NULL) {
934 fprintf(sc.digestfp, " -----------------------------------"
935 "------------------------------------"
937 "You are subscribed to the '%s' "
939 "To post to the list: %s\n",
942 network_deliver_digest(&sc); /* deliver and close */
945 /* Now rewrite the config file */
946 fp = fopen(filename, "w");
948 lprintf(CTDL_CRIT, "ERROR: cannot open %s: %s\n",
949 filename, strerror(errno));
952 fprintf(fp, "lastsent|%ld\n", sc.lastsent);
954 /* Write out the listrecps while freeing from memory at the
955 * same time. Am I clever or what? :)
957 while (sc.listrecps != NULL) {
958 fprintf(fp, "listrecp|%s\n", sc.listrecps->name);
959 nptr = sc.listrecps->next;
963 /* Do the same for digestrecps */
964 while (sc.digestrecps != NULL) {
965 fprintf(fp, "digestrecp|%s\n", sc.digestrecps->name);
966 nptr = sc.digestrecps->next;
967 free(sc.digestrecps);
968 sc.digestrecps = nptr;
970 /* Do the same for participates */
971 while (sc.participates != NULL) {
972 fprintf(fp, "participate|%s\n", sc.participates->name);
973 nptr = sc.participates->next;
974 free(sc.participates);
975 sc.participates = nptr;
977 while (sc.ignet_push_shares != NULL) {
978 /* by checking each node's validity, we automatically
979 * purge nodes which do not exist from room network
980 * configurations at this time.
982 if (is_valid_node(NULL, NULL, sc.ignet_push_shares->remote_nodename) == 0) {
984 fprintf(fp, "ignet_push_share|%s",
985 sc.ignet_push_shares->remote_nodename);
986 if (strlen(sc.ignet_push_shares->remote_roomname) > 0) {
987 fprintf(fp, "|%s", sc.ignet_push_shares->remote_roomname);
990 mptr = sc.ignet_push_shares->next;
991 free(sc.ignet_push_shares);
992 sc.ignet_push_shares = mptr;
994 if (sc.misc != NULL) {
995 fwrite(sc.misc, strlen(sc.misc), 1, fp);
1001 end_critical_section(S_NETCONFIGS);
1007 * Send the *entire* contents of the current room to one specific network node,
1008 * ignoring anything we know about which messages have already undergone
1009 * network processing. This can be used to bring a new node into sync.
1011 int network_sync_to(char *target_node) {
1012 struct SpoolControl sc;
1013 int num_spooled = 0;
1022 /* Grab the configuration line we're looking for */
1023 assoc_file_name(filename, sizeof filename, &CC->room, "netconfigs");
1024 begin_critical_section(S_NETCONFIGS);
1025 fp = fopen(filename, "r");
1027 end_critical_section(S_NETCONFIGS);
1030 while (fgets(buf, sizeof buf, fp) != NULL) {
1031 buf[strlen(buf)-1] = 0;
1032 extract_token(sc_type, buf, 0, '|', sizeof sc_type);
1033 extract_token(sc_node, buf, 1, '|', sizeof sc_node);
1034 extract_token(sc_room, buf, 2, '|', sizeof sc_room);
1035 if ( (!strcasecmp(sc_type, "ignet_push_share"))
1036 && (!strcasecmp(sc_node, target_node)) ) {
1039 /* Concise syntax because we don't need a full linked-list */
1040 memset(&sc, 0, sizeof(struct SpoolControl));
1041 sc.ignet_push_shares = (struct maplist *)
1042 malloc(sizeof(struct maplist));
1043 sc.ignet_push_shares->next = NULL;
1044 safestrncpy(sc.ignet_push_shares->remote_nodename,
1046 sizeof sc.ignet_push_shares->remote_nodename);
1047 safestrncpy(sc.ignet_push_shares->remote_roomname,
1049 sizeof sc.ignet_push_shares->remote_roomname);
1053 end_critical_section(S_NETCONFIGS);
1055 if (!found_node) return(-1);
1057 /* Send ALL messages */
1058 num_spooled = CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL,
1059 network_spool_msg, &sc);
1061 /* Concise cleanup because we know there's only one node in the sc */
1062 free(sc.ignet_push_shares);
1064 lprintf(CTDL_NOTICE, "Synchronized %d messages to <%s>\n",
1065 num_spooled, target_node);
1066 return(num_spooled);
1071 * Implements the NSYN command
1073 void cmd_nsyn(char *argbuf) {
1075 char target_node[256];
1077 if (CtdlAccessCheck(ac_aide)) return;
1079 extract_token(target_node, argbuf, 0, '|', sizeof target_node);
1080 num_spooled = network_sync_to(target_node);
1081 if (num_spooled >= 0) {
1082 cprintf("%d Spooled %d messages.\n", CIT_OK, num_spooled);
1085 cprintf("%d No such room/node share exists.\n",
1086 ERROR + ROOM_NOT_FOUND);
1093 * Batch up and send all outbound traffic from the current room
1095 void network_queue_room(struct ctdlroom *qrbuf, void *data) {
1096 struct RoomProcList *ptr;
1098 ptr = (struct RoomProcList *) malloc(sizeof (struct RoomProcList));
1099 if (ptr == NULL) return;
1101 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
1108 * Learn topology from path fields
1110 void network_learn_topology(char *node, char *path) {
1112 struct NetMap *nmptr;
1114 strcpy(nexthop, "");
1116 if (num_tokens(path, '!') < 3) return;
1117 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
1118 if (!strcasecmp(nmptr->nodename, node)) {
1119 extract_token(nmptr->nexthop, path, 0, '!', sizeof nmptr->nexthop);
1120 nmptr->lastcontact = time(NULL);
1126 /* If we got here then it's not in the map, so add it. */
1127 nmptr = (struct NetMap *) malloc(sizeof (struct NetMap));
1128 strcpy(nmptr->nodename, node);
1129 nmptr->lastcontact = time(NULL);
1130 extract_token(nmptr->nexthop, path, 0, '!', sizeof nmptr->nexthop);
1131 nmptr->next = the_netmap;
1140 * Bounce a message back to the sender
1142 void network_bounce(struct CtdlMessage *msg, char *reason) {
1143 char *oldpath = NULL;
1145 char bouncesource[SIZ];
1146 char recipient[SIZ];
1147 struct recptypes *valid = NULL;
1148 char force_room[ROOMNAMELEN];
1149 static int serialnum = 0;
1152 lprintf(CTDL_DEBUG, "entering network_bounce()\n");
1154 if (msg == NULL) return;
1156 snprintf(bouncesource, sizeof bouncesource, "%s@%s", BOUNCESOURCE, config.c_nodename);
1159 * Give it a fresh message ID
1161 if (msg->cm_fields['I'] != NULL) {
1162 free(msg->cm_fields['I']);
1164 snprintf(buf, sizeof buf, "%ld.%04lx.%04x@%s",
1165 (long)time(NULL), (long)getpid(), ++serialnum, config.c_fqdn);
1166 msg->cm_fields['I'] = strdup(buf);
1169 * FIXME ... right now we're just sending a bounce; we really want to
1170 * include the text of the bounced message.
1172 if (msg->cm_fields['M'] != NULL) {
1173 free(msg->cm_fields['M']);
1175 msg->cm_fields['M'] = strdup(reason);
1176 msg->cm_format_type = 0;
1179 * Turn the message around
1181 if (msg->cm_fields['R'] == NULL) {
1182 free(msg->cm_fields['R']);
1185 if (msg->cm_fields['D'] == NULL) {
1186 free(msg->cm_fields['D']);
1189 snprintf(recipient, sizeof recipient, "%s@%s",
1190 msg->cm_fields['A'], msg->cm_fields['N']);
1192 if (msg->cm_fields['A'] == NULL) {
1193 free(msg->cm_fields['A']);
1196 if (msg->cm_fields['N'] == NULL) {
1197 free(msg->cm_fields['N']);
1200 if (msg->cm_fields['U'] == NULL) {
1201 free(msg->cm_fields['U']);
1204 msg->cm_fields['A'] = strdup(BOUNCESOURCE);
1205 msg->cm_fields['N'] = strdup(config.c_nodename);
1206 msg->cm_fields['U'] = strdup("Delivery Status Notification (Failure)");
1208 /* prepend our node to the path */
1209 if (msg->cm_fields['P'] != NULL) {
1210 oldpath = msg->cm_fields['P'];
1211 msg->cm_fields['P'] = NULL;
1214 oldpath = strdup("unknown_user");
1216 size = strlen(oldpath) + SIZ;
1217 msg->cm_fields['P'] = malloc(size);
1218 snprintf(msg->cm_fields['P'], size, "%s!%s", config.c_nodename, oldpath);
1221 /* Now submit the message */
1222 valid = validate_recipients(recipient);
1223 if (valid != NULL) if (valid->num_error > 0) {
1227 if ( (valid == NULL) || (!strcasecmp(recipient, bouncesource)) ) {
1228 strcpy(force_room, config.c_aideroom);
1231 strcpy(force_room, "");
1233 if ( (valid == NULL) && (strlen(force_room) == 0) ) {
1234 strcpy(force_room, config.c_aideroom);
1236 CtdlSubmitMsg(msg, valid, NULL, NULL, force_room);
1239 if (valid != NULL) free(valid);
1240 CtdlFreeMessage(msg);
1241 lprintf(CTDL_DEBUG, "leaving network_bounce()\n");
1248 * Process a buffer containing a single message from a single file
1249 * from the inbound queue
1251 void network_process_buffer(char *buffer, long size) {
1252 struct CtdlMessage *msg;
1255 struct recptypes *recp = NULL;
1256 char target_room[ROOMNAMELEN];
1257 struct ser_ret sermsg;
1258 char *oldpath = NULL;
1262 unsigned char firstbyte;
1263 unsigned char lastbyte;
1265 /* Validate just a little bit. First byte should be FF and
1266 * last byte should be 00.
1268 memcpy(&firstbyte, &buffer[0], 1);
1269 memcpy(&lastbyte, &buffer[size-1], 1);
1270 if ( (firstbyte != 255) || (lastbyte != 0) ) {
1271 lprintf(CTDL_ERR, "Corrupt message! Ignoring.\n");
1275 /* Set default target room to trash */
1276 strcpy(target_room, TWITROOM);
1278 /* Load the message into memory */
1279 msg = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
1280 memset(msg, 0, sizeof(struct CtdlMessage));
1281 msg->cm_magic = CTDLMESSAGE_MAGIC;
1282 msg->cm_anon_type = buffer[1];
1283 msg->cm_format_type = buffer[2];
1285 for (pos = 3; pos < size; ++pos) {
1286 field = buffer[pos];
1287 msg->cm_fields[field] = strdup(&buffer[pos+1]);
1288 pos = pos + strlen(&buffer[(int)pos]);
1291 /* Check for message routing */
1292 if (msg->cm_fields['D'] != NULL) {
1293 if (strcasecmp(msg->cm_fields['D'], config.c_nodename)) {
1295 /* route the message */
1296 strcpy(nexthop, "");
1297 if (is_valid_node(nexthop, NULL,
1298 msg->cm_fields['D']) == 0) {
1300 /* prepend our node to the path */
1301 if (msg->cm_fields['P'] != NULL) {
1302 oldpath = msg->cm_fields['P'];
1303 msg->cm_fields['P'] = NULL;
1306 oldpath = strdup("unknown_user");
1308 size = strlen(oldpath) + SIZ;
1309 msg->cm_fields['P'] = malloc(size);
1310 snprintf(msg->cm_fields['P'], size, "%s!%s",
1311 config.c_nodename, oldpath);
1314 /* serialize the message */
1315 serialize_message(&sermsg, msg);
1318 if (strlen(nexthop) == 0) {
1319 strcpy(nexthop, msg->cm_fields['D']);
1321 snprintf(filename, sizeof filename,
1322 #ifndef HAVE_SPOOL_DIR
1326 #endif /* HAVE_SPOOL_DIR */
1327 "/network/spoolout/%s", nexthop);
1328 lprintf(CTDL_DEBUG, "Appending to %s\n", filename);
1329 fp = fopen(filename, "ab");
1336 lprintf(CTDL_ERR, "%s: %s\n", filename, strerror(errno));
1339 CtdlFreeMessage(msg);
1343 else { /* invalid destination node name */
1346 "A message you sent could not be delivered due to an invalid destination node"
1347 " name. Please check the address and try sending the message again.\n");
1356 * Check to see if we already have a copy of this message, and
1357 * abort its processing if so. (We used to post a warning to Aide>
1358 * every time this happened, but the network is now so densely
1359 * connected that it's inevitable.)
1361 if (network_usetable(msg) != 0) {
1365 /* Learn network topology from the path */
1366 if ((msg->cm_fields['N'] != NULL) && (msg->cm_fields['P'] != NULL)) {
1367 network_learn_topology(msg->cm_fields['N'],
1368 msg->cm_fields['P']);
1371 /* Is the sending node giving us a very persuasive suggestion about
1372 * which room this message should be saved in? If so, go with that.
1374 if (msg->cm_fields['C'] != NULL) {
1375 safestrncpy(target_room,
1376 msg->cm_fields['C'],
1377 sizeof target_room);
1380 /* Otherwise, does it have a recipient? If so, validate it... */
1381 else if (msg->cm_fields['R'] != NULL) {
1382 recp = validate_recipients(msg->cm_fields['R']);
1383 if (recp != NULL) if (recp->num_error > 0) {
1385 "A message you sent could not be delivered due to an invalid address.\n"
1386 "Please check the address and try sending the message again.\n");
1391 strcpy(target_room, ""); /* no target room if mail */
1394 /* Our last shot at finding a home for this message is to see if
1395 * it has the O field (Originating room) set.
1397 else if (msg->cm_fields['O'] != NULL) {
1398 safestrncpy(target_room,
1399 msg->cm_fields['O'],
1400 sizeof target_room);
1403 /* Strip out fields that are only relevant during transit */
1404 if (msg->cm_fields['D'] != NULL) {
1405 free(msg->cm_fields['D']);
1406 msg->cm_fields['D'] = NULL;
1408 if (msg->cm_fields['C'] != NULL) {
1409 free(msg->cm_fields['C']);
1410 msg->cm_fields['C'] = NULL;
1413 /* save the message into a room */
1414 if (PerformNetprocHooks(msg, target_room) == 0) {
1415 msg->cm_flags = CM_SKIP_HOOKS;
1416 CtdlSubmitMsg(msg, recp, NULL, NULL, target_room);
1418 CtdlFreeMessage(msg);
1424 * Process a single message from a single file from the inbound queue
1426 void network_process_message(FILE *fp, long msgstart, long msgend) {
1431 hold_pos = ftell(fp);
1432 size = msgend - msgstart + 1;
1433 buffer = malloc(size);
1434 if (buffer != NULL) {
1435 fseek(fp, msgstart, SEEK_SET);
1436 fread(buffer, size, 1, fp);
1437 network_process_buffer(buffer, size);
1441 fseek(fp, hold_pos, SEEK_SET);
1446 * Process a single file from the inbound queue
1448 void network_process_file(char *filename) {
1450 long msgstart = (-1L);
1451 long msgend = (-1L);
1456 fp = fopen(filename, "rb");
1458 lprintf(CTDL_CRIT, "Error opening %s: %s\n",
1459 filename, strerror(errno));
1463 lprintf(CTDL_INFO, "network: processing <%s>\n", filename);
1465 /* Look for messages in the data stream and break them out */
1466 while (ch = getc(fp), ch >= 0) {
1469 if (msgstart >= 0L) {
1470 msgend = msgcur - 1;
1471 network_process_message(fp, msgstart, msgend);
1479 msgend = msgcur - 1;
1480 if (msgstart >= 0L) {
1481 network_process_message(fp, msgstart, msgend);
1490 * Process anything in the inbound queue
1492 void network_do_spoolin(void) {
1498 #ifndef HAVE_SPOOL_DIR
1502 #endif /* HAVE_SPOOL_DIR */
1503 "/network/spoolin");
1504 if (dp == NULL) return;
1506 while (d = readdir(dp), d != NULL) {
1507 if ((strcmp(d->d_name, ".")) && (strcmp(d->d_name, ".."))) {
1508 snprintf(filename, sizeof filename,
1509 #ifndef HAVE_SPOOL_DIR
1513 #endif /* HAVE_SPOOL_DIR */
1514 "/network/spoolin/%s", d->d_name);
1515 network_process_file(filename);
1523 * Delete any files in the outbound queue that were intended
1524 * to be sent to nodes which no longer exist.
1526 void network_purge_spoolout(void) {
1534 #ifndef HAVE_SPOOL_DIR
1538 #endif /* HAVE_SPOOL_DIR */
1539 "/network/spoolout");
1540 if (dp == NULL) return;
1542 while (d = readdir(dp), d != NULL) {
1543 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
1545 snprintf(filename, sizeof filename,
1546 #ifndef HAVE_SPOOL_DIR
1550 #endif /* HAVE_SPOOL_DIR */
1551 "/network/spoolout/%s", d->d_name);
1553 strcpy(nexthop, "");
1554 i = is_valid_node(nexthop, NULL, d->d_name);
1556 if ( (i != 0) || (strlen(nexthop) > 0) ) {
1567 * receive network spool from the remote system
1569 void receive_spool(int sock, char *remote_nodename) {
1571 long bytes_received;
1573 static char pbuf[IGNET_PACKET_SIZE];
1574 char tempfilename[PATH_MAX];
1578 strcpy(tempfilename, tmpnam(NULL));
1579 if (sock_puts(sock, "NDOP") < 0) return;
1580 if (sock_gets(sock, buf) < 0) return;
1581 lprintf(CTDL_DEBUG, "<%s\n", buf);
1582 if (buf[0] != '2') {
1585 download_len = extract_long(&buf[4], 0);
1587 bytes_received = 0L;
1588 fp = fopen(tempfilename, "w");
1590 lprintf(CTDL_CRIT, "cannot open download file locally: %s\n",
1595 while (bytes_received < download_len) {
1596 snprintf(buf, sizeof buf, "READ %ld|%ld",
1598 ((download_len - bytes_received > IGNET_PACKET_SIZE)
1599 ? IGNET_PACKET_SIZE : (download_len - bytes_received)));
1600 if (sock_puts(sock, buf) < 0) {
1602 unlink(tempfilename);
1605 if (sock_gets(sock, buf) < 0) {
1607 unlink(tempfilename);
1610 if (buf[0] == '6') {
1611 plen = extract_long(&buf[4], 0);
1612 if (sock_read(sock, pbuf, plen) < 0) {
1614 unlink(tempfilename);
1617 fwrite((char *) pbuf, plen, 1, fp);
1618 bytes_received = bytes_received + plen;
1623 if (sock_puts(sock, "CLOS") < 0) {
1624 unlink(tempfilename);
1627 if (sock_gets(sock, buf) < 0) {
1628 unlink(tempfilename);
1631 if (download_len > 0)
1632 lprintf(CTDL_NOTICE, "Received %ld octets from <%s>",
1633 download_len, remote_nodename);
1634 lprintf(CTDL_DEBUG, "%s", buf);
1635 /* TODO: make move inline. forking is verry expensive. */
1636 snprintf(buf, sizeof buf, "mv %s "
1637 #ifndef HAVE_SPOOL_DIR
1641 #endif /* HAVE_SPOOL_DIR */
1642 "/network/spoolin/%s.%ld",
1643 tempfilename, remote_nodename, (long) getpid());
1650 * transmit network spool to the remote system
1652 void transmit_spool(int sock, char *remote_nodename)
1657 long bytes_to_write, thisblock, bytes_written;
1661 if (sock_puts(sock, "NUOP") < 0) return;
1662 if (sock_gets(sock, buf) < 0) return;
1663 lprintf(CTDL_DEBUG, "<%s\n", buf);
1664 if (buf[0] != '2') {
1668 snprintf(sfname, sizeof sfname,
1669 #ifndef HAVE_SPOOL_DIR
1673 #endif /* HAVE_SPOOL_DIR */
1674 "/network/spoolout/%s", remote_nodename);
1675 fd = open(sfname, O_RDONLY);
1677 if (errno != ENOENT) {
1678 lprintf(CTDL_CRIT, "cannot open upload file locally: %s\n",
1684 while (plen = (long) read(fd, pbuf, IGNET_PACKET_SIZE), plen > 0L) {
1685 bytes_to_write = plen;
1686 while (bytes_to_write > 0L) {
1687 snprintf(buf, sizeof buf, "WRIT %ld", bytes_to_write);
1688 if (sock_puts(sock, buf) < 0) {
1692 if (sock_gets(sock, buf) < 0) {
1696 thisblock = atol(&buf[4]);
1697 if (buf[0] == '7') {
1698 if (sock_write(sock, pbuf,
1699 (int) thisblock) < 0) {
1703 bytes_to_write -= thisblock;
1704 bytes_written += thisblock;
1713 if (sock_puts(sock, "UCLS 1") < 0) return;
1714 if (sock_gets(sock, buf) < 0) return;
1715 lprintf(CTDL_NOTICE, "Sent %ld octets to <%s>\n",
1716 bytes_written, remote_nodename);
1717 lprintf(CTDL_DEBUG, "<%s\n", buf);
1718 if (buf[0] == '2') {
1719 lprintf(CTDL_DEBUG, "Removing <%s>\n", sfname);
1727 * Poll one Citadel node (called by network_poll_other_citadel_nodes() below)
1729 void network_poll_node(char *node, char *secret, char *host, char *port) {
1733 if (network_talking_to(node, NTT_CHECK)) return;
1734 network_talking_to(node, NTT_ADD);
1735 lprintf(CTDL_NOTICE, "Connecting to <%s> at %s:%s\n", node, host, port);
1737 sock = sock_connect(host, port, "tcp");
1739 lprintf(CTDL_ERR, "Could not connect: %s\n", strerror(errno));
1740 network_talking_to(node, NTT_REMOVE);
1744 lprintf(CTDL_DEBUG, "Connected!\n");
1746 /* Read the server greeting */
1747 if (sock_gets(sock, buf) < 0) goto bail;
1748 lprintf(CTDL_DEBUG, ">%s\n", buf);
1750 /* Identify ourselves */
1751 snprintf(buf, sizeof buf, "NETP %s|%s", config.c_nodename, secret);
1752 lprintf(CTDL_DEBUG, "<%s\n", buf);
1753 if (sock_puts(sock, buf) <0) goto bail;
1754 if (sock_gets(sock, buf) < 0) goto bail;
1755 lprintf(CTDL_DEBUG, ">%s\n", buf);
1756 if (buf[0] != '2') goto bail;
1758 /* At this point we are authenticated. */
1759 receive_spool(sock, node);
1760 transmit_spool(sock, node);
1762 sock_puts(sock, "QUIT");
1763 bail: sock_close(sock);
1764 network_talking_to(node, NTT_REMOVE);
1770 * Poll other Citadel nodes and transfer inbound/outbound network data.
1771 * Set "full" to nonzero to force a poll of every node, or to zero to poll
1772 * only nodes to which we have data to send.
1774 void network_poll_other_citadel_nodes(int full_poll) {
1782 char spoolfile[256];
1784 if (working_ignetcfg == NULL) {
1785 lprintf(CTDL_DEBUG, "No nodes defined - not polling\n");
1789 /* Use the string tokenizer to grab one line at a time */
1790 for (i=0; i<num_tokens(working_ignetcfg, '\n'); ++i) {
1791 extract_token(linebuf, working_ignetcfg, i, '\n', sizeof linebuf);
1792 extract_token(node, linebuf, 0, '|', sizeof node);
1793 extract_token(secret, linebuf, 1, '|', sizeof secret);
1794 extract_token(host, linebuf, 2, '|', sizeof host);
1795 extract_token(port, linebuf, 3, '|', sizeof port);
1796 if ( (strlen(node) > 0) && (strlen(secret) > 0)
1797 && (strlen(host) > 0) && strlen(port) > 0) {
1800 snprintf(spoolfile, sizeof spoolfile,
1801 #ifndef HAVE_SPOOL_DIR
1806 "/network/spoolout/%s", node);
1807 if (access(spoolfile, R_OK) == 0) {
1812 network_poll_node(node, secret, host, port);
1823 * It's ok if these directories already exist. Just fail silently.
1825 void create_spool_dirs(void) {
1826 #ifndef HAVE_SPOOL_DIR
1827 mkdir("./network", 0700);
1828 mkdir("./network/systems", 0700);
1829 mkdir("./network/spoolin", 0700);
1830 mkdir("./network/spoolout", 0700);
1832 mkdir(SPOOL_DIR "/network", 0700);
1833 mkdir(SPOOL_DIR "/network/systems", 0700);
1834 mkdir(SPOOL_DIR "/network/spoolin", 0700);
1835 mkdir(SPOOL_DIR "/network/spoolout", 0700);
1836 #endif /* HAVE_SPOOL_DIR */
1844 * network_do_queue()
1846 * Run through the rooms doing various types of network stuff.
1848 void network_do_queue(void) {
1849 static time_t last_run = 0L;
1850 struct RoomProcList *ptr;
1851 int full_processing = 1;
1854 * Run the full set of processing tasks no more frequently
1855 * than once every n seconds
1857 if ( (time(NULL) - last_run) < config.c_net_freq ) {
1858 full_processing = 0;
1861 create_spool_dirs();
1864 * This is a simple concurrency check to make sure only one queue run
1865 * is done at a time. We could do this with a mutex, but since we
1866 * don't really require extremely fine granularity here, we'll do it
1867 * with a static variable instead.
1869 if (doing_queue) return;
1872 /* Load the IGnet Configuration into memory */
1873 load_working_ignetcfg();
1876 * Poll other Citadel nodes. Maybe. If "full_processing" is set
1877 * then we poll everyone. Otherwise we only poll nodes we have stuff
1880 network_poll_other_citadel_nodes(full_processing);
1883 * Load the network map and filter list into memory.
1886 filterlist = load_filter_list();
1889 * Go ahead and run the queue
1891 if (full_processing) {
1892 lprintf(CTDL_DEBUG, "network: loading outbound queue\n");
1893 ForEachRoom(network_queue_room, NULL);
1895 lprintf(CTDL_DEBUG, "network: running outbound queue\n");
1896 while (rplist != NULL) {
1897 network_spoolout_room(rplist->name);
1899 rplist = rplist->next;
1904 lprintf(CTDL_DEBUG, "network: processing inbound queue\n");
1905 network_do_spoolin();
1907 /* Save the network map back to disk */
1908 write_network_map();
1910 /* Free the filter list in memory */
1911 free_filter_list(filterlist);
1914 network_purge_spoolout();
1916 lprintf(CTDL_DEBUG, "network: queue run completed\n");
1918 if (full_processing) {
1919 last_run = time(NULL);
1927 * cmd_netp() - authenticate to the server as another Citadel node polling
1928 * for network traffic
1930 void cmd_netp(char *cmdbuf)
1940 extract_token(node, cmdbuf, 0, '|', sizeof node);
1941 extract_token(pass, cmdbuf, 1, '|', sizeof pass);
1944 lprintf(CTDL_WARNING, "Network node <%s> refused - spooling", node);
1945 cprintf("%d spooling - try again in a few minutes\n",
1946 ERROR + RESOURCE_BUSY);
1950 /* load the IGnet Configuration to check node validity */
1951 load_working_ignetcfg();
1952 v = is_valid_node(nexthop, secret, node);
1955 lprintf(CTDL_WARNING, "Unknown node <%s>\n", node);
1956 cprintf("%d authentication failed\n",
1957 ERROR + PASSWORD_REQUIRED);
1961 if (strcasecmp(pass, secret)) {
1962 lprintf(CTDL_WARNING, "Bad password for network node <%s>", node);
1963 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
1967 if (network_talking_to(node, NTT_CHECK)) {
1968 lprintf(CTDL_WARNING, "Duplicate session for network node <%s>", node);
1969 cprintf("%d Already talking to %s right now\n", ERROR + RESOURCE_BUSY, node);
1973 safestrncpy(CC->net_node, node, sizeof CC->net_node);
1974 network_talking_to(node, NTT_ADD);
1975 lprintf(CTDL_NOTICE, "Network node <%s> logged in\n", CC->net_node);
1976 cprintf("%d authenticated as network node '%s'\n", CIT_OK,
1981 * Module entry point
1983 char *serv_network_init(void)
1985 create_spool_dirs();
1986 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
1987 CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");
1988 CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");
1989 CtdlRegisterProtoHook(cmd_nsyn, "NSYN", "Synchronize room to node");
1990 CtdlRegisterSessionHook(network_do_queue, EVT_TIMER);