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"
73 /* Nonzero while we are doing 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 CtdlMakeTempFileName(tempfilename, 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 * Deliver digest messages
425 void network_deliver_digest(struct SpoolControl *sc) {
428 struct CtdlMessage *msg;
432 size_t instr_len = SIZ;
433 struct CtdlMessage *imsg;
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;
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,
570 "remote|%s|0||\n", nptr->name);
574 * Generate a message from the instructions
576 imsg = malloc(sizeof(struct CtdlMessage));
577 memset(imsg, 0, sizeof(struct CtdlMessage));
578 imsg->cm_magic = CTDLMESSAGE_MAGIC;
579 imsg->cm_anon_type = MES_NORMAL;
580 imsg->cm_format_type = FMT_RFC822;
581 imsg->cm_fields['A'] = strdup("Citadel");
582 imsg->cm_fields['M'] = instr;
584 /* Save delivery instructions in spoolout room */
585 CtdlSubmitMsg(imsg, NULL, SMTP_SPOOLOUT_ROOM);
586 CtdlFreeMessage(imsg);
593 * Spools out one message from the list.
595 void network_spool_msg(long msgnum, void *userdata) {
596 struct SpoolControl *sc;
598 char *newpath = NULL;
599 size_t instr_len = SIZ;
600 struct CtdlMessage *msg = NULL;
601 struct namelist *nptr;
602 struct maplist *mptr;
603 struct ser_ret sermsg;
609 int delete_after_send = 0; /* Set to 1 to delete after spooling */
610 int ok_to_participate = 0;
611 struct recptypes *valid;
613 sc = (struct SpoolControl *)userdata;
616 * Process mailing list recipients
619 if (sc->listrecps != NULL) {
620 /* Fetch the message. We're going to need to modify it
621 * in order to insert the [list name] in it, etc.
623 msg = CtdlFetchMessage(msgnum, 1);
626 /* Prepend "[List name]" to the subject */
627 if (msg->cm_fields['U'] == NULL) {
628 msg->cm_fields['U'] = strdup("(no subject)");
630 snprintf(buf, sizeof buf, "[%s] %s", CC->room.QRname, msg->cm_fields['U']);
631 free(msg->cm_fields['U']);
632 msg->cm_fields['U'] = strdup(buf);
634 /* Set the recipient of the list message to the
635 * email address of the room itself.
636 * FIXME ... I want to be able to pick any address
638 if (msg->cm_fields['R'] != NULL) {
639 free(msg->cm_fields['R']);
641 msg->cm_fields['R'] = malloc(256);
642 snprintf(msg->cm_fields['R'], 256,
643 "room_%s@%s", CC->room.QRname,
645 for (i=0; i<strlen(msg->cm_fields['R']); ++i) {
646 if (isspace(msg->cm_fields['R'][i])) {
647 msg->cm_fields['R'][i] = '_';
651 /* Handle delivery */
652 network_deliver_list(msg, sc);
653 CtdlFreeMessage(msg);
658 * Process digest recipients
660 if ((sc->digestrecps != NULL) && (sc->digestfp != NULL)) {
661 msg = CtdlFetchMessage(msgnum, 1);
663 fprintf(sc->digestfp, " -----------------------------------"
664 "------------------------------------"
666 fprintf(sc->digestfp, "From: ");
667 if (msg->cm_fields['A'] != NULL) {
668 fprintf(sc->digestfp, "%s ", msg->cm_fields['A']);
670 if (msg->cm_fields['F'] != NULL) {
671 fprintf(sc->digestfp, "<%s> ", msg->cm_fields['F']);
673 else if (msg->cm_fields['N'] != NULL) {
674 fprintf(sc->digestfp, "@%s ", msg->cm_fields['N']);
676 fprintf(sc->digestfp, "\n");
677 if (msg->cm_fields['U'] != NULL) {
678 fprintf(sc->digestfp, "Subject: %s\n", msg->cm_fields['U']);
681 CC->redirect_buffer = malloc(SIZ);
682 CC->redirect_len = 0;
683 CC->redirect_alloc = SIZ;
685 safestrncpy(CC->preferred_formats, "text/plain", sizeof CC->preferred_formats);
686 CtdlOutputPreLoadedMsg(msg, MT_CITADEL, HEADERS_NONE, 0, 0);
688 striplt(CC->redirect_buffer);
689 fprintf(sc->digestfp, "\n%s\n", CC->redirect_buffer);
691 free(CC->redirect_buffer);
692 CC->redirect_buffer = NULL;
693 CC->redirect_len = 0;
694 CC->redirect_alloc = 0;
696 sc->num_msgs_spooled += 1;
702 * Process client-side list participations for this room
705 if (sc->participates != NULL) {
706 msg = CtdlFetchMessage(msgnum, 1);
709 /* Only send messages which originated on our own Citadel
710 * network, otherwise we'll end up sending the remote
711 * mailing list's messages back to it, which is rude...
713 ok_to_participate = 0;
714 if (msg->cm_fields['N'] != NULL) {
715 if (!strcasecmp(msg->cm_fields['N'], config.c_nodename)) {
716 ok_to_participate = 1;
718 if (is_valid_node(NULL, NULL, msg->cm_fields['N']) == 0) {
719 ok_to_participate = 1;
722 if (ok_to_participate) {
723 if (msg->cm_fields['F'] != NULL) {
724 free(msg->cm_fields['F']);
726 msg->cm_fields['F'] = malloc(SIZ);
727 /* Replace the Internet email address of the actual
728 * author with the email address of the room itself,
729 * so the remote listserv doesn't reject us.
730 * FIXME ... I want to be able to pick any address
732 snprintf(msg->cm_fields['F'], SIZ,
733 "room_%s@%s", CC->room.QRname,
735 for (i=0; i<strlen(msg->cm_fields['F']); ++i) {
736 if (isspace(msg->cm_fields['F'][i])) {
737 msg->cm_fields['F'][i] = '_';
742 * Figure out how big a buffer we need to allocate
744 for (nptr = sc->participates; nptr != NULL; nptr = nptr->next) {
746 if (msg->cm_fields['R'] == NULL) {
747 free(msg->cm_fields['R']);
749 msg->cm_fields['R'] = strdup(nptr->name);
751 valid = validate_recipients(nptr->name);
752 CtdlSubmitMsg(msg, valid, "");
757 CtdlFreeMessage(msg);
762 * Process IGnet push shares
764 if (sc->ignet_push_shares != NULL) {
766 msg = CtdlFetchMessage(msgnum, 1);
770 /* Prepend our node name to the Path field whenever
771 * sending a message to another IGnet node
773 if (msg->cm_fields['P'] == NULL) {
774 msg->cm_fields['P'] = strdup("username");
776 newpath_len = strlen(msg->cm_fields['P']) +
777 strlen(config.c_nodename) + 2;
778 newpath = malloc(newpath_len);
779 snprintf(newpath, newpath_len, "%s!%s",
780 config.c_nodename, msg->cm_fields['P']);
781 free(msg->cm_fields['P']);
782 msg->cm_fields['P'] = newpath;
785 * Determine if this message is set to be deleted
786 * after sending out on the network
788 if (msg->cm_fields['S'] != NULL) {
789 if (!strcasecmp(msg->cm_fields['S'],
791 delete_after_send = 1;
795 /* Now send it to every node */
796 for (mptr = sc->ignet_push_shares; mptr != NULL;
801 /* Check for valid node name */
802 if (is_valid_node(NULL, NULL, mptr->remote_nodename) != 0) {
803 lprintf(CTDL_ERR, "Invalid node <%s>\n",
804 mptr->remote_nodename);
808 /* Check for split horizon */
809 lprintf(CTDL_DEBUG, "Path is %s\n", msg->cm_fields['P']);
810 bang = num_tokens(msg->cm_fields['P'], '!');
811 if (bang > 1) for (i=0; i<(bang-1); ++i) {
812 extract_token(buf, msg->cm_fields['P'],
814 if (!strcasecmp(buf, mptr->remote_nodename)) {
819 /* Send the message */
823 * Force the message to appear in the correct room
824 * on the far end by setting the C field correctly
826 if (msg->cm_fields['C'] != NULL) {
827 free(msg->cm_fields['C']);
829 if (strlen(mptr->remote_roomname) > 0) {
830 msg->cm_fields['C'] = strdup(mptr->remote_roomname);
833 msg->cm_fields['C'] = strdup(CC->room.QRname);
836 /* serialize it for transmission */
837 serialize_message(&sermsg, msg);
839 /* write it to the spool file */
840 snprintf(filename, sizeof filename,
841 #ifndef HAVE_SPOOL_DIR
845 #endif /* HAVE_SPOOL_DIR */
846 "/network/spoolout/%s",
847 mptr->remote_nodename);
848 lprintf(CTDL_DEBUG, "Appending to %s\n", filename);
849 fp = fopen(filename, "ab");
856 lprintf(CTDL_ERR, "%s: %s\n", filename, strerror(errno));
859 /* free the serialized version */
863 CtdlFreeMessage(msg);
867 /* update lastsent */
868 sc->lastsent = msgnum;
870 /* Delete this message if delete-after-send is set */
871 if (delete_after_send) {
872 CtdlDeleteMessages(CC->room.QRname, msgnum, "", 0);
879 * Batch up and send all outbound traffic from the current room
881 void network_spoolout_room(char *room_to_spool) {
886 char roomname[ROOMNAMELEN];
889 struct SpoolControl sc;
890 struct namelist *nptr = NULL;
891 struct maplist *mptr = NULL;
894 int skipthisline = 0;
897 if (getroom(&CC->room, room_to_spool) != 0) {
898 lprintf(CTDL_CRIT, "ERROR: cannot load <%s>\n", room_to_spool);
902 memset(&sc, 0, sizeof(struct SpoolControl));
903 assoc_file_name(filename, sizeof filename, &CC->room, "netconfigs");
905 begin_critical_section(S_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,
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, "netconfigs");
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,
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);
1184 * Learn topology from path fields
1186 void network_learn_topology(char *node, char *path) {
1188 struct NetMap *nmptr;
1190 strcpy(nexthop, "");
1192 if (num_tokens(path, '!') < 3) return;
1193 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
1194 if (!strcasecmp(nmptr->nodename, node)) {
1195 extract_token(nmptr->nexthop, path, 0, '!', sizeof nmptr->nexthop);
1196 nmptr->lastcontact = time(NULL);
1202 /* If we got here then it's not in the map, so add it. */
1203 nmptr = (struct NetMap *) malloc(sizeof (struct NetMap));
1204 strcpy(nmptr->nodename, node);
1205 nmptr->lastcontact = time(NULL);
1206 extract_token(nmptr->nexthop, path, 0, '!', sizeof nmptr->nexthop);
1207 nmptr->next = the_netmap;
1216 * Bounce a message back to the sender
1218 void network_bounce(struct CtdlMessage *msg, char *reason) {
1219 char *oldpath = NULL;
1221 char bouncesource[SIZ];
1222 char recipient[SIZ];
1223 struct recptypes *valid = NULL;
1224 char force_room[ROOMNAMELEN];
1225 static int serialnum = 0;
1228 lprintf(CTDL_DEBUG, "entering network_bounce()\n");
1230 if (msg == NULL) return;
1232 snprintf(bouncesource, sizeof bouncesource, "%s@%s", BOUNCESOURCE, config.c_nodename);
1235 * Give it a fresh message ID
1237 if (msg->cm_fields['I'] != NULL) {
1238 free(msg->cm_fields['I']);
1240 snprintf(buf, sizeof buf, "%ld.%04lx.%04x@%s",
1241 (long)time(NULL), (long)getpid(), ++serialnum, config.c_fqdn);
1242 msg->cm_fields['I'] = strdup(buf);
1245 * FIXME ... right now we're just sending a bounce; we really want to
1246 * include the text of the bounced message.
1248 if (msg->cm_fields['M'] != NULL) {
1249 free(msg->cm_fields['M']);
1251 msg->cm_fields['M'] = strdup(reason);
1252 msg->cm_format_type = 0;
1255 * Turn the message around
1257 if (msg->cm_fields['R'] == NULL) {
1258 free(msg->cm_fields['R']);
1261 if (msg->cm_fields['D'] == NULL) {
1262 free(msg->cm_fields['D']);
1265 snprintf(recipient, sizeof recipient, "%s@%s",
1266 msg->cm_fields['A'], msg->cm_fields['N']);
1268 if (msg->cm_fields['A'] == NULL) {
1269 free(msg->cm_fields['A']);
1272 if (msg->cm_fields['N'] == NULL) {
1273 free(msg->cm_fields['N']);
1276 if (msg->cm_fields['U'] == NULL) {
1277 free(msg->cm_fields['U']);
1280 msg->cm_fields['A'] = strdup(BOUNCESOURCE);
1281 msg->cm_fields['N'] = strdup(config.c_nodename);
1282 msg->cm_fields['U'] = strdup("Delivery Status Notification (Failure)");
1284 /* prepend our node to the path */
1285 if (msg->cm_fields['P'] != NULL) {
1286 oldpath = msg->cm_fields['P'];
1287 msg->cm_fields['P'] = NULL;
1290 oldpath = strdup("unknown_user");
1292 size = strlen(oldpath) + SIZ;
1293 msg->cm_fields['P'] = malloc(size);
1294 snprintf(msg->cm_fields['P'], size, "%s!%s", config.c_nodename, oldpath);
1297 /* Now submit the message */
1298 valid = validate_recipients(recipient);
1299 if (valid != NULL) if (valid->num_error != 0) {
1303 if ( (valid == NULL) || (!strcasecmp(recipient, bouncesource)) ) {
1304 strcpy(force_room, config.c_aideroom);
1307 strcpy(force_room, "");
1309 if ( (valid == NULL) && (strlen(force_room) == 0) ) {
1310 strcpy(force_room, config.c_aideroom);
1312 CtdlSubmitMsg(msg, valid, force_room);
1315 if (valid != NULL) free(valid);
1316 CtdlFreeMessage(msg);
1317 lprintf(CTDL_DEBUG, "leaving network_bounce()\n");
1324 * Process a buffer containing a single message from a single file
1325 * from the inbound queue
1327 void network_process_buffer(char *buffer, long size) {
1328 struct CtdlMessage *msg;
1331 struct recptypes *recp = NULL;
1332 char target_room[ROOMNAMELEN];
1333 struct ser_ret sermsg;
1334 char *oldpath = NULL;
1338 unsigned char firstbyte;
1339 unsigned char lastbyte;
1341 /* Validate just a little bit. First byte should be FF and
1342 * last byte should be 00.
1344 memcpy(&firstbyte, &buffer[0], 1);
1345 memcpy(&lastbyte, &buffer[size-1], 1);
1346 if ( (firstbyte != 255) || (lastbyte != 0) ) {
1347 lprintf(CTDL_ERR, "Corrupt message! Ignoring.\n");
1351 /* Set default target room to trash */
1352 strcpy(target_room, TWITROOM);
1354 /* Load the message into memory */
1355 msg = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
1356 memset(msg, 0, sizeof(struct CtdlMessage));
1357 msg->cm_magic = CTDLMESSAGE_MAGIC;
1358 msg->cm_anon_type = buffer[1];
1359 msg->cm_format_type = buffer[2];
1361 for (pos = 3; pos < size; ++pos) {
1362 field = buffer[pos];
1363 msg->cm_fields[field] = strdup(&buffer[pos+1]);
1364 pos = pos + strlen(&buffer[(int)pos]);
1367 /* Check for message routing */
1368 if (msg->cm_fields['D'] != NULL) {
1369 if (strcasecmp(msg->cm_fields['D'], config.c_nodename)) {
1371 /* route the message */
1372 strcpy(nexthop, "");
1373 if (is_valid_node(nexthop, NULL,
1374 msg->cm_fields['D']) == 0) {
1376 /* prepend our node to the path */
1377 if (msg->cm_fields['P'] != NULL) {
1378 oldpath = msg->cm_fields['P'];
1379 msg->cm_fields['P'] = NULL;
1382 oldpath = strdup("unknown_user");
1384 size = strlen(oldpath) + SIZ;
1385 msg->cm_fields['P'] = malloc(size);
1386 snprintf(msg->cm_fields['P'], size, "%s!%s",
1387 config.c_nodename, oldpath);
1390 /* serialize the message */
1391 serialize_message(&sermsg, msg);
1394 if (strlen(nexthop) == 0) {
1395 strcpy(nexthop, msg->cm_fields['D']);
1397 snprintf(filename, sizeof filename,
1398 #ifndef HAVE_SPOOL_DIR
1402 #endif /* HAVE_SPOOL_DIR */
1403 "/network/spoolout/%s", nexthop);
1404 lprintf(CTDL_DEBUG, "Appending to %s\n", filename);
1405 fp = fopen(filename, "ab");
1412 lprintf(CTDL_ERR, "%s: %s\n", filename, strerror(errno));
1415 CtdlFreeMessage(msg);
1419 else { /* invalid destination node name */
1422 "A message you sent could not be delivered due to an invalid destination node"
1423 " name. Please check the address and try sending the message again.\n");
1432 * Check to see if we already have a copy of this message, and
1433 * abort its processing if so. (We used to post a warning to Aide>
1434 * every time this happened, but the network is now so densely
1435 * connected that it's inevitable.)
1437 if (network_usetable(msg) != 0) {
1441 /* Learn network topology from the path */
1442 if ((msg->cm_fields['N'] != NULL) && (msg->cm_fields['P'] != NULL)) {
1443 network_learn_topology(msg->cm_fields['N'],
1444 msg->cm_fields['P']);
1447 /* Is the sending node giving us a very persuasive suggestion about
1448 * which room this message should be saved in? If so, go with that.
1450 if (msg->cm_fields['C'] != NULL) {
1451 safestrncpy(target_room,
1452 msg->cm_fields['C'],
1453 sizeof target_room);
1456 /* Otherwise, does it have a recipient? If so, validate it... */
1457 else if (msg->cm_fields['R'] != NULL) {
1458 recp = validate_recipients(msg->cm_fields['R']);
1459 if (recp != NULL) if (recp->num_error != 0) {
1461 "A message you sent could not be delivered due to an invalid address.\n"
1462 "Please check the address and try sending the message again.\n");
1467 strcpy(target_room, ""); /* no target room if mail */
1470 /* Our last shot at finding a home for this message is to see if
1471 * it has the O field (Originating room) set.
1473 else if (msg->cm_fields['O'] != NULL) {
1474 safestrncpy(target_room,
1475 msg->cm_fields['O'],
1476 sizeof target_room);
1479 /* Strip out fields that are only relevant during transit */
1480 if (msg->cm_fields['D'] != NULL) {
1481 free(msg->cm_fields['D']);
1482 msg->cm_fields['D'] = NULL;
1484 if (msg->cm_fields['C'] != NULL) {
1485 free(msg->cm_fields['C']);
1486 msg->cm_fields['C'] = NULL;
1489 /* save the message into a room */
1490 if (PerformNetprocHooks(msg, target_room) == 0) {
1491 msg->cm_flags = CM_SKIP_HOOKS;
1492 CtdlSubmitMsg(msg, recp, target_room);
1494 CtdlFreeMessage(msg);
1500 * Process a single message from a single file from the inbound queue
1502 void network_process_message(FILE *fp, long msgstart, long msgend) {
1507 hold_pos = ftell(fp);
1508 size = msgend - msgstart + 1;
1509 buffer = malloc(size);
1510 if (buffer != NULL) {
1511 fseek(fp, msgstart, SEEK_SET);
1512 fread(buffer, size, 1, fp);
1513 network_process_buffer(buffer, size);
1517 fseek(fp, hold_pos, SEEK_SET);
1522 * Process a single file from the inbound queue
1524 void network_process_file(char *filename) {
1526 long msgstart = (-1L);
1527 long msgend = (-1L);
1532 fp = fopen(filename, "rb");
1534 lprintf(CTDL_CRIT, "Error opening %s: %s\n",
1535 filename, strerror(errno));
1539 lprintf(CTDL_INFO, "network: processing <%s>\n", filename);
1541 /* Look for messages in the data stream and break them out */
1542 while (ch = getc(fp), ch >= 0) {
1545 if (msgstart >= 0L) {
1546 msgend = msgcur - 1;
1547 network_process_message(fp, msgstart, msgend);
1555 msgend = msgcur - 1;
1556 if (msgstart >= 0L) {
1557 network_process_message(fp, msgstart, msgend);
1566 * Process anything in the inbound queue
1568 void network_do_spoolin(void) {
1571 struct stat statbuf;
1573 static time_t last_spoolin_mtime = 0L;
1574 const char *spoolin_dirname =
1575 #ifndef HAVE_SPOOL_DIR
1579 #endif /* HAVE_SPOOL_DIR */
1583 * Check the spoolin directory's modification time. If it hasn't
1584 * been touched, we don't need to scan it.
1586 if (stat(spoolin_dirname, &statbuf)) return;
1587 if (statbuf.st_mtime == last_spoolin_mtime) {
1588 lprintf(CTDL_DEBUG, "network: nothing in inbound queue\n");
1591 last_spoolin_mtime = statbuf.st_mtime;
1592 lprintf(CTDL_DEBUG, "network: processing inbound queue\n");
1595 * Ok, there's something interesting in there, so scan it.
1597 dp = opendir(spoolin_dirname);
1598 if (dp == NULL) return;
1600 while (d = readdir(dp), d != NULL) {
1601 if ((strcmp(d->d_name, ".")) && (strcmp(d->d_name, ".."))) {
1602 snprintf(filename, sizeof filename,
1603 #ifndef HAVE_SPOOL_DIR
1607 #endif /* HAVE_SPOOL_DIR */
1608 "/network/spoolin/%s", d->d_name);
1609 network_process_file(filename);
1617 * Delete any files in the outbound queue that were intended
1618 * to be sent to nodes which no longer exist.
1620 void network_purge_spoolout(void) {
1628 #ifndef HAVE_SPOOL_DIR
1632 #endif /* HAVE_SPOOL_DIR */
1633 "/network/spoolout");
1634 if (dp == NULL) return;
1636 while (d = readdir(dp), d != NULL) {
1637 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
1639 snprintf(filename, sizeof filename,
1640 #ifndef HAVE_SPOOL_DIR
1644 #endif /* HAVE_SPOOL_DIR */
1645 "/network/spoolout/%s", d->d_name);
1647 strcpy(nexthop, "");
1648 i = is_valid_node(nexthop, NULL, d->d_name);
1650 if ( (i != 0) || (strlen(nexthop) > 0) ) {
1661 * receive network spool from the remote system
1663 void receive_spool(int sock, char *remote_nodename) {
1665 long bytes_received;
1667 static char pbuf[IGNET_PACKET_SIZE];
1668 char tempfilename[PATH_MAX];
1672 CtdlMakeTempFileName(tempfilename, sizeof tempfilename);
1673 if (sock_puts(sock, "NDOP") < 0) return;
1674 if (sock_gets(sock, buf) < 0) return;
1675 lprintf(CTDL_DEBUG, "<%s\n", buf);
1676 if (buf[0] != '2') {
1679 download_len = extract_long(&buf[4], 0);
1681 bytes_received = 0L;
1682 fp = fopen(tempfilename, "w");
1684 lprintf(CTDL_CRIT, "cannot open download file locally: %s\n",
1689 while (bytes_received < download_len) {
1690 snprintf(buf, sizeof buf, "READ %ld|%ld",
1692 ((download_len - bytes_received > IGNET_PACKET_SIZE)
1693 ? IGNET_PACKET_SIZE : (download_len - bytes_received)));
1694 if (sock_puts(sock, buf) < 0) {
1696 unlink(tempfilename);
1699 if (sock_gets(sock, buf) < 0) {
1701 unlink(tempfilename);
1704 if (buf[0] == '6') {
1705 plen = extract_long(&buf[4], 0);
1706 if (sock_read(sock, pbuf, plen) < 0) {
1708 unlink(tempfilename);
1711 fwrite((char *) pbuf, plen, 1, fp);
1712 bytes_received = bytes_received + plen;
1717 if (sock_puts(sock, "CLOS") < 0) {
1718 unlink(tempfilename);
1721 if (sock_gets(sock, buf) < 0) {
1722 unlink(tempfilename);
1725 if (download_len > 0)
1726 lprintf(CTDL_NOTICE, "Received %ld octets from <%s>",
1727 download_len, remote_nodename);
1728 lprintf(CTDL_DEBUG, "%s", buf);
1729 /* TODO: make move inline. forking is verry expensive. */
1730 snprintf(buf, sizeof buf, "mv %s "
1731 #ifndef HAVE_SPOOL_DIR
1735 #endif /* HAVE_SPOOL_DIR */
1736 "/network/spoolin/%s.%ld",
1737 tempfilename, remote_nodename, (long) getpid());
1744 * transmit network spool to the remote system
1746 void transmit_spool(int sock, char *remote_nodename)
1751 long bytes_to_write, thisblock, bytes_written;
1755 if (sock_puts(sock, "NUOP") < 0) return;
1756 if (sock_gets(sock, buf) < 0) return;
1757 lprintf(CTDL_DEBUG, "<%s\n", buf);
1758 if (buf[0] != '2') {
1762 snprintf(sfname, sizeof sfname,
1763 #ifndef HAVE_SPOOL_DIR
1767 #endif /* HAVE_SPOOL_DIR */
1768 "/network/spoolout/%s", remote_nodename);
1769 fd = open(sfname, O_RDONLY);
1771 if (errno != ENOENT) {
1772 lprintf(CTDL_CRIT, "cannot open upload file locally: %s\n",
1778 while (plen = (long) read(fd, pbuf, IGNET_PACKET_SIZE), plen > 0L) {
1779 bytes_to_write = plen;
1780 while (bytes_to_write > 0L) {
1781 snprintf(buf, sizeof buf, "WRIT %ld", bytes_to_write);
1782 if (sock_puts(sock, buf) < 0) {
1786 if (sock_gets(sock, buf) < 0) {
1790 thisblock = atol(&buf[4]);
1791 if (buf[0] == '7') {
1792 if (sock_write(sock, pbuf,
1793 (int) thisblock) < 0) {
1797 bytes_to_write -= thisblock;
1798 bytes_written += thisblock;
1807 if (sock_puts(sock, "UCLS 1") < 0) return;
1808 if (sock_gets(sock, buf) < 0) return;
1809 lprintf(CTDL_NOTICE, "Sent %ld octets to <%s>\n",
1810 bytes_written, remote_nodename);
1811 lprintf(CTDL_DEBUG, "<%s\n", buf);
1812 if (buf[0] == '2') {
1813 lprintf(CTDL_DEBUG, "Removing <%s>\n", sfname);
1821 * Poll one Citadel node (called by network_poll_other_citadel_nodes() below)
1823 void network_poll_node(char *node, char *secret, char *host, char *port) {
1827 if (network_talking_to(node, NTT_CHECK)) return;
1828 network_talking_to(node, NTT_ADD);
1829 lprintf(CTDL_NOTICE, "Connecting to <%s> at %s:%s\n", node, host, port);
1831 sock = sock_connect(host, port, "tcp");
1833 lprintf(CTDL_ERR, "Could not connect: %s\n", strerror(errno));
1834 network_talking_to(node, NTT_REMOVE);
1838 lprintf(CTDL_DEBUG, "Connected!\n");
1840 /* Read the server greeting */
1841 if (sock_gets(sock, buf) < 0) goto bail;
1842 lprintf(CTDL_DEBUG, ">%s\n", buf);
1844 /* Identify ourselves */
1845 snprintf(buf, sizeof buf, "NETP %s|%s", config.c_nodename, secret);
1846 lprintf(CTDL_DEBUG, "<%s\n", buf);
1847 if (sock_puts(sock, buf) <0) goto bail;
1848 if (sock_gets(sock, buf) < 0) goto bail;
1849 lprintf(CTDL_DEBUG, ">%s\n", buf);
1850 if (buf[0] != '2') goto bail;
1852 /* At this point we are authenticated. */
1853 receive_spool(sock, node);
1854 transmit_spool(sock, node);
1856 sock_puts(sock, "QUIT");
1857 bail: sock_close(sock);
1858 network_talking_to(node, NTT_REMOVE);
1864 * Poll other Citadel nodes and transfer inbound/outbound network data.
1865 * Set "full" to nonzero to force a poll of every node, or to zero to poll
1866 * only nodes to which we have data to send.
1868 void network_poll_other_citadel_nodes(int full_poll) {
1876 char spoolfile[256];
1878 if (working_ignetcfg == NULL) {
1879 lprintf(CTDL_DEBUG, "No nodes defined - not polling\n");
1883 /* Use the string tokenizer to grab one line at a time */
1884 for (i=0; i<num_tokens(working_ignetcfg, '\n'); ++i) {
1885 extract_token(linebuf, working_ignetcfg, i, '\n', sizeof linebuf);
1886 extract_token(node, linebuf, 0, '|', sizeof node);
1887 extract_token(secret, linebuf, 1, '|', sizeof secret);
1888 extract_token(host, linebuf, 2, '|', sizeof host);
1889 extract_token(port, linebuf, 3, '|', sizeof port);
1890 if ( (strlen(node) > 0) && (strlen(secret) > 0)
1891 && (strlen(host) > 0) && strlen(port) > 0) {
1894 snprintf(spoolfile, sizeof spoolfile,
1895 #ifndef HAVE_SPOOL_DIR
1900 "/network/spoolout/%s", node);
1901 if (access(spoolfile, R_OK) == 0) {
1906 network_poll_node(node, secret, host, port);
1917 * It's ok if these directories already exist. Just fail silently.
1919 void create_spool_dirs(void) {
1920 #ifndef HAVE_SPOOL_DIR
1921 mkdir("./network", 0700);
1922 chown("./network", CTDLUID, (-1));
1923 mkdir("./network/spoolin", 0700);
1924 chown("./network/spoolin", CTDLUID, (-1));
1925 mkdir("./network/spoolout", 0700);
1926 chown("./network/spoolout", CTDLUID, (-1));
1928 mkdir(SPOOL_DIR "/network", 0700);
1929 chown(SPOOL_DIR "./network", CTDLUID, (-1));
1930 mkdir(SPOOL_DIR "/network/spoolin", 0700);
1931 chown(SPOOL_DIR "./network/spoolin", CTDLUID, (-1));
1932 mkdir(SPOOL_DIR "/network/spoolout", 0700);
1933 chown(SPOOL_DIR "./network/spoolout", CTDLUID, (-1));
1934 #endif /* HAVE_SPOOL_DIR */
1942 * network_do_queue()
1944 * Run through the rooms doing various types of network stuff.
1946 void network_do_queue(void) {
1947 static time_t last_run = 0L;
1948 struct RoomProcList *ptr;
1949 int full_processing = 1;
1952 * Run the full set of processing tasks no more frequently
1953 * than once every n seconds
1955 if ( (time(NULL) - last_run) < config.c_net_freq ) {
1956 full_processing = 0;
1960 * This is a simple concurrency check to make sure only one queue run
1961 * is done at a time. We could do this with a mutex, but since we
1962 * don't really require extremely fine granularity here, we'll do it
1963 * with a static variable instead.
1965 if (doing_queue) return;
1968 /* Load the IGnet Configuration into memory */
1969 load_working_ignetcfg();
1972 * Poll other Citadel nodes. Maybe. If "full_processing" is set
1973 * then we poll everyone. Otherwise we only poll nodes we have stuff
1976 network_poll_other_citadel_nodes(full_processing);
1979 * Load the network map and filter list into memory.
1982 filterlist = load_filter_list();
1985 * Go ahead and run the queue
1987 if (full_processing) {
1988 lprintf(CTDL_DEBUG, "network: loading outbound queue\n");
1989 ForEachRoom(network_queue_room, NULL);
1991 lprintf(CTDL_DEBUG, "network: running outbound queue\n");
1992 while (rplist != NULL) {
1993 network_spoolout_room(rplist->name);
1995 rplist = rplist->next;
2000 /* If there is anything in the inbound queue, process it */
2001 network_do_spoolin();
2003 /* Save the network map back to disk */
2004 write_network_map();
2006 /* Free the filter list in memory */
2007 free_filter_list(filterlist);
2010 network_purge_spoolout();
2012 lprintf(CTDL_DEBUG, "network: queue run completed\n");
2014 if (full_processing) {
2015 last_run = time(NULL);
2023 * cmd_netp() - authenticate to the server as another Citadel node polling
2024 * for network traffic
2026 void cmd_netp(char *cmdbuf)
2036 extract_token(node, cmdbuf, 0, '|', sizeof node);
2037 extract_token(pass, cmdbuf, 1, '|', sizeof pass);
2040 lprintf(CTDL_WARNING, "Network node <%s> refused - spooling", node);
2041 cprintf("%d spooling - try again in a few minutes\n",
2042 ERROR + RESOURCE_BUSY);
2046 /* load the IGnet Configuration to check node validity */
2047 load_working_ignetcfg();
2048 v = is_valid_node(nexthop, secret, node);
2051 lprintf(CTDL_WARNING, "Unknown node <%s>\n", node);
2052 cprintf("%d authentication failed\n",
2053 ERROR + PASSWORD_REQUIRED);
2057 if (strcasecmp(pass, secret)) {
2058 lprintf(CTDL_WARNING, "Bad password for network node <%s>", node);
2059 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
2063 if (network_talking_to(node, NTT_CHECK)) {
2064 lprintf(CTDL_WARNING, "Duplicate session for network node <%s>", node);
2065 cprintf("%d Already talking to %s right now\n", ERROR + RESOURCE_BUSY, node);
2069 safestrncpy(CC->net_node, node, sizeof CC->net_node);
2070 network_talking_to(node, NTT_ADD);
2071 lprintf(CTDL_NOTICE, "Network node <%s> logged in\n", CC->net_node);
2072 cprintf("%d authenticated as network node '%s'\n", CIT_OK,
2077 * Module entry point
2079 char *serv_network_init(void)
2081 create_spool_dirs();
2082 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
2083 CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");
2084 CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");
2085 CtdlRegisterProtoHook(cmd_nsyn, "NSYN", "Synchronize room to node");
2086 CtdlRegisterSessionHook(network_do_queue, EVT_TIMER);