4 * This module handles shared rooms, inter-Citadel mail, and outbound
5 * mailing list processing.
7 * Copyright (C) 2000-2002 by Art Cancro and others.
8 * This code is released under the terms of the GNU General Public License.
10 * ** NOTE ** A word on the S_NETCONFIGS semaphore:
11 * This is a fairly high-level type of critical section. It ensures that no
12 * two threads work on the netconfigs files at the same time. Since we do
13 * so many things inside these, here are the rules:
14 * 1. begin_critical_section(S_NETCONFIGS) *before* begin_ any others.
15 * 2. Do *not* perform any I/O with the client during these sections.
20 * Duration of time (in seconds) after which pending list subscribe/unsubscribe
21 * requests that have not been confirmed will be deleted.
23 #define EXP 259200 /* three days */
35 #include <sys/types.h>
37 #if TIME_WITH_SYS_TIME
38 # include <sys/time.h>
42 # include <sys/time.h>
53 #include "sysdep_decls.h"
54 #include "citserver.h"
57 #include "serv_extensions.h"
64 #include "internet_addressing.h"
65 #include "serv_network.h"
66 #include "clientsocket.h"
73 /* Nonzero while we are doing outbound network processing */
74 static int doing_queue = 0;
77 * When we do network processing, it's accomplished in two passes; one to
78 * gather a list of rooms and one to actually do them. It's ok that rplist
79 * is global; this process *only* runs as part of the housekeeping loop and
80 * therefore only one will run at a time.
82 struct RoomProcList *rplist = NULL;
85 * We build a map of network nodes during processing.
87 struct NetMap *the_netmap = NULL;
88 int netmap_changed = 0;
89 char *working_ignetcfg = NULL;
92 * Load or refresh the Citadel network (IGnet) configuration for this node.
94 void load_working_ignetcfg(void) {
98 cfg = CtdlGetSysConfig(IGNETCFG);
103 oldcfg = working_ignetcfg;
104 working_ignetcfg = cfg;
105 if (oldcfg != NULL) {
115 * Keep track of what messages to reject
117 struct FilterList *load_filter_list(void) {
118 char *serialized_list = NULL;
121 struct FilterList *newlist = NULL;
122 struct FilterList *nptr;
124 serialized_list = CtdlGetSysConfig(FILTERLIST);
125 if (serialized_list == NULL) return(NULL); /* if null, no entries */
127 /* Use the string tokenizer to grab one line at a time */
128 for (i=0; i<num_tokens(serialized_list, '\n'); ++i) {
129 extract_token(buf, serialized_list, i, '\n', sizeof buf);
130 nptr = (struct FilterList *) malloc(sizeof(struct FilterList));
131 extract_token(nptr->fl_user, buf, 0, '|', sizeof nptr->fl_user);
132 striplt(nptr->fl_user);
133 extract_token(nptr->fl_room, buf, 1, '|', sizeof nptr->fl_room);
134 striplt(nptr->fl_room);
135 extract_token(nptr->fl_node, buf, 2, '|', sizeof nptr->fl_node);
136 striplt(nptr->fl_node);
138 /* Cowardly refuse to add an any/any/any entry that would
139 * end up filtering every single message.
141 if (strlen(nptr->fl_user) + strlen(nptr->fl_room)
142 + strlen(nptr->fl_node) == 0) {
146 nptr->next = newlist;
151 free(serialized_list);
156 void free_filter_list(struct FilterList *fl) {
157 if (fl == NULL) return;
158 free_filter_list(fl->next);
165 * Check the use table. This is a list of messages which have recently
166 * arrived on the system. It is maintained and queried to prevent the same
167 * message from being entered into the database multiple times if it happens
168 * to arrive multiple times by accident.
170 int network_usetable(struct CtdlMessage *msg) {
173 struct cdbdata *cdbut;
176 /* Bail out if we can't generate a message ID */
180 if (msg->cm_fields['I'] == NULL) {
183 if (strlen(msg->cm_fields['I']) == 0) {
187 /* Generate the message ID */
188 strcpy(msgid, msg->cm_fields['I']);
189 if (haschar(msgid, '@') == 0) {
191 if (msg->cm_fields['N'] != NULL) {
192 strcat(msgid, msg->cm_fields['N']);
199 cdbut = cdb_fetch(CDB_USETABLE, msgid, strlen(msgid));
205 /* If we got to this point, it's unique: add it. */
206 strcpy(ut.ut_msgid, msgid);
207 ut.ut_timestamp = time(NULL);
208 cdb_store(CDB_USETABLE, msgid, strlen(msgid),
209 &ut, sizeof(struct UseTable) );
215 * Read the network map from its configuration file into memory.
217 void read_network_map(void) {
218 char *serialized_map = NULL;
221 struct NetMap *nmptr;
223 serialized_map = CtdlGetSysConfig(IGNETMAP);
224 if (serialized_map == NULL) return; /* if null, no entries */
226 /* Use the string tokenizer to grab one line at a time */
227 for (i=0; i<num_tokens(serialized_map, '\n'); ++i) {
228 extract_token(buf, serialized_map, i, '\n', sizeof buf);
229 nmptr = (struct NetMap *) malloc(sizeof(struct NetMap));
230 extract_token(nmptr->nodename, buf, 0, '|', sizeof nmptr->nodename);
231 nmptr->lastcontact = extract_long(buf, 1);
232 extract_token(nmptr->nexthop, buf, 2, '|', sizeof nmptr->nexthop);
233 nmptr->next = the_netmap;
237 free(serialized_map);
243 * Write the network map from memory back to the configuration file.
245 void write_network_map(void) {
246 char *serialized_map = NULL;
247 struct NetMap *nmptr;
250 if (netmap_changed) {
251 serialized_map = strdup("");
253 if (the_netmap != NULL) {
254 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
255 serialized_map = realloc(serialized_map,
256 (strlen(serialized_map)+SIZ) );
257 if (strlen(nmptr->nodename) > 0) {
258 snprintf(&serialized_map[strlen(serialized_map)],
262 (long)nmptr->lastcontact,
268 CtdlPutSysConfig(IGNETMAP, serialized_map);
269 free(serialized_map);
272 /* Now free the list */
273 while (the_netmap != NULL) {
274 nmptr = the_netmap->next;
284 * Check the network map and determine whether the supplied node name is
285 * valid. If it is not a neighbor node, supply the name of a neighbor node
286 * which is the next hop. If it *is* a neighbor node, we also fill in the
289 int is_valid_node(char *nexthop, char *secret, char *node) {
294 struct NetMap *nmptr;
301 * First try the neighbor nodes
303 if (working_ignetcfg == NULL) {
304 lprintf(CTDL_ERR, "working_ignetcfg is NULL!\n");
305 if (nexthop != NULL) {
312 if (nexthop != NULL) {
316 /* Use the string tokenizer to grab one line at a time */
317 for (i=0; i<num_tokens(working_ignetcfg, '\n'); ++i) {
318 extract_token(linebuf, working_ignetcfg, i, '\n', sizeof linebuf);
319 extract_token(buf, linebuf, 0, '|', sizeof buf);
320 if (!strcasecmp(buf, node)) {
321 if (nexthop != NULL) {
324 if (secret != NULL) {
325 extract_token(secret, linebuf, 1, '|', 256);
332 return(retval); /* yup, it's a direct neighbor */
336 * If we get to this point we have to see if we know the next hop
338 if (the_netmap != NULL) {
339 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
340 if (!strcasecmp(nmptr->nodename, node)) {
341 if (nexthop != NULL) {
342 strcpy(nexthop, nmptr->nexthop);
350 * If we get to this point, the supplied node name is bogus.
352 lprintf(CTDL_ERR, "Invalid node name <%s>\n", node);
360 void cmd_gnet(char *argbuf) {
365 if (CtdlAccessCheck(ac_room_aide)) return;
366 assoc_file_name(filename, sizeof filename, &CC->room, "netconfigs");
367 cprintf("%d Network settings for room #%ld <%s>\n",
369 CC->room.QRnumber, CC->room.QRname);
371 fp = fopen(filename, "r");
373 while (fgets(buf, sizeof buf, fp) != NULL) {
374 buf[strlen(buf)-1] = 0;
375 cprintf("%s\n", buf);
384 void cmd_snet(char *argbuf) {
385 char tempfilename[SIZ];
392 if (CtdlAccessCheck(ac_room_aide)) return;
393 safestrncpy(tempfilename, tmpnam(NULL), sizeof tempfilename);
394 assoc_file_name(filename, sizeof filename, &CC->room, "netconfigs");
396 fp = fopen(tempfilename, "w");
398 cprintf("%d Cannot open %s: %s\n",
399 ERROR + INTERNAL_ERROR,
404 cprintf("%d %s\n", SEND_LISTING, tempfilename);
405 while (client_getln(buf, sizeof buf), strcmp(buf, "000")) {
406 fprintf(fp, "%s\n", buf);
410 /* Now copy the temp file to its permanent location
411 * (We use /bin/mv instead of link() because they may be on
412 * different filesystems)
415 snprintf(buf, sizeof buf, "/bin/mv %s %s", tempfilename, filename);
416 begin_critical_section(S_NETCONFIGS);
418 end_critical_section(S_NETCONFIGS);
423 * Spools out one message from the list.
425 void network_spool_msg(long msgnum, void *userdata) {
426 struct SpoolControl *sc;
428 char *newpath = NULL;
429 size_t instr_len = SIZ;
430 struct CtdlMessage *msg = NULL;
431 struct namelist *nptr;
432 struct maplist *mptr;
433 struct ser_ret sermsg;
439 int delete_after_send = 0; /* Set to 1 to delete after spooling */
440 int ok_to_participate = 0;
441 char *end_of_headers = NULL;
442 struct recptypes *valid;
444 sc = (struct SpoolControl *)userdata;
447 * Process mailing list recipients
450 if (sc->listrecps != NULL) {
451 /* Fetch the message. We're going to need to modify it
452 * for each recipient in order to satisfy foreign mailers, etc.
454 msg = CtdlFetchMessage(msgnum, 1);
457 /* Prepend "[List name]" to the subject */
458 if (msg->cm_fields['U'] == NULL) {
459 msg->cm_fields['U'] = strdup("(no subject)");
461 snprintf(buf, sizeof buf, "[%s] %s", CC->room.QRname, msg->cm_fields['U']);
462 free(msg->cm_fields['U']);
463 msg->cm_fields['U'] = strdup(buf);
465 /* For each recipient... */
466 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
468 if (msg->cm_fields['R'] == NULL) {
469 free(msg->cm_fields['R']);
471 msg->cm_fields['R'] = strdup(nptr->name);
473 valid = validate_recipients(nptr->name);
474 CtdlSubmitMsg(msg, valid, "");
478 CtdlFreeMessage(msg);
483 * Process digest recipients
485 if ((sc->digestrecps != NULL) && (sc->digestfp != NULL)) {
486 msg = CtdlFetchMessage(msgnum, 1);
488 fprintf(sc->digestfp, " -----------------------------------"
489 "------------------------------------"
491 fprintf(sc->digestfp, "From: ");
492 if (msg->cm_fields['A'] != NULL) {
493 fprintf(sc->digestfp, "%s ", msg->cm_fields['A']);
495 if (msg->cm_fields['F'] != NULL) {
496 fprintf(sc->digestfp, "<%s> ", msg->cm_fields['F']);
498 else if (msg->cm_fields['N'] != NULL) {
499 fprintf(sc->digestfp, "@%s ", msg->cm_fields['N']);
501 fprintf(sc->digestfp, "\n");
502 if (msg->cm_fields['U'] != NULL) {
503 fprintf(sc->digestfp, "Subject: %s\n", msg->cm_fields['U']);
506 CC->redirect_buffer = malloc(SIZ);
507 CC->redirect_len = 0;
508 CC->redirect_alloc = SIZ;
510 safestrncpy(CC->preferred_formats, "text/plain", sizeof CC->preferred_formats);
511 CtdlOutputPreLoadedMsg(msg, 0L, MT_MIME, HEADERS_NONE, 0, 0);
513 end_of_headers = bmstrstr(CC->redirect_buffer, "\n\r\n", strncmp);
514 if (end_of_headers == NULL) {
515 end_of_headers = bmstrstr(CC->redirect_buffer, "\n\n", strncmp);
517 if (end_of_headers == NULL) {
518 end_of_headers = CC->redirect_buffer;
520 striplt(end_of_headers);
521 fprintf(sc->digestfp, "\n%s\n", end_of_headers);
523 free(CC->redirect_buffer);
524 CC->redirect_buffer = NULL;
525 CC->redirect_len = 0;
526 CC->redirect_alloc = 0;
528 sc->num_msgs_spooled += 1;
534 * Process client-side list participations for this room
537 if (sc->participates != NULL) {
538 msg = CtdlFetchMessage(msgnum, 1);
541 /* Only send messages which originated on our own Citadel
542 * network, otherwise we'll end up sending the remote
543 * mailing list's messages back to it, which is rude...
545 ok_to_participate = 0;
546 if (msg->cm_fields['N'] != NULL) {
547 if (!strcasecmp(msg->cm_fields['N'], config.c_nodename)) {
548 ok_to_participate = 1;
550 if (is_valid_node(NULL, NULL, msg->cm_fields['N']) == 0) {
551 ok_to_participate = 1;
554 if (ok_to_participate) {
555 if (msg->cm_fields['F'] != NULL) {
556 free(msg->cm_fields['F']);
558 msg->cm_fields['F'] = malloc(SIZ);
559 /* Replace the Internet email address of the actual
560 * author with the email address of the room itself,
561 * so the remote listserv doesn't reject us.
562 * FIXME ... I want to be able to pick any address
564 snprintf(msg->cm_fields['F'], SIZ,
565 "room_%s@%s", CC->room.QRname,
567 for (i=0; i<strlen(msg->cm_fields['F']); ++i) {
568 if (isspace(msg->cm_fields['F'][i])) {
569 msg->cm_fields['F'][i] = '_';
574 * Figure out how big a buffer we need to allocate
576 for (nptr = sc->participates; nptr != NULL; nptr = nptr->next) {
578 if (msg->cm_fields['R'] == NULL) {
579 free(msg->cm_fields['R']);
581 msg->cm_fields['R'] = strdup(nptr->name);
583 valid = validate_recipients(nptr->name);
584 CtdlSubmitMsg(msg, valid, "");
589 CtdlFreeMessage(msg);
594 * Process IGnet push shares
596 if (sc->ignet_push_shares != NULL) {
598 msg = CtdlFetchMessage(msgnum, 1);
602 /* Prepend our node name to the Path field whenever
603 * sending a message to another IGnet node
605 if (msg->cm_fields['P'] == NULL) {
606 msg->cm_fields['P'] = strdup("username");
608 newpath_len = strlen(msg->cm_fields['P']) +
609 strlen(config.c_nodename) + 2;
610 newpath = malloc(newpath_len);
611 snprintf(newpath, newpath_len, "%s!%s",
612 config.c_nodename, msg->cm_fields['P']);
613 free(msg->cm_fields['P']);
614 msg->cm_fields['P'] = newpath;
617 * Determine if this message is set to be deleted
618 * after sending out on the network
620 if (msg->cm_fields['S'] != NULL) {
621 if (!strcasecmp(msg->cm_fields['S'],
623 delete_after_send = 1;
627 /* Now send it to every node */
628 for (mptr = sc->ignet_push_shares; mptr != NULL;
633 /* Check for valid node name */
634 if (is_valid_node(NULL, NULL, mptr->remote_nodename) != 0) {
635 lprintf(CTDL_ERR, "Invalid node <%s>\n",
636 mptr->remote_nodename);
640 /* Check for split horizon */
641 lprintf(CTDL_DEBUG, "Path is %s\n", msg->cm_fields['P']);
642 bang = num_tokens(msg->cm_fields['P'], '!');
643 if (bang > 1) for (i=0; i<(bang-1); ++i) {
644 extract_token(buf, msg->cm_fields['P'],
646 if (!strcasecmp(buf, mptr->remote_nodename)) {
651 /* Send the message */
655 * Force the message to appear in the correct room
656 * on the far end by setting the C field correctly
658 if (msg->cm_fields['C'] != NULL) {
659 free(msg->cm_fields['C']);
661 if (strlen(mptr->remote_roomname) > 0) {
662 msg->cm_fields['C'] = strdup(mptr->remote_roomname);
665 msg->cm_fields['C'] = strdup(CC->room.QRname);
668 /* serialize it for transmission */
669 serialize_message(&sermsg, msg);
671 /* write it to the spool file */
672 snprintf(filename, sizeof filename,
673 #ifndef HAVE_SPOOL_DIR
677 #endif /* HAVE_SPOOL_DIR */
678 "/network/spoolout/%s",
679 mptr->remote_nodename);
680 lprintf(CTDL_DEBUG, "Appending to %s\n", filename);
681 fp = fopen(filename, "ab");
688 lprintf(CTDL_ERR, "%s: %s\n", filename, strerror(errno));
691 /* free the serialized version */
695 CtdlFreeMessage(msg);
699 /* update lastsent */
700 sc->lastsent = msgnum;
702 /* Delete this message if delete-after-send is set */
703 if (delete_after_send) {
704 CtdlDeleteMessages(CC->room.QRname, msgnum, "", 0);
711 * Deliver digest messages
713 void network_deliver_digest(struct SpoolControl *sc) {
716 struct CtdlMessage *msg;
720 size_t instr_len = SIZ;
721 struct CtdlMessage *imsg;
722 struct namelist *nptr;
724 if (sc->num_msgs_spooled < 1) {
725 fclose(sc->digestfp);
730 msg = malloc(sizeof(struct CtdlMessage));
731 memset(msg, 0, sizeof(struct CtdlMessage));
732 msg->cm_magic = CTDLMESSAGE_MAGIC;
733 msg->cm_format_type = FMT_RFC822;
734 msg->cm_anon_type = MES_NORMAL;
736 sprintf(buf, "%ld", time(NULL));
737 msg->cm_fields['T'] = strdup(buf);
738 msg->cm_fields['A'] = strdup(CC->room.QRname);
739 snprintf(buf, sizeof buf, "[%s]", CC->room.QRname);
740 msg->cm_fields['U'] = strdup(buf);
741 sprintf(buf, "room_%s@%s", CC->room.QRname, config.c_fqdn);
742 for (i=0; i<strlen(buf); ++i) {
743 if (isspace(buf[i])) buf[i]='_';
744 buf[i] = tolower(buf[i]);
746 msg->cm_fields['F'] = strdup(buf);
748 fseek(sc->digestfp, 0L, SEEK_END);
749 msglen = ftell(sc->digestfp);
751 msg->cm_fields['M'] = malloc(msglen + 1);
752 fseek(sc->digestfp, 0L, SEEK_SET);
753 fread(msg->cm_fields['M'], (size_t)msglen, 1, sc->digestfp);
754 msg->cm_fields['M'][msglen] = 0;
756 fclose(sc->digestfp);
759 msgnum = CtdlSubmitMsg(msg, NULL, SMTP_SPOOLOUT_ROOM);
760 CtdlFreeMessage(msg);
762 /* Now generate the delivery instructions */
765 * Figure out how big a buffer we need to allocate
767 for (nptr = sc->digestrecps; nptr != NULL; nptr = nptr->next) {
768 instr_len = instr_len + strlen(nptr->name);
774 lprintf(CTDL_DEBUG, "Generating delivery instructions\n");
775 instr = malloc(instr_len);
777 lprintf(CTDL_EMERG, "Cannot allocate %ld bytes for instr...\n",
781 snprintf(instr, instr_len,
782 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
783 "bounceto|postmaster@%s\n" ,
784 SPOOLMIME, msgnum, (long)time(NULL), config.c_fqdn );
786 /* Generate delivery instructions for each recipient */
787 for (nptr = sc->digestrecps; nptr != NULL; nptr = nptr->next) {
788 size_t tmp = strlen(instr);
789 snprintf(&instr[tmp], instr_len - tmp,
790 "remote|%s|0||\n", nptr->name);
794 * Generate a message from the instructions
796 imsg = malloc(sizeof(struct CtdlMessage));
797 memset(imsg, 0, sizeof(struct CtdlMessage));
798 imsg->cm_magic = CTDLMESSAGE_MAGIC;
799 imsg->cm_anon_type = MES_NORMAL;
800 imsg->cm_format_type = FMT_RFC822;
801 imsg->cm_fields['A'] = strdup("Citadel");
802 imsg->cm_fields['M'] = instr;
804 /* Save delivery instructions in spoolout room */
805 CtdlSubmitMsg(imsg, NULL, SMTP_SPOOLOUT_ROOM);
806 CtdlFreeMessage(imsg);
811 * Batch up and send all outbound traffic from the current room
813 void network_spoolout_room(char *room_to_spool) {
818 char roomname[ROOMNAMELEN];
821 struct SpoolControl sc;
822 struct namelist *nptr = NULL;
823 struct maplist *mptr = NULL;
826 int skipthisline = 0;
829 if (getroom(&CC->room, room_to_spool) != 0) {
830 lprintf(CTDL_CRIT, "ERROR: cannot load <%s>\n", room_to_spool);
834 memset(&sc, 0, sizeof(struct SpoolControl));
835 assoc_file_name(filename, sizeof filename, &CC->room, "netconfigs");
837 begin_critical_section(S_NETCONFIGS);
839 fp = fopen(filename, "r");
841 end_critical_section(S_NETCONFIGS);
845 lprintf(CTDL_INFO, "Networking started for <%s>\n", CC->room.QRname);
847 while (fgets(buf, sizeof buf, fp) != NULL) {
848 buf[strlen(buf)-1] = 0;
850 extract_token(instr, buf, 0, '|', sizeof instr);
851 if (!strcasecmp(instr, "lastsent")) {
852 sc.lastsent = extract_long(buf, 1);
854 else if (!strcasecmp(instr, "listrecp")) {
855 nptr = (struct namelist *)
856 malloc(sizeof(struct namelist));
857 nptr->next = sc.listrecps;
858 extract_token(nptr->name, buf, 1, '|', sizeof nptr->name);
861 else if (!strcasecmp(instr, "participate")) {
862 nptr = (struct namelist *)
863 malloc(sizeof(struct namelist));
864 nptr->next = sc.participates;
865 extract_token(nptr->name, buf, 1, '|', sizeof nptr->name);
866 sc.participates = nptr;
868 else if (!strcasecmp(instr, "digestrecp")) {
869 nptr = (struct namelist *)
870 malloc(sizeof(struct namelist));
871 nptr->next = sc.digestrecps;
872 extract_token(nptr->name, buf, 1, '|', sizeof nptr->name);
873 sc.digestrecps = nptr;
875 else if (!strcasecmp(instr, "ignet_push_share")) {
876 /* by checking each node's validity, we automatically
877 * purge nodes which do not exist from room network
878 * configurations at this time.
880 extract_token(nodename, buf, 1, '|', sizeof nodename);
881 extract_token(roomname, buf, 2, '|', sizeof roomname);
882 strcpy(nexthop, "xxx");
883 if (is_valid_node(nexthop, NULL, nodename) == 0) {
884 if (strlen(nexthop) == 0) {
885 mptr = (struct maplist *)
886 malloc(sizeof(struct maplist));
887 mptr->next = sc.ignet_push_shares;
888 strcpy(mptr->remote_nodename, nodename);
889 strcpy(mptr->remote_roomname, roomname);
890 sc.ignet_push_shares = mptr;
895 /* Preserve 'other' lines ... *unless* they happen to
896 * be subscribe/unsubscribe pendings with expired
900 if (!strncasecmp(buf, "subpending|", 11)) {
901 if (time(NULL) - extract_long(buf, 4) > EXP) {
905 if (!strncasecmp(buf, "unsubpending|", 13)) {
906 if (time(NULL) - extract_long(buf, 3) > EXP) {
911 if (skipthisline == 0) {
912 linesize = strlen(buf);
913 sc.misc = realloc(sc.misc,
914 (miscsize + linesize + 2) );
915 sprintf(&sc.misc[miscsize], "%s\n", buf);
916 miscsize = miscsize + linesize + 1;
924 /* If there are digest recipients, we have to build a digest */
925 if (sc.digestrecps != NULL) {
926 sc.digestfp = tmpfile();
927 fprintf(sc.digestfp, "Content-type: text/plain\n\n");
930 /* Do something useful */
931 CtdlForEachMessage(MSGS_GT, sc.lastsent, NULL, NULL,
932 network_spool_msg, &sc);
934 /* If we wrote a digest, deliver it and then close it */
935 snprintf(buf, sizeof buf, "room_%s@%s",
936 CC->room.QRname, config.c_fqdn);
937 for (i=0; i<strlen(buf); ++i) {
938 buf[i] = tolower(buf[i]);
939 if (isspace(buf[i])) buf[i] = '_';
941 if (sc.digestfp != NULL) {
942 fprintf(sc.digestfp, " -----------------------------------"
943 "------------------------------------"
945 "You are subscribed to the '%s' "
947 "To post to the list: %s\n",
950 network_deliver_digest(&sc); /* deliver and close */
953 /* Now rewrite the config file */
954 fp = fopen(filename, "w");
956 lprintf(CTDL_CRIT, "ERROR: cannot open %s: %s\n",
957 filename, strerror(errno));
960 fprintf(fp, "lastsent|%ld\n", sc.lastsent);
962 /* Write out the listrecps while freeing from memory at the
963 * same time. Am I clever or what? :)
965 while (sc.listrecps != NULL) {
966 fprintf(fp, "listrecp|%s\n", sc.listrecps->name);
967 nptr = sc.listrecps->next;
971 /* Do the same for digestrecps */
972 while (sc.digestrecps != NULL) {
973 fprintf(fp, "digestrecp|%s\n", sc.digestrecps->name);
974 nptr = sc.digestrecps->next;
975 free(sc.digestrecps);
976 sc.digestrecps = nptr;
978 /* Do the same for participates */
979 while (sc.participates != NULL) {
980 fprintf(fp, "participate|%s\n", sc.participates->name);
981 nptr = sc.participates->next;
982 free(sc.participates);
983 sc.participates = nptr;
985 while (sc.ignet_push_shares != NULL) {
986 /* by checking each node's validity, we automatically
987 * purge nodes which do not exist from room network
988 * configurations at this time.
990 if (is_valid_node(NULL, NULL, sc.ignet_push_shares->remote_nodename) == 0) {
992 fprintf(fp, "ignet_push_share|%s",
993 sc.ignet_push_shares->remote_nodename);
994 if (strlen(sc.ignet_push_shares->remote_roomname) > 0) {
995 fprintf(fp, "|%s", sc.ignet_push_shares->remote_roomname);
998 mptr = sc.ignet_push_shares->next;
999 free(sc.ignet_push_shares);
1000 sc.ignet_push_shares = mptr;
1002 if (sc.misc != NULL) {
1003 fwrite(sc.misc, strlen(sc.misc), 1, fp);
1009 end_critical_section(S_NETCONFIGS);
1015 * Send the *entire* contents of the current room to one specific network node,
1016 * ignoring anything we know about which messages have already undergone
1017 * network processing. This can be used to bring a new node into sync.
1019 int network_sync_to(char *target_node) {
1020 struct SpoolControl sc;
1021 int num_spooled = 0;
1030 /* Grab the configuration line we're looking for */
1031 assoc_file_name(filename, sizeof filename, &CC->room, "netconfigs");
1032 begin_critical_section(S_NETCONFIGS);
1033 fp = fopen(filename, "r");
1035 end_critical_section(S_NETCONFIGS);
1038 while (fgets(buf, sizeof buf, fp) != NULL) {
1039 buf[strlen(buf)-1] = 0;
1040 extract_token(sc_type, buf, 0, '|', sizeof sc_type);
1041 extract_token(sc_node, buf, 1, '|', sizeof sc_node);
1042 extract_token(sc_room, buf, 2, '|', sizeof sc_room);
1043 if ( (!strcasecmp(sc_type, "ignet_push_share"))
1044 && (!strcasecmp(sc_node, target_node)) ) {
1047 /* Concise syntax because we don't need a full linked-list */
1048 memset(&sc, 0, sizeof(struct SpoolControl));
1049 sc.ignet_push_shares = (struct maplist *)
1050 malloc(sizeof(struct maplist));
1051 sc.ignet_push_shares->next = NULL;
1052 safestrncpy(sc.ignet_push_shares->remote_nodename,
1054 sizeof sc.ignet_push_shares->remote_nodename);
1055 safestrncpy(sc.ignet_push_shares->remote_roomname,
1057 sizeof sc.ignet_push_shares->remote_roomname);
1061 end_critical_section(S_NETCONFIGS);
1063 if (!found_node) return(-1);
1065 /* Send ALL messages */
1066 num_spooled = CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL,
1067 network_spool_msg, &sc);
1069 /* Concise cleanup because we know there's only one node in the sc */
1070 free(sc.ignet_push_shares);
1072 lprintf(CTDL_NOTICE, "Synchronized %d messages to <%s>\n",
1073 num_spooled, target_node);
1074 return(num_spooled);
1079 * Implements the NSYN command
1081 void cmd_nsyn(char *argbuf) {
1083 char target_node[256];
1085 if (CtdlAccessCheck(ac_aide)) return;
1087 extract_token(target_node, argbuf, 0, '|', sizeof target_node);
1088 num_spooled = network_sync_to(target_node);
1089 if (num_spooled >= 0) {
1090 cprintf("%d Spooled %d messages.\n", CIT_OK, num_spooled);
1093 cprintf("%d No such room/node share exists.\n",
1094 ERROR + ROOM_NOT_FOUND);
1101 * Batch up and send all outbound traffic from the current room
1103 void network_queue_room(struct ctdlroom *qrbuf, void *data) {
1104 struct RoomProcList *ptr;
1106 ptr = (struct RoomProcList *) malloc(sizeof (struct RoomProcList));
1107 if (ptr == NULL) return;
1109 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
1116 * Learn topology from path fields
1118 void network_learn_topology(char *node, char *path) {
1120 struct NetMap *nmptr;
1122 strcpy(nexthop, "");
1124 if (num_tokens(path, '!') < 3) return;
1125 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
1126 if (!strcasecmp(nmptr->nodename, node)) {
1127 extract_token(nmptr->nexthop, path, 0, '!', sizeof nmptr->nexthop);
1128 nmptr->lastcontact = time(NULL);
1134 /* If we got here then it's not in the map, so add it. */
1135 nmptr = (struct NetMap *) malloc(sizeof (struct NetMap));
1136 strcpy(nmptr->nodename, node);
1137 nmptr->lastcontact = time(NULL);
1138 extract_token(nmptr->nexthop, path, 0, '!', sizeof nmptr->nexthop);
1139 nmptr->next = the_netmap;
1148 * Bounce a message back to the sender
1150 void network_bounce(struct CtdlMessage *msg, char *reason) {
1151 char *oldpath = NULL;
1153 char bouncesource[SIZ];
1154 char recipient[SIZ];
1155 struct recptypes *valid = NULL;
1156 char force_room[ROOMNAMELEN];
1157 static int serialnum = 0;
1160 lprintf(CTDL_DEBUG, "entering network_bounce()\n");
1162 if (msg == NULL) return;
1164 snprintf(bouncesource, sizeof bouncesource, "%s@%s", BOUNCESOURCE, config.c_nodename);
1167 * Give it a fresh message ID
1169 if (msg->cm_fields['I'] != NULL) {
1170 free(msg->cm_fields['I']);
1172 snprintf(buf, sizeof buf, "%ld.%04lx.%04x@%s",
1173 (long)time(NULL), (long)getpid(), ++serialnum, config.c_fqdn);
1174 msg->cm_fields['I'] = strdup(buf);
1177 * FIXME ... right now we're just sending a bounce; we really want to
1178 * include the text of the bounced message.
1180 if (msg->cm_fields['M'] != NULL) {
1181 free(msg->cm_fields['M']);
1183 msg->cm_fields['M'] = strdup(reason);
1184 msg->cm_format_type = 0;
1187 * Turn the message around
1189 if (msg->cm_fields['R'] == NULL) {
1190 free(msg->cm_fields['R']);
1193 if (msg->cm_fields['D'] == NULL) {
1194 free(msg->cm_fields['D']);
1197 snprintf(recipient, sizeof recipient, "%s@%s",
1198 msg->cm_fields['A'], msg->cm_fields['N']);
1200 if (msg->cm_fields['A'] == NULL) {
1201 free(msg->cm_fields['A']);
1204 if (msg->cm_fields['N'] == NULL) {
1205 free(msg->cm_fields['N']);
1208 if (msg->cm_fields['U'] == NULL) {
1209 free(msg->cm_fields['U']);
1212 msg->cm_fields['A'] = strdup(BOUNCESOURCE);
1213 msg->cm_fields['N'] = strdup(config.c_nodename);
1214 msg->cm_fields['U'] = strdup("Delivery Status Notification (Failure)");
1216 /* prepend our node to the path */
1217 if (msg->cm_fields['P'] != NULL) {
1218 oldpath = msg->cm_fields['P'];
1219 msg->cm_fields['P'] = NULL;
1222 oldpath = strdup("unknown_user");
1224 size = strlen(oldpath) + SIZ;
1225 msg->cm_fields['P'] = malloc(size);
1226 snprintf(msg->cm_fields['P'], size, "%s!%s", config.c_nodename, oldpath);
1229 /* Now submit the message */
1230 valid = validate_recipients(recipient);
1231 if (valid != NULL) if (valid->num_error > 0) {
1235 if ( (valid == NULL) || (!strcasecmp(recipient, bouncesource)) ) {
1236 strcpy(force_room, config.c_aideroom);
1239 strcpy(force_room, "");
1241 if ( (valid == NULL) && (strlen(force_room) == 0) ) {
1242 strcpy(force_room, config.c_aideroom);
1244 CtdlSubmitMsg(msg, valid, force_room);
1247 if (valid != NULL) free(valid);
1248 CtdlFreeMessage(msg);
1249 lprintf(CTDL_DEBUG, "leaving network_bounce()\n");
1256 * Process a buffer containing a single message from a single file
1257 * from the inbound queue
1259 void network_process_buffer(char *buffer, long size) {
1260 struct CtdlMessage *msg;
1263 struct recptypes *recp = NULL;
1264 char target_room[ROOMNAMELEN];
1265 struct ser_ret sermsg;
1266 char *oldpath = NULL;
1270 unsigned char firstbyte;
1271 unsigned char lastbyte;
1273 /* Validate just a little bit. First byte should be FF and
1274 * last byte should be 00.
1276 memcpy(&firstbyte, &buffer[0], 1);
1277 memcpy(&lastbyte, &buffer[size-1], 1);
1278 if ( (firstbyte != 255) || (lastbyte != 0) ) {
1279 lprintf(CTDL_ERR, "Corrupt message! Ignoring.\n");
1283 /* Set default target room to trash */
1284 strcpy(target_room, TWITROOM);
1286 /* Load the message into memory */
1287 msg = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
1288 memset(msg, 0, sizeof(struct CtdlMessage));
1289 msg->cm_magic = CTDLMESSAGE_MAGIC;
1290 msg->cm_anon_type = buffer[1];
1291 msg->cm_format_type = buffer[2];
1293 for (pos = 3; pos < size; ++pos) {
1294 field = buffer[pos];
1295 msg->cm_fields[field] = strdup(&buffer[pos+1]);
1296 pos = pos + strlen(&buffer[(int)pos]);
1299 /* Check for message routing */
1300 if (msg->cm_fields['D'] != NULL) {
1301 if (strcasecmp(msg->cm_fields['D'], config.c_nodename)) {
1303 /* route the message */
1304 strcpy(nexthop, "");
1305 if (is_valid_node(nexthop, NULL,
1306 msg->cm_fields['D']) == 0) {
1308 /* prepend our node to the path */
1309 if (msg->cm_fields['P'] != NULL) {
1310 oldpath = msg->cm_fields['P'];
1311 msg->cm_fields['P'] = NULL;
1314 oldpath = strdup("unknown_user");
1316 size = strlen(oldpath) + SIZ;
1317 msg->cm_fields['P'] = malloc(size);
1318 snprintf(msg->cm_fields['P'], size, "%s!%s",
1319 config.c_nodename, oldpath);
1322 /* serialize the message */
1323 serialize_message(&sermsg, msg);
1326 if (strlen(nexthop) == 0) {
1327 strcpy(nexthop, msg->cm_fields['D']);
1329 snprintf(filename, sizeof filename,
1330 #ifndef HAVE_SPOOL_DIR
1334 #endif /* HAVE_SPOOL_DIR */
1335 "/network/spoolout/%s", nexthop);
1336 lprintf(CTDL_DEBUG, "Appending to %s\n", filename);
1337 fp = fopen(filename, "ab");
1344 lprintf(CTDL_ERR, "%s: %s\n", filename, strerror(errno));
1347 CtdlFreeMessage(msg);
1351 else { /* invalid destination node name */
1354 "A message you sent could not be delivered due to an invalid destination node"
1355 " name. Please check the address and try sending the message again.\n");
1364 * Check to see if we already have a copy of this message, and
1365 * abort its processing if so. (We used to post a warning to Aide>
1366 * every time this happened, but the network is now so densely
1367 * connected that it's inevitable.)
1369 if (network_usetable(msg) != 0) {
1373 /* Learn network topology from the path */
1374 if ((msg->cm_fields['N'] != NULL) && (msg->cm_fields['P'] != NULL)) {
1375 network_learn_topology(msg->cm_fields['N'],
1376 msg->cm_fields['P']);
1379 /* Is the sending node giving us a very persuasive suggestion about
1380 * which room this message should be saved in? If so, go with that.
1382 if (msg->cm_fields['C'] != NULL) {
1383 safestrncpy(target_room,
1384 msg->cm_fields['C'],
1385 sizeof target_room);
1388 /* Otherwise, does it have a recipient? If so, validate it... */
1389 else if (msg->cm_fields['R'] != NULL) {
1390 recp = validate_recipients(msg->cm_fields['R']);
1391 if (recp != NULL) if (recp->num_error > 0) {
1393 "A message you sent could not be delivered due to an invalid address.\n"
1394 "Please check the address and try sending the message again.\n");
1399 strcpy(target_room, ""); /* no target room if mail */
1402 /* Our last shot at finding a home for this message is to see if
1403 * it has the O field (Originating room) set.
1405 else if (msg->cm_fields['O'] != NULL) {
1406 safestrncpy(target_room,
1407 msg->cm_fields['O'],
1408 sizeof target_room);
1411 /* Strip out fields that are only relevant during transit */
1412 if (msg->cm_fields['D'] != NULL) {
1413 free(msg->cm_fields['D']);
1414 msg->cm_fields['D'] = NULL;
1416 if (msg->cm_fields['C'] != NULL) {
1417 free(msg->cm_fields['C']);
1418 msg->cm_fields['C'] = NULL;
1421 /* save the message into a room */
1422 if (PerformNetprocHooks(msg, target_room) == 0) {
1423 msg->cm_flags = CM_SKIP_HOOKS;
1424 CtdlSubmitMsg(msg, recp, target_room);
1426 CtdlFreeMessage(msg);
1432 * Process a single message from a single file from the inbound queue
1434 void network_process_message(FILE *fp, long msgstart, long msgend) {
1439 hold_pos = ftell(fp);
1440 size = msgend - msgstart + 1;
1441 buffer = malloc(size);
1442 if (buffer != NULL) {
1443 fseek(fp, msgstart, SEEK_SET);
1444 fread(buffer, size, 1, fp);
1445 network_process_buffer(buffer, size);
1449 fseek(fp, hold_pos, SEEK_SET);
1454 * Process a single file from the inbound queue
1456 void network_process_file(char *filename) {
1458 long msgstart = (-1L);
1459 long msgend = (-1L);
1464 fp = fopen(filename, "rb");
1466 lprintf(CTDL_CRIT, "Error opening %s: %s\n",
1467 filename, strerror(errno));
1471 lprintf(CTDL_INFO, "network: processing <%s>\n", filename);
1473 /* Look for messages in the data stream and break them out */
1474 while (ch = getc(fp), ch >= 0) {
1477 if (msgstart >= 0L) {
1478 msgend = msgcur - 1;
1479 network_process_message(fp, msgstart, msgend);
1487 msgend = msgcur - 1;
1488 if (msgstart >= 0L) {
1489 network_process_message(fp, msgstart, msgend);
1498 * Process anything in the inbound queue
1500 void network_do_spoolin(void) {
1506 #ifndef HAVE_SPOOL_DIR
1510 #endif /* HAVE_SPOOL_DIR */
1511 "/network/spoolin");
1512 if (dp == NULL) return;
1514 while (d = readdir(dp), d != NULL) {
1515 if ((strcmp(d->d_name, ".")) && (strcmp(d->d_name, ".."))) {
1516 snprintf(filename, sizeof filename,
1517 #ifndef HAVE_SPOOL_DIR
1521 #endif /* HAVE_SPOOL_DIR */
1522 "/network/spoolin/%s", d->d_name);
1523 network_process_file(filename);
1531 * Delete any files in the outbound queue that were intended
1532 * to be sent to nodes which no longer exist.
1534 void network_purge_spoolout(void) {
1542 #ifndef HAVE_SPOOL_DIR
1546 #endif /* HAVE_SPOOL_DIR */
1547 "/network/spoolout");
1548 if (dp == NULL) return;
1550 while (d = readdir(dp), d != NULL) {
1551 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
1553 snprintf(filename, sizeof filename,
1554 #ifndef HAVE_SPOOL_DIR
1558 #endif /* HAVE_SPOOL_DIR */
1559 "/network/spoolout/%s", d->d_name);
1561 strcpy(nexthop, "");
1562 i = is_valid_node(nexthop, NULL, d->d_name);
1564 if ( (i != 0) || (strlen(nexthop) > 0) ) {
1575 * receive network spool from the remote system
1577 void receive_spool(int sock, char *remote_nodename) {
1579 long bytes_received;
1581 static char pbuf[IGNET_PACKET_SIZE];
1582 char tempfilename[PATH_MAX];
1586 strcpy(tempfilename, tmpnam(NULL));
1587 if (sock_puts(sock, "NDOP") < 0) return;
1588 if (sock_gets(sock, buf) < 0) return;
1589 lprintf(CTDL_DEBUG, "<%s\n", buf);
1590 if (buf[0] != '2') {
1593 download_len = extract_long(&buf[4], 0);
1595 bytes_received = 0L;
1596 fp = fopen(tempfilename, "w");
1598 lprintf(CTDL_CRIT, "cannot open download file locally: %s\n",
1603 while (bytes_received < download_len) {
1604 snprintf(buf, sizeof buf, "READ %ld|%ld",
1606 ((download_len - bytes_received > IGNET_PACKET_SIZE)
1607 ? IGNET_PACKET_SIZE : (download_len - bytes_received)));
1608 if (sock_puts(sock, buf) < 0) {
1610 unlink(tempfilename);
1613 if (sock_gets(sock, buf) < 0) {
1615 unlink(tempfilename);
1618 if (buf[0] == '6') {
1619 plen = extract_long(&buf[4], 0);
1620 if (sock_read(sock, pbuf, plen) < 0) {
1622 unlink(tempfilename);
1625 fwrite((char *) pbuf, plen, 1, fp);
1626 bytes_received = bytes_received + plen;
1631 if (sock_puts(sock, "CLOS") < 0) {
1632 unlink(tempfilename);
1635 if (sock_gets(sock, buf) < 0) {
1636 unlink(tempfilename);
1639 if (download_len > 0)
1640 lprintf(CTDL_NOTICE, "Received %ld octets from <%s>",
1641 download_len, remote_nodename);
1642 lprintf(CTDL_DEBUG, "%s", buf);
1643 /* TODO: make move inline. forking is verry expensive. */
1644 snprintf(buf, sizeof buf, "mv %s "
1645 #ifndef HAVE_SPOOL_DIR
1649 #endif /* HAVE_SPOOL_DIR */
1650 "/network/spoolin/%s.%ld",
1651 tempfilename, remote_nodename, (long) getpid());
1658 * transmit network spool to the remote system
1660 void transmit_spool(int sock, char *remote_nodename)
1665 long bytes_to_write, thisblock, bytes_written;
1669 if (sock_puts(sock, "NUOP") < 0) return;
1670 if (sock_gets(sock, buf) < 0) return;
1671 lprintf(CTDL_DEBUG, "<%s\n", buf);
1672 if (buf[0] != '2') {
1676 snprintf(sfname, sizeof sfname,
1677 #ifndef HAVE_SPOOL_DIR
1681 #endif /* HAVE_SPOOL_DIR */
1682 "/network/spoolout/%s", remote_nodename);
1683 fd = open(sfname, O_RDONLY);
1685 if (errno != ENOENT) {
1686 lprintf(CTDL_CRIT, "cannot open upload file locally: %s\n",
1692 while (plen = (long) read(fd, pbuf, IGNET_PACKET_SIZE), plen > 0L) {
1693 bytes_to_write = plen;
1694 while (bytes_to_write > 0L) {
1695 snprintf(buf, sizeof buf, "WRIT %ld", bytes_to_write);
1696 if (sock_puts(sock, buf) < 0) {
1700 if (sock_gets(sock, buf) < 0) {
1704 thisblock = atol(&buf[4]);
1705 if (buf[0] == '7') {
1706 if (sock_write(sock, pbuf,
1707 (int) thisblock) < 0) {
1711 bytes_to_write -= thisblock;
1712 bytes_written += thisblock;
1721 if (sock_puts(sock, "UCLS 1") < 0) return;
1722 if (sock_gets(sock, buf) < 0) return;
1723 lprintf(CTDL_NOTICE, "Sent %ld octets to <%s>\n",
1724 bytes_written, remote_nodename);
1725 lprintf(CTDL_DEBUG, "<%s\n", buf);
1726 if (buf[0] == '2') {
1727 lprintf(CTDL_DEBUG, "Removing <%s>\n", sfname);
1735 * Poll one Citadel node (called by network_poll_other_citadel_nodes() below)
1737 void network_poll_node(char *node, char *secret, char *host, char *port) {
1741 if (network_talking_to(node, NTT_CHECK)) return;
1742 network_talking_to(node, NTT_ADD);
1743 lprintf(CTDL_NOTICE, "Connecting to <%s> at %s:%s\n", node, host, port);
1745 sock = sock_connect(host, port, "tcp");
1747 lprintf(CTDL_ERR, "Could not connect: %s\n", strerror(errno));
1748 network_talking_to(node, NTT_REMOVE);
1752 lprintf(CTDL_DEBUG, "Connected!\n");
1754 /* Read the server greeting */
1755 if (sock_gets(sock, buf) < 0) goto bail;
1756 lprintf(CTDL_DEBUG, ">%s\n", buf);
1758 /* Identify ourselves */
1759 snprintf(buf, sizeof buf, "NETP %s|%s", config.c_nodename, secret);
1760 lprintf(CTDL_DEBUG, "<%s\n", buf);
1761 if (sock_puts(sock, buf) <0) goto bail;
1762 if (sock_gets(sock, buf) < 0) goto bail;
1763 lprintf(CTDL_DEBUG, ">%s\n", buf);
1764 if (buf[0] != '2') goto bail;
1766 /* At this point we are authenticated. */
1767 receive_spool(sock, node);
1768 transmit_spool(sock, node);
1770 sock_puts(sock, "QUIT");
1771 bail: sock_close(sock);
1772 network_talking_to(node, NTT_REMOVE);
1778 * Poll other Citadel nodes and transfer inbound/outbound network data.
1779 * Set "full" to nonzero to force a poll of every node, or to zero to poll
1780 * only nodes to which we have data to send.
1782 void network_poll_other_citadel_nodes(int full_poll) {
1790 char spoolfile[256];
1792 if (working_ignetcfg == NULL) {
1793 lprintf(CTDL_DEBUG, "No nodes defined - not polling\n");
1797 /* Use the string tokenizer to grab one line at a time */
1798 for (i=0; i<num_tokens(working_ignetcfg, '\n'); ++i) {
1799 extract_token(linebuf, working_ignetcfg, i, '\n', sizeof linebuf);
1800 extract_token(node, linebuf, 0, '|', sizeof node);
1801 extract_token(secret, linebuf, 1, '|', sizeof secret);
1802 extract_token(host, linebuf, 2, '|', sizeof host);
1803 extract_token(port, linebuf, 3, '|', sizeof port);
1804 if ( (strlen(node) > 0) && (strlen(secret) > 0)
1805 && (strlen(host) > 0) && strlen(port) > 0) {
1808 snprintf(spoolfile, sizeof spoolfile,
1809 #ifndef HAVE_SPOOL_DIR
1814 "/network/spoolout/%s", node);
1815 if (access(spoolfile, R_OK) == 0) {
1820 network_poll_node(node, secret, host, port);
1831 * It's ok if these directories already exist. Just fail silently.
1833 void create_spool_dirs(void) {
1834 #ifndef HAVE_SPOOL_DIR
1835 mkdir("./network", 0700);
1836 mkdir("./network/systems", 0700);
1837 mkdir("./network/spoolin", 0700);
1838 mkdir("./network/spoolout", 0700);
1840 mkdir(SPOOL_DIR "/network", 0700);
1841 mkdir(SPOOL_DIR "/network/systems", 0700);
1842 mkdir(SPOOL_DIR "/network/spoolin", 0700);
1843 mkdir(SPOOL_DIR "/network/spoolout", 0700);
1844 #endif /* HAVE_SPOOL_DIR */
1852 * network_do_queue()
1854 * Run through the rooms doing various types of network stuff.
1856 void network_do_queue(void) {
1857 static time_t last_run = 0L;
1858 struct RoomProcList *ptr;
1859 int full_processing = 1;
1862 * Run the full set of processing tasks no more frequently
1863 * than once every n seconds
1865 if ( (time(NULL) - last_run) < config.c_net_freq ) {
1866 full_processing = 0;
1869 create_spool_dirs();
1872 * This is a simple concurrency check to make sure only one queue run
1873 * is done at a time. We could do this with a mutex, but since we
1874 * don't really require extremely fine granularity here, we'll do it
1875 * with a static variable instead.
1877 if (doing_queue) return;
1880 /* Load the IGnet Configuration into memory */
1881 load_working_ignetcfg();
1884 * Poll other Citadel nodes. Maybe. If "full_processing" is set
1885 * then we poll everyone. Otherwise we only poll nodes we have stuff
1888 network_poll_other_citadel_nodes(full_processing);
1891 * Load the network map and filter list into memory.
1894 filterlist = load_filter_list();
1897 * Go ahead and run the queue
1899 if (full_processing) {
1900 lprintf(CTDL_DEBUG, "network: loading outbound queue\n");
1901 ForEachRoom(network_queue_room, NULL);
1903 lprintf(CTDL_DEBUG, "network: running outbound queue\n");
1904 while (rplist != NULL) {
1905 network_spoolout_room(rplist->name);
1907 rplist = rplist->next;
1912 lprintf(CTDL_DEBUG, "network: processing inbound queue\n");
1913 network_do_spoolin();
1915 /* Save the network map back to disk */
1916 write_network_map();
1918 /* Free the filter list in memory */
1919 free_filter_list(filterlist);
1922 network_purge_spoolout();
1924 lprintf(CTDL_DEBUG, "network: queue run completed\n");
1926 if (full_processing) {
1927 last_run = time(NULL);
1935 * cmd_netp() - authenticate to the server as another Citadel node polling
1936 * for network traffic
1938 void cmd_netp(char *cmdbuf)
1948 extract_token(node, cmdbuf, 0, '|', sizeof node);
1949 extract_token(pass, cmdbuf, 1, '|', sizeof pass);
1952 lprintf(CTDL_WARNING, "Network node <%s> refused - spooling", node);
1953 cprintf("%d spooling - try again in a few minutes\n",
1954 ERROR + RESOURCE_BUSY);
1958 /* load the IGnet Configuration to check node validity */
1959 load_working_ignetcfg();
1960 v = is_valid_node(nexthop, secret, node);
1963 lprintf(CTDL_WARNING, "Unknown node <%s>\n", node);
1964 cprintf("%d authentication failed\n",
1965 ERROR + PASSWORD_REQUIRED);
1969 if (strcasecmp(pass, secret)) {
1970 lprintf(CTDL_WARNING, "Bad password for network node <%s>", node);
1971 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
1975 if (network_talking_to(node, NTT_CHECK)) {
1976 lprintf(CTDL_WARNING, "Duplicate session for network node <%s>", node);
1977 cprintf("%d Already talking to %s right now\n", ERROR + RESOURCE_BUSY, node);
1981 safestrncpy(CC->net_node, node, sizeof CC->net_node);
1982 network_talking_to(node, NTT_ADD);
1983 lprintf(CTDL_NOTICE, "Network node <%s> logged in\n", CC->net_node);
1984 cprintf("%d authenticated as network node '%s'\n", CIT_OK,
1989 * Module entry point
1991 char *serv_network_init(void)
1993 create_spool_dirs();
1994 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
1995 CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");
1996 CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");
1997 CtdlRegisterProtoHook(cmd_nsyn, "NSYN", "Synchronize room to node");
1998 CtdlRegisterSessionHook(network_do_queue, EVT_TIMER);