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; we have a mutex that keeps it safe.
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, ctdl_netcfg_dir);
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 CtdlMakeTempFileName(tempfilename, sizeof tempfilename);
394 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
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 * Deliver digest messages
425 void network_deliver_digest(struct SpoolControl *sc) {
428 struct CtdlMessage *msg = NULL;
432 size_t instr_len = SIZ;
433 struct CtdlMessage *imsg = NULL;
434 struct namelist *nptr;
436 if (sc->num_msgs_spooled < 1) {
437 fclose(sc->digestfp);
442 msg = malloc(sizeof(struct CtdlMessage));
443 memset(msg, 0, sizeof(struct CtdlMessage));
444 msg->cm_magic = CTDLMESSAGE_MAGIC;
445 msg->cm_format_type = FMT_RFC822;
446 msg->cm_anon_type = MES_NORMAL;
448 sprintf(buf, "%ld", time(NULL));
449 msg->cm_fields['T'] = strdup(buf);
450 msg->cm_fields['A'] = strdup(CC->room.QRname);
451 snprintf(buf, sizeof buf, "[%s]", CC->room.QRname);
452 msg->cm_fields['U'] = strdup(buf);
453 sprintf(buf, "room_%s@%s", CC->room.QRname, config.c_fqdn);
454 for (i=0; i<strlen(buf); ++i) {
455 if (isspace(buf[i])) buf[i]='_';
456 buf[i] = tolower(buf[i]);
458 msg->cm_fields['F'] = strdup(buf);
459 msg->cm_fields['R'] = strdup(buf);
462 * Go fetch the contents of the digest
464 fseek(sc->digestfp, 0L, SEEK_END);
465 msglen = ftell(sc->digestfp);
467 msg->cm_fields['M'] = malloc(msglen + 1);
468 fseek(sc->digestfp, 0L, SEEK_SET);
469 fread(msg->cm_fields['M'], (size_t)msglen, 1, sc->digestfp);
470 msg->cm_fields['M'][msglen] = 0;
472 fclose(sc->digestfp);
475 msgnum = CtdlSubmitMsg(msg, NULL, SMTP_SPOOLOUT_ROOM);
476 CtdlFreeMessage(msg);
478 /* Now generate the delivery instructions */
481 * Figure out how big a buffer we need to allocate
483 for (nptr = sc->digestrecps; nptr != NULL; nptr = nptr->next) {
484 instr_len = instr_len + strlen(nptr->name) + 2;
490 lprintf(CTDL_DEBUG, "Generating delivery instructions\n");
491 instr = malloc(instr_len);
493 lprintf(CTDL_EMERG, "Cannot allocate %ld bytes for instr...\n",
497 snprintf(instr, instr_len,
498 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
499 "bounceto|postmaster@%s\n" ,
500 SPOOLMIME, msgnum, (long)time(NULL), config.c_fqdn );
502 /* Generate delivery instructions for each recipient */
503 for (nptr = sc->digestrecps; nptr != NULL; nptr = nptr->next) {
504 size_t tmp = strlen(instr);
505 snprintf(&instr[tmp], instr_len - tmp,
506 "remote|%s|0||\n", nptr->name);
510 * Generate a message from the instructions
512 imsg = malloc(sizeof(struct CtdlMessage));
513 memset(imsg, 0, sizeof(struct CtdlMessage));
514 imsg->cm_magic = CTDLMESSAGE_MAGIC;
515 imsg->cm_anon_type = MES_NORMAL;
516 imsg->cm_format_type = FMT_RFC822;
517 imsg->cm_fields['A'] = strdup("Citadel");
518 imsg->cm_fields['M'] = instr;
520 /* Save delivery instructions in spoolout room */
521 CtdlSubmitMsg(imsg, NULL, SMTP_SPOOLOUT_ROOM);
522 CtdlFreeMessage(imsg);
527 * Deliver list messages to everyone on the list ... efficiently
529 void network_deliver_list(struct CtdlMessage *msg, struct SpoolControl *sc) {
532 size_t instr_len = SIZ;
533 struct CtdlMessage *imsg = NULL;
534 struct namelist *nptr;
536 /* Don't do this if there were no recipients! */
537 if (sc->listrecps == NULL) return;
539 /* Save the message to disk... */
540 msgnum = CtdlSubmitMsg(msg, NULL, SMTP_SPOOLOUT_ROOM);
542 /* Now generate the delivery instructions */
545 * Figure out how big a buffer we need to allocate
547 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
548 instr_len = instr_len + strlen(nptr->name) + 2;
554 lprintf(CTDL_DEBUG, "Generating delivery instructions\n");
555 instr = malloc(instr_len);
557 lprintf(CTDL_EMERG, "Cannot allocate %ld bytes for instr...\n",
561 snprintf(instr, instr_len,
562 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
563 "bounceto|postmaster@%s\n" ,
564 SPOOLMIME, msgnum, (long)time(NULL), config.c_fqdn );
566 /* Generate delivery instructions for each recipient */
567 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
568 size_t tmp = strlen(instr);
569 snprintf(&instr[tmp], instr_len - tmp, "remote|%s|0||\n", nptr->name);
573 * Generate a message from the instructions
575 imsg = malloc(sizeof(struct CtdlMessage));
576 memset(imsg, 0, sizeof(struct CtdlMessage));
577 imsg->cm_magic = CTDLMESSAGE_MAGIC;
578 imsg->cm_anon_type = MES_NORMAL;
579 imsg->cm_format_type = FMT_RFC822;
580 imsg->cm_fields['A'] = strdup("Citadel");
581 imsg->cm_fields['M'] = instr;
583 /* Save delivery instructions in spoolout room */
584 CtdlSubmitMsg(imsg, NULL, SMTP_SPOOLOUT_ROOM);
585 CtdlFreeMessage(imsg);
592 * Spools out one message from the list.
594 void network_spool_msg(long msgnum, void *userdata) {
595 struct SpoolControl *sc;
597 char *newpath = NULL;
598 size_t instr_len = SIZ;
599 struct CtdlMessage *msg = NULL;
600 struct namelist *nptr;
601 struct maplist *mptr;
602 struct ser_ret sermsg;
608 int delete_after_send = 0; /* Set to 1 to delete after spooling */
609 int ok_to_participate = 0;
610 struct recptypes *valid;
612 sc = (struct SpoolControl *)userdata;
615 * Process mailing list recipients
618 if (sc->listrecps != NULL) {
619 /* Fetch the message. We're going to need to modify it
620 * in order to insert the [list name] in it, etc.
622 msg = CtdlFetchMessage(msgnum, 1);
625 /* Prepend "[List name]" to the subject */
626 if (msg->cm_fields['U'] == NULL) {
627 msg->cm_fields['U'] = strdup("(no subject)");
629 snprintf(buf, sizeof buf, "[%s] %s", CC->room.QRname, msg->cm_fields['U']);
630 free(msg->cm_fields['U']);
631 msg->cm_fields['U'] = strdup(buf);
633 /* Set the recipient of the list message to the
634 * email address of the room itself.
635 * FIXME ... I want to be able to pick any address
637 if (msg->cm_fields['R'] != NULL) {
638 free(msg->cm_fields['R']);
640 msg->cm_fields['R'] = malloc(256);
641 snprintf(msg->cm_fields['R'], 256,
642 "room_%s@%s", CC->room.QRname,
644 for (i=0; i<strlen(msg->cm_fields['R']); ++i) {
645 if (isspace(msg->cm_fields['R'][i])) {
646 msg->cm_fields['R'][i] = '_';
650 /* Handle delivery */
651 network_deliver_list(msg, sc);
652 CtdlFreeMessage(msg);
657 * Process digest recipients
659 if ((sc->digestrecps != NULL) && (sc->digestfp != NULL)) {
660 msg = CtdlFetchMessage(msgnum, 1);
662 fprintf(sc->digestfp, " -----------------------------------"
663 "------------------------------------"
665 fprintf(sc->digestfp, "From: ");
666 if (msg->cm_fields['A'] != NULL) {
667 fprintf(sc->digestfp, "%s ", msg->cm_fields['A']);
669 if (msg->cm_fields['F'] != NULL) {
670 fprintf(sc->digestfp, "<%s> ", msg->cm_fields['F']);
672 else if (msg->cm_fields['N'] != NULL) {
673 fprintf(sc->digestfp, "@%s ", msg->cm_fields['N']);
675 fprintf(sc->digestfp, "\n");
676 if (msg->cm_fields['U'] != NULL) {
677 fprintf(sc->digestfp, "Subject: %s\n", msg->cm_fields['U']);
680 CC->redirect_buffer = malloc(SIZ);
681 CC->redirect_len = 0;
682 CC->redirect_alloc = SIZ;
684 safestrncpy(CC->preferred_formats, "text/plain", sizeof CC->preferred_formats);
685 CtdlOutputPreLoadedMsg(msg, MT_CITADEL, HEADERS_NONE, 0, 0);
687 striplt(CC->redirect_buffer);
688 fprintf(sc->digestfp, "\n%s\n", CC->redirect_buffer);
690 free(CC->redirect_buffer);
691 CC->redirect_buffer = NULL;
692 CC->redirect_len = 0;
693 CC->redirect_alloc = 0;
695 sc->num_msgs_spooled += 1;
701 * Process client-side list participations for this room
704 if (sc->participates != NULL) {
705 msg = CtdlFetchMessage(msgnum, 1);
708 /* Only send messages which originated on our own Citadel
709 * network, otherwise we'll end up sending the remote
710 * mailing list's messages back to it, which is rude...
712 ok_to_participate = 0;
713 if (msg->cm_fields['N'] != NULL) {
714 if (!strcasecmp(msg->cm_fields['N'], config.c_nodename)) {
715 ok_to_participate = 1;
717 if (is_valid_node(NULL, NULL, msg->cm_fields['N']) == 0) {
718 ok_to_participate = 1;
721 if (ok_to_participate) {
722 if (msg->cm_fields['F'] != NULL) {
723 free(msg->cm_fields['F']);
725 msg->cm_fields['F'] = malloc(SIZ);
726 /* Replace the Internet email address of the actual
727 * author with the email address of the room itself,
728 * so the remote listserv doesn't reject us.
729 * FIXME ... I want to be able to pick any address
731 snprintf(msg->cm_fields['F'], SIZ,
732 "room_%s@%s", CC->room.QRname,
734 for (i=0; i<strlen(msg->cm_fields['F']); ++i) {
735 if (isspace(msg->cm_fields['F'][i])) {
736 msg->cm_fields['F'][i] = '_';
741 * Figure out how big a buffer we need to allocate
743 for (nptr = sc->participates; nptr != NULL; nptr = nptr->next) {
745 if (msg->cm_fields['R'] == NULL) {
746 free(msg->cm_fields['R']);
748 msg->cm_fields['R'] = strdup(nptr->name);
750 valid = validate_recipients(nptr->name);
751 CtdlSubmitMsg(msg, valid, "");
752 free_recipients(valid);
756 CtdlFreeMessage(msg);
761 * Process IGnet push shares
763 msg = CtdlFetchMessage(msgnum, 1);
767 /* Prepend our node name to the Path field whenever
768 * sending a message to another IGnet node
770 if (msg->cm_fields['P'] == NULL) {
771 msg->cm_fields['P'] = strdup("username");
773 newpath_len = strlen(msg->cm_fields['P']) +
774 strlen(config.c_nodename) + 2;
775 newpath = malloc(newpath_len);
776 snprintf(newpath, newpath_len, "%s!%s",
777 config.c_nodename, msg->cm_fields['P']);
778 free(msg->cm_fields['P']);
779 msg->cm_fields['P'] = newpath;
782 * Determine if this message is set to be deleted
783 * after sending out on the network
785 if (msg->cm_fields['S'] != NULL) {
786 if (!strcasecmp(msg->cm_fields['S'], "CANCEL")) {
787 delete_after_send = 1;
791 /* Now send it to every node */
792 if (sc->ignet_push_shares != NULL)
793 for (mptr = sc->ignet_push_shares; mptr != NULL;
798 /* Check for valid node name */
799 if (is_valid_node(NULL, NULL, mptr->remote_nodename) != 0) {
800 lprintf(CTDL_ERR, "Invalid node <%s>\n",
801 mptr->remote_nodename);
805 /* Check for split horizon */
806 lprintf(CTDL_DEBUG, "Path is %s\n", msg->cm_fields['P']);
807 bang = num_tokens(msg->cm_fields['P'], '!');
808 if (bang > 1) for (i=0; i<(bang-1); ++i) {
809 extract_token(buf, msg->cm_fields['P'],
811 if (!strcasecmp(buf, mptr->remote_nodename)) {
816 /* Send the message */
820 * Force the message to appear in the correct room
821 * on the far end by setting the C field correctly
823 if (msg->cm_fields['C'] != NULL) {
824 free(msg->cm_fields['C']);
826 if (strlen(mptr->remote_roomname) > 0) {
827 msg->cm_fields['C'] = strdup(mptr->remote_roomname);
830 msg->cm_fields['C'] = strdup(CC->room.QRname);
833 /* serialize it for transmission */
834 serialize_message(&sermsg, msg);
835 if (sermsg.len > 0) {
837 /* write it to the spool file */
838 snprintf(filename, sizeof filename,"%s/%s",
840 mptr->remote_nodename);
841 lprintf(CTDL_DEBUG, "Appending to %s\n", filename);
842 fp = fopen(filename, "ab");
849 lprintf(CTDL_ERR, "%s: %s\n", filename, strerror(errno));
852 /* free the serialized version */
858 CtdlFreeMessage(msg);
861 /* update lastsent */
862 sc->lastsent = msgnum;
864 /* Delete this message if delete-after-send is set */
865 if (delete_after_send) {
866 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");
873 * Batch up and send all outbound traffic from the current room
875 void network_spoolout_room(char *room_to_spool) {
880 char roomname[ROOMNAMELEN];
883 struct SpoolControl sc;
884 struct namelist *nptr = NULL;
885 struct maplist *mptr = NULL;
888 int skipthisline = 0;
892 * If the room doesn't exist, don't try to perform its networking tasks.
893 * Normally this should never happen, but once in a while maybe a room gets
894 * queued for networking and then deleted before it can happen.
896 if (getroom(&CC->room, room_to_spool) != 0) {
897 lprintf(CTDL_CRIT, "ERROR: cannot load <%s>\n", room_to_spool);
901 memset(&sc, 0, sizeof(struct SpoolControl));
902 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
904 begin_critical_section(S_NETCONFIGS);
906 /* Only do net processing for rooms that have netconfigs */
907 fp = fopen(filename, "r");
909 end_critical_section(S_NETCONFIGS);
913 lprintf(CTDL_INFO, "Networking started for <%s>\n", CC->room.QRname);
915 while (fgets(buf, sizeof buf, fp) != NULL) {
916 buf[strlen(buf)-1] = 0;
918 extract_token(instr, buf, 0, '|', sizeof instr);
919 if (!strcasecmp(instr, "lastsent")) {
920 sc.lastsent = extract_long(buf, 1);
922 else if (!strcasecmp(instr, "listrecp")) {
923 nptr = (struct namelist *)
924 malloc(sizeof(struct namelist));
925 nptr->next = sc.listrecps;
926 extract_token(nptr->name, buf, 1, '|', sizeof nptr->name);
929 else if (!strcasecmp(instr, "participate")) {
930 nptr = (struct namelist *)
931 malloc(sizeof(struct namelist));
932 nptr->next = sc.participates;
933 extract_token(nptr->name, buf, 1, '|', sizeof nptr->name);
934 sc.participates = nptr;
936 else if (!strcasecmp(instr, "digestrecp")) {
937 nptr = (struct namelist *)
938 malloc(sizeof(struct namelist));
939 nptr->next = sc.digestrecps;
940 extract_token(nptr->name, buf, 1, '|', sizeof nptr->name);
941 sc.digestrecps = nptr;
943 else if (!strcasecmp(instr, "ignet_push_share")) {
944 /* by checking each node's validity, we automatically
945 * purge nodes which do not exist from room network
946 * configurations at this time.
948 extract_token(nodename, buf, 1, '|', sizeof nodename);
949 extract_token(roomname, buf, 2, '|', sizeof roomname);
950 strcpy(nexthop, "xxx");
951 if (is_valid_node(nexthop, NULL, nodename) == 0) {
952 if (strlen(nexthop) == 0) {
953 mptr = (struct maplist *)
954 malloc(sizeof(struct maplist));
955 mptr->next = sc.ignet_push_shares;
956 strcpy(mptr->remote_nodename, nodename);
957 strcpy(mptr->remote_roomname, roomname);
958 sc.ignet_push_shares = mptr;
963 /* Preserve 'other' lines ... *unless* they happen to
964 * be subscribe/unsubscribe pendings with expired
968 if (!strncasecmp(buf, "subpending|", 11)) {
969 if (time(NULL) - extract_long(buf, 4) > EXP) {
973 if (!strncasecmp(buf, "unsubpending|", 13)) {
974 if (time(NULL) - extract_long(buf, 3) > EXP) {
979 if (skipthisline == 0) {
980 linesize = strlen(buf);
981 sc.misc = realloc(sc.misc,
982 (miscsize + linesize + 2) );
983 sprintf(&sc.misc[miscsize], "%s\n", buf);
984 miscsize = miscsize + linesize + 1;
992 /* If there are digest recipients, we have to build a digest */
993 if (sc.digestrecps != NULL) {
994 sc.digestfp = tmpfile();
995 fprintf(sc.digestfp, "Content-type: text/plain\n\n");
998 /* Do something useful */
999 CtdlForEachMessage(MSGS_GT, sc.lastsent, NULL, NULL, NULL,
1000 network_spool_msg, &sc);
1002 /* If we wrote a digest, deliver it and then close it */
1003 snprintf(buf, sizeof buf, "room_%s@%s",
1004 CC->room.QRname, config.c_fqdn);
1005 for (i=0; i<strlen(buf); ++i) {
1006 buf[i] = tolower(buf[i]);
1007 if (isspace(buf[i])) buf[i] = '_';
1009 if (sc.digestfp != NULL) {
1010 fprintf(sc.digestfp, " -----------------------------------"
1011 "------------------------------------"
1013 "You are subscribed to the '%s' "
1015 "To post to the list: %s\n",
1016 CC->room.QRname, buf
1018 network_deliver_digest(&sc); /* deliver and close */
1021 /* Now rewrite the config file */
1022 fp = fopen(filename, "w");
1024 lprintf(CTDL_CRIT, "ERROR: cannot open %s: %s\n",
1025 filename, strerror(errno));
1028 fprintf(fp, "lastsent|%ld\n", sc.lastsent);
1030 /* Write out the listrecps while freeing from memory at the
1031 * same time. Am I clever or what? :)
1033 while (sc.listrecps != NULL) {
1034 fprintf(fp, "listrecp|%s\n", sc.listrecps->name);
1035 nptr = sc.listrecps->next;
1037 sc.listrecps = nptr;
1039 /* Do the same for digestrecps */
1040 while (sc.digestrecps != NULL) {
1041 fprintf(fp, "digestrecp|%s\n", sc.digestrecps->name);
1042 nptr = sc.digestrecps->next;
1043 free(sc.digestrecps);
1044 sc.digestrecps = nptr;
1046 /* Do the same for participates */
1047 while (sc.participates != NULL) {
1048 fprintf(fp, "participate|%s\n", sc.participates->name);
1049 nptr = sc.participates->next;
1050 free(sc.participates);
1051 sc.participates = nptr;
1053 while (sc.ignet_push_shares != NULL) {
1054 /* by checking each node's validity, we automatically
1055 * purge nodes which do not exist from room network
1056 * configurations at this time.
1058 if (is_valid_node(NULL, NULL, sc.ignet_push_shares->remote_nodename) == 0) {
1060 fprintf(fp, "ignet_push_share|%s",
1061 sc.ignet_push_shares->remote_nodename);
1062 if (strlen(sc.ignet_push_shares->remote_roomname) > 0) {
1063 fprintf(fp, "|%s", sc.ignet_push_shares->remote_roomname);
1066 mptr = sc.ignet_push_shares->next;
1067 free(sc.ignet_push_shares);
1068 sc.ignet_push_shares = mptr;
1070 if (sc.misc != NULL) {
1071 fwrite(sc.misc, strlen(sc.misc), 1, fp);
1077 end_critical_section(S_NETCONFIGS);
1083 * Send the *entire* contents of the current room to one specific network node,
1084 * ignoring anything we know about which messages have already undergone
1085 * network processing. This can be used to bring a new node into sync.
1087 int network_sync_to(char *target_node) {
1088 struct SpoolControl sc;
1089 int num_spooled = 0;
1098 /* Grab the configuration line we're looking for */
1099 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
1100 begin_critical_section(S_NETCONFIGS);
1101 fp = fopen(filename, "r");
1103 end_critical_section(S_NETCONFIGS);
1106 while (fgets(buf, sizeof buf, fp) != NULL) {
1107 buf[strlen(buf)-1] = 0;
1108 extract_token(sc_type, buf, 0, '|', sizeof sc_type);
1109 extract_token(sc_node, buf, 1, '|', sizeof sc_node);
1110 extract_token(sc_room, buf, 2, '|', sizeof sc_room);
1111 if ( (!strcasecmp(sc_type, "ignet_push_share"))
1112 && (!strcasecmp(sc_node, target_node)) ) {
1115 /* Concise syntax because we don't need a full linked-list */
1116 memset(&sc, 0, sizeof(struct SpoolControl));
1117 sc.ignet_push_shares = (struct maplist *)
1118 malloc(sizeof(struct maplist));
1119 sc.ignet_push_shares->next = NULL;
1120 safestrncpy(sc.ignet_push_shares->remote_nodename,
1122 sizeof sc.ignet_push_shares->remote_nodename);
1123 safestrncpy(sc.ignet_push_shares->remote_roomname,
1125 sizeof sc.ignet_push_shares->remote_roomname);
1129 end_critical_section(S_NETCONFIGS);
1131 if (!found_node) return(-1);
1133 /* Send ALL messages */
1134 num_spooled = CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL,
1135 network_spool_msg, &sc);
1137 /* Concise cleanup because we know there's only one node in the sc */
1138 free(sc.ignet_push_shares);
1140 lprintf(CTDL_NOTICE, "Synchronized %d messages to <%s>\n",
1141 num_spooled, target_node);
1142 return(num_spooled);
1147 * Implements the NSYN command
1149 void cmd_nsyn(char *argbuf) {
1151 char target_node[256];
1153 if (CtdlAccessCheck(ac_aide)) return;
1155 extract_token(target_node, argbuf, 0, '|', sizeof target_node);
1156 num_spooled = network_sync_to(target_node);
1157 if (num_spooled >= 0) {
1158 cprintf("%d Spooled %d messages.\n", CIT_OK, num_spooled);
1161 cprintf("%d No such room/node share exists.\n",
1162 ERROR + ROOM_NOT_FOUND);
1169 * Batch up and send all outbound traffic from the current room
1171 void network_queue_room(struct ctdlroom *qrbuf, void *data) {
1172 struct RoomProcList *ptr;
1174 ptr = (struct RoomProcList *) malloc(sizeof (struct RoomProcList));
1175 if (ptr == NULL) return;
1177 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
1178 begin_critical_section(S_RPLIST);
1181 end_critical_section(S_RPLIST);
1184 void destroy_network_queue_room(void)
1186 struct RoomProcList *cur, *p;
1187 struct NetMap *nmcur, *nmp;
1190 begin_critical_section(S_RPLIST);
1198 end_critical_section(S_RPLIST);
1201 while (nmcur != NULL)
1208 if (working_ignetcfg != NULL)
1209 free (working_ignetcfg);
1210 working_ignetcfg = NULL;
1215 * Learn topology from path fields
1217 void network_learn_topology(char *node, char *path) {
1219 struct NetMap *nmptr;
1221 strcpy(nexthop, "");
1223 if (num_tokens(path, '!') < 3) return;
1224 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
1225 if (!strcasecmp(nmptr->nodename, node)) {
1226 extract_token(nmptr->nexthop, path, 0, '!', sizeof nmptr->nexthop);
1227 nmptr->lastcontact = time(NULL);
1233 /* If we got here then it's not in the map, so add it. */
1234 nmptr = (struct NetMap *) malloc(sizeof (struct NetMap));
1235 strcpy(nmptr->nodename, node);
1236 nmptr->lastcontact = time(NULL);
1237 extract_token(nmptr->nexthop, path, 0, '!', sizeof nmptr->nexthop);
1238 nmptr->next = the_netmap;
1247 * Bounce a message back to the sender
1249 void network_bounce(struct CtdlMessage *msg, char *reason) {
1250 char *oldpath = NULL;
1252 char bouncesource[SIZ];
1253 char recipient[SIZ];
1254 struct recptypes *valid = NULL;
1255 char force_room[ROOMNAMELEN];
1256 static int serialnum = 0;
1259 lprintf(CTDL_DEBUG, "entering network_bounce()\n");
1261 if (msg == NULL) return;
1263 snprintf(bouncesource, sizeof bouncesource, "%s@%s", BOUNCESOURCE, config.c_nodename);
1266 * Give it a fresh message ID
1268 if (msg->cm_fields['I'] != NULL) {
1269 free(msg->cm_fields['I']);
1271 snprintf(buf, sizeof buf, "%ld.%04lx.%04x@%s",
1272 (long)time(NULL), (long)getpid(), ++serialnum, config.c_fqdn);
1273 msg->cm_fields['I'] = strdup(buf);
1276 * FIXME ... right now we're just sending a bounce; we really want to
1277 * include the text of the bounced message.
1279 if (msg->cm_fields['M'] != NULL) {
1280 free(msg->cm_fields['M']);
1282 msg->cm_fields['M'] = strdup(reason);
1283 msg->cm_format_type = 0;
1286 * Turn the message around
1288 if (msg->cm_fields['R'] == NULL) {
1289 free(msg->cm_fields['R']);
1292 if (msg->cm_fields['D'] == NULL) {
1293 free(msg->cm_fields['D']);
1296 snprintf(recipient, sizeof recipient, "%s@%s",
1297 msg->cm_fields['A'], msg->cm_fields['N']);
1299 if (msg->cm_fields['A'] == NULL) {
1300 free(msg->cm_fields['A']);
1303 if (msg->cm_fields['N'] == NULL) {
1304 free(msg->cm_fields['N']);
1307 if (msg->cm_fields['U'] == NULL) {
1308 free(msg->cm_fields['U']);
1311 msg->cm_fields['A'] = strdup(BOUNCESOURCE);
1312 msg->cm_fields['N'] = strdup(config.c_nodename);
1313 msg->cm_fields['U'] = strdup("Delivery Status Notification (Failure)");
1315 /* prepend our node to the path */
1316 if (msg->cm_fields['P'] != NULL) {
1317 oldpath = msg->cm_fields['P'];
1318 msg->cm_fields['P'] = NULL;
1321 oldpath = strdup("unknown_user");
1323 size = strlen(oldpath) + SIZ;
1324 msg->cm_fields['P'] = malloc(size);
1325 snprintf(msg->cm_fields['P'], size, "%s!%s", config.c_nodename, oldpath);
1328 /* Now submit the message */
1329 valid = validate_recipients(recipient);
1330 if (valid != NULL) if (valid->num_error != 0) {
1331 free_recipients(valid);
1334 if ( (valid == NULL) || (!strcasecmp(recipient, bouncesource)) ) {
1335 strcpy(force_room, config.c_aideroom);
1338 strcpy(force_room, "");
1340 if ( (valid == NULL) && (strlen(force_room) == 0) ) {
1341 strcpy(force_room, config.c_aideroom);
1343 CtdlSubmitMsg(msg, valid, force_room);
1346 if (valid != NULL) free_recipients(valid);
1347 CtdlFreeMessage(msg);
1348 lprintf(CTDL_DEBUG, "leaving network_bounce()\n");
1355 * Process a buffer containing a single message from a single file
1356 * from the inbound queue
1358 void network_process_buffer(char *buffer, long size) {
1359 struct CtdlMessage *msg = NULL;
1362 struct recptypes *recp = NULL;
1363 char target_room[ROOMNAMELEN];
1364 struct ser_ret sermsg;
1365 char *oldpath = NULL;
1369 unsigned char firstbyte;
1370 unsigned char lastbyte;
1372 /* Validate just a little bit. First byte should be FF and
1373 * last byte should be 00.
1375 memcpy(&firstbyte, &buffer[0], 1);
1376 memcpy(&lastbyte, &buffer[size-1], 1);
1377 if ( (firstbyte != 255) || (lastbyte != 0) ) {
1378 lprintf(CTDL_ERR, "Corrupt message! Ignoring.\n");
1382 /* Set default target room to trash */
1383 strcpy(target_room, TWITROOM);
1385 /* Load the message into memory */
1386 msg = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
1387 memset(msg, 0, sizeof(struct CtdlMessage));
1388 msg->cm_magic = CTDLMESSAGE_MAGIC;
1389 msg->cm_anon_type = buffer[1];
1390 msg->cm_format_type = buffer[2];
1392 for (pos = 3; pos < size; ++pos) {
1393 field = buffer[pos];
1394 msg->cm_fields[field] = strdup(&buffer[pos+1]);
1395 pos = pos + strlen(&buffer[(int)pos]);
1398 /* Check for message routing */
1399 if (msg->cm_fields['D'] != NULL) {
1400 if (strcasecmp(msg->cm_fields['D'], config.c_nodename)) {
1402 /* route the message */
1403 strcpy(nexthop, "");
1404 if (is_valid_node(nexthop, NULL,
1405 msg->cm_fields['D']) == 0) {
1407 /* prepend our node to the path */
1408 if (msg->cm_fields['P'] != NULL) {
1409 oldpath = msg->cm_fields['P'];
1410 msg->cm_fields['P'] = NULL;
1413 oldpath = strdup("unknown_user");
1415 size = strlen(oldpath) + SIZ;
1416 msg->cm_fields['P'] = malloc(size);
1417 snprintf(msg->cm_fields['P'], size, "%s!%s",
1418 config.c_nodename, oldpath);
1421 /* serialize the message */
1422 serialize_message(&sermsg, msg);
1425 if (strlen(nexthop) == 0) {
1426 strcpy(nexthop, msg->cm_fields['D']);
1433 lprintf(CTDL_DEBUG, "Appending to %s\n", filename);
1434 fp = fopen(filename, "ab");
1441 lprintf(CTDL_ERR, "%s: %s\n", filename, strerror(errno));
1444 CtdlFreeMessage(msg);
1448 else { /* invalid destination node name */
1451 "A message you sent could not be delivered due to an invalid destination node"
1452 " name. Please check the address and try sending the message again.\n");
1461 * Check to see if we already have a copy of this message, and
1462 * abort its processing if so. (We used to post a warning to Aide>
1463 * every time this happened, but the network is now so densely
1464 * connected that it's inevitable.)
1466 if (network_usetable(msg) != 0) {
1470 /* Learn network topology from the path */
1471 if ((msg->cm_fields['N'] != NULL) && (msg->cm_fields['P'] != NULL)) {
1472 network_learn_topology(msg->cm_fields['N'],
1473 msg->cm_fields['P']);
1476 /* Is the sending node giving us a very persuasive suggestion about
1477 * which room this message should be saved in? If so, go with that.
1479 if (msg->cm_fields['C'] != NULL) {
1480 safestrncpy(target_room,
1481 msg->cm_fields['C'],
1482 sizeof target_room);
1485 /* Otherwise, does it have a recipient? If so, validate it... */
1486 else if (msg->cm_fields['R'] != NULL) {
1487 recp = validate_recipients(msg->cm_fields['R']);
1488 if (recp != NULL) if (recp->num_error != 0) {
1490 "A message you sent could not be delivered due to an invalid address.\n"
1491 "Please check the address and try sending the message again.\n");
1493 free_recipients(recp);
1496 strcpy(target_room, ""); /* no target room if mail */
1499 /* Our last shot at finding a home for this message is to see if
1500 * it has the O field (Originating room) set.
1502 else if (msg->cm_fields['O'] != NULL) {
1503 safestrncpy(target_room,
1504 msg->cm_fields['O'],
1505 sizeof target_room);
1508 /* Strip out fields that are only relevant during transit */
1509 if (msg->cm_fields['D'] != NULL) {
1510 free(msg->cm_fields['D']);
1511 msg->cm_fields['D'] = NULL;
1513 if (msg->cm_fields['C'] != NULL) {
1514 free(msg->cm_fields['C']);
1515 msg->cm_fields['C'] = NULL;
1518 /* save the message into a room */
1519 if (PerformNetprocHooks(msg, target_room) == 0) {
1520 msg->cm_flags = CM_SKIP_HOOKS;
1521 CtdlSubmitMsg(msg, recp, target_room);
1523 CtdlFreeMessage(msg);
1524 free_recipients(recp);
1529 * Process a single message from a single file from the inbound queue
1531 void network_process_message(FILE *fp, long msgstart, long msgend) {
1536 hold_pos = ftell(fp);
1537 size = msgend - msgstart + 1;
1538 buffer = malloc(size);
1539 if (buffer != NULL) {
1540 fseek(fp, msgstart, SEEK_SET);
1541 fread(buffer, size, 1, fp);
1542 network_process_buffer(buffer, size);
1546 fseek(fp, hold_pos, SEEK_SET);
1551 * Process a single file from the inbound queue
1553 void network_process_file(char *filename) {
1555 long msgstart = (-1L);
1556 long msgend = (-1L);
1561 fp = fopen(filename, "rb");
1563 lprintf(CTDL_CRIT, "Error opening %s: %s\n",
1564 filename, strerror(errno));
1568 lprintf(CTDL_INFO, "network: processing <%s>\n", filename);
1570 /* Look for messages in the data stream and break them out */
1571 while (ch = getc(fp), ch >= 0) {
1574 if (msgstart >= 0L) {
1575 msgend = msgcur - 1;
1576 network_process_message(fp, msgstart, msgend);
1584 msgend = msgcur - 1;
1585 if (msgstart >= 0L) {
1586 network_process_message(fp, msgstart, msgend);
1595 * Process anything in the inbound queue
1597 void network_do_spoolin(void) {
1600 struct stat statbuf;
1602 static time_t last_spoolin_mtime = 0L;
1605 * Check the spoolin directory's modification time. If it hasn't
1606 * been touched, we don't need to scan it.
1608 if (stat(ctdl_netin_dir, &statbuf)) return;
1609 if (statbuf.st_mtime == last_spoolin_mtime) {
1610 lprintf(CTDL_DEBUG, "network: nothing in inbound queue\n");
1613 last_spoolin_mtime = statbuf.st_mtime;
1614 lprintf(CTDL_DEBUG, "network: processing inbound queue\n");
1617 * Ok, there's something interesting in there, so scan it.
1619 dp = opendir(ctdl_netin_dir);
1620 if (dp == NULL) return;
1622 while (d = readdir(dp), d != NULL) {
1623 if ((strcmp(d->d_name, ".")) && (strcmp(d->d_name, ".."))) {
1629 network_process_file(filename);
1637 * Delete any files in the outbound queue that were intended
1638 * to be sent to nodes which no longer exist.
1640 void network_purge_spoolout(void) {
1647 dp = opendir(ctdl_netout_dir);
1648 if (dp == NULL) return;
1650 while (d = readdir(dp), d != NULL) {
1651 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
1659 strcpy(nexthop, "");
1660 i = is_valid_node(nexthop, NULL, d->d_name);
1662 if ( (i != 0) || (strlen(nexthop) > 0) ) {
1673 * receive network spool from the remote system
1675 void receive_spool(int sock, char *remote_nodename) {
1677 long bytes_received;
1679 static char pbuf[IGNET_PACKET_SIZE];
1680 char tempfilename[PATH_MAX];
1684 CtdlMakeTempFileName(tempfilename, sizeof tempfilename);
1685 if (sock_puts(sock, "NDOP") < 0) return;
1686 if (sock_gets(sock, buf) < 0) return;
1687 lprintf(CTDL_DEBUG, "<%s\n", buf);
1688 if (buf[0] != '2') {
1691 download_len = extract_long(&buf[4], 0);
1693 bytes_received = 0L;
1694 fp = fopen(tempfilename, "w");
1696 lprintf(CTDL_CRIT, "cannot open download file locally: %s\n",
1701 while (bytes_received < download_len) {
1702 snprintf(buf, sizeof buf, "READ %ld|%ld",
1704 ((download_len - bytes_received > IGNET_PACKET_SIZE)
1705 ? IGNET_PACKET_SIZE : (download_len - bytes_received)));
1706 if (sock_puts(sock, buf) < 0) {
1708 unlink(tempfilename);
1711 if (sock_gets(sock, buf) < 0) {
1713 unlink(tempfilename);
1716 if (buf[0] == '6') {
1717 plen = extract_long(&buf[4], 0);
1718 if (sock_read(sock, pbuf, plen) < 0) {
1720 unlink(tempfilename);
1723 fwrite((char *) pbuf, plen, 1, fp);
1724 bytes_received = bytes_received + plen;
1729 if (sock_puts(sock, "CLOS") < 0) {
1730 unlink(tempfilename);
1733 if (sock_gets(sock, buf) < 0) {
1734 unlink(tempfilename);
1737 if (download_len > 0)
1738 lprintf(CTDL_NOTICE, "Received %ld octets from <%s>",
1739 download_len, remote_nodename);
1740 lprintf(CTDL_DEBUG, "%s", buf);
1741 /* TODO: make move inline. forking is verry expensive. */
1755 * transmit network spool to the remote system
1757 void transmit_spool(int sock, char *remote_nodename)
1762 long bytes_to_write, thisblock, bytes_written;
1766 if (sock_puts(sock, "NUOP") < 0) return;
1767 if (sock_gets(sock, buf) < 0) return;
1768 lprintf(CTDL_DEBUG, "<%s\n", buf);
1769 if (buf[0] != '2') {
1773 snprintf(sfname, sizeof sfname,
1777 fd = open(sfname, O_RDONLY);
1779 if (errno != ENOENT) {
1780 lprintf(CTDL_CRIT, "cannot open upload file locally: %s\n",
1786 while (plen = (long) read(fd, pbuf, IGNET_PACKET_SIZE), plen > 0L) {
1787 bytes_to_write = plen;
1788 while (bytes_to_write > 0L) {
1789 snprintf(buf, sizeof buf, "WRIT %ld", bytes_to_write);
1790 if (sock_puts(sock, buf) < 0) {
1794 if (sock_gets(sock, buf) < 0) {
1798 thisblock = atol(&buf[4]);
1799 if (buf[0] == '7') {
1800 if (sock_write(sock, pbuf,
1801 (int) thisblock) < 0) {
1805 bytes_to_write -= thisblock;
1806 bytes_written += thisblock;
1815 if (sock_puts(sock, "UCLS 1") < 0) return;
1816 if (sock_gets(sock, buf) < 0) return;
1817 lprintf(CTDL_NOTICE, "Sent %ld octets to <%s>\n",
1818 bytes_written, remote_nodename);
1819 lprintf(CTDL_DEBUG, "<%s\n", buf);
1820 if (buf[0] == '2') {
1821 lprintf(CTDL_DEBUG, "Removing <%s>\n", sfname);
1829 * Poll one Citadel node (called by network_poll_other_citadel_nodes() below)
1831 void network_poll_node(char *node, char *secret, char *host, char *port) {
1835 if (network_talking_to(node, NTT_CHECK)) return;
1836 network_talking_to(node, NTT_ADD);
1837 lprintf(CTDL_NOTICE, "Connecting to <%s> at %s:%s\n", node, host, port);
1839 sock = sock_connect(host, port, "tcp");
1841 lprintf(CTDL_ERR, "Could not connect: %s\n", strerror(errno));
1842 network_talking_to(node, NTT_REMOVE);
1846 lprintf(CTDL_DEBUG, "Connected!\n");
1848 /* Read the server greeting */
1849 if (sock_gets(sock, buf) < 0) goto bail;
1850 lprintf(CTDL_DEBUG, ">%s\n", buf);
1852 /* Identify ourselves */
1853 snprintf(buf, sizeof buf, "NETP %s|%s", config.c_nodename, secret);
1854 lprintf(CTDL_DEBUG, "<%s\n", buf);
1855 if (sock_puts(sock, buf) <0) goto bail;
1856 if (sock_gets(sock, buf) < 0) goto bail;
1857 lprintf(CTDL_DEBUG, ">%s\n", buf);
1858 if (buf[0] != '2') goto bail;
1860 /* At this point we are authenticated. */
1861 receive_spool(sock, node);
1862 transmit_spool(sock, node);
1864 sock_puts(sock, "QUIT");
1865 bail: sock_close(sock);
1866 network_talking_to(node, NTT_REMOVE);
1872 * Poll other Citadel nodes and transfer inbound/outbound network data.
1873 * Set "full" to nonzero to force a poll of every node, or to zero to poll
1874 * only nodes to which we have data to send.
1876 void network_poll_other_citadel_nodes(int full_poll) {
1884 char spoolfile[256];
1886 if (working_ignetcfg == NULL) {
1887 lprintf(CTDL_DEBUG, "No nodes defined - not polling\n");
1891 /* Use the string tokenizer to grab one line at a time */
1892 for (i=0; i<num_tokens(working_ignetcfg, '\n'); ++i) {
1893 extract_token(linebuf, working_ignetcfg, i, '\n', sizeof linebuf);
1894 extract_token(node, linebuf, 0, '|', sizeof node);
1895 extract_token(secret, linebuf, 1, '|', sizeof secret);
1896 extract_token(host, linebuf, 2, '|', sizeof host);
1897 extract_token(port, linebuf, 3, '|', sizeof port);
1898 if ( (strlen(node) > 0) && (strlen(secret) > 0)
1899 && (strlen(host) > 0) && strlen(port) > 0) {
1907 if (access(spoolfile, R_OK) == 0) {
1912 network_poll_node(node, secret, host, port);
1923 * It's ok if these directories already exist. Just fail silently.
1925 void create_spool_dirs(void) {
1926 mkdir(ctdl_spool_dir, 0700);
1927 chown(ctdl_spool_dir, CTDLUID, (-1));
1928 mkdir(ctdl_netin_dir, 0700);
1929 chown(ctdl_netin_dir, CTDLUID, (-1));
1930 mkdir(ctdl_netout_dir, 0700);
1931 chown(ctdl_netout_dir, CTDLUID, (-1));
1939 * network_do_queue()
1941 * Run through the rooms doing various types of network stuff.
1943 void network_do_queue(void) {
1944 static time_t last_run = 0L;
1945 struct RoomProcList *ptr;
1946 int full_processing = 1;
1949 * Run the full set of processing tasks no more frequently
1950 * than once every n seconds
1952 if ( (time(NULL) - last_run) < config.c_net_freq ) {
1953 full_processing = 0;
1957 * This is a simple concurrency check to make sure only one queue run
1958 * is done at a time. We could do this with a mutex, but since we
1959 * don't really require extremely fine granularity here, we'll do it
1960 * with a static variable instead.
1962 if (doing_queue) return;
1965 /* Load the IGnet Configuration into memory */
1966 load_working_ignetcfg();
1969 * Poll other Citadel nodes. Maybe. If "full_processing" is set
1970 * then we poll everyone. Otherwise we only poll nodes we have stuff
1973 network_poll_other_citadel_nodes(full_processing);
1976 * Load the network map and filter list into memory.
1979 filterlist = load_filter_list();
1982 * Go ahead and run the queue
1984 if (full_processing) {
1985 lprintf(CTDL_DEBUG, "network: loading outbound queue\n");
1986 ForEachRoom(network_queue_room, NULL);
1989 if (rplist != NULL) {
1990 lprintf(CTDL_DEBUG, "network: running outbound queue\n");
1991 while (rplist != NULL) {
1992 char spoolroomname[ROOMNAMELEN];
1993 safestrncpy(spoolroomname, rplist->name, sizeof spoolroomname);
1994 begin_critical_section(S_RPLIST);
1996 /* pop this record off the list */
1998 rplist = rplist->next;
2001 /* invalidate any duplicate entries to prevent double processing */
2002 for (ptr=rplist; ptr!=NULL; ptr=ptr->next) {
2003 if (!strcasecmp(ptr->name, spoolroomname)) {
2008 end_critical_section(S_RPLIST);
2009 if (spoolroomname[0] != 0) {
2010 network_spoolout_room(spoolroomname);
2015 /* If there is anything in the inbound queue, process it */
2016 network_do_spoolin();
2018 /* Save the network map back to disk */
2019 write_network_map();
2021 /* Free the filter list in memory */
2022 free_filter_list(filterlist);
2025 network_purge_spoolout();
2027 lprintf(CTDL_DEBUG, "network: queue run completed\n");
2029 if (full_processing) {
2030 last_run = time(NULL);
2038 * cmd_netp() - authenticate to the server as another Citadel node polling
2039 * for network traffic
2041 void cmd_netp(char *cmdbuf)
2051 extract_token(node, cmdbuf, 0, '|', sizeof node);
2052 extract_token(pass, cmdbuf, 1, '|', sizeof pass);
2055 lprintf(CTDL_WARNING, "Network node <%s> refused - spooling", node);
2056 cprintf("%d spooling - try again in a few minutes\n",
2057 ERROR + RESOURCE_BUSY);
2061 /* load the IGnet Configuration to check node validity */
2062 load_working_ignetcfg();
2063 v = is_valid_node(nexthop, secret, node);
2066 lprintf(CTDL_WARNING, "Unknown node <%s>\n", node);
2067 cprintf("%d authentication failed\n",
2068 ERROR + PASSWORD_REQUIRED);
2072 if (strcasecmp(pass, secret)) {
2073 lprintf(CTDL_WARNING, "Bad password for network node <%s>", node);
2074 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
2078 if (network_talking_to(node, NTT_CHECK)) {
2079 lprintf(CTDL_WARNING, "Duplicate session for network node <%s>", node);
2080 cprintf("%d Already talking to %s right now\n", ERROR + RESOURCE_BUSY, node);
2084 safestrncpy(CC->net_node, node, sizeof CC->net_node);
2085 network_talking_to(node, NTT_ADD);
2086 lprintf(CTDL_NOTICE, "Network node <%s> logged in\n", CC->net_node);
2087 cprintf("%d authenticated as network node '%s'\n", CIT_OK,
2092 * Module entry point
2094 char *serv_network_init(void)
2096 create_spool_dirs();
2097 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
2098 CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");
2099 CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");
2100 CtdlRegisterProtoHook(cmd_nsyn, "NSYN", "Synchronize room to node");
2101 CtdlRegisterSessionHook(network_do_queue, EVT_TIMER);