ebac0f6b4de0a4d859316532edc3939efce07804
[citadel.git] / citadel / modules / network / serv_network.c
1 /*
2  * $Id$ 
3  *
4  * This module handles shared rooms, inter-Citadel mail, and outbound
5  * mailing list processing.
6  *
7  * Copyright (C) 2000-2005 by Art Cancro and others.
8  * This code is released under the terms of the GNU General Public License.
9  *
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.
16  *
17  */
18
19 /*
20  * Duration of time (in seconds) after which pending list subscribe/unsubscribe
21  * requests that have not been confirmed will be deleted.
22  */
23 #define EXP     259200  /* three days */
24
25 #include "sysdep.h"
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <stdio.h>
29 #include <fcntl.h>
30 #include <ctype.h>
31 #include <signal.h>
32 #include <pwd.h>
33 #include <errno.h>
34 #include <sys/stat.h>
35 #include <sys/types.h>
36 #include <dirent.h>
37 #if TIME_WITH_SYS_TIME
38 # include <sys/time.h>
39 # include <time.h>
40 #else
41 # if HAVE_SYS_TIME_H
42 #  include <sys/time.h>
43 # else
44 #  include <time.h>
45 # endif
46 #endif
47
48 #include <sys/wait.h>
49 #include <string.h>
50 #include <limits.h>
51 #include <libcitadel.h>
52 #include "citadel.h"
53 #include "server.h"
54 #include "citserver.h"
55 #include "support.h"
56 #include "config.h"
57 #include "room_ops.h"
58 #include "user_ops.h"
59 #include "policy.h"
60 #include "database.h"
61 #include "msgbase.h"
62 #include "internet_addressing.h"
63 #include "serv_network.h"
64 #include "clientsocket.h"
65 #include "file_ops.h"
66 #include "citadel_dirs.h"
67
68 #ifndef HAVE_SNPRINTF
69 #include "snprintf.h"
70 #endif
71
72
73 #include "ctdl_module.h"
74
75
76
77 /* Nonzero while we are doing network processing */
78 static int doing_queue = 0;
79
80 /*
81  * When we do network processing, it's accomplished in two passes; one to
82  * gather a list of rooms and one to actually do them.  It's ok that rplist
83  * is global; we have a mutex that keeps it safe.
84  */
85 struct RoomProcList *rplist = NULL;
86
87 /*
88  * We build a map of network nodes during processing.
89  */
90 NetMap *the_netmap = NULL;
91 int netmap_changed = 0;
92 char *working_ignetcfg = NULL;
93
94 /*
95  * Load or refresh the Citadel network (IGnet) configuration for this node.
96  */
97 void load_working_ignetcfg(void) {
98         char *cfg;
99         char *oldcfg;
100
101         cfg = CtdlGetSysConfig(IGNETCFG);
102         if (cfg == NULL) {
103                 cfg = strdup("");
104         }
105
106         oldcfg = working_ignetcfg;
107         working_ignetcfg = cfg;
108         if (oldcfg != NULL) {
109                 free(oldcfg);
110         }
111 }
112
113
114
115
116
117 /*
118  * Keep track of what messages to reject
119  */
120 FilterList *load_filter_list(void) {
121         char *serialized_list = NULL;
122         int i;
123         char buf[SIZ];
124         FilterList *newlist = NULL;
125         FilterList *nptr;
126
127         serialized_list = CtdlGetSysConfig(FILTERLIST);
128         if (serialized_list == NULL) return(NULL); /* if null, no entries */
129
130         /* Use the string tokenizer to grab one line at a time */
131         for (i=0; i<num_tokens(serialized_list, '\n'); ++i) {
132                 extract_token(buf, serialized_list, i, '\n', sizeof buf);
133                 nptr = (FilterList *) malloc(sizeof(FilterList));
134                 extract_token(nptr->fl_user, buf, 0, '|', sizeof nptr->fl_user);
135                 striplt(nptr->fl_user);
136                 extract_token(nptr->fl_room, buf, 1, '|', sizeof nptr->fl_room);
137                 striplt(nptr->fl_room);
138                 extract_token(nptr->fl_node, buf, 2, '|', sizeof nptr->fl_node);
139                 striplt(nptr->fl_node);
140
141                 /* Cowardly refuse to add an any/any/any entry that would
142                  * end up filtering every single message.
143                  */
144                 if (IsEmptyStr(nptr->fl_user) && 
145                     IsEmptyStr(nptr->fl_room) &&
146                     IsEmptyStr(nptr->fl_node)) {
147                         free(nptr);
148                 }
149                 else {
150                         nptr->next = newlist;
151                         newlist = nptr;
152                 }
153         }
154
155         free(serialized_list);
156         return newlist;
157 }
158
159
160 void free_filter_list(FilterList *fl) {
161         if (fl == NULL) return;
162         free_filter_list(fl->next);
163         free(fl);
164 }
165
166
167
168 /*
169  * Check the use table.  This is a list of messages which have recently
170  * arrived on the system.  It is maintained and queried to prevent the same
171  * message from being entered into the database multiple times if it happens
172  * to arrive multiple times by accident.
173  */
174 int network_usetable(struct CtdlMessage *msg) {
175
176         char msgid[SIZ];
177         struct cdbdata *cdbut;
178         struct UseTable ut;
179
180         /* Bail out if we can't generate a message ID */
181         if (msg == NULL) {
182                 return(0);
183         }
184         if (msg->cm_fields['I'] == NULL) {
185                 return(0);
186         }
187         if (IsEmptyStr(msg->cm_fields['I'])) {
188                 return(0);
189         }
190
191         /* Generate the message ID */
192         strcpy(msgid, msg->cm_fields['I']);
193         if (haschar(msgid, '@') == 0) {
194                 strcat(msgid, "@");
195                 if (msg->cm_fields['N'] != NULL) {
196                         strcat(msgid, msg->cm_fields['N']);
197                 }
198                 else {
199                         return(0);
200                 }
201         }
202
203         cdbut = cdb_fetch(CDB_USETABLE, msgid, strlen(msgid));
204         if (cdbut != NULL) {
205                 cdb_free(cdbut);
206                 return(1);
207         }
208
209         /* If we got to this point, it's unique: add it. */
210         strcpy(ut.ut_msgid, msgid);
211         ut.ut_timestamp = time(NULL);
212         cdb_store(CDB_USETABLE, msgid, strlen(msgid),
213                 &ut, sizeof(struct UseTable) );
214         return(0);
215 }
216
217
218 /* 
219  * Read the network map from its configuration file into memory.
220  */
221 void read_network_map(void) {
222         char *serialized_map = NULL;
223         int i;
224         char buf[SIZ];
225         NetMap *nmptr;
226
227         serialized_map = CtdlGetSysConfig(IGNETMAP);
228         if (serialized_map == NULL) return;     /* if null, no entries */
229
230         /* Use the string tokenizer to grab one line at a time */
231         for (i=0; i<num_tokens(serialized_map, '\n'); ++i) {
232                 extract_token(buf, serialized_map, i, '\n', sizeof buf);
233                 nmptr = (NetMap *) malloc(sizeof(NetMap));
234                 extract_token(nmptr->nodename, buf, 0, '|', sizeof nmptr->nodename);
235                 nmptr->lastcontact = extract_long(buf, 1);
236                 extract_token(nmptr->nexthop, buf, 2, '|', sizeof nmptr->nexthop);
237                 nmptr->next = the_netmap;
238                 the_netmap = nmptr;
239         }
240
241         free(serialized_map);
242         netmap_changed = 0;
243 }
244
245
246 /*
247  * Write the network map from memory back to the configuration file.
248  */
249 void write_network_map(void) {
250         char *serialized_map = NULL;
251         NetMap *nmptr;
252
253
254         if (netmap_changed) {
255                 serialized_map = strdup("");
256         
257                 if (the_netmap != NULL) {
258                         for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
259                                 serialized_map = realloc(serialized_map,
260                                                         (strlen(serialized_map)+SIZ) );
261                                 if (!IsEmptyStr(nmptr->nodename)) {
262                                         snprintf(&serialized_map[strlen(serialized_map)],
263                                                 SIZ,
264                                                 "%s|%ld|%s\n",
265                                                 nmptr->nodename,
266                                                 (long)nmptr->lastcontact,
267                                                 nmptr->nexthop);
268                                 }
269                         }
270                 }
271
272                 CtdlPutSysConfig(IGNETMAP, serialized_map);
273                 free(serialized_map);
274         }
275
276         /* Now free the list */
277         while (the_netmap != NULL) {
278                 nmptr = the_netmap->next;
279                 free(the_netmap);
280                 the_netmap = nmptr;
281         }
282         netmap_changed = 0;
283 }
284
285
286
287 /* 
288  * Check the network map and determine whether the supplied node name is
289  * valid.  If it is not a neighbor node, supply the name of a neighbor node
290  * which is the next hop.  If it *is* a neighbor node, we also fill in the
291  * shared secret.
292  */
293 int is_valid_node(char *nexthop, char *secret, char *node) {
294         int i;
295         char linebuf[SIZ];
296         char buf[SIZ];
297         int retval;
298         NetMap *nmptr;
299
300         if (node == NULL) {
301                 return(-1);
302         }
303
304         /*
305          * First try the neighbor nodes
306          */
307         if (working_ignetcfg == NULL) {
308                 lprintf(CTDL_ERR, "working_ignetcfg is NULL!\n");
309                 if (nexthop != NULL) {
310                         strcpy(nexthop, "");
311                 }
312                 return(-1);
313         }
314
315         retval = (-1);
316         if (nexthop != NULL) {
317                 strcpy(nexthop, "");
318         }
319
320         /* Use the string tokenizer to grab one line at a time */
321         for (i=0; i<num_tokens(working_ignetcfg, '\n'); ++i) {
322                 extract_token(linebuf, working_ignetcfg, i, '\n', sizeof linebuf);
323                 extract_token(buf, linebuf, 0, '|', sizeof buf);
324                 if (!strcasecmp(buf, node)) {
325                         if (nexthop != NULL) {
326                                 strcpy(nexthop, "");
327                         }
328                         if (secret != NULL) {
329                                 extract_token(secret, linebuf, 1, '|', 256);
330                         }
331                         retval = 0;
332                 }
333         }
334
335         if (retval == 0) {
336                 return(retval);         /* yup, it's a direct neighbor */
337         }
338
339         /*      
340          * If we get to this point we have to see if we know the next hop
341          */
342         if (the_netmap != NULL) {
343                 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
344                         if (!strcasecmp(nmptr->nodename, node)) {
345                                 if (nexthop != NULL) {
346                                         strcpy(nexthop, nmptr->nexthop);
347                                 }
348                                 return(0);
349                         }
350                 }
351         }
352
353         /*
354          * If we get to this point, the supplied node name is bogus.
355          */
356         lprintf(CTDL_ERR, "Invalid node name <%s>\n", node);
357         return(-1);
358 }
359
360
361
362
363
364 void cmd_gnet(char *argbuf) {
365         char filename[SIZ];
366         char buf[SIZ];
367         FILE *fp;
368
369         if ( (CC->room.QRflags & QR_MAILBOX) && (CC->user.usernum == atol(CC->room.QRname)) ) {
370                 /* users can edit the netconfigs for their own mailbox rooms */
371         }
372         else if (CtdlAccessCheck(ac_room_aide)) return;
373
374         assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
375         cprintf("%d Network settings for room #%ld <%s>\n",
376                 LISTING_FOLLOWS,
377                 CC->room.QRnumber, CC->room.QRname);
378
379         fp = fopen(filename, "r");
380         if (fp != NULL) {
381                 while (fgets(buf, sizeof buf, fp) != NULL) {
382                         buf[strlen(buf)-1] = 0;
383                         cprintf("%s\n", buf);
384                 }
385                 fclose(fp);
386         }
387
388         cprintf("000\n");
389 }
390
391
392 void cmd_snet(char *argbuf) {
393         char tempfilename[SIZ];
394         char filename[SIZ];
395         char buf[SIZ];
396         FILE *fp, *newfp;
397
398         unbuffer_output();
399
400         if ( (CC->room.QRflags & QR_MAILBOX) && (CC->user.usernum == atol(CC->room.QRname)) ) {
401                 /* users can edit the netconfigs for their own mailbox rooms */
402         }
403         else if (CtdlAccessCheck(ac_room_aide)) return;
404
405         CtdlMakeTempFileName(tempfilename, sizeof tempfilename);
406         assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
407
408         fp = fopen(tempfilename, "w");
409         if (fp == NULL) {
410                 cprintf("%d Cannot open %s: %s\n",
411                         ERROR + INTERNAL_ERROR,
412                         tempfilename,
413                         strerror(errno));
414         }
415
416         cprintf("%d %s\n", SEND_LISTING, tempfilename);
417         while (client_getln(buf, sizeof buf), strcmp(buf, "000")) {
418                 fprintf(fp, "%s\n", buf);
419         }
420         fclose(fp);
421
422         /* Now copy the temp file to its permanent location.
423          * (We copy instead of link because they may be on different filesystems)
424          */
425         begin_critical_section(S_NETCONFIGS);
426         fp = fopen(tempfilename, "r");
427         if (fp != NULL) {
428                 newfp = fopen(filename, "w");
429                 if (newfp != NULL) {
430                         while (fgets(buf, sizeof buf, fp) != NULL) {
431                                 fprintf(newfp, "%s", buf);
432                         }
433                         fclose(newfp);
434                 }
435                 fclose(fp);
436         }
437         end_critical_section(S_NETCONFIGS);
438         unlink(tempfilename);
439 }
440
441
442 /*
443  * Deliver digest messages
444  */
445 void network_deliver_digest(SpoolControl *sc) {
446         char buf[SIZ];
447         int i;
448         struct CtdlMessage *msg = NULL;
449         long msglen;
450         char *recps = NULL;
451         size_t recps_len = SIZ;
452         struct recptypes *valid;
453         namelist *nptr;
454
455         if (sc->num_msgs_spooled < 1) {
456                 fclose(sc->digestfp);
457                 sc->digestfp = NULL;
458                 return;
459         }
460
461         msg = malloc(sizeof(struct CtdlMessage));
462         memset(msg, 0, sizeof(struct CtdlMessage));
463         msg->cm_magic = CTDLMESSAGE_MAGIC;
464         msg->cm_format_type = FMT_RFC822;
465         msg->cm_anon_type = MES_NORMAL;
466
467         sprintf(buf, "%ld", time(NULL));
468         msg->cm_fields['T'] = strdup(buf);
469         msg->cm_fields['A'] = strdup(CC->room.QRname);
470         snprintf(buf, sizeof buf, "[%s]", CC->room.QRname);
471         msg->cm_fields['U'] = strdup(buf);
472         sprintf(buf, "room_%s@%s", CC->room.QRname, config.c_fqdn);
473         for (i=0; buf[i]; ++i) {
474                 if (isspace(buf[i])) buf[i]='_';
475                 buf[i] = tolower(buf[i]);
476         }
477         msg->cm_fields['F'] = strdup(buf);
478         msg->cm_fields['R'] = strdup(buf);
479
480         /*
481          * Go fetch the contents of the digest
482          */
483         fseek(sc->digestfp, 0L, SEEK_END);
484         msglen = ftell(sc->digestfp);
485
486         msg->cm_fields['M'] = malloc(msglen + 1);
487         fseek(sc->digestfp, 0L, SEEK_SET);
488         fread(msg->cm_fields['M'], (size_t)msglen, 1, sc->digestfp);
489         msg->cm_fields['M'][msglen] = 0;
490
491         fclose(sc->digestfp);
492         sc->digestfp = NULL;
493
494         /* Now generate the delivery instructions */
495
496         /* 
497          * Figure out how big a buffer we need to allocate
498          */
499         for (nptr = sc->digestrecps; nptr != NULL; nptr = nptr->next) {
500                 recps_len = recps_len + strlen(nptr->name) + 2;
501         }
502         
503         recps = malloc(recps_len);
504
505         if (recps == NULL) {
506                 lprintf(CTDL_EMERG, "Cannot allocate %ld bytes for recps...\n", (long)recps_len);
507                 abort();
508         }
509
510         strcpy(recps, "");
511
512         /* Each recipient */
513         for (nptr = sc->digestrecps; nptr != NULL; nptr = nptr->next) {
514                 if (nptr != sc->digestrecps) {
515                         strcat(recps, ",");
516                 }
517                 strcat(recps, nptr->name);
518         }
519
520         /* Now submit the message */
521         valid = validate_recipients(recps, NULL, 0);
522         free(recps);
523         CtdlSubmitMsg(msg, valid, NULL);
524         CtdlFreeMessage(msg);
525         free_recipients(valid);
526 }
527
528
529 /*
530  * Deliver list messages to everyone on the list ... efficiently
531  */
532 void network_deliver_list(struct CtdlMessage *msg, SpoolControl *sc) {
533         char *recps = NULL;
534         size_t recps_len = SIZ;
535         struct recptypes *valid;
536         namelist *nptr;
537
538         /* Don't do this if there were no recipients! */
539         if (sc->listrecps == NULL) return;
540
541         /* Now generate the delivery instructions */
542
543         /* 
544          * Figure out how big a buffer we need to allocate
545          */
546         for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
547                 recps_len = recps_len + strlen(nptr->name) + 2;
548         }
549         
550         recps = malloc(recps_len);
551
552         if (recps == NULL) {
553                 lprintf(CTDL_EMERG, "Cannot allocate %ld bytes for recps...\n", (long)recps_len);
554                 abort();
555         }
556
557         strcpy(recps, "");
558
559         /* Each recipient */
560         for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
561                 if (nptr != sc->listrecps) {
562                         strcat(recps, ",");
563                 }
564                 strcat(recps, nptr->name);
565         }
566
567         /* Now submit the message */
568         valid = validate_recipients(recps, NULL, 0);
569         free(recps);
570         CtdlSubmitMsg(msg, valid, NULL);
571         free_recipients(valid);
572         /* Do not call CtdlFreeMessage(msg) here; the caller will free it. */
573 }
574
575
576
577
578 /*
579  * Spools out one message from the list.
580  */
581 void network_spool_msg(long msgnum, void *userdata) {
582         SpoolControl *sc;
583         int i;
584         char *newpath = NULL;
585         size_t instr_len = SIZ;
586         struct CtdlMessage *msg = NULL;
587         namelist *nptr;
588         maplist *mptr;
589         struct ser_ret sermsg;
590         FILE *fp;
591         char filename[SIZ];
592         char buf[SIZ];
593         int bang = 0;
594         int send = 1;
595         int delete_after_send = 0;      /* Set to 1 to delete after spooling */
596         int ok_to_participate = 0;
597         struct recptypes *valid;
598
599         sc = (SpoolControl *)userdata;
600
601         /*
602          * Process mailing list recipients
603          */
604         instr_len = SIZ;
605         if (sc->listrecps != NULL) {
606                 /* Fetch the message.  We're going to need to modify it
607                  * in order to insert the [list name] in it, etc.
608                  */
609                 msg = CtdlFetchMessage(msgnum, 1);
610                 if (msg != NULL) {
611
612                         /* Prepend "[List name]" to the subject */
613                         if (msg->cm_fields['U'] == NULL) {
614                                 msg->cm_fields['U'] = strdup("(no subject)");
615                         }
616                         snprintf(buf, sizeof buf, "[%s] %s", CC->room.QRname, msg->cm_fields['U']);
617                         free(msg->cm_fields['U']);
618                         msg->cm_fields['U'] = strdup(buf);
619
620                         /* Set the recipient of the list message to the
621                          * email address of the room itself.
622                          * FIXME ... I want to be able to pick any address
623                          */
624                         if (msg->cm_fields['R'] != NULL) {
625                                 free(msg->cm_fields['R']);
626                         }
627                         msg->cm_fields['R'] = malloc(256);
628                         snprintf(msg->cm_fields['R'], 256,
629                                 "room_%s@%s", CC->room.QRname,
630                                 config.c_fqdn);
631                         for (i=0; msg->cm_fields['R'][i]; ++i) {
632                                 if (isspace(msg->cm_fields['R'][i])) {
633                                         msg->cm_fields['R'][i] = '_';
634                                 }
635                         }
636
637                         /* Handle delivery */
638                         network_deliver_list(msg, sc);
639                         CtdlFreeMessage(msg);
640                 }
641         }
642
643         /*
644          * Process digest recipients
645          */
646         if ((sc->digestrecps != NULL) && (sc->digestfp != NULL)) {
647                 msg = CtdlFetchMessage(msgnum, 1);
648                 if (msg != NULL) {
649                         fprintf(sc->digestfp,   " -----------------------------------"
650                                                 "------------------------------------"
651                                                 "-------\n");
652                         fprintf(sc->digestfp, "From: ");
653                         if (msg->cm_fields['A'] != NULL) {
654                                 fprintf(sc->digestfp, "%s ", msg->cm_fields['A']);
655                         }
656                         if (msg->cm_fields['F'] != NULL) {
657                                 fprintf(sc->digestfp, "<%s> ", msg->cm_fields['F']);
658                         }
659                         else if (msg->cm_fields['N'] != NULL) {
660                                 fprintf(sc->digestfp, "@%s ", msg->cm_fields['N']);
661                         }
662                         fprintf(sc->digestfp, "\n");
663                         if (msg->cm_fields['U'] != NULL) {
664                                 fprintf(sc->digestfp, "Subject: %s\n", msg->cm_fields['U']);
665                         }
666
667                         CC->redirect_buffer = malloc(SIZ);
668                         CC->redirect_len = 0;
669                         CC->redirect_alloc = SIZ;
670
671                         safestrncpy(CC->preferred_formats, "text/plain", sizeof CC->preferred_formats);
672                         CtdlOutputPreLoadedMsg(msg, MT_CITADEL, HEADERS_NONE, 0, 0);
673
674                         striplt(CC->redirect_buffer);
675                         fprintf(sc->digestfp, "\n%s\n", CC->redirect_buffer);
676
677                         free(CC->redirect_buffer);
678                         CC->redirect_buffer = NULL;
679                         CC->redirect_len = 0;
680                         CC->redirect_alloc = 0;
681
682                         sc->num_msgs_spooled += 1;
683                         free(msg);
684                 }
685         }
686
687         /*
688          * Process client-side list participations for this room
689          */
690         instr_len = SIZ;
691         if (sc->participates != NULL) {
692                 msg = CtdlFetchMessage(msgnum, 1);
693                 if (msg != NULL) {
694
695                         /* Only send messages which originated on our own Citadel
696                          * network, otherwise we'll end up sending the remote
697                          * mailing list's messages back to it, which is rude...
698                          */
699                         ok_to_participate = 0;
700                         if (msg->cm_fields['N'] != NULL) {
701                                 if (!strcasecmp(msg->cm_fields['N'], config.c_nodename)) {
702                                         ok_to_participate = 1;
703                                 }
704                                 if (is_valid_node(NULL, NULL, msg->cm_fields['N']) == 0) {
705                                         ok_to_participate = 1;
706                                 }
707                         }
708                         if (ok_to_participate) {
709                                 if (msg->cm_fields['F'] != NULL) {
710                                         free(msg->cm_fields['F']);
711                                 }
712                                 msg->cm_fields['F'] = malloc(SIZ);
713                                 /* Replace the Internet email address of the actual
714                                 * author with the email address of the room itself,
715                                 * so the remote listserv doesn't reject us.
716                                 * FIXME ... I want to be able to pick any address
717                                 */
718                                 snprintf(msg->cm_fields['F'], SIZ,
719                                         "room_%s@%s", CC->room.QRname,
720                                         config.c_fqdn);
721                                 for (i=0; msg->cm_fields['F'][i]; ++i) {
722                                         if (isspace(msg->cm_fields['F'][i])) {
723                                                 msg->cm_fields['F'][i] = '_';
724                                         }
725                                 }
726
727                                 /* 
728                                  * Figure out how big a buffer we need to allocate
729                                  */
730                                 for (nptr = sc->participates; nptr != NULL; nptr = nptr->next) {
731
732                                         if (msg->cm_fields['R'] == NULL) {
733                                                 free(msg->cm_fields['R']);
734                                         }
735                                         msg->cm_fields['R'] = strdup(nptr->name);
736         
737                                         valid = validate_recipients(nptr->name, NULL, 0);
738                                         CtdlSubmitMsg(msg, valid, "");
739                                         free_recipients(valid);
740                                 }
741                         
742                         }
743                         CtdlFreeMessage(msg);
744                 }
745         }
746         
747         /*
748          * Process IGnet push shares
749          */
750         msg = CtdlFetchMessage(msgnum, 1);
751         if (msg != NULL) {
752                 size_t newpath_len;
753
754                 /* Prepend our node name to the Path field whenever
755                  * sending a message to another IGnet node
756                  */
757                 if (msg->cm_fields['P'] == NULL) {
758                         msg->cm_fields['P'] = strdup("username");
759                 }
760                 newpath_len = strlen(msg->cm_fields['P']) +
761                          strlen(config.c_nodename) + 2;
762                 newpath = malloc(newpath_len);
763                 snprintf(newpath, newpath_len, "%s!%s",
764                          config.c_nodename, msg->cm_fields['P']);
765                 free(msg->cm_fields['P']);
766                 msg->cm_fields['P'] = newpath;
767
768                 /*
769                  * Determine if this message is set to be deleted
770                  * after sending out on the network
771                  */
772                 if (msg->cm_fields['S'] != NULL) {
773                         if (!strcasecmp(msg->cm_fields['S'], "CANCEL")) {
774                                 delete_after_send = 1;
775                         }
776                 }
777
778                 /* Now send it to every node */
779                 if (sc->ignet_push_shares != NULL)
780                   for (mptr = sc->ignet_push_shares; mptr != NULL;
781                     mptr = mptr->next) {
782
783                         send = 1;
784
785                         /* Check for valid node name */
786                         if (is_valid_node(NULL, NULL, mptr->remote_nodename) != 0) {
787                                 lprintf(CTDL_ERR, "Invalid node <%s>\n",
788                                         mptr->remote_nodename);
789                                 send = 0;
790                         }
791
792                         /* Check for split horizon */
793                         lprintf(CTDL_DEBUG, "Path is %s\n", msg->cm_fields['P']);
794                         bang = num_tokens(msg->cm_fields['P'], '!');
795                         if (bang > 1) for (i=0; i<(bang-1); ++i) {
796                                 extract_token(buf, msg->cm_fields['P'],
797                                         i, '!', sizeof buf);
798                                 if (!strcasecmp(buf, mptr->remote_nodename)) {
799                                         send = 0;
800                                 }
801                         }
802
803                         /* Send the message */
804                         if (send == 1) {
805
806                                 /*
807                                  * Force the message to appear in the correct room
808                                  * on the far end by setting the C field correctly
809                                  */
810                                 if (msg->cm_fields['C'] != NULL) {
811                                         free(msg->cm_fields['C']);
812                                 }
813                                 if (!IsEmptyStr(mptr->remote_roomname)) {
814                                         msg->cm_fields['C'] = strdup(mptr->remote_roomname);
815                                 }
816                                 else {
817                                         msg->cm_fields['C'] = strdup(CC->room.QRname);
818                                 }
819
820                                 /* serialize it for transmission */
821                                 serialize_message(&sermsg, msg);
822                                 if (sermsg.len > 0) {
823
824                                         /* write it to the spool file */
825                                         snprintf(filename, sizeof filename,"%s/%s",
826                                                         ctdl_netout_dir,
827                                                         mptr->remote_nodename);
828                                         lprintf(CTDL_DEBUG, "Appending to %s\n", filename);
829                                         fp = fopen(filename, "ab");
830                                         if (fp != NULL) {
831                                                 fwrite(sermsg.ser,
832                                                         sermsg.len, 1, fp);
833                                                 fclose(fp);
834                                         }
835                                         else {
836                                                 lprintf(CTDL_ERR, "%s: %s\n", filename, strerror(errno));
837                                         }
838         
839                                         /* free the serialized version */
840                                         free(sermsg.ser);
841                                 }
842
843                         }
844                 }
845                 CtdlFreeMessage(msg);
846         }
847
848         /* update lastsent */
849         sc->lastsent = msgnum;
850
851         /* Delete this message if delete-after-send is set */
852         if (delete_after_send) {
853                 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");
854         }
855
856 }
857         
858
859 int read_spoolcontrol_file(SpoolControl **scc, char *filename)
860 {
861         FILE *fp;
862         char instr[SIZ];
863         char buf[SIZ];
864         char nodename[256];
865         char roomname[ROOMNAMELEN];
866         char nexthop[256];
867         size_t miscsize = 0;
868         size_t linesize = 0;
869         int skipthisline = 0;
870         namelist *nptr = NULL;
871         maplist *mptr = NULL;
872         SpoolControl *sc;
873
874         fp = fopen(filename, "r");
875         if (fp == NULL) {
876                 return 0;
877         }
878         sc = malloc(sizeof(SpoolControl));
879         memset(sc, 0, sizeof(SpoolControl));
880         *scc = sc;
881
882         while (fgets(buf, sizeof buf, fp) != NULL) {
883                 buf[strlen(buf)-1] = 0;
884
885                 extract_token(instr, buf, 0, '|', sizeof instr);
886                 if (!strcasecmp(instr, "lastsent")) {
887                         sc->lastsent = extract_long(buf, 1);
888                 }
889                 else if (!strcasecmp(instr, "listrecp")) {
890                         nptr = (namelist *)
891                                 malloc(sizeof(namelist));
892                         nptr->next = sc->listrecps;
893                         extract_token(nptr->name, buf, 1, '|', sizeof nptr->name);
894                         sc->listrecps = nptr;
895                 }
896                 else if (!strcasecmp(instr, "participate")) {
897                         nptr = (namelist *)
898                                 malloc(sizeof(namelist));
899                         nptr->next = sc->participates;
900                         extract_token(nptr->name, buf, 1, '|', sizeof nptr->name);
901                         sc->participates = nptr;
902                 }
903                 else if (!strcasecmp(instr, "digestrecp")) {
904                         nptr = (namelist *)
905                                 malloc(sizeof(namelist));
906                         nptr->next = sc->digestrecps;
907                         extract_token(nptr->name, buf, 1, '|', sizeof nptr->name);
908                         sc->digestrecps = nptr;
909                 }
910                 else if (!strcasecmp(instr, "ignet_push_share")) {
911                         /* by checking each node's validity, we automatically
912                          * purge nodes which do not exist from room network
913                          * configurations at this time.
914                          */
915                         extract_token(nodename, buf, 1, '|', sizeof nodename);
916                         extract_token(roomname, buf, 2, '|', sizeof roomname);
917                         strcpy(nexthop, "xxx");
918                         if (is_valid_node(nexthop, NULL, nodename) == 0) {
919                                 if (IsEmptyStr(nexthop)) {
920                                         mptr = (maplist *)
921                                                 malloc(sizeof(maplist));
922                                         mptr->next = sc->ignet_push_shares;
923                                         strcpy(mptr->remote_nodename, nodename);
924                                         strcpy(mptr->remote_roomname, roomname);
925                                         sc->ignet_push_shares = mptr;
926                                 }
927                         }
928                 }
929                 else {
930                         /* Preserve 'other' lines ... *unless* they happen to
931                          * be subscribe/unsubscribe pendings with expired
932                          * timestamps.
933                          */
934                         skipthisline = 0;
935                         if (!strncasecmp(buf, "subpending|", 11)) {
936                                 if (time(NULL) - extract_long(buf, 4) > EXP) {
937                                         skipthisline = 1;
938                                 }
939                         }
940                         if (!strncasecmp(buf, "unsubpending|", 13)) {
941                                 if (time(NULL) - extract_long(buf, 3) > EXP) {
942                                         skipthisline = 1;
943                                 }
944                         }
945
946                         if (skipthisline == 0) {
947                                 linesize = strlen(buf);
948                                 sc->misc = realloc(sc->misc,
949                                         (miscsize + linesize + 2) );
950                                 sprintf(&sc->misc[miscsize], "%s\n", buf);
951                                 miscsize = miscsize + linesize + 1;
952                         }
953                 }
954
955
956         }
957         fclose(fp);
958         return 1;
959 }
960
961 void free_spoolcontrol_struct(SpoolControl **scc)
962 {
963         SpoolControl *sc;
964         namelist *nptr = NULL;
965         maplist *mptr = NULL;
966
967         sc = *scc;
968         while (sc->listrecps != NULL) {
969                 nptr = sc->listrecps->next;
970                 free(sc->listrecps);
971                 sc->listrecps = nptr;
972         }
973         /* Do the same for digestrecps */
974         while (sc->digestrecps != NULL) {
975                 nptr = sc->digestrecps->next;
976                 free(sc->digestrecps);
977                 sc->digestrecps = nptr;
978         }
979         /* Do the same for participates */
980         while (sc->participates != NULL) {
981                 nptr = sc->participates->next;
982                 free(sc->participates);
983                 sc->participates = nptr;
984         }
985         while (sc->ignet_push_shares != NULL) {
986                 mptr = sc->ignet_push_shares->next;
987                 free(sc->ignet_push_shares);
988                 sc->ignet_push_shares = mptr;
989         }
990         free(sc->misc);
991         free(sc);
992         *scc=NULL;
993 }
994
995 int writenfree_spoolcontrol_file(SpoolControl **scc, char *filename)
996 {
997         FILE *fp;
998         SpoolControl *sc;
999         namelist *nptr = NULL;
1000         maplist *mptr = NULL;
1001
1002         sc = *scc;
1003         fp = fopen(filename, "w");
1004         if (fp == NULL) {
1005                 lprintf(CTDL_CRIT, "ERROR: cannot open %s: %s\n",
1006                         filename, strerror(errno));
1007                 free_spoolcontrol_struct(scc);
1008         }
1009         else {
1010                 fprintf(fp, "lastsent|%ld\n", sc->lastsent);
1011
1012                 /* Write out the listrecps while freeing from memory at the
1013                  * same time.  Am I clever or what?  :)
1014                  */
1015                 while (sc->listrecps != NULL) {
1016                         fprintf(fp, "listrecp|%s\n", sc->listrecps->name);
1017                         nptr = sc->listrecps->next;
1018                         free(sc->listrecps);
1019                         sc->listrecps = nptr;
1020                 }
1021                 /* Do the same for digestrecps */
1022                 while (sc->digestrecps != NULL) {
1023                         fprintf(fp, "digestrecp|%s\n", sc->digestrecps->name);
1024                         nptr = sc->digestrecps->next;
1025                         free(sc->digestrecps);
1026                         sc->digestrecps = nptr;
1027                 }
1028                 /* Do the same for participates */
1029                 while (sc->participates != NULL) {
1030                         fprintf(fp, "participate|%s\n", sc->participates->name);
1031                         nptr = sc->participates->next;
1032                         free(sc->participates);
1033                         sc->participates = nptr;
1034                 }
1035                 while (sc->ignet_push_shares != NULL) {
1036                         /* by checking each node's validity, we automatically
1037                          * purge nodes which do not exist from room network
1038                          * configurations at this time.
1039                          */
1040                         if (is_valid_node(NULL, NULL, sc->ignet_push_shares->remote_nodename) == 0) {
1041                         }
1042                         fprintf(fp, "ignet_push_share|%s",
1043                                 sc->ignet_push_shares->remote_nodename);
1044                         if (!IsEmptyStr(sc->ignet_push_shares->remote_roomname)) {
1045                                 fprintf(fp, "|%s", sc->ignet_push_shares->remote_roomname);
1046                         }
1047                         fprintf(fp, "\n");
1048                         mptr = sc->ignet_push_shares->next;
1049                         free(sc->ignet_push_shares);
1050                         sc->ignet_push_shares = mptr;
1051                 }
1052                 if (sc->misc != NULL) {
1053                         fwrite(sc->misc, strlen(sc->misc), 1, fp);
1054                 }
1055                 free(sc->misc);
1056
1057                 fclose(fp);
1058                 free(sc);
1059                 *scc=NULL;
1060         }
1061         return 1;
1062 }
1063 int is_recipient(SpoolControl *sc, const char *Name)
1064 {
1065         namelist *nptr;
1066         size_t len;
1067
1068         len = strlen(Name);
1069         nptr = sc->listrecps;
1070         while (nptr != NULL) {
1071                 if (strncmp(Name, nptr->name, len)==0)
1072                         return 1;
1073                 nptr = nptr->next;
1074         }
1075         /* Do the same for digestrecps */
1076         nptr = sc->digestrecps;
1077         while (nptr != NULL) {
1078                 if (strncmp(Name, nptr->name, len)==0)
1079                         return 1;
1080                 nptr = nptr->next;
1081         }
1082         /* Do the same for participates */
1083         nptr = sc->participates;
1084         while (nptr != NULL) {
1085                 if (strncmp(Name, nptr->name, len)==0)
1086                         return 1;
1087                 nptr = nptr->next;
1088         }
1089         return 0;
1090 }
1091
1092
1093 /*
1094  * Batch up and send all outbound traffic from the current room
1095  */
1096 void network_spoolout_room(char *room_to_spool) {
1097         char buf[SIZ];
1098         char filename[SIZ];
1099         SpoolControl *sc;
1100         int i;
1101
1102         /*
1103          * If the room doesn't exist, don't try to perform its networking tasks.
1104          * Normally this should never happen, but once in a while maybe a room gets
1105          * queued for networking and then deleted before it can happen.
1106          */
1107         if (getroom(&CC->room, room_to_spool) != 0) {
1108                 lprintf(CTDL_CRIT, "ERROR: cannot load <%s>\n", room_to_spool);
1109                 return;
1110         }
1111
1112         assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
1113
1114         lprintf(CTDL_INFO, "Networking started for <%s>\n", CC->room.QRname);
1115         begin_critical_section(S_NETCONFIGS);
1116
1117         /* Only do net processing for rooms that have netconfigs */
1118
1119         if (!read_spoolcontrol_file(&sc, filename))
1120         {
1121                 end_critical_section(S_NETCONFIGS);
1122                 return;
1123         }
1124
1125         /* If there are digest recipients, we have to build a digest */
1126         if (sc->digestrecps != NULL) {
1127                 sc->digestfp = tmpfile();
1128                 fprintf(sc->digestfp, "Content-type: text/plain\n\n");
1129         }
1130
1131         /* Do something useful */
1132         CtdlForEachMessage(MSGS_GT, sc->lastsent, NULL, NULL, NULL,
1133                 network_spool_msg, sc);
1134
1135         /* If we wrote a digest, deliver it and then close it */
1136         snprintf(buf, sizeof buf, "room_%s@%s",
1137                 CC->room.QRname, config.c_fqdn);
1138         for (i=0; buf[i]; ++i) {
1139                 buf[i] = tolower(buf[i]);
1140                 if (isspace(buf[i])) buf[i] = '_';
1141         }
1142         if (sc->digestfp != NULL) {
1143                 fprintf(sc->digestfp,   " -----------------------------------"
1144                                         "------------------------------------"
1145                                         "-------\n"
1146                                         "You are subscribed to the '%s' "
1147                                         "list.\n"
1148                                         "To post to the list: %s\n",
1149                                         CC->room.QRname, buf
1150                 );
1151                 network_deliver_digest(sc);     /* deliver and close */
1152         }
1153
1154         /* Now rewrite the config file */
1155         writenfree_spoolcontrol_file (&sc, filename);
1156         end_critical_section(S_NETCONFIGS);
1157 }
1158
1159
1160
1161 /*
1162  * Send the *entire* contents of the current room to one specific network node,
1163  * ignoring anything we know about which messages have already undergone
1164  * network processing.  This can be used to bring a new node into sync.
1165  */
1166 int network_sync_to(char *target_node) {
1167         SpoolControl sc;
1168         int num_spooled = 0;
1169         int found_node = 0;
1170         char buf[256];
1171         char sc_type[256];
1172         char sc_node[256];
1173         char sc_room[256];
1174         char filename[256];
1175         FILE *fp;
1176
1177         /* Grab the configuration line we're looking for */
1178         assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
1179         begin_critical_section(S_NETCONFIGS);
1180         fp = fopen(filename, "r");
1181         if (fp == NULL) {
1182                 end_critical_section(S_NETCONFIGS);
1183                 return(-1);
1184         }
1185         while (fgets(buf, sizeof buf, fp) != NULL) {
1186                 buf[strlen(buf)-1] = 0;
1187                 extract_token(sc_type, buf, 0, '|', sizeof sc_type);
1188                 extract_token(sc_node, buf, 1, '|', sizeof sc_node);
1189                 extract_token(sc_room, buf, 2, '|', sizeof sc_room);
1190                 if ( (!strcasecmp(sc_type, "ignet_push_share"))
1191                    && (!strcasecmp(sc_node, target_node)) ) {
1192                         found_node = 1;
1193                         
1194                         /* Concise syntax because we don't need a full linked-list */
1195                         memset(&sc, 0, sizeof(SpoolControl));
1196                         sc.ignet_push_shares = (maplist *)
1197                                 malloc(sizeof(maplist));
1198                         sc.ignet_push_shares->next = NULL;
1199                         safestrncpy(sc.ignet_push_shares->remote_nodename,
1200                                 sc_node,
1201                                 sizeof sc.ignet_push_shares->remote_nodename);
1202                         safestrncpy(sc.ignet_push_shares->remote_roomname,
1203                                 sc_room,
1204                                 sizeof sc.ignet_push_shares->remote_roomname);
1205                 }
1206         }
1207         fclose(fp);
1208         end_critical_section(S_NETCONFIGS);
1209
1210         if (!found_node) return(-1);
1211
1212         /* Send ALL messages */
1213         num_spooled = CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL,
1214                 network_spool_msg, &sc);
1215
1216         /* Concise cleanup because we know there's only one node in the sc */
1217         free(sc.ignet_push_shares);
1218
1219         lprintf(CTDL_NOTICE, "Synchronized %d messages to <%s>\n",
1220                 num_spooled, target_node);
1221         return(num_spooled);
1222 }
1223
1224
1225 /*
1226  * Implements the NSYN command
1227  */
1228 void cmd_nsyn(char *argbuf) {
1229         int num_spooled;
1230         char target_node[256];
1231
1232         if (CtdlAccessCheck(ac_aide)) return;
1233
1234         extract_token(target_node, argbuf, 0, '|', sizeof target_node);
1235         num_spooled = network_sync_to(target_node);
1236         if (num_spooled >= 0) {
1237                 cprintf("%d Spooled %d messages.\n", CIT_OK, num_spooled);
1238         }
1239         else {
1240                 cprintf("%d No such room/node share exists.\n",
1241                         ERROR + ROOM_NOT_FOUND);
1242         }
1243 }
1244
1245
1246
1247 /*
1248  * Batch up and send all outbound traffic from the current room
1249  */
1250 void network_queue_room(struct ctdlroom *qrbuf, void *data) {
1251         struct RoomProcList *ptr;
1252
1253         ptr = (struct RoomProcList *) malloc(sizeof (struct RoomProcList));
1254         if (ptr == NULL) return;
1255
1256         safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
1257         begin_critical_section(S_RPLIST);
1258         ptr->next = rplist;
1259         rplist = ptr;
1260         end_critical_section(S_RPLIST);
1261 }
1262
1263 void destroy_network_queue_room(void)
1264 {
1265         struct RoomProcList *cur, *p;
1266         NetMap *nmcur, *nmp;
1267
1268         cur = rplist;
1269         begin_critical_section(S_RPLIST);
1270         while (cur != NULL)
1271         {
1272                 p = cur->next;
1273                 free (cur);
1274                 cur = p;                
1275         }
1276         rplist = NULL;
1277         end_critical_section(S_RPLIST);
1278
1279         nmcur = the_netmap;
1280         while (nmcur != NULL)
1281         {
1282                 nmp = nmcur->next;
1283                 free (nmcur);
1284                 nmcur = nmp;            
1285         }
1286         the_netmap = NULL;
1287         if (working_ignetcfg != NULL)
1288                 free (working_ignetcfg);
1289         working_ignetcfg = NULL;
1290 }
1291
1292
1293 /*
1294  * Learn topology from path fields
1295  */
1296 void network_learn_topology(char *node, char *path) {
1297         char nexthop[256];
1298         NetMap *nmptr;
1299
1300         strcpy(nexthop, "");
1301
1302         if (num_tokens(path, '!') < 3) return;
1303         for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
1304                 if (!strcasecmp(nmptr->nodename, node)) {
1305                         extract_token(nmptr->nexthop, path, 0, '!', sizeof nmptr->nexthop);
1306                         nmptr->lastcontact = time(NULL);
1307                         ++netmap_changed;
1308                         return;
1309                 }
1310         }
1311
1312         /* If we got here then it's not in the map, so add it. */
1313         nmptr = (NetMap *) malloc(sizeof (NetMap));
1314         strcpy(nmptr->nodename, node);
1315         nmptr->lastcontact = time(NULL);
1316         extract_token(nmptr->nexthop, path, 0, '!', sizeof nmptr->nexthop);
1317         nmptr->next = the_netmap;
1318         the_netmap = nmptr;
1319         ++netmap_changed;
1320 }
1321
1322
1323
1324
1325 /*
1326  * Bounce a message back to the sender
1327  */
1328 void network_bounce(struct CtdlMessage *msg, char *reason) {
1329         char *oldpath = NULL;
1330         char buf[SIZ];
1331         char bouncesource[SIZ];
1332         char recipient[SIZ];
1333         struct recptypes *valid = NULL;
1334         char force_room[ROOMNAMELEN];
1335         static int serialnum = 0;
1336         size_t size;
1337
1338         lprintf(CTDL_DEBUG, "entering network_bounce()\n");
1339
1340         if (msg == NULL) return;
1341
1342         snprintf(bouncesource, sizeof bouncesource, "%s@%s", BOUNCESOURCE, config.c_nodename);
1343
1344         /* 
1345          * Give it a fresh message ID
1346          */
1347         if (msg->cm_fields['I'] != NULL) {
1348                 free(msg->cm_fields['I']);
1349         }
1350         snprintf(buf, sizeof buf, "%ld.%04lx.%04x@%s",
1351                 (long)time(NULL), (long)getpid(), ++serialnum, config.c_fqdn);
1352         msg->cm_fields['I'] = strdup(buf);
1353
1354         /*
1355          * FIXME ... right now we're just sending a bounce; we really want to
1356          * include the text of the bounced message.
1357          */
1358         if (msg->cm_fields['M'] != NULL) {
1359                 free(msg->cm_fields['M']);
1360         }
1361         msg->cm_fields['M'] = strdup(reason);
1362         msg->cm_format_type = 0;
1363
1364         /*
1365          * Turn the message around
1366          */
1367         if (msg->cm_fields['R'] == NULL) {
1368                 free(msg->cm_fields['R']);
1369         }
1370
1371         if (msg->cm_fields['D'] == NULL) {
1372                 free(msg->cm_fields['D']);
1373         }
1374
1375         snprintf(recipient, sizeof recipient, "%s@%s",
1376                 msg->cm_fields['A'], msg->cm_fields['N']);
1377
1378         if (msg->cm_fields['A'] == NULL) {
1379                 free(msg->cm_fields['A']);
1380         }
1381
1382         if (msg->cm_fields['N'] == NULL) {
1383                 free(msg->cm_fields['N']);
1384         }
1385
1386         if (msg->cm_fields['U'] == NULL) {
1387                 free(msg->cm_fields['U']);
1388         }
1389
1390         msg->cm_fields['A'] = strdup(BOUNCESOURCE);
1391         msg->cm_fields['N'] = strdup(config.c_nodename);
1392         msg->cm_fields['U'] = strdup("Delivery Status Notification (Failure)");
1393
1394         /* prepend our node to the path */
1395         if (msg->cm_fields['P'] != NULL) {
1396                 oldpath = msg->cm_fields['P'];
1397                 msg->cm_fields['P'] = NULL;
1398         }
1399         else {
1400                 oldpath = strdup("unknown_user");
1401         }
1402         size = strlen(oldpath) + SIZ;
1403         msg->cm_fields['P'] = malloc(size);
1404         snprintf(msg->cm_fields['P'], size, "%s!%s", config.c_nodename, oldpath);
1405         free(oldpath);
1406
1407         /* Now submit the message */
1408         valid = validate_recipients(recipient, NULL, 0);
1409         if (valid != NULL) if (valid->num_error != 0) {
1410                 free_recipients(valid);
1411                 valid = NULL;
1412         }
1413         if ( (valid == NULL) || (!strcasecmp(recipient, bouncesource)) ) {
1414                 strcpy(force_room, config.c_aideroom);
1415         }
1416         else {
1417                 strcpy(force_room, "");
1418         }
1419         if ( (valid == NULL) && IsEmptyStr(force_room) ) {
1420                 strcpy(force_room, config.c_aideroom);
1421         }
1422         CtdlSubmitMsg(msg, valid, force_room);
1423
1424         /* Clean up */
1425         if (valid != NULL) free_recipients(valid);
1426         CtdlFreeMessage(msg);
1427         lprintf(CTDL_DEBUG, "leaving network_bounce()\n");
1428 }
1429
1430
1431
1432
1433 /*
1434  * Process a buffer containing a single message from a single file
1435  * from the inbound queue 
1436  */
1437 void network_process_buffer(char *buffer, long size) {
1438         struct CtdlMessage *msg = NULL;
1439         long pos;
1440         int field;
1441         struct recptypes *recp = NULL;
1442         char target_room[ROOMNAMELEN];
1443         struct ser_ret sermsg;
1444         char *oldpath = NULL;
1445         char filename[SIZ];
1446         FILE *fp;
1447         char nexthop[SIZ];
1448         unsigned char firstbyte;
1449         unsigned char lastbyte;
1450
1451         /* Validate just a little bit.  First byte should be FF and * last byte should be 00. */
1452         firstbyte = buffer[0];
1453         lastbyte = buffer[size-1];
1454         if ( (firstbyte != 255) || (lastbyte != 0) ) {
1455                 lprintf(CTDL_ERR, "Corrupt message ignored.  Length=%ld, firstbyte = %d, lastbyte = %d\n",
1456                         size, firstbyte, lastbyte);
1457                 return;
1458         }
1459
1460         /* Set default target room to trash */
1461         strcpy(target_room, TWITROOM);
1462
1463         /* Load the message into memory */
1464         msg = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
1465         memset(msg, 0, sizeof(struct CtdlMessage));
1466         msg->cm_magic = CTDLMESSAGE_MAGIC;
1467         msg->cm_anon_type = buffer[1];
1468         msg->cm_format_type = buffer[2];
1469
1470         for (pos = 3; pos < size; ++pos) {
1471                 field = buffer[pos];
1472                 msg->cm_fields[field] = strdup(&buffer[pos+1]);
1473                 pos = pos + strlen(&buffer[(int)pos]);
1474         }
1475
1476         /* Check for message routing */
1477         if (msg->cm_fields['D'] != NULL) {
1478                 if (strcasecmp(msg->cm_fields['D'], config.c_nodename)) {
1479
1480                         /* route the message */
1481                         strcpy(nexthop, "");
1482                         if (is_valid_node(nexthop, NULL,
1483                            msg->cm_fields['D']) == 0) {
1484
1485                                 /* prepend our node to the path */
1486                                 if (msg->cm_fields['P'] != NULL) {
1487                                         oldpath = msg->cm_fields['P'];
1488                                         msg->cm_fields['P'] = NULL;
1489                                 }
1490                                 else {
1491                                         oldpath = strdup("unknown_user");
1492                                 }
1493                                 size = strlen(oldpath) + SIZ;
1494                                 msg->cm_fields['P'] = malloc(size);
1495                                 snprintf(msg->cm_fields['P'], size, "%s!%s",
1496                                         config.c_nodename, oldpath);
1497                                 free(oldpath);
1498
1499                                 /* serialize the message */
1500                                 serialize_message(&sermsg, msg);
1501
1502                                 /* now send it */
1503                                 if (IsEmptyStr(nexthop)) {
1504                                         strcpy(nexthop, msg->cm_fields['D']);
1505                                 }
1506                                 snprintf(filename, 
1507                                                  sizeof filename,
1508                                                  "%s/%s",
1509                                                  ctdl_netout_dir,
1510                                                  nexthop);
1511                                 lprintf(CTDL_DEBUG, "Appending to %s\n", filename);
1512                                 fp = fopen(filename, "ab");
1513                                 if (fp != NULL) {
1514                                         fwrite(sermsg.ser,
1515                                                 sermsg.len, 1, fp);
1516                                         fclose(fp);
1517                                 }
1518                                 else {
1519                                         lprintf(CTDL_ERR, "%s: %s\n", filename, strerror(errno));
1520                                 }
1521                                 free(sermsg.ser);
1522                                 CtdlFreeMessage(msg);
1523                                 return;
1524                         }
1525                         
1526                         else {  /* invalid destination node name */
1527
1528                                 network_bounce(msg,
1529 "A message you sent could not be delivered due to an invalid destination node"
1530 " name.  Please check the address and try sending the message again.\n");
1531                                 msg = NULL;
1532                                 return;
1533
1534                         }
1535                 }
1536         }
1537
1538         /*
1539          * Check to see if we already have a copy of this message, and
1540          * abort its processing if so.  (We used to post a warning to Aide>
1541          * every time this happened, but the network is now so densely
1542          * connected that it's inevitable.)
1543          */
1544         if (network_usetable(msg) != 0) {
1545                 CtdlFreeMessage(msg);
1546                 return;
1547         }
1548
1549         /* Learn network topology from the path */
1550         if ((msg->cm_fields['N'] != NULL) && (msg->cm_fields['P'] != NULL)) {
1551                 network_learn_topology(msg->cm_fields['N'], 
1552                                         msg->cm_fields['P']);
1553         }
1554
1555         /* Is the sending node giving us a very persuasive suggestion about
1556          * which room this message should be saved in?  If so, go with that.
1557          */
1558         if (msg->cm_fields['C'] != NULL) {
1559                 safestrncpy(target_room,
1560                         msg->cm_fields['C'],
1561                         sizeof target_room);
1562         }
1563
1564         /* Otherwise, does it have a recipient?  If so, validate it... */
1565         else if (msg->cm_fields['R'] != NULL) {
1566                 recp = validate_recipients(msg->cm_fields['R'], NULL, 0);
1567                 if (recp != NULL) if (recp->num_error != 0) {
1568                         network_bounce(msg,
1569                                 "A message you sent could not be delivered due to an invalid address.\n"
1570                                 "Please check the address and try sending the message again.\n");
1571                         msg = NULL;
1572                         free_recipients(recp);
1573                         return;
1574                 }
1575                 strcpy(target_room, "");        /* no target room if mail */
1576         }
1577
1578         /* Our last shot at finding a home for this message is to see if
1579          * it has the O field (Originating room) set.
1580          */
1581         else if (msg->cm_fields['O'] != NULL) {
1582                 safestrncpy(target_room,
1583                         msg->cm_fields['O'],
1584                         sizeof target_room);
1585         }
1586
1587         /* Strip out fields that are only relevant during transit */
1588         if (msg->cm_fields['D'] != NULL) {
1589                 free(msg->cm_fields['D']);
1590                 msg->cm_fields['D'] = NULL;
1591         }
1592         if (msg->cm_fields['C'] != NULL) {
1593                 free(msg->cm_fields['C']);
1594                 msg->cm_fields['C'] = NULL;
1595         }
1596
1597         /* save the message into a room */
1598         if (PerformNetprocHooks(msg, target_room) == 0) {
1599                 msg->cm_flags = CM_SKIP_HOOKS;
1600                 CtdlSubmitMsg(msg, recp, target_room);
1601         }
1602         CtdlFreeMessage(msg);
1603         free_recipients(recp);
1604 }
1605
1606
1607 /*
1608  * Process a single message from a single file from the inbound queue 
1609  */
1610 void network_process_message(FILE *fp, long msgstart, long msgend) {
1611         long hold_pos;
1612         long size;
1613         char *buffer;
1614
1615         hold_pos = ftell(fp);
1616         size = msgend - msgstart + 1;
1617         buffer = malloc(size);
1618         if (buffer != NULL) {
1619                 fseek(fp, msgstart, SEEK_SET);
1620                 fread(buffer, size, 1, fp);
1621                 network_process_buffer(buffer, size);
1622                 free(buffer);
1623         }
1624
1625         fseek(fp, hold_pos, SEEK_SET);
1626 }
1627
1628
1629 /*
1630  * Process a single file from the inbound queue 
1631  */
1632 void network_process_file(char *filename) {
1633         FILE *fp;
1634         long msgstart = (-1L);
1635         long msgend = (-1L);
1636         long msgcur = 0L;
1637         int ch;
1638
1639
1640         fp = fopen(filename, "rb");
1641         if (fp == NULL) {
1642                 lprintf(CTDL_CRIT, "Error opening %s: %s\n", filename, strerror(errno));
1643                 return;
1644         }
1645
1646         fseek(fp, 0L, SEEK_END);
1647         lprintf(CTDL_INFO, "network: processing %ld bytes from %s\n", ftell(fp), filename);
1648         rewind(fp);
1649
1650         /* Look for messages in the data stream and break them out */
1651         while (ch = getc(fp), ch >= 0) {
1652         
1653                 if (ch == 255) {
1654                         if (msgstart >= 0L) {
1655                                 msgend = msgcur - 1;
1656                                 network_process_message(fp, msgstart, msgend);
1657                         }
1658                         msgstart = msgcur;
1659                 }
1660
1661                 ++msgcur;
1662         }
1663
1664         msgend = msgcur - 1;
1665         if (msgstart >= 0L) {
1666                 network_process_message(fp, msgstart, msgend);
1667         }
1668
1669         fclose(fp);
1670         unlink(filename);
1671 }
1672
1673
1674 /*
1675  * Process anything in the inbound queue
1676  */
1677 void network_do_spoolin(void) {
1678         DIR *dp;
1679         struct dirent *d;
1680         struct stat statbuf;
1681         char filename[256];
1682         static time_t last_spoolin_mtime = 0L;
1683
1684         /*
1685          * Check the spoolin directory's modification time.  If it hasn't
1686          * been touched, we don't need to scan it.
1687          */
1688         if (stat(ctdl_netin_dir, &statbuf)) return;
1689         if (statbuf.st_mtime == last_spoolin_mtime) {
1690                 lprintf(CTDL_DEBUG, "network: nothing in inbound queue\n");
1691                 return;
1692         }
1693         last_spoolin_mtime = statbuf.st_mtime;
1694         lprintf(CTDL_DEBUG, "network: processing inbound queue\n");
1695
1696         /*
1697          * Ok, there's something interesting in there, so scan it.
1698          */
1699         dp = opendir(ctdl_netin_dir);
1700         if (dp == NULL) return;
1701
1702         while (d = readdir(dp), d != NULL) {
1703                 if ((strcmp(d->d_name, ".")) && (strcmp(d->d_name, ".."))) {
1704                         snprintf(filename, 
1705                                          sizeof filename,
1706                                          "%s/%s",
1707                                          ctdl_netin_dir,
1708                                          d->d_name);
1709                         network_process_file(filename);
1710                 }
1711         }
1712
1713         closedir(dp);
1714 }
1715
1716 /*
1717  * Delete any files in the outbound queue that were intended
1718  * to be sent to nodes which no longer exist.
1719  */
1720 void network_purge_spoolout(void) {
1721         DIR *dp;
1722         struct dirent *d;
1723         char filename[256];
1724         char nexthop[256];
1725         int i;
1726
1727         dp = opendir(ctdl_netout_dir);
1728         if (dp == NULL) return;
1729
1730         while (d = readdir(dp), d != NULL) {
1731                 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
1732                         continue;
1733                 snprintf(filename, 
1734                                  sizeof filename,
1735                                  "%s/%s",
1736                                  ctdl_netout_dir,
1737                                  d->d_name);
1738
1739                 strcpy(nexthop, "");
1740                 i = is_valid_node(nexthop, NULL, d->d_name);
1741         
1742                 if ( (i != 0) || !IsEmptyStr(nexthop) ) {
1743                         unlink(filename);
1744                 }
1745         }
1746
1747
1748         closedir(dp);
1749 }
1750
1751
1752 /*
1753  * receive network spool from the remote system
1754  */
1755 void receive_spool(int sock, char *remote_nodename) {
1756         long download_len = 0L;
1757         long bytes_received = 0L;
1758         long bytes_copied = 0L;
1759         char buf[SIZ];
1760         static char pbuf[IGNET_PACKET_SIZE];
1761         char tempfilename[PATH_MAX];
1762         char filename[PATH_MAX];
1763         long plen;
1764         FILE *fp, *newfp;
1765
1766         CtdlMakeTempFileName(tempfilename, sizeof tempfilename);
1767         if (sock_puts(sock, "NDOP") < 0) return;
1768         if (sock_getln(sock, buf, sizeof buf) < 0) return;
1769         lprintf(CTDL_DEBUG, "<%s\n", buf);
1770         if (buf[0] != '2') {
1771                 return;
1772         }
1773         download_len = extract_long(&buf[4], 0);
1774
1775         bytes_received = 0L;
1776         fp = fopen(tempfilename, "w");
1777         if (fp == NULL) {
1778                 lprintf(CTDL_CRIT, "cannot open download file locally: %s\n",
1779                         strerror(errno));
1780                 return;
1781         }
1782
1783         while (bytes_received < download_len) {
1784                 snprintf(buf, sizeof buf, "READ %ld|%ld",
1785                         bytes_received,
1786                      ((download_len - bytes_received > IGNET_PACKET_SIZE)
1787                  ? IGNET_PACKET_SIZE : (download_len - bytes_received)));
1788                 if (sock_puts(sock, buf) < 0) {
1789                         fclose(fp);
1790                         unlink(tempfilename);
1791                         return;
1792                 }
1793                 if (sock_getln(sock, buf, sizeof buf) < 0) {
1794                         fclose(fp);
1795                         unlink(tempfilename);
1796                         return;
1797                 }
1798                 if (buf[0] == '6') {
1799                         plen = extract_long(&buf[4], 0);
1800                         if (sock_read(sock, pbuf, plen, 1) < 0) {
1801                                 fclose(fp);
1802                                 unlink(tempfilename);
1803                                 return;
1804                         }
1805                         fwrite((char *) pbuf, plen, 1, fp);
1806                         bytes_received = bytes_received + plen;
1807                 }
1808         }
1809
1810         fclose(fp);
1811         if (sock_puts(sock, "CLOS") < 0) {
1812                 unlink(tempfilename);
1813                 return;
1814         }
1815         if (sock_getln(sock, buf, sizeof buf) < 0) {
1816                 unlink(tempfilename);
1817                 return;
1818         }
1819         if (download_len > 0) {
1820                 lprintf(CTDL_NOTICE, "Received %ld octets from <%s>\n", download_len, remote_nodename);
1821         }
1822         lprintf(CTDL_DEBUG, "%s\n", buf);
1823         
1824         /* Now copy the temp file to its permanent location.
1825          * (We copy instead of link because they may be on different filesystems)
1826          */
1827         begin_critical_section(S_NETSPOOL);
1828         snprintf(filename, 
1829                          sizeof filename, 
1830                          "%s/%s.%ld",
1831                          ctdl_netin_dir,
1832                          remote_nodename, 
1833                          (long) getpid()
1834         );
1835         fp = fopen(tempfilename, "r");
1836         if (fp != NULL) {
1837                 newfp = fopen(filename, "w");
1838                 if (newfp != NULL) {
1839                         bytes_copied = 0L;
1840                         while (bytes_copied < download_len) {
1841                                 plen = download_len - bytes_copied;
1842                                 if (plen > sizeof buf) {
1843                                         plen = sizeof buf;
1844                                 }
1845                                 fread(buf, plen, 1, fp);
1846                                 fwrite(buf, plen, 1, newfp);
1847                                 bytes_copied += plen;
1848                         }
1849                         fclose(newfp);
1850                 }
1851                 fclose(fp);
1852         }
1853         end_critical_section(S_NETSPOOL);
1854         unlink(tempfilename);
1855 }
1856
1857
1858
1859 /*
1860  * transmit network spool to the remote system
1861  */
1862 void transmit_spool(int sock, char *remote_nodename)
1863 {
1864         char buf[SIZ];
1865         char pbuf[4096];
1866         long plen;
1867         long bytes_to_write, thisblock, bytes_written;
1868         int fd;
1869         char sfname[128];
1870
1871         if (sock_puts(sock, "NUOP") < 0) return;
1872         if (sock_getln(sock, buf, sizeof buf) < 0) return;
1873         lprintf(CTDL_DEBUG, "<%s\n", buf);
1874         if (buf[0] != '2') {
1875                 return;
1876         }
1877
1878         snprintf(sfname, sizeof sfname, 
1879                          "%s/%s",
1880                          ctdl_netout_dir,
1881                          remote_nodename);
1882         fd = open(sfname, O_RDONLY);
1883         if (fd < 0) {
1884                 if (errno != ENOENT) {
1885                         lprintf(CTDL_CRIT, "cannot open upload file locally: %s\n",
1886                                 strerror(errno));
1887                 }
1888                 return;
1889         }
1890         bytes_written = 0;
1891         while (plen = (long) read(fd, pbuf, IGNET_PACKET_SIZE), plen > 0L) {
1892                 bytes_to_write = plen;
1893                 while (bytes_to_write > 0L) {
1894                         snprintf(buf, sizeof buf, "WRIT %ld", bytes_to_write);
1895                         if (sock_puts(sock, buf) < 0) {
1896                                 close(fd);
1897                                 return;
1898                         }
1899                         if (sock_getln(sock, buf, sizeof buf) < 0) {
1900                                 close(fd);
1901                                 return;
1902                         }
1903                         thisblock = atol(&buf[4]);
1904                         if (buf[0] == '7') {
1905                                 if (sock_write(sock, pbuf,
1906                                    (int) thisblock) < 0) {
1907                                         close(fd);
1908                                         return;
1909                                 }
1910                                 bytes_to_write -= thisblock;
1911                                 bytes_written += thisblock;
1912                         } else {
1913                                 goto ABORTUPL;
1914                         }
1915                 }
1916         }
1917
1918 ABORTUPL:
1919         close(fd);
1920         if (sock_puts(sock, "UCLS 1") < 0) return;
1921         if (sock_getln(sock, buf, sizeof buf) < 0) return;
1922         lprintf(CTDL_NOTICE, "Sent %ld octets to <%s>\n",
1923                         bytes_written, remote_nodename);
1924         lprintf(CTDL_DEBUG, "<%s\n", buf);
1925         if (buf[0] == '2') {
1926                 lprintf(CTDL_DEBUG, "Removing <%s>\n", sfname);
1927                 unlink(sfname);
1928         }
1929 }
1930
1931
1932
1933 /*
1934  * Poll one Citadel node (called by network_poll_other_citadel_nodes() below)
1935  */
1936 void network_poll_node(char *node, char *secret, char *host, char *port) {
1937         int sock;
1938         char buf[SIZ];
1939
1940         if (network_talking_to(node, NTT_CHECK)) return;
1941         network_talking_to(node, NTT_ADD);
1942         lprintf(CTDL_NOTICE, "Connecting to <%s> at %s:%s\n", node, host, port);
1943
1944         sock = sock_connect(host, port, "tcp");
1945         if (sock < 0) {
1946                 lprintf(CTDL_ERR, "Could not connect: %s\n", strerror(errno));
1947                 network_talking_to(node, NTT_REMOVE);
1948                 return;
1949         }
1950         
1951         lprintf(CTDL_DEBUG, "Connected!\n");
1952
1953         /* Read the server greeting */
1954         if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
1955         lprintf(CTDL_DEBUG, ">%s\n", buf);
1956
1957         /* Identify ourselves */
1958         snprintf(buf, sizeof buf, "NETP %s|%s", config.c_nodename, secret);
1959         lprintf(CTDL_DEBUG, "<%s\n", buf);
1960         if (sock_puts(sock, buf) <0) goto bail;
1961         if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
1962         lprintf(CTDL_DEBUG, ">%s\n", buf);
1963         if (buf[0] != '2') goto bail;
1964
1965         /* At this point we are authenticated. */
1966         receive_spool(sock, node);
1967         transmit_spool(sock, node);
1968
1969         sock_puts(sock, "QUIT");
1970 bail:   sock_close(sock);
1971         network_talking_to(node, NTT_REMOVE);
1972 }
1973
1974
1975
1976 /*
1977  * Poll other Citadel nodes and transfer inbound/outbound network data.
1978  * Set "full" to nonzero to force a poll of every node, or to zero to poll
1979  * only nodes to which we have data to send.
1980  */
1981 void network_poll_other_citadel_nodes(int full_poll) {
1982         int i;
1983         char linebuf[256];
1984         char node[SIZ];
1985         char host[256];
1986         char port[256];
1987         char secret[256];
1988         int poll = 0;
1989         char spoolfile[256];
1990
1991         if (working_ignetcfg == NULL) {
1992                 lprintf(CTDL_DEBUG, "No nodes defined - not polling\n");
1993                 return;
1994         }
1995
1996         /* Use the string tokenizer to grab one line at a time */
1997         for (i=0; i<num_tokens(working_ignetcfg, '\n'); ++i) {
1998                 extract_token(linebuf, working_ignetcfg, i, '\n', sizeof linebuf);
1999                 extract_token(node, linebuf, 0, '|', sizeof node);
2000                 extract_token(secret, linebuf, 1, '|', sizeof secret);
2001                 extract_token(host, linebuf, 2, '|', sizeof host);
2002                 extract_token(port, linebuf, 3, '|', sizeof port);
2003                 if ( !IsEmptyStr(node) && !IsEmptyStr(secret) 
2004                    && !IsEmptyStr(host) && !IsEmptyStr(port)) {
2005                         poll = full_poll;
2006                         if (poll == 0) {
2007                                 snprintf(spoolfile, 
2008                                                  sizeof spoolfile,
2009                                                  "%s/%s",
2010                                                  ctdl_netout_dir, 
2011                                                  node);
2012                                 if (access(spoolfile, R_OK) == 0) {
2013                                         poll = 1;
2014                                 }
2015                         }
2016                         if (poll) {
2017                                 network_poll_node(node, secret, host, port);
2018                         }
2019                 }
2020         }
2021
2022 }
2023
2024
2025
2026
2027 /*
2028  * It's ok if these directories already exist.  Just fail silently.
2029  */
2030 void create_spool_dirs(void) {
2031         mkdir(ctdl_spool_dir, 0700);
2032         chown(ctdl_spool_dir, CTDLUID, (-1));
2033         mkdir(ctdl_netin_dir, 0700);
2034         chown(ctdl_netin_dir, CTDLUID, (-1));
2035         mkdir(ctdl_netout_dir, 0700);
2036         chown(ctdl_netout_dir, CTDLUID, (-1));
2037 }
2038
2039
2040
2041
2042
2043 /*
2044  * network_do_queue()
2045  * 
2046  * Run through the rooms doing various types of network stuff.
2047  */
2048 void *network_do_queue(void *args) {
2049         static time_t last_run = 0L;
2050         struct RoomProcList *ptr;
2051         int full_processing = 1;
2052
2053         /*
2054          * Run the full set of processing tasks no more frequently
2055          * than once every n seconds
2056          */
2057         if ( (time(NULL) - last_run) < config.c_net_freq ) {
2058                 full_processing = 0;
2059                 CtdlLogPrintf(CTDL_DEBUG, "Network full processing in %ld seconds.\n", config.c_net_freq - (time(NULL)- last_run));
2060         }
2061
2062         /*
2063          * This is a simple concurrency check to make sure only one queue run
2064          * is done at a time.  We could do this with a mutex, but since we
2065          * don't really require extremely fine granularity here, we'll do it
2066          * with a static variable instead.
2067          */
2068         if (doing_queue) return NULL;
2069         doing_queue = 1;
2070
2071         /* Load the IGnet Configuration into memory */
2072         load_working_ignetcfg();
2073
2074         /*
2075          * Poll other Citadel nodes.  Maybe.  If "full_processing" is set
2076          * then we poll everyone.  Otherwise we only poll nodes we have stuff
2077          * to send to.
2078          */
2079         network_poll_other_citadel_nodes(full_processing);
2080
2081         /*
2082          * Load the network map and filter list into memory.
2083          */
2084         read_network_map();
2085         filterlist = load_filter_list();
2086
2087         /* 
2088          * Go ahead and run the queue
2089          */
2090         if (full_processing) {
2091                 lprintf(CTDL_DEBUG, "network: loading outbound queue\n");
2092                 ForEachRoom(network_queue_room, NULL);
2093         }
2094
2095         if (rplist != NULL) {
2096                 lprintf(CTDL_DEBUG, "network: running outbound queue\n");
2097                 while (rplist != NULL) {
2098                         char spoolroomname[ROOMNAMELEN];
2099                         safestrncpy(spoolroomname, rplist->name, sizeof spoolroomname);
2100                         begin_critical_section(S_RPLIST);
2101
2102                         /* pop this record off the list */
2103                         ptr = rplist;
2104                         rplist = rplist->next;
2105                         free(ptr);
2106
2107                         /* invalidate any duplicate entries to prevent double processing */
2108                         for (ptr=rplist; ptr!=NULL; ptr=ptr->next) {
2109                                 if (!strcasecmp(ptr->name, spoolroomname)) {
2110                                         ptr->name[0] = 0;
2111                                 }
2112                         }
2113
2114                         end_critical_section(S_RPLIST);
2115                         if (spoolroomname[0] != 0) {
2116                                 network_spoolout_room(spoolroomname);
2117                         }
2118                 }
2119         }
2120
2121         /* If there is anything in the inbound queue, process it */
2122         network_do_spoolin();
2123
2124         /* Save the network map back to disk */
2125         write_network_map();
2126
2127         /* Free the filter list in memory */
2128         free_filter_list(filterlist);
2129         filterlist = NULL;
2130
2131         network_purge_spoolout();
2132
2133         lprintf(CTDL_DEBUG, "network: queue run completed\n");
2134
2135         if (full_processing) {
2136                 last_run = time(NULL);
2137         }
2138
2139         doing_queue = 0;
2140         CtdlThreadSchedule("IGnet Network", CTDLTHREAD_BIGSTACK, network_do_queue, NULL, time(NULL) + 60);
2141         return NULL;
2142 }
2143
2144
2145 /*
2146  * cmd_netp() - authenticate to the server as another Citadel node polling
2147  *            for network traffic
2148  */
2149 void cmd_netp(char *cmdbuf)
2150 {
2151         char node[256];
2152         char pass[256];
2153         int v;
2154
2155         char secret[256];
2156         char nexthop[256];
2157
2158         /* Authenticate */
2159         extract_token(node, cmdbuf, 0, '|', sizeof node);
2160         extract_token(pass, cmdbuf, 1, '|', sizeof pass);
2161
2162         if (doing_queue) {
2163                 lprintf(CTDL_WARNING, "Network node <%s> refused - spooling", node);
2164                 cprintf("%d spooling - try again in a few minutes\n",
2165                         ERROR + RESOURCE_BUSY);
2166                 return;
2167         }
2168
2169         /* load the IGnet Configuration to check node validity */
2170         load_working_ignetcfg();
2171         v = is_valid_node(nexthop, secret, node);
2172
2173         if (v != 0) {
2174                 lprintf(CTDL_WARNING, "Unknown node <%s>\n", node);
2175                 cprintf("%d authentication failed\n",
2176                         ERROR + PASSWORD_REQUIRED);
2177                 return;
2178         }
2179
2180         if (strcasecmp(pass, secret)) {
2181                 lprintf(CTDL_WARNING, "Bad password for network node <%s>", node);
2182                 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
2183                 return;
2184         }
2185
2186         if (network_talking_to(node, NTT_CHECK)) {
2187                 lprintf(CTDL_WARNING, "Duplicate session for network node <%s>", node);
2188                 cprintf("%d Already talking to %s right now\n", ERROR + RESOURCE_BUSY, node);
2189                 return;
2190         }
2191
2192         safestrncpy(CC->net_node, node, sizeof CC->net_node);
2193         network_talking_to(node, NTT_ADD);
2194         lprintf(CTDL_NOTICE, "Network node <%s> logged in\n", CC->net_node);
2195         cprintf("%d authenticated as network node '%s'\n", CIT_OK,
2196                 CC->net_node);
2197 }
2198
2199 int network_room_handler (struct ctdlroom *room)
2200 {
2201         network_queue_room(room, NULL);
2202         return 0;
2203 }
2204
2205 /*
2206  * Module entry point
2207  */
2208 CTDL_MODULE_INIT(network)
2209 {
2210         if (!threading)
2211         {
2212                 create_spool_dirs();
2213                 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
2214                 CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");
2215                 CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");
2216                 CtdlRegisterProtoHook(cmd_nsyn, "NSYN", "Synchronize room to node");
2217 //              CtdlRegisterSessionHook(network_do_queue, EVT_TIMER);
2218                 CtdlRegisterRoomHook(network_room_handler);
2219                 CtdlRegisterCleanupHook(destroy_network_queue_room);
2220         }
2221         else
2222                 CtdlThreadSchedule("IGnet Network", CTDLTHREAD_BIGSTACK, network_do_queue, NULL, 0);
2223         /* return our Subversion id for the Log */
2224         return "$Id$";
2225 }