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