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