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>
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"
68 #include "citadel_dirs.h"
74 /* Nonzero while we are doing network processing */
75 static int doing_queue = 0;
78 * When we do network processing, it's accomplished in two passes; one to
79 * gather a list of rooms and one to actually do them. It's ok that rplist
80 * is global; this process *only* runs as part of the housekeeping loop and
81 * therefore only one will run at a time.
83 struct RoomProcList *rplist = NULL;
86 * We build a map of network nodes during processing.
88 struct NetMap *the_netmap = NULL;
89 int netmap_changed = 0;
90 char *working_ignetcfg = NULL;
93 * Load or refresh the Citadel network (IGnet) configuration for this node.
95 void load_working_ignetcfg(void) {
99 cfg = CtdlGetSysConfig(IGNETCFG);
104 oldcfg = working_ignetcfg;
105 working_ignetcfg = cfg;
106 if (oldcfg != NULL) {
116 * Keep track of what messages to reject
118 struct FilterList *load_filter_list(void) {
119 char *serialized_list = NULL;
122 struct FilterList *newlist = NULL;
123 struct FilterList *nptr;
125 serialized_list = CtdlGetSysConfig(FILTERLIST);
126 if (serialized_list == NULL) return(NULL); /* if null, no entries */
128 /* Use the string tokenizer to grab one line at a time */
129 for (i=0; i<num_tokens(serialized_list, '\n'); ++i) {
130 extract_token(buf, serialized_list, i, '\n', sizeof buf);
131 nptr = (struct FilterList *) malloc(sizeof(struct FilterList));
132 extract_token(nptr->fl_user, buf, 0, '|', sizeof nptr->fl_user);
133 striplt(nptr->fl_user);
134 extract_token(nptr->fl_room, buf, 1, '|', sizeof nptr->fl_room);
135 striplt(nptr->fl_room);
136 extract_token(nptr->fl_node, buf, 2, '|', sizeof nptr->fl_node);
137 striplt(nptr->fl_node);
139 /* Cowardly refuse to add an any/any/any entry that would
140 * end up filtering every single message.
142 if (strlen(nptr->fl_user) + strlen(nptr->fl_room)
143 + strlen(nptr->fl_node) == 0) {
147 nptr->next = newlist;
152 free(serialized_list);
157 void free_filter_list(struct FilterList *fl) {
158 if (fl == NULL) return;
159 free_filter_list(fl->next);
166 * Check the use table. This is a list of messages which have recently
167 * arrived on the system. It is maintained and queried to prevent the same
168 * message from being entered into the database multiple times if it happens
169 * to arrive multiple times by accident.
171 int network_usetable(struct CtdlMessage *msg) {
174 struct cdbdata *cdbut;
177 /* Bail out if we can't generate a message ID */
181 if (msg->cm_fields['I'] == NULL) {
184 if (strlen(msg->cm_fields['I']) == 0) {
188 /* Generate the message ID */
189 strcpy(msgid, msg->cm_fields['I']);
190 if (haschar(msgid, '@') == 0) {
192 if (msg->cm_fields['N'] != NULL) {
193 strcat(msgid, msg->cm_fields['N']);
200 cdbut = cdb_fetch(CDB_USETABLE, msgid, strlen(msgid));
206 /* If we got to this point, it's unique: add it. */
207 strcpy(ut.ut_msgid, msgid);
208 ut.ut_timestamp = time(NULL);
209 cdb_store(CDB_USETABLE, msgid, strlen(msgid),
210 &ut, sizeof(struct UseTable) );
216 * Read the network map from its configuration file into memory.
218 void read_network_map(void) {
219 char *serialized_map = NULL;
222 struct NetMap *nmptr;
224 serialized_map = CtdlGetSysConfig(IGNETMAP);
225 if (serialized_map == NULL) return; /* if null, no entries */
227 /* Use the string tokenizer to grab one line at a time */
228 for (i=0; i<num_tokens(serialized_map, '\n'); ++i) {
229 extract_token(buf, serialized_map, i, '\n', sizeof buf);
230 nmptr = (struct NetMap *) malloc(sizeof(struct NetMap));
231 extract_token(nmptr->nodename, buf, 0, '|', sizeof nmptr->nodename);
232 nmptr->lastcontact = extract_long(buf, 1);
233 extract_token(nmptr->nexthop, buf, 2, '|', sizeof nmptr->nexthop);
234 nmptr->next = the_netmap;
238 free(serialized_map);
244 * Write the network map from memory back to the configuration file.
246 void write_network_map(void) {
247 char *serialized_map = NULL;
248 struct NetMap *nmptr;
251 if (netmap_changed) {
252 serialized_map = strdup("");
254 if (the_netmap != NULL) {
255 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
256 serialized_map = realloc(serialized_map,
257 (strlen(serialized_map)+SIZ) );
258 if (strlen(nmptr->nodename) > 0) {
259 snprintf(&serialized_map[strlen(serialized_map)],
263 (long)nmptr->lastcontact,
269 CtdlPutSysConfig(IGNETMAP, serialized_map);
270 free(serialized_map);
273 /* Now free the list */
274 while (the_netmap != NULL) {
275 nmptr = the_netmap->next;
285 * Check the network map and determine whether the supplied node name is
286 * valid. If it is not a neighbor node, supply the name of a neighbor node
287 * which is the next hop. If it *is* a neighbor node, we also fill in the
290 int is_valid_node(char *nexthop, char *secret, char *node) {
295 struct NetMap *nmptr;
302 * First try the neighbor nodes
304 if (working_ignetcfg == NULL) {
305 lprintf(CTDL_ERR, "working_ignetcfg is NULL!\n");
306 if (nexthop != NULL) {
313 if (nexthop != NULL) {
317 /* Use the string tokenizer to grab one line at a time */
318 for (i=0; i<num_tokens(working_ignetcfg, '\n'); ++i) {
319 extract_token(linebuf, working_ignetcfg, i, '\n', sizeof linebuf);
320 extract_token(buf, linebuf, 0, '|', sizeof buf);
321 if (!strcasecmp(buf, node)) {
322 if (nexthop != NULL) {
325 if (secret != NULL) {
326 extract_token(secret, linebuf, 1, '|', 256);
333 return(retval); /* yup, it's a direct neighbor */
337 * If we get to this point we have to see if we know the next hop
339 if (the_netmap != NULL) {
340 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
341 if (!strcasecmp(nmptr->nodename, node)) {
342 if (nexthop != NULL) {
343 strcpy(nexthop, nmptr->nexthop);
351 * If we get to this point, the supplied node name is bogus.
353 lprintf(CTDL_ERR, "Invalid node name <%s>\n", node);
361 void cmd_gnet(char *argbuf) {
366 if (CtdlAccessCheck(ac_room_aide)) return;
367 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
368 cprintf("%d Network settings for room #%ld <%s>\n",
370 CC->room.QRnumber, CC->room.QRname);
372 fp = fopen(filename, "r");
374 while (fgets(buf, sizeof buf, fp) != NULL) {
375 buf[strlen(buf)-1] = 0;
376 cprintf("%s\n", buf);
385 void cmd_snet(char *argbuf) {
386 char tempfilename[SIZ];
393 if (CtdlAccessCheck(ac_room_aide)) return;
394 CtdlMakeTempFileName(tempfilename, sizeof tempfilename);
395 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
397 fp = fopen(tempfilename, "w");
399 cprintf("%d Cannot open %s: %s\n",
400 ERROR + INTERNAL_ERROR,
405 cprintf("%d %s\n", SEND_LISTING, tempfilename);
406 while (client_getln(buf, sizeof buf), strcmp(buf, "000")) {
407 fprintf(fp, "%s\n", buf);
411 /* Now copy the temp file to its permanent location
412 * (We use /bin/mv instead of link() because they may be on
413 * different filesystems)
416 snprintf(buf, sizeof buf, "/bin/mv %s %s", tempfilename, filename);
417 begin_critical_section(S_NETCONFIGS);
419 end_critical_section(S_NETCONFIGS);
424 * Deliver digest messages
426 void network_deliver_digest(struct SpoolControl *sc) {
429 struct CtdlMessage *msg;
433 size_t instr_len = SIZ;
434 struct CtdlMessage *imsg;
435 struct namelist *nptr;
437 if (sc->num_msgs_spooled < 1) {
438 fclose(sc->digestfp);
443 msg = malloc(sizeof(struct CtdlMessage));
444 memset(msg, 0, sizeof(struct CtdlMessage));
445 msg->cm_magic = CTDLMESSAGE_MAGIC;
446 msg->cm_format_type = FMT_RFC822;
447 msg->cm_anon_type = MES_NORMAL;
449 sprintf(buf, "%ld", time(NULL));
450 msg->cm_fields['T'] = strdup(buf);
451 msg->cm_fields['A'] = strdup(CC->room.QRname);
452 snprintf(buf, sizeof buf, "[%s]", CC->room.QRname);
453 msg->cm_fields['U'] = strdup(buf);
454 sprintf(buf, "room_%s@%s", CC->room.QRname, config.c_fqdn);
455 for (i=0; i<strlen(buf); ++i) {
456 if (isspace(buf[i])) buf[i]='_';
457 buf[i] = tolower(buf[i]);
459 msg->cm_fields['F'] = strdup(buf);
460 msg->cm_fields['R'] = strdup(buf);
463 * Go fetch the contents of the digest
465 fseek(sc->digestfp, 0L, SEEK_END);
466 msglen = ftell(sc->digestfp);
468 msg->cm_fields['M'] = malloc(msglen + 1);
469 fseek(sc->digestfp, 0L, SEEK_SET);
470 fread(msg->cm_fields['M'], (size_t)msglen, 1, sc->digestfp);
471 msg->cm_fields['M'][msglen] = 0;
473 fclose(sc->digestfp);
476 msgnum = CtdlSubmitMsg(msg, NULL, SMTP_SPOOLOUT_ROOM);
477 CtdlFreeMessage(msg);
479 /* Now generate the delivery instructions */
482 * Figure out how big a buffer we need to allocate
484 for (nptr = sc->digestrecps; nptr != NULL; nptr = nptr->next) {
485 instr_len = instr_len + strlen(nptr->name) + 2;
491 lprintf(CTDL_DEBUG, "Generating delivery instructions\n");
492 instr = malloc(instr_len);
494 lprintf(CTDL_EMERG, "Cannot allocate %ld bytes for instr...\n",
498 snprintf(instr, instr_len,
499 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
500 "bounceto|postmaster@%s\n" ,
501 SPOOLMIME, msgnum, (long)time(NULL), config.c_fqdn );
503 /* Generate delivery instructions for each recipient */
504 for (nptr = sc->digestrecps; nptr != NULL; nptr = nptr->next) {
505 size_t tmp = strlen(instr);
506 snprintf(&instr[tmp], instr_len - tmp,
507 "remote|%s|0||\n", nptr->name);
511 * Generate a message from the instructions
513 imsg = malloc(sizeof(struct CtdlMessage));
514 memset(imsg, 0, sizeof(struct CtdlMessage));
515 imsg->cm_magic = CTDLMESSAGE_MAGIC;
516 imsg->cm_anon_type = MES_NORMAL;
517 imsg->cm_format_type = FMT_RFC822;
518 imsg->cm_fields['A'] = strdup("Citadel");
519 imsg->cm_fields['M'] = instr;
521 /* Save delivery instructions in spoolout room */
522 CtdlSubmitMsg(imsg, NULL, SMTP_SPOOLOUT_ROOM);
523 CtdlFreeMessage(imsg);
528 * Deliver list messages to everyone on the list ... efficiently
530 void network_deliver_list(struct CtdlMessage *msg, struct SpoolControl *sc) {
533 size_t instr_len = SIZ;
534 struct CtdlMessage *imsg;
535 struct namelist *nptr;
537 /* Don't do this if there were no recipients! */
538 if (sc->listrecps == NULL) return;
540 /* Save the message to disk... */
541 msgnum = CtdlSubmitMsg(msg, NULL, SMTP_SPOOLOUT_ROOM);
543 /* Now generate the delivery instructions */
546 * Figure out how big a buffer we need to allocate
548 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
549 instr_len = instr_len + strlen(nptr->name) + 2;
555 lprintf(CTDL_DEBUG, "Generating delivery instructions\n");
556 instr = malloc(instr_len);
558 lprintf(CTDL_EMERG, "Cannot allocate %ld bytes for instr...\n",
562 snprintf(instr, instr_len,
563 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
564 "bounceto|postmaster@%s\n" ,
565 SPOOLMIME, msgnum, (long)time(NULL), config.c_fqdn );
567 /* Generate delivery instructions for each recipient */
568 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
569 size_t tmp = strlen(instr);
570 snprintf(&instr[tmp], instr_len - tmp,
571 "remote|%s|0||\n", nptr->name);
575 * Generate a message from the instructions
577 imsg = malloc(sizeof(struct CtdlMessage));
578 memset(imsg, 0, sizeof(struct CtdlMessage));
579 imsg->cm_magic = CTDLMESSAGE_MAGIC;
580 imsg->cm_anon_type = MES_NORMAL;
581 imsg->cm_format_type = FMT_RFC822;
582 imsg->cm_fields['A'] = strdup("Citadel");
583 imsg->cm_fields['M'] = instr;
585 /* Save delivery instructions in spoolout room */
586 CtdlSubmitMsg(imsg, NULL, SMTP_SPOOLOUT_ROOM);
587 CtdlFreeMessage(imsg);
594 * Spools out one message from the list.
596 void network_spool_msg(long msgnum, void *userdata) {
597 struct SpoolControl *sc;
599 char *newpath = NULL;
600 size_t instr_len = SIZ;
601 struct CtdlMessage *msg = NULL;
602 struct namelist *nptr;
603 struct maplist *mptr;
604 struct ser_ret sermsg;
610 int delete_after_send = 0; /* Set to 1 to delete after spooling */
611 int ok_to_participate = 0;
612 struct recptypes *valid;
614 sc = (struct SpoolControl *)userdata;
617 * Process mailing list recipients
620 if (sc->listrecps != NULL) {
621 /* Fetch the message. We're going to need to modify it
622 * in order to insert the [list name] in it, etc.
624 msg = CtdlFetchMessage(msgnum, 1);
627 /* Prepend "[List name]" to the subject */
628 if (msg->cm_fields['U'] == NULL) {
629 msg->cm_fields['U'] = strdup("(no subject)");
631 snprintf(buf, sizeof buf, "[%s] %s", CC->room.QRname, msg->cm_fields['U']);
632 free(msg->cm_fields['U']);
633 msg->cm_fields['U'] = strdup(buf);
635 /* Set the recipient of the list message to the
636 * email address of the room itself.
637 * FIXME ... I want to be able to pick any address
639 if (msg->cm_fields['R'] != NULL) {
640 free(msg->cm_fields['R']);
642 msg->cm_fields['R'] = malloc(256);
643 snprintf(msg->cm_fields['R'], 256,
644 "room_%s@%s", CC->room.QRname,
646 for (i=0; i<strlen(msg->cm_fields['R']); ++i) {
647 if (isspace(msg->cm_fields['R'][i])) {
648 msg->cm_fields['R'][i] = '_';
652 /* Handle delivery */
653 network_deliver_list(msg, sc);
654 CtdlFreeMessage(msg);
659 * Process digest recipients
661 if ((sc->digestrecps != NULL) && (sc->digestfp != NULL)) {
662 msg = CtdlFetchMessage(msgnum, 1);
664 fprintf(sc->digestfp, " -----------------------------------"
665 "------------------------------------"
667 fprintf(sc->digestfp, "From: ");
668 if (msg->cm_fields['A'] != NULL) {
669 fprintf(sc->digestfp, "%s ", msg->cm_fields['A']);
671 if (msg->cm_fields['F'] != NULL) {
672 fprintf(sc->digestfp, "<%s> ", msg->cm_fields['F']);
674 else if (msg->cm_fields['N'] != NULL) {
675 fprintf(sc->digestfp, "@%s ", msg->cm_fields['N']);
677 fprintf(sc->digestfp, "\n");
678 if (msg->cm_fields['U'] != NULL) {
679 fprintf(sc->digestfp, "Subject: %s\n", msg->cm_fields['U']);
682 CC->redirect_buffer = malloc(SIZ);
683 CC->redirect_len = 0;
684 CC->redirect_alloc = SIZ;
686 safestrncpy(CC->preferred_formats, "text/plain", sizeof CC->preferred_formats);
687 CtdlOutputPreLoadedMsg(msg, MT_CITADEL, HEADERS_NONE, 0, 0);
689 striplt(CC->redirect_buffer);
690 fprintf(sc->digestfp, "\n%s\n", CC->redirect_buffer);
692 free(CC->redirect_buffer);
693 CC->redirect_buffer = NULL;
694 CC->redirect_len = 0;
695 CC->redirect_alloc = 0;
697 sc->num_msgs_spooled += 1;
703 * Process client-side list participations for this room
706 if (sc->participates != NULL) {
707 msg = CtdlFetchMessage(msgnum, 1);
710 /* Only send messages which originated on our own Citadel
711 * network, otherwise we'll end up sending the remote
712 * mailing list's messages back to it, which is rude...
714 ok_to_participate = 0;
715 if (msg->cm_fields['N'] != NULL) {
716 if (!strcasecmp(msg->cm_fields['N'], config.c_nodename)) {
717 ok_to_participate = 1;
719 if (is_valid_node(NULL, NULL, msg->cm_fields['N']) == 0) {
720 ok_to_participate = 1;
723 if (ok_to_participate) {
724 if (msg->cm_fields['F'] != NULL) {
725 free(msg->cm_fields['F']);
727 msg->cm_fields['F'] = malloc(SIZ);
728 /* Replace the Internet email address of the actual
729 * author with the email address of the room itself,
730 * so the remote listserv doesn't reject us.
731 * FIXME ... I want to be able to pick any address
733 snprintf(msg->cm_fields['F'], SIZ,
734 "room_%s@%s", CC->room.QRname,
736 for (i=0; i<strlen(msg->cm_fields['F']); ++i) {
737 if (isspace(msg->cm_fields['F'][i])) {
738 msg->cm_fields['F'][i] = '_';
743 * Figure out how big a buffer we need to allocate
745 for (nptr = sc->participates; nptr != NULL; nptr = nptr->next) {
747 if (msg->cm_fields['R'] == NULL) {
748 free(msg->cm_fields['R']);
750 msg->cm_fields['R'] = strdup(nptr->name);
752 valid = validate_recipients(nptr->name);
753 CtdlSubmitMsg(msg, valid, "");
758 CtdlFreeMessage(msg);
763 * Process IGnet push shares
765 msg = CtdlFetchMessage(msgnum, 1);
769 /* Prepend our node name to the Path field whenever
770 * sending a message to another IGnet node
772 if (msg->cm_fields['P'] == NULL) {
773 msg->cm_fields['P'] = strdup("username");
775 newpath_len = strlen(msg->cm_fields['P']) +
776 strlen(config.c_nodename) + 2;
777 newpath = malloc(newpath_len);
778 snprintf(newpath, newpath_len, "%s!%s",
779 config.c_nodename, msg->cm_fields['P']);
780 free(msg->cm_fields['P']);
781 msg->cm_fields['P'] = newpath;
784 * Determine if this message is set to be deleted
785 * after sending out on the network
787 if (msg->cm_fields['S'] != NULL) {
788 if (!strcasecmp(msg->cm_fields['S'], "CANCEL")) {
789 delete_after_send = 1;
793 /* Now send it to every node */
794 if (sc->ignet_push_shares != NULL)
795 for (mptr = sc->ignet_push_shares; mptr != NULL;
800 /* Check for valid node name */
801 if (is_valid_node(NULL, NULL, mptr->remote_nodename) != 0) {
802 lprintf(CTDL_ERR, "Invalid node <%s>\n",
803 mptr->remote_nodename);
807 /* Check for split horizon */
808 lprintf(CTDL_DEBUG, "Path is %s\n", msg->cm_fields['P']);
809 bang = num_tokens(msg->cm_fields['P'], '!');
810 if (bang > 1) for (i=0; i<(bang-1); ++i) {
811 extract_token(buf, msg->cm_fields['P'],
813 if (!strcasecmp(buf, mptr->remote_nodename)) {
818 /* Send the message */
822 * Force the message to appear in the correct room
823 * on the far end by setting the C field correctly
825 if (msg->cm_fields['C'] != NULL) {
826 free(msg->cm_fields['C']);
828 if (strlen(mptr->remote_roomname) > 0) {
829 msg->cm_fields['C'] = strdup(mptr->remote_roomname);
832 msg->cm_fields['C'] = strdup(CC->room.QRname);
835 /* serialize it for transmission */
836 serialize_message(&sermsg, msg);
837 if (sermsg.len > 0) {
839 /* write it to the spool file */
840 snprintf(filename, sizeof filename,"%s/%s",
842 mptr->remote_nodename);
843 lprintf(CTDL_DEBUG, "Appending to %s\n", filename);
844 fp = fopen(filename, "ab");
851 lprintf(CTDL_ERR, "%s: %s\n", filename, strerror(errno));
854 /* free the serialized version */
860 CtdlFreeMessage(msg);
863 /* update lastsent */
864 sc->lastsent = msgnum;
866 /* Delete this message if delete-after-send is set */
867 if (delete_after_send) {
868 CtdlDeleteMessages(CC->room.QRname, msgnum, "", 0);
875 * Batch up and send all outbound traffic from the current room
877 void network_spoolout_room(char *room_to_spool) {
882 char roomname[ROOMNAMELEN];
885 struct SpoolControl sc;
886 struct namelist *nptr = NULL;
887 struct maplist *mptr = NULL;
890 int skipthisline = 0;
893 if (getroom(&CC->room, room_to_spool) != 0) {
894 lprintf(CTDL_CRIT, "ERROR: cannot load <%s>\n", room_to_spool);
898 memset(&sc, 0, sizeof(struct SpoolControl));
899 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
901 begin_critical_section(S_NETCONFIGS);
903 fp = fopen(filename, "r");
905 end_critical_section(S_NETCONFIGS);
909 lprintf(CTDL_INFO, "Networking started for <%s>\n", CC->room.QRname);
911 while (fgets(buf, sizeof buf, fp) != NULL) {
912 buf[strlen(buf)-1] = 0;
914 extract_token(instr, buf, 0, '|', sizeof instr);
915 if (!strcasecmp(instr, "lastsent")) {
916 sc.lastsent = extract_long(buf, 1);
918 else if (!strcasecmp(instr, "listrecp")) {
919 nptr = (struct namelist *)
920 malloc(sizeof(struct namelist));
921 nptr->next = sc.listrecps;
922 extract_token(nptr->name, buf, 1, '|', sizeof nptr->name);
925 else if (!strcasecmp(instr, "participate")) {
926 nptr = (struct namelist *)
927 malloc(sizeof(struct namelist));
928 nptr->next = sc.participates;
929 extract_token(nptr->name, buf, 1, '|', sizeof nptr->name);
930 sc.participates = nptr;
932 else if (!strcasecmp(instr, "digestrecp")) {
933 nptr = (struct namelist *)
934 malloc(sizeof(struct namelist));
935 nptr->next = sc.digestrecps;
936 extract_token(nptr->name, buf, 1, '|', sizeof nptr->name);
937 sc.digestrecps = nptr;
939 else if (!strcasecmp(instr, "ignet_push_share")) {
940 /* by checking each node's validity, we automatically
941 * purge nodes which do not exist from room network
942 * configurations at this time.
944 extract_token(nodename, buf, 1, '|', sizeof nodename);
945 extract_token(roomname, buf, 2, '|', sizeof roomname);
946 strcpy(nexthop, "xxx");
947 if (is_valid_node(nexthop, NULL, nodename) == 0) {
948 if (strlen(nexthop) == 0) {
949 mptr = (struct maplist *)
950 malloc(sizeof(struct maplist));
951 mptr->next = sc.ignet_push_shares;
952 strcpy(mptr->remote_nodename, nodename);
953 strcpy(mptr->remote_roomname, roomname);
954 sc.ignet_push_shares = mptr;
959 /* Preserve 'other' lines ... *unless* they happen to
960 * be subscribe/unsubscribe pendings with expired
964 if (!strncasecmp(buf, "subpending|", 11)) {
965 if (time(NULL) - extract_long(buf, 4) > EXP) {
969 if (!strncasecmp(buf, "unsubpending|", 13)) {
970 if (time(NULL) - extract_long(buf, 3) > EXP) {
975 if (skipthisline == 0) {
976 linesize = strlen(buf);
977 sc.misc = realloc(sc.misc,
978 (miscsize + linesize + 2) );
979 sprintf(&sc.misc[miscsize], "%s\n", buf);
980 miscsize = miscsize + linesize + 1;
988 /* If there are digest recipients, we have to build a digest */
989 if (sc.digestrecps != NULL) {
990 sc.digestfp = tmpfile();
991 fprintf(sc.digestfp, "Content-type: text/plain\n\n");
994 /* Do something useful */
995 CtdlForEachMessage(MSGS_GT, sc.lastsent, NULL, NULL,
996 network_spool_msg, &sc);
998 /* If we wrote a digest, deliver it and then close it */
999 snprintf(buf, sizeof buf, "room_%s@%s",
1000 CC->room.QRname, config.c_fqdn);
1001 for (i=0; i<strlen(buf); ++i) {
1002 buf[i] = tolower(buf[i]);
1003 if (isspace(buf[i])) buf[i] = '_';
1005 if (sc.digestfp != NULL) {
1006 fprintf(sc.digestfp, " -----------------------------------"
1007 "------------------------------------"
1009 "You are subscribed to the '%s' "
1011 "To post to the list: %s\n",
1012 CC->room.QRname, buf
1014 network_deliver_digest(&sc); /* deliver and close */
1017 /* Now rewrite the config file */
1018 fp = fopen(filename, "w");
1020 lprintf(CTDL_CRIT, "ERROR: cannot open %s: %s\n",
1021 filename, strerror(errno));
1024 fprintf(fp, "lastsent|%ld\n", sc.lastsent);
1026 /* Write out the listrecps while freeing from memory at the
1027 * same time. Am I clever or what? :)
1029 while (sc.listrecps != NULL) {
1030 fprintf(fp, "listrecp|%s\n", sc.listrecps->name);
1031 nptr = sc.listrecps->next;
1033 sc.listrecps = nptr;
1035 /* Do the same for digestrecps */
1036 while (sc.digestrecps != NULL) {
1037 fprintf(fp, "digestrecp|%s\n", sc.digestrecps->name);
1038 nptr = sc.digestrecps->next;
1039 free(sc.digestrecps);
1040 sc.digestrecps = nptr;
1042 /* Do the same for participates */
1043 while (sc.participates != NULL) {
1044 fprintf(fp, "participate|%s\n", sc.participates->name);
1045 nptr = sc.participates->next;
1046 free(sc.participates);
1047 sc.participates = nptr;
1049 while (sc.ignet_push_shares != NULL) {
1050 /* by checking each node's validity, we automatically
1051 * purge nodes which do not exist from room network
1052 * configurations at this time.
1054 if (is_valid_node(NULL, NULL, sc.ignet_push_shares->remote_nodename) == 0) {
1056 fprintf(fp, "ignet_push_share|%s",
1057 sc.ignet_push_shares->remote_nodename);
1058 if (strlen(sc.ignet_push_shares->remote_roomname) > 0) {
1059 fprintf(fp, "|%s", sc.ignet_push_shares->remote_roomname);
1062 mptr = sc.ignet_push_shares->next;
1063 free(sc.ignet_push_shares);
1064 sc.ignet_push_shares = mptr;
1066 if (sc.misc != NULL) {
1067 fwrite(sc.misc, strlen(sc.misc), 1, fp);
1073 end_critical_section(S_NETCONFIGS);
1079 * Send the *entire* contents of the current room to one specific network node,
1080 * ignoring anything we know about which messages have already undergone
1081 * network processing. This can be used to bring a new node into sync.
1083 int network_sync_to(char *target_node) {
1084 struct SpoolControl sc;
1085 int num_spooled = 0;
1094 /* Grab the configuration line we're looking for */
1095 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
1096 begin_critical_section(S_NETCONFIGS);
1097 fp = fopen(filename, "r");
1099 end_critical_section(S_NETCONFIGS);
1102 while (fgets(buf, sizeof buf, fp) != NULL) {
1103 buf[strlen(buf)-1] = 0;
1104 extract_token(sc_type, buf, 0, '|', sizeof sc_type);
1105 extract_token(sc_node, buf, 1, '|', sizeof sc_node);
1106 extract_token(sc_room, buf, 2, '|', sizeof sc_room);
1107 if ( (!strcasecmp(sc_type, "ignet_push_share"))
1108 && (!strcasecmp(sc_node, target_node)) ) {
1111 /* Concise syntax because we don't need a full linked-list */
1112 memset(&sc, 0, sizeof(struct SpoolControl));
1113 sc.ignet_push_shares = (struct maplist *)
1114 malloc(sizeof(struct maplist));
1115 sc.ignet_push_shares->next = NULL;
1116 safestrncpy(sc.ignet_push_shares->remote_nodename,
1118 sizeof sc.ignet_push_shares->remote_nodename);
1119 safestrncpy(sc.ignet_push_shares->remote_roomname,
1121 sizeof sc.ignet_push_shares->remote_roomname);
1125 end_critical_section(S_NETCONFIGS);
1127 if (!found_node) return(-1);
1129 /* Send ALL messages */
1130 num_spooled = CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL,
1131 network_spool_msg, &sc);
1133 /* Concise cleanup because we know there's only one node in the sc */
1134 free(sc.ignet_push_shares);
1136 lprintf(CTDL_NOTICE, "Synchronized %d messages to <%s>\n",
1137 num_spooled, target_node);
1138 return(num_spooled);
1143 * Implements the NSYN command
1145 void cmd_nsyn(char *argbuf) {
1147 char target_node[256];
1149 if (CtdlAccessCheck(ac_aide)) return;
1151 extract_token(target_node, argbuf, 0, '|', sizeof target_node);
1152 num_spooled = network_sync_to(target_node);
1153 if (num_spooled >= 0) {
1154 cprintf("%d Spooled %d messages.\n", CIT_OK, num_spooled);
1157 cprintf("%d No such room/node share exists.\n",
1158 ERROR + ROOM_NOT_FOUND);
1165 * Batch up and send all outbound traffic from the current room
1167 void network_queue_room(struct ctdlroom *qrbuf, void *data) {
1168 struct RoomProcList *ptr;
1170 ptr = (struct RoomProcList *) malloc(sizeof (struct RoomProcList));
1171 if (ptr == NULL) return;
1173 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
1180 * Learn topology from path fields
1182 void network_learn_topology(char *node, char *path) {
1184 struct NetMap *nmptr;
1186 strcpy(nexthop, "");
1188 if (num_tokens(path, '!') < 3) return;
1189 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
1190 if (!strcasecmp(nmptr->nodename, node)) {
1191 extract_token(nmptr->nexthop, path, 0, '!', sizeof nmptr->nexthop);
1192 nmptr->lastcontact = time(NULL);
1198 /* If we got here then it's not in the map, so add it. */
1199 nmptr = (struct NetMap *) malloc(sizeof (struct NetMap));
1200 strcpy(nmptr->nodename, node);
1201 nmptr->lastcontact = time(NULL);
1202 extract_token(nmptr->nexthop, path, 0, '!', sizeof nmptr->nexthop);
1203 nmptr->next = the_netmap;
1212 * Bounce a message back to the sender
1214 void network_bounce(struct CtdlMessage *msg, char *reason) {
1215 char *oldpath = NULL;
1217 char bouncesource[SIZ];
1218 char recipient[SIZ];
1219 struct recptypes *valid = NULL;
1220 char force_room[ROOMNAMELEN];
1221 static int serialnum = 0;
1224 lprintf(CTDL_DEBUG, "entering network_bounce()\n");
1226 if (msg == NULL) return;
1228 snprintf(bouncesource, sizeof bouncesource, "%s@%s", BOUNCESOURCE, config.c_nodename);
1231 * Give it a fresh message ID
1233 if (msg->cm_fields['I'] != NULL) {
1234 free(msg->cm_fields['I']);
1236 snprintf(buf, sizeof buf, "%ld.%04lx.%04x@%s",
1237 (long)time(NULL), (long)getpid(), ++serialnum, config.c_fqdn);
1238 msg->cm_fields['I'] = strdup(buf);
1241 * FIXME ... right now we're just sending a bounce; we really want to
1242 * include the text of the bounced message.
1244 if (msg->cm_fields['M'] != NULL) {
1245 free(msg->cm_fields['M']);
1247 msg->cm_fields['M'] = strdup(reason);
1248 msg->cm_format_type = 0;
1251 * Turn the message around
1253 if (msg->cm_fields['R'] == NULL) {
1254 free(msg->cm_fields['R']);
1257 if (msg->cm_fields['D'] == NULL) {
1258 free(msg->cm_fields['D']);
1261 snprintf(recipient, sizeof recipient, "%s@%s",
1262 msg->cm_fields['A'], msg->cm_fields['N']);
1264 if (msg->cm_fields['A'] == NULL) {
1265 free(msg->cm_fields['A']);
1268 if (msg->cm_fields['N'] == NULL) {
1269 free(msg->cm_fields['N']);
1272 if (msg->cm_fields['U'] == NULL) {
1273 free(msg->cm_fields['U']);
1276 msg->cm_fields['A'] = strdup(BOUNCESOURCE);
1277 msg->cm_fields['N'] = strdup(config.c_nodename);
1278 msg->cm_fields['U'] = strdup("Delivery Status Notification (Failure)");
1280 /* prepend our node to the path */
1281 if (msg->cm_fields['P'] != NULL) {
1282 oldpath = msg->cm_fields['P'];
1283 msg->cm_fields['P'] = NULL;
1286 oldpath = strdup("unknown_user");
1288 size = strlen(oldpath) + SIZ;
1289 msg->cm_fields['P'] = malloc(size);
1290 snprintf(msg->cm_fields['P'], size, "%s!%s", config.c_nodename, oldpath);
1293 /* Now submit the message */
1294 valid = validate_recipients(recipient);
1295 if (valid != NULL) if (valid->num_error != 0) {
1299 if ( (valid == NULL) || (!strcasecmp(recipient, bouncesource)) ) {
1300 strcpy(force_room, config.c_aideroom);
1303 strcpy(force_room, "");
1305 if ( (valid == NULL) && (strlen(force_room) == 0) ) {
1306 strcpy(force_room, config.c_aideroom);
1308 CtdlSubmitMsg(msg, valid, force_room);
1311 if (valid != NULL) free(valid);
1312 CtdlFreeMessage(msg);
1313 lprintf(CTDL_DEBUG, "leaving network_bounce()\n");
1320 * Process a buffer containing a single message from a single file
1321 * from the inbound queue
1323 void network_process_buffer(char *buffer, long size) {
1324 struct CtdlMessage *msg;
1327 struct recptypes *recp = NULL;
1328 char target_room[ROOMNAMELEN];
1329 struct ser_ret sermsg;
1330 char *oldpath = NULL;
1334 unsigned char firstbyte;
1335 unsigned char lastbyte;
1337 /* Validate just a little bit. First byte should be FF and
1338 * last byte should be 00.
1340 memcpy(&firstbyte, &buffer[0], 1);
1341 memcpy(&lastbyte, &buffer[size-1], 1);
1342 if ( (firstbyte != 255) || (lastbyte != 0) ) {
1343 lprintf(CTDL_ERR, "Corrupt message! Ignoring.\n");
1347 /* Set default target room to trash */
1348 strcpy(target_room, TWITROOM);
1350 /* Load the message into memory */
1351 msg = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
1352 memset(msg, 0, sizeof(struct CtdlMessage));
1353 msg->cm_magic = CTDLMESSAGE_MAGIC;
1354 msg->cm_anon_type = buffer[1];
1355 msg->cm_format_type = buffer[2];
1357 for (pos = 3; pos < size; ++pos) {
1358 field = buffer[pos];
1359 msg->cm_fields[field] = strdup(&buffer[pos+1]);
1360 pos = pos + strlen(&buffer[(int)pos]);
1363 /* Check for message routing */
1364 if (msg->cm_fields['D'] != NULL) {
1365 if (strcasecmp(msg->cm_fields['D'], config.c_nodename)) {
1367 /* route the message */
1368 strcpy(nexthop, "");
1369 if (is_valid_node(nexthop, NULL,
1370 msg->cm_fields['D']) == 0) {
1372 /* prepend our node to the path */
1373 if (msg->cm_fields['P'] != NULL) {
1374 oldpath = msg->cm_fields['P'];
1375 msg->cm_fields['P'] = NULL;
1378 oldpath = strdup("unknown_user");
1380 size = strlen(oldpath) + SIZ;
1381 msg->cm_fields['P'] = malloc(size);
1382 snprintf(msg->cm_fields['P'], size, "%s!%s",
1383 config.c_nodename, oldpath);
1386 /* serialize the message */
1387 serialize_message(&sermsg, msg);
1390 if (strlen(nexthop) == 0) {
1391 strcpy(nexthop, msg->cm_fields['D']);
1398 lprintf(CTDL_DEBUG, "Appending to %s\n", filename);
1399 fp = fopen(filename, "ab");
1406 lprintf(CTDL_ERR, "%s: %s\n", filename, strerror(errno));
1409 CtdlFreeMessage(msg);
1413 else { /* invalid destination node name */
1416 "A message you sent could not be delivered due to an invalid destination node"
1417 " name. Please check the address and try sending the message again.\n");
1426 * Check to see if we already have a copy of this message, and
1427 * abort its processing if so. (We used to post a warning to Aide>
1428 * every time this happened, but the network is now so densely
1429 * connected that it's inevitable.)
1431 if (network_usetable(msg) != 0) {
1435 /* Learn network topology from the path */
1436 if ((msg->cm_fields['N'] != NULL) && (msg->cm_fields['P'] != NULL)) {
1437 network_learn_topology(msg->cm_fields['N'],
1438 msg->cm_fields['P']);
1441 /* Is the sending node giving us a very persuasive suggestion about
1442 * which room this message should be saved in? If so, go with that.
1444 if (msg->cm_fields['C'] != NULL) {
1445 safestrncpy(target_room,
1446 msg->cm_fields['C'],
1447 sizeof target_room);
1450 /* Otherwise, does it have a recipient? If so, validate it... */
1451 else if (msg->cm_fields['R'] != NULL) {
1452 recp = validate_recipients(msg->cm_fields['R']);
1453 if (recp != NULL) if (recp->num_error != 0) {
1455 "A message you sent could not be delivered due to an invalid address.\n"
1456 "Please check the address and try sending the message again.\n");
1461 strcpy(target_room, ""); /* no target room if mail */
1464 /* Our last shot at finding a home for this message is to see if
1465 * it has the O field (Originating room) set.
1467 else if (msg->cm_fields['O'] != NULL) {
1468 safestrncpy(target_room,
1469 msg->cm_fields['O'],
1470 sizeof target_room);
1473 /* Strip out fields that are only relevant during transit */
1474 if (msg->cm_fields['D'] != NULL) {
1475 free(msg->cm_fields['D']);
1476 msg->cm_fields['D'] = NULL;
1478 if (msg->cm_fields['C'] != NULL) {
1479 free(msg->cm_fields['C']);
1480 msg->cm_fields['C'] = NULL;
1483 /* save the message into a room */
1484 if (PerformNetprocHooks(msg, target_room) == 0) {
1485 msg->cm_flags = CM_SKIP_HOOKS;
1486 CtdlSubmitMsg(msg, recp, target_room);
1488 CtdlFreeMessage(msg);
1494 * Process a single message from a single file from the inbound queue
1496 void network_process_message(FILE *fp, long msgstart, long msgend) {
1501 hold_pos = ftell(fp);
1502 size = msgend - msgstart + 1;
1503 buffer = malloc(size);
1504 if (buffer != NULL) {
1505 fseek(fp, msgstart, SEEK_SET);
1506 fread(buffer, size, 1, fp);
1507 network_process_buffer(buffer, size);
1511 fseek(fp, hold_pos, SEEK_SET);
1516 * Process a single file from the inbound queue
1518 void network_process_file(char *filename) {
1520 long msgstart = (-1L);
1521 long msgend = (-1L);
1526 fp = fopen(filename, "rb");
1528 lprintf(CTDL_CRIT, "Error opening %s: %s\n",
1529 filename, strerror(errno));
1533 lprintf(CTDL_INFO, "network: processing <%s>\n", filename);
1535 /* Look for messages in the data stream and break them out */
1536 while (ch = getc(fp), ch >= 0) {
1539 if (msgstart >= 0L) {
1540 msgend = msgcur - 1;
1541 network_process_message(fp, msgstart, msgend);
1549 msgend = msgcur - 1;
1550 if (msgstart >= 0L) {
1551 network_process_message(fp, msgstart, msgend);
1560 * Process anything in the inbound queue
1562 void network_do_spoolin(void) {
1565 struct stat statbuf;
1567 static time_t last_spoolin_mtime = 0L;
1570 * Check the spoolin directory's modification time. If it hasn't
1571 * been touched, we don't need to scan it.
1573 if (stat(ctdl_netin_dir, &statbuf)) return;
1574 if (statbuf.st_mtime == last_spoolin_mtime) {
1575 lprintf(CTDL_DEBUG, "network: nothing in inbound queue\n");
1578 last_spoolin_mtime = statbuf.st_mtime;
1579 lprintf(CTDL_DEBUG, "network: processing inbound queue\n");
1582 * Ok, there's something interesting in there, so scan it.
1584 dp = opendir(ctdl_netin_dir);
1585 if (dp == NULL) return;
1587 while (d = readdir(dp), d != NULL) {
1588 if ((strcmp(d->d_name, ".")) && (strcmp(d->d_name, ".."))) {
1594 network_process_file(filename);
1602 * Delete any files in the outbound queue that were intended
1603 * to be sent to nodes which no longer exist.
1605 void network_purge_spoolout(void) {
1612 dp = opendir(ctdl_netout_dir);
1613 if (dp == NULL) return;
1615 while (d = readdir(dp), d != NULL) {
1616 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
1624 strcpy(nexthop, "");
1625 i = is_valid_node(nexthop, NULL, d->d_name);
1627 if ( (i != 0) || (strlen(nexthop) > 0) ) {
1638 * receive network spool from the remote system
1640 void receive_spool(int sock, char *remote_nodename) {
1642 long bytes_received;
1644 static char pbuf[IGNET_PACKET_SIZE];
1645 char tempfilename[PATH_MAX];
1649 CtdlMakeTempFileName(tempfilename, sizeof tempfilename);
1650 if (sock_puts(sock, "NDOP") < 0) return;
1651 if (sock_gets(sock, buf) < 0) return;
1652 lprintf(CTDL_DEBUG, "<%s\n", buf);
1653 if (buf[0] != '2') {
1656 download_len = extract_long(&buf[4], 0);
1658 bytes_received = 0L;
1659 fp = fopen(tempfilename, "w");
1661 lprintf(CTDL_CRIT, "cannot open download file locally: %s\n",
1666 while (bytes_received < download_len) {
1667 snprintf(buf, sizeof buf, "READ %ld|%ld",
1669 ((download_len - bytes_received > IGNET_PACKET_SIZE)
1670 ? IGNET_PACKET_SIZE : (download_len - bytes_received)));
1671 if (sock_puts(sock, buf) < 0) {
1673 unlink(tempfilename);
1676 if (sock_gets(sock, buf) < 0) {
1678 unlink(tempfilename);
1681 if (buf[0] == '6') {
1682 plen = extract_long(&buf[4], 0);
1683 if (sock_read(sock, pbuf, plen) < 0) {
1685 unlink(tempfilename);
1688 fwrite((char *) pbuf, plen, 1, fp);
1689 bytes_received = bytes_received + plen;
1694 if (sock_puts(sock, "CLOS") < 0) {
1695 unlink(tempfilename);
1698 if (sock_gets(sock, buf) < 0) {
1699 unlink(tempfilename);
1702 if (download_len > 0)
1703 lprintf(CTDL_NOTICE, "Received %ld octets from <%s>",
1704 download_len, remote_nodename);
1705 lprintf(CTDL_DEBUG, "%s", buf);
1706 /* TODO: make move inline. forking is verry expensive. */
1720 * transmit network spool to the remote system
1722 void transmit_spool(int sock, char *remote_nodename)
1727 long bytes_to_write, thisblock, bytes_written;
1731 if (sock_puts(sock, "NUOP") < 0) return;
1732 if (sock_gets(sock, buf) < 0) return;
1733 lprintf(CTDL_DEBUG, "<%s\n", buf);
1734 if (buf[0] != '2') {
1738 snprintf(sfname, sizeof sfname,
1742 fd = open(sfname, O_RDONLY);
1744 if (errno != ENOENT) {
1745 lprintf(CTDL_CRIT, "cannot open upload file locally: %s\n",
1751 while (plen = (long) read(fd, pbuf, IGNET_PACKET_SIZE), plen > 0L) {
1752 bytes_to_write = plen;
1753 while (bytes_to_write > 0L) {
1754 snprintf(buf, sizeof buf, "WRIT %ld", bytes_to_write);
1755 if (sock_puts(sock, buf) < 0) {
1759 if (sock_gets(sock, buf) < 0) {
1763 thisblock = atol(&buf[4]);
1764 if (buf[0] == '7') {
1765 if (sock_write(sock, pbuf,
1766 (int) thisblock) < 0) {
1770 bytes_to_write -= thisblock;
1771 bytes_written += thisblock;
1780 if (sock_puts(sock, "UCLS 1") < 0) return;
1781 if (sock_gets(sock, buf) < 0) return;
1782 lprintf(CTDL_NOTICE, "Sent %ld octets to <%s>\n",
1783 bytes_written, remote_nodename);
1784 lprintf(CTDL_DEBUG, "<%s\n", buf);
1785 if (buf[0] == '2') {
1786 lprintf(CTDL_DEBUG, "Removing <%s>\n", sfname);
1794 * Poll one Citadel node (called by network_poll_other_citadel_nodes() below)
1796 void network_poll_node(char *node, char *secret, char *host, char *port) {
1800 if (network_talking_to(node, NTT_CHECK)) return;
1801 network_talking_to(node, NTT_ADD);
1802 lprintf(CTDL_NOTICE, "Connecting to <%s> at %s:%s\n", node, host, port);
1804 sock = sock_connect(host, port, "tcp");
1806 lprintf(CTDL_ERR, "Could not connect: %s\n", strerror(errno));
1807 network_talking_to(node, NTT_REMOVE);
1811 lprintf(CTDL_DEBUG, "Connected!\n");
1813 /* Read the server greeting */
1814 if (sock_gets(sock, buf) < 0) goto bail;
1815 lprintf(CTDL_DEBUG, ">%s\n", buf);
1817 /* Identify ourselves */
1818 snprintf(buf, sizeof buf, "NETP %s|%s", config.c_nodename, secret);
1819 lprintf(CTDL_DEBUG, "<%s\n", buf);
1820 if (sock_puts(sock, buf) <0) goto bail;
1821 if (sock_gets(sock, buf) < 0) goto bail;
1822 lprintf(CTDL_DEBUG, ">%s\n", buf);
1823 if (buf[0] != '2') goto bail;
1825 /* At this point we are authenticated. */
1826 receive_spool(sock, node);
1827 transmit_spool(sock, node);
1829 sock_puts(sock, "QUIT");
1830 bail: sock_close(sock);
1831 network_talking_to(node, NTT_REMOVE);
1837 * Poll other Citadel nodes and transfer inbound/outbound network data.
1838 * Set "full" to nonzero to force a poll of every node, or to zero to poll
1839 * only nodes to which we have data to send.
1841 void network_poll_other_citadel_nodes(int full_poll) {
1849 char spoolfile[256];
1851 if (working_ignetcfg == NULL) {
1852 lprintf(CTDL_DEBUG, "No nodes defined - not polling\n");
1856 /* Use the string tokenizer to grab one line at a time */
1857 for (i=0; i<num_tokens(working_ignetcfg, '\n'); ++i) {
1858 extract_token(linebuf, working_ignetcfg, i, '\n', sizeof linebuf);
1859 extract_token(node, linebuf, 0, '|', sizeof node);
1860 extract_token(secret, linebuf, 1, '|', sizeof secret);
1861 extract_token(host, linebuf, 2, '|', sizeof host);
1862 extract_token(port, linebuf, 3, '|', sizeof port);
1863 if ( (strlen(node) > 0) && (strlen(secret) > 0)
1864 && (strlen(host) > 0) && strlen(port) > 0) {
1872 if (access(spoolfile, R_OK) == 0) {
1877 network_poll_node(node, secret, host, port);
1888 * It's ok if these directories already exist. Just fail silently.
1890 void create_spool_dirs(void) {
1891 mkdir(ctdl_spool_dir, 0700);
1892 chown(ctdl_spool_dir, CTDLUID, (-1));
1893 mkdir(ctdl_netin_dir, 0700);
1894 chown(ctdl_netin_dir, CTDLUID, (-1));
1895 mkdir(ctdl_netout_dir, 0700);
1896 chown(ctdl_netout_dir, CTDLUID, (-1));
1904 * network_do_queue()
1906 * Run through the rooms doing various types of network stuff.
1908 void network_do_queue(void) {
1909 static time_t last_run = 0L;
1910 struct RoomProcList *ptr;
1911 int full_processing = 1;
1914 * Run the full set of processing tasks no more frequently
1915 * than once every n seconds
1917 if ( (time(NULL) - last_run) < config.c_net_freq ) {
1918 full_processing = 0;
1922 * This is a simple concurrency check to make sure only one queue run
1923 * is done at a time. We could do this with a mutex, but since we
1924 * don't really require extremely fine granularity here, we'll do it
1925 * with a static variable instead.
1927 if (doing_queue) return;
1930 /* Load the IGnet Configuration into memory */
1931 load_working_ignetcfg();
1934 * Poll other Citadel nodes. Maybe. If "full_processing" is set
1935 * then we poll everyone. Otherwise we only poll nodes we have stuff
1938 network_poll_other_citadel_nodes(full_processing);
1941 * Load the network map and filter list into memory.
1944 filterlist = load_filter_list();
1947 * Go ahead and run the queue
1949 if (full_processing) {
1950 lprintf(CTDL_DEBUG, "network: loading outbound queue\n");
1951 ForEachRoom(network_queue_room, NULL);
1953 lprintf(CTDL_DEBUG, "network: running outbound queue\n");
1954 while (rplist != NULL) {
1955 network_spoolout_room(rplist->name);
1957 rplist = rplist->next;
1962 /* If there is anything in the inbound queue, process it */
1963 network_do_spoolin();
1965 /* Save the network map back to disk */
1966 write_network_map();
1968 /* Free the filter list in memory */
1969 free_filter_list(filterlist);
1972 network_purge_spoolout();
1974 lprintf(CTDL_DEBUG, "network: queue run completed\n");
1976 if (full_processing) {
1977 last_run = time(NULL);
1985 * cmd_netp() - authenticate to the server as another Citadel node polling
1986 * for network traffic
1988 void cmd_netp(char *cmdbuf)
1998 extract_token(node, cmdbuf, 0, '|', sizeof node);
1999 extract_token(pass, cmdbuf, 1, '|', sizeof pass);
2002 lprintf(CTDL_WARNING, "Network node <%s> refused - spooling", node);
2003 cprintf("%d spooling - try again in a few minutes\n",
2004 ERROR + RESOURCE_BUSY);
2008 /* load the IGnet Configuration to check node validity */
2009 load_working_ignetcfg();
2010 v = is_valid_node(nexthop, secret, node);
2013 lprintf(CTDL_WARNING, "Unknown node <%s>\n", node);
2014 cprintf("%d authentication failed\n",
2015 ERROR + PASSWORD_REQUIRED);
2019 if (strcasecmp(pass, secret)) {
2020 lprintf(CTDL_WARNING, "Bad password for network node <%s>", node);
2021 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
2025 if (network_talking_to(node, NTT_CHECK)) {
2026 lprintf(CTDL_WARNING, "Duplicate session for network node <%s>", node);
2027 cprintf("%d Already talking to %s right now\n", ERROR + RESOURCE_BUSY, node);
2031 safestrncpy(CC->net_node, node, sizeof CC->net_node);
2032 network_talking_to(node, NTT_ADD);
2033 lprintf(CTDL_NOTICE, "Network node <%s> logged in\n", CC->net_node);
2034 cprintf("%d authenticated as network node '%s'\n", CIT_OK,
2039 * Module entry point
2041 char *serv_network_init(void)
2043 create_spool_dirs();
2044 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
2045 CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");
2046 CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");
2047 CtdlRegisterProtoHook(cmd_nsyn, "NSYN", "Synchronize room to node");
2048 CtdlRegisterSessionHook(network_do_queue, EVT_TIMER);