]> code.citadel.org Git - citadel.git/blob - citadel/serv_network.c
* Bugfix for above
[citadel.git] / citadel / 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-2002 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  * FIXME
21  * Don't allow polls during network processing
22  */
23
24 #include "sysdep.h"
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <fcntl.h>
29 #include <ctype.h>
30 #include <signal.h>
31 #include <pwd.h>
32 #include <errno.h>
33 #include <sys/types.h>
34 #include <dirent.h>
35 #if TIME_WITH_SYS_TIME
36 # include <sys/time.h>
37 # include <time.h>
38 #else
39 # if HAVE_SYS_TIME_H
40 #  include <sys/time.h>
41 # else
42 #  include <time.h>
43 # endif
44 #endif
45
46 #include <sys/wait.h>
47 #include <string.h>
48 #include <limits.h>
49 #include "citadel.h"
50 #include "server.h"
51 #include "sysdep_decls.h"
52 #include "citserver.h"
53 #include "support.h"
54 #include "config.h"
55 #include "dynloader.h"
56 #include "room_ops.h"
57 #include "user_ops.h"
58 #include "policy.h"
59 #include "database.h"
60 #include "msgbase.h"
61 #include "tools.h"
62 #include "internet_addressing.h"
63 #include "serv_network.h"
64 #include "clientsocket.h"
65 #include "file_ops.h"
66
67 #ifndef HAVE_SNPRINTF
68 #include "snprintf.h"
69 #endif
70
71
72 /*
73  * When we do network processing, it's accomplished in two passes; one to
74  * gather a list of rooms and one to actually do them.  It's ok that rplist
75  * is global; this process *only* runs as part of the housekeeping loop and
76  * therefore only one will run at a time.
77  */
78 struct RoomProcList *rplist = NULL;
79
80 /*
81  * We build a map of network nodes during processing.
82  */
83 struct NetMap *the_netmap = NULL;
84
85 /*
86  * Keep track of what messages to reject
87  */
88 struct FilterList *load_filter_list(void) {
89         char *serialized_list = NULL;
90         int i;
91         char buf[SIZ];
92         struct FilterList *newlist = NULL;
93         struct FilterList *nptr;
94
95         serialized_list = CtdlGetSysConfig(FILTERLIST);
96         if (serialized_list == NULL) return(NULL); /* if null, no entries */
97
98         /* Use the string tokenizer to grab one line at a time */
99         for (i=0; i<num_tokens(serialized_list, '\n'); ++i) {
100                 extract_token(buf, serialized_list, i, '\n');
101                 nptr = (struct FilterList *) mallok(sizeof(struct FilterList));
102                 extract(nptr->fl_user, buf, 0);
103                 striplt(nptr->fl_user);
104                 extract(nptr->fl_room, buf, 1);
105                 striplt(nptr->fl_room);
106                 extract(nptr->fl_node, buf, 2);
107                 striplt(nptr->fl_node);
108
109                 /* Cowardly refuse to add an any/any/any entry that would
110                  * end up filtering every single message.
111                  */
112                 if (strlen(nptr->fl_user) + strlen(nptr->fl_room)
113                    + strlen(nptr->fl_node) == 0) {
114                         phree(nptr);
115                 }
116                 else {
117                         nptr->next = newlist;
118                         newlist = nptr;
119                 }
120         }
121
122         phree(serialized_list);
123         return newlist;
124 }
125
126
127 void free_filter_list(struct FilterList *fl) {
128         if (fl == NULL) return;
129         free_filter_list(fl->next);
130         phree(fl);
131 }
132
133
134
135 /*
136  * Check the use table.  This is a list of messages which have recently
137  * arrived on the system.  It is maintained and queried to prevent the same
138  * message from being entered into the database multiple times if it happens
139  * to arrive multiple times by accident.
140  */
141 int network_usetable(struct CtdlMessage *msg) {
142
143         char msgid[SIZ];
144         struct cdbdata *cdbut;
145         struct UseTable ut;
146
147         /* Bail out if we can't generate a message ID */
148         if (msg == NULL) {
149                 return(0);
150         }
151         if (msg->cm_fields['I'] == NULL) {
152                 return(0);
153         }
154         if (strlen(msg->cm_fields['I']) == 0) {
155                 return(0);
156         }
157
158         /* Generate the message ID */
159         strcpy(msgid, msg->cm_fields['I']);
160         if (haschar(msgid, '@') == 0) {
161                 strcat(msgid, "@");
162                 if (msg->cm_fields['N'] != NULL) {
163                         strcat(msgid, msg->cm_fields['N']);
164                 }
165                 else {
166                         return(0);
167                 }
168         }
169
170         cdbut = cdb_fetch(CDB_USETABLE, msgid, strlen(msgid));
171         if (cdbut != NULL) {
172                 cdb_free(cdbut);
173                 return(1);
174         }
175
176         /* If we got to this point, it's unique: add it. */
177         strcpy(ut.ut_msgid, msgid);
178         ut.ut_timestamp = time(NULL);
179         cdb_store(CDB_USETABLE, msgid, strlen(msgid),
180                 &ut, sizeof(struct UseTable) );
181         return(0);
182 }
183
184
185 /* 
186  * Read the network map from its configuration file into memory.
187  */
188 void read_network_map(void) {
189         char *serialized_map = NULL;
190         int i;
191         char buf[SIZ];
192         struct NetMap *nmptr;
193
194         serialized_map = CtdlGetSysConfig(IGNETMAP);
195         if (serialized_map == NULL) return;     /* if null, no entries */
196
197         /* Use the string tokenizer to grab one line at a time */
198         for (i=0; i<num_tokens(serialized_map, '\n'); ++i) {
199                 extract_token(buf, serialized_map, i, '\n');
200                 nmptr = (struct NetMap *) mallok(sizeof(struct NetMap));
201                 extract(nmptr->nodename, buf, 0);
202                 nmptr->lastcontact = extract_long(buf, 1);
203                 extract(nmptr->nexthop, buf, 2);
204                 nmptr->next = the_netmap;
205                 the_netmap = nmptr;
206         }
207
208         phree(serialized_map);
209 }
210
211
212 /*
213  * Write the network map from memory back to the configuration file.
214  */
215 void write_network_map(void) {
216         char *serialized_map = NULL;
217         struct NetMap *nmptr;
218
219         serialized_map = strdoop("");
220
221         if (the_netmap != NULL) {
222                 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
223                         serialized_map = reallok(serialized_map,
224                                                 (strlen(serialized_map)+SIZ) );
225                         if (strlen(nmptr->nodename) > 0) {
226                                 snprintf(&serialized_map[strlen(serialized_map)],
227                                         SIZ,
228                                         "%s|%ld|%s\n",
229                                         nmptr->nodename,
230                                         (long)nmptr->lastcontact,
231                                         nmptr->nexthop);
232                         }
233                 }
234         }
235
236         CtdlPutSysConfig(IGNETMAP, serialized_map);
237         phree(serialized_map);
238
239         /* Now free the list */
240         while (the_netmap != NULL) {
241                 nmptr = the_netmap->next;
242                 phree(the_netmap);
243                 the_netmap = nmptr;
244         }
245 }
246
247
248
249 /* 
250  * Check the network map and determine whether the supplied node name is
251  * valid.  If it is not a neighbor node, supply the name of a neighbor node
252  * which is the next hop.  If it *is* a neighbor node, we also fill in the
253  * shared secret.
254  */
255 int is_valid_node(char *nexthop, char *secret, char *node) {
256         char *ignetcfg = NULL;
257         int i;
258         char linebuf[SIZ];
259         char buf[SIZ];
260         int retval;
261         struct NetMap *nmptr;
262
263         if (node == NULL) {
264                 return(-1);
265         }
266
267         /*
268          * First try the neighbor nodes
269          */
270         ignetcfg = CtdlGetSysConfig(IGNETCFG);
271         if (ignetcfg == NULL) {
272                 if (nexthop != NULL) {
273                         strcpy(nexthop, "");
274                 }
275                 return(-1);
276         }
277
278         retval = (-1);
279         if (nexthop != NULL) {
280                 strcpy(nexthop, "");
281         }
282
283         /* Use the string tokenizer to grab one line at a time */
284         for (i=0; i<num_tokens(ignetcfg, '\n'); ++i) {
285                 extract_token(linebuf, ignetcfg, i, '\n');
286                 extract(buf, linebuf, 0);
287                 if (!strcasecmp(buf, node)) {
288                         if (nexthop != NULL) {
289                                 strcpy(nexthop, "");
290                         }
291                         if (secret != NULL) {
292                                 extract(secret, linebuf, 1);
293                         }
294                         retval = 0;
295                 }
296         }
297
298         phree(ignetcfg);
299         if (retval == 0) {
300                 return(retval);         /* yup, it's a direct neighbor */
301         }
302
303         /*      
304          * If we get to this point we have to see if we know the next hop
305          */
306         if (the_netmap != NULL) {
307                 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
308                         if (!strcasecmp(nmptr->nodename, node)) {
309                                 if (nexthop != NULL) {
310                                         strcpy(nexthop, nmptr->nexthop);
311                                 }
312                                 return(0);
313                         }
314                 }
315         }
316
317         /*
318          * If we get to this point, the supplied node name is bogus.
319          */
320         lprintf(5, "Invalid node name <%s>\n", node);
321         return(-1);
322 }
323
324
325
326
327
328 void cmd_gnet(char *argbuf) {
329         char filename[SIZ];
330         char buf[SIZ];
331         FILE *fp;
332
333         if (CtdlAccessCheck(ac_room_aide)) return;
334         assoc_file_name(filename, sizeof filename, &CC->quickroom, "netconfigs");
335         cprintf("%d Network settings for room #%ld <%s>\n",
336                 LISTING_FOLLOWS,
337                 CC->quickroom.QRnumber, CC->quickroom.QRname);
338
339         fp = fopen(filename, "r");
340         if (fp != NULL) {
341                 while (fgets(buf, sizeof buf, fp) != NULL) {
342                         buf[strlen(buf)-1] = 0;
343                         cprintf("%s\n", buf);
344                 }
345                 fclose(fp);
346         }
347
348         cprintf("000\n");
349 }
350
351
352 void cmd_snet(char *argbuf) {
353         char tempfilename[SIZ];
354         char filename[SIZ];
355         char buf[SIZ];
356         FILE *fp;
357
358         if (CtdlAccessCheck(ac_room_aide)) return;
359         safestrncpy(tempfilename, tmpnam(NULL), sizeof tempfilename);
360         assoc_file_name(filename, sizeof filename, &CC->quickroom, "netconfigs");
361
362         fp = fopen(tempfilename, "w");
363         if (fp == NULL) {
364                 cprintf("%d Cannot open %s: %s\n",
365                         ERROR+INTERNAL_ERROR,
366                         tempfilename,
367                         strerror(errno));
368         }
369
370         cprintf("%d %s\n", SEND_LISTING, tempfilename);
371         while (client_gets(buf), strcmp(buf, "000")) {
372                 fprintf(fp, "%s\n", buf);
373         }
374         fclose(fp);
375
376         /* Now copy the temp file to its permanent location
377          * (We use /bin/mv instead of link() because they may be on
378          * different filesystems)
379          */
380         unlink(filename);
381         snprintf(buf, sizeof buf, "/bin/mv %s %s", tempfilename, filename);
382         begin_critical_section(S_NETCONFIGS);
383         system(buf);
384         end_critical_section(S_NETCONFIGS);
385 }
386
387
388 /*
389  * Spools out one message from the list.
390  */
391 void network_spool_msg(long msgnum, void *userdata) {
392         struct SpoolControl *sc;
393         int err;
394         int i;
395         char *newpath = NULL;
396         char *instr = NULL;
397         size_t instr_len = SIZ;
398         struct CtdlMessage *msg = NULL;
399         struct CtdlMessage *imsg;
400         struct namelist *nptr;
401         struct ser_ret sermsg;
402         FILE *fp;
403         char filename[SIZ];
404         char buf[SIZ];
405         int bang = 0;
406         int send = 1;
407         int delete_after_send = 0;      /* Set to 1 to delete after spooling */
408
409         sc = (struct SpoolControl *)userdata;
410
411         /*
412          * Process mailing list recipients
413          */
414         if (sc->listrecps != NULL) {
415         
416                 /* First, copy it to the spoolout room */
417                 err = CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, msgnum, 0);
418                 if (err != 0) return;
419
420                 /* 
421                  * Figure out how big a buffer we need to allocate
422                  */
423                 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
424                         instr_len = instr_len + strlen(nptr->name);
425                 }
426         
427                 /*
428                  * allocate...
429                  */
430                 lprintf(9, "Generating delivery instructions\n");
431                 instr = mallok(instr_len);
432                 if (instr == NULL) {
433                         lprintf(1, "Cannot allocate %ld bytes for instr...\n",
434                                 (long)instr_len);
435                         abort();
436                 }
437                 snprintf(instr, instr_len,
438                         "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
439                         "bounceto|postmaster@%s\n" ,
440                         SPOOLMIME, msgnum, (long)time(NULL), config.c_fqdn );
441         
442                 /* Generate delivery instructions for each recipient */
443                 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
444                         size_t tmp = strlen(instr);
445                         snprintf(&instr[tmp], instr_len - tmp,
446                                  "remote|%s|0||\n", nptr->name);
447                 }
448         
449                 /*
450                  * Generate a message from the instructions
451                  */
452                 imsg = mallok(sizeof(struct CtdlMessage));
453                 memset(imsg, 0, sizeof(struct CtdlMessage));
454                 imsg->cm_magic = CTDLMESSAGE_MAGIC;
455                 imsg->cm_anon_type = MES_NORMAL;
456                 imsg->cm_format_type = FMT_RFC822;
457                 imsg->cm_fields['A'] = strdoop("Citadel");
458                 imsg->cm_fields['M'] = instr;
459         
460                 /* Save delivery instructions in spoolout room */
461                 CtdlSubmitMsg(imsg, NULL, SMTP_SPOOLOUT_ROOM);
462                 CtdlFreeMessage(imsg);
463         }
464
465         /*
466          * Process digest recipients
467          */
468         if ((sc->digestrecps != NULL) && (sc->digestfp != NULL)) {
469                 fprintf(sc->digestfp,   " -----------------------------------"
470                                         "------------------------------------"
471                                         "-------\n");
472                 CtdlRedirectOutput(sc->digestfp, -1);
473                 CtdlOutputMsg(msgnum, MT_RFC822, HEADERS_ALL, 0, 0);
474                 CtdlRedirectOutput(NULL, -1);
475                 sc->num_msgs_spooled += 1;
476         }
477         
478         /*
479          * Process IGnet push shares
480          */
481         if (sc->ignet_push_shares != NULL) {
482         
483                 msg = CtdlFetchMessage(msgnum);
484                 if (msg != NULL) {
485                         size_t newpath_len;
486
487                         /* Prepend our node name to the Path field whenever
488                          * sending a message to another IGnet node
489                          */
490                         if (msg->cm_fields['P'] == NULL) {
491                                 msg->cm_fields['P'] = strdoop("username");
492                         }
493                         newpath_len = strlen(msg->cm_fields['P']) +
494                                  strlen(config.c_nodename) + 2;
495                         newpath = mallok(newpath_len);
496                         snprintf(newpath, newpath_len, "%s!%s",
497                                  config.c_nodename, msg->cm_fields['P']);
498                         phree(msg->cm_fields['P']);
499                         msg->cm_fields['P'] = newpath;
500
501                         /*
502                          * Force the message to appear in the correct room
503                          * on the far end by setting the C field correctly
504                          */
505                         if (msg->cm_fields['C'] != NULL) {
506                                 phree(msg->cm_fields['C']);
507                         }
508                         msg->cm_fields['C'] = strdoop(CC->quickroom.QRname);
509
510                         /*
511                          * Determine if this message is set to be deleted
512                          * after sending out on the network
513                          */
514                         if (msg->cm_fields['S'] != NULL) {
515                                 if (!strcasecmp(msg->cm_fields['S'],
516                                    "CANCEL")) {
517                                         delete_after_send = 1;
518                                 }
519                         }
520
521                         /* 
522                          * Now serialize it for transmission
523                          */
524                         serialize_message(&sermsg, msg);
525
526                         /* Now send it to every node */
527                         for (nptr = sc->ignet_push_shares; nptr != NULL;
528                             nptr = nptr->next) {
529
530                                 send = 1;
531
532                                 /* Check for valid node name */
533                                 if (is_valid_node(NULL,NULL,nptr->name) != 0) {
534                                         lprintf(3, "Invalid node <%s>\n",
535                                                 nptr->name);
536                                         send = 0;
537                                 }
538
539                                 /* Check for split horizon */
540                                 lprintf(9, "Path is %s\n", msg->cm_fields['P']);
541                                 bang = num_tokens(msg->cm_fields['P'], '!');
542                                 if (bang > 1) for (i=0; i<(bang-1); ++i) {
543                                         extract_token(buf, msg->cm_fields['P'],
544                                                 i, '!');
545                                         if (!strcasecmp(buf, nptr->name)) {
546                                                 send = 0;
547                                         }
548                                 }
549
550                                 /* Send the message */
551                                 if (send == 1) {
552                                         snprintf(filename, sizeof filename,
553                                                 "./network/spoolout/%s",
554                                                 nptr->name);
555                                         fp = fopen(filename, "ab");
556                                         if (fp != NULL) {
557                                                 fwrite(sermsg.ser,
558                                                         sermsg.len, 1, fp);
559                                                 fclose(fp);
560                                         }
561                                 }
562                         }
563                         phree(sermsg.ser);
564                         CtdlFreeMessage(msg);
565                 }
566         }
567
568         /* update lastsent */
569         sc->lastsent = msgnum;
570
571         /* Delete this message if delete-after-send is set */
572         if (delete_after_send) {
573                 CtdlDeleteMessages(CC->quickroom.QRname, msgnum, "");
574         }
575
576 }
577         
578
579 /*
580  * Deliver digest messages
581  */
582 void network_deliver_digest(struct SpoolControl *sc) {
583         char buf[SIZ];
584         int i;
585         struct CtdlMessage *msg;
586         long msglen;
587         long msgnum;
588         char *instr = NULL;
589         size_t instr_len = SIZ;
590         struct CtdlMessage *imsg;
591         struct namelist *nptr;
592
593         if (sc->num_msgs_spooled < 1) {
594                 fclose(sc->digestfp);
595                 sc->digestfp = NULL;
596                 return;
597         }
598
599         msg = mallok(sizeof(struct CtdlMessage));
600         memset(msg, 0, sizeof(struct CtdlMessage));
601         msg->cm_magic = CTDLMESSAGE_MAGIC;
602         msg->cm_format_type = FMT_RFC822;
603         msg->cm_anon_type = MES_NORMAL;
604
605         sprintf(buf, "%ld", time(NULL));
606         msg->cm_fields['T'] = strdoop(buf);
607         msg->cm_fields['A'] = strdoop(CC->quickroom.QRname);
608         msg->cm_fields['U'] = strdoop(CC->quickroom.QRname);
609         sprintf(buf, "room_%s@%s", CC->quickroom.QRname, config.c_fqdn);
610         for (i=0; i<strlen(buf); ++i) {
611                 if (isspace(buf[i])) buf[i]='_';
612                 buf[i] = tolower(buf[i]);
613         }
614         msg->cm_fields['F'] = strdoop(buf);
615
616         fseek(sc->digestfp, 0L, SEEK_END);
617         msglen = ftell(sc->digestfp);
618
619         msg->cm_fields['M'] = mallok(msglen + 1);
620         fseek(sc->digestfp, 0L, SEEK_SET);
621         fread(msg->cm_fields['M'], (size_t)msglen, 1, sc->digestfp);
622         msg->cm_fields['M'][msglen] = 0;
623
624         fclose(sc->digestfp);
625         sc->digestfp = NULL;
626
627         msgnum = CtdlSubmitMsg(msg, NULL, SMTP_SPOOLOUT_ROOM);
628         CtdlFreeMessage(msg);
629
630         /* Now generate the delivery instructions */
631
632         /* 
633          * Figure out how big a buffer we need to allocate
634          */
635         for (nptr = sc->digestrecps; nptr != NULL; nptr = nptr->next) {
636                 instr_len = instr_len + strlen(nptr->name);
637         }
638         
639         /*
640          * allocate...
641          */
642         lprintf(9, "Generating delivery instructions\n");
643         instr = mallok(instr_len);
644         if (instr == NULL) {
645                 lprintf(1, "Cannot allocate %ld bytes for instr...\n",
646                         (long)instr_len);
647                 abort();
648         }
649         snprintf(instr, instr_len,
650                 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
651                 "bounceto|postmaster@%s\n" ,
652                 SPOOLMIME, msgnum, (long)time(NULL), config.c_fqdn );
653
654         /* Generate delivery instructions for each recipient */
655         for (nptr = sc->digestrecps; nptr != NULL; nptr = nptr->next) {
656                 size_t tmp = strlen(instr);
657                 snprintf(&instr[tmp], instr_len - tmp,
658                          "remote|%s|0||\n", nptr->name);
659         }
660
661         /*
662          * Generate a message from the instructions
663          */
664         imsg = mallok(sizeof(struct CtdlMessage));
665         memset(imsg, 0, sizeof(struct CtdlMessage));
666         imsg->cm_magic = CTDLMESSAGE_MAGIC;
667         imsg->cm_anon_type = MES_NORMAL;
668         imsg->cm_format_type = FMT_RFC822;
669         imsg->cm_fields['A'] = strdoop("Citadel");
670         imsg->cm_fields['M'] = instr;
671
672         /* Save delivery instructions in spoolout room */
673         CtdlSubmitMsg(imsg, NULL, SMTP_SPOOLOUT_ROOM);
674         CtdlFreeMessage(imsg);
675 }
676
677
678 /*
679  * Batch up and send all outbound traffic from the current room
680  */
681 void network_spoolout_room(char *room_to_spool) {
682         char filename[SIZ];
683         char buf[SIZ];
684         char instr[SIZ];
685         FILE *fp;
686         struct SpoolControl sc;
687         struct namelist *nptr;
688         size_t miscsize = 0;
689         size_t linesize = 0;
690
691         lprintf(7, "Spooling <%s>\n", room_to_spool);
692         if (getroom(&CC->quickroom, room_to_spool) != 0) {
693                 lprintf(1, "ERROR: cannot load <%s>\n", room_to_spool);
694                 return;
695         }
696
697         memset(&sc, 0, sizeof(struct SpoolControl));
698         assoc_file_name(filename, sizeof filename, &CC->quickroom, "netconfigs");
699
700         begin_critical_section(S_NETCONFIGS);
701         end_critical_section(S_NETCONFIGS);
702
703         fp = fopen(filename, "r");
704         if (fp == NULL) {
705                 lprintf(7, "Outbound batch processing skipped for <%s>\n",
706                         CC->quickroom.QRname);
707                 end_critical_section(S_NETCONFIGS);
708                 return;
709         }
710
711         lprintf(5, "Outbound batch processing started for <%s>\n",
712                 CC->quickroom.QRname);
713
714         while (fgets(buf, sizeof buf, fp) != NULL) {
715                 buf[strlen(buf)-1] = 0;
716
717                 extract(instr, buf, 0);
718                 if (!strcasecmp(instr, "lastsent")) {
719                         sc.lastsent = extract_long(buf, 1);
720                 }
721                 else if (!strcasecmp(instr, "listrecp")) {
722                         nptr = (struct namelist *)
723                                 mallok(sizeof(struct namelist));
724                         nptr->next = sc.listrecps;
725                         extract(nptr->name, buf, 1);
726                         sc.listrecps = nptr;
727                 }
728                 else if (!strcasecmp(instr, "digestrecp")) {
729                         nptr = (struct namelist *)
730                                 mallok(sizeof(struct namelist));
731                         nptr->next = sc.digestrecps;
732                         extract(nptr->name, buf, 1);
733                         sc.digestrecps = nptr;
734                 }
735                 else if (!strcasecmp(instr, "ignet_push_share")) {
736                         nptr = (struct namelist *)
737                                 mallok(sizeof(struct namelist));
738                         nptr->next = sc.ignet_push_shares;
739                         extract(nptr->name, buf, 1);
740                         sc.ignet_push_shares = nptr;
741                 }
742                 else {
743                         linesize = strlen(buf);
744                         sc.misc = realloc(sc.misc,
745                                 (miscsize + linesize + 2) );
746                         sprintf(&sc.misc[miscsize], "%s\n", buf);
747                         miscsize = miscsize + linesize + 1;
748                 }
749
750
751         }
752         fclose(fp);
753
754         /* If there are digest recipients, we have to build a digest */
755         if (sc.digestrecps != NULL) {
756                 sc.digestfp = tmpfile();
757                 fprintf(sc.digestfp, "Content-type: text/plain\n\n");
758         }
759
760         /* Do something useful */
761         CtdlForEachMessage(MSGS_GT, sc.lastsent, NULL, NULL,
762                 network_spool_msg, &sc);
763
764         /* If we wrote a digest, deliver it and then close it */
765         if (sc.digestfp != NULL) {
766                 fprintf(sc.digestfp,    " -----------------------------------"
767                                         "------------------------------------"
768                                         "-------\n"
769                                         "You are subscribed to the '%s' "
770                                         "list.\n",
771                                         CC->quickroom.QRname
772                 );
773                 network_deliver_digest(&sc);    /* deliver and close */
774         }
775
776         /* Now rewrite the config file */
777         fp = fopen(filename, "w");
778         if (fp == NULL) {
779                 lprintf(1, "ERROR: cannot open %s: %s\n",
780                         filename, strerror(errno));
781         }
782         else {
783                 fprintf(fp, "lastsent|%ld\n", sc.lastsent);
784
785                 /* Write out the listrecps while freeing from memory at the
786                  * same time.  Am I clever or what?  :)
787                  */
788                 while (sc.listrecps != NULL) {
789                         fprintf(fp, "listrecp|%s\n", sc.listrecps->name);
790                         nptr = sc.listrecps->next;
791                         phree(sc.listrecps);
792                         sc.listrecps = nptr;
793                 }
794                 /* Do the same for digestrecps */
795                 while (sc.digestrecps != NULL) {
796                         fprintf(fp, "digestrecp|%s\n", sc.digestrecps->name);
797                         nptr = sc.digestrecps->next;
798                         phree(sc.digestrecps);
799                         sc.digestrecps = nptr;
800                 }
801                 while (sc.ignet_push_shares != NULL) {
802                         fprintf(fp, "ignet_push_share|%s\n",
803                                 sc.ignet_push_shares->name);
804                         nptr = sc.ignet_push_shares->next;
805                         phree(sc.ignet_push_shares);
806                         sc.ignet_push_shares = nptr;
807                 }
808                 if (sc.misc != NULL) {
809                         fwrite(sc.misc, strlen(sc.misc), 1, fp);
810                 }
811                 phree(sc.misc);
812
813                 fclose(fp);
814         }
815         end_critical_section(S_NETCONFIGS);
816
817         lprintf(5, "Outbound batch processing finished for <%s>\n",
818                 CC->quickroom.QRname);
819 }
820
821
822 /*
823  * Batch up and send all outbound traffic from the current room
824  */
825 void network_queue_room(struct quickroom *qrbuf, void *data) {
826         struct RoomProcList *ptr;
827
828         ptr = (struct RoomProcList *) mallok(sizeof (struct RoomProcList));
829         if (ptr == NULL) return;
830
831         safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
832         ptr->next = rplist;
833         rplist = ptr;
834 }
835
836
837 /*
838  * Learn topology from path fields
839  */
840 void network_learn_topology(char *node, char *path) {
841         char nexthop[SIZ];
842         struct NetMap *nmptr;
843
844         strcpy(nexthop, "");
845
846         if (num_tokens(path, '!') < 3) return;
847         for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
848                 if (!strcasecmp(nmptr->nodename, node)) {
849                         extract_token(nmptr->nexthop, path, 0, '!');
850                         nmptr->lastcontact = time(NULL);
851                         return;
852                 }
853         }
854
855         /* If we got here then it's not in the map, so add it. */
856         nmptr = (struct NetMap *) mallok(sizeof (struct NetMap));
857         strcpy(nmptr->nodename, node);
858         nmptr->lastcontact = time(NULL);
859         extract_token(nmptr->nexthop, path, 0, '!');
860         nmptr->next = the_netmap;
861         the_netmap = nmptr;
862 }
863
864
865
866
867 /*
868  * Bounce a message back to the sender
869  */
870 void network_bounce(struct CtdlMessage *msg, char *reason) {
871         char *oldpath = NULL;
872         char buf[SIZ];
873         char bouncesource[SIZ];
874         char recipient[SIZ];
875         struct recptypes *valid = NULL;
876         char force_room[ROOMNAMELEN];
877         static int serialnum = 0;
878         size_t size;
879
880         lprintf(9, "entering network_bounce()\n");
881
882         if (msg == NULL) return;
883
884         snprintf(bouncesource, sizeof bouncesource, "%s@%s", BOUNCESOURCE, config.c_nodename);
885
886         /* 
887          * Give it a fresh message ID
888          */
889         if (msg->cm_fields['I'] != NULL) {
890                 phree(msg->cm_fields['I']);
891         }
892         snprintf(buf, sizeof buf, "%ld.%04lx.%04x@%s",
893                 (long)time(NULL), (long)getpid(), ++serialnum, config.c_fqdn);
894         msg->cm_fields['I'] = strdoop(buf);
895
896         /*
897          * FIXME ... right now we're just sending a bounce; we really want to
898          * include the text of the bounced message.
899          */
900         if (msg->cm_fields['M'] != NULL) {
901                 phree(msg->cm_fields['M']);
902         }
903         msg->cm_fields['M'] = strdoop(reason);
904         msg->cm_format_type = 0;
905
906         /*
907          * Turn the message around
908          */
909         if (msg->cm_fields['R'] == NULL) {
910                 phree(msg->cm_fields['R']);
911         }
912
913         if (msg->cm_fields['D'] == NULL) {
914                 phree(msg->cm_fields['D']);
915         }
916
917         snprintf(recipient, sizeof recipient, "%s@%s",
918                 msg->cm_fields['A'], msg->cm_fields['N']);
919
920         if (msg->cm_fields['A'] == NULL) {
921                 phree(msg->cm_fields['A']);
922         }
923
924         if (msg->cm_fields['N'] == NULL) {
925                 phree(msg->cm_fields['N']);
926         }
927
928         msg->cm_fields['A'] = strdoop(BOUNCESOURCE);
929         msg->cm_fields['N'] = strdoop(config.c_nodename);
930         
931
932         /* prepend our node to the path */
933         if (msg->cm_fields['P'] != NULL) {
934                 oldpath = msg->cm_fields['P'];
935                 msg->cm_fields['P'] = NULL;
936         }
937         else {
938                 oldpath = strdoop("unknown_user");
939         }
940         size = strlen(oldpath) + SIZ;
941         msg->cm_fields['P'] = mallok(size);
942         snprintf(msg->cm_fields['P'], size, "%s!%s", config.c_nodename, oldpath);
943         phree(oldpath);
944
945         /* Now submit the message */
946         valid = validate_recipients(recipient);
947         if (valid != NULL) if (valid->num_error > 0) {
948                 phree(valid);
949                 valid = NULL;
950         }
951         if ( (valid == NULL) || (!strcasecmp(recipient, bouncesource)) ) {
952                 strcpy(force_room, config.c_aideroom);
953         }
954         else {
955                 strcpy(force_room, "");
956         }
957         if ( (valid == NULL) && (strlen(force_room) == 0) ) {
958                 strcpy(force_room, config.c_aideroom);
959         }
960         CtdlSubmitMsg(msg, valid, force_room);
961
962         /* Clean up */
963         if (valid != NULL) phree(valid);
964         CtdlFreeMessage(msg);
965         lprintf(9, "leaving network_bounce()\n");
966 }
967
968
969
970
971 /*
972  * Process a buffer containing a single message from a single file
973  * from the inbound queue 
974  */
975 void network_process_buffer(char *buffer, long size) {
976         struct CtdlMessage *msg;
977         long pos;
978         int field;
979         struct recptypes *recp = NULL;
980         char target_room[ROOMNAMELEN];
981         struct ser_ret sermsg;
982         char *oldpath = NULL;
983         char filename[SIZ];
984         FILE *fp;
985         char buf[SIZ];
986
987         /* Set default target room to trash */
988         strcpy(target_room, TWITROOM);
989
990         /* Load the message into memory */
991         msg = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
992         memset(msg, 0, sizeof(struct CtdlMessage));
993         msg->cm_magic = CTDLMESSAGE_MAGIC;
994         msg->cm_anon_type = buffer[1];
995         msg->cm_format_type = buffer[2];
996
997         for (pos = 3; pos < size; ++pos) {
998                 field = buffer[pos];
999                 msg->cm_fields[field] = strdoop(&buffer[pos+1]);
1000                 pos = pos + strlen(&buffer[(int)pos]);
1001         }
1002
1003         /* Check for message routing */
1004         if (msg->cm_fields['D'] != NULL) {
1005                 if (strcasecmp(msg->cm_fields['D'], config.c_nodename)) {
1006
1007                         /* route the message */
1008                         if (is_valid_node(NULL, NULL,
1009                            msg->cm_fields['D']) == 0) {
1010
1011                                 /* prepend our node to the path */
1012                                 if (msg->cm_fields['P'] != NULL) {
1013                                         oldpath = msg->cm_fields['P'];
1014                                         msg->cm_fields['P'] = NULL;
1015                                 }
1016                                 else {
1017                                         oldpath = strdoop("unknown_user");
1018                                 }
1019                                 size = strlen(oldpath) + SIZ;
1020                                 msg->cm_fields['P'] = mallok(size);
1021                                 snprintf(msg->cm_fields['P'], size, "%s!%s",
1022                                         config.c_nodename, oldpath);
1023                                 phree(oldpath);
1024
1025                                 /* serialize the message */
1026                                 serialize_message(&sermsg, msg);
1027
1028                                 /* now send it */
1029                                 snprintf(filename, sizeof filename,
1030                                         "./network/spoolout/%s",
1031                                         msg->cm_fields['D']);
1032                                 fp = fopen(filename, "ab");
1033                                 if (fp != NULL) {
1034                                         fwrite(sermsg.ser,
1035                                                 sermsg.len, 1, fp);
1036                                         fclose(fp);
1037                                 }
1038                                 phree(sermsg.ser);
1039                                 CtdlFreeMessage(msg);
1040                                 return;
1041                         }
1042                         
1043                         else {  /* invalid destination node name */
1044
1045                                 network_bounce(msg,
1046 "A message you sent could not be delivered due to an invalid destination node"
1047 " name.  Please check the address and try sending the message again.\n");
1048                                 msg = NULL;
1049                                 return;
1050
1051                         }
1052                 }
1053         }
1054
1055         /*
1056          * Check to see if we already have a copy of this message
1057          */
1058         if (network_usetable(msg) != 0) {
1059                 snprintf(buf, sizeof buf,
1060                         "Loopzapper rejected message <%s> "
1061                         "from <%s> in <%s> @ <%s>\n",
1062                         ((msg->cm_fields['I']!=NULL)?(msg->cm_fields['I']):""),
1063                         ((msg->cm_fields['A']!=NULL)?(msg->cm_fields['A']):""),
1064                         ((msg->cm_fields['O']!=NULL)?(msg->cm_fields['O']):""),
1065                         ((msg->cm_fields['N']!=NULL)?(msg->cm_fields['N']):"")
1066                 );
1067                 aide_message(buf);
1068                 CtdlFreeMessage(msg);
1069                 msg = NULL;
1070                 return;
1071         }
1072
1073         /* Learn network topology from the path */
1074         if ((msg->cm_fields['N'] != NULL) && (msg->cm_fields['P'] != NULL)) {
1075                 network_learn_topology(msg->cm_fields['N'], 
1076                                         msg->cm_fields['P']);
1077         }
1078
1079         /* Does it have a recipient?  If so, validate it... */
1080         if (msg->cm_fields['R'] != NULL) {
1081                 recp = validate_recipients(msg->cm_fields['R']);
1082                 if (recp != NULL) if (recp->num_error > 0) {
1083                         network_bounce(msg,
1084 "A message you sent could not be delivered due to an invalid address.\n"
1085 "Please check the address and try sending the message again.\n");
1086                         msg = NULL;
1087                         phree(recp);
1088                         return;
1089                 }
1090                 strcpy(target_room, "");        /* no target room if mail */
1091         }
1092
1093         else if (msg->cm_fields['C'] != NULL) {
1094                 safestrncpy(target_room,
1095                         msg->cm_fields['C'],
1096                         sizeof target_room);
1097         }
1098
1099         else if (msg->cm_fields['O'] != NULL) {
1100                 safestrncpy(target_room,
1101                         msg->cm_fields['O'],
1102                         sizeof target_room);
1103         }
1104
1105         /* Strip out fields that are only relevant during transit */
1106         if (msg->cm_fields['D'] != NULL) {
1107                 phree(msg->cm_fields['D']);
1108                 msg->cm_fields['D'] = NULL;
1109         }
1110         if (msg->cm_fields['C'] != NULL) {
1111                 phree(msg->cm_fields['C']);
1112                 msg->cm_fields['C'] = NULL;
1113         }
1114
1115         /* save the message into a room */
1116         if (PerformNetprocHooks(msg, target_room) == 0) {
1117                 msg->cm_flags = CM_SKIP_HOOKS;
1118                 CtdlSubmitMsg(msg, recp, target_room);
1119         }
1120         CtdlFreeMessage(msg);
1121         phree(recp);
1122 }
1123
1124
1125 /*
1126  * Process a single message from a single file from the inbound queue 
1127  */
1128 void network_process_message(FILE *fp, long msgstart, long msgend) {
1129         long hold_pos;
1130         long size;
1131         char *buffer;
1132
1133         hold_pos = ftell(fp);
1134         size = msgend - msgstart + 1;
1135         buffer = mallok(size);
1136         if (buffer != NULL) {
1137                 fseek(fp, msgstart, SEEK_SET);
1138                 fread(buffer, size, 1, fp);
1139                 network_process_buffer(buffer, size);
1140                 phree(buffer);
1141         }
1142
1143         fseek(fp, hold_pos, SEEK_SET);
1144 }
1145
1146
1147 /*
1148  * Process a single file from the inbound queue 
1149  */
1150 void network_process_file(char *filename) {
1151         FILE *fp;
1152         long msgstart = (-1L);
1153         long msgend = (-1L);
1154         long msgcur = 0L;
1155         int ch;
1156
1157         lprintf(7, "network: processing <%s>\n", filename);
1158
1159         fp = fopen(filename, "rb");
1160         if (fp == NULL) {
1161                 lprintf(5, "Error opening %s: %s\n",
1162                         filename, strerror(errno));
1163                 return;
1164         }
1165
1166         /* Look for messages in the data stream and break them out */
1167         while (ch = getc(fp), ch >= 0) {
1168         
1169                 if (ch == 255) {
1170                         if (msgstart >= 0L) {
1171                                 msgend = msgcur - 1;
1172                                 network_process_message(fp, msgstart, msgend);
1173                         }
1174                         msgstart = msgcur;
1175                 }
1176
1177                 ++msgcur;
1178         }
1179
1180         msgend = msgcur - 1;
1181         if (msgstart >= 0L) {
1182                 network_process_message(fp, msgstart, msgend);
1183         }
1184
1185         fclose(fp);
1186         unlink(filename);
1187 }
1188
1189
1190 /*
1191  * Process anything in the inbound queue
1192  */
1193 void network_do_spoolin(void) {
1194         DIR *dp;
1195         struct dirent *d;
1196         char filename[SIZ];
1197
1198         dp = opendir("./network/spoolin");
1199         if (dp == NULL) return;
1200
1201         while (d = readdir(dp), d != NULL) {
1202                 snprintf(filename, sizeof filename, "./network/spoolin/%s", d->d_name);
1203                 network_process_file(filename);
1204         }
1205
1206
1207         closedir(dp);
1208 }
1209
1210
1211
1212
1213
1214 /*
1215  * receive network spool from the remote system
1216  */
1217 void receive_spool(int sock, char *remote_nodename) {
1218         long download_len;
1219         long bytes_received;
1220         char buf[SIZ];
1221         static char pbuf[IGNET_PACKET_SIZE];
1222         char tempfilename[PATH_MAX];
1223         long plen;
1224         FILE *fp;
1225
1226         strcpy(tempfilename, tmpnam(NULL));
1227         if (sock_puts(sock, "NDOP") < 0) return;
1228         if (sock_gets(sock, buf) < 0) return;
1229         lprintf(9, "<%s\n", buf);
1230         if (buf[0] != '2') {
1231                 return;
1232         }
1233         download_len = extract_long(&buf[4], 0);
1234
1235         bytes_received = 0L;
1236         fp = fopen(tempfilename, "w");
1237         if (fp == NULL) {
1238                 lprintf(9, "cannot open download file locally: %s\n",
1239                         strerror(errno));
1240                 return;
1241         }
1242
1243         while (bytes_received < download_len) {
1244                 snprintf(buf, sizeof buf, "READ %ld|%ld",
1245                         bytes_received,
1246                      ((download_len - bytes_received > IGNET_PACKET_SIZE)
1247                  ? IGNET_PACKET_SIZE : (download_len - bytes_received)));
1248                 if (sock_puts(sock, buf) < 0) {
1249                         fclose(fp);
1250                         unlink(tempfilename);
1251                         return;
1252                 }
1253                 if (sock_gets(sock, buf) < 0) {
1254                         fclose(fp);
1255                         unlink(tempfilename);
1256                         return;
1257                 }
1258                 if (buf[0] == '6') {
1259                         plen = extract_long(&buf[4], 0);
1260                         if (sock_read(sock, pbuf, plen) < 0) {
1261                                 fclose(fp);
1262                                 unlink(tempfilename);
1263                                 return;
1264                         }
1265                         fwrite((char *) pbuf, plen, 1, fp);
1266                         bytes_received = bytes_received + plen;
1267                 }
1268         }
1269
1270         fclose(fp);
1271         if (sock_puts(sock, "CLOS") < 0) {
1272                 unlink(tempfilename);
1273                 return;
1274         }
1275         if (sock_gets(sock, buf) < 0) {
1276                 unlink(tempfilename);
1277                 return;
1278         }
1279         lprintf(9, "%s\n", buf);
1280         snprintf(buf, sizeof buf, "mv %s ./network/spoolin/%s.%ld",
1281                 tempfilename, remote_nodename, (long) getpid());
1282         system(buf);
1283 }
1284
1285
1286
1287 /*
1288  * transmit network spool to the remote system
1289  */
1290 void transmit_spool(int sock, char *remote_nodename)
1291 {
1292         char buf[SIZ];
1293         char pbuf[4096];
1294         long plen;
1295         long bytes_to_write, thisblock;
1296         int fd;
1297         char sfname[128];
1298
1299         if (sock_puts(sock, "NUOP") < 0) return;
1300         if (sock_gets(sock, buf) < 0) return;
1301         lprintf(9, "<%s\n", buf);
1302         if (buf[0] != '2') {
1303                 return;
1304         }
1305
1306         snprintf(sfname, sizeof sfname, "./network/spoolout/%s", remote_nodename);
1307         fd = open(sfname, O_RDONLY);
1308         if (fd < 0) {
1309                 if (errno == ENOENT) {
1310                         lprintf(9, "Nothing to send.\n");
1311                 } else {
1312                         lprintf(5, "cannot open upload file locally: %s\n",
1313                                 strerror(errno));
1314                 }
1315                 return;
1316         }
1317         while (plen = (long) read(fd, pbuf, IGNET_PACKET_SIZE), plen > 0L) {
1318                 bytes_to_write = plen;
1319                 while (bytes_to_write > 0L) {
1320                         snprintf(buf, sizeof buf, "WRIT %ld", bytes_to_write);
1321                         if (sock_puts(sock, buf) < 0) {
1322                                 close(fd);
1323                                 return;
1324                         }
1325                         if (sock_gets(sock, buf) < 0) {
1326                                 close(fd);
1327                                 return;
1328                         }
1329                         thisblock = atol(&buf[4]);
1330                         if (buf[0] == '7') {
1331                                 if (sock_write(sock, pbuf,
1332                                    (int) thisblock) < 0) {
1333                                         close(fd);
1334                                         return;
1335                                 }
1336                                 bytes_to_write = bytes_to_write - thisblock;
1337                         } else {
1338                                 goto ABORTUPL;
1339                         }
1340                 }
1341         }
1342
1343 ABORTUPL:
1344         close(fd);
1345         if (sock_puts(sock, "UCLS 1") < 0) return;
1346         if (sock_gets(sock, buf) < 0) return;
1347         lprintf(9, "<%s\n", buf);
1348         if (buf[0] == '2') {
1349                 unlink(sfname);
1350         }
1351 }
1352
1353
1354
1355 /*
1356  * Poll one Citadel node (called by network_poll_other_citadel_nodes() below)
1357  */
1358 void network_poll_node(char *node, char *secret, char *host, char *port) {
1359         int sock;
1360         char buf[SIZ];
1361
1362         if (network_talking_to(node, NTT_CHECK)) return;
1363         network_talking_to(node, NTT_ADD);
1364         lprintf(5, "Polling node <%s> at %s:%s\n", node, host, port);
1365
1366         sock = sock_connect(host, port, "tcp");
1367         if (sock < 0) {
1368                 lprintf(7, "Could not connect: %s\n", strerror(errno));
1369                 network_talking_to(node, NTT_REMOVE);
1370                 return;
1371         }
1372         
1373         lprintf(9, "Connected!\n");
1374
1375         /* Read the server greeting */
1376         if (sock_gets(sock, buf) < 0) goto bail;
1377         lprintf(9, ">%s\n", buf);
1378
1379         /* Identify ourselves */
1380         snprintf(buf, sizeof buf, "NETP %s|%s", config.c_nodename, secret);
1381         lprintf(9, "<%s\n", buf);
1382         if (sock_puts(sock, buf) <0) goto bail;
1383         if (sock_gets(sock, buf) < 0) goto bail;
1384         lprintf(9, ">%s\n", buf);
1385         if (buf[0] != '2') goto bail;
1386
1387         /* At this point we are authenticated. */
1388         receive_spool(sock, node);
1389         transmit_spool(sock, node);
1390
1391         sock_puts(sock, "QUIT");
1392 bail:   sock_close(sock);
1393         network_talking_to(node, NTT_REMOVE);
1394 }
1395
1396
1397
1398 /*
1399  * Poll other Citadel nodes and transfer inbound/outbound network data.
1400  */
1401 void network_poll_other_citadel_nodes(void) {
1402         char *ignetcfg = NULL;
1403         int i;
1404         char linebuf[SIZ];
1405         char node[SIZ];
1406         char host[SIZ];
1407         char port[SIZ];
1408         char secret[SIZ];
1409
1410         ignetcfg = CtdlGetSysConfig(IGNETCFG);
1411         if (ignetcfg == NULL) return;   /* no nodes defined */
1412
1413         /* Use the string tokenizer to grab one line at a time */
1414         for (i=0; i<num_tokens(ignetcfg, '\n'); ++i) {
1415                 extract_token(linebuf, ignetcfg, i, '\n');
1416                 extract(node, linebuf, 0);
1417                 extract(secret, linebuf, 1);
1418                 extract(host, linebuf, 2);
1419                 extract(port, linebuf, 3);
1420                 if ( (strlen(node) > 0) && (strlen(secret) > 0) 
1421                    && (strlen(host) > 0) && strlen(port) > 0) {
1422                         network_poll_node(node, secret, host, port);
1423                 }
1424         }
1425
1426         phree(ignetcfg);
1427 }
1428
1429
1430
1431
1432
1433
1434
1435 /*
1436  * network_do_queue()
1437  * 
1438  * Run through the rooms doing various types of network stuff.
1439  */
1440 void network_do_queue(void) {
1441         static int doing_queue = 0;
1442         static time_t last_run = 0L;
1443         struct RoomProcList *ptr;
1444
1445         /*
1446          * Run no more frequently than once every n seconds
1447          */
1448         if ( (time(NULL) - last_run) < config.c_net_freq ) return;
1449
1450         /*
1451          * This is a simple concurrency check to make sure only one queue run
1452          * is done at a time.  We could do this with a mutex, but since we
1453          * don't really require extremely fine granularity here, we'll do it
1454          * with a static variable instead.
1455          */
1456         if (doing_queue) return;
1457         doing_queue = 1;
1458         last_run = time(NULL);
1459
1460         /*
1461          * Poll other Citadel nodes.
1462          */
1463         network_poll_other_citadel_nodes();
1464
1465         /*
1466          * Load the network map and filter list into memory.
1467          */
1468         read_network_map();
1469         filterlist = load_filter_list();
1470
1471         /* 
1472          * Go ahead and run the queue
1473          */
1474         lprintf(7, "network: loading outbound queue\n");
1475         ForEachRoom(network_queue_room, NULL);
1476
1477         lprintf(7, "network: running outbound queue\n");
1478         while (rplist != NULL) {
1479                 network_spoolout_room(rplist->name);
1480                 ptr = rplist;
1481                 rplist = rplist->next;
1482                 phree(ptr);
1483         }
1484
1485         lprintf(7, "network: processing inbound queue\n");
1486         network_do_spoolin();
1487
1488         /* Save the network map back to disk */
1489         write_network_map();
1490
1491         /* Free the filter list in memory */
1492         free_filter_list(filterlist);
1493         filterlist = NULL;
1494
1495         lprintf(7, "network: queue run completed\n");
1496         doing_queue = 0;
1497 }
1498
1499
1500 /*
1501  * cmd_netp() - authenticate to the server as another Citadel node polling
1502  *              for network traffic
1503  */
1504 void cmd_netp(char *cmdbuf)
1505 {
1506         char node[SIZ];
1507         char pass[SIZ];
1508
1509         char secret[SIZ];
1510         char nexthop[SIZ];
1511
1512         extract(node, cmdbuf, 0);
1513         extract(pass, cmdbuf, 1);
1514
1515         if (is_valid_node(nexthop, secret, node) != 0) {
1516                 cprintf("%d authentication failed\n", ERROR);
1517                 return;
1518         }
1519
1520         if (strcasecmp(pass, secret)) {
1521                 cprintf("%d authentication failed\n", ERROR);
1522                 return;
1523         }
1524
1525         if (network_talking_to(node, NTT_CHECK)) {
1526                 cprintf("%d Already talking to %s right now\n", ERROR, node);
1527                 return;
1528         }
1529
1530         safestrncpy(CC->net_node, node, sizeof CC->net_node);
1531         network_talking_to(node, NTT_ADD);
1532         cprintf("%d authenticated as network node '%s'\n", CIT_OK,
1533                 CC->net_node);
1534 }
1535
1536
1537
1538
1539
1540 /*
1541  * Module entry point
1542  */
1543 char *Dynamic_Module_Init(void)
1544 {
1545         CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
1546         CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");
1547         CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");
1548         CtdlRegisterSessionHook(network_do_queue, EVT_TIMER);
1549         return "$Id$";
1550 }